1 /* $OpenBSD: tls_verify.c,v 1.16 2016/08/02 07:47:11 jsing Exp $ */ 2 /* 3 * Copyright (c) 2014 Jeremie Courreges-Anglas <jca@openbsd.org> 4 * 5 * Permission to use, copy, modify, and distribute this software for any 6 * purpose with or without fee is hereby granted, provided that the above 7 * copyright notice and this permission notice appear in all copies. 8 * 9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 */ 17 18 #include <sys/socket.h> 19 20 #include <arpa/inet.h> 21 #include <netinet/in.h> 22 23 #include <string.h> 24 25 #include <openssl/x509v3.h> 26 27 #include "tls_internal.h" 28 29 static int tls_match_name(const char *cert_name, const char *name); 30 static int tls_check_subject_altname(struct tls *ctx, X509 *cert, 31 const char *name); 32 static int tls_check_common_name(struct tls *ctx, X509 *cert, const char *name); 33 34 static int 35 tls_match_name(const char *cert_name, const char *name) 36 { 37 const char *cert_domain, *domain, *next_dot; 38 39 if (strcasecmp(cert_name, name) == 0) 40 return 0; 41 42 /* Wildcard match? */ 43 if (cert_name[0] == '*') { 44 /* 45 * Valid wildcards: 46 * - "*.domain.tld" 47 * - "*.sub.domain.tld" 48 * - etc. 49 * Reject "*.tld". 50 * No attempt to prevent the use of eg. "*.co.uk". 51 */ 52 cert_domain = &cert_name[1]; 53 /* Disallow "*" */ 54 if (cert_domain[0] == '\0') 55 return -1; 56 /* Disallow "*foo" */ 57 if (cert_domain[0] != '.') 58 return -1; 59 /* Disallow "*.." */ 60 if (cert_domain[1] == '.') 61 return -1; 62 next_dot = strchr(&cert_domain[1], '.'); 63 /* Disallow "*.bar" */ 64 if (next_dot == NULL) 65 return -1; 66 /* Disallow "*.bar.." */ 67 if (next_dot[1] == '.') 68 return -1; 69 70 domain = strchr(name, '.'); 71 72 /* No wildcard match against a name with no host part. */ 73 if (name[0] == '.') 74 return -1; 75 /* No wildcard match against a name with no domain part. */ 76 if (domain == NULL || strlen(domain) == 1) 77 return -1; 78 79 if (strcasecmp(cert_domain, domain) == 0) 80 return 0; 81 } 82 83 return -1; 84 } 85 86 /* See RFC 5280 section 4.2.1.6 for SubjectAltName details. */ 87 static int 88 tls_check_subject_altname(struct tls *ctx, X509 *cert, const char *name) 89 { 90 STACK_OF(GENERAL_NAME) *altname_stack = NULL; 91 union tls_addr addrbuf; 92 int addrlen, type; 93 int count, i; 94 int rv = -1; 95 96 altname_stack = X509_get_ext_d2i(cert, NID_subject_alt_name, 97 NULL, NULL); 98 if (altname_stack == NULL) 99 return -1; 100 101 if (inet_pton(AF_INET, name, &addrbuf) == 1) { 102 type = GEN_IPADD; 103 addrlen = 4; 104 } else if (inet_pton(AF_INET6, name, &addrbuf) == 1) { 105 type = GEN_IPADD; 106 addrlen = 16; 107 } else { 108 type = GEN_DNS; 109 addrlen = 0; 110 } 111 112 count = sk_GENERAL_NAME_num(altname_stack); 113 for (i = 0; i < count; i++) { 114 GENERAL_NAME *altname; 115 116 altname = sk_GENERAL_NAME_value(altname_stack, i); 117 if (altname->type != type) 118 continue; 119 120 if (type == GEN_DNS) { 121 unsigned char *data; 122 int format, len; 123 124 format = ASN1_STRING_type(altname->d.dNSName); 125 if (format == V_ASN1_IA5STRING) { 126 data = ASN1_STRING_data(altname->d.dNSName); 127 len = ASN1_STRING_length(altname->d.dNSName); 128 129 if (len < 0 || len != strlen(data)) { 130 tls_set_errorx(ctx, 131 "error verifying name '%s': " 132 "NUL byte in subjectAltName, " 133 "probably a malicious certificate", 134 name); 135 rv = -2; 136 break; 137 } 138 139 /* 140 * Per RFC 5280 section 4.2.1.6: 141 * " " is a legal domain name, but that 142 * dNSName must be rejected. 143 */ 144 if (strcmp(data, " ") == 0) { 145 tls_set_error(ctx, 146 "error verifying name '%s': " 147 "a dNSName of \" \" must not be " 148 "used", name); 149 rv = -2; 150 break; 151 } 152 153 if (tls_match_name(data, name) == 0) { 154 rv = 0; 155 break; 156 } 157 } else { 158 #ifdef DEBUG 159 fprintf(stdout, "%s: unhandled subjectAltName " 160 "dNSName encoding (%d)\n", getprogname(), 161 format); 162 #endif 163 } 164 165 } else if (type == GEN_IPADD) { 166 unsigned char *data; 167 int datalen; 168 169 datalen = ASN1_STRING_length(altname->d.iPAddress); 170 data = ASN1_STRING_data(altname->d.iPAddress); 171 172 if (datalen < 0) { 173 tls_set_errorx(ctx, 174 "Unexpected negative length for an " 175 "IP address: %d", datalen); 176 rv = -2; 177 break; 178 } 179 180 /* 181 * Per RFC 5280 section 4.2.1.6: 182 * IPv4 must use 4 octets and IPv6 must use 16 octets. 183 */ 184 if (datalen == addrlen && 185 memcmp(data, &addrbuf, addrlen) == 0) { 186 rv = 0; 187 break; 188 } 189 } 190 } 191 192 sk_GENERAL_NAME_pop_free(altname_stack, GENERAL_NAME_free); 193 return rv; 194 } 195 196 static int 197 tls_check_common_name(struct tls *ctx, X509 *cert, const char *name) 198 { 199 X509_NAME *subject_name; 200 char *common_name = NULL; 201 union tls_addr addrbuf; 202 int common_name_len; 203 int rv = -1; 204 205 subject_name = X509_get_subject_name(cert); 206 if (subject_name == NULL) 207 goto out; 208 209 common_name_len = X509_NAME_get_text_by_NID(subject_name, 210 NID_commonName, NULL, 0); 211 if (common_name_len < 0) 212 goto out; 213 214 common_name = calloc(common_name_len + 1, 1); 215 if (common_name == NULL) 216 goto out; 217 218 X509_NAME_get_text_by_NID(subject_name, NID_commonName, common_name, 219 common_name_len + 1); 220 221 /* NUL bytes in CN? */ 222 if (common_name_len != strlen(common_name)) { 223 tls_set_errorx(ctx, "error verifying name '%s': " 224 "NUL byte in Common Name field, " 225 "probably a malicious certificate", name); 226 rv = -2; 227 goto out; 228 } 229 230 if (inet_pton(AF_INET, name, &addrbuf) == 1 || 231 inet_pton(AF_INET6, name, &addrbuf) == 1) { 232 /* 233 * We don't want to attempt wildcard matching against IP 234 * addresses, so perform a simple comparison here. 235 */ 236 if (strcmp(common_name, name) == 0) 237 rv = 0; 238 else 239 rv = -1; 240 goto out; 241 } 242 243 if (tls_match_name(common_name, name) == 0) 244 rv = 0; 245 out: 246 free(common_name); 247 return rv; 248 } 249 250 int 251 tls_check_name(struct tls *ctx, X509 *cert, const char *name) 252 { 253 int rv; 254 255 rv = tls_check_subject_altname(ctx, cert, name); 256 if (rv == 0 || rv == -2) 257 return rv; 258 259 return tls_check_common_name(ctx, cert, name); 260 } 261