Blame crypto/x509v3/v3_addr.c

Packit c4476c
/*
Packit c4476c
 * Copyright 2006-2016 The OpenSSL Project Authors. All Rights Reserved.
Packit c4476c
 *
Packit c4476c
 * Licensed under the OpenSSL license (the "License").  You may not use
Packit c4476c
 * this file except in compliance with the License.  You can obtain a copy
Packit c4476c
 * in the file LICENSE in the source distribution or at
Packit c4476c
 * https://www.openssl.org/source/license.html
Packit c4476c
 */
Packit c4476c
Packit c4476c
/*
Packit c4476c
 * Implementation of RFC 3779 section 2.2.
Packit c4476c
 */
Packit c4476c
Packit c4476c
#include <stdio.h>
Packit c4476c
#include <stdlib.h>
Packit c4476c
Packit c4476c
#include "internal/cryptlib.h"
Packit c4476c
#include <openssl/conf.h>
Packit c4476c
#include <openssl/asn1.h>
Packit c4476c
#include <openssl/asn1t.h>
Packit c4476c
#include <openssl/buffer.h>
Packit c4476c
#include <openssl/x509v3.h>
Packit c4476c
#include "crypto/x509.h"
Packit c4476c
#include "ext_dat.h"
Packit c4476c
Packit c4476c
#ifndef OPENSSL_NO_RFC3779
Packit c4476c
Packit c4476c
/*
Packit c4476c
 * OpenSSL ASN.1 template translation of RFC 3779 2.2.3.
Packit c4476c
 */
Packit c4476c
Packit c4476c
ASN1_SEQUENCE(IPAddressRange) = {
Packit c4476c
  ASN1_SIMPLE(IPAddressRange, min, ASN1_BIT_STRING),
Packit c4476c
  ASN1_SIMPLE(IPAddressRange, max, ASN1_BIT_STRING)
Packit c4476c
} ASN1_SEQUENCE_END(IPAddressRange)
Packit c4476c
Packit c4476c
ASN1_CHOICE(IPAddressOrRange) = {
Packit c4476c
  ASN1_SIMPLE(IPAddressOrRange, u.addressPrefix, ASN1_BIT_STRING),
Packit c4476c
  ASN1_SIMPLE(IPAddressOrRange, u.addressRange,  IPAddressRange)
Packit c4476c
} ASN1_CHOICE_END(IPAddressOrRange)
Packit c4476c
Packit c4476c
ASN1_CHOICE(IPAddressChoice) = {
Packit c4476c
  ASN1_SIMPLE(IPAddressChoice,      u.inherit,           ASN1_NULL),
Packit c4476c
  ASN1_SEQUENCE_OF(IPAddressChoice, u.addressesOrRanges, IPAddressOrRange)
Packit c4476c
} ASN1_CHOICE_END(IPAddressChoice)
Packit c4476c
Packit c4476c
ASN1_SEQUENCE(IPAddressFamily) = {
Packit c4476c
  ASN1_SIMPLE(IPAddressFamily, addressFamily,   ASN1_OCTET_STRING),
Packit c4476c
  ASN1_SIMPLE(IPAddressFamily, ipAddressChoice, IPAddressChoice)
Packit c4476c
} ASN1_SEQUENCE_END(IPAddressFamily)
Packit c4476c
Packit c4476c
ASN1_ITEM_TEMPLATE(IPAddrBlocks) =
Packit c4476c
  ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0,
Packit c4476c
                        IPAddrBlocks, IPAddressFamily)
Packit c4476c
static_ASN1_ITEM_TEMPLATE_END(IPAddrBlocks)
Packit c4476c
Packit c4476c
IMPLEMENT_ASN1_FUNCTIONS(IPAddressRange)
Packit c4476c
IMPLEMENT_ASN1_FUNCTIONS(IPAddressOrRange)
Packit c4476c
IMPLEMENT_ASN1_FUNCTIONS(IPAddressChoice)
Packit c4476c
IMPLEMENT_ASN1_FUNCTIONS(IPAddressFamily)
Packit c4476c
Packit c4476c
/*
Packit c4476c
 * How much buffer space do we need for a raw address?
Packit c4476c
 */
Packit c4476c
#define ADDR_RAW_BUF_LEN        16
Packit c4476c
Packit c4476c
/*
Packit c4476c
 * What's the address length associated with this AFI?
Packit c4476c
 */
Packit c4476c
static int length_from_afi(const unsigned afi)
Packit c4476c
{
Packit c4476c
    switch (afi) {
Packit c4476c
    case IANA_AFI_IPV4:
Packit c4476c
        return 4;
Packit c4476c
    case IANA_AFI_IPV6:
Packit c4476c
        return 16;
Packit c4476c
    default:
Packit c4476c
        return 0;
Packit c4476c
    }
Packit c4476c
}
Packit c4476c
Packit c4476c
/*
Packit c4476c
 * Extract the AFI from an IPAddressFamily.
Packit c4476c
 */
Packit c4476c
unsigned int X509v3_addr_get_afi(const IPAddressFamily *f)
Packit c4476c
{
Packit c4476c
    if (f == NULL
Packit c4476c
            || f->addressFamily == NULL
Packit c4476c
            || f->addressFamily->data == NULL
Packit c4476c
            || f->addressFamily->length < 2)
Packit c4476c
        return 0;
Packit c4476c
    return (f->addressFamily->data[0] << 8) | f->addressFamily->data[1];
Packit c4476c
}
Packit c4476c
Packit c4476c
/*
Packit c4476c
 * Expand the bitstring form of an address into a raw byte array.
Packit c4476c
 * At the moment this is coded for simplicity, not speed.
Packit c4476c
 */
Packit c4476c
static int addr_expand(unsigned char *addr,
Packit c4476c
                       const ASN1_BIT_STRING *bs,
Packit c4476c
                       const int length, const unsigned char fill)
Packit c4476c
{
Packit c4476c
    if (bs->length < 0 || bs->length > length)
Packit c4476c
        return 0;
Packit c4476c
    if (bs->length > 0) {
Packit c4476c
        memcpy(addr, bs->data, bs->length);
Packit c4476c
        if ((bs->flags & 7) != 0) {
Packit c4476c
            unsigned char mask = 0xFF >> (8 - (bs->flags & 7));
Packit c4476c
            if (fill == 0)
Packit c4476c
                addr[bs->length - 1] &= ~mask;
Packit c4476c
            else
Packit c4476c
                addr[bs->length - 1] |= mask;
Packit c4476c
        }
Packit c4476c
    }
Packit c4476c
    memset(addr + bs->length, fill, length - bs->length);
Packit c4476c
    return 1;
Packit c4476c
}
Packit c4476c
Packit c4476c
/*
Packit c4476c
 * Extract the prefix length from a bitstring.
Packit c4476c
 */
Packit c4476c
#define addr_prefixlen(bs) ((int) ((bs)->length * 8 - ((bs)->flags & 7)))
Packit c4476c
Packit c4476c
/*
Packit c4476c
 * i2r handler for one address bitstring.
Packit c4476c
 */
Packit c4476c
static int i2r_address(BIO *out,
Packit c4476c
                       const unsigned afi,
Packit c4476c
                       const unsigned char fill, const ASN1_BIT_STRING *bs)
Packit c4476c
{
Packit c4476c
    unsigned char addr[ADDR_RAW_BUF_LEN];
Packit c4476c
    int i, n;
Packit c4476c
Packit c4476c
    if (bs->length < 0)
Packit c4476c
        return 0;
Packit c4476c
    switch (afi) {
Packit c4476c
    case IANA_AFI_IPV4:
Packit c4476c
        if (!addr_expand(addr, bs, 4, fill))
Packit c4476c
            return 0;
Packit c4476c
        BIO_printf(out, "%d.%d.%d.%d", addr[0], addr[1], addr[2], addr[3]);
Packit c4476c
        break;
Packit c4476c
    case IANA_AFI_IPV6:
Packit c4476c
        if (!addr_expand(addr, bs, 16, fill))
Packit c4476c
            return 0;
Packit c4476c
        for (n = 16; n > 1 && addr[n - 1] == 0x00 && addr[n - 2] == 0x00;
Packit c4476c
             n -= 2) ;
Packit c4476c
        for (i = 0; i < n; i += 2)
Packit c4476c
            BIO_printf(out, "%x%s", (addr[i] << 8) | addr[i + 1],
Packit c4476c
                       (i < 14 ? ":" : ""));
Packit c4476c
        if (i < 16)
Packit c4476c
            BIO_puts(out, ":");
Packit c4476c
        if (i == 0)
Packit c4476c
            BIO_puts(out, ":");
Packit c4476c
        break;
Packit c4476c
    default:
Packit c4476c
        for (i = 0; i < bs->length; i++)
Packit c4476c
            BIO_printf(out, "%s%02x", (i > 0 ? ":" : ""), bs->data[i]);
Packit c4476c
        BIO_printf(out, "[%d]", (int)(bs->flags & 7));
Packit c4476c
        break;
Packit c4476c
    }
Packit c4476c
    return 1;
Packit c4476c
}
Packit c4476c
Packit c4476c
/*
Packit c4476c
 * i2r handler for a sequence of addresses and ranges.
Packit c4476c
 */
