From 12984146e6d6ad1fff8f6ae2850d8769f1f49ade Mon Sep 17 00:00:00 2001 From: Filip Skokan Date: Mon, 13 Jun 2022 15:16:39 +0200 Subject: [PATCH] crypto: fix webcrypto import of cfrg raw public keys --- 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 826610a5fd6b8e..0a90dccbd5d1b3 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'); @@ -295,7 +288,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 c37c1535747917..be33737eb4f9d7 100644 --- a/test/parallel/test-webcrypto-export-import-cfrg.js +++ b/test/parallel/test-webcrypto-export-import-cfrg.js @@ -261,6 +261,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) => { @@ -269,6 +283,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);