aboutsummaryrefslogtreecommitdiffstats
path: root/lib/crypto/c_src/bn.c
diff options
context:
space:
mode:
authorDoug Hogan <[email protected]>2019-01-03 19:31:14 -0800
committerDoug Hogan <[email protected]>2019-01-08 00:08:22 -0800
commitd48b585cc3b4b324f30785494d406c7082bf8fdb (patch)
tree765757f379a9f2c9014ad0db007966826fe854b8 /lib/crypto/c_src/bn.c
parente084364e9e41cbc1933a5c0646ce6ad42f4ff8d1 (diff)
downloadotp-d48b585cc3b4b324f30785494d406c7082bf8fdb.tar.gz
otp-d48b585cc3b4b324f30785494d406c7082bf8fdb.tar.bz2
otp-d48b585cc3b4b324f30785494d406c7082bf8fdb.zip
Revamp get_bn_from_mpint()
* Add bounds checking. * Add error checking for OpenSSL calls. * Only set argument *bnp on success.
Diffstat (limited to 'lib/crypto/c_src/bn.c')
-rw-r--r--lib/crypto/c_src/bn.c29
1 files changed, 21 insertions, 8 deletions
diff --git a/lib/crypto/c_src/bn.c b/lib/crypto/c_src/bn.c
index b576c46e1e..a8112350dc 100644
--- a/lib/crypto/c_src/bn.c
+++ b/lib/crypto/c_src/bn.c
@@ -23,18 +23,31 @@
int get_bn_from_mpint(ErlNifEnv* env, ERL_NIF_TERM term, BIGNUM** bnp)
{
+ BIGNUM *ret;
ErlNifBinary bin;
int sz;
- if (!enif_inspect_binary(env,term,&bin)) {
- return 0;
- }
+
+ if (!enif_inspect_binary(env, term, &bin))
+ goto err;
+ if (bin.size > INT_MAX - 4)
+ goto err;
+
ERL_VALGRIND_ASSERT_MEM_DEFINED(bin.data, bin.size);
- sz = bin.size - 4;
- if (sz < 0 || get_int32(bin.data) != sz) {
- return 0;
- }
- *bnp = BN_bin2bn(bin.data+4, sz, NULL);
+
+ if (bin.size < 4)
+ goto err;
+ sz = (int)bin.size - 4;
+ if (get_int32(bin.data) != sz)
+ goto err;
+
+ if ((ret = BN_bin2bn(bin.data+4, sz, NULL)) == NULL)
+ goto err;
+
+ *bnp = ret;
return 1;
+
+ err:
+ return 0;
}
int get_bn_from_bin(ErlNifEnv* env, ERL_NIF_TERM term, BIGNUM** bnp)