SSLAgent.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * Copyright: JessMA Open Source (ldcsaa@gmail.com)
  3. *
  4. * Author : Bruce Liang
  5. * Website : https://github.com/ldcsaa
  6. * Project : https://github.com/ldcsaa/HP-Socket
  7. * Blog : http://www.cnblogs.com/ldcsaa
  8. * Wiki : http://www.oschina.net/p/hp-socket
  9. * QQ Group : 44636872, 75375912
  10. *
  11. * Licensed under the Apache License, Version 2.0 (the "License");
  12. * you may not use this file except in compliance with the License.
  13. * You may obtain a copy of the License at
  14. *
  15. * http://www.apache.org/licenses/LICENSE-2.0
  16. *
  17. * Unless required by applicable law or agreed to in writing, software
  18. * distributed under the License is distributed on an "AS IS" BASIS,
  19. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. * See the License for the specific language governing permissions and
  21. * limitations under the License.
  22. */
  23. #include "SSLAgent.h"
  24. #include "SSLHelper.h"
  25. #ifdef _SSL_SUPPORT
  26. BOOL CSSLAgent::CheckParams()
  27. {
  28. if(!m_sslCtx.IsValid())
  29. {
  30. SetLastError(SE_SSL_ENV_NOT_READY, __FUNCTION__, ERROR_NOT_READY);
  31. return FALSE;
  32. }
  33. return __super::CheckParams();
  34. }
  35. void CSSLAgent::PrepareStart()
  36. {
  37. __super::PrepareStart();
  38. m_sslPool.SetItemCapacity (GetSocketBufferSize());
  39. m_sslPool.SetItemPoolSize (GetFreeBufferObjPool());
  40. m_sslPool.SetItemPoolHold (GetFreeBufferObjHold());
  41. m_sslPool.SetSessionLockTime(GetFreeSocketObjLockTime());
  42. m_sslPool.SetSessionPoolSize(GetFreeSocketObjPool());
  43. m_sslPool.SetSessionPoolHold(GetFreeSocketObjHold());
  44. m_sslPool.Prepare();
  45. }
  46. void CSSLAgent::Reset()
  47. {
  48. m_sslPool.Clear();
  49. m_sslCtx.RemoveThreadLocalState();
  50. __super::Reset();
  51. }
  52. void CSSLAgent::OnWorkerThreadEnd(THR_ID dwThreadID)
  53. {
  54. m_sslCtx.RemoveThreadLocalState();
  55. __super::OnWorkerThreadEnd(dwThreadID);
  56. }
  57. void CSSLAgent::ReleaseGCSocketObj(BOOL bForce)
  58. {
  59. __super::ReleaseGCSocketObj(bForce);
  60. #ifdef USE_EXTERNAL_GC
  61. m_sslPool.ReleaseGCSession(bForce);
  62. #endif
  63. }
  64. BOOL CSSLAgent::SendPackets(CONNID dwConnID, const WSABUF pBuffers[], int iCount)
  65. {
  66. ASSERT(pBuffers && iCount > 0);
  67. TAgentSocketObj* pSocketObj = FindSocketObj(dwConnID);
  68. if(!TAgentSocketObj::IsValid(pSocketObj))
  69. {
  70. ::SetLastError(ERROR_OBJECT_NOT_FOUND);
  71. return FALSE;
  72. }
  73. CSSLSession* pSession = nullptr;
  74. GetConnectionReserved2(pSocketObj, (PVOID*)&pSession);
  75. if(pSession != nullptr)
  76. {
  77. CLocalSafeCounter localcounter(*pSession);
  78. return ::ProcessSend(this, pSocketObj, pSession, pBuffers, iCount);
  79. }
  80. return DoSendPackets(pSocketObj, pBuffers, iCount);
  81. }
  82. EnHandleResult CSSLAgent::FireConnect(TAgentSocketObj* pSocketObj)
  83. {
  84. EnHandleResult result = DoFireConnect(pSocketObj);
  85. if(result != HR_ERROR && m_bSSLAutoHandShake)
  86. DoSSLHandShake(pSocketObj);
  87. return result;
  88. }
  89. EnHandleResult CSSLAgent::FireReceive(TAgentSocketObj* pSocketObj, const BYTE* pData, int iLength)
  90. {
  91. CSSLSession* pSession = nullptr;
  92. GetConnectionReserved2(pSocketObj, (PVOID*)&pSession);
  93. if(pSession != nullptr)
  94. {
  95. CLocalSafeCounter localcounter(*pSession);
  96. return ::ProcessReceive(this, pSocketObj, pSession, pData, iLength);
  97. }
  98. return DoFireReceive(pSocketObj, pData, iLength);
  99. }
  100. EnHandleResult CSSLAgent::FireClose(TAgentSocketObj* pSocketObj, EnSocketOperation enOperation, int iErrorCode)
  101. {
  102. EnHandleResult result = DoFireClose(pSocketObj, enOperation, iErrorCode);
  103. CSSLSession* pSession = nullptr;
  104. GetConnectionReserved2(pSocketObj, (PVOID*)&pSession);
  105. if(pSession != nullptr)
  106. m_sslPool.PutFreeSession(pSession);
  107. return result;
  108. }
  109. BOOL CSSLAgent::StartSSLHandShake(CONNID dwConnID)
  110. {
  111. if(IsSSLAutoHandShake())
  112. {
  113. ::SetLastError(ERROR_INVALID_OPERATION);
  114. return FALSE;
  115. }
  116. TAgentSocketObj* pSocketObj = FindSocketObj(dwConnID);
  117. if(!TAgentSocketObj::IsValid(pSocketObj))
  118. {
  119. ::SetLastError(ERROR_OBJECT_NOT_FOUND);
  120. return FALSE;
  121. }
  122. return StartSSLHandShake(pSocketObj);
  123. }
  124. BOOL CSSLAgent::StartSSLHandShake(TAgentSocketObj* pSocketObj)
  125. {
  126. if(!pSocketObj->HasConnected())
  127. {
  128. ::SetLastError(ERROR_INVALID_STATE);
  129. return FALSE;
  130. }
  131. CReentrantCriSecLock locallock(pSocketObj->csSend);
  132. if(!TAgentSocketObj::IsValid(pSocketObj))
  133. {
  134. ::SetLastError(ERROR_OBJECT_NOT_FOUND);
  135. return FALSE;
  136. }
  137. if(!pSocketObj->HasConnected())
  138. {
  139. ::SetLastError(ERROR_INVALID_STATE);
  140. return FALSE;
  141. }
  142. CSSLSession* pSession = nullptr;
  143. GetConnectionReserved2(pSocketObj, (PVOID*)&pSession);
  144. if(pSession != nullptr)
  145. {
  146. ::SetLastError(ERROR_ALREADY_INITIALIZED);
  147. return FALSE;
  148. }
  149. DoSSLHandShake(pSocketObj);
  150. return TRUE;
  151. }
  152. void CSSLAgent::DoSSLHandShake(TAgentSocketObj* pSocketObj)
  153. {
  154. CSSLSession* pSession = m_sslPool.PickFreeSession(pSocketObj->host);
  155. CLocalSafeCounter localcounter(*pSession);
  156. ENSURE(SetConnectionReserved2(pSocketObj, pSession));
  157. ENSURE(::ProcessHandShake(this, pSocketObj, pSession) == HR_OK);
  158. }
  159. BOOL CSSLAgent::GetSSLSessionInfo(CONNID dwConnID, EnSSLSessionInfo enInfo, LPVOID* lppInfo)
  160. {
  161. ASSERT(lppInfo != nullptr);
  162. *lppInfo = nullptr;
  163. TAgentSocketObj* pSocketObj = FindSocketObj(dwConnID);
  164. if(!TAgentSocketObj::IsValid(pSocketObj))
  165. {
  166. ::SetLastError(ERROR_OBJECT_NOT_FOUND);
  167. return FALSE;
  168. }
  169. CSSLSession* pSession = nullptr;
  170. GetConnectionReserved2(pSocketObj, (PVOID*)&pSession);
  171. if(pSession == nullptr || !pSession->IsValid())
  172. {
  173. ::SetLastError(ERROR_INVALID_STATE);
  174. return FALSE;
  175. }
  176. return pSession->GetSessionInfo(enInfo, lppInfo);
  177. }
  178. #endif