1 /* $OpenBSD: tls_verify.c,v 1.29 2023/11/22 18:23:09 op Exp $ */
3 * Copyright (c) 2014 Jeremie Courreges-Anglas <jca@openbsd.org>
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.
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.
20 #include <sys/socket.h>
22 #include <arpa/inet.h>
23 #include <netinet/in.h>
27 #include <openssl/x509v3.h>
30 #include "tls_internal.h"
33 tls_match_name(const char *cert_name, const char *name)
35 const char *cert_domain, *domain, *next_dot;
37 if (strcasecmp(cert_name, name) == 0)
41 if (cert_name[0] == '*') {
45 * - "*.sub.domain.tld"
48 * No attempt to prevent the use of eg. "*.co.uk".
50 cert_domain = &cert_name[1];
52 if (cert_domain[0] == '\0')
55 if (cert_domain[0] != '.')
58 if (cert_domain[1] == '.')
60 next_dot = strchr(&cert_domain[1], '.');
61 /* Disallow "*.bar" */
64 /* Disallow "*.bar.." */
65 if (next_dot[1] == '.')
68 domain = strchr(name, '.');
70 /* No wildcard match against a name with no host part. */
73 /* No wildcard match against a name with no domain part. */
74 if (domain == NULL || strlen(domain) == 1)
77 if (strcasecmp(cert_domain, domain) == 0)
85 * See RFC 5280 section 4.2.1.6 for SubjectAltName details.
86 * alt_match is set to 1 if a matching alternate name is found.
87 * alt_exists is set to 1 if any known alternate name exists in the certificate.
90 tls_check_subject_altname(struct tls *ctx, X509 *cert, const char *name,
91 int *alt_match, int *alt_exists)
93 STACK_OF(GENERAL_NAME) *altname_stack = NULL;
94 union tls_addr addrbuf;
103 altname_stack = X509_get_ext_d2i(cert, NID_subject_alt_name, &critical,
105 if (altname_stack == NULL) {
106 if (critical != -1) {
107 tls_set_errorx(ctx, "error decoding subjectAltName");
113 if (inet_pton(AF_INET, name, &addrbuf) == 1) {
116 } else if (inet_pton(AF_INET6, name, &addrbuf) == 1) {
124 count = sk_GENERAL_NAME_num(altname_stack);
125 for (i = 0; i < count; i++) {
126 GENERAL_NAME *altname;
128 altname = sk_GENERAL_NAME_value(altname_stack, i);
130 if (altname->type == GEN_DNS || altname->type == GEN_IPADD)
133 if (altname->type != type)
136 if (type == GEN_DNS) {
137 const unsigned char *data;
140 format = ASN1_STRING_type(altname->d.dNSName);
141 if (format == V_ASN1_IA5STRING) {
142 data = ASN1_STRING_get0_data(altname->d.dNSName);
143 len = ASN1_STRING_length(altname->d.dNSName);
145 if (len < 0 || (size_t)len != strlen(data)) {
147 "error verifying name '%s': "
148 "NUL byte in subjectAltName, "
149 "probably a malicious certificate",
155 * Per RFC 5280 section 4.2.1.6:
156 * " " is a legal domain name, but that
157 * dNSName must be rejected.
159 if (strcmp(data, " ") == 0) {
161 "error verifying name '%s': "
162 "a dNSName of \" \" must not be "
167 if (tls_match_name(data, name) == 0) {
173 fprintf(stdout, "%s: unhandled subjectAltName "
174 "dNSName encoding (%d)\n", getprogname(),
179 } else if (type == GEN_IPADD) {
180 const unsigned char *data;
183 datalen = ASN1_STRING_length(altname->d.iPAddress);
184 data = ASN1_STRING_get0_data(altname->d.iPAddress);
188 "Unexpected negative length for an "
189 "IP address: %d", datalen);
194 * Per RFC 5280 section 4.2.1.6:
195 * IPv4 must use 4 octets and IPv6 must use 16 octets.
197 if (datalen == addrlen &&
198 memcmp(data, &addrbuf, addrlen) == 0) {
209 sk_GENERAL_NAME_pop_free(altname_stack, GENERAL_NAME_free);
214 tls_check_common_name(struct tls *ctx, X509 *cert, const char *name,
217 unsigned char *utf8_bytes = NULL;
218 X509_NAME *subject_name;
219 char *common_name = NULL;
220 union tls_addr addrbuf;
228 subject_name = X509_get_subject_name(cert);
229 if (subject_name == NULL)
232 lastpos = X509_NAME_get_index_by_NID(subject_name,
233 NID_commonName, lastpos);
238 if (X509_NAME_get_index_by_NID(subject_name, NID_commonName, lastpos)
241 * Having multiple CN's is possible, and even happened back in
242 * the glory days of mullets and Hammer pants. In anything like
243 * a modern TLS cert, CN is as close to deprecated as it gets,
244 * and having more than one is bad. We therefore fail if we have
245 * more than one CN fed to us in the subject, treating the
246 * certificate as hostile.
248 tls_set_errorx(ctx, "error verifying name '%s': "
249 "Certificate subject contains multiple Common Name fields, "
250 "probably a malicious or malformed certificate", name);
254 data = X509_NAME_ENTRY_get_data(X509_NAME_get_entry(subject_name,
257 * Fail if we cannot encode the CN bytes as UTF-8.
259 if ((common_name_len = ASN1_STRING_to_UTF8(&utf8_bytes, data)) < 0) {
260 tls_set_errorx(ctx, "error verifying name '%s': "
261 "Common Name field cannot be encoded as a UTF-8 string, "
262 "probably a malicious certificate", name);
266 * Fail if the CN is of invalid length. RFC 5280 specifies that a CN
267 * must be between 1 and 64 bytes long.
269 if (common_name_len < 1 || common_name_len > 64) {
270 tls_set_errorx(ctx, "error verifying name '%s': "
271 "Common Name field has invalid length, "
272 "probably a malicious certificate", name);
276 * Fail if the resulting text contains a NUL byte.
278 if (memchr(utf8_bytes, 0, common_name_len) != NULL) {
279 tls_set_errorx(ctx, "error verifying name '%s': "
280 "NUL byte in Common Name field, "
281 "probably a malicious certificate", name);
285 common_name = strndup(utf8_bytes, common_name_len);
286 if (common_name == NULL) {
287 tls_set_error(ctx, "out of memory");
292 * We don't want to attempt wildcard matching against IP addresses,
293 * so perform a simple comparison here.
295 if (inet_pton(AF_INET, name, &addrbuf) == 1 ||
296 inet_pton(AF_INET6, name, &addrbuf) == 1) {
297 if (strcmp(common_name, name) == 0)
302 if (tls_match_name(common_name, name) == 0)
315 tls_check_name(struct tls *ctx, X509 *cert, const char *name, int *match)
321 if (tls_check_subject_altname(ctx, cert, name, match,
326 * As per RFC 6125 section 6.4.4, if any known alternate name existed
327 * in the certificate, we do not attempt to match on the CN.
329 if (*match || alt_exists)
332 return tls_check_common_name(ctx, cert, name, match);