mirror of
https://github.com/nodejs/node.git
synced 2026-11-29 14:27:57 -05:00
crypto: fix default salt length for X509 generation with PSS
Signed-off-by: Tim Perry <pimterry@gmail.com> PR-URL: https://github.com/nodejs/node/pull/67781 Reviewed-By: Filip Skokan <panva.ip@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
1 parent
7ed1ca0256
commit
6ce2fae7f0
4 files changed
+58
-28
No files matched your search
@@ -85,8 +85,8 @@ function getPadding(options, name = 'options') {
|
||||
return getIntOption('padding', options, name);
|
||||
}
|
||||
|
||||
function getSaltLength(options, name = 'options') {
|
||||
let saltLength = getIntOption('saltLength', options, name);
|
||||
function getSaltLength(options) {
|
||||
let saltLength = getIntOption('saltLength', options, 'options');
|
||||
if (options.padding === constants.RSA_PKCS1_PSS_PADDING && saltLength === undefined) {
|
||||
saltLength = constants.RSA_PSS_SALTLEN_MAX_SIGN;
|
||||
}
|
||||
@@ -321,6 +321,6 @@ module.exports = {
|
||||
signOneShot,
|
||||
Verify,
|
||||
verifyOneShot,
|
||||
getIntOption,
|
||||
getPadding,
|
||||
getSaltLength,
|
||||
};
|
||||
@@ -36,8 +36,8 @@ const {
|
||||
preparePublicOrPrivateKey,
|
||||
} = require('internal/crypto/keys');
|
||||
const {
|
||||
getIntOption,
|
||||
getPadding,
|
||||
getSaltLength,
|
||||
} = require('internal/crypto/sig');
|
||||
|
||||
const {
|
||||
@@ -531,7 +531,8 @@ function normalizeOptions(options, mode) {
|
||||
} = preparePrivateKey(signingKey, 'options.signature.key');
|
||||
const digest = getRequired(signature, 'digest', 'options.signature.digest');
|
||||
if (digest !== null) validateString(digest, 'options.signature.digest');
|
||||
const saltLength = getSaltLength(signingKey, 'options.signature.key');
|
||||
const saltLength = getIntOption(
|
||||
'saltLength', signingKey, 'options.signature.key');
|
||||
const padding = getPadding(signingKey, 'options.signature.key');
|
||||
let extensions;
|
||||
if (csr !== undefined) {
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
#include <openssl/x509.h>
|
||||
#include <openssl/x509v3.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <limits>
|
||||
#include <string>
|
||||
@@ -765,6 +766,21 @@ bool RequestVerificationFailed(CryptoErrorStore* errors) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Verifiers commonly reject certificate signatures whose salt is not
|
||||
// digest-sized (RFC 8446 section 4.2.3).
|
||||
int DefaultPssSaltLength(const EVPKeyPointer& key,
|
||||
const ncrypto::Digest& digest) {
|
||||
int salt_length = static_cast<int>(digest.size());
|
||||
if (key.isA(ncrypto::KeyAlgorithm::RSA_PSS)) {
|
||||
const auto pss_params = ncrypto::Rsa::PublicOnly(key).getPssParams();
|
||||
if (pss_params.has_value()) {
|
||||
salt_length =
|
||||
std::max(salt_length, static_cast<int>(pss_params->salt_length));
|
||||
}
|
||||
}
|
||||
return salt_length;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
X509GenerateConfig::X509GenerateConfig(X509GenerateConfig&& other) noexcept
|
||||
@@ -895,9 +911,16 @@ Maybe<void> X509GenerateTraits::AdditionalConfig(
|
||||
params->padding = args[padding_offset].As<v8::Int32>()->Value();
|
||||
}
|
||||
|
||||
const EVPKeyPointer& native_signing_key = signing_key.GetAsymmetricKey();
|
||||
if (native_signing_key.isRsaVariant() && !params->salt_length.has_value() &&
|
||||
params->padding.value_or(native_signing_key.getDefaultSignPadding()) ==
|
||||
RSA_PKCS1_PSS_PADDING) {
|
||||
params->salt_length =
|
||||
DefaultPssSaltLength(native_signing_key, params->digest);
|
||||
}
|
||||
|
||||
// BoringSSL's X.509 signer supports only restricted RSA-PSS parameters.
|
||||
#if NCRYPTO_USE_BORINGSSL
|
||||
const EVPKeyPointer& native_signing_key = signing_key.GetAsymmetricKey();
|
||||
if (native_signing_key.isRsaVariant()) {
|
||||
const int padding =
|
||||
params->padding.value_or(native_signing_key.getDefaultSignPadding());
|
||||
|
||||
@@ -71,6 +71,19 @@ function assertSigned(certificate, publicKey, privateKey) {
|
||||
assert.strictEqual(certificate.checkPrivateKey(privateKey), true);
|
||||
}
|
||||
|
||||
function assertPss(keys, signature, expectedSaltLength) {
|
||||
const certificate = generateX509CertificateSync(
|
||||
baseOptions(keys, { signature }));
|
||||
assertSigned(certificate, keys.publicKey, keys.privateKey);
|
||||
assert.strictEqual(certificate.signatureAlgorithmOid,
|
||||
'1.2.840.113549.1.1.10');
|
||||
// RSASSA-PSS-params saltLength: [2] EXPLICIT INTEGER
|
||||
const encodedSaltLength =
|
||||
Buffer.from([0xa2, 0x03, 0x02, 0x01, expectedSaltLength]);
|
||||
const { parameters } = certificate.signature.algorithm;
|
||||
assert(parameters.includes(encodedSaltLength), parameters.toString('hex'));
|
||||
}
|
||||
|
||||
function assertSynchronousFailure(options, expected) {
|
||||
assert.throws(() => generateX509CertificateSync(options), expected);
|
||||
assert.throws(() => generateX509Certificate(
|
||||
@@ -459,30 +472,23 @@ assertSigned(generateX509CertificateSync(baseOptions(leafKeys, {
|
||||
})), leafKeys.publicKey, leafKeys.privateKey);
|
||||
|
||||
{
|
||||
const certificate = generateX509CertificateSync(baseOptions(leafKeys, {
|
||||
signature: {
|
||||
key: {
|
||||
key: leafKeys.privateKey,
|
||||
padding: constants.RSA_PKCS1_PSS_PADDING,
|
||||
saltLength: 32,
|
||||
},
|
||||
digest: 'sha256',
|
||||
assertPss(leafKeys, {
|
||||
key: {
|
||||
key: leafKeys.privateKey,
|
||||
padding: constants.RSA_PKCS1_PSS_PADDING,
|
||||
},
|
||||
}));
|
||||
assertSigned(certificate, leafKeys.publicKey, leafKeys.privateKey);
|
||||
assert.strictEqual(certificate.signatureAlgorithmOid, '1.2.840.113549.1.1.10');
|
||||
}
|
||||
digest: 'sha256',
|
||||
}, 32);
|
||||
|
||||
if (!isBoringSSL) {
|
||||
const keys = generateKeyPairSync('rsa-pss', {
|
||||
modulusLength: 2048,
|
||||
hashAlgorithm: 'sha256',
|
||||
mgf1HashAlgorithm: 'sha256',
|
||||
saltLength: 64,
|
||||
});
|
||||
const certificate = generateX509CertificateSync(baseOptions(keys));
|
||||
assertSigned(certificate, keys.publicKey, keys.privateKey);
|
||||
assert.strictEqual(certificate.signatureAlgorithmOid, '1.2.840.113549.1.1.10');
|
||||
if (!isBoringSSL) {
|
||||
const restricted = generateKeyPairSync('rsa-pss', {
|
||||
modulusLength: 2048,
|
||||
hashAlgorithm: 'sha256',
|
||||
mgf1HashAlgorithm: 'sha256',
|
||||
saltLength: 64,
|
||||
});
|
||||
assertPss(restricted, { key: restricted.privateKey, digest: 'sha256' }, 64);
|
||||
}
|
||||
}
|
||||
|
||||
assert.strictEqual(generateX509Certificate.length, 2);
|
||||
|
||||
Reference in new issue
Block a user