SSLClient.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. namespace HPSocketCS
  7. {
  8. public class SSLClientEvent
  9. {
  10. public delegate HandleResult OnHandShakeEventHandler(TcpClient sender);
  11. }
  12. public class SSLClient : TcpClient
  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 SSLClientEvent.OnHandShakeEventHandler OnHandShake;
  41. public SSLClient()
  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 SSLClient(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. ~SSLClient()
  64. {
  65. Uninitialize();
  66. }
  67. protected override bool CreateListener()
  68. {
  69. if (IsCreate == true || pListener != IntPtr.Zero || pClient != IntPtr.Zero)
  70. {
  71. return false;
  72. }
  73. pListener = Sdk.Create_HP_TcpClientListener();
  74. if (pListener == IntPtr.Zero)
  75. {
  76. return false;
  77. }
  78. pClient = SSLSdk.Create_HP_SSLClient(pListener);
  79. if (pClient == 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. public new bool Connetion(string address, ushort port, bool async = false)
  134. {
  135. bool ret = false;
  136. if (Initialize())
  137. {
  138. ret = base.Connetion(address, port, async);
  139. }
  140. return ret;
  141. }
  142. public override void Destroy()
  143. {
  144. Stop();
  145. if (pClient != IntPtr.Zero)
  146. {
  147. SSLSdk.Destroy_HP_SSLClient(pClient);
  148. pClient = IntPtr.Zero;
  149. }
  150. if (pListener != IntPtr.Zero)
  151. {
  152. Sdk.Destroy_HP_TcpClientListener(pListener);
  153. pListener = IntPtr.Zero;
  154. }
  155. IsCreate = false;
  156. }
  157. protected HandleResult SDK_OnHandShake(IntPtr connId)
  158. {
  159. if (OnHandShake != null)
  160. {
  161. return OnHandShake(this);
  162. }
  163. return HandleResult.Ignore;
  164. }
  165. }
  166. }