SSLManager.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. //
  2. // SSLManager.h
  3. //
  4. // $Id: //poco/Main/NetSSL_OpenSSL/include/Poco/Net/SSLManager.h#7 $
  5. //
  6. // Library: NetSSL_OpenSSL
  7. // Package: SSLCore
  8. // Module: SSLManager
  9. //
  10. // Definition of the SSLManager class.
  11. //
  12. // Copyright (c) 2006-2009, Applied Informatics Software Engineering GmbH.
  13. // and Contributors.
  14. //
  15. // Permission is hereby granted, free of charge, to any person or organization
  16. // obtaining a copy of the software and accompanying documentation covered by
  17. // this license (the "Software") to use, reproduce, display, distribute,
  18. // execute, and transmit the Software, and to prepare derivative works of the
  19. // Software, and to permit third-parties to whom the Software is furnished to
  20. // do so, all subject to the following:
  21. //
  22. // The copyright notices in the Software and this entire statement, including
  23. // the above license grant, this restriction and the following disclaimer,
  24. // must be included in all copies of the Software, in whole or in part, and
  25. // all derivative works of the Software, unless such copies or derivative
  26. // works are solely in the form of machine-executable object code generated by
  27. // a source language processor.
  28. //
  29. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  30. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  31. // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
  32. // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
  33. // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
  34. // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  35. // DEALINGS IN THE SOFTWARE.
  36. //
  37. #ifndef NetSSL_SSLManager_INCLUDED
  38. #define NetSSL_SSLManager_INCLUDED
  39. #include "Poco/Net/NetSSL.h"
  40. #include "Poco/Net/VerificationErrorArgs.h"
  41. #include "Poco/Net/Context.h"
  42. #include "Poco/Net/PrivateKeyFactoryMgr.h"
  43. #include "Poco/Net/CertificateHandlerFactoryMgr.h"
  44. #include "Poco/Net/InvalidCertificateHandler.h"
  45. #include "Poco/BasicEvent.h"
  46. #include "Poco/SharedPtr.h"
  47. #include <openssl/ssl.h>
  48. namespace Poco {
  49. namespace Net {
  50. class Context;
  51. class NetSSL_API SSLManager
  52. /// SSLManager is a singleton for holding the default server/client
  53. /// Context and PrivateKeyPassphraseHandler.
  54. ///
  55. /// Either initialize via Poco::Util::Application or via the
  56. /// initialize methods of the singleton. Note that the latter initialization must happen very early
  57. /// during program startup before somebody calls defaultClientContext()/defaultServerContext()
  58. /// or any of the passPhraseHandler methods (which tries to auto-initialize
  59. /// the context and passphrase handler based on an Poco::Util::Application configuration).
  60. ///
  61. /// An exemplary documentation which sets either the server or client defaultcontext and creates a PrivateKeyPassphraseHandler
  62. /// that reads the password from the XML file looks like this:
  63. ///
  64. /// <AppConfig>
  65. /// <openSSL>
  66. /// <server|client>
  67. /// <privateKeyFile>mycert.key</privateKeyFile>
  68. /// <certificateFile>mycert.crt</certificateFile>
  69. /// <caConfig>rootcert.pem</caConfig>
  70. /// <verificationMode>relaxed</verificationMode>
  71. /// <verificationDepth>9</verificationDepth>
  72. /// <loadDefaultCAFile>true</loadDefaultCAFile>
  73. /// <cypherList>ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH</cypherList>
  74. /// <privateKeyPassphraseHandler>
  75. /// <name>KeyFileHandler</name>
  76. /// <options>
  77. /// <password>test</password>
  78. /// </options>
  79. /// </privateKeyPassphraseHandler>
  80. /// <invalidCertificateHandler>
  81. /// <name>ConsoleCertificateHandler</name>
  82. /// <options>
  83. /// </options>
  84. /// </invalidCertificateHandler>
  85. /// </server|client>
  86. /// </openSSL>
  87. /// </AppConfig>
  88. {
  89. public:
  90. typedef Poco::SharedPtr<PrivateKeyPassphraseHandler> PrivateKeyPassphraseHandlerPtr;
  91. typedef Poco::SharedPtr<InvalidCertificateHandler> InvalidCertificateHandlerPtr;
  92. Poco::BasicEvent<VerificationErrorArgs> ServerVerificationError;
  93. /// Thrown whenever a certificate error is detected by the server during a handshake.
  94. Poco::BasicEvent<VerificationErrorArgs> ClientVerificationError;
  95. /// Thrown whenever a certificate error is detected by the client during a handshake.
  96. Poco::BasicEvent<std::string> PrivateKeyPassPhrase;
  97. /// Thrown when a encrypted certificate is loaded. Not setting the password
  98. /// in the event parameter will result in a failure to load the certificate.
  99. ///
  100. /// Per default the SSLManager checks the configuration.xml file (path openSSL.privateKeyPassphraseHandler.name)
  101. /// for which default delegate it should register. If nothing is configured,
  102. /// a KeyConsoleHandler is used.
  103. static SSLManager& instance();
  104. /// Returns the instance of the SSLManager singleton.
  105. void initializeServer(PrivateKeyPassphraseHandlerPtr ptrPassPhraseHandler, InvalidCertificateHandlerPtr ptrHandler, Context::Ptr ptrContext);
  106. /// Initializes the server side of the SSLManager with a default passphrase handler, a default invalid certificate handler and a default context. If this method
  107. /// is never called the SSLmanager will try to initialize its members from an application configuration.
  108. ///
  109. /// Note: ALWAYS create the handlers before you create the context!
  110. ///
  111. /// Valid initialization code would be:
  112. /// SharedPtr<PrivateKeyPassphraseHandler> ptrConsole = new KeyConsoleHandler();
  113. /// SharedPtr<InvalidCertificateHandler> ptrCert = new ConsoleCertificateHandler();
  114. /// Context::Ptr ptrContext = new Context("any.pem", "rootcert.pem", Context::Relaxed, 9, false, "ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
  115. ///
  116. /// This method can only be called if no defaultContext is set yet.
  117. void initializeClient(PrivateKeyPassphraseHandlerPtr ptrPassPhraseHandler, InvalidCertificateHandlerPtr ptrHandler, Context::Ptr ptrContext);
  118. /// Initializes the client side of the SSLManager with a default passphrase handler, a default invalid certificate handler and a default context. If this method
  119. /// is never called the SSLmanager will try to initialize its members from an application configuration.
  120. ///
  121. /// Note: ALWAYS create the handlers before you create the context!
  122. ///
  123. /// Valid initialization code would be:
  124. /// SharedPtr<PrivateKeyPassphraseHandler> ptrConsole = new KeyConsoleHandler();
  125. /// SharedPtr<InvalidCertificateHandler> ptrCert = new ConsoleCertificateHandler();
  126. /// Context::Ptr ptrContext = new Context("any.pem", "rootcert.pem", Context::Relaxed, 9, false, "ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
  127. ///
  128. /// This method can only be called if no defaultContext is set yet.
  129. Context::Ptr defaultServerContext();
  130. /// Returns the default context used by the server. The first call to this method initializes the defaultContext
  131. /// from an application configuration.
  132. Context::Ptr defaultClientContext();
  133. /// Returns the default context used by the client. The first call to this method initializes the defaultContext
  134. /// from an application configuration.
  135. PrivateKeyPassphraseHandlerPtr serverPassPhraseHandler();
  136. /// Returns the configured passphrase handler of the server. If none is set, the method will create a default one
  137. /// from an application configuration
  138. InvalidCertificateHandlerPtr serverCertificateHandler();
  139. /// Returns an initialized certificate handler (used by the server to verify client cert) which determines how invalid certificates are treated.
  140. /// If none is set, it will try to auto-initialize one from an application configuration.
  141. PrivateKeyPassphraseHandlerPtr clientPassPhraseHandler();
  142. /// Returns the configured passphrase handler of the client. If none is set, the method will create a default one
  143. /// from an application configuration
  144. InvalidCertificateHandlerPtr clientCertificateHandler();
  145. /// Returns an initialized certificate handler (used by the client to verify server cert) which determines how invalid certificates are treated.
  146. /// If none is set, it will try to auto-initialize one from an application configuration.
  147. PrivateKeyFactoryMgr& privateKeyFactoryMgr();
  148. /// Returns the private key factory manager which stores the
  149. /// factories for the different registered passphrase handlers for private keys.
  150. CertificateHandlerFactoryMgr& certificateHandlerFactoryMgr();
  151. /// Returns the CertificateHandlerFactoryMgr which stores the
  152. /// factories for the different registered certificate handlers.
  153. static const std::string CFG_SERVER_PREFIX;
  154. static const std::string CFG_CLIENT_PREFIX;
  155. protected:
  156. static int verifyClientCallback(int ok, X509_STORE_CTX* pStore);
  157. /// The return value of this method defines how errors in
  158. /// verification are handled. Return 0 to terminate the handshake,
  159. /// or 1 to continue despite the error.
  160. static int verifyServerCallback(int ok, X509_STORE_CTX* pStore);
  161. /// The return value of this method defines how errors in
  162. /// verification are handled. Return 0 to terminate the handshake,
  163. /// or 1 to continue despite the error.
  164. static int privateKeyPasswdCallback(char* pBuf, int size, int flag, void* userData);
  165. /// Method is invoked by OpenSSL to retrieve a passwd for an encrypted certificate.
  166. /// The request is delegated to the PrivatekeyPassword event. This method returns the
  167. /// length of the password.
  168. private:
  169. SSLManager();
  170. /// Creates the SSLManager.
  171. ~SSLManager();
  172. /// Destroys the SSLManager.
  173. void initDefaultContext(bool server);
  174. /// Inits the default context, the first time it is accessed.
  175. void initEvents(bool server);
  176. /// Registers delegates at the events according to the configuration.
  177. void initPassPhraseHandler(bool server);
  178. /// Inits the passphrase handler.
  179. void initCertificateHandler(bool server);
  180. /// Inits the certificate handler.
  181. static int verifyCallback(bool server, int ok, X509_STORE_CTX* pStore);
  182. /// The return value of this method defines how errors in
  183. /// verification are handled. Return 0 to terminate the handshake,
  184. /// or 1 to continue despite the error.
  185. PrivateKeyFactoryMgr _factoryMgr;
  186. CertificateHandlerFactoryMgr _certHandlerFactoryMgr;
  187. Context::Ptr _ptrDefaultServerContext;
  188. PrivateKeyPassphraseHandlerPtr _ptrServerPassPhraseHandler;
  189. InvalidCertificateHandlerPtr _ptrServerCertificateHandler;
  190. Context::Ptr _ptrDefaultClientContext;
  191. PrivateKeyPassphraseHandlerPtr _ptrClientPassPhraseHandler;
  192. InvalidCertificateHandlerPtr _ptrClientCertificateHandler;
  193. static const std::string CFG_PRIV_KEY_FILE;
  194. static const std::string CFG_CERTIFICATE_FILE;
  195. static const std::string CFG_CA_LOCATION;
  196. static const std::string CFG_VER_MODE;
  197. static const Context::VerificationMode VAL_VER_MODE;
  198. static const std::string CFG_VER_DEPTH;
  199. static const int VAL_VER_DEPTH;
  200. static const std::string CFG_ENABLE_DEFAULT_CA;
  201. static const bool VAL_ENABLE_DEFAULT_CA;
  202. static const std::string CFG_CYPHER_LIST;
  203. static const std::string VAL_CYPHER_LIST;
  204. static const std::string CFG_DELEGATE_HANDLER;
  205. static const std::string VAL_DELEGATE_HANDLER;
  206. static const std::string CFG_CERTIFICATE_HANDLER;
  207. static const std::string VAL_CERTIFICATE_HANDLER;
  208. friend class Poco::SingletonHolder<SSLManager>;
  209. friend class Context;
  210. };
  211. //
  212. // inlines
  213. //
  214. inline PrivateKeyFactoryMgr& SSLManager::privateKeyFactoryMgr()
  215. {
  216. return _factoryMgr;
  217. }
  218. inline CertificateHandlerFactoryMgr& SSLManager::certificateHandlerFactoryMgr()
  219. {
  220. return _certHandlerFactoryMgr;
  221. }
  222. inline int SSLManager::verifyServerCallback(int ok, X509_STORE_CTX* pStore)
  223. {
  224. return SSLManager::verifyCallback(true, ok, pStore);
  225. }
  226. inline int SSLManager::verifyClientCallback(int ok, X509_STORE_CTX* pStore)
  227. {
  228. return SSLManager::verifyCallback(false, ok, pStore);
  229. }
  230. } } // namespace Poco::Net
  231. #endif // NetSSL_SSLManager_INCLUDED