Skip to content

Commit df8e2ee

Browse files
committed
x509: fix malformed AKID extension from wolfSSL_X509_set_authority_key_id
1 parent 5151a69 commit df8e2ee

2 files changed

Lines changed: 443 additions & 13 deletions

File tree

src/x509.c

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16333,6 +16333,9 @@ int wolfSSL_X509_set_subject_key_id_ex(WOLFSSL_X509* x509)
1633316333
#endif /* !NO_SHA */
1633416334

1633516335
/* Set Authority Key Identifier from raw bytes.
16336+
* The bytes passed in are the keyIdentifier OCTET STRING contents only,
16337+
* they must not be a pre-encoded AuthorityKeyIdentifier SEQUENCE.
16338+
* The cert encoder wraps them in SEQUENCE { [0] keyIdentifier } at sign time.
1633616339
*
1633716340
* x509 - Certificate to modify
1633816341
* akid - Raw AKID bytes
@@ -16343,27 +16346,38 @@ int wolfSSL_X509_set_subject_key_id_ex(WOLFSSL_X509* x509)
1634316346
int wolfSSL_X509_set_authority_key_id(WOLFSSL_X509* x509,
1634416347
const unsigned char* akid, int akidSz)
1634516348
{
16349+
byte* newAkid = NULL;
16350+
1634616351
WOLFSSL_ENTER("wolfSSL_X509_set_authority_key_id");
1634716352

1634816353
if (x509 == NULL || akid == NULL || akidSz <= 0) {
1634916354
return WOLFSSL_FAILURE;
1635016355
}
1635116356

16352-
/* Allocate/reallocate memory for authKeyIdSrc */
16353-
if (x509->authKeyIdSrc == NULL || (int)x509->authKeyIdSrcSz < akidSz) {
16354-
if (x509->authKeyIdSrc != NULL) {
16355-
XFREE(x509->authKeyIdSrc, x509->heap, DYNAMIC_TYPE_X509_EXT);
16356-
}
16357-
x509->authKeyIdSrc = (byte*)XMALLOC((word32)akidSz, x509->heap,
16358-
DYNAMIC_TYPE_X509_EXT);
16359-
if (x509->authKeyIdSrc == NULL) {
16360-
return WOLFSSL_FAILURE;
16361-
}
16357+
/* Allocate new buffer up front so failure leaves prior state intact */
16358+
newAkid = (byte*)XMALLOC((word32)akidSz, x509->heap, DYNAMIC_TYPE_X509_EXT);
16359+
if (newAkid == NULL) {
16360+
return WOLFSSL_FAILURE;
16361+
}
16362+
XMEMCPY(newAkid, akid, (word32)akidSz);
16363+
16364+
/* Free any prior storage. authKeyIdSrc may be populated from a prior
16365+
* parse cert operation. authKeyId aliases inside that buffer, so
16366+
* authKeyIdSrc must be freed first to avoid a dangling authKeyId. */
16367+
if (x509->authKeyIdSrc != NULL) {
16368+
XFREE(x509->authKeyIdSrc, x509->heap, DYNAMIC_TYPE_X509_EXT);
16369+
x509->authKeyIdSrc = NULL;
16370+
x509->authKeyIdSrcSz = 0;
16371+
}
16372+
else if (x509->authKeyId != NULL) {
16373+
XFREE(x509->authKeyId, x509->heap, DYNAMIC_TYPE_X509_EXT);
1636216374
}
1636316375

16364-
XMEMCPY(x509->authKeyIdSrc, akid, (word32)akidSz);
16365-
x509->authKeyIdSrcSz = (word32)akidSz;
16366-
x509->authKeyId = x509->authKeyIdSrc;
16376+
/* Store newAkid as authKeyId only, do not populate authKeyIdSrc.
16377+
* When authKeyIdSrc is non-NULL, the encoder writes those bytes without
16378+
* SEQUENCE/[0] wrapper. authKeyIdSrc must be NULL here so encoder does
16379+
* wrap them. */
16380+
x509->authKeyId = newAkid;
1636716381
x509->authKeyIdSz = (word32)akidSz;
1636816382
x509->authKeyIdSet = 1;
1636916383

0 commit comments

Comments
 (0)