SSLServer.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 "SSLServer.h"
  24. #include "SSLHelper.h"
  25. #ifdef _SSL_SUPPORT
  26. BOOL CSSLServer::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 CSSLServer::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 CSSLServer::Reset()
  47. {
  48. m_sslPool.Clear();
  49. m_sslCtx.RemoveThreadLocalState();
  50. __super::Reset();
  51. }
  52. void CSSLServer::OnWorkerThreadEnd(THR_ID dwThreadID)
  53. {
  54. m_sslCtx.RemoveThreadLocalState();
  55. __super::OnWorkerThreadEnd(dwThreadID);
  56. }
  57. BOOL CSSLServer::SendPackets(CONNID dwConnID, const WSABUF pBuffers[], int iCount)
  58. {
  59. ASSERT(pBuffers && iCount > 0);
  60. TSocketObj* pSocketObj = FindSocketObj(dwConnID);
  61. if(!TSocketObj::IsValid(pSocketObj))
  62. {
  63. ::SetLastError(ERROR_OBJECT_NOT_FOUND);
  64. return FALSE;
  65. }
  66. CSSLSession* pSession = nullptr;
  67. GetConnectionReserved2(pSocketObj, (PVOID*)&pSession);
  68. if(pSession != nullptr)
  69. {
  70. CLocalSafeCounter localcounter(*pSession);
  71. return ::ProcessSend(this, pSocketObj, pSession, pBuffers, iCount);
  72. }
  73. return DoSendPackets(pSocketObj, pBuffers, iCount);
  74. }
  75. EnHandleResult CSSLServer::FireAccept(TSocketObj* pSocketObj)
  76. {
  77. EnHandleResult result = DoFireAccept(pSocketObj);
  78. if(result != HR_ERROR && m_bSSLAutoHandShake)
  79. DoSSLHandShake(pSocketObj);
  80. return result;
  81. }
  82. EnHandleResult CSSLServer::FireReceive(TSocketObj* pSocketObj, const BYTE* pData, int iLength)
  83. {
  84. CSSLSession* pSession = nullptr;
  85. GetConnectionReserved2(pSocketObj, (PVOID*)&pSession);
  86. if(pSession != nullptr)
  87. {
  88. CLocalSafeCounter localcounter(*pSession);
  89. return ::ProcessReceive(this, pSocketObj, pSession, pData, iLength);
  90. }
  91. return DoFireReceive(pSocketObj, pData, iLength);
  92. }
  93. EnHandleResult CSSLServer::FireClose(TSocketObj* pSocketObj, EnSocketOperation enOperation, int iErrorCode)
  94. {
  95. EnHandleResult result = DoFireClose(pSocketObj, enOperation, iErrorCode);
  96. CSSLSession* pSession = nullptr;
  97. GetConnectionReserved2(pSocketObj, (PVOID*)&pSession);
  98. if(pSession != nullptr)
  99. m_sslPool.PutFreeSession(pSession);
  100. return result;
  101. }
  102. BOOL CSSLServer::StartSSLHandShake(CONNID dwConnID)
  103. {
  104. if(IsSSLAutoHandShake())
  105. {
  106. ::SetLastError(ERROR_INVALID_OPERATION);
  107. return FALSE;
  108. }
  109. TSocketObj* pSocketObj = FindSocketObj(dwConnID);
  110. if(!TSocketObj::IsValid(pSocketObj))
  111. {
  112. ::SetLastError(ERROR_OBJECT_NOT_FOUND);
  113. return FALSE;
  114. }
  115. return StartSSLHandShake(pSocketObj);
  116. }
  117. BOOL CSSLServer::StartSSLHandShake(TSocketObj* pSocketObj)
  118. {
  119. if(!pSocketObj->HasConnected())
  120. {
  121. ::SetLastError(ERROR_INVALID_STATE);
  122. return FALSE;
  123. }
  124. CReentrantCriSecLock locallock(pSocketObj->csSend);
  125. if(!TSocketObj::IsValid(pSocketObj))
  126. {
  127. ::SetLastError(ERROR_OBJECT_NOT_FOUND);
  128. return FALSE;
  129. }
  130. if(!pSocketObj->HasConnected())
  131. {
  132. ::SetLastError(ERROR_INVALID_STATE);
  133. return FALSE;
  134. }
  135. CSSLSession* pSession = nullptr;
  136. GetConnectionReserved2(pSocketObj, (PVOID*)&pSession);
  137. if(pSession != nullptr)
  138. {
  139. ::SetLastError(ERROR_ALREADY_INITIALIZED);
  140. return FALSE;
  141. }
  142. DoSSLHandShake(pSocketObj);
  143. return TRUE;
  144. }
  145. void CSSLServer::DoSSLHandShake(TSocketObj* pSocketObj)
  146. {
  147. CSSLSession* pSession = m_sslPool.PickFreeSession();
  148. CLocalSafeCounter localcounter(*pSession);
  149. ENSURE(SetConnectionReserved2(pSocketObj, pSession));
  150. ENSURE(::ProcessHandShake(this, pSocketObj, pSession) == HR_OK);
  151. }
  152. BOOL CSSLServer::GetSSLSessionInfo(CONNID dwConnID, EnSSLSessionInfo enInfo, LPVOID* lppInfo)
  153. {
  154. ASSERT(lppInfo != nullptr);
  155. *lppInfo = nullptr;
  156. TSocketObj* pSocketObj = FindSocketObj(dwConnID);
  157. if(!TSocketObj::IsValid(pSocketObj))
  158. {
  159. ::SetLastError(ERROR_OBJECT_NOT_FOUND);
  160. return FALSE;
  161. }
  162. CSSLSession* pSession = nullptr;
  163. GetConnectionReserved2(pSocketObj, (PVOID*)&pSession);
  164. if(pSession == nullptr || !pSession->IsValid())
  165. {
  166. ::SetLastError(ERROR_INVALID_STATE);
  167. return FALSE;
  168. }
  169. return pSession->GetSessionInfo(enInfo, lppInfo);
  170. }
  171. #endif