using System; using System.Collections.Generic; using System.Runtime.InteropServices; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary; using System.Linq; using System.Text; using System.IO; using System.Threading; namespace HPSocketCS { public class SSLAgent : TcpAgent { static int ObjectReferer = 0; static string SSLInitLock = "SSL初始化锁"; Sdk.OnHandShake _OnHandShake = null; /// /// 验证模式 /// public SSLVerifyMode VerifyMode { get; set; } /// /// 证书文件(客户端可选) /// public string PemCertFile { get; set; } /// /// 私钥文件(客户端可选) /// public string PemKeyFile { get; set; } /// /// 私钥密码(没有密码则为空) /// public string KeyPasswod { get; set; } /// /// CA 证书文件或目录(单向验证或客户端可选) /// public string CAPemCertFileOrPath { get; set; } /// /// SSL握手成功事件 /// public event SSLEvent.OnHandShakeEventHandler OnHandShake; public SSLAgent() { Interlocked.Increment(ref ObjectReferer); } /// /// /// /// 验证模式 /// 证书文件 /// 私钥文件 /// 私钥密码(没有密码则为空) /// CA 证书文件或目录(单向验证或客户端可选) public SSLAgent(SSLVerifyMode _verifyModel, string _pemCertFile, string _pemKeyFile, string _keyPasswod, string _caPemCertFileOrPath) { Interlocked.Increment(ref ObjectReferer); this.VerifyMode = _verifyModel; this.PemCertFile = _pemCertFile; this.PemKeyFile = _pemKeyFile; this.KeyPasswod = _keyPasswod; this.CAPemCertFileOrPath = _caPemCertFileOrPath; Initialize(); } ~SSLAgent() { Uninitialize(); } protected override bool CreateListener() { if (IsCreate == true || pListener != IntPtr.Zero || pAgent != IntPtr.Zero) { return false; } pListener = Sdk.Create_HP_TcpAgentListener(); if (pListener == IntPtr.Zero) { return false; } pAgent = SSLSdk.Create_HP_SSLAgent(pListener); if (pAgent == IntPtr.Zero) { return false; } IsCreate = true; return true; } protected override void SetCallback() { _OnHandShake = new Sdk.OnHandShake(SDK_OnHandShake); Sdk.HP_Set_FN_Server_OnHandShake(pListener, _OnHandShake); base.SetCallback(); } /// /// 初始化SSL环境 /// /// protected virtual bool Initialize() { lock (SSLInitLock) { if (SSLSdk.HP_SSL_IsValid() == false) { if (string.IsNullOrWhiteSpace(PemCertFile)) { throw new NullReferenceException("PemCertFile"); } if (string.IsNullOrWhiteSpace(PemKeyFile)) { throw new NullReferenceException("PemKeyFile"); } if (string.IsNullOrWhiteSpace(KeyPasswod)) { throw new NullReferenceException("KeyPasswod"); } if (string.IsNullOrWhiteSpace(CAPemCertFileOrPath)) { throw new NullReferenceException("CAPemCertFileOrPath"); } return SSLSdk.HP_SSL_Initialize(SSLSessionMode.Client, VerifyMode, PemCertFile, PemKeyFile, KeyPasswod, CAPemCertFileOrPath); } return true; } } /// /// 反初始化SSL环境 /// protected virtual void Uninitialize() { if (Interlocked.Decrement(ref ObjectReferer) == 0) { SSLSdk.HP_SSL_Cleanup(); } } /// /// 启动通讯组件 /// 启动完成后可开始连接远程服务器 /// /// 绑定地址 /// 是否异步 /// public new bool Start(string address, bool async = false) { bool ret = false; if (Initialize()) { ret = base.Start(address, async); } return ret; } public override void Destroy() { Stop(); if (pAgent != IntPtr.Zero) { SSLSdk.Destroy_HP_SSLAgent(pAgent); pAgent = IntPtr.Zero; } if (pListener != IntPtr.Zero) { Sdk.Destroy_HP_TcpAgentListener(pListener); pListener = IntPtr.Zero; } IsCreate = false; } protected HandleResult SDK_OnHandShake(IntPtr connId) { if (OnHandShake != null) { return OnHandShake(connId); } return HandleResult.Ignore; } } }