Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Appropriately free temporary values on EC import #73078

Merged
merged 1 commit into from
Jul 29, 2022
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -327,10 +327,11 @@ int32_t CryptoNative_EcKeyCreateByKeyParameters(EC_KEY** key, const char* oid, u
if (!nid)
return -1;

*key = EC_KEY_new_by_curve_name(nid);
if (!(*key))
EC_KEY* tmpKey = EC_KEY_new_by_curve_name(nid);
if (tmpKey == NULL)
return -1;

int ret = 0;
BIGNUM* dBn = NULL;
BIGNUM* qxBn = NULL;
BIGNUM* qyBn = NULL;
Expand All @@ -344,7 +345,7 @@ int32_t CryptoNative_EcKeyCreateByKeyParameters(EC_KEY** key, const char* oid, u
if (!qxBn || !qyBn)
goto error;

if (!EC_KEY_set_public_key_affine_coordinates(*key, qxBn, qyBn))
if (!EC_KEY_set_public_key_affine_coordinates(tmpKey, qxBn, qyBn))
goto error;

// Set private key (optional)
Expand All @@ -354,12 +355,12 @@ int32_t CryptoNative_EcKeyCreateByKeyParameters(EC_KEY** key, const char* oid, u
if (!dBn)
goto error;

if (!EC_KEY_set_private_key(*key, dBn))
if (!EC_KEY_set_private_key(tmpKey, dBn))
goto error;
}

// Validate key
if (!EC_KEY_check_key(*key))
if (!EC_KEY_check_key(tmpKey))
goto error;
}

Expand All @@ -373,10 +374,10 @@ int32_t CryptoNative_EcKeyCreateByKeyParameters(EC_KEY** key, const char* oid, u
if (!dBn)
goto error;

if (!EC_KEY_set_private_key(*key, dBn))
if (!EC_KEY_set_private_key(tmpKey, dBn))
goto error;

const EC_GROUP* group = EC_KEY_get0_group(*key);
const EC_GROUP* group = EC_KEY_get0_group(tmpKey);

if (!group)
goto error;
Expand All @@ -389,27 +390,26 @@ int32_t CryptoNative_EcKeyCreateByKeyParameters(EC_KEY** key, const char* oid, u
if (!EC_POINT_mul(group, pubG, dBn, NULL, NULL, NULL))
goto error;

if (!EC_KEY_set_public_key(*key, pubG))
if (!EC_KEY_set_public_key(tmpKey, pubG))
goto error;

if (!EC_KEY_check_key(*key))
if (!EC_KEY_check_key(tmpKey))
goto error;
}

// Success
return 1;
*key = tmpKey;
tmpKey = NULL;
ret = 1;

error:
if (qxBn) BN_free(qxBn);
if (qyBn) BN_free(qyBn);
if (dBn) BN_clear_free(dBn);
if (pubG) EC_POINT_free(pubG);
if (*key)
{
EC_KEY_free(*key);
*key = NULL;
}
return 0;
if (tmpKey) EC_KEY_free(tmpKey);

return ret;
}

EC_KEY* CryptoNative_EcKeyCreateByExplicitParameters(
Expand All @@ -436,6 +436,7 @@ EC_KEY* CryptoNative_EcKeyCreateByExplicitParameters(
ERR_clear_error();

EC_KEY* key = NULL;
EC_KEY* ret = NULL;
EC_POINT* G = NULL;
EC_POINT* pubG = NULL;

Expand Down Expand Up @@ -570,7 +571,8 @@ EC_KEY* CryptoNative_EcKeyCreateByExplicitParameters(
}

// Success
return key;
ret = key;
key = NULL;

error:
if (qxBn) BN_free(qxBn);
Expand All @@ -587,5 +589,6 @@ EC_KEY* CryptoNative_EcKeyCreateByExplicitParameters(
if (pubG) EC_POINT_free(pubG);
if (group) EC_GROUP_free(group);
if (key) EC_KEY_free(key);
return NULL;

return ret;
}