123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading;
- namespace HPSocketCS
- {
- public class SSLServer : TcpServer
- {
-
-
-
- public SSLVerifyMode VerifyMode { get; set; }
-
-
-
- public string PemCertFile { get; set; }
-
-
-
- public string PemKeyFile { get; set; }
-
-
-
- public string KeyPasswod { get; set; }
-
-
-
- public string CAPemCertFileOrPath { get; set; }
-
-
-
-
-
-
-
-
-
-
- public SSLSdk.SNIServerNameCallback SNIServerNameCallback { get; set; }
- public SSLServer()
- {
- }
-
-
-
-
-
-
-
-
-
- public SSLServer(SSLVerifyMode verifyModel, string pemCertFile, string pemKeyFile, string keyPasswod, string caPemCertFileOrPath, SSLSdk.SNIServerNameCallback sniServerNameCallback)
- {
- this.VerifyMode = verifyModel;
- this.PemCertFile = pemCertFile;
- this.PemKeyFile = pemKeyFile;
- this.KeyPasswod = keyPasswod;
- this.CAPemCertFileOrPath = caPemCertFileOrPath;
- this.SNIServerNameCallback = sniServerNameCallback;
- }
- ~SSLServer()
- {
- }
- protected override bool CreateListener()
- {
- if (IsCreate == true || pListener != IntPtr.Zero || pServer != IntPtr.Zero)
- {
- return false;
- }
- pListener = Sdk.Create_HP_TcpServerListener();
- if (pListener == IntPtr.Zero)
- {
- return false;
- }
- pServer = SSLSdk.Create_HP_SSLServer(pListener);
- if (pServer == IntPtr.Zero)
- {
- return false;
- }
- IsCreate = true;
- return true;
- }
-
-
-
-
- public virtual bool Initialize()
- {
- if (pServer != 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_SSLServer_SetupSSLContext(pServer, VerifyMode, PemCertFile, PemKeyFile, KeyPasswod, CAPemCertFileOrPath, SNIServerNameCallback);
- }
- return false;
- }
-
-
-
- public virtual void Uninitialize()
- {
- if (pServer != IntPtr.Zero)
- {
- SSLSdk.HP_SSLServer_CleanupSSLContext(pServer);
- }
- }
-
- public override void Destroy()
- {
- Stop();
- if (pServer != IntPtr.Zero)
- {
- SSLSdk.Destroy_HP_SSLServer(pServer);
- pServer = IntPtr.Zero;
- }
- if (pListener != IntPtr.Zero)
- {
- Sdk.Destroy_HP_TcpServerListener(pListener);
- pListener = IntPtr.Zero;
- }
- IsCreate = false;
- }
-
-
-
-
-
-
-
-
-
-
-
-
- public int AddServerContext(SSLVerifyMode verifyMode, string pemCertFile, string pemKeyFile, string keyPasswod, string caPemCertFileOrPath)
- {
- if (string.IsNullOrWhiteSpace(pemCertFile))
- {
- throw new ArgumentException("参数无效", pemCertFile);
- }
- if (string.IsNullOrWhiteSpace(pemKeyFile))
- {
- throw new ArgumentException("参数无效", pemKeyFile);
- }
- keyPasswod = string.IsNullOrWhiteSpace(keyPasswod) ? null : keyPasswod;
- caPemCertFileOrPath = string.IsNullOrWhiteSpace(caPemCertFileOrPath) ? null : caPemCertFileOrPath;
- return SSLSdk.HP_SSLServer_AddSSLContext(pServer, verifyMode, pemCertFile, pemKeyFile, KeyPasswod, caPemCertFileOrPath);
- }
-
-
-
-
-
-
- public bool StartSSLHandShake(IntPtr connId)
- {
- return SSLSdk.HP_SSLServer_StartSSLHandShake(pServer, connId);
- }
-
-
-
- public bool AutoHandShake
- {
- get
- {
- return SSLSdk.HP_SSLServer_IsSSLAutoHandShake(pServer);
- }
- set
- {
- SSLSdk.HP_SSLServer_SetSSLAutoHandShake(pServer, value);
- }
- }
- }
- }
|