SSLAgent.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.InteropServices;
  4. using System.Runtime.Serialization;
  5. using System.Runtime.Serialization.Formatters.Binary;
  6. using System.Linq;
  7. using System.Text;
  8. using System.IO;
  9. using System.Threading;
  10. namespace HPSocketCS
  11. {
  12. public class SSLAgent : TcpAgent
  13. {
  14. static int ObjectReferer = 0;
  15. static string SSLInitLock = "SSL初始化锁";
  16. Sdk.OnHandShake _OnHandShake = null;
  17. /// <summary>
  18. /// 验证模式
  19. /// </summary>
  20. public SSLVerifyMode VerifyMode { get; set; }
  21. /// <summary>
  22. /// 证书文件(客户端可选)
  23. /// </summary>
  24. public string PemCertFile { get; set; }
  25. /// <summary>
  26. /// 私钥文件(客户端可选)
  27. /// </summary>
  28. public string PemKeyFile { get; set; }
  29. /// <summary>
  30. /// 私钥密码(没有密码则为空)
  31. /// </summary>
  32. public string KeyPasswod { get; set; }
  33. /// <summary>
  34. /// CA 证书文件或目录(单向验证或客户端可选)
  35. /// </summary>
  36. public string CAPemCertFileOrPath { get; set; }
  37. /// <summary>
  38. /// SSL握手成功事件
  39. /// </summary>
  40. public event SSLEvent.OnHandShakeEventHandler OnHandShake;
  41. public SSLAgent()
  42. {
  43. Interlocked.Increment(ref ObjectReferer);
  44. }
  45. /// <summary>
  46. ///
  47. /// </summary>
  48. /// <param name="_verifyModel">验证模式</param>
  49. /// <param name="_pemCertFile">证书文件</param>
  50. /// <param name="_pemKeyFile">私钥文件</param>
  51. /// <param name="_keyPasswod">私钥密码(没有密码则为空)</param>
  52. /// <param name="_caPemCertFileOrPath">CA 证书文件或目录(单向验证或客户端可选)</param>
  53. public SSLAgent(SSLVerifyMode _verifyModel, string _pemCertFile, string _pemKeyFile, string _keyPasswod, string _caPemCertFileOrPath)
  54. {
  55. Interlocked.Increment(ref ObjectReferer);
  56. this.VerifyMode = _verifyModel;
  57. this.PemCertFile = _pemCertFile;
  58. this.PemKeyFile = _pemKeyFile;
  59. this.KeyPasswod = _keyPasswod;
  60. this.CAPemCertFileOrPath = _caPemCertFileOrPath;
  61. Initialize();
  62. }
  63. ~SSLAgent()
  64. {
  65. Uninitialize();
  66. }
  67. protected override bool CreateListener()
  68. {
  69. if (IsCreate == true || pListener != IntPtr.Zero || pAgent != IntPtr.Zero)
  70. {
  71. return false;
  72. }
  73. pListener = Sdk.Create_HP_TcpAgentListener();
  74. if (pListener == IntPtr.Zero)
  75. {
  76. return false;
  77. }
  78. pAgent = SSLSdk.Create_HP_SSLAgent(pListener);
  79. if (pAgent == IntPtr.Zero)
  80. {
  81. return false;
  82. }
  83. IsCreate = true;
  84. return true;
  85. }
  86. protected override void SetCallback()
  87. {
  88. _OnHandShake = new Sdk.OnHandShake(SDK_OnHandShake);
  89. Sdk.HP_Set_FN_Server_OnHandShake(pListener, _OnHandShake);
  90. base.SetCallback();
  91. }
  92. /// <summary>
  93. /// 初始化SSL环境
  94. /// </summary>
  95. /// <returns></returns>
  96. protected virtual bool Initialize()
  97. {
  98. lock (SSLInitLock)
  99. {
  100. if (SSLSdk.HP_SSL_IsValid() == false)
  101. {
  102. if (string.IsNullOrWhiteSpace(PemCertFile))
  103. {
  104. throw new NullReferenceException("PemCertFile");
  105. }
  106. if (string.IsNullOrWhiteSpace(PemKeyFile))
  107. {
  108. throw new NullReferenceException("PemKeyFile");
  109. }
  110. if (string.IsNullOrWhiteSpace(KeyPasswod))
  111. {
  112. throw new NullReferenceException("KeyPasswod");
  113. }
  114. if (string.IsNullOrWhiteSpace(CAPemCertFileOrPath))
  115. {
  116. throw new NullReferenceException("CAPemCertFileOrPath");
  117. }
  118. return SSLSdk.HP_SSL_Initialize(SSLSessionMode.Client, VerifyMode, PemCertFile, PemKeyFile, KeyPasswod, CAPemCertFileOrPath);
  119. }
  120. return true;
  121. }
  122. }
  123. /// <summary>
  124. /// 反初始化SSL环境
  125. /// </summary>
  126. protected virtual void Uninitialize()
  127. {
  128. if (Interlocked.Decrement(ref ObjectReferer) == 0)
  129. {
  130. SSLSdk.HP_SSL_Cleanup();
  131. }
  132. }
  133. /// <summary>
  134. /// 启动通讯组件
  135. /// 启动完成后可开始连接远程服务器
  136. /// </summary>
  137. /// <param name="address">绑定地址</param>
  138. /// <param name="async">是否异步</param>
  139. /// <returns></returns>
  140. public new bool Start(string address, bool async = false)
  141. {
  142. bool ret = false;
  143. if (Initialize())
  144. {
  145. ret = base.Start(address, async);
  146. }
  147. return ret;
  148. }
  149. public override void Destroy()
  150. {
  151. Stop();
  152. if (pAgent != IntPtr.Zero)
  153. {
  154. SSLSdk.Destroy_HP_SSLAgent(pAgent);
  155. pAgent = IntPtr.Zero;
  156. }
  157. if (pListener != IntPtr.Zero)
  158. {
  159. Sdk.Destroy_HP_TcpAgentListener(pListener);
  160. pListener = IntPtr.Zero;
  161. }
  162. IsCreate = false;
  163. }
  164. protected HandleResult SDK_OnHandShake(IntPtr connId)
  165. {
  166. if (OnHandShake != null)
  167. {
  168. return OnHandShake(connId);
  169. }
  170. return HandleResult.Ignore;
  171. }
  172. }
  173. }