From 2130598e91562efbfd594ba14f5f3eda2b2432d6 Mon Sep 17 00:00:00 2001 From: Filip Skokan Date: Thu, 13 May 2021 10:45:11 +0200 Subject: [PATCH] crypto: forbid NODE-ED25519 and NODE-ED448 "raw" key export closes #38655 PR-URL: https://github.com/nodejs/node/pull/38668 Reviewed-By: Gus Caplan Reviewed-By: James M Snell Reviewed-By: Rich Trott --- lib/internal/crypto/webcrypto.js | 7 +++++-- test/parallel/test-webcrypto-ed25519-ed448.js | 15 ++++++--------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/lib/internal/crypto/webcrypto.js b/lib/internal/crypto/webcrypto.js index b28ee9b8167be5..900ac7a6e8ad7b 100644 --- a/lib/internal/crypto/webcrypto.js +++ b/lib/internal/crypto/webcrypto.js @@ -288,8 +288,11 @@ async function exportKeyRaw(key) { case 'NODE-ED25519': // Fall through case 'NODE-ED448': - return lazyRequire('internal/crypto/ec') - .ecExportKey(key, kWebCryptoKeyFormatRaw); + if (key.type === 'public') { + return lazyRequire('internal/crypto/ec') + .ecExportKey(key, kWebCryptoKeyFormatRaw); + } + break; case 'ECDSA': // Fall through case 'ECDH': diff --git a/test/parallel/test-webcrypto-ed25519-ed448.js b/test/parallel/test-webcrypto-ed25519-ed448.js index 91e0fb8bf3fe0d..9c60ceff64b1b4 100644 --- a/test/parallel/test-webcrypto-ed25519-ed448.js +++ b/test/parallel/test-webcrypto-ed25519-ed448.js @@ -268,15 +268,12 @@ async function test2(namedCurve) { true, ['verify']), ]); - const [ - rawKey1, - rawKey2, - ] = await Promise.all([ - subtle.exportKey('raw', privateKey), - subtle.exportKey('raw', publicKey), - ]); - assert.deepStrictEqual(Buffer.from(rawKey1), vector.privateKey); - assert.deepStrictEqual(Buffer.from(rawKey2), vector.publicKey); + const rawPublicKey = await subtle.exportKey('raw', publicKey); + assert.deepStrictEqual(Buffer.from(rawPublicKey), vector.publicKey); + + assert.rejects(subtle.exportKey('raw', privateKey), { + message: new RegExp(`Unable to export a raw ${namedCurve} private key`) + }).then(common.mustCall()); const sig = await subtle.sign( { name: namedCurve },