2 * Copyright 2003-2016 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
11 #include "internal/cryptlib.h"
12 #include <openssl/asn1t.h>
13 #include <openssl/conf.h>
14 #include <openssl/x509v3.h>
16 #include "internal/x509_int.h"
19 static void *v2i_NAME_CONSTRAINTS(const X509V3_EXT_METHOD *method,
21 STACK_OF(CONF_VALUE) *nval);
22 static int i2r_NAME_CONSTRAINTS(const X509V3_EXT_METHOD *method, void *a,
24 static int do_i2r_name_constraints(const X509V3_EXT_METHOD *method,
25 STACK_OF(GENERAL_SUBTREE) *trees, BIO *bp,
26 int ind, const char *name);
27 static int print_nc_ipadd(BIO *bp, ASN1_OCTET_STRING *ip);
29 static int nc_match(GENERAL_NAME *gen, NAME_CONSTRAINTS *nc);
30 static int nc_match_single(GENERAL_NAME *sub, GENERAL_NAME *gen);
31 static int nc_dn(X509_NAME *sub, X509_NAME *nm);
32 static int nc_dns(ASN1_IA5STRING *sub, ASN1_IA5STRING *dns);
33 static int nc_email(ASN1_IA5STRING *sub, ASN1_IA5STRING *eml);
34 static int nc_uri(ASN1_IA5STRING *uri, ASN1_IA5STRING *base);
35 static int nc_ip(ASN1_OCTET_STRING *ip, ASN1_OCTET_STRING *base);
37 const X509V3_EXT_METHOD v3_name_constraints = {
38 NID_name_constraints, 0,
39 ASN1_ITEM_ref(NAME_CONSTRAINTS),
42 0, v2i_NAME_CONSTRAINTS,
43 i2r_NAME_CONSTRAINTS, 0,
47 ASN1_SEQUENCE(GENERAL_SUBTREE) = {
48 ASN1_SIMPLE(GENERAL_SUBTREE, base, GENERAL_NAME),
49 ASN1_IMP_OPT(GENERAL_SUBTREE, minimum, ASN1_INTEGER, 0),
50 ASN1_IMP_OPT(GENERAL_SUBTREE, maximum, ASN1_INTEGER, 1)
51 } ASN1_SEQUENCE_END(GENERAL_SUBTREE)
53 ASN1_SEQUENCE(NAME_CONSTRAINTS) = {
54 ASN1_IMP_SEQUENCE_OF_OPT(NAME_CONSTRAINTS, permittedSubtrees,
56 ASN1_IMP_SEQUENCE_OF_OPT(NAME_CONSTRAINTS, excludedSubtrees,
58 } ASN1_SEQUENCE_END(NAME_CONSTRAINTS)
61 IMPLEMENT_ASN1_ALLOC_FUNCTIONS(GENERAL_SUBTREE)
62 IMPLEMENT_ASN1_ALLOC_FUNCTIONS(NAME_CONSTRAINTS)
64 static void *v2i_NAME_CONSTRAINTS(const X509V3_EXT_METHOD *method,
65 X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval)
68 CONF_VALUE tval, *val;
69 STACK_OF(GENERAL_SUBTREE) **ptree = NULL;
70 NAME_CONSTRAINTS *ncons = NULL;
71 GENERAL_SUBTREE *sub = NULL;
73 ncons = NAME_CONSTRAINTS_new();
76 for (i = 0; i < sk_CONF_VALUE_num(nval); i++) {
77 val = sk_CONF_VALUE_value(nval, i);
78 if (strncmp(val->name, "permitted", 9) == 0 && val->name[9]) {
79 ptree = &ncons->permittedSubtrees;
80 tval.name = val->name + 10;
81 } else if (strncmp(val->name, "excluded", 8) == 0 && val->name[8]) {
82 ptree = &ncons->excludedSubtrees;
83 tval.name = val->name + 9;
85 X509V3err(X509V3_F_V2I_NAME_CONSTRAINTS, X509V3_R_INVALID_SYNTAX);
88 tval.value = val->value;
89 sub = GENERAL_SUBTREE_new();
92 if (!v2i_GENERAL_NAME_ex(sub->base, method, ctx, &tval, 1))
95 *ptree = sk_GENERAL_SUBTREE_new_null();
96 if (*ptree == NULL || !sk_GENERAL_SUBTREE_push(*ptree, sub))
104 X509V3err(X509V3_F_V2I_NAME_CONSTRAINTS, ERR_R_MALLOC_FAILURE);
106 NAME_CONSTRAINTS_free(ncons);
107 GENERAL_SUBTREE_free(sub);
112 static int i2r_NAME_CONSTRAINTS(const X509V3_EXT_METHOD *method, void *a,
115 NAME_CONSTRAINTS *ncons = a;
116 do_i2r_name_constraints(method, ncons->permittedSubtrees,
117 bp, ind, "Permitted");
118 do_i2r_name_constraints(method, ncons->excludedSubtrees,
119 bp, ind, "Excluded");
123 static int do_i2r_name_constraints(const X509V3_EXT_METHOD *method,
124 STACK_OF(GENERAL_SUBTREE) *trees,
125 BIO *bp, int ind, const char *name)
127 GENERAL_SUBTREE *tree;
129 if (sk_GENERAL_SUBTREE_num(trees) > 0)
130 BIO_printf(bp, "%*s%s:\n", ind, "", name);
131 for (i = 0; i < sk_GENERAL_SUBTREE_num(trees); i++) {
132 tree = sk_GENERAL_SUBTREE_value(trees, i);
133 BIO_printf(bp, "%*s", ind + 2, "");
134 if (tree->base->type == GEN_IPADD)
135 print_nc_ipadd(bp, tree->base->d.ip);
137 GENERAL_NAME_print(bp, tree->base);
143 static int print_nc_ipadd(BIO *bp, ASN1_OCTET_STRING *ip)
151 BIO_printf(bp, "%d.%d.%d.%d/%d.%d.%d.%d",
152 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
153 } else if (len == 32) {
154 for (i = 0; i < 16; i++) {
155 BIO_printf(bp, "%X", p[0] << 8 | p[1]);
163 BIO_printf(bp, "IP Address:<invalid>");
168 * Check a certificate conforms to a specified set of constraints.
170 * X509_V_OK: All constraints obeyed.
171 * X509_V_ERR_PERMITTED_VIOLATION: Permitted subtree violation.
172 * X509_V_ERR_EXCLUDED_VIOLATION: Excluded subtree violation.
173 * X509_V_ERR_SUBTREE_MINMAX: Min or max values present and matching type.
174 * X509_V_ERR_UNSUPPORTED_CONSTRAINT_TYPE: Unsupported constraint type.
175 * X509_V_ERR_UNSUPPORTED_CONSTRAINT_SYNTAX: bad unsupported constraint syntax.
176 * X509_V_ERR_UNSUPPORTED_NAME_SYNTAX: bad or unsupported syntax of name
179 int NAME_CONSTRAINTS_check(X509 *x, NAME_CONSTRAINTS *nc)
184 nm = X509_get_subject_name(x);
186 if (X509_NAME_entry_count(nm) > 0) {
188 gntmp.type = GEN_DIRNAME;
189 gntmp.d.directoryName = nm;
191 r = nc_match(&gntmp, nc);
196 gntmp.type = GEN_EMAIL;
198 /* Process any email address attributes in subject name */
202 i = X509_NAME_get_index_by_NID(nm, NID_pkcs9_emailAddress, i);
205 ne = X509_NAME_get_entry(nm, i);
206 gntmp.d.rfc822Name = X509_NAME_ENTRY_get_data(ne);
207 if (gntmp.d.rfc822Name->type != V_ASN1_IA5STRING)
208 return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
210 r = nc_match(&gntmp, nc);
218 for (i = 0; i < sk_GENERAL_NAME_num(x->altname); i++) {
219 GENERAL_NAME *gen = sk_GENERAL_NAME_value(x->altname, i);
220 r = nc_match(gen, nc);
229 static int nc_match(GENERAL_NAME *gen, NAME_CONSTRAINTS *nc)
231 GENERAL_SUBTREE *sub;
235 * Permitted subtrees: if any subtrees exist of matching the type at
236 * least one subtree must match.
239 for (i = 0; i < sk_GENERAL_SUBTREE_num(nc->permittedSubtrees); i++) {
240 sub = sk_GENERAL_SUBTREE_value(nc->permittedSubtrees, i);
241 if (gen->type != sub->base->type)
243 if (sub->minimum || sub->maximum)
244 return X509_V_ERR_SUBTREE_MINMAX;
245 /* If we already have a match don't bother trying any more */
250 r = nc_match_single(gen, sub->base);
253 else if (r != X509_V_ERR_PERMITTED_VIOLATION)
258 return X509_V_ERR_PERMITTED_VIOLATION;
260 /* Excluded subtrees: must not match any of these */
262 for (i = 0; i < sk_GENERAL_SUBTREE_num(nc->excludedSubtrees); i++) {
263 sub = sk_GENERAL_SUBTREE_value(nc->excludedSubtrees, i);
264 if (gen->type != sub->base->type)
266 if (sub->minimum || sub->maximum)
267 return X509_V_ERR_SUBTREE_MINMAX;
269 r = nc_match_single(gen, sub->base);
271 return X509_V_ERR_EXCLUDED_VIOLATION;
272 else if (r != X509_V_ERR_PERMITTED_VIOLATION)
281 static int nc_match_single(GENERAL_NAME *gen, GENERAL_NAME *base)
283 switch (base->type) {
285 return nc_dn(gen->d.directoryName, base->d.directoryName);
288 return nc_dns(gen->d.dNSName, base->d.dNSName);
291 return nc_email(gen->d.rfc822Name, base->d.rfc822Name);
294 return nc_uri(gen->d.uniformResourceIdentifier,
295 base->d.uniformResourceIdentifier);
298 return nc_ip(gen->d.iPAddress, base->d.iPAddress);
301 return X509_V_ERR_UNSUPPORTED_CONSTRAINT_TYPE;
307 * directoryName name constraint matching. The canonical encoding of
308 * X509_NAME makes this comparison easy. It is matched if the subtree is a
309 * subset of the name.
312 static int nc_dn(X509_NAME *nm, X509_NAME *base)
314 /* Ensure canonical encodings are up to date. */
315 if (nm->modified && i2d_X509_NAME(nm, NULL) < 0)
316 return X509_V_ERR_OUT_OF_MEM;
317 if (base->modified && i2d_X509_NAME(base, NULL) < 0)
318 return X509_V_ERR_OUT_OF_MEM;
319 if (base->canon_enclen > nm->canon_enclen)
320 return X509_V_ERR_PERMITTED_VIOLATION;
321 if (memcmp(base->canon_enc, nm->canon_enc, base->canon_enclen))
322 return X509_V_ERR_PERMITTED_VIOLATION;
326 static int nc_dns(ASN1_IA5STRING *dns, ASN1_IA5STRING *base)
328 char *baseptr = (char *)base->data;
329 char *dnsptr = (char *)dns->data;
330 /* Empty matches everything */
334 * Otherwise can add zero or more components on the left so compare RHS
335 * and if dns is longer and expect '.' as preceding character.
337 if (dns->length > base->length) {
338 dnsptr += dns->length - base->length;
339 if (*baseptr != '.' && dnsptr[-1] != '.')
340 return X509_V_ERR_PERMITTED_VIOLATION;
343 if (strcasecmp(baseptr, dnsptr))
344 return X509_V_ERR_PERMITTED_VIOLATION;
350 static int nc_email(ASN1_IA5STRING *eml, ASN1_IA5STRING *base)
352 const char *baseptr = (char *)base->data;
353 const char *emlptr = (char *)eml->data;
355 const char *baseat = strchr(baseptr, '@');
356 const char *emlat = strchr(emlptr, '@');
358 return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
359 /* Special case: initial '.' is RHS match */
360 if (!baseat && (*baseptr == '.')) {
361 if (eml->length > base->length) {
362 emlptr += eml->length - base->length;
363 if (strcasecmp(baseptr, emlptr) == 0)
366 return X509_V_ERR_PERMITTED_VIOLATION;
369 /* If we have anything before '@' match local part */
372 if (baseat != baseptr) {
373 if ((baseat - baseptr) != (emlat - emlptr))
374 return X509_V_ERR_PERMITTED_VIOLATION;
375 /* Case sensitive match of local part */
376 if (strncmp(baseptr, emlptr, emlat - emlptr))
377 return X509_V_ERR_PERMITTED_VIOLATION;
379 /* Position base after '@' */
380 baseptr = baseat + 1;
383 /* Just have hostname left to match: case insensitive */
384 if (strcasecmp(baseptr, emlptr))
385 return X509_V_ERR_PERMITTED_VIOLATION;
391 static int nc_uri(ASN1_IA5STRING *uri, ASN1_IA5STRING *base)
393 const char *baseptr = (char *)base->data;
394 const char *hostptr = (char *)uri->data;
395 const char *p = strchr(hostptr, ':');
397 /* Check for foo:// and skip past it */
398 if (!p || (p[1] != '/') || (p[2] != '/'))
399 return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
402 /* Determine length of hostname part of URI */
404 /* Look for a port indicator as end of hostname first */
406 p = strchr(hostptr, ':');
407 /* Otherwise look for trailing slash */
409 p = strchr(hostptr, '/');
412 hostlen = strlen(hostptr);
414 hostlen = p - hostptr;
417 return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
419 /* Special case: initial '.' is RHS match */
420 if (*baseptr == '.') {
421 if (hostlen > base->length) {
422 p = hostptr + hostlen - base->length;
423 if (strncasecmp(p, baseptr, base->length) == 0)
426 return X509_V_ERR_PERMITTED_VIOLATION;
429 if ((base->length != (int)hostlen)
430 || strncasecmp(hostptr, baseptr, hostlen))
431 return X509_V_ERR_PERMITTED_VIOLATION;
437 static int nc_ip(ASN1_OCTET_STRING *ip, ASN1_OCTET_STRING *base)
439 int hostlen, baselen, i;
440 unsigned char *hostptr, *baseptr, *maskptr;
442 hostlen = ip->length;
443 baseptr = base->data;
444 baselen = base->length;
446 /* Invalid if not IPv4 or IPv6 */
447 if (!((hostlen == 4) || (hostlen == 16)))
448 return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
449 if (!((baselen == 8) || (baselen == 32)))
450 return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
452 /* Do not match IPv4 with IPv6 */
453 if (hostlen * 2 != baselen)
454 return X509_V_ERR_PERMITTED_VIOLATION;
456 maskptr = base->data + hostlen;
458 /* Considering possible not aligned base ipAddress */
459 /* Not checking for wrong mask definition: i.e.: 255.0.255.0 */
460 for (i = 0; i < hostlen; i++)
461 if ((hostptr[i] & maskptr[i]) != (baseptr[i] & maskptr[i]))
462 return X509_V_ERR_PERMITTED_VIOLATION;