mirror of
https://github.com/nodejs/node.git
synced 2026-11-24 21:50:36 -05:00
crypto: support loading private keys through STORE loaders
Accept WHATWG URL objects in private-key inputs and load referenced keys through OpenSSL STORE loaders. Pass optional property queries and passphrases while preserving provider-owned EVP_PKEY objects for ordinary KeyObject and CryptoKey operations. Signed-off-by: Filip Skokan <panva.ip@gmail.com> PR-URL: https://github.com/nodejs/node/pull/63949 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
This commit is contained in:
1 parent
f97c7a3704
commit
565c3daebf
38 files changed
+2102
-139
No files matched your search
@@ -837,7 +837,11 @@ void PublicKeyCipher::Cipher(const FunctionCallbackInfo<Value>& args) {
|
||||
Environment* env = Environment::GetCurrent(args);
|
||||
|
||||
unsigned int offset = 0;
|
||||
auto data = KeyObjectData::GetPublicOrPrivateKeyFromJs(args, &offset);
|
||||
// TODO(panva): Use GetPrivateKeyFromJs() for private operations, then
|
||||
// remove allow_private_key_store and URL handling from
|
||||
// GetPublicOrPrivateKeyFromJs().
|
||||
auto data = KeyObjectData::GetPublicOrPrivateKeyFromJs(
|
||||
args, &offset, operation == PublicKeyCipher::kPrivate);
|
||||
if (!data) return;
|
||||
const auto& pkey = data.GetAsymmetricKey();
|
||||
if (!pkey) return;
|
||||
|
||||
@@ -475,13 +475,15 @@ bool ExportJWKEcKey(Environment* env,
|
||||
THROW_ERR_CRYPTO_INVALID_JWK(env, "Invalid JWK EC key");
|
||||
return false;
|
||||
}
|
||||
// A provider-backed key need not expose its public point.
|
||||
if (ec.getPublicKey() == nullptr) return false;
|
||||
|
||||
const auto pub = ec.getPublicKey();
|
||||
const auto group = ec.getGroup();
|
||||
|
||||
int degree_bits = EC_GROUP_get_degree(group);
|
||||
int degree_bytes =
|
||||
(degree_bits / CHAR_BIT) + (7 + (degree_bits % CHAR_BIT)) / 8;
|
||||
(degree_bits / CHAR_BIT) + (7 + (degree_bits % CHAR_BIT)) / 8;
|
||||
|
||||
auto x = BignumPointer::New();
|
||||
auto y = BignumPointer::New();
|
||||
@@ -543,6 +545,7 @@ bool ExportJWKEcKey(Environment* env,
|
||||
|
||||
if (key.GetKeyType() == kKeyTypePrivate) {
|
||||
auto pvt = ec.getPrivateKey();
|
||||
if (pvt == nullptr) return false;
|
||||
return SetEncodedValue(env, target, env->jwk_d_string(), pvt, degree_bytes)
|
||||
.IsJust();
|
||||
}
|
||||
|
||||
+130
-9
@@ -12,6 +12,7 @@
|
||||
#include "memory_tracker-inl.h"
|
||||
#include "node.h"
|
||||
#include "node_buffer.h"
|
||||
#include "permission/permission.h"
|
||||
#include "string_bytes.h"
|
||||
#include "threadpoolwork-inl.h"
|
||||
#include "util-inl.h"
|
||||
@@ -390,6 +391,12 @@ bool KeyObjectData::ToEncodedPublicKey(
|
||||
THROW_ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS(env);
|
||||
return false;
|
||||
}
|
||||
// A provider-backed key need not expose its public point.
|
||||
if (ec_key.getPublicKey() == nullptr) {
|
||||
THROW_ERR_CRYPTO_OPERATION_FAILED(env,
|
||||
"Failed to export EC public key");
|
||||
return false;
|
||||
}
|
||||
auto form = static_cast<point_conversion_form_t>(config.ec_point_form);
|
||||
const auto group = ec_key.getGroup();
|
||||
const auto point = ec_key.getPublicKey();
|
||||
@@ -441,7 +448,8 @@ bool KeyObjectData::ToEncodedPrivateKey(
|
||||
}
|
||||
const BIGNUM* private_key = ec_key.getPrivateKey();
|
||||
if (private_key == nullptr) {
|
||||
THROW_ERR_CRYPTO_OPERATION_FAILED(env, "Failed to get EC private key");
|
||||
THROW_ERR_CRYPTO_OPERATION_FAILED(env,
|
||||
"Failed to export EC private key");
|
||||
return false;
|
||||
}
|
||||
const auto group = ec_key.getGroup();
|
||||
@@ -764,7 +772,103 @@ KeyObjectData KeyObjectData::GetPrivateKeyFromJs(
|
||||
bool allow_key_object) {
|
||||
Environment* env = Environment::GetCurrent(args);
|
||||
|
||||
// JWK format: data is a JS Object (not buffer), format int is JWK.
|
||||
// Store descriptor: data is a { uri, properties } object, format int is
|
||||
// STORE, and the passphrase slot carries an optional passphrase/PIN.
|
||||
if (args[*offset]->IsObject() && !IsAnyBufferSource(args[*offset]) &&
|
||||
args[*offset + 1]->IsInt32() &&
|
||||
static_cast<EVPKeyPointer::PKFormatType>(
|
||||
args[*offset + 1].As<Int32>()->Value()) ==
|
||||
EVPKeyPointer::PKFormatType::STORE) {
|
||||
Local<Object> store = args[*offset].As<Object>();
|
||||
Local<Value> uri_value;
|
||||
if (!store
|
||||
->Get(env->context(), FIXED_ONE_BYTE_STRING(env->isolate(), "uri"))
|
||||
.ToLocal(&uri_value)) {
|
||||
return {};
|
||||
}
|
||||
CHECK(uri_value->IsString());
|
||||
Utf8Value uri(env->isolate(), uri_value);
|
||||
|
||||
Local<Value> properties_value;
|
||||
if (!store
|
||||
->Get(env->context(),
|
||||
FIXED_ONE_BYTE_STRING(env->isolate(), "properties"))
|
||||
.ToLocal(&properties_value)) {
|
||||
return {};
|
||||
}
|
||||
std::string properties_storage;
|
||||
std::optional<std::string_view> properties;
|
||||
if (properties_value->IsString()) {
|
||||
Utf8Value properties_string(env->isolate(), properties_value);
|
||||
std::string_view properties_view = properties_string.ToStringView();
|
||||
properties_storage.assign(properties_view.data(), properties_view.size());
|
||||
properties = std::string_view(properties_storage);
|
||||
} else {
|
||||
CHECK(properties_value->IsNullOrUndefined());
|
||||
}
|
||||
|
||||
// OpenSSLStore is a global permission. URIs passed to STORE loaders can
|
||||
// contain credentials, so they must not be exposed through permission
|
||||
// errors or diagnostics.
|
||||
THROW_IF_INSUFFICIENT_PERMISSIONS(
|
||||
env, permission::PermissionScope::kOpenSSLStore, "", KeyObjectData());
|
||||
|
||||
std::optional<ArrayBufferOrViewContents<char>> passphrase_content;
|
||||
std::optional<ncrypto::Buffer<const char>> passphrase;
|
||||
if (IsAnyBufferSource(args[*offset + 3])) {
|
||||
passphrase_content.emplace(args[*offset + 3]);
|
||||
if (!passphrase_content->CheckSizeInt32()) [[unlikely]] {
|
||||
THROW_ERR_OUT_OF_RANGE(env, "passphrase is too big");
|
||||
return {};
|
||||
}
|
||||
passphrase = ncrypto::Buffer<const char>{
|
||||
.data = passphrase_content->data(),
|
||||
.len = passphrase_content->size(),
|
||||
};
|
||||
} else {
|
||||
CHECK(args[*offset + 3]->IsNullOrUndefined());
|
||||
}
|
||||
|
||||
*offset += 5;
|
||||
EVPKeyPointer::StorePrivateKeyConfig config{
|
||||
.uri = uri.ToStringView(),
|
||||
.properties = properties,
|
||||
.passphrase = passphrase,
|
||||
};
|
||||
auto res = EVPKeyPointer::TryLoadPrivateKeyFromStore(config);
|
||||
if (res) {
|
||||
return CreateAsymmetric(KeyType::kKeyTypePrivate, std::move(res.value));
|
||||
}
|
||||
switch (res.error.value()) {
|
||||
case EVPKeyPointer::PKParseError::NEED_PASSPHRASE:
|
||||
ERR_clear_error();
|
||||
THROW_ERR_MISSING_PASSPHRASE(env,
|
||||
"Passphrase required for encrypted key");
|
||||
break;
|
||||
case EVPKeyPointer::PKParseError::NOT_RECOGNIZED:
|
||||
ERR_clear_error();
|
||||
THROW_ERR_CRYPTO_OPERATION_FAILED(
|
||||
env, "No private key found through the OpenSSL STORE loader");
|
||||
break;
|
||||
default: {
|
||||
static constexpr const char* msg =
|
||||
"Failed to load private key through an OpenSSL STORE loader";
|
||||
// A loader may report a failure without leaving anything in the error
|
||||
// queue, in which case ThrowCryptoError() would produce a bare Error
|
||||
// carrying no code at all.
|
||||
if (res.openssl_error.value_or(0) == 0) {
|
||||
THROW_ERR_CRYPTO_OPERATION_FAILED(env, msg);
|
||||
} else {
|
||||
ThrowCryptoError(env, res.openssl_error.value(), msg);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
// Object formats: data is a JS Object (not buffer), format int determines
|
||||
// whether this is a JWK or an OpenSSL STORE loader descriptor.
|
||||
if (args[*offset]->IsObject() && !IsAnyBufferSource(args[*offset]) &&
|
||||
args[*offset + 1]->IsInt32()) {
|
||||
auto format = static_cast<EVPKeyPointer::PKFormatType>(
|
||||
@@ -818,7 +922,9 @@ KeyObjectData KeyObjectData::GetPrivateKeyFromJs(
|
||||
}
|
||||
|
||||
KeyObjectData KeyObjectData::GetPublicOrPrivateKeyFromJs(
|
||||
const FunctionCallbackInfo<Value>& args, unsigned int* offset) {
|
||||
const FunctionCallbackInfo<Value>& args,
|
||||
unsigned int* offset,
|
||||
bool allow_private_key_store) {
|
||||
Environment* env = Environment::GetCurrent(args);
|
||||
|
||||
// JWK format: data is a JS Object (not buffer), format int is JWK.
|
||||
@@ -831,6 +937,15 @@ KeyObjectData KeyObjectData::GetPublicOrPrivateKeyFromJs(
|
||||
*offset += 5;
|
||||
return data;
|
||||
}
|
||||
if (format == EVPKeyPointer::PKFormatType::STORE) {
|
||||
if (allow_private_key_store) {
|
||||
return GetPrivateKeyFromJs(args, offset, false);
|
||||
}
|
||||
THROW_ERR_INVALID_ARG_VALUE(
|
||||
env,
|
||||
"URLs for OpenSSL STORE loaders are only accepted for private keys");
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
if (args[*offset]->IsString() || IsAnyBufferSource(args[*offset])) {
|
||||
@@ -1467,8 +1582,11 @@ void KeyObjectHandle::ExportECPublicRaw(
|
||||
}
|
||||
|
||||
ECKeyPointer ec_key(m_pkey);
|
||||
if (!ec_key) {
|
||||
return THROW_ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS(env);
|
||||
if (!ec_key) return THROW_ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS(env);
|
||||
// A provider-backed key need not expose its public point.
|
||||
if (ec_key.getPublicKey() == nullptr) {
|
||||
return THROW_ERR_CRYPTO_OPERATION_FAILED(env,
|
||||
"Failed to export EC public key");
|
||||
}
|
||||
|
||||
CHECK(args[0]->IsInt32());
|
||||
@@ -1500,14 +1618,12 @@ void KeyObjectHandle::ExportECPrivateRaw(
|
||||
}
|
||||
|
||||
ECKeyPointer ec_key(m_pkey);
|
||||
if (!ec_key) {
|
||||
return THROW_ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS(env);
|
||||
}
|
||||
if (!ec_key) return THROW_ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS(env);
|
||||
|
||||
const BIGNUM* private_key = ec_key.getPrivateKey();
|
||||
if (private_key == nullptr) {
|
||||
return THROW_ERR_CRYPTO_OPERATION_FAILED(env,
|
||||
"Failed to get EC private key");
|
||||
"Failed to export EC private key");
|
||||
}
|
||||
|
||||
const auto group = ec_key.getGroup();
|
||||
@@ -1567,6 +1683,8 @@ void KeyObjectHandle::ExportJWK(
|
||||
|
||||
if (ExportJWKInner(env, key->Data(), args[0], args[1]->IsTrue())) {
|
||||
args.GetReturnValue().Set(args[0]);
|
||||
} else if (!env->isolate()->HasPendingException()) {
|
||||
THROW_ERR_CRYPTO_OPERATION_FAILED(env, "Failed to export JWK");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2038,6 +2156,8 @@ void Initialize(Environment* env, Local<Object> target) {
|
||||
static_cast<int>(EVPKeyPointer::PKFormatType::RAW_PRIVATE);
|
||||
constexpr int kKeyFormatRawSeed =
|
||||
static_cast<int>(EVPKeyPointer::PKFormatType::RAW_SEED);
|
||||
constexpr int kKeyFormatStore =
|
||||
static_cast<int>(EVPKeyPointer::PKFormatType::STORE);
|
||||
|
||||
constexpr auto kSigEncDER = DSASigEnc::DER;
|
||||
constexpr auto kSigEncP1363 = DSASigEnc::P1363;
|
||||
@@ -2084,6 +2204,7 @@ void Initialize(Environment* env, Local<Object> target) {
|
||||
NODE_DEFINE_CONSTANT(target, kKeyFormatRawPublic);
|
||||
NODE_DEFINE_CONSTANT(target, kKeyFormatRawPrivate);
|
||||
NODE_DEFINE_CONSTANT(target, kKeyFormatRawSeed);
|
||||
NODE_DEFINE_CONSTANT(target, kKeyFormatStore);
|
||||
NODE_DEFINE_CONSTANT(target, kKeyTypeSecret);
|
||||
NODE_DEFINE_CONSTANT(target, kKeyTypePublic);
|
||||
NODE_DEFINE_CONSTANT(target, kKeyTypePrivate);
|
||||
|
||||
@@ -77,7 +77,9 @@ class KeyObjectData final : public MemoryRetainer {
|
||||
bool allow_key_object);
|
||||
|
||||
static KeyObjectData GetPublicOrPrivateKeyFromJs(
|
||||
const v8::FunctionCallbackInfo<v8::Value>& args, unsigned int* offset);
|
||||
const v8::FunctionCallbackInfo<v8::Value>& args,
|
||||
unsigned int* offset,
|
||||
bool allow_private_key_store = false);
|
||||
|
||||
static v8::Maybe<ncrypto::EVPKeyPointer::PrivateKeyEncodingConfig>
|
||||
GetPrivateKeyEncodingFromJs(const v8::FunctionCallbackInfo<v8::Value>& args,
|
||||
|
||||
+154
-13
@@ -8,6 +8,10 @@
|
||||
#include "env-inl.h"
|
||||
#include "memory_tracker-inl.h"
|
||||
#include "openssl/ec.h"
|
||||
#if NCRYPTO_USE_OPENSSL3_PROVIDER
|
||||
#include <openssl/core_names.h>
|
||||
#include <openssl/evp.h>
|
||||
#endif
|
||||
#include "threadpoolwork-inl.h"
|
||||
#include "v8.h"
|
||||
|
||||
@@ -18,6 +22,7 @@ using ncrypto::ClearErrorOnReturn;
|
||||
using ncrypto::DataPointer;
|
||||
using ncrypto::Digest;
|
||||
using ncrypto::ECDSASigPointer;
|
||||
using ncrypto::ECKeyPointer;
|
||||
using ncrypto::EVPKeyCtxPointer;
|
||||
using ncrypto::EVPKeyPointer;
|
||||
using ncrypto::EVPMDCtxPointer;
|
||||
@@ -390,6 +395,113 @@ bool SupportsContextString(const EVPKeyPointer& key) {
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
|
||||
// Returns true unless the key is known not to be SM2, so that a key whose curve
|
||||
// cannot be determined opts out of the prehashed fallback rather than into it.
|
||||
bool MayBeSM2Key(const EVPKeyPointer& key) {
|
||||
#ifdef OPENSSL_IS_BORINGSSL
|
||||
return false;
|
||||
#else
|
||||
if (key.id() == EVP_PKEY_SM2) return true;
|
||||
if (key.id() != EVP_PKEY_EC) return false;
|
||||
|
||||
#if NCRYPTO_USE_OPENSSL3_PROVIDER
|
||||
// An ECKeyPointer would also need the public point, which a provider-backed
|
||||
// key need not expose.
|
||||
char group_name[64];
|
||||
size_t group_name_len = 0;
|
||||
if (EVP_PKEY_get_utf8_string_param(key.get(),
|
||||
OSSL_PKEY_PARAM_GROUP_NAME,
|
||||
group_name,
|
||||
sizeof(group_name),
|
||||
&group_name_len) != 1) {
|
||||
return true;
|
||||
}
|
||||
return OBJ_sn2nid(group_name) == NID_sm2 ||
|
||||
EC_curve_nist2nid(group_name) == NID_sm2;
|
||||
#else
|
||||
ECKeyPointer ec(key);
|
||||
if (!ec) return true;
|
||||
|
||||
const EC_GROUP* group = ec.getGroup();
|
||||
if (group == nullptr) return true;
|
||||
return EC_GROUP_get_curve_name(group) == NID_sm2;
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
bool CanUsePrehashedFallback(const EVPKeyPointer& key,
|
||||
const Digest& digest,
|
||||
bool has_context) {
|
||||
if (!digest || has_context) return false;
|
||||
|
||||
if (key.isRsaVariant()) return true;
|
||||
|
||||
// SM2 digest signing first hashes the algorithm-specific Z value, so the
|
||||
// lower-level prehashed sign/verify operation is not equivalent.
|
||||
return key.isSigVariant() && !MayBeSM2Key(key);
|
||||
}
|
||||
|
||||
ByteSource SignPrehashed(Environment* env,
|
||||
const EVPKeyPointer& key,
|
||||
const Digest& digest,
|
||||
const ByteSource& input,
|
||||
int padding,
|
||||
std::optional<int> salt_length,
|
||||
DSASigEnc dsa_encoding) {
|
||||
EVPMDCtxPointer context = EVPMDCtxPointer::New();
|
||||
if (!context || !context.digestInit(digest) || !context.digestUpdate(input))
|
||||
[[unlikely]] {
|
||||
return {};
|
||||
}
|
||||
|
||||
auto data = context.digestFinal(context.getExpectedSize());
|
||||
if (!data) [[unlikely]] {
|
||||
return {};
|
||||
}
|
||||
|
||||
EVPKeyCtxPointer pkctx = key.newCtx();
|
||||
if (!pkctx || pkctx.initForSign() <= 0 ||
|
||||
!ApplyRSAOptions(key, pkctx.get(), padding, salt_length) ||
|
||||
!pkctx.setSignatureMd(context)) [[unlikely]] {
|
||||
return {};
|
||||
}
|
||||
|
||||
auto signature = pkctx.sign(data);
|
||||
if (!signature) [[unlikely]] {
|
||||
return {};
|
||||
}
|
||||
|
||||
DCHECK(!signature.isSecure());
|
||||
auto out = ByteSource::Allocated(signature.release());
|
||||
if (UseP1363Encoding(key, dsa_encoding)) {
|
||||
return ConvertSignatureToP1363(env, key, std::move(out));
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
bool VerifyPrehashed(const EVPKeyPointer& key,
|
||||
const Digest& digest,
|
||||
const ByteSource& input,
|
||||
const ByteSource& signature,
|
||||
int padding,
|
||||
std::optional<int> salt_length) {
|
||||
EVPMDCtxPointer context = EVPMDCtxPointer::New();
|
||||
if (!context || !context.digestInit(digest) || !context.digestUpdate(input))
|
||||
[[unlikely]] {
|
||||
return false;
|
||||
}
|
||||
|
||||
auto data = context.digestFinal(context.getExpectedSize());
|
||||
if (!data) [[unlikely]] {
|
||||
return false;
|
||||
}
|
||||
|
||||
EVPKeyCtxPointer pkctx = key.newCtx();
|
||||
return pkctx && pkctx.initForVerify() > 0 &&
|
||||
ApplyRSAOptions(key, pkctx.get(), padding, salt_length) &&
|
||||
pkctx.setSignatureMd(context) && pkctx.verify(signature, data);
|
||||
}
|
||||
} // namespace
|
||||
|
||||
SignBase::Error SignBase::Init(const char* digest) {
|
||||
@@ -807,9 +919,6 @@ bool SignTraits::DeriveBits(Environment* env,
|
||||
ByteSource* out,
|
||||
CryptoJobMode mode,
|
||||
CryptoErrorStore* errors) {
|
||||
auto context = EVPMDCtxPointer::New();
|
||||
if (!context) [[unlikely]]
|
||||
return false;
|
||||
const auto& key = params.key.GetAsymmetricKey();
|
||||
|
||||
bool has_context = (params.flags & SignConfiguration::kHasContextString &&
|
||||
@@ -821,6 +930,19 @@ bool SignTraits::DeriveBits(Environment* env,
|
||||
return false;
|
||||
}
|
||||
|
||||
int padding = params.flags & SignConfiguration::kHasPadding
|
||||
? params.padding
|
||||
: key.getDefaultSignPadding();
|
||||
|
||||
std::optional<int> salt_length =
|
||||
params.flags & SignConfiguration::kHasSaltLength
|
||||
? std::optional<int>(params.salt_length)
|
||||
: std::nullopt;
|
||||
|
||||
auto context = EVPMDCtxPointer::New();
|
||||
if (!context) [[unlikely]]
|
||||
return false;
|
||||
|
||||
auto ctx = ([&] {
|
||||
if (has_context) {
|
||||
ncrypto::Buffer<const unsigned char> context_buf{
|
||||
@@ -849,15 +971,6 @@ bool SignTraits::DeriveBits(Environment* env,
|
||||
return false;
|
||||
}
|
||||
|
||||
int padding = params.flags & SignConfiguration::kHasPadding
|
||||
? params.padding
|
||||
: key.getDefaultSignPadding();
|
||||
|
||||
std::optional<int> salt_length =
|
||||
params.flags & SignConfiguration::kHasSaltLength
|
||||
? std::optional<int>(params.salt_length)
|
||||
: std::nullopt;
|
||||
|
||||
if (!ApplyRSAOptions(key, *ctx, padding, salt_length)) {
|
||||
return false;
|
||||
}
|
||||
@@ -873,6 +986,19 @@ bool SignTraits::DeriveBits(Environment* env,
|
||||
*out = ByteSource::Allocated(data.release());
|
||||
} else {
|
||||
auto data = context.sign(params.data);
|
||||
// Only evaluated on the failure path: CanUsePrehashedFallback() has to
|
||||
// reconstruct EC key material to detect SM2, which is far too
|
||||
// expensive to pay for on every successful sign.
|
||||
if (!data && CanUsePrehashedFallback(key, params.digest, has_context)) {
|
||||
*out = SignPrehashed(env,
|
||||
key,
|
||||
params.digest,
|
||||
params.data,
|
||||
padding,
|
||||
salt_length,
|
||||
params.dsa_encoding);
|
||||
return static_cast<bool>(*out);
|
||||
}
|
||||
if (!data) [[unlikely]] {
|
||||
return false;
|
||||
}
|
||||
@@ -890,9 +1016,24 @@ bool SignTraits::DeriveBits(Environment* env,
|
||||
case SignConfiguration::Mode::Verify: {
|
||||
auto buf = DataPointer::Alloc(1);
|
||||
static_cast<char*>(buf.get())[0] = 0;
|
||||
if (context.verify(params.data, params.signature) &&
|
||||
// EVP_DigestVerify() documents 0 as a verification mismatch. In its
|
||||
// Update/Final path, it maps a failed EVP_DigestVerifyUpdate() to -1.
|
||||
// Some providers fail that combined operation but support raw
|
||||
// verification of a precomputed digest, so only retry negative results.
|
||||
// Retrying 0 would perform a second verification for every mismatch.
|
||||
int verify_result = context.verifyOneShot(params.data, params.signature);
|
||||
if (verify_result == 1 &&
|
||||
!HasSmallOrderEdDsaPoint(key, params.signature)) {
|
||||
static_cast<char*>(buf.get())[0] = 1;
|
||||
} else if (verify_result < 0 &&
|
||||
CanUsePrehashedFallback(key, params.digest, has_context) &&
|
||||
VerifyPrehashed(key,
|
||||
params.digest,
|
||||
params.data,
|
||||
params.signature,
|
||||
padding,
|
||||
salt_length)) {
|
||||
static_cast<char*>(buf.get())[0] = 1;
|
||||
}
|
||||
*out = ByteSource::Allocated(buf.release());
|
||||
}
|
||||
|
||||
@@ -939,6 +939,10 @@ Environment::Environment(IsolateData* isolate_data,
|
||||
if (!options_->allow_ffi) {
|
||||
permission()->Apply(this, {"*"}, permission::PermissionScope::kFFI);
|
||||
}
|
||||
if (!options_->allow_openssl_store) {
|
||||
permission()->Apply(
|
||||
this, {"*"}, permission::PermissionScope::kOpenSSLStore);
|
||||
}
|
||||
if (!options_->allow_worker_threads) {
|
||||
permission()->Apply(
|
||||
this, {"*"}, permission::PermissionScope::kWorkerThreads);
|
||||
|
||||
@@ -730,6 +730,12 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() {
|
||||
kAllowedInEnvvar,
|
||||
false,
|
||||
OptionNamespaces::kPermissionNamespace);
|
||||
AddOption("--allow-openssl-store",
|
||||
"allow use of OpenSSL STORE loaders when any permissions are set",
|
||||
&EnvironmentOptions::allow_openssl_store,
|
||||
kAllowedInEnvvar,
|
||||
false,
|
||||
OptionNamespaces::kPermissionNamespace);
|
||||
AddOption("--experimental-vm-modules",
|
||||
"experimental ES Module support in vm module",
|
||||
&EnvironmentOptions::experimental_vm_modules,
|
||||
|
||||
@@ -153,6 +153,7 @@ class EnvironmentOptions : public Options {
|
||||
bool allow_net = false;
|
||||
bool allow_wasi = false;
|
||||
bool allow_ffi = false;
|
||||
bool allow_openssl_store = false;
|
||||
bool allow_worker_threads = false;
|
||||
bool experimental_vm_modules = EXPERIMENTALS_DEFAULT_VALUE;
|
||||
bool async_context_frame = true;
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
#include "permission/openssl_store_permission.h"
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace node {
|
||||
|
||||
namespace permission {
|
||||
|
||||
// OpenSSLStorePermission manages a single global deny state for the use of
|
||||
// OpenSSL STORE loaders.
|
||||
void OpenSSLStorePermission::Apply(Environment* env,
|
||||
const std::vector<std::string>& allow,
|
||||
PermissionScope scope) {
|
||||
deny_all_ = true;
|
||||
}
|
||||
|
||||
void OpenSSLStorePermission::Drop(Environment* env,
|
||||
PermissionScope scope,
|
||||
const std::string_view& param) {
|
||||
deny_all_ = true;
|
||||
}
|
||||
|
||||
bool OpenSSLStorePermission::is_granted(Environment* env,
|
||||
PermissionScope perm,
|
||||
const std::string_view& param) const {
|
||||
return perm != PermissionScope::kOpenSSLStore || !deny_all_;
|
||||
}
|
||||
|
||||
} // namespace permission
|
||||
} // namespace node
|
||||
@@ -0,0 +1,34 @@
|
||||
#ifndef SRC_PERMISSION_OPENSSL_STORE_PERMISSION_H_
|
||||
#define SRC_PERMISSION_OPENSSL_STORE_PERMISSION_H_
|
||||
|
||||
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
|
||||
|
||||
#include <vector>
|
||||
#include "permission/permission_base.h"
|
||||
|
||||
namespace node {
|
||||
|
||||
namespace permission {
|
||||
|
||||
class OpenSSLStorePermission final : public PermissionBase {
|
||||
public:
|
||||
void Apply(Environment* env,
|
||||
const std::vector<std::string>& allow,
|
||||
PermissionScope scope) override;
|
||||
void Drop(Environment* env,
|
||||
PermissionScope scope,
|
||||
const std::string_view& param = "") override;
|
||||
bool is_granted(Environment* env,
|
||||
PermissionScope perm,
|
||||
const std::string_view& param = "") const override;
|
||||
|
||||
private:
|
||||
bool deny_all_ = false;
|
||||
};
|
||||
|
||||
} // namespace permission
|
||||
|
||||
} // namespace node
|
||||
|
||||
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
|
||||
#endif // SRC_PERMISSION_OPENSSL_STORE_PERMISSION_H_
|
||||
@@ -48,6 +48,8 @@ constexpr std::string_view GetDiagnosticsChannelName(PermissionScope scope) {
|
||||
return "node:permission-model:addon";
|
||||
case PermissionScope::kFFI:
|
||||
return "node:permission-model:ffi";
|
||||
case PermissionScope::kOpenSSLStore:
|
||||
return "node:permission-model:openssl-store";
|
||||
default:
|
||||
return {};
|
||||
}
|
||||
@@ -131,6 +133,8 @@ Permission::Permission() : enabled_(false), warning_only_(false) {
|
||||
std::shared_ptr<PermissionBase> net = std::make_shared<NetPermission>();
|
||||
std::shared_ptr<PermissionBase> addon = std::make_shared<AddonPermission>();
|
||||
std::shared_ptr<FFIPermission> ffi = std::make_shared<FFIPermission>();
|
||||
std::shared_ptr<PermissionBase> openssl_store =
|
||||
std::make_shared<OpenSSLStorePermission>();
|
||||
#define V(Name, _, __, ___) \
|
||||
nodes_.insert(std::make_pair(PermissionScope::k##Name, fs));
|
||||
FILESYSTEM_PERMISSIONS(V)
|
||||
@@ -163,6 +167,10 @@ Permission::Permission() : enabled_(false), warning_only_(false) {
|
||||
nodes_.insert(std::make_pair(PermissionScope::k##Name, ffi));
|
||||
FFI_PERMISSIONS(V)
|
||||
#undef V
|
||||
#define V(Name, _, __, ___) \
|
||||
nodes_.insert(std::make_pair(PermissionScope::k##Name, openssl_store));
|
||||
OPENSSL_STORE_PERMISSIONS(V)
|
||||
#undef V
|
||||
}
|
||||
|
||||
const char* GetErrorFlagSuggestion(node::permission::PermissionScope perm) {
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
#include "permission/fs_permission.h"
|
||||
#include "permission/inspector_permission.h"
|
||||
#include "permission/net_permission.h"
|
||||
#include "permission/openssl_store_permission.h"
|
||||
#include "permission/permission_base.h"
|
||||
#include "permission/wasi_permission.h"
|
||||
#include "permission/worker_permission.h"
|
||||
|
||||
@@ -37,6 +37,9 @@ namespace permission {
|
||||
|
||||
#define FFI_PERMISSIONS(V) V(FFI, "ffi", PermissionsRoot, "--allow-ffi")
|
||||
|
||||
#define OPENSSL_STORE_PERMISSIONS(V) \
|
||||
V(OpenSSLStore, "openssl.store", PermissionsRoot, "--allow-openssl-store")
|
||||
|
||||
#define PERMISSIONS(V) \
|
||||
FILESYSTEM_PERMISSIONS(V) \
|
||||
CHILD_PROCESS_PERMISSIONS(V) \
|
||||
@@ -45,7 +48,8 @@ namespace permission {
|
||||
INSPECTOR_PERMISSIONS(V) \
|
||||
NET_PERMISSIONS(V) \
|
||||
ADDON_PERMISSIONS(V) \
|
||||
FFI_PERMISSIONS(V)
|
||||
FFI_PERMISSIONS(V) \
|
||||
OPENSSL_STORE_PERMISSIONS(V)
|
||||
|
||||
#define V(name, _, __, ___) k##name,
|
||||
enum class PermissionScope {
|
||||
|
||||
Reference in new issue
Block a user