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