rfc2818_verification.ipp 4.7 KB

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