Skip to content

Commit 2151a1b

Browse files
committed
review comments
1 parent 79f214f commit 2151a1b

3 files changed

Lines changed: 51 additions & 45 deletions

File tree

tests/api.c

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22755,7 +22755,7 @@ static int test_wc_GetPubKeyDerFromCert(void)
2275522755
return EXPECT_RESULT();
2275622756
}
2275722757

22758-
static int test_wc_ExportX509PubKeyWithSpki(void)
22758+
static int test_wc_GetSubjectPubKeyInfoDerFromCert(void)
2275922759
{
2276022760
EXPECT_DECLS;
2276122761
#if !defined(NO_RSA) || defined(HAVE_ECC)
@@ -22809,8 +22809,8 @@ static int test_wc_ExportX509PubKeyWithSpki(void)
2280922809
#endif
2281022810

2281122811
/* good test case - RSA DER cert */
22812-
ExpectIntEQ(wc_ExportX509PubKeyWithSpki(rsaCertDer, rsaCertDerSz, keyDer,
22813-
&keyDerSz), 0);
22812+
ExpectIntEQ(wc_GetSubjectPubKeyInfoDerFromCert(rsaCertDer, rsaCertDerSz,
22813+
keyDer, &keyDerSz), 0);
2281422814
ExpectIntGT(keyDerSz, 0);
2281522815

2281622816
/* sanity check, verify we can import DER public key */
@@ -22823,18 +22823,20 @@ static int test_wc_ExportX509PubKeyWithSpki(void)
2282322823

2282422824
/* bad args: certDer */
2282522825
keyDerSz = (word32)sizeof(keyDer);
22826-
ExpectIntEQ(wc_ExportX509PubKeyWithSpki(NULL, rsaCertDerSz, keyDer,
22827-
&keyDerSz),
22826+
ExpectIntEQ(wc_GetSubjectPubKeyInfoDerFromCert(NULL, rsaCertDerSz, keyDer,
22827+
&keyDerSz),
2282822828
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
2282922829

2283022830
/* bad args: 0 sized certSz */
2283122831
keyDerSz = (word32)sizeof(keyDer);
22832-
ExpectIntEQ(wc_ExportX509PubKeyWithSpki(rsaCertDer, 0, keyDer, &keyDerSz),
22832+
ExpectIntEQ(wc_GetSubjectPubKeyInfoDerFromCert(rsaCertDer, 0, keyDer,
22833+
&keyDerSz),
2283322834
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
2283422835

2283522836
/* bad args: NULL inout size */
22836-
ExpectIntEQ(ret = wc_ExportX509PubKeyWithSpki(rsaCertDer, rsaCertDerSz,
22837-
keyDer, NULL),
22837+
ExpectIntEQ(ret = wc_GetSubjectPubKeyInfoDerFromCert(rsaCertDer,
22838+
rsaCertDerSz, keyDer,
22839+
NULL),
2283822840
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
2283922841

2284022842
/* Certificate Request Tests */
@@ -22849,8 +22851,10 @@ static int test_wc_ExportX509PubKeyWithSpki(void)
2284922851

2285022852
/* good test case - RSA DER certificate request */
2285122853
keyDerSz = sizeof(keyDer);
22852-
ExpectIntEQ(ret = wc_ExportX509PubKeyWithSpki(rsaCertDer, rsaCertDerSz,
22853-
keyDer, &keyDerSz), 0);
22854+
ExpectIntEQ(ret = wc_GetSubjectPubKeyInfoDerFromCert(rsaCertDer,
22855+
rsaCertDerSz,
22856+
keyDer,
22857+
&keyDerSz), 0);
2285422858
ExpectIntGT(keyDerSz, 0);
2285522859

2285622860
/* sanity check, verify we can import DER public key */
@@ -22878,8 +22882,8 @@ static int test_wc_ExportX509PubKeyWithSpki(void)
2287822882
/* good test case - ECC */
2287922883
XMEMSET(keyDer, 0, sizeof(keyDer));
2288022884
keyDerSz = sizeof(keyDer);
22881-
ExpectIntEQ(wc_ExportX509PubKeyWithSpki(eccCert, eccCertSz, keyDer,
22882-
&keyDerSz), 0);
22885+
ExpectIntEQ(wc_GetSubjectPubKeyInfoDerFromCert(eccCert, eccCertSz, keyDer,
22886+
&keyDerSz), 0);
2288322887
ExpectIntGT(keyDerSz, 0);
2288422888

2288522889
/* sanity check, verify we can import DER public key */
@@ -66987,7 +66991,7 @@ TEST_CASE testCases[] = {
6698766991
TEST_DECL(test_wc_PubKeyPemToDer),
6698866992
TEST_DECL(test_wc_PemPubKeyToDer),
6698966993
TEST_DECL(test_wc_GetPubKeyDerFromCert),
66990-
TEST_DECL(test_wc_ExportX509PubKeyWithSpki),
66994+
TEST_DECL(test_wc_GetSubjectPubKeyInfoDerFromCert),
6699166995
TEST_DECL(test_wc_CheckCertSigPubKey),
6699266996

6699366997
/* wolfCrypt ASN tests */

wolfcrypt/src/asn.c

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -24628,59 +24628,60 @@ int wc_CertGetPubKey(const byte* cert, word32 certSz,
2462824628
* @return BAD_FUNC_ARG if certDer is NULL, certSz is 0, or pubKeyDerSz is NULL
2462924629
* @return BUFFER_E if the provided buffer is too small
2463024630
*/
24631-
WOLFSSL_API int wc_ExportX509PubKeyWithSpki(const byte* certDer, word32 certSz,
24632-
byte* pubKeyDer,
24633-
word32* pubKeyDerSz)
24631+
WOLFSSL_API int wc_GetSubjectPubKeyInfoDerFromCert(const byte* certDer,
24632+
word32 certSz,
24633+
byte* pubKeyDer,
24634+
word32* pubKeyDerSz)
2463424635
{
2463524636
DecodedCert cert;
2463624637
int ret;
2463724638
word32 startIdx;
2463824639
word32 idx;
2463924640
word32 length;
24640-
int badDate = 0;
24641+
int badDate;
2464124642

2464224643
if (certDer == NULL || certSz == 0 || pubKeyDerSz == NULL) {
2464324644
return BAD_FUNC_ARG;
2464424645
}
2464524646

24646-
/* Initialize decoded cert structure */
24647+
length = 0;
24648+
badDate = 0;
24649+
2464724650
wc_InitDecodedCert(&cert, certDer, certSz, NULL);
2464824651

2464924652
/* Parse up to the SubjectPublicKeyInfo */
2465024653
ret = wc_GetPubX509(&cert, 0, &badDate);
24651-
if (ret < 0) {
24652-
wc_FreeDecodedCert(&cert);
24653-
return ret;
24654-
}
24654+
if (ret >= 0) {
24655+
/* Save the starting index of SubjectPublicKeyInfo */
24656+
startIdx = cert.srcIdx;
2465524657

24656-
/* Save the starting index of SubjectPublicKeyInfo */
24657-
startIdx = cert.srcIdx;
24658+
/* Get the length of the SubjectPublicKeyInfo sequence */
24659+
idx = startIdx;
24660+
ret = GetSequence(certDer, &idx, (int*)&length, certSz);
24661+
if (ret >= 0) {
24662+
/* Calculate total length including sequence header */
24663+
length += (idx - startIdx);
2465824664

24659-
/* Get the length of the SubjectPublicKeyInfo sequence */
24660-
idx = startIdx;
24661-
ret = GetSequence(certDer, &idx, (int*)&length, certSz);
24662-
if (ret < 0) {
24663-
wc_FreeDecodedCert(&cert);
24664-
return ret;
24665+
/* Copy the SubjectPublicKeyInfo if buffer provided */
24666+
if (pubKeyDer != NULL) {
24667+
if (*pubKeyDerSz < (word32)length) {
24668+
ret = BUFFER_E;
24669+
}
24670+
else {
24671+
XMEMCPY(pubKeyDer, &certDer[startIdx], length);
24672+
}
24673+
}
24674+
}
2466524675
}
2466624676

24667-
/* Calculate total length including sequence header */
24668-
length += (idx - startIdx);
24669-
24670-
/* Copy the SubjectPublicKeyInfo if buffer provided */
24671-
if (pubKeyDer != NULL) {
24672-
if (*pubKeyDerSz < (word32)length) {
24673-
wc_FreeDecodedCert(&cert);
24674-
return BUFFER_E;
24675-
}
24676-
XMEMCPY(pubKeyDer, &certDer[startIdx], length);
24677+
if (ret >= 0) {
24678+
ret = 0;
2467724679
}
2467824680

24679-
/* Return the size */
2468024681
*pubKeyDerSz = length;
24681-
2468224682
wc_FreeDecodedCert(&cert);
24683-
return 0;
24683+
24684+
return ret;
2468424685
}
2468524686

2468624687

wolfssl/wolfcrypt/asn_public.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -879,8 +879,9 @@ WOLFSSL_API int wc_ParseCert(
879879

880880
WOLFSSL_API int wc_GetPubKeyDerFromCert(struct DecodedCert* cert,
881881
byte* derKey, word32* derKeySz);
882-
WOLFSSL_API int wc_ExportX509PubKeyWithSpki(const byte* cert, word32 certSz,
883-
byte* pubKey, word32* pubKeySz);
882+
WOLFSSL_API int wc_GetSubjectPubKeyInfoDerFromCert(const byte* cert,
883+
word32 certSz, byte* pubKey,
884+
word32* pubKeySz);
884885

885886
#ifdef WOLFSSL_FPKI
886887
WOLFSSL_API int wc_GetUUIDFromCert(struct DecodedCert* cert,

0 commit comments

Comments
 (0)