ecdh & ecdsa: avoid some possible memory leaks in error conditions.
This commit is contained in:
parent
8792b9a9f3
commit
f8e15dfe8d
2 changed files with 17 additions and 1 deletions
|
@ -30,19 +30,30 @@
|
||||||
|
|
||||||
bool ecdh_generate_public(ecdh_t *ecdh, void *pubkey) {
|
bool ecdh_generate_public(ecdh_t *ecdh, void *pubkey) {
|
||||||
*ecdh = EC_KEY_new_by_curve_name(NID_secp521r1);
|
*ecdh = EC_KEY_new_by_curve_name(NID_secp521r1);
|
||||||
|
if(!*ecdh) {
|
||||||
|
logger(DEBUG_ALWAYS, LOG_ERR, "Generating EC key_by_curve_name failed: %s", ERR_error_string(ERR_get_error(), NULL));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
if(!EC_KEY_generate_key(*ecdh)) {
|
if(!EC_KEY_generate_key(*ecdh)) {
|
||||||
|
EC_KEY_free(*ecdh);
|
||||||
|
*ecdh = NULL;
|
||||||
logger(DEBUG_ALWAYS, LOG_ERR, "Generating EC key failed: %s", ERR_error_string(ERR_get_error(), NULL));
|
logger(DEBUG_ALWAYS, LOG_ERR, "Generating EC key failed: %s", ERR_error_string(ERR_get_error(), NULL));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
const EC_POINT *point = EC_KEY_get0_public_key(*ecdh);
|
const EC_POINT *point = EC_KEY_get0_public_key(*ecdh);
|
||||||
if(!point) {
|
if(!point) {
|
||||||
|
EC_KEY_free(*ecdh);
|
||||||
|
*ecdh = NULL;
|
||||||
logger(DEBUG_ALWAYS, LOG_ERR, "Getting public key failed: %s", ERR_error_string(ERR_get_error(), NULL));
|
logger(DEBUG_ALWAYS, LOG_ERR, "Getting public key failed: %s", ERR_error_string(ERR_get_error(), NULL));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t result = EC_POINT_point2oct(EC_KEY_get0_group(*ecdh), point, POINT_CONVERSION_COMPRESSED, pubkey, ECDH_SIZE, NULL);
|
size_t result = EC_POINT_point2oct(EC_KEY_get0_group(*ecdh), point, POINT_CONVERSION_COMPRESSED, pubkey, ECDH_SIZE, NULL);
|
||||||
if(!result) {
|
if(!result) {
|
||||||
|
EC_KEY_free(*ecdh);
|
||||||
|
*ecdh = NULL;
|
||||||
logger(DEBUG_ALWAYS, LOG_ERR, "Converting EC_POINT to binary failed: %s", ERR_error_string(ERR_get_error(), NULL));
|
logger(DEBUG_ALWAYS, LOG_ERR, "Converting EC_POINT to binary failed: %s", ERR_error_string(ERR_get_error(), NULL));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -59,6 +70,7 @@ bool ecdh_compute_shared(ecdh_t *ecdh, const void *pubkey, void *shared) {
|
||||||
|
|
||||||
int result = EC_POINT_oct2point(EC_KEY_get0_group(*ecdh), point, pubkey, ECDH_SIZE, NULL);
|
int result = EC_POINT_oct2point(EC_KEY_get0_group(*ecdh), point, pubkey, ECDH_SIZE, NULL);
|
||||||
if(!result) {
|
if(!result) {
|
||||||
|
EC_POINT_free(point);
|
||||||
logger(DEBUG_ALWAYS, LOG_ERR, "Converting binary to EC_POINT failed: %s", ERR_error_string(ERR_get_error(), NULL));
|
logger(DEBUG_ALWAYS, LOG_ERR, "Converting binary to EC_POINT failed: %s", ERR_error_string(ERR_get_error(), NULL));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,7 +30,11 @@
|
||||||
//
|
//
|
||||||
bool ecdsa_set_base64_public_key(ecdsa_t *ecdsa, const char *p) {
|
bool ecdsa_set_base64_public_key(ecdsa_t *ecdsa, const char *p) {
|
||||||
*ecdsa = EC_KEY_new_by_curve_name(NID_secp521r1);
|
*ecdsa = EC_KEY_new_by_curve_name(NID_secp521r1);
|
||||||
|
if(!*ecdsa) {
|
||||||
|
logger(DEBUG_ALWAYS, LOG_DEBUG, "EC_KEY_new_by_curve_name failed: %s", ERR_error_string(ERR_get_error(), NULL));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
int len = strlen(p);
|
int len = strlen(p);
|
||||||
unsigned char pubkey[len / 4 * 3 + 3];
|
unsigned char pubkey[len / 4 * 3 + 3];
|
||||||
const unsigned char *ppubkey = pubkey;
|
const unsigned char *ppubkey = pubkey;
|
||||||
|
|
Loading…
Add table
Reference in a new issue