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

Avoid double-free errors in RSA key generation #525

Merged
merged 3 commits into from
Sep 5, 2019
Merged
Show file tree
Hide file tree
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
11 changes: 7 additions & 4 deletions src/soter/boringssl/soter_rsa_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,15 @@ soter_status_t soter_rsa_gen_key(EVP_PKEY_CTX* pkey_ctx, const unsigned key_leng
return SOTER_FAIL;
}

SOTER_IF_FAIL(1 <= EVP_PKEY_CTX_set_rsa_keygen_pubexp(pkey_ctx, pub_exp),
(BN_free(pub_exp), EVP_PKEY_CTX_free(pkey_ctx)));
if (1 > EVP_PKEY_CTX_set_rsa_keygen_pubexp(pkey_ctx, pub_exp)) {
BN_free(pub_exp);
return SOTER_FAIL;
}
/* Override default key size for RSA key. Currently OpenSSL has default key size of 1024.
* LibreSSL has 2048. We will put 2048 explicitly */
SOTER_IF_FAIL((1 <= EVP_PKEY_CTX_set_rsa_keygen_bits(pkey_ctx, rsa_key_length(key_length))),
(BN_free(pub_exp), EVP_PKEY_CTX_free(pkey_ctx)));
if (1 > EVP_PKEY_CTX_set_rsa_keygen_bits(pkey_ctx, rsa_key_length(key_length))) {
return SOTER_FAIL;
}

if (!EVP_PKEY_keygen(pkey_ctx, &pkey)) {
return SOTER_FAIL;
Expand Down
5 changes: 2 additions & 3 deletions src/soter/openssl/soter_rsa_key_pair_gen.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,8 @@ soter_status_t soter_rsa_key_pair_gen_init(soter_rsa_key_pair_gen_t* ctx, const
EVP_PKEY_CTRL_RSA_KEYGEN_BITS,
ignatk marked this conversation as resolved.
Show resolved Hide resolved
rsa_key_length(key_length),
NULL)),
(BN_free(pub_exp), EVP_PKEY_CTX_free(ctx->pkey_ctx)));
SOTER_IF_FAIL(EVP_PKEY_keygen(ctx->pkey_ctx, &pkey),
(BN_free(pub_exp), EVP_PKEY_CTX_free(ctx->pkey_ctx)));
(EVP_PKEY_CTX_free(ctx->pkey_ctx)));
SOTER_IF_FAIL(EVP_PKEY_keygen(ctx->pkey_ctx, &pkey), (EVP_PKEY_CTX_free(ctx->pkey_ctx)));
return SOTER_SUCCESS;
}

Expand Down