SSLSdk.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Runtime.InteropServices;
  6. namespace HPSocketCS
  7. {
  8. /// <summary>
  9. /// SSL 工作模式
  10. /// 描述:标识 SSL 的工作模式,客户端模式或服务端模式
  11. /// </summary>
  12. public enum SSLSessionMode
  13. {
  14. /// <summary>
  15. /// 客户端模式
  16. /// </summary>
  17. Client = 0,
  18. /// <summary>
  19. /// 服务端模式
  20. /// </summary>
  21. Server = 1,
  22. }
  23. /// <summary>
  24. /// 名称:SSL 验证模式
  25. /// 描述:SSL 验证模式选项,SSL_VM_PEER 可以和后面两个选项组合一起
  26. /// </summary>
  27. public enum SSLVerifyMode
  28. {
  29. /// <summary>
  30. /// SSL_VERIFY_NONE
  31. /// </summary>
  32. None = 0x00,
  33. /// <summary>
  34. /// SSL_VERIFY_PEER
  35. /// </summary>
  36. Peer = 0x01,
  37. /// <summary>
  38. /// SSL_VERIFY_FAIL_IF_NO_PEER_CERT
  39. /// </summary>
  40. FailIfNoPeerCert = 0x02,
  41. /// <summary>
  42. /// SSL_VERIFY_CLIENT_ONCE
  43. /// </summary>
  44. ClientOnce = 0x04,
  45. }
  46. /// <summary>
  47. /// Unicode版本
  48. /// </summary>
  49. public class SSLSdk
  50. {
  51. /// <summary>
  52. /// 名称:SNI 服务名称回调函数
  53. /// 描述:根据服务器名称选择 SSL 证书
  54. /// 返回值:
  55. /// 0 -- 成功,使用默认 SSL 证书
  56. /// 正数 -- 成功,使用返回值对应的 SNI 主机证书
  57. /// 负数 -- 失败,中断 SSL 握手
  58. /// </summary>
  59. /// <param name="serverName"></param>
  60. /// <returns></returns>
  61. public delegate int SNIServerNameCallback(string serverName);
  62. /**************** HPSocket4C 导出函数 ****************/
  63. /// <summary>
  64. /// 创建 HP_SSLServer 对象
  65. /// </summary>
  66. /// <param name="pListener"></param>
  67. /// <returns></returns>
  68. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  69. public static extern IntPtr Create_HP_SSLServer(IntPtr pListener);
  70. /// <summary>
  71. /// 创建 HP_SSLAgent 对象
  72. /// </summary>
  73. /// <param name="pListener"></param>
  74. /// <returns></returns>
  75. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  76. public static extern IntPtr Create_HP_SSLAgent(IntPtr pListener);
  77. /// <summary>
  78. /// 创建 HP_SSLClient 对象
  79. /// </summary>
  80. /// <param name="pListener"></param>
  81. /// <returns></returns>
  82. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  83. public static extern IntPtr Create_HP_SSLClient(IntPtr pListener);
  84. /// <summary>
  85. /// 创建 HP_SSLPullServer 对象
  86. /// </summary>
  87. /// <param name="pListener"></param>
  88. /// <returns></returns>
  89. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  90. public static extern IntPtr Create_HP_SSLPullServer(IntPtr pListener);
  91. /// <summary>
  92. /// 创建 HP_SSLPullAgent 对象
  93. /// </summary>
  94. /// <param name="pListener"></param>
  95. /// <returns></returns>
  96. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  97. public static extern IntPtr Create_HP_SSLPullAgent(IntPtr pListener);
  98. /// <summary>
  99. /// 创建 HP_SSLPullClient 对象
  100. /// </summary>
  101. /// <param name="pListener"></param>
  102. /// <returns></returns>
  103. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  104. public static extern IntPtr Create_HP_SSLPullClient(IntPtr pListener);
  105. /// <summary>
  106. /// 创建 HP_SSLPackServer 对象
  107. /// </summary>
  108. /// <param name="pListener"></param>
  109. /// <returns></returns>
  110. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  111. public static extern IntPtr Create_HP_SSLPackServer(IntPtr pListener);
  112. /// <summary>
  113. /// 创建 HP_SSLPackAgent 对象
  114. /// </summary>
  115. /// <param name="pListener"></param>
  116. /// <returns></returns>
  117. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  118. public static extern IntPtr Create_HP_SSLPackAgent(IntPtr pListener);
  119. /// <summary>
  120. /// 创建 HP_SSLPackClient 对象
  121. /// </summary>
  122. /// <param name="pListener"></param>
  123. /// <returns></returns>
  124. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  125. public static extern IntPtr Create_HP_SSLPackClient(IntPtr pListener);
  126. /// <summary>
  127. /// 销毁 HP_SSLServer 对象
  128. /// </summary>
  129. /// <param name="pObj"></param>
  130. /// <returns></returns>
  131. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  132. public static extern void Destroy_HP_SSLServer(IntPtr pObj);
  133. /// <summary>
  134. /// 销毁 HP_SSLAgent 对象
  135. /// </summary>
  136. /// <param name="pObj"></param>
  137. /// <returns></returns>
  138. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  139. public static extern void Destroy_HP_SSLAgent(IntPtr pObj);
  140. /// <summary>
  141. /// 销毁 HP_SSLClient 对象
  142. /// </summary>
  143. /// <param name="pObj"></param>
  144. /// <returns></returns>
  145. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  146. public static extern void Destroy_HP_SSLClient(IntPtr pObj);
  147. /// <summary>
  148. /// 销毁 HP_SSLPullServer 对象
  149. /// </summary>
  150. /// <param name="pObj"></param>
  151. /// <returns></returns>
  152. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  153. public static extern void Destroy_HP_SSLPullServer(IntPtr pObj);
  154. /// <summary>
  155. /// 销毁 HP_SSLPullAgent 对象
  156. /// </summary>
  157. /// <param name="pObj"></param>
  158. /// <returns></returns>
  159. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  160. public static extern void Destroy_HP_SSLPullAgent(IntPtr pObj);
  161. /// <summary>
  162. /// 销毁 HP_SSLPullClient 对象
  163. /// </summary>
  164. /// <param name="pObj"></param>
  165. /// <returns></returns>
  166. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  167. public static extern void Destroy_HP_SSLPullClient(IntPtr pObj);
  168. /// <summary>
  169. /// 销毁 HP_SSLPackServer 对象
  170. /// </summary>
  171. /// <param name="pObj"></param>
  172. /// <returns></returns>
  173. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  174. public static extern void Destroy_HP_SSLPackServer(IntPtr pObj);
  175. /// <summary>
  176. /// 销毁 HP_SSLPackAgent 对象
  177. /// </summary>
  178. /// <param name="pObj"></param>
  179. /// <returns></returns>
  180. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  181. public static extern void Destroy_HP_SSLPackAgent(IntPtr pObj);
  182. /// <summary>
  183. /// 销毁 HP_SSLPackClient 对象
  184. /// </summary>
  185. /// <param name="pObj"></param>
  186. /// <returns></returns>
  187. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  188. public static extern void Destroy_HP_SSLPackClient(IntPtr pObj);
  189. /************************ SSL 初始化方法 ****************************/
  190. /// <summary>
  191. /// 名称:初始化通信组件 SSL 环境参数
  192. /// 描述:SSL 环境参数必须在 SSL 通信组件启动前完成初始化,否则启动失败
  193. /// </summary>
  194. /// <param name="pAgent"></param>
  195. /// <param name="iVerifyMode">SSL 验证模式(参考 EnSSLVerifyMode)</param>
  196. /// <param name="lpszPemCertFile">证书文件(客户端可选)</param>
  197. /// <param name="lpszPemKeyFile">私钥文件(客户端可选)</param>
  198. /// <param name="lpszKeyPasswod">私钥密码(没有密码则为空)</param>
  199. /// <param name="lpszCAPemCertFileOrPath"> CA 证书文件或目录(单向验证或客户端可选)</param>
  200. /// <returns>TRUE.成功 FALSE.失败,可通过 SYS_GetLastError() 获取失败原因</returns>
  201. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  202. public static extern bool HP_SSLAgent_SetupSSLContext(IntPtr pAgent, SSLVerifyMode verifyMode, string lpszPemCertFile, string lpszPemKeyFile, string lpszKeyPasswod, string lpszCAPemCertFileOrPath);
  203. /// <summary>
  204. /// 名称:初始化通信组件 SSL 环境参数
  205. /// 描述:SSL 环境参数必须在 SSL 通信组件启动前完成初始化,否则启动失败
  206. /// </summary>
  207. /// <param name="pClient"></param>
  208. /// <param name="iVerifyMode">SSL 验证模式(参考 EnSSLVerifyMode)</param>
  209. /// <param name="lpszPemCertFile">证书文件(客户端可选)</param>
  210. /// <param name="lpszPemKeyFile">私钥文件(客户端可选)</param>
  211. /// <param name="lpszKeyPasswod">私钥密码(没有密码则为空)</param>
  212. /// <param name="lpszCAPemCertFileOrPath"> CA 证书文件或目录(单向验证或客户端可选)</param>
  213. /// <returns>TRUE.成功 FALSE.失败,可通过 SYS_GetLastError() 获取失败原因</returns>
  214. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  215. public static extern bool HP_SSLClient_SetupSSLContext(IntPtr pClient, SSLVerifyMode verifyMode, string lpszPemCertFile, string lpszPemKeyFile, string lpszKeyPasswod, string lpszCAPemCertFileOrPath);
  216. /// <summary>
  217. /// 名称:初始化通信组件 SSL 环境参数
  218. /// 描述:SSL 环境参数必须在 SSL 通信组件启动前完成初始化,否则启动失败
  219. /// </summary>
  220. /// <param name="pServer"></param>
  221. /// <param name="iVerifyMode">SSL 验证模式(参考 EnSSLVerifyMode)</param>
  222. /// <param name="lpszPemCertFile">证书文件(客户端可选)</param>
  223. /// <param name="lpszPemKeyFile">私钥文件(客户端可选)</param>
  224. /// <param name="lpszKeyPasswod">私钥密码(没有密码则为空)</param>
  225. /// <param name="lpszCAPemCertFileOrPath"> CA 证书文件或目录(单向验证或客户端可选)</param>
  226. /// <param name="fnServerNameCallback">SNI 回调函数指针(可选)</param>
  227. /// <returns>TRUE.成功 FALSE.失败,可通过 SYS_GetLastError() 获取失败原因</returns>
  228. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  229. public static extern bool HP_SSLServer_SetupSSLContext(IntPtr pServer, SSLVerifyMode verifyMode, string lpszPemCertFile, string lpszPemKeyFile, string lpszKeyPasswod, string lpszCAPemCertFileOrPath, SNIServerNameCallback fnServerNameCallback);
  230. /// <summary>
  231. /// 名称:初始化通信组件 SSL 环境参数
  232. /// 描述:SSL 环境参数必须在 SSL 通信组件启动前完成初始化,否则启动失败
  233. /// </summary>
  234. /// <param name="pServer"></param>
  235. /// <param name="iVerifyMode">SSL 验证模式(参考 EnSSLVerifyMode)</param>
  236. /// <param name="lpszPemCertFile">证书文件(客户端可选)</param>
  237. /// <param name="lpszPemKeyFile">私钥文件(客户端可选)</param>
  238. /// <param name="lpszKeyPasswod">私钥密码(没有密码则为空)</param>
  239. /// <param name="lpszCAPemCertFileOrPath"> CA 证书文件或目录(单向验证或客户端可选)</param>
  240. /// <returns>TRUE.成功 FALSE.失败,可通过 SYS_GetLastError() 获取失败原因</returns>
  241. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  242. public static extern int HP_SSLServer_AddSSLContext(IntPtr pServer, SSLVerifyMode verifyMode, string lpszPemCertFile, string lpszPemKeyFile, string lpszKeyPasswod, string lpszCAPemCertFileOrPath);
  243. /// <summary>
  244. /// 名称:清理通信组件 SSL 运行环境
  245. /// 描述:清理通信组件 SSL 运行环境,回收 SSL 相关内存
  246. /// 1、通信组件析构时会自动调用本方法
  247. /// 2、当要重新设置通信组件 SSL 环境参数时,需要先调用本方法清理原先的环境参数
  248. /// </summary>
  249. /// <param name="pAgent"></param>
  250. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  251. public static extern void HP_SSLAgent_CleanupSSLContext(IntPtr pAgent);
  252. /// <summary>
  253. /// 名称:清理通信组件 SSL 运行环境
  254. /// 描述:清理通信组件 SSL 运行环境,回收 SSL 相关内存
  255. /// 1、通信组件析构时会自动调用本方法
  256. /// 2、当要重新设置通信组件 SSL 环境参数时,需要先调用本方法清理原先的环境参数
  257. /// </summary>
  258. /// <param name="pClient"></param>
  259. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  260. public static extern void HP_SSLClient_CleanupSSLContext(IntPtr pClient);
  261. /// <summary>
  262. /// 名称:清理通信组件 SSL 运行环境
  263. /// 描述:清理通信组件 SSL 运行环境,回收 SSL 相关内存
  264. /// 1、通信组件析构时会自动调用本方法
  265. /// 2、当要重新设置通信组件 SSL 环境参数时,需要先调用本方法清理原先的环境参数
  266. /// </summary>
  267. /// <param name="pServer"></param>
  268. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  269. public static extern void HP_SSLServer_CleanupSSLContext(IntPtr pServer);
  270. /// <summary>
  271. /// 清理线程局部环境 SSL 资源
  272. /// 描述:清理 SSL 全局运行环境,回收 SSL 相关内存
  273. /// 任何一个操作 SSL 的线程,通信结束时都需要清理线程局部环境 SSL 资源
  274. /// 1、主线程和 HP-Socket 工作线程在通信结束时会自动清理线程局部环境 SSL 资源。因此,一般情况下不必手工调用本方法
  275. /// 2、特殊情况下,当自定义线程参与 HP-Socket 通信操作并检查到 SSL 内存泄漏时,需在每次通信结束时自定义线程调用本方法
  276. /// </summary>
  277. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  278. public static extern void HP_SSL_RemoveThreadLocalState();
  279. /// <summary>
  280. /// 启动 SSL 握手
  281. /// 当通信组件设置为非自动握手时,需要调用本方法启动 SSL 握手
  282. /// </summary>
  283. /// <param name="pServer"></param>
  284. /// <param name="dwConnID"></param>
  285. /// <returns>TRUE -- 成功,FALSE -- 失败,可通过 SYS_GetLastError() 获取失败原因</returns>
  286. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  287. public static extern bool HP_SSLServer_StartSSLHandShake(IntPtr pServer, IntPtr dwConnID);
  288. /// <summary>
  289. /// 设置通信组件握手方式(默认:TRUE,自动握手)
  290. /// </summary>
  291. /// <param name="pServer"></param>
  292. /// <param name="bAutoHandShake"></param>
  293. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  294. public static extern void HP_SSLServer_SetSSLAutoHandShake(IntPtr pServer, bool bAutoHandShake);
  295. /// <summary>
  296. /// 获取通信组件握手方式
  297. /// </summary>
  298. /// <param name="pServer"></param>
  299. /// <returns></returns>
  300. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  301. public static extern bool HP_SSLServer_IsSSLAutoHandShake(IntPtr pServer);
  302. /// <summary>
  303. /// 启动 SSL 握手
  304. /// 当通信组件设置为非自动握手时,需要调用本方法启动 SSL 握手
  305. /// </summary>
  306. /// <param name="pAgent"></param>
  307. /// <param name="dwConnID"></param>
  308. /// <returns>TRUE -- 成功,FALSE -- 失败,可通过 SYS_GetLastError() 获取失败原因</returns>
  309. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  310. public static extern bool HP_SSLAgent_StartSSLHandShake(IntPtr pAgent, IntPtr dwConnID);
  311. /// <summary>
  312. /// 设置通信组件握手方式(默认:TRUE,自动握手)
  313. /// </summary>
  314. /// <param name="pAgent"></param>
  315. /// <param name="bAutoHandShake"></param>
  316. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  317. public static extern void HP_SSLAgent_SetSSLAutoHandShake(IntPtr pAgent, bool bAutoHandShake);
  318. /// <summary>
  319. /// 获取通信组件握手方式
  320. /// </summary>
  321. /// <param name="pAgent"></param>
  322. /// <returns></returns>
  323. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  324. public static extern bool HP_SSLAgent_IsSSLAutoHandShake(IntPtr pAgent);
  325. /// <summary>
  326. /// 启动 SSL 握手
  327. /// 当通信组件设置为非自动握手时,需要调用本方法启动 SSL 握手
  328. /// </summary>
  329. /// <param name="pClient"></param>
  330. /// <returns>TRUE -- 成功,FALSE -- 失败,可通过 SYS_GetLastError() 获取失败原因</returns>
  331. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  332. public static extern bool HP_SSLClient_StartSSLHandShake(IntPtr pClient);
  333. /// <summary>
  334. /// 设置通信组件握手方式(默认:TRUE,自动握手)
  335. /// </summary>
  336. /// <param name="pClient"></param>
  337. /// <param name="bAutoHandShake"></param>
  338. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  339. public static extern void HP_SSLClient_SetSSLAutoHandShake(IntPtr pClient, bool bAutoHandShake);
  340. /// <summary>
  341. /// 获取通信组件握手方式
  342. /// </summary>
  343. /// <param name="pClient"></param>
  344. /// <returns></returns>
  345. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  346. public static extern bool HP_SSLClient_IsSSLAutoHandShake(IntPtr pClient);
  347. }
  348. }