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