72 #ifndef OPENSSL_NO_RFC3779
111 #define ADDR_RAW_BUF_LEN 16
116 static int length_from_afi(
const unsigned afi)
133 return ((f != NULL &&
145 static int addr_expand(
unsigned char *addr,
148 const unsigned char fill)
154 if ((bs->
flags & 7) != 0) {
155 unsigned char mask = 0xFF >> (8 - (bs->
flags & 7));
157 addr[bs->
length - 1] &= ~mask;
159 addr[bs->
length - 1] |= mask;
169 #define addr_prefixlen(bs) ((int) ((bs)->length * 8 - ((bs)->flags & 7)))
174 static int i2r_address(
BIO *out,
176 const unsigned char fill,
186 if (!addr_expand(addr, bs, 4, fill))
188 BIO_printf(out,
"%d.%d.%d.%d", addr[0], addr[1], addr[2], addr[3]);
191 if (!addr_expand(addr, bs, 16, fill))
193 for (n = 16; n > 1 && addr[n-1] == 0x00 && addr[n-2] == 0x00; n -= 2)
195 for (i = 0; i < n; i += 2)
196 BIO_printf(out,
"%x%s", (addr[i] << 8) | addr[i+1], (i < 14 ?
":" :
""));
203 for (i = 0; i < bs->
length; i++)
214 static int i2r_IPAddressOrRanges(
BIO *out,
216 const IPAddressOrRanges *aors,
250 const IPAddrBlocks *addr =
ext;
263 BIO_printf(out,
"%*sUnknown AFI %u", indent,
"", afi);
275 BIO_puts(out,
" (Unicast/Multicast)");
290 BIO_puts(out,
" (MPLS-labeled VPN)");
304 if (!i2r_IPAddressOrRanges(out,
330 int prefixlen_a = 0, prefixlen_b = 0;
342 prefixlen_a = length * 8;
355 prefixlen_b = length * 8;
359 if ((r = memcmp(addr_a, addr_b, length)) != 0)
362 return prefixlen_a - prefixlen_b;
372 return IPAddressOrRange_cmp(*a, *b, 4);
382 return IPAddressOrRange_cmp(*a, *b, 16);
389 static int range_should_be_prefix(
const unsigned char *
min,
390 const unsigned char *
max,
397 for (i = 0; i < length && min[i] == max[i]; i++)
399 for (j = length - 1; j >= 0 && min[j] == 0x00 && max[j] == 0xFF; j--)
405 mask = min[i] ^ max[i];
407 case 0x01: j = 7;
break;
408 case 0x03: j = 6;
break;
409 case 0x07: j = 5;
break;
410 case 0x0F: j = 4;
break;
411 case 0x1F: j = 3;
break;
412 case 0x3F: j = 2;
break;
413 case 0x7F: j = 1;
break;
416 if ((min[i] & mask) != 0 || (max[i] & mask) != mask)
429 int bytelen = (prefixlen + 7) / 8, bitlen = prefixlen % 8;
451 IPAddressOrRange_free(aor);
468 if ((prefixlen = range_should_be_prefix(min, max, length)) >= 0)
469 return make_addressPrefix(result, min, prefixlen);
471 if ((aor = IPAddressOrRange_new()) == NULL)
484 for (i = length; i > 0 && min[i - 1] == 0x00; --i)
491 unsigned char b = min[i - 1];
493 while ((b & (0xFFU >> j)) != 0)
498 for (i = length; i > 0 && max[i - 1] == 0xFF; --i)
505 unsigned char b = max[i - 1];
507 while ((b & (0xFFU >> j)) != (0xFFU >> j))
516 IPAddressOrRange_free(aor);
525 const unsigned *safi)
528 unsigned char key[3];
532 key[0] = (afi >> 8) & 0xFF;
535 key[2] = *safi & 0xFF;
549 if ((f = IPAddressFamily_new()) == NULL)
565 IPAddressFamily_free(f);
574 const unsigned *safi)
595 static IPAddressOrRanges *make_prefix_or_range(IPAddrBlocks *addr,
597 const unsigned *safi)
600 IPAddressOrRanges *aors = NULL;
631 const unsigned *safi,
635 IPAddressOrRanges *aors = make_prefix_or_range(addr, afi, safi);
637 if (aors == NULL || !make_addressPrefix(&aor, a, prefixlen))
641 IPAddressOrRange_free(aor);
650 const unsigned *safi,
654 IPAddressOrRanges *aors = make_prefix_or_range(addr, afi, safi);
656 int length = length_from_afi(afi);
659 if (!make_addressRange(&aor, min, max, length))
663 IPAddressOrRange_free(aor);
675 if (aor == NULL || min == NULL || max == NULL)
697 int afi_length = length_from_afi(afi);
698 if (aor == NULL || min == NULL || max == NULL ||
699 afi_length == 0 || length < afi_length ||
702 !extract_min_max(aor, min, max, afi_length))
724 int cmp = memcmp(a->
data, b->
data, len);
735 IPAddressOrRanges *aors;
750 if (IPAddressFamily_cmp(&a, &b) >= 0)
786 if (!extract_min_max(a, a_min, a_max, length) ||
787 !extract_min_max(b, b_min, b_max, length))
793 if (memcmp(a_min, b_min, length) >= 0 ||
794 memcmp(a_min, a_max, length) > 0 ||
795 memcmp(b_min, b_max, length) > 0)
802 for (k = length - 1; k >= 0 && b_min[k]-- == 0x00; k--)
804 if (memcmp(a_max, b_min, length) >= 0)
811 range_should_be_prefix(a_min, a_max, length) >= 0)
823 if (!extract_min_max(a, a_min, a_max, length))
825 if (memcmp(a_min, a_max, length) > 0 ||
826 range_should_be_prefix(a_min, a_max, length) >= 0)
841 static int IPAddressOrRanges_canonize(IPAddressOrRanges *aors,
844 int i, j, length = length_from_afi(afi);
860 if (!extract_min_max(a, a_min, a_max, length) ||
861 !extract_min_max(b, b_min, b_max, length))
867 if (memcmp(a_min, a_max, length) > 0 ||
868 memcmp(b_min, b_max, length) > 0)
874 if (memcmp(a_max, b_min, length) >= 0)
881 for (j = length - 1; j >= 0 && b_min[j]-- == 0x00; j--)
883 if (memcmp(a_max, b_min, length) == 0) {
885 if (!make_addressRange(&merged, a_min, b_max, length))
889 IPAddressOrRange_free(a);
890 IPAddressOrRange_free(b);
904 extract_min_max(a, a_min, a_max, length);
905 if (memcmp(a_min, a_max, length) > 0)
935 static void *v2i_IPAddrBlocks(
const struct v3_ext_method *method,
939 static const char v4addr_chars[] =
"0123456789.";
940 static const char v6addr_chars[] =
"0123456789.:abcdefABCDEF";
941 IPAddrBlocks *addr = NULL;
953 unsigned afi, *safi = NULL, safi_;
954 const char *addr_chars;
955 int prefixlen, i1, i2, delim,
length;
975 addr_chars = v4addr_chars;
978 addr_chars = v6addr_chars;
982 length = length_from_afi(afi);
989 *safi = strtoul(val->
value, &t, 0);
990 t += strspn(t,
" \t");
991 if (*safi > 0xFF || *t++ !=
':') {
996 t += strspn(t,
" \t");
1010 if (!strcmp(s,
"inherit")) {
1021 i1 = strspn(s, addr_chars);
1022 i2 = i1 + strspn(s + i1,
" \t");
1034 prefixlen = (int) strtoul(s + i2, &t, 10);
1035 if (t == s + i2 || *t !=
'\0') {
1046 i1 = i2 + strspn(s + i2,
" \t");
1047 i2 = i1 + strspn(s + i1, addr_chars);
1048 if (i1 == i2 || s[i2] !=
'\0') {
1058 if (memcmp(min, max, length_from_afi(afi)) > 0) {
1133 static int addr_contains(IPAddressOrRanges *parent,
1134 IPAddressOrRanges *child,
1141 if (child == NULL || parent == child)
1149 c_min, c_max, length))
1155 p_min, p_max, length))
1157 if (memcmp(p_max, c_max, length) < 0)
1159 if (memcmp(p_min, c_min, length) > 0)
1174 if (a == NULL || a == b)
1197 #define validation_err(_err_) \
1199 if (ctx != NULL) { \
1200 ctx->error = _err_; \
1201 ctx->error_depth = i; \
1202 ctx->current_cert = x; \
1203 ret = ctx->verify_cb(0, ctx); \
1218 IPAddrBlocks *child = NULL;
1238 if ((ext = x->rfc3779_addr) == NULL)
1259 if (x->rfc3779_addr == NULL) {
1297 if (x->rfc3779_addr != NULL) {
1311 #undef validation_err
1318 return v3_addr_validate_path_internal(ctx, ctx->chain, NULL);
1327 int allow_inheritance)
1335 return v3_addr_validate_path_internal(NULL, chain, ext);