Skip to content

Commit 5851994

Browse files
tniessentargos
authored andcommitted
crypto: fix regression in RSA-PSS keygen
Fixes: #39936 Refs: #35093 PR-URL: #39937 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Filip Skokan <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent fc01dd9 commit 5851994

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

src/crypto/crypto_rsa.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@ struct RsaKeyPairParams final : public MemoryRetainer {
2525
unsigned int modulus_bits;
2626
unsigned int exponent;
2727

28-
// The following used for RSA-PSS
28+
// The following options are used for RSA-PSS. If any of them are set, a
29+
// RSASSA-PSS-params sequence will be added to the key.
2930
const EVP_MD* md = nullptr;
3031
const EVP_MD* mgf1_md = nullptr;
31-
int saltlen = 0;
32+
int saltlen = -1;
3233

3334
SET_NO_MEMORY_INFO()
3435
SET_MEMORY_INFO_NAME(RsaKeyPairParams)

test/parallel/test-crypto-keygen.js

+23
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,29 @@ const sec1EncExp = (cipher) => getRegExpForPEM('EC PRIVATE KEY', cipher);
346346
}));
347347
}
348348

349+
{
350+
// 'rsa-pss' should not add a RSASSA-PSS-params sequence by default.
351+
// Regression test for: https://github.com/nodejs/node/issues/39936
352+
353+
generateKeyPair('rsa-pss', {
354+
modulusLength: 512
355+
}, common.mustSucceed((publicKey, privateKey) => {
356+
const expectedKeyDetails = {
357+
modulusLength: 512,
358+
publicExponent: 65537n
359+
};
360+
assert.deepStrictEqual(publicKey.asymmetricKeyDetails, expectedKeyDetails);
361+
assert.deepStrictEqual(privateKey.asymmetricKeyDetails, expectedKeyDetails);
362+
363+
// To allow backporting the fix to versions that do not support
364+
// asymmetricKeyDetails for RSA-PSS params, also verify that the exported
365+
// AlgorithmIdentifier member of the SubjectPublicKeyInfo has the expected
366+
// length of 11 bytes (as opposed to > 11 bytes if node added params).
367+
const spki = publicKey.export({ format: 'der', type: 'spki' });
368+
assert.strictEqual(spki[3], 11, spki.toString('hex'));
369+
}));
370+
}
371+
349372
{
350373
const privateKeyEncoding = {
351374
type: 'pkcs8',

0 commit comments

Comments
 (0)