1 /*
2 * Written by Dr Stephen N Henson ([email protected]) for the OpenSSL
3 * project.
4 */
5 /* ====================================================================
6 * Copyright (c) 2003 The OpenSSL Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * [email protected].
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * ([email protected]). This product includes software written by Tim
55 * Hudson ([email protected]). */
56
57 #include <stdio.h>
58 #include <string.h>
59
60 #include <openssl/asn1t.h>
61 #include <openssl/conf.h>
62 #include <openssl/err.h>
63 #include <openssl/mem.h>
64 #include <openssl/obj.h>
65 #include <openssl/x509.h>
66
67 #include "../internal.h"
68 #include "internal.h"
69
70
71 static void *v2i_NAME_CONSTRAINTS(const X509V3_EXT_METHOD *method,
72 const X509V3_CTX *ctx,
73 const STACK_OF(CONF_VALUE) *nval);
74 static int i2r_NAME_CONSTRAINTS(const X509V3_EXT_METHOD *method, void *a,
75 BIO *bp, int ind);
76 static int do_i2r_name_constraints(const X509V3_EXT_METHOD *method,
77 STACK_OF(GENERAL_SUBTREE) *trees, BIO *bp,
78 int ind, const char *name);
79 static int print_nc_ipadd(BIO *bp, const ASN1_OCTET_STRING *ip);
80
81 static int nc_match(GENERAL_NAME *gen, NAME_CONSTRAINTS *nc);
82 static int nc_match_single(GENERAL_NAME *sub, GENERAL_NAME *gen);
83 static int nc_dn(X509_NAME *sub, X509_NAME *nm);
84 static int nc_dns(const ASN1_IA5STRING *sub, const ASN1_IA5STRING *dns);
85 static int nc_email(const ASN1_IA5STRING *sub, const ASN1_IA5STRING *eml);
86 static int nc_uri(const ASN1_IA5STRING *uri, const ASN1_IA5STRING *base);
87
88 const X509V3_EXT_METHOD v3_name_constraints = {
89 NID_name_constraints,
90 0,
91 ASN1_ITEM_ref(NAME_CONSTRAINTS),
92 0,
93 0,
94 0,
95 0,
96 0,
97 0,
98 0,
99 v2i_NAME_CONSTRAINTS,
100 i2r_NAME_CONSTRAINTS,
101 0,
102 NULL,
103 };
104
105 ASN1_SEQUENCE(GENERAL_SUBTREE) = {
106 ASN1_SIMPLE(GENERAL_SUBTREE, base, GENERAL_NAME),
107 ASN1_IMP_OPT(GENERAL_SUBTREE, minimum, ASN1_INTEGER, 0),
108 ASN1_IMP_OPT(GENERAL_SUBTREE, maximum, ASN1_INTEGER, 1),
109 } ASN1_SEQUENCE_END(GENERAL_SUBTREE)
110
111 ASN1_SEQUENCE(NAME_CONSTRAINTS) = {
112 ASN1_IMP_SEQUENCE_OF_OPT(NAME_CONSTRAINTS, permittedSubtrees,
113 GENERAL_SUBTREE, 0),
114 ASN1_IMP_SEQUENCE_OF_OPT(NAME_CONSTRAINTS, excludedSubtrees,
115 GENERAL_SUBTREE, 1),
116 } ASN1_SEQUENCE_END(NAME_CONSTRAINTS)
117
118
119 IMPLEMENT_ASN1_ALLOC_FUNCTIONS(GENERAL_SUBTREE)
120 IMPLEMENT_ASN1_ALLOC_FUNCTIONS(NAME_CONSTRAINTS)
121
122 static void *v2i_NAME_CONSTRAINTS(const X509V3_EXT_METHOD *method,
123 const X509V3_CTX *ctx,
124 const STACK_OF(CONF_VALUE) *nval) {
125 STACK_OF(GENERAL_SUBTREE) **ptree = NULL;
126 NAME_CONSTRAINTS *ncons = NULL;
127 GENERAL_SUBTREE *sub = NULL;
128 ncons = NAME_CONSTRAINTS_new();
129 if (!ncons) {
130 goto err;
131 }
132 for (size_t i = 0; i < sk_CONF_VALUE_num(nval); i++) {
133 const CONF_VALUE *val = sk_CONF_VALUE_value(nval, i);
134 CONF_VALUE tval;
135 if (!strncmp(val->name, "permitted", 9) && val->name[9]) {
136 ptree = &ncons->permittedSubtrees;
137 tval.name = val->name + 10;
138 } else if (!strncmp(val->name, "excluded", 8) && val->name[8]) {
139 ptree = &ncons->excludedSubtrees;
140 tval.name = val->name + 9;
141 } else {
142 OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_SYNTAX);
143 goto err;
144 }
145 tval.value = val->value;
146 sub = GENERAL_SUBTREE_new();
147 if (!v2i_GENERAL_NAME_ex(sub->base, method, ctx, &tval, 1)) {
148 goto err;
149 }
150 if (!*ptree) {
151 *ptree = sk_GENERAL_SUBTREE_new_null();
152 }
153 if (!*ptree || !sk_GENERAL_SUBTREE_push(*ptree, sub)) {
154 goto err;
155 }
156 sub = NULL;
157 }
158
159 return ncons;
160
161 err:
162 NAME_CONSTRAINTS_free(ncons);
163 GENERAL_SUBTREE_free(sub);
164 return NULL;
165 }
166
i2r_NAME_CONSTRAINTS(const X509V3_EXT_METHOD * method,void * a,BIO * bp,int ind)167 static int i2r_NAME_CONSTRAINTS(const X509V3_EXT_METHOD *method, void *a,
168 BIO *bp, int ind) {
169 NAME_CONSTRAINTS *ncons = a;
170 do_i2r_name_constraints(method, ncons->permittedSubtrees, bp, ind,
171 "Permitted");
172 do_i2r_name_constraints(method, ncons->excludedSubtrees, bp, ind, "Excluded");
173 return 1;
174 }
175
do_i2r_name_constraints(const X509V3_EXT_METHOD * method,STACK_OF (GENERAL_SUBTREE)* trees,BIO * bp,int ind,const char * name)176 static int do_i2r_name_constraints(const X509V3_EXT_METHOD *method,
177 STACK_OF(GENERAL_SUBTREE) *trees, BIO *bp,
178 int ind, const char *name) {
179 GENERAL_SUBTREE *tree;
180 size_t i;
181 if (sk_GENERAL_SUBTREE_num(trees) > 0) {
182 BIO_printf(bp, "%*s%s:\n", ind, "", name);
183 }
184 for (i = 0; i < sk_GENERAL_SUBTREE_num(trees); i++) {
185 tree = sk_GENERAL_SUBTREE_value(trees, i);
186 BIO_printf(bp, "%*s", ind + 2, "");
187 if (tree->base->type == GEN_IPADD) {
188 print_nc_ipadd(bp, tree->base->d.ip);
189 } else {
190 GENERAL_NAME_print(bp, tree->base);
191 }
192 BIO_puts(bp, "\n");
193 }
194 return 1;
195 }
196
print_nc_ipadd(BIO * bp,const ASN1_OCTET_STRING * ip)197 static int print_nc_ipadd(BIO *bp, const ASN1_OCTET_STRING *ip) {
198 int i, len;
199 unsigned char *p;
200 p = ip->data;
201 len = ip->length;
202 BIO_puts(bp, "IP:");
203 if (len == 8) {
204 BIO_printf(bp, "%d.%d.%d.%d/%d.%d.%d.%d", p[0], p[1], p[2], p[3], p[4],
205 p[5], p[6], p[7]);
206 } else if (len == 32) {
207 for (i = 0; i < 16; i++) {
208 uint16_t v = ((uint16_t)p[0] << 8) | p[1];
209 BIO_printf(bp, "%X", v);
210 p += 2;
211 if (i == 7) {
212 BIO_puts(bp, "/");
213 } else if (i != 15) {
214 BIO_puts(bp, ":");
215 }
216 }
217 } else {
218 BIO_printf(bp, "IP Address:<invalid>");
219 }
220 return 1;
221 }
222
223 //-
224 // Check a certificate conforms to a specified set of constraints.
225 // Return values:
226 // X509_V_OK: All constraints obeyed.
227 // X509_V_ERR_PERMITTED_VIOLATION: Permitted subtree violation.
228 // X509_V_ERR_EXCLUDED_VIOLATION: Excluded subtree violation.
229 // X509_V_ERR_SUBTREE_MINMAX: Min or max values present and matching type.
230 // X509_V_ERR_UNSPECIFIED: Unspecified error.
231 // X509_V_ERR_UNSUPPORTED_CONSTRAINT_TYPE: Unsupported constraint type.
232 // X509_V_ERR_UNSUPPORTED_CONSTRAINT_SYNTAX: Bad or unsupported constraint
233 // syntax.
234 // X509_V_ERR_UNSUPPORTED_NAME_SYNTAX: Bad or unsupported syntax of name.
235
NAME_CONSTRAINTS_check(X509 * x,NAME_CONSTRAINTS * nc)236 int NAME_CONSTRAINTS_check(X509 *x, NAME_CONSTRAINTS *nc) {
237 int r, i;
238 size_t j;
239 X509_NAME *nm;
240
241 nm = X509_get_subject_name(x);
242
243 // Guard against certificates with an excessive number of names or
244 // constraints causing a computationally expensive name constraints
245 // check.
246 size_t name_count =
247 X509_NAME_entry_count(nm) + sk_GENERAL_NAME_num(x->altname);
248 size_t constraint_count = sk_GENERAL_SUBTREE_num(nc->permittedSubtrees) +
249 sk_GENERAL_SUBTREE_num(nc->excludedSubtrees);
250 size_t check_count = constraint_count * name_count;
251 if (name_count < (size_t)X509_NAME_entry_count(nm) ||
252 constraint_count < sk_GENERAL_SUBTREE_num(nc->permittedSubtrees) ||
253 (constraint_count && check_count / constraint_count != name_count) ||
254 check_count > 1 << 20) {
255 return X509_V_ERR_UNSPECIFIED;
256 }
257
258 if (X509_NAME_entry_count(nm) > 0) {
259 GENERAL_NAME gntmp;
260 gntmp.type = GEN_DIRNAME;
261 gntmp.d.directoryName = nm;
262
263 r = nc_match(&gntmp, nc);
264
265 if (r != X509_V_OK) {
266 return r;
267 }
268
269 gntmp.type = GEN_EMAIL;
270
271 // Process any email address attributes in subject name
272
273 for (i = -1;;) {
274 i = X509_NAME_get_index_by_NID(nm, NID_pkcs9_emailAddress, i);
275 if (i == -1) {
276 break;
277 }
278 const X509_NAME_ENTRY *ne = X509_NAME_get_entry(nm, i);
279 gntmp.d.rfc822Name = X509_NAME_ENTRY_get_data(ne);
280 if (gntmp.d.rfc822Name->type != V_ASN1_IA5STRING) {
281 return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
282 }
283
284 r = nc_match(&gntmp, nc);
285
286 if (r != X509_V_OK) {
287 return r;
288 }
289 }
290 }
291
292 for (j = 0; j < sk_GENERAL_NAME_num(x->altname); j++) {
293 GENERAL_NAME *gen = sk_GENERAL_NAME_value(x->altname, j);
294 r = nc_match(gen, nc);
295 if (r != X509_V_OK) {
296 return r;
297 }
298 }
299
300 return X509_V_OK;
301 }
302
nc_match(GENERAL_NAME * gen,NAME_CONSTRAINTS * nc)303 static int nc_match(GENERAL_NAME *gen, NAME_CONSTRAINTS *nc) {
304 GENERAL_SUBTREE *sub;
305 int r, match = 0;
306 size_t i;
307
308 // Permitted subtrees: if any subtrees exist of matching the type at
309 // least one subtree must match.
310
311 for (i = 0; i < sk_GENERAL_SUBTREE_num(nc->permittedSubtrees); i++) {
312 sub = sk_GENERAL_SUBTREE_value(nc->permittedSubtrees, i);
313 if (gen->type != sub->base->type) {
314 continue;
315 }
316 if (sub->minimum || sub->maximum) {
317 return X509_V_ERR_SUBTREE_MINMAX;
318 }
319 // If we already have a match don't bother trying any more
320 if (match == 2) {
321 continue;
322 }
323 if (match == 0) {
324 match = 1;
325 }
326 r = nc_match_single(gen, sub->base);
327 if (r == X509_V_OK) {
328 match = 2;
329 } else if (r != X509_V_ERR_PERMITTED_VIOLATION) {
330 return r;
331 }
332 }
333
334 if (match == 1) {
335 return X509_V_ERR_PERMITTED_VIOLATION;
336 }
337
338 // Excluded subtrees: must not match any of these
339
340 for (i = 0; i < sk_GENERAL_SUBTREE_num(nc->excludedSubtrees); i++) {
341 sub = sk_GENERAL_SUBTREE_value(nc->excludedSubtrees, i);
342 if (gen->type != sub->base->type) {
343 continue;
344 }
345 if (sub->minimum || sub->maximum) {
346 return X509_V_ERR_SUBTREE_MINMAX;
347 }
348
349 r = nc_match_single(gen, sub->base);
350 if (r == X509_V_OK) {
351 return X509_V_ERR_EXCLUDED_VIOLATION;
352 } else if (r != X509_V_ERR_PERMITTED_VIOLATION) {
353 return r;
354 }
355 }
356
357 return X509_V_OK;
358 }
359
nc_match_single(GENERAL_NAME * gen,GENERAL_NAME * base)360 static int nc_match_single(GENERAL_NAME *gen, GENERAL_NAME *base) {
361 switch (base->type) {
362 case GEN_DIRNAME:
363 return nc_dn(gen->d.directoryName, base->d.directoryName);
364
365 case GEN_DNS:
366 return nc_dns(gen->d.dNSName, base->d.dNSName);
367
368 case GEN_EMAIL:
369 return nc_email(gen->d.rfc822Name, base->d.rfc822Name);
370
371 case GEN_URI:
372 return nc_uri(gen->d.uniformResourceIdentifier,
373 base->d.uniformResourceIdentifier);
374
375 default:
376 return X509_V_ERR_UNSUPPORTED_CONSTRAINT_TYPE;
377 }
378 }
379
380 // directoryName name constraint matching. The canonical encoding of
381 // X509_NAME makes this comparison easy. It is matched if the subtree is a
382 // subset of the name.
383
nc_dn(X509_NAME * nm,X509_NAME * base)384 static int nc_dn(X509_NAME *nm, X509_NAME *base) {
385 // Ensure canonical encodings are up to date.
386 if (nm->modified && i2d_X509_NAME(nm, NULL) < 0) {
387 return X509_V_ERR_OUT_OF_MEM;
388 }
389 if (base->modified && i2d_X509_NAME(base, NULL) < 0) {
390 return X509_V_ERR_OUT_OF_MEM;
391 }
392 if (base->canon_enclen > nm->canon_enclen) {
393 return X509_V_ERR_PERMITTED_VIOLATION;
394 }
395 if (OPENSSL_memcmp(base->canon_enc, nm->canon_enc, base->canon_enclen)) {
396 return X509_V_ERR_PERMITTED_VIOLATION;
397 }
398 return X509_V_OK;
399 }
400
starts_with(const CBS * cbs,uint8_t c)401 static int starts_with(const CBS *cbs, uint8_t c) {
402 return CBS_len(cbs) > 0 && CBS_data(cbs)[0] == c;
403 }
404
equal_case(const CBS * a,const CBS * b)405 static int equal_case(const CBS *a, const CBS *b) {
406 if (CBS_len(a) != CBS_len(b)) {
407 return 0;
408 }
409 // Note we cannot use |OPENSSL_strncasecmp| because that would stop
410 // iterating at NUL.
411 const uint8_t *a_data = CBS_data(a), *b_data = CBS_data(b);
412 for (size_t i = 0; i < CBS_len(a); i++) {
413 if (OPENSSL_tolower(a_data[i]) != OPENSSL_tolower(b_data[i])) {
414 return 0;
415 }
416 }
417 return 1;
418 }
419
has_suffix_case(const CBS * a,const CBS * b)420 static int has_suffix_case(const CBS *a, const CBS *b) {
421 if (CBS_len(a) < CBS_len(b)) {
422 return 0;
423 }
424 CBS copy = *a;
425 CBS_skip(©, CBS_len(a) - CBS_len(b));
426 return equal_case(©, b);
427 }
428
nc_dns(const ASN1_IA5STRING * dns,const ASN1_IA5STRING * base)429 static int nc_dns(const ASN1_IA5STRING *dns, const ASN1_IA5STRING *base) {
430 CBS dns_cbs, base_cbs;
431 CBS_init(&dns_cbs, dns->data, dns->length);
432 CBS_init(&base_cbs, base->data, base->length);
433
434 // Empty matches everything
435 if (CBS_len(&base_cbs) == 0) {
436 return X509_V_OK;
437 }
438
439 // If |base_cbs| begins with a '.', do a simple suffix comparison. This is
440 // not part of RFC5280, but is part of OpenSSL's original behavior.
441 if (starts_with(&base_cbs, '.')) {
442 if (has_suffix_case(&dns_cbs, &base_cbs)) {
443 return X509_V_OK;
444 }
445 return X509_V_ERR_PERMITTED_VIOLATION;
446 }
447
448 // Otherwise can add zero or more components on the left so compare RHS
449 // and if dns is longer and expect '.' as preceding character.
450 if (CBS_len(&dns_cbs) > CBS_len(&base_cbs)) {
451 uint8_t dot;
452 if (!CBS_skip(&dns_cbs, CBS_len(&dns_cbs) - CBS_len(&base_cbs) - 1) ||
453 !CBS_get_u8(&dns_cbs, &dot) || dot != '.') {
454 return X509_V_ERR_PERMITTED_VIOLATION;
455 }
456 }
457
458 if (!equal_case(&dns_cbs, &base_cbs)) {
459 return X509_V_ERR_PERMITTED_VIOLATION;
460 }
461
462 return X509_V_OK;
463 }
464
nc_email(const ASN1_IA5STRING * eml,const ASN1_IA5STRING * base)465 static int nc_email(const ASN1_IA5STRING *eml, const ASN1_IA5STRING *base) {
466 CBS eml_cbs, base_cbs;
467 CBS_init(&eml_cbs, eml->data, eml->length);
468 CBS_init(&base_cbs, base->data, base->length);
469
470 // TODO(davidben): In OpenSSL 1.1.1, this switched from the first '@' to the
471 // last one. Match them here, or perhaps do an actual parse. Looks like
472 // multiple '@'s may be allowed in quoted strings.
473 CBS eml_local, base_local;
474 if (!CBS_get_until_first(&eml_cbs, &eml_local, '@')) {
475 return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
476 }
477 int base_has_at = CBS_get_until_first(&base_cbs, &base_local, '@');
478
479 // Special case: initial '.' is RHS match
480 if (!base_has_at && starts_with(&base_cbs, '.')) {
481 if (has_suffix_case(&eml_cbs, &base_cbs)) {
482 return X509_V_OK;
483 }
484 return X509_V_ERR_PERMITTED_VIOLATION;
485 }
486
487 // If we have anything before '@' match local part
488 if (base_has_at) {
489 // TODO(davidben): This interprets a constraint of "@example.com" as
490 // "example.com", which is not part of RFC5280.
491 if (CBS_len(&base_local) > 0) {
492 // Case sensitive match of local part
493 if (!CBS_mem_equal(&base_local, CBS_data(&eml_local),
494 CBS_len(&eml_local))) {
495 return X509_V_ERR_PERMITTED_VIOLATION;
496 }
497 }
498 // Position base after '@'
499 assert(starts_with(&base_cbs, '@'));
500 CBS_skip(&base_cbs, 1);
501 }
502
503 // Just have hostname left to match: case insensitive
504 assert(starts_with(&eml_cbs, '@'));
505 CBS_skip(&eml_cbs, 1);
506 if (!equal_case(&base_cbs, &eml_cbs)) {
507 return X509_V_ERR_PERMITTED_VIOLATION;
508 }
509
510 return X509_V_OK;
511 }
512
nc_uri(const ASN1_IA5STRING * uri,const ASN1_IA5STRING * base)513 static int nc_uri(const ASN1_IA5STRING *uri, const ASN1_IA5STRING *base) {
514 CBS uri_cbs, base_cbs;
515 CBS_init(&uri_cbs, uri->data, uri->length);
516 CBS_init(&base_cbs, base->data, base->length);
517
518 // Check for foo:// and skip past it
519 CBS scheme;
520 uint8_t byte;
521 if (!CBS_get_until_first(&uri_cbs, &scheme, ':') ||
522 !CBS_skip(&uri_cbs, 1) || // Skip the colon
523 !CBS_get_u8(&uri_cbs, &byte) || byte != '/' ||
524 !CBS_get_u8(&uri_cbs, &byte) || byte != '/') {
525 return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
526 }
527
528 // Look for a port indicator as end of hostname first. Otherwise look for
529 // trailing slash, or the end of the string.
530 // TODO(davidben): This is not a correct URI parser and mishandles IPv6
531 // literals.
532 CBS host;
533 if (!CBS_get_until_first(&uri_cbs, &host, ':') &&
534 !CBS_get_until_first(&uri_cbs, &host, '/')) {
535 host = uri_cbs;
536 }
537
538 if (CBS_len(&host) == 0) {
539 return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
540 }
541
542 // Special case: initial '.' is RHS match
543 if (starts_with(&base_cbs, '.')) {
544 if (has_suffix_case(&host, &base_cbs)) {
545 return X509_V_OK;
546 }
547 return X509_V_ERR_PERMITTED_VIOLATION;
548 }
549
550 if (!equal_case(&base_cbs, &host)) {
551 return X509_V_ERR_PERMITTED_VIOLATION;
552 }
553
554 return X509_V_OK;
555 }
556