From 9f939f5af7d11299796999af3cbfa4845b505c78 Mon Sep 17 00:00:00 2001 From: Filip Skokan Date: Mon, 8 Apr 2024 08:18:43 +0200 Subject: [PATCH] crypto: reject Ed25519/Ed448 in Sign/Verify prototypes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fixes: #52097 PR-URL: https://github.com/nodejs/node/pull/52340 Fixes: https://github.com/nodejs/node/issues/52097 Reviewed-By: Tobias Nießen Reviewed-By: Antoine du Hamel Reviewed-By: Luigi Pinca --- src/crypto/crypto_sig.cc | 10 ++++++++++ test/parallel/test-crypto-sign-verify.js | 20 ++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/src/crypto/crypto_sig.cc b/src/crypto/crypto_sig.cc index 3b4fdee18a7e6d..ab020efbaeb1ed 100644 --- a/src/crypto/crypto_sig.cc +++ b/src/crypto/crypto_sig.cc @@ -423,6 +423,11 @@ void Sign::SignFinal(const FunctionCallbackInfo& args) { if (!key) return; + if (IsOneShot(key)) { + THROW_ERR_CRYPTO_UNSUPPORTED_OPERATION(env); + return; + } + int padding = GetDefaultSignPadding(key); if (!args[offset]->IsUndefined()) { CHECK(args[offset]->IsInt32()); @@ -548,6 +553,11 @@ void Verify::VerifyFinal(const FunctionCallbackInfo& args) { if (!pkey) return; + if (IsOneShot(pkey)) { + THROW_ERR_CRYPTO_UNSUPPORTED_OPERATION(env); + return; + } + ArrayBufferOrViewContents hbuf(args[offset]); if (UNLIKELY(!hbuf.CheckSizeInt32())) return THROW_ERR_OUT_OF_RANGE(env, "buffer is too big"); diff --git a/test/parallel/test-crypto-sign-verify.js b/test/parallel/test-crypto-sign-verify.js index b39ec22dceb5b7..56e5c16c2867f0 100644 --- a/test/parallel/test-crypto-sign-verify.js +++ b/test/parallel/test-crypto-sign-verify.js @@ -773,3 +773,23 @@ assert.throws( }, { code: 'ERR_INVALID_ARG_TYPE', message: /The "key\.key" property must be of type object/ }); } } + +{ + // Ed25519 and Ed448 must use the one-shot methods + const keys = [{ privateKey: fixtures.readKey('ed25519_private.pem', 'ascii'), + publicKey: fixtures.readKey('ed25519_public.pem', 'ascii') }, + { privateKey: fixtures.readKey('ed448_private.pem', 'ascii'), + publicKey: fixtures.readKey('ed448_public.pem', 'ascii') }]; + + for (const { publicKey, privateKey } of keys) { + assert.throws(() => { + crypto.createSign('SHA256').update('Test123').sign(privateKey); + }, { code: 'ERR_CRYPTO_UNSUPPORTED_OPERATION', message: 'Unsupported crypto operation' }); + assert.throws(() => { + crypto.createVerify('SHA256').update('Test123').verify(privateKey, 'sig'); + }, { code: 'ERR_CRYPTO_UNSUPPORTED_OPERATION', message: 'Unsupported crypto operation' }); + assert.throws(() => { + crypto.createVerify('SHA256').update('Test123').verify(publicKey, 'sig'); + }, { code: 'ERR_CRYPTO_UNSUPPORTED_OPERATION', message: 'Unsupported crypto operation' }); + } +}