rfc2818_verification.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. //
  2. // ssl/rfc2818_verification.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef BOOST_ASIO_SSL_RFC2818_VERIFICATION_HPP
  11. #define BOOST_ASIO_SSL_RFC2818_VERIFICATION_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/config.hpp>
  16. #include <string>
  17. #include <boost/asio/ssl/detail/openssl_types.hpp>
  18. #include <boost/asio/ssl/verify_context.hpp>
  19. #include <boost/asio/detail/push_options.hpp>
  20. namespace boost {
  21. namespace asio {
  22. namespace ssl {
  23. /// Verifies a certificate against a hostname according to the rules described
  24. /// in RFC 2818.
  25. /**
  26. * @par Example
  27. * The following example shows how to synchronously open a secure connection to
  28. * a given host name:
  29. * @code
  30. * using boost::asio::ip::tcp;
  31. * namespace ssl = boost::asio::ssl;
  32. * typedef ssl::stream<tcp::socket> ssl_socket;
  33. *
  34. * // Create a context that uses the default paths for finding CA certificates.
  35. * ssl::context ctx(ssl::context::sslv23);
  36. * ctx.set_default_verify_paths();
  37. *
  38. * // Open a socket and connect it to the remote host.
  39. * boost::asio::io_context io_context;
  40. * ssl_socket sock(io_context, ctx);
  41. * tcp::resolver resolver(io_context);
  42. * tcp::resolver::query query("host.name", "https");
  43. * boost::asio::connect(sock.lowest_layer(), resolver.resolve(query));
  44. * sock.lowest_layer().set_option(tcp::no_delay(true));
  45. *
  46. * // Perform SSL handshake and verify the remote host's certificate.
  47. * sock.set_verify_mode(ssl::verify_peer);
  48. * sock.set_verify_callback(ssl::rfc2818_verification("host.name"));
  49. * sock.handshake(ssl_socket::client);
  50. *
  51. * // ... read and write as normal ...
  52. * @endcode
  53. */
  54. class rfc2818_verification
  55. {
  56. public:
  57. /// The type of the function object's result.
  58. typedef bool result_type;
  59. /// Constructor.
  60. explicit rfc2818_verification(const std::string& host)
  61. : host_(host)
  62. {
  63. }
  64. /// Perform certificate verification.
  65. BOOST_ASIO_DECL bool operator()(bool preverified, verify_context& ctx) const;
  66. private:
  67. // Helper function to check a host name against a pattern.
  68. BOOST_ASIO_DECL static bool match_pattern(const char* pattern,
  69. std::size_t pattern_length, const char* host);
  70. // Helper function to check a host name against an IPv4 address
  71. // The host name to be checked.
  72. std::string host_;
  73. };
  74. } // namespace ssl
  75. } // namespace asio
  76. } // namespace boost
  77. #include <boost/asio/detail/pop_options.hpp>
  78. #if defined(BOOST_ASIO_HEADER_ONLY)
  79. # include <boost/asio/ssl/impl/rfc2818_verification.ipp>
  80. #endif // defined(BOOST_ASIO_HEADER_ONLY)
  81. #endif // BOOST_ASIO_SSL_RFC2818_VERIFICATION_HPP