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