X509V3_ADDR_ADD_INHERIT(3)

πŸ“¦
version 4.4.0 Β· 2 versions
, in arch rolling

okay so bestie, welcome to X509V3_ADDR_ADD_INHERIT(3) from the Library Functions Manual β€” the whole RFC 3779 IP address delegation extensions situation. πŸ’–

✨ NAME ✨

The squad is literally:

  • X509v3_addr_add_inherit
  • X509v3_addr_add_prefix
  • X509v3_addr_add_range
  • X509v3_addr_canonize
  • X509v3_addr_is_canonical

…and they’re all here for RFC 3779 IP address delegation extensions. πŸŽ€

πŸ¦‹ SYNOPSIS πŸ¦‹

library β€œlibcrypto” #include <openssl/x509v3.h>

int
X509v3_addr_add_inherit(IPAddrBlocks *addrblocks, const unsigned afi,
    const unsigned *safi);

int
X509v3_addr_add_prefix(IPAddrBlocks *addrblocks, const unsigned afi,
    const unsigned *safi, unsigned char *prefix, const int prefixlen);

int
X509v3_addr_add_range(IPAddrBlocks *addrblocks, const unsigned afi,
    const unsigned *safi, unsigned char *min, unsigned char *max);

int
X509v3_addr_canonize(IPAddrBlocks *addrblocks);

int
X509v3_addr_is_canonical(IPAddrBlocks *addrblocks);

πŸ’… DESCRIPTION πŸ’…

Okay so an IPAddrBlocks object is the whole vibe of an IP address delegation extension, as defined in RFC 3779, section 2.2.3.1. It holds lists of IP address prefixes and IP address ranges delegated from the issuer to the subject of the certificate. You can instantiate it like the EXAMPLES section says, and its internals are documented over in IPAddressRange_new(3). 🫢

Each list in a well-formed IPAddrBlocks object is uniquely identified by an address family identifier (AFI) and an optional subsequent address family identifier (SAFI). Lists can be absent, or they can contain an β€œinherit” marker to indicate that the resources are to be inherited from the corresponding list of the issuer certificate.

Per spec, an AFI is an unsigned 16-bit integer and a SAFI is an unsigned 8-bit integer. For IPv4 and IPv6 there are the predefined constants IANA_AFI_IPV4 and IANA_AFI_IPV6, and those should be the only values used for afi in this API. In practice, safi is always NULL. afi is generally silently truncated to its lowest 16 bits and, if safi is non-NULL, only the lowest 8 bits of the value pointed at are used. πŸ‘‘

  • X509v3_addr_add_inherit() β€” adds a list with an β€œinherit” marker to addrblocks. If a list corresponding to afi and safi already exists, no action occurs if it is marked β€œinherit”; otherwise the call fails. πŸ’€
  • X509v3_addr_add_prefix() β€” adds a newly allocated internal representation of the prefix of length prefixlen to the list corresponding to afi and the optional safi in addrblocks. If no such list exists, it is created first. If the list exists and is marked β€œinherit”, the call fails. prefix is expected to be a byte array in network byte order. It should point at enough memory to accommodate prefixlen bits, and it is recommended that all the bits not covered by the prefixlen be set to 0. It is the caller’s responsibility to ensure that the prefix has no address in common with any of the prefixes or ranges already in the list. If afi is IANA_AFI_IPV4, prefixlen should be between 0 and 32 (inclusive), and if afi is IANA_AFI_IPV6, prefixlen should be between 0 and 128 (inclusive). 🌸
  • X509v3_addr_add_range() β€” similar to X509v3_addr_add_prefix() for the closed interval of IP addresses between min and max in network presentation. If afi is IANA_AFI_IPV4, min and max should point at 4 bytes of memory, and if afi is IANA_AFI_IPV6, min and max should point at 16 bytes of memory. In case the range of IP addresses between min and max is a prefix, a prefix will be added instead of a range. It is the caller’s responsibility to ensure that min is less than or equal to max and that it does not contain any address already present in the list. Failure to do so will result in a subsequent failure of X509v3_addr_canonize(). 🚨
  • X509v3_addr_canonize() β€” attempts to bring the non-NULL addrblocks into canonical form. An IPAddrBlocks object is said to be in canonical form if it conforms to the ordering specified in RFC 3779: section 2.2.3.3 requires that the list of lists be sorted first by increasing afi and then by increasing safi, where NULL is the minimal SAFI; section 2.2.3.6 requires that each list be in minimal form and sorted. The minimality requirement is that all adjacent prefixes and ranges must be merged into a single range and that each range must be expressed as a prefix, if possible. In particular, any given address can be in at most one list entry. The order is by increasing minimal IP address in network byte order. ✨
  • X509v3_addr_is_canonical() β€” indicates whether addrblocks is in canonical form. πŸ¦‹

