Skip to content

Commit

Permalink
crypto: fix webcrypto import of cfrg raw public keys
Browse files Browse the repository at this point in the history
PR-URL: #43404
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
  • Loading branch information
panva authored and danielleadams committed Jun 16, 2022
1 parent 7f02e22 commit 9f1de2c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
9 changes: 1 addition & 8 deletions lib/internal/crypto/cfrg.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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;
Expand Down
15 changes: 15 additions & 0 deletions test/parallel/test-webcrypto-export-import-cfrg.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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);
Expand Down

0 comments on commit 9f1de2c

Please sign in to comment.