rfc2818_verification.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. //
  2. // ssl/rfc2818_verification.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2021 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. #if !defined(BOOST_ASIO_NO_DEPRECATED)
  17. #include <string>
  18. #include <boost/asio/ssl/detail/openssl_types.hpp>
  19. #include <boost/asio/ssl/verify_context.hpp>
  20. #include <boost/asio/detail/push_options.hpp>
  21. namespace boost {
  22. namespace asio {
  23. namespace ssl {
  24. /// (Deprecated. Use ssl::host_name_verification.) Verifies a certificate
  25. /// against a hostname according to the rules described in RFC 2818.
  26. /**
  27. * @par Example
  28. * The following example shows how to synchronously open a secure connection to
  29. * a given host name:
  30. * @code
  31. * using boost::asio::ip::tcp;
  32. * namespace ssl = boost::asio::ssl;
  33. * typedef ssl::stream<tcp::socket> ssl_socket;
  34. *
  35. * // Create a context that uses the default paths for finding CA certificates.
  36. * ssl::context ctx(ssl::context::sslv23);
  37. * ctx.set_default_verify_paths();
  38. *
  39. * // Open a socket and connect it to the remote host.
  40. * boost::asio::io_context io_context;
  41. * ssl_socket sock(io_context, ctx);
  42. * tcp::resolver resolver(io_context);
  43. * tcp::resolver::query query("host.name", "https");
  44. * boost::asio::connect(sock.lowest_layer(), resolver.resolve(query));
  45. * sock.lowest_layer().set_option(tcp::no_delay(true));
  46. *
  47. * // Perform SSL handshake and verify the remote host's certificate.
  48. * sock.set_verify_mode(ssl::verify_peer);
  49. * sock.set_verify_callback(ssl::rfc2818_verification("host.name"));
  50. * sock.handshake(ssl_socket::client);
  51. *
  52. * // ... read and write as normal ...
  53. * @endcode
  54. */
  55. class rfc2818_verification
  56. {
  57. public:
  58. /// The type of the function object's result.
  59. typedef bool result_type;
  60. /// Constructor.
  61. explicit rfc2818_verification(const std::string& host)
  62. : host_(host)
  63. {
  64. }
  65. /// Perform certificate verification.
  66. BOOST_ASIO_DECL bool operator()(bool preverified, verify_context& ctx) const;
  67. private:
  68. // Helper function to check a host name against a pattern.
  69. BOOST_ASIO_DECL static bool match_pattern(const char* pattern,
  70. std::size_t pattern_length, const char* host);
  71. // Helper function to check a host name against an IPv4 address
  72. // The host name to be checked.
  73. std::string host_;
  74. };
  75. } // namespace ssl
  76. } // namespace asio
  77. } // namespace boost
  78. #include <boost/asio/detail/pop_options.hpp>
  79. #if defined(BOOST_ASIO_HEADER_ONLY)
  80. # include <boost/asio/ssl/impl/rfc2818_verification.ipp>
  81. #endif // defined(BOOST_ASIO_HEADER_ONLY)
  82. #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
  83. #endif // BOOST_ASIO_SSL_RFC2818_VERIFICATION_HPP