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