aboutsummaryrefslogtreecommitdiffstats
path: root/lib/crypto/c_src/rsa.c
diff options
context:
space:
mode:
authorDoug Hogan <[email protected]>2019-01-03 23:07:02 -0800
committerDoug Hogan <[email protected]>2019-01-08 01:11:58 -0800
commit9452fdea68a51adad8c9934cf1dbc2d2b71cb5d6 (patch)
treeff52405c720e2f82cf3cd47883b38ebbcc7fbb84 /lib/crypto/c_src/rsa.c
parent3c50a76d8a3b9c2568e4e0291eccf5491dc9b770 (diff)
downloadotp-9452fdea68a51adad8c9934cf1dbc2d2b71cb5d6.tar.gz
otp-9452fdea68a51adad8c9934cf1dbc2d2b71cb5d6.tar.bz2
otp-9452fdea68a51adad8c9934cf1dbc2d2b71cb5d6.zip
Revamp get_rsa_public_key()
* Added error checking for all OpenSSL calls
Diffstat (limited to 'lib/crypto/c_src/rsa.c')
-rw-r--r--lib/crypto/c_src/rsa.c35
1 files changed, 26 insertions, 9 deletions
diff --git a/lib/crypto/c_src/rsa.c b/lib/crypto/c_src/rsa.c
index ee0c8272a7..cfee30c678 100644
--- a/lib/crypto/c_src/rsa.c
+++ b/lib/crypto/c_src/rsa.c
@@ -120,18 +120,35 @@ int get_rsa_public_key(ErlNifEnv* env, ERL_NIF_TERM key, RSA *rsa)
{
/* key=[E,N] */
ERL_NIF_TERM head, tail;
- BIGNUM *e, *n;
+ BIGNUM *e = NULL, *n = NULL;
- if (!enif_get_list_cell(env, key, &head, &tail)
- || !get_bn_from_bin(env, head, &e)
- || !enif_get_list_cell(env, tail, &head, &tail)
- || !get_bn_from_bin(env, head, &n)
- || !enif_is_empty_list(env, tail)) {
- return 0;
- }
+ if (!enif_get_list_cell(env, key, &head, &tail))
+ goto bad_arg;
+ if (!get_bn_from_bin(env, head, &e))
+ goto bad_arg;
+ if (!enif_get_list_cell(env, tail, &head, &tail))
+ goto bad_arg;
+ if (!get_bn_from_bin(env, head, &n))
+ goto bad_arg;
+ if (!enif_is_empty_list(env, tail))
+ goto bad_arg;
+
+ if (!RSA_set0_key(rsa, n, e, NULL))
+ goto err;
+ /* rsa now owns n and e */
+ n = NULL;
+ e = NULL;
- (void) RSA_set0_key(rsa, n, e, NULL);
return 1;
+
+ bad_arg:
+ err:
+ if (e)
+ BN_free(e);
+ if (n)
+ BN_free(n);
+
+ return 0;
}
/* Creates a term which can be parsed by get_rsa_private_key(). This is a list of plain integer binaries (not mpints). */