Skip to content

Commit 45b7fb9

Browse files
authored
Merge pull request #9489 from julek-wolfssl/zd/20860
Fix AKID CA lookup
2 parents 3062d15 + 22eedee commit 45b7fb9

6 files changed

Lines changed: 120 additions & 20 deletions

File tree

src/ssl.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5760,7 +5760,7 @@ Signer* GetCAByAKID(void* vp, const byte* issuer, word32 issuerSz,
57605760
for (row = 0; row < CA_TABLE_SIZE && ret == NULL; row++) {
57615761
for (signers = cm->caTable[row]; signers != NULL;
57625762
signers = signers->next) {
5763-
if (XMEMCMP(signers->subjectNameHash, nameHash, SIGNER_DIGEST_SIZE)
5763+
if (XMEMCMP(signers->issuerNameHash, nameHash, SIGNER_DIGEST_SIZE)
57645764
== 0 && XMEMCMP(signers->serialHash, serialHash,
57655765
SIGNER_DIGEST_SIZE) == 0) {
57665766
ret = signers;

tests/api/test_x509.c

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@
3636
#include <wolfssl/openssl/x509.h>
3737
#include <wolfssl/openssl/x509v3.h>
3838

39+
#include <wolfssl/internal.h>
40+
#include <wolfssl/wolfcrypt/asn.h>
41+
3942
#if defined(OPENSSL_ALL) && \
4043
defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
4144
#define HAVE_TEST_X509_RFC2818_VERIFICATION_CALLBACK
@@ -148,3 +151,93 @@ int test_x509_rfc2818_verification_callback(void)
148151
#endif
149152
return EXPECT_RESULT();
150153
}
154+
155+
/* Basic unit coverage for GetCAByAKID.
156+
*
157+
* These tests construct a minimal WOLFSSL_CERT_MANAGER and Signer objects in
158+
* memory and then call GetCAByAKID directly, verifying that:
159+
* - a NULL or incomplete input returns NULL,
160+
* - a matching issuer/serial pair returns the expected Signer, and
161+
* - a non-matching pair returns NULL.
162+
*
163+
* These tests are intended to check the behaviour of the lookup logic itself;
164+
* they do not exercise certificate parsing or real CA loading.
165+
*/
166+
int test_x509_GetCAByAKID(void)
167+
{
168+
EXPECT_DECLS;
169+
#ifdef WOLFSSL_AKID_NAME
170+
WOLFSSL_CERT_MANAGER cm;
171+
Signer signerA;
172+
Signer signerB;
173+
Signer* found;
174+
byte issuerBuf[] = { 0x01, 0x02, 0x03, 0x04 };
175+
byte serialBuf[] = { 0x0a, 0x0b, 0x0c, 0x0d };
176+
byte wrongSerial[] = { 0x07, 0x07, 0x07, 0x07 };
177+
byte issuerHash[SIGNER_DIGEST_SIZE];
178+
byte serialHash[SIGNER_DIGEST_SIZE];
179+
word32 row;
180+
181+
XMEMSET(&cm, 0, sizeof(cm));
182+
XMEMSET(&signerA, 0, sizeof(signerA));
183+
XMEMSET(&signerB, 0, sizeof(signerB));
184+
185+
/* Initialize CA mutex so GetCAByAKID can lock/unlock it. */
186+
ExpectIntEQ(wc_InitMutex(&cm.caLock), 0);
187+
188+
/* Place both signers into the same CA table bucket. */
189+
row = 0;
190+
cm.caTable[row] = &signerA;
191+
signerA.next = &signerB;
192+
signerB.next = NULL;
193+
194+
/* Pre-compute the expected name and serial hashes using the same helper
195+
* that GetCAByAKID uses internally. */
196+
ExpectIntEQ(CalcHashId(issuerBuf, sizeof(issuerBuf), issuerHash), 0);
197+
ExpectIntEQ(CalcHashId(serialBuf, sizeof(serialBuf), serialHash), 0);
198+
199+
/* Configure signerA as the matching signer. */
200+
XMEMCPY(signerA.issuerNameHash, issuerHash, SIGNER_DIGEST_SIZE);
201+
XMEMCPY(signerA.serialHash, serialHash, SIGNER_DIGEST_SIZE);
202+
203+
/* Configure signerB with different hashes so it should not match. */
204+
XMEMSET(signerB.issuerNameHash, 0x11, SIGNER_DIGEST_SIZE);
205+
XMEMSET(signerB.serialHash, 0x22, SIGNER_DIGEST_SIZE);
206+
207+
/* 1) NULL manager should yield NULL. */
208+
found = GetCAByAKID(NULL, issuerBuf, (word32)sizeof(issuerBuf),
209+
serialBuf, (word32)sizeof(serialBuf));
210+
ExpectNull(found);
211+
212+
/* 2) NULL issuer should yield NULL. */
213+
found = GetCAByAKID(&cm, NULL, (word32)sizeof(issuerBuf),
214+
serialBuf, (word32)sizeof(serialBuf));
215+
ExpectNull(found);
216+
217+
/* 3) NULL serial should yield NULL. */
218+
found = GetCAByAKID(&cm, issuerBuf, (word32)sizeof(issuerBuf),
219+
NULL, (word32)sizeof(serialBuf));
220+
ExpectNull(found);
221+
222+
/* 4) Zero-length issuer/serial should yield NULL. */
223+
found = GetCAByAKID(&cm, issuerBuf, 0, serialBuf, (word32)sizeof(serialBuf));
224+
ExpectNull(found);
225+
found = GetCAByAKID(&cm, issuerBuf, (word32)sizeof(issuerBuf),
226+
serialBuf, 0);
227+
ExpectNull(found);
228+
229+
/* 5) Non-matching serial should yield NULL. */
230+
found = GetCAByAKID(&cm, issuerBuf, (word32)sizeof(issuerBuf),
231+
wrongSerial, (word32)sizeof(wrongSerial));
232+
ExpectNull(found);
233+
234+
/* 6) Matching issuer/serial should return signerA. */
235+
found = GetCAByAKID(&cm, issuerBuf, (word32)sizeof(issuerBuf),
236+
serialBuf, (word32)sizeof(serialBuf));
237+
ExpectPtrEq(found, &signerA);
238+
239+
wc_FreeMutex(&cm.caLock);
240+
241+
#endif /* WOLFSSL_AKID_NAME */
242+
return EXPECT_RESULT();
243+
}

tests/api/test_x509.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@
2323
#define WOLFCRYPT_TEST_X509_H
2424

2525
int test_x509_rfc2818_verification_callback(void);
26+
int test_x509_GetCAByAKID(void);
2627

2728
#define TEST_X509_DECLS \
28-
TEST_DECL_GROUP("x509", test_x509_rfc2818_verification_callback)
29+
TEST_DECL_GROUP("x509", test_x509_rfc2818_verification_callback), \
30+
TEST_DECL_GROUP("x509", test_x509_GetCAByAKID)
2931

3032
#endif /* WOLFCRYPT_TEST_X509_H */

wolfcrypt/src/asn.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26077,7 +26077,7 @@ int FillSigner(Signer* signer, DecodedCert* cert, int type, DerBuffer *der)
2607726077
#endif
2607826078
XMEMCPY(signer->subjectNameHash, cert->subjectHash,
2607926079
SIGNER_DIGEST_SIZE);
26080-
#if defined(HAVE_OCSP) || defined(HAVE_CRL)
26080+
#if defined(HAVE_OCSP) || defined(HAVE_CRL) || defined(WOLFSSL_AKID_NAME)
2608126081
XMEMCPY(signer->issuerNameHash, cert->issuerHash,
2608226082
SIGNER_DIGEST_SIZE);
2608326083
#endif

wolfssl/internal.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6694,11 +6694,15 @@ WOLFSSL_LOCAL WC_RNG* WOLFSSL_RSA_GetRNG(WOLFSSL_RSA *rsa, WC_RNG **tmpRNG,
66946694
DecodedCert* cert);
66956695
#endif
66966696

6697+
66976698
#ifndef GetCA
66986699
WOLFSSL_LOCAL Signer* GetCA(void* vp, byte* hash);
66996700
#endif
67006701
#if defined(WOLFSSL_AKID_NAME) && !defined(GetCAByAKID)
6701-
WOLFSSL_LOCAL Signer* GetCAByAKID(void* vp, const byte* issuer,
6702+
#ifdef WOLFSSL_API_PREFIX_MAP
6703+
#define GetCAByAKID wolfSSL_GetCAByAKID
6704+
#endif
6705+
WOLFSSL_TEST_VIS Signer* GetCAByAKID(void* vp, const byte* issuer,
67026706
word32 issuerSz, const byte* serial, word32 serialSz);
67036707
#endif
67046708
#if defined(HAVE_OCSP) && !defined(GetCAByKeyHash)

wolfssl/wolfcrypt/asn.h

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1957,7 +1957,7 @@ struct Signer {
19571957
#endif /* !IGNORE_NAME_CONSTRAINTS */
19581958
byte subjectNameHash[SIGNER_DIGEST_SIZE];
19591959
/* sha hash of names in certificate */
1960-
#if defined(HAVE_OCSP) || defined(HAVE_CRL)
1960+
#if defined(HAVE_OCSP) || defined(HAVE_CRL) || defined(WOLFSSL_AKID_NAME)
19611961
byte issuerNameHash[SIGNER_DIGEST_SIZE];
19621962
/* sha hash of issuer names in certificate.
19631963
* Used in OCSP to check for authorized
@@ -2059,21 +2059,6 @@ typedef enum MimeStatus
20592059
} MimeStatus;
20602060
#endif /* HAVE_SMIME */
20612061

2062-
WOLFSSL_LOCAL int HashIdAlg(word32 oidSum);
2063-
WOLFSSL_LOCAL int CalcHashId(const byte* data, word32 len, byte* hash);
2064-
WOLFSSL_LOCAL int CalcHashId_ex(const byte* data, word32 len, byte* hash,
2065-
int hashAlg);
2066-
WOLFSSL_LOCAL int GetHashId(const byte* id, int length, byte* hash,
2067-
int hashAlg);
2068-
WOLFSSL_LOCAL int GetName(DecodedCert* cert, int nameType, int maxIdx);
2069-
2070-
#ifdef ASN_BER_TO_DER
2071-
WOLFSSL_API int wc_BerToDer(const byte* ber, word32 berSz, byte* der,
2072-
word32* derSz);
2073-
#endif
2074-
WOLFSSL_LOCAL int StreamOctetString(const byte* inBuf, word32 inBufSz,
2075-
byte* out, word32* outSz, word32* idx);
2076-
20772062
#ifdef WOLFSSL_API_PREFIX_MAP
20782063
#define FreeAltNames wc_FreeAltNames
20792064
#define AltNameNew wc_AltNameNew
@@ -2098,8 +2083,24 @@ WOLFSSL_LOCAL int StreamOctetString(const byte* inBuf, word32 inBufSz,
20982083
#define GetASNTag wc_GetASNTag
20992084
#define SetAlgoID wc_SetAlgoID
21002085
#define SetAsymKeyDer wc_SetAsymKeyDer
2086+
#define CalcHashId wc_CalcHashId
21012087
#endif /* WOLFSSL_API_PREFIX_MAP */
21022088

2089+
WOLFSSL_LOCAL int HashIdAlg(word32 oidSum);
2090+
WOLFSSL_TEST_VIS int CalcHashId(const byte* data, word32 len, byte* hash);
2091+
WOLFSSL_LOCAL int CalcHashId_ex(const byte* data, word32 len, byte* hash,
2092+
int hashAlg);
2093+
WOLFSSL_LOCAL int GetHashId(const byte* id, int length, byte* hash,
2094+
int hashAlg);
2095+
WOLFSSL_LOCAL int GetName(DecodedCert* cert, int nameType, int maxIdx);
2096+
2097+
#ifdef ASN_BER_TO_DER
2098+
WOLFSSL_API int wc_BerToDer(const byte* ber, word32 berSz, byte* der,
2099+
word32* derSz);
2100+
#endif
2101+
WOLFSSL_LOCAL int StreamOctetString(const byte* inBuf, word32 inBufSz,
2102+
byte* out, word32* outSz, word32* idx);
2103+
21032104
WOLFSSL_ASN_API void FreeAltNames(DNS_entry* altNames, void* heap);
21042105
WOLFSSL_ASN_API DNS_entry* AltNameNew(void* heap);
21052106
WOLFSSL_ASN_API DNS_entry* AltNameDup(DNS_entry* from, void* heap);

0 commit comments

Comments
 (0)