Skip to content

Commit be33e09

Browse files
committed
crypto: ensure valid point on elliptic curve in SubtleCrypto.importKey
1 parent badba8c commit be33e09

File tree

4 files changed

+101
-0
lines changed

4 files changed

+101
-0
lines changed

lib/internal/crypto/ec.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,10 @@ async function ecImportKey(
264264
break;
265265
}
266266

267+
if (keyObject[kHandle].checkKeyData() === 0) {
268+
throw lazyDOMException('Invalid keyData', 'DataError');
269+
}
270+
267271
const {
268272
namedCurve: checkNamedCurve,
269273
} = keyObject[kHandle].keyDetail({});

src/crypto/crypto_keys.cc

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -907,6 +907,7 @@ v8::Local<v8::Function> KeyObjectHandle::Initialize(Environment* env) {
907907
isolate, templ, "getSymmetricKeySize", GetSymmetricKeySize);
908908
SetProtoMethodNoSideEffect(
909909
isolate, templ, "getAsymmetricKeyType", GetAsymmetricKeyType);
910+
SetProtoMethodNoSideEffect(isolate, templ, "checkKeyData", CheckKeyData);
910911
SetProtoMethod(isolate, templ, "export", Export);
911912
SetProtoMethod(isolate, templ, "exportJwk", ExportJWK);
912913
SetProtoMethod(isolate, templ, "initECRaw", InitECRaw);
@@ -926,6 +927,7 @@ void KeyObjectHandle::RegisterExternalReferences(
926927
registry->Register(Init);
927928
registry->Register(GetSymmetricKeySize);
928929
registry->Register(GetAsymmetricKeyType);
930+
registry->Register(CheckKeyData);
929931
registry->Register(Export);
930932
registry->Register(ExportJWK);
931933
registry->Register(InitECRaw);
@@ -1237,6 +1239,38 @@ void KeyObjectHandle::GetAsymmetricKeyType(
12371239
args.GetReturnValue().Set(key->GetAsymmetricKeyType());
12381240
}
12391241

1242+
int KeyObjectHandle::CheckKeyData() const {
1243+
MarkPopErrorOnReturn mark_pop_error_on_return;
1244+
1245+
const ManagedEVPPKey& key = data_->GetAsymmetricKey();
1246+
switch (EVP_PKEY_id(key.get())) {
1247+
case EVP_PKEY_EC: {
1248+
switch (data_->GetKeyType()) {
1249+
case kKeyTypePublic:
1250+
#if OPENSSL_VERSION_MAJOR >= 3
1251+
return EVP_PKEY_public_check_quick(
1252+
EVP_PKEY_CTX_new(key.get(), nullptr));
1253+
#else
1254+
return EVP_PKEY_public_check(EVP_PKEY_CTX_new(key.get(), nullptr));
1255+
#endif
1256+
case kKeyTypePrivate:
1257+
return EVP_PKEY_check(EVP_PKEY_CTX_new(key.get(), nullptr));
1258+
default:
1259+
UNREACHABLE();
1260+
}
1261+
}
1262+
default:
1263+
UNREACHABLE();
1264+
}
1265+
}
1266+
1267+
void KeyObjectHandle::CheckKeyData(const FunctionCallbackInfo<Value>& args) {
1268+
KeyObjectHandle* key;
1269+
ASSIGN_OR_RETURN_UNWRAP(&key, args.Holder());
1270+
1271+
args.GetReturnValue().Set(key->CheckKeyData());
1272+
}
1273+
12401274
void KeyObjectHandle::GetSymmetricKeySize(
12411275
const FunctionCallbackInfo<Value>& args) {
12421276
KeyObjectHandle* key;

src/crypto/crypto_keys.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,9 @@ class KeyObjectHandle : public BaseObject {
193193
const v8::FunctionCallbackInfo<v8::Value>& args);
194194
v8::Local<v8::Value> GetAsymmetricKeyType() const;
195195

196+
static void CheckKeyData(const v8::FunctionCallbackInfo<v8::Value>& args);
197+
int CheckKeyData() const;
198+
196199
static void GetSymmetricKeySize(
197200
const v8::FunctionCallbackInfo<v8::Value>& args);
198201

test/parallel/test-webcrypto-export-import-ec.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,3 +412,63 @@ async function testImportRaw({ name, publicUsages }, namedCurve) {
412412
true, privateUsages), { message: /Invalid key type/ });
413413
}
414414
}
415+
416+
// Bad private keys
417+
{
418+
for (const { namedCurve, key: pkcs8 } of [
419+
// The private key is exactly equal to the order, and the public key is
420+
// private key * order.
421+
{
422+
namedCurve: 'P-256',
423+
key: Buffer.from(
424+
'3066020100301306072a8648ce3d020106082a8648ce3d030107044c304a0201' +
425+
'010420ffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc' +
426+
'632551a12303210000ffffff00000000ffffffffffffffffbce6faada7179e84' +
427+
'f3b9cac2fc632551', 'hex'),
428+
},
429+
// The private key is exactly equal to the order, and the public key is
430+
// omitted.
431+
{
432+
namedCurve: 'P-256',
433+
key: Buffer.from(
434+
'3041020100301306072a8648ce3d020106082a8648ce3d030107042730250201' +
435+
'010420ffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc' +
436+
'632551', 'hex'),
437+
},
438+
// The private key is exactly equal to the order + 11, and the public key is
439+
// private key * order.
440+
{
441+
namedCurve: 'P-521',
442+
key: Buffer.from(
443+
'3081ee020100301006072a8648ce3d020106052b810400230481d63081d30201' +
444+
'01044201ffffffffffffffffffffffffffffffffffffffffffffffffffffffff' +
445+
'fffffffffa51868783bf2f966b7fcc0148f709a5d03bb5c9b8899c47aebb6fb7' +
446+
'1e91386414a181890381860004008a75841259fdedff546f1a39573b4315cfed' +
447+
'5dc7ed7c17849543ef2c54f2991652f3dbc5332663da1bd19b1aebe319108501' +
448+
'5c024fa4c9a902ecc0e02dda0cdb9a0096fb303fcbba2129849d0ca877054fb2' +
449+
'293add566210bd0493ed2e95d4e0b9b82b1bc8a90e8b42a4ab3892331914a953' +
450+
'36dcac80e3f4819b5d58874f92ce48c808', 'hex'),
451+
},
452+
// The private key is exactly equal to the order + 11, and the public key is
453+
// omitted.
454+
{
455+
namedCurve: 'P-521',
456+
key: Buffer.from(
457+
'3060020100301006072a8648ce3d020106052b81040023044930470201010442' +
458+
'01ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff' +
459+
'fffa51868783bf2f966b7fcc0148f709a5d03bb5c9b8899c47aebb6fb71e9138' +
460+
'6414', 'hex'),
461+
},
462+
]) {
463+
for (const [name, privateUsages] of [
464+
['ECDSA', ['sign']],
465+
['ECDH', ['deriveBits', 'deriveBits']],
466+
]) {
467+
assert.rejects(subtle.importKey(
468+
'pkcs8',
469+
pkcs8,
470+
{ name, hash: 'SHA-256', namedCurve },
471+
true, privateUsages), { name: 'DataError', message: /Invalid keyData/ });
472+
}
473+
}
474+
}

0 commit comments

Comments
 (0)