Skip to content

Commit 314cd80

Browse files
committed
crypto: fix webcrypto ed(25519|448) spki/pkcs8 import
1 parent 0f1765e commit 314cd80

File tree

3 files changed

+54
-29
lines changed

3 files changed

+54
-29
lines changed

lib/internal/crypto/ec.js

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -269,14 +269,12 @@ async function ecImportKey(
269269
case 'NODE-X25519':
270270
// Fall through
271271
case 'NODE-X448':
272-
checkNamedCurve = false;
273272
if (algorithm.name !== 'ECDH')
274273
throw lazyDOMException('Invalid algorithm name.', 'DataError');
275274
break;
276275
case 'NODE-ED25519':
277276
// Fall through
278277
case 'NODE-ED448':
279-
checkNamedCurve = false;
280278
if (algorithm.name !== namedCurve)
281279
throw lazyDOMException('Invalid algorithm name.', 'DataError');
282280
break;
@@ -310,7 +308,6 @@ async function ecImportKey(
310308
throw lazyDOMException('Invalid JWK keyData', 'DataError');
311309
switch (keyData.kty) {
312310
case 'OKP': {
313-
checkNamedCurve = false;
314311
const isPublic = keyData.d === undefined;
315312

316313
let type;
@@ -395,7 +392,6 @@ async function ecImportKey(
395392
case 'NODE-X25519':
396393
// Fall through
397394
case 'NODE-X448':
398-
checkNamedCurve = false;
399395
if (algorithm.public !== undefined)
400396
validateBoolean(algorithm.public, 'algorithm.public');
401397
if (algorithm.name !== 'ECDH')
@@ -409,7 +405,6 @@ async function ecImportKey(
409405
case 'NODE-ED25519':
410406
// Fall through
411407
case 'NODE-ED448':
412-
checkNamedCurve = false;
413408
if (algorithm.public !== undefined)
414409
validateBoolean(algorithm.public, 'algorithm.public');
415410
if (algorithm.name !== namedCurve)
@@ -436,30 +431,27 @@ async function ecImportKey(
436431
throw lazyDOMException('Invalid key type', 'DataError');
437432
break;
438433
case 'ECDH':
439-
if (
440-
algorithm.namedCurve === 'NODE-X25519' &&
441-
keyObject.asymmetricKeyType !== 'x25519'
442-
) {
443-
throw lazyDOMException('Invalid key type', 'DataError');
444-
} else if (
445-
algorithm.namedCurve === 'NODE-X448' &&
446-
keyObject.asymmetricKeyType !== 'x448'
447-
) {
448-
throw lazyDOMException('Invalid key type', 'DataError');
449-
} else if (
450-
algorithm.namedCurve.startsWith('P') &&
451-
keyObject.asymmetricKeyType !== 'ec'
452-
) {
434+
if (algorithm.namedCurve === 'NODE-X25519') {
435+
if (keyObject.asymmetricKeyType !== 'x25519')
436+
throw lazyDOMException('Invalid key type', 'DataError');
437+
checkNamedCurve = false
438+
} else if (algorithm.namedCurve === 'NODE-X448') {
439+
if (keyObject.asymmetricKeyType !== 'x448')
440+
throw lazyDOMException('Invalid key type', 'DataError');
441+
checkNamedCurve = false
442+
} else if (keyObject.asymmetricKeyType !== 'ec') {
453443
throw lazyDOMException('Invalid key type', 'DataError');
454444
}
455445
break;
456446
case 'NODE-ED25519':
457447
if (keyObject.asymmetricKeyType !== 'ed25519')
458448
throw lazyDOMException('Invalid key type', 'DataError');
449+
checkNamedCurve = false;
459450
break;
460451
case 'NODE-ED448':
461452
if (keyObject.asymmetricKeyType !== 'ed448')
462453
throw lazyDOMException('Invalid key type', 'DataError');
454+
checkNamedCurve = false;
463455
break;
464456
}
465457

test/parallel/test-webcrypto-ed25519-ed448.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,20 @@ assert.rejects(
382382
assert.strictEqual(cryptoKey.algorithm.name, namedCurve);
383383
}, common.mustNotCall());
384384

385+
subtle.importKey(
386+
keyObject.type === 'private' ? 'pkcs8' : 'spki',
387+
keyObject.export({
388+
format: 'der',
389+
type: keyObject.type === 'private' ? 'pkcs8' : 'spki',
390+
}),
391+
{ name: namedCurve, namedCurve },
392+
true,
393+
keyObject.type === 'private' ? ['sign'] : ['verify'],
394+
).then((cryptoKey) => {
395+
assert.strictEqual(cryptoKey.type, keyObject.type);
396+
assert.strictEqual(cryptoKey.algorithm.name, namedCurve);
397+
}, common.mustNotCall());
398+
385399
assert.rejects(
386400
subtle.importKey(
387401
'node.keyObject',

test/parallel/test-webcrypto-x25519-x448.js

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -285,16 +285,35 @@ assert.rejects(
285285
const { publicKey, privateKey } = generateKeyPairSync(asymmetricKeyType);
286286
for (const keyObject of [publicKey, privateKey]) {
287287
const namedCurve = `NODE-${asymmetricKeyType.toUpperCase()}`;
288-
subtle.importKey(
289-
'node.keyObject',
290-
keyObject,
291-
{ name: 'ECDH', namedCurve },
292-
true,
293-
keyObject.type === 'private' ? ['deriveBits', 'deriveKey'] : [],
294-
).then((cryptoKey) => {
295-
assert.strictEqual(cryptoKey.type, keyObject.type);
296-
assert.strictEqual(cryptoKey.algorithm.name, 'ECDH');
297-
}, common.mustNotCall());
288+
{
289+
subtle.importKey(
290+
'node.keyObject',
291+
keyObject,
292+
{ name: 'ECDH', namedCurve },
293+
true,
294+
keyObject.type === 'private' ? ['deriveBits', 'deriveKey'] : [],
295+
).then((cryptoKey) => {
296+
assert.strictEqual(cryptoKey.type, keyObject.type);
297+
assert.strictEqual(cryptoKey.algorithm.name, 'ECDH');
298+
}, common.mustNotCall());
299+
}
300+
301+
{
302+
subtle.importKey(
303+
keyObject.type === 'private' ? 'pkcs8' : 'spki',
304+
keyObject.export({
305+
format: 'der',
306+
type: keyObject.type === 'private' ? 'pkcs8' : 'spki',
307+
}),
308+
{ name: namedCurve, namedCurve },
309+
true,
310+
keyObject.type === 'private' ? ['deriveBits'] : [],
311+
).then((cryptoKey) => {
312+
assert.strictEqual(cryptoKey.type, keyObject.type);
313+
assert.strictEqual(cryptoKey.algorithm.name, 'ECDH');
314+
assert.strictEqual(cryptoKey.algorithm.namedCurve, namedCurve);
315+
}, common.mustNotCall());
316+
}
298317
}
299318
}
300319
}

0 commit comments

Comments
 (0)