]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/x509v3/v3_asid.c
Remove OPENSSL_assert() from crypto/x509v3
[thirdparty/openssl.git] / crypto / x509v3 / v3_asid.c
1 /*
2 * Copyright 2006-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 /*
11 * Implementation of RFC 3779 section 3.2.
12 */
13
14 #include <assert.h>
15 #include <stdio.h>
16 #include <string.h>
17 #include "internal/cryptlib.h"
18 #include <openssl/conf.h>
19 #include <openssl/asn1.h>
20 #include <openssl/asn1t.h>
21 #include <openssl/x509v3.h>
22 #include <openssl/x509.h>
23 #include "internal/x509_int.h"
24 #include <openssl/bn.h>
25 #include "ext_dat.h"
26
27 #ifndef OPENSSL_NO_RFC3779
28
29 /*
30 * OpenSSL ASN.1 template translation of RFC 3779 3.2.3.
31 */
32
33 ASN1_SEQUENCE(ASRange) = {
34 ASN1_SIMPLE(ASRange, min, ASN1_INTEGER),
35 ASN1_SIMPLE(ASRange, max, ASN1_INTEGER)
36 } ASN1_SEQUENCE_END(ASRange)
37
38 ASN1_CHOICE(ASIdOrRange) = {
39 ASN1_SIMPLE(ASIdOrRange, u.id, ASN1_INTEGER),
40 ASN1_SIMPLE(ASIdOrRange, u.range, ASRange)
41 } ASN1_CHOICE_END(ASIdOrRange)
42
43 ASN1_CHOICE(ASIdentifierChoice) = {
44 ASN1_SIMPLE(ASIdentifierChoice, u.inherit, ASN1_NULL),
45 ASN1_SEQUENCE_OF(ASIdentifierChoice, u.asIdsOrRanges, ASIdOrRange)
46 } ASN1_CHOICE_END(ASIdentifierChoice)
47
48 ASN1_SEQUENCE(ASIdentifiers) = {
49 ASN1_EXP_OPT(ASIdentifiers, asnum, ASIdentifierChoice, 0),
50 ASN1_EXP_OPT(ASIdentifiers, rdi, ASIdentifierChoice, 1)
51 } ASN1_SEQUENCE_END(ASIdentifiers)
52
53 IMPLEMENT_ASN1_FUNCTIONS(ASRange)
54 IMPLEMENT_ASN1_FUNCTIONS(ASIdOrRange)
55 IMPLEMENT_ASN1_FUNCTIONS(ASIdentifierChoice)
56 IMPLEMENT_ASN1_FUNCTIONS(ASIdentifiers)
57
58 /*
59 * i2r method for an ASIdentifierChoice.
60 */
61 static int i2r_ASIdentifierChoice(BIO *out,
62 ASIdentifierChoice *choice,
63 int indent, const char *msg)
64 {
65 int i;
66 char *s;
67 if (choice == NULL)
68 return 1;
69 BIO_printf(out, "%*s%s:\n", indent, "", msg);
70 switch (choice->type) {
71 case ASIdentifierChoice_inherit:
72 BIO_printf(out, "%*sinherit\n", indent + 2, "");
73 break;
74 case ASIdentifierChoice_asIdsOrRanges:
75 for (i = 0; i < sk_ASIdOrRange_num(choice->u.asIdsOrRanges); i++) {
76 ASIdOrRange *aor =
77 sk_ASIdOrRange_value(choice->u.asIdsOrRanges, i);
78 switch (aor->type) {
79 case ASIdOrRange_id:
80 if ((s = i2s_ASN1_INTEGER(NULL, aor->u.id)) == NULL)
81 return 0;
82 BIO_printf(out, "%*s%s\n", indent + 2, "", s);
83 OPENSSL_free(s);
84 break;
85 case ASIdOrRange_range:
86 if ((s = i2s_ASN1_INTEGER(NULL, aor->u.range->min)) == NULL)
87 return 0;
88 BIO_printf(out, "%*s%s-", indent + 2, "", s);
89 OPENSSL_free(s);
90 if ((s = i2s_ASN1_INTEGER(NULL, aor->u.range->max)) == NULL)
91 return 0;
92 BIO_printf(out, "%s\n", s);
93 OPENSSL_free(s);
94 break;
95 default:
96 return 0;
97 }
98 }
99 break;
100 default:
101 return 0;
102 }
103 return 1;
104 }
105
106 /*
107 * i2r method for an ASIdentifier extension.
108 */
109 static int i2r_ASIdentifiers(const X509V3_EXT_METHOD *method,
110 void *ext, BIO *out, int indent)
111 {
112 ASIdentifiers *asid = ext;
113 return (i2r_ASIdentifierChoice(out, asid->asnum, indent,
114 "Autonomous System Numbers") &&
115 i2r_ASIdentifierChoice(out, asid->rdi, indent,
116 "Routing Domain Identifiers"));
117 }
118
119 /*
120 * Sort comparison function for a sequence of ASIdOrRange elements.
121 */
122 static int ASIdOrRange_cmp(const ASIdOrRange *const *a_,
123 const ASIdOrRange *const *b_)
124 {
125 const ASIdOrRange *a = *a_, *b = *b_;
126
127 assert((a->type == ASIdOrRange_id && a->u.id != NULL) ||
128 (a->type == ASIdOrRange_range && a->u.range != NULL &&
129 a->u.range->min != NULL && a->u.range->max != NULL));
130
131 assert((b->type == ASIdOrRange_id && b->u.id != NULL) ||
132 (b->type == ASIdOrRange_range && b->u.range != NULL &&
133 b->u.range->min != NULL && b->u.range->max != NULL));
134
135 if (a->type == ASIdOrRange_id && b->type == ASIdOrRange_id)
136 return ASN1_INTEGER_cmp(a->u.id, b->u.id);
137
138 if (a->type == ASIdOrRange_range && b->type == ASIdOrRange_range) {
139 int r = ASN1_INTEGER_cmp(a->u.range->min, b->u.range->min);
140 return r != 0 ? r : ASN1_INTEGER_cmp(a->u.range->max,
141 b->u.range->max);
142 }
143
144 if (a->type == ASIdOrRange_id)
145 return ASN1_INTEGER_cmp(a->u.id, b->u.range->min);
146 else
147 return ASN1_INTEGER_cmp(a->u.range->min, b->u.id);
148 }
149
150 /*
151 * Add an inherit element.
152 */
153 int X509v3_asid_add_inherit(ASIdentifiers *asid, int which)
154 {
155 ASIdentifierChoice **choice;
156 if (asid == NULL)
157 return 0;
158 switch (which) {
159 case V3_ASID_ASNUM:
160 choice = &asid->asnum;
161 break;
162 case V3_ASID_RDI:
163 choice = &asid->rdi;
164 break;
165 default:
166 return 0;
167 }
168 if (*choice == NULL) {
169 if ((*choice = ASIdentifierChoice_new()) == NULL)
170 return 0;
171 if (!ossl_assert((*choice)->u.inherit == NULL))
172 return 0;
173 if (((*choice)->u.inherit = ASN1_NULL_new()) == NULL)
174 return 0;
175 (*choice)->type = ASIdentifierChoice_inherit;
176 }
177 return (*choice)->type == ASIdentifierChoice_inherit;
178 }
179
180 /*
181 * Add an ID or range to an ASIdentifierChoice.
182 */
183 int X509v3_asid_add_id_or_range(ASIdentifiers *asid,
184 int which, ASN1_INTEGER *min, ASN1_INTEGER *max)
185 {
186 ASIdentifierChoice **choice;
187 ASIdOrRange *aor;
188 if (asid == NULL)
189 return 0;
190 switch (which) {
191 case V3_ASID_ASNUM:
192 choice = &asid->asnum;
193 break;
194 case V3_ASID_RDI:
195 choice = &asid->rdi;
196 break;
197 default:
198 return 0;
199 }
200 if (*choice != NULL && (*choice)->type == ASIdentifierChoice_inherit)
201 return 0;
202 if (*choice == NULL) {
203 if ((*choice = ASIdentifierChoice_new()) == NULL)
204 return 0;
205 if (!ossl_assert((*choice)->u.asIdsOrRanges == NULL))
206 return 0;
207 (*choice)->u.asIdsOrRanges = sk_ASIdOrRange_new(ASIdOrRange_cmp);
208 if ((*choice)->u.asIdsOrRanges == NULL)
209 return 0;
210 (*choice)->type = ASIdentifierChoice_asIdsOrRanges;
211 }
212 if ((aor = ASIdOrRange_new()) == NULL)
213 return 0;
214 if (max == NULL) {
215 aor->type = ASIdOrRange_id;
216 aor->u.id = min;
217 } else {
218 aor->type = ASIdOrRange_range;
219 if ((aor->u.range = ASRange_new()) == NULL)
220 goto err;
221 ASN1_INTEGER_free(aor->u.range->min);
222 aor->u.range->min = min;
223 ASN1_INTEGER_free(aor->u.range->max);
224 aor->u.range->max = max;
225 }
226 if (!(sk_ASIdOrRange_push((*choice)->u.asIdsOrRanges, aor)))
227 goto err;
228 return 1;
229
230 err:
231 ASIdOrRange_free(aor);
232 return 0;
233 }
234
235 /*
236 * Extract min and max values from an ASIdOrRange.
237 */
238 static int extract_min_max(ASIdOrRange *aor,
239 ASN1_INTEGER **min, ASN1_INTEGER **max)
240 {
241 if (!ossl_assert(aor != NULL && min != NULL && max != NULL))
242 return 0;
243 switch (aor->type) {
244 case ASIdOrRange_id:
245 *min = aor->u.id;
246 *max = aor->u.id;
247 return 1;
248 case ASIdOrRange_range:
249 *min = aor->u.range->min;
250 *max = aor->u.range->max;
251 return 1;
252 }
253
254 return 0;
255 }
256
257 /*
258 * Check whether an ASIdentifierChoice is in canonical form.
259 */
260 static int ASIdentifierChoice_is_canonical(ASIdentifierChoice *choice)
261 {
262 ASN1_INTEGER *a_max_plus_one = NULL;
263 BIGNUM *bn = NULL;
264 int i, ret = 0;
265
266 /*
267 * Empty element or inheritance is canonical.
268 */
269 if (choice == NULL || choice->type == ASIdentifierChoice_inherit)
270 return 1;
271
272 /*
273 * If not a list, or if empty list, it's broken.
274 */
275 if (choice->type != ASIdentifierChoice_asIdsOrRanges ||
276 sk_ASIdOrRange_num(choice->u.asIdsOrRanges) == 0)
277 return 0;
278
279 /*
280 * It's a list, check it.
281 */
282 for (i = 0; i < sk_ASIdOrRange_num(choice->u.asIdsOrRanges) - 1; i++) {
283 ASIdOrRange *a = sk_ASIdOrRange_value(choice->u.asIdsOrRanges, i);
284 ASIdOrRange *b = sk_ASIdOrRange_value(choice->u.asIdsOrRanges, i + 1);
285 ASN1_INTEGER *a_min = NULL, *a_max = NULL, *b_min = NULL, *b_max =
286 NULL;
287
288 if (!extract_min_max(a, &a_min, &a_max)
289 || !extract_min_max(b, &b_min, &b_max))
290 goto done;
291
292 /*
293 * Punt misordered list, overlapping start, or inverted range.
294 */
295 if (ASN1_INTEGER_cmp(a_min, b_min) >= 0 ||
296 ASN1_INTEGER_cmp(a_min, a_max) > 0 ||
297 ASN1_INTEGER_cmp(b_min, b_max) > 0)
298 goto done;
299
300 /*
301 * Calculate a_max + 1 to check for adjacency.
302 */
303 if ((bn == NULL && (bn = BN_new()) == NULL) ||
304 ASN1_INTEGER_to_BN(a_max, bn) == NULL ||
305 !BN_add_word(bn, 1) ||
306 (a_max_plus_one =
307 BN_to_ASN1_INTEGER(bn, a_max_plus_one)) == NULL) {
308 X509V3err(X509V3_F_ASIDENTIFIERCHOICE_IS_CANONICAL,
309 ERR_R_MALLOC_FAILURE);
310 goto done;
311 }
312
313 /*
314 * Punt if adjacent or overlapping.
315 */
316 if (ASN1_INTEGER_cmp(a_max_plus_one, b_min) >= 0)
317 goto done;
318 }
319
320 /*
321 * Check for inverted range.
322 */
323 i = sk_ASIdOrRange_num(choice->u.asIdsOrRanges) - 1;
324 {
325 ASIdOrRange *a = sk_ASIdOrRange_value(choice->u.asIdsOrRanges, i);
326 ASN1_INTEGER *a_min, *a_max;
327 if (a != NULL && a->type == ASIdOrRange_range) {
328 if (!extract_min_max(a, &a_min, &a_max)
329 || ASN1_INTEGER_cmp(a_min, a_max) > 0)
330 goto done;
331 }
332 }
333
334 ret = 1;
335
336 done:
337 ASN1_INTEGER_free(a_max_plus_one);
338 BN_free(bn);
339 return ret;
340 }
341
342 /*
343 * Check whether an ASIdentifier extension is in canonical form.
344 */
345 int X509v3_asid_is_canonical(ASIdentifiers *asid)
346 {
347 return (asid == NULL ||
348 (ASIdentifierChoice_is_canonical(asid->asnum) &&
349 ASIdentifierChoice_is_canonical(asid->rdi)));
350 }
351
352 /*
353 * Whack an ASIdentifierChoice into canonical form.
354 */
355 static int ASIdentifierChoice_canonize(ASIdentifierChoice *choice)
356 {
357 ASN1_INTEGER *a_max_plus_one = NULL;
358 BIGNUM *bn = NULL;
359 int i, ret = 0;
360
361 /*
362 * Nothing to do for empty element or inheritance.
363 */
364 if (choice == NULL || choice->type == ASIdentifierChoice_inherit)
365 return 1;
366
367 /*
368 * If not a list, or if empty list, it's broken.
369 */
370 if (choice->type != ASIdentifierChoice_asIdsOrRanges ||
371 sk_ASIdOrRange_num(choice->u.asIdsOrRanges) == 0) {
372 X509V3err(X509V3_F_ASIDENTIFIERCHOICE_CANONIZE,
373 X509V3_R_EXTENSION_VALUE_ERROR);
374 return 0;
375 }
376
377 /*
378 * We have a non-empty list. Sort it.
379 */
380 sk_ASIdOrRange_sort(choice->u.asIdsOrRanges);
381
382 /*
383 * Now check for errors and suboptimal encoding, rejecting the
384 * former and fixing the latter.
385 */
386 for (i = 0; i < sk_ASIdOrRange_num(choice->u.asIdsOrRanges) - 1; i++) {
387 ASIdOrRange *a = sk_ASIdOrRange_value(choice->u.asIdsOrRanges, i);
388 ASIdOrRange *b = sk_ASIdOrRange_value(choice->u.asIdsOrRanges, i + 1);
389 ASN1_INTEGER *a_min = NULL, *a_max = NULL, *b_min = NULL, *b_max =
390 NULL;
391
392 if (!extract_min_max(a, &a_min, &a_max)
393 || !extract_min_max(b, &b_min, &b_max))
394 goto done;
395
396 /*
397 * Make sure we're properly sorted (paranoia).
398 */
399 if (!ossl_assert(ASN1_INTEGER_cmp(a_min, b_min) <= 0))
400 goto done;
401
402 /*
403 * Punt inverted ranges.
404 */
405 if (ASN1_INTEGER_cmp(a_min, a_max) > 0 ||
406 ASN1_INTEGER_cmp(b_min, b_max) > 0)
407 goto done;
408
409 /*
410 * Check for overlaps.
411 */
412 if (ASN1_INTEGER_cmp(a_max, b_min) >= 0) {
413 X509V3err(X509V3_F_ASIDENTIFIERCHOICE_CANONIZE,
414 X509V3_R_EXTENSION_VALUE_ERROR);
415 goto done;
416 }
417
418 /*
419 * Calculate a_max + 1 to check for adjacency.
420 */
421 if ((bn == NULL && (bn = BN_new()) == NULL) ||
422 ASN1_INTEGER_to_BN(a_max, bn) == NULL ||
423 !BN_add_word(bn, 1) ||
424 (a_max_plus_one =
425 BN_to_ASN1_INTEGER(bn, a_max_plus_one)) == NULL) {
426 X509V3err(X509V3_F_ASIDENTIFIERCHOICE_CANONIZE,
427 ERR_R_MALLOC_FAILURE);
428 goto done;
429 }
430
431 /*
432 * If a and b are adjacent, merge them.
433 */
434 if (ASN1_INTEGER_cmp(a_max_plus_one, b_min) == 0) {
435 ASRange *r;
436 switch (a->type) {
437 case ASIdOrRange_id:
438 if ((r = OPENSSL_malloc(sizeof(*r))) == NULL) {
439 X509V3err(X509V3_F_ASIDENTIFIERCHOICE_CANONIZE,
440 ERR_R_MALLOC_FAILURE);
441 goto done;
442 }
443 r->min = a_min;
444 r->max = b_max;
445 a->type = ASIdOrRange_range;
446 a->u.range = r;
447 break;
448 case ASIdOrRange_range:
449 ASN1_INTEGER_free(a->u.range->max);
450 a->u.range->max = b_max;
451 break;
452 }
453 switch (b->type) {
454 case ASIdOrRange_id:
455 b->u.id = NULL;
456 break;
457 case ASIdOrRange_range:
458 b->u.range->max = NULL;
459 break;
460 }
461 ASIdOrRange_free(b);
462 (void)sk_ASIdOrRange_delete(choice->u.asIdsOrRanges, i + 1);
463 i--;
464 continue;
465 }
466 }
467
468 /*
469 * Check for final inverted range.
470 */
471 i = sk_ASIdOrRange_num(choice->u.asIdsOrRanges) - 1;
472 {
473 ASIdOrRange *a = sk_ASIdOrRange_value(choice->u.asIdsOrRanges, i);
474 ASN1_INTEGER *a_min, *a_max;
475 if (a != NULL && a->type == ASIdOrRange_range) {
476 if (!extract_min_max(a, &a_min, &a_max)
477 || ASN1_INTEGER_cmp(a_min, a_max) > 0)
478 goto done;
479 }
480 }
481
482 /* Paranoia */
483 if (!ossl_assert(ASIdentifierChoice_is_canonical(choice)))
484 goto done;
485
486 ret = 1;
487
488 done:
489 ASN1_INTEGER_free(a_max_plus_one);
490 BN_free(bn);
491 return ret;
492 }
493
494 /*
495 * Whack an ASIdentifier extension into canonical form.
496 */
497 int X509v3_asid_canonize(ASIdentifiers *asid)
498 {
499 return (asid == NULL ||
500 (ASIdentifierChoice_canonize(asid->asnum) &&
501 ASIdentifierChoice_canonize(asid->rdi)));
502 }
503
504 /*
505 * v2i method for an ASIdentifier extension.
506 */
507 static void *v2i_ASIdentifiers(const struct v3_ext_method *method,
508 struct v3_ext_ctx *ctx,
509 STACK_OF(CONF_VALUE) *values)
510 {
511 ASN1_INTEGER *min = NULL, *max = NULL;
512 ASIdentifiers *asid = NULL;
513 int i;
514
515 if ((asid = ASIdentifiers_new()) == NULL) {
516 X509V3err(X509V3_F_V2I_ASIDENTIFIERS, ERR_R_MALLOC_FAILURE);
517 return NULL;
518 }
519
520 for (i = 0; i < sk_CONF_VALUE_num(values); i++) {
521 CONF_VALUE *val = sk_CONF_VALUE_value(values, i);
522 int i1 = 0, i2 = 0, i3 = 0, is_range = 0, which = 0;
523
524 /*
525 * Figure out whether this is an AS or an RDI.
526 */
527 if (!name_cmp(val->name, "AS")) {
528 which = V3_ASID_ASNUM;
529 } else if (!name_cmp(val->name, "RDI")) {
530 which = V3_ASID_RDI;
531 } else {
532 X509V3err(X509V3_F_V2I_ASIDENTIFIERS,
533 X509V3_R_EXTENSION_NAME_ERROR);
534 X509V3_conf_err(val);
535 goto err;
536 }
537
538 /*
539 * Handle inheritance.
540 */
541 if (strcmp(val->value, "inherit") == 0) {
542 if (X509v3_asid_add_inherit(asid, which))
543 continue;
544 X509V3err(X509V3_F_V2I_ASIDENTIFIERS,
545 X509V3_R_INVALID_INHERITANCE);
546 X509V3_conf_err(val);
547 goto err;
548 }
549
550 /*
551 * Number, range, or mistake, pick it apart and figure out which.
552 */
553 i1 = strspn(val->value, "0123456789");
554 if (val->value[i1] == '\0') {
555 is_range = 0;
556 } else {
557 is_range = 1;
558 i2 = i1 + strspn(val->value + i1, " \t");
559 if (val->value[i2] != '-') {
560 X509V3err(X509V3_F_V2I_ASIDENTIFIERS,
561 X509V3_R_INVALID_ASNUMBER);
562 X509V3_conf_err(val);
563 goto err;
564 }
565 i2++;
566 i2 = i2 + strspn(val->value + i2, " \t");
567 i3 = i2 + strspn(val->value + i2, "0123456789");
568 if (val->value[i3] != '\0') {
569 X509V3err(X509V3_F_V2I_ASIDENTIFIERS,
570 X509V3_R_INVALID_ASRANGE);
571 X509V3_conf_err(val);
572 goto err;
573 }
574 }
575
576 /*
577 * Syntax is ok, read and add it.
578 */
579 if (!is_range) {
580 if (!X509V3_get_value_int(val, &min)) {
581 X509V3err(X509V3_F_V2I_ASIDENTIFIERS, ERR_R_MALLOC_FAILURE);
582 goto err;
583 }
584 } else {
585 char *s = OPENSSL_strdup(val->value);
586 if (s == NULL) {
587 X509V3err(X509V3_F_V2I_ASIDENTIFIERS, ERR_R_MALLOC_FAILURE);
588 goto err;
589 }
590 s[i1] = '\0';
591 min = s2i_ASN1_INTEGER(NULL, s);
592 max = s2i_ASN1_INTEGER(NULL, s + i2);
593 OPENSSL_free(s);
594 if (min == NULL || max == NULL) {
595 X509V3err(X509V3_F_V2I_ASIDENTIFIERS, ERR_R_MALLOC_FAILURE);
596 goto err;
597 }
598 if (ASN1_INTEGER_cmp(min, max) > 0) {
599 X509V3err(X509V3_F_V2I_ASIDENTIFIERS,
600 X509V3_R_EXTENSION_VALUE_ERROR);
601 goto err;
602 }
603 }
604 if (!X509v3_asid_add_id_or_range(asid, which, min, max)) {
605 X509V3err(X509V3_F_V2I_ASIDENTIFIERS, ERR_R_MALLOC_FAILURE);
606 goto err;
607 }
608 min = max = NULL;
609 }
610
611 /*
612 * Canonize the result, then we're done.
613 */
614 if (!X509v3_asid_canonize(asid))
615 goto err;
616 return asid;
617
618 err:
619 ASIdentifiers_free(asid);
620 ASN1_INTEGER_free(min);
621 ASN1_INTEGER_free(max);
622 return NULL;
623 }
624
625 /*
626 * OpenSSL dispatch.
627 */
628 const X509V3_EXT_METHOD v3_asid = {
629 NID_sbgp_autonomousSysNum, /* nid */
630 0, /* flags */
631 ASN1_ITEM_ref(ASIdentifiers), /* template */
632 0, 0, 0, 0, /* old functions, ignored */
633 0, /* i2s */
634 0, /* s2i */
635 0, /* i2v */
636 v2i_ASIdentifiers, /* v2i */
637 i2r_ASIdentifiers, /* i2r */
638 0, /* r2i */
639 NULL /* extension-specific data */
640 };
641
642 /*
643 * Figure out whether extension uses inheritance.
644 */
645 int X509v3_asid_inherits(ASIdentifiers *asid)
646 {
647 return (asid != NULL &&
648 ((asid->asnum != NULL &&
649 asid->asnum->type == ASIdentifierChoice_inherit) ||
650 (asid->rdi != NULL &&
651 asid->rdi->type == ASIdentifierChoice_inherit)));
652 }
653
654 /*
655 * Figure out whether parent contains child.
656 */
657 static int asid_contains(ASIdOrRanges *parent, ASIdOrRanges *child)
658 {
659 ASN1_INTEGER *p_min = NULL, *p_max = NULL, *c_min = NULL, *c_max = NULL;
660 int p, c;
661
662 if (child == NULL || parent == child)
663 return 1;
664 if (parent == NULL)
665 return 0;
666
667 p = 0;
668 for (c = 0; c < sk_ASIdOrRange_num(child); c++) {
669 if (!extract_min_max(sk_ASIdOrRange_value(child, c), &c_min, &c_max))
670 return 0;
671 for (;; p++) {
672 if (p >= sk_ASIdOrRange_num(parent))
673 return 0;
674 extract_min_max(sk_ASIdOrRange_value(parent, p), &p_min, &p_max);
675 if (ASN1_INTEGER_cmp(p_max, c_max) < 0)
676 continue;
677 if (ASN1_INTEGER_cmp(p_min, c_min) > 0)
678 return 0;
679 break;
680 }
681 }
682
683 return 1;
684 }
685
686 /*
687 * Test whether a is a subset of b.
688 */
689 int X509v3_asid_subset(ASIdentifiers *a, ASIdentifiers *b)
690 {
691 return (a == NULL ||
692 a == b ||
693 (b != NULL &&
694 !X509v3_asid_inherits(a) &&
695 !X509v3_asid_inherits(b) &&
696 asid_contains(b->asnum->u.asIdsOrRanges,
697 a->asnum->u.asIdsOrRanges) &&
698 asid_contains(b->rdi->u.asIdsOrRanges,
699 a->rdi->u.asIdsOrRanges)));
700 }
701
702 /*
703 * Validation error handling via callback.
704 */
705 #define validation_err(_err_) \
706 do { \
707 if (ctx != NULL) { \
708 ctx->error = _err_; \
709 ctx->error_depth = i; \
710 ctx->current_cert = x; \
711 ret = ctx->verify_cb(0, ctx); \
712 } else { \
713 ret = 0; \
714 } \
715 if (!ret) \
716 goto done; \
717 } while (0)
718
719 /*
720 * Core code for RFC 3779 3.3 path validation.
721 */
722 static int asid_validate_path_internal(X509_STORE_CTX *ctx,
723 STACK_OF(X509) *chain,
724 ASIdentifiers *ext)
725 {
726 ASIdOrRanges *child_as = NULL, *child_rdi = NULL;
727 int i, ret = 1, inherit_as = 0, inherit_rdi = 0;
728 X509 *x;
729
730 if (!ossl_assert(chain != NULL && sk_X509_num(chain) > 0)
731 || !ossl_assert(ctx != NULL || ext != NULL)
732 || !ossl_assert(ctx == NULL || ctx->verify_cb != NULL)) {
733 if (ctx != NULL)
734 ctx->error = X509_V_ERR_UNSPECIFIED;
735 return 0;
736 }
737
738
739 /*
740 * Figure out where to start. If we don't have an extension to
741 * check, we're done. Otherwise, check canonical form and
742 * set up for walking up the chain.
743 */
744 if (ext != NULL) {
745 i = -1;
746 x = NULL;
747 } else {
748 i = 0;
749 x = sk_X509_value(chain, i);
750 if (!ossl_assert(x != NULL)) {
751 if (ctx != NULL)
752 ctx->error = X509_V_ERR_UNSPECIFIED;
753 return 0;
754 }
755 if ((ext = x->rfc3779_asid) == NULL)
756 goto done;
757 }
758 if (!X509v3_asid_is_canonical(ext))
759 validation_err(X509_V_ERR_INVALID_EXTENSION);
760 if (ext->asnum != NULL) {
761 switch (ext->asnum->type) {
762 case ASIdentifierChoice_inherit:
763 inherit_as = 1;
764 break;
765 case ASIdentifierChoice_asIdsOrRanges:
766 child_as = ext->asnum->u.asIdsOrRanges;
767 break;
768 }
769 }
770 if (ext->rdi != NULL) {
771 switch (ext->rdi->type) {
772 case ASIdentifierChoice_inherit:
773 inherit_rdi = 1;
774 break;
775 case ASIdentifierChoice_asIdsOrRanges:
776 child_rdi = ext->rdi->u.asIdsOrRanges;
777 break;
778 }
779 }
780
781 /*
782 * Now walk up the chain. Extensions must be in canonical form, no
783 * cert may list resources that its parent doesn't list.
784 */
785 for (i++; i < sk_X509_num(chain); i++) {
786 x = sk_X509_value(chain, i);
787 if (!ossl_assert(x != NULL)) {
788 if (ctx != NULL)
789 ctx->error = X509_V_ERR_UNSPECIFIED;
790 return 0;
791 }
792 if (x->rfc3779_asid == NULL) {
793 if (child_as != NULL || child_rdi != NULL)
794 validation_err(X509_V_ERR_UNNESTED_RESOURCE);
795 continue;
796 }
797 if (!X509v3_asid_is_canonical(x->rfc3779_asid))
798 validation_err(X509_V_ERR_INVALID_EXTENSION);
799 if (x->rfc3779_asid->asnum == NULL && child_as != NULL) {
800 validation_err(X509_V_ERR_UNNESTED_RESOURCE);
801 child_as = NULL;
802 inherit_as = 0;
803 }
804 if (x->rfc3779_asid->asnum != NULL &&
805 x->rfc3779_asid->asnum->type ==
806 ASIdentifierChoice_asIdsOrRanges) {
807 if (inherit_as
808 || asid_contains(x->rfc3779_asid->asnum->u.asIdsOrRanges,
809 child_as)) {
810 child_as = x->rfc3779_asid->asnum->u.asIdsOrRanges;
811 inherit_as = 0;
812 } else {
813 validation_err(X509_V_ERR_UNNESTED_RESOURCE);
814 }
815 }
816 if (x->rfc3779_asid->rdi == NULL && child_rdi != NULL) {
817 validation_err(X509_V_ERR_UNNESTED_RESOURCE);
818 child_rdi = NULL;
819 inherit_rdi = 0;
820 }
821 if (x->rfc3779_asid->rdi != NULL &&
822 x->rfc3779_asid->rdi->type == ASIdentifierChoice_asIdsOrRanges) {
823 if (inherit_rdi ||
824 asid_contains(x->rfc3779_asid->rdi->u.asIdsOrRanges,
825 child_rdi)) {
826 child_rdi = x->rfc3779_asid->rdi->u.asIdsOrRanges;
827 inherit_rdi = 0;
828 } else {
829 validation_err(X509_V_ERR_UNNESTED_RESOURCE);
830 }
831 }
832 }
833
834 /*
835 * Trust anchor can't inherit.
836 */
837 if (!ossl_assert(x != NULL)) {
838 if (ctx != NULL)
839 ctx->error = X509_V_ERR_UNSPECIFIED;
840 return 0;
841 }
842 if (x->rfc3779_asid != NULL) {
843 if (x->rfc3779_asid->asnum != NULL &&
844 x->rfc3779_asid->asnum->type == ASIdentifierChoice_inherit)
845 validation_err(X509_V_ERR_UNNESTED_RESOURCE);
846 if (x->rfc3779_asid->rdi != NULL &&
847 x->rfc3779_asid->rdi->type == ASIdentifierChoice_inherit)
848 validation_err(X509_V_ERR_UNNESTED_RESOURCE);
849 }
850
851 done:
852 return ret;
853 }
854
855 #undef validation_err
856
857 /*
858 * RFC 3779 3.3 path validation -- called from X509_verify_cert().
859 */
860 int X509v3_asid_validate_path(X509_STORE_CTX *ctx)
861 {
862 if (ctx->chain == NULL
863 || sk_X509_num(ctx->chain) == 0
864 || ctx->verify_cb == NULL)
865 return 0;
866 return asid_validate_path_internal(ctx, ctx->chain, NULL);
867 }
868
869 /*
870 * RFC 3779 3.3 path validation of an extension.
871 * Test whether chain covers extension.
872 */
873 int X509v3_asid_validate_resource_set(STACK_OF(X509) *chain,
874 ASIdentifiers *ext, int allow_inheritance)
875 {
876 if (ext == NULL)
877 return 1;
878 if (chain == NULL || sk_X509_num(chain) == 0)
879 return 0;
880 if (!allow_inheritance && X509v3_asid_inherits(ext))
881 return 0;
882 return asid_validate_path_internal(NULL, chain, ext);
883 }
884
885 #endif /* OPENSSL_NO_RFC3779 */