Skip to content

Commit

Permalink
Fix memory leak and remove unneeded casts
Browse files Browse the repository at this point in the history
  • Loading branch information
qpernil committed Feb 8, 2024
1 parent 0bc86bb commit eadfae3
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
2 changes: 1 addition & 1 deletion pkcs11/tests/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ CK_SESSION_HANDLE open_session(CK_FUNCTION_LIST_PTR p11) {
char config[256];
assert(strlen(connector_url) + strlen("connector=") < 256);
sprintf(config, "connector=%s", connector_url);
initArgs.pReserved = (void *) config;
initArgs.pReserved = config;
CK_RV rv = p11->C_Initialize(&initArgs);
assert(rv == CKR_OK);

Expand Down
4 changes: 2 additions & 2 deletions pkcs11/tests/ecdh_derive_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ static bool yh_derive(unsigned char *peerkey_bytes, int peerkey_len,
params.pPublicData = peerkey_bytes;
params.ulPublicDataLen = peerkey_len;

CK_MECHANISM mechanism = {CKM_ECDH1_DERIVE, (void *) &params, sizeof(params)};
CK_MECHANISM mechanism = {CKM_ECDH1_DERIVE, &params, sizeof(params)};

CK_OBJECT_CLASS key_class = CKO_SECRET_KEY;
CK_KEY_TYPE key_type = CKK_GENERIC_SECRET;
Expand Down Expand Up @@ -208,7 +208,7 @@ static size_t openssl_derive(EVP_PKEY *private_key, EVP_PKEY *peer_key,
}

/* Create the buffer */
*ecdh_key = OPENSSL_malloc(len);
*ecdh_key = malloc(len);
if (*ecdh_key == NULL) {
fail("Failed to allocate the buffer to hold the ECDH key derived with "
"openssl");
Expand Down
10 changes: 5 additions & 5 deletions pkcs11/tests/ecdh_sp800_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ static bool yh_derive(unsigned char *peerkey_bytes, int peerkey_len,
params.pPublicData = peerkey_bytes;
params.ulPublicDataLen = peerkey_len;

CK_MECHANISM mechanism = {CKM_ECDH1_DERIVE, (void *) &params, sizeof(params)};
CK_MECHANISM mechanism = {CKM_ECDH1_DERIVE, &params, sizeof(params)};

CK_OBJECT_CLASS key_class = CKO_SECRET_KEY;
CK_KEY_TYPE key_type = CKK_AES;
Expand Down Expand Up @@ -200,6 +200,7 @@ static size_t openssl_derive(CK_ULONG kdf, EVP_PKEY *private_key,
}

EVP_MD_CTX *mdctx = NULL;
unsigned char *derived = NULL;
size_t len = 0;
/* Initialize derivation function*/
if (EVP_PKEY_derive_init(ctx) != 1) {
Expand All @@ -220,7 +221,7 @@ static size_t openssl_derive(CK_ULONG kdf, EVP_PKEY *private_key,
}

/* Create the buffer */
unsigned char *derived = OPENSSL_malloc(len);
derived = malloc(len);
if (derived == NULL) {
fail("Failed to allocate the buffer to hold the ECDH key derived with "
"openssl");
Expand Down Expand Up @@ -298,10 +299,9 @@ static size_t openssl_derive(CK_ULONG kdf, EVP_PKEY *private_key,
}

c_free:
free(derived);
EVP_PKEY_CTX_free(ctx);
if (mdctx != NULL) {
EVP_MD_CTX_destroy(mdctx);
}
EVP_MD_CTX_destroy(mdctx);
EVP_PKEY_free(peer_key);
EVP_PKEY_free(private_key);

Expand Down

0 comments on commit eadfae3

Please sign in to comment.