Skip to content

Commit 90097ab

Browse files
RaisinTendanielleadams
authored andcommitted
src,crypto: remove uses of AllocatedBuffer from crypto_sig
Signed-off-by: Darshan Sen <[email protected]> PR-URL: #40895 Reviewed-By: James M Snell <[email protected]>
1 parent 29739f8 commit 90097ab

File tree

2 files changed

+40
-28
lines changed

2 files changed

+40
-28
lines changed

src/crypto/crypto_sig.cc

+38-26
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
namespace node {
1414

15+
using v8::ArrayBuffer;
16+
using v8::BackingStore;
1517
using v8::FunctionCallbackInfo;
1618
using v8::FunctionTemplate;
1719
using v8::HandleScope;
@@ -69,34 +71,41 @@ bool ApplyRSAOptions(const ManagedEVPPKey& pkey,
6971
return true;
7072
}
7173

72-
AllocatedBuffer Node_SignFinal(Environment* env,
73-
EVPMDPointer&& mdctx,
74-
const ManagedEVPPKey& pkey,
75-
int padding,
76-
Maybe<int> pss_salt_len) {
74+
std::unique_ptr<BackingStore> Node_SignFinal(Environment* env,
75+
EVPMDPointer&& mdctx,
76+
const ManagedEVPPKey& pkey,
77+
int padding,
78+
Maybe<int> pss_salt_len) {
7779
unsigned char m[EVP_MAX_MD_SIZE];
7880
unsigned int m_len;
7981

8082
if (!EVP_DigestFinal_ex(mdctx.get(), m, &m_len))
81-
return AllocatedBuffer();
83+
return nullptr;
8284

8385
int signed_sig_len = EVP_PKEY_size(pkey.get());
8486
CHECK_GE(signed_sig_len, 0);
8587
size_t sig_len = static_cast<size_t>(signed_sig_len);
86-
AllocatedBuffer sig = AllocatedBuffer::AllocateManaged(env, sig_len);
87-
unsigned char* ptr = reinterpret_cast<unsigned char*>(sig.data());
88-
88+
std::unique_ptr<BackingStore> sig;
89+
{
90+
NoArrayBufferZeroFillScope no_zero_fill_scope(env->isolate_data());
91+
sig = ArrayBuffer::NewBackingStore(env->isolate(), sig_len);
92+
}
8993
EVPKeyCtxPointer pkctx(EVP_PKEY_CTX_new(pkey.get(), nullptr));
9094
if (pkctx &&
9195
EVP_PKEY_sign_init(pkctx.get()) &&
9296
ApplyRSAOptions(pkey, pkctx.get(), padding, pss_salt_len) &&
9397
EVP_PKEY_CTX_set_signature_md(pkctx.get(), EVP_MD_CTX_md(mdctx.get())) &&
94-
EVP_PKEY_sign(pkctx.get(), ptr, &sig_len, m, m_len)) {
95-
sig.Resize(sig_len);
98+
EVP_PKEY_sign(pkctx.get(), static_cast<unsigned char*>(sig->Data()),
99+
&sig_len, m, m_len)) {
100+
CHECK_LE(sig_len, sig->ByteLength());
101+
if (sig_len == 0)
102+
sig = ArrayBuffer::NewBackingStore(env->isolate(), 0);
103+
else
104+
sig = BackingStore::Reallocate(env->isolate(), std::move(sig), sig_len);
96105
return sig;
97106
}
98107

99-
return AllocatedBuffer();
108+
return nullptr;
100109
}
101110

102111
int GetDefaultSignPadding(const ManagedEVPPKey& m_pkey) {
@@ -138,20 +147,20 @@ bool ExtractP1363(
138147
}
139148

140149
// Returns the maximum size of each of the integers (r, s) of the DSA signature.
141-
AllocatedBuffer ConvertSignatureToP1363(Environment* env,
142-
const ManagedEVPPKey& pkey,
143-
AllocatedBuffer&& signature) {
150+
std::unique_ptr<BackingStore> ConvertSignatureToP1363(Environment* env,
151+
const ManagedEVPPKey& pkey, std::unique_ptr<BackingStore>&& signature) {
144152
unsigned int n = GetBytesOfRS(pkey);
145153
if (n == kNoDsaSignature)
146154
return std::move(signature);
147155

148-
const unsigned char* sig_data =
149-
reinterpret_cast<unsigned char*>(signature.data());
150-
151-
AllocatedBuffer buf = AllocatedBuffer::AllocateManaged(env, 2 * n);
152-
unsigned char* data = reinterpret_cast<unsigned char*>(buf.data());
153-
154-
if (!ExtractP1363(sig_data, data, signature.size(), n))
156+
std::unique_ptr<BackingStore> buf;
157+
{
158+
NoArrayBufferZeroFillScope no_zero_fill_scope(env->isolate_data());
159+
buf = ArrayBuffer::NewBackingStore(env->isolate(), 2 * n);
160+
}
161+
if (!ExtractP1363(static_cast<unsigned char*>(signature->Data()),
162+
static_cast<unsigned char*>(buf->Data()),
163+
signature->ByteLength(), n))
155164
return std::move(signature);
156165

157166
return buf;
@@ -391,12 +400,12 @@ Sign::SignResult Sign::SignFinal(
391400
if (!ValidateDSAParameters(pkey.get()))
392401
return SignResult(kSignPrivateKey);
393402

394-
AllocatedBuffer buffer =
403+
std::unique_ptr<BackingStore> buffer =
395404
Node_SignFinal(env(), std::move(mdctx), pkey, padding, salt_len);
396-
Error error = buffer.data() == nullptr ? kSignPrivateKey : kSignOk;
405+
Error error = buffer ? kSignOk : kSignPrivateKey;
397406
if (error == kSignOk && dsa_sig_enc == kSigEncP1363) {
398407
buffer = ConvertSignatureToP1363(env(), pkey, std::move(buffer));
399-
CHECK_NOT_NULL(buffer.data());
408+
CHECK_NOT_NULL(buffer->Data());
400409
}
401410
return SignResult(error, std::move(buffer));
402411
}
@@ -438,7 +447,10 @@ void Sign::SignFinal(const FunctionCallbackInfo<Value>& args) {
438447
if (ret.error != kSignOk)
439448
return crypto::CheckThrow(env, ret.error);
440449

441-
args.GetReturnValue().Set(ret.signature.ToBuffer().FromMaybe(Local<Value>()));
450+
Local<ArrayBuffer> ab =
451+
ArrayBuffer::New(env->isolate(), std::move(ret.signature));
452+
args.GetReturnValue().Set(
453+
Buffer::New(env, ab, 0, ab->ByteLength()).FromMaybe(Local<Value>()));
442454
}
443455

444456
Verify::Verify(Environment* env, Local<Object> wrap)

src/crypto/crypto_sig.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ class Sign : public SignBase {
5353

5454
struct SignResult {
5555
Error error;
56-
AllocatedBuffer signature;
56+
std::unique_ptr<v8::BackingStore> signature;
5757

5858
explicit SignResult(
5959
Error err,
60-
AllocatedBuffer&& sig = AllocatedBuffer())
60+
std::unique_ptr<v8::BackingStore>&& sig = nullptr)
6161
: error(err), signature(std::move(sig)) {}
6262
};
6363

0 commit comments

Comments
 (0)