2 * Copyright 2003-2017 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>
18 #include "internal/x509_int.h"
21 static void *v2i_NAME_CONSTRAINTS(const X509V3_EXT_METHOD *method,
23 STACK_OF(CONF_VALUE) *nval);
24 static int i2r_NAME_CONSTRAINTS(const X509V3_EXT_METHOD *method, void *a,
26 static int do_i2r_name_constraints(const X509V3_EXT_METHOD *method,
27 STACK_OF(GENERAL_SUBTREE) *trees, BIO *bp,
28 int ind, const char *name);
29 static int print_nc_ipadd(BIO *bp, ASN1_OCTET_STRING *ip);
31 static int nc_match(GENERAL_NAME *gen, NAME_CONSTRAINTS *nc);
32 static int nc_match_single(GENERAL_NAME *sub, GENERAL_NAME *gen);
33 static int nc_dn(X509_NAME *sub, X509_NAME *nm);
34 static int nc_dns(ASN1_IA5STRING *sub, ASN1_IA5STRING *dns);
35 static int nc_email(ASN1_IA5STRING *sub, ASN1_IA5STRING *eml);
36 static int nc_uri(ASN1_IA5STRING *uri, ASN1_IA5STRING *base);
37 static int nc_ip(ASN1_OCTET_STRING *ip, ASN1_OCTET_STRING *base);
39 const X509V3_EXT_METHOD v3_name_constraints = {
40 NID_name_constraints, 0,
41 ASN1_ITEM_ref(NAME_CONSTRAINTS),
44 0, v2i_NAME_CONSTRAINTS,
45 i2r_NAME_CONSTRAINTS, 0,
49 ASN1_SEQUENCE(GENERAL_SUBTREE) = {
50 ASN1_SIMPLE(GENERAL_SUBTREE, base, GENERAL_NAME),
51 ASN1_IMP_OPT(GENERAL_SUBTREE, minimum, ASN1_INTEGER, 0),
52 ASN1_IMP_OPT(GENERAL_SUBTREE, maximum, ASN1_INTEGER, 1)
53 } ASN1_SEQUENCE_END(GENERAL_SUBTREE)
55 ASN1_SEQUENCE(NAME_CONSTRAINTS) = {
56 ASN1_IMP_SEQUENCE_OF_OPT(NAME_CONSTRAINTS, permittedSubtrees,
58 ASN1_IMP_SEQUENCE_OF_OPT(NAME_CONSTRAINTS, excludedSubtrees,
60 } ASN1_SEQUENCE_END(NAME_CONSTRAINTS)
63 IMPLEMENT_ASN1_ALLOC_FUNCTIONS(GENERAL_SUBTREE)
64 IMPLEMENT_ASN1_ALLOC_FUNCTIONS(NAME_CONSTRAINTS)
67 * We cannot use strncasecmp here because that applies locale specific rules.
68 * For example in Turkish 'I' is not the uppercase character for 'i'. We need to
69 * do a simple ASCII case comparison ignoring the locale (that is why we use
70 * numeric constants below).
72 static int ia5ncasecmp(const char *s1, const char *s2, size_t n)
74 for (; n > 0; n--, s1++, s2++) {
76 unsigned char c1 = (unsigned char)*s1, c2 = (unsigned char)*s2;
78 /* Convert to lower case */
79 if (c1 >= 0x41 /* A */ && c1 <= 0x5A /* Z */)
81 if (c2 >= 0x41 /* A */ && c2 <= 0x5A /* Z */)
92 } else if (*s1 == 0) {
93 /* If we get here we know that *s2 == 0 too */
101 static int ia5casecmp(const char *s1, const char *s2)
103 return ia5ncasecmp(s1, s2, SIZE_MAX);
106 static void *v2i_NAME_CONSTRAINTS(const X509V3_EXT_METHOD *method,
107 X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval)
110 CONF_VALUE tval, *val;
111 STACK_OF(GENERAL_SUBTREE) **ptree = NULL;
112 NAME_CONSTRAINTS *ncons = NULL;
113 GENERAL_SUBTREE *sub = NULL;
115 ncons = NAME_CONSTRAINTS_new();
118 for (i = 0; i < sk_CONF_VALUE_num(nval); i++) {
119 val = sk_CONF_VALUE_value(nval, i);
120 if (strncmp(val->name, "permitted", 9) == 0 && val->name[9]) {
121 ptree = &ncons->permittedSubtrees;
122 tval.name = val->name + 10;
123 } else if (strncmp(val->name, "excluded", 8) == 0 && val->name[8]) {
124 ptree = &ncons->excludedSubtrees;
125 tval.name = val->name + 9;
127 X509V3err(X509V3_F_V2I_NAME_CONSTRAINTS, X509V3_R_INVALID_SYNTAX);
130 tval.value = val->value;
131 sub = GENERAL_SUBTREE_new();
134 if (!v2i_GENERAL_NAME_ex(sub->base, method, ctx, &tval, 1))
137 *ptree = sk_GENERAL_SUBTREE_new_null();
138 if (*ptree == NULL || !sk_GENERAL_SUBTREE_push(*ptree, sub))
146 X509V3err(X509V3_F_V2I_NAME_CONSTRAINTS, ERR_R_MALLOC_FAILURE);
148 NAME_CONSTRAINTS_free(ncons);
149 GENERAL_SUBTREE_free(sub);
154 static int i2r_NAME_CONSTRAINTS(const X509V3_EXT_METHOD *method, void *a,
157 NAME_CONSTRAINTS *ncons = a;
158 do_i2r_name_constraints(method, ncons->permittedSubtrees,
159 bp, ind, "Permitted");
160 do_i2r_name_constraints(method, ncons->excludedSubtrees,
161 bp, ind, "Excluded");
165 static int do_i2r_name_constraints(const X509V3_EXT_METHOD *method,
166 STACK_OF(GENERAL_SUBTREE) *trees,
167 BIO *bp, int ind, const char *name)
169 GENERAL_SUBTREE *tree;
171 if (sk_GENERAL_SUBTREE_num(trees) > 0)
172 BIO_printf(bp, "%*s%s:\n", ind, "", name);
173 for (i = 0; i < sk_GENERAL_SUBTREE_num(trees); i++) {
174 tree = sk_GENERAL_SUBTREE_value(trees, i);
175 BIO_printf(bp, "%*s", ind + 2, "");
176 if (tree->base->type == GEN_IPADD)
177 print_nc_ipadd(bp, tree->base->d.ip);
179 GENERAL_NAME_print(bp, tree->base);
185 static int print_nc_ipadd(BIO *bp, ASN1_OCTET_STRING *ip)
193 BIO_printf(bp, "%d.%d.%d.%d/%d.%d.%d.%d",
194 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
195 } else if (len == 32) {
196 for (i = 0; i < 16; i++) {
197 BIO_printf(bp, "%X", p[0] << 8 | p[1]);
205 BIO_printf(bp, "IP Address:<invalid>");
209 #define NAME_CHECK_MAX (1 << 20)
211 static int add_lengths(int *out, int a, int b)
213 /* sk_FOO_num(NULL) returns -1 but is effectively 0 when iterating. */
226 * Check a certificate conforms to a specified set of constraints.
228 * X509_V_OK: All constraints obeyed.
229 * X509_V_ERR_PERMITTED_VIOLATION: Permitted subtree violation.
230 * X509_V_ERR_EXCLUDED_VIOLATION: Excluded subtree violation.
231 * X509_V_ERR_SUBTREE_MINMAX: Min or max values present and matching type.
232 * X509_V_ERR_UNSUPPORTED_CONSTRAINT_TYPE: Unsupported constraint type.
233 * X509_V_ERR_UNSUPPORTED_CONSTRAINT_SYNTAX: bad unsupported constraint syntax.
234 * X509_V_ERR_UNSUPPORTED_NAME_SYNTAX: bad or unsupported syntax of name
237 int NAME_CONSTRAINTS_check(X509 *x, NAME_CONSTRAINTS *nc)
239 int r, i, name_count, constraint_count;
242 nm = X509_get_subject_name(x);
245 * Guard against certificates with an excessive number of names or
246 * constraints causing a computationally expensive name constraints check.
248 if (!add_lengths(&name_count, X509_NAME_entry_count(nm),
249 sk_GENERAL_NAME_num(x->altname))
250 || !add_lengths(&constraint_count,
251 sk_GENERAL_SUBTREE_num(nc->permittedSubtrees),
252 sk_GENERAL_SUBTREE_num(nc->excludedSubtrees))
253 || (name_count > 0 && constraint_count > NAME_CHECK_MAX / name_count))
254 return X509_V_ERR_UNSPECIFIED;
256 if (X509_NAME_entry_count(nm) > 0) {
258 gntmp.type = GEN_DIRNAME;
259 gntmp.d.directoryName = nm;
261 r = nc_match(&gntmp, nc);
266 gntmp.type = GEN_EMAIL;
268 /* Process any email address attributes in subject name */
271 const X509_NAME_ENTRY *ne;
273 i = X509_NAME_get_index_by_NID(nm, NID_pkcs9_emailAddress, i);
276 ne = X509_NAME_get_entry(nm, i);
277 gntmp.d.rfc822Name = X509_NAME_ENTRY_get_data(ne);
278 if (gntmp.d.rfc822Name->type != V_ASN1_IA5STRING)
279 return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
281 r = nc_match(&gntmp, nc);
289 for (i = 0; i < sk_GENERAL_NAME_num(x->altname); i++) {
290 GENERAL_NAME *gen = sk_GENERAL_NAME_value(x->altname, i);
291 r = nc_match(gen, nc);
300 int NAME_CONSTRAINTS_check_CN(X509 *x, NAME_CONSTRAINTS *nc)
308 stmp.type = V_ASN1_IA5STRING;
309 gntmp.type = GEN_DNS;
310 gntmp.d.dNSName = &stmp;
312 nm = X509_get_subject_name(x);
314 /* Process any commonName attributes in subject name */
320 i = X509_NAME_get_index_by_NID(nm, NID_commonName, i);
323 ne = X509_NAME_get_entry(nm, i);
324 hn = X509_NAME_ENTRY_get_data(ne);
325 /* Only process attributes that look like host names */
326 if (asn1_valid_host(hn)) {
328 int hlen = ASN1_STRING_to_UTF8(&h, hn);
330 return X509_V_ERR_OUT_OF_MEM;
335 r = nc_match(&gntmp, nc);
346 static int nc_match(GENERAL_NAME *gen, NAME_CONSTRAINTS *nc)
348 GENERAL_SUBTREE *sub;
352 * Permitted subtrees: if any subtrees exist of matching the type at
353 * least one subtree must match.
356 for (i = 0; i < sk_GENERAL_SUBTREE_num(nc->permittedSubtrees); i++) {
357 sub = sk_GENERAL_SUBTREE_value(nc->permittedSubtrees, i);
358 if (gen->type != sub->base->type)
360 if (sub->minimum || sub->maximum)
361 return X509_V_ERR_SUBTREE_MINMAX;
362 /* If we already have a match don't bother trying any more */
367 r = nc_match_single(gen, sub->base);
370 else if (r != X509_V_ERR_PERMITTED_VIOLATION)
375 return X509_V_ERR_PERMITTED_VIOLATION;
377 /* Excluded subtrees: must not match any of these */
379 for (i = 0; i < sk_GENERAL_SUBTREE_num(nc->excludedSubtrees); i++) {
380 sub = sk_GENERAL_SUBTREE_value(nc->excludedSubtrees, i);
381 if (gen->type != sub->base->type)
383 if (sub->minimum || sub->maximum)
384 return X509_V_ERR_SUBTREE_MINMAX;
386 r = nc_match_single(gen, sub->base);
388 return X509_V_ERR_EXCLUDED_VIOLATION;
389 else if (r != X509_V_ERR_PERMITTED_VIOLATION)
398 static int nc_match_single(GENERAL_NAME *gen, GENERAL_NAME *base)
400 switch (base->type) {
402 return nc_dn(gen->d.directoryName, base->d.directoryName);
405 return nc_dns(gen->d.dNSName, base->d.dNSName);
408 return nc_email(gen->d.rfc822Name, base->d.rfc822Name);
411 return nc_uri(gen->d.uniformResourceIdentifier,
412 base->d.uniformResourceIdentifier);
415 return nc_ip(gen->d.iPAddress, base->d.iPAddress);
418 return X509_V_ERR_UNSUPPORTED_CONSTRAINT_TYPE;
424 * directoryName name constraint matching. The canonical encoding of
425 * X509_NAME makes this comparison easy. It is matched if the subtree is a
426 * subset of the name.
429 static int nc_dn(X509_NAME *nm, X509_NAME *base)
431 /* Ensure canonical encodings are up to date. */
432 if (nm->modified && i2d_X509_NAME(nm, NULL) < 0)
433 return X509_V_ERR_OUT_OF_MEM;
434 if (base->modified && i2d_X509_NAME(base, NULL) < 0)
435 return X509_V_ERR_OUT_OF_MEM;
436 if (base->canon_enclen > nm->canon_enclen)
437 return X509_V_ERR_PERMITTED_VIOLATION;
438 if (memcmp(base->canon_enc, nm->canon_enc, base->canon_enclen))
439 return X509_V_ERR_PERMITTED_VIOLATION;
443 static int nc_dns(ASN1_IA5STRING *dns, ASN1_IA5STRING *base)
445 char *baseptr = (char *)base->data;
446 char *dnsptr = (char *)dns->data;
447 /* Empty matches everything */
451 * Otherwise can add zero or more components on the left so compare RHS
452 * and if dns is longer and expect '.' as preceding character.
454 if (dns->length > base->length) {
455 dnsptr += dns->length - base->length;
456 if (*baseptr != '.' && dnsptr[-1] != '.')
457 return X509_V_ERR_PERMITTED_VIOLATION;
460 if (ia5casecmp(baseptr, dnsptr))
461 return X509_V_ERR_PERMITTED_VIOLATION;
467 static int nc_email(ASN1_IA5STRING *eml, ASN1_IA5STRING *base)
469 const char *baseptr = (char *)base->data;
470 const char *emlptr = (char *)eml->data;
472 const char *baseat = strchr(baseptr, '@');
473 const char *emlat = strchr(emlptr, '@');
475 return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
476 /* Special case: initial '.' is RHS match */
477 if (!baseat && (*baseptr == '.')) {
478 if (eml->length > base->length) {
479 emlptr += eml->length - base->length;
480 if (ia5casecmp(baseptr, emlptr) == 0)
483 return X509_V_ERR_PERMITTED_VIOLATION;
486 /* If we have anything before '@' match local part */
489 if (baseat != baseptr) {
490 if ((baseat - baseptr) != (emlat - emlptr))
491 return X509_V_ERR_PERMITTED_VIOLATION;
492 /* Case sensitive match of local part */
493 if (strncmp(baseptr, emlptr, emlat - emlptr))
494 return X509_V_ERR_PERMITTED_VIOLATION;
496 /* Position base after '@' */
497 baseptr = baseat + 1;
500 /* Just have hostname left to match: case insensitive */
501 if (ia5casecmp(baseptr, emlptr))
502 return X509_V_ERR_PERMITTED_VIOLATION;
508 static int nc_uri(ASN1_IA5STRING *uri, ASN1_IA5STRING *base)
510 const char *baseptr = (char *)base->data;
511 const char *hostptr = (char *)uri->data;
512 const char *p = strchr(hostptr, ':');
514 /* Check for foo:// and skip past it */
515 if (!p || (p[1] != '/') || (p[2] != '/'))
516 return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
519 /* Determine length of hostname part of URI */
521 /* Look for a port indicator as end of hostname first */
523 p = strchr(hostptr, ':');
524 /* Otherwise look for trailing slash */
526 p = strchr(hostptr, '/');
529 hostlen = strlen(hostptr);
531 hostlen = p - hostptr;
534 return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
536 /* Special case: initial '.' is RHS match */
537 if (*baseptr == '.') {
538 if (hostlen > base->length) {
539 p = hostptr + hostlen - base->length;
540 if (ia5ncasecmp(p, baseptr, base->length) == 0)
543 return X509_V_ERR_PERMITTED_VIOLATION;
546 if ((base->length != (int)hostlen)
547 || ia5ncasecmp(hostptr, baseptr, hostlen))
548 return X509_V_ERR_PERMITTED_VIOLATION;
554 static int nc_ip(ASN1_OCTET_STRING *ip, ASN1_OCTET_STRING *base)
556 int hostlen, baselen, i;
557 unsigned char *hostptr, *baseptr, *maskptr;
559 hostlen = ip->length;
560 baseptr = base->data;
561 baselen = base->length;
563 /* Invalid if not IPv4 or IPv6 */
564 if (!((hostlen == 4) || (hostlen == 16)))
565 return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
566 if (!((baselen == 8) || (baselen == 32)))
567 return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
569 /* Do not match IPv4 with IPv6 */
570 if (hostlen * 2 != baselen)
571 return X509_V_ERR_PERMITTED_VIOLATION;
573 maskptr = base->data + hostlen;
575 /* Considering possible not aligned base ipAddress */
576 /* Not checking for wrong mask definition: i.e.: 255.0.255.0 */
577 for (i = 0; i < hostlen; i++)
578 if ((hostptr[i] & maskptr[i]) != (baseptr[i] & maskptr[i]))
579 return X509_V_ERR_PERMITTED_VIOLATION;