SSLHttpAgent.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 HttpsAgent : SSLHttpAgent
  9. {
  10. public HttpsAgent()
  11. : base()
  12. {
  13. }
  14. /// <summary>
  15. ///
  16. /// </summary>
  17. /// <param name="verifyModel">验证模式</param>
  18. /// <param name="pemCertFile">证书文件</param>
  19. /// <param name="pemKeyFile">私钥文件</param>
  20. /// <param name="keyPasswod">私钥密码(没有密码则为空)</param>
  21. /// <param name="caPemCertFileOrPath">CA 证书文件或目录(单向验证或客户端可选)</param>
  22. public HttpsAgent(SSLVerifyMode verifyModel, string pemCertFile, string pemKeyFile, string keyPasswod, string caPemCertFileOrPath)
  23. : base(verifyModel, pemCertFile, pemKeyFile, keyPasswod, caPemCertFileOrPath)
  24. {
  25. }
  26. }
  27. public class SSLHttpAgent : HttpAgent
  28. {
  29. /// <summary>
  30. /// 验证模式
  31. /// </summary>
  32. public SSLVerifyMode VerifyMode { get; set; }
  33. /// <summary>
  34. /// 证书文件(客户端可选)
  35. /// </summary>
  36. public string PemCertFile { get; set; }
  37. /// <summary>
  38. /// 私钥文件(客户端可选)
  39. /// </summary>
  40. public string PemKeyFile { get; set; }
  41. /// <summary>
  42. /// 私钥密码(没有密码则为空)
  43. /// </summary>
  44. public string KeyPasswod { get; set; }
  45. /// <summary>
  46. /// CA 证书文件或目录(单向验证或客户端可选)
  47. /// </summary>
  48. public string CAPemCertFileOrPath { get; set; }
  49. public SSLHttpAgent()
  50. : base()
  51. {
  52. }
  53. /// <summary>
  54. ///
  55. /// </summary>
  56. /// <param name="verifyModel">验证模式</param>
  57. /// <param name="pemCertFile">证书文件</param>
  58. /// <param name="pemKeyFile">私钥文件</param>
  59. /// <param name="keyPasswod">私钥密码(没有密码则为空)</param>
  60. /// <param name="caPemCertFileOrPath">CA 证书文件或目录(单向验证或客户端可选)</param>
  61. public SSLHttpAgent(SSLVerifyMode verifyModel, string pemCertFile, string pemKeyFile, string keyPasswod, string caPemCertFileOrPath)
  62. {
  63. this.VerifyMode = verifyModel;
  64. this.PemCertFile = pemCertFile;
  65. this.PemKeyFile = pemKeyFile;
  66. this.KeyPasswod = keyPasswod;
  67. this.CAPemCertFileOrPath = caPemCertFileOrPath;
  68. }
  69. protected override bool CreateListener()
  70. {
  71. if (IsCreate == true || pListener != IntPtr.Zero || pAgent != IntPtr.Zero)
  72. {
  73. return false;
  74. }
  75. pListener = HttpSdk.Create_HP_HttpAgentListener();
  76. if (pListener == IntPtr.Zero)
  77. {
  78. return false;
  79. }
  80. pAgent = SSLHttpSdk.Create_HP_HttpsAgent(pListener);
  81. if (pAgent == IntPtr.Zero)
  82. {
  83. return false;
  84. }
  85. IsCreate = true;
  86. return true;
  87. }
  88. /// <summary>
  89. /// 初始化SSL环境
  90. /// </summary>
  91. /// <returns></returns>
  92. public virtual bool Initialize()
  93. {
  94. if (pAgent != IntPtr.Zero)
  95. {
  96. PemCertFile = string.IsNullOrWhiteSpace(PemCertFile) ? null : PemCertFile;
  97. PemKeyFile = string.IsNullOrWhiteSpace(PemKeyFile) ? null : PemKeyFile;
  98. KeyPasswod = string.IsNullOrWhiteSpace(KeyPasswod) ? null : KeyPasswod;
  99. CAPemCertFileOrPath = string.IsNullOrWhiteSpace(CAPemCertFileOrPath) ? null : CAPemCertFileOrPath;
  100. return SSLSdk.HP_SSLAgent_SetupSSLContext(pAgent, VerifyMode, PemCertFile, PemKeyFile, KeyPasswod, CAPemCertFileOrPath);
  101. }
  102. return true;
  103. }
  104. /// <summary>
  105. /// 反初始化SSL环境
  106. /// </summary>
  107. public virtual void Uninitialize()
  108. {
  109. if (pAgent != IntPtr.Zero)
  110. {
  111. SSLSdk.HP_SSLAgent_CleanupSSLContext(pAgent);
  112. }
  113. }
  114. public override void Destroy()
  115. {
  116. Stop();
  117. if (pAgent != IntPtr.Zero)
  118. {
  119. SSLHttpSdk.Destroy_HP_HttpsAgent(pAgent);
  120. pAgent = IntPtr.Zero;
  121. }
  122. if (pListener != IntPtr.Zero)
  123. {
  124. HttpSdk.Destroy_HP_HttpAgentListener(pListener);
  125. pListener = IntPtr.Zero;
  126. }
  127. IsCreate = false;
  128. }
  129. }
  130. }