crypto: check ed/x webcrypto key import algorithm names

PR-URL: https://github.com/nodejs/node/pull/37305
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Filip Skokan authored and Antoine du Hamel committed 2021-02-26 11:16:52 +01:00
1 parent 83cce8717f
commit 8ddee73efc
4 files changed
+125 -14

No files matched your search

+53 -1
View File
@@ -6,7 +6,10 @@ if (!common.hasCrypto)
common.skip('missing crypto');
const assert = require('assert');
const { subtle } = require('crypto').webcrypto;
const {
generateKeyPairSync,
webcrypto: { subtle }
} = require('crypto');
async function generateKey(namedCurve) {
return subtle.generateKey(
@@ -365,3 +368,52 @@ assert.rejects(
{
message: /Unsupported named curves for ECDSA/
});
{
for (const asymmetricKeyType of ['ed25519', 'ed448']) {
const { publicKey, privateKey } = generateKeyPairSync(asymmetricKeyType);
for (const keyObject of [publicKey, privateKey]) {
const namedCurve = `NODE-${asymmetricKeyType.toUpperCase()}`;
subtle.importKey(
'node.keyObject',
keyObject,
{ name: namedCurve, namedCurve },
true,
keyObject.type === 'private' ? ['sign'] : ['verify'],
).then((cryptoKey) => {
assert.strictEqual(cryptoKey.type, keyObject.type);
assert.strictEqual(cryptoKey.algorithm.name, namedCurve);
}, common.mustNotCall());
assert.rejects(
subtle.importKey(
'node.keyObject',
keyObject,
{
name: 'ECDSA',
namedCurve,
},
true,
keyObject.type === 'private' ? ['sign'] : ['verify']
),
{
message: /Invalid algorithm name/
});
assert.rejects(
subtle.importKey(
'node.keyObject',
keyObject,
{
name: 'ECDH',
namedCurve,
},
true,
keyObject.type === 'private' ? ['deriveBits', 'deriveKey'] : [],
),
{
message: /Invalid algorithm name/
});
}
}
}
+24 -3
View File
@@ -6,7 +6,11 @@ if (!common.hasCrypto)
common.skip('missing crypto');
const assert = require('assert');
const { subtle } = require('crypto').webcrypto;
const {
generateKeyPairSync,
webcrypto: { subtle }
} = require('crypto');
// X25519 and X448 are ECDH named curves that should work
// with the existing ECDH mechanisms with no additional
@@ -260,7 +264,7 @@ assert.rejects(
namedCurve: 'NODE-X25519'
},
true,
['deriveBits']).then(common.mustCall());
['deriveBits']).then(common.mustCall(), common.mustNotCall());
// Public JWK import
subtle.importKey(
@@ -275,5 +279,22 @@ assert.rejects(
namedCurve: 'NODE-X25519'
},
true,
[]).then(common.mustCall());
[]).then(common.mustCall(), common.mustNotCall());
for (const asymmetricKeyType of ['x25519', 'x448']) {
const { publicKey, privateKey } = generateKeyPairSync(asymmetricKeyType);
for (const keyObject of [publicKey, privateKey]) {
const namedCurve = `NODE-${asymmetricKeyType.toUpperCase()}`;
subtle.importKey(
'node.keyObject',
keyObject,
{ name: 'ECDH', namedCurve },
true,
keyObject.type === 'private' ? ['deriveBits', 'deriveKey'] : [],
).then((cryptoKey) => {
assert.strictEqual(cryptoKey.type, keyObject.type);
assert.strictEqual(cryptoKey.algorithm.name, 'ECDH');
}, common.mustNotCall());
}
}
}