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