rfc2818_verification.hpp 2.8 KB

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