Packit c4476c
static int i2r_IPAddressOrRanges(BIO *out,
Packit c4476c
                                 const int indent,
Packit c4476c
                                 const IPAddressOrRanges *aors,
Packit c4476c
                                 const unsigned afi)
Packit c4476c
{
Packit c4476c
    int i;
Packit c4476c
    for (i = 0; i < sk_IPAddressOrRange_num(aors); i++) {
Packit c4476c
        const IPAddressOrRange *aor = sk_IPAddressOrRange_value(aors, i);
Packit c4476c
        BIO_printf(out, "%*s", indent, "");
Packit c4476c
        switch (aor->type) {
Packit c4476c
        case IPAddressOrRange_addressPrefix:
Packit c4476c
            if (!i2r_address(out, afi, 0x00, aor->u.addressPrefix))
Packit c4476c
                return 0;
Packit c4476c
            BIO_printf(out, "/%d\n", addr_prefixlen(aor->u.addressPrefix));
Packit c4476c
            continue;
Packit c4476c
        case IPAddressOrRange_addressRange:
Packit c4476c
            if (!i2r_address(out, afi, 0x00, aor->u.addressRange->min))
Packit c4476c
                return 0;
Packit c4476c
            BIO_puts(out, "-");
Packit c4476c
            if (!i2r_address(out, afi, 0xFF, aor->u.addressRange->max))
Packit c4476c
                return 0;
Packit c4476c
            BIO_puts(out, "\n");
Packit c4476c
            continue;
Packit c4476c
        }
Packit c4476c
    }
Packit c4476c
    return 1;
Packit c4476c
}
Packit c4476c
Packit c4476c
/*
Packit c4476c
 * i2r handler for an IPAddrBlocks extension.
Packit c4476c
 */
Packit c4476c
static int i2r_IPAddrBlocks(const X509V3_EXT_METHOD *method,
Packit c4476c
                            void *ext, BIO *out, int indent)
Packit c4476c
{
Packit c4476c
    const IPAddrBlocks *addr = ext;
Packit c4476c
    int i;
Packit c4476c
    for (i = 0; i < sk_IPAddressFamily_num(addr); i++) {
Packit c4476c
        IPAddressFamily *f = sk_IPAddressFamily_value(addr, i);
Packit c4476c
        const unsigned int afi = X509v3_addr_get_afi(f);
Packit c4476c
        switch (afi) {
Packit c4476c
        case IANA_AFI_IPV4:
Packit c4476c
            BIO_printf(out, "%*sIPv4", indent, "");
Packit c4476c
            break;
Packit c4476c
        case IANA_AFI_IPV6:
Packit c4476c
            BIO_printf(out, "%*sIPv6", indent, "");
Packit c4476c
            break;
Packit c4476c
        default:
Packit c4476c
            BIO_printf(out, "%*sUnknown AFI %u", indent, "", afi);
Packit c4476c
            break;
Packit c4476c
        }
Packit c4476c
        if (f->addressFamily->length > 2) {
Packit c4476c
            switch (f->addressFamily->data[2]) {
Packit c4476c
            case 1:
Packit c4476c
                BIO_puts(out, " (Unicast)");
Packit c4476c
                break;
Packit c4476c
            case 2:
Packit c4476c
                BIO_puts(out, " (Multicast)");
Packit c4476c
                break;
Packit c4476c
            case 3:
Packit c4476c
                BIO_puts(out, " (Unicast/Multicast)");
Packit c4476c
                break;
Packit c4476c
            case 4:
Packit c4476c
                BIO_puts(out, " (MPLS)");
Packit c4476c
                break;
Packit c4476c
            case 64:
Packit c4476c
                BIO_puts(out, " (Tunnel)");
Packit c4476c
                break;
Packit c4476c
            case 65:
Packit c4476c
                BIO_puts(out, " (VPLS)");
Packit c4476c
                break;
Packit c4476c
            case 66:
Packit c4476c
                BIO_puts(out, " (BGP MDT)");
Packit c4476c
                break;
Packit c4476c
            case 128:
Packit c4476c
                BIO_puts(out, " (MPLS-labeled VPN)");
Packit c4476c
                break;
Packit c4476c
            default:
Packit c4476c
                BIO_printf(out, " (Unknown SAFI %u)",
Packit c4476c
                           (unsigned)f->addressFamily->data[2]);
Packit c4476c
                break;
Packit c4476c
            }
Packit c4476c
        }
Packit c4476c
        switch (f->ipAddressChoice->type) {
Packit c4476c
        case IPAddressChoice_inherit:
Packit c4476c
            BIO_puts(out, ": inherit\n");
Packit c4476c
            break;
Packit c4476c
        case IPAddressChoice_addressesOrRanges:
Packit c4476c
            BIO_puts(out, ":\n");
Packit c4476c
            if (!i2r_IPAddressOrRanges(out,
Packit c4476c
                                       indent + 2,
Packit c4476c
                                       f->ipAddressChoice->
Packit c4476c
                                       u.addressesOrRanges, afi))
Packit c4476c
                return 0;
Packit c4476c
            break;
Packit c4476c
        }
Packit c4476c
    }
Packit c4476c
    return 1;
Packit c4476c
}
Packit c4476c
Packit c4476c
/*
Packit c4476c
 * Sort comparison function for a sequence of IPAddressOrRange
Packit c4476c
 * elements.
Packit c4476c
 *
Packit c4476c
 * There's no sane answer we can give if addr_expand() fails, and an
Packit c4476c
 * assertion failure on externally supplied data is seriously uncool,
Packit c4476c
 * so we just arbitrarily declare that if given invalid inputs this
Packit c4476c
 * function returns -1.  If this messes up your preferred sort order
Packit c4476c
 * for garbage input, tough noogies.
Packit c4476c
 */
Packit c4476c
static int IPAddressOrRange_cmp(const IPAddressOrRange *a,
Packit c4476c
                                const IPAddressOrRange *b, const int length)
Packit c4476c
{
Packit c4476c
    unsigned char addr_a[ADDR_RAW_BUF_LEN], addr_b[ADDR_RAW_BUF_LEN];
Packit c4476c
    int prefixlen_a = 0, prefixlen_b = 0;
Packit c4476c
    int r;
Packit c4476c
Packit c4476c
    switch (a->type) {
Packit c4476c
    case IPAddressOrRange_addressPrefix:
Packit c4476c
        if (!addr_expand(addr_a, a->u.addressPrefix, length, 0x00))
Packit c4476c
            return -1;
Packit c4476c
        prefixlen_a = addr_prefixlen(a->u.addressPrefix);
Packit c4476c
        break;
Packit c4476c
    case IPAddressOrRange_addressRange:
Packit c4476c
        if (!addr_expand(addr_a, a->u.addressRange->min, length, 0x00))
Packit c4476c
            return -1;
Packit c4476c
        prefixlen_a = length * 8;
Packit c4476c
        break;
Packit c4476c
    }
Packit c4476c
Packit c4476c
    switch (b->type) {
Packit c4476c
    case IPAddressOrRange_addressPrefix:
Packit c4476c
        if (!addr_expand(addr_b, b->u.addressPrefix, length, 0x00))
Packit c4476c
            return -1;
Packit c4476c
        prefixlen_b = addr_prefixlen(b->u.addressPrefix);
Packit c4476c
        break;
Packit c4476c
    case IPAddressOrRange_addressRange:
Packit c4476c
        if (!addr_expand(addr_b, b->u.addressRange->min, length, 0x00))
Packit c4476c
            return -1;
Packit c4476c
        prefixlen_b = length * 8;
Packit c4476c
        break;
Packit c4476c
    }
Packit c4476c
Packit c4476c
    if ((r = memcmp(addr_a, addr_b, length)) != 0)
Packit c4476c
        return r;
Packit c4476c
    else
Packit c4476c
        return prefixlen_a - prefixlen_b;
Packit c4476c
}
Packit c4476c
Packit c4476c
/*
Packit c4476c
 * IPv4-specific closure over IPAddressOrRange_cmp, since sk_sort()
Packit c4476c
 * comparison routines are only allowed two arguments.
Packit c4476c
 */
Packit c4476c
static int v4IPAddressOrRange_cmp(const IPAddressOrRange *const *a,
Packit c4476c
                                  const IPAddressOrRange *const *b)
Packit c4476c
{
Packit c4476c
    return IPAddressOrRange_cmp(*a, *b, 4);
Packit c4476c
}
Packit c4476c
Packit c4476c
/*
Packit c4476c
 * IPv6-specific closure over IPAddressOrRange_cmp, since sk_sort()
Packit c4476c
 * comparison routines are only allowed two arguments.
Packit c4476c
 */
Packit c4476c
static int v6IPAddressOrRange_cmp(const IPAddressOrRange *const *a,
Packit c4476c
                                  const IPAddressOrRange *const *b)
