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