summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/x509v3
diff options
context:
space:
mode:
authorcvs2svn <admin@example.com>2011-11-03 02:32:25 +0000
committercvs2svn <admin@example.com>2011-11-03 02:32:25 +0000
commit279182cf9975981f7d3c660b5cafa392216fcd94 (patch)
tree3ee21f04641147b64fb808a991bfcf16598fbe1f /src/lib/libcrypto/x509v3
parentbd081fc6d9aca84ff3cbde2c626c28668f73d967 (diff)
downloadopenbsd-openssl_1_0_0_e.tar.gz
openbsd-openssl_1_0_0_e.tar.bz2
openbsd-openssl_1_0_0_e.zip
This commit was manufactured by cvs2git to create tag 'openssl_1_0_0_e'.openssl_1_0_0_e
Diffstat (limited to 'src/lib/libcrypto/x509v3')
-rw-r--r--src/lib/libcrypto/x509v3/v3_addr.c1293
-rw-r--r--src/lib/libcrypto/x509v3/v3_asid.c843
2 files changed, 2136 insertions, 0 deletions
diff --git a/src/lib/libcrypto/x509v3/v3_addr.c b/src/lib/libcrypto/x509v3/v3_addr.c
new file mode 100644
index 0000000000..0d70e8696d
--- /dev/null
+++ b/src/lib/libcrypto/x509v3/v3_addr.c
@@ -0,0 +1,1293 @@
1/*
2 * Contributed to the OpenSSL Project by the American Registry for
3 * Internet Numbers ("ARIN").
4 */
5/* ====================================================================
6 * Copyright (c) 2006 The OpenSSL Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
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 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
56 */
57
58/*
59 * Implementation of RFC 3779 section 2.2.
60 */
61
62#include <stdio.h>
63#include <stdlib.h>
64
65#include "cryptlib.h"
66#include <openssl/conf.h>
67#include <openssl/asn1.h>
68#include <openssl/asn1t.h>
69#include <openssl/buffer.h>
70#include <openssl/x509v3.h>
71
72#ifndef OPENSSL_NO_RFC3779
73
74/*
75 * OpenSSL ASN.1 template translation of RFC 3779 2.2.3.
76 */
77
78ASN1_SEQUENCE(IPAddressRange) = {
79 ASN1_SIMPLE(IPAddressRange, min, ASN1_BIT_STRING),
80 ASN1_SIMPLE(IPAddressRange, max, ASN1_BIT_STRING)
81} ASN1_SEQUENCE_END(IPAddressRange)
82
83ASN1_CHOICE(IPAddressOrRange) = {
84 ASN1_SIMPLE(IPAddressOrRange, u.addressPrefix, ASN1_BIT_STRING),
85 ASN1_SIMPLE(IPAddressOrRange, u.addressRange, IPAddressRange)
86} ASN1_CHOICE_END(IPAddressOrRange)
87
88ASN1_CHOICE(IPAddressChoice) = {
89 ASN1_SIMPLE(IPAddressChoice, u.inherit, ASN1_NULL),
90 ASN1_SEQUENCE_OF(IPAddressChoice, u.addressesOrRanges, IPAddressOrRange)
91} ASN1_CHOICE_END(IPAddressChoice)
92
93ASN1_SEQUENCE(IPAddressFamily) = {
94 ASN1_SIMPLE(IPAddressFamily, addressFamily, ASN1_OCTET_STRING),
95 ASN1_SIMPLE(IPAddressFamily, ipAddressChoice, IPAddressChoice)
96} ASN1_SEQUENCE_END(IPAddressFamily)
97
98ASN1_ITEM_TEMPLATE(IPAddrBlocks) =
99 ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0,
100 IPAddrBlocks, IPAddressFamily)
101ASN1_ITEM_TEMPLATE_END(IPAddrBlocks)
102
103IMPLEMENT_ASN1_FUNCTIONS(IPAddressRange)
104IMPLEMENT_ASN1_FUNCTIONS(IPAddressOrRange)
105IMPLEMENT_ASN1_FUNCTIONS(IPAddressChoice)
106IMPLEMENT_ASN1_FUNCTIONS(IPAddressFamily)
107
108/*
109 * How much buffer space do we need for a raw address?
110 */
111#define ADDR_RAW_BUF_LEN 16
112
113/*
114 * What's the address length associated with this AFI?
115 */
116static int length_from_afi(const unsigned afi)
117{
118 switch (afi) {
119 case IANA_AFI_IPV4:
120 return 4;
121 case IANA_AFI_IPV6:
122 return 16;
123 default:
124 return 0;
125 }
126}
127
128/*
129 * Extract the AFI from an IPAddressFamily.
130 */
131unsigned int v3_addr_get_afi(const IPAddressFamily *f)
132{
133 return ((f != NULL &&
134 f->addressFamily != NULL &&
135 f->addressFamily->data != NULL)
136 ? ((f->addressFamily->data[0] << 8) |
137 (f->addressFamily->data[1]))
138 : 0);
139}
140
141/*
142 * Expand the bitstring form of an address into a raw byte array.
143 * At the moment this is coded for simplicity, not speed.
144 */
145static void addr_expand(unsigned char *addr,
146 const ASN1_BIT_STRING *bs,
147 const int length,
148 const unsigned char fill)
149{
150 OPENSSL_assert(bs->length >= 0 && bs->length <= length);
151 if (bs->length > 0) {
152 memcpy(addr, bs->data, bs->length);
153 if ((bs->flags & 7) != 0) {
154 unsigned char mask = 0xFF >> (8 - (bs->flags & 7));
155 if (fill == 0)
156 addr[bs->length - 1] &= ~mask;
157 else
158 addr[bs->length - 1] |= mask;
159 }
160 }
161 memset(addr + bs->length, fill, length - bs->length);
162}
163
164/*
165 * Extract the prefix length from a bitstring.
166 */
167#define addr_prefixlen(bs) ((int) ((bs)->length * 8 - ((bs)->flags & 7)))
168
169/*
170 * i2r handler for one address bitstring.
171 */
172static int i2r_address(BIO *out,
173 const unsigned afi,
174 const unsigned char fill,
175 const ASN1_BIT_STRING *bs)
176{
177 unsigned char addr[ADDR_RAW_BUF_LEN];
178 int i, n;
179
180 if (bs->length < 0)
181 return 0;
182 switch (afi) {
183 case IANA_AFI_IPV4:
184 if (bs->length > 4)
185 return 0;
186 addr_expand(addr, bs, 4, fill);
187 BIO_printf(out, "%d.%d.%d.%d", addr[0], addr[1], addr[2], addr[3]);
188 break;
189 case IANA_AFI_IPV6:
190 if (bs->length > 16)
191 return 0;
192 addr_expand(addr, bs, 16, fill);
193 for (n = 16; n > 1 && addr[n-1] == 0x00 && addr[n-2] == 0x00; n -= 2)
194 ;
195 for (i = 0; i < n; i += 2)
196 BIO_printf(out, "%x%s", (addr[i] << 8) | addr[i+1], (i < 14 ? ":" : ""));
197 if (i < 16)
198 BIO_puts(out, ":");
199 if (i == 0)
200 BIO_puts(out, ":");
201 break;
202 default:
203 for (i = 0; i < bs->length; i++)
204 BIO_printf(out, "%s%02x", (i > 0 ? ":" : ""), bs->data[i]);
205 BIO_printf(out, "[%d]", (int) (bs->flags & 7));
206 break;
207 }
208 return 1;
209}
210
211/*
212 * i2r handler for a sequence of addresses and ranges.
213 */
214static int i2r_IPAddressOrRanges(BIO *out,
215 const int indent,
216 const IPAddressOrRanges *aors,
217 const unsigned afi)
218{
219 int i;
220 for (i = 0; i < sk_IPAddressOrRange_num(aors); i++) {
221 const IPAddressOrRange *aor = sk_IPAddressOrRange_value(aors, i);
222 BIO_printf(out, "%*s", indent, "");
223 switch (aor->type) {
224 case IPAddressOrRange_addressPrefix:
225 if (!i2r_address(out, afi, 0x00, aor->u.addressPrefix))
226 return 0;
227 BIO_printf(out, "/%d\n", addr_prefixlen(aor->u.addressPrefix));
228 continue;
229 case IPAddressOrRange_addressRange:
230 if (!i2r_address(out, afi, 0x00, aor->u.addressRange->min))
231 return 0;
232 BIO_puts(out, "-");
233 if (!i2r_address(out, afi, 0xFF, aor->u.addressRange->max))
234 return 0;
235 BIO_puts(out, "\n");
236 continue;
237 }
238 }
239 return 1;
240}
241
242/*
243 * i2r handler for an IPAddrBlocks extension.
244 */
245static int i2r_IPAddrBlocks(const X509V3_EXT_METHOD *method,
246 void *ext,
247 BIO *out,
248 int indent)
249{
250 const IPAddrBlocks *addr = ext;
251 int i;
252 for (i = 0; i < sk_IPAddressFamily_num(addr); i++) {
253 IPAddressFamily *f = sk_IPAddressFamily_value(addr, i);
254 const unsigned int afi = v3_addr_get_afi(f);
255 switch (afi) {
256 case IANA_AFI_IPV4:
257 BIO_printf(out, "%*sIPv4", indent, "");
258 break;
259 case IANA_AFI_IPV6:
260 BIO_printf(out, "%*sIPv6", indent, "");
261 break;
262 default:
263 BIO_printf(out, "%*sUnknown AFI %u", indent, "", afi);
264 break;
265 }
266 if (f->addressFamily->length > 2) {
267 switch (f->addressFamily->data[2]) {
268 case 1:
269 BIO_puts(out, " (Unicast)");
270 break;
271 case 2:
272 BIO_puts(out, " (Multicast)");
273 break;
274 case 3:
275 BIO_puts(out, " (Unicast/Multicast)");
276 break;
277 case 4:
278 BIO_puts(out, " (MPLS)");
279 break;
280 case 64:
281 BIO_puts(out, " (Tunnel)");
282 break;
283 case 65:
284 BIO_puts(out, " (VPLS)");
285 break;
286 case 66:
287 BIO_puts(out, " (BGP MDT)");
288 break;
289 case 128:
290 BIO_puts(out, " (MPLS-labeled VPN)");
291 break;
292 default:
293 BIO_printf(out, " (Unknown SAFI %u)",
294 (unsigned) f->addressFamily->data[2]);
295 break;
296 }
297 }
298 switch (f->ipAddressChoice->type) {
299 case IPAddressChoice_inherit:
300 BIO_puts(out, ": inherit\n");
301 break;
302 case IPAddressChoice_addressesOrRanges:
303 BIO_puts(out, ":\n");
304 if (!i2r_IPAddressOrRanges(out,
305 indent + 2,
306 f->ipAddressChoice->u.addressesOrRanges,
307 afi))
308 return 0;
309 break;
310 }
311 }
312 return 1;
313}
314
315/*
316 * Sort comparison function for a sequence of IPAddressOrRange
317 * elements.
318 */
319static int IPAddressOrRange_cmp(const IPAddressOrRange *a,
320 const IPAddressOrRange *b,
321 const int length)
322{
323 unsigned char addr_a[ADDR_RAW_BUF_LEN], addr_b[ADDR_RAW_BUF_LEN];
324 int prefixlen_a = 0, prefixlen_b = 0;
325 int r;
326
327 switch (a->type) {
328 case IPAddressOrRange_addressPrefix:
329 addr_expand(addr_a, a->u.addressPrefix, length, 0x00);
330 prefixlen_a = addr_prefixlen(a->u.addressPrefix);
331 break;
332 case IPAddressOrRange_addressRange:
333 addr_expand(addr_a, a->u.addressRange->min, length, 0x00);
334 prefixlen_a = length * 8;
335 break;
336 }
337
338 switch (b->type) {
339 case IPAddressOrRange_addressPrefix:
340 addr_expand(addr_b, b->u.addressPrefix, length, 0x00);
341 prefixlen_b = addr_prefixlen(b->u.addressPrefix);
342 break;
343 case IPAddressOrRange_addressRange:
344 addr_expand(addr_b, b->u.addressRange->min, length, 0x00);
345 prefixlen_b = length * 8;
346 break;
347 }
348
349 if ((r = memcmp(addr_a, addr_b, length)) != 0)
350 return r;
351 else
352 return prefixlen_a - prefixlen_b;
353}
354
355/*
356 * IPv4-specific closure over IPAddressOrRange_cmp, since sk_sort()
357 * comparision routines are only allowed two arguments.
358 */
359static int v4IPAddressOrRange_cmp(const IPAddressOrRange * const *a,
360 const IPAddressOrRange * const *b)
361{
362 return IPAddressOrRange_cmp(*a, *b, 4);
363}
364
365/*
366 * IPv6-specific closure over IPAddressOrRange_cmp, since sk_sort()
367 * comparision routines are only allowed two arguments.
368 */
369static int v6IPAddressOrRange_cmp(const IPAddressOrRange * const *a,
370 const IPAddressOrRange * const *b)
371{
372 return IPAddressOrRange_cmp(*a, *b, 16);
373}
374
375/*
376 * Calculate whether a range collapses to a prefix.
377 * See last paragraph of RFC 3779 2.2.3.7.
378 */
379static int range_should_be_prefix(const unsigned char *min,
380 const unsigned char *max,
381 const int length)
382{
383 unsigned char mask;
384 int i, j;
385
386 for (i = 0; i < length && min[i] == max[i]; i++)
387 ;
388 for (j = length - 1; j >= 0 && min[j] == 0x00 && max[j] == 0xFF; j--)
389 ;
390 if (i < j)
391 return -1;
392 if (i > j)
393 return i * 8;
394 mask = min[i] ^ max[i];
395 switch (mask) {
396 case 0x01: j = 7; break;
397 case 0x03: j = 6; break;
398 case 0x07: j = 5; break;
399 case 0x0F: j = 4; break;
400 case 0x1F: j = 3; break;
401 case 0x3F: j = 2; break;
402 case 0x7F: j = 1; break;
403 default: return -1;
404 }
405 if ((min[i] & mask) != 0 || (max[i] & mask) != mask)
406 return -1;
407 else
408 return i * 8 + j;
409}
410
411/*
412 * Construct a prefix.
413 */
414static int make_addressPrefix(IPAddressOrRange **result,
415 unsigned char *addr,
416 const int prefixlen)
417{
418 int bytelen = (prefixlen + 7) / 8, bitlen = prefixlen % 8;
419 IPAddressOrRange *aor = IPAddressOrRange_new();
420
421 if (aor == NULL)
422 return 0;
423 aor->type = IPAddressOrRange_addressPrefix;
424 if (aor->u.addressPrefix == NULL &&
425 (aor->u.addressPrefix = ASN1_BIT_STRING_new()) == NULL)
426 goto err;
427 if (!ASN1_BIT_STRING_set(aor->u.addressPrefix, addr, bytelen))
428 goto err;
429 aor->u.addressPrefix->flags &= ~7;
430 aor->u.addressPrefix->flags |= ASN1_STRING_FLAG_BITS_LEFT;
431 if (bitlen > 0) {
432 aor->u.addressPrefix->data[bytelen - 1] &= ~(0xFF >> bitlen);
433 aor->u.addressPrefix->flags |= 8 - bitlen;
434 }
435
436 *result = aor;
437 return 1;
438
439 err:
440 IPAddressOrRange_free(aor);
441 return 0;
442}
443
444/*
445 * Construct a range. If it can be expressed as a prefix,
446 * return a prefix instead. Doing this here simplifies
447 * the rest of the code considerably.
448 */
449static int make_addressRange(IPAddressOrRange **result,
450 unsigned char *min,
451 unsigned char *max,
452 const int length)
453{
454 IPAddressOrRange *aor;
455 int i, prefixlen;
456
457 if ((prefixlen = range_should_be_prefix(min, max, length)) >= 0)
458 return make_addressPrefix(result, min, prefixlen);
459
460 if ((aor = IPAddressOrRange_new()) == NULL)
461 return 0;
462 aor->type = IPAddressOrRange_addressRange;
463 OPENSSL_assert(aor->u.addressRange == NULL);
464 if ((aor->u.addressRange = IPAddressRange_new()) == NULL)
465 goto err;
466 if (aor->u.addressRange->min == NULL &&
467 (aor->u.addressRange->min = ASN1_BIT_STRING_new()) == NULL)
468 goto err;
469 if (aor->u.addressRange->max == NULL &&
470 (aor->u.addressRange->max = ASN1_BIT_STRING_new()) == NULL)
471 goto err;
472
473 for (i = length; i > 0 && min[i - 1] == 0x00; --i)
474 ;
475 if (!ASN1_BIT_STRING_set(aor->u.addressRange->min, min, i))
476 goto err;
477 aor->u.addressRange->min->flags &= ~7;
478 aor->u.addressRange->min->flags |= ASN1_STRING_FLAG_BITS_LEFT;
479 if (i > 0) {
480 unsigned char b = min[i - 1];
481 int j = 1;
482 while ((b & (0xFFU >> j)) != 0)
483 ++j;
484 aor->u.addressRange->min->flags |= 8 - j;
485 }
486
487 for (i = length; i > 0 && max[i - 1] == 0xFF; --i)
488 ;
489 if (!ASN1_BIT_STRING_set(aor->u.addressRange->max, max, i))
490 goto err;
491 aor->u.addressRange->max->flags &= ~7;
492 aor->u.addressRange->max->flags |= ASN1_STRING_FLAG_BITS_LEFT;
493 if (i > 0) {
494 unsigned char b = max[i - 1];
495 int j = 1;
496 while ((b & (0xFFU >> j)) != (0xFFU >> j))
497 ++j;
498 aor->u.addressRange->max->flags |= 8 - j;
499 }
500
501 *result = aor;
502 return 1;
503
504 err:
505 IPAddressOrRange_free(aor);
506 return 0;
507}
508
509/*
510 * Construct a new address family or find an existing one.
511 */
512static IPAddressFamily *make_IPAddressFamily(IPAddrBlocks *addr,
513 const unsigned afi,
514 const unsigned *safi)
515{
516 IPAddressFamily *f;
517 unsigned char key[3];
518 unsigned keylen;
519 int i;
520
521 key[0] = (afi >> 8) & 0xFF;
522 key[1] = afi & 0xFF;
523 if (safi != NULL) {
524 key[2] = *safi & 0xFF;
525 keylen = 3;
526 } else {
527 keylen = 2;
528 }
529
530 for (i = 0; i < sk_IPAddressFamily_num(addr); i++) {
531 f = sk_IPAddressFamily_value(addr, i);
532 OPENSSL_assert(f->addressFamily->data != NULL);
533 if (f->addressFamily->length == keylen &&
534 !memcmp(f->addressFamily->data, key, keylen))
535 return f;
536 }
537
538 if ((f = IPAddressFamily_new()) == NULL)
539 goto err;
540 if (f->ipAddressChoice == NULL &&
541 (f->ipAddressChoice = IPAddressChoice_new()) == NULL)
542 goto err;
543 if (f->addressFamily == NULL &&
544 (f->addressFamily = ASN1_OCTET_STRING_new()) == NULL)
545 goto err;
546 if (!ASN1_OCTET_STRING_set(f->addressFamily, key, keylen))
547 goto err;
548 if (!sk_IPAddressFamily_push(addr, f))
549 goto err;
550
551 return f;
552
553 err:
554 IPAddressFamily_free(f);
555 return NULL;
556}
557
558/*
559 * Add an inheritance element.
560 */
561int v3_addr_add_inherit(IPAddrBlocks *addr,
562 const unsigned afi,
563 const unsigned *safi)
564{
565 IPAddressFamily *f = make_IPAddressFamily(addr, afi, safi);
566 if (f == NULL ||
567 f->ipAddressChoice == NULL ||
568 (f->ipAddressChoice->type == IPAddressChoice_addressesOrRanges &&
569 f->ipAddressChoice->u.addressesOrRanges != NULL))
570 return 0;
571 if (f->ipAddressChoice->type == IPAddressChoice_inherit &&
572 f->ipAddressChoice->u.inherit != NULL)
573 return 1;
574 if (f->ipAddressChoice->u.inherit == NULL &&
575 (f->ipAddressChoice->u.inherit = ASN1_NULL_new()) == NULL)
576 return 0;
577 f->ipAddressChoice->type = IPAddressChoice_inherit;
578 return 1;
579}
580
581/*
582 * Construct an IPAddressOrRange sequence, or return an existing one.
583 */
584static IPAddressOrRanges *make_prefix_or_range(IPAddrBlocks *addr,
585 const unsigned afi,
586 const unsigned *safi)
587{
588 IPAddressFamily *f = make_IPAddressFamily(addr, afi, safi);
589 IPAddressOrRanges *aors = NULL;
590
591 if (f == NULL ||
592 f->ipAddressChoice == NULL ||
593 (f->ipAddressChoice->type == IPAddressChoice_inherit &&
594 f->ipAddressChoice->u.inherit != NULL))
595 return NULL;
596 if (f->ipAddressChoice->type == IPAddressChoice_addressesOrRanges)
597 aors = f->ipAddressChoice->u.addressesOrRanges;
598 if (aors != NULL)
599 return aors;
600 if ((aors = sk_IPAddressOrRange_new_null()) == NULL)
601 return NULL;
602 switch (afi) {
603 case IANA_AFI_IPV4:
604 sk_IPAddressOrRange_set_cmp_func(aors, v4IPAddressOrRange_cmp);
605 break;
606 case IANA_AFI_IPV6:
607 sk_IPAddressOrRange_set_cmp_func(aors, v6IPAddressOrRange_cmp);
608 break;
609 }
610 f->ipAddressChoice->type = IPAddressChoice_addressesOrRanges;
611 f->ipAddressChoice->u.addressesOrRanges = aors;
612 return aors;
613}
614
615/*
616 * Add a prefix.
617 */
618int v3_addr_add_prefix(IPAddrBlocks *addr,
619 const unsigned afi,
620 const unsigned *safi,
621 unsigned char *a,
622 const int prefixlen)
623{
624 IPAddressOrRanges *aors = make_prefix_or_range(addr, afi, safi);
625 IPAddressOrRange *aor;
626 if (aors == NULL || !make_addressPrefix(&aor, a, prefixlen))
627 return 0;
628 if (sk_IPAddressOrRange_push(aors, aor))
629 return 1;
630 IPAddressOrRange_free(aor);
631 return 0;
632}
633
634/*
635 * Add a range.
636 */
637int v3_addr_add_range(IPAddrBlocks *addr,
638 const unsigned afi,
639 const unsigned *safi,
640 unsigned char *min,
641 unsigned char *max)
642{
643 IPAddressOrRanges *aors = make_prefix_or_range(addr, afi, safi);
644 IPAddressOrRange *aor;
645 int length = length_from_afi(afi);
646 if (aors == NULL)
647 return 0;
648 if (!make_addressRange(&aor, min, max, length))
649 return 0;
650 if (sk_IPAddressOrRange_push(aors, aor))
651 return 1;
652 IPAddressOrRange_free(aor);
653 return 0;
654}
655
656/*
657 * Extract min and max values from an IPAddressOrRange.
658 */
659static void extract_min_max(IPAddressOrRange *aor,
660 unsigned char *min,
661 unsigned char *max,
662 int length)
663{
664 OPENSSL_assert(aor != NULL && min != NULL && max != NULL);
665 switch (aor->type) {
666 case IPAddressOrRange_addressPrefix:
667 addr_expand(min, aor->u.addressPrefix, length, 0x00);
668 addr_expand(max, aor->u.addressPrefix, length, 0xFF);
669 return;
670 case IPAddressOrRange_addressRange:
671 addr_expand(min, aor->u.addressRange->min, length, 0x00);
672 addr_expand(max, aor->u.addressRange->max, length, 0xFF);
673 return;
674 }
675}
676
677/*
678 * Public wrapper for extract_min_max().
679 */
680int v3_addr_get_range(IPAddressOrRange *aor,
681 const unsigned afi,
682 unsigned char *min,
683 unsigned char *max,
684 const int length)
685{
686 int afi_length = length_from_afi(afi);
687 if (aor == NULL || min == NULL || max == NULL ||
688 afi_length == 0 || length < afi_length ||
689 (aor->type != IPAddressOrRange_addressPrefix &&
690 aor->type != IPAddressOrRange_addressRange))
691 return 0;
692 extract_min_max(aor, min, max, afi_length);
693 return afi_length;
694}
695
696/*
697 * Sort comparision function for a sequence of IPAddressFamily.
698 *
699 * The last paragraph of RFC 3779 2.2.3.3 is slightly ambiguous about
700 * the ordering: I can read it as meaning that IPv6 without a SAFI
701 * comes before IPv4 with a SAFI, which seems pretty weird. The
702 * examples in appendix B suggest that the author intended the
703 * null-SAFI rule to apply only within a single AFI, which is what I
704 * would have expected and is what the following code implements.
705 */
706static int IPAddressFamily_cmp(const IPAddressFamily * const *a_,
707 const IPAddressFamily * const *b_)
708{
709 const ASN1_OCTET_STRING *a = (*a_)->addressFamily;
710 const ASN1_OCTET_STRING *b = (*b_)->addressFamily;
711 int len = ((a->length <= b->length) ? a->length : b->length);
712 int cmp = memcmp(a->data, b->data, len);
713 return cmp ? cmp : a->length - b->length;
714}
715
716/*
717 * Check whether an IPAddrBLocks is in canonical form.
718 */
719int v3_addr_is_canonical(IPAddrBlocks *addr)
720{
721 unsigned char a_min[ADDR_RAW_BUF_LEN], a_max[ADDR_RAW_BUF_LEN];
722 unsigned char b_min[ADDR_RAW_BUF_LEN], b_max[ADDR_RAW_BUF_LEN];
723 IPAddressOrRanges *aors;
724 int i, j, k;
725
726 /*
727 * Empty extension is cannonical.
728 */
729 if (addr == NULL)
730 return 1;
731
732 /*
733 * Check whether the top-level list is in order.
734 */
735 for (i = 0; i < sk_IPAddressFamily_num(addr) - 1; i++) {
736 const IPAddressFamily *a = sk_IPAddressFamily_value(addr, i);
737 const IPAddressFamily *b = sk_IPAddressFamily_value(addr, i + 1);
738 if (IPAddressFamily_cmp(&a, &b) >= 0)
739 return 0;
740 }
741
742 /*
743 * Top level's ok, now check each address family.
744 */
745 for (i = 0; i < sk_IPAddressFamily_num(addr); i++) {
746 IPAddressFamily *f = sk_IPAddressFamily_value(addr, i);
747 int length = length_from_afi(v3_addr_get_afi(f));
748
749 /*
750 * Inheritance is canonical. Anything other than inheritance or
751 * a SEQUENCE OF IPAddressOrRange is an ASN.1 error or something.
752 */
753 if (f == NULL || f->ipAddressChoice == NULL)
754 return 0;
755 switch (f->ipAddressChoice->type) {
756 case IPAddressChoice_inherit:
757 continue;
758 case IPAddressChoice_addressesOrRanges:
759 break;
760 default:
761 return 0;
762 }
763
764 /*
765 * It's an IPAddressOrRanges sequence, check it.
766 */
767 aors = f->ipAddressChoice->u.addressesOrRanges;
768 if (sk_IPAddressOrRange_num(aors) == 0)
769 return 0;
770 for (j = 0; j < sk_IPAddressOrRange_num(aors) - 1; j++) {
771 IPAddressOrRange *a = sk_IPAddressOrRange_value(aors, j);
772 IPAddressOrRange *b = sk_IPAddressOrRange_value(aors, j + 1);
773
774 extract_min_max(a, a_min, a_max, length);
775 extract_min_max(b, b_min, b_max, length);
776
777 /*
778 * Punt misordered list, overlapping start, or inverted range.
779 */
780 if (memcmp(a_min, b_min, length) >= 0 ||
781 memcmp(a_min, a_max, length) > 0 ||
782 memcmp(b_min, b_max, length) > 0)
783 return 0;
784
785 /*
786 * Punt if adjacent or overlapping. Check for adjacency by
787 * subtracting one from b_min first.
788 */
789 for (k = length - 1; k >= 0 && b_min[k]-- == 0x00; k--)
790 ;
791 if (memcmp(a_max, b_min, length) >= 0)
792 return 0;
793
794 /*
795 * Check for range that should be expressed as a prefix.
796 */
797 if (a->type == IPAddressOrRange_addressRange &&
798 range_should_be_prefix(a_min, a_max, length) >= 0)
799 return 0;
800 }
801
802 /*
803 * Check final range to see if it should be a prefix.
804 */
805 j = sk_IPAddressOrRange_num(aors) - 1;
806 {
807 IPAddressOrRange *a = sk_IPAddressOrRange_value(aors, j);
808 if (a->type == IPAddressOrRange_addressRange) {
809 extract_min_max(a, a_min, a_max, length);
810 if (range_should_be_prefix(a_min, a_max, length) >= 0)
811 return 0;
812 }
813 }
814 }
815
816 /*
817 * If we made it through all that, we're happy.
818 */
819 return 1;
820}
821
822/*
823 * Whack an IPAddressOrRanges into canonical form.
824 */
825static int IPAddressOrRanges_canonize(IPAddressOrRanges *aors,
826 const unsigned afi)
827{
828 int i, j, length = length_from_afi(afi);
829
830 /*
831 * Sort the IPAddressOrRanges sequence.
832 */
833 sk_IPAddressOrRange_sort(aors);
834
835 /*
836 * Clean up representation issues, punt on duplicates or overlaps.
837 */
838 for (i = 0; i < sk_IPAddressOrRange_num(aors) - 1; i++) {
839 IPAddressOrRange *a = sk_IPAddressOrRange_value(aors, i);
840 IPAddressOrRange *b = sk_IPAddressOrRange_value(aors, i + 1);
841 unsigned char a_min[ADDR_RAW_BUF_LEN], a_max[ADDR_RAW_BUF_LEN];
842 unsigned char b_min[ADDR_RAW_BUF_LEN], b_max[ADDR_RAW_BUF_LEN];
843
844 extract_min_max(a, a_min, a_max, length);
845 extract_min_max(b, b_min, b_max, length);
846
847 /*
848 * Punt overlaps.
849 */
850 if (memcmp(a_max, b_min, length) >= 0)
851 return 0;
852
853 /*
854 * Merge if a and b are adjacent. We check for
855 * adjacency by subtracting one from b_min first.
856 */
857 for (j = length - 1; j >= 0 && b_min[j]-- == 0x00; j--)
858 ;
859 if (memcmp(a_max, b_min, length) == 0) {
860 IPAddressOrRange *merged;
861 if (!make_addressRange(&merged, a_min, b_max, length))
862 return 0;
863 sk_IPAddressOrRange_set(aors, i, merged);
864 sk_IPAddressOrRange_delete(aors, i + 1);
865 IPAddressOrRange_free(a);
866 IPAddressOrRange_free(b);
867 --i;
868 continue;
869 }
870 }
871
872 return 1;
873}
874
875/*
876 * Whack an IPAddrBlocks extension into canonical form.
877 */
878int v3_addr_canonize(IPAddrBlocks *addr)
879{
880 int i;
881 for (i = 0; i < sk_IPAddressFamily_num(addr); i++) {
882 IPAddressFamily *f = sk_IPAddressFamily_value(addr, i);
883 if (f->ipAddressChoice->type == IPAddressChoice_addressesOrRanges &&
884 !IPAddressOrRanges_canonize(f->ipAddressChoice->u.addressesOrRanges,
885 v3_addr_get_afi(f)))
886 return 0;
887 }
888 sk_IPAddressFamily_set_cmp_func(addr, IPAddressFamily_cmp);
889 sk_IPAddressFamily_sort(addr);
890 OPENSSL_assert(v3_addr_is_canonical(addr));
891 return 1;
892}
893
894/*
895 * v2i handler for the IPAddrBlocks extension.
896 */
897static void *v2i_IPAddrBlocks(const struct v3_ext_method *method,
898 struct v3_ext_ctx *ctx,
899 STACK_OF(CONF_VALUE) *values)
900{
901 static const char v4addr_chars[] = "0123456789.";
902 static const char v6addr_chars[] = "0123456789.:abcdefABCDEF";
903 IPAddrBlocks *addr = NULL;
904 char *s = NULL, *t;
905 int i;
906
907 if ((addr = sk_IPAddressFamily_new(IPAddressFamily_cmp)) == NULL) {
908 X509V3err(X509V3_F_V2I_IPADDRBLOCKS, ERR_R_MALLOC_FAILURE);
909 return NULL;
910 }
911
912 for (i = 0; i < sk_CONF_VALUE_num(values); i++) {
913 CONF_VALUE *val = sk_CONF_VALUE_value(values, i);
914 unsigned char min[ADDR_RAW_BUF_LEN], max[ADDR_RAW_BUF_LEN];
915 unsigned afi, *safi = NULL, safi_;
916 const char *addr_chars;
917 int prefixlen, i1, i2, delim, length;
918
919 if ( !name_cmp(val->name, "IPv4")) {
920 afi = IANA_AFI_IPV4;
921 } else if (!name_cmp(val->name, "IPv6")) {
922 afi = IANA_AFI_IPV6;
923 } else if (!name_cmp(val->name, "IPv4-SAFI")) {
924 afi = IANA_AFI_IPV4;
925 safi = &safi_;
926 } else if (!name_cmp(val->name, "IPv6-SAFI")) {
927 afi = IANA_AFI_IPV6;
928 safi = &safi_;
929 } else {
930 X509V3err(X509V3_F_V2I_IPADDRBLOCKS, X509V3_R_EXTENSION_NAME_ERROR);
931 X509V3_conf_err(val);
932 goto err;
933 }
934
935 switch (afi) {
936 case IANA_AFI_IPV4:
937 addr_chars = v4addr_chars;
938 break;
939 case IANA_AFI_IPV6:
940 addr_chars = v6addr_chars;
941 break;
942 }
943
944 length = length_from_afi(afi);
945
946 /*
947 * Handle SAFI, if any, and BUF_strdup() so we can null-terminate
948 * the other input values.
949 */
950 if (safi != NULL) {
951 *safi = strtoul(val->value, &t, 0);
952 t += strspn(t, " \t");
953 if (*safi > 0xFF || *t++ != ':') {
954 X509V3err(X509V3_F_V2I_IPADDRBLOCKS, X509V3_R_INVALID_SAFI);
955 X509V3_conf_err(val);
956 goto err;
957 }
958 t += strspn(t, " \t");
959 s = BUF_strdup(t);
960 } else {
961 s = BUF_strdup(val->value);
962 }
963 if (s == NULL) {
964 X509V3err(X509V3_F_V2I_IPADDRBLOCKS, ERR_R_MALLOC_FAILURE);
965 goto err;
966 }
967
968 /*
969 * Check for inheritance. Not worth additional complexity to
970 * optimize this (seldom-used) case.
971 */
972 if (!strcmp(s, "inherit")) {
973 if (!v3_addr_add_inherit(addr, afi, safi)) {
974 X509V3err(X509V3_F_V2I_IPADDRBLOCKS, X509V3_R_INVALID_INHERITANCE);
975 X509V3_conf_err(val);
976 goto err;
977 }
978 OPENSSL_free(s);
979 s = NULL;
980 continue;
981 }
982
983 i1 = strspn(s, addr_chars);
984 i2 = i1 + strspn(s + i1, " \t");
985 delim = s[i2++];
986 s[i1] = '\0';
987
988 if (a2i_ipadd(min, s) != length) {
989 X509V3err(X509V3_F_V2I_IPADDRBLOCKS, X509V3_R_INVALID_IPADDRESS);
990 X509V3_conf_err(val);
991 goto err;
992 }
993
994 switch (delim) {
995 case '/':
996 prefixlen = (int) strtoul(s + i2, &t, 10);
997 if (t == s + i2 || *t != '\0') {
998 X509V3err(X509V3_F_V2I_IPADDRBLOCKS, X509V3_R_EXTENSION_VALUE_ERROR);
999 X509V3_conf_err(val);
1000 goto err;
1001 }
1002 if (!v3_addr_add_prefix(addr, afi, safi, min, prefixlen)) {
1003 X509V3err(X509V3_F_V2I_IPADDRBLOCKS, ERR_R_MALLOC_FAILURE);
1004 goto err;
1005 }
1006 break;
1007 case '-':
1008 i1 = i2 + strspn(s + i2, " \t");
1009 i2 = i1 + strspn(s + i1, addr_chars);
1010 if (i1 == i2 || s[i2] != '\0') {
1011 X509V3err(X509V3_F_V2I_IPADDRBLOCKS, X509V3_R_EXTENSION_VALUE_ERROR);
1012 X509V3_conf_err(val);
1013 goto err;
1014 }
1015 if (a2i_ipadd(max, s + i1) != length) {
1016 X509V3err(X509V3_F_V2I_IPADDRBLOCKS, X509V3_R_INVALID_IPADDRESS);
1017 X509V3_conf_err(val);
1018 goto err;
1019 }
1020 if (!v3_addr_add_range(addr, afi, safi, min, max)) {
1021 X509V3err(X509V3_F_V2I_IPADDRBLOCKS, ERR_R_MALLOC_FAILURE);
1022 goto err;
1023 }
1024 break;
1025 case '\0':
1026 if (!v3_addr_add_prefix(addr, afi, safi, min, length * 8)) {
1027 X509V3err(X509V3_F_V2I_IPADDRBLOCKS, ERR_R_MALLOC_FAILURE);
1028 goto err;
1029 }
1030 break;
1031 default:
1032 X509V3err(X509V3_F_V2I_IPADDRBLOCKS, X509V3_R_EXTENSION_VALUE_ERROR);
1033 X509V3_conf_err(val);
1034 goto err;
1035 }
1036
1037 OPENSSL_free(s);
1038 s = NULL;
1039 }
1040
1041 /*
1042 * Canonize the result, then we're done.
1043 */
1044 if (!v3_addr_canonize(addr))
1045 goto err;
1046 return addr;
1047
1048 err:
1049 OPENSSL_free(s);
1050 sk_IPAddressFamily_pop_free(addr, IPAddressFamily_free);
1051 return NULL;
1052}
1053
1054/*
1055 * OpenSSL dispatch
1056 */
1057const X509V3_EXT_METHOD v3_addr = {
1058 NID_sbgp_ipAddrBlock, /* nid */
1059 0, /* flags */
1060 ASN1_ITEM_ref(IPAddrBlocks), /* template */
1061 0, 0, 0, 0, /* old functions, ignored */
1062 0, /* i2s */
1063 0, /* s2i */
1064 0, /* i2v */
1065 v2i_IPAddrBlocks, /* v2i */
1066 i2r_IPAddrBlocks, /* i2r */
1067 0, /* r2i */
1068 NULL /* extension-specific data */
1069};
1070
1071/*
1072 * Figure out whether extension sues inheritance.
1073 */
1074int v3_addr_inherits(IPAddrBlocks *addr)
1075{
1076 int i;
1077 if (addr == NULL)
1078 return 0;
1079 for (i = 0; i < sk_IPAddressFamily_num(addr); i++) {
1080 IPAddressFamily *f = sk_IPAddressFamily_value(addr, i);
1081 if (f->ipAddressChoice->type == IPAddressChoice_inherit)
1082 return 1;
1083 }
1084 return 0;
1085}
1086
1087/*
1088 * Figure out whether parent contains child.
1089 */
1090static int addr_contains(IPAddressOrRanges *parent,
1091 IPAddressOrRanges *child,
1092 int length)
1093{
1094 unsigned char p_min[ADDR_RAW_BUF_LEN], p_max[ADDR_RAW_BUF_LEN];
1095 unsigned char c_min[ADDR_RAW_BUF_LEN], c_max[ADDR_RAW_BUF_LEN];
1096 int p, c;
1097
1098 if (child == NULL || parent == child)
1099 return 1;
1100 if (parent == NULL)
1101 return 0;
1102
1103 p = 0;
1104 for (c = 0; c < sk_IPAddressOrRange_num(child); c++) {
1105 extract_min_max(sk_IPAddressOrRange_value(child, c),
1106 c_min, c_max, length);
1107 for (;; p++) {
1108 if (p >= sk_IPAddressOrRange_num(parent))
1109 return 0;
1110 extract_min_max(sk_IPAddressOrRange_value(parent, p),
1111 p_min, p_max, length);
1112 if (memcmp(p_max, c_max, length) < 0)
1113 continue;
1114 if (memcmp(p_min, c_min, length) > 0)
1115 return 0;
1116 break;
1117 }
1118 }
1119
1120 return 1;
1121}
1122
1123/*
1124 * Test whether a is a subset of b.
1125 */
1126int v3_addr_subset(IPAddrBlocks *a, IPAddrBlocks *b)
1127{
1128 int i;
1129 if (a == NULL || a == b)
1130 return 1;
1131 if (b == NULL || v3_addr_inherits(a) || v3_addr_inherits(b))
1132 return 0;
1133 sk_IPAddressFamily_set_cmp_func(b, IPAddressFamily_cmp);
1134 for (i = 0; i < sk_IPAddressFamily_num(a); i++) {
1135 IPAddressFamily *fa = sk_IPAddressFamily_value(a, i);
1136 int j = sk_IPAddressFamily_find(b, fa);
1137 IPAddressFamily *fb;
1138 fb = sk_IPAddressFamily_value(b, j);
1139 if (fb == NULL)
1140 return 0;
1141 if (!addr_contains(fb->ipAddressChoice->u.addressesOrRanges,
1142 fa->ipAddressChoice->u.addressesOrRanges,
1143 length_from_afi(v3_addr_get_afi(fb))))
1144 return 0;
1145 }
1146 return 1;
1147}
1148
1149/*
1150 * Validation error handling via callback.
1151 */
1152#define validation_err(_err_) \
1153 do { \
1154 if (ctx != NULL) { \
1155 ctx->error = _err_; \
1156 ctx->error_depth = i; \
1157 ctx->current_cert = x; \
1158 ret = ctx->verify_cb(0, ctx); \
1159 } else { \
1160 ret = 0; \
1161 } \
1162 if (!ret) \
1163 goto done; \
1164 } while (0)
1165
1166/*
1167 * Core code for RFC 3779 2.3 path validation.
1168 */
1169static int v3_addr_validate_path_internal(X509_STORE_CTX *ctx,
1170 STACK_OF(X509) *chain,
1171 IPAddrBlocks *ext)
1172{
1173 IPAddrBlocks *child = NULL;
1174 int i, j, ret = 1;
1175 X509 *x;
1176
1177 OPENSSL_assert(chain != NULL && sk_X509_num(chain) > 0);
1178 OPENSSL_assert(ctx != NULL || ext != NULL);
1179 OPENSSL_assert(ctx == NULL || ctx->verify_cb != NULL);
1180
1181 /*
1182 * Figure out where to start. If we don't have an extension to
1183 * check, we're done. Otherwise, check canonical form and
1184 * set up for walking up the chain.
1185 */
1186 if (ext != NULL) {
1187 i = -1;
1188 x = NULL;
1189 } else {
1190 i = 0;
1191 x = sk_X509_value(chain, i);
1192 OPENSSL_assert(x != NULL);
1193 if ((ext = x->rfc3779_addr) == NULL)
1194 goto done;
1195 }
1196 if (!v3_addr_is_canonical(ext))
1197 validation_err(X509_V_ERR_INVALID_EXTENSION);
1198 sk_IPAddressFamily_set_cmp_func(ext, IPAddressFamily_cmp);
1199 if ((child = sk_IPAddressFamily_dup(ext)) == NULL) {
1200 X509V3err(X509V3_F_V3_ADDR_VALIDATE_PATH_INTERNAL, ERR_R_MALLOC_FAILURE);
1201 ret = 0;
1202 goto done;
1203 }
1204
1205 /*
1206 * Now walk up the chain. No cert may list resources that its
1207 * parent doesn't list.
1208 */
1209 for (i++; i < sk_X509_num(chain); i++) {
1210 x = sk_X509_value(chain, i);
1211 OPENSSL_assert(x != NULL);
1212 if (!v3_addr_is_canonical(x->rfc3779_addr))
1213 validation_err(X509_V_ERR_INVALID_EXTENSION);
1214 if (x->rfc3779_addr == NULL) {
1215 for (j = 0; j < sk_IPAddressFamily_num(child); j++) {
1216 IPAddressFamily *fc = sk_IPAddressFamily_value(child, j);
1217 if (fc->ipAddressChoice->type != IPAddressChoice_inherit) {
1218 validation_err(X509_V_ERR_UNNESTED_RESOURCE);
1219 break;
1220 }
1221 }
1222 continue;
1223 }
1224 sk_IPAddressFamily_set_cmp_func(x->rfc3779_addr, IPAddressFamily_cmp);
1225 for (j = 0; j < sk_IPAddressFamily_num(child); j++) {
1226 IPAddressFamily *fc = sk_IPAddressFamily_value(child, j);
1227 int k = sk_IPAddressFamily_find(x->rfc3779_addr, fc);
1228 IPAddressFamily *fp = sk_IPAddressFamily_value(x->rfc3779_addr, k);
1229 if (fp == NULL) {
1230 if (fc->ipAddressChoice->type == IPAddressChoice_addressesOrRanges) {
1231 validation_err(X509_V_ERR_UNNESTED_RESOURCE);
1232 break;
1233 }
1234 continue;
1235 }
1236 if (fp->ipAddressChoice->type == IPAddressChoice_addressesOrRanges) {
1237 if (fc->ipAddressChoice->type == IPAddressChoice_inherit ||
1238 addr_contains(fp->ipAddressChoice->u.addressesOrRanges,
1239 fc->ipAddressChoice->u.addressesOrRanges,
1240 length_from_afi(v3_addr_get_afi(fc))))
1241 sk_IPAddressFamily_set(child, j, fp);
1242 else
1243 validation_err(X509_V_ERR_UNNESTED_RESOURCE);
1244 }
1245 }
1246 }
1247
1248 /*
1249 * Trust anchor can't inherit.
1250 */
1251 OPENSSL_assert(x != NULL);
1252 if (x->rfc3779_addr != NULL) {
1253 for (j = 0; j < sk_IPAddressFamily_num(x->rfc3779_addr); j++) {
1254 IPAddressFamily *fp = sk_IPAddressFamily_value(x->rfc3779_addr, j);
1255 if (fp->ipAddressChoice->type == IPAddressChoice_inherit &&
1256 sk_IPAddressFamily_find(child, fp) >= 0)
1257 validation_err(X509_V_ERR_UNNESTED_RESOURCE);
1258 }
1259 }
1260
1261 done:
1262 sk_IPAddressFamily_free(child);
1263 return ret;
1264}
1265
1266#undef validation_err
1267
1268/*
1269 * RFC 3779 2.3 path validation -- called from X509_verify_cert().
1270 */
1271int v3_addr_validate_path(X509_STORE_CTX *ctx)
1272{
1273 return v3_addr_validate_path_internal(ctx, ctx->chain, NULL);
1274}
1275
1276/*
1277 * RFC 3779 2.3 path validation of an extension.
1278 * Test whether chain covers extension.
1279 */
1280int v3_addr_validate_resource_set(STACK_OF(X509) *chain,
1281 IPAddrBlocks *ext,
1282 int allow_inheritance)
1283{
1284 if (ext == NULL)
1285 return 1;
1286 if (chain == NULL || sk_X509_num(chain) == 0)
1287 return 0;
1288 if (!allow_inheritance && v3_addr_inherits(ext))
1289 return 0;
1290 return v3_addr_validate_path_internal(NULL, chain, ext);
1291}
1292
1293#endif /* OPENSSL_NO_RFC3779 */
diff --git a/src/lib/libcrypto/x509v3/v3_asid.c b/src/lib/libcrypto/x509v3/v3_asid.c
new file mode 100644
index 0000000000..3f434c0603
--- /dev/null
+++ b/src/lib/libcrypto/x509v3/v3_asid.c
@@ -0,0 +1,843 @@
1/*
2 * Contributed to the OpenSSL Project by the American Registry for
3 * Internet Numbers ("ARIN").
4 */
5/* ====================================================================
6 * Copyright (c) 2006 The OpenSSL Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
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 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
56 */
57
58/*
59 * Implementation of RFC 3779 section 3.2.
60 */
61
62#include <stdio.h>
63#include <string.h>
64#include "cryptlib.h"
65#include <openssl/conf.h>
66#include <openssl/asn1.h>
67#include <openssl/asn1t.h>
68#include <openssl/x509v3.h>
69#include <openssl/x509.h>
70#include <openssl/bn.h>
71
72#ifndef OPENSSL_NO_RFC3779
73
74/*
75 * OpenSSL ASN.1 template translation of RFC 3779 3.2.3.
76 */
77
78ASN1_SEQUENCE(ASRange) = {
79 ASN1_SIMPLE(ASRange, min, ASN1_INTEGER),
80 ASN1_SIMPLE(ASRange, max, ASN1_INTEGER)
81} ASN1_SEQUENCE_END(ASRange)
82
83ASN1_CHOICE(ASIdOrRange) = {
84 ASN1_SIMPLE(ASIdOrRange, u.id, ASN1_INTEGER),
85 ASN1_SIMPLE(ASIdOrRange, u.range, ASRange)
86} ASN1_CHOICE_END(ASIdOrRange)
87
88ASN1_CHOICE(ASIdentifierChoice) = {
89 ASN1_SIMPLE(ASIdentifierChoice, u.inherit, ASN1_NULL),
90 ASN1_SEQUENCE_OF(ASIdentifierChoice, u.asIdsOrRanges, ASIdOrRange)
91} ASN1_CHOICE_END(ASIdentifierChoice)
92
93ASN1_SEQUENCE(ASIdentifiers) = {
94 ASN1_EXP_OPT(ASIdentifiers, asnum, ASIdentifierChoice, 0),
95 ASN1_EXP_OPT(ASIdentifiers, rdi, ASIdentifierChoice, 1)
96} ASN1_SEQUENCE_END(ASIdentifiers)
97
98IMPLEMENT_ASN1_FUNCTIONS(ASRange)
99IMPLEMENT_ASN1_FUNCTIONS(ASIdOrRange)
100IMPLEMENT_ASN1_FUNCTIONS(ASIdentifierChoice)
101IMPLEMENT_ASN1_FUNCTIONS(ASIdentifiers)
102
103/*
104 * i2r method for an ASIdentifierChoice.
105 */
106static int i2r_ASIdentifierChoice(BIO *out,
107 ASIdentifierChoice *choice,
108 int indent,
109 const char *msg)
110{
111 int i;
112 char *s;
113 if (choice == NULL)
114 return 1;
115 BIO_printf(out, "%*s%s:\n", indent, "", msg);
116 switch (choice->type) {
117 case ASIdentifierChoice_inherit:
118 BIO_printf(out, "%*sinherit\n", indent + 2, "");
119 break;
120 case ASIdentifierChoice_asIdsOrRanges:
121 for (i = 0; i < sk_ASIdOrRange_num(choice->u.asIdsOrRanges); i++) {
122 ASIdOrRange *aor = 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;
147 }
148 return 1;
149}
150
151/*
152 * i2r method for an ASIdentifier extension.
153 */
154static int i2r_ASIdentifiers(const X509V3_EXT_METHOD *method,
155 void *ext,
156 BIO *out,
157 int indent)
158{
159 ASIdentifiers *asid = ext;
160 return (i2r_ASIdentifierChoice(out, asid->asnum, indent,
161 "Autonomous System Numbers") &&
162 i2r_ASIdentifierChoice(out, asid->rdi, indent,
163 "Routing Domain Identifiers"));
164}
165
166/*
167 * Sort comparision function for a sequence of ASIdOrRange elements.
168 */
169static int ASIdOrRange_cmp(const ASIdOrRange * const *a_,
170 const ASIdOrRange * const *b_)
171{
172 const ASIdOrRange *a = *a_, *b = *b_;
173
174 OPENSSL_assert((a->type == ASIdOrRange_id && a->u.id != NULL) ||
175 (a->type == ASIdOrRange_range && a->u.range != NULL &&
176 a->u.range->min != NULL && a->u.range->max != NULL));
177
178 OPENSSL_assert((b->type == ASIdOrRange_id && b->u.id != NULL) ||
179 (b->type == ASIdOrRange_range && b->u.range != NULL &&
180 b->u.range->min != NULL && b->u.range->max != NULL));
181
182 if (a->type == ASIdOrRange_id && b->type == ASIdOrRange_id)
183 return ASN1_INTEGER_cmp(a->u.id, b->u.id);
184
185 if (a->type == ASIdOrRange_range && b->type == ASIdOrRange_range) {
186 int r = ASN1_INTEGER_cmp(a->u.range->min, b->u.range->min);
187 return r != 0 ? r : ASN1_INTEGER_cmp(a->u.range->max, b->u.range->max);
188 }
189
190 if (a->type == ASIdOrRange_id)
191 return ASN1_INTEGER_cmp(a->u.id, b->u.range->min);
192 else
193 return ASN1_INTEGER_cmp(a->u.range->min, b->u.id);
194}
195
196/*
197 * Add an inherit element.
198 */
199int v3_asid_add_inherit(ASIdentifiers *asid, int which)
200{
201 ASIdentifierChoice **choice;
202 if (asid == NULL)
203 return 0;
204 switch (which) {
205 case V3_ASID_ASNUM:
206 choice = &asid->asnum;
207 break;
208 case V3_ASID_RDI:
209 choice = &asid->rdi;
210 break;
211 default:
212 return 0;
213 }
214 if (*choice == NULL) {
215 if ((*choice = ASIdentifierChoice_new()) == NULL)
216 return 0;
217 OPENSSL_assert((*choice)->u.inherit == NULL);
218 if (((*choice)->u.inherit = ASN1_NULL_new()) == NULL)
219 return 0;
220 (*choice)->type = ASIdentifierChoice_inherit;
221 }
222 return (*choice)->type == ASIdentifierChoice_inherit;
223}
224
225/*
226 * Add an ID or range to an ASIdentifierChoice.
227 */
228int v3_asid_add_id_or_range(ASIdentifiers *asid,
229 int which,
230 ASN1_INTEGER *min,
231 ASN1_INTEGER *max)
232{
233 ASIdentifierChoice **choice;
234 ASIdOrRange *aor;
235 if (asid == NULL)
236 return 0;
237 switch (which) {
238 case V3_ASID_ASNUM:
239 choice = &asid->asnum;
240 break;
241 case V3_ASID_RDI:
242 choice = &asid->rdi;
243 break;
244 default:
245 return 0;
246 }
247 if (*choice != NULL && (*choice)->type == ASIdentifierChoice_inherit)
248 return 0;
249 if (*choice == NULL) {
250 if ((*choice = ASIdentifierChoice_new()) == NULL)
251 return 0;
252 OPENSSL_assert((*choice)->u.asIdsOrRanges == NULL);
253 (*choice)->u.asIdsOrRanges = sk_ASIdOrRange_new(ASIdOrRange_cmp);
254 if ((*choice)->u.asIdsOrRanges == NULL)
255 return 0;
256 (*choice)->type = ASIdentifierChoice_asIdsOrRanges;
257 }
258 if ((aor = ASIdOrRange_new()) == NULL)
259 return 0;
260 if (max == NULL) {
261 aor->type = ASIdOrRange_id;
262 aor->u.id = min;
263 } else {
264 aor->type = ASIdOrRange_range;
265 if ((aor->u.range = ASRange_new()) == NULL)
266 goto err;
267 ASN1_INTEGER_free(aor->u.range->min);
268 aor->u.range->min = min;
269 ASN1_INTEGER_free(aor->u.range->max);
270 aor->u.range->max = max;
271 }
272 if (!(sk_ASIdOrRange_push((*choice)->u.asIdsOrRanges, aor)))
273 goto err;
274 return 1;
275
276 err:
277 ASIdOrRange_free(aor);
278 return 0;
279}
280
281/*
282 * Extract min and max values from an ASIdOrRange.
283 */
284static void extract_min_max(ASIdOrRange *aor,
285 ASN1_INTEGER **min,
286 ASN1_INTEGER **max)
287{
288 OPENSSL_assert(aor != NULL && min != NULL && max != NULL);
289 switch (aor->type) {
290 case ASIdOrRange_id:
291 *min = aor->u.id;
292 *max = aor->u.id;
293 return;
294 case ASIdOrRange_range:
295 *min = aor->u.range->min;
296 *max = aor->u.range->max;
297 return;
298 }
299}
300
301/*
302 * Check whether an ASIdentifierChoice is in canonical form.
303 */
304static int ASIdentifierChoice_is_canonical(ASIdentifierChoice *choice)
305{
306 ASN1_INTEGER *a_max_plus_one = NULL;
307 BIGNUM *bn = NULL;
308 int i, ret = 0;
309
310 /*
311 * Empty element or inheritance is canonical.
312 */
313 if (choice == NULL || choice->type == ASIdentifierChoice_inherit)
314 return 1;
315
316 /*
317 * If not a list, or if empty list, it's broken.
318 */
319 if (choice->type != ASIdentifierChoice_asIdsOrRanges ||
320 sk_ASIdOrRange_num(choice->u.asIdsOrRanges) == 0)
321 return 0;
322
323 /*
324 * It's a list, check it.
325 */
326 for (i = 0; i < sk_ASIdOrRange_num(choice->u.asIdsOrRanges) - 1; i++) {
327 ASIdOrRange *a = sk_ASIdOrRange_value(choice->u.asIdsOrRanges, i);
328 ASIdOrRange *b = sk_ASIdOrRange_value(choice->u.asIdsOrRanges, i + 1);
329 ASN1_INTEGER *a_min, *a_max, *b_min, *b_max;
330
331 extract_min_max(a, &a_min, &a_max);
332 extract_min_max(b, &b_min, &b_max);
333
334 /*
335 * Punt misordered list, overlapping start, or inverted range.
336 */
337 if (ASN1_INTEGER_cmp(a_min, b_min) >= 0 ||
338 ASN1_INTEGER_cmp(a_min, a_max) > 0 ||
339 ASN1_INTEGER_cmp(b_min, b_max) > 0)
340 goto done;
341
342 /*
343 * Calculate a_max + 1 to check for adjacency.
344 */
345 if ((bn == NULL && (bn = BN_new()) == NULL) ||
346 ASN1_INTEGER_to_BN(a_max, bn) == NULL ||
347 !BN_add_word(bn, 1) ||
348 (a_max_plus_one = BN_to_ASN1_INTEGER(bn, a_max_plus_one)) == NULL) {
349 X509V3err(X509V3_F_ASIDENTIFIERCHOICE_IS_CANONICAL,
350 ERR_R_MALLOC_FAILURE);
351 goto done;
352 }
353
354 /*
355 * Punt if adjacent or overlapping.
356 */
357 if (ASN1_INTEGER_cmp(a_max_plus_one, b_min) >= 0)
358 goto done;
359 }
360
361 ret = 1;
362
363 done:
364 ASN1_INTEGER_free(a_max_plus_one);
365 BN_free(bn);
366 return ret;
367}
368
369/*
370 * Check whether an ASIdentifier extension is in canonical form.
371 */
372int v3_asid_is_canonical(ASIdentifiers *asid)
373{
374 return (asid == NULL ||
375 (ASIdentifierChoice_is_canonical(asid->asnum) &&
376 ASIdentifierChoice_is_canonical(asid->rdi)));
377}
378
379/*
380 * Whack an ASIdentifierChoice into canonical form.
381 */
382static int ASIdentifierChoice_canonize(ASIdentifierChoice *choice)
383{
384 ASN1_INTEGER *a_max_plus_one = NULL;
385 BIGNUM *bn = NULL;
386 int i, ret = 0;
387
388 /*
389 * Nothing to do for empty element or inheritance.
390 */
391 if (choice == NULL || choice->type == ASIdentifierChoice_inherit)
392 return 1;
393
394 /*
395 * We have a list. Sort it.
396 */
397 OPENSSL_assert(choice->type == ASIdentifierChoice_asIdsOrRanges);
398 sk_ASIdOrRange_sort(choice->u.asIdsOrRanges);
399
400 /*
401 * Now check for errors and suboptimal encoding, rejecting the
402 * former and fixing the latter.
403 */
404 for (i = 0; i < sk_ASIdOrRange_num(choice->u.asIdsOrRanges) - 1; i++) {
405 ASIdOrRange *a = sk_ASIdOrRange_value(choice->u.asIdsOrRanges, i);
406 ASIdOrRange *b = sk_ASIdOrRange_value(choice->u.asIdsOrRanges, i + 1);
407 ASN1_INTEGER *a_min, *a_max, *b_min, *b_max;
408
409 extract_min_max(a, &a_min, &a_max);
410 extract_min_max(b, &b_min, &b_max);
411
412 /*
413 * Make sure we're properly sorted (paranoia).
414 */
415 OPENSSL_assert(ASN1_INTEGER_cmp(a_min, b_min) <= 0);
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 ||
431 !BN_add_word(bn, 1) ||
432 (a_max_plus_one = BN_to_ASN1_INTEGER(bn, a_max_plus_one)) == NULL) {
433 X509V3err(X509V3_F_ASIDENTIFIERCHOICE_CANONIZE, ERR_R_MALLOC_FAILURE);
434 goto done;
435 }
436
437 /*
438 * If a and b are adjacent, merge them.
439 */
440 if (ASN1_INTEGER_cmp(a_max_plus_one, b_min) == 0) {
441 ASRange *r;
442 switch (a->type) {
443 case ASIdOrRange_id:
444 if ((r = OPENSSL_malloc(sizeof(ASRange))) == NULL) {
445 X509V3err(X509V3_F_ASIDENTIFIERCHOICE_CANONIZE,
446 ERR_R_MALLOC_FAILURE);
447 goto done;
448 }
449 r->min = a_min;
450 r->max = b_max;
451 a->type = ASIdOrRange_range;
452 a->u.range = r;
453 break;
454 case ASIdOrRange_range:
455 ASN1_INTEGER_free(a->u.range->max);
456 a->u.range->max = b_max;
457 break;
458 }
459 switch (b->type) {
460 case ASIdOrRange_id:
461 b->u.id = NULL;
462 break;
463 case ASIdOrRange_range:
464 b->u.range->max = NULL;
465 break;
466 }
467 ASIdOrRange_free(b);
468 sk_ASIdOrRange_delete(choice->u.asIdsOrRanges, i + 1);
469 i--;
470 continue;
471 }
472 }
473
474 OPENSSL_assert(ASIdentifierChoice_is_canonical(choice)); /* Paranoia */
475
476 ret = 1;
477
478 done:
479 ASN1_INTEGER_free(a_max_plus_one);
480 BN_free(bn);
481 return ret;
482}
483
484/*
485 * Whack an ASIdentifier extension into canonical form.
486 */
487int v3_asid_canonize(ASIdentifiers *asid)
488{
489 return (asid == NULL ||
490 (ASIdentifierChoice_canonize(asid->asnum) &&
491 ASIdentifierChoice_canonize(asid->rdi)));
492}
493
494/*
495 * v2i method for an ASIdentifier extension.
496 */
497static void *v2i_ASIdentifiers(const struct v3_ext_method *method,
498 struct v3_ext_ctx *ctx,
499 STACK_OF(CONF_VALUE) *values)
500{
501 ASIdentifiers *asid = NULL;
502 int i;
503
504 if ((asid = ASIdentifiers_new()) == NULL) {
505 X509V3err(X509V3_F_V2I_ASIDENTIFIERS, ERR_R_MALLOC_FAILURE);
506 return NULL;
507 }
508
509 for (i = 0; i < sk_CONF_VALUE_num(values); i++) {
510 CONF_VALUE *val = sk_CONF_VALUE_value(values, i);
511 ASN1_INTEGER *min = NULL, *max = NULL;
512 int i1, i2, i3, is_range, which;
513
514 /*
515 * Figure out whether this is an AS or an RDI.
516 */
517 if ( !name_cmp(val->name, "AS")) {
518 which = V3_ASID_ASNUM;
519 } else if (!name_cmp(val->name, "RDI")) {
520 which = V3_ASID_RDI;
521 } else {
522 X509V3err(X509V3_F_V2I_ASIDENTIFIERS, X509V3_R_EXTENSION_NAME_ERROR);
523 X509V3_conf_err(val);
524 goto err;
525 }
526
527 /*
528 * Handle inheritance.
529 */
530 if (!strcmp(val->value, "inherit")) {
531 if (v3_asid_add_inherit(asid, which))
532 continue;
533 X509V3err(X509V3_F_V2I_ASIDENTIFIERS, X509V3_R_INVALID_INHERITANCE);
534 X509V3_conf_err(val);
535 goto err;
536 }
537
538 /*
539 * Number, range, or mistake, pick it apart and figure out which.
540 */
541 i1 = strspn(val->value, "0123456789");
542 if (val->value[i1] == '\0') {
543 is_range = 0;
544 } else {
545 is_range = 1;
546 i2 = i1 + strspn(val->value + i1, " \t");
547 if (val->value[i2] != '-') {
548 X509V3err(X509V3_F_V2I_ASIDENTIFIERS, X509V3_R_INVALID_ASNUMBER);
549 X509V3_conf_err(val);
550 goto err;
551 }
552 i2++;
553 i2 = i2 + strspn(val->value + i2, " \t");
554 i3 = i2 + strspn(val->value + i2, "0123456789");
555 if (val->value[i3] != '\0') {
556 X509V3err(X509V3_F_V2I_ASIDENTIFIERS, X509V3_R_INVALID_ASRANGE);
557 X509V3_conf_err(val);
558 goto err;
559 }
560 }
561
562 /*
563 * Syntax is ok, read and add it.
564 */
565 if (!is_range) {
566 if (!X509V3_get_value_int(val, &min)) {
567 X509V3err(X509V3_F_V2I_ASIDENTIFIERS, ERR_R_MALLOC_FAILURE);
568 goto err;
569 }
570 } else {
571 char *s = BUF_strdup(val->value);
572 if (s == NULL) {
573 X509V3err(X509V3_F_V2I_ASIDENTIFIERS, ERR_R_MALLOC_FAILURE);
574 goto err;
575 }
576 s[i1] = '\0';
577 min = s2i_ASN1_INTEGER(NULL, s);
578 max = s2i_ASN1_INTEGER(NULL, s + i2);
579 OPENSSL_free(s);
580 if (min == NULL || max == NULL) {
581 ASN1_INTEGER_free(min);
582 ASN1_INTEGER_free(max);
583 X509V3err(X509V3_F_V2I_ASIDENTIFIERS, ERR_R_MALLOC_FAILURE);
584 goto err;
585 }
586 }
587 if (!v3_asid_add_id_or_range(asid, which, min, max)) {
588 ASN1_INTEGER_free(min);
589 ASN1_INTEGER_free(max);
590 X509V3err(X509V3_F_V2I_ASIDENTIFIERS, ERR_R_MALLOC_FAILURE);
591 goto err;
592 }
593 }
594
595 /*
596 * Canonize the result, then we're done.
597 */
598 if (!v3_asid_canonize(asid))
599 goto err;
600 return asid;
601
602 err:
603 ASIdentifiers_free(asid);
604 return NULL;
605}
606
607/*
608 * OpenSSL dispatch.
609 */
610const X509V3_EXT_METHOD v3_asid = {
611 NID_sbgp_autonomousSysNum, /* nid */
612 0, /* flags */
613 ASN1_ITEM_ref(ASIdentifiers), /* template */
614 0, 0, 0, 0, /* old functions, ignored */
615 0, /* i2s */
616 0, /* s2i */
617 0, /* i2v */
618 v2i_ASIdentifiers, /* v2i */
619 i2r_ASIdentifiers, /* i2r */
620 0, /* r2i */
621 NULL /* extension-specific data */
622};
623
624/*
625 * Figure out whether extension uses inheritance.
626 */
627int v3_asid_inherits(ASIdentifiers *asid)
628{
629 return (asid != NULL &&
630 ((asid->asnum != NULL &&
631 asid->asnum->type == ASIdentifierChoice_inherit) ||
632 (asid->rdi != NULL &&
633 asid->rdi->type == ASIdentifierChoice_inherit)));
634}
635
636/*
637 * Figure out whether parent contains child.
638 */
639static int asid_contains(ASIdOrRanges *parent, ASIdOrRanges *child)
640{
641 ASN1_INTEGER *p_min, *p_max, *c_min, *c_max;
642 int p, c;
643
644 if (child == NULL || parent == child)
645 return 1;
646 if (parent == NULL)
647 return 0;
648
649 p = 0;
650 for (c = 0; c < sk_ASIdOrRange_num(child); c++) {
651 extract_min_max(sk_ASIdOrRange_value(child, c), &c_min, &c_max);
652 for (;; p++) {
653 if (p >= sk_ASIdOrRange_num(parent))
654 return 0;
655 extract_min_max(sk_ASIdOrRange_value(parent, p), &p_min, &p_max);
656 if (ASN1_INTEGER_cmp(p_max, c_max) < 0)
657 continue;
658 if (ASN1_INTEGER_cmp(p_min, c_min) > 0)
659 return 0;
660 break;
661 }
662 }
663
664 return 1;
665}
666
667/*
668 * Test whether a is a subet of b.
669 */
670int v3_asid_subset(ASIdentifiers *a, ASIdentifiers *b)
671{
672 return (a == NULL ||
673 a == b ||
674 (b != NULL &&
675 !v3_asid_inherits(a) &&
676 !v3_asid_inherits(b) &&
677 asid_contains(b->asnum->u.asIdsOrRanges,
678 a->asnum->u.asIdsOrRanges) &&
679 asid_contains(b->rdi->u.asIdsOrRanges,
680 a->rdi->u.asIdsOrRanges)));
681}
682
683/*
684 * Validation error handling via callback.
685 */
686#define validation_err(_err_) \
687 do { \
688 if (ctx != NULL) { \
689 ctx->error = _err_; \
690 ctx->error_depth = i; \
691 ctx->current_cert = x; \
692 ret = ctx->verify_cb(0, ctx); \
693 } else { \
694 ret = 0; \
695 } \
696 if (!ret) \
697 goto done; \
698 } while (0)
699
700/*
701 * Core code for RFC 3779 3.3 path validation.
702 */
703static int v3_asid_validate_path_internal(X509_STORE_CTX *ctx,
704 STACK_OF(X509) *chain,
705 ASIdentifiers *ext)
706{
707 ASIdOrRanges *child_as = NULL, *child_rdi = NULL;
708 int i, ret = 1, inherit_as = 0, inherit_rdi = 0;
709 X509 *x;
710
711 OPENSSL_assert(chain != NULL && sk_X509_num(chain) > 0);
712 OPENSSL_assert(ctx != NULL || ext != NULL);
713 OPENSSL_assert(ctx == NULL || ctx->verify_cb != NULL);
714
715 /*
716 * Figure out where to start. If we don't have an extension to
717 * check, we're done. Otherwise, check canonical form and
718 * set up for walking up the chain.
719 */
720 if (ext != NULL) {
721 i = -1;
722 x = NULL;
723 } else {
724 i = 0;
725 x = sk_X509_value(chain, i);
726 OPENSSL_assert(x != NULL);
727 if ((ext = x->rfc3779_asid) == NULL)
728 goto done;
729 }
730 if (!v3_asid_is_canonical(ext))
731 validation_err(X509_V_ERR_INVALID_EXTENSION);
732 if (ext->asnum != NULL) {
733 switch (ext->asnum->type) {
734 case ASIdentifierChoice_inherit:
735 inherit_as = 1;
736 break;
737 case ASIdentifierChoice_asIdsOrRanges:
738 child_as = ext->asnum->u.asIdsOrRanges;
739 break;
740 }
741 }
742 if (ext->rdi != NULL) {
743 switch (ext->rdi->type) {
744 case ASIdentifierChoice_inherit:
745 inherit_rdi = 1;
746 break;
747 case ASIdentifierChoice_asIdsOrRanges:
748 child_rdi = ext->rdi->u.asIdsOrRanges;
749 break;
750 }
751 }
752
753 /*
754 * Now walk up the chain. Extensions must be in canonical form, no
755 * cert may list resources that its parent doesn't list.
756 */
757 for (i++; i < sk_X509_num(chain); i++) {
758 x = sk_X509_value(chain, i);
759 OPENSSL_assert(x != NULL);
760 if (x->rfc3779_asid == NULL) {
761 if (child_as != NULL || child_rdi != NULL)
762 validation_err(X509_V_ERR_UNNESTED_RESOURCE);
763 continue;
764 }
765 if (!v3_asid_is_canonical(x->rfc3779_asid))
766 validation_err(X509_V_ERR_INVALID_EXTENSION);
767 if (x->rfc3779_asid->asnum == NULL && child_as != NULL) {
768 validation_err(X509_V_ERR_UNNESTED_RESOURCE);
769 child_as = NULL;
770 inherit_as = 0;
771 }
772 if (x->rfc3779_asid->asnum != NULL &&
773 x->rfc3779_asid->asnum->type == ASIdentifierChoice_asIdsOrRanges) {
774 if (inherit_as ||
775 asid_contains(x->rfc3779_asid->asnum->u.asIdsOrRanges, child_as)) {
776 child_as = x->rfc3779_asid->asnum->u.asIdsOrRanges;
777 inherit_as = 0;
778 } else {
779 validation_err(X509_V_ERR_UNNESTED_RESOURCE);
780 }
781 }
782 if (x->rfc3779_asid->rdi == NULL && child_rdi != NULL) {
783 validation_err(X509_V_ERR_UNNESTED_RESOURCE);
784 child_rdi = NULL;
785 inherit_rdi = 0;
786 }
787 if (x->rfc3779_asid->rdi != NULL &&
788 x->rfc3779_asid->rdi->type == ASIdentifierChoice_asIdsOrRanges) {
789 if (inherit_rdi ||
790 asid_contains(x->rfc3779_asid->rdi->u.asIdsOrRanges, child_rdi)) {
791 child_rdi = x->rfc3779_asid->rdi->u.asIdsOrRanges;
792 inherit_rdi = 0;
793 } else {
794 validation_err(X509_V_ERR_UNNESTED_RESOURCE);
795 }
796 }
797 }
798
799 /*
800 * Trust anchor can't inherit.
801 */
802 OPENSSL_assert(x != NULL);
803 if (x->rfc3779_asid != NULL) {
804 if (x->rfc3779_asid->asnum != NULL &&
805 x->rfc3779_asid->asnum->type == ASIdentifierChoice_inherit)
806 validation_err(X509_V_ERR_UNNESTED_RESOURCE);
807 if (x->rfc3779_asid->rdi != NULL &&
808 x->rfc3779_asid->rdi->type == ASIdentifierChoice_inherit)
809 validation_err(X509_V_ERR_UNNESTED_RESOURCE);
810 }
811
812 done:
813 return ret;
814}
815
816#undef validation_err
817
818/*
819 * RFC 3779 3.3 path validation -- called from X509_verify_cert().
820 */
821int v3_asid_validate_path(X509_STORE_CTX *ctx)
822{
823 return v3_asid_validate_path_internal(ctx, ctx->chain, NULL);
824}
825
826/*
827 * RFC 3779 3.3 path validation of an extension.
828 * Test whether chain covers extension.
829 */
830int v3_asid_validate_resource_set(STACK_OF(X509) *chain,
831 ASIdentifiers *ext,
832 int allow_inheritance)
833{
834 if (ext == NULL)
835 return 1;
836 if (chain == NULL || sk_X509_num(chain) == 0)
837 return 0;
838 if (!allow_inheritance && v3_asid_inherits(ext))
839 return 0;
840 return v3_asid_validate_path_internal(NULL, chain, ext);
841}
842
843#endif /* OPENSSL_NO_RFC3779 */