|
36 | 36 | #include <wolfssl/openssl/x509.h> |
37 | 37 | #include <wolfssl/openssl/x509v3.h> |
38 | 38 |
|
| 39 | +#include <wolfssl/internal.h> |
| 40 | +#include <wolfssl/wolfcrypt/asn.h> |
| 41 | + |
39 | 42 | #if defined(OPENSSL_ALL) && \ |
40 | 43 | defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) |
41 | 44 | #define HAVE_TEST_X509_RFC2818_VERIFICATION_CALLBACK |
@@ -148,3 +151,93 @@ int test_x509_rfc2818_verification_callback(void) |
148 | 151 | #endif |
149 | 152 | return EXPECT_RESULT(); |
150 | 153 | } |
| 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 | +} |
0 commit comments