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