mirror of
https://github.com/nodejs/node.git
synced 2026-11-21 12:59:16 -05:00
src: allocate Buffer memory using ArrayBuffer allocator
Always use the right allocator for memory that is turned into an `ArrayBuffer` at a later point. This enables embedders to use their own `ArrayBuffer::Allocator`s, and is inspired by Electron’s electron/node@f61bae3440. It should render their downstream patch unnecessary. Refs: https://github.com/electron/node/commit/f61bae3440e1bfcc83bba6ff0785adfb89b4045e PR-URL: https://github.com/nodejs/node/pull/26207 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
This commit is contained in:
16 files changed
+254
-317
No files matched your search
+126
-151
@@ -52,15 +52,6 @@ static const int X509_NAME_FLAGS = ASN1_STRFLGS_ESC_CTRL
|
||||
| XN_FLAG_FN_SN;
|
||||
|
||||
namespace node {
|
||||
namespace Buffer {
|
||||
// OpenSSL uses `unsigned char*` for raw data, make this easier for us.
|
||||
v8::MaybeLocal<v8::Object> New(Environment* env, unsigned char* udata,
|
||||
size_t length) {
|
||||
char* data = reinterpret_cast<char*>(udata);
|
||||
return Buffer::New(env, data, length);
|
||||
}
|
||||
} // namespace Buffer
|
||||
|
||||
namespace crypto {
|
||||
|
||||
using node::THROW_ERR_TLS_INVALID_PROTOCOL_METHOD;
|
||||
@@ -1651,13 +1642,18 @@ static MaybeLocal<Object> ECPointToBuffer(Environment* env,
|
||||
if (error != nullptr) *error = "Failed to get public key length";
|
||||
return MaybeLocal<Object>();
|
||||
}
|
||||
MallocedBuffer<unsigned char> buf(len);
|
||||
len = EC_POINT_point2oct(group, point, form, buf.data, buf.size, nullptr);
|
||||
AllocatedBuffer buf = env->AllocateManaged(len);
|
||||
len = EC_POINT_point2oct(group,
|
||||
point,
|
||||
form,
|
||||
reinterpret_cast<unsigned char*>(buf.data()),
|
||||
buf.size(),
|
||||
nullptr);
|
||||
if (len == 0) {
|
||||
if (error != nullptr) *error = "Failed to get public key";
|
||||
return MaybeLocal<Object>();
|
||||
}
|
||||
return Buffer::New(env, buf.release(), len);
|
||||
return buf.ToBuffer();
|
||||
}
|
||||
|
||||
|
||||
@@ -2036,9 +2032,9 @@ void SSLWrap<Base>::GetFinished(const FunctionCallbackInfo<Value>& args) {
|
||||
if (len == 0)
|
||||
return;
|
||||
|
||||
char* buf = Malloc(len);
|
||||
CHECK_EQ(len, SSL_get_finished(w->ssl_.get(), buf, len));
|
||||
args.GetReturnValue().Set(Buffer::New(env, buf, len).ToLocalChecked());
|
||||
AllocatedBuffer buf = env->AllocateManaged(len);
|
||||
CHECK_EQ(len, SSL_get_finished(w->ssl_.get(), buf.data(), len));
|
||||
args.GetReturnValue().Set(buf.ToBuffer().ToLocalChecked());
|
||||
}
|
||||
|
||||
|
||||
@@ -2059,9 +2055,9 @@ void SSLWrap<Base>::GetPeerFinished(const FunctionCallbackInfo<Value>& args) {
|
||||
if (len == 0)
|
||||
return;
|
||||
|
||||
char* buf = Malloc(len);
|
||||
CHECK_EQ(len, SSL_get_peer_finished(w->ssl_.get(), buf, len));
|
||||
args.GetReturnValue().Set(Buffer::New(env, buf, len).ToLocalChecked());
|
||||
AllocatedBuffer buf = env->AllocateManaged(len);
|
||||
CHECK_EQ(len, SSL_get_peer_finished(w->ssl_.get(), buf.data(), len));
|
||||
args.GetReturnValue().Set(buf.ToBuffer().ToLocalChecked());
|
||||
}
|
||||
|
||||
|
||||
@@ -2079,10 +2075,10 @@ void SSLWrap<Base>::GetSession(const FunctionCallbackInfo<Value>& args) {
|
||||
int slen = i2d_SSL_SESSION(sess, nullptr);
|
||||
CHECK_GT(slen, 0);
|
||||
|
||||
char* sbuf = Malloc(slen);
|
||||
unsigned char* p = reinterpret_cast<unsigned char*>(sbuf);
|
||||
AllocatedBuffer sbuf = env->AllocateManaged(slen);
|
||||
unsigned char* p = reinterpret_cast<unsigned char*>(sbuf.data());
|
||||
i2d_SSL_SESSION(sess, &p);
|
||||
args.GetReturnValue().Set(Buffer::New(env, sbuf, slen).ToLocalChecked());
|
||||
args.GetReturnValue().Set(sbuf.ToBuffer().ToLocalChecked());
|
||||
}
|
||||
|
||||
|
||||
@@ -3963,11 +3959,9 @@ void CipherBase::SetAAD(const FunctionCallbackInfo<Value>& args) {
|
||||
args.GetReturnValue().Set(b); // Possibly report invalid state failure
|
||||
}
|
||||
|
||||
|
||||
CipherBase::UpdateResult CipherBase::Update(const char* data,
|
||||
int len,
|
||||
unsigned char** out,
|
||||
int* out_len) {
|
||||
AllocatedBuffer* out) {
|
||||
if (!ctx_)
|
||||
return kErrorState;
|
||||
MarkPopErrorOnReturn mark_pop_error_on_return;
|
||||
@@ -3985,27 +3979,27 @@ CipherBase::UpdateResult CipherBase::Update(const char* data,
|
||||
CHECK(MaybePassAuthTagToOpenSSL());
|
||||
}
|
||||
|
||||
*out_len = 0;
|
||||
int buff_len = len + EVP_CIPHER_CTX_block_size(ctx_.get());
|
||||
int buf_len = len + EVP_CIPHER_CTX_block_size(ctx_.get());
|
||||
// For key wrapping algorithms, get output size by calling
|
||||
// EVP_CipherUpdate() with null output.
|
||||
if (kind_ == kCipher && mode == EVP_CIPH_WRAP_MODE &&
|
||||
EVP_CipherUpdate(ctx_.get(),
|
||||
nullptr,
|
||||
&buff_len,
|
||||
&buf_len,
|
||||
reinterpret_cast<const unsigned char*>(data),
|
||||
len) != 1) {
|
||||
return kErrorState;
|
||||
}
|
||||
|
||||
*out = Malloc<unsigned char>(buff_len);
|
||||
*out = env()->AllocateManaged(buf_len);
|
||||
int r = EVP_CipherUpdate(ctx_.get(),
|
||||
*out,
|
||||
out_len,
|
||||
reinterpret_cast<unsigned char*>(out->data()),
|
||||
&buf_len,
|
||||
reinterpret_cast<const unsigned char*>(data),
|
||||
len);
|
||||
|
||||
CHECK_LE(*out_len, buff_len);
|
||||
CHECK_LE(static_cast<size_t>(buf_len), out->size());
|
||||
out->Resize(buf_len);
|
||||
|
||||
// When in CCM mode, EVP_CipherUpdate will fail if the authentication tag is
|
||||
// invalid. In that case, remember the error and throw in final().
|
||||
@@ -4023,9 +4017,8 @@ void CipherBase::Update(const FunctionCallbackInfo<Value>& args) {
|
||||
CipherBase* cipher;
|
||||
ASSIGN_OR_RETURN_UNWRAP(&cipher, args.Holder());
|
||||
|
||||
unsigned char* out = nullptr;
|
||||
AllocatedBuffer out;
|
||||
UpdateResult r;
|
||||
int out_len = 0;
|
||||
|
||||
// Only copy the data if we have to, because it's a string
|
||||
if (args[0]->IsString()) {
|
||||
@@ -4033,15 +4026,14 @@ void CipherBase::Update(const FunctionCallbackInfo<Value>& args) {
|
||||
if (!decoder.Decode(env, args[0].As<String>(), args[1], UTF8)
|
||||
.FromMaybe(false))
|
||||
return;
|
||||
r = cipher->Update(decoder.out(), decoder.size(), &out, &out_len);
|
||||
r = cipher->Update(decoder.out(), decoder.size(), &out);
|
||||
} else {
|
||||
char* buf = Buffer::Data(args[0]);
|
||||
size_t buflen = Buffer::Length(args[0]);
|
||||
r = cipher->Update(buf, buflen, &out, &out_len);
|
||||
r = cipher->Update(buf, buflen, &out);
|
||||
}
|
||||
|
||||
if (r != kSuccess) {
|
||||
free(out);
|
||||
if (r == kErrorState) {
|
||||
ThrowCryptoError(env, ERR_get_error(),
|
||||
"Trying to add data in unsupported state");
|
||||
@@ -4049,11 +4041,9 @@ void CipherBase::Update(const FunctionCallbackInfo<Value>& args) {
|
||||
return;
|
||||
}
|
||||
|
||||
CHECK(out != nullptr || out_len == 0);
|
||||
Local<Object> buf =
|
||||
Buffer::New(env, reinterpret_cast<char*>(out), out_len).ToLocalChecked();
|
||||
CHECK(out.data() != nullptr || out.size() == 0);
|
||||
|
||||
args.GetReturnValue().Set(buf);
|
||||
args.GetReturnValue().Set(out.ToBuffer().ToLocalChecked());
|
||||
}
|
||||
|
||||
|
||||
@@ -4073,14 +4063,13 @@ void CipherBase::SetAutoPadding(const FunctionCallbackInfo<Value>& args) {
|
||||
args.GetReturnValue().Set(b); // Possibly report invalid state failure
|
||||
}
|
||||
|
||||
|
||||
bool CipherBase::Final(unsigned char** out, int* out_len) {
|
||||
bool CipherBase::Final(AllocatedBuffer* out) {
|
||||
if (!ctx_)
|
||||
return false;
|
||||
|
||||
const int mode = EVP_CIPHER_CTX_mode(ctx_.get());
|
||||
|
||||
*out = Malloc<unsigned char>(
|
||||
*out = env()->AllocateManaged(
|
||||
static_cast<size_t>(EVP_CIPHER_CTX_block_size(ctx_.get())));
|
||||
|
||||
if (kind_ == kDecipher && IsSupportedAuthenticatedMode(ctx_.get())) {
|
||||
@@ -4092,8 +4081,17 @@ bool CipherBase::Final(unsigned char** out, int* out_len) {
|
||||
bool ok;
|
||||
if (kind_ == kDecipher && mode == EVP_CIPH_CCM_MODE) {
|
||||
ok = !pending_auth_failed_;
|
||||
*out = AllocatedBuffer(env()); // Empty buffer.
|
||||
} else {
|
||||
ok = EVP_CipherFinal_ex(ctx_.get(), *out, out_len) == 1;
|
||||
int out_len = out->size();
|
||||
ok = EVP_CipherFinal_ex(ctx_.get(),
|
||||
reinterpret_cast<unsigned char*>(out->data()),
|
||||
&out_len) == 1;
|
||||
|
||||
if (out_len >= 0)
|
||||
out->Resize(out_len);
|
||||
else
|
||||
*out = AllocatedBuffer(); // *out will not be used.
|
||||
|
||||
if (ok && kind_ == kCipher && IsAuthenticatedMode()) {
|
||||
// In GCM mode, the authentication tag length can be specified in advance,
|
||||
@@ -4122,33 +4120,21 @@ void CipherBase::Final(const FunctionCallbackInfo<Value>& args) {
|
||||
ASSIGN_OR_RETURN_UNWRAP(&cipher, args.Holder());
|
||||
if (cipher->ctx_ == nullptr) return env->ThrowError("Unsupported state");
|
||||
|
||||
unsigned char* out_value = nullptr;
|
||||
int out_len = -1;
|
||||
AllocatedBuffer out;
|
||||
|
||||
// Check IsAuthenticatedMode() first, Final() destroys the EVP_CIPHER_CTX.
|
||||
const bool is_auth_mode = cipher->IsAuthenticatedMode();
|
||||
bool r = cipher->Final(&out_value, &out_len);
|
||||
bool r = cipher->Final(&out);
|
||||
|
||||
if (out_len <= 0 || !r) {
|
||||
free(out_value);
|
||||
out_value = nullptr;
|
||||
out_len = 0;
|
||||
if (!r) {
|
||||
const char* msg = is_auth_mode ?
|
||||
"Unsupported state or unable to authenticate data" :
|
||||
"Unsupported state";
|
||||
if (!r) {
|
||||
const char* msg = is_auth_mode
|
||||
? "Unsupported state or unable to authenticate data"
|
||||
: "Unsupported state";
|
||||
|
||||
return ThrowCryptoError(env,
|
||||
ERR_get_error(),
|
||||
msg);
|
||||
}
|
||||
return ThrowCryptoError(env, ERR_get_error(), msg);
|
||||
}
|
||||
|
||||
Local<Object> buf = Buffer::New(
|
||||
env,
|
||||
reinterpret_cast<char*>(out_value),
|
||||
out_len).ToLocalChecked();
|
||||
args.GetReturnValue().Set(buf);
|
||||
args.GetReturnValue().Set(out.ToBuffer().ToLocalChecked());
|
||||
}
|
||||
|
||||
|
||||
@@ -4508,20 +4494,21 @@ void Sign::SignUpdate(const FunctionCallbackInfo<Value>& args) {
|
||||
sign->CheckThrow(err);
|
||||
}
|
||||
|
||||
static MallocedBuffer<unsigned char> Node_SignFinal(EVPMDPointer&& mdctx,
|
||||
const ManagedEVPPKey& pkey,
|
||||
int padding,
|
||||
int pss_salt_len) {
|
||||
static AllocatedBuffer Node_SignFinal(Environment* env,
|
||||
EVPMDPointer&& mdctx,
|
||||
const ManagedEVPPKey& pkey,
|
||||
int padding,
|
||||
int pss_salt_len) {
|
||||
unsigned char m[EVP_MAX_MD_SIZE];
|
||||
unsigned int m_len;
|
||||
|
||||
if (!EVP_DigestFinal_ex(mdctx.get(), m, &m_len))
|
||||
return MallocedBuffer<unsigned char>();
|
||||
return AllocatedBuffer();
|
||||
|
||||
int signed_sig_len = EVP_PKEY_size(pkey.get());
|
||||
CHECK_GE(signed_sig_len, 0);
|
||||
size_t sig_len = static_cast<size_t>(signed_sig_len);
|
||||
MallocedBuffer<unsigned char> sig(sig_len);
|
||||
AllocatedBuffer sig = env->AllocateManaged(sig_len);
|
||||
|
||||
EVPKeyCtxPointer pkctx(EVP_PKEY_CTX_new(pkey.get(), nullptr));
|
||||
if (pkctx &&
|
||||
@@ -4529,12 +4516,16 @@ static MallocedBuffer<unsigned char> Node_SignFinal(EVPMDPointer&& mdctx,
|
||||
ApplyRSAOptions(pkey, pkctx.get(), padding, pss_salt_len) &&
|
||||
EVP_PKEY_CTX_set_signature_md(pkctx.get(),
|
||||
EVP_MD_CTX_md(mdctx.get())) > 0 &&
|
||||
EVP_PKEY_sign(pkctx.get(), sig.data, &sig_len, m, m_len) > 0) {
|
||||
sig.Truncate(sig_len);
|
||||
EVP_PKEY_sign(pkctx.get(),
|
||||
reinterpret_cast<unsigned char*>(sig.data()),
|
||||
&sig_len,
|
||||
m,
|
||||
m_len) > 0) {
|
||||
sig.Resize(sig_len);
|
||||
return sig;
|
||||
}
|
||||
|
||||
return MallocedBuffer<unsigned char>();
|
||||
return AllocatedBuffer();
|
||||
}
|
||||
|
||||
Sign::SignResult Sign::SignFinal(
|
||||
@@ -4573,16 +4564,14 @@ Sign::SignResult Sign::SignFinal(
|
||||
}
|
||||
#endif // NODE_FIPS_MODE
|
||||
|
||||
MallocedBuffer<unsigned char> buffer =
|
||||
Node_SignFinal(std::move(mdctx), pkey, padding, salt_len);
|
||||
Error error = buffer.is_empty() ? kSignPrivateKey : kSignOk;
|
||||
AllocatedBuffer buffer =
|
||||
Node_SignFinal(env(), std::move(mdctx), pkey, padding, salt_len);
|
||||
Error error = buffer.data() == nullptr ? kSignPrivateKey : kSignOk;
|
||||
return SignResult(error, std::move(buffer));
|
||||
}
|
||||
|
||||
|
||||
void Sign::SignFinal(const FunctionCallbackInfo<Value>& args) {
|
||||
Environment* env = Environment::GetCurrent(args);
|
||||
|
||||
Sign* sign;
|
||||
ASSIGN_OR_RETURN_UNWRAP(&sign, args.Holder());
|
||||
|
||||
@@ -4607,13 +4596,7 @@ void Sign::SignFinal(const FunctionCallbackInfo<Value>& args) {
|
||||
if (ret.error != kSignOk)
|
||||
return sign->CheckThrow(ret.error);
|
||||
|
||||
MallocedBuffer<unsigned char> sig =
|
||||
std::move(ret.signature);
|
||||
|
||||
Local<Object> rc =
|
||||
Buffer::New(env, reinterpret_cast<char*>(sig.release()), sig.size)
|
||||
.ToLocalChecked();
|
||||
args.GetReturnValue().Set(rc);
|
||||
args.GetReturnValue().Set(ret.signature.ToBuffer().ToLocalChecked());
|
||||
}
|
||||
|
||||
void Verify::Initialize(Environment* env, Local<Object> target) {
|
||||
@@ -4722,16 +4705,15 @@ void Verify::VerifyFinal(const FunctionCallbackInfo<Value>& args) {
|
||||
args.GetReturnValue().Set(verify_result);
|
||||
}
|
||||
|
||||
|
||||
template <PublicKeyCipher::Operation operation,
|
||||
PublicKeyCipher::EVP_PKEY_cipher_init_t EVP_PKEY_cipher_init,
|
||||
PublicKeyCipher::EVP_PKEY_cipher_t EVP_PKEY_cipher>
|
||||
bool PublicKeyCipher::Cipher(const ManagedEVPPKey& pkey,
|
||||
bool PublicKeyCipher::Cipher(Environment* env,
|
||||
const ManagedEVPPKey& pkey,
|
||||
int padding,
|
||||
const unsigned char* data,
|
||||
int len,
|
||||
unsigned char** out,
|
||||
size_t* out_len) {
|
||||
AllocatedBuffer* out) {
|
||||
EVPKeyCtxPointer ctx(EVP_PKEY_CTX_new(pkey.get(), nullptr));
|
||||
if (!ctx)
|
||||
return false;
|
||||
@@ -4740,14 +4722,21 @@ bool PublicKeyCipher::Cipher(const ManagedEVPPKey& pkey,
|
||||
if (EVP_PKEY_CTX_set_rsa_padding(ctx.get(), padding) <= 0)
|
||||
return false;
|
||||
|
||||
if (EVP_PKEY_cipher(ctx.get(), nullptr, out_len, data, len) <= 0)
|
||||
size_t out_len = 0;
|
||||
if (EVP_PKEY_cipher(ctx.get(), nullptr, &out_len, data, len) <= 0)
|
||||
return false;
|
||||
|
||||
*out = Malloc<unsigned char>(*out_len);
|
||||
*out = env->AllocateManaged(out_len);
|
||||
|
||||
if (EVP_PKEY_cipher(ctx.get(), *out, out_len, data, len) <= 0)
|
||||
if (EVP_PKEY_cipher(ctx.get(),
|
||||
reinterpret_cast<unsigned char*>(out->data()),
|
||||
&out_len,
|
||||
data,
|
||||
len) <= 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
out->Resize(out_len);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -4770,33 +4759,22 @@ void PublicKeyCipher::Cipher(const FunctionCallbackInfo<Value>& args) {
|
||||
uint32_t padding;
|
||||
if (!args[offset + 1]->Uint32Value(env->context()).To(&padding)) return;
|
||||
|
||||
unsigned char* out_value = nullptr;
|
||||
size_t out_len = 0;
|
||||
AllocatedBuffer out;
|
||||
|
||||
ClearErrorOnReturn clear_error_on_return;
|
||||
|
||||
bool r = Cipher<operation, EVP_PKEY_cipher_init, EVP_PKEY_cipher>(
|
||||
env,
|
||||
pkey,
|
||||
padding,
|
||||
reinterpret_cast<const unsigned char*>(buf),
|
||||
len,
|
||||
&out_value,
|
||||
&out_len);
|
||||
&out);
|
||||
|
||||
if (out_len == 0 || !r) {
|
||||
free(out_value);
|
||||
out_value = nullptr;
|
||||
out_len = 0;
|
||||
if (!r) {
|
||||
return ThrowCryptoError(env,
|
||||
ERR_get_error());
|
||||
}
|
||||
}
|
||||
if (!r)
|
||||
return ThrowCryptoError(env, ERR_get_error());
|
||||
|
||||
Local<Object> vbuf =
|
||||
Buffer::New(env, reinterpret_cast<char*>(out_value), out_len)
|
||||
.ToLocalChecked();
|
||||
args.GetReturnValue().Set(vbuf);
|
||||
args.GetReturnValue().Set(out.ToBuffer().ToLocalChecked());
|
||||
}
|
||||
|
||||
|
||||
@@ -4961,10 +4939,11 @@ void DiffieHellman::GenerateKeys(const FunctionCallbackInfo<Value>& args) {
|
||||
DH_get0_key(diffieHellman->dh_.get(), &pub_key, nullptr);
|
||||
const int size = BN_num_bytes(pub_key);
|
||||
CHECK_GE(size, 0);
|
||||
char* data = Malloc(size);
|
||||
AllocatedBuffer data = env->AllocateManaged(size);
|
||||
CHECK_EQ(size,
|
||||
BN_bn2binpad(pub_key, reinterpret_cast<unsigned char*>(data), size));
|
||||
args.GetReturnValue().Set(Buffer::New(env, data, size).ToLocalChecked());
|
||||
BN_bn2binpad(
|
||||
pub_key, reinterpret_cast<unsigned char*>(data.data()), size));
|
||||
args.GetReturnValue().Set(data.ToBuffer().ToLocalChecked());
|
||||
}
|
||||
|
||||
|
||||
@@ -4981,10 +4960,11 @@ void DiffieHellman::GetField(const FunctionCallbackInfo<Value>& args,
|
||||
|
||||
const int size = BN_num_bytes(num);
|
||||
CHECK_GE(size, 0);
|
||||
char* data = Malloc(size);
|
||||
CHECK_EQ(size,
|
||||
BN_bn2binpad(num, reinterpret_cast<unsigned char*>(data), size));
|
||||
args.GetReturnValue().Set(Buffer::New(env, data, size).ToLocalChecked());
|
||||
AllocatedBuffer data = env->AllocateManaged(size);
|
||||
CHECK_EQ(
|
||||
size,
|
||||
BN_bn2binpad(num, reinterpret_cast<unsigned char*>(data.data()), size));
|
||||
args.GetReturnValue().Set(data.ToBuffer().ToLocalChecked());
|
||||
}
|
||||
|
||||
void DiffieHellman::GetPrime(const FunctionCallbackInfo<Value>& args) {
|
||||
@@ -5042,9 +5022,9 @@ void DiffieHellman::ComputeSecret(const FunctionCallbackInfo<Value>& args) {
|
||||
Buffer::Length(args[0]),
|
||||
nullptr));
|
||||
|
||||
MallocedBuffer<char> data(DH_size(diffieHellman->dh_.get()));
|
||||
AllocatedBuffer ret = env->AllocateManaged(DH_size(diffieHellman->dh_.get()));
|
||||
|
||||
int size = DH_compute_key(reinterpret_cast<unsigned char*>(data.data),
|
||||
int size = DH_compute_key(reinterpret_cast<unsigned char*>(ret.data()),
|
||||
key.get(),
|
||||
diffieHellman->dh_.get());
|
||||
|
||||
@@ -5079,14 +5059,13 @@ void DiffieHellman::ComputeSecret(const FunctionCallbackInfo<Value>& args) {
|
||||
// DH_compute_key returns number of bytes in a remainder of exponent, which
|
||||
// may have less bytes than a prime number. Therefore add 0-padding to the
|
||||
// allocated buffer.
|
||||
if (static_cast<size_t>(size) != data.size) {
|
||||
CHECK_GT(data.size, static_cast<size_t>(size));
|
||||
memmove(data.data + data.size - size, data.data, size);
|
||||
memset(data.data, 0, data.size - size);
|
||||
if (static_cast<size_t>(size) != ret.size()) {
|
||||
CHECK_GT(ret.size(), static_cast<size_t>(size));
|
||||
memmove(ret.data() + ret.size() - size, ret.data(), size);
|
||||
memset(ret.data(), 0, ret.size() - size);
|
||||
}
|
||||
|
||||
args.GetReturnValue().Set(
|
||||
Buffer::New(env->isolate(), data.release(), data.size).ToLocalChecked());
|
||||
args.GetReturnValue().Set(ret.ToBuffer().ToLocalChecked());
|
||||
}
|
||||
|
||||
void DiffieHellman::SetKey(const FunctionCallbackInfo<Value>& args,
|
||||
@@ -5260,15 +5239,14 @@ void ECDH::ComputeSecret(const FunctionCallbackInfo<Value>& args) {
|
||||
// NOTE: field_size is in bits
|
||||
int field_size = EC_GROUP_get_degree(ecdh->group_);
|
||||
size_t out_len = (field_size + 7) / 8;
|
||||
char* out = node::Malloc(out_len);
|
||||
AllocatedBuffer out = env->AllocateManaged(out_len);
|
||||
|
||||
int r = ECDH_compute_key(out, out_len, pub.get(), ecdh->key_.get(), nullptr);
|
||||
if (!r) {
|
||||
free(out);
|
||||
int r = ECDH_compute_key(
|
||||
out.data(), out_len, pub.get(), ecdh->key_.get(), nullptr);
|
||||
if (!r)
|
||||
return env->ThrowError("Failed to compute ECDH key");
|
||||
}
|
||||
|
||||
Local<Object> buf = Buffer::New(env, out, out_len).ToLocalChecked();
|
||||
Local<Object> buf = out.ToBuffer().ToLocalChecked();
|
||||
args.GetReturnValue().Set(buf);
|
||||
}
|
||||
|
||||
@@ -5310,11 +5288,12 @@ void ECDH::GetPrivateKey(const FunctionCallbackInfo<Value>& args) {
|
||||
return env->ThrowError("Failed to get ECDH private key");
|
||||
|
||||
const int size = BN_num_bytes(b);
|
||||
unsigned char* out = node::Malloc<unsigned char>(size);
|
||||
CHECK_EQ(size, BN_bn2binpad(b, out, size));
|
||||
AllocatedBuffer out = env->AllocateManaged(size);
|
||||
CHECK_EQ(size, BN_bn2binpad(b,
|
||||
reinterpret_cast<unsigned char*>(out.data()),
|
||||
size));
|
||||
|
||||
Local<Object> buf =
|
||||
Buffer::New(env, reinterpret_cast<char*>(out), size).ToLocalChecked();
|
||||
Local<Object> buf = out.ToBuffer().ToLocalChecked();
|
||||
args.GetReturnValue().Set(buf);
|
||||
}
|
||||
|
||||
@@ -6066,31 +6045,28 @@ void VerifySpkac(const FunctionCallbackInfo<Value>& args) {
|
||||
args.GetReturnValue().Set(verify_result);
|
||||
}
|
||||
|
||||
|
||||
char* ExportPublicKey(const char* data, int len, size_t* size) {
|
||||
char* buf = nullptr;
|
||||
|
||||
AllocatedBuffer ExportPublicKey(Environment* env,
|
||||
const char* data,
|
||||
int len,
|
||||
size_t* size) {
|
||||
BIOPointer bio(BIO_new(BIO_s_mem()));
|
||||
if (!bio)
|
||||
return nullptr;
|
||||
if (!bio) return AllocatedBuffer();
|
||||
|
||||
NetscapeSPKIPointer spki(NETSCAPE_SPKI_b64_decode(data, len));
|
||||
if (!spki)
|
||||
return nullptr;
|
||||
if (!spki) return AllocatedBuffer();
|
||||
|
||||
EVPKeyPointer pkey(NETSCAPE_SPKI_get_pubkey(spki.get()));
|
||||
if (!pkey)
|
||||
return nullptr;
|
||||
if (!pkey) return AllocatedBuffer();
|
||||
|
||||
if (PEM_write_bio_PUBKEY(bio.get(), pkey.get()) <= 0)
|
||||
return nullptr;
|
||||
return AllocatedBuffer();
|
||||
|
||||
BUF_MEM* ptr;
|
||||
BIO_get_mem_ptr(bio.get(), &ptr);
|
||||
|
||||
*size = ptr->length;
|
||||
buf = Malloc(*size);
|
||||
memcpy(buf, ptr->data, *size);
|
||||
AllocatedBuffer buf = env->AllocateManaged(*size);
|
||||
memcpy(buf.data(), ptr->data, *size);
|
||||
|
||||
return buf;
|
||||
}
|
||||
@@ -6107,12 +6083,11 @@ void ExportPublicKey(const FunctionCallbackInfo<Value>& args) {
|
||||
CHECK_NOT_NULL(data);
|
||||
|
||||
size_t pkey_size;
|
||||
char* pkey = ExportPublicKey(data, length, &pkey_size);
|
||||
if (pkey == nullptr)
|
||||
AllocatedBuffer pkey = ExportPublicKey(env, data, length, &pkey_size);
|
||||
if (pkey.data() == nullptr)
|
||||
return args.GetReturnValue().SetEmptyString();
|
||||
|
||||
Local<Value> out = Buffer::New(env, pkey, pkey_size).ToLocalChecked();
|
||||
args.GetReturnValue().Set(out);
|
||||
args.GetReturnValue().Set(pkey.ToBuffer().ToLocalChecked());
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user