SSLClient.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. #pragma once
  24. #include "TcpClient.h"
  25. #include "SSLHelper.h"
  26. #ifdef _SSL_SUPPORT
  27. class CSSLClient : public CTcpClient
  28. {
  29. using __super = CTcpClient;
  30. public:
  31. using __super::Wait;
  32. public:
  33. virtual BOOL IsSecure() {return TRUE;}
  34. virtual BOOL SendPackets(const WSABUF pBuffers[], int iCount);
  35. virtual BOOL SetupSSLContext(int iVerifyMode = SSL_VM_NONE, LPCTSTR lpszPemCertFile = nullptr, LPCTSTR lpszPemKeyFile = nullptr, LPCTSTR lpszKeyPassword = nullptr, LPCTSTR lpszCAPemCertFileOrPath = nullptr)
  36. {return m_sslCtx.Initialize(SSL_SM_CLIENT, iVerifyMode, FALSE, (LPVOID)lpszPemCertFile, (LPVOID)lpszPemKeyFile, (LPVOID)lpszKeyPassword, (LPVOID)lpszCAPemCertFileOrPath, nullptr);}
  37. virtual BOOL SetupSSLContextByMemory(int iVerifyMode = SSL_VM_NONE, LPCSTR lpszPemCert = nullptr, LPCSTR lpszPemKey = nullptr, LPCSTR lpszKeyPassword = nullptr, LPCSTR lpszCAPemCert = nullptr)
  38. {return m_sslCtx.Initialize(SSL_SM_CLIENT, iVerifyMode, TRUE, (LPVOID)lpszPemCert, (LPVOID)lpszPemKey, (LPVOID)lpszKeyPassword, (LPVOID)lpszCAPemCert, nullptr);}
  39. virtual void CleanupSSLContext()
  40. {m_sslCtx.Cleanup();}
  41. virtual BOOL StartSSLHandShake();
  42. public:
  43. virtual void SetSSLAutoHandShake(BOOL bAutoHandShake) {ENSURE_HAS_STOPPED(); m_bSSLAutoHandShake = bAutoHandShake;}
  44. virtual void SetSSLCipherList (LPCTSTR lpszCipherList){ENSURE_HAS_STOPPED(); m_sslCtx.SetCipherList(lpszCipherList);}
  45. virtual BOOL IsSSLAutoHandShake () {return m_bSSLAutoHandShake;}
  46. virtual LPCTSTR GetSSLCipherList() {return m_sslCtx.GetCipherList();}
  47. virtual BOOL GetSSLSessionInfo(EnSSLSessionInfo enInfo, LPVOID* lppInfo);
  48. protected:
  49. virtual EnHandleResult FireConnect();
  50. virtual EnHandleResult FireReceive(const BYTE* pData, int iLength);
  51. virtual BOOL CheckParams();
  52. virtual void PrepareStart();
  53. virtual void Reset();
  54. virtual void OnWorkerThreadEnd(THR_ID dwThreadID);
  55. protected:
  56. virtual BOOL StartSSLHandShakeNoCheck();
  57. private:
  58. void DoSSLHandShake();
  59. private:
  60. friend EnHandleResult ProcessHandShake<>(CSSLClient* pThis, CSSLClient* pSocketObj, CSSLSession* pSession);
  61. friend EnHandleResult ProcessReceive<>(CSSLClient* pThis, CSSLClient* pSocketObj, CSSLSession* pSession, const BYTE* pData, int iLength);
  62. friend BOOL ProcessSend<>(CSSLClient* pThis, CSSLClient* pSocketObj, CSSLSession* pSession, const WSABUF * pBuffers, int iCount);
  63. public:
  64. CSSLClient(ITcpClientListener* pListener)
  65. : CTcpClient (pListener)
  66. , m_sslSession (m_itPool)
  67. , m_dwMainThreadID (0)
  68. , m_bSSLAutoHandShake (TRUE)
  69. {
  70. }
  71. virtual ~CSSLClient()
  72. {
  73. ENSURE_STOP();
  74. }
  75. private:
  76. THR_ID m_dwMainThreadID;
  77. BOOL m_bSSLAutoHandShake;
  78. CSSLContext m_sslCtx;
  79. CSSLSession m_sslSession;
  80. };
  81. #endif