diff options
author | Doug Hogan <[email protected]> | 2019-01-03 19:36:18 -0800 |
---|---|---|
committer | Doug Hogan <[email protected]> | 2019-01-08 00:08:22 -0800 |
commit | 1ff19cc7870d14fc4ba946994162dfd0006e6459 (patch) | |
tree | 68c53cf5d6dd6f804e716e64af3aadff6f5d6d99 | |
parent | 792e83f15c43771342b9f36d940d2c2bd1cfa336 (diff) | |
download | otp-1ff19cc7870d14fc4ba946994162dfd0006e6459.tar.gz otp-1ff19cc7870d14fc4ba946994162dfd0006e6459.tar.bz2 otp-1ff19cc7870d14fc4ba946994162dfd0006e6459.zip |
Revamp bin_from_bn()
* Add error handling for all OpenSSL calls
- There was nothing returned on error before so use enif_make_badarg().
* Add bounds checking before casting.
-rw-r--r-- | lib/crypto/c_src/bn.c | 10 |
1 files changed, 8 insertions, 2 deletions
diff --git a/lib/crypto/c_src/bn.c b/lib/crypto/c_src/bn.c index a111269539..75ddd4c2f2 100644 --- a/lib/crypto/c_src/bn.c +++ b/lib/crypto/c_src/bn.c @@ -79,11 +79,17 @@ ERL_NIF_TERM bin_from_bn(ErlNifEnv* env, const BIGNUM *bn) ERL_NIF_TERM term; /* Copy the bignum into an erlang binary. */ - bn_len = BN_num_bytes(bn); - bin_ptr = enif_make_new_binary(env, bn_len, &term); + if ((bn_len = BN_num_bytes(bn)) < 0) + goto err; + if ((bin_ptr = enif_make_new_binary(env, (size_t)bn_len, &term)) == NULL) + goto err; + BN_bn2bin(bn, bin_ptr); return term; + + err: + return enif_make_badarg(env); } ERL_NIF_TERM mod_exp_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) |