Skip to content
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
27 changes: 21 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
}
}
Expand Down
24 changes: 24 additions & 0 deletions test/jwa.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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();
});
});
}
}


Expand Down