123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Runtime.InteropServices;
- namespace HPSocketCS
- {
- /// <summary>
- /// SSL 工作模式
- /// 描述:标识 SSL 的工作模式,客户端模式或服务端模式
- /// </summary>
- public enum SSLSessionMode
- {
- /// <summary>
- /// 客户端模式
- /// </summary>
- Client = 0,
- /// <summary>
- /// 服务端模式
- /// </summary>
- Server = 1,
- }
- /// <summary>
- /// 名称:SSL 验证模式
- /// 描述:SSL 验证模式选项,SSL_VM_PEER 可以和后面两个选项组合一起
- /// </summary>
- public enum SSLVerifyMode
- {
- /// <summary>
- /// SSL_VERIFY_NONE
- /// </summary>
- None = 0x00,
- /// <summary>
- /// SSL_VERIFY_PEER
- /// </summary>
- Peer = 0x01,
- /// <summary>
- /// SSL_VERIFY_FAIL_IF_NO_PEER_CERT
- /// </summary>
- FailIfNoPeerCert = 0x02,
- /// <summary>
- /// SSL_VERIFY_CLIENT_ONCE
- /// </summary>
- ClientOnce = 0x04,
- }
- /// <summary>
- /// Unicode版本
- /// </summary>
- public class SSLSdk
- {
- /// <summary>
- /// 名称:SNI 服务名称回调函数
- /// 描述:根据服务器名称选择 SSL 证书
- /// 返回值:
- /// 0 -- 成功,使用默认 SSL 证书
- /// 正数 -- 成功,使用返回值对应的 SNI 主机证书
- /// 负数 -- 失败,中断 SSL 握手
- /// </summary>
- /// <param name="serverName"></param>
- /// <returns></returns>
- public delegate int SNIServerNameCallback(string serverName);
- /**************** HPSocket4C 导出函数 ****************/
- /// <summary>
- /// 创建 HP_SSLServer 对象
- /// </summary>
- /// <param name="pListener"></param>
- /// <returns></returns>
- [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
- public static extern IntPtr Create_HP_SSLServer(IntPtr pListener);
- /// <summary>
- /// 创建 HP_SSLAgent 对象
- /// </summary>
- /// <param name="pListener"></param>
- /// <returns></returns>
- [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
- public static extern IntPtr Create_HP_SSLAgent(IntPtr pListener);
- /// <summary>
- /// 创建 HP_SSLClient 对象
- /// </summary>
- /// <param name="pListener"></param>
- /// <returns></returns>
- [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
- public static extern IntPtr Create_HP_SSLClient(IntPtr pListener);
- /// <summary>
- /// 创建 HP_SSLPullServer 对象
- /// </summary>
- /// <param name="pListener"></param>
- /// <returns></returns>
- [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
- public static extern IntPtr Create_HP_SSLPullServer(IntPtr pListener);
- /// <summary>
- /// 创建 HP_SSLPullAgent 对象
- /// </summary>
- /// <param name="pListener"></param>
- /// <returns></returns>
- [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
- public static extern IntPtr Create_HP_SSLPullAgent(IntPtr pListener);
- /// <summary>
- /// 创建 HP_SSLPullClient 对象
- /// </summary>
- /// <param name="pListener"></param>
- /// <returns></returns>
- [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
- public static extern IntPtr Create_HP_SSLPullClient(IntPtr pListener);
- /// <summary>
- /// 创建 HP_SSLPackServer 对象
- /// </summary>
- /// <param name="pListener"></param>
- /// <returns></returns>
- [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
- public static extern IntPtr Create_HP_SSLPackServer(IntPtr pListener);
- /// <summary>
- /// 创建 HP_SSLPackAgent 对象
- /// </summary>
- /// <param name="pListener"></param>
- /// <returns></returns>
- [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
- public static extern IntPtr Create_HP_SSLPackAgent(IntPtr pListener);
- /// <summary>
- /// 创建 HP_SSLPackClient 对象
- /// </summary>
- /// <param name="pListener"></param>
- /// <returns></returns>
- [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
- public static extern IntPtr Create_HP_SSLPackClient(IntPtr pListener);
- /// <summary>
- /// 销毁 HP_SSLServer 对象
- /// </summary>
- /// <param name="pObj"></param>
- /// <returns></returns>
- [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
- public static extern void Destroy_HP_SSLServer(IntPtr pObj);
- /// <summary>
- /// 销毁 HP_SSLAgent 对象
- /// </summary>
- /// <param name="pObj"></param>
- /// <returns></returns>
- [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
- public static extern void Destroy_HP_SSLAgent(IntPtr pObj);
- /// <summary>
- /// 销毁 HP_SSLClient 对象
- /// </summary>
- /// <param name="pObj"></param>
- /// <returns></returns>
- [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
- public static extern void Destroy_HP_SSLClient(IntPtr pObj);
- /// <summary>
- /// 销毁 HP_SSLPullServer 对象
- /// </summary>
- /// <param name="pObj"></param>
- /// <returns></returns>
- [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
- public static extern void Destroy_HP_SSLPullServer(IntPtr pObj);
- /// <summary>
- /// 销毁 HP_SSLPullAgent 对象
- /// </summary>
- /// <param name="pObj"></param>
- /// <returns></returns>
- [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
- public static extern void Destroy_HP_SSLPullAgent(IntPtr pObj);
- /// <summary>
- /// 销毁 HP_SSLPullClient 对象
- /// </summary>
- /// <param name="pObj"></param>
- /// <returns></returns>
- [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
- public static extern void Destroy_HP_SSLPullClient(IntPtr pObj);
- /// <summary>
- /// 销毁 HP_SSLPackServer 对象
- /// </summary>
- /// <param name="pObj"></param>
- /// <returns></returns>
- [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
- public static extern void Destroy_HP_SSLPackServer(IntPtr pObj);
- /// <summary>
- /// 销毁 HP_SSLPackAgent 对象
- /// </summary>
- /// <param name="pObj"></param>
- /// <returns></returns>
- [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
- public static extern void Destroy_HP_SSLPackAgent(IntPtr pObj);
- /// <summary>
- /// 销毁 HP_SSLPackClient 对象
- /// </summary>
- /// <param name="pObj"></param>
- /// <returns></returns>
- [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
- public static extern void Destroy_HP_SSLPackClient(IntPtr pObj);
- /************************ SSL 初始化方法 ****************************/
- /// <summary>
- /// 名称:初始化通信组件 SSL 环境参数
- /// 描述:SSL 环境参数必须在 SSL 通信组件启动前完成初始化,否则启动失败
- /// </summary>
- /// <param name="pAgent"></param>
- /// <param name="iVerifyMode">SSL 验证模式(参考 EnSSLVerifyMode)</param>
- /// <param name="lpszPemCertFile">证书文件(客户端可选)</param>
- /// <param name="lpszPemKeyFile">私钥文件(客户端可选)</param>
- /// <param name="lpszKeyPasswod">私钥密码(没有密码则为空)</param>
- /// <param name="lpszCAPemCertFileOrPath"> CA 证书文件或目录(单向验证或客户端可选)</param>
- /// <returns>TRUE.成功 FALSE.失败,可通过 SYS_GetLastError() 获取失败原因</returns>
- [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
- public static extern bool HP_SSLAgent_SetupSSLContext(IntPtr pAgent, SSLVerifyMode verifyMode, string lpszPemCertFile, string lpszPemKeyFile, string lpszKeyPasswod, string lpszCAPemCertFileOrPath);
- /// <summary>
- /// 名称:初始化通信组件 SSL 环境参数
- /// 描述:SSL 环境参数必须在 SSL 通信组件启动前完成初始化,否则启动失败
- /// </summary>
- /// <param name="pClient"></param>
- /// <param name="iVerifyMode">SSL 验证模式(参考 EnSSLVerifyMode)</param>
- /// <param name="lpszPemCertFile">证书文件(客户端可选)</param>
- /// <param name="lpszPemKeyFile">私钥文件(客户端可选)</param>
- /// <param name="lpszKeyPasswod">私钥密码(没有密码则为空)</param>
- /// <param name="lpszCAPemCertFileOrPath"> CA 证书文件或目录(单向验证或客户端可选)</param>
- /// <returns>TRUE.成功 FALSE.失败,可通过 SYS_GetLastError() 获取失败原因</returns>
- [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
- public static extern bool HP_SSLClient_SetupSSLContext(IntPtr pClient, SSLVerifyMode verifyMode, string lpszPemCertFile, string lpszPemKeyFile, string lpszKeyPasswod, string lpszCAPemCertFileOrPath);
- /// <summary>
- /// 名称:初始化通信组件 SSL 环境参数
- /// 描述:SSL 环境参数必须在 SSL 通信组件启动前完成初始化,否则启动失败
- /// </summary>
- /// <param name="pServer"></param>
- /// <param name="iVerifyMode">SSL 验证模式(参考 EnSSLVerifyMode)</param>
- /// <param name="lpszPemCertFile">证书文件(客户端可选)</param>
- /// <param name="lpszPemKeyFile">私钥文件(客户端可选)</param>
- /// <param name="lpszKeyPasswod">私钥密码(没有密码则为空)</param>
- /// <param name="lpszCAPemCertFileOrPath"> CA 证书文件或目录(单向验证或客户端可选)</param>
- /// <param name="fnServerNameCallback">SNI 回调函数指针(可选)</param>
- /// <returns>TRUE.成功 FALSE.失败,可通过 SYS_GetLastError() 获取失败原因</returns>
- [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
- public static extern bool HP_SSLServer_SetupSSLContext(IntPtr pServer, SSLVerifyMode verifyMode, string lpszPemCertFile, string lpszPemKeyFile, string lpszKeyPasswod, string lpszCAPemCertFileOrPath, SNIServerNameCallback fnServerNameCallback);
-
- /// <summary>
- /// 名称:初始化通信组件 SSL 环境参数
- /// 描述:SSL 环境参数必须在 SSL 通信组件启动前完成初始化,否则启动失败
- /// </summary>
- /// <param name="pServer"></param>
- /// <param name="iVerifyMode">SSL 验证模式(参考 EnSSLVerifyMode)</param>
- /// <param name="lpszPemCertFile">证书文件(客户端可选)</param>
- /// <param name="lpszPemKeyFile">私钥文件(客户端可选)</param>
- /// <param name="lpszKeyPasswod">私钥密码(没有密码则为空)</param>
- /// <param name="lpszCAPemCertFileOrPath"> CA 证书文件或目录(单向验证或客户端可选)</param>
- /// <returns>TRUE.成功 FALSE.失败,可通过 SYS_GetLastError() 获取失败原因</returns>
- [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
- public static extern int HP_SSLServer_AddSSLContext(IntPtr pServer, SSLVerifyMode verifyMode, string lpszPemCertFile, string lpszPemKeyFile, string lpszKeyPasswod, string lpszCAPemCertFileOrPath);
- /// <summary>
- /// 名称:清理通信组件 SSL 运行环境
- /// 描述:清理通信组件 SSL 运行环境,回收 SSL 相关内存
- /// 1、通信组件析构时会自动调用本方法
- /// 2、当要重新设置通信组件 SSL 环境参数时,需要先调用本方法清理原先的环境参数
- /// </summary>
- /// <param name="pAgent"></param>
- [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
- public static extern void HP_SSLAgent_CleanupSSLContext(IntPtr pAgent);
- /// <summary>
- /// 名称:清理通信组件 SSL 运行环境
- /// 描述:清理通信组件 SSL 运行环境,回收 SSL 相关内存
- /// 1、通信组件析构时会自动调用本方法
- /// 2、当要重新设置通信组件 SSL 环境参数时,需要先调用本方法清理原先的环境参数
- /// </summary>
- /// <param name="pClient"></param>
- [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
- public static extern void HP_SSLClient_CleanupSSLContext(IntPtr pClient);
- /// <summary>
- /// 名称:清理通信组件 SSL 运行环境
- /// 描述:清理通信组件 SSL 运行环境,回收 SSL 相关内存
- /// 1、通信组件析构时会自动调用本方法
- /// 2、当要重新设置通信组件 SSL 环境参数时,需要先调用本方法清理原先的环境参数
- /// </summary>
- /// <param name="pServer"></param>
- [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
- public static extern void HP_SSLServer_CleanupSSLContext(IntPtr pServer);
-
- /// <summary>
- /// 清理线程局部环境 SSL 资源
- /// 描述:清理 SSL 全局运行环境,回收 SSL 相关内存
- /// 任何一个操作 SSL 的线程,通信结束时都需要清理线程局部环境 SSL 资源
- /// 1、主线程和 HP-Socket 工作线程在通信结束时会自动清理线程局部环境 SSL 资源。因此,一般情况下不必手工调用本方法
- /// 2、特殊情况下,当自定义线程参与 HP-Socket 通信操作并检查到 SSL 内存泄漏时,需在每次通信结束时自定义线程调用本方法
- /// </summary>
- [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
- public static extern void HP_SSL_RemoveThreadLocalState();
- /// <summary>
- /// 启动 SSL 握手
- /// 当通信组件设置为非自动握手时,需要调用本方法启动 SSL 握手
- /// </summary>
- /// <param name="pServer"></param>
- /// <param name="dwConnID"></param>
- /// <returns>TRUE -- 成功,FALSE -- 失败,可通过 SYS_GetLastError() 获取失败原因</returns>
- [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
- public static extern bool HP_SSLServer_StartSSLHandShake(IntPtr pServer, IntPtr dwConnID);
- /// <summary>
- /// 设置通信组件握手方式(默认:TRUE,自动握手)
- /// </summary>
- /// <param name="pServer"></param>
- /// <param name="bAutoHandShake"></param>
- [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
- public static extern void HP_SSLServer_SetSSLAutoHandShake(IntPtr pServer, bool bAutoHandShake);
- /// <summary>
- /// 获取通信组件握手方式
- /// </summary>
- /// <param name="pServer"></param>
- /// <returns></returns>
- [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
- public static extern bool HP_SSLServer_IsSSLAutoHandShake(IntPtr pServer);
- /// <summary>
- /// 启动 SSL 握手
- /// 当通信组件设置为非自动握手时,需要调用本方法启动 SSL 握手
- /// </summary>
- /// <param name="pAgent"></param>
- /// <param name="dwConnID"></param>
- /// <returns>TRUE -- 成功,FALSE -- 失败,可通过 SYS_GetLastError() 获取失败原因</returns>
- [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
- public static extern bool HP_SSLAgent_StartSSLHandShake(IntPtr pAgent, IntPtr dwConnID);
- /// <summary>
- /// 设置通信组件握手方式(默认:TRUE,自动握手)
- /// </summary>
- /// <param name="pAgent"></param>
- /// <param name="bAutoHandShake"></param>
- [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
- public static extern void HP_SSLAgent_SetSSLAutoHandShake(IntPtr pAgent, bool bAutoHandShake);
- /// <summary>
- /// 获取通信组件握手方式
- /// </summary>
- /// <param name="pAgent"></param>
- /// <returns></returns>
- [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
- public static extern bool HP_SSLAgent_IsSSLAutoHandShake(IntPtr pAgent);
- /// <summary>
- /// 启动 SSL 握手
- /// 当通信组件设置为非自动握手时,需要调用本方法启动 SSL 握手
- /// </summary>
- /// <param name="pClient"></param>
- /// <returns>TRUE -- 成功,FALSE -- 失败,可通过 SYS_GetLastError() 获取失败原因</returns>
- [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
- public static extern bool HP_SSLClient_StartSSLHandShake(IntPtr pClient);
- /// <summary>
- /// 设置通信组件握手方式(默认:TRUE,自动握手)
- /// </summary>
- /// <param name="pClient"></param>
- /// <param name="bAutoHandShake"></param>
- [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
- public static extern void HP_SSLClient_SetSSLAutoHandShake(IntPtr pClient, bool bAutoHandShake);
- /// <summary>
- /// 获取通信组件握手方式
- /// </summary>
- /// <param name="pClient"></param>
- /// <returns></returns>
- [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
- public static extern bool HP_SSLClient_IsSSLAutoHandShake(IntPtr pClient);
- }
- }
|