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