crypto: use provider EC group names · nodejs/node@8feccc5 · GitHub
Skip to content

Commit 8feccc5

Browse files
panvaaduh95
authored andcommitted
crypto: use provider EC group names
Pass group names through EC key generation and report provider names in key details without requiring an OpenSSL NID. Preserve established curve aliases and synchronous invalid-curve errors. Filter built-in curves through EC parameter generation and refresh getCurves() results when FIPS properties change. Signed-off-by: Filip Skokan <panva.ip@gmail.com> Assisted-by: Codex PR-URL: #66108 Backport-PR-URL: #66233 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent 764e0d1 commit 8feccc5

8 files changed

Lines changed: 336 additions & 19 deletions

File tree

‎deps/ncrypto/ncrypto.cc‎

Lines changed: 67 additions & 6 deletions

‎deps/ncrypto/ncrypto.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -771,6 +771,8 @@ class Ec final {
771771

772772
static int GetCurveIdFromName(const char* name);
773773
static int GetCurveId(const EVPKeyPointer& key);
774+
static std::optional<std::string> GetCurveName(const EVPKeyPointer& key);
775+
static bool CheckCurveName(const char* name);
774776
static DataPointer TryExportPublic(const EVPKeyPointer& key,
775777
point_conversion_form_t form);
776778
static DataPointer ExportPrivate(const EVPKeyPointer& key);
@@ -1150,6 +1152,7 @@ class EVPKeyCtxPointer final {
11501152
bool setDhParameters(int prime_size, uint32_t generator);
11511153
bool setDsaParameters(uint32_t bits, std::optional<int> q_bits);
11521154
bool setEcParameters(int curve, int encoding);
1155+
bool setEcParameters(const char* group_name, int encoding);
11531156

11541157
bool setRsaOaepMd(const Digest& md);
11551158
bool setRsaMgf1Md(const Digest& md);

‎lib/internal/crypto/util.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ const {
8080
const { Buffer } = require('buffer');
8181

8282
const {
83-
cachedResult,
8483
emitExperimentalWarning,
8584
filterDuplicateStrings,
8685
lazyDOMException,
@@ -192,7 +191,8 @@ const getMacs = cachedArrayByFipsGeneration(
192191
_macCache = undefined;
193192
});
194193

195-
const getCurves = cachedResult(() => filterDuplicateStrings(_getCurves()));
194+
const getCurves = cachedArrayByFipsGeneration(
195+
() => filterDuplicateStrings(_getCurves()));
196196

197197
function setEngine(id, flags) {
198198
validateString(id, 'id');

‎src/crypto/crypto_ec.cc‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -409,14 +409,18 @@ void ECDH::ConvertKey(const FunctionCallbackInfo<Value>& args) {
409409
}
410410
}
411411

412+
void EcKeyPairParams::MemoryInfo(MemoryTracker* tracker) const {
413+
tracker->TrackField("curve_name", curve_name);
414+
}
415+
412416
EVPKeyCtxPointer EcKeyGenTraits::Setup(EcKeyPairGenConfig* params) {
413417
EVPKeyCtxPointer key_ctx;
414418
if (params->params.algorithm != nullptr) {
415419
key_ctx = EVPKeyCtxPointer::NewFromAlgorithm(*params->params.algorithm);
416420
} else {
417421
auto param_ctx = EVPKeyCtxPointer::NewFromAlgorithm(KeyAlgorithm::EC);
418422
if (!param_ctx.initForParamgen() ||
419-
!param_ctx.setEcParameters(params->params.curve_nid,
423+
!param_ctx.setEcParameters(params->params.curve_name.c_str(),
420424
params->params.param_encoding)) {
421425
return {};
422426
}
@@ -450,11 +454,11 @@ Maybe<void> EcKeyGenTraits::AdditionalConfig(
450454
Utf8Value curve_name(env->isolate(), args[*offset]);
451455
params->params.algorithm = ncrypto::Ec::GetNamedKeyAlgorithm(*curve_name);
452456
if (params->params.algorithm == nullptr) {
453-
params->params.curve_nid = Ec::GetCurveIdFromName(*curve_name);
454-
if (params->params.curve_nid == NID_undef) {
457+
if (!Ec::CheckCurveName(*curve_name)) {
455458
THROW_ERR_CRYPTO_INVALID_CURVE(env);
456459
return Nothing<void>();
457460
}
461+
params->params.curve_name = *curve_name;
458462
}
459463

460464
// param encoding
@@ -762,13 +766,13 @@ bool GetEcKeyDetail(Environment* env,
762766
const auto& m_pkey = key.GetAsymmetricKey();
763767
DCHECK(m_pkey.isA(KeyAlgorithm::EC));
764768

765-
int nid = Ec::GetCurveId(m_pkey);
766-
if (nid == NID_undef) return true;
769+
const auto name = Ec::GetCurveName(m_pkey);
770+
if (!name) return true;
767771

768772
return target
769773
->Set(env->context(),
770774
env->named_curve_string(),
771-
OneByteString(env->isolate(), OBJ_nid2sn(nid)))
775+
OneByteString(env->isolate(), name.value()))
772776
.IsJust();
773777
}
774778

‎src/crypto/crypto_ec.h‎

Lines changed: 4 additions & 2 deletions

0 commit comments

Comments
 (0)