SSLClient.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 "SSLClient.h"
  24. #include "SSLHelper.h"
  25. #ifdef _SSL_SUPPORT
  26. BOOL CSSLClient::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 CSSLClient::PrepareStart()
  36. {
  37. m_dwMainThreadID = SELF_THREAD_ID;
  38. __super::PrepareStart();
  39. }
  40. void CSSLClient::Reset()
  41. {
  42. m_sslSession.Reset();
  43. if(m_dwMainThreadID != 0)
  44. {
  45. m_sslCtx.RemoveThreadLocalState(m_dwMainThreadID);
  46. m_dwMainThreadID = 0;
  47. }
  48. __super::Reset();
  49. }
  50. void CSSLClient::OnWorkerThreadEnd(THR_ID dwThreadID)
  51. {
  52. m_sslCtx.RemoveThreadLocalState();
  53. __super::OnWorkerThreadEnd(dwThreadID);
  54. }
  55. BOOL CSSLClient::SendPackets(const WSABUF pBuffers[], int iCount)
  56. {
  57. ASSERT(pBuffers && iCount > 0);
  58. if(m_sslSession.IsValid())
  59. return ::ProcessSend(this, this, &m_sslSession, pBuffers, iCount);
  60. else
  61. return DoSendPackets(this, pBuffers, iCount);
  62. }
  63. EnHandleResult CSSLClient::FireConnect()
  64. {
  65. EnHandleResult result = DoFireConnect(this);
  66. if(result != HR_ERROR && m_bSSLAutoHandShake)
  67. DoSSLHandShake();
  68. return result;
  69. }
  70. EnHandleResult CSSLClient::FireReceive(const BYTE* pData, int iLength)
  71. {
  72. if(m_sslSession.IsValid())
  73. return ::ProcessReceive(this, this, &m_sslSession, pData, iLength);
  74. else
  75. return DoFireReceive(this, pData, iLength);
  76. }
  77. BOOL CSSLClient::StartSSLHandShake()
  78. {
  79. if(IsSSLAutoHandShake())
  80. {
  81. ::SetLastError(ERROR_INVALID_OPERATION);
  82. return FALSE;
  83. }
  84. return StartSSLHandShakeNoCheck();
  85. }
  86. BOOL CSSLClient::StartSSLHandShakeNoCheck()
  87. {
  88. if(!IsConnected())
  89. {
  90. ::SetLastError(ERROR_INVALID_STATE);
  91. return FALSE;
  92. }
  93. CCriSecLock locallock(m_sslSession.GetSendLock());
  94. if(!IsConnected())
  95. {
  96. ::SetLastError(ERROR_INVALID_STATE);
  97. return FALSE;
  98. }
  99. if(m_sslSession.IsValid())
  100. {
  101. ::SetLastError(ERROR_ALREADY_INITIALIZED);
  102. return FALSE;
  103. }
  104. DoSSLHandShake();
  105. return TRUE;
  106. }
  107. void CSSLClient::DoSSLHandShake()
  108. {
  109. m_sslSession.Renew(m_sslCtx, m_strHost);
  110. ENSURE(::ProcessHandShake(this, this, &m_sslSession) == HR_OK);
  111. }
  112. BOOL CSSLClient::GetSSLSessionInfo(EnSSLSessionInfo enInfo, LPVOID* lppInfo)
  113. {
  114. if(!m_sslSession.IsValid())
  115. {
  116. ::SetLastError(ERROR_INVALID_STATE);
  117. return FALSE;
  118. }
  119. return m_sslSession.GetSessionInfo(enInfo, lppInfo);
  120. }
  121. #endif