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
&& f
->addressFamily
->data
!= NULL
)
135 ? ((f
->addressFamily
->data
[0] << 8) | (f
->addressFamily
->data
[1]))
140 * Expand the bitstring form of an address into a raw byte array.
141 * At the moment this is coded for simplicity, not speed.
143 static int addr_expand(unsigned char *addr
,
144 const ASN1_BIT_STRING
*bs
,
145 const int length
, const unsigned char fill
)
147 if (bs
->length
< 0 || bs
->length
> length
)
149 if (bs
->length
> 0) {
150 memcpy(addr
, bs
->data
, bs
->length
);
151 if ((bs
->flags
& 7) != 0) {
152 unsigned char mask
= 0xFF >> (8 - (bs
->flags
& 7));
154 addr
[bs
->length
- 1] &= ~mask
;
156 addr
[bs
->length
- 1] |= mask
;
159 memset(addr
+ bs
->length
, fill
, length
- bs
->length
);
164 * Extract the prefix length from a bitstring.
166 # define addr_prefixlen(bs) ((int) ((bs)->length * 8 - ((bs)->flags & 7)))
169 * i2r handler for one address bitstring.
171 static int i2r_address(BIO
*out
,
173 const unsigned char fill
, const ASN1_BIT_STRING
*bs
)
175 unsigned char addr
[ADDR_RAW_BUF_LEN
];
182 if (!addr_expand(addr
, bs
, 4, fill
))
184 BIO_printf(out
, "%d.%d.%d.%d", addr
[0], addr
[1], addr
[2], addr
[3]);
187 if (!addr_expand(addr
, bs
, 16, fill
))
189 for (n
= 16; n
> 1 && addr
[n
- 1] == 0x00 && addr
[n
- 2] == 0x00;
191 for (i
= 0; i
< n
; i
+= 2)
192 BIO_printf(out
, "%x%s", (addr
[i
] << 8) | addr
[i
+ 1],
193 (i
< 14 ? ":" : ""));
200 for (i
= 0; i
< bs
->length
; i
++)
201 BIO_printf(out
, "%s%02x", (i
> 0 ? ":" : ""), bs
->data
[i
]);
202 BIO_printf(out
, "[%d]", (int)(bs
->flags
& 7));
209 * i2r handler for a sequence of addresses and ranges.
211 static int i2r_IPAddressOrRanges(BIO
*out
,
213 const IPAddressOrRanges
*aors
,
217 for (i
= 0; i
< sk_IPAddressOrRange_num(aors
); i
++) {
218 const IPAddressOrRange
*aor
= sk_IPAddressOrRange_value(aors
, i
);
219 BIO_printf(out
, "%*s", indent
, "");
221 case IPAddressOrRange_addressPrefix
:
222 if (!i2r_address(out
, afi
, 0x00, aor
->u
.addressPrefix
))
224 BIO_printf(out
, "/%d\n", addr_prefixlen(aor
->u
.addressPrefix
));
226 case IPAddressOrRange_addressRange
:
227 if (!i2r_address(out
, afi
, 0x00, aor
->u
.addressRange
->min
))
230 if (!i2r_address(out
, afi
, 0xFF, aor
->u
.addressRange
->max
))
240 * i2r handler for an IPAddrBlocks extension.
242 static int i2r_IPAddrBlocks(const X509V3_EXT_METHOD
*method
,
243 void *ext
, BIO
*out
, int indent
)
245 const IPAddrBlocks
*addr
= ext
;
247 for (i
= 0; i
< sk_IPAddressFamily_num(addr
); i
++) {
248 IPAddressFamily
*f
= sk_IPAddressFamily_value(addr
, i
);
249 const unsigned int afi
= v3_addr_get_afi(f
);
252 BIO_printf(out
, "%*sIPv4", indent
, "");
255 BIO_printf(out
, "%*sIPv6", indent
, "");
258 BIO_printf(out
, "%*sUnknown AFI %u", indent
, "", afi
);
261 if (f
->addressFamily
->length
> 2) {
262 switch (f
->addressFamily
->data
[2]) {
264 BIO_puts(out
, " (Unicast)");
267 BIO_puts(out
, " (Multicast)");
270 BIO_puts(out
, " (Unicast/Multicast)");
273 BIO_puts(out
, " (MPLS)");
276 BIO_puts(out
, " (Tunnel)");
279 BIO_puts(out
, " (VPLS)");
282 BIO_puts(out
, " (BGP MDT)");
285 BIO_puts(out
, " (MPLS-labeled VPN)");
288 BIO_printf(out
, " (Unknown SAFI %u)",
289 (unsigned)f
->addressFamily
->data
[2]);
293 switch (f
->ipAddressChoice
->type
) {
294 case IPAddressChoice_inherit
:
295 BIO_puts(out
, ": inherit\n");
297 case IPAddressChoice_addressesOrRanges
:
298 BIO_puts(out
, ":\n");
299 if (!i2r_IPAddressOrRanges(out
,
302 u
.addressesOrRanges
, afi
))
311 * Sort comparison function for a sequence of IPAddressOrRange
314 * There's no sane answer we can give if addr_expand() fails, and an
315 * assertion failure on externally supplied data is seriously uncool,
316 * so we just arbitrarily declare that if given invalid inputs this
317 * function returns -1. If this messes up your preferred sort order
318 * for garbage input, tough noogies.
320 static int IPAddressOrRange_cmp(const IPAddressOrRange
*a
,
321 const IPAddressOrRange
*b
, const int length
)
323 unsigned char addr_a
[ADDR_RAW_BUF_LEN
], addr_b
[ADDR_RAW_BUF_LEN
];
324 int prefixlen_a
= 0, prefixlen_b
= 0;
328 case IPAddressOrRange_addressPrefix
:
329 if (!addr_expand(addr_a
, a
->u
.addressPrefix
, length
, 0x00))
331 prefixlen_a
= addr_prefixlen(a
->u
.addressPrefix
);
333 case IPAddressOrRange_addressRange
:
334 if (!addr_expand(addr_a
, a
->u
.addressRange
->min
, length
, 0x00))
336 prefixlen_a
= length
* 8;
341 case IPAddressOrRange_addressPrefix
:
342 if (!addr_expand(addr_b
, b
->u
.addressPrefix
, length
, 0x00))
344 prefixlen_b
= addr_prefixlen(b
->u
.addressPrefix
);
346 case IPAddressOrRange_addressRange
:
347 if (!addr_expand(addr_b
, b
->u
.addressRange
->min
, length
, 0x00))
349 prefixlen_b
= length
* 8;
353 if ((r
= memcmp(addr_a
, addr_b
, length
)) != 0)
356 return prefixlen_a
- prefixlen_b
;
360 * IPv4-specific closure over IPAddressOrRange_cmp, since sk_sort()
361 * comparision routines are only allowed two arguments.
363 static int v4IPAddressOrRange_cmp(const IPAddressOrRange
*const *a
,
364 const IPAddressOrRange
*const *b
)
366 return IPAddressOrRange_cmp(*a
, *b
, 4);
370 * IPv6-specific closure over IPAddressOrRange_cmp, since sk_sort()
371 * comparision routines are only allowed two arguments.
373 static int v6IPAddressOrRange_cmp(const IPAddressOrRange
*const *a
,
374 const IPAddressOrRange
*const *b
)
376 return IPAddressOrRange_cmp(*a
, *b
, 16);
380 * Calculate whether a range collapses to a prefix.
381 * See last paragraph of RFC 3779 2.2.3.7.
383 static int range_should_be_prefix(const unsigned char *min
,
384 const unsigned char *max
, const int length
)
389 OPENSSL_assert(memcmp(min
, max
, length
) <= 0);
390 for (i
= 0; i
< length
&& min
[i
] == max
[i
]; i
++) ;
391 for (j
= length
- 1; j
>= 0 && min
[j
] == 0x00 && max
[j
] == 0xFF; j
--) ;
396 mask
= min
[i
] ^ max
[i
];
422 if ((min
[i
] & mask
) != 0 || (max
[i
] & mask
) != mask
)
429 * Construct a prefix.
431 static int make_addressPrefix(IPAddressOrRange
**result
,
432 unsigned char *addr
, const int prefixlen
)
434 int bytelen
= (prefixlen
+ 7) / 8, bitlen
= prefixlen
% 8;
435 IPAddressOrRange
*aor
= IPAddressOrRange_new();
439 aor
->type
= IPAddressOrRange_addressPrefix
;
440 if (aor
->u
.addressPrefix
== NULL
&&
441 (aor
->u
.addressPrefix
= ASN1_BIT_STRING_new()) == NULL
)
443 if (!ASN1_BIT_STRING_set(aor
->u
.addressPrefix
, addr
, bytelen
))
445 aor
->u
.addressPrefix
->flags
&= ~7;
446 aor
->u
.addressPrefix
->flags
|= ASN1_STRING_FLAG_BITS_LEFT
;
448 aor
->u
.addressPrefix
->data
[bytelen
- 1] &= ~(0xFF >> bitlen
);
449 aor
->u
.addressPrefix
->flags
|= 8 - bitlen
;
456 IPAddressOrRange_free(aor
);
461 * Construct a range. If it can be expressed as a prefix,
462 * return a prefix instead. Doing this here simplifies
463 * the rest of the code considerably.
465 static int make_addressRange(IPAddressOrRange
**result
,
467 unsigned char *max
, const int length
)
469 IPAddressOrRange
*aor
;
472 if ((prefixlen
= range_should_be_prefix(min
, max
, length
)) >= 0)
473 return make_addressPrefix(result
, min
, prefixlen
);
475 if ((aor
= IPAddressOrRange_new()) == NULL
)
477 aor
->type
= IPAddressOrRange_addressRange
;
478 OPENSSL_assert(aor
->u
.addressRange
== NULL
);
479 if ((aor
->u
.addressRange
= IPAddressRange_new()) == NULL
)
481 if (aor
->u
.addressRange
->min
== NULL
&&
482 (aor
->u
.addressRange
->min
= ASN1_BIT_STRING_new()) == NULL
)
484 if (aor
->u
.addressRange
->max
== NULL
&&
485 (aor
->u
.addressRange
->max
= ASN1_BIT_STRING_new()) == NULL
)
488 for (i
= length
; i
> 0 && min
[i
- 1] == 0x00; --i
) ;
489 if (!ASN1_BIT_STRING_set(aor
->u
.addressRange
->min
, min
, i
))
491 aor
->u
.addressRange
->min
->flags
&= ~7;
492 aor
->u
.addressRange
->min
->flags
|= ASN1_STRING_FLAG_BITS_LEFT
;
494 unsigned char b
= min
[i
- 1];
496 while ((b
& (0xFFU
>> j
)) != 0)
498 aor
->u
.addressRange
->min
->flags
|= 8 - j
;
501 for (i
= length
; i
> 0 && max
[i
- 1] == 0xFF; --i
) ;
502 if (!ASN1_BIT_STRING_set(aor
->u
.addressRange
->max
, max
, i
))
504 aor
->u
.addressRange
->max
->flags
&= ~7;
505 aor
->u
.addressRange
->max
->flags
|= ASN1_STRING_FLAG_BITS_LEFT
;
507 unsigned char b
= max
[i
- 1];
509 while ((b
& (0xFFU
>> j
)) != (0xFFU
>> j
))
511 aor
->u
.addressRange
->max
->flags
|= 8 - j
;
518 IPAddressOrRange_free(aor
);
523 * Construct a new address family or find an existing one.
525 static IPAddressFamily
*make_IPAddressFamily(IPAddrBlocks
*addr
,
527 const unsigned *safi
)
530 unsigned char key
[3];
534 key
[0] = (afi
>> 8) & 0xFF;
537 key
[2] = *safi
& 0xFF;
543 for (i
= 0; i
< sk_IPAddressFamily_num(addr
); i
++) {
544 f
= sk_IPAddressFamily_value(addr
, i
);
545 OPENSSL_assert(f
->addressFamily
->data
!= NULL
);
546 if (f
->addressFamily
->length
== keylen
&&
547 !memcmp(f
->addressFamily
->data
, key
, keylen
))
551 if ((f
= IPAddressFamily_new()) == NULL
)
553 if (f
->ipAddressChoice
== NULL
&&
554 (f
->ipAddressChoice
= IPAddressChoice_new()) == NULL
)
556 if (f
->addressFamily
== NULL
&&
557 (f
->addressFamily
= ASN1_OCTET_STRING_new()) == NULL
)
559 if (!ASN1_OCTET_STRING_set(f
->addressFamily
, key
, keylen
))
561 if (!sk_IPAddressFamily_push(addr
, f
))
567 IPAddressFamily_free(f
);
572 * Add an inheritance element.
574 int v3_addr_add_inherit(IPAddrBlocks
*addr
,
575 const unsigned afi
, const unsigned *safi
)
577 IPAddressFamily
*f
= make_IPAddressFamily(addr
, afi
, safi
);
579 f
->ipAddressChoice
== NULL
||
580 (f
->ipAddressChoice
->type
== IPAddressChoice_addressesOrRanges
&&
581 f
->ipAddressChoice
->u
.addressesOrRanges
!= NULL
))
583 if (f
->ipAddressChoice
->type
== IPAddressChoice_inherit
&&
584 f
->ipAddressChoice
->u
.inherit
!= NULL
)
586 if (f
->ipAddressChoice
->u
.inherit
== NULL
&&
587 (f
->ipAddressChoice
->u
.inherit
= ASN1_NULL_new()) == NULL
)
589 f
->ipAddressChoice
->type
= IPAddressChoice_inherit
;
594 * Construct an IPAddressOrRange sequence, or return an existing one.
596 static IPAddressOrRanges
*make_prefix_or_range(IPAddrBlocks
*addr
,
598 const unsigned *safi
)
600 IPAddressFamily
*f
= make_IPAddressFamily(addr
, afi
, safi
);
601 IPAddressOrRanges
*aors
= NULL
;
604 f
->ipAddressChoice
== NULL
||
605 (f
->ipAddressChoice
->type
== IPAddressChoice_inherit
&&
606 f
->ipAddressChoice
->u
.inherit
!= NULL
))
608 if (f
->ipAddressChoice
->type
== IPAddressChoice_addressesOrRanges
)
609 aors
= f
->ipAddressChoice
->u
.addressesOrRanges
;
612 if ((aors
= sk_IPAddressOrRange_new_null()) == NULL
)
616 (void)sk_IPAddressOrRange_set_cmp_func(aors
, v4IPAddressOrRange_cmp
);
619 (void)sk_IPAddressOrRange_set_cmp_func(aors
, v6IPAddressOrRange_cmp
);
622 f
->ipAddressChoice
->type
= IPAddressChoice_addressesOrRanges
;
623 f
->ipAddressChoice
->u
.addressesOrRanges
= aors
;
630 int v3_addr_add_prefix(IPAddrBlocks
*addr
,
632 const unsigned *safi
,
633 unsigned char *a
, const int prefixlen
)
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
,
651 unsigned char *min
, unsigned char *max
)
653 IPAddressOrRanges
*aors
= make_prefix_or_range(addr
, afi
, safi
);
654 IPAddressOrRange
*aor
;
655 int length
= length_from_afi(afi
);
658 if (!make_addressRange(&aor
, min
, max
, length
))
660 if (sk_IPAddressOrRange_push(aors
, aor
))
662 IPAddressOrRange_free(aor
);
667 * Extract min and max values from an IPAddressOrRange.
669 static int extract_min_max(IPAddressOrRange
*aor
,
670 unsigned char *min
, unsigned char *max
, int length
)
672 if (aor
== NULL
|| min
== NULL
|| max
== NULL
)
675 case IPAddressOrRange_addressPrefix
:
676 return (addr_expand(min
, aor
->u
.addressPrefix
, length
, 0x00) &&
677 addr_expand(max
, aor
->u
.addressPrefix
, length
, 0xFF));
678 case IPAddressOrRange_addressRange
:
679 return (addr_expand(min
, aor
->u
.addressRange
->min
, length
, 0x00) &&
680 addr_expand(max
, aor
->u
.addressRange
->max
, length
, 0xFF));
686 * Public wrapper for extract_min_max().
688 int v3_addr_get_range(IPAddressOrRange
*aor
,
691 unsigned char *max
, const int length
)
693 int afi_length
= length_from_afi(afi
);
694 if (aor
== NULL
|| min
== NULL
|| max
== NULL
||
695 afi_length
== 0 || length
< afi_length
||
696 (aor
->type
!= IPAddressOrRange_addressPrefix
&&
697 aor
->type
!= IPAddressOrRange_addressRange
) ||
698 !extract_min_max(aor
, min
, max
, afi_length
))
705 * Sort comparision function for a sequence of IPAddressFamily.
707 * The last paragraph of RFC 3779 2.2.3.3 is slightly ambiguous about
708 * the ordering: I can read it as meaning that IPv6 without a SAFI
709 * comes before IPv4 with a SAFI, which seems pretty weird. The
710 * examples in appendix B suggest that the author intended the
711 * null-SAFI rule to apply only within a single AFI, which is what I
712 * would have expected and is what the following code implements.
714 static int IPAddressFamily_cmp(const IPAddressFamily
*const *a_
,
715 const IPAddressFamily
*const *b_
)
717 const ASN1_OCTET_STRING
*a
= (*a_
)->addressFamily
;
718 const ASN1_OCTET_STRING
*b
= (*b_
)->addressFamily
;
719 int len
= ((a
->length
<= b
->length
) ? a
->length
: b
->length
);
720 int cmp
= memcmp(a
->data
, b
->data
, len
);
721 return cmp
? cmp
: a
->length
- b
->length
;
725 * Check whether an IPAddrBLocks is in canonical form.
727 int v3_addr_is_canonical(IPAddrBlocks
*addr
)
729 unsigned char a_min
[ADDR_RAW_BUF_LEN
], a_max
[ADDR_RAW_BUF_LEN
];
730 unsigned char b_min
[ADDR_RAW_BUF_LEN
], b_max
[ADDR_RAW_BUF_LEN
];
731 IPAddressOrRanges
*aors
;
735 * Empty extension is cannonical.
741 * Check whether the top-level list is in order.
743 for (i
= 0; i
< sk_IPAddressFamily_num(addr
) - 1; i
++) {
744 const IPAddressFamily
*a
= sk_IPAddressFamily_value(addr
, i
);
745 const IPAddressFamily
*b
= sk_IPAddressFamily_value(addr
, i
+ 1);
746 if (IPAddressFamily_cmp(&a
, &b
) >= 0)
751 * Top level's ok, now check each address family.
753 for (i
= 0; i
< sk_IPAddressFamily_num(addr
); i
++) {
754 IPAddressFamily
*f
= sk_IPAddressFamily_value(addr
, i
);
755 int length
= length_from_afi(v3_addr_get_afi(f
));
758 * Inheritance is canonical. Anything other than inheritance or
759 * a SEQUENCE OF IPAddressOrRange is an ASN.1 error or something.
761 if (f
== NULL
|| f
->ipAddressChoice
== NULL
)
763 switch (f
->ipAddressChoice
->type
) {
764 case IPAddressChoice_inherit
:
766 case IPAddressChoice_addressesOrRanges
:
773 * It's an IPAddressOrRanges sequence, check it.
775 aors
= f
->ipAddressChoice
->u
.addressesOrRanges
;
776 if (sk_IPAddressOrRange_num(aors
) == 0)
778 for (j
= 0; j
< sk_IPAddressOrRange_num(aors
) - 1; j
++) {
779 IPAddressOrRange
*a
= sk_IPAddressOrRange_value(aors
, j
);
780 IPAddressOrRange
*b
= sk_IPAddressOrRange_value(aors
, j
+ 1);
782 if (!extract_min_max(a
, a_min
, a_max
, length
) ||
783 !extract_min_max(b
, b_min
, b_max
, length
))
787 * Punt misordered list, overlapping start, or inverted range.
789 if (memcmp(a_min
, b_min
, length
) >= 0 ||
790 memcmp(a_min
, a_max
, length
) > 0 ||
791 memcmp(b_min
, b_max
, length
) > 0)
795 * Punt if adjacent or overlapping. Check for adjacency by
796 * subtracting one from b_min first.
798 for (k
= length
- 1; k
>= 0 && b_min
[k
]-- == 0x00; k
--) ;
799 if (memcmp(a_max
, b_min
, length
) >= 0)
803 * Check for range that should be expressed as a prefix.
805 if (a
->type
== IPAddressOrRange_addressRange
&&
806 range_should_be_prefix(a_min
, a_max
, length
) >= 0)
811 * Check range to see if it's inverted or should be a
814 j
= sk_IPAddressOrRange_num(aors
) - 1;
816 IPAddressOrRange
*a
= sk_IPAddressOrRange_value(aors
, j
);
817 if (a
!= NULL
&& a
->type
== IPAddressOrRange_addressRange
) {
818 if (!extract_min_max(a
, a_min
, a_max
, length
))
820 if (memcmp(a_min
, a_max
, length
) > 0 ||
821 range_should_be_prefix(a_min
, a_max
, length
) >= 0)
828 * If we made it through all that, we're happy.
834 * Whack an IPAddressOrRanges into canonical form.
836 static int IPAddressOrRanges_canonize(IPAddressOrRanges
*aors
,
839 int i
, j
, length
= length_from_afi(afi
);
842 * Sort the IPAddressOrRanges sequence.
844 sk_IPAddressOrRange_sort(aors
);
847 * Clean up representation issues, punt on duplicates or overlaps.
849 for (i
= 0; i
< sk_IPAddressOrRange_num(aors
) - 1; i
++) {
850 IPAddressOrRange
*a
= sk_IPAddressOrRange_value(aors
, i
);
851 IPAddressOrRange
*b
= sk_IPAddressOrRange_value(aors
, i
+ 1);
852 unsigned char a_min
[ADDR_RAW_BUF_LEN
], a_max
[ADDR_RAW_BUF_LEN
];
853 unsigned char b_min
[ADDR_RAW_BUF_LEN
], b_max
[ADDR_RAW_BUF_LEN
];
855 if (!extract_min_max(a
, a_min
, a_max
, length
) ||
856 !extract_min_max(b
, b_min
, b_max
, length
))
860 * Punt inverted ranges.
862 if (memcmp(a_min
, a_max
, length
) > 0 ||
863 memcmp(b_min
, b_max
, length
) > 0)
869 if (memcmp(a_max
, b_min
, length
) >= 0)
873 * Merge if a and b are adjacent. We check for
874 * adjacency by subtracting one from b_min first.
876 for (j
= length
- 1; j
>= 0 && b_min
[j
]-- == 0x00; j
--) ;
877 if (memcmp(a_max
, b_min
, length
) == 0) {
878 IPAddressOrRange
*merged
;
879 if (!make_addressRange(&merged
, a_min
, b_max
, length
))
881 (void)sk_IPAddressOrRange_set(aors
, i
, merged
);
882 (void)sk_IPAddressOrRange_delete(aors
, i
+ 1);
883 IPAddressOrRange_free(a
);
884 IPAddressOrRange_free(b
);
891 * Check for inverted final range.
893 j
= sk_IPAddressOrRange_num(aors
) - 1;
895 IPAddressOrRange
*a
= sk_IPAddressOrRange_value(aors
, j
);
896 if (a
!= NULL
&& a
->type
== IPAddressOrRange_addressRange
) {
897 unsigned char a_min
[ADDR_RAW_BUF_LEN
], a_max
[ADDR_RAW_BUF_LEN
];
898 extract_min_max(a
, a_min
, a_max
, length
);
899 if (memcmp(a_min
, a_max
, length
) > 0)
908 * Whack an IPAddrBlocks extension into canonical form.
910 int v3_addr_canonize(IPAddrBlocks
*addr
)
913 for (i
= 0; i
< sk_IPAddressFamily_num(addr
); i
++) {
914 IPAddressFamily
*f
= sk_IPAddressFamily_value(addr
, i
);
915 if (f
->ipAddressChoice
->type
== IPAddressChoice_addressesOrRanges
&&
916 !IPAddressOrRanges_canonize(f
->ipAddressChoice
->
921 (void)sk_IPAddressFamily_set_cmp_func(addr
, IPAddressFamily_cmp
);
922 sk_IPAddressFamily_sort(addr
);
923 OPENSSL_assert(v3_addr_is_canonical(addr
));
928 * v2i handler for the IPAddrBlocks extension.
930 static void *v2i_IPAddrBlocks(const struct v3_ext_method
*method
,
931 struct v3_ext_ctx
*ctx
,
932 STACK_OF(CONF_VALUE
) *values
)
934 static const char v4addr_chars
[] = "0123456789.";
935 static const char v6addr_chars
[] = "0123456789.:abcdefABCDEF";
936 IPAddrBlocks
*addr
= NULL
;
940 if ((addr
= sk_IPAddressFamily_new(IPAddressFamily_cmp
)) == NULL
) {
941 X509V3err(X509V3_F_V2I_IPADDRBLOCKS
, ERR_R_MALLOC_FAILURE
);
945 for (i
= 0; i
< sk_CONF_VALUE_num(values
); i
++) {
946 CONF_VALUE
*val
= sk_CONF_VALUE_value(values
, i
);
947 unsigned char min
[ADDR_RAW_BUF_LEN
], max
[ADDR_RAW_BUF_LEN
];
948 unsigned afi
, *safi
= NULL
, safi_
;
949 const char *addr_chars
;
950 int prefixlen
, i1
, i2
, delim
, length
;
952 if (!name_cmp(val
->name
, "IPv4")) {
954 } else if (!name_cmp(val
->name
, "IPv6")) {
956 } else if (!name_cmp(val
->name
, "IPv4-SAFI")) {
959 } else if (!name_cmp(val
->name
, "IPv6-SAFI")) {
963 X509V3err(X509V3_F_V2I_IPADDRBLOCKS
,
964 X509V3_R_EXTENSION_NAME_ERROR
);
965 X509V3_conf_err(val
);
971 addr_chars
= v4addr_chars
;
974 addr_chars
= v6addr_chars
;
978 length
= length_from_afi(afi
);
981 * Handle SAFI, if any, and BUF_strdup() so we can null-terminate
982 * the other input values.
985 *safi
= strtoul(val
->value
, &t
, 0);
986 t
+= strspn(t
, " \t");
987 if (*safi
> 0xFF || *t
++ != ':') {
988 X509V3err(X509V3_F_V2I_IPADDRBLOCKS
, X509V3_R_INVALID_SAFI
);
989 X509V3_conf_err(val
);
992 t
+= strspn(t
, " \t");
995 s
= BUF_strdup(val
->value
);
998 X509V3err(X509V3_F_V2I_IPADDRBLOCKS
, ERR_R_MALLOC_FAILURE
);
1003 * Check for inheritance. Not worth additional complexity to
1004 * optimize this (seldom-used) case.
1006 if (!strcmp(s
, "inherit")) {
1007 if (!v3_addr_add_inherit(addr
, afi
, safi
)) {
1008 X509V3err(X509V3_F_V2I_IPADDRBLOCKS
,
1009 X509V3_R_INVALID_INHERITANCE
);
1010 X509V3_conf_err(val
);
1018 i1
= strspn(s
, addr_chars
);
1019 i2
= i1
+ strspn(s
+ i1
, " \t");
1023 if (a2i_ipadd(min
, s
) != length
) {
1024 X509V3err(X509V3_F_V2I_IPADDRBLOCKS
, X509V3_R_INVALID_IPADDRESS
);
1025 X509V3_conf_err(val
);
1031 prefixlen
= (int)strtoul(s
+ i2
, &t
, 10);
1032 if (t
== s
+ i2
|| *t
!= '\0') {
1033 X509V3err(X509V3_F_V2I_IPADDRBLOCKS
,
1034 X509V3_R_EXTENSION_VALUE_ERROR
);
1035 X509V3_conf_err(val
);
1038 if (!v3_addr_add_prefix(addr
, afi
, safi
, min
, prefixlen
)) {
1039 X509V3err(X509V3_F_V2I_IPADDRBLOCKS
, ERR_R_MALLOC_FAILURE
);
1044 i1
= i2
+ strspn(s
+ i2
, " \t");
1045 i2
= i1
+ strspn(s
+ i1
, addr_chars
);
1046 if (i1
== i2
|| s
[i2
] != '\0') {
1047 X509V3err(X509V3_F_V2I_IPADDRBLOCKS
,
1048 X509V3_R_EXTENSION_VALUE_ERROR
);
1049 X509V3_conf_err(val
);
1052 if (a2i_ipadd(max
, s
+ i1
) != length
) {
1053 X509V3err(X509V3_F_V2I_IPADDRBLOCKS
,
1054 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
,
1060 X509V3_R_EXTENSION_VALUE_ERROR
);
1061 X509V3_conf_err(val
);
1064 if (!v3_addr_add_range(addr
, afi
, safi
, min
, max
)) {
1065 X509V3err(X509V3_F_V2I_IPADDRBLOCKS
, ERR_R_MALLOC_FAILURE
);
1070 if (!v3_addr_add_prefix(addr
, afi
, safi
, min
, length
* 8)) {
1071 X509V3err(X509V3_F_V2I_IPADDRBLOCKS
, ERR_R_MALLOC_FAILURE
);
1076 X509V3err(X509V3_F_V2I_IPADDRBLOCKS
,
1077 X509V3_R_EXTENSION_VALUE_ERROR
);
1078 X509V3_conf_err(val
);
1087 * Canonize the result, then we're done.
1089 if (!v3_addr_canonize(addr
))
1095 sk_IPAddressFamily_pop_free(addr
, IPAddressFamily_free
);
1102 const X509V3_EXT_METHOD v3_addr
= {
1103 NID_sbgp_ipAddrBlock
, /* nid */
1105 ASN1_ITEM_ref(IPAddrBlocks
), /* template */
1106 0, 0, 0, 0, /* old functions, ignored */
1110 v2i_IPAddrBlocks
, /* v2i */
1111 i2r_IPAddrBlocks
, /* i2r */
1113 NULL
/* extension-specific data */
1117 * Figure out whether extension sues inheritance.
1119 int v3_addr_inherits(IPAddrBlocks
*addr
)
1124 for (i
= 0; i
< sk_IPAddressFamily_num(addr
); i
++) {
1125 IPAddressFamily
*f
= sk_IPAddressFamily_value(addr
, i
);
1126 if (f
->ipAddressChoice
->type
== IPAddressChoice_inherit
)
1133 * Figure out whether parent contains child.
1135 static int addr_contains(IPAddressOrRanges
*parent
,
1136 IPAddressOrRanges
*child
, int length
)
1138 unsigned char p_min
[ADDR_RAW_BUF_LEN
], p_max
[ADDR_RAW_BUF_LEN
];
1139 unsigned char c_min
[ADDR_RAW_BUF_LEN
], c_max
[ADDR_RAW_BUF_LEN
];
1142 if (child
== NULL
|| parent
== child
)
1148 for (c
= 0; c
< sk_IPAddressOrRange_num(child
); c
++) {
1149 if (!extract_min_max(sk_IPAddressOrRange_value(child
, c
),
1150 c_min
, c_max
, length
))
1153 if (p
>= sk_IPAddressOrRange_num(parent
))
1155 if (!extract_min_max(sk_IPAddressOrRange_value(parent
, p
),
1156 p_min
, p_max
, length
))
1158 if (memcmp(p_max
, c_max
, length
) < 0)
1160 if (memcmp(p_min
, c_min
, length
) > 0)
1170 * Test whether a is a subset of b.
1172 int v3_addr_subset(IPAddrBlocks
*a
, IPAddrBlocks
*b
)
1175 if (a
== NULL
|| a
== b
)
1177 if (b
== NULL
|| v3_addr_inherits(a
) || v3_addr_inherits(b
))
1179 (void)sk_IPAddressFamily_set_cmp_func(b
, IPAddressFamily_cmp
);
1180 for (i
= 0; i
< sk_IPAddressFamily_num(a
); i
++) {
1181 IPAddressFamily
*fa
= sk_IPAddressFamily_value(a
, i
);
1182 int j
= sk_IPAddressFamily_find(b
, fa
);
1183 IPAddressFamily
*fb
;
1184 fb
= sk_IPAddressFamily_value(b
, j
);
1187 if (!addr_contains(fb
->ipAddressChoice
->u
.addressesOrRanges
,
1188 fa
->ipAddressChoice
->u
.addressesOrRanges
,
1189 length_from_afi(v3_addr_get_afi(fb
))))
1196 * Validation error handling via callback.
1198 # define validation_err(_err_) \
1200 if (ctx != NULL) { \
1201 ctx->error = _err_; \
1202 ctx->error_depth = i; \
1203 ctx->current_cert = x; \
1204 ret = ctx->verify_cb(0, ctx); \
1213 * Core code for RFC 3779 2.3 path validation.
1215 static int v3_addr_validate_path_internal(X509_STORE_CTX
*ctx
,
1216 STACK_OF(X509
) *chain
,
1219 IPAddrBlocks
*child
= NULL
;
1223 OPENSSL_assert(chain
!= NULL
&& sk_X509_num(chain
) > 0);
1224 OPENSSL_assert(ctx
!= NULL
|| ext
!= NULL
);
1225 OPENSSL_assert(ctx
== NULL
|| ctx
->verify_cb
!= NULL
);
1228 * Figure out where to start. If we don't have an extension to
1229 * check, we're done. Otherwise, check canonical form and
1230 * set up for walking up the chain.
1237 x
= sk_X509_value(chain
, i
);
1238 OPENSSL_assert(x
!= NULL
);
1239 if ((ext
= x
->rfc3779_addr
) == NULL
)
1242 if (!v3_addr_is_canonical(ext
))
1243 validation_err(X509_V_ERR_INVALID_EXTENSION
);
1244 (void)sk_IPAddressFamily_set_cmp_func(ext
, IPAddressFamily_cmp
);
1245 if ((child
= sk_IPAddressFamily_dup(ext
)) == NULL
) {
1246 X509V3err(X509V3_F_V3_ADDR_VALIDATE_PATH_INTERNAL
,
1247 ERR_R_MALLOC_FAILURE
);
1253 * Now walk up the chain. No cert may list resources that its
1254 * parent doesn't list.
1256 for (i
++; i
< sk_X509_num(chain
); i
++) {
1257 x
= sk_X509_value(chain
, i
);
1258 OPENSSL_assert(x
!= NULL
);
1259 if (!v3_addr_is_canonical(x
->rfc3779_addr
))
1260 validation_err(X509_V_ERR_INVALID_EXTENSION
);
1261 if (x
->rfc3779_addr
== NULL
) {
1262 for (j
= 0; j
< sk_IPAddressFamily_num(child
); j
++) {
1263 IPAddressFamily
*fc
= sk_IPAddressFamily_value(child
, j
);
1264 if (fc
->ipAddressChoice
->type
!= IPAddressChoice_inherit
) {
1265 validation_err(X509_V_ERR_UNNESTED_RESOURCE
);
1271 (void)sk_IPAddressFamily_set_cmp_func(x
->rfc3779_addr
,
1272 IPAddressFamily_cmp
);
1273 for (j
= 0; j
< sk_IPAddressFamily_num(child
); j
++) {
1274 IPAddressFamily
*fc
= sk_IPAddressFamily_value(child
, j
);
1275 int k
= sk_IPAddressFamily_find(x
->rfc3779_addr
, fc
);
1276 IPAddressFamily
*fp
=
1277 sk_IPAddressFamily_value(x
->rfc3779_addr
, k
);
1279 if (fc
->ipAddressChoice
->type
==
1280 IPAddressChoice_addressesOrRanges
) {
1281 validation_err(X509_V_ERR_UNNESTED_RESOURCE
);
1286 if (fp
->ipAddressChoice
->type
==
1287 IPAddressChoice_addressesOrRanges
) {
1288 if (fc
->ipAddressChoice
->type
== IPAddressChoice_inherit
1289 || addr_contains(fp
->ipAddressChoice
->u
.addressesOrRanges
,
1290 fc
->ipAddressChoice
->u
.addressesOrRanges
,
1291 length_from_afi(v3_addr_get_afi(fc
))))
1292 sk_IPAddressFamily_set(child
, j
, fp
);
1294 validation_err(X509_V_ERR_UNNESTED_RESOURCE
);
1300 * Trust anchor can't inherit.
1302 OPENSSL_assert(x
!= NULL
);
1303 if (x
->rfc3779_addr
!= NULL
) {
1304 for (j
= 0; j
< sk_IPAddressFamily_num(x
->rfc3779_addr
); j
++) {
1305 IPAddressFamily
*fp
=
1306 sk_IPAddressFamily_value(x
->rfc3779_addr
, j
);
1307 if (fp
->ipAddressChoice
->type
== IPAddressChoice_inherit
1308 && sk_IPAddressFamily_find(child
, fp
) >= 0)
1309 validation_err(X509_V_ERR_UNNESTED_RESOURCE
);
1314 sk_IPAddressFamily_free(child
);
1318 # undef validation_err
1321 * RFC 3779 2.3 path validation -- called from X509_verify_cert().
1323 int v3_addr_validate_path(X509_STORE_CTX
*ctx
)
1325 return v3_addr_validate_path_internal(ctx
, ctx
->chain
, NULL
);
1329 * RFC 3779 2.3 path validation of an extension.
1330 * Test whether chain covers extension.
1332 int v3_addr_validate_resource_set(STACK_OF(X509
) *chain
,
1333 IPAddrBlocks
*ext
, int allow_inheritance
)
1337 if (chain
== NULL
|| sk_X509_num(chain
) == 0)
1339 if (!allow_inheritance
&& v3_addr_inherits(ext
))
1341 return v3_addr_validate_path_internal(NULL
, chain
, ext
);
1344 #endif /* OPENSSL_NO_RFC3779 */