SSLServer.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 SSLServer : TcpServer
  9. {
  10. static int ObjectReferer = 0;
  11. static string SSLInitLock = "SSL初始化锁";
  12. Sdk.OnHandShake _OnHandShake = null;
  13. /// <summary>
  14. /// 验证模式
  15. /// </summary>
  16. public SSLVerifyMode VerifyMode { get; set; }
  17. /// <summary>
  18. /// 证书文件(客户端可选)
  19. /// </summary>
  20. public string PemCertFile { get; set; }
  21. /// <summary>
  22. /// 私钥文件(客户端可选)
  23. /// </summary>
  24. public string PemKeyFile { get; set; }
  25. /// <summary>
  26. /// 私钥密码(没有密码则为空)
  27. /// </summary>
  28. public string KeyPasswod { get; set; }
  29. /// <summary>
  30. /// CA 证书文件或目录(单向验证或客户端可选)
  31. /// </summary>
  32. public string CAPemCertFileOrPath { get; set; }
  33. /// <summary>
  34. /// SSL握手成功事件
  35. /// </summary>
  36. public event SSLEvent.OnHandShakeEventHandler OnHandShake;
  37. public SSLServer()
  38. {
  39. Interlocked.Increment(ref ObjectReferer);
  40. }
  41. /// <summary>
  42. ///
  43. /// </summary>
  44. /// <param name="_verifyModel">验证模式</param>
  45. /// <param name="_pemCertFile">证书文件</param>
  46. /// <param name="_pemKeyFile">私钥文件</param>
  47. /// <param name="_keyPasswod">私钥密码(没有密码则为空)</param>
  48. /// <param name="_caPemCertFileOrPath">CA 证书文件或目录(单向验证或客户端可选)</param>
  49. public SSLServer(SSLVerifyMode _verifyModel, string _pemCertFile, string _pemKeyFile, string _keyPasswod, string _caPemCertFileOrPath)
  50. {
  51. Interlocked.Increment(ref ObjectReferer);
  52. this.VerifyMode = _verifyModel;
  53. this.PemCertFile = _pemCertFile;
  54. this.PemKeyFile = _pemKeyFile;
  55. this.KeyPasswod = _keyPasswod;
  56. this.CAPemCertFileOrPath = _caPemCertFileOrPath;
  57. Initialize();
  58. }
  59. ~SSLServer()
  60. {
  61. Uninitialize();
  62. }
  63. protected override bool CreateListener()
  64. {
  65. if (IsCreate == true || pListener != IntPtr.Zero || pServer != IntPtr.Zero)
  66. {
  67. return false;
  68. }
  69. pListener = Sdk.Create_HP_TcpServerListener();
  70. if (pListener == IntPtr.Zero)
  71. {
  72. return false;
  73. }
  74. pServer = SSLSdk.Create_HP_SSLServer(pListener);
  75. if (pServer == IntPtr.Zero)
  76. {
  77. return false;
  78. }
  79. IsCreate = true;
  80. return true;
  81. }
  82. protected override void SetCallback()
  83. {
  84. _OnHandShake = new Sdk.OnHandShake(SDK_OnHandShake);
  85. Sdk.HP_Set_FN_Server_OnHandShake(pListener, _OnHandShake);
  86. base.SetCallback();
  87. }
  88. /// <summary>
  89. /// 初始化SSL环境
  90. /// </summary>
  91. /// <returns></returns>
  92. protected virtual bool Initialize()
  93. {
  94. lock (SSLInitLock)
  95. {
  96. if (SSLSdk.HP_SSL_IsValid() == false)
  97. {
  98. if (string.IsNullOrWhiteSpace(PemCertFile))
  99. {
  100. throw new NullReferenceException("PemCertFile");
  101. }
  102. if (string.IsNullOrWhiteSpace(PemKeyFile))
  103. {
  104. throw new NullReferenceException("PemKeyFile");
  105. }
  106. if (string.IsNullOrWhiteSpace(KeyPasswod))
  107. {
  108. throw new NullReferenceException("KeyPasswod");
  109. }
  110. if (string.IsNullOrWhiteSpace(CAPemCertFileOrPath))
  111. {
  112. throw new NullReferenceException("CAPemCertFileOrPath");
  113. }
  114. return SSLSdk.HP_SSL_Initialize(SSLSessionMode.Server, VerifyMode, PemCertFile, PemKeyFile, KeyPasswod, CAPemCertFileOrPath);
  115. }
  116. return true;
  117. }
  118. }
  119. /// <summary>
  120. /// 反初始化SSL环境
  121. /// </summary>
  122. protected virtual void Uninitialize()
  123. {
  124. if (Interlocked.Decrement(ref ObjectReferer) == 0)
  125. {
  126. SSLSdk.HP_SSL_Cleanup();
  127. }
  128. }
  129. public new bool Start()
  130. {
  131. bool ret = false;
  132. if (Initialize())
  133. {
  134. ret = base.Start();
  135. }
  136. return ret;
  137. }
  138. public override void Destroy()
  139. {
  140. Stop();
  141. if (pServer != IntPtr.Zero)
  142. {
  143. SSLSdk.Destroy_HP_SSLServer(pServer);
  144. pServer = IntPtr.Zero;
  145. }
  146. if (pListener != IntPtr.Zero)
  147. {
  148. Sdk.Destroy_HP_TcpServerListener(pListener);
  149. pListener = IntPtr.Zero;
  150. }
  151. IsCreate = false;
  152. }
  153. protected HandleResult SDK_OnHandShake(IntPtr connId)
  154. {
  155. if (OnHandShake != null)
  156. {
  157. return OnHandShake(connId);
  158. }
  159. return HandleResult.Ignore;
  160. }
  161. }
  162. }