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