From c4cee3efcef71391aafb739fceae91c2e7888c7b Mon Sep 17 00:00:00 2001 From: Filip Skokan Date: Thu, 5 Jan 2023 13:59:40 +0100 Subject: [PATCH] crypto: return correct bit length in KeyObject's asymmetricKeyDetails --- src/crypto/crypto_dsa.cc | 4 ++-- src/crypto/crypto_rsa.cc | 2 +- test/parallel/test-crypto-keygen.js | 28 ++++++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/crypto/crypto_dsa.cc b/src/crypto/crypto_dsa.cc index 862c091b05f588..6839283fbf4ca7 100644 --- a/src/crypto/crypto_dsa.cc +++ b/src/crypto/crypto_dsa.cc @@ -147,8 +147,8 @@ Maybe 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( diff --git a/src/crypto/crypto_rsa.cc b/src/crypto/crypto_rsa.cc index 4eacb1f142b6e0..47a42246eddfc7 100644 --- a/src/crypto/crypto_rsa.cc +++ b/src/crypto/crypto_rsa.cc @@ -529,7 +529,7 @@ Maybe 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( diff --git a/test/parallel/test-crypto-keygen.js b/test/parallel/test-crypto-keygen.js index 1e967a4052bf05..9d08e2d9fab3dd 100644 --- a/test/parallel/test-crypto-keygen.js +++ b/test/parallel/test-crypto-keygen.js @@ -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); + })); + } +}