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