Skip to content

Commit

Permalink
crypto: fix rsa key gen with non-default exponent
Browse files Browse the repository at this point in the history
EVP_PKEY_CTX_set_rsa_keygen_pubexp() accepts ownership of the exponent
on success, so do not free it.

Fixes: #27087

PR-URL: #27092
Reviewed-By: Ben Noordhuis <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Tobias Nießen <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
sam-github committed Apr 8, 2019
1 parent f96a660 commit 0911e88
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/node_crypto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6084,8 +6084,10 @@ class RSAKeyPairGenerationConfig : public KeyPairGenerationConfig {
BignumPointer bn(BN_new());
CHECK_NOT_NULL(bn.get());
CHECK(BN_set_word(bn.get(), exponent_));
// EVP_CTX acceps ownership of bn on success.
if (EVP_PKEY_CTX_set_rsa_keygen_pubexp(ctx.get(), bn.get()) <= 0)
return false;
bn.release();
}

return true;
Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-crypto-keygen.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ const sec1EncExp = (cipher) => getRegExpForPEM('EC PRIVATE KEY', cipher);
// To make the test faster, we will only test sync key generation once and
// with a relatively small key.
const ret = generateKeyPairSync('rsa', {
publicExponent: 0x10001,
publicExponent: 3,
modulusLength: 512,
publicKeyEncoding: {
type: 'pkcs1',
Expand Down Expand Up @@ -160,7 +160,7 @@ const sec1EncExp = (cipher) => getRegExpForPEM('EC PRIVATE KEY', cipher);

// Now do the same with an encrypted private key.
generateKeyPair('rsa', {
publicExponent: 0x10001,
publicExponent: 0x1001,
modulusLength: 512,
publicKeyEncoding,
privateKeyEncoding: {
Expand Down

0 comments on commit 0911e88

Please sign in to comment.