Skip to content

Commit 3a6ccc1

Browse files
committed
Fix FillSigner to clear pubkeystored
1 parent 4b8c524 commit 3a6ccc1

3 files changed

Lines changed: 123 additions & 5 deletions

File tree

wolfcrypt/src/asn.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26906,7 +26906,9 @@ int FillSigner(Signer* signer, DecodedCert* cert, int type, DerBuffer *der)
2690626906
signer->extKeyUsage = cert->extExtKeyUsage;
2690726907
signer->next = NULL; /* If Key Usage not set, all uses valid. */
2690826908
cert->publicKey = 0; /* in case lock fails don't free here. */
26909+
cert->pubKeyStored = 0;
2690926910
cert->subjectCN = 0;
26911+
cert->subjectCNStored = 0;
2691026912
#ifndef IGNORE_NAME_CONSTRAINTS
2691126913
cert->permittedNames = NULL;
2691226914
cert->excludedNames = NULL;

wolfcrypt/test/test.c

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -822,6 +822,7 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t scrypt_test(void);
822822
#if !defined(NO_ASN_TIME) && !defined(NO_RSA) && defined(WOLFSSL_TEST_CERT) && \
823823
!defined(NO_FILESYSTEM)
824824
WOLFSSL_TEST_SUBROUTINE wc_test_ret_t cert_test(void);
825+
static wc_test_ret_t fill_signer_twice_test(void);
825826
#endif
826827
#if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT) && \
827828
!defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(WOLFSSL_GEN_CERT)
@@ -2733,6 +2734,11 @@ options: [-s max_relative_stack_bytes] [-m max_relative_heap_memory_bytes]\n\
27332734
TEST_FAIL("CERT test failed!\n", ret);
27342735
else
27352736
TEST_PASS("CERT test passed!\n");
2737+
2738+
if ( (ret = fill_signer_twice_test()) != 0)
2739+
TEST_FAIL("FILL SIGNER test failed!\n", ret);
2740+
else
2741+
TEST_PASS("FILL SIGNER test passed!\n");
27362742
#endif
27372743

27382744
#if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT) && \
@@ -22252,6 +22258,111 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t cert_test(void)
2225222258
}
2225322259
#endif /* WOLFSSL_TEST_CERT */
2225422260

22261+
#if !defined(NO_ASN_TIME) && !defined(NO_RSA) && defined(WOLFSSL_TEST_CERT) && \
22262+
!defined(NO_FILESYSTEM)
22263+
/* Test that FillSigner clears pubKeyStored/subjectCNStored after transferring
22264+
* ownership, so a second call doesn't copy stale NULL pointers. */
22265+
static wc_test_ret_t fill_signer_twice_test(void)
22266+
{
22267+
DecodedCert cert;
22268+
Signer* signer1 = NULL;
22269+
Signer* signer2 = NULL;
22270+
DerBuffer* der = NULL;
22271+
byte* tmp = NULL;
22272+
size_t bytes;
22273+
XFILE file;
22274+
wc_test_ret_t ret;
22275+
22276+
WOLFSSL_ENTER("fill_signer_twice_test");
22277+
22278+
tmp = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
22279+
if (tmp == NULL)
22280+
return WC_TEST_RET_ENC_ERRNO;
22281+
22282+
/* Load a DER certificate. */
22283+
file = XFOPEN(certExtNc, "rb");
22284+
if (!file) {
22285+
ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
22286+
}
22287+
bytes = XFREAD(tmp, 1, FOURK_BUF, file);
22288+
XFCLOSE(file);
22289+
if (bytes == 0)
22290+
ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
22291+
22292+
/* Create a DerBuffer for FillSigner (needed when WOLFSSL_SIGNER_DER_CERT
22293+
* is defined). */
22294+
ret = AllocDer(&der, (word32)bytes, CERT_TYPE, HEAP_HINT);
22295+
if (ret != 0)
22296+
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
22297+
XMEMCPY(der->buffer, tmp, bytes);
22298+
22299+
InitDecodedCert(&cert, tmp, (word32)bytes, 0);
22300+
ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
22301+
if (ret != 0)
22302+
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
22303+
22304+
/* After parsing, pubKeyStored should be set and publicKey non-NULL. */
22305+
if (!cert.pubKeyStored || cert.publicKey == NULL) {
22306+
ERROR_OUT(WC_TEST_RET_ENC_NC, done);
22307+
}
22308+
22309+
/* First FillSigner: transfers publicKey and subjectCN ownership. */
22310+
signer1 = MakeSigner(HEAP_HINT);
22311+
if (signer1 == NULL)
22312+
ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
22313+
22314+
ret = FillSigner(signer1, &cert, CA_TYPE, der);
22315+
if (ret != 0)
22316+
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
22317+
22318+
/* signer1 should have received the publicKey. */
22319+
if (signer1->publicKey == NULL) {
22320+
ERROR_OUT(WC_TEST_RET_ENC_NC, done);
22321+
}
22322+
22323+
/* After FillSigner, cert->publicKey should be NULL. */
22324+
if (cert.publicKey != NULL) {
22325+
ERROR_OUT(WC_TEST_RET_ENC_NC, done);
22326+
}
22327+
22328+
/* BUG CHECK: pubKeyStored should have been cleared to 0.
22329+
* If it is still set, a second FillSigner would copy a NULL pointer. */
22330+
if (cert.pubKeyStored != 0) {
22331+
ERROR_OUT(WC_TEST_RET_ENC_NC, done);
22332+
}
22333+
22334+
/* Also check subjectCNStored is cleared. */
22335+
if (cert.subjectCNStored != 0) {
22336+
ERROR_OUT(WC_TEST_RET_ENC_NC, done);
22337+
}
22338+
22339+
/* Second FillSigner on the same cert should not copy NULL pointers. */
22340+
signer2 = MakeSigner(HEAP_HINT);
22341+
if (signer2 == NULL)
22342+
ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
22343+
22344+
ret = FillSigner(signer2, &cert, CA_TYPE, der);
22345+
if (ret != 0)
22346+
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
22347+
22348+
/* signer2 should NOT have a publicKey (since cert no longer owns one). */
22349+
if (signer2->publicKey != NULL) {
22350+
ERROR_OUT(WC_TEST_RET_ENC_NC, done);
22351+
}
22352+
22353+
done:
22354+
FreeDecodedCert(&cert);
22355+
if (signer1 != NULL)
22356+
FreeSigner(signer1, HEAP_HINT);
22357+
if (signer2 != NULL)
22358+
FreeSigner(signer2, HEAP_HINT);
22359+
FreeDer(&der);
22360+
XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
22361+
22362+
return ret;
22363+
}
22364+
#endif /* !NO_ASN_TIME && !NO_RSA && WOLFSSL_TEST_CERT && !NO_FILESYSTEM */
22365+
2225522366
#if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT) && \
2225622367
!defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(WOLFSSL_GEN_CERT)
2225722368
WOLFSSL_TEST_SUBROUTINE wc_test_ret_t certext_test(void)

