SSLAgent.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 "TcpAgent.h"
  25. #include "SSLHelper.h"
  26. #ifdef _SSL_SUPPORT
  27. class CSSLAgent : public CTcpAgent
  28. {
  29. using __super = CTcpAgent;
  30. public:
  31. using __super::Wait;
  32. public:
  33. virtual BOOL IsSecure() {return TRUE;}
  34. virtual BOOL SendPackets(CONNID dwConnID, 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(CONNID dwConnID);
  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(CONNID dwConnID, EnSSLSessionInfo enInfo, LPVOID* lppInfo);
  48. protected:
  49. virtual EnHandleResult FireConnect(TAgentSocketObj* pSocketObj);
  50. virtual EnHandleResult FireReceive(TAgentSocketObj* pSocketObj, const BYTE* pData, int iLength);
  51. virtual EnHandleResult FireClose(TAgentSocketObj* pSocketObj, EnSocketOperation enOperation, int iErrorCode);
  52. virtual BOOL CheckParams();
  53. virtual void PrepareStart();
  54. virtual void Reset();
  55. virtual void OnWorkerThreadEnd(THR_ID dwThreadID);
  56. virtual void ReleaseGCSocketObj(BOOL bForce = FALSE);
  57. protected:
  58. virtual BOOL StartSSLHandShake(TAgentSocketObj* pSocketObj);
  59. private:
  60. void DoSSLHandShake(TAgentSocketObj* pSocketObj);
  61. private:
  62. friend EnHandleResult ProcessHandShake<>(CSSLAgent* pThis, TAgentSocketObj* pSocketObj, CSSLSession* pSession);
  63. friend EnHandleResult ProcessReceive<>(CSSLAgent* pThis, TAgentSocketObj* pSocketObj, CSSLSession* pSession, const BYTE* pData, int iLength);
  64. friend BOOL ProcessSend<>(CSSLAgent* pThis, TAgentSocketObj* pSocketObj, CSSLSession* pSession, const WSABUF * pBuffers, int iCount);
  65. public:
  66. CSSLAgent(ITcpAgentListener* pListener)
  67. : CTcpAgent(pListener)
  68. , m_sslPool(m_sslCtx)
  69. , m_bSSLAutoHandShake(TRUE)
  70. {
  71. }
  72. virtual ~CSSLAgent()
  73. {
  74. ENSURE_STOP();
  75. }
  76. private:
  77. BOOL m_bSSLAutoHandShake;
  78. CSSLContext m_sslCtx;
  79. CSSLSessionPool m_sslPool;
  80. };
  81. #endif