Packit c4476c
{
Packit c4476c
    return IPAddressOrRange_cmp(*a, *b, 16);
Packit c4476c
}
Packit c4476c
Packit c4476c
/*
Packit c4476c
 * Calculate whether a range collapses to a prefix.
Packit c4476c
 * See last paragraph of RFC 3779 2.2.3.7.
Packit c4476c
 */
Packit c4476c
static int range_should_be_prefix(const unsigned char *min,
Packit c4476c
                                  const unsigned char *max, const int length)
Packit c4476c
{
Packit c4476c
    unsigned char mask;
Packit c4476c
    int i, j;
Packit c4476c
Packit c4476c
    if (memcmp(min, max, length) <= 0)
Packit c4476c
        return -1;
Packit c4476c
    for (i = 0; i < length && min[i] == max[i]; i++) ;
Packit c4476c
    for (j = length - 1; j >= 0 && min[j] == 0x00 && max[j] == 0xFF; j--) ;
Packit c4476c
    if (i < j)
Packit c4476c
        return -1;
Packit c4476c
    if (i > j)
Packit c4476c
        return i * 8;
Packit c4476c
    mask = min[i] ^ max[i];
Packit c4476c
    switch (mask) {
Packit c4476c
    case 0x01:
Packit c4476c
        j = 7;
Packit c4476c
        break;
Packit c4476c
    case 0x03:
Packit c4476c
        j = 6;
Packit c4476c
        break;
Packit c4476c
    case 0x07:
Packit c4476c
        j = 5;
Packit c4476c
        break;
Packit c4476c
    case 0x0F:
Packit c4476c
        j = 4;
Packit c4476c
        break;
Packit c4476c
    case 0x1F:
Packit c4476c
        j = 3;
Packit c4476c
        break;
Packit c4476c
    case 0x3F:
Packit c4476c
        j = 2;
Packit c4476c
        break;
Packit c4476c
    case 0x7F:
Packit c4476c
        j = 1;
Packit c4476c
        break;
Packit c4476c
    default:
Packit c4476c
        return -1;
Packit c4476c
    }
Packit c4476c
    if ((min[i] & mask) != 0 || (max[i] & mask) != mask)
Packit c4476c
        return -1;
Packit c4476c
    else
Packit c4476c
        return i * 8 + j;
Packit c4476c
}
Packit c4476c
Packit c4476c
/*
Packit c4476c
 * Construct a prefix.
Packit c4476c
 */
Packit c4476c
static int make_addressPrefix(IPAddressOrRange **result,
Packit c4476c
                              unsigned char *addr, const int prefixlen)
Packit c4476c
{
Packit c4476c
    int bytelen = (prefixlen + 7) / 8, bitlen = prefixlen % 8;
Packit c4476c
    IPAddressOrRange *aor = IPAddressOrRange_new();
Packit c4476c
Packit c4476c
    if (aor == NULL)
Packit c4476c
        return 0;
Packit c4476c
    aor->type = IPAddressOrRange_addressPrefix;
Packit c4476c
    if (aor->u.addressPrefix == NULL &&
Packit c4476c
        (aor->u.addressPrefix = ASN1_BIT_STRING_new()) == NULL)
Packit c4476c
        goto err;
Packit c4476c
    if (!ASN1_BIT_STRING_set(aor->u.addressPrefix, addr, bytelen))
Packit c4476c
        goto err;
Packit c4476c
    aor->u.addressPrefix->flags &= ~7;
Packit c4476c
    aor->u.addressPrefix->flags |= ASN1_STRING_FLAG_BITS_LEFT;
Packit c4476c
    if (bitlen > 0) {
Packit c4476c
        aor->u.addressPrefix->data[bytelen - 1] &= ~(0xFF >> bitlen);
Packit c4476c
        aor->u.addressPrefix->flags |= 8 - bitlen;
Packit c4476c
    }
Packit c4476c
Packit c4476c
    *result = aor;
Packit c4476c
    return 1;
Packit c4476c
Packit c4476c
 err:
Packit c4476c
    IPAddressOrRange_free(aor);
Packit c4476c
    return 0;
Packit c4476c
}
Packit c4476c
Packit c4476c
/*
Packit c4476c
 * Construct a range.  If it can be expressed as a prefix,
Packit c4476c
 * return a prefix instead.  Doing this here simplifies
Packit c4476c
 * the rest of the code considerably.
Packit c4476c
 */
Packit c4476c
static int make_addressRange(IPAddressOrRange **result,
Packit c4476c
                             unsigned char *min,
Packit c4476c
                             unsigned char *max, const int length)
Packit c4476c
{
Packit c4476c
    IPAddressOrRange *aor;
Packit c4476c
    int i, prefixlen;
Packit c4476c
Packit c4476c
    if ((prefixlen = range_should_be_prefix(min, max, length)) >= 0)
Packit c4476c
        return make_addressPrefix(result, min, prefixlen);
Packit c4476c
Packit c4476c
    if ((aor = IPAddressOrRange_new()) == NULL)
Packit c4476c
        return 0;
Packit c4476c
    aor->type = IPAddressOrRange_addressRange;
Packit c4476c
    if ((aor->u.addressRange = IPAddressRange_new()) == NULL)
Packit c4476c
        goto err;
Packit c4476c
    if (aor->u.addressRange->min == NULL &&
Packit c4476c
        (aor->u.addressRange->min = ASN1_BIT_STRING_new()) == NULL)
Packit c4476c
        goto err;
Packit c4476c
    if (aor->u.addressRange->max == NULL &&
Packit c4476c
        (aor->u.addressRange->max = ASN1_BIT_STRING_new()) == NULL)
Packit c4476c
        goto err;
Packit c4476c
Packit c4476c
    for (i = length; i > 0 && min[i - 1] == 0x00; --i) ;
Packit c4476c
    if (!ASN1_BIT_STRING_set(aor->u.addressRange->min, min, i))
Packit c4476c
        goto err;
Packit c4476c
    aor->u.addressRange->min->flags &= ~7;
Packit c4476c
    aor->u.addressRange->min->flags |= ASN1_STRING_FLAG_BITS_LEFT;
Packit c4476c
    if (i > 0) {
Packit c4476c
        unsigned char b = min[i - 1];
Packit c4476c
        int j = 1;
Packit c4476c
        while ((b & (0xFFU >> j)) != 0)
Packit c4476c
            ++j;
Packit c4476c
        aor->u.addressRange->min->flags |= 8 - j;
Packit c4476c
    }
Packit c4476c
Packit c4476c
    for (i = length; i > 0 && max[i - 1] == 0xFF; --i) ;
Packit c4476c
    if (!ASN1_BIT_STRING_set(aor->u.addressRange->max, max, i))
Packit c4476c
        goto err;
Packit c4476c
    aor->u.addressRange->max->flags &= ~7;
Packit c4476c
    aor->u.addressRange->max->flags |= ASN1_STRING_FLAG_BITS_LEFT;
Packit c4476c
    if (i > 0) {
Packit c4476c
        unsigned char b = max[i - 1];
Packit c4476c
        int j = 1;
Packit c4476c
        while ((b & (0xFFU >> j)) != (0xFFU >> j))
Packit c4476c
            ++j;
Packit c4476c
        aor->u.addressRange->max->flags |= 8 - j;
Packit c4476c
    }
Packit c4476c
Packit c4476c
    *result = aor;
Packit c4476c
    return 1;
Packit c4476c
Packit c4476c
 err:
Packit c4476c
    IPAddressOrRange_free(aor);
Packit c4476c
    return 0;
Packit c4476c
}
Packit c4476c
Packit c4476c
/*
Packit c4476c
 * Construct a new address family or find an existing one.
Packit c4476c
 */
Packit c4476c
static IPAddressFamily *make_IPAddressFamily(IPAddrBlocks *addr,
Packit c4476c
                                             const unsigned afi,
Packit c4476c
                                             const unsigned *safi)
Packit c4476c
{
Packit c4476c
    IPAddressFamily *f;
Packit c4476c
    unsigned char key[3];
Packit c4476c
    int keylen;
Packit c4476c
    int i;
Packit c4476c
Packit c4476c
    key[0] = (afi >> 8) & 0xFF;
Packit c4476c
    key[1] = afi & 0xFF;
Packit c4476c
    if (safi != NULL) {
Packit c4476c
        key[2] = *safi & 0xFF;
Packit c4476c
        keylen = 3;
Packit c4476c
    } else {
Packit c4476c
        keylen = 2;
Packit c4476c
    }
Packit c4476c
Packit c4476c
    for (i = 0; i < sk_IPAddressFamily_num(addr); i++) {
Packit c4476c
        f = sk_IPAddressFamily_value(addr, i);
Packit c4476c
        if (f->addressFamily->length == keylen &&
Packit c4476c
            !memcmp(f->addressFamily->data, key, keylen))
Packit c4476c
            return f;
Packit c4476c
    }
Packit c4476c
Packit c4476c
    if ((f = IPAddressFamily_new()) == NULL)
Packit c4476c
        goto err;
Packit c4476c
    if (f->ipAddressChoice == NULL &&
Packit c4476c
        (f->ipAddressChoice = IPAddressChoice_new()) == NULL)
Packit c4476c
        goto err;
Packit c4476c
    if (f->addressFamily == NULL &&
Packit c4476c
        (f->addressFamily = ASN1_OCTET_STRING_new()) == NULL)
Packit c4476c
        goto err;
Packit c4476c
    if (!ASN1_OCTET_STRING_set(f->addressFamily, key, keylen))
Packit c4476c
        goto err;
Packit c4476c
    if (!sk_IPAddressFamily_push(addr, f))
Packit c4476c
        goto err;
Packit c4476c
Packit c4476c
    return f;
Packit c4476c
Packit c4476c
 err:
Packit c4476c
    IPAddressFamily_free(f);
Packit c4476c
    return NULL;
Packit c4476c
}
Packit c4476c
Packit c4476c
/*
Packit c4476c
 * Add an inheritance element.
Packit c4476c
 */
