host_name_verification.hpp 2.5 KB

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