In some edge cases BN_CTX_end was being called without first calling
BN_CTX_start. This creates a situation where the state of the big
number allocator is corrupted and may lead to crashes.
Fixes #13812
Reviewed-by: Matt Caswell <matt@openssl.org>
Reviewed-by: Tomas Mraz <tmraz@fedoraproject.org>
(Merged from https://github.com/openssl/openssl/pull/13813)
}
mods = OPENSSL_zalloc(sizeof(*mods) * NUMPRIMES);
- if (mods == NULL)
- goto err;
+ if (mods == NULL) {
+ ERR_raise(ERR_LIB_BN, ERR_R_MALLOC_FAILURE);
+ return 0;
+ }
BN_CTX_start(ctx);
t = BN_CTX_get(ctx);
int r;
BIGNUM *A, *b, *q, *t, *x, *y;
int e, i, j;
+ int used_ctx = 0;
if (!BN_is_odd(p) || BN_abs_is_word(p, 1)) {
if (BN_abs_is_word(p, 2)) {
}
BN_CTX_start(ctx);
+ used_ctx = 1;
A = BN_CTX_get(ctx);
b = BN_CTX_get(ctx);
q = BN_CTX_get(ctx);
BN_clear_free(ret);
ret = NULL;
}
- BN_CTX_end(ctx);
+ if (used_ctx)
+ BN_CTX_end(ctx);
bn_check_top(ret);
return ret;
}
* exceeded.
*/
if (!BN_priv_rand_ex(Xp, nbits, BN_RAND_TOP_TWO, BN_RAND_BOTTOM_ANY, ctx))
- goto err;
+ return 0;
BN_CTX_start(ctx);
t = BN_CTX_get(ctx);
EC_POINT **points = NULL;
EC_PRE_COMP *pre_comp;
int ret = 0;
+ int used_ctx = 0;
#ifndef FIPS_MODULE
BN_CTX *new_ctx = NULL;
#endif
goto err;
BN_CTX_start(ctx);
+ used_ctx = 1;
order = EC_GROUP_get0_order(group);
if (order == NULL)
ret = 1;
err:
- BN_CTX_end(ctx);
+ if (used_ctx)
+ BN_CTX_end(ctx);
#ifndef FIPS_MODULE
BN_CTX_free(new_ctx);
#endif