From 90097ab891b8af92544c0be0acceefe94f2d1be2 Mon Sep 17 00:00:00 2001 From: Darshan Sen Date: Sat, 27 Nov 2021 21:25:25 +0530 Subject: [PATCH] src,crypto: remove uses of `AllocatedBuffer` from `crypto_sig` Signed-off-by: Darshan Sen PR-URL: https://github.com/nodejs/node/pull/40895 Reviewed-By: James M Snell --- src/crypto/crypto_sig.cc | 64 ++++++++++++++++++++++++---------------- src/crypto/crypto_sig.h | 4 +-- 2 files changed, 40 insertions(+), 28 deletions(-) diff --git a/src/crypto/crypto_sig.cc b/src/crypto/crypto_sig.cc index 703104d75ace79..90031b0ac4257f 100644 --- a/src/crypto/crypto_sig.cc +++ b/src/crypto/crypto_sig.cc @@ -12,6 +12,8 @@ namespace node { +using v8::ArrayBuffer; +using v8::BackingStore; using v8::FunctionCallbackInfo; using v8::FunctionTemplate; using v8::HandleScope; @@ -69,34 +71,41 @@ bool ApplyRSAOptions(const ManagedEVPPKey& pkey, return true; } -AllocatedBuffer Node_SignFinal(Environment* env, - EVPMDPointer&& mdctx, - const ManagedEVPPKey& pkey, - int padding, - Maybe pss_salt_len) { +std::unique_ptr Node_SignFinal(Environment* env, + EVPMDPointer&& mdctx, + const ManagedEVPPKey& pkey, + int padding, + Maybe pss_salt_len) { unsigned char m[EVP_MAX_MD_SIZE]; unsigned int m_len; if (!EVP_DigestFinal_ex(mdctx.get(), m, &m_len)) - return AllocatedBuffer(); + return nullptr; int signed_sig_len = EVP_PKEY_size(pkey.get()); CHECK_GE(signed_sig_len, 0); size_t sig_len = static_cast(signed_sig_len); - AllocatedBuffer sig = AllocatedBuffer::AllocateManaged(env, sig_len); - unsigned char* ptr = reinterpret_cast(sig.data()); - + std::unique_ptr sig; + { + NoArrayBufferZeroFillScope no_zero_fill_scope(env->isolate_data()); + sig = ArrayBuffer::NewBackingStore(env->isolate(), sig_len); + } EVPKeyCtxPointer pkctx(EVP_PKEY_CTX_new(pkey.get(), nullptr)); if (pkctx && EVP_PKEY_sign_init(pkctx.get()) && ApplyRSAOptions(pkey, pkctx.get(), padding, pss_salt_len) && EVP_PKEY_CTX_set_signature_md(pkctx.get(), EVP_MD_CTX_md(mdctx.get())) && - EVP_PKEY_sign(pkctx.get(), ptr, &sig_len, m, m_len)) { - sig.Resize(sig_len); + EVP_PKEY_sign(pkctx.get(), static_cast(sig->Data()), + &sig_len, m, m_len)) { + CHECK_LE(sig_len, sig->ByteLength()); + if (sig_len == 0) + sig = ArrayBuffer::NewBackingStore(env->isolate(), 0); + else + sig = BackingStore::Reallocate(env->isolate(), std::move(sig), sig_len); return sig; } - return AllocatedBuffer(); + return nullptr; } int GetDefaultSignPadding(const ManagedEVPPKey& m_pkey) { @@ -138,20 +147,20 @@ bool ExtractP1363( } // Returns the maximum size of each of the integers (r, s) of the DSA signature. -AllocatedBuffer ConvertSignatureToP1363(Environment* env, - const ManagedEVPPKey& pkey, - AllocatedBuffer&& signature) { +std::unique_ptr ConvertSignatureToP1363(Environment* env, + const ManagedEVPPKey& pkey, std::unique_ptr&& signature) { unsigned int n = GetBytesOfRS(pkey); if (n == kNoDsaSignature) return std::move(signature); - const unsigned char* sig_data = - reinterpret_cast(signature.data()); - - AllocatedBuffer buf = AllocatedBuffer::AllocateManaged(env, 2 * n); - unsigned char* data = reinterpret_cast(buf.data()); - - if (!ExtractP1363(sig_data, data, signature.size(), n)) + std::unique_ptr buf; + { + NoArrayBufferZeroFillScope no_zero_fill_scope(env->isolate_data()); + buf = ArrayBuffer::NewBackingStore(env->isolate(), 2 * n); + } + if (!ExtractP1363(static_cast(signature->Data()), + static_cast(buf->Data()), + signature->ByteLength(), n)) return std::move(signature); return buf; @@ -391,12 +400,12 @@ Sign::SignResult Sign::SignFinal( if (!ValidateDSAParameters(pkey.get())) return SignResult(kSignPrivateKey); - AllocatedBuffer buffer = + std::unique_ptr buffer = Node_SignFinal(env(), std::move(mdctx), pkey, padding, salt_len); - Error error = buffer.data() == nullptr ? kSignPrivateKey : kSignOk; + Error error = buffer ? kSignOk : kSignPrivateKey; if (error == kSignOk && dsa_sig_enc == kSigEncP1363) { buffer = ConvertSignatureToP1363(env(), pkey, std::move(buffer)); - CHECK_NOT_NULL(buffer.data()); + CHECK_NOT_NULL(buffer->Data()); } return SignResult(error, std::move(buffer)); } @@ -438,7 +447,10 @@ void Sign::SignFinal(const FunctionCallbackInfo& args) { if (ret.error != kSignOk) return crypto::CheckThrow(env, ret.error); - args.GetReturnValue().Set(ret.signature.ToBuffer().FromMaybe(Local())); + Local ab = + ArrayBuffer::New(env->isolate(), std::move(ret.signature)); + args.GetReturnValue().Set( + Buffer::New(env, ab, 0, ab->ByteLength()).FromMaybe(Local())); } Verify::Verify(Environment* env, Local wrap) diff --git a/src/crypto/crypto_sig.h b/src/crypto/crypto_sig.h index eb643685c0bdde..eba18be7c7d019 100644 --- a/src/crypto/crypto_sig.h +++ b/src/crypto/crypto_sig.h @@ -53,11 +53,11 @@ class Sign : public SignBase { struct SignResult { Error error; - AllocatedBuffer signature; + std::unique_ptr signature; explicit SignResult( Error err, - AllocatedBuffer&& sig = AllocatedBuffer()) + std::unique_ptr&& sig = nullptr) : error(err), signature(std::move(sig)) {} };