SSLServer.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. /// <summary>
  11. /// 验证模式
  12. /// </summary>
  13. public SSLVerifyMode VerifyMode { get; set; }
  14. /// <summary>
  15. /// 证书文件(客户端可选)
  16. /// </summary>
  17. public string PemCertFile { get; set; }
  18. /// <summary>
  19. /// 私钥文件(客户端可选)
  20. /// </summary>
  21. public string PemKeyFile { get; set; }
  22. /// <summary>
  23. /// 私钥密码(没有密码则为空)
  24. /// </summary>
  25. public string KeyPasswod { get; set; }
  26. /// <summary>
  27. /// CA 证书文件或目录(单向验证或客户端可选)
  28. /// </summary>
  29. public string CAPemCertFileOrPath { get; set; }
  30. /// <summary>
  31. /// 名称:SNI 服务名称回调函数
  32. /// 描述:根据服务器名称选择 SSL 证书
  33. /// 返回值:
  34. /// 0 -- 成功,使用默认 SSL 证书
  35. /// 正数 -- 成功,使用返回值对应的 SNI 主机证书
  36. /// 负数 -- 失败,中断 SSL 握手
  37. /// </summary>
  38. /// <param name="serverName"></param>
  39. /// <returns></returns>
  40. public SSLSdk.SNIServerNameCallback SNIServerNameCallback { get; set; }
  41. public SSLServer()
  42. {
  43. }
  44. /// <summary>
  45. ///
  46. /// </summary>
  47. /// <param name="verifyModel">验证模式</param>
  48. /// <param name="pemCertFile">证书文件(客户端可选)</param>
  49. /// <param name="pemKeyFile">私钥文件(客户端可选)</param>
  50. /// <param name="keyPasswod">私钥密码(没有密码则为空)</param>
  51. /// <param name="caPemCertFileOrPath">CA 证书文件或目录(单向验证或客户端可选)</param>
  52. /// <param name="sniServerNameCallback">SNI 回调函数指针(可选)</param>
  53. public SSLServer(SSLVerifyMode verifyModel, string pemCertFile, string pemKeyFile, string keyPasswod, string caPemCertFileOrPath, SSLSdk.SNIServerNameCallback sniServerNameCallback)
  54. {
  55. this.VerifyMode = verifyModel;
  56. this.PemCertFile = pemCertFile;
  57. this.PemKeyFile = pemKeyFile;
  58. this.KeyPasswod = keyPasswod;
  59. this.CAPemCertFileOrPath = caPemCertFileOrPath;
  60. this.SNIServerNameCallback = sniServerNameCallback;
  61. }
  62. ~SSLServer()
  63. {
  64. }
  65. protected override bool CreateListener()
  66. {
  67. if (IsCreate == true || pListener != IntPtr.Zero || pServer != IntPtr.Zero)
  68. {
  69. return false;
  70. }
  71. pListener = Sdk.Create_HP_TcpServerListener();
  72. if (pListener == IntPtr.Zero)
  73. {
  74. return false;
  75. }
  76. pServer = SSLSdk.Create_HP_SSLServer(pListener);
  77. if (pServer == IntPtr.Zero)
  78. {
  79. return false;
  80. }
  81. IsCreate = true;
  82. return true;
  83. }
  84. /// <summary>
  85. /// 初始化SSL环境
  86. /// </summary>
  87. /// <returns></returns>
  88. public virtual bool Initialize()
  89. {
  90. if (pServer != IntPtr.Zero)
  91. {
  92. PemCertFile = string.IsNullOrWhiteSpace(PemCertFile) ? null : PemCertFile;
  93. PemKeyFile = string.IsNullOrWhiteSpace(PemKeyFile) ? null : PemKeyFile;
  94. KeyPasswod = string.IsNullOrWhiteSpace(KeyPasswod) ? null : KeyPasswod;
  95. CAPemCertFileOrPath = string.IsNullOrWhiteSpace(CAPemCertFileOrPath) ? null : CAPemCertFileOrPath;
  96. return SSLSdk.HP_SSLServer_SetupSSLContext(pServer, VerifyMode, PemCertFile, PemKeyFile, KeyPasswod, CAPemCertFileOrPath, SNIServerNameCallback);
  97. }
  98. return false;
  99. }
  100. /// <summary>
  101. /// 反初始化SSL环境
  102. /// </summary>
  103. public virtual void Uninitialize()
  104. {
  105. if (pServer != IntPtr.Zero)
  106. {
  107. SSLSdk.HP_SSLServer_CleanupSSLContext(pServer);
  108. }
  109. }
  110. public override void Destroy()
  111. {
  112. Stop();
  113. if (pServer != IntPtr.Zero)
  114. {
  115. SSLSdk.Destroy_HP_SSLServer(pServer);
  116. pServer = IntPtr.Zero;
  117. }
  118. if (pListener != IntPtr.Zero)
  119. {
  120. Sdk.Destroy_HP_TcpServerListener(pListener);
  121. pListener = IntPtr.Zero;
  122. }
  123. IsCreate = false;
  124. }
  125. /// <summary>
  126. /// 名称:增加 SNI 主机证书(只用于服务端)
  127. /// 描述:SSL 服务端在 SetupSSLContext() 成功后可以调用本方法增加多个 SNI 主机证书
  128. /// 返回值:正数 -- 成功,并返回 SNI 主机证书对应的索引,该索引用于在 SNI 回调函数中定位 SNI 主机
  129. /// 返回值:负数 -- 失败,可通过 SYS_GetLastError() 获取失败原因
  130. /// </summary>
  131. /// <param name="verifyMode">SSL 验证模式(参考 EnSSLVerifyMode)</param>
  132. /// <param name="pemCertFile">证书文件</param>
  133. /// <param name="pemKeyFile">私钥文件</param>
  134. /// <param name="keyPasswod">私钥密码(没有密码则为空)</param>
  135. /// <param name="caPemCertFileOrPath">CA 证书文件或目录(单向验证可选)</param>
  136. /// <returns></returns>
  137. public int AddServerContext(SSLVerifyMode verifyMode, string pemCertFile, string pemKeyFile, string keyPasswod, string caPemCertFileOrPath)
  138. {
  139. if (string.IsNullOrWhiteSpace(pemCertFile))
  140. {
  141. throw new ArgumentException("参数无效", pemCertFile);
  142. }
  143. if (string.IsNullOrWhiteSpace(pemKeyFile))
  144. {
  145. throw new ArgumentException("参数无效", pemKeyFile);
  146. }
  147. keyPasswod = string.IsNullOrWhiteSpace(keyPasswod) ? null : keyPasswod;
  148. caPemCertFileOrPath = string.IsNullOrWhiteSpace(caPemCertFileOrPath) ? null : caPemCertFileOrPath;
  149. return SSLSdk.HP_SSLServer_AddSSLContext(pServer, verifyMode, pemCertFile, pemKeyFile, KeyPasswod, caPemCertFileOrPath);
  150. }
  151. /// <summary>
  152. /// 启动 SSL 握手
  153. /// 当通信组件设置为非自动握手时,需要调用本方法启动 SSL 握手
  154. /// </summary>
  155. /// <param name="connId"></param>
  156. /// <returns></returns>
  157. public bool StartSSLHandShake(IntPtr connId)
  158. {
  159. return SSLSdk.HP_SSLServer_StartSSLHandShake(pServer, connId);
  160. }
  161. /// <summary>
  162. /// 获取或设置通信组件握手方式(默认:TRUE,自动握手)
  163. /// </summary>
  164. public bool AutoHandShake
  165. {
  166. get
  167. {
  168. return SSLSdk.HP_SSLServer_IsSSLAutoHandShake(pServer);
  169. }
  170. set
  171. {
  172. SSLSdk.HP_SSLServer_SetSSLAutoHandShake(pServer, value);
  173. }
  174. }
  175. }
  176. }