Packit c4476c
int X509v3_addr_add_inherit(IPAddrBlocks *addr,
Packit c4476c
                            const unsigned afi, const unsigned *safi)
Packit c4476c
{
Packit c4476c
    IPAddressFamily *f = make_IPAddressFamily(addr, afi, safi);
Packit c4476c
    if (f == NULL ||
Packit c4476c
        f->ipAddressChoice == NULL ||
Packit c4476c
        (f->ipAddressChoice->type == IPAddressChoice_addressesOrRanges &&
Packit c4476c
         f->ipAddressChoice->u.addressesOrRanges != NULL))
Packit c4476c
        return 0;
Packit c4476c
    if (f->ipAddressChoice->type == IPAddressChoice_inherit &&
Packit c4476c
        f->ipAddressChoice->u.inherit != NULL)
Packit c4476c
        return 1;
Packit c4476c
    if (f->ipAddressChoice->u.inherit == NULL &&
Packit c4476c
        (f->ipAddressChoice->u.inherit = ASN1_NULL_new()) == NULL)
Packit c4476c
        return 0;
Packit c4476c
    f->ipAddressChoice->type = IPAddressChoice_inherit;
Packit c4476c
    return 1;
Packit c4476c
}
Packit c4476c
Packit c4476c
/*
Packit c4476c
 * Construct an IPAddressOrRange sequence, or return an existing one.
Packit c4476c
 */
Packit c4476c
static IPAddressOrRanges *make_prefix_or_range(IPAddrBlocks *addr,
Packit c4476c
                                               const unsigned afi,
Packit c4476c
                                               const unsigned *safi)
Packit c4476c
{
Packit c4476c
    IPAddressFamily *f = make_IPAddressFamily(addr, afi, safi);
Packit c4476c
    IPAddressOrRanges *aors = NULL;
Packit c4476c
Packit c4476c
    if (f == NULL ||
Packit c4476c
        f->ipAddressChoice == NULL ||
Packit c4476c
        (f->ipAddressChoice->type == IPAddressChoice_inherit &&
Packit c4476c
         f->ipAddressChoice->u.inherit != NULL))
Packit c4476c
        return NULL;
Packit c4476c
    if (f->ipAddressChoice->type == IPAddressChoice_addressesOrRanges)
Packit c4476c
        aors = f->ipAddressChoice->u.addressesOrRanges;
Packit c4476c
    if (aors != NULL)
Packit c4476c
        return aors;
Packit c4476c
    if ((aors = sk_IPAddressOrRange_new_null()) == NULL)
Packit c4476c
        return NULL;
Packit c4476c
    switch (afi) {
Packit c4476c
    case IANA_AFI_IPV4:
Packit c4476c
        (void)sk_IPAddressOrRange_set_cmp_func(aors, v4IPAddressOrRange_cmp);
Packit c4476c
        break;
Packit c4476c
    case IANA_AFI_IPV6:
Packit c4476c
        (void)sk_IPAddressOrRange_set_cmp_func(aors, v6IPAddressOrRange_cmp);
Packit c4476c
        break;
Packit c4476c
    }
Packit c4476c
    f->ipAddressChoice->type = IPAddressChoice_addressesOrRanges;
Packit c4476c
    f->ipAddressChoice->u.addressesOrRanges = aors;
Packit c4476c
    return aors;
Packit c4476c
}
Packit c4476c
Packit c4476c
/*
Packit c4476c
 * Add a prefix.
Packit c4476c
 */
Packit c4476c
int X509v3_addr_add_prefix(IPAddrBlocks *addr,
Packit c4476c
                           const unsigned afi,
Packit c4476c
                           const unsigned *safi,
Packit c4476c
                           unsigned char *a, const int prefixlen)
Packit c4476c
{
Packit c4476c
    IPAddressOrRanges *aors = make_prefix_or_range(addr, afi, safi);
Packit c4476c
    IPAddressOrRange *aor;
Packit c4476c
    if (aors == NULL || !make_addressPrefix(&aor, a, prefixlen))
Packit c4476c
        return 0;
Packit c4476c
    if (sk_IPAddressOrRange_push(aors, aor))
Packit c4476c
        return 1;
Packit c4476c
    IPAddressOrRange_free(aor);
Packit c4476c
    return 0;
Packit c4476c
}
Packit c4476c
Packit c4476c
/*
Packit c4476c
 * Add a range.
Packit c4476c
 */
Packit c4476c
int X509v3_addr_add_range(IPAddrBlocks *addr,
Packit c4476c
                          const unsigned afi,
Packit c4476c
                          const unsigned *safi,
Packit c4476c
                          unsigned char *min, unsigned char *max)
Packit c4476c
{
Packit c4476c
    IPAddressOrRanges *aors = make_prefix_or_range(addr, afi, safi);
Packit c4476c
    IPAddressOrRange *aor;
Packit c4476c
    int length = length_from_afi(afi);
Packit c4476c
    if (aors == NULL)
Packit c4476c
        return 0;
Packit c4476c
    if (!make_addressRange(&aor, min, max, length))
Packit c4476c
        return 0;
Packit c4476c
    if (sk_IPAddressOrRange_push(aors, aor))
Packit c4476c
        return 1;
Packit c4476c
    IPAddressOrRange_free(aor);
Packit c4476c
    return 0;
Packit c4476c
}
Packit c4476c
Packit c4476c
/*
Packit c4476c
 * Extract min and max values from an IPAddressOrRange.
Packit c4476c
 */
Packit c4476c
static int extract_min_max(IPAddressOrRange *aor,
Packit c4476c
                           unsigned char *min, unsigned char *max, int length)
Packit c4476c
{
Packit c4476c
    if (aor == NULL || min == NULL || max == NULL)
Packit c4476c
        return 0;
Packit c4476c
    switch (aor->type) {
Packit c4476c
    case IPAddressOrRange_addressPrefix:
Packit c4476c
        return (addr_expand(min, aor->u.addressPrefix, length, 0x00) &&
Packit c4476c
                addr_expand(max, aor->u.addressPrefix, length, 0xFF));
Packit c4476c
    case IPAddressOrRange_addressRange:
Packit c4476c
        return (addr_expand(min, aor->u.addressRange->min, length, 0x00) &&
Packit c4476c
                addr_expand(max, aor->u.addressRange->max, length, 0xFF));
Packit c4476c
    }
Packit c4476c
    return 0;
Packit c4476c
}
Packit c4476c
Packit c4476c
/*
Packit c4476c
 * Public wrapper for extract_min_max().
Packit c4476c
 */
Packit c4476c
int X509v3_addr_get_range(IPAddressOrRange *aor,
Packit c4476c
                          const unsigned afi,
Packit c4476c
                          unsigned char *min,
Packit c4476c
                          unsigned char *max, const int length)
Packit c4476c
{
Packit c4476c
    int afi_length = length_from_afi(afi);
Packit c4476c
    if (aor == NULL || min == NULL || max == NULL ||
Packit c4476c
        afi_length == 0 || length < afi_length ||
Packit c4476c
        (aor->type != IPAddressOrRange_addressPrefix &&
Packit c4476c
         aor->type != IPAddressOrRange_addressRange) ||
Packit c4476c
        !extract_min_max(aor, min, max, afi_length))
Packit c4476c
        return 0;
Packit c4476c
Packit c4476c
    return afi_length;
Packit c4476c
}
Packit c4476c
Packit c4476c
/*
Packit c4476c
 * Sort comparison function for a sequence of IPAddressFamily.
Packit c4476c
 *
Packit c4476c
 * The last paragraph of RFC 3779 2.2.3.3 is slightly ambiguous about
Packit c4476c
 * the ordering: I can read it as meaning that IPv6 without a SAFI
Packit c4476c
 * comes before IPv4 with a SAFI, which seems pretty weird.  The
Packit c4476c
 * examples in appendix B suggest that the author intended the
Packit c4476c
 * null-SAFI rule to apply only within a single AFI, which is what I
Packit c4476c
 * would have expected and is what the following code implements.
Packit c4476c
 */
