From 1baf50cee1c0905df45a8a3400163e80dad4abe0 Mon Sep 17 00:00:00 2001 From: gc <30398469+gc@users.noreply.github.com> Date: Mon, 4 Oct 2021 19:47:22 +1100 Subject: [PATCH 1/2] crypto: remove incorrect constructor invocation --- lib/internal/crypto/ec.js | 2 +- ...st-crypto-subtle-hash-constructor-error.js | 36 +++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 test/parallel/test-crypto-subtle-hash-constructor-error.js diff --git a/lib/internal/crypto/ec.js b/lib/internal/crypto/ec.js index c684d6c66af26a..b64922bcdc883f 100644 --- a/lib/internal/crypto/ec.js +++ b/lib/internal/crypto/ec.js @@ -482,7 +482,7 @@ function ecdsaSignVerify(key, data, { name, hash }, signature) { // Fall through case 'NODE-ED448': if (hash !== undefined) - throw new lazyDOMException(`Hash is not permitted for ${name}`); + throw lazyDOMException(`Hash is not permitted for ${name}`); break; default: if (hash === undefined) diff --git a/test/parallel/test-crypto-subtle-hash-constructor-error.js b/test/parallel/test-crypto-subtle-hash-constructor-error.js new file mode 100644 index 00000000000000..0d7f4aee1e7982 --- /dev/null +++ b/test/parallel/test-crypto-subtle-hash-constructor-error.js @@ -0,0 +1,36 @@ +'use strict'; + +const common = require('../common'); + +if (!common.hasCrypto) + common.skip('missing crypto'); + +const assert = require('assert'); +const crypto = require('crypto').webcrypto; + + +async function generateKey() { + const { privateKey } = await crypto.subtle.generateKey( + { + name: 'NODE-ED25519', + namedCurve: 'NODE-ED25519' + }, + true, + ['sign', 'verify'] + ); + const signature = await crypto.subtle.sign( + { + name: 'NODE-ED25519', + hash: 'SHA-256' + }, + privateKey, + '-' + ); + return signature; +} + +generateKey().catch(common.mustCall((err) => { + assert.strictEqual(err.name, 'DOMException'); + assert.strictEqual(err.message, 'Hash is not permitted for NODE-ED25519'); + assert.ok(err instanceof DOMException); +})) \ No newline at end of file From 89099ededa011fc6bb0a73d0e81bec27e1029dc4 Mon Sep 17 00:00:00 2001 From: Filip Skokan Date: Mon, 4 Oct 2021 20:51:53 +0200 Subject: [PATCH 2/2] fixup! crypto: remove incorrect constructor invocation --- ...st-crypto-subtle-hash-constructor-error.js | 36 ------------- test/parallel/test-webcrypto-ed25519-ed448.js | 50 +++++++++++++++++++ 2 files changed, 50 insertions(+), 36 deletions(-) delete mode 100644 test/parallel/test-crypto-subtle-hash-constructor-error.js diff --git a/test/parallel/test-crypto-subtle-hash-constructor-error.js b/test/parallel/test-crypto-subtle-hash-constructor-error.js deleted file mode 100644 index 0d7f4aee1e7982..00000000000000 --- a/test/parallel/test-crypto-subtle-hash-constructor-error.js +++ /dev/null @@ -1,36 +0,0 @@ -'use strict'; - -const common = require('../common'); - -if (!common.hasCrypto) - common.skip('missing crypto'); - -const assert = require('assert'); -const crypto = require('crypto').webcrypto; - - -async function generateKey() { - const { privateKey } = await crypto.subtle.generateKey( - { - name: 'NODE-ED25519', - namedCurve: 'NODE-ED25519' - }, - true, - ['sign', 'verify'] - ); - const signature = await crypto.subtle.sign( - { - name: 'NODE-ED25519', - hash: 'SHA-256' - }, - privateKey, - '-' - ); - return signature; -} - -generateKey().catch(common.mustCall((err) => { - assert.strictEqual(err.name, 'DOMException'); - assert.strictEqual(err.message, 'Hash is not permitted for NODE-ED25519'); - assert.ok(err instanceof DOMException); -})) \ No newline at end of file diff --git a/test/parallel/test-webcrypto-ed25519-ed448.js b/test/parallel/test-webcrypto-ed25519-ed448.js index 8abca247738621..a0d858a4ab4837 100644 --- a/test/parallel/test-webcrypto-ed25519-ed448.js +++ b/test/parallel/test-webcrypto-ed25519-ed448.js @@ -429,3 +429,53 @@ assert.rejects( } } } + +{ + // See: https://github.com/nodejs/node/pull/40300 + for (const namedCurve of ['NODE-ED25519', 'NODE-ED448']) { + assert.rejects( + (async () => { + const { privateKey } = await generateKey(namedCurve); + return subtle.sign( + { + name: namedCurve, + hash: 'SHA-256' + }, + privateKey, + Buffer.from('abc') + ); + })(), + (err) => { + assert.strictEqual(err.message, `Hash is not permitted for ${namedCurve}`); + assert(err instanceof DOMException); + return true; + }).then(common.mustCall()); + + assert.rejects( + (async () => { + const { publicKey, privateKey } = await generateKey(namedCurve); + const signature = await subtle.sign( + { + name: namedCurve, + }, + privateKey, + Buffer.from('abc') + ).catch(common.mustNotCall()); + + return subtle.verify( + { + name: namedCurve, + hash: 'SHA-256', + }, + publicKey, + signature, + Buffer.from('abc') + ); + })(), + (err) => { + assert.strictEqual(err.message, `Hash is not permitted for ${namedCurve}`); + assert(err instanceof DOMException); + return true; + }).then(common.mustCall()); + } +}