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