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 "e_os.h" /* for strncasecmp */
11 #include "internal/cryptlib.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)
66 static void *v2i_NAME_CONSTRAINTS(const X509V3_EXT_METHOD *method,
67 X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval)
70 CONF_VALUE tval, *val;
71 STACK_OF(GENERAL_SUBTREE) **ptree = NULL;
72 NAME_CONSTRAINTS *ncons = NULL;
73 GENERAL_SUBTREE *sub = NULL;
75 ncons = NAME_CONSTRAINTS_new();
78 for (i = 0; i < sk_CONF_VALUE_num(nval); i++) {
79 val = sk_CONF_VALUE_value(nval, i);
80 if (strncmp(val->name, "permitted", 9) == 0 && val->name[9]) {
81 ptree = &ncons->permittedSubtrees;
82 tval.name = val->name + 10;
83 } else if (strncmp(val->name, "excluded", 8) == 0 && val->name[8]) {
84 ptree = &ncons->excludedSubtrees;
85 tval.name = val->name + 9;
87 X509V3err(X509V3_F_V2I_NAME_CONSTRAINTS, X509V3_R_INVALID_SYNTAX);
90 tval.value = val->value;
91 sub = GENERAL_SUBTREE_new();
94 if (!v2i_GENERAL_NAME_ex(sub->base, method, ctx, &tval, 1))
97 *ptree = sk_GENERAL_SUBTREE_new_null();
98 if (*ptree == NULL || !sk_GENERAL_SUBTREE_push(*ptree, sub))
106 X509V3err(X509V3_F_V2I_NAME_CONSTRAINTS, ERR_R_MALLOC_FAILURE);
108 NAME_CONSTRAINTS_free(ncons);
109 GENERAL_SUBTREE_free(sub);
114 static int i2r_NAME_CONSTRAINTS(const X509V3_EXT_METHOD *method, void *a,
117 NAME_CONSTRAINTS *ncons = a;
118 do_i2r_name_constraints(method, ncons->permittedSubtrees,
119 bp, ind, "Permitted");
120 do_i2r_name_constraints(method, ncons->excludedSubtrees,
121 bp, ind, "Excluded");
125 static int do_i2r_name_constraints(const X509V3_EXT_METHOD *method,
126 STACK_OF(GENERAL_SUBTREE) *trees,
127 BIO *bp, int ind, const char *name)
129 GENERAL_SUBTREE *tree;
131 if (sk_GENERAL_SUBTREE_num(trees) > 0)
132 BIO_printf(bp, "%*s%s:\n", ind, "", name);
133 for (i = 0; i < sk_GENERAL_SUBTREE_num(trees); i++) {
134 tree = sk_GENERAL_SUBTREE_value(trees, i);
135 BIO_printf(bp, "%*s", ind + 2, "");
136 if (tree->base->type == GEN_IPADD)
137 print_nc_ipadd(bp, tree->base->d.ip);
139 GENERAL_NAME_print(bp, tree->base);
145 static int print_nc_ipadd(BIO *bp, ASN1_OCTET_STRING *ip)
153 BIO_printf(bp, "%d.%d.%d.%d/%d.%d.%d.%d",
154 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
155 } else if (len == 32) {
156 for (i = 0; i < 16; i++) {
157 BIO_printf(bp, "%X", p[0] << 8 | p[1]);
165 BIO_printf(bp, "IP Address:<invalid>");
170 * Check a certificate conforms to a specified set of constraints.
172 * X509_V_OK: All constraints obeyed.
173 * X509_V_ERR_PERMITTED_VIOLATION: Permitted subtree violation.
174 * X509_V_ERR_EXCLUDED_VIOLATION: Excluded subtree violation.
175 * X509_V_ERR_SUBTREE_MINMAX: Min or max values present and matching type.
176 * X509_V_ERR_UNSUPPORTED_CONSTRAINT_TYPE: Unsupported constraint type.
177 * X509_V_ERR_UNSUPPORTED_CONSTRAINT_SYNTAX: bad unsupported constraint syntax.
178 * X509_V_ERR_UNSUPPORTED_NAME_SYNTAX: bad or unsupported syntax of name
181 int NAME_CONSTRAINTS_check(X509 *x, NAME_CONSTRAINTS *nc)
186 nm = X509_get_subject_name(x);
188 if (X509_NAME_entry_count(nm) > 0) {
190 gntmp.type = GEN_DIRNAME;
191 gntmp.d.directoryName = nm;
193 r = nc_match(&gntmp, nc);
198 gntmp.type = GEN_EMAIL;
200 /* Process any email address attributes in subject name */
203 const X509_NAME_ENTRY *ne;
205 i = X509_NAME_get_index_by_NID(nm, NID_pkcs9_emailAddress, i);
208 ne = X509_NAME_get_entry(nm, i);
209 gntmp.d.rfc822Name = X509_NAME_ENTRY_get_data(ne);
210 if (gntmp.d.rfc822Name->type != V_ASN1_IA5STRING)
211 return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
213 r = nc_match(&gntmp, nc);
221 for (i = 0; i < sk_GENERAL_NAME_num(x->altname); i++) {
222 GENERAL_NAME *gen = sk_GENERAL_NAME_value(x->altname, i);
223 r = nc_match(gen, nc);
232 int NAME_CONSTRAINTS_check_CN(X509 *x, NAME_CONSTRAINTS *nc)
240 stmp.type = V_ASN1_IA5STRING;
241 gntmp.type = GEN_DNS;
242 gntmp.d.dNSName = &stmp;
244 nm = X509_get_subject_name(x);
246 /* Process any commonName attributes in subject name */
252 i = X509_NAME_get_index_by_NID(nm, NID_commonName, i);
255 ne = X509_NAME_get_entry(nm, i);
256 hn = X509_NAME_ENTRY_get_data(ne);
257 /* Only process attributes that look like host names */
258 if (asn1_valid_host(hn)) {
260 int hlen = ASN1_STRING_to_UTF8(&h, hn);
262 return X509_V_ERR_OUT_OF_MEM;
267 r = nc_match(&gntmp, nc);
278 static int nc_match(GENERAL_NAME *gen, NAME_CONSTRAINTS *nc)
280 GENERAL_SUBTREE *sub;
284 * Permitted subtrees: if any subtrees exist of matching the type at
285 * least one subtree must match.
288 for (i = 0; i < sk_GENERAL_SUBTREE_num(nc->permittedSubtrees); i++) {
289 sub = sk_GENERAL_SUBTREE_value(nc->permittedSubtrees, i);
290 if (gen->type != sub->base->type)
292 if (sub->minimum || sub->maximum)
293 return X509_V_ERR_SUBTREE_MINMAX;
294 /* If we already have a match don't bother trying any more */
299 r = nc_match_single(gen, sub->base);
302 else if (r != X509_V_ERR_PERMITTED_VIOLATION)
307 return X509_V_ERR_PERMITTED_VIOLATION;
309 /* Excluded subtrees: must not match any of these */
311 for (i = 0; i < sk_GENERAL_SUBTREE_num(nc->excludedSubtrees); i++) {
312 sub = sk_GENERAL_SUBTREE_value(nc->excludedSubtrees, i);
313 if (gen->type != sub->base->type)
315 if (sub->minimum || sub->maximum)
316 return X509_V_ERR_SUBTREE_MINMAX;
318 r = nc_match_single(gen, sub->base);
320 return X509_V_ERR_EXCLUDED_VIOLATION;
321 else if (r != X509_V_ERR_PERMITTED_VIOLATION)
330 static int nc_match_single(GENERAL_NAME *gen, GENERAL_NAME *base)
332 switch (base->type) {
334 return nc_dn(gen->d.directoryName, base->d.directoryName);
337 return nc_dns(gen->d.dNSName, base->d.dNSName);
340 return nc_email(gen->d.rfc822Name, base->d.rfc822Name);
343 return nc_uri(gen->d.uniformResourceIdentifier,
344 base->d.uniformResourceIdentifier);
347 return nc_ip(gen->d.iPAddress, base->d.iPAddress);
350 return X509_V_ERR_UNSUPPORTED_CONSTRAINT_TYPE;
356 * directoryName name constraint matching. The canonical encoding of
357 * X509_NAME makes this comparison easy. It is matched if the subtree is a
358 * subset of the name.
361 static int nc_dn(X509_NAME *nm, X509_NAME *base)
363 /* Ensure canonical encodings are up to date. */
364 if (nm->modified && i2d_X509_NAME(nm, NULL) < 0)
365 return X509_V_ERR_OUT_OF_MEM;
366 if (base->modified && i2d_X509_NAME(base, NULL) < 0)
367 return X509_V_ERR_OUT_OF_MEM;
368 if (base->canon_enclen > nm->canon_enclen)
369 return X509_V_ERR_PERMITTED_VIOLATION;
370 if (memcmp(base->canon_enc, nm->canon_enc, base->canon_enclen))
371 return X509_V_ERR_PERMITTED_VIOLATION;
375 static int nc_dns(ASN1_IA5STRING *dns, ASN1_IA5STRING *base)
377 char *baseptr = (char *)base->data;
378 char *dnsptr = (char *)dns->data;
379 /* Empty matches everything */
383 * Otherwise can add zero or more components on the left so compare RHS
384 * and if dns is longer and expect '.' as preceding character.
386 if (dns->length > base->length) {
387 dnsptr += dns->length - base->length;
388 if (*baseptr != '.' && dnsptr[-1] != '.')
389 return X509_V_ERR_PERMITTED_VIOLATION;
392 if (strcasecmp(baseptr, dnsptr))
393 return X509_V_ERR_PERMITTED_VIOLATION;
399 static int nc_email(ASN1_IA5STRING *eml, ASN1_IA5STRING *base)
401 const char *baseptr = (char *)base->data;
402 const char *emlptr = (char *)eml->data;
404 const char *baseat = strchr(baseptr, '@');
405 const char *emlat = strchr(emlptr, '@');
407 return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
408 /* Special case: initial '.' is RHS match */
409 if (!baseat && (*baseptr == '.')) {
410 if (eml->length > base->length) {
411 emlptr += eml->length - base->length;
412 if (strcasecmp(baseptr, emlptr) == 0)
415 return X509_V_ERR_PERMITTED_VIOLATION;
418 /* If we have anything before '@' match local part */
421 if (baseat != baseptr) {
422 if ((baseat - baseptr) != (emlat - emlptr))
423 return X509_V_ERR_PERMITTED_VIOLATION;
424 /* Case sensitive match of local part */
425 if (strncmp(baseptr, emlptr, emlat - emlptr))
426 return X509_V_ERR_PERMITTED_VIOLATION;
428 /* Position base after '@' */
429 baseptr = baseat + 1;
432 /* Just have hostname left to match: case insensitive */
433 if (strcasecmp(baseptr, emlptr))
434 return X509_V_ERR_PERMITTED_VIOLATION;
440 static int nc_uri(ASN1_IA5STRING *uri, ASN1_IA5STRING *base)
442 const char *baseptr = (char *)base->data;
443 const char *hostptr = (char *)uri->data;
444 const char *p = strchr(hostptr, ':');
446 /* Check for foo:// and skip past it */
447 if (!p || (p[1] != '/') || (p[2] != '/'))
448 return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
451 /* Determine length of hostname part of URI */
453 /* Look for a port indicator as end of hostname first */
455 p = strchr(hostptr, ':');
456 /* Otherwise look for trailing slash */
458 p = strchr(hostptr, '/');
461 hostlen = strlen(hostptr);
463 hostlen = p - hostptr;
466 return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
468 /* Special case: initial '.' is RHS match */
469 if (*baseptr == '.') {
470 if (hostlen > base->length) {
471 p = hostptr + hostlen - base->length;
472 if (strncasecmp(p, baseptr, base->length) == 0)
475 return X509_V_ERR_PERMITTED_VIOLATION;
478 if ((base->length != (int)hostlen)
479 || strncasecmp(hostptr, baseptr, hostlen))
480 return X509_V_ERR_PERMITTED_VIOLATION;
486 static int nc_ip(ASN1_OCTET_STRING *ip, ASN1_OCTET_STRING *base)
488 int hostlen, baselen, i;
489 unsigned char *hostptr, *baseptr, *maskptr;
491 hostlen = ip->length;
492 baseptr = base->data;
493 baselen = base->length;
495 /* Invalid if not IPv4 or IPv6 */
496 if (!((hostlen == 4) || (hostlen == 16)))
497 return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
498 if (!((baselen == 8) || (baselen == 32)))
499 return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
501 /* Do not match IPv4 with IPv6 */
502 if (hostlen * 2 != baselen)
503 return X509_V_ERR_PERMITTED_VIOLATION;
505 maskptr = base->data + hostlen;
507 /* Considering possible not aligned base ipAddress */
508 /* Not checking for wrong mask definition: i.e.: 255.0.255.0 */
509 for (i = 0; i < hostlen; i++)
510 if ((hostptr[i] & maskptr[i]) != (baseptr[i] & maskptr[i]))
511 return X509_V_ERR_PERMITTED_VIOLATION;