1
0

AsyncSslSocketLayer.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /* CAsyncSslSocketLayer by Tim Kosse
  2. mailto: [email protected])
  3. Version 2.0 (2005-02-27)
  4. -------------------------------------------------------------
  5. Introduction
  6. ------------
  7. CAsyncSslSocketLayer is a layer class for CAsyncSocketEx which allows you to establish SSL secured
  8. connections. Support for both client and server side is provided.
  9. How to use
  10. ----------
  11. Using this class is really simple. In the easiest case, just add an instance of
  12. CAsyncSslSocketLayer to your socket and call InitClientSsl after creation of the socket.
  13. This class only has a couple of public functions:
  14. - int InitSSLConnection(bool clientMode);
  15. This functions establishes an SSL connection. The clientMode parameter specifies whether the SSL connection
  16. is in server or in client mode.
  17. Most likely you want to call this function right after calling Create for the socket.
  18. But sometimes, you'll need to call this function later. One example is for an FTP connection
  19. with explicit SSL: In this case you would have to call InitSSLConnection after receiving the reply
  20. to an 'AUTH SSL' command.
  21. InitSSLConnection returns 0 on success, else an error code as described below under SSL_FAILURE
  22. - Is UsingSSL();
  23. Returns true if you've previously called InitClientSsl()
  24. - SetNotifyReply(SetNotifyReply(int nID, int nCode, int result);
  25. You can call this function only after receiving a layerspecific callback with the SSL_VERIFY_CERT
  26. id. Set result to 1 if you trust the certificate and 0 if you don't trust it.
  27. nID has to be the priv_data element of the t_SslCertData structure and nCode has to be SSL_VERIFY_CERT.
  28. This layer sends some layerspecific notifications to your socket instance, you can handle them in
  29. OnLayerCallback of your socket class.
  30. Valid notification IDs are:
  31. - SSL_INFO 0
  32. There are two possible values for param2:
  33. SSL_INFO_ESTABLISHED 0 - You'll get this notification if the SSL negotiation was successful
  34. SSL_INFO_SHUTDOWNCOMPLETE 1 - You'll get this notification if the SSL connection has been shut
  35. down successfully. See below for details.
  36. - SSL_FAILURE 1
  37. This notification is sent if the SSL connection could not be established or if an existing
  38. connection failed. Valid values for param2 are:
  39. - SSL_FAILURE_UNKNOWN 0 - Details may have been sent with a SSL_VERBOSE_* notification.
  40. - SSL_FAILURE_ESTABLISH 1 - Problem during SSL negotiation
  41. - SSL_FAILURE_INITSSL 4
  42. - SSL_FAILURE_VERIFYCERT 8 - The remote SSL certificate was invalid
  43. - SSL_FAILURE_CERTREJECTED 16 - The remote SSL certificate was rejected by user
  44. - SSL_VERIFY_CERT 2
  45. This notification is sent each time a remote certificate has to be verified.
  46. param2 is a pointer to a t_SslCertData structure which contains some information
  47. about the remote certificate.
  48. You have to set the reply to this message using the SetNotifyReply function.
  49. Be careful with closing the connection after sending data, not all data may have been sent already.
  50. Before closing the connection, you should call Shutdown() and wait for the SSL_INFO_SHUTDOWNCOMPLETE
  51. notification. This assures that all encrypted data really has been sent.
  52. License
  53. -------
  54. Feel free to use this class, as long as you don't claim that you wrote it
  55. and this copyright notice stays intact in the source files.
  56. If you want to use this class in a commercial application, a short message
  57. to [email protected] would be appreciated but is not required.
  58. This product includes software developed by the OpenSSL Project
  59. for use in the OpenSSL Toolkit. (https://www.openssl.org/)
  60. */
  61. //---------------------------------------------------------------------------
  62. #ifndef AsyncSslSocketLayerH
  63. #define AsyncSslSocketLayerH
  64. //---------------------------------------------------------------------------
  65. #include "AsyncSocketExLayer.h"
  66. #include <openssl/ssl.h>
  67. //---------------------------------------------------------------------------
  68. // Details of SSL certificate, can be used by app to verify if certificate is valid
  69. struct t_SslCertData
  70. {
  71. ~t_SslCertData()
  72. {
  73. delete [] certificate;
  74. }
  75. struct t_Contact
  76. {
  77. TCHAR Organization[256];
  78. TCHAR Unit[256];
  79. TCHAR CommonName[256];
  80. TCHAR Mail[256];
  81. TCHAR Country[256];
  82. TCHAR StateProvince[256];
  83. TCHAR Town[256];
  84. TCHAR Other[1024];
  85. } subject, issuer;
  86. struct t_validTime
  87. {
  88. // Year, Month, day, hour, minute, second
  89. int y, M, d, h, m, s;
  90. } validFrom, validUntil;
  91. TCHAR subjectAltName[10240];
  92. unsigned char hashSha1[20];
  93. unsigned char hashSha256[32];
  94. unsigned char * certificate;
  95. size_t certificateLen;
  96. int verificationResult;
  97. int verificationDepth;
  98. int priv_data; //Internal data, do not modify
  99. };
  100. //---------------------------------------------------------------------------
  101. class CCriticalSectionWrapper;
  102. class CFileZillaTools;
  103. //---------------------------------------------------------------------------
  104. class CAsyncSslSocketLayer : public CAsyncSocketExLayer
  105. {
  106. public:
  107. BOOL SetCertStorage(CString file);
  108. CAsyncSslSocketLayer();
  109. virtual ~CAsyncSslSocketLayer();
  110. void SetNotifyReply(int nID, int nCode, int result);
  111. BOOL GetPeerCertificateData(t_SslCertData & SslCertData, LPCTSTR & Error);
  112. std::string GetTlsVersionStr();
  113. std::string GetCipherName();
  114. void SetClientCertificate(X509 * Certificate, EVP_PKEY * PrivateKey);
  115. bool IsUsingSSL();
  116. int InitSSLConnection(bool clientMode,
  117. CAsyncSslSocketLayer * main,
  118. bool sessionreuse, const CString & host, CFileZillaTools * tools,
  119. void* pContext = 0);
  120. // Send raw text, useful to send a confirmation after the ssl connection
  121. // has been initialized
  122. int SendRaw(const void * lpBuf, int nBufLen, int nFlags = 0);
  123. void* GetContext() { return m_ssl_ctx; }
  124. private:
  125. virtual void Close();
  126. virtual BOOL Connect(LPCTSTR lpszHostAddress, UINT nHostPort);
  127. virtual BOOL Connect(const SOCKADDR* lpSockAddr, int nSockAddrLen);
  128. virtual void OnConnect(int nErrorCode);
  129. virtual void OnReceive(int nErrorCode);
  130. virtual void OnSend(int nErrorCode);
  131. virtual void OnClose(int nErrorCode);
  132. virtual int Receive(void * lpBuf, int nBufLen, int nFlags = 0);
  133. virtual int Send(const void * lpBuf, int nBufLen, int nFlags = 0);
  134. virtual BOOL ShutDown( int nHow = sends );
  135. void ResetSslSession();
  136. void PrintSessionInfo();
  137. BOOL ShutDownComplete();
  138. int InitSSL();
  139. void UnloadSSL();
  140. void PrintLastErrorMsg();
  141. bool HandleSession(SSL_SESSION * Session);
  142. void TriggerEvents();
  143. // Will be called from the OpenSSL library
  144. static void apps_ssl_info_callback(const SSL * s, int where, int ret);
  145. static int verify_callback(int preverify_ok, X509_STORE_CTX * ctx);
  146. static int ProvideClientCert(
  147. SSL * Ssl, X509 ** Certificate, EVP_PKEY ** PrivateKey);
  148. static int NewSessionCallback(struct ssl_st * Ssl, SSL_SESSION * Session);
  149. static CAsyncSslSocketLayer * LookupLayer(SSL * Ssl);
  150. bool m_bUseSSL;
  151. BOOL m_bFailureSent;
  152. // Critical section for thread synchronization
  153. static CCriticalSectionWrapper m_sCriticalSection;
  154. // Status variables
  155. static int m_nSslRefCount;
  156. BOOL m_bSslInitialized;
  157. int m_nShutDown;
  158. int m_nNetworkError;
  159. int m_nSslAsyncNotifyId;
  160. BOOL m_bBlocking;
  161. BOOL m_bSslEstablished;
  162. CString m_CertStorage;
  163. int m_nVerificationResult;
  164. int m_nVerificationDepth;
  165. static struct t_SslLayerList
  166. {
  167. CAsyncSslSocketLayer * pLayer;
  168. t_SslLayerList * pNext;
  169. } * m_pSslLayerList;
  170. // SSL data
  171. SSL_CTX* m_ssl_ctx; // SSL context
  172. static std::map<SSL_CTX *, int> m_contextRefCount;
  173. SSL* m_ssl; // current session handle
  174. SSL_SESSION * m_sessionid;
  175. bool m_sessionreuse;
  176. bool m_sessionreuse_failed;
  177. CAsyncSslSocketLayer * m_Main;
  178. // Data channels for encrypted/unencrypted data
  179. BIO* m_nbio; // Network side, sends/receives encrypted data
  180. BIO* m_ibio; // Internal side, won't be used directly
  181. BIO* m_sslbio; // The data to encrypt / the decrypted data has to go though this bio
  182. // Send buffer
  183. char* m_pNetworkSendBuffer;
  184. int m_nNetworkSendBufferLen;
  185. int m_nNetworkSendBufferMaxLen;
  186. char* m_pRetrySendBuffer;
  187. int m_nRetrySendBufferLen;
  188. bool m_mayTriggerRead;
  189. bool m_mayTriggerWrite;
  190. bool m_mayTriggerReadUp;
  191. bool m_mayTriggerWriteUp;
  192. bool m_onCloseCalled;
  193. std::string m_TlsVersionStr;
  194. std::string m_CipherName;
  195. X509 * FCertificate;
  196. EVP_PKEY * FPrivateKey;
  197. };
  198. //---------------------------------------------------------------------------
  199. #define SSL_INFO 0
  200. #define SSL_FAILURE 1
  201. #define SSL_VERIFY_CERT 2
  202. //---------------------------------------------------------------------------
  203. #define SSL_INFO_ESTABLISHED 0
  204. #define SSL_INFO_SHUTDOWNCOMPLETE 1
  205. //---------------------------------------------------------------------------
  206. #define SSL_FAILURE_UNKNOWN 0
  207. #define SSL_FAILURE_ESTABLISH 1
  208. #define SSL_FAILURE_INITSSL 4
  209. #define SSL_FAILURE_VERIFYCERT 8
  210. #define SSL_FAILURE_CERTREJECTED 16
  211. //---------------------------------------------------------------------------
  212. #endif // AsyncSslSocketLayerH