Fix to only free existing key in SetStaticEphemeralKey if the incoming algorithm type has been specified.

This commit is contained in:
David Garske
2020-11-10 13:08:04 -08:00
parent 5cda549d00
commit ea21ddf540

View File

@@ -48888,13 +48888,17 @@ static int SetStaticEphemeralKey(StaticKeyExchangeInfo_t* staticKE, int keyAlgo,
return BAD_FUNC_ARG; return BAD_FUNC_ARG;
} }
WOLFSSL_ENTER("SetStaticEphemeralKey");
/* if key is already set free it */ /* if key is already set free it */
#ifndef NO_DH #ifndef NO_DH
if (staticKE->dhKey && (ctx == NULL || staticKE->dhKey != ctx->staticKE.dhKey)) if (keyAlgo == WC_PK_TYPE_DH && staticKE->dhKey &&
(ctx == NULL || staticKE->dhKey != ctx->staticKE.dhKey))
FreeDer(&staticKE->dhKey); FreeDer(&staticKE->dhKey);
#endif #endif
#ifdef HAVE_ECC #ifdef HAVE_ECC
if (staticKE->ecKey && (ctx == NULL || staticKE->ecKey != ctx->staticKE.ecKey)) if (keyAlgo == WC_PK_TYPE_ECDH && staticKE->ecKey &&
(ctx == NULL || staticKE->ecKey != ctx->staticKE.ecKey))
FreeDer(&staticKE->ecKey); FreeDer(&staticKE->ecKey);
#endif #endif
@@ -48967,6 +48971,9 @@ static int SetStaticEphemeralKey(StaticKeyExchangeInfo_t* staticKE, int keyAlgo,
XFREE(keyBuf, heap, DYNAMIC_TYPE_TMP_BUFFER); XFREE(keyBuf, heap, DYNAMIC_TYPE_TMP_BUFFER);
} }
#endif #endif
WOLFSSL_LEAVE("SetStaticEphemeralKey", ret);
return ret; return ret;
} }