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