host_name_verification.hpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. //
  2. // ssl/host_name_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_HOST_NAME_VERIFICATION_HPP
  11. #define BOOST_ASIO_SSL_HOST_NAME_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 host_name according to the rules described
  24. /// in RFC 6125.
  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::host_name_verification("host.name"));
  49. * sock.handshake(ssl_socket::client);
  50. *
  51. * // ... read and write as normal ...
  52. * @endcode
  53. */
  54. class host_name_verification
  55. {
  56. public:
  57. /// The type of the function object's result.
  58. typedef bool result_type;
  59. /// Constructor.
  60. explicit host_name_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 an IPv4 address
  68. // The host name to be checked.
  69. std::string host_;
  70. };
  71. } // namespace ssl
  72. } // namespace asio
  73. } // namespace boost
  74. #include <boost/asio/detail/pop_options.hpp>
  75. #if defined(BOOST_ASIO_HEADER_ONLY)
  76. # include <boost/asio/ssl/impl/host_name_verification.ipp>
  77. #endif // defined(BOOST_ASIO_HEADER_ONLY)
  78. #endif // BOOST_ASIO_SSL_HOST_NAME_VERIFICATION_HPP