SSLAgent.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. /// <summary>
  17. /// 验证模式
  18. /// </summary>
  19. public SSLVerifyMode VerifyMode { get; set; }
  20. /// <summary>
  21. /// 证书文件(客户端可选)
  22. /// </summary>
  23. public string PemCertFile { get; set; }
  24. /// <summary>
  25. /// 私钥文件(客户端可选)
  26. /// </summary>
  27. public string PemKeyFile { get; set; }
  28. /// <summary>
  29. /// 私钥密码(没有密码则为空)
  30. /// </summary>
  31. public string KeyPasswod { get; set; }
  32. /// <summary>
  33. /// CA 证书文件或目录(单向验证或客户端可选)
  34. /// </summary>
  35. public string CAPemCertFileOrPath { get; set; }
  36. public SSLAgent()
  37. {
  38. Interlocked.Increment(ref ObjectReferer);
  39. }
  40. /// <summary>
  41. ///
  42. /// </summary>
  43. /// <param name="_verifyModel">验证模式</param>
  44. /// <param name="_pemCertFile">证书文件</param>
  45. /// <param name="_pemKeyFile">私钥文件</param>
  46. /// <param name="_keyPasswod">私钥密码(没有密码则为空)</param>
  47. /// <param name="_caPemCertFileOrPath">CA 证书文件或目录(单向验证或客户端可选)</param>
  48. public SSLAgent(SSLVerifyMode _verifyModel, string _pemCertFile, string _pemKeyFile, string _keyPasswod, string _caPemCertFileOrPath)
  49. {
  50. Interlocked.Increment(ref ObjectReferer);
  51. this.VerifyMode = _verifyModel;
  52. this.PemCertFile = _pemCertFile;
  53. this.PemKeyFile = _pemKeyFile;
  54. this.KeyPasswod = _keyPasswod;
  55. this.CAPemCertFileOrPath = _caPemCertFileOrPath;
  56. //Initialize();
  57. }
  58. ~SSLAgent()
  59. {
  60. //Uninitialize();
  61. }
  62. protected override bool CreateListener()
  63. {
  64. if (IsCreate == true || pListener != IntPtr.Zero || pAgent != IntPtr.Zero)
  65. {
  66. return false;
  67. }
  68. pListener = Sdk.Create_HP_TcpAgentListener();
  69. if (pListener == IntPtr.Zero)
  70. {
  71. return false;
  72. }
  73. pAgent = SSLSdk.Create_HP_SSLAgent(pListener);
  74. if (pAgent == IntPtr.Zero)
  75. {
  76. return false;
  77. }
  78. IsCreate = true;
  79. return true;
  80. }
  81. /// <summary>
  82. /// 初始化SSL环境
  83. /// </summary>
  84. /// <returns></returns>
  85. protected virtual bool Initialize()
  86. {
  87. lock (SSLInitLock)
  88. {
  89. //if (SSLSdk.HP_SSL_IsValid() == false)
  90. {
  91. PemCertFile = string.IsNullOrWhiteSpace(PemCertFile) ? null : PemCertFile;
  92. PemKeyFile = string.IsNullOrWhiteSpace(PemKeyFile) ? null : PemKeyFile;
  93. KeyPasswod = string.IsNullOrWhiteSpace(KeyPasswod) ? null : KeyPasswod;
  94. CAPemCertFileOrPath = string.IsNullOrWhiteSpace(CAPemCertFileOrPath) ? null : CAPemCertFileOrPath;
  95. var ret = SSLSdk.HP_SSLAgent_SetupSSLContext(pAgent, VerifyMode, PemCertFile, PemKeyFile, KeyPasswod, CAPemCertFileOrPath);
  96. System.Diagnostics.Trace.WriteLine($"ssl Initialize : {ret}");
  97. }
  98. return true;
  99. }
  100. }
  101. /// <summary>
  102. /// 反初始化SSL环境
  103. /// </summary>
  104. protected virtual void Uninitialize()
  105. {
  106. if (Interlocked.Decrement(ref ObjectReferer) == 0 && pAgent != IntPtr.Zero)
  107. {
  108. SSLSdk.HP_SSLAgent_CleanupSSLContext(pAgent);
  109. }
  110. }
  111. /// <summary>
  112. /// 启动通讯组件
  113. /// 启动完成后可开始连接远程服务器
  114. /// </summary>
  115. /// <param name="address">绑定地址</param>
  116. /// <param name="async">是否异步</param>
  117. /// <returns></returns>
  118. public new bool Start(string address, bool async = false)
  119. {
  120. Uninitialize();
  121. bool ret = false;
  122. if (Initialize())
  123. {
  124. ret = base.Start(address, async);
  125. }
  126. return ret;
  127. }
  128. public override void Destroy()
  129. {
  130. Stop();
  131. if (pAgent != IntPtr.Zero)
  132. {
  133. SSLSdk.Destroy_HP_SSLAgent(pAgent);
  134. pAgent = IntPtr.Zero;
  135. }
  136. if (pListener != IntPtr.Zero)
  137. {
  138. Sdk.Destroy_HP_TcpAgentListener(pListener);
  139. pListener = IntPtr.Zero;
  140. }
  141. IsCreate = false;
  142. }
  143. /// <summary>
  144. /// 启动 SSL 握手
  145. /// 当通信组件设置为非自动握手时,需要调用本方法启动 SSL 握手
  146. /// </summary>
  147. /// <param name="connId"></param>
  148. /// <returns></returns>
  149. public bool StartSSLHandShake(IntPtr connId)
  150. {
  151. return SSLSdk.HP_SSLAgent_StartSSLHandShake(pAgent, connId);
  152. }
  153. /// <summary>
  154. /// 获取或设置通信组件握手方式(默认:TRUE,自动握手)
  155. /// </summary>
  156. public bool AutoHandShake
  157. {
  158. get
  159. {
  160. return SSLSdk.HP_SSLAgent_IsSSLAutoHandShake(pAgent);
  161. }
  162. set
  163. {
  164. SSLSdk.HP_SSLAgent_SetSSLAutoHandShake(pAgent, value);
  165. }
  166. }
  167. }
  168. }