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