2 // ssl/impl/rfc2818_verification.ipp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2011 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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)
11 #ifndef BOOST_ASIO_SSL_IMPL_RFC2818_VERIFICATION_IPP
12 #define BOOST_ASIO_SSL_IMPL_RFC2818_VERIFICATION_IPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.hpp>
20 #if !defined(BOOST_ASIO_ENABLE_OLD_SSL)
23 # include <boost/asio/ip/address.hpp>
24 # include <boost/asio/ssl/rfc2818_verification.hpp>
25 # include <boost/asio/ssl/detail/openssl_types.hpp>
26 #endif // !defined(BOOST_ASIO_ENABLE_OLD_SSL)
28 #include <boost/asio/detail/push_options.hpp>
34 #if !defined(BOOST_ASIO_ENABLE_OLD_SSL)
36 bool rfc2818_verification::operator()(
37 bool preverified, verify_context& ctx) const
39 using namespace std; // For memcmp.
41 // Don't bother looking at certificates that have failed pre-verification.
45 // We're only interested in checking the certificate at the end of the chain.
46 int depth = X509_STORE_CTX_get_error_depth(ctx.native_handle());
50 // Try converting the host name to an address. If it is an address then we
51 // need to look for an IP address in the certificate rather than a host name.
52 boost::system::error_code ec;
53 ip::address address = ip::address::from_string(host_, ec);
54 bool is_address = !ec;
56 X509* cert = X509_STORE_CTX_get_current_cert(ctx.native_handle());
58 // Go through the alternate names in the certificate looking for matching DNS
59 // or IP address entries.
60 GENERAL_NAMES* gens = static_cast<GENERAL_NAMES*>(
61 X509_get_ext_d2i(cert, NID_subject_alt_name, 0, 0));
62 for (int i = 0; i < sk_GENERAL_NAME_num(gens); ++i)
64 GENERAL_NAME* gen = sk_GENERAL_NAME_value(gens, i);
65 if (gen->type == GEN_DNS && !is_address)
67 ASN1_IA5STRING* domain = gen->d.dNSName;
68 if (domain->type == V_ASN1_IA5STRING && domain->data && domain->length)
70 const char* pattern = reinterpret_cast<const char*>(domain->data);
71 std::size_t pattern_length = domain->length;
72 if (match_pattern(pattern, pattern_length, host_.c_str()))
76 else if (gen->type == GEN_IPADD && is_address)
78 ASN1_OCTET_STRING* ip_address = gen->d.iPAddress;
79 if (ip_address->type == V_ASN1_OCTET_STRING && ip_address->data)
81 if (address.is_v4() && ip_address->length == 4)
83 ip::address_v4::bytes_type bytes = address.to_v4().to_bytes();
84 if (memcmp(bytes.data(), ip_address->data, 4) == 0)
87 else if (address.is_v6() && ip_address->length == 16)
89 ip::address_v6::bytes_type bytes = address.to_v6().to_bytes();
90 if (memcmp(bytes.data(), ip_address->data, 16) == 0)
97 // No match in the alternate names, so try the common names. We should only
98 // use the "most specific" common name, which is the last one in the list.
99 X509_NAME* name = X509_get_subject_name(cert);
101 ASN1_STRING* common_name = 0;
102 while ((i = X509_NAME_get_index_by_NID(name, NID_commonName, i)) >= 0)
104 X509_NAME_ENTRY* name_entry = X509_NAME_get_entry(name, i);
105 common_name = X509_NAME_ENTRY_get_data(name_entry);
107 if (common_name && common_name->data && common_name->length)
109 const char* pattern = reinterpret_cast<const char*>(common_name->data);
110 std::size_t pattern_length = common_name->length;
111 if (match_pattern(pattern, pattern_length, host_.c_str()))
118 bool rfc2818_verification::match_pattern(const char* pattern,
119 std::size_t pattern_length, const char* host)
121 using namespace std; // For tolower.
123 const char* p = pattern;
124 const char* p_end = p + pattern_length;
125 const char* h = host;
127 while (p != p_end && *h)
132 while (*h && *h != '.')
133 if (match_pattern(p, p_end - p, h++))
136 else if (tolower(*p) == tolower(*h))
147 return p == p_end && !*h;
150 #endif // !defined(BOOST_ASIO_ENABLE_OLD_SSL)
156 #include <boost/asio/detail/pop_options.hpp>
158 #endif // BOOST_ASIO_SSL_IMPL_RFC2818_VERIFICATION_IPP