Skip to content

Commit 7b82224

Browse files
committed
tests: add unit coverage for GetCAByAKID
1 parent 8741805 commit 7b82224

4 files changed

Lines changed: 98 additions & 3 deletions

File tree

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 */

wolfssl/internal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6698,7 +6698,7 @@ WOLFSSL_LOCAL WC_RNG* WOLFSSL_RSA_GetRNG(WOLFSSL_RSA *rsa, WC_RNG **tmpRNG,
66986698
WOLFSSL_LOCAL Signer* GetCA(void* vp, byte* hash);
66996699
#endif
67006700
#if defined(WOLFSSL_AKID_NAME) && !defined(GetCAByAKID)
6701-
WOLFSSL_LOCAL Signer* GetCAByAKID(void* vp, const byte* issuer,
6701+
WOLFSSL_TEST_VIS Signer* GetCAByAKID(void* vp, const byte* issuer,
67026702
word32 issuerSz, const byte* serial, word32 serialSz);
67036703
#endif
67046704
#if defined(HAVE_OCSP) && !defined(GetCAByKeyHash)

wolfssl/wolfcrypt/asn.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2060,7 +2060,7 @@ typedef enum MimeStatus
20602060
#endif /* HAVE_SMIME */
20612061

20622062
WOLFSSL_LOCAL int HashIdAlg(word32 oidSum);
2063-
WOLFSSL_LOCAL int CalcHashId(const byte* data, word32 len, byte* hash);
2063+
WOLFSSL_TEST_VIS int CalcHashId(const byte* data, word32 len, byte* hash);
20642064
WOLFSSL_LOCAL int CalcHashId_ex(const byte* data, word32 len, byte* hash,
20652065
int hashAlg);
20662066
WOLFSSL_LOCAL int GetHashId(const byte* id, int length, byte* hash,

0 commit comments

Comments
 (0)