123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- 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初始化锁";
- /// <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 SSLAgent()
- {
- Interlocked.Increment(ref ObjectReferer);
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="_verifyModel">验证模式</param>
- /// <param name="_pemCertFile">证书文件</param>
- /// <param name="_pemKeyFile">私钥文件</param>
- /// <param name="_keyPasswod">私钥密码(没有密码则为空)</param>
- /// <param name="_caPemCertFileOrPath">CA 证书文件或目录(单向验证或客户端可选)</param>
- 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;
- }
- /// <summary>
- /// 初始化SSL环境
- /// </summary>
- /// <returns></returns>
- protected virtual bool Initialize()
- {
- lock (SSLInitLock)
- {
- //if (SSLSdk.HP_SSL_IsValid() == false)
- {
- PemCertFile = string.IsNullOrWhiteSpace(PemCertFile) ? null : PemCertFile;
- PemKeyFile = string.IsNullOrWhiteSpace(PemKeyFile) ? null : PemKeyFile;
- KeyPasswod = string.IsNullOrWhiteSpace(KeyPasswod) ? null : KeyPasswod;
- CAPemCertFileOrPath = string.IsNullOrWhiteSpace(CAPemCertFileOrPath) ? null : CAPemCertFileOrPath;
- var ret = SSLSdk.HP_SSLAgent_SetupSSLContext(pAgent, VerifyMode, PemCertFile, PemKeyFile, KeyPasswod, CAPemCertFileOrPath);
- System.Diagnostics.Trace.WriteLine($"ssl Initialize : {ret}");
- }
- return true;
- }
- }
- /// <summary>
- /// 反初始化SSL环境
- /// </summary>
- protected virtual void Uninitialize()
- {
- if (Interlocked.Decrement(ref ObjectReferer) == 0 && pAgent != IntPtr.Zero)
- {
- SSLSdk.HP_SSLAgent_CleanupSSLContext(pAgent);
- }
- }
- /// <summary>
- /// 启动通讯组件
- /// 启动完成后可开始连接远程服务器
- /// </summary>
- /// <param name="address">绑定地址</param>
- /// <param name="async">是否异步</param>
- /// <returns></returns>
- public new bool Start(string address, bool async = false)
- {
- Uninitialize();
- 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;
- }
- /// <summary>
- /// 启动 SSL 握手
- /// 当通信组件设置为非自动握手时,需要调用本方法启动 SSL 握手
- /// </summary>
- /// <param name="connId"></param>
- /// <returns></returns>
- public bool StartSSLHandShake(IntPtr connId)
- {
- return SSLSdk.HP_SSLAgent_StartSSLHandShake(pAgent, connId);
- }
- /// <summary>
- /// 获取或设置通信组件握手方式(默认:TRUE,自动握手)
- /// </summary>
- public bool AutoHandShake
- {
- get
- {
- return SSLSdk.HP_SSLAgent_IsSSLAutoHandShake(pAgent);
- }
- set
- {
- SSLSdk.HP_SSLAgent_SetSSLAutoHandShake(pAgent, value);
- }
- }
- }
- }
|