Context.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. //
  2. // Context.h
  3. //
  4. // $Id: //poco/Main/NetSSL_OpenSSL/include/Poco/Net/Context.h#9 $
  5. //
  6. // Library: NetSSL_OpenSSL
  7. // Package: SSLCore
  8. // Module: Context
  9. //
  10. // Definition of the Context 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_Context_INCLUDED
  38. #define NetSSL_Context_INCLUDED
  39. #include "Poco/Net/NetSSL.h"
  40. #include "Poco/RefCountedObject.h"
  41. #include "Poco/AutoPtr.h"
  42. #include <openssl/ssl.h>
  43. namespace Poco {
  44. namespace Net {
  45. class NetSSL_API Context: public Poco::RefCountedObject
  46. /// This class encapsulates context information for
  47. /// an SSL server or client, such as the certificate
  48. /// verification mode and the location of certificates
  49. /// and private key files, as well as the list of
  50. /// supported ciphers.
  51. {
  52. public:
  53. typedef Poco::AutoPtr<Context> Ptr;
  54. enum Usage
  55. {
  56. CLIENT_USE, /// Context is used by a client.
  57. SERVER_USE /// Context is used by a server.
  58. };
  59. enum VerificationMode
  60. {
  61. VERIFY_NONE = SSL_VERIFY_NONE,
  62. /// Server: The server will not send a client certificate
  63. /// request to the client, so the client will not send a certificate.
  64. ///
  65. /// Client: If not using an anonymous cipher (by default disabled),
  66. /// the server will send a certificate which will be checked, but
  67. /// the result of the check will be ignored.
  68. VERIFY_RELAXED = SSL_VERIFY_PEER,
  69. /// Server: The server sends a client certificate request to the
  70. /// client. The certificate returned (if any) is checked.
  71. /// If the verification process fails, the TLS/SSL handshake is
  72. /// immediately terminated with an alert message containing the
  73. /// reason for the verification failure.
  74. ///
  75. /// Client: The server certificate is verified, if one is provided.
  76. /// If the verification process fails, the TLS/SSL handshake is
  77. /// immediately terminated with an alert message containing the
  78. /// reason for the verification failure.
  79. VERIFY_STRICT = SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
  80. /// Server: If the client did not return a certificate, the TLS/SSL
  81. /// handshake is immediately terminated with a handshake failure
  82. /// alert. This flag must be used together with SSL_VERIFY_PEER.
  83. ///
  84. /// Client: Same as VERIFY_RELAXED.
  85. VERIFY_ONCE = SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE
  86. /// Server: Only request a client certificate on the initial
  87. /// TLS/SSL handshake. Do not ask for a client certificate
  88. /// again in case of a renegotiation.
  89. ///
  90. /// Client: Same as VERIFY_RELAXED.
  91. };
  92. Context(
  93. Usage usage,
  94. const std::string& privateKeyFile,
  95. const std::string& certificateFile,
  96. const std::string& caLocation,
  97. VerificationMode verificationMode = VERIFY_RELAXED,
  98. int verificationDepth = 9,
  99. bool loadDefaultCAs = false,
  100. const std::string& cipherList = "ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
  101. /// Creates a Context.
  102. ///
  103. /// * usage specifies whether the context is used by a client or server.
  104. /// * privateKeyFile contains the path to the private key file used for encryption.
  105. /// Can be empty if no private key file is used.
  106. /// * certificateFile contains the path to the certificate file (in PEM format).
  107. /// If the private key and the certificate are stored in the same file, this
  108. /// can be empty if privateKeyFile is given.
  109. /// * caLocation contains the path to the file or directory containing the
  110. /// CA/root certificates. Can be empty if the OpenSSL builtin CA certificates
  111. /// are used (see loadDefaultCAs).
  112. /// * verificationMode specifies whether and how peer certificates are validated.
  113. /// * verificationDepth sets the upper limit for verification chain sizes. Verification
  114. /// will fail if a certificate chain larger than this is encountered.
  115. /// * loadDefaultCAs specifies wheter the builtin CA certificates from OpenSSL are used.
  116. /// * cipherList specifies the supported ciphers in OpenSSL notation.
  117. ~Context();
  118. /// Destroys the Context.
  119. SSL_CTX* sslContext() const;
  120. /// Returns the underlying OpenSSL SSL Context object.
  121. Usage usage() const;
  122. /// Returns whether the context is for use by a client or by a server.
  123. Context::VerificationMode verificationMode() const;
  124. /// Returns the verification mode.
  125. private:
  126. Usage _usage;
  127. VerificationMode _mode;
  128. SSL_CTX* _pSSLContext;
  129. };
  130. //
  131. // inlines
  132. //
  133. inline Context::Usage Context::usage() const
  134. {
  135. return _usage;
  136. }
  137. inline Context::VerificationMode Context::verificationMode() const
  138. {
  139. return _mode;
  140. }
  141. inline SSL_CTX* Context::sslContext() const
  142. {
  143. return _pSSLContext;
  144. }
  145. } } // namespace Poco::Net
  146. #endif // NetSSL_Context_INCLUDED