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