rfc2818_verification.hpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. //
  2. // ssl/rfc2818_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_RFC2818_VERIFICATION_HPP
  11. #define 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 "asio/detail/config.hpp"
  16. #if !defined(ASIO_NO_DEPRECATED)
  17. #include <string>
  18. #include "asio/ssl/detail/openssl_types.hpp"
  19. #include "asio/ssl/verify_context.hpp"
  20. #include "asio/detail/push_options.hpp"
  21. namespace asio {
  22. namespace ssl {
  23. /// (Deprecated. Use ssl::host_name_verification.) Verifies a certificate
  24. /// against a hostname according to the rules described 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 asio::ip::tcp;
  31. * namespace ssl = 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. * 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. * 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. ASIO_DECL bool operator()(bool preverified, verify_context& ctx) const;
  66. private:
  67. // Helper function to check a host name against a pattern.
  68. 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. #include "asio/detail/pop_options.hpp"
  77. #if defined(ASIO_HEADER_ONLY)
  78. # include "asio/ssl/impl/rfc2818_verification.ipp"
  79. #endif // defined(ASIO_HEADER_ONLY)
  80. #endif // !defined(ASIO_NO_DEPRECATED)
  81. #endif // ASIO_SSL_RFC2818_VERIFICATION_HPP