开源的socket服务端客户端,支持C# C++

SSLHelper.h 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /*
  2. * Copyright: JessMA Open Source (ldcsaa@gmail.com)
  3. *
  4. * Version : 4.1.3
  5. * Author : Bruce Liang
  6. * Website : http://www.jessma.org
  7. * Project : https://github.com/ldcsaa
  8. * Blog : http://www.cnblogs.com/ldcsaa
  9. * Wiki : http://www.oschina.net/p/hp-socket
  10. * QQ Group : 75375912
  11. *
  12. * Licensed under the Apache License, Version 2.0 (the "License");
  13. * you may not use this file except in compliance with the License.
  14. * You may obtain a copy of the License at
  15. *
  16. * http://www.apache.org/licenses/LICENSE-2.0
  17. *
  18. * Unless required by applicable law or agreed to in writing, software
  19. * distributed under the License is distributed on an "AS IS" BASIS,
  20. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  21. * See the License for the specific language governing permissions and
  22. * limitations under the License.
  23. */
  24. #pragma once
  25. #pragma warning(push)
  26. #pragma warning(disable: 4005)
  27. #include "openssl/ssl.h"
  28. #pragma warning(pop)
  29. #include "HPTypeDef.h"
  30. #include "../../Common/Src/bufferpool.h"
  31. #define OPENSSL_VERSION_1_1_0 0x10100000L
  32. /************************************************************************
  33. 名称:SSL 握手状态
  34. 描述:标识当前连接的 SSL 握手状态
  35. ************************************************************************/
  36. enum EnSSLHandShakeStatus
  37. {
  38. SSL_HSS_INIT = 0, // 初始状态
  39. SSL_HSS_PROC = 1, // 正在握手
  40. SSL_HSS_SUCC = 2, // 握手成功
  41. };
  42. #if OPENSSL_VERSION_NUMBER < OPENSSL_VERSION_1_1_0
  43. /* SSL CRYPTO DYNLOCK 结构 */
  44. typedef struct CRYPTO_dynlock_value
  45. {
  46. CSimpleRWLock cs;
  47. } DynamicLock;
  48. #endif
  49. /************************************************************************
  50. 名称:SSL Context
  51. 描述:初始化和清理 SSL 全局运行环境
  52. ************************************************************************/
  53. class CSSLContext
  54. {
  55. public:
  56. /* 获取 CSSLContext 单例对象 */
  57. static CSSLContext& getInstance() {return sm_Instance;}
  58. public:
  59. /*
  60. * 名称:初始化 SSL 全局环境参数
  61. * 描述:SSL 全局环境参数必须在 SSL 通信组件启动前完成初始化,否则启动失败
  62. *
  63. * 参数: enSessionMode -- SSL 工作模式(参考 EnSSLSessionMode)
  64. * iVerifyMode -- SSL 验证模式(参考 EnSSLVerifyMode)
  65. * lpszPemCertFile -- 证书文件(客户端可选)
  66. * lpszPemKeyFile -- 私钥文件(客户端可选)
  67. * lpszKeyPasswod -- 私钥密码(没有密码则为空)
  68. * lpszCAPemCertFileOrPath -- CA 证书文件或目录(单向验证或客户端可选)
  69. * fnServerNameCallback -- SNI 回调函数指针(可选,只用于服务端)
  70. *
  71. * 返回值: TRUE -- 成功
  72. * FALSE -- 失败,可通过 ::GetLastError() 获取失败原因
  73. */
  74. BOOL Initialize(EnSSLSessionMode enSessionMode, int iVerifyMode = SSL_VM_NONE, LPCTSTR lpszPemCertFile = nullptr, LPCTSTR lpszPemKeyFile = nullptr, LPCTSTR lpszKeyPasswod = nullptr, LPCTSTR lpszCAPemCertFileOrPath = nullptr, Fn_SNI_ServerNameCallback fnServerNameCallback = nullptr);
  75. /*
  76. * 名称:增加 SNI 主机证书(只用于服务端)
  77. * 描述:SSL 服务端在 Initialize() 成功后可以调用本方法增加多个 SNI 主机证书
  78. *
  79. * 参数: iVerifyMode -- SSL 验证模式(参考 EnSSLVerifyMode)
  80. * lpszPemCertFile -- 证书文件
  81. * lpszPemKeyFile -- 私钥文件
  82. * lpszKeyPasswod -- 私钥密码(没有密码则为空)
  83. * lpszCAPemCertFileOrPath -- CA 证书文件或目录(单向验证可选)
  84. *
  85. * 返回值: 正数 -- 成功,并返回 SNI 主机证书对应的索引,该索引用于在 SNI 回调函数中定位 SNI 主机
  86. * 负数 -- 失败,可通过 ::GetLastError() 获取失败原因
  87. */
  88. int AddServerContext(int iVerifyMode, LPCTSTR lpszPemCertFile, LPCTSTR lpszPemKeyFile, LPCTSTR lpszKeyPasswod = nullptr, LPCTSTR lpszCAPemCertFileOrPath = nullptr);
  89. /*
  90. * 名称:清理 SSL 全局运行环境
  91. * 描述:清理 SSL 全局运行环境,回收 SSL 相关内存
  92. * 1、CSSLContext 的析构函数会自动调用本方法
  93. * 2、当要重新设置 SSL 全局环境参数时,需要先调用本方法清理原先的环境参数
  94. *
  95. * 参数: 无
  96. *
  97. * 返回值:无
  98. */
  99. void Cleanup();
  100. /*
  101. * 名称:清理线程局部环境 SSL 资源
  102. * 描述:任何一个操作 SSL 的线程,在通信结束时都需要清理线程局部环境 SSL 资源
  103. * 1、主线程和 HP-Socket 工作线程在通信结束时会自动清理线程局部环境 SSL 资源。因此,一般情况下不必手工调用本方法
  104. * 2、特殊情况下,当自定义线程参与 HP-Socket 通信操作并检查到 SSL 内存泄漏时,需在每次通信结束时自定义线程调用本方法
  105. *
  106. * 参数: dwThreadID -- 线程 ID(0:当前线程)
  107. *
  108. * 返回值:无
  109. */
  110. void RemoveThreadLocalState(DWORD dwThreadID = 0) {CleanupThreadState(dwThreadID);}
  111. /* 获取 SSL 全局运行环境 SSL_CTX 对象 */
  112. SSL_CTX* GetContext (int i) const;
  113. /* 获取 SSL 全局运行环境默认 SSL_CTX 对象 */
  114. SSL_CTX* GetDefaultContext () const {return m_sslCtx;}
  115. /* 获取 SSL 全局运行环境的配置模式,配置模式参考:EnSSLSessionMode */
  116. EnSSLSessionMode GetSessionMode () const {return m_enSessionMode;}
  117. /* 检查 SSL 全局运行环境是否初始化完成 */
  118. BOOL IsValid () const {return m_sslCtx != nullptr;}
  119. private:
  120. CSSLContext()
  121. : m_enSessionMode (SSL_SM_SERVER)
  122. , m_sslCtx (nullptr)
  123. , m_fnServerNameCallback(nullptr)
  124. #if OPENSSL_VERSION_NUMBER < OPENSSL_VERSION_1_1_0
  125. , m_iLockNum (0)
  126. , m_pcsLocks (nullptr)
  127. #endif
  128. {
  129. }
  130. ~CSSLContext() {Cleanup();}
  131. private:
  132. void Setup();
  133. void SetServerNameCallback(Fn_SNI_ServerNameCallback fn);
  134. int AddContext(int iVerifyMode, LPCTSTR lpszPemCertFile, LPCTSTR lpszPemKeyFile, LPCTSTR lpszKeyPasswod, LPCTSTR lpszCAPemCertFileOrPath);
  135. BOOL LoadCertAndKey(SSL_CTX* sslCtx, int iVerifyMode, LPCTSTR lpszPemCertFile, LPCTSTR lpszPemKeyFile, LPCTSTR lpszKeyPasswod, LPCTSTR lpszCAPemCertFileOrPath);
  136. private:
  137. static void CleanupGlobalState();
  138. static void CleanupThreadState(DWORD dwThreadID = 0);
  139. static int CALLBACK InternalServerNameCallback(SSL* ssl, int* ad, void* arg);
  140. #if OPENSSL_VERSION_NUMBER < OPENSSL_VERSION_1_1_0
  141. static void ssl_lock_callback(int mode, int n, const char *file, int line);
  142. static CRYPTO_dynlock_value* ssl_lock_dyn_create_callback(const char *file, int line);
  143. static void ssl_lock_dyn_callback(int mode, CRYPTO_dynlock_value* l, const char *file, int line);
  144. static void ssl_lock_dyn_destroy_callback(CRYPTO_dynlock_value* l, const char *file, int line);
  145. #endif
  146. private:
  147. EnSSLSessionMode m_enSessionMode;
  148. vector<SSL_CTX*> m_lsSslCtxs;
  149. SSL_CTX* m_sslCtx;
  150. Fn_SNI_ServerNameCallback m_fnServerNameCallback;
  151. #if OPENSSL_VERSION_NUMBER < OPENSSL_VERSION_1_1_0
  152. int m_iLockNum;
  153. CSimpleRWLock* m_pcsLocks;
  154. #endif
  155. private:
  156. static CSSLContext sm_Instance;
  157. };
  158. extern CSSLContext& g_SSL;
  159. class CSSLSession
  160. {
  161. public:
  162. BOOL WriteRecvChannel(const BYTE* pData, int iLength);
  163. BOOL ReadRecvChannel();
  164. BOOL WriteSendChannel(const BYTE* pData, int iLength);
  165. BOOL WriteSendChannel(const WSABUF pBuffers[], int iCount);
  166. BOOL ReadSendChannel();
  167. const WSABUF& GetRecvBuffer() const {return m_bufRecv;}
  168. const WSABUF& GetSendBuffer() const {return m_bufSend;}
  169. CSSLSession* Renew(LPCSTR lpszHostName = nullptr);
  170. BOOL Reset();
  171. BOOL IsValid() const {return GetStatus() != SSL_HSS_INIT;}
  172. BOOL IsHandShaking() const {return GetStatus() == SSL_HSS_PROC;}
  173. BOOL IsReady() const {return GetStatus() == SSL_HSS_SUCC;}
  174. EnSSLHandShakeStatus GetStatus() const {return m_enStatus;}
  175. DWORD GetFreeTime() const {return m_dwFreeTime;}
  176. CCriSec& GetSendLock() {return m_csSend;}
  177. private:
  178. BOOL IsFatalError(int iBytes);
  179. public:
  180. CSSLSession(CItemPool& itPool)
  181. : m_enStatus(SSL_HSS_INIT)
  182. , m_itPool (itPool)
  183. , m_ssl (nullptr)
  184. , m_bioSend (nullptr)
  185. , m_bioRecv (nullptr)
  186. , m_pitSend (nullptr)
  187. , m_pitRecv (nullptr)
  188. {
  189. }
  190. ~CSSLSession()
  191. {
  192. Reset();
  193. }
  194. private:
  195. CItemPool& m_itPool;
  196. CCriSec m_csSend;
  197. DWORD m_dwFreeTime;
  198. EnSSLHandShakeStatus m_enStatus;
  199. SSL* m_ssl;
  200. BIO* m_bioSend;
  201. BIO* m_bioRecv;
  202. TItem* m_pitSend;
  203. TItem* m_pitRecv;
  204. WSABUF m_bufSend;
  205. WSABUF m_bufRecv;
  206. };
  207. class CSSLSessionPool
  208. {
  209. typedef CRingPool<CSSLSession> TSSLSessionList;
  210. typedef CCASQueue<CSSLSession> TSSLSessionQueue;
  211. public:
  212. CSSLSession* PickFreeSession (LPCSTR lpszHostName = nullptr);
  213. void PutFreeSession (CSSLSession* pSession);
  214. void Prepare ();
  215. void Clear ();
  216. private:
  217. void ReleaseGCSession (BOOL bForce = FALSE);
  218. public:
  219. void SetItemCapacity (DWORD dwItemCapacity) {m_itPool.SetItemCapacity(dwItemCapacity);}
  220. void SetItemPoolSize (DWORD dwItemPoolSize) {m_itPool.SetPoolSize(dwItemPoolSize);}
  221. void SetItemPoolHold (DWORD dwItemPoolHold) {m_itPool.SetPoolHold(dwItemPoolHold);}
  222. void SetSessionLockTime (DWORD dwSessionLockTime) {m_dwSessionLockTime = dwSessionLockTime;}
  223. void SetSessionPoolSize (DWORD dwSessionPoolSize) {m_dwSessionPoolSize = dwSessionPoolSize;}
  224. void SetSessionPoolHold (DWORD dwSessionPoolHold) {m_dwSessionPoolHold = dwSessionPoolHold;}
  225. DWORD GetItemCapacity () {return m_itPool.GetItemCapacity();}
  226. DWORD GetItemPoolSize () {return m_itPool.GetPoolSize();}
  227. DWORD GetItemPoolHold () {return m_itPool.GetPoolHold();}
  228. DWORD GetSessionLockTime() {return m_dwSessionLockTime;}
  229. DWORD GetSessionPoolSize() {return m_dwSessionPoolSize;}
  230. DWORD GetSessionPoolHold() {return m_dwSessionPoolHold;}
  231. public:
  232. CSSLSessionPool(DWORD dwPoolSize = DEFAULT_SESSION_POOL_SIZE,
  233. DWORD dwPoolHold = DEFAULT_SESSION_POOL_HOLD,
  234. DWORD dwLockTime = DEFAULT_SESSION_LOCK_TIME)
  235. : m_dwSessionPoolSize(dwPoolSize)
  236. , m_dwSessionPoolHold(dwPoolHold)
  237. , m_dwSessionLockTime(dwLockTime)
  238. {
  239. }
  240. ~CSSLSessionPool() {Clear();}
  241. DECLARE_NO_COPY_CLASS(CSSLSessionPool)
  242. public:
  243. static const DWORD DEFAULT_ITEM_CAPACITY;
  244. static const DWORD DEFAULT_ITEM_POOL_SIZE;
  245. static const DWORD DEFAULT_ITEM_POOL_HOLD;
  246. static const DWORD DEFAULT_SESSION_LOCK_TIME;
  247. static const DWORD DEFAULT_SESSION_POOL_SIZE;
  248. static const DWORD DEFAULT_SESSION_POOL_HOLD;
  249. private:
  250. CItemPool m_itPool;
  251. DWORD m_dwSessionLockTime;
  252. DWORD m_dwSessionPoolSize;
  253. DWORD m_dwSessionPoolHold;
  254. TSSLSessionList m_lsFreeSession;
  255. TSSLSessionQueue m_lsGCSession;
  256. };
  257. template<class T, class S> EnHandleResult ProcessHandShake(T* pThis, S* pSocketObj, CSSLSession* pSession)
  258. {
  259. EnHandleResult result = HR_OK;
  260. CCriSecLock locallock(pSession->GetSendLock());
  261. while(TRUE)
  262. {
  263. VERIFY(pSession->ReadSendChannel());
  264. const WSABUF& buffer = pSession->GetSendBuffer();
  265. if(buffer.len == 0)
  266. break;
  267. if(!pThis->DoSendPackets(pSocketObj, &buffer, 1))
  268. {
  269. result = HR_ERROR;
  270. break;
  271. }
  272. }
  273. return result;
  274. }
  275. template<class T, class S> EnHandleResult ProcessReceive(T* pThis, S* pSocketObj, CSSLSession* pSession, const BYTE* pData, int iLength)
  276. {
  277. if(!pSession->WriteRecvChannel(pData, iLength))
  278. return HR_ERROR;
  279. EnHandleResult result = HR_OK;
  280. EnSSLHandShakeStatus enStatus = pSession->GetStatus();
  281. while(TRUE)
  282. {
  283. if(!pSession->ReadRecvChannel())
  284. return HR_ERROR;
  285. if(enStatus == SSL_HSS_PROC && pSession->IsReady())
  286. {
  287. result = ProcessHandShake(pThis, pSocketObj, pSession);
  288. if(result == HR_ERROR)
  289. break;
  290. enStatus = SSL_HSS_SUCC;
  291. result = pThis->DoFireHandShake(pSocketObj);
  292. if(result == HR_ERROR)
  293. break;
  294. }
  295. const WSABUF& buffer = pSession->GetRecvBuffer();
  296. if(buffer.len == 0)
  297. break;
  298. result = pThis->DoFireReceive(pSocketObj, (const BYTE*)buffer.buf, buffer.len);
  299. if(result == HR_ERROR)
  300. break;
  301. }
  302. if(result != HR_ERROR && pSession->IsHandShaking())
  303. result = ::ProcessHandShake(pThis, pSocketObj, pSession);
  304. return result;
  305. }
  306. template<class T, class S> BOOL ProcessSend(T* pThis, S* pSocketObj, CSSLSession* pSession, const WSABUF * pBuffers, int iCount)
  307. {
  308. if(pSession == nullptr || !pSession->IsReady())
  309. {
  310. ::SetLastError(ERROR_INVALID_STATE);
  311. return FALSE;
  312. }
  313. CCriSecLock locallock(pSession->GetSendLock());
  314. if(!pSession->IsReady())
  315. {
  316. ::SetLastError(ERROR_INVALID_STATE);
  317. return FALSE;
  318. }
  319. VERIFY(pSession->WriteSendChannel(pBuffers, iCount));
  320. while(TRUE)
  321. {
  322. VERIFY(pSession->ReadSendChannel());
  323. const WSABUF& buffer = pSession->GetSendBuffer();
  324. if(buffer.len == 0)
  325. break;
  326. if(!pThis->DoSendPackets(pSocketObj, &buffer, 1))
  327. return FALSE;
  328. }
  329. return TRUE;
  330. }