Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

crypto: fix webcrypto import of cfrg raw public keys #43404

Merged
merged 1 commit into from
Jun 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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;
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 @@ -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) => {
Expand All @@ -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);
Expand Down