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