Packit c4476c
static int IPAddressFamily_cmp(const IPAddressFamily *const *a_,
Packit c4476c
                               const IPAddressFamily *const *b_)
Packit c4476c
{
Packit c4476c
    const ASN1_OCTET_STRING *a = (*a_)->addressFamily;
Packit c4476c
    const ASN1_OCTET_STRING *b = (*b_)->addressFamily;
Packit c4476c
    int len = ((a->length <= b->length) ? a->length : b->length);
Packit c4476c
    int cmp = memcmp(a->data, b->data, len);
Packit c4476c
    return cmp ? cmp : a->length - b->length;
Packit c4476c
}
Packit c4476c
Packit c4476c
/*
Packit c4476c
 * Check whether an IPAddrBLocks is in canonical form.
Packit c4476c
 */
Packit c4476c
int X509v3_addr_is_canonical(IPAddrBlocks *addr)
Packit c4476c
{
Packit c4476c
    unsigned char a_min[ADDR_RAW_BUF_LEN], a_max[ADDR_RAW_BUF_LEN];
Packit c4476c
    unsigned char b_min[ADDR_RAW_BUF_LEN], b_max[ADDR_RAW_BUF_LEN];
Packit c4476c
    IPAddressOrRanges *aors;
Packit c4476c
    int i, j, k;
Packit c4476c
Packit c4476c
    /*
Packit c4476c
     * Empty extension is canonical.
Packit c4476c
     */
Packit c4476c
    if (addr == NULL)
Packit c4476c
        return 1;
Packit c4476c
Packit c4476c
    /*
Packit c4476c
     * Check whether the top-level list is in order.
Packit c4476c
     */
Packit c4476c
    for (i = 0; i < sk_IPAddressFamily_num(addr) - 1; i++) {
Packit c4476c
        const IPAddressFamily *a = sk_IPAddressFamily_value(addr, i);
Packit c4476c
        const IPAddressFamily *b = sk_IPAddressFamily_value(addr, i + 1);
Packit c4476c
        if (IPAddressFamily_cmp(&a, &b) >= 0)
Packit c4476c
            return 0;
Packit c4476c
    }
Packit c4476c
Packit c4476c
    /*
Packit c4476c
     * Top level's ok, now check each address family.
Packit c4476c
     */
Packit c4476c
    for (i = 0; i < sk_IPAddressFamily_num(addr); i++) {
Packit c4476c
        IPAddressFamily *f = sk_IPAddressFamily_value(addr, i);
Packit c4476c
        int length = length_from_afi(X509v3_addr_get_afi(f));
Packit c4476c
Packit c4476c
        /*
Packit c4476c
         * Inheritance is canonical.  Anything other than inheritance or
Packit c4476c
         * a SEQUENCE OF IPAddressOrRange is an ASN.1 error or something.
Packit c4476c
         */
Packit c4476c
        if (f == NULL || f->ipAddressChoice == NULL)
Packit c4476c
            return 0;
Packit c4476c
        switch (f->ipAddressChoice->type) {
Packit c4476c
        case IPAddressChoice_inherit:
Packit c4476c
            continue;
Packit c4476c
        case IPAddressChoice_addressesOrRanges:
Packit c4476c
            break;
Packit c4476c
        default:
Packit c4476c
            return 0;
Packit c4476c
        }
Packit c4476c
Packit c4476c
        /*
Packit c4476c
         * It's an IPAddressOrRanges sequence, check it.
Packit c4476c
         */
Packit c4476c
        aors = f->ipAddressChoice->u.addressesOrRanges;
Packit c4476c
        if (sk_IPAddressOrRange_num(aors) == 0)
Packit c4476c
            return 0;
Packit c4476c
        for (j = 0; j < sk_IPAddressOrRange_num(aors) - 1; j++) {
Packit c4476c
            IPAddressOrRange *a = sk_IPAddressOrRange_value(aors, j);
Packit c4476c
            IPAddressOrRange *b = sk_IPAddressOrRange_value(aors, j + 1);
Packit c4476c
Packit c4476c
            if (!extract_min_max(a, a_min, a_max, length) ||
Packit c4476c
                !extract_min_max(b, b_min, b_max, length))
Packit c4476c
                return 0;
Packit c4476c
Packit c4476c
            /*
Packit c4476c
             * Punt misordered list, overlapping start, or inverted range.
Packit c4476c
             */
Packit c4476c
            if (memcmp(a_min, b_min, length) >= 0 ||
Packit c4476c
                memcmp(a_min, a_max, length) > 0 ||
Packit c4476c
                memcmp(b_min, b_max, length) > 0)
Packit c4476c
                return 0;
Packit c4476c
Packit c4476c
            /*
Packit c4476c
             * Punt if adjacent or overlapping.  Check for adjacency by
Packit c4476c
             * subtracting one from b_min first.
Packit c4476c
             */
Packit c4476c
            for (k = length - 1; k >= 0 && b_min[k]-- == 0x00; k--) ;
Packit c4476c
            if (memcmp(a_max, b_min, length) >= 0)
Packit c4476c
                return 0;
Packit c4476c
Packit c4476c
            /*
Packit c4476c
             * Check for range that should be expressed as a prefix.
Packit c4476c
             */
Packit c4476c
            if (a->type == IPAddressOrRange_addressRange &&
Packit c4476c
                range_should_be_prefix(a_min, a_max, length) >= 0)
Packit c4476c
                return 0;
Packit c4476c
        }
Packit c4476c
Packit c4476c
        /*
Packit c4476c
         * Check range to see if it's inverted or should be a
Packit c4476c
         * prefix.
Packit c4476c
         */
Packit c4476c
        j = sk_IPAddressOrRange_num(aors) - 1;
Packit c4476c
        {
Packit c4476c
            IPAddressOrRange *a = sk_IPAddressOrRange_value(aors, j);
Packit c4476c
            if (a != NULL && a->type == IPAddressOrRange_addressRange) {
Packit c4476c
                if (!extract_min_max(a, a_min, a_max, length))
Packit c4476c
                    return 0;
Packit c4476c
                if (memcmp(a_min, a_max, length) > 0 ||
Packit c4476c
                    range_should_be_prefix(a_min, a_max, length) >= 0)
Packit c4476c
                    return 0;
Packit c4476c
            }
Packit c4476c
        }
Packit c4476c
    }
Packit c4476c
Packit c4476c
    /*
Packit c4476c
     * If we made it through all that, we're happy.
Packit c4476c
     */
Packit c4476c
    return 1;
Packit c4476c
}
Packit c4476c
Packit c4476c
/*
Packit c4476c
 * Whack an IPAddressOrRanges into canonical form.
Packit c4476c
 */
Packit c4476c
static int IPAddressOrRanges_canonize(IPAddressOrRanges *aors,
Packit c4476c
                                      const unsigned afi)
Packit c4476c
{
Packit c4476c
    int i, j, length = length_from_afi(afi);
Packit c4476c
Packit c4476c
    /*
Packit c4476c
     * Sort the IPAddressOrRanges sequence.
Packit c4476c
     */
Packit c4476c
    sk_IPAddressOrRange_sort(aors);
Packit c4476c
Packit c4476c
    /*
Packit c4476c
     * Clean up representation issues, punt on duplicates or overlaps.
Packit c4476c
     */
Packit c4476c
    for (i = 0; i < sk_IPAddressOrRange_num(aors) - 1; i++) {
Packit c4476c
        IPAddressOrRange *a = sk_IPAddressOrRange_value(aors, i);
Packit c4476c
        IPAddressOrRange *b = sk_IPAddressOrRange_value(aors, i + 1);
Packit c4476c
        unsigned char a_min[ADDR_RAW_BUF_LEN], a_max[ADDR_RAW_BUF_LEN];
Packit c4476c
        unsigned char b_min[ADDR_RAW_BUF_LEN], b_max[ADDR_RAW_BUF_LEN];
Packit c4476c
Packit c4476c
        if (!extract_min_max(a, a_min, a_max, length) ||
Packit c4476c
            !extract_min_max(b, b_min, b_max, length))
Packit c4476c
            return 0;
Packit c4476c
Packit c4476c
        /*
Packit c4476c
         * Punt inverted ranges.
Packit c4476c
         */
Packit c4476c
        if (memcmp(a_min, a_max, length) > 0 ||
Packit c4476c
            memcmp(b_min, b_max, length) > 0)
Packit c4476c
            return 0;
Packit c4476c
Packit c4476c
        /*
Packit c4476c
         * Punt overlaps.
Packit c4476c
         */
Packit c4476c
        if (memcmp(a_max, b_min, length) >= 0)
Packit c4476c
            return 0;
Packit c4476c
Packit c4476c
        /*
Packit c4476c
         * Merge if a and b are adjacent.  We check for
Packit c4476c
         * adjacency by subtracting one from b_min first.
Packit c4476c
         */
Packit c4476c
        for (j = length - 1; j >= 0 && b_min[j]-- == 0x00; j--) ;
Packit c4476c
        if (memcmp(a_max, b_min, length) == 0) {
Packit c4476c
            IPAddressOrRange *merged;
Packit c4476c
            if (!make_addressRange(&merged, a_min, b_max, length))
Packit c4476c
                return 0;
Packit c4476c
            (void)sk_IPAddressOrRange_set(aors, i, merged);
Packit c4476c
            (void)sk_IPAddressOrRange_delete(aors, i + 1);
Packit c4476c
            IPAddressOrRange_free(a);
Packit c4476c
            IPAddressOrRange_free(b);
Packit c4476c
            --i;
Packit c4476c
            continue;
Packit c4476c
        }
Packit c4476c
    }
Packit c4476c
Packit c4476c
    /*
Packit c4476c
     * Check for inverted final range.
Packit c4476c
     */
Packit c4476c
    j = sk_IPAddressOrRange_num(aors) - 1;
Packit c4476c
    {
Packit c4476c
        IPAddressOrRange *a = sk_IPAddressOrRange_value(aors, j);
Packit c4476c
        if (a != NULL && a->type == IPAddressOrRange_addressRange) {
Packit c4476c
            unsigned char a_min[ADDR_RAW_BUF_LEN], a_max[ADDR_RAW_BUF_LEN];
Packit c4476c
            if (!extract_min_max(a, a_min, a_max, length))
Packit c4476c
                return 0;
Packit c4476c
            if (memcmp(a_min, a_max, length) > 0)
Packit c4476c
                return 0;
Packit c4476c
        }
Packit c4476c
    }
Packit c4476c
Packit c4476c
    return 1;
Packit c4476c
}
Packit c4476c
Packit c4476c
/*
Packit c4476c
 * Whack an IPAddrBlocks extension into canonical form.
Packit c4476c
 */
