|
12 | 12 |
|
13 | 13 | namespace node {
|
14 | 14 |
|
| 15 | +using v8::ArrayBuffer; |
| 16 | +using v8::BackingStore; |
15 | 17 | using v8::FunctionCallbackInfo;
|
16 | 18 | using v8::FunctionTemplate;
|
17 | 19 | using v8::HandleScope;
|
@@ -69,34 +71,41 @@ bool ApplyRSAOptions(const ManagedEVPPKey& pkey,
|
69 | 71 | return true;
|
70 | 72 | }
|
71 | 73 |
|
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) { |
77 | 79 | unsigned char m[EVP_MAX_MD_SIZE];
|
78 | 80 | unsigned int m_len;
|
79 | 81 |
|
80 | 82 | if (!EVP_DigestFinal_ex(mdctx.get(), m, &m_len))
|
81 |
| - return AllocatedBuffer(); |
| 83 | + return nullptr; |
82 | 84 |
|
83 | 85 | int signed_sig_len = EVP_PKEY_size(pkey.get());
|
84 | 86 | CHECK_GE(signed_sig_len, 0);
|
85 | 87 | 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 | + } |
89 | 93 | EVPKeyCtxPointer pkctx(EVP_PKEY_CTX_new(pkey.get(), nullptr));
|
90 | 94 | if (pkctx &&
|
91 | 95 | EVP_PKEY_sign_init(pkctx.get()) &&
|
92 | 96 | ApplyRSAOptions(pkey, pkctx.get(), padding, pss_salt_len) &&
|
93 | 97 | 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); |
96 | 105 | return sig;
|
97 | 106 | }
|
98 | 107 |
|
99 |
| - return AllocatedBuffer(); |
| 108 | + return nullptr; |
100 | 109 | }
|
101 | 110 |
|
102 | 111 | int GetDefaultSignPadding(const ManagedEVPPKey& m_pkey) {
|
@@ -138,20 +147,20 @@ bool ExtractP1363(
|
138 | 147 | }
|
139 | 148 |
|
140 | 149 | // 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) { |
144 | 152 | unsigned int n = GetBytesOfRS(pkey);
|
145 | 153 | if (n == kNoDsaSignature)
|
146 | 154 | return std::move(signature);
|
147 | 155 |
|
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)) |
155 | 164 | return std::move(signature);
|
156 | 165 |
|
157 | 166 | return buf;
|
@@ -391,12 +400,12 @@ Sign::SignResult Sign::SignFinal(
|
391 | 400 | if (!ValidateDSAParameters(pkey.get()))
|
392 | 401 | return SignResult(kSignPrivateKey);
|
393 | 402 |
|
394 |
| - AllocatedBuffer buffer = |
| 403 | + std::unique_ptr<BackingStore> buffer = |
395 | 404 | Node_SignFinal(env(), std::move(mdctx), pkey, padding, salt_len);
|
396 |
| - Error error = buffer.data() == nullptr ? kSignPrivateKey : kSignOk; |
| 405 | + Error error = buffer ? kSignOk : kSignPrivateKey; |
397 | 406 | if (error == kSignOk && dsa_sig_enc == kSigEncP1363) {
|
398 | 407 | buffer = ConvertSignatureToP1363(env(), pkey, std::move(buffer));
|
399 |
| - CHECK_NOT_NULL(buffer.data()); |
| 408 | + CHECK_NOT_NULL(buffer->Data()); |
400 | 409 | }
|
401 | 410 | return SignResult(error, std::move(buffer));
|
402 | 411 | }
|
@@ -438,7 +447,10 @@ void Sign::SignFinal(const FunctionCallbackInfo<Value>& args) {
|
438 | 447 | if (ret.error != kSignOk)
|
439 | 448 | return crypto::CheckThrow(env, ret.error);
|
440 | 449 |
|
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>())); |
442 | 454 | }
|
443 | 455 |
|
444 | 456 | Verify::Verify(Environment* env, Local<Object> wrap)
|
|
0 commit comments