wolfssl/wolfcrypt/asn.h

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2254,6 +2254,11 @@ typedef enum MimeStatus
22542254
*/
22552255
#define GetCAByAKID wolfSSL_GetCAByAKID
22562256
#endif
2257+
#define FillSigner wc_FillSigner
2258+
#define MakeSigner wc_MakeSigner
2259+
#define FreeSigner wc_FreeSigner
2260+
#define AllocDer wc_AllocDer
2261+
#define FreeDer wc_FreeDer
22572262
#endif /* WOLFSSL_API_PREFIX_MAP */
22582263

22592264
WOLFSSL_LOCAL int HashIdAlg(word32 oidSum);
@@ -2363,9 +2368,9 @@ WOLFSSL_LOCAL int wc_GetPubX509(DecodedCert* cert, int verify, int* badDate);
23632368
WOLFSSL_LOCAL const byte* OidFromId(word32 id, word32 type, word32* oidSz);
23642369
WOLFSSL_LOCAL Signer* findSignerByKeyHash(Signer *list, byte *hash);
23652370
WOLFSSL_LOCAL Signer* findSignerByName(Signer *list, byte *hash);
2366-
WOLFSSL_LOCAL int FillSigner(Signer* signer, DecodedCert* cert, int type, DerBuffer *der);
2367-
WOLFSSL_LOCAL Signer* MakeSigner(void* heap);
2368-
WOLFSSL_LOCAL void FreeSigner(Signer* signer, void* heap);
2371+
WOLFSSL_TEST_VIS int FillSigner(Signer* signer, DecodedCert* cert, int type, DerBuffer *der);
2372+
WOLFSSL_TEST_VIS Signer* MakeSigner(void* heap);
2373+
WOLFSSL_TEST_VIS void FreeSigner(Signer* signer, void* heap);
23692374
WOLFSSL_LOCAL void FreeSignerTable(Signer** table, int rows, void* heap);
23702375
WOLFSSL_LOCAL void FreeSignerTableType(Signer** table, int rows, byte type,
23712376
void* heap);
@@ -2608,11 +2613,11 @@ WOLFSSL_LOCAL int wc_EncryptedInfoParse(EncryptedInfo* info,
26082613
WOLFSSL_LOCAL int PemToDer(const unsigned char* buff, long longSz, int type,
26092614
DerBuffer** pDer, void* heap, EncryptedInfo* info,
26102615
int* keyFormat);
2611-
WOLFSSL_LOCAL int AllocDer(DerBuffer** der, word32 length, int type,
2616+
WOLFSSL_TEST_VIS int AllocDer(DerBuffer** der, word32 length, int type,
26122617
void* heap);
26132618
WOLFSSL_LOCAL int AllocCopyDer(DerBuffer** der, const unsigned char* buff,
26142619
word32 length, int type, void* heap);
2615-
WOLFSSL_LOCAL void FreeDer(DerBuffer** der);
2620+
WOLFSSL_TEST_VIS void FreeDer(DerBuffer** der);
26162621

26172622
#ifdef WOLFSSL_ASN_PARSE_KEYUSAGE
26182623
WOLFSSL_LOCAL int ParseKeyUsageStr(const char* value, word16* keyUsage,

0 commit comments

Comments
 (0)