Packit c4476c
int X509v3_addr_canonize(IPAddrBlocks *addr)
Packit c4476c
{
Packit c4476c
    int i;
Packit c4476c
    for (i = 0; i < sk_IPAddressFamily_num(addr); i++) {
Packit c4476c
        IPAddressFamily *f = sk_IPAddressFamily_value(addr, i);
Packit c4476c
        if (f->ipAddressChoice->type == IPAddressChoice_addressesOrRanges &&
Packit c4476c
            !IPAddressOrRanges_canonize(f->ipAddressChoice->
Packit c4476c
                                        u.addressesOrRanges,
Packit c4476c
                                        X509v3_addr_get_afi(f)))
Packit c4476c
            return 0;
Packit c4476c
    }
Packit c4476c
    (void)sk_IPAddressFamily_set_cmp_func(addr, IPAddressFamily_cmp);
Packit c4476c
    sk_IPAddressFamily_sort(addr);
Packit c4476c
    if (!ossl_assert(X509v3_addr_is_canonical(addr)))
Packit c4476c
        return 0;
Packit c4476c
    return 1;
Packit c4476c
}
Packit c4476c
Packit c4476c
/*
Packit c4476c
 * v2i handler for the IPAddrBlocks extension.
Packit c4476c
 */
Packit c4476c
static void *v2i_IPAddrBlocks(const struct v3_ext_method *method,
Packit c4476c
                              struct v3_ext_ctx *ctx,
Packit c4476c
                              STACK_OF(CONF_VALUE) *values)
Packit c4476c
{
Packit c4476c
    static const char v4addr_chars[] = "0123456789.";
Packit c4476c
    static const char v6addr_chars[] = "0123456789.:abcdefABCDEF";
Packit c4476c
    IPAddrBlocks *addr = NULL;
Packit c4476c
    char *s = NULL, *t;
Packit c4476c
    int i;
Packit c4476c
Packit c4476c
    if ((addr = sk_IPAddressFamily_new(IPAddressFamily_cmp)) == NULL) {
Packit c4476c
        X509V3err(X509V3_F_V2I_IPADDRBLOCKS, ERR_R_MALLOC_FAILURE);
Packit c4476c
        return NULL;
Packit c4476c
    }
Packit c4476c
Packit c4476c
    for (i = 0; i < sk_CONF_VALUE_num(values); i++) {
Packit c4476c
        CONF_VALUE *val = sk_CONF_VALUE_value(values, i);
Packit c4476c
        unsigned char min[ADDR_RAW_BUF_LEN], max[ADDR_RAW_BUF_LEN];
Packit c4476c
        unsigned afi, *safi = NULL, safi_;
Packit c4476c
        const char *addr_chars = NULL;
Packit c4476c
        int prefixlen, i1, i2, delim, length;
Packit c4476c
Packit c4476c
        if (!name_cmp(val->name, "IPv4")) {
Packit c4476c
            afi = IANA_AFI_IPV4;
Packit c4476c
        } else if (!name_cmp(val->name, "IPv6")) {
Packit c4476c
            afi = IANA_AFI_IPV6;
Packit c4476c
        } else if (!name_cmp(val->name, "IPv4-SAFI")) {
Packit c4476c
            afi = IANA_AFI_IPV4;
Packit c4476c
            safi = &safi_;
Packit c4476c
        } else if (!name_cmp(val->name, "IPv6-SAFI")) {
Packit c4476c
            afi = IANA_AFI_IPV6;
Packit c4476c
            safi = &safi_;
Packit c4476c
        } else {
Packit c4476c
            X509V3err(X509V3_F_V2I_IPADDRBLOCKS,
Packit c4476c
                      X509V3_R_EXTENSION_NAME_ERROR);
Packit c4476c
            X509V3_conf_err(val);
Packit c4476c
            goto err;
Packit c4476c
        }
Packit c4476c
Packit c4476c
        switch (afi) {
Packit c4476c
        case IANA_AFI_IPV4:
Packit c4476c
            addr_chars = v4addr_chars;
Packit c4476c
            break;
Packit c4476c
        case IANA_AFI_IPV6:
Packit c4476c
            addr_chars = v6addr_chars;
Packit c4476c
            break;
Packit c4476c
        }
Packit c4476c
Packit c4476c
        length = length_from_afi(afi);
Packit c4476c
Packit c4476c
        /*
Packit c4476c
         * Handle SAFI, if any, and OPENSSL_strdup() so we can null-terminate
Packit c4476c
         * the other input values.
Packit c4476c
         */
Packit c4476c
        if (safi != NULL) {
Packit c4476c
            *safi = strtoul(val->value, &t, 0);
Packit c4476c
            t += strspn(t, " \t");
Packit c4476c
            if (*safi > 0xFF || *t++ != ':') {
Packit c4476c
                X509V3err(X509V3_F_V2I_IPADDRBLOCKS, X509V3_R_INVALID_SAFI);
Packit c4476c
                X509V3_conf_err(val);
Packit c4476c
                goto err;
Packit c4476c
            }
Packit c4476c
            t += strspn(t, " \t");
Packit c4476c
            s = OPENSSL_strdup(t);
Packit c4476c
        } else {
Packit c4476c
            s = OPENSSL_strdup(val->value);
Packit c4476c
        }
Packit c4476c
        if (s == NULL) {
Packit c4476c
            X509V3err(X509V3_F_V2I_IPADDRBLOCKS, ERR_R_MALLOC_FAILURE);
Packit c4476c
            goto err;
Packit c4476c
        }
Packit c4476c
Packit c4476c
        /*
Packit c4476c
         * Check for inheritance.  Not worth additional complexity to
Packit c4476c
         * optimize this (seldom-used) case.
Packit c4476c
         */
Packit c4476c
        if (strcmp(s, "inherit") == 0) {
Packit c4476c
            if (!X509v3_addr_add_inherit(addr, afi, safi)) {
Packit c4476c
                X509V3err(X509V3_F_V2I_IPADDRBLOCKS,
Packit c4476c
                          X509V3_R_INVALID_INHERITANCE);
Packit c4476c
                X509V3_conf_err(val);
Packit c4476c
                goto err;
Packit c4476c
            }
Packit c4476c
            OPENSSL_free(s);
Packit c4476c
            s = NULL;
Packit c4476c
            continue;
Packit c4476c
        }
Packit c4476c
Packit c4476c
        i1 = strspn(s, addr_chars);
Packit c4476c
        i2 = i1 + strspn(s + i1, " \t");
Packit c4476c
        delim = s[i2++];
Packit c4476c
        s[i1] = '\0';
Packit c4476c
Packit c4476c
        if (a2i_ipadd(min, s) != length) {
Packit c4476c
            X509V3err(X509V3_F_V2I_IPADDRBLOCKS, X509V3_R_INVALID_IPADDRESS);
Packit c4476c
            X509V3_conf_err(val);
Packit c4476c
            goto err;
Packit c4476c
        }
Packit c4476c
Packit c4476c
        switch (delim) {
Packit c4476c
        case '/':
Packit c4476c
            prefixlen = (int)strtoul(s + i2, &t, 10);
Packit c4476c
            if (t == s + i2 || *t != '\0') {
Packit c4476c
                X509V3err(X509V3_F_V2I_IPADDRBLOCKS,
Packit c4476c
                          X509V3_R_EXTENSION_VALUE_ERROR);
Packit c4476c
                X509V3_conf_err(val);
Packit c4476c
                goto err;
Packit c4476c
            }
Packit c4476c
            if (!X509v3_addr_add_prefix(addr, afi, safi, min, prefixlen)) {
Packit c4476c
                X509V3err(X509V3_F_V2I_IPADDRBLOCKS, ERR_R_MALLOC_FAILURE);
Packit c4476c
                goto err;
Packit c4476c
            }
Packit c4476c
            break;
Packit c4476c
        case '-':
Packit c4476c
            i1 = i2 + strspn(s + i2, " \t");
Packit c4476c
            i2 = i1 + strspn(s + i1, addr_chars);
Packit c4476c
            if (i1 == i2 || s[i2] != '\0') {
Packit c4476c
                X509V3err(X509V3_F_V2I_IPADDRBLOCKS,
Packit c4476c
                          X509V3_R_EXTENSION_VALUE_ERROR);
Packit c4476c
                X509V3_conf_err(val);
Packit c4476c
                goto err;
Packit c4476c
            }
Packit c4476c
            if (a2i_ipadd(max, s + i1) != length) {
Packit c4476c
                X509V3err(X509V3_F_V2I_IPADDRBLOCKS,
Packit c4476c
                          X509V3_R_INVALID_IPADDRESS);
Packit c4476c
                X509V3_conf_err(val);
Packit c4476c
                goto err;
Packit c4476c
            }
Packit c4476c
            if (memcmp(min, max, length_from_afi(afi)) > 0) {
Packit c4476c
                X509V3err(X509V3_F_V2I_IPADDRBLOCKS,
Packit c4476c
                          X509V3_R_EXTENSION_VALUE_ERROR);
Packit c4476c
                X509V3_conf_err(val);
Packit c4476c
                goto err;
Packit c4476c
            }
Packit c4476c
            if (!X509v3_addr_add_range(addr, afi, safi, min, max)) {
Packit c4476c
                X509V3err(X509V3_F_V2I_IPADDRBLOCKS, ERR_R_MALLOC_FAILURE);
Packit c4476c
                goto err;
Packit c4476c
            }
Packit c4476c
            break;
Packit c4476c
        case '\0':
Packit c4476c
            if (!X509v3_addr_add_prefix(addr, afi, safi, min, length * 8)) {
Packit c4476c
                X509V3err(X509V3_F_V2I_IPADDRBLOCKS, ERR_R_MALLOC_FAILURE);
Packit c4476c
                goto err;
Packit c4476c
            }
Packit c4476c
            break;
Packit c4476c
        default:
Packit c4476c
            X509V3err(X509V3_F_V2I_IPADDRBLOCKS,
Packit c4476c
                      X509V3_R_EXTENSION_VALUE_ERROR);
Packit c4476c
            X509V3_conf_err(val);
Packit c4476c
            goto err;
Packit c4476c
        }
Packit c4476c
Packit c4476c
        OPENSSL_free(s);
Packit c4476c
        s = NULL;
Packit c4476c
    }
Packit c4476c
Packit c4476c
    /*
Packit c4476c
     * Canonize the result, then we're done.
Packit c4476c
     */
Packit c4476c
    if (!X509v3_addr_canonize(addr))
Packit c4476c
        goto err;
Packit c4476c
    return addr;
Packit c4476c
Packit c4476c
 err:
Packit c4476c
    OPENSSL_free(s);
Packit c4476c
    sk_IPAddressFamily_pop_free(addr, IPAddressFamily_free);
Packit c4476c
    return NULL;
Packit c4476c
}
Packit c4476c
Packit c4476c
/*
Packit c4476c
 * OpenSSL dispatch
Packit c4476c
 */
