diff options
author | Doug Hogan <[email protected]> | 2019-01-03 22:20:01 -0800 |
---|---|---|
committer | Doug Hogan <[email protected]> | 2019-01-08 01:11:57 -0800 |
commit | 12f1147428dc3160c50a6642aba96f01008eeb71 (patch) | |
tree | 450a2e909b2a3f35180739644f105ce8c1fda3b7 /lib/crypto | |
parent | a23b898f54b9601872c130ccfc7b2a86f6124e4f (diff) | |
download | otp-12f1147428dc3160c50a6642aba96f01008eeb71.tar.gz otp-12f1147428dc3160c50a6642aba96f01008eeb71.tar.bz2 otp-12f1147428dc3160c50a6642aba96f01008eeb71.zip |
Revamp hash_final_nif()
* Add error handling for all OpenSSL calls.
Diffstat (limited to 'lib/crypto')
-rw-r--r-- | lib/crypto/c_src/hash.c | 36 |
1 files changed, 24 insertions, 12 deletions
diff --git a/lib/crypto/c_src/hash.c b/lib/crypto/c_src/hash.c index 482a3e1a12..ee7a589b49 100644 --- a/lib/crypto/c_src/hash.c +++ b/lib/crypto/c_src/hash.c @@ -175,25 +175,37 @@ ERL_NIF_TERM hash_final_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) EVP_MD_CTX *new_ctx; ERL_NIF_TERM ret; unsigned ret_size; + unsigned char *outp; - if (!enif_get_resource(env, argv[0], evp_md_ctx_rtype, (void**)&ctx)) { - return enif_make_badarg(env); - } + if (argc != 1) + goto bad_arg; + if (!enif_get_resource(env, argv[0], evp_md_ctx_rtype, (void**)&ctx)) + goto bad_arg; ret_size = (unsigned)EVP_MD_CTX_size(ctx->ctx); ASSERT(0 < ret_size && ret_size <= EVP_MAX_MD_SIZE); - new_ctx = EVP_MD_CTX_new(); - if (!EVP_MD_CTX_copy(new_ctx, ctx->ctx) || - !EVP_DigestFinal(new_ctx, - enif_make_new_binary(env, ret_size, &ret), - &ret_size)) { - EVP_MD_CTX_free(new_ctx); - return atom_notsup; - } - EVP_MD_CTX_free(new_ctx); + if ((new_ctx = EVP_MD_CTX_new()) == NULL) + goto err; + if (EVP_MD_CTX_copy(new_ctx, ctx->ctx) != 1) + goto err; + if ((outp = enif_make_new_binary(env, ret_size, &ret)) == NULL) + goto err; + if (EVP_DigestFinal(new_ctx, outp, &ret_size) != 1) + goto err; + ASSERT(ret_size == (unsigned)EVP_MD_CTX_size(ctx->ctx)); + goto done; + bad_arg: + return enif_make_badarg(env); + + err: + ret = atom_notsup; + + done: + if (new_ctx) + EVP_MD_CTX_free(new_ctx); return ret; } |