From 9f1de2c005d55205bb7c6b6bbbf8963fc951bc6f Mon Sep 17 00:00:00 2001 From: Filip Skokan Date: Wed, 15 Jun 2022 15:33:55 +0200 Subject: [PATCH] crypto: fix webcrypto import of cfrg raw public keys MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/43404 Reviewed-By: James M Snell Reviewed-By: Tobias Nießen --- lib/internal/crypto/cfrg.js | 9 +-------- .../parallel/test-webcrypto-export-import-cfrg.js | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/lib/internal/crypto/cfrg.js b/lib/internal/crypto/cfrg.js index 5dd4e44bdd06cc..6910aa84134a57 100644 --- a/lib/internal/crypto/cfrg.js +++ b/lib/internal/crypto/cfrg.js @@ -72,13 +72,6 @@ function verifyAcceptableCfrgKeyUse(name, type, usages) { } } -function createECPublicKeyRaw(name, keyData) { - const handle = new KeyObjectHandle(); - keyData = getArrayBufferOrView(keyData, 'keyData'); - if (handle.initECRaw(name.toLowerCase(), keyData)) - return new PublicKeyObject(handle); -} - function createCFRGRawKey(name, keyData, isPublic) { const handle = new KeyObjectHandle(); keyData = getArrayBufferOrView(keyData, 'keyData'); @@ -297,7 +290,7 @@ async function cfrgImportKey( } case 'raw': { verifyAcceptableCfrgKeyUse(name, 'public', usagesSet); - keyObject = createECPublicKeyRaw(name, keyData); + keyObject = createCFRGRawKey(name, keyData, true); if (keyObject === undefined) throw lazyDOMException('Unable to import CFRG key', 'OperationError'); break; diff --git a/test/parallel/test-webcrypto-export-import-cfrg.js b/test/parallel/test-webcrypto-export-import-cfrg.js index 531cb51c1b8b8c..6d162ac61c2e30 100644 --- a/test/parallel/test-webcrypto-export-import-cfrg.js +++ b/test/parallel/test-webcrypto-export-import-cfrg.js @@ -281,6 +281,20 @@ async function testImportJwk({ name, publicUsages, privateUsages }, extractable) } } +async function testImportRaw({ name, publicUsages }) { + const jwk = keyData[name].jwk; + + const publicKey = await subtle.importKey( + 'raw', + Buffer.from(jwk.x, 'base64url'), + { name }, + true, publicUsages); + + assert.strictEqual(publicKey.type, 'public'); + assert.deepStrictEqual(publicKey.usages, publicUsages); + assert.strictEqual(publicKey.algorithm.name, name); +} + (async function() { const tests = []; testVectors.forEach((vector) => { @@ -289,6 +303,7 @@ async function testImportJwk({ name, publicUsages, privateUsages }, extractable) tests.push(testImportPkcs8(vector, extractable)); tests.push(testImportJwk(vector, extractable)); }); + tests.push(testImportRaw(vector)); }); await Promise.all(tests);