2 * Copyright 2006-2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 * Implementation of RFC 3779 section 2.2.
17 #include "internal/cryptlib.h"
18 #include <openssl/conf.h>
19 #include <openssl/asn1.h>
20 #include <openssl/asn1t.h>
21 #include <openssl/buffer.h>
22 #include <openssl/x509v3.h>
23 #include "crypto/x509.h"
25 #include "x509_local.h"
27 #ifndef OPENSSL_NO_RFC3779
29 DEFINE_STACK_OF(IPAddressOrRange
)
30 DEFINE_STACK_OF(IPAddressFamily
)
31 DEFINE_STACK_OF(CONF_VALUE
)
34 * OpenSSL ASN.1 template translation of RFC 3779 2.2.3.
37 ASN1_SEQUENCE(IPAddressRange
) = {
38 ASN1_SIMPLE(IPAddressRange
, min
, ASN1_BIT_STRING
),
39 ASN1_SIMPLE(IPAddressRange
, max
, ASN1_BIT_STRING
)
40 } ASN1_SEQUENCE_END(IPAddressRange
)
42 ASN1_CHOICE(IPAddressOrRange
) = {
43 ASN1_SIMPLE(IPAddressOrRange
, u
.addressPrefix
, ASN1_BIT_STRING
),
44 ASN1_SIMPLE(IPAddressOrRange
, u
.addressRange
, IPAddressRange
)
45 } ASN1_CHOICE_END(IPAddressOrRange
)
47 ASN1_CHOICE(IPAddressChoice
) = {
48 ASN1_SIMPLE(IPAddressChoice
, u
.inherit
, ASN1_NULL
),
49 ASN1_SEQUENCE_OF(IPAddressChoice
, u
.addressesOrRanges
, IPAddressOrRange
)
50 } ASN1_CHOICE_END(IPAddressChoice
)
52 ASN1_SEQUENCE(IPAddressFamily
) = {
53 ASN1_SIMPLE(IPAddressFamily
, addressFamily
, ASN1_OCTET_STRING
),
54 ASN1_SIMPLE(IPAddressFamily
, ipAddressChoice
, IPAddressChoice
)
55 } ASN1_SEQUENCE_END(IPAddressFamily
)
57 ASN1_ITEM_TEMPLATE(IPAddrBlocks
) =
58 ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF
, 0,
59 IPAddrBlocks
, IPAddressFamily
)
60 static_ASN1_ITEM_TEMPLATE_END(IPAddrBlocks
)
62 IMPLEMENT_ASN1_FUNCTIONS(IPAddressRange
)
63 IMPLEMENT_ASN1_FUNCTIONS(IPAddressOrRange
)
64 IMPLEMENT_ASN1_FUNCTIONS(IPAddressChoice
)
65 IMPLEMENT_ASN1_FUNCTIONS(IPAddressFamily
)
68 * How much buffer space do we need for a raw address?
70 #define ADDR_RAW_BUF_LEN 16
73 * What's the address length associated with this AFI?
75 static int length_from_afi(const unsigned afi
)
88 * Extract the AFI from an IPAddressFamily.
90 unsigned int X509v3_addr_get_afi(const IPAddressFamily
*f
)
93 || f
->addressFamily
== NULL
94 || f
->addressFamily
->data
== NULL
95 || f
->addressFamily
->length
< 2)
97 return (f
->addressFamily
->data
[0] << 8) | f
->addressFamily
->data
[1];
101 * Expand the bitstring form of an address into a raw byte array.
102 * At the moment this is coded for simplicity, not speed.
104 static int addr_expand(unsigned char *addr
,
105 const ASN1_BIT_STRING
*bs
,
106 const int length
, const unsigned char fill
)
108 if (bs
->length
< 0 || bs
->length
> length
)
110 if (bs
->length
> 0) {
111 memcpy(addr
, bs
->data
, bs
->length
);
112 if ((bs
->flags
& 7) != 0) {
113 unsigned char mask
= 0xFF >> (8 - (bs
->flags
& 7));
115 addr
[bs
->length
- 1] &= ~mask
;
117 addr
[bs
->length
- 1] |= mask
;
120 memset(addr
+ bs
->length
, fill
, length
- bs
->length
);
125 * Extract the prefix length from a bitstring.
127 #define addr_prefixlen(bs) ((int) ((bs)->length * 8 - ((bs)->flags & 7)))
130 * i2r handler for one address bitstring.
132 static int i2r_address(BIO
*out
,
134 const unsigned char fill
, const ASN1_BIT_STRING
*bs
)
136 unsigned char addr
[ADDR_RAW_BUF_LEN
];
143 if (!addr_expand(addr
, bs
, 4, fill
))
145 BIO_printf(out
, "%d.%d.%d.%d", addr
[0], addr
[1], addr
[2], addr
[3]);
147 /* TODO possibly combine with ipaddr_to_asc() */
149 if (!addr_expand(addr
, bs
, 16, fill
))
151 for (n
= 16; n
> 1 && addr
[n
- 1] == 0x00 && addr
[n
- 2] == 0x00;
153 for (i
= 0; i
< n
; i
+= 2)
154 BIO_printf(out
, "%x%s", (addr
[i
] << 8) | addr
[i
+ 1],
155 (i
< 14 ? ":" : ""));
162 for (i
= 0; i
< bs
->length
; i
++)
163 BIO_printf(out
, "%s%02x", (i
> 0 ? ":" : ""), bs
->data
[i
]);
164 BIO_printf(out
, "[%d]", (int)(bs
->flags
& 7));
171 * i2r handler for a sequence of addresses and ranges.
173 static int i2r_IPAddressOrRanges(BIO
*out
,
175 const IPAddressOrRanges
*aors
,
179 for (i
= 0; i
< sk_IPAddressOrRange_num(aors
); i
++) {
180 const IPAddressOrRange
*aor
= sk_IPAddressOrRange_value(aors
, i
);
181 BIO_printf(out
, "%*s", indent
, "");
183 case IPAddressOrRange_addressPrefix
:
184 if (!i2r_address(out
, afi
, 0x00, aor
->u
.addressPrefix
))
186 BIO_printf(out
, "/%d\n", addr_prefixlen(aor
->u
.addressPrefix
));
188 case IPAddressOrRange_addressRange
:
189 if (!i2r_address(out
, afi
, 0x00, aor
->u
.addressRange
->min
))
192 if (!i2r_address(out
, afi
, 0xFF, aor
->u
.addressRange
->max
))
202 * i2r handler for an IPAddrBlocks extension.
204 static int i2r_IPAddrBlocks(const X509V3_EXT_METHOD
*method
,
205 void *ext
, BIO
*out
, int indent
)
207 const IPAddrBlocks
*addr
= ext
;
209 for (i
= 0; i
< sk_IPAddressFamily_num(addr
); i
++) {
210 IPAddressFamily
*f
= sk_IPAddressFamily_value(addr
, i
);
211 const unsigned int afi
= X509v3_addr_get_afi(f
);
214 BIO_printf(out
, "%*sIPv4", indent
, "");
217 BIO_printf(out
, "%*sIPv6", indent
, "");
220 BIO_printf(out
, "%*sUnknown AFI %u", indent
, "", afi
);
223 if (f
->addressFamily
->length
> 2) {
224 switch (f
->addressFamily
->data
[2]) {
226 BIO_puts(out
, " (Unicast)");
229 BIO_puts(out
, " (Multicast)");
232 BIO_puts(out
, " (Unicast/Multicast)");
235 BIO_puts(out
, " (MPLS)");
238 BIO_puts(out
, " (Tunnel)");
241 BIO_puts(out
, " (VPLS)");
244 BIO_puts(out
, " (BGP MDT)");
247 BIO_puts(out
, " (MPLS-labeled VPN)");
250 BIO_printf(out
, " (Unknown SAFI %u)",
251 (unsigned)f
->addressFamily
->data
[2]);
255 switch (f
->ipAddressChoice
->type
) {
256 case IPAddressChoice_inherit
:
257 BIO_puts(out
, ": inherit\n");
259 case IPAddressChoice_addressesOrRanges
:
260 BIO_puts(out
, ":\n");
261 if (!i2r_IPAddressOrRanges(out
,
264 u
.addressesOrRanges
, afi
))
273 * Sort comparison function for a sequence of IPAddressOrRange
276 * There's no sane answer we can give if addr_expand() fails, and an
277 * assertion failure on externally supplied data is seriously uncool,
278 * so we just arbitrarily declare that if given invalid inputs this
279 * function returns -1. If this messes up your preferred sort order
280 * for garbage input, tough noogies.
282 static int IPAddressOrRange_cmp(const IPAddressOrRange
*a
,
283 const IPAddressOrRange
*b
, const int length
)
285 unsigned char addr_a
[ADDR_RAW_BUF_LEN
], addr_b
[ADDR_RAW_BUF_LEN
];
286 int prefixlen_a
= 0, prefixlen_b
= 0;
290 case IPAddressOrRange_addressPrefix
:
291 if (!addr_expand(addr_a
, a
->u
.addressPrefix
, length
, 0x00))
293 prefixlen_a
= addr_prefixlen(a
->u
.addressPrefix
);
295 case IPAddressOrRange_addressRange
:
296 if (!addr_expand(addr_a
, a
->u
.addressRange
->min
, length
, 0x00))
298 prefixlen_a
= length
* 8;
303 case IPAddressOrRange_addressPrefix
:
304 if (!addr_expand(addr_b
, b
->u
.addressPrefix
, length
, 0x00))
306 prefixlen_b
= addr_prefixlen(b
->u
.addressPrefix
);
308 case IPAddressOrRange_addressRange
:
309 if (!addr_expand(addr_b
, b
->u
.addressRange
->min
, length
, 0x00))
311 prefixlen_b
= length
* 8;
315 if ((r
= memcmp(addr_a
, addr_b
, length
)) != 0)
318 return prefixlen_a
- prefixlen_b
;
322 * IPv4-specific closure over IPAddressOrRange_cmp, since sk_sort()
323 * comparison routines are only allowed two arguments.
325 static int v4IPAddressOrRange_cmp(const IPAddressOrRange
*const *a
,
326 const IPAddressOrRange
*const *b
)
328 return IPAddressOrRange_cmp(*a
, *b
, 4);
332 * IPv6-specific closure over IPAddressOrRange_cmp, since sk_sort()
333 * comparison routines are only allowed two arguments.
335 static int v6IPAddressOrRange_cmp(const IPAddressOrRange
*const *a
,
336 const IPAddressOrRange
*const *b
)
338 return IPAddressOrRange_cmp(*a
, *b
, 16);
342 * Calculate whether a range collapses to a prefix.
343 * See last paragraph of RFC 3779 2.2.3.7.
345 static int range_should_be_prefix(const unsigned char *min
,
346 const unsigned char *max
, const int length
)
351 if (memcmp(min
, max
, length
) <= 0)
353 for (i
= 0; i
< length
&& min
[i
] == max
[i
]; i
++) ;
354 for (j
= length
- 1; j
>= 0 && min
[j
] == 0x00 && max
[j
] == 0xFF; j
--) ;
359 mask
= min
[i
] ^ max
[i
];
385 if ((min
[i
] & mask
) != 0 || (max
[i
] & mask
) != mask
)
392 * Construct a prefix.
394 static int make_addressPrefix(IPAddressOrRange
**result
,
395 unsigned char *addr
, const int prefixlen
)
397 int bytelen
= (prefixlen
+ 7) / 8, bitlen
= prefixlen
% 8;
398 IPAddressOrRange
*aor
= IPAddressOrRange_new();
402 aor
->type
= IPAddressOrRange_addressPrefix
;
403 if (aor
->u
.addressPrefix
== NULL
&&
404 (aor
->u
.addressPrefix
= ASN1_BIT_STRING_new()) == NULL
)
406 if (!ASN1_BIT_STRING_set(aor
->u
.addressPrefix
, addr
, bytelen
))
408 aor
->u
.addressPrefix
->flags
&= ~7;
409 aor
->u
.addressPrefix
->flags
|= ASN1_STRING_FLAG_BITS_LEFT
;
411 aor
->u
.addressPrefix
->data
[bytelen
- 1] &= ~(0xFF >> bitlen
);
412 aor
->u
.addressPrefix
->flags
|= 8 - bitlen
;
419 IPAddressOrRange_free(aor
);
424 * Construct a range. If it can be expressed as a prefix,
425 * return a prefix instead. Doing this here simplifies
426 * the rest of the code considerably.
428 static int make_addressRange(IPAddressOrRange
**result
,
430 unsigned char *max
, const int length
)
432 IPAddressOrRange
*aor
;
435 if ((prefixlen
= range_should_be_prefix(min
, max
, length
)) >= 0)
436 return make_addressPrefix(result
, min
, prefixlen
);
438 if ((aor
= IPAddressOrRange_new()) == NULL
)
440 aor
->type
= IPAddressOrRange_addressRange
;
441 if ((aor
->u
.addressRange
= IPAddressRange_new()) == NULL
)
443 if (aor
->u
.addressRange
->min
== NULL
&&
444 (aor
->u
.addressRange
->min
= ASN1_BIT_STRING_new()) == NULL
)
446 if (aor
->u
.addressRange
->max
== NULL
&&
447 (aor
->u
.addressRange
->max
= ASN1_BIT_STRING_new()) == NULL
)
450 for (i
= length
; i
> 0 && min
[i
- 1] == 0x00; --i
) ;
451 if (!ASN1_BIT_STRING_set(aor
->u
.addressRange
->min
, min
, i
))
453 aor
->u
.addressRange
->min
->flags
&= ~7;
454 aor
->u
.addressRange
->min
->flags
|= ASN1_STRING_FLAG_BITS_LEFT
;
456 unsigned char b
= min
[i
- 1];
458 while ((b
& (0xFFU
>> j
)) != 0)
460 aor
->u
.addressRange
->min
->flags
|= 8 - j
;
463 for (i
= length
; i
> 0 && max
[i
- 1] == 0xFF; --i
) ;
464 if (!ASN1_BIT_STRING_set(aor
->u
.addressRange
->max
, max
, i
))
466 aor
->u
.addressRange
->max
->flags
&= ~7;
467 aor
->u
.addressRange
->max
->flags
|= ASN1_STRING_FLAG_BITS_LEFT
;
469 unsigned char b
= max
[i
- 1];
471 while ((b
& (0xFFU
>> j
)) != (0xFFU
>> j
))
473 aor
->u
.addressRange
->max
->flags
|= 8 - j
;
480 IPAddressOrRange_free(aor
);
485 * Construct a new address family or find an existing one.
487 static IPAddressFamily
*make_IPAddressFamily(IPAddrBlocks
*addr
,
489 const unsigned *safi
)
492 unsigned char key
[3];
496 key
[0] = (afi
>> 8) & 0xFF;
499 key
[2] = *safi
& 0xFF;
505 for (i
= 0; i
< sk_IPAddressFamily_num(addr
); i
++) {
506 f
= sk_IPAddressFamily_value(addr
, i
);
507 if (f
->addressFamily
->length
== keylen
&&
508 !memcmp(f
->addressFamily
->data
, key
, keylen
))
512 if ((f
= IPAddressFamily_new()) == NULL
)
514 if (f
->ipAddressChoice
== NULL
&&
515 (f
->ipAddressChoice
= IPAddressChoice_new()) == NULL
)
517 if (f
->addressFamily
== NULL
&&
518 (f
->addressFamily
= ASN1_OCTET_STRING_new()) == NULL
)
520 if (!ASN1_OCTET_STRING_set(f
->addressFamily
, key
, keylen
))
522 if (!sk_IPAddressFamily_push(addr
, f
))
528 IPAddressFamily_free(f
);
533 * Add an inheritance element.
535 int X509v3_addr_add_inherit(IPAddrBlocks
*addr
,
536 const unsigned afi
, const unsigned *safi
)
538 IPAddressFamily
*f
= make_IPAddressFamily(addr
, afi
, safi
);
540 f
->ipAddressChoice
== NULL
||
541 (f
->ipAddressChoice
->type
== IPAddressChoice_addressesOrRanges
&&
542 f
->ipAddressChoice
->u
.addressesOrRanges
!= NULL
))
544 if (f
->ipAddressChoice
->type
== IPAddressChoice_inherit
&&
545 f
->ipAddressChoice
->u
.inherit
!= NULL
)
547 if (f
->ipAddressChoice
->u
.inherit
== NULL
&&
548 (f
->ipAddressChoice
->u
.inherit
= ASN1_NULL_new()) == NULL
)
550 f
->ipAddressChoice
->type
= IPAddressChoice_inherit
;
555 * Construct an IPAddressOrRange sequence, or return an existing one.
557 static IPAddressOrRanges
*make_prefix_or_range(IPAddrBlocks
*addr
,
559 const unsigned *safi
)
561 IPAddressFamily
*f
= make_IPAddressFamily(addr
, afi
, safi
);
562 IPAddressOrRanges
*aors
= NULL
;
565 f
->ipAddressChoice
== NULL
||
566 (f
->ipAddressChoice
->type
== IPAddressChoice_inherit
&&
567 f
->ipAddressChoice
->u
.inherit
!= NULL
))
569 if (f
->ipAddressChoice
->type
== IPAddressChoice_addressesOrRanges
)
570 aors
= f
->ipAddressChoice
->u
.addressesOrRanges
;
573 if ((aors
= sk_IPAddressOrRange_new_null()) == NULL
)
577 (void)sk_IPAddressOrRange_set_cmp_func(aors
, v4IPAddressOrRange_cmp
);
580 (void)sk_IPAddressOrRange_set_cmp_func(aors
, v6IPAddressOrRange_cmp
);
583 f
->ipAddressChoice
->type
= IPAddressChoice_addressesOrRanges
;
584 f
->ipAddressChoice
->u
.addressesOrRanges
= aors
;
591 int X509v3_addr_add_prefix(IPAddrBlocks
*addr
,
593 const unsigned *safi
,
594 unsigned char *a
, const int prefixlen
)
596 IPAddressOrRanges
*aors
= make_prefix_or_range(addr
, afi
, safi
);
597 IPAddressOrRange
*aor
;
598 if (aors
== NULL
|| !make_addressPrefix(&aor
, a
, prefixlen
))
600 if (sk_IPAddressOrRange_push(aors
, aor
))
602 IPAddressOrRange_free(aor
);
609 int X509v3_addr_add_range(IPAddrBlocks
*addr
,
611 const unsigned *safi
,
612 unsigned char *min
, unsigned char *max
)
614 IPAddressOrRanges
*aors
= make_prefix_or_range(addr
, afi
, safi
);
615 IPAddressOrRange
*aor
;
616 int length
= length_from_afi(afi
);
619 if (!make_addressRange(&aor
, min
, max
, length
))
621 if (sk_IPAddressOrRange_push(aors
, aor
))
623 IPAddressOrRange_free(aor
);
628 * Extract min and max values from an IPAddressOrRange.
630 static int extract_min_max(IPAddressOrRange
*aor
,
631 unsigned char *min
, unsigned char *max
, int length
)
633 if (aor
== NULL
|| min
== NULL
|| max
== NULL
)
636 case IPAddressOrRange_addressPrefix
:
637 return (addr_expand(min
, aor
->u
.addressPrefix
, length
, 0x00) &&
638 addr_expand(max
, aor
->u
.addressPrefix
, length
, 0xFF));
639 case IPAddressOrRange_addressRange
:
640 return (addr_expand(min
, aor
->u
.addressRange
->min
, length
, 0x00) &&
641 addr_expand(max
, aor
->u
.addressRange
->max
, length
, 0xFF));
647 * Public wrapper for extract_min_max().
649 int X509v3_addr_get_range(IPAddressOrRange
*aor
,
652 unsigned char *max
, const int length
)
654 int afi_length
= length_from_afi(afi
);
655 if (aor
== NULL
|| min
== NULL
|| max
== NULL
||
656 afi_length
== 0 || length
< afi_length
||
657 (aor
->type
!= IPAddressOrRange_addressPrefix
&&
658 aor
->type
!= IPAddressOrRange_addressRange
) ||
659 !extract_min_max(aor
, min
, max
, afi_length
))
666 * Sort comparison function for a sequence of IPAddressFamily.
668 * The last paragraph of RFC 3779 2.2.3.3 is slightly ambiguous about
669 * the ordering: I can read it as meaning that IPv6 without a SAFI
670 * comes before IPv4 with a SAFI, which seems pretty weird. The
671 * examples in appendix B suggest that the author intended the
672 * null-SAFI rule to apply only within a single AFI, which is what I
673 * would have expected and is what the following code implements.
675 static int IPAddressFamily_cmp(const IPAddressFamily
*const *a_
,
676 const IPAddressFamily
*const *b_
)
678 const ASN1_OCTET_STRING
*a
= (*a_
)->addressFamily
;
679 const ASN1_OCTET_STRING
*b
= (*b_
)->addressFamily
;
680 int len
= ((a
->length
<= b
->length
) ? a
->length
: b
->length
);
681 int cmp
= memcmp(a
->data
, b
->data
, len
);
682 return cmp
? cmp
: a
->length
- b
->length
;
686 * Check whether an IPAddrBLocks is in canonical form.
688 int X509v3_addr_is_canonical(IPAddrBlocks
*addr
)
690 unsigned char a_min
[ADDR_RAW_BUF_LEN
], a_max
[ADDR_RAW_BUF_LEN
];
691 unsigned char b_min
[ADDR_RAW_BUF_LEN
], b_max
[ADDR_RAW_BUF_LEN
];
692 IPAddressOrRanges
*aors
;
696 * Empty extension is canonical.
702 * Check whether the top-level list is in order.
704 for (i
= 0; i
< sk_IPAddressFamily_num(addr
) - 1; i
++) {
705 const IPAddressFamily
*a
= sk_IPAddressFamily_value(addr
, i
);
706 const IPAddressFamily
*b
= sk_IPAddressFamily_value(addr
, i
+ 1);
707 if (IPAddressFamily_cmp(&a
, &b
) >= 0)
712 * Top level's ok, now check each address family.
714 for (i
= 0; i
< sk_IPAddressFamily_num(addr
); i
++) {
715 IPAddressFamily
*f
= sk_IPAddressFamily_value(addr
, i
);
716 int length
= length_from_afi(X509v3_addr_get_afi(f
));
719 * Inheritance is canonical. Anything other than inheritance or
720 * a SEQUENCE OF IPAddressOrRange is an ASN.1 error or something.
722 if (f
== NULL
|| f
->ipAddressChoice
== NULL
)
724 switch (f
->ipAddressChoice
->type
) {
725 case IPAddressChoice_inherit
:
727 case IPAddressChoice_addressesOrRanges
:
734 * It's an IPAddressOrRanges sequence, check it.
736 aors
= f
->ipAddressChoice
->u
.addressesOrRanges
;
737 if (sk_IPAddressOrRange_num(aors
) == 0)
739 for (j
= 0; j
< sk_IPAddressOrRange_num(aors
) - 1; j
++) {
740 IPAddressOrRange
*a
= sk_IPAddressOrRange_value(aors
, j
);
741 IPAddressOrRange
*b
= sk_IPAddressOrRange_value(aors
, j
+ 1);
743 if (!extract_min_max(a
, a_min
, a_max
, length
) ||
744 !extract_min_max(b
, b_min
, b_max
, length
))
748 * Punt misordered list, overlapping start, or inverted range.
750 if (memcmp(a_min
, b_min
, length
) >= 0 ||
751 memcmp(a_min
, a_max
, length
) > 0 ||
752 memcmp(b_min
, b_max
, length
) > 0)
756 * Punt if adjacent or overlapping. Check for adjacency by
757 * subtracting one from b_min first.
759 for (k
= length
- 1; k
>= 0 && b_min
[k
]-- == 0x00; k
--) ;
760 if (memcmp(a_max
, b_min
, length
) >= 0)
764 * Check for range that should be expressed as a prefix.
766 if (a
->type
== IPAddressOrRange_addressRange
&&
767 range_should_be_prefix(a_min
, a_max
, length
) >= 0)
772 * Check range to see if it's inverted or should be a
775 j
= sk_IPAddressOrRange_num(aors
) - 1;
777 IPAddressOrRange
*a
= sk_IPAddressOrRange_value(aors
, j
);
778 if (a
!= NULL
&& a
->type
== IPAddressOrRange_addressRange
) {
779 if (!extract_min_max(a
, a_min
, a_max
, length
))
781 if (memcmp(a_min
, a_max
, length
) > 0 ||
782 range_should_be_prefix(a_min
, a_max
, length
) >= 0)
789 * If we made it through all that, we're happy.
795 * Whack an IPAddressOrRanges into canonical form.
797 static int IPAddressOrRanges_canonize(IPAddressOrRanges
*aors
,
800 int i
, j
, length
= length_from_afi(afi
);
803 * Sort the IPAddressOrRanges sequence.
805 sk_IPAddressOrRange_sort(aors
);
808 * Clean up representation issues, punt on duplicates or overlaps.
810 for (i
= 0; i
< sk_IPAddressOrRange_num(aors
) - 1; i
++) {
811 IPAddressOrRange
*a
= sk_IPAddressOrRange_value(aors
, i
);
812 IPAddressOrRange
*b
= sk_IPAddressOrRange_value(aors
, i
+ 1);
813 unsigned char a_min
[ADDR_RAW_BUF_LEN
], a_max
[ADDR_RAW_BUF_LEN
];
814 unsigned char b_min
[ADDR_RAW_BUF_LEN
], b_max
[ADDR_RAW_BUF_LEN
];
816 if (!extract_min_max(a
, a_min
, a_max
, length
) ||
817 !extract_min_max(b
, b_min
, b_max
, length
))
821 * Punt inverted ranges.
823 if (memcmp(a_min
, a_max
, length
) > 0 ||
824 memcmp(b_min
, b_max
, length
) > 0)
830 if (memcmp(a_max
, b_min
, length
) >= 0)
834 * Merge if a and b are adjacent. We check for
835 * adjacency by subtracting one from b_min first.
837 for (j
= length
- 1; j
>= 0 && b_min
[j
]-- == 0x00; j
--) ;
838 if (memcmp(a_max
, b_min
, length
) == 0) {
839 IPAddressOrRange
*merged
;
840 if (!make_addressRange(&merged
, a_min
, b_max
, length
))
842 (void)sk_IPAddressOrRange_set(aors
, i
, merged
);
843 (void)sk_IPAddressOrRange_delete(aors
, i
+ 1);
844 IPAddressOrRange_free(a
);
845 IPAddressOrRange_free(b
);
852 * Check for inverted final range.
854 j
= sk_IPAddressOrRange_num(aors
) - 1;
856 IPAddressOrRange
*a
= sk_IPAddressOrRange_value(aors
, j
);
857 if (a
!= NULL
&& a
->type
== IPAddressOrRange_addressRange
) {
858 unsigned char a_min
[ADDR_RAW_BUF_LEN
], a_max
[ADDR_RAW_BUF_LEN
];
859 if (!extract_min_max(a
, a_min
, a_max
, length
))
861 if (memcmp(a_min
, a_max
, length
) > 0)
870 * Whack an IPAddrBlocks extension into canonical form.
872 int X509v3_addr_canonize(IPAddrBlocks
*addr
)
875 for (i
= 0; i
< sk_IPAddressFamily_num(addr
); i
++) {
876 IPAddressFamily
*f
= sk_IPAddressFamily_value(addr
, i
);
877 if (f
->ipAddressChoice
->type
== IPAddressChoice_addressesOrRanges
&&
878 !IPAddressOrRanges_canonize(f
->ipAddressChoice
->
880 X509v3_addr_get_afi(f
)))
883 (void)sk_IPAddressFamily_set_cmp_func(addr
, IPAddressFamily_cmp
);
884 sk_IPAddressFamily_sort(addr
);
885 if (!ossl_assert(X509v3_addr_is_canonical(addr
)))
891 * v2i handler for the IPAddrBlocks extension.
893 static void *v2i_IPAddrBlocks(const struct v3_ext_method
*method
,
894 struct v3_ext_ctx
*ctx
,
895 STACK_OF(CONF_VALUE
) *values
)
897 static const char v4addr_chars
[] = "0123456789.";
898 static const char v6addr_chars
[] = "0123456789.:abcdefABCDEF";
899 IPAddrBlocks
*addr
= NULL
;
903 if ((addr
= sk_IPAddressFamily_new(IPAddressFamily_cmp
)) == NULL
) {
904 X509V3err(X509V3_F_V2I_IPADDRBLOCKS
, ERR_R_MALLOC_FAILURE
);
908 for (i
= 0; i
< sk_CONF_VALUE_num(values
); i
++) {
909 CONF_VALUE
*val
= sk_CONF_VALUE_value(values
, i
);
910 unsigned char min
[ADDR_RAW_BUF_LEN
], max
[ADDR_RAW_BUF_LEN
];
911 unsigned afi
, *safi
= NULL
, safi_
;
912 const char *addr_chars
= NULL
;
913 int prefixlen
, i1
, i2
, delim
, length
;
915 if (!v3_name_cmp(val
->name
, "IPv4")) {
917 } else if (!v3_name_cmp(val
->name
, "IPv6")) {
919 } else if (!v3_name_cmp(val
->name
, "IPv4-SAFI")) {
922 } else if (!v3_name_cmp(val
->name
, "IPv6-SAFI")) {
926 X509V3err(X509V3_F_V2I_IPADDRBLOCKS
,
927 X509V3_R_EXTENSION_NAME_ERROR
);
928 ERR_add_error_data(1, val
->name
);
934 addr_chars
= v4addr_chars
;
937 addr_chars
= v6addr_chars
;
941 length
= length_from_afi(afi
);
944 * Handle SAFI, if any, and OPENSSL_strdup() so we can null-terminate
945 * the other input values.
948 *safi
= strtoul(val
->value
, &t
, 0);
949 t
+= strspn(t
, " \t");
950 if (*safi
> 0xFF || *t
++ != ':') {
951 X509V3err(X509V3_F_V2I_IPADDRBLOCKS
, X509V3_R_INVALID_SAFI
);
952 X509V3_conf_add_error_name_value(val
);
955 t
+= strspn(t
, " \t");
956 s
= OPENSSL_strdup(t
);
958 s
= OPENSSL_strdup(val
->value
);
961 X509V3err(X509V3_F_V2I_IPADDRBLOCKS
, ERR_R_MALLOC_FAILURE
);
966 * Check for inheritance. Not worth additional complexity to
967 * optimize this (seldom-used) case.
969 if (strcmp(s
, "inherit") == 0) {
970 if (!X509v3_addr_add_inherit(addr
, afi
, safi
)) {
971 X509V3err(X509V3_F_V2I_IPADDRBLOCKS
,
972 X509V3_R_INVALID_INHERITANCE
);
973 X509V3_conf_add_error_name_value(val
);
981 i1
= strspn(s
, addr_chars
);
982 i2
= i1
+ strspn(s
+ i1
, " \t");
986 if (a2i_ipadd(min
, s
) != length
) {
987 X509V3err(X509V3_F_V2I_IPADDRBLOCKS
, X509V3_R_INVALID_IPADDRESS
);
988 X509V3_conf_add_error_name_value(val
);
994 prefixlen
= (int)strtoul(s
+ i2
, &t
, 10);
995 if (t
== s
+ i2
|| *t
!= '\0') {
996 X509V3err(X509V3_F_V2I_IPADDRBLOCKS
,
997 X509V3_R_EXTENSION_VALUE_ERROR
);
998 X509V3_conf_add_error_name_value(val
);
1001 if (!X509v3_addr_add_prefix(addr
, afi
, safi
, min
, prefixlen
)) {
1002 X509V3err(X509V3_F_V2I_IPADDRBLOCKS
, ERR_R_MALLOC_FAILURE
);
1007 i1
= i2
+ strspn(s
+ i2
, " \t");
1008 i2
= i1
+ strspn(s
+ i1
, addr_chars
);
1009 if (i1
== i2
|| s
[i2
] != '\0') {
1010 X509V3err(X509V3_F_V2I_IPADDRBLOCKS
,
1011 X509V3_R_EXTENSION_VALUE_ERROR
);
1012 X509V3_conf_add_error_name_value(val
);
1015 if (a2i_ipadd(max
, s
+ i1
) != length
) {
1016 X509V3err(X509V3_F_V2I_IPADDRBLOCKS
,
1017 X509V3_R_INVALID_IPADDRESS
);
1018 X509V3_conf_add_error_name_value(val
);
1021 if (memcmp(min
, max
, length_from_afi(afi
)) > 0) {
1022 X509V3err(X509V3_F_V2I_IPADDRBLOCKS
,
1023 X509V3_R_EXTENSION_VALUE_ERROR
);
1024 X509V3_conf_add_error_name_value(val
);
1027 if (!X509v3_addr_add_range(addr
, afi
, safi
, min
, max
)) {
1028 X509V3err(X509V3_F_V2I_IPADDRBLOCKS
, ERR_R_MALLOC_FAILURE
);
1033 if (!X509v3_addr_add_prefix(addr
, afi
, safi
, min
, length
* 8)) {
1034 X509V3err(X509V3_F_V2I_IPADDRBLOCKS
, ERR_R_MALLOC_FAILURE
);
1039 X509V3err(X509V3_F_V2I_IPADDRBLOCKS
,
1040 X509V3_R_EXTENSION_VALUE_ERROR
);
1041 X509V3_conf_add_error_name_value(val
);
1050 * Canonize the result, then we're done.
1052 if (!X509v3_addr_canonize(addr
))
1058 sk_IPAddressFamily_pop_free(addr
, IPAddressFamily_free
);
1065 const X509V3_EXT_METHOD v3_addr
= {
1066 NID_sbgp_ipAddrBlock
, /* nid */
1068 ASN1_ITEM_ref(IPAddrBlocks
), /* template */
1069 0, 0, 0, 0, /* old functions, ignored */
1073 v2i_IPAddrBlocks
, /* v2i */
1074 i2r_IPAddrBlocks
, /* i2r */
1076 NULL
/* extension-specific data */
1080 * Figure out whether extension sues inheritance.
1082 int X509v3_addr_inherits(IPAddrBlocks
*addr
)
1087 for (i
= 0; i
< sk_IPAddressFamily_num(addr
); i
++) {
1088 IPAddressFamily
*f
= sk_IPAddressFamily_value(addr
, i
);
1089 if (f
->ipAddressChoice
->type
== IPAddressChoice_inherit
)
1096 * Figure out whether parent contains child.
1098 static int addr_contains(IPAddressOrRanges
*parent
,
1099 IPAddressOrRanges
*child
, int length
)
1101 unsigned char p_min
[ADDR_RAW_BUF_LEN
], p_max
[ADDR_RAW_BUF_LEN
];
1102 unsigned char c_min
[ADDR_RAW_BUF_LEN
], c_max
[ADDR_RAW_BUF_LEN
];
1105 if (child
== NULL
|| parent
== child
)
1111 for (c
= 0; c
< sk_IPAddressOrRange_num(child
); c
++) {
1112 if (!extract_min_max(sk_IPAddressOrRange_value(child
, c
),
1113 c_min
, c_max
, length
))
1116 if (p
>= sk_IPAddressOrRange_num(parent
))
1118 if (!extract_min_max(sk_IPAddressOrRange_value(parent
, p
),
1119 p_min
, p_max
, length
))
1121 if (memcmp(p_max
, c_max
, length
) < 0)
1123 if (memcmp(p_min
, c_min
, length
) > 0)
1133 * Test whether a is a subset of b.
1135 int X509v3_addr_subset(IPAddrBlocks
*a
, IPAddrBlocks
*b
)
1138 if (a
== NULL
|| a
== b
)
1140 if (b
== NULL
|| X509v3_addr_inherits(a
) || X509v3_addr_inherits(b
))
1142 (void)sk_IPAddressFamily_set_cmp_func(b
, IPAddressFamily_cmp
);
1143 for (i
= 0; i
< sk_IPAddressFamily_num(a
); i
++) {
1144 IPAddressFamily
*fa
= sk_IPAddressFamily_value(a
, i
);
1145 int j
= sk_IPAddressFamily_find(b
, fa
);
1146 IPAddressFamily
*fb
;
1147 fb
= sk_IPAddressFamily_value(b
, j
);
1150 if (!addr_contains(fb
->ipAddressChoice
->u
.addressesOrRanges
,
1151 fa
->ipAddressChoice
->u
.addressesOrRanges
,
1152 length_from_afi(X509v3_addr_get_afi(fb
))))
1159 * Validation error handling via callback.
1161 #define validation_err(_err_) \
1163 if (ctx != NULL) { \
1164 ctx->error = _err_; \
1165 ctx->error_depth = i; \
1166 ctx->current_cert = x; \
1167 ret = ctx->verify_cb(0, ctx); \
1176 * Core code for RFC 3779 2.3 path validation.
1178 * Returns 1 for success, 0 on error.
1180 * When returning 0, ctx->error MUST be set to an appropriate value other than
1183 static int addr_validate_path_internal(X509_STORE_CTX
*ctx
,
1184 STACK_OF(X509
) *chain
,
1187 IPAddrBlocks
*child
= NULL
;
1191 if (!ossl_assert(chain
!= NULL
&& sk_X509_num(chain
) > 0)
1192 || !ossl_assert(ctx
!= NULL
|| ext
!= NULL
)
1193 || !ossl_assert(ctx
== NULL
|| ctx
->verify_cb
!= NULL
)) {
1195 ctx
->error
= X509_V_ERR_UNSPECIFIED
;
1200 * Figure out where to start. If we don't have an extension to
1201 * check, we're done. Otherwise, check canonical form and
1202 * set up for walking up the chain.
1209 x
= sk_X509_value(chain
, i
);
1210 if ((ext
= x
->rfc3779_addr
) == NULL
)
1213 if (!X509v3_addr_is_canonical(ext
))
1214 validation_err(X509_V_ERR_INVALID_EXTENSION
);
1215 (void)sk_IPAddressFamily_set_cmp_func(ext
, IPAddressFamily_cmp
);
1216 if ((child
= sk_IPAddressFamily_dup(ext
)) == NULL
) {
1217 X509V3err(X509V3_F_ADDR_VALIDATE_PATH_INTERNAL
,
1218 ERR_R_MALLOC_FAILURE
);
1220 ctx
->error
= X509_V_ERR_OUT_OF_MEM
;
1226 * Now walk up the chain. No cert may list resources that its
1227 * parent doesn't list.
1229 for (i
++; i
< sk_X509_num(chain
); i
++) {
1230 x
= sk_X509_value(chain
, i
);
1231 if (!X509v3_addr_is_canonical(x
->rfc3779_addr
))
1232 validation_err(X509_V_ERR_INVALID_EXTENSION
);
1233 if (x
->rfc3779_addr
== NULL
) {
1234 for (j
= 0; j
< sk_IPAddressFamily_num(child
); j
++) {
1235 IPAddressFamily
*fc
= sk_IPAddressFamily_value(child
, j
);
1236 if (fc
->ipAddressChoice
->type
!= IPAddressChoice_inherit
) {
1237 validation_err(X509_V_ERR_UNNESTED_RESOURCE
);
1243 (void)sk_IPAddressFamily_set_cmp_func(x
->rfc3779_addr
,
1244 IPAddressFamily_cmp
);
1245 for (j
= 0; j
< sk_IPAddressFamily_num(child
); j
++) {
1246 IPAddressFamily
*fc
= sk_IPAddressFamily_value(child
, j
);
1247 int k
= sk_IPAddressFamily_find(x
->rfc3779_addr
, fc
);
1248 IPAddressFamily
*fp
=
1249 sk_IPAddressFamily_value(x
->rfc3779_addr
, k
);
1251 if (fc
->ipAddressChoice
->type
==
1252 IPAddressChoice_addressesOrRanges
) {
1253 validation_err(X509_V_ERR_UNNESTED_RESOURCE
);
1258 if (fp
->ipAddressChoice
->type
==
1259 IPAddressChoice_addressesOrRanges
) {
1260 if (fc
->ipAddressChoice
->type
== IPAddressChoice_inherit
1261 || addr_contains(fp
->ipAddressChoice
->u
.addressesOrRanges
,
1262 fc
->ipAddressChoice
->u
.addressesOrRanges
,
1263 length_from_afi(X509v3_addr_get_afi(fc
))))
1264 sk_IPAddressFamily_set(child
, j
, fp
);
1266 validation_err(X509_V_ERR_UNNESTED_RESOURCE
);
1272 * Trust anchor can't inherit.
1274 if (x
->rfc3779_addr
!= NULL
) {
1275 for (j
= 0; j
< sk_IPAddressFamily_num(x
->rfc3779_addr
); j
++) {
1276 IPAddressFamily
*fp
=
1277 sk_IPAddressFamily_value(x
->rfc3779_addr
, j
);
1278 if (fp
->ipAddressChoice
->type
== IPAddressChoice_inherit
1279 && sk_IPAddressFamily_find(child
, fp
) >= 0)
1280 validation_err(X509_V_ERR_UNNESTED_RESOURCE
);
1285 sk_IPAddressFamily_free(child
);
1289 #undef validation_err
1292 * RFC 3779 2.3 path validation -- called from X509_verify_cert().
1294 int X509v3_addr_validate_path(X509_STORE_CTX
*ctx
)
1296 if (ctx
->chain
== NULL
1297 || sk_X509_num(ctx
->chain
) == 0
1298 || ctx
->verify_cb
== NULL
) {
1299 ctx
->error
= X509_V_ERR_UNSPECIFIED
;
1302 return addr_validate_path_internal(ctx
, ctx
->chain
, NULL
);
1306 * RFC 3779 2.3 path validation of an extension.
1307 * Test whether chain covers extension.
1309 int X509v3_addr_validate_resource_set(STACK_OF(X509
) *chain
,
1310 IPAddrBlocks
*ext
, int allow_inheritance
)
1314 if (chain
== NULL
|| sk_X509_num(chain
) == 0)
1316 if (!allow_inheritance
&& X509v3_addr_inherits(ext
))
1318 return addr_validate_path_internal(NULL
, chain
, ext
);
1321 #endif /* OPENSSL_NO_RFC3779 */