Check result of acquiring OSSL_FN or OSSL_FN_CTX

These calls might return NULL because they
allocate memory.

Reviewed-by: Igor Ustinov <igus@openssl.foundation>
Reviewed-by: Daniel Kubec <kubec@openssl.foundation>
MergeDate: Fri Jun 26 14:09:33 2026
(Merged from https://github.com/openssl/openssl/pull/31613)
This commit is contained in:
Tomas Mraz 2026-06-19 10:54:06 +02:00
parent a7b662c282
commit cbc3f0b448
3 changed files with 20 additions and 0 deletions

View file

@ -152,6 +152,9 @@ int BN_uadd(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)
top++;
OSSL_FN *rf = bn_acquire_ossl_fn(r, (int)top);
if (rf == NULL)
return 0;
int ret = OSSL_FN_add(rf, a->data, b->data);
bn_release(r, (int)top);
@ -224,6 +227,9 @@ int BN_usub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)
size_t top = calculate_max_top(a, b);
OSSL_FN *rf = bn_acquire_ossl_fn(r, (int)top);
if (rf == NULL)
return 0;
int ret = OSSL_FN_sub(rf, a->data, b->data);
bn_release(r, (int)top);

View file

@ -34,10 +34,17 @@ int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
*/
size_t top = a->top + b->top;
OSSL_FN *rf = bn_acquire_ossl_fn(r, (int)top);
if (rf == NULL)
return 0;
size_t max = a->dmax + b->dmax;
OSSL_FN_CTX *fnctx = bn_ctx_acquire_ossl_fn_ctx(ctx, 1, 1, max);
if (fnctx == NULL) {
bn_release(r, r->top);
return 0;
}
int ret = OSSL_FN_mul(rf, a->data, b->data, fnctx);
bn_release(r, (int)top);

View file

@ -36,10 +36,17 @@ int BN_sqr(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)
*/
size_t top = a->top * 2;
OSSL_FN *rf = bn_acquire_ossl_fn(r, (int)top);
if (rf == NULL)
return 0;
size_t max = a->dmax * 2;
OSSL_FN_CTX *fnctx = bn_ctx_acquire_ossl_fn_ctx(ctx, 1, 2, max * 4);
if (fnctx == NULL) {
bn_release(r, r->top);
return 0;
}
int ret = OSSL_FN_sqr(rf, a->data, fnctx);
bn_release(r, (int)top);
r->neg = 0;