diff --git a/index.js b/index.js index 5072c34..fc8de29 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,30 @@ 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' && key !== null && 'key' in key; +} + 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..bce7530 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) { @@ -139,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(); + }); + }); + } }