crypto: add support for RSA-PSS keys by tniessen · Pull Request #26960 · nodejs/node · GitHub
Skip to content
Closed
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
31 changes: 27 additions & 4 deletions doc/api/crypto.md
30 changes: 24 additions & 6 deletions lib/internal/crypto/keygen.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const { AsyncWrap, Providers } = internalBinding('async_wrap');
const {
generateKeyPairRSA,
generateKeyPairRSAPSS,
generateKeyPairDSA,
generateKeyPairEC,
generateKeyPairNid,
Expand Down Expand Up @@ -141,6 +142,7 @@ function check(type, options, callback) {
let impl;
switch (type) {
case 'rsa':
case 'rsa-pss':
{
const { modulusLength } = needOptions();
if (!isUint32(modulusLength))
Expand All @@ -153,10 +155,27 @@ function check(type, options, callback) {
throw new ERR_INVALID_OPT_VALUE('publicExponent', publicExponent);
}

impl = (wrap) => generateKeyPairRSA(modulusLength, publicExponent,
publicFormat, publicType,
privateFormat, privateType,
cipher, passphrase, wrap);
if (type === 'rsa') {
impl = (wrap) => generateKeyPairRSA(modulusLength, publicExponent,
publicFormat, publicType,
privateFormat, privateType,
cipher, passphrase, wrap);
break;
}

const { hash, mgf1Hash, saltLength } = options;
if (hash !== undefined && typeof hash !== 'string')
throw new ERR_INVALID_OPT_VALUE('hash', hash);
if (mgf1Hash !== undefined && typeof mgf1Hash !== 'string')
throw new ERR_INVALID_OPT_VALUE('mgf1Hash', mgf1Hash);
if (saltLength !== undefined && !isUint32(saltLength))
throw new ERR_INVALID_OPT_VALUE('saltLength', saltLength);

impl = (wrap) => generateKeyPairRSAPSS(modulusLength, publicExponent,
hash, mgf1Hash, saltLength,
publicFormat, publicType,
privateFormat, privateType,
cipher, passphrase, wrap);
}
break;
case 'dsa':
Expand Down Expand Up @@ -225,8 +244,7 @@ function check(type, options, callback) {
break;
default:
throw new ERR_INVALID_ARG_VALUE('type', type,
"must be one of 'rsa', 'dsa', 'ec', " +
"'ed25519', 'ed448', 'x25519', 'x448'");
'must be a supported key type');
Comment thread
tniessen marked this conversation as resolved.
Outdated
}

if (options) {
Expand Down
12 changes: 4 additions & 8 deletions lib/internal/crypto/sig.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ const {
signOneShot: _signOneShot,
verifyOneShot: _verifyOneShot
} = internalBinding('crypto');
const {
RSA_PSS_SALTLEN_AUTO,
RSA_PKCS1_PADDING
} = internalBinding('constants').crypto;
const {
getDefaultEncoding,
kHandle,
Expand Down Expand Up @@ -59,14 +55,14 @@ Sign.prototype.update = function update(data, encoding) {
legacyNativeHandle(Sign);

function getPadding(options) {
return getIntOption('padding', RSA_PKCS1_PADDING, options);
return getIntOption('padding', options);
}

function getSaltLength(options) {
return getIntOption('saltLength', RSA_PSS_SALTLEN_AUTO, options);
return getIntOption('saltLength', options);
}

function getIntOption(name, defaultValue, options) {
function getIntOption(name, options) {
const value = options[name];
if (value !== undefined) {
if (value === value >> 0) {
Expand All @@ -75,7 +71,7 @@ function getIntOption(name, defaultValue, options) {
throw new ERR_INVALID_OPT_VALUE(name, value);
}
}
return defaultValue;
return undefined;
}

Sign.prototype.sign = function sign(options, encoding) {
Expand Down
1 change: 1 addition & 0 deletions src/env.h
Loading