2 * Contributed to the OpenSSL Project by the American Registry for
3 * Internet Numbers ("ARIN").
5 /* ====================================================================
6 * Copyright (c) 2006 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
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 * ====================================================================
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).
59 * Implementation of RFC 3779 section 2.2.
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>
72 #ifndef OPENSSL_NO_RFC3779
75 * OpenSSL ASN.1 template translation of RFC 3779 2.2.3.
78 ASN1_SEQUENCE(IPAddressRange
) = {
79 ASN1_SIMPLE(IPAddressRange
, min
, ASN1_BIT_STRING
),
80 ASN1_SIMPLE(IPAddressRange
, max
, ASN1_BIT_STRING
)
81 } ASN1_SEQUENCE_END(IPAddressRange
)
83 ASN1_CHOICE(IPAddressOrRange
) = {
84 ASN1_SIMPLE(IPAddressOrRange
, u
.addressPrefix
, ASN1_BIT_STRING
),
85 ASN1_SIMPLE(IPAddressOrRange
, u
.addressRange
, IPAddressRange
)
86 } ASN1_CHOICE_END(IPAddressOrRange
)
88 ASN1_CHOICE(IPAddressChoice
) = {
89 ASN1_SIMPLE(IPAddressChoice
, u
.inherit
, ASN1_NULL
),
90 ASN1_SEQUENCE_OF(IPAddressChoice
, u
.addressesOrRanges
, IPAddressOrRange
)
91 } ASN1_CHOICE_END(IPAddressChoice
)
93 ASN1_SEQUENCE(IPAddressFamily
) = {
94 ASN1_SIMPLE(IPAddressFamily
, addressFamily
, ASN1_OCTET_STRING
),
95 ASN1_SIMPLE(IPAddressFamily
, ipAddressChoice
, IPAddressChoice
)
96 } ASN1_SEQUENCE_END(IPAddressFamily
)
98 ASN1_ITEM_TEMPLATE(IPAddrBlocks
) =
99 ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF
, 0,
100 IPAddrBlocks
, IPAddressFamily
)
101 ASN1_ITEM_TEMPLATE_END(IPAddrBlocks
)
103 IMPLEMENT_ASN1_FUNCTIONS(IPAddressRange
)
104 IMPLEMENT_ASN1_FUNCTIONS(IPAddressOrRange
)
105 IMPLEMENT_ASN1_FUNCTIONS(IPAddressChoice
)
106 IMPLEMENT_ASN1_FUNCTIONS(IPAddressFamily
)
109 * How much buffer space do we need for a raw address?
111 #define ADDR_RAW_BUF_LEN 16
114 * What's the address length associated with this AFI?
116 static int length_from_afi(const unsigned afi
)
129 * Extract the AFI from an IPAddressFamily.
131 unsigned int v3_addr_get_afi(const IPAddressFamily
*f
)
133 return ((f
!= NULL
&&
134 f
->addressFamily
!= NULL
&&
135 f
->addressFamily
->data
!= NULL
)
136 ? ((f
->addressFamily
->data
[0] << 8) |
137 (f
->addressFamily
->data
[1]))
142 * Expand the bitstring form of an address into a raw byte array.
143 * At the moment this is coded for simplicity, not speed.
145 static int addr_expand(unsigned char *addr
,
146 const ASN1_BIT_STRING
*bs
,
148 const unsigned char fill
)
150 if (bs
->length
< 0 || bs
->length
> length
)
152 if (bs
->length
> 0) {
153 memcpy(addr
, bs
->data
, bs
->length
);
154 if ((bs
->flags
& 7) != 0) {
155 unsigned char mask
= 0xFF >> (8 - (bs
->flags
& 7));
157 addr
[bs
->length
- 1] &= ~mask
;
159 addr
[bs
->length
- 1] |= mask
;
162 memset(addr
+ bs
->length
, fill
, length
- bs
->length
);
167 * Extract the prefix length from a bitstring.
169 #define addr_prefixlen(bs) ((int) ((bs)->length * 8 - ((bs)->flags & 7)))
172 * i2r handler for one address bitstring.
174 static int i2r_address(BIO
*out
,
176 const unsigned char fill
,
177 const ASN1_BIT_STRING
*bs
)
179 unsigned char addr
[ADDR_RAW_BUF_LEN
];
186 if (!addr_expand(addr
, bs
, 4, fill
))
188 BIO_printf(out
, "%d.%d.%d.%d", addr
[0], addr
[1], addr
[2], addr
[3]);
191 if (!addr_expand(addr
, bs
, 16, fill
))
193 for (n
= 16; n
> 1 && addr
[n
-1] == 0x00 && addr
[n
-2] == 0x00; n
-= 2)
195 for (i
= 0; i
< n
; i
+= 2)
196 BIO_printf(out
, "%x%s", (addr
[i
] << 8) | addr
[i
+1], (i
< 14 ? ":" : ""));
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));
212 * i2r handler for a sequence of addresses and ranges.
214 static int i2r_IPAddressOrRanges(BIO
*out
,
216 const IPAddressOrRanges
*aors
,
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
, "");
224 case IPAddressOrRange_addressPrefix
:
225 if (!i2r_address(out
, afi
, 0x00, aor
->u
.addressPrefix
))
227 BIO_printf(out
, "/%d\n", addr_prefixlen(aor
->u
.addressPrefix
));
229 case IPAddressOrRange_addressRange
:
230 if (!i2r_address(out
, afi
, 0x00, aor
->u
.addressRange
->min
))
233 if (!i2r_address(out
, afi
, 0xFF, aor
->u
.addressRange
->max
))
243 * i2r handler for an IPAddrBlocks extension.
245 static int i2r_IPAddrBlocks(const X509V3_EXT_METHOD
*method
,
250 const IPAddrBlocks
*addr
= ext
;
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
);
257 BIO_printf(out
, "%*sIPv4", indent
, "");
260 BIO_printf(out
, "%*sIPv6", indent
, "");
263 BIO_printf(out
, "%*sUnknown AFI %u", indent
, "", afi
);
266 if (f
->addressFamily
->length
> 2) {
267 switch (f
->addressFamily
->data
[2]) {
269 BIO_puts(out
, " (Unicast)");
272 BIO_puts(out
, " (Multicast)");
275 BIO_puts(out
, " (Unicast/Multicast)");
278 BIO_puts(out
, " (MPLS)");
281 BIO_puts(out
, " (Tunnel)");
284 BIO_puts(out
, " (VPLS)");
287 BIO_puts(out
, " (BGP MDT)");
290 BIO_puts(out
, " (MPLS-labeled VPN)");
293 BIO_printf(out
, " (Unknown SAFI %u)",
294 (unsigned) f
->addressFamily
->data
[2]);
298 switch (f
->ipAddressChoice
->type
) {
299 case IPAddressChoice_inherit
:
300 BIO_puts(out
, ": inherit\n");
302 case IPAddressChoice_addressesOrRanges
:
303 BIO_puts(out
, ":\n");
304 if (!i2r_IPAddressOrRanges(out
,
306 f
->ipAddressChoice
->u
.addressesOrRanges
,
316 * Sort comparison function for a sequence of IPAddressOrRange
319 * There's no sane answer we can give if addr_expand() fails, and an
320 * assertion failure on externally supplied data is seriously uncool,
321 * so we just arbitrarily declare that if given invalid inputs this
322 * function returns -1. If this messes up your preferred sort order
323 * for garbage input, tough noogies.
325 static int IPAddressOrRange_cmp(const IPAddressOrRange
*a
,
326 const IPAddressOrRange
*b
,
329 unsigned char addr_a
[ADDR_RAW_BUF_LEN
], addr_b
[ADDR_RAW_BUF_LEN
];
330 int prefixlen_a
= 0, prefixlen_b
= 0;
334 case IPAddressOrRange_addressPrefix
:
335 if (!addr_expand(addr_a
, a
->u
.addressPrefix
, length
, 0x00))
337 prefixlen_a
= addr_prefixlen(a
->u
.addressPrefix
);
339 case IPAddressOrRange_addressRange
:
340 if (!addr_expand(addr_a
, a
->u
.addressRange
->min
, length
, 0x00))
342 prefixlen_a
= length
* 8;
347 case IPAddressOrRange_addressPrefix
:
348 if (!addr_expand(addr_b
, b
->u
.addressPrefix
, length
, 0x00))
350 prefixlen_b
= addr_prefixlen(b
->u
.addressPrefix
);
352 case IPAddressOrRange_addressRange
:
353 if (!addr_expand(addr_b
, b
->u
.addressRange
->min
, length
, 0x00))
355 prefixlen_b
= length
* 8;
359 if ((r
= memcmp(addr_a
, addr_b
, length
)) != 0)
362 return prefixlen_a
- prefixlen_b
;
366 * IPv4-specific closure over IPAddressOrRange_cmp, since sk_sort()
367 * comparision routines are only allowed two arguments.
369 static int v4IPAddressOrRange_cmp(const IPAddressOrRange
* const *a
,
370 const IPAddressOrRange
* const *b
)
372 return IPAddressOrRange_cmp(*a
, *b
, 4);
376 * IPv6-specific closure over IPAddressOrRange_cmp, since sk_sort()
377 * comparision routines are only allowed two arguments.
379 static int v6IPAddressOrRange_cmp(const IPAddressOrRange
* const *a
,
380 const IPAddressOrRange
* const *b
)
382 return IPAddressOrRange_cmp(*a
, *b
, 16);
386 * Calculate whether a range collapses to a prefix.
387 * See last paragraph of RFC 3779 2.2.3.7.
389 static int range_should_be_prefix(const unsigned char *min
,
390 const unsigned char *max
,
396 OPENSSL_assert(memcmp(min
, max
, length
) <= 0);
397 for (i
= 0; i
< length
&& min
[i
] == max
[i
]; i
++)
399 for (j
= length
- 1; j
>= 0 && min
[j
] == 0x00 && max
[j
] == 0xFF; j
--)
405 mask
= min
[i
] ^ max
[i
];
407 case 0x01: j
= 7; break;
408 case 0x03: j
= 6; break;
409 case 0x07: j
= 5; break;
410 case 0x0F: j
= 4; break;
411 case 0x1F: j
= 3; break;
412 case 0x3F: j
= 2; break;
413 case 0x7F: j
= 1; break;
416 if ((min
[i
] & mask
) != 0 || (max
[i
] & mask
) != mask
)
423 * Construct a prefix.
425 static int make_addressPrefix(IPAddressOrRange
**result
,
429 int bytelen
= (prefixlen
+ 7) / 8, bitlen
= prefixlen
% 8;
430 IPAddressOrRange
*aor
= IPAddressOrRange_new();
434 aor
->type
= IPAddressOrRange_addressPrefix
;
435 if (aor
->u
.addressPrefix
== NULL
&&
436 (aor
->u
.addressPrefix
= ASN1_BIT_STRING_new()) == NULL
)
438 if (!ASN1_BIT_STRING_set(aor
->u
.addressPrefix
, addr
, bytelen
))
440 aor
->u
.addressPrefix
->flags
&= ~7;
441 aor
->u
.addressPrefix
->flags
|= ASN1_STRING_FLAG_BITS_LEFT
;
443 aor
->u
.addressPrefix
->data
[bytelen
- 1] &= ~(0xFF >> bitlen
);
444 aor
->u
.addressPrefix
->flags
|= 8 - bitlen
;
451 IPAddressOrRange_free(aor
);
456 * Construct a range. If it can be expressed as a prefix,
457 * return a prefix instead. Doing this here simplifies
458 * the rest of the code considerably.
460 static int make_addressRange(IPAddressOrRange
**result
,
465 IPAddressOrRange
*aor
;
468 if ((prefixlen
= range_should_be_prefix(min
, max
, length
)) >= 0)
469 return make_addressPrefix(result
, min
, prefixlen
);
471 if ((aor
= IPAddressOrRange_new()) == NULL
)
473 aor
->type
= IPAddressOrRange_addressRange
;
474 OPENSSL_assert(aor
->u
.addressRange
== NULL
);
475 if ((aor
->u
.addressRange
= IPAddressRange_new()) == NULL
)
477 if (aor
->u
.addressRange
->min
== NULL
&&
478 (aor
->u
.addressRange
->min
= ASN1_BIT_STRING_new()) == NULL
)
480 if (aor
->u
.addressRange
->max
== NULL
&&
481 (aor
->u
.addressRange
->max
= ASN1_BIT_STRING_new()) == NULL
)
484 for (i
= length
; i
> 0 && min
[i
- 1] == 0x00; --i
)
486 if (!ASN1_BIT_STRING_set(aor
->u
.addressRange
->min
, min
, i
))
488 aor
->u
.addressRange
->min
->flags
&= ~7;
489 aor
->u
.addressRange
->min
->flags
|= ASN1_STRING_FLAG_BITS_LEFT
;
491 unsigned char b
= min
[i
- 1];
493 while ((b
& (0xFFU
>> j
)) != 0)
495 aor
->u
.addressRange
->min
->flags
|= 8 - j
;
498 for (i
= length
; i
> 0 && max
[i
- 1] == 0xFF; --i
)
500 if (!ASN1_BIT_STRING_set(aor
->u
.addressRange
->max
, max
, i
))
502 aor
->u
.addressRange
->max
->flags
&= ~7;
503 aor
->u
.addressRange
->max
->flags
|= ASN1_STRING_FLAG_BITS_LEFT
;
505 unsigned char b
= max
[i
- 1];
507 while ((b
& (0xFFU
>> j
)) != (0xFFU
>> j
))
509 aor
->u
.addressRange
->max
->flags
|= 8 - j
;
516 IPAddressOrRange_free(aor
);
521 * Construct a new address family or find an existing one.
523 static IPAddressFamily
*make_IPAddressFamily(IPAddrBlocks
*addr
,
525 const unsigned *safi
)
528 unsigned char key
[3];
532 key
[0] = (afi
>> 8) & 0xFF;
535 key
[2] = *safi
& 0xFF;
541 for (i
= 0; i
< sk_IPAddressFamily_num(addr
); i
++) {
542 f
= sk_IPAddressFamily_value(addr
, i
);
543 OPENSSL_assert(f
->addressFamily
->data
!= NULL
);
544 if (f
->addressFamily
->length
== keylen
&&
545 !memcmp(f
->addressFamily
->data
, key
, keylen
))
549 if ((f
= IPAddressFamily_new()) == NULL
)
551 if (f
->ipAddressChoice
== NULL
&&
552 (f
->ipAddressChoice
= IPAddressChoice_new()) == NULL
)
554 if (f
->addressFamily
== NULL
&&
555 (f
->addressFamily
= ASN1_OCTET_STRING_new()) == NULL
)
557 if (!ASN1_OCTET_STRING_set(f
->addressFamily
, key
, keylen
))
559 if (!sk_IPAddressFamily_push(addr
, f
))
565 IPAddressFamily_free(f
);
570 * Add an inheritance element.
572 int v3_addr_add_inherit(IPAddrBlocks
*addr
,
574 const unsigned *safi
)
576 IPAddressFamily
*f
= make_IPAddressFamily(addr
, afi
, safi
);
578 f
->ipAddressChoice
== NULL
||
579 (f
->ipAddressChoice
->type
== IPAddressChoice_addressesOrRanges
&&
580 f
->ipAddressChoice
->u
.addressesOrRanges
!= NULL
))
582 if (f
->ipAddressChoice
->type
== IPAddressChoice_inherit
&&
583 f
->ipAddressChoice
->u
.inherit
!= NULL
)
585 if (f
->ipAddressChoice
->u
.inherit
== NULL
&&
586 (f
->ipAddressChoice
->u
.inherit
= ASN1_NULL_new()) == NULL
)
588 f
->ipAddressChoice
->type
= IPAddressChoice_inherit
;
593 * Construct an IPAddressOrRange sequence, or return an existing one.
595 static IPAddressOrRanges
*make_prefix_or_range(IPAddrBlocks
*addr
,
597 const unsigned *safi
)
599 IPAddressFamily
*f
= make_IPAddressFamily(addr
, afi
, safi
);
600 IPAddressOrRanges
*aors
= NULL
;
603 f
->ipAddressChoice
== NULL
||
604 (f
->ipAddressChoice
->type
== IPAddressChoice_inherit
&&
605 f
->ipAddressChoice
->u
.inherit
!= NULL
))
607 if (f
->ipAddressChoice
->type
== IPAddressChoice_addressesOrRanges
)
608 aors
= f
->ipAddressChoice
->u
.addressesOrRanges
;
611 if ((aors
= sk_IPAddressOrRange_new_null()) == NULL
)
615 (void) sk_IPAddressOrRange_set_cmp_func(aors
, v4IPAddressOrRange_cmp
);
618 (void) sk_IPAddressOrRange_set_cmp_func(aors
, v6IPAddressOrRange_cmp
);
621 f
->ipAddressChoice
->type
= IPAddressChoice_addressesOrRanges
;
622 f
->ipAddressChoice
->u
.addressesOrRanges
= aors
;
629 int v3_addr_add_prefix(IPAddrBlocks
*addr
,
631 const unsigned *safi
,
635 IPAddressOrRanges
*aors
= make_prefix_or_range(addr
, afi
, safi
);
636 IPAddressOrRange
*aor
;
637 if (aors
== NULL
|| !make_addressPrefix(&aor
, a
, prefixlen
))
639 if (sk_IPAddressOrRange_push(aors
, aor
))
641 IPAddressOrRange_free(aor
);
648 int v3_addr_add_range(IPAddrBlocks
*addr
,
650 const unsigned *safi
,
654 IPAddressOrRanges
*aors
= make_prefix_or_range(addr
, afi
, safi
);
655 IPAddressOrRange
*aor
;
656 int length
= length_from_afi(afi
);
659 if (!make_addressRange(&aor
, min
, max
, length
))
661 if (sk_IPAddressOrRange_push(aors
, aor
))
663 IPAddressOrRange_free(aor
);
668 * Extract min and max values from an IPAddressOrRange.
670 static int extract_min_max(IPAddressOrRange
*aor
,
675 if (aor
== NULL
|| min
== NULL
|| max
== NULL
)
678 case IPAddressOrRange_addressPrefix
:
679 return (addr_expand(min
, aor
->u
.addressPrefix
, length
, 0x00) &&
680 addr_expand(max
, aor
->u
.addressPrefix
, length
, 0xFF));
681 case IPAddressOrRange_addressRange
:
682 return (addr_expand(min
, aor
->u
.addressRange
->min
, length
, 0x00) &&
683 addr_expand(max
, aor
->u
.addressRange
->max
, length
, 0xFF));
689 * Public wrapper for extract_min_max().
691 int v3_addr_get_range(IPAddressOrRange
*aor
,
697 int afi_length
= length_from_afi(afi
);
698 if (aor
== NULL
|| min
== NULL
|| max
== NULL
||
699 afi_length
== 0 || length
< afi_length
||
700 (aor
->type
!= IPAddressOrRange_addressPrefix
&&
701 aor
->type
!= IPAddressOrRange_addressRange
) ||
702 !extract_min_max(aor
, min
, max
, afi_length
))
709 * Sort comparision function for a sequence of IPAddressFamily.
711 * The last paragraph of RFC 3779 2.2.3.3 is slightly ambiguous about
712 * the ordering: I can read it as meaning that IPv6 without a SAFI
713 * comes before IPv4 with a SAFI, which seems pretty weird. The
714 * examples in appendix B suggest that the author intended the
715 * null-SAFI rule to apply only within a single AFI, which is what I
716 * would have expected and is what the following code implements.
718 static int IPAddressFamily_cmp(const IPAddressFamily
* const *a_
,
719 const IPAddressFamily
* const *b_
)
721 const ASN1_OCTET_STRING
*a
= (*a_
)->addressFamily
;
722 const ASN1_OCTET_STRING
*b
= (*b_
)->addressFamily
;
723 int len
= ((a
->length
<= b
->length
) ? a
->length
: b
->length
);
724 int cmp
= memcmp(a
->data
, b
->data
, len
);
725 return cmp
? cmp
: a
->length
- b
->length
;
729 * Check whether an IPAddrBLocks is in canonical form.
731 int v3_addr_is_canonical(IPAddrBlocks
*addr
)
733 unsigned char a_min
[ADDR_RAW_BUF_LEN
], a_max
[ADDR_RAW_BUF_LEN
];
734 unsigned char b_min
[ADDR_RAW_BUF_LEN
], b_max
[ADDR_RAW_BUF_LEN
];
735 IPAddressOrRanges
*aors
;
739 * Empty extension is cannonical.
745 * Check whether the top-level list is in order.
747 for (i
= 0; i
< sk_IPAddressFamily_num(addr
) - 1; i
++) {
748 const IPAddressFamily
*a
= sk_IPAddressFamily_value(addr
, i
);
749 const IPAddressFamily
*b
= sk_IPAddressFamily_value(addr
, i
+ 1);
750 if (IPAddressFamily_cmp(&a
, &b
) >= 0)
755 * Top level's ok, now check each address family.
757 for (i
= 0; i
< sk_IPAddressFamily_num(addr
); i
++) {
758 IPAddressFamily
*f
= sk_IPAddressFamily_value(addr
, i
);
759 int length
= length_from_afi(v3_addr_get_afi(f
));
762 * Inheritance is canonical. Anything other than inheritance or
763 * a SEQUENCE OF IPAddressOrRange is an ASN.1 error or something.
765 if (f
== NULL
|| f
->ipAddressChoice
== NULL
)
767 switch (f
->ipAddressChoice
->type
) {
768 case IPAddressChoice_inherit
:
770 case IPAddressChoice_addressesOrRanges
:
777 * It's an IPAddressOrRanges sequence, check it.
779 aors
= f
->ipAddressChoice
->u
.addressesOrRanges
;
780 if (sk_IPAddressOrRange_num(aors
) == 0)
782 for (j
= 0; j
< sk_IPAddressOrRange_num(aors
) - 1; j
++) {
783 IPAddressOrRange
*a
= sk_IPAddressOrRange_value(aors
, j
);
784 IPAddressOrRange
*b
= sk_IPAddressOrRange_value(aors
, j
+ 1);
786 if (!extract_min_max(a
, a_min
, a_max
, length
) ||
787 !extract_min_max(b
, b_min
, b_max
, length
))
791 * Punt misordered list, overlapping start, or inverted range.
793 if (memcmp(a_min
, b_min
, length
) >= 0 ||
794 memcmp(a_min
, a_max
, length
) > 0 ||
795 memcmp(b_min
, b_max
, length
) > 0)
799 * Punt if adjacent or overlapping. Check for adjacency by
800 * subtracting one from b_min first.
802 for (k
= length
- 1; k
>= 0 && b_min
[k
]-- == 0x00; k
--)
804 if (memcmp(a_max
, b_min
, length
) >= 0)
808 * Check for range that should be expressed as a prefix.
810 if (a
->type
== IPAddressOrRange_addressRange
&&
811 range_should_be_prefix(a_min
, a_max
, length
) >= 0)
816 * Check range to see if it's inverted or should be a
819 j
= sk_IPAddressOrRange_num(aors
) - 1;
821 IPAddressOrRange
*a
= sk_IPAddressOrRange_value(aors
, j
);
822 if (a
!= NULL
&& a
->type
== IPAddressOrRange_addressRange
) {
823 if (!extract_min_max(a
, a_min
, a_max
, length
))
825 if (memcmp(a_min
, a_max
, length
) > 0 ||
826 range_should_be_prefix(a_min
, a_max
, length
) >= 0)
833 * If we made it through all that, we're happy.
839 * Whack an IPAddressOrRanges into canonical form.
841 static int IPAddressOrRanges_canonize(IPAddressOrRanges
*aors
,
844 int i
, j
, length
= length_from_afi(afi
);
847 * Sort the IPAddressOrRanges sequence.
849 sk_IPAddressOrRange_sort(aors
);
852 * Clean up representation issues, punt on duplicates or overlaps.
854 for (i
= 0; i
< sk_IPAddressOrRange_num(aors
) - 1; i
++) {
855 IPAddressOrRange
*a
= sk_IPAddressOrRange_value(aors
, i
);
856 IPAddressOrRange
*b
= sk_IPAddressOrRange_value(aors
, i
+ 1);
857 unsigned char a_min
[ADDR_RAW_BUF_LEN
], a_max
[ADDR_RAW_BUF_LEN
];
858 unsigned char b_min
[ADDR_RAW_BUF_LEN
], b_max
[ADDR_RAW_BUF_LEN
];
860 if (!extract_min_max(a
, a_min
, a_max
, length
) ||
861 !extract_min_max(b
, b_min
, b_max
, length
))
865 * Punt inverted ranges.
867 if (memcmp(a_min
, a_max
, length
) > 0 ||
868 memcmp(b_min
, b_max
, length
) > 0)
874 if (memcmp(a_max
, b_min
, length
) >= 0)
878 * Merge if a and b are adjacent. We check for
879 * adjacency by subtracting one from b_min first.
881 for (j
= length
- 1; j
>= 0 && b_min
[j
]-- == 0x00; j
--)
883 if (memcmp(a_max
, b_min
, length
) == 0) {
884 IPAddressOrRange
*merged
;
885 if (!make_addressRange(&merged
, a_min
, b_max
, length
))
887 (void) sk_IPAddressOrRange_set(aors
, i
, merged
);
888 (void) sk_IPAddressOrRange_delete(aors
, i
+ 1);
889 IPAddressOrRange_free(a
);
890 IPAddressOrRange_free(b
);
897 * Check for inverted final range.
899 j
= sk_IPAddressOrRange_num(aors
) - 1;
901 IPAddressOrRange
*a
= sk_IPAddressOrRange_value(aors
, j
);
902 if (a
!= NULL
&& a
->type
== IPAddressOrRange_addressRange
) {
903 unsigned char a_min
[ADDR_RAW_BUF_LEN
], a_max
[ADDR_RAW_BUF_LEN
];
904 extract_min_max(a
, a_min
, a_max
, length
);
905 if (memcmp(a_min
, a_max
, length
) > 0)
914 * Whack an IPAddrBlocks extension into canonical form.
916 int v3_addr_canonize(IPAddrBlocks
*addr
)
919 for (i
= 0; i
< sk_IPAddressFamily_num(addr
); i
++) {
920 IPAddressFamily
*f
= sk_IPAddressFamily_value(addr
, i
);
921 if (f
->ipAddressChoice
->type
== IPAddressChoice_addressesOrRanges
&&
922 !IPAddressOrRanges_canonize(f
->ipAddressChoice
->u
.addressesOrRanges
,
926 (void) sk_IPAddressFamily_set_cmp_func(addr
, IPAddressFamily_cmp
);
927 sk_IPAddressFamily_sort(addr
);
928 OPENSSL_assert(v3_addr_is_canonical(addr
));
933 * v2i handler for the IPAddrBlocks extension.
935 static void *v2i_IPAddrBlocks(const struct v3_ext_method
*method
,
936 struct v3_ext_ctx
*ctx
,
937 STACK_OF(CONF_VALUE
) *values
)
939 static const char v4addr_chars
[] = "0123456789.";
940 static const char v6addr_chars
[] = "0123456789.:abcdefABCDEF";
941 IPAddrBlocks
*addr
= NULL
;
945 if ((addr
= sk_IPAddressFamily_new(IPAddressFamily_cmp
)) == NULL
) {
946 X509V3err(X509V3_F_V2I_IPADDRBLOCKS
, ERR_R_MALLOC_FAILURE
);
950 for (i
= 0; i
< sk_CONF_VALUE_num(values
); i
++) {
951 CONF_VALUE
*val
= sk_CONF_VALUE_value(values
, i
);
952 unsigned char min
[ADDR_RAW_BUF_LEN
], max
[ADDR_RAW_BUF_LEN
];
953 unsigned afi
, *safi
= NULL
, safi_
;
954 const char *addr_chars
;
955 int prefixlen
, i1
, i2
, delim
, length
;
957 if ( !name_cmp(val
->name
, "IPv4")) {
959 } else if (!name_cmp(val
->name
, "IPv6")) {
961 } else if (!name_cmp(val
->name
, "IPv4-SAFI")) {
964 } else if (!name_cmp(val
->name
, "IPv6-SAFI")) {
968 X509V3err(X509V3_F_V2I_IPADDRBLOCKS
, X509V3_R_EXTENSION_NAME_ERROR
);
969 X509V3_conf_err(val
);
975 addr_chars
= v4addr_chars
;
978 addr_chars
= v6addr_chars
;
982 length
= length_from_afi(afi
);
985 * Handle SAFI, if any, and BUF_strdup() so we can null-terminate
986 * the other input values.
989 *safi
= strtoul(val
->value
, &t
, 0);
990 t
+= strspn(t
, " \t");
991 if (*safi
> 0xFF || *t
++ != ':') {
992 X509V3err(X509V3_F_V2I_IPADDRBLOCKS
, X509V3_R_INVALID_SAFI
);
993 X509V3_conf_err(val
);
996 t
+= strspn(t
, " \t");
999 s
= BUF_strdup(val
->value
);
1002 X509V3err(X509V3_F_V2I_IPADDRBLOCKS
, ERR_R_MALLOC_FAILURE
);
1007 * Check for inheritance. Not worth additional complexity to
1008 * optimize this (seldom-used) case.
1010 if (!strcmp(s
, "inherit")) {
1011 if (!v3_addr_add_inherit(addr
, afi
, safi
)) {
1012 X509V3err(X509V3_F_V2I_IPADDRBLOCKS
, X509V3_R_INVALID_INHERITANCE
);
1013 X509V3_conf_err(val
);
1021 i1
= strspn(s
, addr_chars
);
1022 i2
= i1
+ strspn(s
+ i1
, " \t");
1026 if (a2i_ipadd(min
, s
) != length
) {
1027 X509V3err(X509V3_F_V2I_IPADDRBLOCKS
, X509V3_R_INVALID_IPADDRESS
);
1028 X509V3_conf_err(val
);
1034 prefixlen
= (int) strtoul(s
+ i2
, &t
, 10);
1035 if (t
== s
+ i2
|| *t
!= '\0') {
1036 X509V3err(X509V3_F_V2I_IPADDRBLOCKS
, X509V3_R_EXTENSION_VALUE_ERROR
);
1037 X509V3_conf_err(val
);
1040 if (!v3_addr_add_prefix(addr
, afi
, safi
, min
, prefixlen
)) {
1041 X509V3err(X509V3_F_V2I_IPADDRBLOCKS
, ERR_R_MALLOC_FAILURE
);
1046 i1
= i2
+ strspn(s
+ i2
, " \t");
1047 i2
= i1
+ strspn(s
+ i1
, addr_chars
);
1048 if (i1
== i2
|| s
[i2
] != '\0') {
1049 X509V3err(X509V3_F_V2I_IPADDRBLOCKS
, X509V3_R_EXTENSION_VALUE_ERROR
);
1050 X509V3_conf_err(val
);
1053 if (a2i_ipadd(max
, s
+ i1
) != length
) {
1054 X509V3err(X509V3_F_V2I_IPADDRBLOCKS
, X509V3_R_INVALID_IPADDRESS
);
1055 X509V3_conf_err(val
);
1058 if (memcmp(min
, max
, length_from_afi(afi
)) > 0) {
1059 X509V3err(X509V3_F_V2I_IPADDRBLOCKS
, X509V3_R_EXTENSION_VALUE_ERROR
);
1060 X509V3_conf_err(val
);
1063 if (!v3_addr_add_range(addr
, afi
, safi
, min
, max
)) {
1064 X509V3err(X509V3_F_V2I_IPADDRBLOCKS
, ERR_R_MALLOC_FAILURE
);
1069 if (!v3_addr_add_prefix(addr
, afi
, safi
, min
, length
* 8)) {
1070 X509V3err(X509V3_F_V2I_IPADDRBLOCKS
, ERR_R_MALLOC_FAILURE
);
1075 X509V3err(X509V3_F_V2I_IPADDRBLOCKS
, X509V3_R_EXTENSION_VALUE_ERROR
);
1076 X509V3_conf_err(val
);
1085 * Canonize the result, then we're done.
1087 if (!v3_addr_canonize(addr
))
1093 sk_IPAddressFamily_pop_free(addr
, IPAddressFamily_free
);
1100 const X509V3_EXT_METHOD v3_addr
= {
1101 NID_sbgp_ipAddrBlock
, /* nid */
1103 ASN1_ITEM_ref(IPAddrBlocks
), /* template */
1104 0, 0, 0, 0, /* old functions, ignored */
1108 v2i_IPAddrBlocks
, /* v2i */
1109 i2r_IPAddrBlocks
, /* i2r */
1111 NULL
/* extension-specific data */
1115 * Figure out whether extension sues inheritance.
1117 int v3_addr_inherits(IPAddrBlocks
*addr
)
1122 for (i
= 0; i
< sk_IPAddressFamily_num(addr
); i
++) {
1123 IPAddressFamily
*f
= sk_IPAddressFamily_value(addr
, i
);
1124 if (f
->ipAddressChoice
->type
== IPAddressChoice_inherit
)
1131 * Figure out whether parent contains child.
1133 static int addr_contains(IPAddressOrRanges
*parent
,
1134 IPAddressOrRanges
*child
,
1137 unsigned char p_min
[ADDR_RAW_BUF_LEN
], p_max
[ADDR_RAW_BUF_LEN
];
1138 unsigned char c_min
[ADDR_RAW_BUF_LEN
], c_max
[ADDR_RAW_BUF_LEN
];
1141 if (child
== NULL
|| parent
== child
)
1147 for (c
= 0; c
< sk_IPAddressOrRange_num(child
); c
++) {
1148 if (!extract_min_max(sk_IPAddressOrRange_value(child
, c
),
1149 c_min
, c_max
, length
))
1152 if (p
>= sk_IPAddressOrRange_num(parent
))
1154 if (!extract_min_max(sk_IPAddressOrRange_value(parent
, p
),
1155 p_min
, p_max
, length
))
1157 if (memcmp(p_max
, c_max
, length
) < 0)
1159 if (memcmp(p_min
, c_min
, length
) > 0)
1169 * Test whether a is a subset of b.
1171 int v3_addr_subset(IPAddrBlocks
*a
, IPAddrBlocks
*b
)
1174 if (a
== NULL
|| a
== b
)
1176 if (b
== NULL
|| v3_addr_inherits(a
) || v3_addr_inherits(b
))
1178 (void) sk_IPAddressFamily_set_cmp_func(b
, IPAddressFamily_cmp
);
1179 for (i
= 0; i
< sk_IPAddressFamily_num(a
); i
++) {
1180 IPAddressFamily
*fa
= sk_IPAddressFamily_value(a
, i
);
1181 int j
= sk_IPAddressFamily_find(b
, fa
);
1182 IPAddressFamily
*fb
;
1183 fb
= sk_IPAddressFamily_value(b
, j
);
1186 if (!addr_contains(fb
->ipAddressChoice
->u
.addressesOrRanges
,
1187 fa
->ipAddressChoice
->u
.addressesOrRanges
,
1188 length_from_afi(v3_addr_get_afi(fb
))))
1195 * Validation error handling via callback.
1197 #define validation_err(_err_) \
1199 if (ctx != NULL) { \
1200 ctx->error = _err_; \
1201 ctx->error_depth = i; \
1202 ctx->current_cert = x; \
1203 ret = ctx->verify_cb(0, ctx); \
1212 * Core code for RFC 3779 2.3 path validation.
1214 static int v3_addr_validate_path_internal(X509_STORE_CTX
*ctx
,
1215 STACK_OF(X509
) *chain
,
1218 IPAddrBlocks
*child
= NULL
;
1222 OPENSSL_assert(chain
!= NULL
&& sk_X509_num(chain
) > 0);
1223 OPENSSL_assert(ctx
!= NULL
|| ext
!= NULL
);
1224 OPENSSL_assert(ctx
== NULL
|| ctx
->verify_cb
!= NULL
);
1227 * Figure out where to start. If we don't have an extension to
1228 * check, we're done. Otherwise, check canonical form and
1229 * set up for walking up the chain.
1236 x
= sk_X509_value(chain
, i
);
1237 OPENSSL_assert(x
!= NULL
);
1238 if ((ext
= x
->rfc3779_addr
) == NULL
)
1241 if (!v3_addr_is_canonical(ext
))
1242 validation_err(X509_V_ERR_INVALID_EXTENSION
);
1243 (void) sk_IPAddressFamily_set_cmp_func(ext
, IPAddressFamily_cmp
);
1244 if ((child
= sk_IPAddressFamily_dup(ext
)) == NULL
) {
1245 X509V3err(X509V3_F_V3_ADDR_VALIDATE_PATH_INTERNAL
, ERR_R_MALLOC_FAILURE
);
1251 * Now walk up the chain. No cert may list resources that its
1252 * parent doesn't list.
1254 for (i
++; i
< sk_X509_num(chain
); i
++) {
1255 x
= sk_X509_value(chain
, i
);
1256 OPENSSL_assert(x
!= NULL
);
1257 if (!v3_addr_is_canonical(x
->rfc3779_addr
))
1258 validation_err(X509_V_ERR_INVALID_EXTENSION
);
1259 if (x
->rfc3779_addr
== NULL
) {
1260 for (j
= 0; j
< sk_IPAddressFamily_num(child
); j
++) {
1261 IPAddressFamily
*fc
= sk_IPAddressFamily_value(child
, j
);
1262 if (fc
->ipAddressChoice
->type
!= IPAddressChoice_inherit
) {
1263 validation_err(X509_V_ERR_UNNESTED_RESOURCE
);
1269 (void) sk_IPAddressFamily_set_cmp_func(x
->rfc3779_addr
, IPAddressFamily_cmp
);
1270 for (j
= 0; j
< sk_IPAddressFamily_num(child
); j
++) {
1271 IPAddressFamily
*fc
= sk_IPAddressFamily_value(child
, j
);
1272 int k
= sk_IPAddressFamily_find(x
->rfc3779_addr
, fc
);
1273 IPAddressFamily
*fp
= sk_IPAddressFamily_value(x
->rfc3779_addr
, k
);
1275 if (fc
->ipAddressChoice
->type
== IPAddressChoice_addressesOrRanges
) {
1276 validation_err(X509_V_ERR_UNNESTED_RESOURCE
);
1281 if (fp
->ipAddressChoice
->type
== IPAddressChoice_addressesOrRanges
) {
1282 if (fc
->ipAddressChoice
->type
== IPAddressChoice_inherit
||
1283 addr_contains(fp
->ipAddressChoice
->u
.addressesOrRanges
,
1284 fc
->ipAddressChoice
->u
.addressesOrRanges
,
1285 length_from_afi(v3_addr_get_afi(fc
))))
1286 sk_IPAddressFamily_set(child
, j
, fp
);
1288 validation_err(X509_V_ERR_UNNESTED_RESOURCE
);
1294 * Trust anchor can't inherit.
1296 OPENSSL_assert(x
!= NULL
);
1297 if (x
->rfc3779_addr
!= NULL
) {
1298 for (j
= 0; j
< sk_IPAddressFamily_num(x
->rfc3779_addr
); j
++) {
1299 IPAddressFamily
*fp
= sk_IPAddressFamily_value(x
->rfc3779_addr
, j
);
1300 if (fp
->ipAddressChoice
->type
== IPAddressChoice_inherit
&&
1301 sk_IPAddressFamily_find(child
, fp
) >= 0)
1302 validation_err(X509_V_ERR_UNNESTED_RESOURCE
);
1307 sk_IPAddressFamily_free(child
);
1311 #undef validation_err
1314 * RFC 3779 2.3 path validation -- called from X509_verify_cert().
1316 int v3_addr_validate_path(X509_STORE_CTX
*ctx
)
1318 return v3_addr_validate_path_internal(ctx
, ctx
->chain
, NULL
);
1322 * RFC 3779 2.3 path validation of an extension.
1323 * Test whether chain covers extension.
1325 int v3_addr_validate_resource_set(STACK_OF(X509
) *chain
,
1327 int allow_inheritance
)
1331 if (chain
== NULL
|| sk_X509_num(chain
) == 0)
1333 if (!allow_inheritance
&& v3_addr_inherits(ext
))
1335 return v3_addr_validate_path_internal(NULL
, chain
, ext
);
1338 #endif /* OPENSSL_NO_RFC3779 */