Skip to content

Commit f0dec58

Browse files
panvaBethGriggs
authored andcommittedSep 21, 2021
crypto: fix webcrypto ed(25519|448) spki/pkcs8 import
PR-URL: #40131 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de>
1 parent a63a4bc commit f0dec58

File tree

3 files changed

+41
-19
lines changed

3 files changed

+41
-19
lines changed
 

‎lib/internal/crypto/ec.js

+11-19
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

+15
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,21 @@ 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+
assert.strictEqual(cryptoKey.algorithm.namedCurve, namedCurve);
398+
}, common.mustNotCall());
399+
385400
assert.rejects(
386401
subtle.importKey(
387402
'node.keyObject',

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

+15
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,21 @@ assert.rejects(
295295
assert.strictEqual(cryptoKey.type, keyObject.type);
296296
assert.strictEqual(cryptoKey.algorithm.name, 'ECDH');
297297
}, common.mustNotCall());
298+
299+
subtle.importKey(
300+
keyObject.type === 'private' ? 'pkcs8' : 'spki',
301+
keyObject.export({
302+
format: 'der',
303+
type: keyObject.type === 'private' ? 'pkcs8' : 'spki',
304+
}),
305+
{ name: 'ECDH', namedCurve },
306+
true,
307+
keyObject.type === 'private' ? ['deriveBits'] : [],
308+
).then((cryptoKey) => {
309+
assert.strictEqual(cryptoKey.type, keyObject.type);
310+
assert.strictEqual(cryptoKey.algorithm.name, 'ECDH');
311+
assert.strictEqual(cryptoKey.algorithm.namedCurve, namedCurve);
312+
}, common.mustNotCall());
298313
}
299314
}
300315
}

0 commit comments

Comments
 (0)
Please sign in to comment.