rfc2818_verification.ipp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. //
  2. // ssl/impl/rfc2818_verification.ipp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2024 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_IMPL_RFC2818_VERIFICATION_IPP
  11. #define BOOST_ASIO_SSL_IMPL_RFC2818_VERIFICATION_IPP
  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 <cctype>
  18. #include <cstring>
  19. #include <boost/asio/ip/address.hpp>
  20. #include <boost/asio/ssl/rfc2818_verification.hpp>
  21. #include <boost/asio/ssl/detail/openssl_types.hpp>
  22. #include <boost/asio/detail/push_options.hpp>
  23. namespace boost {
  24. namespace asio {
  25. namespace ssl {
  26. bool rfc2818_verification::operator()(
  27. bool preverified, verify_context& ctx) const
  28. {
  29. using namespace std; // For memcmp.
  30. // Don't bother looking at certificates that have failed pre-verification.
  31. if (!preverified)
  32. return false;
  33. // We're only interested in checking the certificate at the end of the chain.
  34. int depth = X509_STORE_CTX_get_error_depth(ctx.native_handle());
  35. if (depth > 0)
  36. return true;
  37. // Try converting the host name to an address. If it is an address then we
  38. // need to look for an IP address in the certificate rather than a host name.
  39. boost::system::error_code ec;
  40. ip::address address = ip::make_address(host_, ec);
  41. bool is_address = !ec;
  42. X509* cert = X509_STORE_CTX_get_current_cert(ctx.native_handle());
  43. // Go through the alternate names in the certificate looking for matching DNS
  44. // or IP address entries.
  45. GENERAL_NAMES* gens = static_cast<GENERAL_NAMES*>(
  46. X509_get_ext_d2i(cert, NID_subject_alt_name, 0, 0));
  47. for (int i = 0; i < sk_GENERAL_NAME_num(gens); ++i)
  48. {
  49. GENERAL_NAME* gen = sk_GENERAL_NAME_value(gens, i);
  50. if (gen->type == GEN_DNS && !is_address)
  51. {
  52. ASN1_IA5STRING* domain = gen->d.dNSName;
  53. if (domain->type == V_ASN1_IA5STRING && domain->data && domain->length)
  54. {
  55. const char* pattern = reinterpret_cast<const char*>(domain->data);
  56. std::size_t pattern_length = domain->length;
  57. if (match_pattern(pattern, pattern_length, host_.c_str()))
  58. {
  59. GENERAL_NAMES_free(gens);
  60. return true;
  61. }
  62. }
  63. }
  64. else if (gen->type == GEN_IPADD && is_address)
  65. {
  66. ASN1_OCTET_STRING* ip_address = gen->d.iPAddress;
  67. if (ip_address->type == V_ASN1_OCTET_STRING && ip_address->data)
  68. {
  69. if (address.is_v4() && ip_address->length == 4)
  70. {
  71. ip::address_v4::bytes_type bytes = address.to_v4().to_bytes();
  72. if (memcmp(bytes.data(), ip_address->data, 4) == 0)
  73. {
  74. GENERAL_NAMES_free(gens);
  75. return true;
  76. }
  77. }
  78. else if (address.is_v6() && ip_address->length == 16)
  79. {
  80. ip::address_v6::bytes_type bytes = address.to_v6().to_bytes();
  81. if (memcmp(bytes.data(), ip_address->data, 16) == 0)
  82. {
  83. GENERAL_NAMES_free(gens);
  84. return true;
  85. }
  86. }
  87. }
  88. }
  89. }
  90. GENERAL_NAMES_free(gens);
  91. // No match in the alternate names, so try the common names. We should only
  92. // use the "most specific" common name, which is the last one in the list.
  93. X509_NAME* name = X509_get_subject_name(cert);
  94. int i = -1;
  95. ASN1_STRING* common_name = 0;
  96. while ((i = X509_NAME_get_index_by_NID(name, NID_commonName, i)) >= 0)
  97. {
  98. X509_NAME_ENTRY* name_entry = X509_NAME_get_entry(name, i);
  99. common_name = X509_NAME_ENTRY_get_data(name_entry);
  100. }
  101. if (common_name && common_name->data && common_name->length)
  102. {
  103. const char* pattern = reinterpret_cast<const char*>(common_name->data);
  104. std::size_t pattern_length = common_name->length;
  105. if (match_pattern(pattern, pattern_length, host_.c_str()))
  106. return true;
  107. }
  108. return false;
  109. }
  110. bool rfc2818_verification::match_pattern(const char* pattern,
  111. std::size_t pattern_length, const char* host)
  112. {
  113. using namespace std; // For tolower.
  114. const char* p = pattern;
  115. const char* p_end = p + pattern_length;
  116. const char* h = host;
  117. while (p != p_end && *h)
  118. {
  119. if (*p == '*')
  120. {
  121. ++p;
  122. while (*h && *h != '.')
  123. if (match_pattern(p, p_end - p, h++))
  124. return true;
  125. }
  126. else if (tolower(*p) == tolower(*h))
  127. {
  128. ++p;
  129. ++h;
  130. }
  131. else
  132. {
  133. return false;
  134. }
  135. }
  136. return p == p_end && !*h;
  137. }
  138. } // namespace ssl
  139. } // namespace asio
  140. } // namespace boost
  141. #include <boost/asio/detail/pop_options.hpp>
  142. #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
  143. #endif // BOOST_ASIO_SSL_IMPL_RFC2818_VERIFICATION_IPP