mirror of
https://github.com/nodejs/node.git
synced 2026-11-30 11:55:44 -05:00
crypto: use names for asymmetric key algorithms
Provider-backed keys can have no numeric OpenSSL ID. Identify and construct asymmetric keys by algorithm name, replacing the custom PQC name-to-NID substitution with provider-aware matching in ncrypto. Centralize known algorithm names, public key-type names, capabilities, and backend compatibility in ncrypto. Use named key generation jobs and remove asymmetric EVP_PKEY constants from the internal JavaScript binding. Signed-off-by: Filip Skokan <panva.ip@gmail.com> Assisted-by: Codex PR-URL: https://github.com/nodejs/node/pull/65966 Backport-PR-URL: https://github.com/nodejs/node/pull/66233 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
This commit is contained in:
1 parent
ddbf974b47
commit
a4effe6809
35 files changed
+1911
-1584
No files matched your search
@@ -45,7 +45,8 @@ if (Object.keys(keyFixtures).length === 0) {
|
||||
}
|
||||
|
||||
const bench = common.createBenchmark(main, {
|
||||
keyType: Object.keys(keyFixtures),
|
||||
// Keep one size per family by default; other fixtures remain available via keyType.
|
||||
keyType: ['rsa', 'p-256', 'x25519', 'ml-kem-768'].filter((type) => keyFixtures[type]),
|
||||
mode: ['sync', 'async', 'async-parallel'],
|
||||
keyFormat: ['keyObject', 'keyObject.unique', 'pem', 'der', 'jwk',
|
||||
'raw-public', 'raw-private', 'raw-seed'],
|
||||
@@ -57,6 +58,9 @@ const bench = common.createBenchmark(main, {
|
||||
// assess whether mutexes over the key material impact the operation
|
||||
if (p.keyFormat === 'keyObject.unique')
|
||||
return p.mode === 'async-parallel';
|
||||
// Compare execution modes with pre-imported keys; measure parsing synchronously.
|
||||
if (p.mode !== 'sync' && p.keyFormat !== 'keyObject')
|
||||
return false;
|
||||
// raw-public is only supported for encapsulate, not rsa
|
||||
if (p.keyFormat === 'raw-public')
|
||||
return p.keyType !== 'rsa' && p.op === 'encapsulate';
|
||||
@@ -127,7 +131,8 @@ function main({ n, mode, keyFormat, keyType, op }) {
|
||||
keyFixtures[keyType].publicKey :
|
||||
keyFixtures[keyType].privateKey;
|
||||
const createKeyFn = isEncapsulate ? crypto.createPublicKey : crypto.createPrivateKey;
|
||||
const pems = [...Buffer.alloc(n)].map(() => pemSource);
|
||||
const count = keyFormat === 'keyObject.unique' ? n : 1;
|
||||
const pems = Array(count).fill(pemSource);
|
||||
const keyObjects = pems.map(createKeyFn);
|
||||
|
||||
// Warm up OpenSSL's provider operation cache for each key object
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
'use strict';
|
||||
|
||||
const common = require('../common.js');
|
||||
const { hasOpenSSL, hasFIPS, isBoringSSL } = require('../../test/common/crypto.js');
|
||||
const fixtures = require('../../test/common/fixtures.js');
|
||||
const { createPrivateKey, createPublicKey } = require('crypto');
|
||||
|
||||
const keys = {
|
||||
'rsa': 'rsa_private_2048',
|
||||
'rsa-pss': 'rsa_pss_private_2048',
|
||||
'p-256': 'ec_p256_private',
|
||||
'p-384': 'ec_p384_private',
|
||||
'p-521': 'ec_p521_private',
|
||||
'ed25519': 'ed25519_private',
|
||||
'x25519': 'x25519_private',
|
||||
};
|
||||
if (!isBoringSSL) {
|
||||
keys.ed448 = 'ed448_private';
|
||||
keys.x448 = 'x448_private';
|
||||
if (!hasFIPS()) keys['rsa-multiprime'] = 'rsa_private_2048_3_primes';
|
||||
}
|
||||
if (hasOpenSSL(3, 5) || isBoringSSL) {
|
||||
keys['ml-dsa-44'] = 'ml_dsa_44_private_seed_only';
|
||||
keys['ml-dsa-87'] = 'ml_dsa_87_private_seed_only';
|
||||
keys['ml-kem-768'] = 'ml_kem_768_private_seed_only';
|
||||
keys['ml-kem-1024'] = 'ml_kem_1024_private_seed_only';
|
||||
}
|
||||
if (hasOpenSSL(3, 5)) {
|
||||
keys['slh-dsa-sha2-128s'] = 'slh_dsa_sha2_128s_private';
|
||||
keys['slh-dsa-shake-256s'] = 'slh_dsa_shake_256s_private';
|
||||
}
|
||||
|
||||
const bench = common.createBenchmark(main, {
|
||||
// Keep one size per family by default; other fixtures remain available via keyType.
|
||||
keyType: ['rsa', 'rsa-pss', 'p-256', 'ed25519', 'x25519',
|
||||
'ml-dsa-44', 'ml-kem-768', 'slh-dsa-sha2-128s'].filter((type) => keys[type]),
|
||||
operation: ['import', 'export'],
|
||||
// PEM can be selected with format=pem; DER covers ASN.1 encoding by default.
|
||||
format: ['jwk', 'der', 'raw-public', 'raw-private', 'raw-seed'],
|
||||
type: ['public', 'private'],
|
||||
n: [1e4],
|
||||
}, {
|
||||
combinationFilter({ keyType, format, type }) {
|
||||
if (format === 'jwk') return keyType !== 'rsa-pss';
|
||||
if (!format.startsWith('raw-')) return true;
|
||||
if (keyType.startsWith('rsa')) return false;
|
||||
if (format === 'raw-public') return type === 'public';
|
||||
if (format === 'raw-private') return type === 'private' && !keyType.startsWith('ml-');
|
||||
return type === 'private' && keyType.startsWith('ml-');
|
||||
},
|
||||
});
|
||||
|
||||
function main({ keyType, operation, format, type, n }) {
|
||||
const privateKey = createPrivateKey(fixtures.readKey(`${keys[keyType]}.pem`));
|
||||
const key = type === 'private' ? privateKey : createPublicKey(privateKey);
|
||||
const options = { format };
|
||||
if (format === 'pem' || format === 'der') {
|
||||
options.type = type === 'private' ? 'pkcs8' : 'spki';
|
||||
}
|
||||
let run;
|
||||
if (operation === 'export') {
|
||||
run = () => key.export(options);
|
||||
} else {
|
||||
const input = { ...options, key: key.export(options) };
|
||||
if (format.startsWith('raw-')) {
|
||||
input.asymmetricKeyType = key.asymmetricKeyType;
|
||||
if (input.asymmetricKeyType === 'ec') {
|
||||
input.namedCurve = key.asymmetricKeyDetails.namedCurve;
|
||||
}
|
||||
}
|
||||
const importKey = type === 'private' ? createPrivateKey : createPublicKey;
|
||||
run = () => importKey(input);
|
||||
}
|
||||
// Resolve provider operations and warm the JS path before timing.
|
||||
for (let i = 0; i < 100; i++) run();
|
||||
bench.start();
|
||||
for (let i = 0; i < n; i++) run();
|
||||
bench.end(n);
|
||||
}
|
||||
@@ -25,9 +25,6 @@ if (hasOpenSSL(3, 5)) {
|
||||
|
||||
const data = crypto.randomBytes(256);
|
||||
|
||||
let pems;
|
||||
let keyObjects;
|
||||
|
||||
const bench = common.createBenchmark(main, {
|
||||
keyType: Object.keys(keyFixtures),
|
||||
mode: ['sync', 'async', 'async-parallel'],
|
||||
@@ -39,6 +36,9 @@ const bench = common.createBenchmark(main, {
|
||||
// assess whether mutexes over the key material impact the operation
|
||||
if (p.keyFormat === 'keyObject.unique')
|
||||
return p.mode === 'async-parallel';
|
||||
// Compare execution modes with pre-imported keys; measure parsing synchronously.
|
||||
if (p.mode !== 'sync' && p.keyFormat !== 'keyObject')
|
||||
return false;
|
||||
// raw-private is not supported for rsa and ml-dsa
|
||||
if (p.keyFormat === 'raw-private')
|
||||
return p.keyType !== 'rsa' && !p.keyType.startsWith('ml-');
|
||||
@@ -97,8 +97,9 @@ function measureAsyncParallel(n, digest, privateKey, keys) {
|
||||
}
|
||||
|
||||
function main({ n, mode, keyFormat, keyType }) {
|
||||
pems ||= [...Buffer.alloc(n)].map(() => keyFixtures[keyType]);
|
||||
keyObjects ||= pems.map(crypto.createPrivateKey);
|
||||
const count = keyFormat === 'keyObject.unique' ? n : 1;
|
||||
const pems = Array(count).fill(keyFixtures[keyType]);
|
||||
const keyObjects = pems.map(crypto.createPrivateKey);
|
||||
|
||||
// Warm up OpenSSL's provider operation cache for each key object
|
||||
for (const keyObject of keyObjects) {
|
||||
|
||||
@@ -32,9 +32,6 @@ if (hasOpenSSL(3, 5)) {
|
||||
|
||||
const data = crypto.randomBytes(256);
|
||||
|
||||
let pems;
|
||||
let keyObjects;
|
||||
|
||||
const bench = common.createBenchmark(main, {
|
||||
keyType: Object.keys(keyFixtures),
|
||||
mode: ['sync', 'async', 'async-parallel'],
|
||||
@@ -46,6 +43,9 @@ const bench = common.createBenchmark(main, {
|
||||
// assess whether mutexes over the key material impact the operation
|
||||
if (p.keyFormat === 'keyObject.unique')
|
||||
return p.mode === 'async-parallel';
|
||||
// Compare execution modes with pre-imported keys; measure parsing synchronously.
|
||||
if (p.mode !== 'sync' && p.keyFormat !== 'keyObject')
|
||||
return false;
|
||||
// raw-public is not supported by rsa
|
||||
if (p.keyFormat === 'raw-public')
|
||||
return p.keyType !== 'rsa';
|
||||
@@ -104,8 +104,9 @@ function measureAsyncParallel(n, digest, signature, publicKey, keys) {
|
||||
}
|
||||
|
||||
function main({ n, mode, keyFormat, keyType }) {
|
||||
pems ||= [...Buffer.alloc(n)].map(() => keyFixtures[keyType].publicKey);
|
||||
keyObjects ||= pems.map(crypto.createPublicKey);
|
||||
const count = keyFormat === 'keyObject.unique' ? n : 1;
|
||||
const pems = Array(count).fill(keyFixtures[keyType].publicKey);
|
||||
const keyObjects = pems.map(crypto.createPublicKey);
|
||||
|
||||
// Warm up OpenSSL's provider operation cache for each key object
|
||||
const warmupDigest = keyType === 'rsa' || keyType === 'ec' ? 'sha256' : null;
|
||||
|
||||
Vendored
+678
-294
File diff suppressed because it is too large.
Load diff
Vendored
+119
-63
@@ -12,6 +12,7 @@
|
||||
#include <openssl/rsa.h>
|
||||
#include <openssl/ssl.h>
|
||||
#include <openssl/x509.h>
|
||||
#include <array>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
@@ -120,53 +121,6 @@
|
||||
#define OPENSSL_WITH_AES_GCM_SIV 0
|
||||
#endif
|
||||
|
||||
#if defined(OPENSSL_IS_BORINGSSL) || OPENSSL_VERSION_PREREQ(3, 2)
|
||||
#define OPENSSL_WITH_SIGNATURE_CONTEXT_STRING 1
|
||||
#else
|
||||
#define OPENSSL_WITH_SIGNATURE_CONTEXT_STRING 0
|
||||
#endif
|
||||
|
||||
#if !defined(OPENSSL_IS_BORINGSSL) && OPENSSL_VERSION_PREREQ(3, 2)
|
||||
#define OPENSSL_WITH_OPENSSL_DHKEM 1
|
||||
#else
|
||||
#define OPENSSL_WITH_OPENSSL_DHKEM 0
|
||||
#endif
|
||||
|
||||
#if OPENSSL_WITH_KEM && !defined(OPENSSL_IS_BORINGSSL) && \
|
||||
!OPENSSL_VERSION_PREREQ(3, 5)
|
||||
#define OPENSSL_WITH_KEM_OPERATION_PARAM 1
|
||||
#else
|
||||
#define OPENSSL_WITH_KEM_OPERATION_PARAM 0
|
||||
#endif
|
||||
|
||||
// Post-quantum cryptography support. Keep these explicit so code can
|
||||
// distinguish provider API shape from the available algorithm set.
|
||||
#if !defined(OPENSSL_IS_BORINGSSL) && OPENSSL_VERSION_PREREQ(3, 5)
|
||||
#define OPENSSL_WITH_OPENSSL_PQC 1
|
||||
#else
|
||||
#define OPENSSL_WITH_OPENSSL_PQC 0
|
||||
#endif
|
||||
|
||||
#ifdef OPENSSL_IS_BORINGSSL
|
||||
#define OPENSSL_WITH_BORINGSSL_PQC 1
|
||||
#else
|
||||
#define OPENSSL_WITH_BORINGSSL_PQC 0
|
||||
#endif
|
||||
|
||||
#define OPENSSL_WITH_PQC \
|
||||
(OPENSSL_WITH_OPENSSL_PQC || OPENSSL_WITH_BORINGSSL_PQC)
|
||||
#define OPENSSL_WITH_PQC_ML_KEM_512 OPENSSL_WITH_OPENSSL_PQC
|
||||
#define OPENSSL_WITH_PQC_SLH_DSA OPENSSL_WITH_OPENSSL_PQC
|
||||
|
||||
#if OPENSSL_WITH_OPENSSL_PQC
|
||||
#define EVP_PKEY_ML_KEM_512 NID_ML_KEM_512
|
||||
#define EVP_PKEY_ML_KEM_768 NID_ML_KEM_768
|
||||
#define EVP_PKEY_ML_KEM_1024 NID_ML_KEM_1024
|
||||
#elif OPENSSL_WITH_BORINGSSL_PQC
|
||||
#define EVP_PKEY_ML_KEM_768 NID_ML_KEM_768
|
||||
#define EVP_PKEY_ML_KEM_1024 NID_ML_KEM_1024
|
||||
#endif
|
||||
|
||||
#if OPENSSL_VERSION_PREREQ(3, 0)
|
||||
#define OSSL3_CONST const
|
||||
#else
|
||||
@@ -366,6 +320,7 @@ class DataPointer;
|
||||
class DHPointer;
|
||||
class ECKeyPointer;
|
||||
class EVPKeyPointer;
|
||||
class KeyAlgorithm;
|
||||
class MacCache;
|
||||
class EVPMacCtxPointer;
|
||||
class EVPMacPointer;
|
||||
@@ -750,6 +705,8 @@ class Rsa final {
|
||||
const PublicKey getPublicKey() const;
|
||||
const PrivateKey getPrivateKey() const;
|
||||
const OtherPrimeInfos getOtherPrimeInfos() const;
|
||||
// Check that n is the product of all private-key prime factors.
|
||||
bool checkPrimeProduct() const;
|
||||
const std::optional<PssParams> getPssParams() const;
|
||||
|
||||
bool setPublicKey(BignumPointer&& n, BignumPointer&& e);
|
||||
@@ -821,6 +778,7 @@ class Ec final {
|
||||
BignumPointer* y,
|
||||
BignumPointer* priv,
|
||||
int* degree);
|
||||
static const KeyAlgorithm* GetNamedKeyAlgorithm(const char* name);
|
||||
|
||||
using GetCurveCallback = std::function<bool(const char*)>;
|
||||
static bool GetCurves(GetCurveCallback callback);
|
||||
@@ -1094,6 +1052,79 @@ class CipherCtxPointer final {
|
||||
DeleteFnPtr<EVP_CIPHER_CTX, EVP_CIPHER_CTX_free> ctx_;
|
||||
};
|
||||
|
||||
// Known key algorithms are identified by provider names, never synthetic NIDs.
|
||||
// Descriptors have static lifetime; availability is queried from the backend.
|
||||
class KeyAlgorithm final {
|
||||
public:
|
||||
static const KeyAlgorithm RSA;
|
||||
static const KeyAlgorithm RSA_PSS;
|
||||
static const KeyAlgorithm DSA;
|
||||
static const KeyAlgorithm DH;
|
||||
static const KeyAlgorithm EC;
|
||||
static const KeyAlgorithm ED25519;
|
||||
static const KeyAlgorithm ED448;
|
||||
static const KeyAlgorithm X25519;
|
||||
static const KeyAlgorithm X448;
|
||||
static const KeyAlgorithm SM2;
|
||||
static const KeyAlgorithm ML_DSA_44;
|
||||
static const KeyAlgorithm ML_DSA_65;
|
||||
static const KeyAlgorithm ML_DSA_87;
|
||||
static const KeyAlgorithm ML_KEM_512;
|
||||
static const KeyAlgorithm ML_KEM_768;
|
||||
static const KeyAlgorithm ML_KEM_1024;
|
||||
static const KeyAlgorithm SLH_DSA_SHA2_128F;
|
||||
static const KeyAlgorithm SLH_DSA_SHA2_128S;
|
||||
static const KeyAlgorithm SLH_DSA_SHA2_192F;
|
||||
static const KeyAlgorithm SLH_DSA_SHA2_192S;
|
||||
static const KeyAlgorithm SLH_DSA_SHA2_256F;
|
||||
static const KeyAlgorithm SLH_DSA_SHA2_256S;
|
||||
static const KeyAlgorithm SLH_DSA_SHAKE_128F;
|
||||
static const KeyAlgorithm SLH_DSA_SHAKE_128S;
|
||||
static const KeyAlgorithm SLH_DSA_SHAKE_192F;
|
||||
static const KeyAlgorithm SLH_DSA_SHAKE_192S;
|
||||
static const KeyAlgorithm SLH_DSA_SHAKE_256F;
|
||||
static const KeyAlgorithm SLH_DSA_SHAKE_256S;
|
||||
|
||||
// Look up a canonical name case-insensitively, including unavailable
|
||||
// algorithms.
|
||||
static const KeyAlgorithm* FromName(const char* name);
|
||||
using Callback = std::function<void(const KeyAlgorithm&)>;
|
||||
static void ForEachPqc(Callback callback);
|
||||
|
||||
const char* name() const { return name_; }
|
||||
const char* keyTypeName() const {
|
||||
return key_type_name_[0] == '\0' ? nullptr : key_type_name_.data();
|
||||
}
|
||||
bool isRsa() const;
|
||||
bool isAvailable() const;
|
||||
bool isPqc() const;
|
||||
bool isOkp() const;
|
||||
bool isOneShot() const;
|
||||
bool supportsRawPublic() const;
|
||||
bool supportsRawPrivate() const;
|
||||
size_t seedSize() const;
|
||||
|
||||
private:
|
||||
enum class Family { Other, EdDSA, XDH, MLDSA, MLKEM, SLHDSA };
|
||||
static constexpr size_t kMaxKeyTypeNameLength = 32;
|
||||
template <size_t N>
|
||||
constexpr KeyAlgorithm(const char (&name)[N],
|
||||
Family family,
|
||||
bool has_key_type = true)
|
||||
: name_(name), family_(family) {
|
||||
static_assert(N <= kMaxKeyTypeNameLength);
|
||||
if (has_key_type) {
|
||||
for (size_t i = 0; i < N; i++) {
|
||||
key_type_name_[i] =
|
||||
name[i] >= 'A' && name[i] <= 'Z' ? name[i] + ('a' - 'A') : name[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
const char* name_;
|
||||
std::array<char, kMaxKeyTypeNameLength> key_type_name_{};
|
||||
Family family_;
|
||||
};
|
||||
|
||||
class EVPKeyCtxPointer final {
|
||||
public:
|
||||
EVPKeyCtxPointer();
|
||||
@@ -1156,7 +1187,8 @@ class EVPKeyCtxPointer final {
|
||||
int initForSign();
|
||||
|
||||
static EVPKeyCtxPointer New(const EVPKeyPointer& key);
|
||||
static EVPKeyCtxPointer NewFromID(int id);
|
||||
static EVPKeyCtxPointer NewFromName(const char* name);
|
||||
static EVPKeyCtxPointer NewFromAlgorithm(const KeyAlgorithm& algorithm);
|
||||
|
||||
private:
|
||||
DeleteFnPtr<EVP_PKEY_CTX, EVP_PKEY_CTX_free> ctx_;
|
||||
@@ -1165,14 +1197,12 @@ class EVPKeyCtxPointer final {
|
||||
class EVPKeyPointer final {
|
||||
public:
|
||||
static EVPKeyPointer New();
|
||||
static EVPKeyPointer NewRawPublic(int id,
|
||||
static EVPKeyPointer NewRawPublic(const KeyAlgorithm& algorithm,
|
||||
const Buffer<const unsigned char>& data);
|
||||
static EVPKeyPointer NewRawPrivate(int id,
|
||||
static EVPKeyPointer NewRawPrivate(const KeyAlgorithm& algorithm,
|
||||
const Buffer<const unsigned char>& data);
|
||||
#if OPENSSL_WITH_PQC
|
||||
static EVPKeyPointer NewRawSeed(int id,
|
||||
static EVPKeyPointer NewRawSeed(const KeyAlgorithm& algorithm,
|
||||
const Buffer<const unsigned char>& data);
|
||||
#endif
|
||||
static EVPKeyPointer NewDH(DHPointer&& dh);
|
||||
#if NCRYPTO_USE_OPENSSL3_PROVIDER
|
||||
static EVPKeyPointer NewRSA(const Rsa& rsa);
|
||||
@@ -1277,11 +1307,19 @@ class EVPKeyPointer final {
|
||||
void reset(EVP_PKEY* pkey = nullptr);
|
||||
EVP_PKEY* release();
|
||||
|
||||
static int id(const EVP_PKEY* key);
|
||||
static int base_id(const EVP_PKEY* key);
|
||||
|
||||
int id() const;
|
||||
int base_id() const;
|
||||
static bool isA(const EVP_PKEY* key, const char* name);
|
||||
bool isA(const char* name) const;
|
||||
static bool isA(const EVP_PKEY* key, const KeyAlgorithm& algorithm);
|
||||
bool isA(const KeyAlgorithm& algorithm) const;
|
||||
// Resolve a known algorithm without caching key or provider state.
|
||||
const KeyAlgorithm* getAlgorithm() const;
|
||||
// Stable public key-type name, or nullptr for an unsupported key type.
|
||||
const char* getKeyTypeName() const;
|
||||
bool supportsRawPublic() const;
|
||||
bool supportsRawPrivate() const;
|
||||
bool supportsContextString() const;
|
||||
bool hasSmallOrderEdDsaPoint(
|
||||
const Buffer<const unsigned char>& signature) const;
|
||||
int bits() const;
|
||||
size_t size() const;
|
||||
|
||||
@@ -1291,9 +1329,22 @@ class EVPKeyPointer final {
|
||||
DataPointer rawPrivateKey() const;
|
||||
BIOPointer derPublicKey() const;
|
||||
|
||||
#if OPENSSL_WITH_PQC
|
||||
DataPointer rawSeed() const;
|
||||
#endif
|
||||
enum class RawExportError { UNSUPPORTED_KEY_TYPE, MISSING_SEED, FAILED };
|
||||
Result<DataPointer, RawExportError> rawSeed() const;
|
||||
|
||||
struct RawJwkData {
|
||||
const KeyAlgorithm* algorithm = nullptr;
|
||||
DataPointer public_key;
|
||||
DataPointer private_key;
|
||||
};
|
||||
// Raw JWK material for OKP and AKP keys. Private bytes use the JWK
|
||||
// representation (a seed for ML-DSA/ML-KEM, a raw private key otherwise).
|
||||
Result<RawJwkData, RawExportError> exportRawJwk(bool include_private) const;
|
||||
static EVPKeyPointer NewRawJwk(
|
||||
const KeyAlgorithm& algorithm,
|
||||
const Buffer<const unsigned char>& public_key,
|
||||
const std::optional<Buffer<const unsigned char>>& private_key =
|
||||
std::nullopt);
|
||||
|
||||
Result<BIOPointer, bool> writePrivateKey(
|
||||
const PrivateKeyEncodingConfig& config) const;
|
||||
@@ -1309,9 +1360,10 @@ class EVPKeyPointer final {
|
||||
operator Rsa() const;
|
||||
operator Dsa() const;
|
||||
|
||||
static bool isRsaVariant(const EVP_PKEY* key);
|
||||
bool isRsaVariant() const;
|
||||
bool isOneShotVariant() const;
|
||||
bool isSigVariant() const;
|
||||
bool mayBeSM2() const;
|
||||
bool validateDsaParameters() const;
|
||||
|
||||
private:
|
||||
@@ -2079,6 +2131,10 @@ class EnginePointer final {
|
||||
// FIPS
|
||||
bool isFipsEnabled();
|
||||
|
||||
// Configure seed-preserving PQC private-key encoding when the backend supports
|
||||
// it.
|
||||
void ConfigurePqcEncoding();
|
||||
|
||||
bool setFipsEnabled(bool enabled, CryptoErrorList* errors);
|
||||
|
||||
uint64_t getFipsStateGeneration();
|
||||
@@ -2190,7 +2246,7 @@ class KEM final {
|
||||
const Buffer<const void>& ciphertext);
|
||||
|
||||
private:
|
||||
#if OPENSSL_WITH_KEM_OPERATION_PARAM
|
||||
#if NCRYPTO_USE_OPENSSL3_PROVIDER
|
||||
static bool SetOperationParameter(EVP_PKEY_CTX* ctx,
|
||||
const EVPKeyPointer& key);
|
||||
#endif
|
||||
|
||||
@@ -18,11 +18,7 @@ const {
|
||||
kWebCryptoKeyFormatPKCS8,
|
||||
kWebCryptoKeyFormatRaw,
|
||||
kWebCryptoKeyFormatSPKI,
|
||||
NidKeyPairGenJob,
|
||||
EVP_PKEY_ED25519,
|
||||
EVP_PKEY_ED448,
|
||||
EVP_PKEY_X25519,
|
||||
EVP_PKEY_X448,
|
||||
NamedKeyPairGenJob,
|
||||
} = internalBinding('crypto');
|
||||
|
||||
const {
|
||||
@@ -69,21 +65,14 @@ function cfrgGenerateKey(algorithm, extractable, usages) {
|
||||
const { name } = algorithm;
|
||||
const allowedUsages = kUsages[name];
|
||||
const usagesSet = validateKeyUsages(usages, allowedUsages.keygen, name);
|
||||
const nid = {
|
||||
'__proto__': null,
|
||||
'Ed25519': EVP_PKEY_ED25519,
|
||||
'Ed448': EVP_PKEY_ED448,
|
||||
'X25519': EVP_PKEY_X25519,
|
||||
'X448': EVP_PKEY_X448,
|
||||
}[name];
|
||||
|
||||
const keyAlgorithm = { name };
|
||||
const keyUsages = getKeyPairUsages(usagesSet, allowedUsages);
|
||||
validateUsagesNotEmpty(keyUsages.private);
|
||||
|
||||
return jobPromise(() => new NidKeyPairGenJob(
|
||||
return jobPromise(() => new NamedKeyPairGenJob(
|
||||
kCryptoJobWebCrypto,
|
||||
nid,
|
||||
name,
|
||||
keyAlgorithm,
|
||||
getUsagesMask(keyUsages.public),
|
||||
getUsagesMask(keyUsages.private),
|
||||
@@ -178,7 +167,7 @@ function cfrgImportKey(
|
||||
}
|
||||
case 'raw': {
|
||||
verifyAcceptableKeyUse(name, usagesSet, allowedUsages.public);
|
||||
handle = importRawKey(true, keyData, kKeyFormatRawPublic, name);
|
||||
handle = importRawKey(true, keyData, kKeyFormatRawPublic, StringPrototypeToLowerCase(name));
|
||||
break;
|
||||
}
|
||||
default:
|
||||
|
||||
@@ -5,6 +5,7 @@ const {
|
||||
BigInt,
|
||||
PromiseWithResolvers,
|
||||
SafeSet,
|
||||
StringPrototypeToLowerCase,
|
||||
TypedArrayOf,
|
||||
TypedArrayPrototypeGetBuffer,
|
||||
TypedArrayPrototypeGetByteLength,
|
||||
@@ -93,8 +94,8 @@ const kKemPq768EncapsulationKeyLength = 1184;
|
||||
const kKemPq768CiphertextLength = 1088;
|
||||
const kKemPq1024EncapsulationKeyLength = 1568;
|
||||
const kKemPq1024CiphertextLength = 1568;
|
||||
const kKemPq768Name = 'ML-KEM-768';
|
||||
const kKemPq1024Name = 'ML-KEM-1024';
|
||||
const kKemPq768KeyType = 'ml-kem-768';
|
||||
const kKemPq1024KeyType = 'ml-kem-1024';
|
||||
const kX25519Name = 'X25519';
|
||||
const kEcdhName = 'ECDH';
|
||||
const kEcKeyType = 'ec';
|
||||
@@ -131,7 +132,7 @@ const kAlgorithms = {
|
||||
'__proto__': null,
|
||||
'MLKEM768-P256': withHybridLengths({
|
||||
name: 'MLKEM768-P256',
|
||||
kemPqName: kKemPq768Name,
|
||||
kemPqKeyType: kKemPq768KeyType,
|
||||
kemPqEncapsulationKeyLength: kKemPq768EncapsulationKeyLength,
|
||||
kemPqCiphertextLength: kKemPq768CiphertextLength,
|
||||
groupName: kEcdhName,
|
||||
@@ -149,11 +150,11 @@ const kAlgorithms = {
|
||||
}),
|
||||
'MLKEM768-X25519': withHybridLengths({
|
||||
name: 'MLKEM768-X25519',
|
||||
kemPqName: kKemPq768Name,
|
||||
kemPqKeyType: kKemPq768KeyType,
|
||||
kemPqEncapsulationKeyLength: kKemPq768EncapsulationKeyLength,
|
||||
kemPqCiphertextLength: kKemPq768CiphertextLength,
|
||||
groupName: kX25519Name,
|
||||
groupKeyType: kX25519Name,
|
||||
groupKeyType: StringPrototypeToLowerCase(kX25519Name),
|
||||
// Curve25519 RandomScalar is identity and Exp is X25519.
|
||||
// https://www.rfc-editor.org/rfc/rfc7748#section-5
|
||||
groupElementLength: kX25519KeyLength,
|
||||
@@ -166,7 +167,7 @@ const kAlgorithms = {
|
||||
}),
|
||||
'MLKEM1024-P384': withHybridLengths({
|
||||
name: 'MLKEM1024-P384',
|
||||
kemPqName: kKemPq1024Name,
|
||||
kemPqKeyType: kKemPq1024KeyType,
|
||||
kemPqEncapsulationKeyLength: kKemPq1024EncapsulationKeyLength,
|
||||
kemPqCiphertextLength: kKemPq1024CiphertextLength,
|
||||
groupName: kEcdhName,
|
||||
@@ -400,7 +401,7 @@ function importKemPqDecapsulationHandle(seed, config) {
|
||||
kKeyTypePrivate,
|
||||
seed,
|
||||
kKeyFormatRawSeed,
|
||||
config.kemPqName);
|
||||
config.kemPqKeyType);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -414,7 +415,7 @@ function importKemPqEncapsulationHandle(rawEncapsulationKey, config) {
|
||||
kKeyTypePublic,
|
||||
rawEncapsulationKey,
|
||||
kKeyFormatRawPublic,
|
||||
config.kemPqName);
|
||||
config.kemPqKeyType);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -4,41 +4,21 @@ const {
|
||||
FunctionPrototypeCall,
|
||||
ObjectDefineProperty,
|
||||
SafeArrayIterator,
|
||||
StringPrototypeToLowerCase,
|
||||
} = primordials;
|
||||
|
||||
const {
|
||||
DhKeyPairGenJob,
|
||||
DsaKeyPairGenJob,
|
||||
EcKeyPairGenJob,
|
||||
NidKeyPairGenJob,
|
||||
NamedKeyPairGenJob,
|
||||
getPqcKeyTypes,
|
||||
RsaKeyPairGenJob,
|
||||
SecretKeyGenJob,
|
||||
kCryptoJobAsync,
|
||||
kCryptoJobSync,
|
||||
kKeyVariantRSA_PSS,
|
||||
kKeyVariantRSA_SSA_PKCS1_v1_5,
|
||||
EVP_PKEY_ED25519,
|
||||
EVP_PKEY_ED448,
|
||||
EVP_PKEY_ML_DSA_44,
|
||||
EVP_PKEY_ML_DSA_65,
|
||||
EVP_PKEY_ML_DSA_87,
|
||||
EVP_PKEY_ML_KEM_1024,
|
||||
EVP_PKEY_ML_KEM_512,
|
||||
EVP_PKEY_ML_KEM_768,
|
||||
EVP_PKEY_SLH_DSA_SHA2_128F,
|
||||
EVP_PKEY_SLH_DSA_SHA2_128S,
|
||||
EVP_PKEY_SLH_DSA_SHA2_192F,
|
||||
EVP_PKEY_SLH_DSA_SHA2_192S,
|
||||
EVP_PKEY_SLH_DSA_SHA2_256F,
|
||||
EVP_PKEY_SLH_DSA_SHA2_256S,
|
||||
EVP_PKEY_SLH_DSA_SHAKE_128F,
|
||||
EVP_PKEY_SLH_DSA_SHAKE_128S,
|
||||
EVP_PKEY_SLH_DSA_SHAKE_192F,
|
||||
EVP_PKEY_SLH_DSA_SHAKE_192S,
|
||||
EVP_PKEY_SLH_DSA_SHAKE_256F,
|
||||
EVP_PKEY_SLH_DSA_SHAKE_256S,
|
||||
EVP_PKEY_X25519,
|
||||
EVP_PKEY_X448,
|
||||
OPENSSL_EC_NAMED_CURVE,
|
||||
OPENSSL_EC_EXPLICIT_CURVE,
|
||||
} = internalBinding('crypto');
|
||||
@@ -180,31 +160,16 @@ function parseKeyEncoding(keyType, options = kEmptyObject) {
|
||||
];
|
||||
}
|
||||
|
||||
const nidOnlyKeyPairs = {
|
||||
const namedKeyPairs = {
|
||||
'__proto__': null,
|
||||
'ed25519': EVP_PKEY_ED25519,
|
||||
'ed448': EVP_PKEY_ED448,
|
||||
'x25519': EVP_PKEY_X25519,
|
||||
'x448': EVP_PKEY_X448,
|
||||
'ml-dsa-44': EVP_PKEY_ML_DSA_44,
|
||||
'ml-dsa-65': EVP_PKEY_ML_DSA_65,
|
||||
'ml-dsa-87': EVP_PKEY_ML_DSA_87,
|
||||
'ml-kem-512': EVP_PKEY_ML_KEM_512,
|
||||
'ml-kem-768': EVP_PKEY_ML_KEM_768,
|
||||
'ml-kem-1024': EVP_PKEY_ML_KEM_1024,
|
||||
'slh-dsa-sha2-128f': EVP_PKEY_SLH_DSA_SHA2_128F,
|
||||
'slh-dsa-sha2-128s': EVP_PKEY_SLH_DSA_SHA2_128S,
|
||||
'slh-dsa-sha2-192f': EVP_PKEY_SLH_DSA_SHA2_192F,
|
||||
'slh-dsa-sha2-192s': EVP_PKEY_SLH_DSA_SHA2_192S,
|
||||
'slh-dsa-sha2-256f': EVP_PKEY_SLH_DSA_SHA2_256F,
|
||||
'slh-dsa-sha2-256s': EVP_PKEY_SLH_DSA_SHA2_256S,
|
||||
'slh-dsa-shake-128f': EVP_PKEY_SLH_DSA_SHAKE_128F,
|
||||
'slh-dsa-shake-128s': EVP_PKEY_SLH_DSA_SHAKE_128S,
|
||||
'slh-dsa-shake-192f': EVP_PKEY_SLH_DSA_SHAKE_192F,
|
||||
'slh-dsa-shake-192s': EVP_PKEY_SLH_DSA_SHAKE_192S,
|
||||
'slh-dsa-shake-256f': EVP_PKEY_SLH_DSA_SHAKE_256F,
|
||||
'slh-dsa-shake-256s': EVP_PKEY_SLH_DSA_SHAKE_256S,
|
||||
'ed25519': 'Ed25519',
|
||||
'ed448': 'Ed448',
|
||||
'x25519': 'X25519',
|
||||
'x448': 'X448',
|
||||
};
|
||||
for (const name of new SafeArrayIterator(getPqcKeyTypes())) {
|
||||
namedKeyPairs[StringPrototypeToLowerCase(name)] = name;
|
||||
}
|
||||
|
||||
function createJob(mode, type, options) {
|
||||
validateString(type, 'type');
|
||||
@@ -375,10 +340,10 @@ function createJob(mode, type, options) {
|
||||
...encoding);
|
||||
}
|
||||
default: {
|
||||
if (nidOnlyKeyPairs[type] === undefined) {
|
||||
if (namedKeyPairs[type] === undefined) {
|
||||
throw new ERR_INVALID_ARG_VALUE('type', type, 'must be a supported key type');
|
||||
}
|
||||
return new NidKeyPairGenJob(mode, nidOnlyKeyPairs[type], ...encoding);
|
||||
return new NamedKeyPairGenJob(mode, namedKeyPairs[type], ...encoding);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,10 +21,7 @@ const {
|
||||
kWebCryptoKeyFormatRaw,
|
||||
kWebCryptoKeyFormatPKCS8,
|
||||
kWebCryptoKeyFormatSPKI,
|
||||
NidKeyPairGenJob,
|
||||
EVP_PKEY_ML_DSA_44,
|
||||
EVP_PKEY_ML_DSA_65,
|
||||
EVP_PKEY_ML_DSA_87,
|
||||
NamedKeyPairGenJob,
|
||||
} = internalBinding('crypto');
|
||||
|
||||
const {
|
||||
@@ -63,20 +60,13 @@ function mlDsaGenerateKey(algorithm, extractable, usages) {
|
||||
const { name } = algorithm;
|
||||
const usagesSet = validateKeyUsages(usages, kUsages.keygen, name);
|
||||
|
||||
const nid = {
|
||||
'__proto__': null,
|
||||
'ML-DSA-44': EVP_PKEY_ML_DSA_44,
|
||||
'ML-DSA-65': EVP_PKEY_ML_DSA_65,
|
||||
'ML-DSA-87': EVP_PKEY_ML_DSA_87,
|
||||
}[name];
|
||||
|
||||
const keyAlgorithm = { name };
|
||||
const keyUsages = getKeyPairUsages(usagesSet, kUsages);
|
||||
validateUsagesNotEmpty(keyUsages.private);
|
||||
|
||||
return jobPromise(() => new NidKeyPairGenJob(
|
||||
return jobPromise(() => new NamedKeyPairGenJob(
|
||||
kCryptoJobWebCrypto,
|
||||
nid,
|
||||
name,
|
||||
keyAlgorithm,
|
||||
getUsagesMask(keyUsages.public),
|
||||
getUsagesMask(keyUsages.private),
|
||||
@@ -194,7 +184,9 @@ function mlDsaImportKey(
|
||||
name,
|
||||
usagesSet,
|
||||
isPublic ? kUsages.public : kUsages.private);
|
||||
handle = importRawKey(isPublic, keyData, isPublic ? kKeyFormatRawPublic : kKeyFormatRawSeed, name);
|
||||
handle = importRawKey(isPublic, keyData,
|
||||
isPublic ? kKeyFormatRawPublic : kKeyFormatRawSeed,
|
||||
StringPrototypeToLowerCase(name));
|
||||
break;
|
||||
}
|
||||
default:
|
||||
|
||||
@@ -18,10 +18,7 @@ const {
|
||||
kWebCryptoKeyFormatPKCS8,
|
||||
kWebCryptoKeyFormatRaw,
|
||||
kWebCryptoKeyFormatSPKI,
|
||||
NidKeyPairGenJob,
|
||||
EVP_PKEY_ML_KEM_512,
|
||||
EVP_PKEY_ML_KEM_768,
|
||||
EVP_PKEY_ML_KEM_1024,
|
||||
NamedKeyPairGenJob,
|
||||
} = internalBinding('crypto');
|
||||
|
||||
const {
|
||||
@@ -62,20 +59,13 @@ function mlKemGenerateKey(algorithm, extractable, usages) {
|
||||
const { name } = algorithm;
|
||||
const usagesSet = validateKeyUsages(usages, kUsages.keygen, name);
|
||||
|
||||
const nid = {
|
||||
'__proto__': null,
|
||||
'ML-KEM-512': EVP_PKEY_ML_KEM_512,
|
||||
'ML-KEM-768': EVP_PKEY_ML_KEM_768,
|
||||
'ML-KEM-1024': EVP_PKEY_ML_KEM_1024,
|
||||
}[name];
|
||||
|
||||
const keyAlgorithm = { name };
|
||||
const keyUsages = getKeyPairUsages(usagesSet, kUsages);
|
||||
validateUsagesNotEmpty(keyUsages.private);
|
||||
|
||||
return jobPromise(() => new NidKeyPairGenJob(
|
||||
return jobPromise(() => new NamedKeyPairGenJob(
|
||||
kCryptoJobWebCrypto,
|
||||
nid,
|
||||
name,
|
||||
keyAlgorithm,
|
||||
getUsagesMask(keyUsages.public),
|
||||
getUsagesMask(keyUsages.private),
|
||||
@@ -171,7 +161,9 @@ function mlKemImportKey(
|
||||
name,
|
||||
usagesSet,
|
||||
isPublic ? kUsages.public : kUsages.private);
|
||||
handle = importRawKey(isPublic, keyData, isPublic ? kKeyFormatRawPublic : kKeyFormatRawSeed, name);
|
||||
handle = importRawKey(isPublic, keyData,
|
||||
isPublic ? kKeyFormatRawPublic : kKeyFormatRawSeed,
|
||||
StringPrototypeToLowerCase(name));
|
||||
break;
|
||||
}
|
||||
case 'jwk': {
|
||||
|
||||
+12
-15
@@ -42,17 +42,12 @@ const {
|
||||
getCachedAliases,
|
||||
getCachedMacAliases,
|
||||
getOpenSSLSecLevelCrypto: getOpenSSLSecLevel,
|
||||
EVP_PKEY_ML_DSA_44,
|
||||
EVP_PKEY_ML_DSA_65,
|
||||
EVP_PKEY_ML_DSA_87,
|
||||
EVP_PKEY_ML_KEM_512,
|
||||
EVP_PKEY_ML_KEM_768,
|
||||
EVP_PKEY_ML_KEM_1024,
|
||||
kKeyVariantAES_OCB_128: hasAesOcbMode,
|
||||
Argon2Job,
|
||||
getFipsCrypto,
|
||||
getFipsCryptoGeneration,
|
||||
KmacJob,
|
||||
getPqcKeyTypes,
|
||||
} = internalBinding('crypto');
|
||||
|
||||
const isFips = getFipsCrypto() === 1;
|
||||
@@ -493,6 +488,8 @@ const kAlgorithmDefinitions = {
|
||||
};
|
||||
|
||||
// Conditionally supported algorithms
|
||||
const pqcKeyTypes = getPqcKeyTypes();
|
||||
|
||||
const conditionalAlgorithms = {
|
||||
'AES-OCB': !!hasAesOcbMode,
|
||||
'Argon2d': !!Argon2Job,
|
||||
@@ -509,21 +506,21 @@ const conditionalAlgorithms = {
|
||||
'KMAC256': !!KmacJob,
|
||||
'KT128': !isFips,
|
||||
'KT256': !isFips,
|
||||
'ML-DSA-44': !!EVP_PKEY_ML_DSA_44,
|
||||
'ML-DSA-65': !!EVP_PKEY_ML_DSA_65,
|
||||
'ML-DSA-87': !!EVP_PKEY_ML_DSA_87,
|
||||
'ML-KEM-512': !!EVP_PKEY_ML_KEM_512,
|
||||
'ML-KEM-768': !!EVP_PKEY_ML_KEM_768,
|
||||
'ML-KEM-1024': !!EVP_PKEY_ML_KEM_1024,
|
||||
'MLKEM768-P256': !isFips && !!EVP_PKEY_ML_KEM_768 &&
|
||||
'ML-DSA-44': ArrayPrototypeIncludes(pqcKeyTypes, 'ML-DSA-44'),
|
||||
'ML-DSA-65': ArrayPrototypeIncludes(pqcKeyTypes, 'ML-DSA-65'),
|
||||
'ML-DSA-87': ArrayPrototypeIncludes(pqcKeyTypes, 'ML-DSA-87'),
|
||||
'ML-KEM-512': ArrayPrototypeIncludes(pqcKeyTypes, 'ML-KEM-512'),
|
||||
'ML-KEM-768': ArrayPrototypeIncludes(pqcKeyTypes, 'ML-KEM-768'),
|
||||
'ML-KEM-1024': ArrayPrototypeIncludes(pqcKeyTypes, 'ML-KEM-1024'),
|
||||
'MLKEM768-P256': !isFips && ArrayPrototypeIncludes(pqcKeyTypes, 'ML-KEM-768') &&
|
||||
(!process.features.openssl_is_boringssl ||
|
||||
(ArrayPrototypeIncludes(getHashes(), 'sha3-256') &&
|
||||
ArrayPrototypeIncludes(getHashes(), 'shake256'))),
|
||||
'MLKEM768-X25519': !isFips && !!EVP_PKEY_ML_KEM_768 &&
|
||||
'MLKEM768-X25519': !isFips && ArrayPrototypeIncludes(pqcKeyTypes, 'ML-KEM-768') &&
|
||||
(!process.features.openssl_is_boringssl ||
|
||||
(ArrayPrototypeIncludes(getHashes(), 'sha3-256') &&
|
||||
ArrayPrototypeIncludes(getHashes(), 'shake256'))),
|
||||
'MLKEM1024-P384': !isFips && !!EVP_PKEY_ML_KEM_1024 &&
|
||||
'MLKEM1024-P384': !isFips && ArrayPrototypeIncludes(pqcKeyTypes, 'ML-KEM-1024') &&
|
||||
(!process.features.openssl_is_boringssl ||
|
||||
(ArrayPrototypeIncludes(getHashes(), 'sha3-256') &&
|
||||
ArrayPrototypeIncludes(getHashes(), 'shake256'))),
|
||||
|
||||
+97
-33
@@ -42,9 +42,11 @@ following table:
|
||||
| `crypto_hash` | Basic hash (e.g. SHA-256) functions. |
|
||||
| `crypto_hkdf` | HKDF (Key derivation) implementation. |
|
||||
| `crypto_hmac` | HMAC implementations. |
|
||||
| `crypto_keygen` | Secret and asymmetric key generation jobs. |
|
||||
| `crypto_keys` | Utilities for using and generating secret, private, and public keys. |
|
||||
| `crypto_mac` | Provider-generic MAC implementations. |
|
||||
| `crypto_pbkdf2` | PBKDF2 key / bit generation implementation. |
|
||||
| `crypto_pqc` | Post-quantum algorithm enumeration. |
|
||||
| `crypto_rsa` | RSA Key Generation functions. |
|
||||
| `crypto_scrypt` | Scrypt key / bit generation implementation. |
|
||||
| `crypto_sig` | General digital signature and verification utilities. |
|
||||
@@ -58,41 +60,36 @@ When new crypto protocols are added, they will be added into their own
|
||||
|
||||
## Helpful concepts
|
||||
|
||||
Node.js currently uses OpenSSL to provide it's crypto substructure.
|
||||
Node.js currently uses OpenSSL to provide its crypto substructure.
|
||||
(Some custom Node.js distributions -- such as Electron -- use BoringSSL
|
||||
instead.)
|
||||
|
||||
This section aims to explain some of the utilities that have been
|
||||
provided to make working with the OpenSSL APIs a bit easier.
|
||||
|
||||
### The ncrypto boundary
|
||||
|
||||
[`deps/ncrypto`](../../deps/ncrypto) provides the OpenSSL and BoringSSL
|
||||
wrappers used by this subsystem. Put backend adaptation, algorithm metadata,
|
||||
and reusable cryptographic operations there. Keep JavaScript argument
|
||||
validation, public API policy and errors, V8 objects, and crypto job integration
|
||||
in `src/crypto` and `lib/internal/crypto`.
|
||||
|
||||
For provider operations, query the supported algorithms and parameters through
|
||||
ncrypto. Version guards are still needed when a C API is unavailable in older
|
||||
headers or when handling a known version-specific behavior.
|
||||
|
||||
### Pointer types
|
||||
|
||||
Most of the key OpenSSL types need to be explicitly freed when they are
|
||||
no longer needed. Failure to do so introduces memory leaks. To make this
|
||||
easier (and less error prone), the `crypto_util.h` defines a number of
|
||||
smart-pointer aliases that should be used:
|
||||
Most OpenSSL objects need to be explicitly freed when they are no longer
|
||||
needed. Use the ownership wrappers declared in
|
||||
[`ncrypto.h`](../../deps/ncrypto/ncrypto.h) to manage their lifetime.
|
||||
|
||||
```cpp
|
||||
using X509Pointer = DeleteFnPtr<X509, X509_free>;
|
||||
using BIOPointer = DeleteFnPtr<BIO, BIO_free_all>;
|
||||
using SSLCtxPointer = DeleteFnPtr<SSL_CTX, SSL_CTX_free>;
|
||||
using SSLSessionPointer = DeleteFnPtr<SSL_SESSION, SSL_SESSION_free>;
|
||||
using SSLPointer = DeleteFnPtr<SSL, SSL_free>;
|
||||
using PKCS8Pointer = DeleteFnPtr<PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO_free>;
|
||||
using EVPKeyPointer = DeleteFnPtr<EVP_PKEY, EVP_PKEY_free>;
|
||||
using EVPKeyCtxPointer = DeleteFnPtr<EVP_PKEY_CTX, EVP_PKEY_CTX_free>;
|
||||
using EVPMDCtxPointer = DeleteFnPtr<EVP_MD_CTX, EVP_MD_CTX_free>;
|
||||
using RSAPointer = DeleteFnPtr<RSA, RSA_free>;
|
||||
using ECPointer = DeleteFnPtr<EC_KEY, EC_KEY_free>;
|
||||
using BignumPointer = DeleteFnPtr<BIGNUM, BN_clear_free>;
|
||||
using NetscapeSPKIPointer = DeleteFnPtr<NETSCAPE_SPKI, NETSCAPE_SPKI_free>;
|
||||
using ECGroupPointer = DeleteFnPtr<EC_GROUP, EC_GROUP_free>;
|
||||
using ECPointPointer = DeleteFnPtr<EC_POINT, EC_POINT_free>;
|
||||
using ECKeyPointer = DeleteFnPtr<EC_KEY, EC_KEY_free>;
|
||||
using DHPointer = DeleteFnPtr<DH, DH_free>;
|
||||
using ECDSASigPointer = DeleteFnPtr<ECDSA_SIG, ECDSA_SIG_free>;
|
||||
using CipherCtxPointer = DeleteFnPtr<EVP_CIPHER_CTX, EVP_CIPHER_CTX_free>;
|
||||
```
|
||||
Some wrappers, such as `PKCS8Pointer`, are `DeleteFnPtr` aliases. Others,
|
||||
including `EVPKeyPointer`, `EVPKeyCtxPointer`, `EVPMDCtxPointer`, `BIOPointer`,
|
||||
`BignumPointer`, and `ECKeyPointer`, are dedicated classes that also provide
|
||||
operations on the wrapped state. Their representation can vary by backend;
|
||||
use their methods to keep that adaptation inside ncrypto.
|
||||
|
||||
Examples of these being used are pervasive through the `src/crypto` code.
|
||||
|
||||
@@ -153,6 +150,70 @@ threadpool).
|
||||
Refer to `crypto_keys.h` and `crypto_keys.cc` for all code relating to the
|
||||
core key objects.
|
||||
|
||||
#### Asymmetric key algorithms
|
||||
|
||||
Use ncrypto's `KeyAlgorithm` descriptors to identify known algorithms in C++.
|
||||
They hold canonical algorithm names and metadata, with static lifetime, so
|
||||
callers can retain descriptor pointers across asynchronous jobs. For example,
|
||||
`KeyAlgorithm::RSA_PSS` names `RSA-PSS` and `KeyAlgorithm::ML_DSA_44` names
|
||||
`ML-DSA-44`.
|
||||
|
||||
For an existing key, use `key.isA(KeyAlgorithm::RSA_PSS)` or another descriptor.
|
||||
On OpenSSL 3 and later this uses `EVP_PKEY_is_a()` to recognize provider aliases.
|
||||
Numeric key IDs are unsuitable for provider-only keys: OpenSSL can return `-1`
|
||||
for their ID. Numeric adapters for BoringSSL and legacy OpenSSL stay private to
|
||||
ncrypto.
|
||||
|
||||
When a function needs algorithm metadata, use `key.getAlgorithm()`. It returns
|
||||
a pointer to a static descriptor, or `nullptr` for an empty key or an unrecognized
|
||||
algorithm. Reuse it for multiple checks within that function. Key-based helpers
|
||||
resolve the algorithm internally. Keep RSA distinct from RSA-PSS, and EC distinct
|
||||
from SM2.
|
||||
|
||||
`key.rawSeed()` validates seed support and extracts the seed in one operation.
|
||||
Its result distinguishes an unsupported key type from an unavailable seed.
|
||||
OKP and AKP JWKs share ncrypto's `key.exportRawJwk()` and
|
||||
`EVPKeyPointer::NewRawJwk()` operations. Export returns the recognized algorithm
|
||||
and public/private bytes, selecting seed versus raw private material internally.
|
||||
Import checks that public bytes match the supplied private material. Node.js
|
||||
handles the distinct JWK field names, input-name validation, base64url encoding,
|
||||
JavaScript objects, and errors. RSA and EC retain their component and coordinate
|
||||
representations; ncrypto handles prime-product validation and affine-coordinate
|
||||
extraction through `Rsa` and `Ec`.
|
||||
|
||||
`Ec::GetCurveId()`, `GetKeyComponents()`, and the raw-export helpers read
|
||||
provider parameters directly where supported, with backend adaptation in
|
||||
ncrypto. Keep these operations on the `EVPKeyPointer` when an `ECKeyPointer`
|
||||
reconstruction is unnecessary.
|
||||
|
||||
`key.getKeyTypeName()` returns the descriptor's lowercase public key-type name,
|
||||
or `nullptr` when the key has no recognized public type. For example, ML-DSA-44
|
||||
returns `ml-dsa-44`, while SM2 has no public key-type name. These names live in
|
||||
ncrypto alongside the algorithm metadata.
|
||||
|
||||
For construction, use `EVPKeyCtxPointer::NewFromAlgorithm()` with a descriptor,
|
||||
or `NewFromName()` when a backend algorithm name is needed.
|
||||
`EVPKeyPointer::NewRawPublic()`, `NewRawPrivate()`, and `NewRawSeed()` take
|
||||
descriptors too. Use the key's capability helpers for raw formats and signature
|
||||
contexts; recognizing an algorithm does not establish support for an operation.
|
||||
|
||||
`KeyAlgorithm::FromName()` looks up known canonical names case-insensitively
|
||||
using the same `CaseInsensitiveNameEqual` as the digest, cipher, and MAC caches.
|
||||
It does not resolve arbitrary provider aliases or establish availability.
|
||||
`isAvailable()` checks whether the backend can create a context for the algorithm.
|
||||
Public input validation remains specific to each API: PQC JWK `alg` values use
|
||||
exact canonical names such as `ML-DSA-44`, while raw imports require exact public
|
||||
`asymmetricKeyType` values such as `ml-dsa-44`.
|
||||
|
||||
The internal JavaScript binding exposes `getPqcKeyTypes()` for the available
|
||||
known PQC algorithm names in their canonical spelling. Named key generation
|
||||
passes algorithm names to `NamedKeyPairGenJob`, which resolves the name to a static
|
||||
`KeyAlgorithm` descriptor. Asymmetric key IDs are not exposed to JavaScript.
|
||||
|
||||
Real EC curve and ASN.1/OID NIDs still have their own uses. The EC generation
|
||||
path keeps Ed/X algorithm descriptors separate from curve NIDs while preserving
|
||||
the existing accepted curve-name aliases.
|
||||
|
||||
#### `KeyObjectData`
|
||||
|
||||
`KeyObjectData` is an internal thread-safe structure used to wrap either
|
||||
@@ -326,9 +387,10 @@ They perform their actions immediately.
|
||||
|
||||
```js
|
||||
// Example synchronous single-call operation
|
||||
const { timingSafeEqual } = require('node:crypto');
|
||||
const a = new Uint8Array(10);
|
||||
const b = new Uint8Array(10);
|
||||
crypto.timingSafeEqual(a, b);
|
||||
timingSafeEqual(a, b);
|
||||
```
|
||||
|
||||
Asynchronous single-call operations generally perform a
|
||||
@@ -337,8 +399,10 @@ defer the actual crypto-operation work to the libuv threadpool.
|
||||
|
||||
```js
|
||||
// Example asynchronous single-call operation
|
||||
const { randomFill } = require('node:crypto');
|
||||
const buf = new Uint8Array(10);
|
||||
crypto.randomFill(buf, (err, buf) => {
|
||||
randomFill(buf, (err, buf) => {
|
||||
if (err) throw err;
|
||||
console.log(buf);
|
||||
});
|
||||
```
|
||||
@@ -353,12 +417,12 @@ all asynchronous single-call operations are Promise-based.
|
||||
// Example Web Crypto API asynchronous single-call operation
|
||||
const { subtle } = globalThis.crypto;
|
||||
|
||||
subtle.generateKeys({ name: 'HMAC', length: 256 }, true, ['sign'])
|
||||
subtle.generateKey({ name: 'HMAC', hash: 'SHA-256', length: 256 }, true, ['sign'])
|
||||
.then((key) => {
|
||||
console.log(key);
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error('an error occurred');
|
||||
console.error('an error occurred', error);
|
||||
});
|
||||
```
|
||||
|
||||
@@ -372,12 +436,12 @@ can be performed over time.
|
||||
|
||||
```js
|
||||
// Example stream-oriented operation
|
||||
const hash = crypto.createHash('sha256');
|
||||
let updates = 10;
|
||||
const { createHash } = require('node:crypto');
|
||||
const hash = createHash('sha256');
|
||||
setTimeout(() => {
|
||||
hash.update('hello world');
|
||||
setTimeout(() => {
|
||||
console.log(hash.digest();)
|
||||
console.log(hash.digest());
|
||||
}, 1000);
|
||||
}, 1000);
|
||||
```
|
||||
+16
-20
@@ -29,6 +29,7 @@ namespace node {
|
||||
|
||||
using ncrypto::ClearErrorOnReturn;
|
||||
using ncrypto::EVPKeyPointer;
|
||||
using ncrypto::KeyAlgorithm;
|
||||
using ncrypto::SSLPointer;
|
||||
using ncrypto::SSLSessionPointer;
|
||||
using ncrypto::StackOfX509;
|
||||
@@ -217,31 +218,26 @@ MaybeLocal<Object> GetEphemeralKey(Environment* env, const SSLPointer& ssl) {
|
||||
|
||||
bool found = false;
|
||||
if (EVPKeyPointer key = ssl.getPeerTempKey()) {
|
||||
int kid = key.id();
|
||||
switch (kid) {
|
||||
case EVP_PKEY_DH: {
|
||||
values[0] = env->dh_string();
|
||||
values[2] = Integer::New(env->isolate(), key.bits());
|
||||
found = true;
|
||||
break;
|
||||
const auto* algorithm = key.getAlgorithm();
|
||||
if (algorithm == &KeyAlgorithm::DH) {
|
||||
values[0] = env->dh_string();
|
||||
values[2] = Integer::New(env->isolate(), key.bits());
|
||||
found = true;
|
||||
} else if (algorithm == &KeyAlgorithm::EC ||
|
||||
algorithm == &KeyAlgorithm::X25519 ||
|
||||
algorithm == &KeyAlgorithm::X448) {
|
||||
const char* curve_name = nullptr;
|
||||
if (algorithm == &KeyAlgorithm::EC) {
|
||||
const int nid = ncrypto::Ec::GetCurveId(key);
|
||||
if (nid != NID_undef) curve_name = OBJ_nid2sn(nid);
|
||||
} else {
|
||||
curve_name = algorithm->name();
|
||||
}
|
||||
case EVP_PKEY_EC:
|
||||
case EVP_PKEY_X25519:
|
||||
case EVP_PKEY_X448: {
|
||||
const char* curve_name;
|
||||
if (kid == EVP_PKEY_EC) {
|
||||
int nid = ncrypto::Ec::GetCurveId(key);
|
||||
if (nid == NID_undef) break;
|
||||
curve_name = OBJ_nid2sn(nid);
|
||||
} else {
|
||||
curve_name = OBJ_nid2sn(kid);
|
||||
}
|
||||
if (curve_name == nullptr) break;
|
||||
if (curve_name != nullptr) {
|
||||
values[0] = env->ecdh_string();
|
||||
values[1] = OneByteString(env->isolate(), curve_name);
|
||||
values[2] = Integer::New(env->isolate(), key.bits());
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,6 +41,7 @@ using ncrypto::Digest;
|
||||
using ncrypto::EnginePointer;
|
||||
#endif // !OPENSSL_NO_ENGINE
|
||||
using ncrypto::EVPKeyPointer;
|
||||
using ncrypto::KeyAlgorithm;
|
||||
using ncrypto::MarkPopErrorOnReturn;
|
||||
using ncrypto::SSLPointer;
|
||||
using ncrypto::StackOfX509;
|
||||
@@ -2032,7 +2033,7 @@ void SecureContext::SetDHParam(const FunctionCallbackInfo<Value>& args) {
|
||||
|
||||
#if NCRYPTO_USE_OPENSSL3_PROVIDER
|
||||
EVPKeyPointer params(PEM_read_bio_Parameters(bio.get(), nullptr));
|
||||
if (params && params.id() == EVP_PKEY_DH) dh.reset(params.release());
|
||||
if (params && params.isA(KeyAlgorithm::DH)) dh.reset(params.release());
|
||||
#else
|
||||
dh.reset(PEM_read_bio_DHparams(bio.get(), nullptr, nullptr, nullptr));
|
||||
#endif
|
||||
|
||||
@@ -21,6 +21,7 @@ using ncrypto::DataPointer;
|
||||
using ncrypto::DHPointer;
|
||||
using ncrypto::EVPKeyCtxPointer;
|
||||
using ncrypto::EVPKeyPointer;
|
||||
using ncrypto::KeyAlgorithm;
|
||||
using v8::ArrayBuffer;
|
||||
using v8::BackingStoreInitializationMode;
|
||||
using v8::BackingStoreOnFailureMode;
|
||||
@@ -459,17 +460,13 @@ EVPKeyCtxPointer DhKeyGenTraits::Setup(DhKeyPairGenConfig* params) {
|
||||
|
||||
key_params = EVPKeyPointer::NewDH(std::move(dh));
|
||||
} else if (int* prime_size = std::get_if<int>(¶ms->params.prime)) {
|
||||
auto param_ctx = EVPKeyCtxPointer::NewFromID(EVP_PKEY_DH);
|
||||
#ifndef OPENSSL_IS_BORINGSSL
|
||||
auto param_ctx = EVPKeyCtxPointer::NewFromAlgorithm(KeyAlgorithm::DH);
|
||||
if (!param_ctx.initForParamgen() ||
|
||||
!param_ctx.setDhParameters(*prime_size, params->params.generator)) {
|
||||
return {};
|
||||
}
|
||||
|
||||
key_params = param_ctx.paramgen();
|
||||
#else
|
||||
return {};
|
||||
#endif
|
||||
} else {
|
||||
UNREACHABLE();
|
||||
}
|
||||
@@ -559,7 +556,7 @@ bool DHBitsTraits::DeriveBits(Environment* env,
|
||||
bool GetDhKeyDetail(Environment* env,
|
||||
const KeyObjectData& key,
|
||||
Local<Object> target) {
|
||||
CHECK_EQ(key.GetAsymmetricKey().id(), EVP_PKEY_DH);
|
||||
DCHECK(key.GetAsymmetricKey().isA(KeyAlgorithm::DH));
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -16,6 +16,7 @@ namespace node {
|
||||
|
||||
using ncrypto::Dsa;
|
||||
using ncrypto::EVPKeyCtxPointer;
|
||||
using ncrypto::KeyAlgorithm;
|
||||
using v8::FunctionCallbackInfo;
|
||||
using v8::Int32;
|
||||
using v8::JustVoid;
|
||||
@@ -28,7 +29,7 @@ using v8::Value;
|
||||
|
||||
namespace crypto {
|
||||
EVPKeyCtxPointer DsaKeyGenTraits::Setup(DsaKeyPairGenConfig* params) {
|
||||
auto param_ctx = EVPKeyCtxPointer::NewFromID(EVP_PKEY_DSA);
|
||||
auto param_ctx = EVPKeyCtxPointer::NewFromAlgorithm(KeyAlgorithm::DSA);
|
||||
|
||||
if (!param_ctx || !param_ctx.initForParamgen() ||
|
||||
!param_ctx.setDsaParameters(
|
||||
|
||||
+27
-159
@@ -6,7 +6,6 @@
|
||||
#include "env-inl.h"
|
||||
#include "memory_tracker-inl.h"
|
||||
#include "node_buffer.h"
|
||||
#include "string_bytes.h"
|
||||
#include "threadpoolwork-inl.h"
|
||||
#include "v8.h"
|
||||
|
||||
@@ -26,6 +25,7 @@ using ncrypto::ECKeyPointer;
|
||||
using ncrypto::ECPointPointer;
|
||||
using ncrypto::EVPKeyCtxPointer;
|
||||
using ncrypto::EVPKeyPointer;
|
||||
using ncrypto::KeyAlgorithm;
|
||||
using ncrypto::MarkPopErrorOnReturn;
|
||||
using v8::Array;
|
||||
using v8::ArrayBuffer;
|
||||
@@ -411,29 +411,18 @@ void ECDH::ConvertKey(const FunctionCallbackInfo<Value>& args) {
|
||||
|
||||
EVPKeyCtxPointer EcKeyGenTraits::Setup(EcKeyPairGenConfig* params) {
|
||||
EVPKeyCtxPointer key_ctx;
|
||||
switch (params->params.curve_nid) {
|
||||
case EVP_PKEY_ED25519:
|
||||
// Fall through
|
||||
case EVP_PKEY_ED448:
|
||||
// Fall through
|
||||
case EVP_PKEY_X25519:
|
||||
// Fall through
|
||||
case EVP_PKEY_X448:
|
||||
key_ctx = EVPKeyCtxPointer::NewFromID(params->params.curve_nid);
|
||||
break;
|
||||
default: {
|
||||
auto param_ctx = EVPKeyCtxPointer::NewFromID(EVP_PKEY_EC);
|
||||
if (!param_ctx.initForParamgen() ||
|
||||
!param_ctx.setEcParameters(params->params.curve_nid,
|
||||
params->params.param_encoding)) {
|
||||
return {};
|
||||
}
|
||||
|
||||
auto key_params = param_ctx.paramgen();
|
||||
if (!key_params) return {};
|
||||
|
||||
key_ctx = key_params.newCtx();
|
||||
if (params->params.algorithm != nullptr) {
|
||||
key_ctx = EVPKeyCtxPointer::NewFromAlgorithm(*params->params.algorithm);
|
||||
} else {
|
||||
auto param_ctx = EVPKeyCtxPointer::NewFromAlgorithm(KeyAlgorithm::EC);
|
||||
if (!param_ctx.initForParamgen() ||
|
||||
!param_ctx.setEcParameters(params->params.curve_nid,
|
||||
params->params.param_encoding)) {
|
||||
return {};
|
||||
}
|
||||
auto key_params = param_ctx.paramgen();
|
||||
if (!key_params) return {};
|
||||
key_ctx = key_params.newCtx();
|
||||
}
|
||||
|
||||
if (!key_ctx.initForKeygen()) return {};
|
||||
@@ -459,10 +448,13 @@ Maybe<void> EcKeyGenTraits::AdditionalConfig(
|
||||
CHECK(args[*offset]->IsString()); // curve name
|
||||
|
||||
Utf8Value curve_name(env->isolate(), args[*offset]);
|
||||
params->params.curve_nid = Ec::GetCurveIdFromName(*curve_name);
|
||||
if (params->params.curve_nid == NID_undef) {
|
||||
THROW_ERR_CRYPTO_INVALID_CURVE(env);
|
||||
return Nothing<void>();
|
||||
params->params.algorithm = ncrypto::Ec::GetNamedKeyAlgorithm(*curve_name);
|
||||
if (params->params.algorithm == nullptr) {
|
||||
params->params.curve_nid = Ec::GetCurveIdFromName(*curve_name);
|
||||
if (params->params.curve_nid == NID_undef) {
|
||||
THROW_ERR_CRYPTO_INVALID_CURVE(env);
|
||||
return Nothing<void>();
|
||||
}
|
||||
}
|
||||
|
||||
// param encoding
|
||||
@@ -491,7 +483,7 @@ WebCryptoKeyExportStatus EC_Raw_Export(const KeyObjectData& key_data,
|
||||
CHECK(m_pkey);
|
||||
Mutex::ScopedLock lock(key_data.mutex());
|
||||
|
||||
if (m_pkey.id() != EVP_PKEY_EC) {
|
||||
if (!m_pkey.isA(KeyAlgorithm::EC)) {
|
||||
switch (key_data.GetKeyType()) {
|
||||
case kKeyTypePrivate: {
|
||||
auto data = m_pkey.rawPrivateKey();
|
||||
@@ -569,7 +561,7 @@ WebCryptoKeyExportStatus ECKeyExportTraits::DoExport(
|
||||
return WebCryptoKeyExportStatus::INVALID_KEY_TYPE;
|
||||
|
||||
const auto& m_pkey = key_data.GetAsymmetricKey();
|
||||
if (m_pkey.id() != EVP_PKEY_EC) {
|
||||
if (!m_pkey.isA(KeyAlgorithm::EC)) {
|
||||
return PKEY_SPKI_Export(key_data, out);
|
||||
} else {
|
||||
// Ensure exported key is in uncompressed point format.
|
||||
@@ -623,7 +615,7 @@ bool ExportJWKEcKey(Environment* env,
|
||||
Local<Object> target) {
|
||||
Mutex::ScopedLock lock(key.mutex());
|
||||
const auto& m_pkey = key.GetAsymmetricKey();
|
||||
CHECK_EQ(m_pkey.id(), EVP_PKEY_EC);
|
||||
DCHECK(m_pkey.isA(KeyAlgorithm::EC));
|
||||
|
||||
BignumPointer x;
|
||||
BignumPointer y;
|
||||
@@ -647,18 +639,10 @@ bool ExportJWKEcKey(Environment* env,
|
||||
return false;
|
||||
}
|
||||
|
||||
if (SetEncodedValue(
|
||||
env,
|
||||
target,
|
||||
env->jwk_x_string(),
|
||||
x.get(),
|
||||
degree_bytes).IsNothing() ||
|
||||
SetEncodedValue(
|
||||
env,
|
||||
target,
|
||||
env->jwk_y_string(),
|
||||
y.get(),
|
||||
degree_bytes).IsNothing()) {
|
||||
if (SetEncodedValue(env, target, env->jwk_x_string(), x.get(), degree_bytes)
|
||||
.IsNothing() ||
|
||||
SetEncodedValue(env, target, env->jwk_y_string(), y.get(), degree_bytes)
|
||||
.IsNothing()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -698,122 +682,6 @@ bool ExportJWKEcKey(Environment* env,
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ExportJWKEdKey(Environment* env,
|
||||
const KeyObjectData& key,
|
||||
Local<Object> target) {
|
||||
Mutex::ScopedLock lock(key.mutex());
|
||||
const auto& pkey = key.GetAsymmetricKey();
|
||||
|
||||
const char* curve = ([&] {
|
||||
switch (pkey.id()) {
|
||||
case EVP_PKEY_ED25519:
|
||||
return "Ed25519";
|
||||
case EVP_PKEY_ED448:
|
||||
return "Ed448";
|
||||
case EVP_PKEY_X25519:
|
||||
return "X25519";
|
||||
case EVP_PKEY_X448:
|
||||
return "X448";
|
||||
default:
|
||||
UNREACHABLE();
|
||||
}
|
||||
})();
|
||||
|
||||
static constexpr auto trySetKey = [](Environment* env,
|
||||
DataPointer data,
|
||||
Local<Object> target,
|
||||
Local<String> key) {
|
||||
Local<Value> encoded;
|
||||
if (!data) return false;
|
||||
const ncrypto::Buffer<const char> out = data;
|
||||
return StringBytes::Encode(env->isolate(), out.data, out.len, BASE64URL)
|
||||
.ToLocal(&encoded) &&
|
||||
target->DefineOwnProperty(env->context(), key, encoded)
|
||||
.FromMaybe(false);
|
||||
};
|
||||
|
||||
return !(
|
||||
!target
|
||||
->DefineOwnProperty(env->context(),
|
||||
env->jwk_crv_string(),
|
||||
OneByteString(env->isolate(), curve))
|
||||
.FromMaybe(false) ||
|
||||
(key.GetKeyType() == kKeyTypePrivate &&
|
||||
!trySetKey(env, pkey.rawPrivateKey(), target, env->jwk_d_string())) ||
|
||||
!trySetKey(env, pkey.rawPublicKey(), target, env->jwk_x_string()) ||
|
||||
!target
|
||||
->DefineOwnProperty(
|
||||
env->context(), env->jwk_kty_string(), env->jwk_okp_string())
|
||||
.FromMaybe(false));
|
||||
}
|
||||
KeyObjectData ImportJWKEdKey(Environment* env, Local<Object> jwk) {
|
||||
Local<Value> crv_value;
|
||||
Local<Value> x_value;
|
||||
Local<Value> d_value;
|
||||
|
||||
if (!jwk->Get(env->context(), env->jwk_crv_string()).ToLocal(&crv_value) ||
|
||||
!jwk->Get(env->context(), env->jwk_x_string()).ToLocal(&x_value) ||
|
||||
!jwk->Get(env->context(), env->jwk_d_string()).ToLocal(&d_value)) {
|
||||
return {};
|
||||
}
|
||||
|
||||
if (!crv_value->IsString() || !x_value->IsString() ||
|
||||
(!d_value->IsUndefined() && !d_value->IsString())) {
|
||||
THROW_ERR_CRYPTO_INVALID_JWK(env, "Invalid JWK OKP key");
|
||||
return {};
|
||||
}
|
||||
|
||||
Utf8Value crv(env->isolate(), crv_value.As<String>());
|
||||
|
||||
static constexpr struct {
|
||||
const char* name;
|
||||
int nid;
|
||||
} kCurveToNid[] = {
|
||||
{"Ed25519", EVP_PKEY_ED25519},
|
||||
{"Ed448", EVP_PKEY_ED448},
|
||||
{"X25519", EVP_PKEY_X25519},
|
||||
{"X448", EVP_PKEY_X448},
|
||||
};
|
||||
|
||||
int id = NID_undef;
|
||||
for (const auto& entry : kCurveToNid) {
|
||||
if (strcmp(*crv, entry.name) == 0) {
|
||||
id = entry.nid;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (id == NID_undef) {
|
||||
THROW_ERR_CRYPTO_INVALID_JWK(env, "Invalid JWK OKP key");
|
||||
return {};
|
||||
}
|
||||
|
||||
KeyType type = d_value->IsString() ? kKeyTypePrivate : kKeyTypePublic;
|
||||
|
||||
ByteSource raw;
|
||||
if (type == kKeyTypePrivate) {
|
||||
raw = ByteSource::FromEncodedString(env, d_value.As<String>());
|
||||
} else {
|
||||
raw = ByteSource::FromEncodedString(env, x_value.As<String>());
|
||||
}
|
||||
|
||||
typedef EVPKeyPointer (*new_key_fn)(
|
||||
int, const ncrypto::Buffer<const unsigned char>&);
|
||||
new_key_fn fn = type == kKeyTypePrivate ? EVPKeyPointer::NewRawPrivate
|
||||
: EVPKeyPointer::NewRawPublic;
|
||||
|
||||
auto pkey = fn(id,
|
||||
ncrypto::Buffer<const unsigned char>{
|
||||
.data = raw.data<const unsigned char>(),
|
||||
.len = raw.size(),
|
||||
});
|
||||
if (!pkey) {
|
||||
THROW_ERR_CRYPTO_INVALID_JWK(env, "Invalid JWK OKP key");
|
||||
return {};
|
||||
}
|
||||
|
||||
return KeyObjectData::CreateAsymmetric(type, std::move(pkey));
|
||||
}
|
||||
KeyObjectData ImportJWKEcKey(Environment* env, Local<Object> jwk) {
|
||||
Local<Value> crv_value;
|
||||
if (!jwk->Get(env->context(), env->jwk_crv_string()).ToLocal(&crv_value) ||
|
||||
@@ -892,7 +760,7 @@ bool GetEcKeyDetail(Environment* env,
|
||||
Local<Object> target) {
|
||||
Mutex::ScopedLock lock(key.mutex());
|
||||
const auto& m_pkey = key.GetAsymmetricKey();
|
||||
CHECK_EQ(m_pkey.id(), EVP_PKEY_EC);
|
||||
DCHECK(m_pkey.isA(KeyAlgorithm::EC));
|
||||
|
||||
int nid = Ec::GetCurveId(m_pkey);
|
||||
if (nid == NID_undef) return true;
|
||||
|
||||
@@ -59,7 +59,8 @@ class ECDH final : public BaseObject {
|
||||
};
|
||||
|
||||
struct EcKeyPairParams final : public MemoryRetainer {
|
||||
int curve_nid;
|
||||
const ncrypto::KeyAlgorithm* algorithm = nullptr;
|
||||
int curve_nid = NID_undef;
|
||||
int param_encoding;
|
||||
SET_NO_MEMORY_INFO()
|
||||
SET_MEMORY_INFO_NAME(EcKeyPairParams)
|
||||
@@ -113,12 +114,6 @@ bool ExportJWKEcKey(Environment* env,
|
||||
const KeyObjectData& key,
|
||||
v8::Local<v8::Object> target);
|
||||
|
||||
bool ExportJWKEdKey(Environment* env,
|
||||
const KeyObjectData& key,
|
||||
v8::Local<v8::Object> target);
|
||||
|
||||
KeyObjectData ImportJWKEdKey(Environment* env, v8::Local<v8::Object> jwk);
|
||||
|
||||
KeyObjectData ImportJWKEcKey(Environment* env, v8::Local<v8::Object> jwk);
|
||||
|
||||
bool GetEcKeyDetail(Environment* env,
|
||||
|
||||
+12
-11
@@ -15,7 +15,6 @@ namespace node {
|
||||
using ncrypto::DataPointer;
|
||||
using ncrypto::EVPKeyCtxPointer;
|
||||
using v8::FunctionCallbackInfo;
|
||||
using v8::Int32;
|
||||
using v8::JustVoid;
|
||||
using v8::Local;
|
||||
using v8::Maybe;
|
||||
@@ -25,30 +24,32 @@ using v8::Uint32;
|
||||
using v8::Value;
|
||||
|
||||
namespace crypto {
|
||||
// NidKeyPairGenJob input arguments:
|
||||
// NamedKeyPairGenJob input arguments:
|
||||
// 1. CryptoJobMode
|
||||
// 2. NID
|
||||
// 2. Algorithm name
|
||||
// 3. Public Format
|
||||
// 4. Public Type
|
||||
// 5. Private Format
|
||||
// 6. Private Type
|
||||
// 7. Cipher
|
||||
// 8. Passphrase
|
||||
Maybe<void> NidKeyPairGenTraits::AdditionalConfig(
|
||||
Maybe<void> NamedKeyPairGenTraits::AdditionalConfig(
|
||||
CryptoJobMode mode,
|
||||
const FunctionCallbackInfo<Value>& args,
|
||||
unsigned int* offset,
|
||||
NidKeyPairGenConfig* params) {
|
||||
CHECK(args[*offset]->IsInt32());
|
||||
params->params.id = args[*offset].As<Int32>()->Value();
|
||||
NamedKeyPairGenConfig* params) {
|
||||
CHECK(args[*offset]->IsString());
|
||||
Utf8Value name(args.GetIsolate(), args[*offset]);
|
||||
params->params.algorithm = ncrypto::KeyAlgorithm::FromName(*name);
|
||||
CHECK_NOT_NULL(params->params.algorithm);
|
||||
|
||||
*offset += 1;
|
||||
|
||||
return JustVoid();
|
||||
}
|
||||
|
||||
EVPKeyCtxPointer NidKeyPairGenTraits::Setup(NidKeyPairGenConfig* params) {
|
||||
auto ctx = EVPKeyCtxPointer::NewFromID(params->params.id);
|
||||
EVPKeyCtxPointer NamedKeyPairGenTraits::Setup(NamedKeyPairGenConfig* params) {
|
||||
auto ctx = EVPKeyCtxPointer::NewFromAlgorithm(*params->params.algorithm);
|
||||
if (!ctx || !ctx.initForKeygen()) return {};
|
||||
return ctx;
|
||||
}
|
||||
@@ -96,12 +97,12 @@ MaybeLocal<Value> SecretKeyGenTraits::EncodeKey(Environment* env,
|
||||
|
||||
namespace Keygen {
|
||||
void Initialize(Environment* env, Local<Object> target) {
|
||||
NidKeyPairGenJob::Initialize(env, target);
|
||||
NamedKeyPairGenJob::Initialize(env, target);
|
||||
SecretKeyGenJob::Initialize(env, target);
|
||||
}
|
||||
|
||||
void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
|
||||
NidKeyPairGenJob::RegisterExternalReferences(registry);
|
||||
NamedKeyPairGenJob::RegisterExternalReferences(registry);
|
||||
SecretKeyGenJob::RegisterExternalReferences(registry);
|
||||
}
|
||||
} // namespace Keygen
|
||||
|
||||
+11
-11
@@ -352,29 +352,29 @@ struct KeyPairGenConfig final : public MemoryRetainer {
|
||||
SET_SELF_SIZE(KeyPairGenConfig)
|
||||
};
|
||||
|
||||
struct NidKeyPairParams final : public MemoryRetainer {
|
||||
int id;
|
||||
struct NamedKeyPairParams final : public MemoryRetainer {
|
||||
const ncrypto::KeyAlgorithm* algorithm = nullptr;
|
||||
SET_NO_MEMORY_INFO()
|
||||
SET_MEMORY_INFO_NAME(NidKeyPairParams)
|
||||
SET_SELF_SIZE(NidKeyPairParams)
|
||||
SET_MEMORY_INFO_NAME(NamedKeyPairParams)
|
||||
SET_SELF_SIZE(NamedKeyPairParams)
|
||||
};
|
||||
|
||||
using NidKeyPairGenConfig = KeyPairGenConfig<NidKeyPairParams>;
|
||||
using NamedKeyPairGenConfig = KeyPairGenConfig<NamedKeyPairParams>;
|
||||
|
||||
struct NidKeyPairGenTraits final {
|
||||
using AdditionalParameters = NidKeyPairGenConfig;
|
||||
static constexpr const char* JobName = "NidKeyPairGenJob";
|
||||
struct NamedKeyPairGenTraits final {
|
||||
using AdditionalParameters = NamedKeyPairGenConfig;
|
||||
static constexpr const char* JobName = "NamedKeyPairGenJob";
|
||||
|
||||
static ncrypto::EVPKeyCtxPointer Setup(NidKeyPairGenConfig* params);
|
||||
static ncrypto::EVPKeyCtxPointer Setup(NamedKeyPairGenConfig* params);
|
||||
|
||||
static v8::Maybe<void> AdditionalConfig(
|
||||
CryptoJobMode mode,
|
||||
const v8::FunctionCallbackInfo<v8::Value>& args,
|
||||
unsigned int* offset,
|
||||
NidKeyPairGenConfig* params);
|
||||
NamedKeyPairGenConfig* params);
|
||||
};
|
||||
|
||||
using NidKeyPairGenJob = KeyGenJob<KeyPairGenTraits<NidKeyPairGenTraits>>;
|
||||
using NamedKeyPairGenJob = KeyGenJob<KeyPairGenTraits<NamedKeyPairGenTraits>>;
|
||||
using SecretKeyGenJob = KeyGenJob<SecretKeyGenTraits>;
|
||||
} // namespace node::crypto
|
||||
|
||||
|
||||
+203
-272
@@ -5,7 +5,6 @@
|
||||
#include "crypto/crypto_dh.h"
|
||||
#include "crypto/crypto_dsa.h"
|
||||
#include "crypto/crypto_ec.h"
|
||||
#include "crypto/crypto_pqc.h"
|
||||
#include "crypto/crypto_rsa.h"
|
||||
#include "crypto/crypto_util.h"
|
||||
#include "env-inl.h"
|
||||
@@ -26,6 +25,7 @@ using ncrypto::ECKeyPointer;
|
||||
using ncrypto::ECPointPointer;
|
||||
using ncrypto::EVPKeyCtxPointer;
|
||||
using ncrypto::EVPKeyPointer;
|
||||
using ncrypto::KeyAlgorithm;
|
||||
using ncrypto::MarkPopErrorOnReturn;
|
||||
using ncrypto::PKCS8Pointer;
|
||||
using v8::Array;
|
||||
@@ -178,28 +178,128 @@ KeyObjectData ImportJWKSecretKey(Environment* env, Local<Object> jwk) {
|
||||
ByteSource::FromEncodedString(env, key.As<String>()));
|
||||
}
|
||||
|
||||
static bool ExportJWKRawKey(Environment* env,
|
||||
const KeyObjectData& key,
|
||||
Local<Object> target) {
|
||||
Mutex::ScopedLock lock(key.mutex());
|
||||
auto result =
|
||||
key.GetAsymmetricKey().exportRawJwk(key.GetKeyType() == kKeyTypePrivate);
|
||||
if (!result) {
|
||||
if (result.error == EVPKeyPointer::RawExportError::MISSING_SEED) {
|
||||
THROW_ERR_CRYPTO_OPERATION_FAILED(env,
|
||||
"key does not have an available seed");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
const auto& data = result.value;
|
||||
const bool is_akp = data.algorithm->isPqc();
|
||||
const auto name_field =
|
||||
is_akp ? env->jwk_alg_string() : env->jwk_crv_string();
|
||||
const auto public_field =
|
||||
is_akp ? env->jwk_pub_string() : env->jwk_x_string();
|
||||
const auto private_field =
|
||||
is_akp ? env->jwk_priv_string() : env->jwk_d_string();
|
||||
const auto kty = is_akp ? env->jwk_akp_string() : env->jwk_okp_string();
|
||||
const auto context = env->context();
|
||||
const auto set_key = [&](const ncrypto::DataPointer& bytes,
|
||||
Local<String> field) {
|
||||
Local<Value> encoded;
|
||||
return StringBytes::Encode(
|
||||
env->isolate(), bytes.get<const char>(), bytes.size(), BASE64URL)
|
||||
.ToLocal(&encoded) &&
|
||||
target->DefineOwnProperty(context, field, encoded).FromMaybe(false);
|
||||
};
|
||||
return target->DefineOwnProperty(context, env->jwk_kty_string(), kty)
|
||||
.FromMaybe(false) &&
|
||||
target
|
||||
->DefineOwnProperty(
|
||||
context,
|
||||
name_field,
|
||||
OneByteString(env->isolate(), data.algorithm->name()))
|
||||
.FromMaybe(false) &&
|
||||
set_key(data.public_key, public_field) &&
|
||||
(!data.private_key || set_key(data.private_key, private_field));
|
||||
}
|
||||
|
||||
static KeyObjectData ImportJWKRawKey(Environment* env,
|
||||
Local<Object> jwk,
|
||||
bool is_akp) {
|
||||
const auto name_field =
|
||||
is_akp ? env->jwk_alg_string() : env->jwk_crv_string();
|
||||
const auto public_field =
|
||||
is_akp ? env->jwk_pub_string() : env->jwk_x_string();
|
||||
const auto private_field =
|
||||
is_akp ? env->jwk_priv_string() : env->jwk_d_string();
|
||||
const char* invalid_key =
|
||||
is_akp ? "Invalid JWK AKP key" : "Invalid JWK OKP key";
|
||||
Local<Value> name_value;
|
||||
Local<Value> public_value;
|
||||
Local<Value> private_value;
|
||||
if (!jwk->Get(env->context(), name_field).ToLocal(&name_value) ||
|
||||
!jwk->Get(env->context(), public_field).ToLocal(&public_value) ||
|
||||
!jwk->Get(env->context(), private_field).ToLocal(&private_value)) {
|
||||
return {};
|
||||
}
|
||||
const bool valid_material =
|
||||
public_value->IsString() &&
|
||||
(private_value->IsUndefined() || private_value->IsString());
|
||||
// OKP validates the field types first; AKP validates its algorithm first.
|
||||
if (!is_akp && (!name_value->IsString() || !valid_material)) {
|
||||
THROW_ERR_CRYPTO_INVALID_JWK(env, invalid_key);
|
||||
return {};
|
||||
}
|
||||
Utf8Value name(env->isolate(),
|
||||
name_value->IsString() ? name_value.As<String>()
|
||||
: String::Empty(env->isolate()));
|
||||
const auto* algorithm = KeyAlgorithm::FromName(*name);
|
||||
if (algorithm == nullptr ||
|
||||
(is_akp ? !algorithm->isPqc() || !algorithm->isAvailable()
|
||||
: !algorithm->isOkp()) ||
|
||||
strcmp(*name, algorithm->name()) != 0) {
|
||||
THROW_ERR_CRYPTO_INVALID_JWK(
|
||||
env, is_akp ? "Unsupported JWK AKP \"alg\"" : invalid_key);
|
||||
return {};
|
||||
}
|
||||
if (!valid_material) {
|
||||
THROW_ERR_CRYPTO_INVALID_JWK(env, invalid_key);
|
||||
return {};
|
||||
}
|
||||
ByteSource private_bytes;
|
||||
std::optional<ncrypto::Buffer<const unsigned char>> private_key;
|
||||
const KeyType type =
|
||||
private_value->IsString() ? kKeyTypePrivate : kKeyTypePublic;
|
||||
if (type == kKeyTypePrivate) {
|
||||
private_bytes =
|
||||
ByteSource::FromEncodedString(env, private_value.As<String>());
|
||||
private_key = ncrypto::Buffer<const unsigned char>{
|
||||
private_bytes.data<const unsigned char>(), private_bytes.size()};
|
||||
}
|
||||
const auto public_bytes =
|
||||
ByteSource::FromEncodedString(env, public_value.As<String>());
|
||||
auto pkey = EVPKeyPointer::NewRawJwk(
|
||||
*algorithm,
|
||||
{public_bytes.data<const unsigned char>(), public_bytes.size()},
|
||||
private_key);
|
||||
if (!pkey) {
|
||||
THROW_ERR_CRYPTO_INVALID_JWK(env, invalid_key);
|
||||
return {};
|
||||
}
|
||||
return KeyObjectData::CreateAsymmetric(type, std::move(pkey));
|
||||
}
|
||||
|
||||
bool ExportJWKAsymmetricKey(Environment* env,
|
||||
const KeyObjectData& key,
|
||||
Local<Object> target,
|
||||
bool handleRsaPss) {
|
||||
const int id = key.GetAsymmetricKey().id();
|
||||
#if OPENSSL_WITH_PQC
|
||||
if (IsPqcKeyId(id)) return ExportJwkPqcKey(env, key, target);
|
||||
#endif
|
||||
switch (id) {
|
||||
case EVP_PKEY_RSA_PSS: {
|
||||
if (handleRsaPss) return ExportJWKRsaKey(env, key, target);
|
||||
break;
|
||||
}
|
||||
case EVP_PKEY_RSA:
|
||||
return ExportJWKRsaKey(env, key, target);
|
||||
case EVP_PKEY_EC:
|
||||
return ExportJWKEcKey(env, key, target);
|
||||
case EVP_PKEY_ED25519:
|
||||
case EVP_PKEY_ED448:
|
||||
case EVP_PKEY_X25519:
|
||||
case EVP_PKEY_X448:
|
||||
return ExportJWKEdKey(env, key, target);
|
||||
const auto& pkey = key.GetAsymmetricKey();
|
||||
const auto* algorithm = pkey.getAlgorithm();
|
||||
if (algorithm == &KeyAlgorithm::RSA ||
|
||||
(handleRsaPss && algorithm == &KeyAlgorithm::RSA_PSS)) {
|
||||
return ExportJWKRsaKey(env, key, target);
|
||||
}
|
||||
if (algorithm == &KeyAlgorithm::EC) return ExportJWKEcKey(env, key, target);
|
||||
if (algorithm != nullptr && (algorithm->isOkp() || algorithm->isPqc())) {
|
||||
return ExportJWKRawKey(env, key, target);
|
||||
}
|
||||
THROW_ERR_CRYPTO_JWK_UNSUPPORTED_KEY_TYPE(env);
|
||||
return false;
|
||||
@@ -226,16 +326,15 @@ bool GetAsymmetricKeyDetail(Environment* env,
|
||||
THROW_ERR_CRYPTO_OPERATION_FAILED(env);
|
||||
return false;
|
||||
}
|
||||
switch (key.GetAsymmetricKey().id()) {
|
||||
case EVP_PKEY_RSA:
|
||||
// Fall through
|
||||
case EVP_PKEY_RSA2:
|
||||
// Fall through
|
||||
case EVP_PKEY_RSA_PSS: return GetRsaKeyDetail(env, key, target);
|
||||
case EVP_PKEY_DSA: return GetDsaKeyDetail(env, key, target);
|
||||
case EVP_PKEY_EC: return GetEcKeyDetail(env, key, target);
|
||||
case EVP_PKEY_DH: return GetDhKeyDetail(env, key, target);
|
||||
const auto& pkey = key.GetAsymmetricKey();
|
||||
const auto* algorithm = pkey.getAlgorithm();
|
||||
// Preserve RSA2 support on legacy backends without exposing its numeric ID.
|
||||
if (algorithm != nullptr ? algorithm->isRsa() : pkey.isRsaVariant()) {
|
||||
return GetRsaKeyDetail(env, key, target);
|
||||
}
|
||||
if (algorithm == &KeyAlgorithm::DSA) return GetDsaKeyDetail(env, key, target);
|
||||
if (algorithm == &KeyAlgorithm::EC) return GetEcKeyDetail(env, key, target);
|
||||
if (algorithm == &KeyAlgorithm::DH) return GetDhKeyDetail(env, key, target);
|
||||
THROW_ERR_CRYPTO_INVALID_KEYTYPE(env);
|
||||
return false;
|
||||
}
|
||||
@@ -269,103 +368,30 @@ bool ExportJWKInner(Environment* env,
|
||||
env, key, result.As<Object>(), handleRsaPss);
|
||||
}
|
||||
|
||||
int GetNidFromName(const char* name) {
|
||||
static constexpr struct {
|
||||
const char* name;
|
||||
int nid;
|
||||
} kNameToNid[] = {
|
||||
{"Ed25519", EVP_PKEY_ED25519},
|
||||
{"Ed448", EVP_PKEY_ED448},
|
||||
{"X25519", EVP_PKEY_X25519},
|
||||
{"X448", EVP_PKEY_X448},
|
||||
};
|
||||
for (const auto& entry : kNameToNid) {
|
||||
if (StringEqualNoCase(name, entry.name)) return entry.nid;
|
||||
}
|
||||
#if OPENSSL_WITH_PQC
|
||||
return GetPqcNidFromName(name);
|
||||
#else
|
||||
return NID_undef;
|
||||
#endif
|
||||
}
|
||||
|
||||
bool IsUnavailablePqcKeyType(Environment* env, Local<String> key_type) {
|
||||
return key_type->StringEquals(env->crypto_ml_dsa_44_string()) ||
|
||||
key_type->StringEquals(env->crypto_ml_dsa_65_string()) ||
|
||||
key_type->StringEquals(env->crypto_ml_dsa_87_string()) ||
|
||||
key_type->StringEquals(env->crypto_ml_kem_512_string()) ||
|
||||
key_type->StringEquals(env->crypto_ml_kem_768_string()) ||
|
||||
key_type->StringEquals(env->crypto_ml_kem_1024_string()) ||
|
||||
key_type->StringEquals(env->crypto_slh_dsa_sha2_128f_string()) ||
|
||||
key_type->StringEquals(env->crypto_slh_dsa_sha2_128s_string()) ||
|
||||
key_type->StringEquals(env->crypto_slh_dsa_sha2_192f_string()) ||
|
||||
key_type->StringEquals(env->crypto_slh_dsa_sha2_192s_string()) ||
|
||||
key_type->StringEquals(env->crypto_slh_dsa_sha2_256f_string()) ||
|
||||
key_type->StringEquals(env->crypto_slh_dsa_sha2_256s_string()) ||
|
||||
key_type->StringEquals(env->crypto_slh_dsa_shake_128f_string()) ||
|
||||
key_type->StringEquals(env->crypto_slh_dsa_shake_128s_string()) ||
|
||||
key_type->StringEquals(env->crypto_slh_dsa_shake_192f_string()) ||
|
||||
key_type->StringEquals(env->crypto_slh_dsa_shake_192s_string()) ||
|
||||
key_type->StringEquals(env->crypto_slh_dsa_shake_256f_string()) ||
|
||||
key_type->StringEquals(env->crypto_slh_dsa_shake_256s_string());
|
||||
}
|
||||
|
||||
bool IsUnsupportedRawKeyType(Environment* env, Local<String> key_type) {
|
||||
return key_type->StringEquals(env->crypto_rsa_string()) ||
|
||||
key_type->StringEquals(env->crypto_rsa_pss_string()) ||
|
||||
key_type->StringEquals(env->crypto_dsa_string()) ||
|
||||
key_type->StringEquals(env->crypto_dh_string());
|
||||
}
|
||||
|
||||
void ValidateRawKeyImportFormat(Environment* env,
|
||||
Local<String> key_type,
|
||||
const char* key_type_name,
|
||||
int id,
|
||||
const KeyAlgorithm* algorithm,
|
||||
EVPKeyPointer::PKFormatType format) {
|
||||
auto validate_raw_format =
|
||||
[&](EVPKeyPointer::PKFormatType expected_private_format) {
|
||||
if (format == EVPKeyPointer::PKFormatType::RAW_PUBLIC ||
|
||||
format == expected_private_format) {
|
||||
return;
|
||||
}
|
||||
THROW_ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS(env);
|
||||
};
|
||||
|
||||
if (key_type->StringEquals(env->crypto_ec_string())) {
|
||||
return validate_raw_format(EVPKeyPointer::PKFormatType::RAW_PRIVATE);
|
||||
if (algorithm == nullptr) {
|
||||
THROW_ERR_INVALID_ARG_VALUE(
|
||||
env, "Invalid asymmetricKeyType: %s", key_type_name);
|
||||
return;
|
||||
}
|
||||
|
||||
switch (id) {
|
||||
case EVP_PKEY_X25519:
|
||||
case EVP_PKEY_X448:
|
||||
case EVP_PKEY_ED25519:
|
||||
case EVP_PKEY_ED448:
|
||||
return validate_raw_format(EVPKeyPointer::PKFormatType::RAW_PRIVATE);
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
#if OPENSSL_WITH_PQC
|
||||
if (IsPqcSeedKeyId(id)) {
|
||||
return validate_raw_format(EVPKeyPointer::PKFormatType::RAW_SEED);
|
||||
}
|
||||
if (IsPqcRawPrivateKeyId(id)) {
|
||||
return validate_raw_format(EVPKeyPointer::PKFormatType::RAW_PRIVATE);
|
||||
}
|
||||
#endif
|
||||
|
||||
if (IsUnavailablePqcKeyType(env, key_type)) {
|
||||
if (algorithm->isPqc() && !algorithm->isAvailable()) {
|
||||
THROW_ERR_INVALID_ARG_VALUE(env, "Unsupported key type");
|
||||
return;
|
||||
}
|
||||
|
||||
if (IsUnsupportedRawKeyType(env, key_type)) {
|
||||
THROW_ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS(env);
|
||||
const auto private_format = algorithm->seedSize() != 0
|
||||
? EVPKeyPointer::PKFormatType::RAW_SEED
|
||||
: EVPKeyPointer::PKFormatType::RAW_PRIVATE;
|
||||
const bool supports_raw =
|
||||
algorithm == &KeyAlgorithm::EC || algorithm->supportsRawPublic();
|
||||
if (supports_raw && (format == EVPKeyPointer::PKFormatType::RAW_PUBLIC ||
|
||||
format == private_format)) {
|
||||
return;
|
||||
}
|
||||
|
||||
THROW_ERR_INVALID_ARG_VALUE(
|
||||
env, "Invalid asymmetricKeyType: %s", key_type_name);
|
||||
THROW_ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS(env);
|
||||
}
|
||||
} // namespace
|
||||
|
||||
@@ -386,7 +412,8 @@ bool KeyObjectData::ToEncodedPublicKey(
|
||||
} else if (config.format == EVPKeyPointer::PKFormatType::RAW_PUBLIC) {
|
||||
Mutex::ScopedLock lock(mutex());
|
||||
const auto& pkey = GetAsymmetricKey();
|
||||
if (pkey.id() == EVP_PKEY_EC) {
|
||||
const auto* algorithm = pkey.getAlgorithm();
|
||||
if (algorithm == &KeyAlgorithm::EC) {
|
||||
auto form = static_cast<point_conversion_form_t>(config.ec_point_form);
|
||||
auto bytes = ncrypto::Ec::TryExportPublic(pkey, form);
|
||||
if (bytes)
|
||||
@@ -406,12 +433,8 @@ bool KeyObjectData::ToEncodedPublicKey(
|
||||
env, ec_key.getGroup(), ec_key.getPublicKey(), form)
|
||||
.ToLocal(out);
|
||||
}
|
||||
const int id = pkey.id();
|
||||
bool is_raw_supported = id == EVP_PKEY_ED25519 || id == EVP_PKEY_ED448 ||
|
||||
id == EVP_PKEY_X25519 || id == EVP_PKEY_X448;
|
||||
#if OPENSSL_WITH_PQC
|
||||
is_raw_supported = is_raw_supported || IsPqcKeyId(id);
|
||||
#endif
|
||||
const bool is_raw_supported =
|
||||
algorithm != nullptr && algorithm->supportsRawPublic();
|
||||
if (!is_raw_supported) {
|
||||
THROW_ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS(env);
|
||||
return false;
|
||||
@@ -444,7 +467,8 @@ bool KeyObjectData::ToEncodedPrivateKey(
|
||||
} else if (config.format == EVPKeyPointer::PKFormatType::RAW_PRIVATE) {
|
||||
Mutex::ScopedLock lock(mutex());
|
||||
const auto& pkey = GetAsymmetricKey();
|
||||
if (pkey.id() == EVP_PKEY_EC) {
|
||||
const auto* algorithm = pkey.getAlgorithm();
|
||||
if (algorithm == &KeyAlgorithm::EC) {
|
||||
auto buf = ncrypto::Ec::ExportPrivate(pkey);
|
||||
if (!buf) {
|
||||
THROW_ERR_CRYPTO_OPERATION_FAILED(env,
|
||||
@@ -453,12 +477,8 @@ bool KeyObjectData::ToEncodedPrivateKey(
|
||||
}
|
||||
return Buffer::Copy(env, buf.get<const char>(), buf.size()).ToLocal(out);
|
||||
}
|
||||
const int id = pkey.id();
|
||||
bool is_raw_supported = id == EVP_PKEY_ED25519 || id == EVP_PKEY_ED448 ||
|
||||
id == EVP_PKEY_X25519 || id == EVP_PKEY_X448;
|
||||
#if OPENSSL_WITH_PQC
|
||||
is_raw_supported = is_raw_supported || IsPqcRawPrivateKeyId(id);
|
||||
#endif
|
||||
const bool is_raw_supported =
|
||||
algorithm != nullptr && algorithm->supportsRawPrivate();
|
||||
if (!is_raw_supported) {
|
||||
THROW_ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS(env);
|
||||
return false;
|
||||
@@ -471,24 +491,21 @@ bool KeyObjectData::ToEncodedPrivateKey(
|
||||
return Buffer::Copy(env, raw_data.get<const char>(), raw_data.size())
|
||||
.ToLocal(out);
|
||||
} else if (config.format == EVPKeyPointer::PKFormatType::RAW_SEED) {
|
||||
#if OPENSSL_WITH_PQC
|
||||
Mutex::ScopedLock lock(mutex());
|
||||
const auto& pkey = GetAsymmetricKey();
|
||||
if (!IsPqcSeedKeyId(pkey.id())) {
|
||||
THROW_ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS(env);
|
||||
return false;
|
||||
}
|
||||
auto raw_data = pkey.rawSeed();
|
||||
if (!raw_data) {
|
||||
THROW_ERR_CRYPTO_OPERATION_FAILED(env, "Failed to get raw seed");
|
||||
if (raw_data.error ==
|
||||
EVPKeyPointer::RawExportError::UNSUPPORTED_KEY_TYPE) {
|
||||
THROW_ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS(env);
|
||||
} else {
|
||||
THROW_ERR_CRYPTO_OPERATION_FAILED(env, "Failed to get raw seed");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return Buffer::Copy(env, raw_data.get<const char>(), raw_data.size())
|
||||
return Buffer::Copy(
|
||||
env, raw_data.value.get<const char>(), raw_data.value.size())
|
||||
.ToLocal(out);
|
||||
#else
|
||||
THROW_ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS(env);
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
return WritePrivateKey(env, GetAsymmetricKey(), config).ToLocal(out);
|
||||
@@ -566,7 +583,7 @@ static KeyObjectData ImportRawKey(Environment* env,
|
||||
const unsigned char* key_data,
|
||||
size_t key_data_len,
|
||||
EVPKeyPointer::PKFormatType format,
|
||||
Local<String> key_type,
|
||||
const KeyAlgorithm* algorithm,
|
||||
const char* key_type_name,
|
||||
const char* named_curve,
|
||||
KeyType target_type) {
|
||||
@@ -576,14 +593,13 @@ static KeyObjectData ImportRawKey(Environment* env,
|
||||
}
|
||||
};
|
||||
|
||||
const int id = GetNidFromName(key_type_name);
|
||||
ValidateRawKeyImportFormat(env, key_type, key_type_name, id, format);
|
||||
ValidateRawKeyImportFormat(env, key_type_name, algorithm, format);
|
||||
if (env->isolate()->HasPendingException()) {
|
||||
return {};
|
||||
}
|
||||
|
||||
// EC keys
|
||||
if (key_type->StringEquals(env->crypto_ec_string())) {
|
||||
if (algorithm == &KeyAlgorithm::EC) {
|
||||
int curve_nid = ncrypto::Ec::GetCurveIdFromName(named_curve);
|
||||
if (curve_nid == NID_undef) {
|
||||
THROW_ERR_CRYPTO_INVALID_CURVE(env);
|
||||
@@ -641,45 +657,23 @@ static KeyObjectData ImportRawKey(Environment* env,
|
||||
return KeyObjectData::CreateAsymmetric(target_type, std::move(pkey));
|
||||
}
|
||||
|
||||
typedef EVPKeyPointer (*new_key_fn)(
|
||||
int, const ncrypto::Buffer<const unsigned char>&);
|
||||
new_key_fn fn = nullptr;
|
||||
switch (id) {
|
||||
case EVP_PKEY_X25519:
|
||||
case EVP_PKEY_X448:
|
||||
case EVP_PKEY_ED25519:
|
||||
case EVP_PKEY_ED448:
|
||||
fn = target_type == kKeyTypePrivate ? EVPKeyPointer::NewRawPrivate
|
||||
: EVPKeyPointer::NewRawPublic;
|
||||
break;
|
||||
default:
|
||||
#if OPENSSL_WITH_PQC
|
||||
if (IsPqcKeyId(id)) {
|
||||
if (target_type == kKeyTypePrivate) {
|
||||
fn = IsPqcSeedKeyId(id) ? EVPKeyPointer::NewRawSeed
|
||||
: EVPKeyPointer::NewRawPrivate;
|
||||
} else {
|
||||
fn = EVPKeyPointer::NewRawPublic;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
break;
|
||||
const ncrypto::Buffer<const unsigned char> buffer{
|
||||
.data = key_data,
|
||||
.len = key_data_len,
|
||||
};
|
||||
EVPKeyPointer pkey;
|
||||
if (target_type == kKeyTypePublic) {
|
||||
pkey = EVPKeyPointer::NewRawPublic(*algorithm, buffer);
|
||||
} else if (algorithm->seedSize() != 0) {
|
||||
pkey = EVPKeyPointer::NewRawSeed(*algorithm, buffer);
|
||||
} else {
|
||||
pkey = EVPKeyPointer::NewRawPrivate(*algorithm, buffer);
|
||||
}
|
||||
|
||||
if (fn != nullptr) {
|
||||
auto pkey = fn(id,
|
||||
ncrypto::Buffer<const unsigned char>{
|
||||
.data = key_data,
|
||||
.len = key_data_len,
|
||||
});
|
||||
if (!pkey) {
|
||||
throw_invalid();
|
||||
return {};
|
||||
}
|
||||
return KeyObjectData::CreateAsymmetric(target_type, std::move(pkey));
|
||||
if (!pkey) {
|
||||
throw_invalid();
|
||||
return {};
|
||||
}
|
||||
|
||||
return {};
|
||||
return KeyObjectData::CreateAsymmetric(target_type, std::move(pkey));
|
||||
}
|
||||
|
||||
// Shared helper for importing a JWK asymmetric key. Extracts kty from the
|
||||
@@ -697,14 +691,9 @@ static KeyObjectData ImportJWKFromArgs(Environment* env, Local<Object> jwk) {
|
||||
} else if (*kty_string == std::string_view("EC")) {
|
||||
return ImportJWKEcKey(env, jwk);
|
||||
} else if (*kty_string == std::string_view("OKP")) {
|
||||
return ImportJWKEdKey(env, jwk);
|
||||
return ImportJWKRawKey(env, jwk, /* is_akp */ false);
|
||||
} else if (*kty_string == std::string_view("AKP")) {
|
||||
#if OPENSSL_WITH_PQC
|
||||
return ImportJWKPqcKey(env, jwk);
|
||||
#else
|
||||
THROW_ERR_INVALID_ARG_VALUE(env, "Unsupported key type");
|
||||
return {};
|
||||
#endif
|
||||
return ImportJWKRawKey(env, jwk, /* is_akp */ true);
|
||||
}
|
||||
|
||||
THROW_ERR_CRYPTO_INVALID_JWK(
|
||||
@@ -733,11 +722,16 @@ static KeyObjectData ImportRawKeyFromArgs(
|
||||
}
|
||||
|
||||
CHECK(args[offset + 2]->IsString());
|
||||
Local<String> key_type = args[offset + 2].As<String>();
|
||||
Utf8Value key_type_name(env->isolate(), key_type);
|
||||
Utf8Value key_type_name(env->isolate(), args[offset + 2]);
|
||||
const auto* algorithm = KeyAlgorithm::FromName(*key_type_name);
|
||||
// Raw key types require their exact public spelling.
|
||||
if (algorithm != nullptr &&
|
||||
(algorithm->keyTypeName() == nullptr ||
|
||||
key_type_name.ToStringView() != algorithm->keyTypeName())) {
|
||||
algorithm = nullptr;
|
||||
}
|
||||
|
||||
DCHECK_IMPLIES(key_type->StringEquals(env->crypto_ec_string()),
|
||||
args[offset + 4]->IsString());
|
||||
DCHECK_IMPLIES(algorithm == &KeyAlgorithm::EC, args[offset + 4]->IsString());
|
||||
Utf8Value curve(env->isolate(),
|
||||
args[offset + 4]->IsString() ? args[offset + 4].As<String>()
|
||||
: String::Empty(env->isolate()));
|
||||
@@ -746,7 +740,7 @@ static KeyObjectData ImportRawKeyFromArgs(
|
||||
key_data.data(),
|
||||
key_data.size(),
|
||||
format,
|
||||
key_type,
|
||||
algorithm,
|
||||
*key_type_name,
|
||||
*curve,
|
||||
type);
|
||||
@@ -1364,33 +1358,10 @@ void KeyObjectHandle::GetKeyDetail(const FunctionCallbackInfo<Value>& args) {
|
||||
}
|
||||
|
||||
Local<Value> KeyObjectHandle::GetAsymmetricKeyType() const {
|
||||
switch (data_.GetAsymmetricKey().id()) {
|
||||
case EVP_PKEY_RSA:
|
||||
return env()->crypto_rsa_string();
|
||||
case EVP_PKEY_RSA_PSS:
|
||||
return env()->crypto_rsa_pss_string();
|
||||
case EVP_PKEY_DSA:
|
||||
return env()->crypto_dsa_string();
|
||||
case EVP_PKEY_DH:
|
||||
return env()->crypto_dh_string();
|
||||
case EVP_PKEY_EC:
|
||||
return env()->crypto_ec_string();
|
||||
case EVP_PKEY_ED25519:
|
||||
return env()->crypto_ed25519_string();
|
||||
case EVP_PKEY_ED448:
|
||||
return env()->crypto_ed448_string();
|
||||
case EVP_PKEY_X25519:
|
||||
return env()->crypto_x25519_string();
|
||||
case EVP_PKEY_X448:
|
||||
return env()->crypto_x448_string();
|
||||
#if OPENSSL_WITH_PQC
|
||||
default:
|
||||
return GetPqcAsymmetricKeyType(env(), data_.GetAsymmetricKey().id());
|
||||
#else
|
||||
default:
|
||||
return Undefined(env()->isolate());
|
||||
#endif
|
||||
}
|
||||
const char* name = data_.GetAsymmetricKey().getKeyTypeName();
|
||||
if (name == nullptr) return Undefined(env()->isolate());
|
||||
return OneByteString(
|
||||
env()->isolate(), name, -1, NewStringType::kInternalized);
|
||||
}
|
||||
|
||||
void KeyObjectHandle::GetAsymmetricKeyType(
|
||||
@@ -1407,7 +1378,7 @@ bool KeyObjectHandle::CheckEcKeyData() const {
|
||||
const auto& key = data_.GetAsymmetricKey();
|
||||
EVPKeyCtxPointer ctx = key.newCtx();
|
||||
CHECK(ctx);
|
||||
CHECK_EQ(key.id(), EVP_PKEY_EC);
|
||||
DCHECK(key.isA(KeyAlgorithm::EC));
|
||||
|
||||
return data_.GetKeyType() == kKeyTypePrivate ? ctx.privateCheck()
|
||||
: ctx.publicCheck();
|
||||
@@ -1498,12 +1469,7 @@ void KeyObjectHandle::RawPublicKey(
|
||||
Mutex::ScopedLock lock(data.mutex());
|
||||
const auto& pkey = data.GetAsymmetricKey();
|
||||
|
||||
const int id = pkey.id();
|
||||
bool is_raw_supported = id == EVP_PKEY_ED25519 || id == EVP_PKEY_ED448 ||
|
||||
id == EVP_PKEY_X25519 || id == EVP_PKEY_X448;
|
||||
#if OPENSSL_WITH_PQC
|
||||
is_raw_supported = is_raw_supported || IsPqcKeyId(id);
|
||||
#endif
|
||||
const bool is_raw_supported = pkey.supportsRawPublic();
|
||||
if (!is_raw_supported) {
|
||||
return THROW_ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS(env);
|
||||
}
|
||||
@@ -1531,12 +1497,7 @@ void KeyObjectHandle::RawPrivateKey(
|
||||
Mutex::ScopedLock lock(data.mutex());
|
||||
const auto& pkey = data.GetAsymmetricKey();
|
||||
|
||||
const int id = pkey.id();
|
||||
bool is_raw_supported = id == EVP_PKEY_ED25519 || id == EVP_PKEY_ED448 ||
|
||||
id == EVP_PKEY_X25519 || id == EVP_PKEY_X448;
|
||||
#if OPENSSL_WITH_PQC
|
||||
is_raw_supported = is_raw_supported || IsPqcRawPrivateKeyId(id);
|
||||
#endif
|
||||
const bool is_raw_supported = pkey.supportsRawPrivate();
|
||||
if (!is_raw_supported) {
|
||||
return THROW_ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS(env);
|
||||
}
|
||||
@@ -1563,7 +1524,7 @@ void KeyObjectHandle::ExportECPublicRaw(
|
||||
|
||||
Mutex::ScopedLock lock(data.mutex());
|
||||
const auto& m_pkey = data.GetAsymmetricKey();
|
||||
if (m_pkey.id() != EVP_PKEY_EC) {
|
||||
if (!m_pkey.isA(KeyAlgorithm::EC)) {
|
||||
return THROW_ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS(env);
|
||||
}
|
||||
|
||||
@@ -1602,7 +1563,7 @@ void KeyObjectHandle::ExportECPrivateRaw(
|
||||
|
||||
Mutex::ScopedLock lock(data.mutex());
|
||||
const auto& m_pkey = data.GetAsymmetricKey();
|
||||
if (m_pkey.id() != EVP_PKEY_EC) {
|
||||
if (!m_pkey.isA(KeyAlgorithm::EC)) {
|
||||
return THROW_ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS(env);
|
||||
}
|
||||
|
||||
@@ -1624,25 +1585,20 @@ void KeyObjectHandle::RawSeed(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
const KeyObjectData& data = key->Data();
|
||||
CHECK_EQ(data.GetKeyType(), kKeyTypePrivate);
|
||||
|
||||
#if OPENSSL_WITH_PQC
|
||||
Mutex::ScopedLock lock(data.mutex());
|
||||
const auto& pkey = data.GetAsymmetricKey();
|
||||
|
||||
if (!IsPqcSeedKeyId(pkey.id())) {
|
||||
return THROW_ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS(env);
|
||||
}
|
||||
|
||||
auto raw_data = pkey.rawSeed();
|
||||
if (!raw_data) {
|
||||
if (raw_data.error == EVPKeyPointer::RawExportError::UNSUPPORTED_KEY_TYPE) {
|
||||
return THROW_ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS(env);
|
||||
}
|
||||
return THROW_ERR_CRYPTO_OPERATION_FAILED(env, "Failed to get raw seed");
|
||||
}
|
||||
|
||||
args.GetReturnValue().Set(
|
||||
Buffer::Copy(env, raw_data.get<const char>(), raw_data.size())
|
||||
Buffer::Copy(env, raw_data.value.get<const char>(), raw_data.value.size())
|
||||
.FromMaybe(Local<Value>()));
|
||||
#else
|
||||
return THROW_ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS(env);
|
||||
#endif
|
||||
}
|
||||
|
||||
void KeyObjectHandle::ExportJWK(
|
||||
@@ -2222,6 +2178,7 @@ void NativeCryptoKey::CryptoKeyTransferData::MemoryInfo(
|
||||
|
||||
namespace Keys {
|
||||
void Initialize(Environment* env, Local<Object> target) {
|
||||
Local<Context> context = env->context();
|
||||
target->Set(env->context(),
|
||||
FIXED_ONE_BYTE_STRING(env->isolate(), "KeyObjectHandle"),
|
||||
KeyObjectHandle::Initialize(env)).Check();
|
||||
@@ -2256,34 +2213,7 @@ void Initialize(Environment* env, Local<Object> target) {
|
||||
NODE_DEFINE_CONSTANT(target, kWebCryptoKeyFormatPKCS8);
|
||||
NODE_DEFINE_CONSTANT(target, kWebCryptoKeyFormatSPKI);
|
||||
NODE_DEFINE_CONSTANT(target, kWebCryptoKeyFormatJWK);
|
||||
NODE_DEFINE_CONSTANT(target, EVP_PKEY_ED25519);
|
||||
NODE_DEFINE_CONSTANT(target, EVP_PKEY_ED448);
|
||||
#if OPENSSL_WITH_PQC
|
||||
NODE_DEFINE_CONSTANT(target, EVP_PKEY_ML_DSA_44);
|
||||
NODE_DEFINE_CONSTANT(target, EVP_PKEY_ML_DSA_65);
|
||||
NODE_DEFINE_CONSTANT(target, EVP_PKEY_ML_DSA_87);
|
||||
#if OPENSSL_WITH_PQC_ML_KEM_512
|
||||
NODE_DEFINE_CONSTANT(target, EVP_PKEY_ML_KEM_512);
|
||||
#endif
|
||||
NODE_DEFINE_CONSTANT(target, EVP_PKEY_ML_KEM_768);
|
||||
NODE_DEFINE_CONSTANT(target, EVP_PKEY_ML_KEM_1024);
|
||||
#if OPENSSL_WITH_PQC_SLH_DSA
|
||||
NODE_DEFINE_CONSTANT(target, EVP_PKEY_SLH_DSA_SHA2_128F);
|
||||
NODE_DEFINE_CONSTANT(target, EVP_PKEY_SLH_DSA_SHA2_128S);
|
||||
NODE_DEFINE_CONSTANT(target, EVP_PKEY_SLH_DSA_SHA2_192F);
|
||||
NODE_DEFINE_CONSTANT(target, EVP_PKEY_SLH_DSA_SHA2_192S);
|
||||
NODE_DEFINE_CONSTANT(target, EVP_PKEY_SLH_DSA_SHA2_256F);
|
||||
NODE_DEFINE_CONSTANT(target, EVP_PKEY_SLH_DSA_SHA2_256S);
|
||||
NODE_DEFINE_CONSTANT(target, EVP_PKEY_SLH_DSA_SHAKE_128F);
|
||||
NODE_DEFINE_CONSTANT(target, EVP_PKEY_SLH_DSA_SHAKE_128S);
|
||||
NODE_DEFINE_CONSTANT(target, EVP_PKEY_SLH_DSA_SHAKE_192F);
|
||||
NODE_DEFINE_CONSTANT(target, EVP_PKEY_SLH_DSA_SHAKE_192S);
|
||||
NODE_DEFINE_CONSTANT(target, EVP_PKEY_SLH_DSA_SHAKE_256F);
|
||||
NODE_DEFINE_CONSTANT(target, EVP_PKEY_SLH_DSA_SHAKE_256S);
|
||||
#endif
|
||||
#endif
|
||||
NODE_DEFINE_CONSTANT(target, EVP_PKEY_X25519);
|
||||
NODE_DEFINE_CONSTANT(target, EVP_PKEY_X448);
|
||||
SetMethod(context, target, "getPqcKeyTypes", GetPqcKeyTypes);
|
||||
NODE_DEFINE_CONSTANT(target, kKeyEncodingPKCS1);
|
||||
NODE_DEFINE_CONSTANT(target, kKeyEncodingPKCS8);
|
||||
NODE_DEFINE_CONSTANT(target, kKeyEncodingSPKI);
|
||||
@@ -2304,6 +2234,7 @@ void Initialize(Environment* env, Local<Object> target) {
|
||||
|
||||
void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
|
||||
KeyObjectHandle::RegisterExternalReferences(registry);
|
||||
registry->Register(GetPqcKeyTypes);
|
||||
}
|
||||
} // namespace Keys
|
||||
|
||||
|
||||
+10
-271
@@ -1,283 +1,22 @@
|
||||
#include "crypto/crypto_pqc.h"
|
||||
#include "crypto/crypto_util.h"
|
||||
#include "env-inl.h"
|
||||
#include "string_bytes.h"
|
||||
#include "ncrypto.h"
|
||||
#include "util-inl.h"
|
||||
#include "v8.h"
|
||||
|
||||
namespace node {
|
||||
|
||||
using ncrypto::DataPointer;
|
||||
using ncrypto::EVPKeyPointer;
|
||||
using v8::Local;
|
||||
using v8::Object;
|
||||
using v8::String;
|
||||
using v8::Value;
|
||||
|
||||
namespace crypto {
|
||||
|
||||
#if OPENSSL_WITH_PQC
|
||||
namespace {
|
||||
using PqcKeyTypeGetter = Local<String> (Environment::*)() const;
|
||||
|
||||
enum PqcAlgorithmFlag {
|
||||
kPqcRawPrivate = 1 << 0,
|
||||
kPqcRawSeed = 1 << 1,
|
||||
kPqcSignature = 1 << 2,
|
||||
};
|
||||
|
||||
struct PqcAlgorithm {
|
||||
int id;
|
||||
const char* name;
|
||||
PqcKeyTypeGetter key_type;
|
||||
int flags;
|
||||
};
|
||||
|
||||
// ML-DSA and ML-KEM carry private material as a seed. SLH-DSA uses the
|
||||
// expanded private key and is only exposed by OpenSSL.
|
||||
constexpr int kPqcMlDsaFlags = kPqcRawSeed | kPqcSignature;
|
||||
constexpr int kPqcMlKemFlags = kPqcRawSeed;
|
||||
constexpr int kPqcSlhDsaFlags = kPqcRawPrivate | kPqcSignature;
|
||||
|
||||
constexpr PqcAlgorithm kPqcAlgorithms[] = {
|
||||
{EVP_PKEY_ML_DSA_44,
|
||||
"ML-DSA-44",
|
||||
&Environment::crypto_ml_dsa_44_string,
|
||||
kPqcMlDsaFlags},
|
||||
{EVP_PKEY_ML_DSA_65,
|
||||
"ML-DSA-65",
|
||||
&Environment::crypto_ml_dsa_65_string,
|
||||
kPqcMlDsaFlags},
|
||||
{EVP_PKEY_ML_DSA_87,
|
||||
"ML-DSA-87",
|
||||
&Environment::crypto_ml_dsa_87_string,
|
||||
kPqcMlDsaFlags},
|
||||
{EVP_PKEY_ML_KEM_768,
|
||||
"ML-KEM-768",
|
||||
&Environment::crypto_ml_kem_768_string,
|
||||
kPqcMlKemFlags},
|
||||
{EVP_PKEY_ML_KEM_1024,
|
||||
"ML-KEM-1024",
|
||||
&Environment::crypto_ml_kem_1024_string,
|
||||
kPqcMlKemFlags},
|
||||
|
||||
#if OPENSSL_WITH_PQC_ML_KEM_512
|
||||
{EVP_PKEY_ML_KEM_512,
|
||||
"ML-KEM-512",
|
||||
&Environment::crypto_ml_kem_512_string,
|
||||
kPqcMlKemFlags},
|
||||
#endif
|
||||
#if OPENSSL_WITH_PQC_SLH_DSA
|
||||
{EVP_PKEY_SLH_DSA_SHA2_128F,
|
||||
"SLH-DSA-SHA2-128f",
|
||||
&Environment::crypto_slh_dsa_sha2_128f_string,
|
||||
kPqcSlhDsaFlags},
|
||||
{EVP_PKEY_SLH_DSA_SHA2_128S,
|
||||
"SLH-DSA-SHA2-128s",
|
||||
&Environment::crypto_slh_dsa_sha2_128s_string,
|
||||
kPqcSlhDsaFlags},
|
||||
{EVP_PKEY_SLH_DSA_SHA2_192F,
|
||||
"SLH-DSA-SHA2-192f",
|
||||
&Environment::crypto_slh_dsa_sha2_192f_string,
|
||||
kPqcSlhDsaFlags},
|
||||
{EVP_PKEY_SLH_DSA_SHA2_192S,
|
||||
"SLH-DSA-SHA2-192s",
|
||||
&Environment::crypto_slh_dsa_sha2_192s_string,
|
||||
kPqcSlhDsaFlags},
|
||||
{EVP_PKEY_SLH_DSA_SHA2_256F,
|
||||
"SLH-DSA-SHA2-256f",
|
||||
&Environment::crypto_slh_dsa_sha2_256f_string,
|
||||
kPqcSlhDsaFlags},
|
||||
{EVP_PKEY_SLH_DSA_SHA2_256S,
|
||||
"SLH-DSA-SHA2-256s",
|
||||
&Environment::crypto_slh_dsa_sha2_256s_string,
|
||||
kPqcSlhDsaFlags},
|
||||
{EVP_PKEY_SLH_DSA_SHAKE_128F,
|
||||
"SLH-DSA-SHAKE-128f",
|
||||
&Environment::crypto_slh_dsa_shake_128f_string,
|
||||
kPqcSlhDsaFlags},
|
||||
{EVP_PKEY_SLH_DSA_SHAKE_128S,
|
||||
"SLH-DSA-SHAKE-128s",
|
||||
&Environment::crypto_slh_dsa_shake_128s_string,
|
||||
kPqcSlhDsaFlags},
|
||||
{EVP_PKEY_SLH_DSA_SHAKE_192F,
|
||||
"SLH-DSA-SHAKE-192f",
|
||||
&Environment::crypto_slh_dsa_shake_192f_string,
|
||||
kPqcSlhDsaFlags},
|
||||
{EVP_PKEY_SLH_DSA_SHAKE_192S,
|
||||
"SLH-DSA-SHAKE-192s",
|
||||
&Environment::crypto_slh_dsa_shake_192s_string,
|
||||
kPqcSlhDsaFlags},
|
||||
{EVP_PKEY_SLH_DSA_SHAKE_256F,
|
||||
"SLH-DSA-SHAKE-256f",
|
||||
&Environment::crypto_slh_dsa_shake_256f_string,
|
||||
kPqcSlhDsaFlags},
|
||||
{EVP_PKEY_SLH_DSA_SHAKE_256S,
|
||||
"SLH-DSA-SHAKE-256s",
|
||||
&Environment::crypto_slh_dsa_shake_256s_string,
|
||||
kPqcSlhDsaFlags},
|
||||
#endif
|
||||
};
|
||||
|
||||
const PqcAlgorithm* FindPqcAlgorithmById(int id) {
|
||||
for (const auto& alg : kPqcAlgorithms) {
|
||||
if (alg.id == id) return &alg;
|
||||
}
|
||||
return nullptr;
|
||||
void GetPqcKeyTypes(const v8::FunctionCallbackInfo<Value>& args) {
|
||||
v8::LocalVector<Value> names(args.GetIsolate());
|
||||
ncrypto::KeyAlgorithm::ForEachPqc(
|
||||
[&](const ncrypto::KeyAlgorithm& algorithm) {
|
||||
names.push_back(OneByteString(args.GetIsolate(), algorithm.name()));
|
||||
});
|
||||
args.GetReturnValue().Set(
|
||||
v8::Array::New(args.GetIsolate(), names.data(), names.size()));
|
||||
}
|
||||
|
||||
const PqcAlgorithm* FindPqcAlgorithmByName(const char* name) {
|
||||
for (const auto& alg : kPqcAlgorithms) {
|
||||
if (strcmp(name, alg.name) == 0) return &alg;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
bool HasPqcAlgorithmFlag(const PqcAlgorithm* alg, PqcAlgorithmFlag flag) {
|
||||
return alg != nullptr && (alg->flags & flag) != 0;
|
||||
}
|
||||
|
||||
bool TrySetEncodedKey(Environment* env,
|
||||
DataPointer data,
|
||||
Local<Object> target,
|
||||
Local<String> key) {
|
||||
Local<Value> encoded;
|
||||
if (!data) return false;
|
||||
const ncrypto::Buffer<const char> out = data;
|
||||
return StringBytes::Encode(env->isolate(), out.data, out.len, BASE64URL)
|
||||
.ToLocal(&encoded) &&
|
||||
target->DefineOwnProperty(env->context(), key, encoded)
|
||||
.FromMaybe(false);
|
||||
}
|
||||
} // namespace
|
||||
|
||||
bool ExportJwkPqcKey(Environment* env,
|
||||
const KeyObjectData& key,
|
||||
Local<Object> target) {
|
||||
Mutex::ScopedLock lock(key.mutex());
|
||||
const auto& pkey = key.GetAsymmetricKey();
|
||||
|
||||
const PqcAlgorithm* alg = FindPqcAlgorithmById(pkey.id());
|
||||
CHECK(alg);
|
||||
|
||||
if (key.GetKeyType() == kKeyTypePrivate) {
|
||||
const bool uses_seed = HasPqcAlgorithmFlag(alg, kPqcRawSeed);
|
||||
DataPointer priv_data = uses_seed ? pkey.rawSeed() : pkey.rawPrivateKey();
|
||||
if (uses_seed && !priv_data) {
|
||||
THROW_ERR_CRYPTO_OPERATION_FAILED(env,
|
||||
"key does not have an available seed");
|
||||
return false;
|
||||
}
|
||||
if (!TrySetEncodedKey(
|
||||
env, std::move(priv_data), target, env->jwk_priv_string())) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return !(!target
|
||||
->DefineOwnProperty(env->context(),
|
||||
env->jwk_kty_string(),
|
||||
env->jwk_akp_string())
|
||||
.FromMaybe(false) ||
|
||||
!target
|
||||
->DefineOwnProperty(env->context(),
|
||||
env->jwk_alg_string(),
|
||||
OneByteString(env->isolate(), alg->name))
|
||||
.FromMaybe(false) ||
|
||||
!TrySetEncodedKey(
|
||||
env, pkey.rawPublicKey(), target, env->jwk_pub_string()));
|
||||
}
|
||||
|
||||
KeyObjectData ImportJWKPqcKey(Environment* env, Local<Object> jwk) {
|
||||
Local<Value> alg_value;
|
||||
Local<Value> pub_value;
|
||||
Local<Value> priv_value;
|
||||
|
||||
if (!jwk->Get(env->context(), env->jwk_alg_string()).ToLocal(&alg_value) ||
|
||||
!jwk->Get(env->context(), env->jwk_pub_string()).ToLocal(&pub_value) ||
|
||||
!jwk->Get(env->context(), env->jwk_priv_string()).ToLocal(&priv_value)) {
|
||||
return {};
|
||||
}
|
||||
|
||||
Utf8Value alg_str(env->isolate(),
|
||||
alg_value->IsString() ? alg_value.As<String>()
|
||||
: String::Empty(env->isolate()));
|
||||
|
||||
const PqcAlgorithm* alg = FindPqcAlgorithmByName(*alg_str);
|
||||
if (!alg) {
|
||||
THROW_ERR_CRYPTO_INVALID_JWK(env, "Unsupported JWK AKP \"alg\"");
|
||||
return {};
|
||||
}
|
||||
|
||||
if (!pub_value->IsString() ||
|
||||
(!priv_value->IsUndefined() && !priv_value->IsString())) {
|
||||
THROW_ERR_CRYPTO_INVALID_JWK(env, "Invalid JWK AKP key");
|
||||
return {};
|
||||
}
|
||||
|
||||
KeyType type = priv_value->IsString() ? kKeyTypePrivate : kKeyTypePublic;
|
||||
|
||||
EVPKeyPointer pkey;
|
||||
if (type == kKeyTypePrivate) {
|
||||
ByteSource priv =
|
||||
ByteSource::FromEncodedString(env, priv_value.As<String>());
|
||||
ncrypto::Buffer<const unsigned char> buf{
|
||||
.data = priv.data<const unsigned char>(),
|
||||
.len = priv.size(),
|
||||
};
|
||||
pkey = HasPqcAlgorithmFlag(alg, kPqcRawSeed)
|
||||
? EVPKeyPointer::NewRawSeed(alg->id, buf)
|
||||
: EVPKeyPointer::NewRawPrivate(alg->id, buf);
|
||||
} else {
|
||||
ByteSource pub = ByteSource::FromEncodedString(env, pub_value.As<String>());
|
||||
pkey =
|
||||
EVPKeyPointer::NewRawPublic(alg->id,
|
||||
ncrypto::Buffer<const unsigned char>{
|
||||
.data = pub.data<const unsigned char>(),
|
||||
.len = pub.size(),
|
||||
});
|
||||
}
|
||||
|
||||
if (!pkey) {
|
||||
THROW_ERR_CRYPTO_INVALID_JWK(env, "Invalid JWK AKP key");
|
||||
return {};
|
||||
}
|
||||
|
||||
return KeyObjectData::CreateAsymmetric(type, std::move(pkey));
|
||||
}
|
||||
|
||||
bool IsPqcKeyId(int id) {
|
||||
return FindPqcAlgorithmById(id) != nullptr;
|
||||
}
|
||||
|
||||
bool IsPqcRawPrivateKeyId(int id) {
|
||||
const PqcAlgorithm* alg = FindPqcAlgorithmById(id);
|
||||
return HasPqcAlgorithmFlag(alg, kPqcRawPrivate);
|
||||
}
|
||||
|
||||
bool IsPqcSeedKeyId(int id) {
|
||||
const PqcAlgorithm* alg = FindPqcAlgorithmById(id);
|
||||
return HasPqcAlgorithmFlag(alg, kPqcRawSeed);
|
||||
}
|
||||
|
||||
bool IsPqcSignatureKeyId(int id) {
|
||||
const PqcAlgorithm* alg = FindPqcAlgorithmById(id);
|
||||
return HasPqcAlgorithmFlag(alg, kPqcSignature);
|
||||
}
|
||||
|
||||
int GetPqcNidFromName(const char* name) {
|
||||
for (const auto& alg : kPqcAlgorithms) {
|
||||
if (StringEqualNoCase(name, alg.name)) return alg.id;
|
||||
}
|
||||
return NID_undef;
|
||||
}
|
||||
|
||||
Local<Value> GetPqcAsymmetricKeyType(Environment* env, int id) {
|
||||
const PqcAlgorithm* alg = FindPqcAlgorithmById(id);
|
||||
if (alg == nullptr) return v8::Undefined(env->isolate());
|
||||
|
||||
Local<String> key_type = (env->*(alg->key_type))();
|
||||
return key_type.As<Value>();
|
||||
}
|
||||
#endif
|
||||
} // namespace crypto
|
||||
} // namespace node
|
||||
+1
-25
@@ -3,35 +3,11 @@
|
||||
|
||||
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
|
||||
|
||||
#include "crypto/crypto_keys.h"
|
||||
#include "env.h"
|
||||
#include "v8.h"
|
||||
|
||||
namespace node {
|
||||
namespace crypto {
|
||||
#if OPENSSL_WITH_PQC
|
||||
bool ExportJwkPqcKey(Environment* env,
|
||||
const KeyObjectData& key,
|
||||
v8::Local<v8::Object> target);
|
||||
|
||||
KeyObjectData ImportJWKPqcKey(Environment* env, v8::Local<v8::Object> jwk);
|
||||
|
||||
// Returns true for PQC algorithms that support raw private key export/import.
|
||||
bool IsPqcRawPrivateKeyId(int id);
|
||||
// Returns true if the given EVP_PKEY id is a PQC algorithm known to Node.
|
||||
bool IsPqcKeyId(int id);
|
||||
// Returns true for PQC algorithms that carry the private key as a seed
|
||||
// (ML-DSA, ML-KEM). Returns false for algorithms that use the expanded
|
||||
// private key (SLH-DSA), or for non-PQC ids.
|
||||
bool IsPqcSeedKeyId(int id);
|
||||
// Returns true for PQC signature algorithms (ML-DSA, SLH-DSA). Returns false
|
||||
// for ML-KEM or for non-PQC ids.
|
||||
bool IsPqcSignatureKeyId(int id);
|
||||
// Returns the EVP_PKEY id for the given PQC algorithm name, or NID_undef.
|
||||
int GetPqcNidFromName(const char* name);
|
||||
// Returns the JS asymmetricKeyType string for a PQC id, or undefined.
|
||||
v8::Local<v8::Value> GetPqcAsymmetricKeyType(Environment* env, int id);
|
||||
#endif
|
||||
void GetPqcKeyTypes(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
} // namespace crypto
|
||||
} // namespace node
|
||||
|
||||
|
||||
@@ -19,6 +19,7 @@ using ncrypto::DataPointer;
|
||||
using ncrypto::Digest;
|
||||
using ncrypto::EVPKeyCtxPointer;
|
||||
using ncrypto::EVPKeyPointer;
|
||||
using ncrypto::KeyAlgorithm;
|
||||
#if NCRYPTO_USE_LEGACY_KEY_TYPES
|
||||
using ncrypto::RSAPointer;
|
||||
#endif
|
||||
@@ -57,9 +58,9 @@ bool IsRsaPssDigestEncodable(const Digest& digest) {
|
||||
} // namespace
|
||||
|
||||
EVPKeyCtxPointer RsaKeyGenTraits::Setup(RsaKeyPairGenConfig* params) {
|
||||
auto ctx = EVPKeyCtxPointer::NewFromID(
|
||||
params->params.variant == kKeyVariantRSA_PSS ? EVP_PKEY_RSA_PSS
|
||||
: EVP_PKEY_RSA);
|
||||
auto ctx = EVPKeyCtxPointer::NewFromAlgorithm(
|
||||
params->params.variant == kKeyVariantRSA_PSS ? KeyAlgorithm::RSA_PSS
|
||||
: KeyAlgorithm::RSA);
|
||||
|
||||
if (!ctx.initForKeygen() ||
|
||||
!ctx.setRsaKeygenBits(params->params.modulus_bits)) {
|
||||
@@ -546,25 +547,7 @@ KeyObjectData ImportJWKRsaKey(Environment* env, Local<Object> jwk) {
|
||||
return {};
|
||||
}
|
||||
|
||||
// Verify that n is the product of all prime factors.
|
||||
const auto& pub = rsa_view.getPublicKey();
|
||||
const auto& priv = rsa_view.getPrivateKey();
|
||||
auto product = BignumPointer::New();
|
||||
BN_CTX* ctx = BN_CTX_new();
|
||||
bool n_valid =
|
||||
ctx && product && BN_mul(product.get(), priv.p, priv.q, ctx) == 1;
|
||||
for (const auto& info : rsa_view.getOtherPrimeInfos()) {
|
||||
auto next = BignumPointer::New();
|
||||
if (!n_valid || !next ||
|
||||
BN_mul(next.get(), product.get(), info.r, ctx) != 1) {
|
||||
n_valid = false;
|
||||
break;
|
||||
}
|
||||
product = std::move(next);
|
||||
}
|
||||
n_valid = n_valid && BN_cmp(product.get(), pub.n) == 0;
|
||||
BN_CTX_free(ctx);
|
||||
if (!n_valid) {
|
||||
if (!rsa_view.checkPrimeProduct()) {
|
||||
THROW_ERR_CRYPTO_INVALID_JWK(env, "Invalid JWK RSA key");
|
||||
return {};
|
||||
}
|
||||
@@ -624,7 +607,7 @@ bool GetRsaKeyDetail(Environment* env,
|
||||
return false;
|
||||
}
|
||||
|
||||
if (m_pkey.id() == EVP_PKEY_RSA_PSS) {
|
||||
if (m_pkey.isA(KeyAlgorithm::RSA_PSS)) {
|
||||
// Due to the way ASN.1 encoding works, default values are omitted when
|
||||
// encoding the data structure. However, there are also RSA-PSS keys for
|
||||
// which no parameters are set. In that case, the ASN.1 RSASSA-PSS-params
|
||||
|
||||
+16
-202
@@ -22,7 +22,6 @@ using ncrypto::ClearErrorOnReturn;
|
||||
using ncrypto::DataPointer;
|
||||
using ncrypto::Digest;
|
||||
using ncrypto::ECDSASigPointer;
|
||||
using ncrypto::ECKeyPointer;
|
||||
using ncrypto::EVPKeyCtxPointer;
|
||||
using ncrypto::EVPKeyPointer;
|
||||
using ncrypto::EVPMDCtxPointer;
|
||||
@@ -47,12 +46,11 @@ using v8::Value;
|
||||
namespace crypto {
|
||||
namespace {
|
||||
int GetPaddingFromJS(const EVPKeyPointer& key, Local<Value> val) {
|
||||
int padding = key.getDefaultSignPadding();
|
||||
if (!val->IsUndefined()) [[likely]] {
|
||||
CHECK(val->IsInt32());
|
||||
padding = val.As<Int32>()->Value();
|
||||
return val.As<Int32>()->Value();
|
||||
}
|
||||
return padding;
|
||||
return key.getDefaultSignPadding();
|
||||
}
|
||||
|
||||
std::optional<int> GetSaltLenFromJS(Local<Value> val) {
|
||||
@@ -83,147 +81,6 @@ bool ApplyRSAOptions(const EVPKeyPointer& pkey,
|
||||
return true;
|
||||
}
|
||||
|
||||
constexpr size_t kEd25519PointSize = 32;
|
||||
constexpr size_t kEd448PointSize = 57;
|
||||
|
||||
// Ed25519 has cofactor 8, so the first eight entries are the full
|
||||
// canonical small-order subgroup: identity, one point of order 2,
|
||||
// two points of order 4, and four points of order 8.
|
||||
constexpr unsigned char kEd25519SmallOrderPoints[][kEd25519PointSize] = {
|
||||
// Identity.
|
||||
{0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
|
||||
// Order 2.
|
||||
{0xec, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f},
|
||||
// Order 4.
|
||||
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80},
|
||||
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
|
||||
// Order 8.
|
||||
{0xc7, 0x17, 0x6a, 0x70, 0x3d, 0x4d, 0xd8, 0x4f, 0xba, 0x3c, 0x0b,
|
||||
0x76, 0x0d, 0x10, 0x67, 0x0f, 0x2a, 0x20, 0x53, 0xfa, 0x2c, 0x39,
|
||||
0xcc, 0xc6, 0x4e, 0xc7, 0xfd, 0x77, 0x92, 0xac, 0x03, 0x7a},
|
||||
{0xc7, 0x17, 0x6a, 0x70, 0x3d, 0x4d, 0xd8, 0x4f, 0xba, 0x3c, 0x0b,
|
||||
0x76, 0x0d, 0x10, 0x67, 0x0f, 0x2a, 0x20, 0x53, 0xfa, 0x2c, 0x39,
|
||||
0xcc, 0xc6, 0x4e, 0xc7, 0xfd, 0x77, 0x92, 0xac, 0x03, 0xfa},
|
||||
{0x26, 0xe8, 0x95, 0x8f, 0xc2, 0xb2, 0x27, 0xb0, 0x45, 0xc3, 0xf4,
|
||||
0x89, 0xf2, 0xef, 0x98, 0xf0, 0xd5, 0xdf, 0xac, 0x05, 0xd3, 0xc6,
|
||||
0x33, 0x39, 0xb1, 0x38, 0x02, 0x88, 0x6d, 0x53, 0xfc, 0x05},
|
||||
{0x26, 0xe8, 0x95, 0x8f, 0xc2, 0xb2, 0x27, 0xb0, 0x45, 0xc3, 0xf4,
|
||||
0x89, 0xf2, 0xef, 0x98, 0xf0, 0xd5, 0xdf, 0xac, 0x05, 0xd3, 0xc6,
|
||||
0x33, 0x39, 0xb1, 0x38, 0x02, 0x88, 0x6d, 0x53, 0xfc, 0x85},
|
||||
// Non-canonical encodings of the same small-order points.
|
||||
{0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80},
|
||||
{0xec, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
|
||||
{0xee, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f},
|
||||
{0xee, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
|
||||
{0xed, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
|
||||
{0xed, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f},
|
||||
};
|
||||
|
||||
// Ed448 has cofactor 4, so these four entries are the full canonical
|
||||
// small-order subgroup: identity, one point of order 2, and two points
|
||||
// of order 4.
|
||||
constexpr unsigned char kEd448SmallOrderPoints[][kEd448PointSize] = {
|
||||
// Identity.
|
||||
{0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
|
||||
// Order 2.
|
||||
{0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00},
|
||||
// Order 4.
|
||||
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
|
||||
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80},
|
||||
};
|
||||
|
||||
template <size_t PointSize, size_t Count>
|
||||
bool ContainsPoint(const unsigned char* candidate,
|
||||
const unsigned char (&points)[Count][PointSize]) {
|
||||
for (const auto& point : points) {
|
||||
if (memcmp(candidate, point, PointSize) == 0) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool IsSmallOrderEdDsaPoint(int id,
|
||||
const unsigned char* candidate,
|
||||
size_t size) {
|
||||
switch (id) {
|
||||
case EVP_PKEY_ED25519:
|
||||
return size == kEd25519PointSize &&
|
||||
ContainsPoint(candidate, kEd25519SmallOrderPoints);
|
||||
case EVP_PKEY_ED448:
|
||||
return size == kEd448PointSize &&
|
||||
ContainsPoint(candidate, kEd448SmallOrderPoints);
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool HasSmallOrderEdDsaPoint(const EVPKeyPointer& key,
|
||||
const ByteSource& signature) {
|
||||
const int id = key.id();
|
||||
size_t point_size;
|
||||
|
||||
switch (id) {
|
||||
case EVP_PKEY_ED25519:
|
||||
point_size = kEd25519PointSize;
|
||||
break;
|
||||
case EVP_PKEY_ED448:
|
||||
point_size = kEd448PointSize;
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
|
||||
if (signature.size() != point_size * 2) return false;
|
||||
|
||||
if (IsSmallOrderEdDsaPoint(id, signature.data<unsigned char>(), point_size)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
unsigned char raw_public_key[kEd448PointSize];
|
||||
size_t raw_public_key_size = point_size;
|
||||
if (EVP_PKEY_get_raw_public_key(
|
||||
key.get(), raw_public_key, &raw_public_key_size) != 1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return IsSmallOrderEdDsaPoint(id, raw_public_key, raw_public_key_size);
|
||||
}
|
||||
|
||||
std::unique_ptr<BackingStore> Node_SignFinal(Environment* env,
|
||||
EVPMDCtxPointer&& mdctx,
|
||||
const EVPKeyPointer& pkey,
|
||||
@@ -380,54 +237,7 @@ void CheckThrow(Environment* env, SignBase::Error error) {
|
||||
}
|
||||
|
||||
bool UseP1363Encoding(const EVPKeyPointer& key, const DSASigEnc dsa_encoding) {
|
||||
return key.isSigVariant() && dsa_encoding == DSASigEnc::P1363;
|
||||
}
|
||||
|
||||
bool SupportsContextString(const EVPKeyPointer& key) {
|
||||
if (!OPENSSL_WITH_SIGNATURE_CONTEXT_STRING) return false;
|
||||
|
||||
const int id = key.id();
|
||||
#if OPENSSL_WITH_PQC
|
||||
if (IsPqcSignatureKeyId(id)) return true;
|
||||
#endif
|
||||
#ifndef OPENSSL_IS_BORINGSSL
|
||||
if (id == EVP_PKEY_ED25519 || id == EVP_PKEY_ED448) return true;
|
||||
#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
|
||||
return dsa_encoding == DSASigEnc::P1363 && key.isSigVariant();
|
||||
}
|
||||
|
||||
bool CanUsePrehashedFallback(const EVPKeyPointer& key,
|
||||
@@ -439,7 +249,7 @@ bool CanUsePrehashedFallback(const EVPKeyPointer& key,
|
||||
|
||||
// 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);
|
||||
return key.isSigVariant() && !key.mayBeSM2();
|
||||
}
|
||||
|
||||
ByteSource SignPrehashed(Environment* env,
|
||||
@@ -644,7 +454,8 @@ void Sign::SignFinal(const FunctionCallbackInfo<Value>& args) {
|
||||
if (!key) [[unlikely]]
|
||||
return;
|
||||
|
||||
if (key.isOneShotVariant()) [[unlikely]] {
|
||||
const auto* algorithm = key.getAlgorithm();
|
||||
if (algorithm != nullptr && algorithm->isOneShot()) [[unlikely]] {
|
||||
THROW_ERR_CRYPTO_UNSUPPORTED_OPERATION(env);
|
||||
return;
|
||||
}
|
||||
@@ -761,7 +572,8 @@ void Verify::VerifyFinal(const FunctionCallbackInfo<Value>& args) {
|
||||
if (!key) [[unlikely]]
|
||||
return;
|
||||
|
||||
if (key.isOneShotVariant()) [[unlikely]] {
|
||||
const auto* algorithm = key.getAlgorithm();
|
||||
if (algorithm != nullptr && algorithm->isOneShot()) [[unlikely]] {
|
||||
THROW_ERR_CRYPTO_UNSUPPORTED_OPERATION(env);
|
||||
return;
|
||||
}
|
||||
@@ -924,14 +736,15 @@ bool SignTraits::DeriveBits(Environment* env,
|
||||
bool has_context = (params.flags & SignConfiguration::kHasContextString &&
|
||||
params.context_string.size() > 0);
|
||||
|
||||
if (has_context && !SupportsContextString(key)) {
|
||||
if (has_context && !key.supportsContextString()) {
|
||||
if (can_throw) crypto::CheckThrow(env, SignBase::Error::ContextUnsupported);
|
||||
return false;
|
||||
}
|
||||
|
||||
int padding = params.flags & SignConfiguration::kHasPadding
|
||||
? params.padding
|
||||
: key.getDefaultSignPadding();
|
||||
int padding = params.padding;
|
||||
if (!(params.flags & SignConfiguration::kHasPadding)) {
|
||||
padding = key.getDefaultSignPadding();
|
||||
}
|
||||
|
||||
std::optional<int> salt_length =
|
||||
params.flags & SignConfiguration::kHasSaltLength
|
||||
@@ -978,7 +791,8 @@ bool SignTraits::DeriveBits(Environment* env,
|
||||
|
||||
switch (params.mode) {
|
||||
case SignConfiguration::Mode::Sign: {
|
||||
if (key.isOneShotVariant()) {
|
||||
const auto* algorithm = key.getAlgorithm();
|
||||
if (algorithm != nullptr && algorithm->isOneShot()) {
|
||||
auto data = context.signOneShot(params.data);
|
||||
if (!data) [[unlikely]] {
|
||||
if (can_throw) crypto::CheckThrow(env, SignBase::Error::PrivateKey);
|
||||
@@ -1029,7 +843,7 @@ bool SignTraits::DeriveBits(Environment* env,
|
||||
// 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)) {
|
||||
!key.hasSmallOrderEdDsaPoint(params.signature)) {
|
||||
static_cast<char*>(buf.get())[0] = 1;
|
||||
} else if (verify_result < 0 &&
|
||||
CanUsePrehashedFallback(key, params.digest, has_context) &&
|
||||
|
||||
@@ -2062,19 +2062,19 @@ void TLSWrap::GetSharedSigalgs(const FunctionCallbackInfo<Value>& args) {
|
||||
nullptr);
|
||||
|
||||
switch (sign_nid) {
|
||||
case EVP_PKEY_RSA:
|
||||
case NID_rsaEncryption:
|
||||
sig_with_md = "RSA+";
|
||||
break;
|
||||
|
||||
case EVP_PKEY_RSA_PSS:
|
||||
case NID_rsassaPss:
|
||||
sig_with_md = "RSA-PSS+";
|
||||
break;
|
||||
|
||||
case EVP_PKEY_DSA:
|
||||
case NID_dsa:
|
||||
sig_with_md = "DSA+";
|
||||
break;
|
||||
|
||||
case EVP_PKEY_EC:
|
||||
case NID_X9_62_id_ecPublicKey:
|
||||
sig_with_md = "ECDSA+";
|
||||
break;
|
||||
|
||||
|
||||
@@ -527,6 +527,8 @@ void InitCryptoOnce() {
|
||||
|
||||
OPENSSL_init_ssl(0, settings);
|
||||
InstallFipsIndicatorCallback();
|
||||
|
||||
ncrypto::ConfigurePqcEncoding();
|
||||
OPENSSL_INIT_free(settings);
|
||||
settings = nullptr;
|
||||
|
||||
|
||||
@@ -105,33 +105,6 @@
|
||||
V(code_string, "code") \
|
||||
V(config_string, "config") \
|
||||
V(constants_string, "constants") \
|
||||
V(crypto_dh_string, "dh") \
|
||||
V(crypto_dsa_string, "dsa") \
|
||||
V(crypto_ec_string, "ec") \
|
||||
V(crypto_ed25519_string, "ed25519") \
|
||||
V(crypto_ed448_string, "ed448") \
|
||||
V(crypto_ml_dsa_44_string, "ml-dsa-44") \
|
||||
V(crypto_ml_dsa_65_string, "ml-dsa-65") \
|
||||
V(crypto_ml_dsa_87_string, "ml-dsa-87") \
|
||||
V(crypto_ml_kem_512_string, "ml-kem-512") \
|
||||
V(crypto_ml_kem_768_string, "ml-kem-768") \
|
||||
V(crypto_ml_kem_1024_string, "ml-kem-1024") \
|
||||
V(crypto_slh_dsa_sha2_128f_string, "slh-dsa-sha2-128f") \
|
||||
V(crypto_slh_dsa_sha2_128s_string, "slh-dsa-sha2-128s") \
|
||||
V(crypto_slh_dsa_sha2_192f_string, "slh-dsa-sha2-192f") \
|
||||
V(crypto_slh_dsa_sha2_192s_string, "slh-dsa-sha2-192s") \
|
||||
V(crypto_slh_dsa_sha2_256f_string, "slh-dsa-sha2-256f") \
|
||||
V(crypto_slh_dsa_sha2_256s_string, "slh-dsa-sha2-256s") \
|
||||
V(crypto_slh_dsa_shake_128f_string, "slh-dsa-shake-128f") \
|
||||
V(crypto_slh_dsa_shake_128s_string, "slh-dsa-shake-128s") \
|
||||
V(crypto_slh_dsa_shake_192f_string, "slh-dsa-shake-192f") \
|
||||
V(crypto_slh_dsa_shake_192s_string, "slh-dsa-shake-192s") \
|
||||
V(crypto_slh_dsa_shake_256f_string, "slh-dsa-shake-256f") \
|
||||
V(crypto_slh_dsa_shake_256s_string, "slh-dsa-shake-256s") \
|
||||
V(crypto_x25519_string, "x25519") \
|
||||
V(crypto_x448_string, "x448") \
|
||||
V(crypto_rsa_string, "rsa") \
|
||||
V(crypto_rsa_pss_string, "rsa-pss") \
|
||||
V(cwd_string, "cwd") \
|
||||
V(data_string, "data") \
|
||||
V(database_string, "database") \
|
||||
|
||||
@@ -10,6 +10,11 @@
|
||||
|
||||
#include <climits>
|
||||
|
||||
using ncrypto::Ec;
|
||||
using ncrypto::EVPKeyCtxPointer;
|
||||
using ncrypto::EVPKeyPointer;
|
||||
using ncrypto::KeyAlgorithm;
|
||||
|
||||
/*
|
||||
* This test verifies that a call to NewRootCertDir with the build time
|
||||
* configuration option --openssl-system-ca-path set to an missing file, will
|
||||
@@ -65,3 +70,419 @@ TEST(NodeCrypto, TryGetIntCipherOutputLength) {
|
||||
EXPECT_FALSE(node::crypto::TryGetIntCipherOutputLength(
|
||||
0, static_cast<size_t>(INT_MAX) + 1, &output_len));
|
||||
}
|
||||
|
||||
TEST(NodeCrypto, KeyAlgorithmNames) {
|
||||
EXPECT_EQ(KeyAlgorithm::FromName("rsa-pss"), &KeyAlgorithm::RSA_PSS);
|
||||
EXPECT_EQ(KeyAlgorithm::FromName("ML-DSA-44"), &KeyAlgorithm::ML_DSA_44);
|
||||
EXPECT_EQ(KeyAlgorithm::FromName("unknown-key-algorithm"), nullptr);
|
||||
EXPECT_EQ(KeyAlgorithm::FromName(nullptr), nullptr);
|
||||
EXPECT_FALSE(EVPKeyPointer().isA(KeyAlgorithm::RSA));
|
||||
EXPECT_FALSE(EVPKeyPointer::isA(nullptr, KeyAlgorithm::RSA));
|
||||
auto empty = EVPKeyPointer::New();
|
||||
ASSERT_TRUE(empty);
|
||||
EXPECT_FALSE(empty.isA(KeyAlgorithm::RSA));
|
||||
EXPECT_FALSE(empty.supportsContextString());
|
||||
EXPECT_FALSE(EVPKeyPointer().supportsContextString());
|
||||
EXPECT_FALSE(empty.rawSeed());
|
||||
EXPECT_FALSE(EVPKeyPointer().rawSeed());
|
||||
EXPECT_FALSE(empty.isA("unknown-key-algorithm"));
|
||||
EXPECT_FALSE(empty.isA(static_cast<const char*>(nullptr)));
|
||||
}
|
||||
|
||||
TEST(NodeCrypto, UnsupportedRawExports) {
|
||||
using Error = EVPKeyPointer::RawExportError;
|
||||
EVPKeyPointer key;
|
||||
for (int i = 0; i < 2; i++) {
|
||||
const auto seed = key.rawSeed();
|
||||
EXPECT_FALSE(seed);
|
||||
EXPECT_EQ(seed.error, Error::UNSUPPORTED_KEY_TYPE);
|
||||
const auto jwk = key.exportRawJwk(/* include_private */ false);
|
||||
EXPECT_FALSE(jwk);
|
||||
EXPECT_EQ(jwk.error, Error::UNSUPPORTED_KEY_TYPE);
|
||||
key = EVPKeyPointer::New();
|
||||
ASSERT_TRUE(key);
|
||||
}
|
||||
auto ctx = EVPKeyCtxPointer::NewFromAlgorithm(KeyAlgorithm::RSA);
|
||||
ASSERT_TRUE(ctx);
|
||||
ASSERT_EQ(EVP_PKEY_keygen_init(ctx.get()), 1);
|
||||
EVP_PKEY* raw = nullptr;
|
||||
ASSERT_EQ(EVP_PKEY_keygen(ctx.get(), &raw), 1);
|
||||
key.reset(raw);
|
||||
EXPECT_EQ(key.rawSeed().error, Error::UNSUPPORTED_KEY_TYPE);
|
||||
EXPECT_EQ(key.exportRawJwk(/* include_private */ false).error,
|
||||
Error::UNSUPPORTED_KEY_TYPE);
|
||||
}
|
||||
|
||||
namespace {
|
||||
void CheckRawJwkImport(const EVPKeyPointer& key) {
|
||||
auto jwk = key.exportRawJwk(/* include_private */ true);
|
||||
ASSERT_TRUE(jwk);
|
||||
const auto& data = jwk.value;
|
||||
const ncrypto::Buffer<const unsigned char> pub = data.public_key;
|
||||
const ncrypto::Buffer<const unsigned char> priv = data.private_key;
|
||||
auto private_key = EVPKeyPointer::NewRawJwk(*data.algorithm, pub, priv);
|
||||
ASSERT_TRUE(private_key);
|
||||
auto exported = private_key.exportRawJwk(/* include_private */ true);
|
||||
ASSERT_TRUE(exported);
|
||||
EXPECT_EQ(exported.value.algorithm, data.algorithm);
|
||||
ASSERT_EQ(exported.value.private_key.size(), priv.len);
|
||||
EXPECT_EQ(memcmp(exported.value.private_key.get(), priv.data, priv.len), 0);
|
||||
auto public_key = EVPKeyPointer::NewRawJwk(*data.algorithm, pub);
|
||||
ASSERT_TRUE(public_key);
|
||||
auto exported_public = public_key.exportRawJwk(/* include_private */ false);
|
||||
ASSERT_TRUE(exported_public);
|
||||
EXPECT_FALSE(exported_public.value.private_key);
|
||||
ASSERT_EQ(exported_public.value.public_key.size(), pub.len);
|
||||
EXPECT_EQ(memcmp(exported_public.value.public_key.get(), pub.data, pub.len),
|
||||
0);
|
||||
|
||||
auto different_pub = ncrypto::DataPointer::Copy({pub.data, pub.len});
|
||||
ASSERT_TRUE(different_pub);
|
||||
ASSERT_GT(different_pub.size(), 0u);
|
||||
different_pub.get<unsigned char>()[0] ^= 1;
|
||||
const ncrypto::Buffer<const unsigned char> mismatch = different_pub;
|
||||
EXPECT_FALSE(EVPKeyPointer::NewRawJwk(*data.algorithm, mismatch, priv));
|
||||
EXPECT_FALSE(EVPKeyPointer::NewRawJwk(
|
||||
*data.algorithm, pub, ncrypto::Buffer<const unsigned char>{nullptr, 0}));
|
||||
EXPECT_FALSE(EVPKeyPointer::NewRawJwk(KeyAlgorithm::RSA, pub, priv));
|
||||
}
|
||||
} // namespace
|
||||
|
||||
TEST(NodeCrypto, RsaPrimeProduct) {
|
||||
ncrypto::ClearErrorOnReturn clear_errors;
|
||||
EXPECT_FALSE(ncrypto::Rsa().checkPrimeProduct());
|
||||
auto ctx = EVPKeyCtxPointer::NewFromAlgorithm(KeyAlgorithm::RSA);
|
||||
ASSERT_TRUE(ctx);
|
||||
ASSERT_TRUE(ctx.initForKeygen());
|
||||
EVP_PKEY* raw = nullptr;
|
||||
ASSERT_EQ(EVP_PKEY_keygen(ctx.get(), &raw), 1);
|
||||
EVPKeyPointer key(raw);
|
||||
ncrypto::Rsa rsa = key;
|
||||
ASSERT_TRUE(rsa);
|
||||
EXPECT_TRUE(rsa.checkPrimeProduct());
|
||||
auto pub = rsa.getPublicKey();
|
||||
ncrypto::BignumPointer n(BN_dup(pub.n));
|
||||
ncrypto::BignumPointer e(BN_dup(pub.e));
|
||||
ASSERT_TRUE(n);
|
||||
ASSERT_TRUE(e);
|
||||
ASSERT_EQ(BN_add_word(n.get(), 1), 1);
|
||||
ASSERT_TRUE(rsa.setPublicKey(std::move(n), std::move(e)));
|
||||
EXPECT_FALSE(rsa.checkPrimeProduct());
|
||||
}
|
||||
|
||||
TEST(NodeCrypto, EcKeyComponents) {
|
||||
ncrypto::ClearErrorOnReturn clear_errors;
|
||||
ncrypto::BignumPointer x;
|
||||
ncrypto::BignumPointer y;
|
||||
ncrypto::BignumPointer priv;
|
||||
int degree = 0;
|
||||
EXPECT_FALSE(
|
||||
ncrypto::Ec::GetKeyComponents(EVPKeyPointer(), &x, &y, &priv, °ree));
|
||||
EXPECT_EQ(ncrypto::Ec::GetCurveId(EVPKeyPointer()), NID_undef);
|
||||
EXPECT_FALSE(ncrypto::Ec::TryExportPublic(EVPKeyPointer(),
|
||||
POINT_CONVERSION_UNCOMPRESSED));
|
||||
EXPECT_FALSE(ncrypto::Ec::ExportPrivate(EVPKeyPointer()));
|
||||
auto empty = EVPKeyPointer::New();
|
||||
ASSERT_TRUE(empty);
|
||||
EXPECT_FALSE(ncrypto::Ec::GetKeyComponents(empty, &x, &y, &priv, °ree));
|
||||
for (int nid : {NID_X9_62_prime256v1, NID_secp384r1, NID_secp521r1}) {
|
||||
auto ec = ncrypto::ECKeyPointer::NewByCurveName(nid);
|
||||
ASSERT_TRUE(ec);
|
||||
EXPECT_FALSE(ec.checkPrivateKey());
|
||||
auto parameters = EVPKeyPointer::New();
|
||||
ASSERT_TRUE(parameters);
|
||||
ASSERT_TRUE(parameters.set(ec));
|
||||
EXPECT_EQ(ncrypto::Ec::GetCurveId(parameters), nid);
|
||||
EXPECT_FALSE(
|
||||
ncrypto::Ec::GetKeyComponents(parameters, &x, &y, nullptr, °ree));
|
||||
ASSERT_TRUE(ec.generate());
|
||||
EXPECT_TRUE(ec.checkPrivateKey());
|
||||
auto key = EVPKeyPointer::New();
|
||||
ASSERT_TRUE(key);
|
||||
ASSERT_TRUE(key.set(ec));
|
||||
EXPECT_TRUE(key.isA(KeyAlgorithm::EC));
|
||||
EXPECT_EQ(ncrypto::Ec::GetCurveId(key), nid);
|
||||
ASSERT_TRUE(ncrypto::Ec::GetKeyComponents(key, &x, &y, &priv, °ree));
|
||||
EXPECT_EQ(degree, EC_GROUP_get_degree(ec.getGroup()));
|
||||
EXPECT_EQ(BN_cmp(priv.get(), ec.getPrivateKey()), 0);
|
||||
const size_t width = (degree + 7) / 8;
|
||||
auto point = ncrypto::DataPointer::Alloc(1 + 2 * width);
|
||||
ASSERT_TRUE(point);
|
||||
ASSERT_EQ(EC_POINT_point2oct(ec.getGroup(),
|
||||
ec.getPublicKey(),
|
||||
POINT_CONVERSION_UNCOMPRESSED,
|
||||
point.get<unsigned char>(),
|
||||
point.size(),
|
||||
nullptr),
|
||||
point.size());
|
||||
auto x_bytes = x.encodePadded(width);
|
||||
auto y_bytes = y.encodePadded(width);
|
||||
ASSERT_TRUE(x_bytes);
|
||||
ASSERT_TRUE(y_bytes);
|
||||
EXPECT_EQ(memcmp(x_bytes.get(), point.get<unsigned char>() + 1, width), 0);
|
||||
EXPECT_EQ(
|
||||
memcmp(y_bytes.get(), point.get<unsigned char>() + 1 + width, width),
|
||||
0);
|
||||
auto raw_private = ncrypto::Ec::ExportPrivate(key);
|
||||
auto expected_private = priv.encodePadded(width);
|
||||
ASSERT_TRUE(raw_private);
|
||||
ASSERT_EQ(raw_private.size(), expected_private.size());
|
||||
EXPECT_EQ(
|
||||
memcmp(raw_private.get(), expected_private.get(), raw_private.size()),
|
||||
0);
|
||||
auto raw_public =
|
||||
ncrypto::Ec::TryExportPublic(key, POINT_CONVERSION_UNCOMPRESSED);
|
||||
#if NCRYPTO_USE_OPENSSL3_PROVIDER
|
||||
ASSERT_TRUE(raw_public);
|
||||
ASSERT_EQ(raw_public.size(), point.size());
|
||||
EXPECT_EQ(memcmp(raw_public.get(), point.get(), point.size()), 0);
|
||||
#else
|
||||
EXPECT_FALSE(raw_public);
|
||||
#endif
|
||||
EXPECT_FALSE(
|
||||
ncrypto::Ec::TryExportPublic(key, POINT_CONVERSION_COMPRESSED));
|
||||
auto public_ec = ncrypto::ECKeyPointer::NewByCurveName(nid);
|
||||
ASSERT_TRUE(public_ec);
|
||||
ASSERT_TRUE(public_ec.setPublicKeyRaw(x, y));
|
||||
auto public_key = EVPKeyPointer::New();
|
||||
ASSERT_TRUE(public_key);
|
||||
ASSERT_TRUE(public_key.set(public_ec));
|
||||
EXPECT_TRUE(
|
||||
ncrypto::Ec::GetKeyComponents(public_key, &x, &y, nullptr, °ree));
|
||||
EXPECT_FALSE(
|
||||
ncrypto::Ec::GetKeyComponents(public_key, &x, &y, &priv, °ree));
|
||||
EXPECT_FALSE(ncrypto::Ec::ExportPrivate(public_key));
|
||||
}
|
||||
}
|
||||
|
||||
TEST(NodeCrypto, ResolveKeyAlgorithm) {
|
||||
ncrypto::ClearErrorOnReturn clear_errors;
|
||||
EXPECT_EQ(EVPKeyPointer().getAlgorithm(), nullptr);
|
||||
auto key = EVPKeyPointer::New();
|
||||
ASSERT_TRUE(key);
|
||||
EXPECT_EQ(key.getAlgorithm(), nullptr);
|
||||
|
||||
const unsigned char seed[64] = {};
|
||||
for (const auto* algorithm : {&KeyAlgorithm::ED25519,
|
||||
&KeyAlgorithm::X25519,
|
||||
&KeyAlgorithm::ML_DSA_44,
|
||||
&KeyAlgorithm::ML_KEM_768}) {
|
||||
if (!algorithm->isAvailable()) continue;
|
||||
const ncrypto::Buffer<const unsigned char> input{
|
||||
seed, algorithm->seedSize() == 0 ? 32 : algorithm->seedSize()};
|
||||
key = algorithm->seedSize() == 0
|
||||
? EVPKeyPointer::NewRawPrivate(*algorithm, input)
|
||||
: EVPKeyPointer::NewRawSeed(*algorithm, input);
|
||||
ASSERT_TRUE(key);
|
||||
EXPECT_EQ(key.getAlgorithm(), algorithm);
|
||||
CheckRawJwkImport(key);
|
||||
if (algorithm->seedSize() != 0) {
|
||||
auto exported = key.rawSeed();
|
||||
ASSERT_TRUE(exported);
|
||||
ASSERT_EQ(exported.value.size(), input.len);
|
||||
EXPECT_EQ(memcmp(exported.value.get(), input.data, input.len), 0);
|
||||
}
|
||||
EVPKeyPointer moved(std::move(key));
|
||||
EXPECT_EQ(key.getAlgorithm(), nullptr);
|
||||
EXPECT_EQ(moved.getAlgorithm(), algorithm);
|
||||
key.reset(moved.release());
|
||||
EXPECT_EQ(moved.getAlgorithm(), nullptr);
|
||||
EXPECT_EQ(key.getAlgorithm(), algorithm);
|
||||
key.reset();
|
||||
EXPECT_EQ(key.getAlgorithm(), nullptr);
|
||||
}
|
||||
#if NCRYPTO_USE_OPENSSL3_PROVIDER
|
||||
// Legacy keys must resolve even without provider-backed key material.
|
||||
key = EVPKeyPointer::New();
|
||||
ASSERT_EQ(EVP_PKEY_set_type(key.get(), NID_rsaEncryption), 1);
|
||||
EXPECT_EQ(key.getAlgorithm(), &KeyAlgorithm::RSA);
|
||||
#endif
|
||||
}
|
||||
|
||||
TEST(NodeCrypto, PublicKeyTypeNames) {
|
||||
EXPECT_EQ(EVPKeyPointer().getKeyTypeName(), nullptr);
|
||||
EXPECT_EQ(EVPKeyPointer::New().getKeyTypeName(), nullptr);
|
||||
EXPECT_STREQ(KeyAlgorithm::RSA.keyTypeName(), "rsa");
|
||||
EXPECT_STREQ(KeyAlgorithm::RSA_PSS.keyTypeName(), "rsa-pss");
|
||||
EXPECT_STREQ(KeyAlgorithm::ML_DSA_44.keyTypeName(), "ml-dsa-44");
|
||||
EXPECT_STREQ(KeyAlgorithm::SLH_DSA_SHAKE_256S.keyTypeName(),
|
||||
"slh-dsa-shake-256s");
|
||||
EXPECT_EQ(KeyAlgorithm::SM2.keyTypeName(), nullptr);
|
||||
const unsigned char seed[32] = {};
|
||||
auto key =
|
||||
EVPKeyPointer::NewRawPrivate(KeyAlgorithm::ED25519, {seed, sizeof(seed)});
|
||||
ASSERT_TRUE(key);
|
||||
EXPECT_STREQ(key.getKeyTypeName(), "ed25519");
|
||||
}
|
||||
|
||||
#if NCRYPTO_USE_OPENSSL3_PROVIDER
|
||||
TEST(NodeCrypto, LegacyKeyAlgorithmResolution) {
|
||||
ncrypto::ClearErrorOnReturn clear_errors;
|
||||
auto key = EVPKeyPointer::New();
|
||||
ASSERT_TRUE(key);
|
||||
ASSERT_EQ(EVP_PKEY_set_type(key.get(), NID_rsassaPss), 1);
|
||||
EXPECT_EQ(key.getAlgorithm(), &KeyAlgorithm::RSA_PSS);
|
||||
EXPECT_STREQ(key.getKeyTypeName(), "rsa-pss");
|
||||
#ifndef OPENSSL_NO_SM2
|
||||
ASSERT_EQ(EVP_PKEY_set_type(key.get(), NID_sm2), 1);
|
||||
EXPECT_TRUE(key.isA(KeyAlgorithm::SM2));
|
||||
EXPECT_FALSE(key.isA(KeyAlgorithm::EC));
|
||||
EXPECT_EQ(key.getAlgorithm(), &KeyAlgorithm::SM2);
|
||||
EXPECT_EQ(key.getKeyTypeName(), nullptr);
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
|
||||
TEST(NodeCrypto, NamedKeysAndEcCurves) {
|
||||
EXPECT_EQ(Ec::GetNamedKeyAlgorithm("ED25519"), &KeyAlgorithm::ED25519);
|
||||
EXPECT_EQ(Ec::GetNamedKeyAlgorithm("X25519"), &KeyAlgorithm::X25519);
|
||||
EXPECT_EQ(Ec::GetNamedKeyAlgorithm("P-256"), nullptr);
|
||||
EXPECT_EQ(Ec::GetNamedKeyAlgorithm("prime256v1"), nullptr);
|
||||
EXPECT_EQ(Ec::GetNamedKeyAlgorithm("unknown-curve"), nullptr);
|
||||
EXPECT_EQ(Ec::GetCurveIdFromName("P-256"),
|
||||
Ec::GetCurveIdFromName("prime256v1"));
|
||||
}
|
||||
|
||||
TEST(NodeCrypto, NamedRawKey) {
|
||||
const unsigned char seed[32] = {};
|
||||
const ncrypto::Buffer<const unsigned char> input{seed, sizeof(seed)};
|
||||
auto key = EVPKeyPointer::NewRawPrivate(KeyAlgorithm::ED25519, input);
|
||||
ASSERT_TRUE(key);
|
||||
EXPECT_TRUE(key.isA(KeyAlgorithm::ED25519));
|
||||
EXPECT_FALSE(key.isA(KeyAlgorithm::X25519));
|
||||
ASSERT_EQ(key.getAlgorithm(), &KeyAlgorithm::ED25519);
|
||||
EXPECT_TRUE(key.getAlgorithm()->isOneShot());
|
||||
EXPECT_TRUE(key.supportsRawPublic());
|
||||
EXPECT_TRUE(key.supportsRawPrivate());
|
||||
EXPECT_EQ(key.getAlgorithm()->seedSize(), 0);
|
||||
#if NCRYPTO_USE_OPENSSL3_PROVIDER
|
||||
// Provider aliases are recognized without comparing the primary type name.
|
||||
EXPECT_TRUE(key.isA("1.3.101.112"));
|
||||
auto ctx = ncrypto::EVPKeyCtxPointer::NewFromName("1.3.101.112");
|
||||
ASSERT_TRUE(ctx);
|
||||
ASSERT_TRUE(ctx.initForKeygen());
|
||||
EVP_PKEY* generated = nullptr;
|
||||
ASSERT_EQ(EVP_PKEY_keygen(ctx.get(), &generated), 1);
|
||||
EXPECT_TRUE(EVPKeyPointer(generated).isA(KeyAlgorithm::ED25519));
|
||||
#endif
|
||||
auto pub = key.rawPublicKey();
|
||||
ASSERT_TRUE(pub);
|
||||
auto imported = EVPKeyPointer::NewRawPublic(KeyAlgorithm::ED25519, pub);
|
||||
ASSERT_TRUE(imported);
|
||||
EXPECT_TRUE(imported.isA(KeyAlgorithm::ED25519));
|
||||
auto exported = imported.rawPublicKey();
|
||||
ASSERT_EQ(pub.size(), exported.size());
|
||||
EXPECT_EQ(memcmp(pub.get(), exported.get(), pub.size()), 0);
|
||||
}
|
||||
|
||||
TEST(NodeCrypto, NamedRsaPssKey) {
|
||||
ncrypto::ClearErrorOnReturn clear_errors;
|
||||
EXPECT_FALSE(EVPKeyCtxPointer::NewFromName("unknown-key-algorithm"));
|
||||
EXPECT_FALSE(EVPKeyCtxPointer::NewFromName(nullptr));
|
||||
#ifndef OPENSSL_IS_BORINGSSL
|
||||
auto ctx = EVPKeyCtxPointer::NewFromAlgorithm(KeyAlgorithm::RSA_PSS);
|
||||
ASSERT_TRUE(ctx);
|
||||
ASSERT_TRUE(ctx.initForKeygen());
|
||||
ASSERT_TRUE(ctx.setRsaKeygenBits(2048));
|
||||
EVP_PKEY* raw = nullptr;
|
||||
ASSERT_EQ(EVP_PKEY_keygen(ctx.get(), &raw), 1);
|
||||
EVPKeyPointer key(raw);
|
||||
EXPECT_TRUE(key.isA(KeyAlgorithm::RSA_PSS));
|
||||
EXPECT_FALSE(key.isA(KeyAlgorithm::RSA));
|
||||
EXPECT_FALSE(key.supportsContextString());
|
||||
EXPECT_TRUE(key.isRsaVariant());
|
||||
EXPECT_EQ(key.getDefaultSignPadding(), RSA_PKCS1_PSS_PADDING);
|
||||
EXPECT_FALSE(key.supportsRawPublic());
|
||||
ASSERT_EQ(key.getAlgorithm(), &KeyAlgorithm::RSA_PSS);
|
||||
EXPECT_FALSE(key.getAlgorithm()->isOneShot());
|
||||
#endif
|
||||
}
|
||||
|
||||
TEST(NodeCrypto, ProviderPqcKeyWithoutLegacyId) {
|
||||
ncrypto::ClearErrorOnReturn clear_errors;
|
||||
if (!KeyAlgorithm::ML_DSA_44.isAvailable() ||
|
||||
!KeyAlgorithm::ML_KEM_768.isAvailable())
|
||||
GTEST_SKIP();
|
||||
const unsigned char seed[64] = {};
|
||||
for (const auto* algorithm :
|
||||
{&KeyAlgorithm::ML_DSA_44, &KeyAlgorithm::ML_KEM_768}) {
|
||||
const ncrypto::Buffer<const unsigned char> input{seed,
|
||||
algorithm->seedSize()};
|
||||
auto key = EVPKeyPointer::NewRawSeed(*algorithm, input);
|
||||
ASSERT_TRUE(key);
|
||||
#if NCRYPTO_USE_OPENSSL3_PROVIDER
|
||||
EXPECT_EQ(EVP_PKEY_id(key.get()), -1);
|
||||
#endif
|
||||
EXPECT_TRUE(key.isA(*algorithm));
|
||||
ASSERT_EQ(key.getAlgorithm(), algorithm);
|
||||
EXPECT_EQ(key.getAlgorithm()->seedSize(), input.len);
|
||||
EXPECT_TRUE(key.supportsRawPublic());
|
||||
EXPECT_FALSE(key.supportsRawPrivate());
|
||||
auto exported_seed = key.rawSeed();
|
||||
ASSERT_TRUE(exported_seed);
|
||||
ASSERT_EQ(exported_seed.value.size(), input.len);
|
||||
EXPECT_EQ(memcmp(exported_seed.value.get(), seed, input.len), 0);
|
||||
auto pub = key.rawPublicKey();
|
||||
ASSERT_TRUE(pub);
|
||||
auto imported = EVPKeyPointer::NewRawPublic(*algorithm, pub);
|
||||
ASSERT_TRUE(imported);
|
||||
EXPECT_EQ(imported.getAlgorithm(), algorithm);
|
||||
|
||||
auto private_jwk = key.exportRawJwk(/* include_private */ true);
|
||||
ASSERT_TRUE(private_jwk);
|
||||
EXPECT_EQ(private_jwk.value.algorithm, algorithm);
|
||||
ASSERT_EQ(private_jwk.value.private_key.size(), input.len);
|
||||
EXPECT_EQ(memcmp(private_jwk.value.private_key.get(), seed, input.len), 0);
|
||||
ASSERT_EQ(private_jwk.value.public_key.size(), pub.size());
|
||||
EXPECT_EQ(memcmp(private_jwk.value.public_key.get(), pub.get(), pub.size()),
|
||||
0);
|
||||
auto public_jwk = imported.exportRawJwk(/* include_private */ false);
|
||||
ASSERT_TRUE(public_jwk);
|
||||
EXPECT_EQ(public_jwk.value.algorithm, algorithm);
|
||||
EXPECT_FALSE(public_jwk.value.private_key);
|
||||
ASSERT_EQ(public_jwk.value.public_key.size(), pub.size());
|
||||
EXPECT_EQ(memcmp(public_jwk.value.public_key.get(), pub.get(), pub.size()),
|
||||
0);
|
||||
EXPECT_EQ(imported.rawSeed().error,
|
||||
EVPKeyPointer::RawExportError::MISSING_SEED);
|
||||
EXPECT_EQ(imported.exportRawJwk(/* include_private */ true).error,
|
||||
EVPKeyPointer::RawExportError::MISSING_SEED);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(NodeCrypto, PqcJwkRawPrivateExport) {
|
||||
ncrypto::ClearErrorOnReturn clear_errors;
|
||||
const auto& algorithm = KeyAlgorithm::SLH_DSA_SHA2_128S;
|
||||
if (!algorithm.isAvailable()) GTEST_SKIP();
|
||||
auto ctx = EVPKeyCtxPointer::NewFromAlgorithm(algorithm);
|
||||
ASSERT_TRUE(ctx);
|
||||
ASSERT_EQ(EVP_PKEY_keygen_init(ctx.get()), 1);
|
||||
EVP_PKEY* raw = nullptr;
|
||||
ASSERT_EQ(EVP_PKEY_keygen(ctx.get(), &raw), 1);
|
||||
EVPKeyPointer key(raw);
|
||||
CheckRawJwkImport(key);
|
||||
auto priv = key.rawPrivateKey();
|
||||
auto pub = key.rawPublicKey();
|
||||
ASSERT_TRUE(priv);
|
||||
ASSERT_TRUE(pub);
|
||||
auto jwk = key.exportRawJwk(/* include_private */ true);
|
||||
ASSERT_TRUE(jwk);
|
||||
EXPECT_EQ(jwk.value.algorithm, &algorithm);
|
||||
ASSERT_EQ(jwk.value.private_key.size(), priv.size());
|
||||
EXPECT_EQ(memcmp(jwk.value.private_key.get(), priv.get(), priv.size()), 0);
|
||||
ASSERT_EQ(jwk.value.public_key.size(), pub.size());
|
||||
EXPECT_EQ(memcmp(jwk.value.public_key.get(), pub.get(), pub.size()), 0);
|
||||
EXPECT_EQ(key.rawSeed().error,
|
||||
EVPKeyPointer::RawExportError::UNSUPPORTED_KEY_TYPE);
|
||||
}
|
||||
|
||||
#ifdef OPENSSL_IS_BORINGSSL
|
||||
TEST(NodeCrypto, UnavailableBoringSSLKeyAlgorithms) {
|
||||
ncrypto::ClearErrorOnReturn clear_errors;
|
||||
for (const auto* algorithm :
|
||||
{&KeyAlgorithm::SM2, &KeyAlgorithm::X448, &KeyAlgorithm::ED448}) {
|
||||
EXPECT_FALSE(algorithm->isAvailable());
|
||||
EXPECT_FALSE(EVPKeyCtxPointer::NewFromAlgorithm(*algorithm));
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,43 @@
|
||||
'use strict';
|
||||
|
||||
const common = require('../common');
|
||||
if (!common.hasCrypto)
|
||||
common.skip('missing crypto');
|
||||
|
||||
const assert = require('node:assert');
|
||||
const { createPrivateKey } = require('node:crypto');
|
||||
const fixtures = require('../common/fixtures');
|
||||
const { hasOpenSSL, isBoringSSL } = require('../common/crypto');
|
||||
|
||||
const cases = [
|
||||
['ed25519_private.pem', 'OKP', 'crv', 'x', 'd'],
|
||||
];
|
||||
if (hasOpenSSL(3, 5) || isBoringSSL) {
|
||||
cases.push(['ml_dsa_44_private_seed_only.pem', 'AKP', 'alg', 'pub', 'priv']);
|
||||
}
|
||||
|
||||
for (const [file, kty, name, pub, priv] of cases) {
|
||||
const jwk = createPrivateKey(fixtures.readKey(file)).export({ format: 'jwk' });
|
||||
const invalid = { code: 'ERR_CRYPTO_INVALID_JWK', message: `Invalid JWK ${kty} key` };
|
||||
const invalidName = kty === 'AKP' ? {
|
||||
code: 'ERR_CRYPTO_INVALID_JWK', message: 'Unsupported JWK AKP "alg"',
|
||||
} : invalid;
|
||||
const importKey = (key) => createPrivateKey({ format: 'jwk', key });
|
||||
|
||||
assert.throws(() => importKey({ ...jwk, [name]: 'unknown' }), invalidName);
|
||||
assert.throws(() => importKey({ ...jwk, [name]: jwk[name].toLowerCase() }), invalidName);
|
||||
assert.throws(() => importKey({ ...jwk, [pub]: undefined }), invalid);
|
||||
assert.throws(() => importKey({ ...jwk, [priv]: 1 }), invalid);
|
||||
assert.throws(() => importKey({ ...jwk, [name]: 'unknown', [pub]: undefined }), invalidName);
|
||||
|
||||
// A recognized algorithm from the other schema remains invalid.
|
||||
const otherName = kty === 'AKP' ? 'Ed25519' : 'ML-DSA-44';
|
||||
assert.throws(() => importKey({ ...jwk, [name]: otherName }), invalidName);
|
||||
|
||||
for (const field of [name, pub, priv]) {
|
||||
const error = new Error(`getter for ${field}`);
|
||||
const key = { ...jwk };
|
||||
Object.defineProperty(key, field, { get() { throw error; } });
|
||||
assert.throws(() => importKey(key), (actual) => actual === error);
|
||||
}
|
||||
}
|
||||
@@ -79,7 +79,7 @@ for (const [asymmetricKeyType, pubLen] of [
|
||||
assertPublicKey(createPublicKey(key));
|
||||
key.export({ format: 'der', type: 'pkcs8' });
|
||||
if (hasSeed) {
|
||||
assert.strictEqual(key.export({ format: 'pem', type: 'pkcs8' }), keys.private);
|
||||
assert.strictEqual(key.export({ format: 'pem', type: 'pkcs8' }), keys.private_seed_only);
|
||||
const jwk = key.export({ format: 'jwk' });
|
||||
assertPrivateJwk(jwk);
|
||||
assert.strictEqual(key.equals(createPrivateKey({ format: 'jwk', key: jwk })), true);
|
||||
|
||||
@@ -79,7 +79,7 @@ for (const [asymmetricKeyType, pubLen] of [
|
||||
assertPublicKey(createPublicKey(key));
|
||||
key.export({ format: 'der', type: 'pkcs8' });
|
||||
if (hasSeed) {
|
||||
assert.strictEqual(key.export({ format: 'pem', type: 'pkcs8' }), keys.private);
|
||||
assert.strictEqual(key.export({ format: 'pem', type: 'pkcs8' }), keys.private_seed_only);
|
||||
const jwk = key.export({ format: 'jwk' });
|
||||
assertPrivateJwk(jwk);
|
||||
assert.strictEqual(key.equals(createPrivateKey({ format: 'jwk', key: jwk })), true);
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
'use strict';
|
||||
|
||||
const common = require('../common');
|
||||
if (!common.hasCrypto)
|
||||
common.skip('missing crypto');
|
||||
|
||||
const assert = require('node:assert');
|
||||
const { createPrivateKey, createPublicKey } = require('node:crypto');
|
||||
const { hasOpenSSL, hasFIPS, isBoringSSL } = require('../common/crypto');
|
||||
|
||||
const key = Buffer.alloc(32);
|
||||
const formats = ['raw-public', 'raw-private', 'raw-seed'];
|
||||
|
||||
// Recognized classical types reject raw encodings with a format error.
|
||||
for (const asymmetricKeyType of ['rsa', 'rsa-pss', 'dsa', 'dh']) {
|
||||
for (const format of formats) {
|
||||
assert.throws(() => createPublicKey({ key, format, asymmetricKeyType }), {
|
||||
code: 'ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS',
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Only exact public names are accepted, excluding aliases and unexposed types.
|
||||
for (const asymmetricKeyType of [
|
||||
'RSA', 'RSA-PSS', 'DSA', 'DH', 'EC', 'sm2', 'SM2', 'unknown',
|
||||
'Ed25519', 'ED25519', 'Ed448', 'ED448', 'X25519', 'X448',
|
||||
'ML-DSA-44', 'Ml-Dsa-65', 'ML-DSA-87',
|
||||
'ML-KEM-512', 'Ml-Kem-768', 'ML-KEM-1024', 'SLH-DSA-SHA2-128s',
|
||||
'rsaEncryption', 'id-ecPublicKey', '1.3.101.112', '1.3.101.110',
|
||||
'ed25519 ', ' ml-dsa-44',
|
||||
'rsa\0suffix', 'ec\0suffix', 'ed25519\0suffix', 'ml-dsa-44\0suffix',
|
||||
]) {
|
||||
for (const format of formats) {
|
||||
assert.throws(() => createPublicKey({
|
||||
key, format, asymmetricKeyType, namedCurve: 'P-256',
|
||||
}), {
|
||||
code: 'ERR_INVALID_ARG_VALUE',
|
||||
message: `Invalid asymmetricKeyType: ${asymmetricKeyType.split('\0')[0]}`,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
assert.throws(() => createPublicKey({
|
||||
key, format: 'raw-seed', asymmetricKeyType: 'ec', namedCurve: 'P-256',
|
||||
}), { code: 'ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS' });
|
||||
|
||||
// Recognized OKP and PQC names retain their raw-format restrictions.
|
||||
for (const asymmetricKeyType of ['ed25519', 'x25519']) {
|
||||
const options = { key, format: 'raw-private', asymmetricKeyType };
|
||||
if (asymmetricKeyType === 'x25519' && hasFIPS(3, 5)) {
|
||||
assert.throws(() => createPrivateKey(options), {
|
||||
code: 'ERR_INVALID_ARG_VALUE', message: 'Invalid key data',
|
||||
});
|
||||
} else {
|
||||
const imported = createPrivateKey(options);
|
||||
assert.strictEqual(imported.asymmetricKeyType, asymmetricKeyType);
|
||||
}
|
||||
assert.throws(() => createPrivateKey({
|
||||
key, format: 'raw-seed', asymmetricKeyType,
|
||||
}), { code: 'ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS' });
|
||||
}
|
||||
|
||||
{
|
||||
const asymmetricKeyType = 'ml-dsa-44';
|
||||
if (hasOpenSSL(3, 5) || isBoringSSL) {
|
||||
const imported = createPrivateKey({
|
||||
key, format: 'raw-seed', asymmetricKeyType,
|
||||
});
|
||||
assert.strictEqual(imported.asymmetricKeyType, 'ml-dsa-44');
|
||||
assert.throws(() => createPrivateKey({
|
||||
key, format: 'raw-private', asymmetricKeyType,
|
||||
}), { code: 'ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS' });
|
||||
} else {
|
||||
for (const format of formats) {
|
||||
assert.throws(() => createPublicKey({ key, format, asymmetricKeyType }), {
|
||||
code: 'ERR_INVALID_ARG_VALUE',
|
||||
message: 'Unsupported key type',
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
Vendored
+5
-26
@@ -382,19 +382,19 @@ declare namespace InternalCryptoBinding {
|
||||
): CryptoJobWebCrypto<SignJobResult<S>>;
|
||||
}
|
||||
|
||||
interface NidKeyPairGenJobConstructor {
|
||||
interface NamedKeyPairGenJobConstructor {
|
||||
new<
|
||||
M extends CryptoJobRegularMode,
|
||||
PublicFormat extends PublicKeyFormat = undefined,
|
||||
PrivateFormat extends PrivateKeyFormat = undefined,
|
||||
>(
|
||||
mode: M,
|
||||
nid: number,
|
||||
name: string,
|
||||
...encoding: KeyPairEncodingArgs<PublicFormat, PrivateFormat>
|
||||
): CryptoJobForMode<M, GeneratedKeyPair<PublicFormat, PrivateFormat>>;
|
||||
new(
|
||||
mode: CryptoJobWebCryptoMode,
|
||||
nid: number,
|
||||
name: string,
|
||||
algorithm: object,
|
||||
publicUsagesMask: number,
|
||||
privateUsagesMask: number,
|
||||
@@ -831,7 +831,8 @@ export interface CryptoBinding {
|
||||
KEMEncapsulateJob?: InternalCryptoBinding.KEMEncapsulateJobConstructor;
|
||||
KangarooTwelveJob: InternalCryptoBinding.KangarooTwelveJobConstructor;
|
||||
KmacJob: InternalCryptoBinding.KmacJobConstructor;
|
||||
NidKeyPairGenJob: InternalCryptoBinding.NidKeyPairGenJobConstructor;
|
||||
getPqcKeyTypes(): string[];
|
||||
NamedKeyPairGenJob: InternalCryptoBinding.NamedKeyPairGenJobConstructor;
|
||||
PBKDF2Job: InternalCryptoBinding.PBKDF2JobConstructor;
|
||||
RandomBytesJob: InternalCryptoBinding.RandomBytesJobConstructor;
|
||||
RandomPrimeJob: InternalCryptoBinding.RandomPrimeJobConstructor;
|
||||
@@ -883,28 +884,6 @@ export interface CryptoBinding {
|
||||
Sign: new () => InternalCryptoBinding.SignHandle;
|
||||
Verify: new () => InternalCryptoBinding.VerifyHandle;
|
||||
|
||||
EVP_PKEY_ED25519: number;
|
||||
EVP_PKEY_ED448: number;
|
||||
EVP_PKEY_ML_DSA_44: number;
|
||||
EVP_PKEY_ML_DSA_65: number;
|
||||
EVP_PKEY_ML_DSA_87: number;
|
||||
EVP_PKEY_ML_KEM_512: number;
|
||||
EVP_PKEY_ML_KEM_768: number;
|
||||
EVP_PKEY_ML_KEM_1024: number;
|
||||
EVP_PKEY_SLH_DSA_SHA2_128F: number;
|
||||
EVP_PKEY_SLH_DSA_SHA2_128S: number;
|
||||
EVP_PKEY_SLH_DSA_SHA2_192F: number;
|
||||
EVP_PKEY_SLH_DSA_SHA2_192S: number;
|
||||
EVP_PKEY_SLH_DSA_SHA2_256F: number;
|
||||
EVP_PKEY_SLH_DSA_SHA2_256S: number;
|
||||
EVP_PKEY_SLH_DSA_SHAKE_128F: number;
|
||||
EVP_PKEY_SLH_DSA_SHAKE_128S: number;
|
||||
EVP_PKEY_SLH_DSA_SHAKE_192F: number;
|
||||
EVP_PKEY_SLH_DSA_SHAKE_192S: number;
|
||||
EVP_PKEY_SLH_DSA_SHAKE_256F: number;
|
||||
EVP_PKEY_SLH_DSA_SHAKE_256S: number;
|
||||
EVP_PKEY_X25519: number;
|
||||
EVP_PKEY_X448: number;
|
||||
OPENSSL_EC_EXPLICIT_CURVE: number;
|
||||
OPENSSL_EC_NAMED_CURVE: number;
|
||||
RSA_PKCS1_PSS_PADDING: number;
|
||||
|
||||
Reference in new issue
Block a user