1*a466cc55SCy Schubert /* Obtained from: https://github.com/iSECPartners/ssl-conservatory */
2*a466cc55SCy Schubert 
3*a466cc55SCy Schubert /*
4*a466cc55SCy Schubert Copyright (C) 2012, iSEC Partners.
5*a466cc55SCy Schubert 
6*a466cc55SCy Schubert Permission is hereby granted, free of charge, to any person obtaining a copy of
7*a466cc55SCy Schubert this software and associated documentation files (the "Software"), to deal in
8*a466cc55SCy Schubert the Software without restriction, including without limitation the rights to
9*a466cc55SCy Schubert use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10*a466cc55SCy Schubert of the Software, and to permit persons to whom the Software is furnished to do
11*a466cc55SCy Schubert so, subject to the following conditions:
12*a466cc55SCy Schubert 
13*a466cc55SCy Schubert The above copyright notice and this permission notice shall be included in all
14*a466cc55SCy Schubert copies or substantial portions of the Software.
15*a466cc55SCy Schubert 
16*a466cc55SCy Schubert THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17*a466cc55SCy Schubert IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18*a466cc55SCy Schubert FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19*a466cc55SCy Schubert AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20*a466cc55SCy Schubert LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21*a466cc55SCy Schubert OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*a466cc55SCy Schubert SOFTWARE.
23*a466cc55SCy Schubert  */
24*a466cc55SCy Schubert 
25*a466cc55SCy Schubert /*
26*a466cc55SCy Schubert  * Helper functions to perform basic hostname validation using OpenSSL.
27*a466cc55SCy Schubert  *
28*a466cc55SCy Schubert  * Please read "everything-you-wanted-to-know-about-openssl.pdf" before
29*a466cc55SCy Schubert  * attempting to use this code. This whitepaper describes how the code works,
30*a466cc55SCy Schubert  * how it should be used, and what its limitations are.
31*a466cc55SCy Schubert  *
32*a466cc55SCy Schubert  * Author:  Alban Diquet
33*a466cc55SCy Schubert  * License: See LICENSE
34*a466cc55SCy Schubert  *
35*a466cc55SCy Schubert  */
36*a466cc55SCy Schubert 
37*a466cc55SCy Schubert // Get rid of OSX 10.7 and greater deprecation warnings.
38*a466cc55SCy Schubert #if defined(__APPLE__) && defined(__clang__)
39*a466cc55SCy Schubert #pragma clang diagnostic ignored "-Wdeprecated-declarations"
40*a466cc55SCy Schubert #endif
41*a466cc55SCy Schubert 
42*a466cc55SCy Schubert #include <openssl/x509v3.h>
43*a466cc55SCy Schubert #include <openssl/ssl.h>
44*a466cc55SCy Schubert #include <string.h>
45*a466cc55SCy Schubert 
46*a466cc55SCy Schubert #include "openssl_hostname_validation.h"
47*a466cc55SCy Schubert #include "hostcheck.h"
48*a466cc55SCy Schubert 
49*a466cc55SCy Schubert #define HOSTNAME_MAX_SIZE 255
50*a466cc55SCy Schubert 
51*a466cc55SCy Schubert #if (OPENSSL_VERSION_NUMBER < 0x10100000L) || \
52*a466cc55SCy Schubert 	(defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x20700000L)
53*a466cc55SCy Schubert #define ASN1_STRING_get0_data ASN1_STRING_data
54*a466cc55SCy Schubert #endif
55*a466cc55SCy Schubert 
56*a466cc55SCy Schubert /**
57*a466cc55SCy Schubert * Tries to find a match for hostname in the certificate's Common Name field.
58*a466cc55SCy Schubert *
59*a466cc55SCy Schubert * Returns MatchFound if a match was found.
60*a466cc55SCy Schubert * Returns MatchNotFound if no matches were found.
61*a466cc55SCy Schubert * Returns MalformedCertificate if the Common Name had a NUL character embedded in it.
62*a466cc55SCy Schubert * Returns Error if the Common Name could not be extracted.
63*a466cc55SCy Schubert */
matches_common_name(const char * hostname,const X509 * server_cert)64*a466cc55SCy Schubert static HostnameValidationResult matches_common_name(const char *hostname, const X509 *server_cert) {
65*a466cc55SCy Schubert         int common_name_loc = -1;
66*a466cc55SCy Schubert         X509_NAME_ENTRY *common_name_entry = NULL;
67*a466cc55SCy Schubert         ASN1_STRING *common_name_asn1 = NULL;
68*a466cc55SCy Schubert         const char *common_name_str = NULL;
69*a466cc55SCy Schubert 
70*a466cc55SCy Schubert         // Find the position of the CN field in the Subject field of the certificate
71*a466cc55SCy Schubert         common_name_loc = X509_NAME_get_index_by_NID(X509_get_subject_name((X509 *) server_cert), NID_commonName, -1);
72*a466cc55SCy Schubert         if (common_name_loc < 0) {
73*a466cc55SCy Schubert                 return Error;
74*a466cc55SCy Schubert         }
75*a466cc55SCy Schubert 
76*a466cc55SCy Schubert         // Extract the CN field
77*a466cc55SCy Schubert         common_name_entry = X509_NAME_get_entry(X509_get_subject_name((X509 *) server_cert), common_name_loc);
78*a466cc55SCy Schubert         if (common_name_entry == NULL) {
79*a466cc55SCy Schubert                 return Error;
80*a466cc55SCy Schubert         }
81*a466cc55SCy Schubert 
82*a466cc55SCy Schubert         // Convert the CN field to a C string
83*a466cc55SCy Schubert         common_name_asn1 = X509_NAME_ENTRY_get_data(common_name_entry);
84*a466cc55SCy Schubert         if (common_name_asn1 == NULL) {
85*a466cc55SCy Schubert                 return Error;
86*a466cc55SCy Schubert         }
87*a466cc55SCy Schubert         common_name_str = (char *) ASN1_STRING_get0_data(common_name_asn1);
88*a466cc55SCy Schubert 
89*a466cc55SCy Schubert         // Make sure there isn't an embedded NUL character in the CN
90*a466cc55SCy Schubert         if ((size_t)ASN1_STRING_length(common_name_asn1) != strlen(common_name_str)) {
91*a466cc55SCy Schubert                 return MalformedCertificate;
92*a466cc55SCy Schubert         }
93*a466cc55SCy Schubert 
94*a466cc55SCy Schubert         // Compare expected hostname with the CN
95*a466cc55SCy Schubert         if (Curl_cert_hostcheck(common_name_str, hostname) == CURL_HOST_MATCH) {
96*a466cc55SCy Schubert                 return MatchFound;
97*a466cc55SCy Schubert         }
98*a466cc55SCy Schubert         else {
99*a466cc55SCy Schubert                 return MatchNotFound;
100*a466cc55SCy Schubert         }
101*a466cc55SCy Schubert }
102*a466cc55SCy Schubert 
103*a466cc55SCy Schubert 
104*a466cc55SCy Schubert /**
105*a466cc55SCy Schubert * Tries to find a match for hostname in the certificate's Subject Alternative Name extension.
106*a466cc55SCy Schubert *
107*a466cc55SCy Schubert * Returns MatchFound if a match was found.
108*a466cc55SCy Schubert * Returns MatchNotFound if no matches were found.
109*a466cc55SCy Schubert * Returns MalformedCertificate if any of the hostnames had a NUL character embedded in it.
110*a466cc55SCy Schubert * Returns NoSANPresent if the SAN extension was not present in the certificate.
111*a466cc55SCy Schubert */
matches_subject_alternative_name(const char * hostname,const X509 * server_cert)112*a466cc55SCy Schubert static HostnameValidationResult matches_subject_alternative_name(const char *hostname, const X509 *server_cert) {
113*a466cc55SCy Schubert         HostnameValidationResult result = MatchNotFound;
114*a466cc55SCy Schubert         int i;
115*a466cc55SCy Schubert         int san_names_nb = -1;
116*a466cc55SCy Schubert         STACK_OF(GENERAL_NAME) *san_names = NULL;
117*a466cc55SCy Schubert 
118*a466cc55SCy Schubert         // Try to extract the names within the SAN extension from the certificate
119*a466cc55SCy Schubert         san_names = X509_get_ext_d2i((X509 *) server_cert, NID_subject_alt_name, NULL, NULL);
120*a466cc55SCy Schubert         if (san_names == NULL) {
121*a466cc55SCy Schubert                 return NoSANPresent;
122*a466cc55SCy Schubert         }
123*a466cc55SCy Schubert         san_names_nb = sk_GENERAL_NAME_num(san_names);
124*a466cc55SCy Schubert 
125*a466cc55SCy Schubert         // Check each name within the extension
126*a466cc55SCy Schubert         for (i=0; i<san_names_nb; i++) {
127*a466cc55SCy Schubert                 const GENERAL_NAME *current_name = sk_GENERAL_NAME_value(san_names, i);
128*a466cc55SCy Schubert 
129*a466cc55SCy Schubert                 if (current_name->type == GEN_DNS) {
130*a466cc55SCy Schubert                         // Current name is a DNS name, let's check it
131*a466cc55SCy Schubert                         const char *dns_name = (char *) ASN1_STRING_get0_data(current_name->d.dNSName);
132*a466cc55SCy Schubert 
133*a466cc55SCy Schubert                         // Make sure there isn't an embedded NUL character in the DNS name
134*a466cc55SCy Schubert                         if ((size_t)ASN1_STRING_length(current_name->d.dNSName) != strlen(dns_name)) {
135*a466cc55SCy Schubert                                 result = MalformedCertificate;
136*a466cc55SCy Schubert                                 break;
137*a466cc55SCy Schubert                         }
138*a466cc55SCy Schubert                         else { // Compare expected hostname with the DNS name
139*a466cc55SCy Schubert                                 if (Curl_cert_hostcheck(dns_name, hostname)
140*a466cc55SCy Schubert                                     == CURL_HOST_MATCH) {
141*a466cc55SCy Schubert                                         result = MatchFound;
142*a466cc55SCy Schubert                                         break;
143*a466cc55SCy Schubert                                 }
144*a466cc55SCy Schubert                         }
145*a466cc55SCy Schubert                 }
146*a466cc55SCy Schubert         }
147*a466cc55SCy Schubert         sk_GENERAL_NAME_pop_free(san_names, GENERAL_NAME_free);
148*a466cc55SCy Schubert 
149*a466cc55SCy Schubert         return result;
150*a466cc55SCy Schubert }
151*a466cc55SCy Schubert 
152*a466cc55SCy Schubert 
153*a466cc55SCy Schubert /**
154*a466cc55SCy Schubert * Validates the server's identity by looking for the expected hostname in the
155*a466cc55SCy Schubert * server's certificate. As described in RFC 6125, it first tries to find a match
156*a466cc55SCy Schubert * in the Subject Alternative Name extension. If the extension is not present in
157*a466cc55SCy Schubert * the certificate, it checks the Common Name instead.
158*a466cc55SCy Schubert *
159*a466cc55SCy Schubert * Returns MatchFound if a match was found.
160*a466cc55SCy Schubert * Returns MatchNotFound if no matches were found.
161*a466cc55SCy Schubert * Returns MalformedCertificate if any of the hostnames had a NUL character embedded in it.
162*a466cc55SCy Schubert * Returns Error if there was an error.
163*a466cc55SCy Schubert */
validate_hostname(const char * hostname,const X509 * server_cert)164*a466cc55SCy Schubert HostnameValidationResult validate_hostname(const char *hostname, const X509 *server_cert) {
165*a466cc55SCy Schubert         HostnameValidationResult result;
166*a466cc55SCy Schubert 
167*a466cc55SCy Schubert         if((hostname == NULL) || (server_cert == NULL))
168*a466cc55SCy Schubert                 return Error;
169*a466cc55SCy Schubert 
170*a466cc55SCy Schubert         // First try the Subject Alternative Names extension
171*a466cc55SCy Schubert         result = matches_subject_alternative_name(hostname, server_cert);
172*a466cc55SCy Schubert         if (result == NoSANPresent) {
173*a466cc55SCy Schubert                 // Extension was not found: try the Common Name
174*a466cc55SCy Schubert                 result = matches_common_name(hostname, server_cert);
175*a466cc55SCy Schubert         }
176*a466cc55SCy Schubert 
177*a466cc55SCy Schubert         return result;
178*a466cc55SCy Schubert }
179