πŸ”₯ RETURN VALUES πŸ”₯

All these functions return 1 on success and 0 on failure. Memory allocation failure is one possible reason for all of them. Sometimes an error code can be obtained by ERR_get_error(3). 😭

  • X509v3_addr_add_inherit() fails if the list corresponding to afi and the optional safi already exists and is not marked β€œinherit”.
  • X509v3_addr_add_prefix() and X509v3_addr_add_range() fail if a list corresponding to afi and the optional safi already exists and is marked β€œinherit”, or if prefixlen is outside the interval [0,32] for IPv4 addresses or [0,128] for IPv6 addresses.
  • X509v3_addr_canonize() fails if one of the lists in addrblocks is malformed, in particular if it contains corrupt, overlapping, or duplicate entries. Corruption includes ranges where max is strictly smaller than min. The error conditions are generally indistinguishable. πŸ’€
  • X509v3_addr_is_canonical() returns 1 if addrblocks is in canonical form. A return value of 0 can indicate non-canonical form or a corrupted list.

🌈 EXAMPLES 🌈

Construct the first extension from RFC 3779, Appendix B. πŸŽ€

#include <sys/socket.h>
#include <arpa/inet.h>

#include <err.h>
#include <stdio.h>
#include <unistd.h>

#include <openssl/asn1.h>
#include <openssl/objects.h>
#include <openssl/x509.h>
#include <openssl/x509v3.h>

const char *prefixes[] = {
        "10.0.32/20", "10.0.64/24", "10.1/16",
        "10.2.48/20", "10.2.64/24", "10.3/16",
};
#define N_PREFIXES (sizeof(prefixes) / sizeof(prefixes[0]))

static void
hexdump(const unsigned char *buf, size_t len)
{
        size_t i;

        for (i = 1; i <= len; i++)
                printf(" 0x%02x,%s", buf[i - 1], i % 8 ? "" : "\n");
        if (len % 8)
                printf("\n");
}

int
main(void)
{
        IPAddrBlocks *addrblocks;
        X509_EXTENSION *ext;
        unsigned char *der;
        int der_len;
        size_t i;

        if (pledge("stdio", NULL) == -1)
                err(1, "pledge");

        /*
         * Somebody forgot to implement IPAddrBlocks_new().  IPAddrBlocks
         * is the same as STACK_OF(IPAddressFamily).  As such, it should
         * have IPAddressFamily_cmp() as its comparison function.  It is
         * not possible to call sk_new(3) because IPAddressFamily_cmp()
         * is not part of the public API.  The correct comparison function
         * can be installed as a side-effect of X509v3_addr_canonize(3).
         */
        if ((addrblocks = sk_IPAddressFamily_new_null()) == NULL)
                err(1, "sk_IPAddressFamily_new_null");
        if (!X509v3_addr_canonize(addrblocks))
                errx(1, "X509v3_addr_canonize");

        /* Add the prefixes as IPv4 unicast. */
        for (i = 0; i < N_PREFIXES; i++) {
                unsigned char addr[16] = {0};
                int len;
                int unicast = 1; /* SAFI for unicast forwarding. */

                len = inet_net_pton(AF_INET, prefixes[i], addr,
                    sizeof(addr));
                if (len == -1)
                        errx(1, "inet_net_pton(%s)", prefixes[i]);
                if (!X509v3_addr_add_prefix(addrblocks, IANA_AFI_IPV4,
                    &unicast, addr, len))
                        errx(1, "X509v3_addr_add_prefix(%s)", prefixes[i]);
        }
        if (!X509v3_addr_add_inherit(addrblocks, IANA_AFI_IPV6, NULL))
                errx(1, "X509v3_addr_add_inherit");

        /*
         * Ensure the extension is in canonical form.  Otherwise the two
         * adjacent prefixes 10.2.48/20 and 10.2.64/24 are not merged into
         * the range 10.2.48.0--10.2.64.255.  This results in invalid DER
         * encoding from X509V3_EXT_i2d(3) and i2d_X509_EXTENSION(3).
         */
        if (!X509v3_addr_canonize(addrblocks))
                errx(1, "X509v3_addr_canonize");

        /* Create the extension with the correct OID; mark it critical. */
        ext = X509V3_EXT_i2d(NID_sbgp_ipAddrBlock, 1, addrblocks);
        if (ext == NULL)
                errx(1, "X509V3_EXT_i2d");

        der = NULL;
        if ((der_len = i2d_X509_EXTENSION(ext, &der)) <= 0)
                errx(1, "i2d_X509_EXTENSION");

        hexdump(der, der_len);

        /* One way of implementing IPAddrBlocks_free(). */
        sk_IPAddressFamily_pop_free(addrblocks, IPAddressFamily_free);
        X509_EXTENSION_free(ext);
        free(der);

        return 0;
}

