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