123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading;
- namespace HPSocketCS
- {
- public class SSLClient : TcpClient
- {
- /// <summary>
- /// 验证模式
- /// </summary>
- public SSLVerifyMode VerifyMode { get; set; }
- /// <summary>
- /// 证书文件(客户端可选)
- /// </summary>
- public string PemCertFile { get; set; }
- /// <summary>
- /// 私钥文件(客户端可选)
- /// </summary>
- public string PemKeyFile { get; set; }
- /// <summary>
- /// 私钥密码(没有密码则为空)
- /// </summary>
- public string KeyPasswod { get; set; }
- /// <summary>
- /// CA 证书文件或目录(单向验证或客户端可选)
- /// </summary>
- public string CAPemCertFileOrPath { get; set; }
- public SSLClient()
- {
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="verifyModel">验证模式</param>
- /// <param name="pemCertFile">证书文件(客户端可选)</param>
- /// <param name="pemKeyFile">私钥文件(客户端可选)</param>
- /// <param name="keyPasswod">私钥密码(没有密码则为空)</param>
- /// <param name="caPemCertFileOrPath">CA 证书文件或目录(单向验证或客户端可选)</param>
- public SSLClient(SSLVerifyMode verifyModel, string pemCertFile, string pemKeyFile, string keyPasswod, string caPemCertFileOrPath)
- {
- this.VerifyMode = verifyModel;
- this.PemCertFile = pemCertFile;
- this.PemKeyFile = pemKeyFile;
- this.KeyPasswod = keyPasswod;
- this.CAPemCertFileOrPath = caPemCertFileOrPath;
- }
-
- protected override bool CreateListener()
- {
- if (IsCreate == true || pListener != IntPtr.Zero || pClient != IntPtr.Zero)
- {
- return false;
- }
- pListener = Sdk.Create_HP_TcpClientListener();
- if (pListener == IntPtr.Zero)
- {
- return false;
- }
- pClient = SSLSdk.Create_HP_SSLClient(pListener);
- if (pClient == IntPtr.Zero)
- {
- return false;
- }
- IsCreate = true;
- return true;
- }
- /// <summary>
- /// 初始化SSL环境
- /// </summary>
- /// <returns></returns>
- public virtual bool Initialize()
- {
- if (pClient != IntPtr.Zero)
- {
- PemCertFile = string.IsNullOrWhiteSpace(PemCertFile) ? null : PemCertFile;
- PemKeyFile = string.IsNullOrWhiteSpace(PemKeyFile) ? null : PemKeyFile;
- KeyPasswod = string.IsNullOrWhiteSpace(KeyPasswod) ? null : KeyPasswod;
- CAPemCertFileOrPath = string.IsNullOrWhiteSpace(CAPemCertFileOrPath) ? null : CAPemCertFileOrPath;
- return SSLSdk.HP_SSLClient_SetupSSLContext(pClient, VerifyMode, PemCertFile, PemKeyFile, KeyPasswod, CAPemCertFileOrPath);
- }
- return false;
- }
- /// <summary>
- /// 反初始化SSL环境
- /// </summary>
- public virtual void Uninitialize()
- {
- if (pClient != IntPtr.Zero)
- {
- SSLSdk.HP_SSLClient_CleanupSSLContext(pClient);
- }
- }
-
- public override void Destroy()
- {
- Stop();
- if (pClient != IntPtr.Zero)
- {
- SSLSdk.Destroy_HP_SSLClient(pClient);
- pClient = IntPtr.Zero;
- }
- if (pListener != IntPtr.Zero)
- {
- Sdk.Destroy_HP_TcpClientListener(pListener);
- pListener = IntPtr.Zero;
- }
- IsCreate = false;
- }
- /// <summary>
- /// 启动 SSL 握手
- /// 当通信组件设置为非自动握手时,需要调用本方法启动 SSL 握手
- /// </summary>
- /// <param name="connId"></param>
- /// <returns></returns>
- public bool StartSSLHandShake()
- {
- return SSLSdk.HP_SSLClient_StartSSLHandShake(pClient);
- }
- /// <summary>
- /// 获取或设置通信组件握手方式(默认:TRUE,自动握手)
- /// </summary>
- public bool AutoHandShake
- {
- get
- {
- return SSLSdk.HP_SSLClient_IsSSLAutoHandShake(pClient);
- }
- set
- {
- SSLSdk.HP_SSLClient_SetSSLAutoHandShake(pClient, value);
- }
- }
- }
- }
|