SSLClient.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 SSLClient : TcpClient
  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. public SSLClient()
  31. {
  32. }
  33. /// <summary>
  34. ///
  35. /// </summary>
  36. /// <param name="verifyModel">验证模式</param>
  37. /// <param name="pemCertFile">证书文件(客户端可选)</param>
  38. /// <param name="pemKeyFile">私钥文件(客户端可选)</param>
  39. /// <param name="keyPasswod">私钥密码(没有密码则为空)</param>
  40. /// <param name="caPemCertFileOrPath">CA 证书文件或目录(单向验证或客户端可选)</param>
  41. public SSLClient(SSLVerifyMode verifyModel, string pemCertFile, string pemKeyFile, string keyPasswod, string caPemCertFileOrPath)
  42. {
  43. this.VerifyMode = verifyModel;
  44. this.PemCertFile = pemCertFile;
  45. this.PemKeyFile = pemKeyFile;
  46. this.KeyPasswod = keyPasswod;
  47. this.CAPemCertFileOrPath = caPemCertFileOrPath;
  48. }
  49. protected override bool CreateListener()
  50. {
  51. if (IsCreate == true || pListener != IntPtr.Zero || pClient != IntPtr.Zero)
  52. {
  53. return false;
  54. }
  55. pListener = Sdk.Create_HP_TcpClientListener();
  56. if (pListener == IntPtr.Zero)
  57. {
  58. return false;
  59. }
  60. pClient = SSLSdk.Create_HP_SSLClient(pListener);
  61. if (pClient == IntPtr.Zero)
  62. {
  63. return false;
  64. }
  65. IsCreate = true;
  66. return true;
  67. }
  68. /// <summary>
  69. /// 初始化SSL环境
  70. /// </summary>
  71. /// <returns></returns>
  72. public virtual bool Initialize()
  73. {
  74. if (pClient != IntPtr.Zero)
  75. {
  76. PemCertFile = string.IsNullOrWhiteSpace(PemCertFile) ? null : PemCertFile;
  77. PemKeyFile = string.IsNullOrWhiteSpace(PemKeyFile) ? null : PemKeyFile;
  78. KeyPasswod = string.IsNullOrWhiteSpace(KeyPasswod) ? null : KeyPasswod;
  79. CAPemCertFileOrPath = string.IsNullOrWhiteSpace(CAPemCertFileOrPath) ? null : CAPemCertFileOrPath;
  80. return SSLSdk.HP_SSLClient_SetupSSLContext(pClient, VerifyMode, PemCertFile, PemKeyFile, KeyPasswod, CAPemCertFileOrPath);
  81. }
  82. return false;
  83. }
  84. /// <summary>
  85. /// 反初始化SSL环境
  86. /// </summary>
  87. public virtual void Uninitialize()
  88. {
  89. if (pClient != IntPtr.Zero)
  90. {
  91. SSLSdk.HP_SSLClient_CleanupSSLContext(pClient);
  92. }
  93. }
  94. public override void Destroy()
  95. {
  96. Stop();
  97. if (pClient != IntPtr.Zero)
  98. {
  99. SSLSdk.Destroy_HP_SSLClient(pClient);
  100. pClient = IntPtr.Zero;
  101. }
  102. if (pListener != IntPtr.Zero)
  103. {
  104. Sdk.Destroy_HP_TcpClientListener(pListener);
  105. pListener = IntPtr.Zero;
  106. }
  107. IsCreate = false;
  108. }
  109. /// <summary>
  110. /// 启动 SSL 握手
  111. /// 当通信组件设置为非自动握手时,需要调用本方法启动 SSL 握手
  112. /// </summary>
  113. /// <param name="connId"></param>
  114. /// <returns></returns>
  115. public bool StartSSLHandShake()
  116. {
  117. return SSLSdk.HP_SSLClient_StartSSLHandShake(pClient);
  118. }
  119. /// <summary>
  120. /// 获取或设置通信组件握手方式(默认:TRUE,自动握手)
  121. /// </summary>
  122. public bool AutoHandShake
  123. {
  124. get
  125. {
  126. return SSLSdk.HP_SSLClient_IsSSLAutoHandShake(pClient);
  127. }
  128. set
  129. {
  130. SSLSdk.HP_SSLClient_SetSSLAutoHandShake(pClient, value);
  131. }
  132. }
  133. }
  134. }