Packit c4476c
const X509V3_EXT_METHOD v3_addr = {
Packit c4476c
    NID_sbgp_ipAddrBlock,       /* nid */
Packit c4476c
    0,                          /* flags */
Packit c4476c
    ASN1_ITEM_ref(IPAddrBlocks), /* template */
Packit c4476c
    0, 0, 0, 0,                 /* old functions, ignored */
Packit c4476c
    0,                          /* i2s */
Packit c4476c
    0,                          /* s2i */
Packit c4476c
    0,                          /* i2v */
Packit c4476c
    v2i_IPAddrBlocks,           /* v2i */
Packit c4476c
    i2r_IPAddrBlocks,           /* i2r */
Packit c4476c
    0,                          /* r2i */
Packit c4476c
    NULL                        /* extension-specific data */
Packit c4476c
};
Packit c4476c
Packit c4476c
/*
Packit c4476c
 * Figure out whether extension sues inheritance.
Packit c4476c
 */
Packit c4476c
int X509v3_addr_inherits(IPAddrBlocks *addr)
Packit c4476c
{
Packit c4476c
    int i;
Packit c4476c
    if (addr == NULL)
Packit c4476c
        return 0;
Packit c4476c
    for (i = 0; i < sk_IPAddressFamily_num(addr); i++) {
Packit c4476c
        IPAddressFamily *f = sk_IPAddressFamily_value(addr, i);
Packit c4476c
        if (f->ipAddressChoice->type == IPAddressChoice_inherit)
Packit c4476c
            return 1;
Packit c4476c
    }
Packit c4476c
    return 0;
Packit c4476c
}
Packit c4476c
Packit c4476c
/*
Packit c4476c
 * Figure out whether parent contains child.
Packit c4476c
 */
Packit c4476c
static int addr_contains(IPAddressOrRanges *parent,
Packit c4476c
                         IPAddressOrRanges *child, int length)
Packit c4476c
{
Packit c4476c
    unsigned char p_min[ADDR_RAW_BUF_LEN], p_max[ADDR_RAW_BUF_LEN];
Packit c4476c
    unsigned char c_min[ADDR_RAW_BUF_LEN], c_max[ADDR_RAW_BUF_LEN];
Packit c4476c
    int p, c;
Packit c4476c
Packit c4476c
    if (child == NULL || parent == child)
Packit c4476c
        return 1;
Packit c4476c
    if (parent == NULL)
Packit c4476c
        return 0;
Packit c4476c
Packit c4476c
    p = 0;
Packit c4476c
    for (c = 0; c < sk_IPAddressOrRange_num(child); c++) {
Packit c4476c
        if (!extract_min_max(sk_IPAddressOrRange_value(child, c),
Packit c4476c
                             c_min, c_max, length))
Packit c4476c
            return -1;
Packit c4476c
        for (;; p++) {
Packit c4476c
            if (p >= sk_IPAddressOrRange_num(parent))
Packit c4476c
                return 0;
Packit c4476c
            if (!extract_min_max(sk_IPAddressOrRange_value(parent, p),
Packit c4476c
                                 p_min, p_max, length))
Packit c4476c
                return 0;
Packit c4476c
            if (memcmp(p_max, c_max, length) < 0)
Packit c4476c
                continue;
Packit c4476c
            if (memcmp(p_min, c_min, length) > 0)
Packit c4476c
                return 0;
Packit c4476c
            break;
Packit c4476c
        }
Packit c4476c
    }
Packit c4476c
Packit c4476c
    return 1;
Packit c4476c
}
Packit c4476c
Packit c4476c
/*
Packit c4476c
 * Test whether a is a subset of b.
Packit c4476c
 */
Packit c4476c
int X509v3_addr_subset(IPAddrBlocks *a, IPAddrBlocks *b)
Packit c4476c
{
Packit c4476c
    int i;
Packit c4476c
    if (a == NULL || a == b)
Packit c4476c
        return 1;
Packit c4476c
    if (b == NULL || X509v3_addr_inherits(a) || X509v3_addr_inherits(b))
Packit c4476c
        return 0;
Packit c4476c
    (void)sk_IPAddressFamily_set_cmp_func(b, IPAddressFamily_cmp);
Packit c4476c
    for (i = 0; i < sk_IPAddressFamily_num(a); i++) {
Packit c4476c
        IPAddressFamily *fa = sk_IPAddressFamily_value(a, i);
Packit c4476c
        int j = sk_IPAddressFamily_find(b, fa);
Packit c4476c
        IPAddressFamily *fb;
Packit c4476c
        fb = sk_IPAddressFamily_value(b, j);
Packit c4476c
        if (fb == NULL)
Packit c4476c
            return 0;
Packit c4476c
        if (!addr_contains(fb->ipAddressChoice->u.addressesOrRanges,
Packit c4476c
                           fa->ipAddressChoice->u.addressesOrRanges,
Packit c4476c
                           length_from_afi(X509v3_addr_get_afi(fb))))
Packit c4476c
            return 0;
Packit c4476c
    }
Packit c4476c
    return 1;
Packit c4476c
}
Packit c4476c
Packit c4476c
/*
Packit c4476c
 * Validation error handling via callback.
Packit c4476c
 */
