Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion doc/dox_comments/header_files/asn_public.h
Original file line number Diff line number Diff line change
Expand Up @@ -2154,7 +2154,11 @@ int wc_GetPubKeyDerFromCert(struct DecodedCert* cert,
\code
DsaKey key;
word32 idx = 0;
int ret = wc_DsaParamsDecode(derBuf, &idx, &key, derSz);
int ret = wc_InitDsaKey(&key);
if (ret == 0) {
ret = wc_DsaParamsDecode(derBuf, &idx, &key, derSz);
wc_FreeDsaKey(&key);
}
\endcode

\sa wc_DsaKeyToParamsDer
Expand Down
12 changes: 10 additions & 2 deletions doc/dox_comments/header_files/dh.h
Original file line number Diff line number Diff line change
Expand Up @@ -938,8 +938,16 @@ int wc_DhGeneratePublic(DhKey* key, byte* priv, word32 privSz,
\code
DhKey key;
byte priv[256], pub[256];
int ret = wc_DhImportKeyPair(&key, priv, sizeof(priv), pub,
sizeof(pub));
int ret2;
int ret = wc_InitDhKey(&key);
if (ret == 0) {
ret = wc_DhImportKeyPair(&key, priv, sizeof(priv), pub,
sizeof(pub));
ret2 = wc_FreeDhKey(&key);
if (ret == 0) {
ret = ret2;
}
}
\endcode

\sa wc_DhExportKeyPair
Expand Down
15 changes: 15 additions & 0 deletions tests/api/test_dh.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ int test_wc_DhPublicKeyDecode(void)
ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,&key, 0),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
inOutIdx = 0;
ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,&key,
sizeof_dh_pub_key_der_2048), 0);
/* Decode again into the same key; the first values must be released,
* not orphaned. */
inOutIdx = 0;
ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,&key,
sizeof_dh_pub_key_der_2048), 0);
ExpectIntNE(key.p.used, 0);
Expand Down Expand Up @@ -274,6 +279,10 @@ int test_wc_DhSetKey(void)
if (params != NULL) {
/* valid: known FFDHE prime, short-circuits the primality test */
ExpectIntEQ(wc_InitDhKey(&key), 0);
ExpectIntEQ(wc_DhSetKey(&key, params->p, params->p_len, params->g,
params->g_len), 0);
/* Set again on the same key; the first values must be released,
* not orphaned. */
ExpectIntEQ(wc_DhSetKey(&key, params->p, params->p_len, params->g,
params->g_len), 0);
wc_FreeDhKey(&key);
Expand Down Expand Up @@ -762,6 +771,9 @@ int test_wc_DhImportExportKeyPair(void)
/* both */
ExpectIntEQ(wc_InitDhKey(&key), 0);
ExpectIntEQ(wc_DhImportKeyPair(&key, priv, privSz, pub, pubSz), 0);
/* Import again into the same key; the first values must be released,
* not orphaned. */
ExpectIntEQ(wc_DhImportKeyPair(&key, priv, privSz, pub, pubSz), 0);

/* export bad args */
privOutSz = sizeof(privOut);
Expand Down Expand Up @@ -1033,6 +1045,9 @@ int test_wc_DhGenerateParams_and_ExportRaw(void)
WC_NO_ERR_TRACE(BAD_FUNC_ARG));

ExpectIntEQ(wc_DhGenerateParams(&rng, 1024, &dh), 0);
/* Generate again into the same key; the first values must be released,
* not orphaned. */
ExpectIntEQ(wc_DhGenerateParams(&rng, 1024, &dh), 0);

/* bad args */
pSz = sizeof(pOut); qSz = sizeof(qOut); gSz = sizeof(gOut);
Expand Down
14 changes: 14 additions & 0 deletions tests/api/test_dsa.c
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@ int test_wc_DsaPublicPrivateKeyDecode(void)

