1 /*
2 * Copyright (C) 2003-2016 Free Software Foundation, Inc.
3 * Copyright (C) 2015-2016 Red Hat, Inc.
4 * Copyright (C) 2002 Andrew McDonald
5 *
6 * This file is part of GnuTLS.
7 *
8 * The GnuTLS is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License
10 * as published by the Free Software Foundation; either version 2.1 of
11 * the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>
20 *
21 */
22
23 #include "gnutls_int.h"
24 #include <str.h>
25 #include <x509_int.h>
26 #include <common.h>
27 #include "errors.h"
28 #include <system.h>
29 #include <netinet/in.h>
30 #include <arpa/inet.h>
31
32 /**
33 * gnutls_x509_crt_check_hostname:
34 * @cert: should contain an gnutls_x509_crt_t type
35 * @hostname: A null terminated string that contains a DNS name
36 *
37 * This function will check if the given certificate's subject matches
38 * the given hostname. This is a basic implementation of the matching
39 * described in RFC6125, and takes into account wildcards,
40 * and the DNSName/IPAddress subject alternative name PKIX extension.
41 *
42 * For details see also gnutls_x509_crt_check_hostname2().
43 *
44 * Returns: non-zero for a successful match, and zero on failure.
45 **/
46 unsigned
gnutls_x509_crt_check_hostname(gnutls_x509_crt_t cert,const char * hostname)47 gnutls_x509_crt_check_hostname(gnutls_x509_crt_t cert,
48 const char *hostname)
49 {
50 return gnutls_x509_crt_check_hostname2(cert, hostname, 0);
51 }
52
53 static int
check_ip(gnutls_x509_crt_t cert,const void * ip,unsigned ip_size)54 check_ip(gnutls_x509_crt_t cert, const void *ip, unsigned ip_size)
55 {
56 char temp[16];
57 size_t temp_size;
58 unsigned i;
59 int ret = 0;
60
61 /* try matching against:
62 * 1) a IPaddress alternative name (subjectAltName) extension
63 * in the certificate
64 */
65
66 /* Check through all included subjectAltName extensions, comparing
67 * against all those of type IPAddress.
68 */
69 for (i = 0; !(ret < 0); i++) {
70 temp_size = sizeof(temp);
71 ret = gnutls_x509_crt_get_subject_alt_name(cert, i,
72 temp,
73 &temp_size,
74 NULL);
75
76 if (ret == GNUTLS_SAN_IPADDRESS) {
77 if (temp_size == ip_size && memcmp(temp, ip, ip_size) == 0)
78 return 1;
79 } else if (ret == GNUTLS_E_SHORT_MEMORY_BUFFER) {
80 ret = 0;
81 }
82 }
83
84 /* not found a matching IP
85 */
86 return 0;
87 }
88
89 /**
90 * gnutls_x509_crt_check_ip:
91 * @cert: should contain an gnutls_x509_crt_t type
92 * @ip: A pointer to the raw IP address
93 * @ip_size: the number of bytes in ip (4 or 16)
94 * @flags: should be zero
95 *
96 * This function will check if the IP allowed IP addresses in
97 * the certificate's subject alternative name match the provided
98 * IP address.
99 *
100 * Returns: non-zero for a successful match, and zero on failure.
101 **/
102 unsigned
gnutls_x509_crt_check_ip(gnutls_x509_crt_t cert,const unsigned char * ip,unsigned int ip_size,unsigned int flags)103 gnutls_x509_crt_check_ip(gnutls_x509_crt_t cert,
104 const unsigned char *ip, unsigned int ip_size,
105 unsigned int flags)
106 {
107 return check_ip(cert, ip, ip_size);
108 }
109
110 /* whether gnutls_x509_crt_check_hostname2() will consider these
111 * alternative name types. This is to satisfy RFC6125 requirement
112 * that we do not fallback to CN-ID if we encounter a supported name
113 * type.
114 */
115 #define IS_SAN_SUPPORTED(san) (san==GNUTLS_SAN_DNSNAME||san==GNUTLS_SAN_IPADDRESS)
116
117 /**
118 * gnutls_x509_crt_check_hostname2:
119 * @cert: should contain an gnutls_x509_crt_t type
120 * @hostname: A null terminated string that contains a DNS name
121 * @flags: gnutls_certificate_verify_flags
122 *
123 * This function will check if the given certificate's subject matches
124 * the given hostname. This is a basic implementation of the matching
125 * described in RFC6125, and takes into account wildcards,
126 * and the DNSName/IPAddress subject alternative name PKIX extension.
127 *
128 * IPv4 addresses are accepted by this function in the dotted-decimal
129 * format (e.g, ddd.ddd.ddd.ddd), and IPv6 addresses in the hexadecimal
130 * x:x:x:x:x:x:x:x format. For them the IPAddress subject alternative
131 * name extension is consulted. Previous versions to 3.6.0 of GnuTLS
132 * in case of a non-match would consult (in a non-standard extension)
133 * the DNSname and CN fields. This is no longer the case.
134 *
135 * When the flag %GNUTLS_VERIFY_DO_NOT_ALLOW_WILDCARDS is specified no
136 * wildcards are considered. Otherwise they are only considered if the
137 * domain name consists of three components or more, and the wildcard
138 * starts at the leftmost position.
139
140 * When the flag %GNUTLS_VERIFY_DO_NOT_ALLOW_IP_MATCHES is specified,
141 * the input will be treated as a DNS name, and matching of textual IP addresses
142 * against the IPAddress part of the alternative name will not be allowed.
143 *
144 * The function gnutls_x509_crt_check_ip() is available for matching
145 * IP addresses.
146 *
147 * Returns: non-zero for a successful match, and zero on failure.
148 *
149 * Since: 3.3.0
150 **/
151 unsigned
gnutls_x509_crt_check_hostname2(gnutls_x509_crt_t cert,const char * hostname,unsigned int flags)152 gnutls_x509_crt_check_hostname2(gnutls_x509_crt_t cert,
153 const char *hostname, unsigned int flags)
154 {
155 char dnsname[MAX_CN];
156 size_t dnsnamesize;
157 int found_dnsname = 0;
158 int ret = 0;
159 int i = 0;
160 struct in_addr ipv4;
161 char *p = NULL;
162 char *a_hostname;
163 unsigned have_other_addresses = 0;
164 gnutls_datum_t out;
165
166 /* check whether @hostname is an ip address */
167 if (!(flags & GNUTLS_VERIFY_DO_NOT_ALLOW_IP_MATCHES) &&
168 ((p=strchr(hostname, ':')) != NULL || inet_pton(AF_INET, hostname, &ipv4) != 0)) {
169
170 if (p != NULL) {
171 struct in6_addr ipv6;
172
173 ret = inet_pton(AF_INET6, hostname, &ipv6);
174 if (ret == 0) {
175 gnutls_assert();
176 goto hostname_fallback;
177 }
178 ret = check_ip(cert, &ipv6, 16);
179 } else {
180 ret = check_ip(cert, &ipv4, 4);
181 }
182
183 /* Prior to 3.6.0 we were accepting misconfigured servers, that place their IP
184 * in the DNS field of subjectAlternativeName. That is no longer the case. */
185 return ret;
186 }
187
188 hostname_fallback:
189 /* convert the provided hostname to ACE-Labels domain. */
190 ret = gnutls_idna_map (hostname, strlen(hostname), &out, 0);
191 if (ret < 0) {
192 _gnutls_debug_log("unable to convert hostname %s to IDNA format\n", hostname);
193 a_hostname = (char*)hostname;
194 } else {
195 a_hostname = (char*)out.data;
196 }
197
198 /* try matching against:
199 * 1) a DNS name as an alternative name (subjectAltName) extension
200 * in the certificate
201 * 2) the common name (CN) in the certificate, if the certificate is acceptable for TLS_WWW_SERVER purpose
202 *
203 * either of these may be of the form: *.domain.tld
204 *
205 * only try (2) if there is no subjectAltName extension of
206 * type dNSName, and there is a single CN.
207 */
208
209 /* Check through all included subjectAltName extensions, comparing
210 * against all those of type dNSName.
211 */
212 for (i = 0; !(ret < 0); i++) {
213
214 dnsnamesize = sizeof(dnsname);
215 ret = gnutls_x509_crt_get_subject_alt_name(cert, i,
216 dnsname,
217 &dnsnamesize,
218 NULL);
219
220 if (ret == GNUTLS_SAN_DNSNAME) {
221 found_dnsname = 1;
222
223 if (_gnutls_has_embedded_null(dnsname, dnsnamesize)) {
224 _gnutls_debug_log("certificate has %s with embedded null in name\n", dnsname);
225 continue;
226 }
227
228 if (!_gnutls_str_is_print(dnsname, dnsnamesize)) {
229 _gnutls_debug_log("invalid (non-ASCII) name in certificate %.*s\n", (int)dnsnamesize, dnsname);
230 continue;
231 }
232
233 ret = _gnutls_hostname_compare(dnsname, dnsnamesize, a_hostname, flags);
234 if (ret != 0) {
235 ret = 1;
236 goto cleanup;
237 }
238 } else {
239 if (IS_SAN_SUPPORTED(ret))
240 have_other_addresses = 1;
241 }
242 }
243
244 if (!have_other_addresses && !found_dnsname && _gnutls_check_key_purpose(cert, GNUTLS_KP_TLS_WWW_SERVER, 0) != 0) {
245 /* did not get the necessary extension, use CN instead, if the
246 * certificate would have been acceptable for a TLS WWW server purpose.
247 * That is because only for that purpose the CN is a valid field to
248 * store the hostname.
249 */
250
251 /* enforce the RFC6125 (§1.8) requirement that only
252 * a single CN must be present */
253 dnsnamesize = sizeof(dnsname);
254 ret = gnutls_x509_crt_get_dn_by_oid
255 (cert, OID_X520_COMMON_NAME, 1, 0, dnsname,
256 &dnsnamesize);
257 if (ret != GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE) {
258 ret = 0;
259 goto cleanup;
260 }
261
262 dnsnamesize = sizeof(dnsname);
263 ret = gnutls_x509_crt_get_dn_by_oid
264 (cert, OID_X520_COMMON_NAME, 0, 0, dnsname,
265 &dnsnamesize);
266 if (ret < 0) {
267 ret = 0;
268 goto cleanup;
269 }
270
271 if (_gnutls_has_embedded_null(dnsname, dnsnamesize)) {
272 _gnutls_debug_log("certificate has CN %s with embedded null in name\n", dnsname);
273 ret = 0;
274 goto cleanup;
275 }
276
277 if (!_gnutls_str_is_print(dnsname, dnsnamesize)) {
278 _gnutls_debug_log("invalid (non-ASCII) name in certificate CN %.*s\n", (int)dnsnamesize, dnsname);
279 ret = 0;
280 goto cleanup;
281 }
282
283 ret = _gnutls_hostname_compare(dnsname, dnsnamesize, a_hostname, flags);
284 if (ret != 0) {
285 ret = 1;
286 goto cleanup;
287 }
288 }
289
290 /* not found a matching name
291 */
292 ret = 0;
293 cleanup:
294 if (a_hostname != hostname) {
295 gnutls_free(a_hostname);
296 }
297 return ret;
298 }
299