Skip to content

Commit 292ea54

Browse files
committed
wolfcrypt/src/asn.c: fixes for invalid memory access in wc_DsaPublicKeyDecode() and wc_EccPublicKeyDecode(), detected by cppcheck-force-source, lms-xmss-wolfssl-all-clang-sanitizer, and sanitizer-clang-all-noasm.
1 parent 52d5d0a commit 292ea54

1 file changed

Lines changed: 35 additions & 36 deletions

File tree

wolfcrypt/src/asn.c

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -11461,14 +11461,15 @@ int wc_DsaPublicKeyDecode(const byte* input, word32* inOutIdx, DsaKey* key,
1146111461

1146211462
/* Validated parameters. */
1146311463
if ((input == NULL) || (inOutIdx == NULL) || (key == NULL)) {
11464-
ret = BAD_FUNC_ARG;
11464+
return BAD_FUNC_ARG;
1146511465
}
1146611466

11467-
if (ret == 0) {
11468-
ALLOC_ASNGETDATA(dataASN, dsaPubKeyASN_Length, ret, key->heap);
11469-
}
11467+
ALLOC_ASNGETDATA(dataASN, dsaPubKeyASN_Length, ret, key->heap);
1147011468

11471-
if (ret == 0) {
11469+
if (ret != 0)
11470+
return ret;
11471+
11472+
{
1147211473
int i;
1147311474

1147411475
/* Clear dynamic data items. */
@@ -30137,44 +30138,42 @@ int wc_EccPublicKeyDecode(const byte* input, word32* inOutIdx,
3013730138
int pubIdx = ECCPUBLICKEYASN_IDX_PUBKEY;
3013830139

3013930140
if ((input == NULL) || (inOutIdx == NULL) || (key == NULL) || (inSz == 0)) {
30140-
ret = BAD_FUNC_ARG;
30141+
return BAD_FUNC_ARG;
3014130142
}
3014230143

30143-
if (ret == 0) {
30144-
ALLOC_ASNGETDATA(dataASN, eccKeyASN_Length, ret, key->heap);
30145-
}
30144+
ALLOC_ASNGETDATA(dataASN, eccKeyASN_Length, ret, key->heap);
30145+
if (ret != 0)
30146+
return ret;
3014630147

30147-
if (ret == 0) {
30148-
/* Clear dynamic data for ECC public key. */
30149-
XMEMSET(dataASN, 0, sizeof(*dataASN) * eccPublicKeyASN_Length);
30148+
/* Clear dynamic data for ECC public key. */
30149+
XMEMSET(dataASN, 0, sizeof(*dataASN) * eccPublicKeyASN_Length);
3015030150
#if !defined(WOLFSSL_SM2) || !defined(WOLFSSL_SM3)
30151-
/* Set required ECDSA OID and ignore the curve OID type. */
30152-
GetASN_ExpBuffer(&dataASN[ECCPUBLICKEYASN_IDX_ALGOID_OID], keyEcdsaOid,
30153-
sizeof(keyEcdsaOid));
30151+
/* Set required ECDSA OID and ignore the curve OID type. */
30152+
GetASN_ExpBuffer(&dataASN[ECCPUBLICKEYASN_IDX_ALGOID_OID], keyEcdsaOid,
30153+
sizeof(keyEcdsaOid));
3015430154
#else
30155-
GetASN_OID(&dataASN[ECCPUBLICKEYASN_IDX_ALGOID_OID], oidKeyType);
30155+
GetASN_OID(&dataASN[ECCPUBLICKEYASN_IDX_ALGOID_OID], oidKeyType);
3015630156
#endif
30157+
GetASN_OID(&dataASN[oidIdx], oidCurveType);
30158+
/* Decode the public ECC key. */
30159+
ret = GetASN_Items(eccPublicKeyASN, dataASN, eccPublicKeyASN_Length, 1,
30160+
input, inOutIdx, inSz);
30161+
if (ret != 0) {
30162+
oidIdx = ECCKEYASN_IDX_CURVEID;
30163+
#ifdef WOLFSSL_CUSTOM_CURVES
30164+
specIdx = ECCKEYASN_IDX_CURVEPARAMS;
30165+
#endif
30166+
pubIdx = ECCKEYASN_IDX_PUBKEY_VAL;
30167+
30168+
/* Clear dynamic data for ECC private key. */
30169+
XMEMSET(dataASN, 0, sizeof(*dataASN) * eccKeyASN_Length);
30170+
/* Check named curve OID type. */
3015730171
GetASN_OID(&dataASN[oidIdx], oidCurveType);
30158-
/* Decode the public ECC key. */
30159-
ret = GetASN_Items(eccPublicKeyASN, dataASN, eccPublicKeyASN_Length, 1,
30160-
input, inOutIdx, inSz);
30172+
/* Try private key format .*/
30173+
ret = GetASN_Items(eccKeyASN, dataASN, eccKeyASN_Length, 1, input,
30174+
inOutIdx, inSz);
3016130175
if (ret != 0) {
30162-
oidIdx = ECCKEYASN_IDX_CURVEID;
30163-
#ifdef WOLFSSL_CUSTOM_CURVES
30164-
specIdx = ECCKEYASN_IDX_CURVEPARAMS;
30165-
#endif
30166-
pubIdx = ECCKEYASN_IDX_PUBKEY_VAL;
30167-
30168-
/* Clear dynamic data for ECC private key. */
30169-
XMEMSET(dataASN, 0, sizeof(*dataASN) * eccKeyASN_Length);
30170-
/* Check named curve OID type. */
30171-
GetASN_OID(&dataASN[oidIdx], oidCurveType);
30172-
/* Try private key format .*/
30173-
ret = GetASN_Items(eccKeyASN, dataASN, eccKeyASN_Length, 1, input,
30174-
inOutIdx, inSz);
30175-
if (ret != 0) {
30176-
ret = ASN_PARSE_E;
30177-
}
30176+
ret = ASN_PARSE_E;
3017830177
}
3017930178
}
3018030179

@@ -30215,7 +30214,7 @@ int wc_EccPublicKeyDecode(const byte* input, word32* inOutIdx,
3021530214
}
3021630215
}
3021730216

30218-
FREE_ASNGETDATA(dataASN, key->heap);
30217+
FREE_ASNGETDATA(dataASN, key);
3021930218
return ret;
3022030219
}
3022130220
#endif /* WOLFSSL_ASN_TEMPLATE */

0 commit comments

Comments
 (0)