Skip to content

Commit 63fd322

Browse files
authored
Merge pull request #10641 from rlm2002/zd21890
Fixes for SM2/3 and FindMultiAttrib
2 parents d56fa79 + b167c26 commit 63fd322

3 files changed

Lines changed: 66 additions & 3 deletions

File tree

tests/api/test_asn.c

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1495,6 +1495,58 @@ int test_DecodeAltNames_length_underflow(void)
14951495
return EXPECT_RESULT();
14961496
}
14971497

1498+
int test_ParseCert_SM3wSM2_short_pubkey(void)
1499+
{
1500+
EXPECT_DECLS;
1501+
1502+
#if !defined(NO_CERTS) && !defined(NO_ASN) && !defined(NO_SKID) && \
1503+
defined(WOLFSSL_SM2) && defined(WOLFSSL_SM3)
1504+
/* Malformed cert: the SubjectPublicKeyInfo is an id-ecPublicKey key on the
1505+
* sm2p256v1 curve with only a 4-byte public key body, whole SPKI is 30
1506+
* bytes with no subjectKeyIdentifier extension and SKID derived from the
1507+
* key. */
1508+
static const byte sm2ShortKeyCert[] = {
1509+
0x30, 0x81, 0xa7,
1510+
0x30, 0x56,
1511+
0xa0, 0x03, 0x02, 0x01, 0x02,
1512+
0x02, 0x01, 0x01,
1513+
0x30, 0x0a, 0x06, 0x08,
1514+
0x2a, 0x81, 0x1c, 0xcf, 0x55, 0x01, 0x83, 0x75,
1515+
0x30, 0x00,
1516+
0x30, 0x1e,
1517+
0x17, 0x0d, 0x32, 0x35, 0x31, 0x31, 0x31, 0x33,
1518+
0x32, 0x30, 0x34, 0x31, 0x32, 0x31, 0x5a,
1519+
0x17, 0x0d, 0x32, 0x38, 0x30, 0x38, 0x30, 0x39,
1520+
0x32, 0x30, 0x34, 0x31, 0x32, 0x31, 0x5a,
1521+
0x30, 0x00,
1522+
0x30, 0x1c,
1523+
0x30, 0x13,
1524+
0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01,
1525+
0x06, 0x08, 0x2a, 0x81, 0x1c, 0xcf, 0x55, 0x01, 0x82, 0x2d,
1526+
0x03, 0x05, 0x00, 0x04, 0x11, 0x22, 0x33,
1527+
0x30, 0x0a, 0x06, 0x08,
1528+
0x2a, 0x81, 0x1c, 0xcf, 0x55, 0x01, 0x83, 0x75,
1529+
0x03, 0x41, 0x00,
1530+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1531+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1532+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1533+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1534+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1535+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1536+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1537+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
1538+
};
1539+
DecodedCert cert;
1540+
1541+
wc_InitDecodedCert(&cert, sm2ShortKeyCert, (word32)sizeof(sm2ShortKeyCert),
1542+
NULL);
1543+
ExpectIntEQ(wc_ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL),
1544+
WC_NO_ERR_TRACE(BUFFER_E));
1545+
wc_FreeDecodedCert(&cert);
1546+
#endif
1547+
return EXPECT_RESULT();
1548+
}
1549+
14981550
int test_SerialNumber0_RootCA(void)
14991551
{
15001552
EXPECT_DECLS;

tests/api/test_asn.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ int test_wolfssl_local_MatchUriNameConstraint(void);
3535
int test_wc_DecodeRsaPssParams(void);
3636
int test_SerialNumber0_RootCA(void);
3737
int test_DecodeAltNames_length_underflow(void);
38+
int test_ParseCert_SM3wSM2_short_pubkey(void);
3839
int test_wc_DecodeObjectId(void);
3940
int test_ToTraditional_ex_handcrafted(void);
4041
int test_ToTraditional_ex_roundtrip(void);
@@ -53,6 +54,7 @@ int test_ToTraditional_ex_mldsa_bad_params(void);
5354
TEST_DECL_GROUP("asn", test_wc_DecodeRsaPssParams), \
5455
TEST_DECL_GROUP("asn", test_SerialNumber0_RootCA), \
5556
TEST_DECL_GROUP("asn", test_DecodeAltNames_length_underflow), \
57+
TEST_DECL_GROUP("asn", test_ParseCert_SM3wSM2_short_pubkey), \
5658
TEST_DECL_GROUP("asn", test_wc_DecodeObjectId), \
5759
TEST_DECL_GROUP("asn", test_ToTraditional_ex_handcrafted), \
5860
TEST_DECL_GROUP("asn", test_ToTraditional_ex_roundtrip), \

wolfcrypt/src/asn.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23490,6 +23490,10 @@ int ParseCertRelative(DecodedCert* cert, int type, int verify, void* cm,
2349023490
if (cert->extSubjKeyIdSet == 0 && cert->publicKey != NULL &&
2349123491
cert->pubKeySize > 0) {
2349223492
if (cert->signatureOID == CTC_SM3wSM2) {
23493+
if (cert->pubKeySize < 65) {
23494+
WOLFSSL_ERROR_VERBOSE(BUFFER_E);
23495+
return BUFFER_E;
23496+
}
2349323497
/* TODO: GmSSL creates IDs this way but whole public key info
2349423498
* block should be hashed. */
2349523499
ret = CalcHashId_ex(cert->publicKey + cert->pubKeySize - 65, 65,
@@ -27262,7 +27266,7 @@ static void SetRdnItems(ASNItem* namesASN, ASNSetData* dataASN, const byte* oid,
2726227266
static int FindMultiAttrib(CertName* name, int id, int* idx)
2726327267
{
2726427268
int i;
27265-
for (i = *idx + 1; i < CTC_MAX_ATTRIB; i++) {
27269+
for (i = *idx + 1; i >= 0 && i < CTC_MAX_ATTRIB; i++) {
2726627270
if (name->name[i].sz > 0 && name->name[i].id == id) {
2726727271
break;
2726827272
}
@@ -30806,11 +30810,16 @@ static int SetAuthKeyIdFromDcert(Cert* cert, DecodedCert* decoded)
3080630810
#if defined(WOLFSSL_SM2) && defined(WOLFSSL_SM3)
3080730811
cert->akidSz = wc_HashGetDigestSize(wc_HashTypeConvert(HashIdAlg(
3080830812
cert->sigType)));
30813+
if (cert->akidSz <= 0) {
30814+
ret = HASH_TYPE_E;
30815+
}
3080930816
#else
3081030817
cert->akidSz = KEYID_SIZE;
3081130818
#endif
30812-
/* Put the SKID of CA to AKID of certificate */
30813-
XMEMCPY(cert->akid, decoded->extSubjKeyId, (size_t)cert->akidSz);
30819+
if (ret == 0) {
30820+
/* Put the SKID of CA to AKID of certificate */
30821+
XMEMCPY(cert->akid, decoded->extSubjKeyId, (size_t)cert->akidSz);
30822+
}
3081430823
}
3081530824

3081630825
return ret;

0 commit comments

Comments
 (0)