From e19e5fceeedf57b995a791709c1a83a336e16fbd Mon Sep 17 00:00:00 2001 From: Cyril David <2257+cyx@users.noreply.github.com> Date: Thu, 26 Nov 2026 10:06:49 -0800 Subject: [PATCH 1/2] fix: sign RSA-PSS with one-shot crypto.sign The OpenSSL FIPS provider rejects RSA-PSS through createSign().sign(), which signs a precomputed digest, with "error:1C80007A:Provider routines::invalid digest". One-shot crypto.sign() is accepted and produces the same PSS signature. Fall back to createSign() where crypto.sign() is not available. Also spread a { key, passphrase } private key into the sign options: crypto rejects it nested under `key`, so PS* signing with a passphrase never worked. Co-Authored-By: Claude --- index.js | 26 ++++++++++++++++++++------ test/jwa.test.js | 10 ++++++++++ 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index 5072c34..75ba1ed 100644 --- a/index.js +++ b/index.js @@ -9,6 +9,7 @@ var MSG_INVALID_VERIFIER_KEY = 'key must be a string or a buffer'; var MSG_INVALID_SIGNER_KEY = 'key must be a string, a buffer or an object'; var supportsKeyObjects = typeof crypto.createPublicKey === 'function'; +var supportsOneShotSign = typeof crypto.sign === 'function'; if (supportsKeyObjects) { MSG_INVALID_VERIFIER_KEY += ' or a KeyObject'; MSG_INVALID_SECRET += 'or a KeyObject'; @@ -179,16 +180,29 @@ function createKeyVerifier(bits) { } } +function isKeyOptions(key) { + return typeof key === 'object' && !Buffer.isBuffer(key) && + !(supportsKeyObjects && key instanceof crypto.KeyObject); +} + function createPSSKeySigner(bits) { return function sign(thing, privateKey) { checkIsPrivateKey(privateKey); thing = normalizeInput(thing); - var signer = crypto.createSign('RSA-SHA' + bits); - var sig = (signer.update(thing), signer.sign({ - key: privateKey, - padding: crypto.constants.RSA_PKCS1_PSS_PADDING, - saltLength: crypto.constants.RSA_PSS_SALTLEN_DIGEST - }, 'base64')); + // A { key, passphrase } object is spread into the options, as crypto + // does not accept it nested under `key`. + var options = isKeyOptions(privateKey) ? Object.assign({}, privateKey) : { key: privateKey }; + options.padding = crypto.constants.RSA_PKCS1_PSS_PADDING; + options.saltLength = crypto.constants.RSA_PSS_SALTLEN_DIGEST; + var sig; + if (supportsOneShotSign) { + // One-shot signing: the OpenSSL FIPS provider rejects PSS through + // createSign().sign(), which signs a precomputed digest. + sig = crypto.sign('sha' + bits, Buffer.from(thing), options).toString('base64'); + } else { + var signer = crypto.createSign('RSA-SHA' + bits); + sig = (signer.update(thing), signer.sign(options, 'base64')); + } return fromBase64(sig); } } diff --git a/test/jwa.test.js b/test/jwa.test.js index 33ee11c..68c81df 100644 --- a/test/jwa.test.js +++ b/test/jwa.test.js @@ -127,6 +127,16 @@ if (semver.satisfies(nodeVersion, '^6.12.0 || >=8.0.0')) { t.end(); }); + test('RSA-PSS with passphrase signing, verifying', function (t) { + const input = 'test input'; + BIT_DEPTHS.forEach(function (bits) { + const algo = jwa('PS'+bits); + const sig = algo.sign(input, {key: rsaPrivateKeyWithPassphrase, passphrase: 'test_pass'}); + t.ok(algo.verify(input, sig, rsaPublicKeyWithPassphrase), 'should verify'); + }); + t.end(); + }); + if (SUPPORTS_KEY_OBJECTS) { BIT_DEPTHS.forEach(function (bits) { test('PS'+bits+': signing, verifying (KeyObject)', function (t) { From ee307a8f79cd57ed59e7bb63af8757bdaad84372 Mon Sep 17 00:00:00 2001 From: Cyril David <2257+cyx@users.noreply.github.com> Date: Thu, 26 Nov 2026 10:06:49 -0800 Subject: [PATCH 2/2] fix: keep signing RSA-PSS with a CryptoKey isKeyOptions treated a CryptoKey as a { key, passphrase } object, so PS* signing with one threw. Only spread objects that carry a `key` property. Co-Authored-By: Claude --- index.js | 5 +++-- test/jwa.test.js | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 75ba1ed..fc8de29 100644 --- a/index.js +++ b/index.js @@ -180,9 +180,10 @@ function createKeyVerifier(bits) { } } +// A plain { key, ... } object. Buffers, KeyObjects and CryptoKeys have no +// `key` property and are passed as the key itself. function isKeyOptions(key) { - return typeof key === 'object' && !Buffer.isBuffer(key) && - !(supportsKeyObjects && key instanceof crypto.KeyObject); + return typeof key === 'object' && key !== null && 'key' in key; } function createPSSKeySigner(bits) { diff --git a/test/jwa.test.js b/test/jwa.test.js index 68c81df..bce7530 100644 --- a/test/jwa.test.js +++ b/test/jwa.test.js @@ -149,6 +149,20 @@ if (semver.satisfies(nodeVersion, '^6.12.0 || >=8.0.0')) { }); }); } + + if (crypto.webcrypto && typeof crypto.KeyObject.prototype.toCryptoKey === 'function') { + BIT_DEPTHS.forEach(function (bits) { + test('PS'+bits+': signing, verifying (CryptoKey)', function (t) { + const input = 'h. jon benjamin'; + const algo = jwa('PS'+bits); + const privateKey = crypto.createPrivateKey(rsaPrivateKey) + .toCryptoKey({ name: 'RSA-PSS', hash: 'SHA-' + bits }, false, ['sign']); + const sig = algo.sign(input, privateKey); + t.ok(algo.verify(input, sig, crypto.createPublicKey(rsaPublicKey)), 'should verify'); + t.end(); + }); + }); + } }