Implement the missing public API d2i_IPAddrBlocks() and i2d_IPAddrBlocks() using ASN1_item_d2i(3): πŸ’…

IPAddrBlocks *
d2i_IPAddrBlocks(IPAddrBlocks **addrblocks, const unsigned char **in,
    long len)
{
        const X509V3_EXT_METHOD *v3_addr;

        if ((v3_addr = X509V3_EXT_get_nid(NID_sbgp_ipAddrBlock)) == NULL)
                return NULL;
        return (IPAddrBlocks *)ASN1_item_d2i((ASN1_VALUE **)addrblocks,
            in, len, ASN1_ITEM_ptr(v3_addr->it));
}

int
i2d_IPAddrBlocks(IPAddrBlocks *addrblocks, unsigned char **out)
{
        const X509V3_EXT_METHOD *v3_addr;

        if ((v3_addr = X509V3_EXT_get_nid(NID_sbgp_ipAddrBlock)) == NULL)
                return -1;
        return ASN1_item_i2d((ASN1_VALUE *)addrblocks, out,
            ASN1_ITEM_ptr(v3_addr->it));
}

The use of the undocumented macro ASN1_ITEM_ptr() is necessary if compatibility with modern versions of other implementations is desired. 🧚

πŸŽ€ SEE ALSO πŸŽ€

⭐ STANDARDS ⭐

πŸ•°οΈ HISTORY πŸ•°οΈ

These functions first appeared in OpenSSL 0.9.8e and have been available since OpenBSD 7.1. periodt. πŸ’…

πŸ’€ BUGS πŸ’€

okay bestie, buckle up, because this is the part where we air out the filth 🫢 none of this is cute, but you need to know it, iykyk.

  • the missing function family is real and it's a problem β€” IPAddrBlocks_new(), IPAddrBlocks_free(), d2i_IPAddrBlocks(), and i2d_IPAddrBlocks() literally do not exist 😭 and IPAddrBlocks_it is not public either. like, not exported, not yours to touch. the examples up above show you how to implement all four of the missing functions yourself using only the public API. yes, manually. yes, it's giving DIY era. πŸ’…
  • X509v3_addr_add_range() is too trusting β€” it should check for inverted range bounds and overlaps on insertion, and it should fail loudly. instead it happily creates a nonsensical addrblocks that then fails to be canonized by X509v3_addr_canonize(). so you get garbage in, garbage that never canonicalizes out πŸ”₯ not the slay we wanted.
  • the NULL situation is a whole mess, no cap β€” if you pass NULL to X509v3_asid_canonize(3), it succeeds. meanwhile X509v3_addr_is_canonical() considers NULL to be a canonical IPAddrBlocks. cute, right? πŸ™ˆ but X509v3_addr_canonize() β€” the other one β€” crashes with a NULL dereference πŸ’€ so like, the same family of functions cannot agree on what NULL even means. iconic in the worst way possible.
  • AFI support is IPv4 and IPv6 only β€” the code only supports the IPv4 and IPv6 AFIs. and this restriction is not consistently enforced across implementations, so different stacks will vibe-check you differently. ⭐
  • the stray-bits bug (derogatory) β€” in some implementations, X509v3_addr_add_range() fails to clear the unused bits set to 1 in the last octet of the ASN1_BIT_STRING representation of max. and this confuses some software, which, honestly, fair β€” nobody likes a leftover 1 just chilling in there 😭
fr fr: if you're touching ranges or NULL values here, test against more than one implementation. the vibes are not consistent and the standard is not holding your hand, bestie.

and that's the whole page, gorgeous πŸŽ€ you made it to the end of the X509V3_ADDR_ADD_INHERIT(3) lore. this one came from Debian, dated May 10, 2026, and it's filed under X509V3_ADDR_ADD_INHERIT(3) β€” go forth and canonize responsibly πŸŒˆπŸ’œ

πŸ“¦ where this page comes from
  • arch rolling: libressl 4.4.0-1 Β· ISC AND custom:OpenSSL
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms