Skip to content

Commit

Permalink
crypto: return correct bit length in KeyObject's asymmetricKeyDetails
Browse files Browse the repository at this point in the history
PR-URL: #46106
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
  • Loading branch information
panva authored and RafaelGSS committed Jan 20, 2023
1 parent 5304c89 commit 1146f02
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/crypto/crypto_dsa.cc
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ Maybe<bool> GetDsaKeyDetail(

DSA_get0_pqg(dsa, &p, &q, nullptr);

size_t modulus_length = BN_num_bytes(p) * CHAR_BIT;
size_t divisor_length = BN_num_bytes(q) * CHAR_BIT;
size_t modulus_length = BN_num_bits(p);
size_t divisor_length = BN_num_bits(q);

if (target
->Set(
Expand Down
2 changes: 1 addition & 1 deletion src/crypto/crypto_rsa.cc
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ Maybe<bool> GetRsaKeyDetail(

RSA_get0_key(rsa, &n, &e, nullptr);

size_t modulus_length = BN_num_bytes(n) * CHAR_BIT;
size_t modulus_length = BN_num_bits(n);

if (target
->Set(
Expand Down
28 changes: 28 additions & 0 deletions test/parallel/test-crypto-keygen.js
Original file line number Diff line number Diff line change
Expand Up @@ -1817,3 +1817,31 @@ generateKeyPair('rsa', {
hashAlgorithm: 'sha1'
}, common.mustNotCall()), { code: 'ERR_INVALID_ARG_VALUE' });
}

{
// https://github.com/nodejs/node/issues/46102#issuecomment-1372153541

generateKeyPair('rsa', {
modulusLength: 513,
}, common.mustSucceed((publicKey, privateKey) => {
assert.strictEqual(privateKey.asymmetricKeyDetails.modulusLength, 513);
assert.strictEqual(publicKey.asymmetricKeyDetails.modulusLength, 513);
}));

generateKeyPair('rsa-pss', {
modulusLength: 513,
}, common.mustSucceed((publicKey, privateKey) => {
assert.strictEqual(privateKey.asymmetricKeyDetails.modulusLength, 513);
assert.strictEqual(publicKey.asymmetricKeyDetails.modulusLength, 513);
}));

if (common.hasOpenSSL3) {
generateKeyPair('dsa', {
modulusLength: 2049,
divisorLength: 256,
}, common.mustSucceed((publicKey, privateKey) => {
assert.strictEqual(privateKey.asymmetricKeyDetails.modulusLength, 2049);
assert.strictEqual(publicKey.asymmetricKeyDetails.modulusLength, 2049);
}));
}
}

0 comments on commit 1146f02

Please sign in to comment.