Index | Thread | Search

From:
Claudio Jeker <cjeker@diehard.n-r-g.com>
Subject:
Re: rpki-client: parse ASPA eContent using CBS
To:
Theo Buehler <tb@theobuehler.org>
Cc:
tech@openbsd.org
Date:
Thu, 12 Nov 2026 13:51:50 +0100

Download raw body.

Thread
On Thu, Nov 12, 2026 at 01:11:07PM +0100, Theo Buehler wrote:
> On Thu, Nov 12, 2026 at 09:40:01AM +0100, Claudio Jeker wrote:
> > On Thu, Nov 12, 2026 at 07:25:09AM +0100, Theo Buehler wrote:
> > > 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).
> > 
> > This looks very nice. A few comments below.
> 
> Thanks. I like the diff at the end much better now.
>  
> > > +	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;
> > > +		}
> > 
> > Would it make more sense to use cbs_get_asid() here and then also do the
> > other checks done in the second loop here first?
> 
> Not sure it makes more sense, but it definitely makes it easier to follow
> if both loops are using the same pattern. I kind of wanted to avoid parsing
> the integers twice, but since that's cheap, I switched to cbs_get_asid()
> and reworked the second loop to match.
> 
> > Some of the checks are trivial. The one using the prev element to ensure
> > the array is a bit trickier so I'm not too fuzzed if that one remains in
> > the 2nd loop for now.
> 
> It felt kind of weird to move some of the checks into the first loop,
> so I did not do that. It also seems easier to check against the draft's
> section 3.3 if the checks are close together.
> 
> > > @@ -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;
> > > +	}
> > > +
> > 
> > This should not be possible, right? Since the first loop can only succeed
> > if there is no trailing garbage. If this happens we have some serious CBS
> > bug.
> 
> Right. I added this as a sanity check to ensure the two loops did the same
> (expected) thing. Now that both loops just pull everything off copies of
> the same CBS, this is no longer needed.
> 
> > > +	/*
> > > +	 * 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;
> > >  	}
> 
> Since you asked further down: this check here ensures nothing comes *after*
> the ASProviderAttestation sequence in the econtent. Since the econtent is a
> single sequence, I check that it is fully consumed right here.
> 
> > In most places you did this check right after
> > 	CBS_get_asn1(..., CBS_ASN1_SEQUENCE)
> > 
> > e.g.
> > 	if (!CBS_get_asn1(&econtent, &cbs, CBS_ASN1_SEQUENCE)) {
> > 	}
> > 	if (CBS_len(&econtent) > 0) {
> > 
> > So I would do the same here and move the check up before the
> > aspa_parse_providers call.
> 
> I think I've been pretty consistent about ensuring the CBS is fully
> consumed at the end whenever possible.
> 
> The situation here is a bit different than the check after the econtent.
> We're now consuming the individual sequence elements and parse them right
> away (first the version then the customerASID and then the providers).
> Once we parsed the last member we ensure nothing further is there:
> 
>   ASProviderAttestation ::= SEQUENCE {
>     version       [0] INTEGER DEFAULT 0,
>     customerASID      CAS,
>     providers         ProviderASSet
> ----> we're here now <---
>   }
>   
> I left that check at the end of the function, I hope the further
> refactoring makes it clearer why I did it this way.

I think I confused myself with checking garbage at the end of the sequence
and after the sequence. In this case the providers as a unbound set eat
all the the data and so the check inside the the sequence is not really
required. aspa_parse_providers() ensures that there is no trailing
garbage but doing the explicit check is better.
 
> > > +	if (!present) {
> > > +		if (expected == 0)
> > > +			return 1;
> > > +		warnx("%s: unexpected version 0", fn);
> > 
> > I would use the same error message from below here:
> > 
> > 		warnx("%s: unexpected version (expected %llu, got 0)", fn,
> > 		    (unsigned long long)expected);
> 
> I was trying too hard to match the existing error messages. This funcion
> was a straight CBS-based reimplementation of the already existing
> valid_econtent_version(). So the diff below adjusts the error there
> since your complaint really applies to that function.
> 
> > So cbs_get_econtent_version() is the first thing that does this fine
> > grained version checking here. All other helpers manage don't do that.
> 
> Yeah, I agree. I wasn't super happy with it either.
> 
> > This function does two things in one go and I think this is why it is
> > problematic. I would expect cbs_get_econtent_version to just return the
> > encoded version and then have the callee do the comparison against the
> > expected version (which could e.g. be valid_econtent_version(fn, x, y)).
> > Then I think then cbs_get_econtent_version() would not need the extra fn
> > argument anymore. Also the naming makes more sense then what it is now.
> 
> I originally had something like that. I factored it a bit differently
> than that, though.
> 
> I don't feel strongly about passing in an aspa to aspa_parse_version(),
> but it felt more consistent.
> 
> cvs diff makes a bit of a mess, so a git diff it is.
> 
> diff --git a/usr.sbin/rpki-client/aspa.c b/usr.sbin/rpki-client/aspa.c
> index 444c0f9d99..3d58f1f928 100644
> --- a/usr.sbin/rpki-client/aspa.c
> +++ b/usr.sbin/rpki-client/aspa.c
> @@ -17,68 +17,100 @@ aspa_parse_version(const char *fn, CBS *cbs, struct aspa *aspa)
>   * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
>   */
>  
> +#include <sys/tree.h>
> +
>  #include <assert.h>
>  #include <err.h>
>  #include <stdint.h>
>  #include <stdlib.h>
>  #include <string.h>
> -#include <unistd.h>
> -
> -#include <openssl/asn1.h>
> -#include <openssl/asn1t.h>
> -#include <openssl/stack.h>
> -#include <openssl/safestack.h>
> -#include <openssl/x509.h>
> -
> +#include <time.h>
> +
> +#include "bytestring.h"
>  #include "extern.h"
> -#include "rpki-asn1.h"
> -
> -/*
> - * ASPA eContent definition in draft-ietf-sidrops-aspa-profile-20, 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)
> -{
> -	const ASN1_INTEGER	*pa;
> -	size_t			 providersz, i;
> -
> -	if ((providersz = sk_ASN1_INTEGER_num(providers)) == 0) {
> +#include "rpki-der.h"
> +
> +/*
> + * ASPA eContent definition in draft-ietf-sidrops-aspa-profile-30, section 3.
> + */
> +
> +static int
> +aspa_parse_version(const char *fn, CBS *cbs, struct aspa *aspa)
> +{
> +	uint64_t version;
> +
> +	if (!cbs_get_econtent_version(cbs, &version)) {
> +		warnx("%s: ASPA failed to parse eContent version", fn);
> +		return 0;
> +	}
> +	if (version != 1) {
> +		warnx("%s: unexpected version (expected %d, got %llu)",
> +		    fn, 1, (unsigned long long)version);
> +		return 0;
> +	}
> +	return 1;
> +}
> +
> +static int
> +aspa_parse_customerASID(const char *fn, CBS *cbs, struct aspa *aspa)
> +{
> +	if (!cbs_get_asid(cbs, &aspa->custasid)) {
> +		warnx("%s: malformed CustomerASID", fn);
> +		return 0;
> +	}
> +	if (aspa->custasid == 0) {
> +		warnx("%s: CustomerASID 0 not allowed", fn);
> +		return 0;
> +	}
> +	return 1;
> +}
> +
> +static int
> +aspa_parse_providers(const char *fn, CBS *cbs, struct aspa *aspa)
> +{
> +	CBS child, providers;
> +	size_t num;
> +
> +	assert(aspa->providers == NULL && aspa->num_providers == 0);
> +
> +	/* ProviderASSet ::= SEQUENCE (SIZE(1..MAX)) OF PAS */
> +	if (!CBS_get_asn1(cbs, &child, CBS_ASN1_SEQUENCE)) {
> +		warnx("%s: failed to parse ProviderASSet", fn);
> +		return 0;
> +	}
> +
> +	num = 0;
> +	CBS_dup(&child, &providers);
> +	while (CBS_len(&providers) > 0) {
> +		uint32_t provider;
> +
> +		if (!cbs_get_asid(&providers, &provider)) {
> +			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;
>  	}
>  
> -	if (providersz >= MAX_ASPA_PROVIDERS) {
> -		warnx("%s: ASPA: too many providers (more than %d)", fn,
> -		    MAX_ASPA_PROVIDERS);
> -		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);
> -
> -	for (i = 0; i < providersz; i++) {
> -		uint32_t provider = 0;
> -
> -		pa = sk_ASN1_INTEGER_value(providers, i);
> -
> -		if (!as_id_parse(pa, &provider)) {
> +	aspa->num_providers = num;
> +
> +	num = 0;
> +	CBS_dup(&child, &providers);
> +	while (CBS_len(&providers) > 0) {
> +		uint32_t provider;
> +
> +		if (!cbs_get_asid(&providers, &provider)) {
>  			warnx("%s: ASPA: malformed ProviderAS", fn);
>  			return 0;
>  		}
> @@ -89,20 +121,22 @@ aspa_parse_providers(const char *fn, CBS *cbs, struct aspa *aspa)
>  			return 0;
>  		}
>  
> -		if (i > 0) {
> -			if (aspa->providers[i - 1] > provider) {
> +		assert(num < aspa->num_providers);
> +		if (num > 0) {
> +			if (aspa->providers[num - 1] > provider) {
>  				warnx("%s: ASPA: invalid ProviderASSet order",
>  				    fn);
>  				return 0;
>  			}
> -			if (aspa->providers[i - 1] == provider) {
> +			if (aspa->providers[num - 1] == provider) {
>  				warnx("%s: ASPA: duplicate ProviderAS", fn);
>  				return 0;
>  			}
>  		}
>  
> -		aspa->providers[aspa->num_providers++] = provider;
> +		aspa->providers[num++] = provider;
>  	}
> +	assert(aspa->num_providers == num);
>  
>  	if (aspa->num_providers > 1 && aspa->providers[0] == 0) {
>  		warnx("%s: ASPA: invalid ProviderASSet", fn);
> @@ -117,44 +151,42 @@ aspa_parse_providers(const char *fn, CBS *cbs, struct aspa *aspa)
>   * 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;
> -
> -	oder = d;
> -	if ((aspa_asn1 = d2i_ASProviderAttestation(NULL, &d, dsz)) == NULL) {
> +	CBS cbs, econtent;
> +	struct aspa *aspa = obj;
> +
> +	CBS_init(&econtent, d, dsz);
> +
> +	/*
> +	 * 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;
> -	}
> -	if (d != oder + dsz) {
> -		warnx("%s: %td bytes trailing garbage in eContent", fn,
> -		    oder + dsz - d);
> -		goto out;
> -	}
> -
> -	if (!valid_econtent_version(fn, aspa_asn1->version, 1))
> -		goto out;
> -
> -	if (!as_id_parse(aspa_asn1->customerASID, &aspa->custasid)) {
> -		warnx("%s: malformed CustomerASID", fn);
> -		goto out;
> -	}
> -	if (aspa->custasid == 0) {
> -		warnx("%s: CustomerASID 0 not allowed", fn);
> -		goto out;
> -	}
> -
> -	if (!aspa_parse_providers(fn, aspa, aspa_asn1->providers))
> -		goto out;
> -
> -	rc = 1;
> - out:
> -	ASProviderAttestation_free(aspa_asn1);
> -	return rc;
> +		return 0;
> +	}
> +	if (CBS_len(&econtent) > 0) {
> +		warnx("%s: %zu bytes trailing garbage in eContent",
> +		    fn, CBS_len(&cbs));
> +		return 0;
> +	}
> +
> +	if (!aspa_parse_version(fn, &cbs, aspa))
> +		return 0;
> +	if (!aspa_parse_customerASID(fn, &cbs, aspa))
> +		return 0;
> +	if (!aspa_parse_providers(fn, &cbs, aspa))
> +		return 0;
> +
> +	if (CBS_len(&cbs) > 0) {
> +		warnx("%s: trailing garbage in ASProviderAttestation", fn);
> +		return 0;
> +	}
> +
> +	return 1;
>  }
>  
>  static int
> diff --git a/usr.sbin/rpki-client/rpki-asn1.h b/usr.sbin/rpki-client/rpki-asn1.h
> index c02d038888..23bfae360e 100644
> --- a/usr.sbin/rpki-client/rpki-asn1.h
> +++ b/usr.sbin/rpki-client/rpki-asn1.h
> @@ -26,22 +26,6 @@ DECLARE_ASN1_FUNCTIONS(ASProviderAttestation);
>  #include <openssl/x509.h>
>  
>  /*
> - * 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
>   */
> diff --git a/usr.sbin/rpki-client/rpki-der.c b/usr.sbin/rpki-client/rpki-der.c
> index c2925812cd..616ae991ab 100644
> --- a/usr.sbin/rpki-client/rpki-der.c
> +++ b/usr.sbin/rpki-client/rpki-der.c
> @@ -111,3 +111,47 @@ cbs_is_rfc7935_signatureAlgorithm_value(CBS *cbs)
>  
>  	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 return its value.
> + */
> +int
> +cbs_get_econtent_version(CBS *cbs, uint64_t *out_version)
> +{
> +	CBS version;
> +	uint64_t u64;
> +	int present;
> +
> +	if (!CBS_get_optional_asn1(cbs, &version, &present,
> +	    CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 0x00))
> +		return 0;
> +	if (!present) {
> +		*out_version = 0;
> +		return 1;
> +	}
> +
> +	if (!CBS_get_asn1_uint64(&version, &u64))
> +		return 0;
> +	if (u64 == 0)
> +		return 0;
> +
> +	*out_version = u64;
> +
> +	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;
> +}
> diff --git a/usr.sbin/rpki-client/rpki-der.h b/usr.sbin/rpki-client/rpki-der.h
> index 4f7869df73..6ac26811c8 100644
> --- a/usr.sbin/rpki-client/rpki-der.h
> +++ b/usr.sbin/rpki-client/rpki-der.h
> @@ -32,4 +32,7 @@ int cbs_is_sha256_algorithmIdentifier_value(CBS *);
>  int cbs_is_sha256_algorithmIdentifier_value(CBS *);
>  int cbs_is_rfc7935_signatureAlgorithm_value(CBS *);
>  
> +int cbs_get_econtent_version(CBS *, uint64_t *);
> +int cbs_get_asid(CBS *, uint32_t *);
> +
>  #endif /* RPKI_DER_H */
> diff --git a/usr.sbin/rpki-client/validate.c b/usr.sbin/rpki-client/validate.c
> index 73ce8e0f52..feb42f48ae 100644
> --- a/usr.sbin/rpki-client/validate.c
> +++ b/usr.sbin/rpki-client/validate.c
> @@ -538,7 +538,8 @@ valid_econtent_version(const char *fn, const ASN1_INTEGER *aint,
>  	if (aint == NULL) {
>  		if (expected == 0)
>  			return 1;
> -		warnx("%s: unexpected version 0", fn);
> +		warnx("%s: unexpected version (expected %llu, got 0)",
> +		    fn, (unsigned long long)expected);
>  		return 0;
>  	}
>  
> 

OK claudio@

-- 
:wq Claudio