Merge pull request #2689 from tmael/pkey_freeMutex

Free EVP ctx pkey
This commit is contained in:
JacobBarthelmeh
2020-01-06 23:15:00 +07:00
committed by GitHub
3 changed files with 24 additions and 3 deletions

View File

@@ -23549,7 +23549,9 @@ void wolfSSL_EVP_PKEY_free(WOLFSSL_EVP_PKEY* key)
break;
}
wc_FreeMutex(&key->refMutex);
if (wc_FreeMutex(&key->refMutex) != 0) {
WOLFSSL_MSG("Couldn't free pkey mutex");
}
XFREE(key, key->heap, DYNAMIC_TYPE_PUBLIC_KEY);
}
}

View File

@@ -26276,9 +26276,16 @@ static void test_wolfSSL_EVP_PKEY_encrypt(void)
AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
WOLFSSL_SUCCESS);
/* Test pkey references count is decremented. pkey shouldn't be destroyed
since ctx uses it.*/
AssertIntEQ(pkey->references, 2);
EVP_PKEY_free(pkey);
AssertIntEQ(pkey->references, 1);
/* Encrypt data */
AssertIntEQ(EVP_PKEY_encrypt(ctx, outEnc, &outEncLen,
(const unsigned char*)in, inlen), WOLFSSL_SUCCESS);
/* Decrypt data */
AssertIntEQ(EVP_PKEY_decrypt_init(ctx), WOLFSSL_SUCCESS);
@@ -26305,8 +26312,6 @@ static void test_wolfSSL_EVP_PKEY_encrypt(void)
WOLFSSL_SUCCESS);
AssertIntEQ(XMEMCMP(inTmp, outDecTmp, outDecLen), 0);
#endif
EVP_PKEY_free(pkey);
EVP_PKEY_CTX_free(ctx);
XFREE(outEnc, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(outDec, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);

View File

@@ -901,6 +901,8 @@ int wolfSSL_EVP_PKEY_CTX_free(WOLFSSL_EVP_PKEY_CTX *ctx)
{
if (ctx == NULL) return 0;
WOLFSSL_ENTER("EVP_PKEY_CTX_free");
if (ctx->pkey != NULL)
wolfSSL_EVP_PKEY_free(ctx->pkey);
XFREE(ctx, NULL, DYNAMIC_TYPE_PUBLIC_KEY);
return WOLFSSL_SUCCESS;
}
@@ -916,6 +918,7 @@ int wolfSSL_EVP_PKEY_CTX_free(WOLFSSL_EVP_PKEY_CTX *ctx)
WOLFSSL_EVP_PKEY_CTX *wolfSSL_EVP_PKEY_CTX_new(WOLFSSL_EVP_PKEY *pkey, WOLFSSL_ENGINE *e)
{
WOLFSSL_EVP_PKEY_CTX* ctx;
int type = NID_undef;
if (pkey == NULL) return 0;
if (e != NULL) return 0;
@@ -929,7 +932,18 @@ WOLFSSL_EVP_PKEY_CTX *wolfSSL_EVP_PKEY_CTX_new(WOLFSSL_EVP_PKEY *pkey, WOLFSSL_E
#if !defined(NO_RSA) && !defined(HAVE_USER_RSA)
ctx->padding = RSA_PKCS1_PADDING;
#endif
type = wolfSSL_EVP_PKEY_type(pkey->type);
if ((type == EVP_PKEY_RSA) ||
(type == EVP_PKEY_DSA) ||
(type == EVP_PKEY_EC)) {
if (wc_LockMutex(&pkey->refMutex) != 0) {
WOLFSSL_MSG("Couldn't lock pkey mutex");
}
pkey->references++;
wc_UnLockMutex(&pkey->refMutex);
}
return ctx;
}