summaryrefslogtreecommitdiff
path: root/src/node_crypto.cc
diff options
context:
space:
mode:
authorBen Noordhuis <info@bnoordhuis.nl>2018-11-29 11:09:12 +0100
committerDaniel Bevenius <daniel.bevenius@gmail.com>2018-12-03 07:29:56 +0100
commit3513b0c3d9711e5eee47ef15b7913f0ce4483663 (patch)
treeb1a7bc1772c92d42fcdbe231bca68477ed6830e9 /src/node_crypto.cc
parent5b90902b8bf260518d73aa2afbccb10dcf82e78a (diff)
downloadandroid-node-v8-3513b0c3d9711e5eee47ef15b7913f0ce4483663.tar.gz
android-node-v8-3513b0c3d9711e5eee47ef15b7913f0ce4483663.tar.bz2
android-node-v8-3513b0c3d9711e5eee47ef15b7913f0ce4483663.zip
crypto: harden bignum-to-binary conversions
PR-URL: https://github.com/nodejs/node/pull/24719 Refs: https://github.com/nodejs/node/issues/24645 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'src/node_crypto.cc')
-rw-r--r--src/node_crypto.cc20
1 files changed, 10 insertions, 10 deletions
diff --git a/src/node_crypto.cc b/src/node_crypto.cc
index 16d1951ff7..f2223a324b 100644
--- a/src/node_crypto.cc
+++ b/src/node_crypto.cc
@@ -4211,9 +4211,11 @@ void DiffieHellman::GenerateKeys(const FunctionCallbackInfo<Value>& args) {
const BIGNUM* pub_key;
DH_get0_key(diffieHellman->dh_.get(), &pub_key, nullptr);
- size_t size = BN_num_bytes(pub_key);
+ const int size = BN_num_bytes(pub_key);
+ CHECK_GE(size, 0);
char* data = Malloc(size);
- BN_bn2bin(pub_key, reinterpret_cast<unsigned char*>(data));
+ CHECK_EQ(size,
+ BN_bn2binpad(pub_key, reinterpret_cast<unsigned char*>(data), size));
args.GetReturnValue().Set(Buffer::New(env, data, size).ToLocalChecked());
}
@@ -4229,9 +4231,11 @@ void DiffieHellman::GetField(const FunctionCallbackInfo<Value>& args,
const BIGNUM* num = get_field(dh->dh_.get());
if (num == nullptr) return env->ThrowError(err_if_null);
- size_t size = BN_num_bytes(num);
+ const int size = BN_num_bytes(num);
+ CHECK_GE(size, 0);
char* data = Malloc(size);
- BN_bn2bin(num, reinterpret_cast<unsigned char*>(data));
+ CHECK_EQ(size,
+ BN_bn2binpad(num, reinterpret_cast<unsigned char*>(data), size));
args.GetReturnValue().Set(Buffer::New(env, data, size).ToLocalChecked());
}
@@ -4567,13 +4571,9 @@ void ECDH::GetPrivateKey(const FunctionCallbackInfo<Value>& args) {
if (b == nullptr)
return env->ThrowError("Failed to get ECDH private key");
- int size = BN_num_bytes(b);
+ const int size = BN_num_bytes(b);
unsigned char* out = node::Malloc<unsigned char>(size);
-
- if (size != BN_bn2bin(b, out)) {
- free(out);
- return env->ThrowError("Failed to convert ECDH private key to Buffer");
- }
+ CHECK_EQ(size, BN_bn2binpad(b, out, size));
Local<Object> buf =
Buffer::New(env, reinterpret_cast<char*>(out), size).ToLocalChecked();