From 40487f229363d084bafaa90a60ac9997ca968362 Mon Sep 17 00:00:00 2001 From: Filip Skokan Date: Wed, 3 Apr 2024 10:44:18 +0200 Subject: [PATCH] crypto: reject Ed25519/Ed448 in Sign/Verify prototypes fixes: #52097 --- 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 5b1acf2677d1b6..1ed50b441978d3 100644 --- a/src/crypto/crypto_sig.cc +++ b/src/crypto/crypto_sig.cc @@ -418,6 +418,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()); @@ -543,6 +548,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' }); + } +}