Skip to content

Commit

Permalink
Make sure d2i_X509() consumed all data
Browse files Browse the repository at this point in the history
An artefact of d2i_*() functions is that once they're satisfied,
there still might be trailing garbage in the field that's being
decoded.

Callers of d2i_*() functions generally should conform that all
data has been consumed.
  • Loading branch information
job committed Feb 5, 2024
1 parent 2278558 commit 1165270
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/asn1/signed_data.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ static int
handle_sdata_certificate(ANY_t *cert_encoded, struct signed_object_args *args,
OCTET_STRING_t *sid, ANY_t *signedData, SignatureValue_t *signature)
{
const unsigned char *tmp;
const unsigned char *otmp, *tmp;
X509 *cert;
enum rpki_policy policy;
int error;
Expand All @@ -78,12 +78,16 @@ handle_sdata_certificate(ANY_t *cert_encoded, struct signed_object_args *args,
* pointer.
*/
tmp = (const unsigned char *) cert_encoded->buf;

otmp = tmp;
cert = d2i_X509(NULL, &tmp, cert_encoded->size);
if (cert == NULL) {
error = val_crypto_err("Signed object's 'certificate' element does not decode into a Certificate");
goto end1;
}
if (tmp != otmp + cert_encoded->size) {
error = val_crypto_err("Signed object's 'certificate' element contains trailing garbage");
goto end1;
}

x509_name_pr_debug("Issuer", X509_get_issuer_name(cert));

Expand Down

0 comments on commit 1165270

Please sign in to comment.