summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/x509v3/v3_asid.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/x509v3/v3_asid.c')
-rw-r--r--src/lib/libcrypto/x509v3/v3_asid.c1027
1 files changed, 0 insertions, 1027 deletions
diff --git a/src/lib/libcrypto/x509v3/v3_asid.c b/src/lib/libcrypto/x509v3/v3_asid.c
deleted file mode 100644
index 10f82c573d..0000000000
--- a/src/lib/libcrypto/x509v3/v3_asid.c
+++ /dev/null
@@ -1,1027 +0,0 @@
1/* $OpenBSD: v3_asid.c,v 1.12 2015/02/09 16:03:11 jsing Exp $ */
2/*
3 * Contributed to the OpenSSL Project by the American Registry for
4 * Internet Numbers ("ARIN").
5 */
6/* ====================================================================
7 * Copyright (c) 2006 The OpenSSL Project. All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
19 * distribution.
20 *
21 * 3. All advertising materials mentioning features or use of this
22 * software must display the following acknowledgment:
23 * "This product includes software developed by the OpenSSL Project
24 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25 *
26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27 * endorse or promote products derived from this software without
28 * prior written permission. For written permission, please contact
29 * licensing@OpenSSL.org.
30 *
31 * 5. Products derived from this software may not be called "OpenSSL"
32 * nor may "OpenSSL" appear in their names without prior written
33 * permission of the OpenSSL Project.
34 *
35 * 6. Redistributions of any form whatsoever must retain the following
36 * acknowledgment:
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 * ====================================================================
53 *
54 * This product includes cryptographic software written by Eric Young
55 * (eay@cryptsoft.com). This product includes software written by Tim
56 * Hudson (tjh@cryptsoft.com).
57 */
58
59/*
60 * Implementation of RFC 3779 section 3.2.
61 */
62
63#include <stdio.h>
64#include <string.h>
65
66#include <openssl/opensslconf.h>
67
68#include <openssl/asn1.h>
69#include <openssl/asn1t.h>
70#include <openssl/bn.h>
71#include <openssl/conf.h>
72#include <openssl/x509v3.h>
73#include <openssl/x509.h>
74
75#ifndef OPENSSL_NO_RFC3779
76
77/*
78 * OpenSSL ASN.1 template translation of RFC 3779 3.2.3.
79 */
80
81ASN1_SEQUENCE(ASRange) = {
82 ASN1_SIMPLE(ASRange, min, ASN1_INTEGER),
83 ASN1_SIMPLE(ASRange, max, ASN1_INTEGER)
84} ASN1_SEQUENCE_END(ASRange)
85
86ASN1_CHOICE(ASIdOrRange) = {
87 ASN1_SIMPLE(ASIdOrRange, u.id, ASN1_INTEGER),
88 ASN1_SIMPLE(ASIdOrRange, u.range, ASRange)
89} ASN1_CHOICE_END(ASIdOrRange)
90
91ASN1_CHOICE(ASIdentifierChoice) = {
92 ASN1_SIMPLE(ASIdentifierChoice, u.inherit, ASN1_NULL),
93 ASN1_SEQUENCE_OF(ASIdentifierChoice, u.asIdsOrRanges, ASIdOrRange)
94} ASN1_CHOICE_END(ASIdentifierChoice)
95
96ASN1_SEQUENCE(ASIdentifiers) = {
97 ASN1_EXP_OPT(ASIdentifiers, asnum, ASIdentifierChoice, 0),
98 ASN1_EXP_OPT(ASIdentifiers, rdi, ASIdentifierChoice, 1)
99} ASN1_SEQUENCE_END(ASIdentifiers)
100
101
102ASRange *
103d2i_ASRange(ASRange **a, const unsigned char **in, long len)
104{
105 return (ASRange *)ASN1_item_d2i((ASN1_VALUE **)a, in, len,
106 &ASRange_it);
107}
108
109int
110i2d_ASRange(ASRange *a, unsigned char **out)
111{
112 return ASN1_item_i2d((ASN1_VALUE *)a, out, &ASRange_it);
113}
114
115ASRange *
116ASRange_new(void)
117{
118 return (ASRange *)ASN1_item_new(&ASRange_it);
119}
120
121void
122ASRange_free(ASRange *a)
123{
124 ASN1_item_free((ASN1_VALUE *)a, &ASRange_it);
125}
126
127ASIdOrRange *
128d2i_ASIdOrRange(ASIdOrRange **a, const unsigned char **in, long len)
129{
130 return (ASIdOrRange *)ASN1_item_d2i((ASN1_VALUE **)a, in, len,
131 &ASIdOrRange_it);
132}
133
134int
135i2d_ASIdOrRange(ASIdOrRange *a, unsigned char **out)
136{
137 return ASN1_item_i2d((ASN1_VALUE *)a, out, &ASIdOrRange_it);
138}
139
140ASIdOrRange *
141ASIdOrRange_new(void)
142{
143 return (ASIdOrRange *)ASN1_item_new(&ASIdOrRange_it);
144}
145
146void
147ASIdOrRange_free(ASIdOrRange *a)
148{
149 ASN1_item_free((ASN1_VALUE *)a, &ASIdOrRange_it);
150}
151
152ASIdentifierChoice *
153d2i_ASIdentifierChoice(ASIdentifierChoice **a, const unsigned char **in, long len)
154{
155 return (ASIdentifierChoice *)ASN1_item_d2i((ASN1_VALUE **)a, in, len,
156 &ASIdentifierChoice_it);
157}
158
159int
160i2d_ASIdentifierChoice(ASIdentifierChoice *a, unsigned char **out)
161{
162 return ASN1_item_i2d((ASN1_VALUE *)a, out, &ASIdentifierChoice_it);
163}
164
165ASIdentifierChoice *
166ASIdentifierChoice_new(void)
167{
168 return (ASIdentifierChoice *)ASN1_item_new(&ASIdentifierChoice_it);
169}
170
171void
172ASIdentifierChoice_free(ASIdentifierChoice *a)
173{
174 ASN1_item_free((ASN1_VALUE *)a, &ASIdentifierChoice_it);
175}
176
177ASIdentifiers *
178d2i_ASIdentifiers(ASIdentifiers **a, const unsigned char **in, long len)
179{
180 return (ASIdentifiers *)ASN1_item_d2i((ASN1_VALUE **)a, in, len,
181 &ASIdentifiers_it);
182}
183
184int
185i2d_ASIdentifiers(ASIdentifiers *a, unsigned char **out)
186{
187 return ASN1_item_i2d((ASN1_VALUE *)a, out, &ASIdentifiers_it);
188}
189
190ASIdentifiers *
191ASIdentifiers_new(void)
192{
193 return (ASIdentifiers *)ASN1_item_new(&ASIdentifiers_it);
194}
195
196void
197ASIdentifiers_free(ASIdentifiers *a)
198{
199 ASN1_item_free((ASN1_VALUE *)a, &ASIdentifiers_it);
200}
201
202/*
203 * i2r method for an ASIdentifierChoice.
204 */
205static int
206i2r_ASIdentifierChoice(BIO *out, ASIdentifierChoice *choice, int indent,
207 const char *msg)
208{
209 int i;
210 char *s;
211
212 if (choice == NULL)
213 return 1;
214 BIO_printf(out, "%*s%s:\n", indent, "", msg);
215 switch (choice->type) {
216 case ASIdentifierChoice_inherit:
217 BIO_printf(out, "%*sinherit\n", indent + 2, "");
218 break;
219 case ASIdentifierChoice_asIdsOrRanges:
220 for (i = 0; i < sk_ASIdOrRange_num(choice->u.asIdsOrRanges);
221 i++) {
222 ASIdOrRange *aor =
223 sk_ASIdOrRange_value(choice->u.asIdsOrRanges, i);
224 switch (aor->type) {
225 case ASIdOrRange_id:
226 if ((s = i2s_ASN1_INTEGER(NULL, aor->u.id)) ==
227 NULL)
228 return 0;
229 BIO_printf(out, "%*s%s\n", indent + 2, "", s);
230 free(s);
231 break;
232 case ASIdOrRange_range:
233 if ((s = i2s_ASN1_INTEGER(NULL,
234 aor->u.range->min)) == NULL)
235 return 0;
236 BIO_printf(out, "%*s%s-", indent + 2, "", s);
237 free(s);
238 if ((s = i2s_ASN1_INTEGER(NULL,
239 aor->u.range->max)) == NULL)
240 return 0;
241 BIO_printf(out, "%s\n", s);
242 free(s);
243 break;
244 default:
245 return 0;
246 }
247 }
248 break;
249
250 default:
251 return 0;
252 }
253 return 1;
254}
255
256/*
257 * i2r method for an ASIdentifier extension.
258 */
259static int
260i2r_ASIdentifiers(const X509V3_EXT_METHOD *method, void *ext, BIO *out,
261 int indent)
262{
263 ASIdentifiers *asid = ext;
264
265 return (i2r_ASIdentifierChoice(out, asid->asnum, indent,
266 "Autonomous System Numbers") &&
267 i2r_ASIdentifierChoice(out, asid->rdi, indent,
268 "Routing Domain Identifiers"));
269}
270
271/*
272 * Sort comparision function for a sequence of ASIdOrRange elements.
273 */
274static int
275ASIdOrRange_cmp(const ASIdOrRange * const *a_, const ASIdOrRange * const *b_)
276{
277 const ASIdOrRange *a = *a_, *b = *b_;
278
279 OPENSSL_assert((a->type == ASIdOrRange_id && a->u.id != NULL) ||
280 (a->type == ASIdOrRange_range && a->u.range != NULL &&
281 a->u.range->min != NULL && a->u.range->max != NULL));
282
283 OPENSSL_assert((b->type == ASIdOrRange_id && b->u.id != NULL) ||
284 (b->type == ASIdOrRange_range && b->u.range != NULL &&
285 b->u.range->min != NULL && b->u.range->max != NULL));
286
287 if (a->type == ASIdOrRange_id && b->type == ASIdOrRange_id)
288 return ASN1_INTEGER_cmp(a->u.id, b->u.id);
289
290 if (a->type == ASIdOrRange_range && b->type == ASIdOrRange_range) {
291 int r = ASN1_INTEGER_cmp(a->u.range->min, b->u.range->min);
292 return r != 0 ? r :
293 ASN1_INTEGER_cmp(a->u.range->max, b->u.range->max);
294 }
295
296 if (a->type == ASIdOrRange_id)
297 return ASN1_INTEGER_cmp(a->u.id, b->u.range->min);
298 else
299 return ASN1_INTEGER_cmp(a->u.range->min, b->u.id);
300}
301
302/*
303 * Add an inherit element.
304 */
305int
306v3_asid_add_inherit(ASIdentifiers *asid, int which)
307{
308 ASIdentifierChoice **choice;
309
310 if (asid == NULL)
311 return 0;
312 switch (which) {
313 case V3_ASID_ASNUM:
314 choice = &asid->asnum;
315 break;
316 case V3_ASID_RDI:
317 choice = &asid->rdi;
318 break;
319 default:
320 return 0;
321 }
322 if (*choice == NULL) {
323 if ((*choice = ASIdentifierChoice_new()) == NULL)
324 return 0;
325 OPENSSL_assert((*choice)->u.inherit == NULL);
326 if (((*choice)->u.inherit = ASN1_NULL_new()) == NULL)
327 return 0;
328 (*choice)->type = ASIdentifierChoice_inherit;
329 }
330 return (*choice)->type == ASIdentifierChoice_inherit;
331}
332
333/*
334 * Add an ID or range to an ASIdentifierChoice.
335 */
336int
337v3_asid_add_id_or_range(ASIdentifiers *asid, int which, ASN1_INTEGER *min,
338 ASN1_INTEGER *max)
339{
340 ASIdentifierChoice **choice;
341 ASIdOrRange *aor;
342
343 if (asid == NULL)
344 return 0;
345 switch (which) {
346 case V3_ASID_ASNUM:
347 choice = &asid->asnum;
348 break;
349 case V3_ASID_RDI:
350 choice = &asid->rdi;
351 break;
352 default:
353 return 0;
354 }
355 if (*choice != NULL && (*choice)->type == ASIdentifierChoice_inherit)
356 return 0;
357 if (*choice == NULL) {
358 if ((*choice = ASIdentifierChoice_new()) == NULL)
359 return 0;
360 OPENSSL_assert((*choice)->u.asIdsOrRanges == NULL);
361 (*choice)->u.asIdsOrRanges =
362 sk_ASIdOrRange_new(ASIdOrRange_cmp);
363 if ((*choice)->u.asIdsOrRanges == NULL)
364 return 0;
365 (*choice)->type = ASIdentifierChoice_asIdsOrRanges;
366 }
367 if ((aor = ASIdOrRange_new()) == NULL)
368 return 0;
369 if (max == NULL) {
370 aor->type = ASIdOrRange_id;
371 aor->u.id = min;
372 } else {
373 aor->type = ASIdOrRange_range;
374 if ((aor->u.range = ASRange_new()) == NULL)
375 goto err;
376 ASN1_INTEGER_free(aor->u.range->min);
377 aor->u.range->min = min;
378 ASN1_INTEGER_free(aor->u.range->max);
379 aor->u.range->max = max;
380 }
381 if (!(sk_ASIdOrRange_push((*choice)->u.asIdsOrRanges, aor)))
382 goto err;
383 return 1;
384
385err:
386 ASIdOrRange_free(aor);
387 return 0;
388}
389
390/*
391 * Extract min and max values from an ASIdOrRange.
392 */
393static void
394extract_min_max(ASIdOrRange *aor, ASN1_INTEGER **min, ASN1_INTEGER **max)
395{
396 OPENSSL_assert(aor != NULL && min != NULL && max != NULL);
397
398 switch (aor->type) {
399 case ASIdOrRange_id:
400 *min = aor->u.id;
401 *max = aor->u.id;
402 return;
403 case ASIdOrRange_range:
404 *min = aor->u.range->min;
405 *max = aor->u.range->max;
406 return;
407 }
408}
409
410/*
411 * Check whether an ASIdentifierChoice is in canonical form.
412 */
413static int
414ASIdentifierChoice_is_canonical(ASIdentifierChoice *choice)
415{
416 ASN1_INTEGER *a_max_plus_one = NULL;
417 BIGNUM *bn = NULL;
418 int i, ret = 0;
419
420 /*
421 * Empty element or inheritance is canonical.
422 */
423 if (choice == NULL || choice->type == ASIdentifierChoice_inherit)
424 return 1;
425
426 /*
427 * If not a list, or if empty list, it's broken.
428 */
429 if (choice->type != ASIdentifierChoice_asIdsOrRanges ||
430 sk_ASIdOrRange_num(choice->u.asIdsOrRanges) == 0)
431 return 0;
432
433 /*
434 * It's a list, check it.
435 */
436 for (i = 0; i < sk_ASIdOrRange_num(choice->u.asIdsOrRanges) - 1; i++) {
437 ASIdOrRange *a =
438 sk_ASIdOrRange_value(choice->u.asIdsOrRanges, i);
439 ASIdOrRange *b =
440 sk_ASIdOrRange_value(choice->u.asIdsOrRanges, i + 1);
441 ASN1_INTEGER *a_min, *a_max, *b_min, *b_max;
442
443 extract_min_max(a, &a_min, &a_max);
444 extract_min_max(b, &b_min, &b_max);
445
446 /*
447 * Punt misordered list, overlapping start, or inverted range.
448 */
449 if (ASN1_INTEGER_cmp(a_min, b_min) >= 0 ||
450 ASN1_INTEGER_cmp(a_min, a_max) > 0 ||
451 ASN1_INTEGER_cmp(b_min, b_max) > 0)
452 goto done;
453
454 /*
455 * Calculate a_max + 1 to check for adjacency.
456 */
457 if ((bn == NULL && (bn = BN_new()) == NULL) ||
458 ASN1_INTEGER_to_BN(a_max, bn) == NULL ||
459 !BN_add_word(bn, 1) || (a_max_plus_one =
460 BN_to_ASN1_INTEGER(bn, a_max_plus_one)) == NULL) {
461 X509V3err(X509V3_F_ASIDENTIFIERCHOICE_IS_CANONICAL,
462 ERR_R_MALLOC_FAILURE);
463 goto done;
464 }
465
466 /*
467 * Punt if adjacent or overlapping.
468 */
469 if (ASN1_INTEGER_cmp(a_max_plus_one, b_min) >= 0)
470 goto done;
471 }
472
473 /*
474 * Check for inverted range.
475 */
476 i = sk_ASIdOrRange_num(choice->u.asIdsOrRanges) - 1;
477 {
478 ASIdOrRange *a =
479 sk_ASIdOrRange_value(choice->u.asIdsOrRanges, i);
480 ASN1_INTEGER *a_min, *a_max;
481
482 if (a != NULL && a->type == ASIdOrRange_range) {
483 extract_min_max(a, &a_min, &a_max);
484 if (ASN1_INTEGER_cmp(a_min, a_max) > 0)
485 goto done;
486 }
487 }
488
489 ret = 1;
490
491done:
492 ASN1_INTEGER_free(a_max_plus_one);
493 BN_free(bn);
494 return ret;
495}
496
497/*
498 * Check whether an ASIdentifier extension is in canonical form.
499 */
500int
501v3_asid_is_canonical(ASIdentifiers *asid)
502{
503 return (asid == NULL ||
504 (ASIdentifierChoice_is_canonical(asid->asnum) &&
505 ASIdentifierChoice_is_canonical(asid->rdi)));
506}
507
508/*
509 * Whack an ASIdentifierChoice into canonical form.
510 */
511static int
512ASIdentifierChoice_canonize(ASIdentifierChoice *choice)
513{
514 ASN1_INTEGER *a_max_plus_one = NULL;
515 BIGNUM *bn = NULL;
516 int i, ret = 0;
517
518 /*
519 * Nothing to do for empty element or inheritance.
520 */
521 if (choice == NULL || choice->type == ASIdentifierChoice_inherit)
522 return 1;
523
524 /*
525 * If not a list, or if empty list, it's broken.
526 */
527 if (choice->type != ASIdentifierChoice_asIdsOrRanges ||
528 sk_ASIdOrRange_num(choice->u.asIdsOrRanges) == 0) {
529 X509V3err(X509V3_F_ASIDENTIFIERCHOICE_CANONIZE,
530 X509V3_R_EXTENSION_VALUE_ERROR);
531 return 0;
532 }
533
534 /*
535 * We have a non-empty list. Sort it.
536 */
537 sk_ASIdOrRange_sort(choice->u.asIdsOrRanges);
538
539 /*
540 * Now check for errors and suboptimal encoding, rejecting the
541 * former and fixing the latter.
542 */
543 for (i = 0; i < sk_ASIdOrRange_num(choice->u.asIdsOrRanges) - 1; i++) {
544 ASIdOrRange *a =
545 sk_ASIdOrRange_value(choice->u.asIdsOrRanges, i);
546 ASIdOrRange *b =
547 sk_ASIdOrRange_value(choice->u.asIdsOrRanges, i + 1);
548 ASN1_INTEGER *a_min, *a_max, *b_min, *b_max;
549
550 extract_min_max(a, &a_min, &a_max);
551 extract_min_max(b, &b_min, &b_max);
552
553 /*
554 * Make sure we're properly sorted (paranoia).
555 */
556 OPENSSL_assert(ASN1_INTEGER_cmp(a_min, b_min) <= 0);
557
558 /*
559 * Punt inverted ranges.
560 */
561 if (ASN1_INTEGER_cmp(a_min, a_max) > 0 ||
562 ASN1_INTEGER_cmp(b_min, b_max) > 0)
563 goto done;
564
565 /*
566 * Check for overlaps.
567 */
568 if (ASN1_INTEGER_cmp(a_max, b_min) >= 0) {
569 X509V3err(X509V3_F_ASIDENTIFIERCHOICE_CANONIZE,
570 X509V3_R_EXTENSION_VALUE_ERROR);
571 goto done;
572 }
573
574 /*
575 * Calculate a_max + 1 to check for adjacency.
576 */
577 if ((bn == NULL && (bn = BN_new()) == NULL) ||
578 ASN1_INTEGER_to_BN(a_max, bn) == NULL ||
579 !BN_add_word(bn, 1) || (a_max_plus_one =
580 BN_to_ASN1_INTEGER(bn, a_max_plus_one)) == NULL) {
581 X509V3err(X509V3_F_ASIDENTIFIERCHOICE_CANONIZE,
582 ERR_R_MALLOC_FAILURE);
583 goto done;
584 }
585
586 /*
587 * If a and b are adjacent, merge them.
588 */
589 if (ASN1_INTEGER_cmp(a_max_plus_one, b_min) == 0) {
590 ASRange *r;
591 switch (a->type) {
592 case ASIdOrRange_id:
593 if ((r = malloc(sizeof(ASRange))) == NULL) {
594 X509V3err(X509V3_F_ASIDENTIFIERCHOICE_CANONIZE,
595 ERR_R_MALLOC_FAILURE);
596 goto done;
597 }
598 r->min = a_min;
599 r->max = b_max;
600 a->type = ASIdOrRange_range;
601 a->u.range = r;
602 break;
603 case ASIdOrRange_range:
604 ASN1_INTEGER_free(a->u.range->max);
605 a->u.range->max = b_max;
606 break;
607 }
608 switch (b->type) {
609 case ASIdOrRange_id:
610 b->u.id = NULL;
611 break;
612 case ASIdOrRange_range:
613 b->u.range->max = NULL;
614 break;
615 }
616 ASIdOrRange_free(b);
617 (void) sk_ASIdOrRange_delete(
618 choice->u.asIdsOrRanges, i + 1);
619 i--;
620 continue;
621 }
622 }
623
624 /*
625 * Check for final inverted range.
626 */
627 i = sk_ASIdOrRange_num(choice->u.asIdsOrRanges) - 1;
628 {
629 ASIdOrRange *a =
630 sk_ASIdOrRange_value(choice->u.asIdsOrRanges, i);
631 ASN1_INTEGER *a_min, *a_max;
632 if (a != NULL && a->type == ASIdOrRange_range) {
633 extract_min_max(a, &a_min, &a_max);
634 if (ASN1_INTEGER_cmp(a_min, a_max) > 0)
635 goto done;
636 }
637 }
638
639 OPENSSL_assert(ASIdentifierChoice_is_canonical(choice)); /* Paranoia */
640
641 ret = 1;
642
643done:
644 ASN1_INTEGER_free(a_max_plus_one);
645 BN_free(bn);
646 return ret;
647}
648
649/*
650 * Whack an ASIdentifier extension into canonical form.
651 */
652int
653v3_asid_canonize(ASIdentifiers *asid)
654{
655 return (asid == NULL ||
656 (ASIdentifierChoice_canonize(asid->asnum) &&
657 ASIdentifierChoice_canonize(asid->rdi)));
658}
659
660/*
661 * v2i method for an ASIdentifier extension.
662 */
663static void *
664v2i_ASIdentifiers(const struct v3_ext_method *method, struct v3_ext_ctx *ctx,
665 STACK_OF(CONF_VALUE) *values)
666{
667 ASN1_INTEGER *min = NULL, *max = NULL;
668 ASIdentifiers *asid = NULL;
669 int i;
670
671 if ((asid = ASIdentifiers_new()) == NULL) {
672 X509V3err(X509V3_F_V2I_ASIDENTIFIERS, ERR_R_MALLOC_FAILURE);
673 return NULL;
674 }
675
676 for (i = 0; i < sk_CONF_VALUE_num(values); i++) {
677 CONF_VALUE *val = sk_CONF_VALUE_value(values, i);
678 int i1, i2, i3, is_range, which;
679
680 /*
681 * Figure out whether this is an AS or an RDI.
682 */
683 if (!name_cmp(val->name, "AS")) {
684 which = V3_ASID_ASNUM;
685 } else if (!name_cmp(val->name, "RDI")) {
686 which = V3_ASID_RDI;
687 } else {
688 X509V3err(X509V3_F_V2I_ASIDENTIFIERS,
689 X509V3_R_EXTENSION_NAME_ERROR);
690 X509V3_conf_err(val);
691 goto err;
692 }
693
694 /*
695 * Handle inheritance.
696 */
697 if (!strcmp(val->value, "inherit")) {
698 if (v3_asid_add_inherit(asid, which))
699 continue;
700 X509V3err(X509V3_F_V2I_ASIDENTIFIERS,
701 X509V3_R_INVALID_INHERITANCE);
702 X509V3_conf_err(val);
703 goto err;
704 }
705
706 /*
707 * Number, range, or mistake, pick it apart and figure out which.
708 */
709 i1 = strspn(val->value, "0123456789");
710 if (val->value[i1] == '\0') {
711 is_range = 0;
712 } else {
713 is_range = 1;
714 i2 = i1 + strspn(val->value + i1, " \t");
715 if (val->value[i2] != '-') {
716 X509V3err(X509V3_F_V2I_ASIDENTIFIERS,
717 X509V3_R_INVALID_ASNUMBER);
718 X509V3_conf_err(val);
719 goto err;
720 }
721 i2++;
722 i2 = i2 + strspn(val->value + i2, " \t");
723 i3 = i2 + strspn(val->value + i2, "0123456789");
724 if (val->value[i3] != '\0') {
725 X509V3err(X509V3_F_V2I_ASIDENTIFIERS,
726 X509V3_R_INVALID_ASRANGE);
727 X509V3_conf_err(val);
728 goto err;
729 }
730 }
731
732 /*
733 * Syntax is ok, read and add it.
734 */
735 if (!is_range) {
736 if (!X509V3_get_value_int(val, &min)) {
737 X509V3err(X509V3_F_V2I_ASIDENTIFIERS,
738 ERR_R_MALLOC_FAILURE);
739 goto err;
740 }
741 } else {
742 char *s = strdup(val->value);
743 if (s == NULL) {
744 X509V3err(X509V3_F_V2I_ASIDENTIFIERS,
745 ERR_R_MALLOC_FAILURE);
746 goto err;
747 }
748 s[i1] = '\0';
749 min = s2i_ASN1_INTEGER(NULL, s);
750 max = s2i_ASN1_INTEGER(NULL, s + i2);
751 free(s);
752 if (min == NULL || max == NULL) {
753 X509V3err(X509V3_F_V2I_ASIDENTIFIERS,
754 ERR_R_MALLOC_FAILURE);
755 goto err;
756 }
757 if (ASN1_INTEGER_cmp(min, max) > 0) {
758 X509V3err(X509V3_F_V2I_ASIDENTIFIERS,
759 X509V3_R_EXTENSION_VALUE_ERROR);
760 goto err;
761 }
762 }
763 if (!v3_asid_add_id_or_range(asid, which, min, max)) {
764 X509V3err(X509V3_F_V2I_ASIDENTIFIERS,
765 ERR_R_MALLOC_FAILURE);
766 goto err;
767 }
768 min = max = NULL;
769 }
770
771 /*
772 * Canonize the result, then we're done.
773 */
774 if (!v3_asid_canonize(asid))
775 goto err;
776 return asid;
777
778err:
779 ASIdentifiers_free(asid);
780 ASN1_INTEGER_free(min);
781 ASN1_INTEGER_free(max);
782 return NULL;
783}
784
785/*
786 * OpenSSL dispatch.
787 */
788const X509V3_EXT_METHOD v3_asid = {
789 NID_sbgp_autonomousSysNum, /* nid */
790 0, /* flags */
791 ASN1_ITEM_ref(ASIdentifiers), /* template */
792 0, 0, 0, 0, /* old functions, ignored */
793 0, /* i2s */
794 0, /* s2i */
795 0, /* i2v */
796 v2i_ASIdentifiers, /* v2i */
797 i2r_ASIdentifiers, /* i2r */
798 0, /* r2i */
799 NULL /* extension-specific data */
800};
801
802/*
803 * Figure out whether extension uses inheritance.
804 */
805int
806v3_asid_inherits(ASIdentifiers *asid)
807{
808 return (asid != NULL &&
809 ((asid->asnum != NULL &&
810 asid->asnum->type == ASIdentifierChoice_inherit) ||
811 (asid->rdi != NULL &&
812 asid->rdi->type == ASIdentifierChoice_inherit)));
813}
814
815/*
816 * Figure out whether parent contains child.
817 */
818static int
819asid_contains(ASIdOrRanges *parent, ASIdOrRanges *child)
820{
821 ASN1_INTEGER *p_min, *p_max, *c_min, *c_max;
822 int p, c;
823
824 if (child == NULL || parent == child)
825 return 1;
826 if (parent == NULL)
827 return 0;
828
829 p = 0;
830 for (c = 0; c < sk_ASIdOrRange_num(child); c++) {
831 extract_min_max(sk_ASIdOrRange_value(child, c),
832 &c_min, &c_max);
833 for (; ; p++) {
834 if (p >= sk_ASIdOrRange_num(parent))
835 return 0;
836 extract_min_max(sk_ASIdOrRange_value(parent, p),
837 &p_min, &p_max);
838 if (ASN1_INTEGER_cmp(p_max, c_max) < 0)
839 continue;
840 if (ASN1_INTEGER_cmp(p_min, c_min) > 0)
841 return 0;
842 break;
843 }
844 }
845
846 return 1;
847}
848
849/*
850 * Test whether a is a subet of b.
851 */
852int
853v3_asid_subset(ASIdentifiers *a, ASIdentifiers *b)
854{
855 return (a == NULL || a == b ||
856 (b != NULL && !v3_asid_inherits(a) && !v3_asid_inherits(b) &&
857 asid_contains(b->asnum->u.asIdsOrRanges,
858 a->asnum->u.asIdsOrRanges) &&
859 asid_contains(b->rdi->u.asIdsOrRanges,
860 a->rdi->u.asIdsOrRanges)));
861}
862
863/*
864 * Validation error handling via callback.
865 */
866#define validation_err(_err_) \
867 do { \
868 if (ctx != NULL) { \
869 ctx->error = _err_; \
870 ctx->error_depth = i; \
871 ctx->current_cert = x; \
872 ret = ctx->verify_cb(0, ctx); \
873 } else { \
874 ret = 0; \
875 } \
876 if (!ret) \
877 goto done; \
878 } while (0)
879
880/*
881 * Core code for RFC 3779 3.3 path validation.
882 */
883static int
884v3_asid_validate_path_internal(X509_STORE_CTX *ctx, STACK_OF(X509) *chain,
885 ASIdentifiers *ext)
886{
887 ASIdOrRanges *child_as = NULL, *child_rdi = NULL;
888 int i, ret = 1, inherit_as = 0, inherit_rdi = 0;
889 X509 *x;
890
891 OPENSSL_assert(chain != NULL && sk_X509_num(chain) > 0);
892 OPENSSL_assert(ctx != NULL || ext != NULL);
893 OPENSSL_assert(ctx == NULL || ctx->verify_cb != NULL);
894
895 /*
896 * Figure out where to start. If we don't have an extension to
897 * check, we're done. Otherwise, check canonical form and
898 * set up for walking up the chain.
899 */
900 if (ext != NULL) {
901 i = -1;
902 x = NULL;
903 } else {
904 i = 0;
905 x = sk_X509_value(chain, i);
906 OPENSSL_assert(x != NULL);
907 if ((ext = x->rfc3779_asid) == NULL)
908 goto done;
909 }
910 if (!v3_asid_is_canonical(ext))
911 validation_err(X509_V_ERR_INVALID_EXTENSION);
912 if (ext->asnum != NULL) {
913 switch (ext->asnum->type) {
914 case ASIdentifierChoice_inherit:
915 inherit_as = 1;
916 break;
917 case ASIdentifierChoice_asIdsOrRanges:
918 child_as = ext->asnum->u.asIdsOrRanges;
919 break;
920 }
921 }
922 if (ext->rdi != NULL) {
923 switch (ext->rdi->type) {
924 case ASIdentifierChoice_inherit:
925 inherit_rdi = 1;
926 break;
927 case ASIdentifierChoice_asIdsOrRanges:
928 child_rdi = ext->rdi->u.asIdsOrRanges;
929 break;
930 }
931 }
932
933 /*
934 * Now walk up the chain. Extensions must be in canonical form, no
935 * cert may list resources that its parent doesn't list.
936 */
937 for (i++; i < sk_X509_num(chain); i++) {
938 x = sk_X509_value(chain, i);
939 OPENSSL_assert(x != NULL);
940 if (x->rfc3779_asid == NULL) {
941 if (child_as != NULL || child_rdi != NULL)
942 validation_err(X509_V_ERR_UNNESTED_RESOURCE);
943 continue;
944 }
945 if (!v3_asid_is_canonical(x->rfc3779_asid))
946 validation_err(X509_V_ERR_INVALID_EXTENSION);
947 if (x->rfc3779_asid->asnum == NULL && child_as != NULL) {
948 validation_err(X509_V_ERR_UNNESTED_RESOURCE);
949 child_as = NULL;
950 inherit_as = 0;
951 }
952 if (x->rfc3779_asid->asnum != NULL &&
953 x->rfc3779_asid->asnum->type ==
954 ASIdentifierChoice_asIdsOrRanges) {
955 if (inherit_as || asid_contains(
956 x->rfc3779_asid->asnum->u.asIdsOrRanges,
957 child_as)) {
958 child_as = x->rfc3779_asid->asnum->u.asIdsOrRanges;
959 inherit_as = 0;
960 } else {
961 validation_err(X509_V_ERR_UNNESTED_RESOURCE);
962 }
963 }
964 if (x->rfc3779_asid->rdi == NULL && child_rdi != NULL) {
965 validation_err(X509_V_ERR_UNNESTED_RESOURCE);
966 child_rdi = NULL;
967 inherit_rdi = 0;
968 }
969 if (x->rfc3779_asid->rdi != NULL &&
970 x->rfc3779_asid->rdi->type ==
971 ASIdentifierChoice_asIdsOrRanges) {
972 if (inherit_rdi || asid_contains(
973 x->rfc3779_asid->rdi->u.asIdsOrRanges, child_rdi)) {
974 child_rdi = x->rfc3779_asid->rdi->u.asIdsOrRanges;
975 inherit_rdi = 0;
976 } else {
977 validation_err(X509_V_ERR_UNNESTED_RESOURCE);
978 }
979 }
980 }
981
982 /*
983 * Trust anchor can't inherit.
984 */
985 OPENSSL_assert(x != NULL);
986 if (x->rfc3779_asid != NULL) {
987 if (x->rfc3779_asid->asnum != NULL &&
988 x->rfc3779_asid->asnum->type == ASIdentifierChoice_inherit)
989 validation_err(X509_V_ERR_UNNESTED_RESOURCE);
990 if (x->rfc3779_asid->rdi != NULL &&
991 x->rfc3779_asid->rdi->type == ASIdentifierChoice_inherit)
992 validation_err(X509_V_ERR_UNNESTED_RESOURCE);
993 }
994
995done:
996 return ret;
997}
998
999#undef validation_err
1000
1001/*
1002 * RFC 3779 3.3 path validation -- called from X509_verify_cert().
1003 */
1004int
1005v3_asid_validate_path(X509_STORE_CTX *ctx)
1006{
1007 return v3_asid_validate_path_internal(ctx, ctx->chain, NULL);
1008}
1009
1010/*
1011 * RFC 3779 3.3 path validation of an extension.
1012 * Test whether chain covers extension.
1013 */
1014int
1015v3_asid_validate_resource_set(STACK_OF(X509) *chain, ASIdentifiers *ext,
1016 int allow_inheritance)
1017{
1018 if (ext == NULL)
1019 return 1;
1020 if (chain == NULL || sk_X509_num(chain) == 0)
1021 return 0;
1022 if (!allow_inheritance && v3_asid_inherits(ext))
1023 return 0;
1024 return v3_asid_validate_path_internal(NULL, chain, ext);
1025}
1026
1027#endif /* OPENSSL_NO_RFC3779 */