2 * Copyright 2003-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 #include "internal/cryptlib.h"
11 #include "internal/numbers.h"
13 #include "internal/asn1_int.h"
14 #include <openssl/asn1t.h>
15 #include <openssl/conf.h>
16 #include <openssl/x509v3.h>
17 #include <openssl/bn.h>
19 #include "internal/x509_int.h"
22 static void *v2i_NAME_CONSTRAINTS(const X509V3_EXT_METHOD *method,
24 STACK_OF(CONF_VALUE) *nval);
25 static int i2r_NAME_CONSTRAINTS(const X509V3_EXT_METHOD *method, void *a,
27 static int do_i2r_name_constraints(const X509V3_EXT_METHOD *method,
28 STACK_OF(GENERAL_SUBTREE) *trees, BIO *bp,
29 int ind, const char *name);
30 static int print_nc_ipadd(BIO *bp, ASN1_OCTET_STRING *ip);
32 static int nc_match(GENERAL_NAME *gen, NAME_CONSTRAINTS *nc);
33 static int nc_match_single(GENERAL_NAME *sub, GENERAL_NAME *gen);
34 static int nc_dn(X509_NAME *sub, X509_NAME *nm);
35 static int nc_dns(ASN1_IA5STRING *sub, ASN1_IA5STRING *dns);
36 static int nc_email(ASN1_IA5STRING *sub, ASN1_IA5STRING *eml);
37 static int nc_uri(ASN1_IA5STRING *uri, ASN1_IA5STRING *base);
38 static int nc_ip(ASN1_OCTET_STRING *ip, ASN1_OCTET_STRING *base);
40 const X509V3_EXT_METHOD v3_name_constraints = {
41 NID_name_constraints, 0,
42 ASN1_ITEM_ref(NAME_CONSTRAINTS),
45 0, v2i_NAME_CONSTRAINTS,
46 i2r_NAME_CONSTRAINTS, 0,
50 ASN1_SEQUENCE(GENERAL_SUBTREE) = {
51 ASN1_SIMPLE(GENERAL_SUBTREE, base, GENERAL_NAME),
52 ASN1_IMP_OPT(GENERAL_SUBTREE, minimum, ASN1_INTEGER, 0),
53 ASN1_IMP_OPT(GENERAL_SUBTREE, maximum, ASN1_INTEGER, 1)
54 } ASN1_SEQUENCE_END(GENERAL_SUBTREE)
56 ASN1_SEQUENCE(NAME_CONSTRAINTS) = {
57 ASN1_IMP_SEQUENCE_OF_OPT(NAME_CONSTRAINTS, permittedSubtrees,
59 ASN1_IMP_SEQUENCE_OF_OPT(NAME_CONSTRAINTS, excludedSubtrees,
61 } ASN1_SEQUENCE_END(NAME_CONSTRAINTS)
64 IMPLEMENT_ASN1_ALLOC_FUNCTIONS(GENERAL_SUBTREE)
65 IMPLEMENT_ASN1_ALLOC_FUNCTIONS(NAME_CONSTRAINTS)
68 * We cannot use strncasecmp here because that applies locale specific rules.
69 * For example in Turkish 'I' is not the uppercase character for 'i'. We need to
70 * do a simple ASCII case comparison ignoring the locale (that is why we use
71 * numeric constants below).
73 static int ia5ncasecmp(const char *s1, const char *s2, size_t n)
75 for (; n > 0; n--, s1++, s2++) {
77 unsigned char c1 = (unsigned char)*s1, c2 = (unsigned char)*s2;
79 /* Convert to lower case */
80 if (c1 >= 0x41 /* A */ && c1 <= 0x5A /* Z */)
82 if (c2 >= 0x41 /* A */ && c2 <= 0x5A /* Z */)
93 } else if (*s1 == 0) {
94 /* If we get here we know that *s2 == 0 too */
102 static int ia5casecmp(const char *s1, const char *s2)
104 return ia5ncasecmp(s1, s2, SIZE_MAX);
107 static void *v2i_NAME_CONSTRAINTS(const X509V3_EXT_METHOD *method,
108 X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval)
111 CONF_VALUE tval, *val;
112 STACK_OF(GENERAL_SUBTREE) **ptree = NULL;
113 NAME_CONSTRAINTS *ncons = NULL;
114 GENERAL_SUBTREE *sub = NULL;
116 ncons = NAME_CONSTRAINTS_new();
119 for (i = 0; i < sk_CONF_VALUE_num(nval); i++) {
120 val = sk_CONF_VALUE_value(nval, i);
121 if (strncmp(val->name, "permitted", 9) == 0 && val->name[9]) {
122 ptree = &ncons->permittedSubtrees;
123 tval.name = val->name + 10;
124 } else if (strncmp(val->name, "excluded", 8) == 0 && val->name[8]) {
125 ptree = &ncons->excludedSubtrees;
126 tval.name = val->name + 9;
128 X509V3err(X509V3_F_V2I_NAME_CONSTRAINTS, X509V3_R_INVALID_SYNTAX);
131 tval.value = val->value;
132 sub = GENERAL_SUBTREE_new();
135 if (!v2i_GENERAL_NAME_ex(sub->base, method, ctx, &tval, 1))
138 *ptree = sk_GENERAL_SUBTREE_new_null();
139 if (*ptree == NULL || !sk_GENERAL_SUBTREE_push(*ptree, sub))
147 X509V3err(X509V3_F_V2I_NAME_CONSTRAINTS, ERR_R_MALLOC_FAILURE);
149 NAME_CONSTRAINTS_free(ncons);
150 GENERAL_SUBTREE_free(sub);
155 static int i2r_NAME_CONSTRAINTS(const X509V3_EXT_METHOD *method, void *a,
158 NAME_CONSTRAINTS *ncons = a;
159 do_i2r_name_constraints(method, ncons->permittedSubtrees,
160 bp, ind, "Permitted");
161 do_i2r_name_constraints(method, ncons->excludedSubtrees,
162 bp, ind, "Excluded");
166 static int do_i2r_name_constraints(const X509V3_EXT_METHOD *method,
167 STACK_OF(GENERAL_SUBTREE) *trees,
168 BIO *bp, int ind, const char *name)
170 GENERAL_SUBTREE *tree;
172 if (sk_GENERAL_SUBTREE_num(trees) > 0)
173 BIO_printf(bp, "%*s%s:\n", ind, "", name);
174 for (i = 0; i < sk_GENERAL_SUBTREE_num(trees); i++) {
175 tree = sk_GENERAL_SUBTREE_value(trees, i);
176 BIO_printf(bp, "%*s", ind + 2, "");
177 if (tree->base->type == GEN_IPADD)
178 print_nc_ipadd(bp, tree->base->d.ip);
180 GENERAL_NAME_print(bp, tree->base);
186 static int print_nc_ipadd(BIO *bp, ASN1_OCTET_STRING *ip)
194 BIO_printf(bp, "%d.%d.%d.%d/%d.%d.%d.%d",
195 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
196 } else if (len == 32) {
197 for (i = 0; i < 16; i++) {
198 BIO_printf(bp, "%X", p[0] << 8 | p[1]);
206 BIO_printf(bp, "IP Address:<invalid>");
210 #define NAME_CHECK_MAX (1 << 20)
212 static int add_lengths(int *out, int a, int b)
214 /* sk_FOO_num(NULL) returns -1 but is effectively 0 when iterating. */
227 * Check a certificate conforms to a specified set of constraints.
229 * X509_V_OK: All constraints obeyed.
230 * X509_V_ERR_PERMITTED_VIOLATION: Permitted subtree violation.
231 * X509_V_ERR_EXCLUDED_VIOLATION: Excluded subtree violation.
232 * X509_V_ERR_SUBTREE_MINMAX: Min or max values present and matching type.
233 * X509_V_ERR_UNSUPPORTED_CONSTRAINT_TYPE: Unsupported constraint type.
234 * X509_V_ERR_UNSUPPORTED_CONSTRAINT_SYNTAX: bad unsupported constraint syntax.
235 * X509_V_ERR_UNSUPPORTED_NAME_SYNTAX: bad or unsupported syntax of name
238 int NAME_CONSTRAINTS_check(X509 *x, NAME_CONSTRAINTS *nc)
240 int r, i, name_count, constraint_count;
243 nm = X509_get_subject_name(x);
246 * Guard against certificates with an excessive number of names or
247 * constraints causing a computationally expensive name constraints check.
249 if (!add_lengths(&name_count, X509_NAME_entry_count(nm),
250 sk_GENERAL_NAME_num(x->altname))
251 || !add_lengths(&constraint_count,
252 sk_GENERAL_SUBTREE_num(nc->permittedSubtrees),
253 sk_GENERAL_SUBTREE_num(nc->excludedSubtrees))
254 || (name_count > 0 && constraint_count > NAME_CHECK_MAX / name_count))
255 return X509_V_ERR_UNSPECIFIED;
257 if (X509_NAME_entry_count(nm) > 0) {
259 gntmp.type = GEN_DIRNAME;
260 gntmp.d.directoryName = nm;
262 r = nc_match(&gntmp, nc);
267 gntmp.type = GEN_EMAIL;
269 /* Process any email address attributes in subject name */
272 const X509_NAME_ENTRY *ne;
274 i = X509_NAME_get_index_by_NID(nm, NID_pkcs9_emailAddress, i);
277 ne = X509_NAME_get_entry(nm, i);
278 gntmp.d.rfc822Name = X509_NAME_ENTRY_get_data(ne);
279 if (gntmp.d.rfc822Name->type != V_ASN1_IA5STRING)
280 return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
282 r = nc_match(&gntmp, nc);
290 for (i = 0; i < sk_GENERAL_NAME_num(x->altname); i++) {
291 GENERAL_NAME *gen = sk_GENERAL_NAME_value(x->altname, i);
292 r = nc_match(gen, nc);
301 static int cn2dnsid(ASN1_STRING *cn, unsigned char **dnsid, size_t *idlen)
304 unsigned char *utf8_value;
308 /* Don't leave outputs uninitialized */
313 * Per RFC 6125, DNS-IDs representing internationalized domain names appear
314 * in certificates in A-label encoded form:
316 * https://tools.ietf.org/html/rfc6125#section-6.4.2
318 * The same applies to CNs which are intended to represent DNS names.
319 * However, while in the SAN DNS-IDs are IA5Strings, as CNs they may be
320 * needlessly encoded in 16-bit Unicode. We perform a conversion to UTF-8
321 * to ensure that we get an ASCII representation of any CNs that are
322 * representable as ASCII, but just not encoded as ASCII. The UTF-8 form
323 * may contain some non-ASCII octets, and that's fine, such CNs are not
324 * valid legacy DNS names.
326 * Note, 'int' is the return type of ASN1_STRING_to_UTF8() so that's what
327 * we must use for 'utf8_length'.
329 if ((utf8_length = ASN1_STRING_to_UTF8(&utf8_value, cn)) < 0)
330 return X509_V_ERR_OUT_OF_MEM;
333 * Some certificates have had names that include a *trailing* NUL byte.
334 * Remove these harmless NUL characters. They would otherwise yield false
335 * alarms with the following embedded NUL check.
337 while (utf8_length > 0 && utf8_value[utf8_length - 1] == '\0')
340 /* Reject *embedded* NULs */
341 if ((size_t)utf8_length != strlen((char *)utf8_value)) {
342 OPENSSL_free(utf8_value);
343 return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
347 * XXX: Deviation from strict DNS name syntax, also check names with '_'
348 * Check DNS name syntax, any '-' or '.' must be internal,
349 * and on either side of each '.' we can't have a '-' or '.'.
351 * If the name has just one label, we don't consider it a DNS name. This
352 * means that "CN=sometld" cannot be precluded by DNS name constraints, but
353 * that is not a problem.
355 for (i = 0; i < utf8_length; ++i) {
356 unsigned char c = utf8_value[i];
358 if ((c >= 'a' && c <= 'z')
359 || (c >= 'A' && c <= 'Z')
360 || (c >= '0' && c <= '9')
364 /* Dot and hyphen cannot be first or last. */
365 if (i > 0 && i < utf8_length - 1) {
369 * Next to a dot the preceding and following characters must not be
370 * another dot or a hyphen. Otherwise, record that the name is
371 * plausible, since it has two or more labels.
374 && utf8_value[i + 1] != '.'
375 && utf8_value[i - 1] != '-'
376 && utf8_value[i + 1] != '-') {
387 *idlen = (size_t)utf8_length;
390 OPENSSL_free(utf8_value);
395 * Check CN against DNS-ID name constraints.
397 int NAME_CONSTRAINTS_check_CN(X509 *x, NAME_CONSTRAINTS *nc)
400 X509_NAME *nm = X509_get_subject_name(x);
405 stmp.type = V_ASN1_IA5STRING;
406 gntmp.type = GEN_DNS;
407 gntmp.d.dNSName = &stmp;
409 /* Process any commonName attributes in subject name */
414 unsigned char *idval;
417 i = X509_NAME_get_index_by_NID(nm, NID_commonName, i);
420 ne = X509_NAME_get_entry(nm, i);
421 cn = X509_NAME_ENTRY_get_data(ne);
423 /* Only process attributes that look like host names */
424 if ((r = cn2dnsid(cn, &idval, &idlen)) != X509_V_OK)
431 r = nc_match(&gntmp, nc);
440 * Return nonzero if the GeneralSubtree has valid 'minimum' field
441 * (must be absent or 0) and valid 'maximum' field (must be absent).
443 static int nc_minmax_valid(GENERAL_SUBTREE *sub) {
451 bn = ASN1_INTEGER_to_BN(sub->minimum, NULL);
452 if (bn == NULL || !BN_is_zero(bn))
460 static int nc_match(GENERAL_NAME *gen, NAME_CONSTRAINTS *nc)
462 GENERAL_SUBTREE *sub;
466 * Permitted subtrees: if any subtrees exist of matching the type at
467 * least one subtree must match.
470 for (i = 0; i < sk_GENERAL_SUBTREE_num(nc->permittedSubtrees); i++) {
471 sub = sk_GENERAL_SUBTREE_value(nc->permittedSubtrees, i);
472 if (gen->type != sub->base->type)
474 if (!nc_minmax_valid(sub))
475 return X509_V_ERR_SUBTREE_MINMAX;
476 /* If we already have a match don't bother trying any more */
481 r = nc_match_single(gen, sub->base);
484 else if (r != X509_V_ERR_PERMITTED_VIOLATION)
489 return X509_V_ERR_PERMITTED_VIOLATION;
491 /* Excluded subtrees: must not match any of these */
493 for (i = 0; i < sk_GENERAL_SUBTREE_num(nc->excludedSubtrees); i++) {
494 sub = sk_GENERAL_SUBTREE_value(nc->excludedSubtrees, i);
495 if (gen->type != sub->base->type)
497 if (!nc_minmax_valid(sub))
498 return X509_V_ERR_SUBTREE_MINMAX;
500 r = nc_match_single(gen, sub->base);
502 return X509_V_ERR_EXCLUDED_VIOLATION;
503 else if (r != X509_V_ERR_PERMITTED_VIOLATION)
512 static int nc_match_single(GENERAL_NAME *gen, GENERAL_NAME *base)
514 switch (base->type) {
516 return nc_dn(gen->d.directoryName, base->d.directoryName);
519 return nc_dns(gen->d.dNSName, base->d.dNSName);
522 return nc_email(gen->d.rfc822Name, base->d.rfc822Name);
525 return nc_uri(gen->d.uniformResourceIdentifier,
526 base->d.uniformResourceIdentifier);
529 return nc_ip(gen->d.iPAddress, base->d.iPAddress);
532 return X509_V_ERR_UNSUPPORTED_CONSTRAINT_TYPE;
538 * directoryName name constraint matching. The canonical encoding of
539 * X509_NAME makes this comparison easy. It is matched if the subtree is a
540 * subset of the name.
543 static int nc_dn(X509_NAME *nm, X509_NAME *base)
545 /* Ensure canonical encodings are up to date. */
546 if (nm->modified && i2d_X509_NAME(nm, NULL) < 0)
547 return X509_V_ERR_OUT_OF_MEM;
548 if (base->modified && i2d_X509_NAME(base, NULL) < 0)
549 return X509_V_ERR_OUT_OF_MEM;
550 if (base->canon_enclen > nm->canon_enclen)
551 return X509_V_ERR_PERMITTED_VIOLATION;
552 if (memcmp(base->canon_enc, nm->canon_enc, base->canon_enclen))
553 return X509_V_ERR_PERMITTED_VIOLATION;
557 static int nc_dns(ASN1_IA5STRING *dns, ASN1_IA5STRING *base)
559 char *baseptr = (char *)base->data;
560 char *dnsptr = (char *)dns->data;
561 /* Empty matches everything */
565 * Otherwise can add zero or more components on the left so compare RHS
566 * and if dns is longer and expect '.' as preceding character.
568 if (dns->length > base->length) {
569 dnsptr += dns->length - base->length;
570 if (*baseptr != '.' && dnsptr[-1] != '.')
571 return X509_V_ERR_PERMITTED_VIOLATION;
574 if (ia5casecmp(baseptr, dnsptr))
575 return X509_V_ERR_PERMITTED_VIOLATION;
581 static int nc_email(ASN1_IA5STRING *eml, ASN1_IA5STRING *base)
583 const char *baseptr = (char *)base->data;
584 const char *emlptr = (char *)eml->data;
586 const char *baseat = strchr(baseptr, '@');
587 const char *emlat = strchr(emlptr, '@');
589 return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
590 /* Special case: initial '.' is RHS match */
591 if (!baseat && (*baseptr == '.')) {
592 if (eml->length > base->length) {
593 emlptr += eml->length - base->length;
594 if (ia5casecmp(baseptr, emlptr) == 0)
597 return X509_V_ERR_PERMITTED_VIOLATION;
600 /* If we have anything before '@' match local part */
603 if (baseat != baseptr) {
604 if ((baseat - baseptr) != (emlat - emlptr))
605 return X509_V_ERR_PERMITTED_VIOLATION;
606 /* Case sensitive match of local part */
607 if (strncmp(baseptr, emlptr, emlat - emlptr))
608 return X509_V_ERR_PERMITTED_VIOLATION;
610 /* Position base after '@' */
611 baseptr = baseat + 1;
614 /* Just have hostname left to match: case insensitive */
615 if (ia5casecmp(baseptr, emlptr))
616 return X509_V_ERR_PERMITTED_VIOLATION;
622 static int nc_uri(ASN1_IA5STRING *uri, ASN1_IA5STRING *base)
624 const char *baseptr = (char *)base->data;
625 const char *hostptr = (char *)uri->data;
626 const char *p = strchr(hostptr, ':');
628 /* Check for foo:// and skip past it */
629 if (!p || (p[1] != '/') || (p[2] != '/'))
630 return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
633 /* Determine length of hostname part of URI */
635 /* Look for a port indicator as end of hostname first */
637 p = strchr(hostptr, ':');
638 /* Otherwise look for trailing slash */
640 p = strchr(hostptr, '/');
643 hostlen = strlen(hostptr);
645 hostlen = p - hostptr;
648 return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
650 /* Special case: initial '.' is RHS match */
651 if (*baseptr == '.') {
652 if (hostlen > base->length) {
653 p = hostptr + hostlen - base->length;
654 if (ia5ncasecmp(p, baseptr, base->length) == 0)
657 return X509_V_ERR_PERMITTED_VIOLATION;
660 if ((base->length != (int)hostlen)
661 || ia5ncasecmp(hostptr, baseptr, hostlen))
662 return X509_V_ERR_PERMITTED_VIOLATION;
668 static int nc_ip(ASN1_OCTET_STRING *ip, ASN1_OCTET_STRING *base)
670 int hostlen, baselen, i;
671 unsigned char *hostptr, *baseptr, *maskptr;
673 hostlen = ip->length;
674 baseptr = base->data;
675 baselen = base->length;
677 /* Invalid if not IPv4 or IPv6 */
678 if (!((hostlen == 4) || (hostlen == 16)))
679 return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
680 if (!((baselen == 8) || (baselen == 32)))
681 return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
683 /* Do not match IPv4 with IPv6 */
684 if (hostlen * 2 != baselen)
685 return X509_V_ERR_PERMITTED_VIOLATION;
687 maskptr = base->data + hostlen;
689 /* Considering possible not aligned base ipAddress */
690 /* Not checking for wrong mask definition: i.e.: 255.0.255.0 */
691 for (i = 0; i < hostlen; i++)
692 if ((hostptr[i] & maskptr[i]) != (baseptr[i] & maskptr[i]))
693 return X509_V_ERR_PERMITTED_VIOLATION;