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: ensure expected JWK alg in SubtleCrypto.importKey RSA imports #57450

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
11 changes: 3 additions & 8 deletions lib/internal/crypto/hashnames.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

const {
ObjectKeys,
StringPrototypeToLowerCase,
} = primordials;

const kHashContextNode = 1;
Expand Down Expand Up @@ -58,22 +57,18 @@ const kHashNames = {
for (let n = 0; n < keys.length; n++) {
const contexts = ObjectKeys(kHashNames[keys[n]]);
for (let i = 0; i < contexts.length; i++) {
const alias =
StringPrototypeToLowerCase(kHashNames[keys[n]][contexts[i]]);
const alias = kHashNames[keys[n]][contexts[i]];
if (kHashNames[alias] === undefined)
kHashNames[alias] = kHashNames[keys[n]];
}
}
}

function normalizeHashName(name, context = kHashContextNode) {
if (typeof name !== 'string')
return name;
name = StringPrototypeToLowerCase(name);
const alias = kHashNames[name]?.[context];
return alias || name;
return kHashNames[name]?.[context];
}


normalizeHashName.kContextNode = kHashContextNode;
normalizeHashName.kContextWebCrypto = kHashContextWebCrypto;
normalizeHashName.kContextJwkRsa = kHashContextJwkRsa;
Expand Down
10 changes: 7 additions & 3 deletions lib/internal/crypto/rsa.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,9 +275,13 @@ function rsaImportKey(
}

if (keyData.alg !== undefined) {
const hash =
normalizeHashName(keyData.alg, normalizeHashName.kContextWebCrypto);
if (hash !== algorithm.hash.name)
const expected =
normalizeHashName(algorithm.hash.name,
algorithm.name === 'RSASSA-PKCS1-v1_5' ? normalizeHashName.kContextJwkRsa :
algorithm.name === 'RSA-PSS' ? normalizeHashName.kContextJwkRsaPss :
normalizeHashName.kContextJwkRsaOaep);

if (keyData.alg !== expected)
throw lazyDOMException(
'JWK "alg" does not match the requested algorithm',
'DataError');
Expand Down
72 changes: 64 additions & 8 deletions test/parallel/test-webcrypto-export-import-rsa.js
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,19 @@ async function testImportJwk(

const jwk = keyData[size].jwk;

let alg;
switch (name) {
case 'RSA-PSS':
alg = `PS${hash === 'SHA-1' ? 1 : hash.substring(4)}`;
break;
case 'RSA-OAEP':
alg = `RSA-OAEP${hash === 'SHA-1' ? '' : hash.substring(3)}`;
break;
case 'RSASSA-PKCS1-v1_5':
alg = `RS${hash === 'SHA-1' ? 1 : hash.substring(4)}`;
break;
}

const [
publicKey,
privateKey,
Expand All @@ -394,14 +407,14 @@ async function testImportJwk(
kty: jwk.kty,
n: jwk.n,
e: jwk.e,
alg: `PS${hash.substring(4)}`
alg,
},
{ name, hash },
extractable,
publicUsages),
subtle.importKey(
'jwk',
{ ...jwk, alg: `PS${hash.substring(4)}` },
{ ...jwk, alg },
{ name, hash },
extractable,
privateUsages),
Expand Down Expand Up @@ -435,6 +448,8 @@ async function testImportJwk(

assert.strictEqual(pubJwk.kty, 'RSA');
assert.strictEqual(pvtJwk.kty, 'RSA');
assert.strictEqual(pubJwk.alg, alg);
assert.strictEqual(pvtJwk.alg, alg);
assert.strictEqual(pubJwk.n, jwk.n);
assert.strictEqual(pvtJwk.n, jwk.n);
assert.strictEqual(pubJwk.e, jwk.e);
Expand Down Expand Up @@ -483,30 +498,71 @@ async function testImportJwk(
}

{
let invalidAlg = name === 'RSA-OAEP' ? name : name === 'RSA-PSS' ? 'PS' : 'RS';
await assert.rejects(
subtle.importKey(
'jwk',
{ kty: jwk.kty, n: jwk.n, e: jwk.e, alg: alg.toLowerCase() },
{ name, hash },
extractable,
publicUsages),
{ message: 'JWK "alg" does not match the requested algorithm' });
await assert.rejects(
subtle.importKey(
'jwk',
{ ...jwk, alg: alg.toLowerCase() },
{ name, hash },
extractable,
privateUsages),
{ message: 'JWK "alg" does not match the requested algorithm' });
}

{
let invalidAlgHash = name === 'RSA-OAEP' ? name : name === 'RSA-PSS' ? 'PS' : 'RS';
switch (name) {
case 'RSA-OAEP':
if (hash === 'SHA-1')
invalidAlg += '-256';
invalidAlgHash += '-256';
break;
default:
if (hash === 'SHA-256')
invalidAlg += '384';
invalidAlgHash += '384';
else
invalidAlg += '256';
invalidAlgHash += '256';
}
await assert.rejects(
subtle.importKey(
'jwk',
{ kty: jwk.kty, n: jwk.n, e: jwk.e, alg: invalidAlg },
{ kty: jwk.kty, n: jwk.n, e: jwk.e, alg: invalidAlgHash },
{ name, hash },
extractable,
publicUsages),
{ message: 'JWK "alg" does not match the requested algorithm' });
await assert.rejects(
subtle.importKey(
'jwk',
{ ...jwk, alg: invalidAlg },
{ ...jwk, alg: invalidAlgHash },
{ name, hash },
extractable,
privateUsages),
{ message: 'JWK "alg" does not match the requested algorithm' });
}

{
const invalidAlgType = name === 'RSA-PSS' ? `RS${hash.substring(4)}` : `PS${hash.substring(4)}`;
await assert.rejects(
subtle.importKey(
'jwk',
{ kty: jwk.kty, n: jwk.n, e: jwk.e, alg: invalidAlgType },
{ name, hash },
extractable,
publicUsages),
{ message: 'JWK "alg" does not match the requested algorithm' }).catch((e) => {
throw e;
});
await assert.rejects(
subtle.importKey(
'jwk',
{ ...jwk, alg: invalidAlgType },
{ name, hash },
extractable,
privateUsages),
Expand Down
Loading