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