xref: /openbsd-src/lib/libcrypto/x509/x509_asid.c (revision 3374c67d44f9b75b98444cbf63020f777792342e)
1 /*	$OpenBSD: x509_asid.c,v 1.38 2022/11/26 16:08:54 tb Exp $ */
2 /*
3  * Contributed to the OpenSSL Project by the American Registry for
4  * Internet Numbers ("ARIN").
5  */
6 /* ====================================================================
7  * Copyright (c) 2006-2018 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 <stdlib.h>
65 #include <string.h>
66 
67 #include <openssl/asn1.h>
68 #include <openssl/asn1t.h>
69 #include <openssl/bn.h>
70 #include <openssl/conf.h>
71 #include <openssl/err.h>
72 #include <openssl/x509.h>
73 #include <openssl/x509.h>
74 #include <openssl/x509v3.h>
75 
76 #include "x509_local.h"
77 
78 #ifndef OPENSSL_NO_RFC3779
79 
80 static const ASN1_TEMPLATE ASRange_seq_tt[] = {
81 	{
82 		.flags = 0,
83 		.tag = 0,
84 		.offset = offsetof(ASRange, min),
85 		.field_name = "min",
86 		.item = &ASN1_INTEGER_it,
87 	},
88 	{
89 		.flags = 0,
90 		.tag = 0,
91 		.offset = offsetof(ASRange, max),
92 		.field_name = "max",
93 		.item = &ASN1_INTEGER_it,
94 	},
95 };
96 
97 const ASN1_ITEM ASRange_it = {
98 	.itype = ASN1_ITYPE_SEQUENCE,
99 	.utype = V_ASN1_SEQUENCE,
100 	.templates = ASRange_seq_tt,
101 	.tcount = sizeof(ASRange_seq_tt) / sizeof(ASN1_TEMPLATE),
102 	.funcs = NULL,
103 	.size = sizeof(ASRange),
104 	.sname = "ASRange",
105 };
106 
107 static const ASN1_TEMPLATE ASIdOrRange_ch_tt[] = {
108 	{
109 		.flags = 0,
110 		.tag = 0,
111 		.offset = offsetof(ASIdOrRange, u.id),
112 		.field_name = "u.id",
113 		.item = &ASN1_INTEGER_it,
114 	},
115 	{
116 		.flags = 0,
117 		.tag = 0,
118 		.offset = offsetof(ASIdOrRange, u.range),
119 		.field_name = "u.range",
120 		.item = &ASRange_it,
121 	},
122 };
123 
124 const ASN1_ITEM ASIdOrRange_it = {
125 	.itype = ASN1_ITYPE_CHOICE,
126 	.utype = offsetof(ASIdOrRange, type),
127 	.templates = ASIdOrRange_ch_tt,
128 	.tcount = sizeof(ASIdOrRange_ch_tt) / sizeof(ASN1_TEMPLATE),
129 	.funcs = NULL,
130 	.size = sizeof(ASIdOrRange),
131 	.sname = "ASIdOrRange",
132 };
133 
134 static const ASN1_TEMPLATE ASIdentifierChoice_ch_tt[] = {
135 	{
136 		.flags = 0,
137 		.tag = 0,
138 		.offset = offsetof(ASIdentifierChoice, u.inherit),
139 		.field_name = "u.inherit",
140 		.item = &ASN1_NULL_it,
141 	},
142 	{
143 		.flags = ASN1_TFLG_SEQUENCE_OF,
144 		.tag = 0,
145 		.offset = offsetof(ASIdentifierChoice, u.asIdsOrRanges),
146 		.field_name = "u.asIdsOrRanges",
147 		.item = &ASIdOrRange_it,
148 	},
149 };
150 
151 const ASN1_ITEM ASIdentifierChoice_it = {
152 	.itype = ASN1_ITYPE_CHOICE,
153 	.utype = offsetof(ASIdentifierChoice, type),
154 	.templates = ASIdentifierChoice_ch_tt,
155 	.tcount = sizeof(ASIdentifierChoice_ch_tt) / sizeof(ASN1_TEMPLATE),
156 	.funcs = NULL,
157 	.size = sizeof(ASIdentifierChoice),
158 	.sname = "ASIdentifierChoice",
159 };
160 
161 static const ASN1_TEMPLATE ASIdentifiers_seq_tt[] = {
162 	{
163 		.flags = ASN1_TFLG_EXPLICIT | ASN1_TFLG_OPTIONAL,
164 		.tag = 0,
165 		.offset = offsetof(ASIdentifiers, asnum),
166 		.field_name = "asnum",
167 		.item = &ASIdentifierChoice_it,
168 	},
169 	{
170 		.flags = ASN1_TFLG_EXPLICIT | ASN1_TFLG_OPTIONAL,
171 		.tag = 1,
172 		.offset = offsetof(ASIdentifiers, rdi),
173 		.field_name = "rdi",
174 		.item = &ASIdentifierChoice_it,
175 	},
176 };
177 
178 const ASN1_ITEM ASIdentifiers_it = {
179 	.itype = ASN1_ITYPE_SEQUENCE,
180 	.utype = V_ASN1_SEQUENCE,
181 	.templates = ASIdentifiers_seq_tt,
182 	.tcount = sizeof(ASIdentifiers_seq_tt) / sizeof(ASN1_TEMPLATE),
183 	.funcs = NULL,
184 	.size = sizeof(ASIdentifiers),
185 	.sname = "ASIdentifiers",
186 };
187 
188 ASRange *
189 d2i_ASRange(ASRange **a, const unsigned char **in, long len)
190 {
191 	return (ASRange *)ASN1_item_d2i((ASN1_VALUE **)a, in, len,
192 	    &ASRange_it);
193 }
194 LCRYPTO_ALIAS(d2i_ASRange)
195 
196 int
197 i2d_ASRange(ASRange *a, unsigned char **out)
198 {
199 	return ASN1_item_i2d((ASN1_VALUE *)a, out, &ASRange_it);
200 }
201 LCRYPTO_ALIAS(i2d_ASRange)
202 
203 ASRange *
204 ASRange_new(void)
205 {
206 	return (ASRange *)ASN1_item_new(&ASRange_it);
207 }
208 LCRYPTO_ALIAS(ASRange_new)
209 
210 void
211 ASRange_free(ASRange *a)
212 {
213 	ASN1_item_free((ASN1_VALUE *)a, &ASRange_it);
214 }
215 LCRYPTO_ALIAS(ASRange_free)
216 
217 ASIdOrRange *
218 d2i_ASIdOrRange(ASIdOrRange **a, const unsigned char **in, long len)
219 {
220 	return (ASIdOrRange *)ASN1_item_d2i((ASN1_VALUE **)a, in, len,
221 	    &ASIdOrRange_it);
222 }
223 LCRYPTO_ALIAS(d2i_ASIdOrRange)
224 
225 int
226 i2d_ASIdOrRange(ASIdOrRange *a, unsigned char **out)
227 {
228 	return ASN1_item_i2d((ASN1_VALUE *)a, out, &ASIdOrRange_it);
229 }
230 LCRYPTO_ALIAS(i2d_ASIdOrRange)
231 
232 ASIdOrRange *
233 ASIdOrRange_new(void)
234 {
235 	return (ASIdOrRange *)ASN1_item_new(&ASIdOrRange_it);
236 }
237 LCRYPTO_ALIAS(ASIdOrRange_new)
238 
239 void
240 ASIdOrRange_free(ASIdOrRange *a)
241 {
242 	ASN1_item_free((ASN1_VALUE *)a, &ASIdOrRange_it);
243 }
244 LCRYPTO_ALIAS(ASIdOrRange_free)
245 
246 ASIdentifierChoice *
247 d2i_ASIdentifierChoice(ASIdentifierChoice **a, const unsigned char **in,
248     long len)
249 {
250 	return (ASIdentifierChoice *)ASN1_item_d2i((ASN1_VALUE **)a, in, len,
251 	    &ASIdentifierChoice_it);
252 }
253 LCRYPTO_ALIAS(d2i_ASIdentifierChoice)
254 
255 int
256 i2d_ASIdentifierChoice(ASIdentifierChoice *a, unsigned char **out)
257 {
258 	return ASN1_item_i2d((ASN1_VALUE *)a, out, &ASIdentifierChoice_it);
259 }
260 LCRYPTO_ALIAS(i2d_ASIdentifierChoice)
261 
262 ASIdentifierChoice *
263 ASIdentifierChoice_new(void)
264 {
265 	return (ASIdentifierChoice *)ASN1_item_new(&ASIdentifierChoice_it);
266 }
267 LCRYPTO_ALIAS(ASIdentifierChoice_new)
268 
269 void
270 ASIdentifierChoice_free(ASIdentifierChoice *a)
271 {
272 	ASN1_item_free((ASN1_VALUE *)a, &ASIdentifierChoice_it);
273 }
274 LCRYPTO_ALIAS(ASIdentifierChoice_free)
275 
276 ASIdentifiers *
277 d2i_ASIdentifiers(ASIdentifiers **a, const unsigned char **in, long len)
278 {
279 	return (ASIdentifiers *)ASN1_item_d2i((ASN1_VALUE **)a, in, len,
280 	    &ASIdentifiers_it);
281 }
282 LCRYPTO_ALIAS(d2i_ASIdentifiers)
283 
284 int
285 i2d_ASIdentifiers(ASIdentifiers *a, unsigned char **out)
286 {
287 	return ASN1_item_i2d((ASN1_VALUE *)a, out, &ASIdentifiers_it);
288 }
289 LCRYPTO_ALIAS(i2d_ASIdentifiers)
290 
291 ASIdentifiers *
292 ASIdentifiers_new(void)
293 {
294 	return (ASIdentifiers *)ASN1_item_new(&ASIdentifiers_it);
295 }
296 LCRYPTO_ALIAS(ASIdentifiers_new)
297 
298 void
299 ASIdentifiers_free(ASIdentifiers *a)
300 {
301 	ASN1_item_free((ASN1_VALUE *)a, &ASIdentifiers_it);
302 }
303 LCRYPTO_ALIAS(ASIdentifiers_free)
304 
305 /*
306  * i2r method for an ASIdentifierChoice.
307  */
308 static int
309 i2r_ASIdentifierChoice(BIO *out, ASIdentifierChoice *choice, int indent,
310     const char *msg)
311 {
312 	int i;
313 	char *s;
314 	if (choice == NULL)
315 		return 1;
316 	BIO_printf(out, "%*s%s:\n", indent, "", msg);
317 	switch (choice->type) {
318 	case ASIdentifierChoice_inherit:
319 		BIO_printf(out, "%*sinherit\n", indent + 2, "");
320 		break;
321 	case ASIdentifierChoice_asIdsOrRanges:
322 		for (i = 0; i < sk_ASIdOrRange_num(choice->u.asIdsOrRanges);
323 		    i++) {
324 			ASIdOrRange *aor =
325 			    sk_ASIdOrRange_value(choice->u.asIdsOrRanges, i);
326 			switch (aor->type) {
327 			case ASIdOrRange_id:
328 				if ((s = i2s_ASN1_INTEGER(NULL, aor->u.id)) ==
329 				    NULL)
330 					return 0;
331 				BIO_printf(out, "%*s%s\n", indent + 2, "", s);
332 				free(s);
333 				break;
334 			case ASIdOrRange_range:
335 				if ((s = i2s_ASN1_INTEGER(NULL,
336 				    aor->u.range->min)) == NULL)
337 					return 0;
338 				BIO_printf(out, "%*s%s-", indent + 2, "", s);
339 				free(s);
340 				if ((s = i2s_ASN1_INTEGER(NULL,
341 				    aor->u.range->max)) == NULL)
342 					return 0;
343 				BIO_printf(out, "%s\n", s);
344 				free(s);
345 				break;
346 			default:
347 				return 0;
348 			}
349 		}
350 		break;
351 	default:
352 		return 0;
353 	}
354 	return 1;
355 }
356 
357 /*
358  * i2r method for an ASIdentifier extension.
359  */
360 static int
361 i2r_ASIdentifiers(const X509V3_EXT_METHOD *method, void *ext, BIO *out,
362     int indent)
363 {
364 	ASIdentifiers *asid = ext;
365 	return (i2r_ASIdentifierChoice(out, asid->asnum, indent,
366 	    "Autonomous System Numbers") &&
367 	    i2r_ASIdentifierChoice(out, asid->rdi, indent,
368 	    "Routing Domain Identifiers"));
369 }
370 
371 /*
372  * Sort comparison function for a sequence of ASIdOrRange elements.
373  */
374 static int
375 ASIdOrRange_cmp(const ASIdOrRange *const *a_, const ASIdOrRange *const *b_)
376 {
377 	const ASIdOrRange *a = *a_, *b = *b_;
378 
379 	/* XXX: these asserts need to be replaced */
380 	OPENSSL_assert((a->type == ASIdOrRange_id && a->u.id != NULL) ||
381 	    (a->type == ASIdOrRange_range && a->u.range != NULL &&
382 	     a->u.range->min != NULL && a->u.range->max != NULL));
383 
384 	OPENSSL_assert((b->type == ASIdOrRange_id && b->u.id != NULL) ||
385 	    (b->type == ASIdOrRange_range && b->u.range != NULL &&
386 	     b->u.range->min != NULL && b->u.range->max != NULL));
387 
388 	if (a->type == ASIdOrRange_id && b->type == ASIdOrRange_id)
389 		return ASN1_INTEGER_cmp(a->u.id, b->u.id);
390 
391 	if (a->type == ASIdOrRange_range && b->type == ASIdOrRange_range) {
392 		int r = ASN1_INTEGER_cmp(a->u.range->min, b->u.range->min);
393 		return r != 0 ? r : ASN1_INTEGER_cmp(a->u.range->max,
394 		    b->u.range->max);
395 	}
396 
397 	if (a->type == ASIdOrRange_id)
398 		return ASN1_INTEGER_cmp(a->u.id, b->u.range->min);
399 	else
400 		return ASN1_INTEGER_cmp(a->u.range->min, b->u.id);
401 }
402 
403 /*
404  * Add an inherit element.
405  */
406 int
407 X509v3_asid_add_inherit(ASIdentifiers *asid, int which)
408 {
409 	ASIdentifierChoice **choice;
410 	if (asid == NULL)
411 		return 0;
412 	switch (which) {
413 	case V3_ASID_ASNUM:
414 		choice = &asid->asnum;
415 		break;
416 	case V3_ASID_RDI:
417 		choice = &asid->rdi;
418 		break;
419 	default:
420 		return 0;
421 	}
422 	if (*choice == NULL) {
423 		if ((*choice = ASIdentifierChoice_new()) == NULL)
424 			return 0;
425 		if (((*choice)->u.inherit = ASN1_NULL_new()) == NULL)
426 			return 0;
427 		(*choice)->type = ASIdentifierChoice_inherit;
428 	}
429 	return (*choice)->type == ASIdentifierChoice_inherit;
430 }
431 LCRYPTO_ALIAS(X509v3_asid_add_inherit)
432 
433 /*
434  * Add an ID or range to an ASIdentifierChoice.
435  */
436 int
437 X509v3_asid_add_id_or_range(ASIdentifiers *asid, int which, ASN1_INTEGER *min,
438     ASN1_INTEGER *max)
439 {
440 	ASIdentifierChoice **choice;
441 	ASIdOrRange *aor;
442 	if (asid == NULL)
443 		return 0;
444 	switch (which) {
445 	case V3_ASID_ASNUM:
446 		choice = &asid->asnum;
447 		break;
448 	case V3_ASID_RDI:
449 		choice = &asid->rdi;
450 		break;
451 	default:
452 		return 0;
453 	}
454 	if (*choice != NULL && (*choice)->type == ASIdentifierChoice_inherit)
455 		return 0;
456 	if (*choice == NULL) {
457 		if ((*choice = ASIdentifierChoice_new()) == NULL)
458 			return 0;
459 		(*choice)->u.asIdsOrRanges = sk_ASIdOrRange_new(ASIdOrRange_cmp);
460 		if ((*choice)->u.asIdsOrRanges == NULL)
461 			return 0;
462 		(*choice)->type = ASIdentifierChoice_asIdsOrRanges;
463 	}
464 	if ((aor = ASIdOrRange_new()) == NULL)
465 		return 0;
466 	if (max == NULL) {
467 		aor->type = ASIdOrRange_id;
468 		aor->u.id = min;
469 	} else {
470 		aor->type = ASIdOrRange_range;
471 		if ((aor->u.range = ASRange_new()) == NULL)
472 			goto err;
473 		ASN1_INTEGER_free(aor->u.range->min);
474 		aor->u.range->min = min;
475 		ASN1_INTEGER_free(aor->u.range->max);
476 		aor->u.range->max = max;
477 	}
478 	if (!(sk_ASIdOrRange_push((*choice)->u.asIdsOrRanges, aor)))
479 		goto err;
480 	return 1;
481 
482  err:
483 	ASIdOrRange_free(aor);
484 	return 0;
485 }
486 LCRYPTO_ALIAS(X509v3_asid_add_id_or_range)
487 
488 /*
489  * Extract min and max values from an ASIdOrRange.
490  */
491 static int
492 extract_min_max(ASIdOrRange *aor, ASN1_INTEGER **min, ASN1_INTEGER **max)
493 {
494 	switch (aor->type) {
495 	case ASIdOrRange_id:
496 		*min = aor->u.id;
497 		*max = aor->u.id;
498 		return 1;
499 	case ASIdOrRange_range:
500 		*min = aor->u.range->min;
501 		*max = aor->u.range->max;
502 		return 1;
503 	}
504 
505 	return 0;
506 }
507 
508 /*
509  * Check whether an ASIdentifierChoice is in canonical form.
510  */
511 static int
512 ASIdentifierChoice_is_canonical(ASIdentifierChoice *choice)
513 {
514 	ASN1_INTEGER *a_max_plus_one = NULL;
515 	ASN1_INTEGER *orig;
516 	BIGNUM *bn = NULL;
517 	int i, ret = 0;
518 
519 	/*
520 	 * Empty element or inheritance is canonical.
521 	 */
522 	if (choice == NULL || choice->type == ASIdentifierChoice_inherit)
523 		return 1;
524 
525 	/*
526 	 * If not a list, or if empty list, it's broken.
527 	 */
528 	if (choice->type != ASIdentifierChoice_asIdsOrRanges ||
529 	    sk_ASIdOrRange_num(choice->u.asIdsOrRanges) == 0)
530 		return 0;
531 
532 	/*
533 	 * It's a list, check it.
534 	 */
535 	for (i = 0; i < sk_ASIdOrRange_num(choice->u.asIdsOrRanges) - 1; i++) {
536 		ASIdOrRange *a = sk_ASIdOrRange_value(choice->u.asIdsOrRanges,
537 		    i);
538 		ASIdOrRange *b = sk_ASIdOrRange_value(choice->u.asIdsOrRanges,
539 		    i + 1);
540 		ASN1_INTEGER *a_min = NULL,
541 		*a_max = NULL,
542 		*b_min = NULL,
543 		*b_max =
544 		    NULL;
545 
546 		if (!extract_min_max(a, &a_min, &a_max) ||
547 		    !extract_min_max(b, &b_min, &b_max))
548 			goto done;
549 
550 		/*
551 		 * Punt misordered list, overlapping start, or inverted range.
552 		 */
553 		if (ASN1_INTEGER_cmp(a_min, b_min) >= 0 ||
554 		    ASN1_INTEGER_cmp(a_min, a_max) > 0 ||
555 		    ASN1_INTEGER_cmp(b_min, b_max) > 0)
556 			goto done;
557 
558 		/*
559 		 * Calculate a_max + 1 to check for adjacency.
560 		 */
561 		if ((bn == NULL && (bn = BN_new()) == NULL) ||
562 		    ASN1_INTEGER_to_BN(a_max, bn) == NULL ||
563 		    !BN_add_word(bn, 1)) {
564 			X509V3error(ERR_R_MALLOC_FAILURE);
565 			goto done;
566 		}
567 
568 		if ((a_max_plus_one =
569 		    BN_to_ASN1_INTEGER(bn, orig = a_max_plus_one)) == NULL) {
570 			a_max_plus_one = orig;
571 			X509V3error(ERR_R_MALLOC_FAILURE);
572 			goto done;
573 		}
574 
575 		/*
576 		 * Punt if adjacent or overlapping.
577 		 */
578 		if (ASN1_INTEGER_cmp(a_max_plus_one, b_min) >= 0)
579 			goto done;
580 	}
581 
582 	/*
583 	 * Check for inverted range.
584 	 */
585 	i = sk_ASIdOrRange_num(choice->u.asIdsOrRanges) - 1;
586 	{
587 		ASIdOrRange *a = sk_ASIdOrRange_value(choice->u.asIdsOrRanges,
588 		    i);
589 		ASN1_INTEGER *a_min, *a_max;
590 		if (a != NULL && a->type == ASIdOrRange_range) {
591 			if (!extract_min_max(a, &a_min, &a_max) ||
592 			    ASN1_INTEGER_cmp(a_min, a_max) > 0)
593 				goto done;
594 		}
595 	}
596 
597 	ret = 1;
598 
599  done:
600 	ASN1_INTEGER_free(a_max_plus_one);
601 	BN_free(bn);
602 	return ret;
603 }
604 
605 /*
606  * Check whether an ASIdentifier extension is in canonical form.
607  */
608 int
609 X509v3_asid_is_canonical(ASIdentifiers *asid)
610 {
611 	return (asid == NULL ||
612 	    (ASIdentifierChoice_is_canonical(asid->asnum) &&
613 	     ASIdentifierChoice_is_canonical(asid->rdi)));
614 }
615 LCRYPTO_ALIAS(X509v3_asid_is_canonical)
616 
617 /*
618  * Whack an ASIdentifierChoice into canonical form.
619  */
620 static int
621 ASIdentifierChoice_canonize(ASIdentifierChoice *choice)
622 {
623 	ASN1_INTEGER *a_max_plus_one = NULL;
624 	ASN1_INTEGER *orig;
625 	BIGNUM *bn = NULL;
626 	int i, ret = 0;
627 
628 	/*
629 	 * Nothing to do for empty element or inheritance.
630 	 */
631 	if (choice == NULL || choice->type == ASIdentifierChoice_inherit)
632 		return 1;
633 
634 	/*
635 	 * If not a list, or if empty list, it's broken.
636 	 */
637 	if (choice->type != ASIdentifierChoice_asIdsOrRanges ||
638 	    sk_ASIdOrRange_num(choice->u.asIdsOrRanges) == 0) {
639 		X509V3error(X509V3_R_EXTENSION_VALUE_ERROR);
640 		return 0;
641 	}
642 
643 	/*
644 	 * We have a non-empty list.  Sort it.
645 	 */
646 	sk_ASIdOrRange_sort(choice->u.asIdsOrRanges);
647 
648 	/*
649 	 * Now check for errors and suboptimal encoding, rejecting the
650 	 * former and fixing the latter.
651 	 */
652 	for (i = 0; i < sk_ASIdOrRange_num(choice->u.asIdsOrRanges) - 1; i++) {
653 		ASIdOrRange *a = sk_ASIdOrRange_value(choice->u.asIdsOrRanges,
654 		    i);
655 		ASIdOrRange *b = sk_ASIdOrRange_value(choice->u.asIdsOrRanges,
656 		    i + 1);
657 		ASN1_INTEGER *a_min = NULL,
658 		*a_max = NULL,
659 		*b_min = NULL,
660 		*b_max =
661 		    NULL;
662 
663 		if (!extract_min_max(a, &a_min, &a_max) ||
664 		    !extract_min_max(b, &b_min, &b_max))
665 			goto done;
666 
667 		/*
668 		 * Make sure we're properly sorted (paranoia).
669 		 */
670 		if (ASN1_INTEGER_cmp(a_min, b_min) > 0)
671 			goto done;
672 
673 		/*
674 		 * Punt inverted ranges.
675 		 */
676 		if (ASN1_INTEGER_cmp(a_min, a_max) > 0 ||
677 		    ASN1_INTEGER_cmp(b_min, b_max) > 0)
678 			goto done;
679 
680 		/*
681 		 * Check for overlaps.
682 		 */
683 		if (ASN1_INTEGER_cmp(a_max, b_min) >= 0) {
684 			X509V3error(X509V3_R_EXTENSION_VALUE_ERROR);
685 			goto done;
686 		}
687 
688 		/*
689 		 * Calculate a_max + 1 to check for adjacency.
690 		 */
691 		if ((bn == NULL && (bn = BN_new()) == NULL) ||
692 		    ASN1_INTEGER_to_BN(a_max, bn) == NULL ||
693 		    !BN_add_word(bn, 1)) {
694 			X509V3error(ERR_R_MALLOC_FAILURE);
695 			goto done;
696 		}
697 
698 		if ((a_max_plus_one =
699 		    BN_to_ASN1_INTEGER(bn, orig = a_max_plus_one)) == NULL) {
700 			a_max_plus_one = orig;
701 			X509V3error(ERR_R_MALLOC_FAILURE);
702 			goto done;
703 		}
704 
705 		/*
706 		 * If a and b are adjacent, merge them.
707 		 */
708 		if (ASN1_INTEGER_cmp(a_max_plus_one, b_min) == 0) {
709 			ASRange *r;
710 			switch (a->type) {
711 			case ASIdOrRange_id:
712 				if ((r = calloc(1, sizeof(*r))) == NULL) {
713 					X509V3error(ERR_R_MALLOC_FAILURE);
714 					goto done;
715 				}
716 				r->min = a_min;
717 				r->max = b_max;
718 				a->type = ASIdOrRange_range;
719 				a->u.range = r;
720 				break;
721 			case ASIdOrRange_range:
722 				ASN1_INTEGER_free(a->u.range->max);
723 				a->u.range->max = b_max;
724 				break;
725 			}
726 			switch (b->type) {
727 			case ASIdOrRange_id:
728 				b->u.id = NULL;
729 				break;
730 			case ASIdOrRange_range:
731 				b->u.range->max = NULL;
732 				break;
733 			}
734 			ASIdOrRange_free(b);
735 			(void)sk_ASIdOrRange_delete(choice->u.asIdsOrRanges,
736 			    i + 1);
737 			i--;
738 			continue;
739 		}
740 	}
741 
742 	/*
743 	 * Check for final inverted range.
744 	 */
745 	i = sk_ASIdOrRange_num(choice->u.asIdsOrRanges) - 1;
746 	{
747 		ASIdOrRange *a = sk_ASIdOrRange_value(choice->u.asIdsOrRanges,
748 		    i);
749 		ASN1_INTEGER *a_min, *a_max;
750 		if (a != NULL && a->type == ASIdOrRange_range) {
751 			if (!extract_min_max(a, &a_min, &a_max) ||
752 			    ASN1_INTEGER_cmp(a_min, a_max) > 0)
753 				goto done;
754 		}
755 	}
756 
757 	/* Paranoia */
758 	if (!ASIdentifierChoice_is_canonical(choice))
759 		goto done;
760 
761 	ret = 1;
762 
763  done:
764 	ASN1_INTEGER_free(a_max_plus_one);
765 	BN_free(bn);
766 	return ret;
767 }
768 
769 /*
770  * Whack an ASIdentifier extension into canonical form.
771  */
772 int
773 X509v3_asid_canonize(ASIdentifiers *asid)
774 {
775 	if (asid == NULL)
776 		return 1;
777 
778 	if (!ASIdentifierChoice_canonize(asid->asnum))
779 		return 0;
780 
781 	return ASIdentifierChoice_canonize(asid->rdi);
782 }
783 LCRYPTO_ALIAS(X509v3_asid_canonize)
784 
785 /*
786  * v2i method for an ASIdentifier extension.
787  */
788 static void *
789 v2i_ASIdentifiers(const struct v3_ext_method *method, struct v3_ext_ctx *ctx,
790     STACK_OF(CONF_VALUE)*values)
791 {
792 	ASN1_INTEGER *min = NULL, *max = NULL;
793 	ASIdentifiers *asid = NULL;
794 	int i;
795 
796 	if ((asid = ASIdentifiers_new()) == NULL) {
797 		X509V3error(ERR_R_MALLOC_FAILURE);
798 		return NULL;
799 	}
800 
801 	for (i = 0; i < sk_CONF_VALUE_num(values); i++) {
802 		CONF_VALUE *val = sk_CONF_VALUE_value(values, i);
803 		int i1 = 0, i2 = 0, i3 = 0, is_range = 0, which = 0;
804 
805 		/*
806 		 * Figure out whether this is an AS or an RDI.
807 		 */
808 		if (!name_cmp(val->name, "AS")) {
809 			which = V3_ASID_ASNUM;
810 		} else if (!name_cmp(val->name, "RDI")) {
811 			which = V3_ASID_RDI;
812 		} else {
813 			X509V3error(X509V3_R_EXTENSION_NAME_ERROR);
814 			X509V3_conf_err(val);
815 			goto err;
816 		}
817 
818 		/*
819 		 * Handle inheritance.
820 		 */
821 		if (strcmp(val->value, "inherit") == 0) {
822 			if (X509v3_asid_add_inherit(asid, which))
823 				continue;
824 			X509V3error(X509V3_R_INVALID_INHERITANCE);
825 			X509V3_conf_err(val);
826 			goto err;
827 		}
828 
829 		/*
830 		 * Number, range, or mistake, pick it apart and figure out which
831 		 */
832 		i1 = strspn(val->value, "0123456789");
833 		if (val->value[i1] == '\0') {
834 			is_range = 0;
835 		} else {
836 			is_range = 1;
837 			i2 = i1 + strspn(val->value + i1, " \t");
838 			if (val->value[i2] != '-') {
839 				X509V3error(X509V3_R_INVALID_ASNUMBER);
840 				X509V3_conf_err(val);
841 				goto err;
842 			}
843 			i2++;
844 			i2 = i2 + strspn(val->value + i2, " \t");
845 			i3 = i2 + strspn(val->value + i2, "0123456789");
846 			if (val->value[i3] != '\0') {
847 				X509V3error(X509V3_R_INVALID_ASRANGE);
848 				X509V3_conf_err(val);
849 				goto err;
850 			}
851 		}
852 
853 		/*
854 		 * Syntax is ok, read and add it.
855 		 */
856 		if (!is_range) {
857 			if (!X509V3_get_value_int(val, &min)) {
858 				X509V3error(ERR_R_MALLOC_FAILURE);
859 				goto err;
860 			}
861 		} else {
862 			char *s = strdup(val->value);
863 			if (s == NULL) {
864 				X509V3error(ERR_R_MALLOC_FAILURE);
865 				goto err;
866 			}
867 			s[i1] = '\0';
868 			min = s2i_ASN1_INTEGER(NULL, s);
869 			max = s2i_ASN1_INTEGER(NULL, s + i2);
870 			free(s);
871 			if (min == NULL || max == NULL) {
872 				X509V3error(ERR_R_MALLOC_FAILURE);
873 				goto err;
874 			}
875 			if (ASN1_INTEGER_cmp(min, max) > 0) {
876 				X509V3error(X509V3_R_EXTENSION_VALUE_ERROR);
877 				goto err;
878 			}
879 		}
880 		if (!X509v3_asid_add_id_or_range(asid, which, min, max)) {
881 			X509V3error(ERR_R_MALLOC_FAILURE);
882 			goto err;
883 		}
884 		min = max = NULL;
885 	}
886 
887 	/*
888 	 * Canonize the result, then we're done.
889 	 */
890 	if (!X509v3_asid_canonize(asid))
891 		goto err;
892 	return asid;
893 
894  err:
895 	ASIdentifiers_free(asid);
896 	ASN1_INTEGER_free(min);
897 	ASN1_INTEGER_free(max);
898 	return NULL;
899 }
900 
901 /*
902  * OpenSSL dispatch.
903  */
904 const X509V3_EXT_METHOD v3_asid = {
905 	.ext_nid = NID_sbgp_autonomousSysNum,
906 	.ext_flags = 0,
907 	.it = &ASIdentifiers_it,
908 	.ext_new = NULL,
909 	.ext_free = NULL,
910 	.d2i = NULL,
911 	.i2d = NULL,
912 	.i2s = NULL,
913 	.s2i = NULL,
914 	.i2v = NULL,
915 	.v2i = v2i_ASIdentifiers,
916 	.i2r = i2r_ASIdentifiers,
917 	.r2i = NULL,
918 	.usr_data = NULL,
919 };
920 
921 /*
922  * Figure out whether extension uses inheritance.
923  */
924 int
925 X509v3_asid_inherits(ASIdentifiers *asid)
926 {
927 	if (asid == NULL)
928 		return 0;
929 
930 	if (asid->asnum != NULL) {
931 		if (asid->asnum->type == ASIdentifierChoice_inherit)
932 			return 1;
933 	}
934 
935 	if (asid->rdi != NULL) {
936 		if (asid->rdi->type == ASIdentifierChoice_inherit)
937 			return 1;
938 	}
939 
940 	return 0;
941 }
942 LCRYPTO_ALIAS(X509v3_asid_inherits)
943 
944 /*
945  * Figure out whether parent contains child.
946  */
947 static int
948 asid_contains(ASIdOrRanges *parent, ASIdOrRanges *child)
949 {
950 	ASN1_INTEGER *p_min = NULL, *p_max = NULL, *c_min = NULL, *c_max = NULL;
951 	int p, c;
952 
953 	if (child == NULL || parent == child)
954 		return 1;
955 
956 	if (parent == NULL)
957 		return 0;
958 
959 	p = 0;
960 	for (c = 0; c < sk_ASIdOrRange_num(child); c++) {
961 		if (!extract_min_max(sk_ASIdOrRange_value(child, c), &c_min,
962 		    &c_max))
963 			return 0;
964 		for (;; p++) {
965 			if (p >= sk_ASIdOrRange_num(parent))
966 				return 0;
967 			if (!extract_min_max(sk_ASIdOrRange_value(parent, p),
968 			    &p_min, &p_max))
969 				return 0;
970 			if (ASN1_INTEGER_cmp(p_max, c_max) < 0)
971 				continue;
972 			if (ASN1_INTEGER_cmp(p_min, c_min) > 0)
973 				return 0;
974 			break;
975 		}
976 	}
977 
978 	return 1;
979 }
980 
981 /*
982  * Test whether child is a subset of parent.
983  */
984 int
985 X509v3_asid_subset(ASIdentifiers *child, ASIdentifiers *parent)
986 {
987 	if (child == NULL || child == parent)
988 		return 1;
989 
990 	if (parent == NULL)
991 		return 0;
992 
993 	if (X509v3_asid_inherits(child) || X509v3_asid_inherits(parent))
994 		return 0;
995 
996 	if (child->asnum != NULL) {
997 		if (parent->asnum == NULL)
998 			return 0;
999 
1000 		if (!asid_contains(parent->asnum->u.asIdsOrRanges,
1001 		    child->asnum->u.asIdsOrRanges))
1002 			return 0;
1003 	}
1004 
1005 	if (child->rdi != NULL) {
1006 		if (parent->rdi == NULL)
1007 			return 0;
1008 
1009 		if (!asid_contains(parent->rdi->u.asIdsOrRanges,
1010 		    child->rdi->u.asIdsOrRanges))
1011 			return 0;
1012 	}
1013 
1014 	return 1;
1015 }
1016 LCRYPTO_ALIAS(X509v3_asid_subset)
1017 
1018 /*
1019  * Validation error handling via callback.
1020  */
1021 #define validation_err(_err_)           \
1022   do {                                  \
1023     if (ctx != NULL) {                  \
1024       ctx->error = _err_;               \
1025       ctx->error_depth = i;             \
1026       ctx->current_cert = x;            \
1027       ret = ctx->verify_cb(0, ctx);     \
1028     } else {                            \
1029       ret = 0;                          \
1030     }                                   \
1031     if (!ret)                           \
1032       goto done;                        \
1033   } while (0)
1034 
1035 /*
1036  * Core code for RFC 3779 3.3 path validation.
1037  */
1038 static int
1039 asid_validate_path_internal(X509_STORE_CTX *ctx, STACK_OF(X509) *chain,
1040     ASIdentifiers *ext)
1041 {
1042 	ASIdOrRanges *child_as = NULL, *child_rdi = NULL;
1043 	int i, ret = 1, inherit_as = 0, inherit_rdi = 0;
1044 	X509 *x;
1045 
1046 	/* We need a non-empty chain to test against. */
1047 	if (sk_X509_num(chain) <= 0)
1048 		goto err;
1049 	/* We need either a store ctx or an extension to work with. */
1050 	if (ctx == NULL && ext == NULL)
1051 		goto err;
1052 	/* If there is a store ctx, it needs a verify_cb. */
1053 	if (ctx != NULL && ctx->verify_cb == NULL)
1054 		goto err;
1055 
1056 	/*
1057 	 * Figure out where to start. If we don't have an extension to check,
1058 	 * (either extracted from the leaf or passed by the caller), we're done.
1059 	 * Otherwise, check canonical form and set up for walking up the chain.
1060 	 */
1061 	if (ext != NULL) {
1062 		i = -1;
1063 		x = NULL;
1064 		if (!X509v3_asid_is_canonical(ext))
1065 			validation_err(X509_V_ERR_INVALID_EXTENSION);
1066 	} else {
1067 		i = 0;
1068 		x = sk_X509_value(chain, i);
1069 		if ((X509_get_extension_flags(x) & EXFLAG_INVALID) != 0)
1070 			goto done;
1071 		if ((ext = x->rfc3779_asid) == NULL)
1072 			goto done;
1073 	}
1074 	if (ext->asnum != NULL) {
1075 		switch (ext->asnum->type) {
1076 		case ASIdentifierChoice_inherit:
1077 			inherit_as = 1;
1078 			break;
1079 		case ASIdentifierChoice_asIdsOrRanges:
1080 			child_as = ext->asnum->u.asIdsOrRanges;
1081 			break;
1082 		}
1083 	}
1084 	if (ext->rdi != NULL) {
1085 		switch (ext->rdi->type) {
1086 		case ASIdentifierChoice_inherit:
1087 			inherit_rdi = 1;
1088 			break;
1089 		case ASIdentifierChoice_asIdsOrRanges:
1090 			child_rdi = ext->rdi->u.asIdsOrRanges;
1091 			break;
1092 		}
1093 	}
1094 
1095 	/*
1096 	 * Now walk up the chain.  Extensions must be in canonical form, no
1097 	 * cert may list resources that its parent doesn't list.
1098 	 */
1099 	for (i++; i < sk_X509_num(chain); i++) {
1100 		x = sk_X509_value(chain, i);
1101 
1102 		if ((X509_get_extension_flags(x) & EXFLAG_INVALID) != 0)
1103 			validation_err(X509_V_ERR_INVALID_EXTENSION);
1104 		if (x->rfc3779_asid == NULL) {
1105 			if (child_as != NULL || child_rdi != NULL)
1106 				validation_err(X509_V_ERR_UNNESTED_RESOURCE);
1107 			continue;
1108 		}
1109 		if (x->rfc3779_asid->asnum == NULL && child_as != NULL) {
1110 			validation_err(X509_V_ERR_UNNESTED_RESOURCE);
1111 			child_as = NULL;
1112 			inherit_as = 0;
1113 		}
1114 		if (x->rfc3779_asid->asnum != NULL &&
1115 		    x->rfc3779_asid->asnum->type ==
1116 		    ASIdentifierChoice_asIdsOrRanges) {
1117 			if (inherit_as ||
1118 			    asid_contains(x->rfc3779_asid->asnum->u.asIdsOrRanges,
1119 			    child_as)) {
1120 				child_as = x->rfc3779_asid->asnum->u.asIdsOrRanges;
1121 				inherit_as = 0;
1122 			} else {
1123 				validation_err(X509_V_ERR_UNNESTED_RESOURCE);
1124 			}
1125 		}
1126 		if (x->rfc3779_asid->rdi == NULL && child_rdi != NULL) {
1127 			validation_err(X509_V_ERR_UNNESTED_RESOURCE);
1128 			child_rdi = NULL;
1129 			inherit_rdi = 0;
1130 		}
1131 		if (x->rfc3779_asid->rdi != NULL &&
1132 		    x->rfc3779_asid->rdi->type == ASIdentifierChoice_asIdsOrRanges) {
1133 			if (inherit_rdi ||
1134 			    asid_contains(x->rfc3779_asid->rdi->u.asIdsOrRanges,
1135 			    child_rdi)) {
1136 				child_rdi = x->rfc3779_asid->rdi->u.asIdsOrRanges;
1137 				inherit_rdi = 0;
1138 			} else {
1139 				validation_err(X509_V_ERR_UNNESTED_RESOURCE);
1140 			}
1141 		}
1142 	}
1143 
1144 	/*
1145 	 * Trust anchor can't inherit.
1146 	 */
1147 
1148 	if (x == NULL)
1149 		goto err;
1150 
1151 	if (x->rfc3779_asid != NULL) {
1152 		if (x->rfc3779_asid->asnum != NULL &&
1153 		    x->rfc3779_asid->asnum->type == ASIdentifierChoice_inherit)
1154 			validation_err(X509_V_ERR_UNNESTED_RESOURCE);
1155 		if (x->rfc3779_asid->rdi != NULL &&
1156 		    x->rfc3779_asid->rdi->type == ASIdentifierChoice_inherit)
1157 			validation_err(X509_V_ERR_UNNESTED_RESOURCE);
1158 	}
1159 
1160  done:
1161 	return ret;
1162 
1163  err:
1164 	if (ctx != NULL)
1165 		ctx->error = X509_V_ERR_UNSPECIFIED;
1166 
1167 	return 0;
1168 }
1169 
1170 #undef validation_err
1171 
1172 /*
1173  * RFC 3779 3.3 path validation -- called from X509_verify_cert().
1174  */
1175 int
1176 X509v3_asid_validate_path(X509_STORE_CTX *ctx)
1177 {
1178 	if (sk_X509_num(ctx->chain) <= 0 || ctx->verify_cb == NULL) {
1179 		ctx->error = X509_V_ERR_UNSPECIFIED;
1180 		return 0;
1181 	}
1182 	return asid_validate_path_internal(ctx, ctx->chain, NULL);
1183 }
1184 LCRYPTO_ALIAS(X509v3_asid_validate_path)
1185 
1186 /*
1187  * RFC 3779 3.3 path validation of an extension.
1188  * Test whether chain covers extension.
1189  */
1190 int
1191 X509v3_asid_validate_resource_set(STACK_OF(X509) *chain, ASIdentifiers *ext,
1192     int allow_inheritance)
1193 {
1194 	if (ext == NULL)
1195 		return 1;
1196 	if (sk_X509_num(chain) <= 0)
1197 		return 0;
1198 	if (!allow_inheritance && X509v3_asid_inherits(ext))
1199 		return 0;
1200 	return asid_validate_path_internal(NULL, chain, ext);
1201 }
1202 LCRYPTO_ALIAS(X509v3_asid_validate_resource_set)
1203 
1204 #endif                          /* OPENSSL_NO_RFC3779 */
1205