Skip to content

Commit 1146f02

Browse files
panvaRafaelGSS
authored andcommittedJan 20, 2023
crypto: return correct bit length in KeyObject's asymmetricKeyDetails
PR-URL: #46106 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]>
1 parent 5304c89 commit 1146f02

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed
 

‎src/crypto/crypto_dsa.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,8 @@ Maybe<bool> GetDsaKeyDetail(
147147

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

150-
size_t modulus_length = BN_num_bytes(p) * CHAR_BIT;
151-
size_t divisor_length = BN_num_bytes(q) * CHAR_BIT;
150+
size_t modulus_length = BN_num_bits(p);
151+
size_t divisor_length = BN_num_bits(q);
152152

153153
if (target
154154
->Set(

‎src/crypto/crypto_rsa.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ Maybe<bool> GetRsaKeyDetail(
529529

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

532-
size_t modulus_length = BN_num_bytes(n) * CHAR_BIT;
532+
size_t modulus_length = BN_num_bits(n);
533533

534534
if (target
535535
->Set(

‎test/parallel/test-crypto-keygen.js

+28
Original file line numberDiff line numberDiff line change
@@ -1817,3 +1817,31 @@ generateKeyPair('rsa', {
18171817
hashAlgorithm: 'sha1'
18181818
}, common.mustNotCall()), { code: 'ERR_INVALID_ARG_VALUE' });
18191819
}
1820+
1821+
{
1822+
// https://github.com/nodejs/node/issues/46102#issuecomment-1372153541
1823+
1824+
generateKeyPair('rsa', {
1825+
modulusLength: 513,
1826+
}, common.mustSucceed((publicKey, privateKey) => {
1827+
assert.strictEqual(privateKey.asymmetricKeyDetails.modulusLength, 513);
1828+
assert.strictEqual(publicKey.asymmetricKeyDetails.modulusLength, 513);
1829+
}));
1830+
1831+
generateKeyPair('rsa-pss', {
1832+
modulusLength: 513,
1833+
}, common.mustSucceed((publicKey, privateKey) => {
1834+
assert.strictEqual(privateKey.asymmetricKeyDetails.modulusLength, 513);
1835+
assert.strictEqual(publicKey.asymmetricKeyDetails.modulusLength, 513);
1836+
}));
1837+
1838+
if (common.hasOpenSSL3) {
1839+
generateKeyPair('dsa', {
1840+
modulusLength: 2049,
1841+
divisorLength: 256,
1842+
}, common.mustSucceed((publicKey, privateKey) => {
1843+
assert.strictEqual(privateKey.asymmetricKeyDetails.modulusLength, 2049);
1844+
assert.strictEqual(publicKey.asymmetricKeyDetails.modulusLength, 2049);
1845+
}));
1846+
}
1847+
}

0 commit comments

Comments
 (0)
Please sign in to comment.