Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

crypto: generateKeyPair('ec') should not support NODE-ED* and NODE-X* #37063

Closed
wants to merge 4 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
crypto: generateKeyPair('ec') should not support NODE-ED* and NODE-X*
Fixes #37055
panva committed Jan 25, 2021

Verified

This commit was signed with the committer’s verified signature.
davidmigloz David Miguel Lozano
commit 76f81802b3186c71e171c19fe0e41ffcd89c8d60
20 changes: 19 additions & 1 deletion lib/internal/crypto/ec.js
Original file line number Diff line number Diff line change
@@ -166,7 +166,25 @@ async function ecGenerateKey(algorithm, extractable, keyUsages) {
// Fall through
}
return new Promise((resolve, reject) => {
generateKeyPair('ec', { namedCurve }, (err, pubKey, privKey) => {
let generate;
switch (namedCurve) {
case 'NODE-ED25519':
generate = generateKeyPair.bind(undefined, 'ed25519');
break;
case 'NODE-ED448':
generate = generateKeyPair.bind(undefined, 'ed448');
break;
case 'NODE-X25519':
generate = generateKeyPair.bind(undefined, 'x25519');
break;
case 'NODE-X448':
generate = generateKeyPair.bind(undefined, 'x448');
break;
default:
generate = generateKeyPair.bind(undefined, 'ec', { namedCurve });
break;
}
generate((err, pubKey, privKey) => {
panva marked this conversation as resolved.
Show resolved Hide resolved
if (err) {
return reject(lazyDOMException(
'The operation failed for an operation-specific reason',
12 changes: 0 additions & 12 deletions src/crypto/crypto_ec.cc
Original file line number Diff line number Diff line change
@@ -36,18 +36,6 @@ int GetCurveFromName(const char* name) {
int nid = EC_curve_nist2nid(name);
if (nid == NID_undef)
nid = OBJ_sn2nid(name);
// If there is still no match, check manually for known curves
if (nid == NID_undef) {
if (strcmp(name, "NODE-ED25519") == 0) {
nid = EVP_PKEY_ED25519;
} else if (strcmp(name, "NODE-ED448") == 0) {
nid = EVP_PKEY_ED448;
} else if (strcmp(name, "NODE-X25519") == 0) {
nid = EVP_PKEY_X25519;
} else if (strcmp(name, "NODE-X448") == 0) {
nid = EVP_PKEY_X448;
}
}
return nid;
}

16 changes: 16 additions & 0 deletions test/parallel/test-crypto-keygen.js
Original file line number Diff line number Diff line change
@@ -1283,3 +1283,19 @@ const sec1EncExp = (cipher) => getRegExpForPEM('EC PRIVATE KEY', cipher);
}));
}
}

{
// Proprietary Web Cryptography API ECDH/ECDSA namedCurve parameters
// should not be recognized in this API.
// See https://github.com/nodejs/node/issues/37055
const curves = ['NODE-ED25519', 'NODE-ED448', 'NODE-X25519', 'NODE-X448'];
for (const namedCurve of curves) {
assert.throws(
() => generateKeyPair('ec', { namedCurve }, common.mustNotCall()),
{
name: 'TypeError',
message: 'Invalid EC curve name'
}
);
}
}