ExpectIntEQ(wc_InitDsaKey(&key), 0);
ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes), 0);
/* Decode again into the same key; the first values must be released,
* not orphaned. */
idx = 0;
ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes), 0);
/* Test bad args. */
ExpectIntEQ(wc_DsaPrivateKeyDecode(NULL, &idx, &key, bytes), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, NULL, &key, bytes), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
Expand All @@ -190,6 +194,10 @@ int test_wc_DsaPublicPrivateKeyDecode(void)
ExpectIntEQ(wc_InitDsaKey(&key), 0);
idx = 0; /* Reset */
ExpectIntEQ(wc_DsaPublicKeyDecode(tmp, &idx, &key, bytes), 0);
/* Decode again into the same key; the first values must be released,
* not orphaned. */
idx = 0;
ExpectIntEQ(wc_DsaPublicKeyDecode(tmp, &idx, &key, bytes), 0);
/* Test bad args. */
ExpectIntEQ(wc_DsaPublicKeyDecode(NULL, &idx, &key, bytes), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_DsaPublicKeyDecode(tmp, NULL, &key, bytes), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
Expand Down Expand Up @@ -218,6 +226,9 @@ int test_wc_MakeDsaKey(void)
ExpectIntEQ(wc_InitDsaKey(&genKey), 0);
ExpectIntEQ(wc_InitRng(&rng), 0);

ExpectIntEQ(wc_MakeDsaParameters(&rng, ONEK_BUF, &genKey), 0);
/* Generate again into the same key; the first values must be released,
* not orphaned. */
ExpectIntEQ(wc_MakeDsaParameters(&rng, ONEK_BUF, &genKey), 0);
/* Test bad args. */
ExpectIntEQ(wc_MakeDsaParameters(NULL, ONEK_BUF, &genKey), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
Expand All @@ -226,6 +237,9 @@ int test_wc_MakeDsaKey(void)
WC_NO_ERR_TRACE(BAD_FUNC_ARG));

ExpectIntEQ(wc_MakeDsaKey(&rng, &genKey), 0);
/* Generate again into the same key; the first key must be wiped and
* released, not orphaned. */
ExpectIntEQ(wc_MakeDsaKey(&rng, &genKey), 0);
/* Test bad args. */
ExpectIntEQ(wc_MakeDsaKey(NULL, &genKey), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_MakeDsaKey(&rng, NULL), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
Expand Down
18 changes: 18 additions & 0 deletions tests/api/test_ecc.c
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,14 @@ int test_wc_ecc_make_key(void)
#endif
ExpectIntEQ(ret, 0);

/* Generate again into the same key; the first private scalar and public
* point must be released, not orphaned. */
ret = wc_ecc_make_key(&rng, KEY14, &key);
#if defined(WOLFSSL_ASYNC_CRYPT)
ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
#endif
ExpectIntEQ(ret, 0);

/* Pass in bad args. */
ExpectIntEQ(wc_ecc_make_key(NULL, KEY14, &key),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
Expand Down Expand Up @@ -1504,6 +1512,9 @@ int test_wc_ecc_import_x963(void)
PRIVATE_KEY_LOCK();

ExpectIntEQ(wc_ecc_import_x963(x963, x963Len, &pubKey), 0);
/* Import again into the same key; the first values must be released,
* not orphaned. */
ExpectIntEQ(wc_ecc_import_x963(x963, x963Len, &pubKey), 0);

/* Test bad args. */
ExpectIntEQ(wc_ecc_import_x963(NULL, x963Len, &pubKey),
Expand Down Expand Up @@ -2392,6 +2403,9 @@ int test_wc_ecc_import_raw(void)

/* Test good import */
ExpectIntEQ(wc_ecc_import_raw(&key, qx, qy, d, curveName), 0);
/* Import again into the same key; the first values must be released,
* not orphaned. */
ExpectIntEQ(wc_ecc_import_raw(&key, qx, qy, d, curveName), 0);

/* Test bad args. */
ExpectIntEQ(wc_ecc_import_raw(NULL, qx, qy, d, curveName),
Expand Down Expand Up @@ -2479,6 +2493,10 @@ int test_wc_ecc_import_unsigned(void)

ExpectIntEQ(wc_ecc_init(&key), 0);

ExpectIntEQ(wc_ecc_import_unsigned(&key, (byte*)qx, (byte*)qy, (byte*)d,
curveId), 0);
/* Import again into the same key; the first values must be released,
* not orphaned. */
ExpectIntEQ(wc_ecc_import_unsigned(&key, (byte*)qx, (byte*)qy, (byte*)d,
curveId), 0);
/* Test bad args. */
Expand Down
11 changes: 11 additions & 0 deletions tests/api/test_rsa.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ int test_wc_RsaPrivateKeyDecode(void)
#endif /* Use cert buffers. */
}

ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, &idx, &key, (word32)bytes), 0);
/* Decode again into the same key; the first values must be released,
* not orphaned. */
idx = 0;
ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, &idx, &key, (word32)bytes), 0);

