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