Skip to content

Commit

Permalink
Free allocated OSSL_PARAM_BLD on failures when iterating
Browse files Browse the repository at this point in the history
  • Loading branch information
anakinj committed Dec 28, 2022
1 parent 7f95cd8 commit 6d3fe30
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions ext/openssl/ossl_pkey.c
Original file line number Diff line number Diff line change
Expand Up @@ -441,24 +441,31 @@ add_ec_parameters_to_builder(VALUE key, VALUE value, VALUE arg) {

if(strcmp(OSSL_PKEY_PARAM_GROUP_NAME, key_ptr) == 0) {
StringValue(value);
if(!OSSL_PARAM_BLD_push_utf8_string(params_builder, key_ptr, RSTRING_PTR(value), RSTRING_LENINT(value)))
if(!OSSL_PARAM_BLD_push_utf8_string(params_builder, key_ptr, RSTRING_PTR(value), RSTRING_LENINT(value))) {
OSSL_PARAM_BLD_free(params_builder);
ossl_raise(ePKeyError, "OSSL_PARAM_BLD_push_utf8_string");
}
return ST_CONTINUE;
}

if(strcmp(OSSL_PKEY_PARAM_PUB_KEY, key_ptr) == 0) {
StringValue(value);
if(!OSSL_PARAM_BLD_push_octet_string(params_builder, OSSL_PKEY_PARAM_PUB_KEY, RSTRING_PTR(value), RSTRING_LENINT(value)))
if(!OSSL_PARAM_BLD_push_octet_string(params_builder, OSSL_PKEY_PARAM_PUB_KEY, RSTRING_PTR(value), RSTRING_LENINT(value))) {
OSSL_PARAM_BLD_free(params_builder);
ossl_raise(ePKeyError, "OSSL_PARAM_BLD_push_octet_string");
}
return ST_CONTINUE;
}

if(strcmp(OSSL_PKEY_PARAM_PRIV_KEY, key_ptr) == 0) {
if(!OSSL_PARAM_BLD_push_BN(params_builder, OSSL_PKEY_PARAM_PRIV_KEY, GetBNPtr(value)))
if(!OSSL_PARAM_BLD_push_BN(params_builder, OSSL_PKEY_PARAM_PRIV_KEY, GetBNPtr(value))) {
OSSL_PARAM_BLD_free(params_builder);
ossl_raise(ePKeyError, "OSSL_PARAM_BLD_push_BN");
}
return ST_CONTINUE;
}

OSSL_PARAM_BLD_free(params_builder);
ossl_raise(ePKeyError, "Unsupported parameter \"%s\"", key_ptr);
}

Expand Down Expand Up @@ -524,11 +531,15 @@ add_rsa_parameters_to_builder(VALUE key, VALUE value, VALUE arg) {
strcmp(OSSL_PKEY_PARAM_RSA_COEFFICIENT7, key_ptr) == 0 ||
strcmp(OSSL_PKEY_PARAM_RSA_COEFFICIENT8, key_ptr) == 0 ||
strcmp(OSSL_PKEY_PARAM_RSA_COEFFICIENT9, key_ptr) == 0) {
if(!OSSL_PARAM_BLD_push_BN(params_builder, key_ptr, GetBNPtr(value)))
if(!OSSL_PARAM_BLD_push_BN(params_builder, key_ptr, GetBNPtr(value))) {
OSSL_PARAM_BLD_free(params_builder);
ossl_raise(ePKeyError, "OSSL_PARAM_BLD_push_BN");
}

return ST_CONTINUE;
}

OSSL_PARAM_BLD_free(params_builder);
ossl_raise(ePKeyError, "Unsupported parameter \"%s\"", key_ptr);
}

Expand Down Expand Up @@ -571,18 +582,23 @@ add_ffc_parameters_to_builder(VALUE key, VALUE value, VALUE arg) {
strcmp(OSSL_PKEY_PARAM_FFC_VALIDATE_PQ, key_ptr) == 0 ||
strcmp(OSSL_PKEY_PARAM_FFC_VALIDATE_G, key_ptr) == 0 ||
strcmp(OSSL_PKEY_PARAM_FFC_VALIDATE_LEGACY, key_ptr) == 0) {
if(!OSSL_PARAM_BLD_push_BN(params_builder, key_ptr, GetBNPtr(value)))
if(!OSSL_PARAM_BLD_push_BN(params_builder, key_ptr, GetBNPtr(value))) {
OSSL_PARAM_BLD_free(params_builder);
ossl_raise(ePKeyError, "OSSL_PARAM_BLD_push_BN");
}
return ST_CONTINUE;
}

if(strcmp(OSSL_PKEY_PARAM_FFC_SEED, key_ptr) == 0) {
StringValue(value);
if(!OSSL_PARAM_BLD_push_octet_string(params_builder, key_ptr, RSTRING_PTR(value), RSTRING_LENINT(value)))
if(!OSSL_PARAM_BLD_push_octet_string(params_builder, key_ptr, RSTRING_PTR(value), RSTRING_LENINT(value))) {
OSSL_PARAM_BLD_free(params_builder);
ossl_raise(ePKeyError, "OSSL_PARAM_BLD_push_octet_string");
}
return ST_CONTINUE;
}

OSSL_PARAM_BLD_free(params_builder);
ossl_raise(ePKeyError, "Unsupported parameter \"%s\"", key_ptr);
}

Expand Down

0 comments on commit 6d3fe30

Please sign in to comment.