Packit c4476c
#define validation_err(_err_)           \
Packit c4476c
  do {                                  \
Packit c4476c
    if (ctx != NULL) {                  \
Packit c4476c
      ctx->error = _err_;               \
Packit c4476c
      ctx->error_depth = i;             \
Packit c4476c
      ctx->current_cert = x;            \
Packit c4476c
      ret = ctx->verify_cb(0, ctx);     \
Packit c4476c
    } else {                            \
Packit c4476c
      ret = 0;                          \
Packit c4476c
    }                                   \
Packit c4476c
    if (!ret)                           \
Packit c4476c
      goto done;                        \
Packit c4476c
  } while (0)
Packit c4476c
Packit c4476c
/*
Packit c4476c
 * Core code for RFC 3779 2.3 path validation.
Packit c4476c
 *
Packit c4476c
 * Returns 1 for success, 0 on error.
Packit c4476c
 *
Packit c4476c
 * When returning 0, ctx->error MUST be set to an appropriate value other than
Packit c4476c
 * X509_V_OK.
Packit c4476c
 */
Packit c4476c
static int addr_validate_path_internal(X509_STORE_CTX *ctx,
Packit c4476c
                                       STACK_OF(X509) *chain,
Packit c4476c
                                       IPAddrBlocks *ext)
Packit c4476c
{
Packit c4476c
    IPAddrBlocks *child = NULL;
Packit c4476c
    int i, j, ret = 1;
Packit c4476c
    X509 *x;
Packit c4476c
Packit c4476c
    if (!ossl_assert(chain != NULL && sk_X509_num(chain) > 0)
Packit c4476c
            || !ossl_assert(ctx != NULL || ext != NULL)
Packit c4476c
            || !ossl_assert(ctx == NULL || ctx->verify_cb != NULL)) {
Packit c4476c
        if (ctx != NULL)
Packit c4476c
            ctx->error = X509_V_ERR_UNSPECIFIED;
Packit c4476c
        return 0;
Packit c4476c
    }
Packit c4476c
Packit c4476c
    /*
Packit c4476c
     * Figure out where to start.  If we don't have an extension to
Packit c4476c
     * check, we're done.  Otherwise, check canonical form and
Packit c4476c
     * set up for walking up the chain.
Packit c4476c
     */
Packit c4476c
    if (ext != NULL) {
Packit c4476c
        i = -1;
Packit c4476c
        x = NULL;
Packit c4476c
    } else {
Packit c4476c
        i = 0;
Packit c4476c
        x = sk_X509_value(chain, i);
Packit c4476c
        if ((ext = x->rfc3779_addr) == NULL)
Packit c4476c
            goto done;
Packit c4476c
    }
Packit c4476c
    if (!X509v3_addr_is_canonical(ext))
Packit c4476c
        validation_err(X509_V_ERR_INVALID_EXTENSION);
Packit c4476c
    (void)sk_IPAddressFamily_set_cmp_func(ext, IPAddressFamily_cmp);
Packit c4476c
    if ((child = sk_IPAddressFamily_dup(ext)) == NULL) {
Packit c4476c
        X509V3err(X509V3_F_ADDR_VALIDATE_PATH_INTERNAL,
Packit c4476c
                  ERR_R_MALLOC_FAILURE);
Packit c4476c
        if (ctx != NULL)
Packit c4476c
            ctx->error = X509_V_ERR_OUT_OF_MEM;
Packit c4476c
        ret = 0;
Packit c4476c
        goto done;
Packit c4476c
    }
Packit c4476c
Packit c4476c
    /*
Packit c4476c
     * Now walk up the chain.  No cert may list resources that its
Packit c4476c
     * parent doesn't list.
Packit c4476c
     */
Packit c4476c
    for (i++; i < sk_X509_num(chain); i++) {
Packit c4476c
        x = sk_X509_value(chain, i);
Packit c4476c
        if (!X509v3_addr_is_canonical(x->rfc3779_addr))
Packit c4476c
            validation_err(X509_V_ERR_INVALID_EXTENSION);
Packit c4476c
        if (x->rfc3779_addr == NULL) {
Packit c4476c
            for (j = 0; j < sk_IPAddressFamily_num(child); j++) {
Packit c4476c
                IPAddressFamily *fc = sk_IPAddressFamily_value(child, j);
Packit c4476c
                if (fc->ipAddressChoice->type != IPAddressChoice_inherit) {
Packit c4476c
                    validation_err(X509_V_ERR_UNNESTED_RESOURCE);
Packit c4476c
                    break;
Packit c4476c
                }
Packit c4476c
            }
Packit c4476c
            continue;
Packit c4476c
        }
Packit c4476c
        (void)sk_IPAddressFamily_set_cmp_func(x->rfc3779_addr,
Packit c4476c
                                              IPAddressFamily_cmp);
Packit c4476c
        for (j = 0; j < sk_IPAddressFamily_num(child); j++) {
Packit c4476c
            IPAddressFamily *fc = sk_IPAddressFamily_value(child, j);
Packit c4476c
            int k = sk_IPAddressFamily_find(x->rfc3779_addr, fc);
Packit c4476c
            IPAddressFamily *fp =
Packit c4476c
                sk_IPAddressFamily_value(x->rfc3779_addr, k);
Packit c4476c
            if (fp == NULL) {
Packit c4476c
                if (fc->ipAddressChoice->type ==
Packit c4476c
                    IPAddressChoice_addressesOrRanges) {
Packit c4476c
                    validation_err(X509_V_ERR_UNNESTED_RESOURCE);
Packit c4476c
                    break;
Packit c4476c
                }
Packit c4476c
                continue;
Packit c4476c
            }
Packit c4476c
            if (fp->ipAddressChoice->type ==
Packit c4476c
                IPAddressChoice_addressesOrRanges) {
Packit c4476c
                if (fc->ipAddressChoice->type == IPAddressChoice_inherit
Packit c4476c
                    || addr_contains(fp->ipAddressChoice->u.addressesOrRanges,
Packit c4476c
                                     fc->ipAddressChoice->u.addressesOrRanges,
Packit c4476c
                                     length_from_afi(X509v3_addr_get_afi(fc))))
Packit c4476c
                    sk_IPAddressFamily_set(child, j, fp);
Packit c4476c
                else
Packit c4476c
                    validation_err(X509_V_ERR_UNNESTED_RESOURCE);
Packit c4476c
            }
Packit c4476c
        }
Packit c4476c
    }
Packit c4476c
Packit c4476c
    /*
Packit c4476c
     * Trust anchor can't inherit.
Packit c4476c
     */
Packit c4476c
    if (x->rfc3779_addr != NULL) {
Packit c4476c
        for (j = 0; j < sk_IPAddressFamily_num(x->rfc3779_addr); j++) {
Packit c4476c
            IPAddressFamily *fp =
Packit c4476c
                sk_IPAddressFamily_value(x->rfc3779_addr, j);
Packit c4476c
            if (fp->ipAddressChoice->type == IPAddressChoice_inherit
Packit c4476c
                && sk_IPAddressFamily_find(child, fp) >= 0)
Packit c4476c
                validation_err(X509_V_ERR_UNNESTED_RESOURCE);
Packit c4476c
        }
Packit c4476c
    }
Packit c4476c
Packit c4476c
 done:
Packit c4476c
    sk_IPAddressFamily_free(child);
Packit c4476c
    return ret;
Packit c4476c
}
Packit c4476c
Packit c4476c
#undef validation_err
Packit c4476c
Packit c4476c
/*
Packit c4476c
 * RFC 3779 2.3 path validation -- called from X509_verify_cert().
Packit c4476c
 */
Packit c4476c
int X509v3_addr_validate_path(X509_STORE_CTX *ctx)
Packit c4476c
{
Packit c4476c
    if (ctx->chain == NULL
Packit c4476c
            || sk_X509_num(ctx->chain) == 0
Packit c4476c
            || ctx->verify_cb == NULL) {
Packit c4476c
        ctx->error = X509_V_ERR_UNSPECIFIED;
Packit c4476c
        return 0;
Packit c4476c
    }
Packit c4476c
    return addr_validate_path_internal(ctx, ctx->chain, NULL);
Packit c4476c
}
Packit c4476c
Packit c4476c
/*
Packit c4476c
 * RFC 3779 2.3 path validation of an extension.
Packit c4476c
 * Test whether chain covers extension.
Packit c4476c
 */
Packit c4476c
int X509v3_addr_validate_resource_set(STACK_OF(X509) *chain,
Packit c4476c
                                  IPAddrBlocks *ext, int allow_inheritance)
Packit c4476c
{
Packit c4476c
    if (ext == NULL)
Packit c4476c
        return 1;
Packit c4476c
    if (chain == NULL || sk_X509_num(chain) == 0)
Packit c4476c
        return 0;
Packit c4476c
    if (!allow_inheritance && X509v3_addr_inherits(ext))
Packit c4476c
        return 0;
Packit c4476c
    return addr_validate_path_internal(NULL, chain, ext);
Packit c4476c
}
Packit c4476c
Packit c4476c
#endif                          /* OPENSSL_NO_RFC3779 */