From: Theo Buehler Subject: rpki-client: parse ASPA eContent using CBS To: tech@openbsd.org Date: Thu, 12 Nov 2026 07:25:09 +0100 ASPAs have a rather simple eContent and the existing parser can pretty much stay as it is. After this change the only remaining OpenSSL type that is directly used in aspa.c is the ASN1_OBJECT for the aspa_oid. The main side effect is that the parser now enforces DER encoding. This adds two helpers to rpki-der.c, one for getting the eContent version which is a CBS-based reimplementation of valid_econtent_version() and one for replacing as_id_parse(). I don't love how one of them warns and the other one is quiet, but I also don't like passing warning fragments into helpers to make warnings useful. We can polish such things later. The rest is pretty straightforward. Since there is no templated ASN.1 macro magic and STACK_OF anymore, we need to count the providers by hand, other than that there is not much to say. One could do the parsing of the providers in a single pass and build the aspa->providers array using a CBB, but I chose to keep things mostly as they are to keep the churn relatively small. This passes the fixture test. There are no ASPAs that aren't strictly DER encoded (there still are a few with version 0). Index: aspa.c =================================================================== RCS file: /cvs/src/usr.sbin/rpki-client/aspa.c,v diff -u -p -r1.50 aspa.c --- aspa.c 11 Nov 2026 20:08:27 -0000 1.50 +++ aspa.c 12 Nov 2026 05:54:06 -0000 @@ -17,91 +17,75 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ +#include + #include #include #include #include #include -#include - -#include -#include -#include -#include -#include +#include +#include "bytestring.h" #include "extern.h" -#include "rpki-asn1.h" +#include "rpki-der.h" /* - * ASPA eContent definition in draft-ietf-sidrops-aspa-profile-20, section 3. + * ASPA eContent definition in draft-ietf-sidrops-aspa-profile-30, section 3. */ -ASN1_ITEM_EXP ASProviderAttestation_it; - -ASN1_SEQUENCE(ASProviderAttestation) = { - ASN1_EXP_OPT(ASProviderAttestation, version, ASN1_INTEGER, 0), - ASN1_SIMPLE(ASProviderAttestation, customerASID, ASN1_INTEGER), - ASN1_SEQUENCE_OF(ASProviderAttestation, providers, ASN1_INTEGER), -} ASN1_SEQUENCE_END(ASProviderAttestation); - -IMPLEMENT_ASN1_FUNCTIONS(ASProviderAttestation); - -/* - * Parse the ProviderASSet sequence. - * Return zero on failure, non-zero on success. - */ static int -aspa_parse_providers(const char *fn, struct aspa *aspa, - const STACK_OF(ASN1_INTEGER) *providers) +aspa_parse_providers(const char *fn, CBS *cbs, struct aspa *aspa) { - const ASN1_INTEGER *pa; - size_t providersz, i; + CBS providers; + size_t i, num = 0; - if ((providersz = sk_ASN1_INTEGER_num(providers)) == 0) { - warnx("%s: ASPA: ProviderASSet needs at least one entry", fn); - return 0; - } + assert(aspa->providers == NULL); - if (providersz >= MAX_ASPA_PROVIDERS) { - warnx("%s: ASPA: too many providers (more than %d)", fn, - MAX_ASPA_PROVIDERS); + CBS_dup(cbs, &providers); + while (CBS_len(&providers) > 0) { + if (!CBS_get_asn1(&providers, NULL, CBS_ASN1_INTEGER)) { + warnx("%s: ASPA: malformed providerAS", fn); + return 0; + } + if (++num >= MAX_ASPA_PROVIDERS) { + warnx("%s: ASPA: too many providers (more than %d)", + fn, MAX_ASPA_PROVIDERS); + return 0; + } + } + if (num == 0) { + warnx("%s: ASPA: ProviderASSet needs at least one entry", fn); return 0; } - aspa->providers = calloc(providersz, sizeof(aspa->providers[0])); - if (aspa->providers == NULL) + if ((aspa->providers = calloc(num, sizeof(aspa->providers[0]))) == NULL) err(1, NULL); + aspa->num_providers = num; - for (i = 0; i < providersz; i++) { - uint32_t provider = 0; - - pa = sk_ASN1_INTEGER_value(providers, i); - - if (!as_id_parse(pa, &provider)) { + for (i = 0; i < num; i++) { + if (!cbs_get_asid(cbs, &aspa->providers[i])) { warnx("%s: ASPA: malformed ProviderAS", fn); return 0; } - if (aspa->custasid == provider) { + if (aspa->custasid == aspa->providers[i]) { warnx("%s: ASPA: CustomerASID can't also be Provider", fn); return 0; } if (i > 0) { - if (aspa->providers[i - 1] > provider) { + if (aspa->providers[i - 1] > aspa->providers[i]) { warnx("%s: ASPA: invalid ProviderASSet order", fn); return 0; } - if (aspa->providers[i - 1] == provider) { + if (aspa->providers[i - 1] == aspa->providers[i]) { warnx("%s: ASPA: duplicate ProviderAS", fn); return 0; } } - - aspa->providers[aspa->num_providers++] = provider; } if (aspa->num_providers > 1 && aspa->providers[0] == 0) { @@ -109,6 +93,11 @@ aspa_parse_providers(const char *fn, str return 0; } + if (CBS_len(cbs) > 0) { + warnx("%s: ASPA: trailing garbage in ProviderASSet", fn); + return 0; + } + return 1; } @@ -117,44 +106,57 @@ aspa_parse_providers(const char *fn, str * Returns zero on failure, non-zero on success. */ static int -aspa_parse_econtent(const char *fn, void *obj, const unsigned char *d, - size_t dsz) +aspa_parse_econtent(const char *fn, void *obj, const uint8_t *d, size_t dsz) { - struct aspa *aspa = obj; - const unsigned char *oder; - ASProviderAttestation *aspa_asn1; - int rc = 0; + CBS cbs, econtent, providers; + struct aspa *aspa = obj; + + CBS_init(&econtent, d, dsz); - oder = d; - if ((aspa_asn1 = d2i_ASProviderAttestation(NULL, &d, dsz)) == NULL) { + /* + * ASProviderAttestation ::= SEQUENCE { + * version [0] INTEGER DEFAULT 0, + * customerASID CAS, + * providers ProviderASSet } + */ + if (!CBS_get_asn1(&econtent, &cbs, CBS_ASN1_SEQUENCE)) { warnx("%s: ASPA: failed to parse ASProviderAttestation", fn); - goto out; + return 0; } - if (d != oder + dsz) { - warnx("%s: %td bytes trailing garbage in eContent", fn, - oder + dsz - d); - goto out; + if (CBS_len(&econtent) > 0) { + warnx("%s: %zu bytes trailing garbage in eContent", + fn, CBS_len(&cbs)); + return 0; } - if (!valid_econtent_version(fn, aspa_asn1->version, 1)) - goto out; + /* Section 3.1: version MUST be 1 */ + if (!cbs_get_econtent_version(fn, &cbs, 1)) + return 0; - if (!as_id_parse(aspa_asn1->customerASID, &aspa->custasid)) { + /* Section 3.2: customerASID contains a positive integer */ + if (!cbs_get_asid(&cbs, &aspa->custasid)) { warnx("%s: malformed CustomerASID", fn); - goto out; + return 0; } if (aspa->custasid == 0) { warnx("%s: CustomerASID 0 not allowed", fn); - goto out; + return 0; + } + + /* ProviderASSet ::= SEQUENCE (SIZE(1..MAX)) OF PAS */ + if (!CBS_get_asn1(&cbs, &providers, CBS_ASN1_SEQUENCE)) { + warnx("%s: failed to parse ProviderASSet", fn); + return 0; } + if (!aspa_parse_providers(fn, &providers, aspa)) + return 0; - if (!aspa_parse_providers(fn, aspa, aspa_asn1->providers)) - goto out; + if (CBS_len(&cbs) > 0) { + warnx("%s: trailing garbage in ASProviderAttestation", fn); + return 0; + } - rc = 1; - out: - ASProviderAttestation_free(aspa_asn1); - return rc; + return 1; } static int Index: rpki-asn1.h =================================================================== RCS file: /cvs/src/usr.sbin/rpki-client/rpki-asn1.h,v diff -u -p -r1.14 rpki-asn1.h --- rpki-asn1.h 7 Apr 2026 11:52:21 -0000 1.14 +++ rpki-asn1.h 12 Nov 2026 04:52:32 -0000 @@ -26,22 +26,6 @@ #include /* - * Autonomous System Provider Authorization (ASPA) - * reference: draft-ietf-sidrops-aspa-profile - */ - -extern ASN1_ITEM_EXP ASProviderAttestation_it; - -typedef struct { - ASN1_INTEGER *version; - ASN1_INTEGER *customerASID; - STACK_OF(ASN1_INTEGER) *providers; -} ASProviderAttestation; - -DECLARE_ASN1_FUNCTIONS(ASProviderAttestation); - - -/* * Canonical Cache Representation (CCR) * reference: draft-ietf-sidrops-rpki-ccr */ Index: rpki-der.c =================================================================== RCS file: /cvs/src/usr.sbin/rpki-client/rpki-der.c,v diff -u -p -r1.2 rpki-der.c --- rpki-der.c 3 Nov 2026 15:04:31 -0000 1.2 +++ rpki-der.c 12 Nov 2026 04:52:32 -0000 @@ -111,3 +111,60 @@ cbs_is_rfc7935_signatureAlgorithm_value( return 0; } + +/* + * Assuming the version is explicitly tagged with default 0, as in + * version [0] INTEGER DEFAULT 0, + * check whether the version is correctly encoded and matches expected. + */ +int +cbs_get_econtent_version(const char *fn, CBS *cbs, uint64_t expected) +{ + CBS version; + uint64_t u64; + int present; + + if (!CBS_get_optional_asn1(cbs, &version, &present, + CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 0x00)) { + warnx("%s: failed to get optional version", fn); + return 0; + } + if (!present) { + if (expected == 0) + return 1; + warnx("%s: unexpected version 0", fn); + return 0; + } + + if (!CBS_get_asn1_uint64(&version, &u64)) { + warnx("%s: CBS_get_asn1_uint64 failed", fn); + return 0; + } + + if (u64 == 0) { + warnx("%s: incorrect encoding for version 0", fn); + return 0; + } + + if (u64 != expected) { + warnx("%s: unexpected version (expected %llu, got %llu)", fn, + (unsigned long long)expected, (unsigned long long)u64); + return 0; + } + + return 1; +} + +int +cbs_get_asid(CBS *cbs, uint32_t *out) +{ + uint64_t asid; + + if (!CBS_get_asn1_uint64(cbs, &asid)) + return 0; + if (asid > UINT32_MAX) + return 0; + + *out = asid; + return 1; +} Index: rpki-der.h =================================================================== RCS file: /cvs/src/usr.sbin/rpki-client/rpki-der.h,v diff -u -p -r1.1 rpki-der.h --- rpki-der.h 3 Nov 2026 14:33:14 -0000 1.1 +++ rpki-der.h 12 Nov 2026 04:52:32 -0000 @@ -32,4 +32,7 @@ int cbs_oid_value_matches_nid(CBS *, int int cbs_is_sha256_algorithmIdentifier_value(CBS *); int cbs_is_rfc7935_signatureAlgorithm_value(CBS *); +int cbs_get_econtent_version(const char *, CBS *, uint64_t); +int cbs_get_asid(CBS *, uint32_t *); + #endif /* RPKI_DER_H */