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

crypto: return correct bit length in KeyObject's asymmetricKeyDetails #46106

Merged
merged 1 commit into from
Jan 7, 2023
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
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);
}));
}
}