rfc2818_verification.ipp 4.8 KB

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