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