/* Test bad args. */
Expand Down Expand Up @@ -181,6 +185,10 @@ int test_wc_RsaPublicKeyDecode(void)
#endif
}

ExpectIntEQ(wc_RsaPublicKeyDecode(tmp, &idx, &keyPub, (word32)bytes), 0);
/* Decode again into the same key; the first values must be released,
* not orphaned. */
idx = 0;
ExpectIntEQ(wc_RsaPublicKeyDecode(tmp, &idx, &keyPub, (word32)bytes), 0);

/* Pass in bad args. */
Expand Down Expand Up @@ -396,6 +404,9 @@ int test_wc_MakeRsaKey(void)
ExpectIntEQ(wc_InitRsaKey(&genKey, HEAP_HINT), 0);
ExpectIntEQ(wc_InitRng(&rng), 0);
ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, &rng), 0);
/* Generate again into the same key; the first key must be wiped and
* released, not orphaned. */
ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, &rng), 0);
DoExpectIntEQ(wc_FreeRsaKey(&genKey), 0);

/* Test bad args. */
Expand Down
40 changes: 37 additions & 3 deletions wolfcrypt/src/asn.c
Original file line number Diff line number Diff line change
Expand Up @@ -9284,6 +9284,8 @@ static int _RsaPrivateKeyDecode(const byte* input, word32* inOutIdx,
int i;
/* Extract all public fields. */
for (i = 0; i < RSA_ASN_INTS; i++) {
/* Wipe and free an earlier value; GetASN_MP() inits again. */
mp_forcezero(GetRsaInt(key, i));
GetASN_MP(&dataASN[(byte)RSAKEYASN_IDX_N + i],
GetRsaInt(key, i));
}
Expand Down Expand Up @@ -12432,6 +12434,11 @@ int wc_DhPublicKeyDecode(const byte* input, word32* inOutIdx,
if (GetSequence(input, inOutIdx, &length, inSz) < 0)
return ASN_PARSE_E;

/* Free p, g and pub from wc_InitDhKey() or an earlier load; GetInt()
* inits them again. */
mp_clear(&key->p);
mp_clear(&key->g);
mp_clear(&key->pub);
if (GetInt(&key->p, input, inOutIdx, inSz) < 0)
return ASN_DH_KEY_E;

Expand Down Expand Up @@ -12582,6 +12589,10 @@ int wc_DhKeyDecode(const byte* input, word32* inOutIdx, DhKey* key, word32 inSz)
if (ret == 0) {
/* Initialize data and set mp_ints to hold p and g. */
XMEMSET(dataASN, 0, sizeof(*dataASN) * dhParamASN_Length);
/* Free p and g from wc_InitDhKey() or an earlier load; GetASN_MP()
* inits them again. */
mp_clear(&key->p);
mp_clear(&key->g);
GetASN_MP(&dataASN[DHPARAMASN_IDX_PRIME], &key->p);
GetASN_MP(&dataASN[DHPARAMASN_IDX_BASE], &key->g);
/* Try simple PKCS #3 template. */
Expand All @@ -12596,6 +12607,10 @@ int wc_DhKeyDecode(const byte* input, word32* inOutIdx, DhKey* key, word32 inSz)
XMEMSET(dataASN, 0, sizeof(*dataASN) * dhKeyPkcs8ASN_Length);
GetASN_ExpBuffer(&dataASN[DHKEYPKCS8ASN_IDX_PKEYALGO_OID],
keyDhOid, sizeof(keyDhOid));
/* Wipe and free q, priv and pub from an earlier load. */
mp_clear(&key->q);
mp_forcezero(&key->priv);
mp_clear(&key->pub);
GetASN_MP(&dataASN[DHKEYPKCS8ASN_IDX_PKEYALGO_PARAM_P], &key->p);
GetASN_MP(&dataASN[DHKEYPKCS8ASN_IDX_PKEYALGO_PARAM_G], &key->g);
GetASN_MP(&dataASN[DHKEYPKCS8ASN_IDX_PKEYALGO_PARAM_Q], &key->q);
Expand Down Expand Up @@ -12928,8 +12943,11 @@ int wc_DsaPublicKeyDecode(const byte* input, word32* inOutIdx, DsaKey* key,
/* seq
* p, q, g, y
* Start DSA ints from DSAKEYASN_IDX_VER instead of DSAKEYASN_IDX_P */
for (i = 0; i < DSA_INTS - 1; i++)
for (i = 0; i < DSA_INTS - 1; i++) {
/* Free an earlier value; GetASN_MP() inits it again. */
mp_clear(GetDsaInt(key, i));
GetASN_MP(&dataASN[(int)DSAKEYASN_IDX_VER + i], GetDsaInt(key, i));
}
/* Parse as simple form. */
ret = GetASN_Items(dsaKeyASN, dataASN, dsaPublicKeyASN_Length, 0, input,
inOutIdx, inSz);
Expand All @@ -12939,11 +12957,14 @@ int wc_DsaPublicKeyDecode(const byte* input, word32* inOutIdx, DsaKey* key,
/* Set DSA OID to expect. */
GetASN_ExpBuffer(&dataASN[DSAPUBKEYASN_IDX_ALGOID_OID],
keyDsaOid, sizeof(keyDsaOid));
/* p, q, g */
for (i = 0; i < DSA_INTS - 2; i++)
/* p, q, g; the simple form above may have filled them. */
for (i = 0; i < DSA_INTS - 2; i++) {
mp_clear(GetDsaInt(key, i));
GetASN_MP(&dataASN[(int)DSAPUBKEYASN_IDX_ALGOID_PARAMS_P + i],
GetDsaInt(key, i));
}
/* y */
mp_clear(GetDsaInt(key, i));
GetASN_MP(&dataASN[DSAPUBKEYASN_IDX_PUBKEY_Y], GetDsaInt(key, i));
/* Parse as SubjectPublicKeyInfo. */
ret = GetASN_Items(dsaPubKeyASN, dataASN, dsaPubKeyASN_Length, 1,
Expand Down Expand Up @@ -12973,6 +12994,11 @@ int wc_DsaParamsDecode(const byte* input, word32* inOutIdx, DsaKey* key,
return ASN_PARSE_E;

maxIdx = (word32)(*inOutIdx + (word32)length);
/* Free p, q and g from wc_InitDsaKey() or earlier parameters; GetInt()
* inits them again. */
mp_clear(&key->p);
mp_clear(&key->q);
mp_clear(&key->g);
if (GetInt(&key->p, input, inOutIdx, maxIdx) < 0 ||
GetInt(&key->q, input, inOutIdx, maxIdx) < 0 ||
GetInt(&key->g, input, inOutIdx, maxIdx) < 0)
Expand Down Expand Up @@ -13044,6 +13070,11 @@ int wc_DsaPrivateKeyDecode(const byte* input, word32* inOutIdx, DsaKey* key,
if (ret == 0) {
int i;

/* Wipe and free values from wc_InitDsaKey() or an earlier decode;
* GetASN_MP() inits them again. */
for (i = 0; i < DSA_INTS; i++) {
mp_forcezero(GetDsaInt(key, i));
}
/* Try dsaKeyOctASN */
/* Initialize key data and set mp_ints for params */
for (i = 0; i < DSA_INTS - 2; i++) {
Expand Down Expand Up @@ -42206,6 +42237,9 @@ static int _RsaPublicKeyDecodeRaw(const byte* n, word32 nSz, const byte* e,

key->type = RSA_PUBLIC;

/* Free n and e from wc_InitRsaKey() or an earlier decode before re-init. */
mp_clear(&key->n);
mp_clear(&key->e);
if (mp_init(&key->n) != MP_OKAY) {
return MP_INIT_E;
}
Expand Down
30 changes: 30 additions & 0 deletions wolfcrypt/src/asn_orig.c
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,8 @@ static int _RsaPrivateKeyDecode(const byte* input, word32* inOutIdx,

/* Extract all public fields. */
for (i = 0; i < RSA_INT_CNT; i++) {
/* Wipe and free an earlier value; GetInt() inits it again. */
mp_forcezero(GetRsaInt(key, i));
if (GetInt(GetRsaInt(key, i), input, inOutIdx, inSz) < 0) {
for (i--; i >= 0; i--) {
mp_clear(GetRsaInt(key, i));
Expand Down Expand Up @@ -929,6 +931,10 @@ int wc_DhKeyDecode(const byte* input, word32* inOutIdx, DhKey* key, word32 inSz)
temp = *inOutIdx;
#endif
#endif
/* Free p and g from wc_InitDhKey() or an earlier load; GetInt() inits
* them again. */
mp_clear(&key->p);
mp_clear(&key->g);
/* Assume input started after 1.2.840.113549.1.3.1 dhKeyAgreement */
if (GetInt(&key->p, input, inOutIdx, inSz) < 0) {
ret = ASN_DH_KEY_E;
Expand Down Expand Up @@ -975,6 +981,7 @@ int wc_DhKeyDecode(const byte* input, word32* inOutIdx, DhKey* key, word32 inSz)
ret = (CheckBitString(input, inOutIdx, &length, inSz, 0, NULL) == 0);
if (ret > 0) {
/* Found Bit String */
mp_clear(&key->pub);
if (GetInt(&key->pub, input, inOutIdx, inSz) == 0) {
WOLFSSL_MSG("Found Public Key");
ret = 0;
Expand All @@ -984,6 +991,7 @@ int wc_DhKeyDecode(const byte* input, word32* inOutIdx, DhKey* key, word32 inSz)
ret = (GetOctetString(input, inOutIdx, &length, inSz) >= 0);
if (ret > 0) {
/* Found Octet String */
mp_forcezero(&key->priv);
if (GetInt(&key->priv, input, inOutIdx, inSz) == 0) {
WOLFSSL_MSG("Found Private Key");

Expand Down Expand Up @@ -1192,6 +1200,18 @@ int wc_DhParamsLoad(const byte* input, word32 inSz, byte* p, word32* pInOutSz,

#endif
#ifndef NO_DSA
/* Free p, q, g and y, filled by wc_InitDsaKey(), an earlier decode or a
* partly failed parse, before GetInt() inits them again.
*/
static void DsaFreePublicInts(DsaKey* key)
{
int i;

for (i = 0; i < DSA_INTS - 1; i++) {
mp_clear(GetDsaInt(key, i));
}
}

int wc_DsaPublicKeyDecode(const byte* input, word32* inOutIdx, DsaKey* key,
word32 inSz)
{
Expand All @@ -1206,6 +1226,7 @@ int wc_DsaPublicKeyDecode(const byte* input, word32* inOutIdx, DsaKey* key,
if (GetSequence(input, inOutIdx, &length, inSz) < 0)
return ASN_PARSE_E;

DsaFreePublicInts(key);
maxIdx = (word32)(*inOutIdx + (word32)length);
if (GetInt(&key->p, input, inOutIdx, maxIdx) < 0 ||
GetInt(&key->q, input, inOutIdx, maxIdx) < 0 ||
Comment thread
kaleb-himes marked this conversation as resolved.
Resolved
Expand All @@ -1224,6 +1245,8 @@ int wc_DsaPublicKeyDecode(const byte* input, word32* inOutIdx, DsaKey* key,
if (GetSequence(input, inOutIdx, &length, inSz) < 0)
return ASN_PARSE_E;

/* The simple form above may have filled some of them. */
DsaFreePublicInts(key);
if (GetInt(&key->p, input, inOutIdx, inSz) < 0 ||
GetInt(&key->q, input, inOutIdx, inSz) < 0 ||
GetInt(&key->g, input, inOutIdx, inSz) < 0)
Expand All @@ -1246,13 +1269,20 @@ int wc_DsaPrivateKeyDecode(const byte* input, word32* inOutIdx, DsaKey* key,
word32 inSz)
{
int length, version, ret = 0, temp = 0;
int i;
word32 algId = 0;

/* Sanity checks on input */
if (input == NULL || inOutIdx == NULL || key == NULL) {
return BAD_FUNC_ARG;
}

/* Wipe and free values from wc_InitDsaKey() or an earlier decode;
* GetInt() inits them again. */
for (i = 0; i < DSA_INTS; i++) {
mp_forcezero(GetDsaInt(key, i));
}

/* if has pkcs8 header skip it */
if (ToTraditionalInline_ex(input, inOutIdx, inSz, &algId) < 0) {
/* ignore error, did not have pkcs8 header */
Expand Down
Loading
Loading