]> git.sesse.net Git - casparcg/blob - dependencies/boost/boost/asio/ssl/impl/rfc2818_verification.ipp
Manually merged pull request #222
[casparcg] / dependencies / boost / boost / asio / ssl / impl / rfc2818_verification.ipp
1 //
2 // ssl/impl/rfc2818_verification.ipp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2011 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
11 #ifndef BOOST_ASIO_SSL_IMPL_RFC2818_VERIFICATION_IPP
12 #define BOOST_ASIO_SSL_IMPL_RFC2818_VERIFICATION_IPP
13
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
15 # pragma once
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
17
18 #include <boost/asio/detail/config.hpp>
19
20 #if !defined(BOOST_ASIO_ENABLE_OLD_SSL)
21 # include <cctype>
22 # include <cstring>
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)
27
28 #include <boost/asio/detail/push_options.hpp>
29
30 namespace boost {
31 namespace asio {
32 namespace ssl {
33
34 #if !defined(BOOST_ASIO_ENABLE_OLD_SSL)
35
36 bool rfc2818_verification::operator()(
37     bool preverified, verify_context& ctx) const
38 {
39   using namespace std; // For memcmp.
40
41   // Don't bother looking at certificates that have failed pre-verification.
42   if (!preverified)
43     return false;
44
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());
47   if (depth > 0)
48     return true;
49
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;
55
56   X509* cert = X509_STORE_CTX_get_current_cert(ctx.native_handle());
57
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)
63   {
64     GENERAL_NAME* gen = sk_GENERAL_NAME_value(gens, i);
65     if (gen->type == GEN_DNS && !is_address)
66     {
67       ASN1_IA5STRING* domain = gen->d.dNSName;
68       if (domain->type == V_ASN1_IA5STRING && domain->data && domain->length)
69       {
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()))
73           return true;
74       }
75     }
76     else if (gen->type == GEN_IPADD && is_address)
77     {
78       ASN1_OCTET_STRING* ip_address = gen->d.iPAddress;
79       if (ip_address->type == V_ASN1_OCTET_STRING && ip_address->data)
80       {
81         if (address.is_v4() && ip_address->length == 4)
82         {
83           ip::address_v4::bytes_type bytes = address.to_v4().to_bytes();
84           if (memcmp(bytes.data(), ip_address->data, 4) == 0)
85             return true;
86         }
87         else if (address.is_v6() && ip_address->length == 16)
88         {
89           ip::address_v6::bytes_type bytes = address.to_v6().to_bytes();
90           if (memcmp(bytes.data(), ip_address->data, 16) == 0)
91             return true;
92         }
93       }
94     }
95   }
96
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);
100   int i = -1;
101   ASN1_STRING* common_name = 0;
102   while ((i = X509_NAME_get_index_by_NID(name, NID_commonName, i)) >= 0)
103   {
104     X509_NAME_ENTRY* name_entry = X509_NAME_get_entry(name, i);
105     common_name = X509_NAME_ENTRY_get_data(name_entry);
106   }
107   if (common_name && common_name->data && common_name->length)
108   {
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()))
112       return true;
113   }
114
115   return false;
116 }
117
118 bool rfc2818_verification::match_pattern(const char* pattern,
119     std::size_t pattern_length, const char* host)
120 {
121   using namespace std; // For tolower.
122
123   const char* p = pattern;
124   const char* p_end = p + pattern_length;
125   const char* h = host;
126
127   while (p != p_end && *h)
128   {
129     if (*p == '*')
130     {
131       ++p;
132       while (*h && *h != '.')
133         if (match_pattern(p, p_end - p, h++))
134           return true;
135     }
136     else if (tolower(*p) == tolower(*h))
137     {
138       ++p;
139       ++h;
140     }
141     else
142     {
143       return false;
144     }
145   }
146
147   return p == p_end && !*h;
148 }
149
150 #endif // !defined(BOOST_ASIO_ENABLE_OLD_SSL)
151
152 } // namespace ssl
153 } // namespace asio
154 } // namespace boost
155
156 #include <boost/asio/detail/pop_options.hpp>
157
158 #endif // BOOST_ASIO_SSL_IMPL_RFC2818_VERIFICATION_IPP