rfc2818_verification.ipp 4.6 KB

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