VirtualBox

source: vbox/trunk/src/libs/openssl-1.1.0g/crypto/x509v3/v3_ncons.c@ 69881

Last change on this file since 69881 was 69881, checked in by vboxsync, 7 years ago

Update OpenSSL to 1.1.0g.
bugref:8070: src/libs maintenance

  • Property svn:eol-style set to native
File size: 17.3 KB
Line 
1/*
2 * Copyright 2003-2016 The OpenSSL Project Authors. All Rights Reserved.
3 *
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
8 */
9
10#include <stdio.h>
11#include "internal/cryptlib.h"
12#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
18#include "internal/x509_int.h"
19#include "ext_dat.h"
20
21static void *v2i_NAME_CONSTRAINTS(const X509V3_EXT_METHOD *method,
22 X509V3_CTX *ctx,
23 STACK_OF(CONF_VALUE) *nval);
24static int i2r_NAME_CONSTRAINTS(const X509V3_EXT_METHOD *method, void *a,
25 BIO *bp, int ind);
26static int do_i2r_name_constraints(const X509V3_EXT_METHOD *method,
27 STACK_OF(GENERAL_SUBTREE) *trees, BIO *bp,
28 int ind, const char *name);
29static int print_nc_ipadd(BIO *bp, ASN1_OCTET_STRING *ip);
30
31static int nc_match(GENERAL_NAME *gen, NAME_CONSTRAINTS *nc);
32static int nc_match_single(GENERAL_NAME *sub, GENERAL_NAME *gen);
33static int nc_dn(X509_NAME *sub, X509_NAME *nm);
34static int nc_dns(ASN1_IA5STRING *sub, ASN1_IA5STRING *dns);
35static int nc_email(ASN1_IA5STRING *sub, ASN1_IA5STRING *eml);
36static int nc_uri(ASN1_IA5STRING *uri, ASN1_IA5STRING *base);
37static int nc_ip(ASN1_OCTET_STRING *ip, ASN1_OCTET_STRING *base);
38
39const X509V3_EXT_METHOD v3_name_constraints = {
40 NID_name_constraints, 0,
41 ASN1_ITEM_ref(NAME_CONSTRAINTS),
42 0, 0, 0, 0,
43 0, 0,
44 0, v2i_NAME_CONSTRAINTS,
45 i2r_NAME_CONSTRAINTS, 0,
46 NULL
47};
48
49ASN1_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)
54
55ASN1_SEQUENCE(NAME_CONSTRAINTS) = {
56 ASN1_IMP_SEQUENCE_OF_OPT(NAME_CONSTRAINTS, permittedSubtrees,
57 GENERAL_SUBTREE, 0),
58 ASN1_IMP_SEQUENCE_OF_OPT(NAME_CONSTRAINTS, excludedSubtrees,
59 GENERAL_SUBTREE, 1),
60} ASN1_SEQUENCE_END(NAME_CONSTRAINTS)
61
62
63IMPLEMENT_ASN1_ALLOC_FUNCTIONS(GENERAL_SUBTREE)
64IMPLEMENT_ASN1_ALLOC_FUNCTIONS(NAME_CONSTRAINTS)
65
66/*
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).
71 */
72static int ia5ncasecmp(const char *s1, const char *s2, size_t n)
73{
74 for (; n > 0; n--, s1++, s2++) {
75 if (*s1 != *s2) {
76 unsigned char c1 = (unsigned char)*s1, c2 = (unsigned char)*s2;
77
78 /* Convert to lower case */
79 if (c1 >= 0x41 /* A */ && c1 <= 0x5A /* Z */)
80 c1 += 0x20;
81 if (c2 >= 0x41 /* A */ && c2 <= 0x5A /* Z */)
82 c2 += 0x20;
83
84 if (c1 == c2)
85 continue;
86
87 if (c1 < c2)
88 return -1;
89
90 /* c1 > c2 */
91 return 1;
92 } else if (*s1 == 0) {
93 /* If we get here we know that *s2 == 0 too */
94 return 0;
95 }
96 }
97
98 return 0;
99}
100
101static int ia5casecmp(const char *s1, const char *s2)
102{
103 return ia5ncasecmp(s1, s2, SIZE_MAX);
104}
105
106static void *v2i_NAME_CONSTRAINTS(const X509V3_EXT_METHOD *method,
107 X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval)
108{
109 int i;
110 CONF_VALUE tval, *val;
111 STACK_OF(GENERAL_SUBTREE) **ptree = NULL;
112 NAME_CONSTRAINTS *ncons = NULL;
113 GENERAL_SUBTREE *sub = NULL;
114
115 ncons = NAME_CONSTRAINTS_new();
116 if (ncons == NULL)
117 goto memerr;
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;
126 } else {
127 X509V3err(X509V3_F_V2I_NAME_CONSTRAINTS, X509V3_R_INVALID_SYNTAX);
128 goto err;
129 }
130 tval.value = val->value;
131 sub = GENERAL_SUBTREE_new();
132 if (sub == NULL)
133 goto memerr;
134 if (!v2i_GENERAL_NAME_ex(sub->base, method, ctx, &tval, 1))
135 goto err;
136 if (*ptree == NULL)
137 *ptree = sk_GENERAL_SUBTREE_new_null();
138 if (*ptree == NULL || !sk_GENERAL_SUBTREE_push(*ptree, sub))
139 goto memerr;
140 sub = NULL;
141 }
142
143 return ncons;
144
145 memerr:
146 X509V3err(X509V3_F_V2I_NAME_CONSTRAINTS, ERR_R_MALLOC_FAILURE);
147 err:
148 NAME_CONSTRAINTS_free(ncons);
149 GENERAL_SUBTREE_free(sub);
150
151 return NULL;
152}
153
154static int i2r_NAME_CONSTRAINTS(const X509V3_EXT_METHOD *method, void *a,
155 BIO *bp, int ind)
156{
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");
162 return 1;
163}
164
165static int do_i2r_name_constraints(const X509V3_EXT_METHOD *method,
166 STACK_OF(GENERAL_SUBTREE) *trees,
167 BIO *bp, int ind, const char *name)
168{
169 GENERAL_SUBTREE *tree;
170 int i;
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);
178 else
179 GENERAL_NAME_print(bp, tree->base);
180 BIO_puts(bp, "\n");
181 }
182 return 1;
183}
184
185static int print_nc_ipadd(BIO *bp, ASN1_OCTET_STRING *ip)
186{
187 int i, len;
188 unsigned char *p;
189 p = ip->data;
190 len = ip->length;
191 BIO_puts(bp, "IP:");
192 if (len == 8) {
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]);
198 p += 2;
199 if (i == 7)
200 BIO_puts(bp, "/");
201 else if (i != 15)
202 BIO_puts(bp, ":");
203 }
204 } else
205 BIO_printf(bp, "IP Address:<invalid>");
206 return 1;
207}
208
209#define NAME_CHECK_MAX (1 << 20)
210
211static int add_lengths(int *out, int a, int b)
212{
213 /* sk_FOO_num(NULL) returns -1 but is effectively 0 when iterating. */
214 if (a < 0)
215 a = 0;
216 if (b < 0)
217 b = 0;
218
219 if (a > INT_MAX - b)
220 return 0;
221 *out = a + b;
222 return 1;
223}
224
225/*-
226 * Check a certificate conforms to a specified set of constraints.
227 * Return values:
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
235 */
236
237int NAME_CONSTRAINTS_check(X509 *x, NAME_CONSTRAINTS *nc)
238{
239 int r, i, name_count, constraint_count;
240 X509_NAME *nm;
241
242 nm = X509_get_subject_name(x);
243
244 /*
245 * Guard against certificates with an excessive number of names or
246 * constraints causing a computationally expensive name constraints check.
247 */
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;
255
256 if (X509_NAME_entry_count(nm) > 0) {
257 GENERAL_NAME gntmp;
258 gntmp.type = GEN_DIRNAME;
259 gntmp.d.directoryName = nm;
260
261 r = nc_match(&gntmp, nc);
262
263 if (r != X509_V_OK)
264 return r;
265
266 gntmp.type = GEN_EMAIL;
267
268 /* Process any email address attributes in subject name */
269
270 for (i = -1;;) {
271 const X509_NAME_ENTRY *ne;
272
273 i = X509_NAME_get_index_by_NID(nm, NID_pkcs9_emailAddress, i);
274 if (i == -1)
275 break;
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;
280
281 r = nc_match(&gntmp, nc);
282
283 if (r != X509_V_OK)
284 return r;
285 }
286
287 }
288
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);
292 if (r != X509_V_OK)
293 return r;
294 }
295
296 return X509_V_OK;
297
298}
299
300int NAME_CONSTRAINTS_check_CN(X509 *x, NAME_CONSTRAINTS *nc)
301{
302 int r, i;
303 X509_NAME *nm;
304
305 ASN1_STRING stmp;
306 GENERAL_NAME gntmp;
307 stmp.flags = 0;
308 stmp.type = V_ASN1_IA5STRING;
309 gntmp.type = GEN_DNS;
310 gntmp.d.dNSName = &stmp;
311
312 nm = X509_get_subject_name(x);
313
314 /* Process any commonName attributes in subject name */
315
316 for (i = -1;;) {
317 X509_NAME_ENTRY *ne;
318 ASN1_STRING *hn;
319 i = X509_NAME_get_index_by_NID(nm, NID_commonName, i);
320 if (i == -1)
321 break;
322 ne = X509_NAME_get_entry(nm, i);
323 hn = X509_NAME_ENTRY_get_data(ne);
324 /* Only process attributes that look like host names */
325 if (asn1_valid_host(hn)) {
326 unsigned char *h;
327 int hlen = ASN1_STRING_to_UTF8(&h, hn);
328 if (hlen <= 0)
329 return X509_V_ERR_OUT_OF_MEM;
330
331 stmp.length = hlen;
332 stmp.data = h;
333
334 r = nc_match(&gntmp, nc);
335
336 OPENSSL_free(h);
337
338 if (r != X509_V_OK)
339 return r;
340 }
341 }
342 return X509_V_OK;
343}
344
345static int nc_match(GENERAL_NAME *gen, NAME_CONSTRAINTS *nc)
346{
347 GENERAL_SUBTREE *sub;
348 int i, r, match = 0;
349
350 /*
351 * Permitted subtrees: if any subtrees exist of matching the type at
352 * least one subtree must match.
353 */
354
355 for (i = 0; i < sk_GENERAL_SUBTREE_num(nc->permittedSubtrees); i++) {
356 sub = sk_GENERAL_SUBTREE_value(nc->permittedSubtrees, i);
357 if (gen->type != sub->base->type)
358 continue;
359 if (sub->minimum || sub->maximum)
360 return X509_V_ERR_SUBTREE_MINMAX;
361 /* If we already have a match don't bother trying any more */
362 if (match == 2)
363 continue;
364 if (match == 0)
365 match = 1;
366 r = nc_match_single(gen, sub->base);
367 if (r == X509_V_OK)
368 match = 2;
369 else if (r != X509_V_ERR_PERMITTED_VIOLATION)
370 return r;
371 }
372
373 if (match == 1)
374 return X509_V_ERR_PERMITTED_VIOLATION;
375
376 /* Excluded subtrees: must not match any of these */
377
378 for (i = 0; i < sk_GENERAL_SUBTREE_num(nc->excludedSubtrees); i++) {
379 sub = sk_GENERAL_SUBTREE_value(nc->excludedSubtrees, i);
380 if (gen->type != sub->base->type)
381 continue;
382 if (sub->minimum || sub->maximum)
383 return X509_V_ERR_SUBTREE_MINMAX;
384
385 r = nc_match_single(gen, sub->base);
386 if (r == X509_V_OK)
387 return X509_V_ERR_EXCLUDED_VIOLATION;
388 else if (r != X509_V_ERR_PERMITTED_VIOLATION)
389 return r;
390
391 }
392
393 return X509_V_OK;
394
395}
396
397static int nc_match_single(GENERAL_NAME *gen, GENERAL_NAME *base)
398{
399 switch (base->type) {
400 case GEN_DIRNAME:
401 return nc_dn(gen->d.directoryName, base->d.directoryName);
402
403 case GEN_DNS:
404 return nc_dns(gen->d.dNSName, base->d.dNSName);
405
406 case GEN_EMAIL:
407 return nc_email(gen->d.rfc822Name, base->d.rfc822Name);
408
409 case GEN_URI:
410 return nc_uri(gen->d.uniformResourceIdentifier,
411 base->d.uniformResourceIdentifier);
412
413 case GEN_IPADD:
414 return nc_ip(gen->d.iPAddress, base->d.iPAddress);
415
416 default:
417 return X509_V_ERR_UNSUPPORTED_CONSTRAINT_TYPE;
418 }
419
420}
421
422/*
423 * directoryName name constraint matching. The canonical encoding of
424 * X509_NAME makes this comparison easy. It is matched if the subtree is a
425 * subset of the name.
426 */
427
428static int nc_dn(X509_NAME *nm, X509_NAME *base)
429{
430 /* Ensure canonical encodings are up to date. */
431 if (nm->modified && i2d_X509_NAME(nm, NULL) < 0)
432 return X509_V_ERR_OUT_OF_MEM;
433 if (base->modified && i2d_X509_NAME(base, NULL) < 0)
434 return X509_V_ERR_OUT_OF_MEM;
435 if (base->canon_enclen > nm->canon_enclen)
436 return X509_V_ERR_PERMITTED_VIOLATION;
437 if (memcmp(base->canon_enc, nm->canon_enc, base->canon_enclen))
438 return X509_V_ERR_PERMITTED_VIOLATION;
439 return X509_V_OK;
440}
441
442static int nc_dns(ASN1_IA5STRING *dns, ASN1_IA5STRING *base)
443{
444 char *baseptr = (char *)base->data;
445 char *dnsptr = (char *)dns->data;
446 /* Empty matches everything */
447 if (!*baseptr)
448 return X509_V_OK;
449 /*
450 * Otherwise can add zero or more components on the left so compare RHS
451 * and if dns is longer and expect '.' as preceding character.
452 */
453 if (dns->length > base->length) {
454 dnsptr += dns->length - base->length;
455 if (*baseptr != '.' && dnsptr[-1] != '.')
456 return X509_V_ERR_PERMITTED_VIOLATION;
457 }
458
459 if (ia5casecmp(baseptr, dnsptr))
460 return X509_V_ERR_PERMITTED_VIOLATION;
461
462 return X509_V_OK;
463
464}
465
466static int nc_email(ASN1_IA5STRING *eml, ASN1_IA5STRING *base)
467{
468 const char *baseptr = (char *)base->data;
469 const char *emlptr = (char *)eml->data;
470
471 const char *baseat = strchr(baseptr, '@');
472 const char *emlat = strchr(emlptr, '@');
473 if (!emlat)
474 return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
475 /* Special case: initial '.' is RHS match */
476 if (!baseat && (*baseptr == '.')) {
477 if (eml->length > base->length) {
478 emlptr += eml->length - base->length;
479 if (ia5casecmp(baseptr, emlptr) == 0)
480 return X509_V_OK;
481 }
482 return X509_V_ERR_PERMITTED_VIOLATION;
483 }
484
485 /* If we have anything before '@' match local part */
486
487 if (baseat) {
488 if (baseat != baseptr) {
489 if ((baseat - baseptr) != (emlat - emlptr))
490 return X509_V_ERR_PERMITTED_VIOLATION;
491 /* Case sensitive match of local part */
492 if (strncmp(baseptr, emlptr, emlat - emlptr))
493 return X509_V_ERR_PERMITTED_VIOLATION;
494 }
495 /* Position base after '@' */
496 baseptr = baseat + 1;
497 }
498 emlptr = emlat + 1;
499 /* Just have hostname left to match: case insensitive */
500 if (ia5casecmp(baseptr, emlptr))
501 return X509_V_ERR_PERMITTED_VIOLATION;
502
503 return X509_V_OK;
504
505}
506
507static int nc_uri(ASN1_IA5STRING *uri, ASN1_IA5STRING *base)
508{
509 const char *baseptr = (char *)base->data;
510 const char *hostptr = (char *)uri->data;
511 const char *p = strchr(hostptr, ':');
512 int hostlen;
513 /* Check for foo:// and skip past it */
514 if (!p || (p[1] != '/') || (p[2] != '/'))
515 return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
516 hostptr = p + 3;
517
518 /* Determine length of hostname part of URI */
519
520 /* Look for a port indicator as end of hostname first */
521
522 p = strchr(hostptr, ':');
523 /* Otherwise look for trailing slash */
524 if (!p)
525 p = strchr(hostptr, '/');
526
527 if (!p)
528 hostlen = strlen(hostptr);
529 else
530 hostlen = p - hostptr;
531
532 if (hostlen == 0)
533 return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
534
535 /* Special case: initial '.' is RHS match */
536 if (*baseptr == '.') {
537 if (hostlen > base->length) {
538 p = hostptr + hostlen - base->length;
539 if (ia5ncasecmp(p, baseptr, base->length) == 0)
540 return X509_V_OK;
541 }
542 return X509_V_ERR_PERMITTED_VIOLATION;
543 }
544
545 if ((base->length != (int)hostlen)
546 || ia5ncasecmp(hostptr, baseptr, hostlen))
547 return X509_V_ERR_PERMITTED_VIOLATION;
548
549 return X509_V_OK;
550
551}
552
553static int nc_ip(ASN1_OCTET_STRING *ip, ASN1_OCTET_STRING *base)
554{
555 int hostlen, baselen, i;
556 unsigned char *hostptr, *baseptr, *maskptr;
557 hostptr = ip->data;
558 hostlen = ip->length;
559 baseptr = base->data;
560 baselen = base->length;
561
562 /* Invalid if not IPv4 or IPv6 */
563 if (!((hostlen == 4) || (hostlen == 16)))
564 return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
565 if (!((baselen == 8) || (baselen == 32)))
566 return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
567
568 /* Do not match IPv4 with IPv6 */
569 if (hostlen * 2 != baselen)
570 return X509_V_ERR_PERMITTED_VIOLATION;
571
572 maskptr = base->data + hostlen;
573
574 /* Considering possible not aligned base ipAddress */
575 /* Not checking for wrong mask definition: i.e.: 255.0.255.0 */
576 for (i = 0; i < hostlen; i++)
577 if ((hostptr[i] & maskptr[i]) != (baseptr[i] & maskptr[i]))
578 return X509_V_ERR_PERMITTED_VIOLATION;
579
580 return X509_V_OK;
581
582}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette