free NULL cleanup 7

This gets BN_.*free:
    BN_BLINDING_free BN_CTX_free BN_FLG_FREE BN_GENCB_free
    BN_MONT_CTX_free BN_RECP_CTX_free BN_clear_free BN_free BUF_MEM_free

Also fix a call to DSA_SIG_free to ccgost engine and remove some #ifdef'd
dead code in engines/e_ubsec.

Reviewed-by: Richard Levitte <levitte@openssl.org>
This commit is contained in:
Rich Salz 2015-04-30 21:37:06 -04:00
parent 34166d4189
commit 23a1d5e97c
80 changed files with 384 additions and 733 deletions

View File

@ -1532,6 +1532,7 @@ int rand_serial(BIGNUM *b, ASN1_INTEGER *ai)
{ {
BIGNUM *btmp; BIGNUM *btmp;
int ret = 0; int ret = 0;
if (b) if (b)
btmp = b; btmp = b;
else else
@ -1549,7 +1550,7 @@ int rand_serial(BIGNUM *b, ASN1_INTEGER *ai)
error: error:
if (!b) if (btmp != b)
BN_free(btmp); BN_free(btmp);
return ret; return ret;

View File

@ -1289,10 +1289,8 @@ end_of_options:
if (!save_serial(crlnumberfile, "new", crlnumber, NULL)) if (!save_serial(crlnumberfile, "new", crlnumber, NULL))
goto end; goto end;
if (crlnumber) {
BN_free(crlnumber); BN_free(crlnumber);
crlnumber = NULL; crlnumber = NULL;
}
if (!do_X509_CRL_sign(crl, pkey, dgst, sigopts)) if (!do_X509_CRL_sign(crl, pkey, dgst, sigopts))
goto end; goto end;

View File

@ -328,7 +328,6 @@ int dsaparam_main(int argc, char **argv)
app_RAND_write_file(NULL); app_RAND_write_file(NULL);
ret = 0; ret = 0;
end: end:
if (cb != NULL)
BN_GENCB_free(cb); BN_GENCB_free(cb);
BIO_free(in); BIO_free(in);
BIO_free_all(out); BIO_free_all(out);

View File

@ -483,17 +483,11 @@ int ecparam_main(int argc, char **argv)
ret = 0; ret = 0;
end: end:
if (ec_p)
BN_free(ec_p); BN_free(ec_p);
if (ec_a)
BN_free(ec_a); BN_free(ec_a);
if (ec_b)
BN_free(ec_b); BN_free(ec_b);
if (ec_gen)
BN_free(ec_gen); BN_free(ec_gen);
if (ec_order)
BN_free(ec_order); BN_free(ec_order);
if (ec_cofactor)
BN_free(ec_cofactor); BN_free(ec_cofactor);
if (buffer) if (buffer)
OPENSSL_free(buffer); OPENSSL_free(buffer);

View File

@ -214,9 +214,7 @@ int genrsa_main(int argc, char **argv)
ret = 0; ret = 0;
end: end:
if (bn)
BN_free(bn); BN_free(bn);
if (cb)
BN_GENCB_free(cb); BN_GENCB_free(cb);
RSA_free(rsa); RSA_free(rsa);
BIO_free_all(out); BIO_free_all(out);

View File

@ -241,7 +241,6 @@ static unsigned int psk_client_cb(SSL *ssl, const char *hint, char *identity,
if (!ret) { if (!ret) {
BIO_printf(bio_err, "Could not convert PSK key '%s' to BIGNUM\n", BIO_printf(bio_err, "Could not convert PSK key '%s' to BIGNUM\n",
psk_key); psk_key);
if (bn)
BN_free(bn); BN_free(bn);
return 0; return 0;
} }
@ -320,11 +319,8 @@ static int srp_Verify_N_and_g(const BIGNUM *N, const BIGNUM *g)
BN_mod_exp(r, g, p, N, bn_ctx) && BN_mod_exp(r, g, p, N, bn_ctx) &&
BN_add_word(r, 1) && BN_cmp(r, N) == 0; BN_add_word(r, 1) && BN_cmp(r, N) == 0;
if (r)
BN_free(r); BN_free(r);
if (p)
BN_free(p); BN_free(p);
if (bn_ctx)
BN_CTX_free(bn_ctx); BN_CTX_free(bn_ctx);
return ret; return ret;
} }

View File

@ -315,7 +315,6 @@ static unsigned int psk_server_cb(SSL *ssl, const char *identity,
if (!ret) { if (!ret) {
BIO_printf(bio_err, "Could not convert PSK key '%s' to BIGNUM\n", BIO_printf(bio_err, "Could not convert PSK key '%s' to BIGNUM\n",
psk_key); psk_key);
if (bn)
BN_free(bn); BN_free(bn);
return 0; return 0;
} }

View File

@ -201,13 +201,11 @@ int a2d_ASN1_OBJECT(unsigned char *out, int olen, const char *buf, int num)
} }
if (tmp != ftmp) if (tmp != ftmp)
OPENSSL_free(tmp); OPENSSL_free(tmp);
if (bl)
BN_free(bl); BN_free(bl);
return (len); return (len);
err: err:
if (tmp != ftmp) if (tmp != ftmp)
OPENSSL_free(tmp); OPENSSL_free(tmp);
if (bl)
BN_free(bl); BN_free(bl);
return (0); return (0);
} }

View File

@ -166,7 +166,6 @@ BN_BLINDING *BN_BLINDING_new(const BIGNUM *A, const BIGNUM *Ai, BIGNUM *mod)
CRYPTO_THREADID_current(&ret->tid); CRYPTO_THREADID_current(&ret->tid);
return (ret); return (ret);
err: err:
if (ret != NULL)
BN_BLINDING_free(ret); BN_BLINDING_free(ret);
return (NULL); return (NULL);
} }
@ -176,13 +175,9 @@ void BN_BLINDING_free(BN_BLINDING *r)
if (r == NULL) if (r == NULL)
return; return;
if (r->A != NULL)
BN_free(r->A); BN_free(r->A);
if (r->Ai != NULL)
BN_free(r->Ai); BN_free(r->Ai);
if (r->e != NULL)
BN_free(r->e); BN_free(r->e);
if (r->mod != NULL)
BN_free(r->mod); BN_free(r->mod);
OPENSSL_free(r); OPENSSL_free(r);
} }
@ -331,7 +326,6 @@ BN_BLINDING *BN_BLINDING_create_param(BN_BLINDING *b,
goto err; goto err;
if (e != NULL) { if (e != NULL) {
if (ret->e != NULL)
BN_free(ret->e); BN_free(ret->e);
ret->e = BN_dup(e); ret->e = BN_dup(e);
} }
@ -374,7 +368,7 @@ BN_BLINDING *BN_BLINDING_create_param(BN_BLINDING *b,
return ret; return ret;
err: err:
if (b == NULL && ret != NULL) { if (b == NULL) {
BN_BLINDING_free(ret); BN_BLINDING_free(ret);
ret = NULL; ret = NULL;
} }

View File

@ -73,7 +73,6 @@ BIGNUM *BN_generate_prime(BIGNUM *ret, int bits, int safe,
{ {
BN_GENCB cb; BN_GENCB cb;
BIGNUM *rnd = NULL; BIGNUM *rnd = NULL;
int found = 0;
BN_GENCB_set_old(&cb, callback, cb_arg); BN_GENCB_set_old(&cb, callback, cb_arg);
@ -86,11 +85,10 @@ BIGNUM *BN_generate_prime(BIGNUM *ret, int bits, int safe,
goto err; goto err;
/* we have a prime :-) */ /* we have a prime :-) */
found = 1; return ret;
err: err:
if (!found && (ret == NULL) && (rnd != NULL))
BN_free(rnd); BN_free(rnd);
return (found ? rnd : NULL); return NULL;
} }
int BN_is_prime(const BIGNUM *a, int checks, int BN_is_prime(const BIGNUM *a, int checks,

View File

@ -564,7 +564,7 @@ int BN_mod_exp_mont(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,
goto err; goto err;
ret = 1; ret = 1;
err: err:
if ((in_mont == NULL) && (mont != NULL)) if (in_mont == NULL)
BN_MONT_CTX_free(mont); BN_MONT_CTX_free(mont);
BN_CTX_end(ctx); BN_CTX_end(ctx);
bn_check_top(rr); bn_check_top(rr);
@ -1127,7 +1127,7 @@ int BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,
goto err; goto err;
ret = 1; ret = 1;
err: err:
if ((in_mont == NULL) && (mont != NULL)) if (in_mont == NULL)
BN_MONT_CTX_free(mont); BN_MONT_CTX_free(mont);
if (powerbuf != NULL) { if (powerbuf != NULL) {
OPENSSL_cleanse(powerbuf, powerbufLen); OPENSSL_cleanse(powerbuf, powerbufLen);
@ -1277,7 +1277,7 @@ int BN_mod_exp_mont_word(BIGNUM *rr, BN_ULONG a, const BIGNUM *p,
} }
ret = 1; ret = 1;
err: err:
if ((in_mont == NULL) && (mont != NULL)) if (in_mont == NULL)
BN_MONT_CTX_free(mont); BN_MONT_CTX_free(mont);
BN_CTX_end(ctx); BN_CTX_end(ctx);
bn_check_top(rr); bn_check_top(rr);

View File

@ -295,7 +295,7 @@ int BN_mod_exp2_mont(BIGNUM *rr, const BIGNUM *a1, const BIGNUM *p1,
goto err; goto err;
ret = 1; ret = 1;
err: err:
if ((in_mont == NULL) && (mont != NULL)) if (in_mont == NULL)
BN_MONT_CTX_free(mont); BN_MONT_CTX_free(mont);
BN_CTX_end(ctx); BN_CTX_end(ctx);
bn_check_top(rr); bn_check_top(rr);

View File

@ -541,7 +541,6 @@ BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret)
i = ((n - 1) / BN_BYTES) + 1; i = ((n - 1) / BN_BYTES) + 1;
m = ((n - 1) % (BN_BYTES)); m = ((n - 1) % (BN_BYTES));
if (bn_wexpand(ret, (int)i) == NULL) { if (bn_wexpand(ret, (int)i) == NULL) {
if (bn)
BN_free(bn); BN_free(bn);
return NULL; return NULL;
} }

View File

@ -254,7 +254,6 @@ int BN_mod_lshift(BIGNUM *r, const BIGNUM *a, int n, const BIGNUM *m,
ret = BN_mod_lshift_quick(r, r, n, (abs_m ? abs_m : m)); ret = BN_mod_lshift_quick(r, r, n, (abs_m ? abs_m : m));
bn_check_top(r); bn_check_top(r);
if (abs_m)
BN_free(abs_m); BN_free(abs_m);
return ret; return ret;
} }

View File

@ -285,10 +285,9 @@ int BN_generate_prime_ex(BIGNUM *ret, int bits, int safe,
/* we have a prime :-) */ /* we have a prime :-) */
found = 1; found = 1;
err: err:
if (ctx != NULL) { if (ctx != NULL)
BN_CTX_end(ctx); BN_CTX_end(ctx);
BN_CTX_free(ctx); BN_CTX_free(ctx);
}
bn_check_top(ret); bn_check_top(ret);
return found; return found;
} }
@ -397,7 +396,6 @@ int BN_is_prime_fasttest_ex(const BIGNUM *a, int checks, BN_CTX *ctx_passed,
if (ctx_passed == NULL) if (ctx_passed == NULL)
BN_CTX_free(ctx); BN_CTX_free(ctx);
} }
if (mont != NULL)
BN_MONT_CTX_free(mont); BN_MONT_CTX_free(mont);
return (ret); return (ret);

View File

@ -158,7 +158,6 @@ char *BN_bn2dec(const BIGNUM *a)
err: err:
if (bn_data != NULL) if (bn_data != NULL)
OPENSSL_free(bn_data); OPENSSL_free(bn_data);
if (t != NULL)
BN_free(t); BN_free(t);
if (!ok && buf) { if (!ok && buf) {
OPENSSL_free(buf); OPENSSL_free(buf);

View File

@ -398,9 +398,8 @@ BIGNUM *BN_mod_sqrt(BIGNUM *in, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
end: end:
if (err) { if (err) {
if (ret != NULL && ret != in) { if (ret != in)
BN_clear_free(ret); BN_clear_free(ret);
}
ret = NULL; ret = NULL;
} }
BN_CTX_end(ctx); BN_CTX_end(ctx);

View File

@ -463,7 +463,6 @@ static int int_dh_bn_cpy(BIGNUM **dst, const BIGNUM *src)
return 0; return 0;
} else } else
a = NULL; a = NULL;
if (*dst)
BN_free(*dst); BN_free(*dst);
*dst = a; *dst = a;
return 1; return 1;

View File

@ -159,7 +159,6 @@ int DH_check_pub_key(const DH *dh, const BIGNUM *pub_key, int *ret)
ok = 1; ok = 1;
err: err:
if (q != NULL)
BN_free(q); BN_free(q);
return (ok); return (ok);
} }

View File

@ -172,11 +172,9 @@ static int generate_key(DH *dh)
prk = priv_key; prk = priv_key;
if (!dh->meth->bn_mod_exp(dh, pub_key, dh->g, prk, dh->p, ctx, mont)) { if (!dh->meth->bn_mod_exp(dh, pub_key, dh->g, prk, dh->p, ctx, mont)) {
if (local_prk)
BN_free(local_prk); BN_free(local_prk);
goto err; goto err;
} }
if (local_prk)
BN_free(local_prk); BN_free(local_prk);
} }
@ -187,9 +185,9 @@ static int generate_key(DH *dh)
if (ok != 1) if (ok != 1)
DHerr(DH_F_GENERATE_KEY, ERR_R_BN_LIB); DHerr(DH_F_GENERATE_KEY, ERR_R_BN_LIB);
if ((pub_key != NULL) && (dh->pub_key == NULL)) if (pub_key != dh->pub_key)
BN_free(pub_key); BN_free(pub_key);
if ((priv_key != NULL) && (dh->priv_key == NULL)) if (priv_key != dh->priv_key)
BN_free(priv_key); BN_free(priv_key);
BN_CTX_free(ctx); BN_CTX_free(ctx);
return (ok); return (ok);
@ -273,7 +271,6 @@ static int dh_init(DH *dh)
static int dh_finish(DH *dh) static int dh_finish(DH *dh)
{ {
if (dh->method_mont_p)
BN_MONT_CTX_free(dh->method_mont_p); BN_MONT_CTX_free(dh->method_mont_p);
return (1); return (1);
} }

View File

@ -194,21 +194,14 @@ void DH_free(DH *r)
CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DH, r, &r->ex_data); CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DH, r, &r->ex_data);
if (r->p != NULL)
BN_clear_free(r->p); BN_clear_free(r->p);
if (r->g != NULL)
BN_clear_free(r->g); BN_clear_free(r->g);
if (r->q != NULL)
BN_clear_free(r->q); BN_clear_free(r->q);
if (r->j != NULL)
BN_clear_free(r->j); BN_clear_free(r->j);
if (r->seed) if (r->seed)
OPENSSL_free(r->seed); OPENSSL_free(r->seed);
if (r->counter != NULL)
BN_clear_free(r->counter); BN_clear_free(r->counter);
if (r->pub_key != NULL)
BN_clear_free(r->pub_key); BN_clear_free(r->pub_key);
if (r->priv_key != NULL)
BN_clear_free(r->priv_key); BN_clear_free(r->priv_key);
OPENSSL_free(r); OPENSSL_free(r);
} }

View File

@ -383,7 +383,6 @@ static int pkey_dh_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
if (dctx->use_dsa) { if (dctx->use_dsa) {
DSA *dsa_dh; DSA *dsa_dh;
dsa_dh = dsa_dh_generate(dctx, pcb); dsa_dh = dsa_dh_generate(dctx, pcb);
if (pcb)
BN_GENCB_free(pcb); BN_GENCB_free(pcb);
if (!dsa_dh) if (!dsa_dh)
return 0; return 0;
@ -397,13 +396,11 @@ static int pkey_dh_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
#endif #endif
dh = DH_new(); dh = DH_new();
if (!dh) { if (!dh) {
if (pcb)
BN_GENCB_free(pcb); BN_GENCB_free(pcb);
return 0; return 0;
} }
ret = DH_generate_parameters_ex(dh, ret = DH_generate_parameters_ex(dh,
dctx->prime_len, dctx->generator, pcb); dctx->prime_len, dctx->generator, pcb);
if (pcb)
BN_GENCB_free(pcb); BN_GENCB_free(pcb);
if (ret) if (ret)
EVP_PKEY_assign_DH(pkey, dh); EVP_PKEY_assign_DH(pkey, dh);

View File

@ -366,19 +366,16 @@ static int dsa_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
if ((a = BN_dup(from->pkey.dsa->p)) == NULL) if ((a = BN_dup(from->pkey.dsa->p)) == NULL)
return 0; return 0;
if (to->pkey.dsa->p != NULL)
BN_free(to->pkey.dsa->p); BN_free(to->pkey.dsa->p);
to->pkey.dsa->p = a; to->pkey.dsa->p = a;
if ((a = BN_dup(from->pkey.dsa->q)) == NULL) if ((a = BN_dup(from->pkey.dsa->q)) == NULL)
return 0; return 0;
if (to->pkey.dsa->q != NULL)
BN_free(to->pkey.dsa->q); BN_free(to->pkey.dsa->q);
to->pkey.dsa->q = a; to->pkey.dsa->q = a;
if ((a = BN_dup(from->pkey.dsa->g)) == NULL) if ((a = BN_dup(from->pkey.dsa->g)) == NULL)
return 0; return 0;
if (to->pkey.dsa->g != NULL)
BN_free(to->pkey.dsa->g); BN_free(to->pkey.dsa->g);
to->pkey.dsa->g = a; to->pkey.dsa->g = a;
return 1; return 1;

View File

@ -326,11 +326,8 @@ int dsa_builtin_paramgen(DSA *ret, size_t bits, size_t qbits,
ok = 1; ok = 1;
err: err:
if (ok) { if (ok) {
if (ret->p)
BN_free(ret->p); BN_free(ret->p);
if (ret->q)
BN_free(ret->q); BN_free(ret->q);
if (ret->g)
BN_free(ret->g); BN_free(ret->g);
ret->p = BN_dup(p); ret->p = BN_dup(p);
ret->q = BN_dup(q); ret->q = BN_dup(q);
@ -346,11 +343,9 @@ int dsa_builtin_paramgen(DSA *ret, size_t bits, size_t qbits,
if (seed_out) if (seed_out)
memcpy(seed_out, seed, qsize); memcpy(seed_out, seed, qsize);
} }
if (ctx) { if (ctx)
BN_CTX_end(ctx); BN_CTX_end(ctx);
BN_CTX_free(ctx); BN_CTX_free(ctx);
}
if (mont != NULL)
BN_MONT_CTX_free(mont); BN_MONT_CTX_free(mont);
return ok; return ok;
} }
@ -631,16 +626,13 @@ int dsa_builtin_paramgen2(DSA *ret, size_t L, size_t N,
err: err:
if (ok == 1) { if (ok == 1) {
if (p != ret->p) { if (p != ret->p) {
if (ret->p)
BN_free(ret->p); BN_free(ret->p);
ret->p = BN_dup(p); ret->p = BN_dup(p);
} }
if (q != ret->q) { if (q != ret->q) {
if (ret->q)
BN_free(ret->q); BN_free(ret->q);
ret->q = BN_dup(q); ret->q = BN_dup(q);
} }
if (ret->g)
BN_free(ret->g); BN_free(ret->g);
ret->g = BN_dup(g); ret->g = BN_dup(g);
if (ret->p == NULL || ret->q == NULL || ret->g == NULL) { if (ret->p == NULL || ret->q == NULL || ret->g == NULL) {
@ -656,11 +648,9 @@ int dsa_builtin_paramgen2(DSA *ret, size_t L, size_t N,
OPENSSL_free(seed); OPENSSL_free(seed);
if (seed_out != seed_tmp) if (seed_out != seed_tmp)
OPENSSL_free(seed_tmp); OPENSSL_free(seed_tmp);
if (ctx) { if (ctx)
BN_CTX_end(ctx); BN_CTX_end(ctx);
BN_CTX_free(ctx); BN_CTX_free(ctx);
}
if (mont != NULL)
BN_MONT_CTX_free(mont); BN_MONT_CTX_free(mont);
EVP_MD_CTX_cleanup(&mctx); EVP_MD_CTX_cleanup(&mctx);
return ok; return ok;
@ -696,7 +686,6 @@ int dsa_paramgen_check_g(DSA *dsa)
rv = 0; rv = 0;
err: err:
BN_CTX_end(ctx); BN_CTX_end(ctx);
if (mont)
BN_MONT_CTX_free(mont); BN_MONT_CTX_free(mont);
BN_CTX_free(ctx); BN_CTX_free(ctx);
return rv; return rv;

View File

@ -111,11 +111,9 @@ static int dsa_builtin_keygen(DSA *dsa)
prk = priv_key; prk = priv_key;
if (!BN_mod_exp(pub_key, dsa->g, prk, dsa->p, ctx)) { if (!BN_mod_exp(pub_key, dsa->g, prk, dsa->p, ctx)) {
if (local_prk != NULL)
BN_free(local_prk); BN_free(local_prk);
goto err; goto err;
} }
if (local_prk != NULL)
BN_free(local_prk); BN_free(local_prk);
} }
@ -124,11 +122,10 @@ static int dsa_builtin_keygen(DSA *dsa)
ok = 1; ok = 1;
err: err:
if ((pub_key != NULL) && (dsa->pub_key == NULL)) if (pub_key != dsa->pub_key)
BN_free(pub_key); BN_free(pub_key);
if ((priv_key != NULL) && (dsa->priv_key == NULL)) if (priv_key != dsa->priv_key)
BN_free(priv_key); BN_free(priv_key);
if (ctx != NULL)
BN_CTX_free(ctx); BN_CTX_free(ctx);
return (ok); return (ok);
} }

View File

@ -202,19 +202,12 @@ void DSA_free(DSA *r)
CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DSA, r, &r->ex_data); CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DSA, r, &r->ex_data);
if (r->p != NULL)
BN_clear_free(r->p); BN_clear_free(r->p);
if (r->q != NULL)
BN_clear_free(r->q); BN_clear_free(r->q);
if (r->g != NULL)
BN_clear_free(r->g); BN_clear_free(r->g);
if (r->pub_key != NULL)
BN_clear_free(r->pub_key); BN_clear_free(r->pub_key);
if (r->priv_key != NULL)
BN_clear_free(r->priv_key); BN_clear_free(r->priv_key);
if (r->kinv != NULL)
BN_clear_free(r->kinv); BN_clear_free(r->kinv);
if (r->r != NULL)
BN_clear_free(r->r); BN_clear_free(r->r);
OPENSSL_free(r); OPENSSL_free(r);
} }

View File

@ -214,11 +214,9 @@ static DSA_SIG *dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa)
BN_free(r); BN_free(r);
BN_free(s); BN_free(s);
} }
if (ctx != NULL)
BN_CTX_free(ctx); BN_CTX_free(ctx);
BN_clear_free(m); BN_clear_free(m);
BN_clear_free(xr); BN_clear_free(xr);
if (kinv != NULL) /* dsa->kinv is NULL now if we used it */
BN_clear_free(kinv); BN_clear_free(kinv);
return (ret); return (ret);
} }
@ -313,21 +311,18 @@ static int dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in,
if ((kinv = BN_mod_inverse(NULL, k, dsa->q, ctx)) == NULL) if ((kinv = BN_mod_inverse(NULL, k, dsa->q, ctx)) == NULL)
goto err; goto err;
if (*kinvp != NULL)
BN_clear_free(*kinvp); BN_clear_free(*kinvp);
*kinvp = kinv; *kinvp = kinv;
kinv = NULL; kinv = NULL;
if (*rp != NULL)
BN_clear_free(*rp); BN_clear_free(*rp);
*rp = r; *rp = r;
ret = 1; ret = 1;
err: err:
if (!ret) { if (!ret) {
DSAerr(DSA_F_DSA_SIGN_SETUP, ERR_R_BN_LIB); DSAerr(DSA_F_DSA_SIGN_SETUP, ERR_R_BN_LIB);
if (r != NULL)
BN_clear_free(r); BN_clear_free(r);
} }
if (ctx_in == NULL) if (ctx != ctx_in)
BN_CTX_free(ctx); BN_CTX_free(ctx);
BN_clear_free(k); BN_clear_free(k);
BN_clear_free(kq); BN_clear_free(kq);
@ -422,13 +417,9 @@ static int dsa_do_verify(const unsigned char *dgst, int dgst_len,
err: err:
if (ret < 0) if (ret < 0)
DSAerr(DSA_F_DSA_DO_VERIFY, ERR_R_BN_LIB); DSAerr(DSA_F_DSA_DO_VERIFY, ERR_R_BN_LIB);
if (ctx != NULL)
BN_CTX_free(ctx); BN_CTX_free(ctx);
if (u1)
BN_free(u1); BN_free(u1);
if (u2)
BN_free(u2); BN_free(u2);
if (t1)
BN_free(t1); BN_free(t1);
return (ret); return (ret);
} }
@ -441,7 +432,6 @@ static int dsa_init(DSA *dsa)
static int dsa_finish(DSA *dsa) static int dsa_finish(DSA *dsa)
{ {
if (dsa->method_mont_p)
BN_MONT_CTX_free(dsa->method_mont_p); BN_MONT_CTX_free(dsa->method_mont_p);
return (1); return (1);
} }

View File

@ -253,13 +253,11 @@ static int pkey_dsa_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
pcb = NULL; pcb = NULL;
dsa = DSA_new(); dsa = DSA_new();
if (!dsa) { if (!dsa) {
if (pcb)
BN_GENCB_free(pcb); BN_GENCB_free(pcb);
return 0; return 0;
} }
ret = dsa_builtin_paramgen(dsa, dctx->nbits, dctx->qbits, dctx->pmd, ret = dsa_builtin_paramgen(dsa, dctx->nbits, dctx->qbits, dctx->pmd,
NULL, 0, NULL, NULL, NULL, pcb); NULL, 0, NULL, NULL, NULL, pcb);
if (pcb)
BN_GENCB_free(pcb); BN_GENCB_free(pcb);
if (ret) if (ret)
EVP_PKEY_assign_DSA(pkey, dsa); EVP_PKEY_assign_DSA(pkey, dsa);

View File

@ -439,7 +439,6 @@ int ec_GF2m_simple_mul(const EC_GROUP *group, EC_POINT *r,
err: err:
EC_POINT_free(p); EC_POINT_free(p);
EC_POINT_free(acc); EC_POINT_free(acc);
if (new_ctx != NULL)
BN_CTX_free(new_ctx); BN_CTX_free(new_ctx);
return ret; return ret;
} }

View File

@ -160,7 +160,6 @@ int ec_GF2m_simple_set_compressed_coordinates(const EC_GROUP *group,
err: err:
BN_CTX_end(ctx); BN_CTX_end(ctx);
if (new_ctx != NULL)
BN_CTX_free(new_ctx); BN_CTX_free(new_ctx);
return ret; return ret;
} }
@ -278,14 +277,12 @@ size_t ec_GF2m_simple_point2oct(const EC_GROUP *group, const EC_POINT *point,
if (used_ctx) if (used_ctx)
BN_CTX_end(ctx); BN_CTX_end(ctx);
if (new_ctx != NULL)
BN_CTX_free(new_ctx); BN_CTX_free(new_ctx);
return ret; return ret;
err: err:
if (used_ctx) if (used_ctx)
BN_CTX_end(ctx); BN_CTX_end(ctx);
if (new_ctx != NULL)
BN_CTX_free(new_ctx); BN_CTX_free(new_ctx);
return 0; return 0;
} }
@ -396,7 +393,6 @@ int ec_GF2m_simple_oct2point(const EC_GROUP *group, EC_POINT *point,
err: err:
BN_CTX_end(ctx); BN_CTX_end(ctx);
if (new_ctx != NULL)
BN_CTX_free(new_ctx); BN_CTX_free(new_ctx);
return ret; return ret;
} }

View File

@ -135,11 +135,8 @@ int ec_GF2m_simple_group_init(EC_GROUP *group)
group->b = BN_new(); group->b = BN_new();
if (!group->field || !group->a || !group->b) { if (!group->field || !group->a || !group->b) {
if (group->field)
BN_free(group->field); BN_free(group->field);
if (group->a)
BN_free(group->a); BN_free(group->a);
if (group->b)
BN_free(group->b); BN_free(group->b);
return 0; return 0;
} }
@ -318,7 +315,6 @@ int ec_GF2m_simple_group_check_discriminant(const EC_GROUP *group,
err: err:
if (ctx != NULL) if (ctx != NULL)
BN_CTX_end(ctx); BN_CTX_end(ctx);
if (new_ctx != NULL)
BN_CTX_free(new_ctx); BN_CTX_free(new_ctx);
return ret; return ret;
} }
@ -331,11 +327,8 @@ int ec_GF2m_simple_point_init(EC_POINT *point)
point->Z = BN_new(); point->Z = BN_new();
if (!point->X || !point->Y || !point->Z) { if (!point->X || !point->Y || !point->Z) {
if (point->X)
BN_free(point->X); BN_free(point->X);
if (point->Y)
BN_free(point->Y); BN_free(point->Y);
if (point->Z)
BN_free(point->Z); BN_free(point->Z);
return 0; return 0;
} }
@ -569,7 +562,6 @@ int ec_GF2m_simple_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
err: err:
BN_CTX_end(ctx); BN_CTX_end(ctx);
if (new_ctx != NULL)
BN_CTX_free(new_ctx); BN_CTX_free(new_ctx);
return ret; return ret;
} }
@ -663,7 +655,6 @@ int ec_GF2m_simple_is_on_curve(const EC_GROUP *group, const EC_POINT *point,
err: err:
if (ctx) if (ctx)
BN_CTX_end(ctx); BN_CTX_end(ctx);
if (new_ctx)
BN_CTX_free(new_ctx); BN_CTX_free(new_ctx);
return ret; return ret;
} }
@ -716,7 +707,6 @@ int ec_GF2m_simple_cmp(const EC_GROUP *group, const EC_POINT *a,
err: err:
if (ctx) if (ctx)
BN_CTX_end(ctx); BN_CTX_end(ctx);
if (new_ctx)
BN_CTX_free(new_ctx); BN_CTX_free(new_ctx);
return ret; return ret;
} }
@ -758,7 +748,6 @@ int ec_GF2m_simple_make_affine(const EC_GROUP *group, EC_POINT *point,
err: err:
if (ctx) if (ctx)
BN_CTX_end(ctx); BN_CTX_end(ctx);
if (new_ctx)
BN_CTX_free(new_ctx); BN_CTX_free(new_ctx);
return ret; return ret;
} }

View File

@ -500,11 +500,8 @@ static int do_EC_KEY_print(BIO *bp, const EC_KEY *x, int off, int ktype)
err: err:
if (!ret) if (!ret)
ECerr(EC_F_DO_EC_KEY_PRINT, reason); ECerr(EC_F_DO_EC_KEY_PRINT, reason);
if (pub_key)
BN_free(pub_key); BN_free(pub_key);
if (order)
BN_free(order); BN_free(order);
if (ctx)
BN_CTX_free(ctx); BN_CTX_free(ctx);
if (buffer != NULL) if (buffer != NULL)
OPENSSL_free(buffer); OPENSSL_free(buffer);

View File

@ -421,7 +421,7 @@ static int ec_asn1_group2fieldid(const EC_GROUP *group, X9_62_FIELDID *field)
ok = 1; ok = 1;
err:if (tmp) err:
BN_free(tmp); BN_free(tmp);
return (ok); return (ok);
} }
@ -524,13 +524,12 @@ static int ec_asn1_group2curve(const EC_GROUP *group, X9_62_CURVE *curve)
ok = 1; ok = 1;
err:if (buffer_1) err:
if (buffer_1)
OPENSSL_free(buffer_1); OPENSSL_free(buffer_1);
if (buffer_2) if (buffer_2)
OPENSSL_free(buffer_2); OPENSSL_free(buffer_2);
if (tmp_1)
BN_free(tmp_1); BN_free(tmp_1);
if (tmp_2)
BN_free(tmp_2); BN_free(tmp_2);
return (ok); return (ok);
} }
@ -538,7 +537,6 @@ static int ec_asn1_group2curve(const EC_GROUP *group, X9_62_CURVE *curve)
static ECPARAMETERS *ec_asn1_group2parameters(const EC_GROUP *group, static ECPARAMETERS *ec_asn1_group2parameters(const EC_GROUP *group,
ECPARAMETERS *param) ECPARAMETERS *param)
{ {
int ok = 0;
size_t len = 0; size_t len = 0;
ECPARAMETERS *ret = NULL; ECPARAMETERS *ret = NULL;
BIGNUM *tmp = NULL; BIGNUM *tmp = NULL;
@ -624,18 +622,15 @@ static ECPARAMETERS *ec_asn1_group2parameters(const EC_GROUP *group,
} }
} }
ok = 1; return ret;
err:if (!ok) { err:
if (ret && !param) if (!param)
ECPARAMETERS_free(ret); ECPARAMETERS_free(ret);
ret = NULL;
}
if (tmp)
BN_free(tmp); BN_free(tmp);
if (buffer) if (buffer)
OPENSSL_free(buffer); OPENSSL_free(buffer);
return (ret); return NULL;
} }
ECPKPARAMETERS *ec_asn1_group2pkparameters(const EC_GROUP *group, ECPKPARAMETERS *ec_asn1_group2pkparameters(const EC_GROUP *group,
@ -891,10 +886,8 @@ static EC_GROUP *ec_asn1_parameters2group(const ECPARAMETERS *params)
/* extract the cofactor (optional) */ /* extract the cofactor (optional) */
if (params->cofactor == NULL) { if (params->cofactor == NULL) {
if (b) {
BN_free(b); BN_free(b);
b = NULL; b = NULL;
}
} else if ((b = ASN1_INTEGER_to_BN(params->cofactor, b)) == NULL) { } else if ((b = ASN1_INTEGER_to_BN(params->cofactor, b)) == NULL) {
ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_ASN1_LIB); ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_ASN1_LIB);
goto err; goto err;
@ -913,11 +906,8 @@ static EC_GROUP *ec_asn1_parameters2group(const ECPARAMETERS *params)
ret = NULL; ret = NULL;
} }
if (p)
BN_free(p); BN_free(p);
if (a)
BN_free(a); BN_free(a);
if (b)
BN_free(b); BN_free(b);
EC_POINT_free(point); EC_POINT_free(point);
return (ret); return (ret);

View File

@ -112,7 +112,6 @@ int EC_GROUP_check(const EC_GROUP *group, BN_CTX *ctx)
err: err:
if (ctx != NULL) if (ctx != NULL)
BN_CTX_end(ctx); BN_CTX_end(ctx);
if (new_ctx != NULL)
BN_CTX_free(new_ctx); BN_CTX_free(new_ctx);
EC_POINT_free(point); EC_POINT_free(point);
return ret; return ret;

View File

@ -3115,19 +3115,12 @@ static EC_GROUP *ec_group_new_from_data(const ec_list_element curve)
group = NULL; group = NULL;
} }
EC_POINT_free(P); EC_POINT_free(P);
if (ctx)
BN_CTX_free(ctx); BN_CTX_free(ctx);
if (p)
BN_free(p); BN_free(p);
if (a)
BN_free(a); BN_free(a);
if (b)
BN_free(b); BN_free(b);
if (order)
BN_free(order); BN_free(order);
if (x)
BN_free(x); BN_free(x);
if (y)
BN_free(y); BN_free(y);
return group; return group;
} }

View File

@ -122,7 +122,6 @@ void EC_KEY_free(EC_KEY *r)
EC_GROUP_free(r->group); EC_GROUP_free(r->group);
EC_POINT_free(r->pub_key); EC_POINT_free(r->pub_key);
if (r->priv_key != NULL)
BN_clear_free(r->priv_key); BN_clear_free(r->priv_key);
EC_EX_DATA_free_all_data(&r->method_data); EC_EX_DATA_free_all_data(&r->method_data);
@ -266,13 +265,11 @@ int EC_KEY_generate_key(EC_KEY *eckey)
ok = 1; ok = 1;
err: err:
if (order)
BN_free(order); BN_free(order);
if (eckey->pub_key == NULL) if (eckey->pub_key == NULL)
EC_POINT_free(pub_key); EC_POINT_free(pub_key);
if (priv_key != NULL && eckey->priv_key == NULL) if (eckey->priv_key != priv_key)
BN_free(priv_key); BN_free(priv_key);
if (ctx != NULL)
BN_CTX_free(ctx); BN_CTX_free(ctx);
return (ok); return (ok);
} }
@ -339,7 +336,6 @@ int EC_KEY_check_key(const EC_KEY *eckey)
} }
ok = 1; ok = 1;
err: err:
if (ctx != NULL)
BN_CTX_free(ctx); BN_CTX_free(ctx);
EC_POINT_free(point); EC_POINT_free(point);
return (ok); return (ok);
@ -413,7 +409,6 @@ int EC_KEY_set_public_key_affine_coordinates(EC_KEY *key, BIGNUM *x,
ok = 1; ok = 1;
err: err:
if (ctx)
BN_CTX_free(ctx); BN_CTX_free(ctx);
EC_POINT_free(point); EC_POINT_free(point);
return ok; return ok;
@ -439,7 +434,6 @@ const BIGNUM *EC_KEY_get0_private_key(const EC_KEY *key)
int EC_KEY_set_private_key(EC_KEY *key, const BIGNUM *priv_key) int EC_KEY_set_private_key(EC_KEY *key, const BIGNUM *priv_key)
{ {
if (key->priv_key)
BN_clear_free(key->priv_key); BN_clear_free(key->priv_key);
key->priv_key = BN_dup(priv_key); key->priv_key = BN_dup(priv_key);
return (key->priv_key == NULL) ? 0 : 1; return (key->priv_key == NULL) ? 0 : 1;

View File

@ -117,9 +117,7 @@ EC_GROUP *EC_GROUP_new(const EC_METHOD *meth)
return ret; return ret;
err: err:
if (ret->order)
BN_free(ret->order); BN_free(ret->order);
if (ret->cofactor)
BN_free(ret->cofactor); BN_free(ret->cofactor);
OPENSSL_free(ret); OPENSSL_free(ret);
return NULL; return NULL;
@ -135,7 +133,6 @@ void EC_GROUP_free(EC_GROUP *group)
EC_EX_DATA_free_all_data(&group->extra_data); EC_EX_DATA_free_all_data(&group->extra_data);
if (group->mont_data)
BN_MONT_CTX_free(group->mont_data); BN_MONT_CTX_free(group->mont_data);
EC_POINT_free(group->generator); EC_POINT_free(group->generator);
@ -160,7 +157,6 @@ void EC_GROUP_clear_free(EC_GROUP *group)
EC_EX_DATA_clear_free_all_data(&group->extra_data); EC_EX_DATA_clear_free_all_data(&group->extra_data);
if (group->mont_data)
BN_MONT_CTX_free(group->mont_data); BN_MONT_CTX_free(group->mont_data);
EC_POINT_clear_free(group->generator); EC_POINT_clear_free(group->generator);
@ -208,11 +204,9 @@ int EC_GROUP_copy(EC_GROUP *dest, const EC_GROUP *src)
return 0; return 0;
} else { } else {
/* src->generator == NULL */ /* src->generator == NULL */
if (dest->mont_data != NULL) {
BN_MONT_CTX_free(dest->mont_data); BN_MONT_CTX_free(dest->mont_data);
dest->mont_data = NULL; dest->mont_data = NULL;
} }
}
if (src->generator != NULL) { if (src->generator != NULL) {
if (dest->generator == NULL) { if (dest->generator == NULL) {
@ -507,8 +501,7 @@ int EC_GROUP_cmp(const EC_GROUP *a, const EC_GROUP *b, BN_CTX *ctx)
b3 = BN_CTX_get(ctx); b3 = BN_CTX_get(ctx);
if (!b3) { if (!b3) {
BN_CTX_end(ctx); BN_CTX_end(ctx);
if (ctx_new) BN_CTX_free(ctx_new);
BN_CTX_free(ctx);
return -1; return -1;
} }
@ -535,8 +528,7 @@ int EC_GROUP_cmp(const EC_GROUP *a, const EC_GROUP *b, BN_CTX *ctx)
!EC_GROUP_get_cofactor(a, a2, ctx) || !EC_GROUP_get_cofactor(a, a2, ctx) ||
!EC_GROUP_get_cofactor(b, b2, ctx)) { !EC_GROUP_get_cofactor(b, b2, ctx)) {
BN_CTX_end(ctx); BN_CTX_end(ctx);
if (ctx_new) BN_CTX_free(ctx_new);
BN_CTX_free(ctx);
return -1; return -1;
} }
if (BN_cmp(a1, b1) || BN_cmp(a2, b2)) if (BN_cmp(a1, b1) || BN_cmp(a2, b2))
@ -544,8 +536,7 @@ int EC_GROUP_cmp(const EC_GROUP *a, const EC_GROUP *b, BN_CTX *ctx)
} }
BN_CTX_end(ctx); BN_CTX_end(ctx);
if (ctx_new) BN_CTX_free(ctx_new);
BN_CTX_free(ctx);
return r; return r;
} }
@ -1093,10 +1084,8 @@ int ec_precompute_mont_data(EC_GROUP *group)
BN_CTX *ctx = BN_CTX_new(); BN_CTX *ctx = BN_CTX_new();
int ret = 0; int ret = 0;
if (group->mont_data) {
BN_MONT_CTX_free(group->mont_data); BN_MONT_CTX_free(group->mont_data);
group->mont_data = NULL; group->mont_data = NULL;
}
if (ctx == NULL) if (ctx == NULL)
goto err; goto err;
@ -1115,7 +1104,6 @@ int ec_precompute_mont_data(EC_GROUP *group)
err: err:
if (ctx)
BN_CTX_free(ctx); BN_CTX_free(ctx);
return ret; return ret;
} }

View File

@ -553,7 +553,6 @@ int ec_wNAF_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
ret = 1; ret = 1;
err: err:
if (new_ctx != NULL)
BN_CTX_free(new_ctx); BN_CTX_free(new_ctx);
EC_POINT_free(tmp); EC_POINT_free(tmp);
if (wsize != NULL) if (wsize != NULL)
@ -746,7 +745,6 @@ int ec_wNAF_precompute_mult(EC_GROUP *group, BN_CTX *ctx)
err: err:
if (ctx != NULL) if (ctx != NULL)
BN_CTX_end(ctx); BN_CTX_end(ctx);
if (new_ctx != NULL)
BN_CTX_free(new_ctx); BN_CTX_free(new_ctx);
if (pre_comp) if (pre_comp)
ec_pre_comp_free(pre_comp); ec_pre_comp_free(pre_comp);

View File

@ -318,19 +318,12 @@ int ECPKParameters_print(BIO *bp, const EC_GROUP *x, int off)
err: err:
if (!ret) if (!ret)
ECerr(EC_F_ECPKPARAMETERS_PRINT, reason); ECerr(EC_F_ECPKPARAMETERS_PRINT, reason);
if (p)
BN_free(p); BN_free(p);
if (a)
BN_free(a); BN_free(a);
if (b)
BN_free(b); BN_free(b);
if (gen)
BN_free(gen); BN_free(gen);
if (order)
BN_free(order); BN_free(order);
if (cofactor)
BN_free(cofactor); BN_free(cofactor);
if (ctx)
BN_CTX_free(ctx); BN_CTX_free(ctx);
if (buffer != NULL) if (buffer != NULL)
OPENSSL_free(buffer); OPENSSL_free(buffer);

View File

@ -122,40 +122,28 @@ int ec_GFp_mont_group_init(EC_GROUP *group)
void ec_GFp_mont_group_finish(EC_GROUP *group) void ec_GFp_mont_group_finish(EC_GROUP *group)
{ {
if (group->field_data1 != NULL) {
BN_MONT_CTX_free(group->field_data1); BN_MONT_CTX_free(group->field_data1);
group->field_data1 = NULL; group->field_data1 = NULL;
}
if (group->field_data2 != NULL) {
BN_free(group->field_data2); BN_free(group->field_data2);
group->field_data2 = NULL; group->field_data2 = NULL;
}
ec_GFp_simple_group_finish(group); ec_GFp_simple_group_finish(group);
} }
void ec_GFp_mont_group_clear_finish(EC_GROUP *group) void ec_GFp_mont_group_clear_finish(EC_GROUP *group)
{ {
if (group->field_data1 != NULL) {
BN_MONT_CTX_free(group->field_data1); BN_MONT_CTX_free(group->field_data1);
group->field_data1 = NULL; group->field_data1 = NULL;
}
if (group->field_data2 != NULL) {
BN_clear_free(group->field_data2); BN_clear_free(group->field_data2);
group->field_data2 = NULL; group->field_data2 = NULL;
}
ec_GFp_simple_group_clear_finish(group); ec_GFp_simple_group_clear_finish(group);
} }
int ec_GFp_mont_group_copy(EC_GROUP *dest, const EC_GROUP *src) int ec_GFp_mont_group_copy(EC_GROUP *dest, const EC_GROUP *src)
{ {
if (dest->field_data1 != NULL) {
BN_MONT_CTX_free(dest->field_data1); BN_MONT_CTX_free(dest->field_data1);
dest->field_data1 = NULL; dest->field_data1 = NULL;
}
if (dest->field_data2 != NULL) {
BN_clear_free(dest->field_data2); BN_clear_free(dest->field_data2);
dest->field_data2 = NULL; dest->field_data2 = NULL;
}
if (!ec_GFp_simple_group_copy(dest, src)) if (!ec_GFp_simple_group_copy(dest, src))
return 0; return 0;
@ -176,10 +164,8 @@ int ec_GFp_mont_group_copy(EC_GROUP *dest, const EC_GROUP *src)
return 1; return 1;
err: err:
if (dest->field_data1 != NULL) {
BN_MONT_CTX_free(dest->field_data1); BN_MONT_CTX_free(dest->field_data1);
dest->field_data1 = NULL; dest->field_data1 = NULL;
}
return 0; return 0;
} }
@ -191,14 +177,10 @@ int ec_GFp_mont_group_set_curve(EC_GROUP *group, const BIGNUM *p,
BIGNUM *one = NULL; BIGNUM *one = NULL;
int ret = 0; int ret = 0;
if (group->field_data1 != NULL) {
BN_MONT_CTX_free(group->field_data1); BN_MONT_CTX_free(group->field_data1);
group->field_data1 = NULL; group->field_data1 = NULL;
}
if (group->field_data2 != NULL) {
BN_free(group->field_data2); BN_free(group->field_data2);
group->field_data2 = NULL; group->field_data2 = NULL;
}
if (ctx == NULL) { if (ctx == NULL) {
ctx = new_ctx = BN_CTX_new(); ctx = new_ctx = BN_CTX_new();
@ -234,9 +216,7 @@ int ec_GFp_mont_group_set_curve(EC_GROUP *group, const BIGNUM *p,
} }
err: err:
if (new_ctx != NULL)
BN_CTX_free(new_ctx); BN_CTX_free(new_ctx);
if (mont != NULL)
BN_MONT_CTX_free(mont); BN_MONT_CTX_free(mont);
return ret; return ret;
} }

View File

@ -150,7 +150,6 @@ int ec_GFp_nist_group_set_curve(EC_GROUP *group, const BIGNUM *p,
err: err:
BN_CTX_end(ctx); BN_CTX_end(ctx);
if (new_ctx != NULL)
BN_CTX_free(new_ctx); BN_CTX_free(new_ctx);
return ret; return ret;
} }
@ -176,7 +175,6 @@ int ec_GFp_nist_field_mul(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
ret = 1; ret = 1;
err: err:
if (ctx_new)
BN_CTX_free(ctx_new); BN_CTX_free(ctx_new);
return ret; return ret;
} }
@ -202,7 +200,6 @@ int ec_GFp_nist_field_sqr(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
ret = 1; ret = 1;
err: err:
if (ctx_new)
BN_CTX_free(ctx_new); BN_CTX_free(ctx_new);
return ret; return ret;
} }

View File

@ -1291,7 +1291,6 @@ int ec_GFp_nistp224_group_set_curve(EC_GROUP *group, const BIGNUM *p,
ret = ec_GFp_simple_group_set_curve(group, p, a, b, ctx); ret = ec_GFp_simple_group_set_curve(group, p, a, b, ctx);
err: err:
BN_CTX_end(ctx); BN_CTX_end(ctx);
if (new_ctx != NULL)
BN_CTX_free(new_ctx); BN_CTX_free(new_ctx);
return ret; return ret;
} }
@ -1573,7 +1572,6 @@ int ec_GFp_nistp224_points_mul(const EC_GROUP *group, EC_POINT *r,
err: err:
BN_CTX_end(ctx); BN_CTX_end(ctx);
EC_POINT_free(generator); EC_POINT_free(generator);
if (new_ctx != NULL)
BN_CTX_free(new_ctx); BN_CTX_free(new_ctx);
if (secrets != NULL) if (secrets != NULL)
OPENSSL_free(secrets); OPENSSL_free(secrets);
@ -1709,7 +1707,6 @@ int ec_GFp_nistp224_precompute_mult(EC_GROUP *group, BN_CTX *ctx)
err: err:
BN_CTX_end(ctx); BN_CTX_end(ctx);
EC_POINT_free(generator); EC_POINT_free(generator);
if (new_ctx != NULL)
BN_CTX_free(new_ctx); BN_CTX_free(new_ctx);
nistp224_pre_comp_free(pre); nistp224_pre_comp_free(pre);
return ret; return ret;

View File

@ -1906,7 +1906,6 @@ int ec_GFp_nistp256_group_set_curve(EC_GROUP *group, const BIGNUM *p,
ret = ec_GFp_simple_group_set_curve(group, p, a, b, ctx); ret = ec_GFp_simple_group_set_curve(group, p, a, b, ctx);
err: err:
BN_CTX_end(ctx); BN_CTX_end(ctx);
if (new_ctx != NULL)
BN_CTX_free(new_ctx); BN_CTX_free(new_ctx);
return ret; return ret;
} }
@ -2193,7 +2192,6 @@ int ec_GFp_nistp256_points_mul(const EC_GROUP *group, EC_POINT *r,
err: err:
BN_CTX_end(ctx); BN_CTX_end(ctx);
EC_POINT_free(generator); EC_POINT_free(generator);
if (new_ctx != NULL)
BN_CTX_free(new_ctx); BN_CTX_free(new_ctx);
if (secrets != NULL) if (secrets != NULL)
OPENSSL_free(secrets); OPENSSL_free(secrets);
@ -2339,7 +2337,6 @@ int ec_GFp_nistp256_precompute_mult(EC_GROUP *group, BN_CTX *ctx)
err: err:
BN_CTX_end(ctx); BN_CTX_end(ctx);
EC_POINT_free(generator); EC_POINT_free(generator);
if (new_ctx != NULL)
BN_CTX_free(new_ctx); BN_CTX_free(new_ctx);
nistp256_pre_comp_free(pre); nistp256_pre_comp_free(pre);
return ret; return ret;

View File

@ -1735,7 +1735,6 @@ int ec_GFp_nistp521_group_set_curve(EC_GROUP *group, const BIGNUM *p,
ret = ec_GFp_simple_group_set_curve(group, p, a, b, ctx); ret = ec_GFp_simple_group_set_curve(group, p, a, b, ctx);
err: err:
BN_CTX_end(ctx); BN_CTX_end(ctx);
if (new_ctx != NULL)
BN_CTX_free(new_ctx); BN_CTX_free(new_ctx);
return ret; return ret;
} }
@ -2021,7 +2020,6 @@ int ec_GFp_nistp521_points_mul(const EC_GROUP *group, EC_POINT *r,
err: err:
BN_CTX_end(ctx); BN_CTX_end(ctx);
EC_POINT_free(generator); EC_POINT_free(generator);
if (new_ctx != NULL)
BN_CTX_free(new_ctx); BN_CTX_free(new_ctx);
if (secrets != NULL) if (secrets != NULL)
OPENSSL_free(secrets); OPENSSL_free(secrets);
@ -2137,7 +2135,6 @@ int ec_GFp_nistp521_precompute_mult(EC_GROUP *group, BN_CTX *ctx)
err: err:
BN_CTX_end(ctx); BN_CTX_end(ctx);
EC_POINT_free(generator); EC_POINT_free(generator);
if (new_ctx != NULL)
BN_CTX_free(new_ctx); BN_CTX_free(new_ctx);
nistp521_pre_comp_free(pre); nistp521_pre_comp_free(pre);
return ret; return ret;

View File

@ -1121,9 +1121,7 @@ __owur static int ecp_nistz256_set_from_affine(EC_POINT *out, const EC_GROUP *gr
ret = EC_POINT_set_affine_coordinates_GFp(group, out, x, y, ctx); ret = EC_POINT_set_affine_coordinates_GFp(group, out, x, y, ctx);
if (x)
BN_free(x); BN_free(x);
if (y)
BN_free(y); BN_free(y);
return ret; return ret;

View File

@ -202,7 +202,6 @@ int ec_GFp_simple_set_compressed_coordinates(const EC_GROUP *group,
err: err:
BN_CTX_end(ctx); BN_CTX_end(ctx);
if (new_ctx != NULL)
BN_CTX_free(new_ctx); BN_CTX_free(new_ctx);
return ret; return ret;
} }
@ -312,14 +311,12 @@ size_t ec_GFp_simple_point2oct(const EC_GROUP *group, const EC_POINT *point,
if (used_ctx) if (used_ctx)
BN_CTX_end(ctx); BN_CTX_end(ctx);
if (new_ctx != NULL)
BN_CTX_free(new_ctx); BN_CTX_free(new_ctx);
return ret; return ret;
err: err:
if (used_ctx) if (used_ctx)
BN_CTX_end(ctx); BN_CTX_end(ctx);
if (new_ctx != NULL)
BN_CTX_free(new_ctx); BN_CTX_free(new_ctx);
return 0; return 0;
} }
@ -422,7 +419,6 @@ int ec_GFp_simple_oct2point(const EC_GROUP *group, EC_POINT *point,
err: err:
BN_CTX_end(ctx); BN_CTX_end(ctx);
if (new_ctx != NULL)
BN_CTX_free(new_ctx); BN_CTX_free(new_ctx);
return ret; return ret;
} }

View File

@ -225,7 +225,6 @@ int ec_GFp_simple_group_set_curve(EC_GROUP *group,
err: err:
BN_CTX_end(ctx); BN_CTX_end(ctx);
if (new_ctx != NULL)
BN_CTX_free(new_ctx); BN_CTX_free(new_ctx);
return ret; return ret;
} }
@ -271,7 +270,6 @@ int ec_GFp_simple_group_get_curve(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
ret = 1; ret = 1;
err: err:
if (new_ctx)
BN_CTX_free(new_ctx); BN_CTX_free(new_ctx);
return ret; return ret;
} }
@ -350,7 +348,6 @@ int ec_GFp_simple_group_check_discriminant(const EC_GROUP *group, BN_CTX *ctx)
err: err:
if (ctx != NULL) if (ctx != NULL)
BN_CTX_end(ctx); BN_CTX_end(ctx);
if (new_ctx != NULL)
BN_CTX_free(new_ctx); BN_CTX_free(new_ctx);
return ret; return ret;
} }
@ -363,11 +360,8 @@ int ec_GFp_simple_point_init(EC_POINT *point)
point->Z_is_one = 0; point->Z_is_one = 0;
if (!point->X || !point->Y || !point->Z) { if (!point->X || !point->Y || !point->Z) {
if (point->X)
BN_free(point->X); BN_free(point->X);
if (point->Y)
BN_free(point->Y); BN_free(point->Y);
if (point->Z)
BN_free(point->Z); BN_free(point->Z);
return 0; return 0;
} }
@ -466,7 +460,6 @@ int ec_GFp_simple_set_Jprojective_coordinates_GFp(const EC_GROUP *group,
ret = 1; ret = 1;
err: err:
if (new_ctx != NULL)
BN_CTX_free(new_ctx); BN_CTX_free(new_ctx);
return ret; return ret;
} }
@ -516,7 +509,6 @@ int ec_GFp_simple_get_Jprojective_coordinates_GFp(const EC_GROUP *group,
ret = 1; ret = 1;
err: err:
if (new_ctx != NULL)
BN_CTX_free(new_ctx); BN_CTX_free(new_ctx);
return ret; return ret;
} }
@ -649,7 +641,6 @@ int ec_GFp_simple_point_get_affine_coordinates(const EC_GROUP *group,
err: err:
BN_CTX_end(ctx); BN_CTX_end(ctx);
if (new_ctx != NULL)
BN_CTX_free(new_ctx); BN_CTX_free(new_ctx);
return ret; return ret;
} }
@ -836,7 +827,6 @@ int ec_GFp_simple_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
end: end:
if (ctx) /* otherwise we already called BN_CTX_end */ if (ctx) /* otherwise we already called BN_CTX_end */
BN_CTX_end(ctx); BN_CTX_end(ctx);
if (new_ctx != NULL)
BN_CTX_free(new_ctx); BN_CTX_free(new_ctx);
return ret; return ret;
} }
@ -979,7 +969,6 @@ int ec_GFp_simple_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
err: err:
BN_CTX_end(ctx); BN_CTX_end(ctx);
if (new_ctx != NULL)
BN_CTX_free(new_ctx); BN_CTX_free(new_ctx);
return ret; return ret;
} }
@ -1097,7 +1086,6 @@ int ec_GFp_simple_is_on_curve(const EC_GROUP *group, const EC_POINT *point,
err: err:
BN_CTX_end(ctx); BN_CTX_end(ctx);
if (new_ctx != NULL)
BN_CTX_free(new_ctx); BN_CTX_free(new_ctx);
return ret; return ret;
} }
@ -1206,7 +1194,6 @@ int ec_GFp_simple_cmp(const EC_GROUP *group, const EC_POINT *a,
end: end:
BN_CTX_end(ctx); BN_CTX_end(ctx);
if (new_ctx != NULL)
BN_CTX_free(new_ctx); BN_CTX_free(new_ctx);
return ret; return ret;
} }
@ -1246,7 +1233,6 @@ int ec_GFp_simple_make_affine(const EC_GROUP *group, EC_POINT *point,
err: err:
BN_CTX_end(ctx); BN_CTX_end(ctx);
if (new_ctx != NULL)
BN_CTX_free(new_ctx); BN_CTX_free(new_ctx);
return ret; return ret;
} }
@ -1398,7 +1384,6 @@ int ec_GFp_simple_points_make_affine(const EC_GROUP *group, size_t num,
err: err:
BN_CTX_end(ctx); BN_CTX_end(ctx);
if (new_ctx != NULL)
BN_CTX_free(new_ctx); BN_CTX_free(new_ctx);
if (prod_Z != NULL) { if (prod_Z != NULL) {
for (i = 0; i < num; i++) { for (i = 0; i < num; i++) {

View File

@ -205,7 +205,6 @@ static int ecdh_compute_key(void *out, size_t outlen, const EC_POINT *pub_key,
EC_POINT_free(tmp); EC_POINT_free(tmp);
if (ctx) if (ctx)
BN_CTX_end(ctx); BN_CTX_end(ctx);
if (ctx)
BN_CTX_free(ctx); BN_CTX_free(ctx);
if (buf) if (buf)
OPENSSL_free(buf); OPENSSL_free(buf);

View File

@ -223,9 +223,7 @@ static int ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in,
} }
/* clear old values if necessary */ /* clear old values if necessary */
if (*rp != NULL)
BN_clear_free(*rp); BN_clear_free(*rp);
if (*kinvp != NULL)
BN_clear_free(*kinvp); BN_clear_free(*kinvp);
/* save the pre-computed values */ /* save the pre-computed values */
*rp = r; *rp = r;
@ -233,17 +231,13 @@ static int ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in,
ret = 1; ret = 1;
err: err:
if (!ret) { if (!ret) {
if (k != NULL)
BN_clear_free(k); BN_clear_free(k);
if (r != NULL)
BN_clear_free(r); BN_clear_free(r);
} }
if (ctx_in == NULL) if (ctx != ctx_in)
BN_CTX_free(ctx); BN_CTX_free(ctx);
if (order != NULL)
BN_free(order); BN_free(order);
EC_POINT_free(tmp_point); EC_POINT_free(tmp_point);
if (X)
BN_clear_free(X); BN_clear_free(X);
return (ret); return (ret);
} }
@ -351,15 +345,10 @@ static ECDSA_SIG *ecdsa_do_sign(const unsigned char *dgst, int dgst_len,
ECDSA_SIG_free(ret); ECDSA_SIG_free(ret);
ret = NULL; ret = NULL;
} }
if (ctx)
BN_CTX_free(ctx); BN_CTX_free(ctx);
if (m)
BN_clear_free(m); BN_clear_free(m);
if (tmp)
BN_clear_free(tmp); BN_clear_free(tmp);
if (order)
BN_free(order); BN_free(order);
if (kinv)
BN_clear_free(kinv); BN_clear_free(kinv);
return ret; return ret;
} }

View File

@ -589,12 +589,10 @@ int OBJ_obj2txt(char *buf, int buf_len, const ASN1_OBJECT *a, int no_name)
} }
} }
if (bl)
BN_free(bl); BN_free(bl);
return n; return n;
err: err:
if (bl)
BN_free(bl); BN_free(bl);
return -1; return -1;
} }

View File

@ -337,7 +337,6 @@ static EVP_PKEY *b2i_dss(const unsigned char **in, unsigned int length,
PEMerr(PEM_F_B2I_DSS, ERR_R_MALLOC_FAILURE); PEMerr(PEM_F_B2I_DSS, ERR_R_MALLOC_FAILURE);
DSA_free(dsa); DSA_free(dsa);
EVP_PKEY_free(ret); EVP_PKEY_free(ret);
if (ctx)
BN_CTX_free(ctx); BN_CTX_free(ctx);
return NULL; return NULL;
} }

View File

@ -203,17 +203,11 @@ int RSA_check_key_ex(const RSA *key, BN_GENCB *cb)
} }
err: err:
if (i != NULL)
BN_free(i); BN_free(i);
if (j != NULL)
BN_free(j); BN_free(j);
if (k != NULL)
BN_free(k); BN_free(k);
if (l != NULL)
BN_free(l); BN_free(l);
if (m != NULL)
BN_free(m); BN_free(m);
if (ctx != NULL)
BN_CTX_free(ctx); BN_CTX_free(ctx);
return (ret); return (ret);
} }

View File

@ -100,10 +100,8 @@ int RSA_flags(const RSA *r)
void RSA_blinding_off(RSA *rsa) void RSA_blinding_off(RSA *rsa)
{ {
if (rsa->blinding != NULL) {
BN_BLINDING_free(rsa->blinding); BN_BLINDING_free(rsa->blinding);
rsa->blinding = NULL; rsa->blinding = NULL;
}
rsa->flags &= ~RSA_FLAG_BLINDING; rsa->flags &= ~RSA_FLAG_BLINDING;
rsa->flags |= RSA_FLAG_NO_BLINDING; rsa->flags |= RSA_FLAG_NO_BLINDING;
} }
@ -213,11 +211,10 @@ BN_BLINDING *RSA_setup_blinding(RSA *rsa, BN_CTX *in_ctx)
CRYPTO_THREADID_current(BN_BLINDING_thread_id(ret)); CRYPTO_THREADID_current(BN_BLINDING_thread_id(ret));
err: err:
BN_CTX_end(ctx); BN_CTX_end(ctx);
if (in_ctx == NULL) if (ctx != in_ctx)
BN_CTX_free(ctx); BN_CTX_free(ctx);
if (rsa->e == NULL) if (e != rsa->e)
BN_free(e); BN_free(e);
if (local_n)
BN_free(local_n); BN_free(local_n);
return ret; return ret;

View File

@ -99,10 +99,8 @@ RSA *RSA_generate_key(int bits, unsigned long e_value,
return rsa; return rsa;
} }
err: err:
if (e)
BN_free(e); BN_free(e);
RSA_free(rsa); RSA_free(rsa);
if (cb)
BN_GENCB_free(cb); BN_GENCB_free(cb);
return 0; return 0;
} }

View File

@ -239,10 +239,9 @@ static int RSA_eay_public_encrypt(int flen, const unsigned char *from,
r = num; r = num;
err: err:
if (ctx != NULL) { if (ctx != NULL)
BN_CTX_end(ctx); BN_CTX_end(ctx);
BN_CTX_free(ctx); BN_CTX_free(ctx);
}
OPENSSL_clear_free(buf, num); OPENSSL_clear_free(buf, num);
return (r); return (r);
} }
@ -434,18 +433,15 @@ static int RSA_eay_private_encrypt(int flen, const unsigned char *from,
if (rsa->flags & RSA_FLAG_CACHE_PUBLIC) if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
if (!BN_MONT_CTX_set_locked if (!BN_MONT_CTX_set_locked
(&rsa->_method_mod_n, CRYPTO_LOCK_RSA, rsa->n, ctx)) { (&rsa->_method_mod_n, CRYPTO_LOCK_RSA, rsa->n, ctx)) {
if (local_d)
BN_free(local_d); BN_free(local_d);
goto err; goto err;
} }
if (!rsa->meth->bn_mod_exp(ret, f, d, rsa->n, ctx, if (!rsa->meth->bn_mod_exp(ret, f, d, rsa->n, ctx,
rsa->_method_mod_n)) { rsa->_method_mod_n)) {
if (local_d)
BN_free(local_d); BN_free(local_d);
goto err; goto err;
} }
if (local_d)
BN_free(local_d); BN_free(local_d);
} }
@ -473,10 +469,9 @@ static int RSA_eay_private_encrypt(int flen, const unsigned char *from,
r = num; r = num;
err: err:
if (ctx != NULL) { if (ctx != NULL)
BN_CTX_end(ctx); BN_CTX_end(ctx);
BN_CTX_free(ctx); BN_CTX_free(ctx);
}
OPENSSL_clear_free(buf, num); OPENSSL_clear_free(buf, num);
return (r); return (r);
} }
@ -570,17 +565,14 @@ static int RSA_eay_private_decrypt(int flen, const unsigned char *from,
if (rsa->flags & RSA_FLAG_CACHE_PUBLIC) if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
if (!BN_MONT_CTX_set_locked if (!BN_MONT_CTX_set_locked
(&rsa->_method_mod_n, CRYPTO_LOCK_RSA, rsa->n, ctx)) { (&rsa->_method_mod_n, CRYPTO_LOCK_RSA, rsa->n, ctx)) {
if (local_d)
BN_free(local_d); BN_free(local_d);
goto err; goto err;
} }
if (!rsa->meth->bn_mod_exp(ret, f, d, rsa->n, ctx, if (!rsa->meth->bn_mod_exp(ret, f, d, rsa->n, ctx,
rsa->_method_mod_n)) { rsa->_method_mod_n)) {
if (local_d)
BN_free(local_d); BN_free(local_d);
goto err; goto err;
} }
if (local_d)
BN_free(local_d); BN_free(local_d);
} }
@ -612,10 +604,9 @@ static int RSA_eay_private_decrypt(int flen, const unsigned char *from,
RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT, RSA_R_PADDING_CHECK_FAILED); RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT, RSA_R_PADDING_CHECK_FAILED);
err: err:
if (ctx != NULL) { if (ctx != NULL)
BN_CTX_end(ctx); BN_CTX_end(ctx);
BN_CTX_free(ctx); BN_CTX_free(ctx);
}
OPENSSL_clear_free(buf, num); OPENSSL_clear_free(buf, num);
return (r); return (r);
} }
@ -712,10 +703,9 @@ static int RSA_eay_public_decrypt(int flen, const unsigned char *from,
RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT, RSA_R_PADDING_CHECK_FAILED); RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT, RSA_R_PADDING_CHECK_FAILED);
err: err:
if (ctx != NULL) { if (ctx != NULL)
BN_CTX_end(ctx); BN_CTX_end(ctx);
BN_CTX_free(ctx); BN_CTX_free(ctx);
}
OPENSSL_clear_free(buf, num); OPENSSL_clear_free(buf, num);
return (r); return (r);
} }
@ -769,16 +759,12 @@ static int RSA_eay_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
(&rsa->_method_mod_p, CRYPTO_LOCK_RSA, p, ctx) (&rsa->_method_mod_p, CRYPTO_LOCK_RSA, p, ctx)
|| !BN_MONT_CTX_set_locked(&rsa->_method_mod_q, || !BN_MONT_CTX_set_locked(&rsa->_method_mod_q,
CRYPTO_LOCK_RSA, q, ctx)) { CRYPTO_LOCK_RSA, q, ctx)) {
if (local_p)
BN_free(local_p); BN_free(local_p);
if (local_q)
BN_free(local_q); BN_free(local_q);
goto err; goto err;
} }
} }
if (local_p)
BN_free(local_p); BN_free(local_p);
if (local_q)
BN_free(local_q); BN_free(local_q);
} }
@ -900,24 +886,18 @@ static int RSA_eay_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
d = rsa->d; d = rsa->d;
if (!rsa->meth->bn_mod_exp(r0, I, d, rsa->n, ctx, if (!rsa->meth->bn_mod_exp(r0, I, d, rsa->n, ctx,
rsa->_method_mod_n)) { rsa->_method_mod_n)) {
if (local_d)
BN_free(local_d); BN_free(local_d);
goto err; goto err;
} }
if (local_d)
BN_free(local_d); BN_free(local_d);
} }
} }
ret = 1; ret = 1;
err: err:
if (local_dmp1)
BN_free(local_dmp1); BN_free(local_dmp1);
if (local_dmq1)
BN_free(local_dmq1); BN_free(local_dmq1);
if (local_c)
BN_free(local_c); BN_free(local_c);
if (local_r1)
BN_free(local_r1); BN_free(local_r1);
BN_CTX_end(ctx); BN_CTX_end(ctx);
return (ret); return (ret);
@ -931,11 +911,8 @@ static int RSA_eay_init(RSA *rsa)
static int RSA_eay_finish(RSA *rsa) static int RSA_eay_finish(RSA *rsa)
{ {
if (rsa->_method_mod_n != NULL)
BN_MONT_CTX_free(rsa->_method_mod_n); BN_MONT_CTX_free(rsa->_method_mod_n);
if (rsa->_method_mod_p != NULL)
BN_MONT_CTX_free(rsa->_method_mod_p); BN_MONT_CTX_free(rsa->_method_mod_p);
if (rsa->_method_mod_q != NULL)
BN_MONT_CTX_free(rsa->_method_mod_q); BN_MONT_CTX_free(rsa->_method_mod_q);
return (1); return (1);
} }

View File

@ -227,20 +227,16 @@ static int rsa_builtin_keygen(RSA *rsa, int bits, BIGNUM *e_value,
ok = 1; ok = 1;
err: err:
if (local_r0)
BN_free(local_r0); BN_free(local_r0);
if (local_d)
BN_free(local_d); BN_free(local_d);
if (local_p)
BN_free(local_p); BN_free(local_p);
if (ok == -1) { if (ok == -1) {
RSAerr(RSA_F_RSA_BUILTIN_KEYGEN, ERR_LIB_BN); RSAerr(RSA_F_RSA_BUILTIN_KEYGEN, ERR_LIB_BN);
ok = 0; ok = 0;
} }
if (ctx != NULL) { if (ctx != NULL)
BN_CTX_end(ctx); BN_CTX_end(ctx);
BN_CTX_free(ctx); BN_CTX_free(ctx);
}
return ok; return ok;
} }

View File

@ -223,25 +223,15 @@ void RSA_free(RSA *r)
CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, r, &r->ex_data); CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, r, &r->ex_data);
if (r->n != NULL)
BN_clear_free(r->n); BN_clear_free(r->n);
if (r->e != NULL)
BN_clear_free(r->e); BN_clear_free(r->e);
if (r->d != NULL)
BN_clear_free(r->d); BN_clear_free(r->d);
if (r->p != NULL)
BN_clear_free(r->p); BN_clear_free(r->p);
if (r->q != NULL)
BN_clear_free(r->q); BN_clear_free(r->q);
if (r->dmp1 != NULL)
BN_clear_free(r->dmp1); BN_clear_free(r->dmp1);
if (r->dmq1 != NULL)
BN_clear_free(r->dmq1); BN_clear_free(r->dmq1);
if (r->iqmp != NULL)
BN_clear_free(r->iqmp); BN_clear_free(r->iqmp);
if (r->blinding != NULL)
BN_BLINDING_free(r->blinding); BN_BLINDING_free(r->blinding);
if (r->mt_blinding != NULL)
BN_BLINDING_free(r->mt_blinding); BN_BLINDING_free(r->mt_blinding);
OPENSSL_free_locked(r->bignum_data); OPENSSL_free_locked(r->bignum_data);
OPENSSL_free(r); OPENSSL_free(r);

View File

@ -160,7 +160,6 @@ static void pkey_rsa_cleanup(EVP_PKEY_CTX *ctx)
{ {
RSA_PKEY_CTX *rctx = ctx->data; RSA_PKEY_CTX *rctx = ctx->data;
if (rctx) { if (rctx) {
if (rctx->pub_exp)
BN_free(rctx->pub_exp); BN_free(rctx->pub_exp);
if (rctx->tbuf) if (rctx->tbuf)
OPENSSL_free(rctx->tbuf); OPENSSL_free(rctx->tbuf);

View File

@ -179,11 +179,9 @@ int RSA_X931_derive_ex(RSA *rsa, BIGNUM *p1, BIGNUM *p2, BIGNUM *q1,
ret = 1; ret = 1;
err: err:
if (ctx) { if (ctx)
BN_CTX_end(ctx); BN_CTX_end(ctx);
BN_CTX_free(ctx); BN_CTX_free(ctx);
}
if (ctx2)
BN_CTX_free(ctx2); BN_CTX_free(ctx2);
return ret; return ret;
@ -234,10 +232,9 @@ int RSA_X931_generate_key_ex(RSA *rsa, int bits, const BIGNUM *e,
ok = 1; ok = 1;
error: error:
if (ctx) { if (ctx)
BN_CTX_end(ctx); BN_CTX_end(ctx);
BN_CTX_free(ctx); BN_CTX_free(ctx);
}
if (ok) if (ok)
return 1; return 1;

View File

@ -228,8 +228,7 @@ BIGNUM *SRP_Calc_A(BIGNUM *a, BIGNUM *N, BIGNUM *g)
BN_CTX *bn_ctx; BN_CTX *bn_ctx;
BIGNUM *A = NULL; BIGNUM *A = NULL;
if (a == NULL || N == NULL || g == NULL || if (a == NULL || N == NULL || g == NULL || (bn_ctx = BN_CTX_new()) == NULL)
(bn_ctx = BN_CTX_new()) == NULL)
return NULL; return NULL;
if ((A = BN_new()) != NULL && !BN_mod_exp(A, g, a, N, bn_ctx)) { if ((A = BN_new()) != NULL && !BN_mod_exp(A, g, a, N, bn_ctx)) {
@ -252,7 +251,8 @@ BIGNUM *SRP_Calc_client_key(BIGNUM *N, BIGNUM *B, BIGNUM *g, BIGNUM *x,
if ((tmp = BN_new()) == NULL || if ((tmp = BN_new()) == NULL ||
(tmp2 = BN_new()) == NULL || (tmp2 = BN_new()) == NULL ||
(tmp3 = BN_new()) == NULL || (K = BN_new()) == NULL) (tmp3 = BN_new()) == NULL ||
(K = BN_new()) == NULL)
goto err; goto err;
if (!BN_mod_exp(tmp, g, x, N, bn_ctx)) if (!BN_mod_exp(tmp, g, x, N, bn_ctx))

View File

@ -946,9 +946,7 @@ static DSA_SIG *DSA_zencod_do_sign(const unsigned char *dgst, int dlen,
return sig; return sig;
FAILED: FAILED:
if (bn_r)
BN_free(bn_r); BN_free(bn_r);
if (bn_s)
BN_free(bn_s); BN_free(bn_s);
return NULL; return NULL;
} }
@ -1083,9 +1081,9 @@ static int DH_zencod_generate_key(DH *dh)
return 1; return 1;
FAILED: FAILED:
if (!dh->priv_key && bn_prv) if (!dh->priv_key)
BN_free(bn_prv); BN_free(bn_prv);
if (!dh->pub_key && bn_pub) if (!dh->pub_key)
BN_free(bn_pub); BN_free(bn_pub);
return 0; return 0;

View File

@ -41,6 +41,7 @@ BN_BLINDING_new() allocates a new B<BN_BLINDING> structure and copies
the B<A> and B<Ai> values into the newly created B<BN_BLINDING> object. the B<A> and B<Ai> values into the newly created B<BN_BLINDING> object.
BN_BLINDING_free() frees the B<BN_BLINDING> structure. BN_BLINDING_free() frees the B<BN_BLINDING> structure.
If B<b> is NULL, nothing is done.
BN_BLINDING_update() updates the B<BN_BLINDING> parameters by squaring BN_BLINDING_update() updates the B<BN_BLINDING> parameters by squaring
the B<A> and B<Ai> or, after specific number of uses and if the the B<A> and B<Ai> or, after specific number of uses and if the

View File

@ -27,6 +27,7 @@ created by BN_CTX_new(), also the structure itself.
If L<BN_CTX_start(3)|BN_CTX_start(3)> has been used on the B<BN_CTX>, If L<BN_CTX_start(3)|BN_CTX_start(3)> has been used on the B<BN_CTX>,
L<BN_CTX_end(3)|BN_CTX_end(3)> must be called before the B<BN_CTX> L<BN_CTX_end(3)|BN_CTX_end(3)> must be called before the B<BN_CTX>
may be freed by BN_CTX_free(). may be freed by BN_CTX_free().
If B<c> is NULL, nothing is done.
=head1 RETURN VALUES =head1 RETURN VALUES

View File

@ -111,8 +111,8 @@ B<BN_GENCB> structure that are supported: "new" style and "old" style. New
programs should prefer the "new" style, whilst the "old" style is provided programs should prefer the "new" style, whilst the "old" style is provided
for backwards compatibility purposes. for backwards compatibility purposes.
A BN_GENCB structure should be created through a call to BN_GENCB_new, and freed A BN_GENCB structure should be created through a call to BN_GENCB_new(),
through a call to BN_GENCB_free. and freed through a call to BN_GENCB_free().
For "new" style callbacks a BN_GENCB structure should be initialised with a For "new" style callbacks a BN_GENCB structure should be initialised with a
call to BN_GENCB_set(), where B<gencb> is a B<BN_GENCB *>, B<callback> is of call to BN_GENCB_set(), where B<gencb> is a B<BN_GENCB *>, B<callback> is of

View File

@ -41,6 +41,7 @@ BN_MONT_CTX_copy() copies the B<BN_MONT_CTX> I<from> to I<to>.
BN_MONT_CTX_free() frees the components of the B<BN_MONT_CTX>, and, if BN_MONT_CTX_free() frees the components of the B<BN_MONT_CTX>, and, if
it was created by BN_MONT_CTX_new(), also the structure itself. it was created by BN_MONT_CTX_new(), also the structure itself.
If B<mont> is NULL, nothing is done.
BN_mod_mul_montgomery() computes Mont(I<a>,I<b>):=I<a>*I<b>*R^-1 and places BN_mod_mul_montgomery() computes Mont(I<a>,I<b>):=I<a>*I<b>*R^-1 and places
the result in I<r>. the result in I<r>.

View File

@ -34,6 +34,7 @@ BN_RECP_CTX_init() initializes an existing uninitialized B<BN_RECP>.
BN_RECP_CTX_free() frees the components of the B<BN_RECP>, and, if it BN_RECP_CTX_free() frees the components of the B<BN_RECP>, and, if it
was created by BN_RECP_CTX_new(), also the structure itself. was created by BN_RECP_CTX_new(), also the structure itself.
If B<recp> is NULL, nothing is done.
BN_RECP_CTX_set() stores B<m> in B<recp> and sets it up for computing BN_RECP_CTX_set() stores B<m> in B<recp> and sets it up for computing
1/B<m> and shifting it left by BN_num_bits(B<m>)+1 to make it an 1/B<m> and shifting it left by BN_num_bits(B<m>)+1 to make it an

View File

@ -27,6 +27,7 @@ to the value 0.
BN_free() frees the components of the B<BIGNUM>, and if it was created BN_free() frees the components of the B<BIGNUM>, and if it was created
by BN_new(), also the structure itself. BN_clear_free() additionally by BN_new(), also the structure itself. BN_clear_free() additionally
overwrites the data before the memory is returned to the system. overwrites the data before the memory is returned to the system.
If B<a> is NULL, nothing is done.
=head1 RETURN VALUES =head1 RETURN VALUES

View File

@ -111,10 +111,9 @@ int fill_GOST2001_params(EC_KEY *eckey, int nid)
err: err:
if (P) EC_POINT_free(P); if (P) EC_POINT_free(P);
if (grp) EC_GROUP_free(grp); if (grp) EC_GROUP_free(grp);
if (ctx) { if (ctx)
BN_CTX_end(ctx); BN_CTX_end(ctx);
BN_CTX_free(ctx); BN_CTX_free(ctx);
}
return ok; return ok;
} }
@ -241,15 +240,13 @@ DSA_SIG *gost2001_do_sign(const unsigned char *dgst, int dlen, EC_KEY *eckey)
ret = newsig; ret = newsig;
err: err:
if (ctx) { if (ctx)
BN_CTX_end(ctx); BN_CTX_end(ctx);
BN_CTX_free(ctx); BN_CTX_free(ctx);
}
if (C) EC_POINT_free(C); if (C) EC_POINT_free(C);
if (md) BN_free(md); BN_free(md);
if (!ret && newsig) { if (!ret)
DSA_SIG_free(newsig); DSA_SIG_free(newsig);
}
return ret; return ret;
} }
@ -365,11 +362,10 @@ int gost2001_do_verify(const unsigned char *dgst, int dgst_len,
} }
err: err:
if (C) EC_POINT_free(C); if (C) EC_POINT_free(C);
if (ctx) { if (ctx)
BN_CTX_end(ctx); BN_CTX_end(ctx);
BN_CTX_free(ctx); BN_CTX_free(ctx);
} BN_free(md);
if (md) BN_free(md);
return ok; return ok;
} }
@ -418,10 +414,9 @@ int gost2001_compute_public(EC_KEY *ec)
ok = 256; ok = 256;
err: err:
if (pub_key) EC_POINT_free(pub_key); if (pub_key) EC_POINT_free(pub_key);
if (ctx) { if (ctx)
BN_CTX_end(ctx); BN_CTX_end(ctx);
BN_CTX_free(ctx); BN_CTX_free(ctx);
}
return ok; return ok;
} }

View File

@ -525,10 +525,12 @@ static int param_copy_gost94(EVP_PKEY *to, const EVP_PKEY *from)
dto = DSA_new(); dto = DSA_new();
EVP_PKEY_assign(to, EVP_PKEY_base_id(from), dto); EVP_PKEY_assign(to, EVP_PKEY_base_id(from), dto);
} }
#define COPYBIGNUM(a,b,x) if (a->x) BN_free(a->x); a->x=BN_dup(b->x); BN_free(dto->p);
COPYBIGNUM(dto, dfrom, p) dto->p = BN_dup(dfrom->p);
COPYBIGNUM(dto, dfrom, q) BN_free(dto->q);
COPYBIGNUM(dto, dfrom, g) dto->q = BN_dup(dfrom->q);
BN_free(dto->g);
dto->g = BN_dup(dfrom->g);
if (dto->priv_key) if (dto->priv_key)
gost94_compute_public(dto); gost94_compute_public(dto);
@ -758,9 +760,7 @@ static int pub_encode_gost01(X509_PUBKEY *pub, const EVP_PKEY *pk)
Y = BN_new(); Y = BN_new();
if (!X || !Y) { if (!X || !Y) {
GOSTerr(GOST_F_PUB_ENCODE_GOST01, ERR_R_MALLOC_FAILURE); GOSTerr(GOST_F_PUB_ENCODE_GOST01, ERR_R_MALLOC_FAILURE);
if (X)
BN_free(X); BN_free(X);
if (Y)
BN_free(Y); BN_free(Y);
BN_free(order); BN_free(order);
return 0; return 0;

View File

@ -114,13 +114,11 @@ DSA_SIG *gost_do_sign(const unsigned char *dgst, int dlen, DSA *dsa)
ret = newsig; ret = newsig;
err: err:
BN_free(md); BN_free(md);
if (ctx) { if (ctx)
BN_CTX_end(ctx); BN_CTX_end(ctx);
BN_CTX_free(ctx); BN_CTX_free(ctx);
} if (!ret)
if (!ret && newsig) {
DSA_SIG_free(newsig); DSA_SIG_free(newsig);
}
return ret; return ret;
} }
@ -214,13 +212,11 @@ int gost_do_verify(const unsigned char *dgst, int dgst_len,
GOSTerr(GOST_F_GOST_DO_VERIFY, GOST_R_SIGNATURE_MISMATCH); GOSTerr(GOST_F_GOST_DO_VERIFY, GOST_R_SIGNATURE_MISMATCH);
} }
err: err:
if (md)
BN_free(md); BN_free(md);
if (ctx) { if (ctx)
BN_CTX_end(ctx); BN_CTX_end(ctx);
BN_CTX_free(ctx); BN_CTX_free(ctx);
} return (ok == 0);
return ok;
} }
/* /*
@ -268,19 +264,13 @@ int fill_GOST94_params(DSA *dsa, int nid)
return 0; return 0;
} }
#define dump_signature(a,b,c) #define dump_signature(a,b,c)
if (dsa->p) {
BN_free(dsa->p); BN_free(dsa->p);
}
dsa->p = NULL; dsa->p = NULL;
BN_dec2bn(&(dsa->p), params->p); BN_dec2bn(&(dsa->p), params->p);
if (dsa->q) {
BN_free(dsa->q); BN_free(dsa->q);
}
dsa->q = NULL; dsa->q = NULL;
BN_dec2bn(&(dsa->q), params->q); BN_dec2bn(&(dsa->q), params->q);
if (dsa->g) {
BN_free(dsa->g); BN_free(dsa->g);
}
dsa->g = NULL; dsa->g = NULL;
BN_dec2bn(&(dsa->g), params->a); BN_dec2bn(&(dsa->g), params->a);
return 1; return 1;

View File

@ -717,9 +717,7 @@ static int cswift_mod_exp_crt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
OPENSSL_free(sw_param.up.crt.dmq1.value); OPENSSL_free(sw_param.up.crt.dmq1.value);
if (sw_param.up.crt.iqmp.value) if (sw_param.up.crt.iqmp.value)
OPENSSL_free(sw_param.up.crt.iqmp.value); OPENSSL_free(sw_param.up.crt.iqmp.value);
if (result)
BN_free(result); BN_free(result);
if (argument)
BN_free(argument); BN_free(argument);
if (acquired) if (acquired)
release_context(hac); release_context(hac);
@ -877,10 +875,9 @@ static DSA_SIG *cswift_dsa_sign(const unsigned char *dgst, int dlen, DSA *dsa)
err: err:
if (acquired) if (acquired)
release_context(hac); release_context(hac);
if (ctx) { if (ctx)
BN_CTX_end(ctx); BN_CTX_end(ctx);
BN_CTX_free(ctx); BN_CTX_free(ctx);
}
return to_return; return to_return;
} }
@ -980,10 +977,9 @@ static int cswift_dsa_verify(const unsigned char *dgst, int dgst_len,
err: err:
if (acquired) if (acquired)
release_context(hac); release_context(hac);
if (ctx) { if (ctx)
BN_CTX_end(ctx); BN_CTX_end(ctx);
BN_CTX_free(ctx); BN_CTX_free(ctx);
}
return to_return; return to_return;
} }
# endif # endif

View File

@ -680,40 +680,6 @@ static int ubsec_mod_exp_crt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
} }
# endif # endif
# ifndef OPENSSL_NO_DSA
# ifdef NOT_USED
static int ubsec_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1,
BIGNUM *p1, BIGNUM *a2, BIGNUM *p2, BIGNUM *m,
BN_CTX *ctx, BN_MONT_CTX *in_mont)
{
BIGNUM t;
int to_return = 0;
BN_init(&t);
/* let rr = a1 ^ p1 mod m */
if (!ubsec_mod_exp(rr, a1, p1, m, ctx))
goto end;
/* let t = a2 ^ p2 mod m */
if (!ubsec_mod_exp(&t, a2, p2, m, ctx))
goto end;
/* let rr = rr * t mod m */
if (!BN_mod_mul(rr, rr, &t, m, ctx))
goto end;
to_return = 1;
end:
BN_free(&t);
return to_return;
}
static int ubsec_mod_exp_dsa(DSA *dsa, BIGNUM *r, BIGNUM *a,
const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
BN_MONT_CTX *m_ctx)
{
return ubsec_mod_exp(r, a, p, m, ctx);
}
# endif
# endif
# ifndef OPENSSL_NO_RSA # ifndef OPENSSL_NO_RSA
/* /*
@ -825,9 +791,7 @@ static DSA_SIG *ubsec_dsa_do_sign(const unsigned char *dgst, int dlen,
err: err:
if (!to_return) { if (!to_return) {
if (r)
BN_free(r); BN_free(r);
if (s)
BN_free(s); BN_free(s);
} }
BN_clear_free(&m); BN_clear_free(&m);

View File

@ -273,14 +273,10 @@ int SSL_set_srp_server_param_pw(SSL *s, const char *user, const char *pass,
return -1; return -1;
s->srp_ctx.N = BN_dup(GN->N); s->srp_ctx.N = BN_dup(GN->N);
s->srp_ctx.g = BN_dup(GN->g); s->srp_ctx.g = BN_dup(GN->g);
if (s->srp_ctx.v != NULL) {
BN_clear_free(s->srp_ctx.v); BN_clear_free(s->srp_ctx.v);
s->srp_ctx.v = NULL; s->srp_ctx.v = NULL;
}
if (s->srp_ctx.s != NULL) {
BN_clear_free(s->srp_ctx.s); BN_clear_free(s->srp_ctx.s);
s->srp_ctx.s = NULL; s->srp_ctx.s = NULL;
}
if (!SRP_create_verifier_BN if (!SRP_create_verifier_BN
(user, pass, &s->srp_ctx.s, &s->srp_ctx.v, GN->N, GN->g)) (user, pass, &s->srp_ctx.s, &s->srp_ctx.v, GN->N, GN->g))
return -1; return -1;

View File

@ -761,13 +761,9 @@ int test_sqr(BIO *bp, BN_CTX *ctx)
} }
ret = 1; ret = 1;
err: err:
if (a != NULL)
BN_free(a); BN_free(a);
if (c != NULL)
BN_free(c); BN_free(c);
if (d != NULL)
BN_free(d); BN_free(d);
if (e != NULL)
BN_free(e); BN_free(e);
return ret; return ret;
} }
@ -1669,13 +1665,9 @@ int test_kron(BIO *bp, BN_CTX *ctx)
fflush(stderr); fflush(stderr);
ret = 1; ret = 1;
err: err:
if (a != NULL)
BN_free(a); BN_free(a);
if (b != NULL)
BN_free(b); BN_free(b);
if (r != NULL)
BN_free(r); BN_free(r);
if (t != NULL)
BN_free(t); BN_free(t);
return ret; return ret;
} }
@ -1764,11 +1756,8 @@ int test_sqrt(BIO *bp, BN_CTX *ctx)
} }
ret = 1; ret = 1;
err: err:
if (a != NULL)
BN_free(a); BN_free(a);
if (p != NULL)
BN_free(p); BN_free(p);
if (r != NULL)
BN_free(r); BN_free(r);
return ret; return ret;
} }

View File

@ -201,7 +201,6 @@ int main(int argc, char *argv[])
OPENSSL_free(bbuf); OPENSSL_free(bbuf);
DH_free(b); DH_free(b);
DH_free(a); DH_free(a);
if (_cb)
BN_GENCB_free(_cb); BN_GENCB_free(_cb);
BIO_free(out); BIO_free(out);
# ifdef OPENSSL_SYS_NETWARE # ifdef OPENSSL_SYS_NETWARE

View File

@ -212,7 +212,6 @@ int main(int argc, char **argv)
if (!ret) if (!ret)
ERR_print_errors(bio_err); ERR_print_errors(bio_err);
DSA_free(dsa); DSA_free(dsa);
if (cb != NULL)
BN_GENCB_free(cb); BN_GENCB_free(cb);
CRYPTO_cleanup_all_ex_data(); CRYPTO_cleanup_all_ex_data();
ERR_remove_thread_state(NULL); ERR_remove_thread_state(NULL);

View File

@ -282,13 +282,9 @@ static int test_ecdh_curve(int nid, const char *text, BN_CTX *ctx, BIO *out)
OPENSSL_free(abuf); OPENSSL_free(abuf);
if (bbuf != NULL) if (bbuf != NULL)
OPENSSL_free(bbuf); OPENSSL_free(bbuf);
if (x_a)
BN_free(x_a); BN_free(x_a);
if (y_a)
BN_free(y_a); BN_free(y_a);
if (x_b)
BN_free(x_b); BN_free(x_b);
if (y_b)
BN_free(y_b); BN_free(y_b);
EC_KEY_free(b); EC_KEY_free(b);
EC_KEY_free(a); EC_KEY_free(a);
@ -390,7 +386,6 @@ static EC_KEY *mk_eckey(int nid, const unsigned char *p, size_t plen)
goto err; goto err;
ok = 1; ok = 1;
err: err:
if (priv)
BN_clear_free(priv); BN_clear_free(priv);
EC_POINT_free(pub); EC_POINT_free(pub);
if (ok) if (ok)
@ -526,7 +521,6 @@ int main(int argc, char *argv[])
err: err:
ERR_print_errors_fp(stderr); ERR_print_errors_fp(stderr);
if (ctx)
BN_CTX_free(ctx); BN_CTX_free(ctx);
BIO_free(out); BIO_free(out);
CRYPTO_cleanup_all_ex_data(); CRYPTO_cleanup_all_ex_data();

View File

@ -243,14 +243,10 @@ int x9_62_test_internal(BIO *out, int nid, const char *r_in, const char *s_in)
EC_KEY_free(key); EC_KEY_free(key);
if (signature) if (signature)
ECDSA_SIG_free(signature); ECDSA_SIG_free(signature);
if (r)
BN_free(r); BN_free(r);
if (s)
BN_free(s); BN_free(s);
EVP_MD_CTX_cleanup(&md_ctx); EVP_MD_CTX_cleanup(&md_ctx);
if (kinv)
BN_clear_free(kinv); BN_clear_free(kinv);
if (rp)
BN_clear_free(rp); BN_clear_free(rp);
return ret; return ret;
} }

View File

@ -851,7 +851,6 @@ static void prime_field_tests(void)
BN_free(scalar3); BN_free(scalar3);
} }
if (ctx)
BN_CTX_free(ctx); BN_CTX_free(ctx);
BN_free(p); BN_free(p);
BN_free(a); BN_free(a);
@ -1326,7 +1325,6 @@ static void char2_field_tests(void)
fprintf(stdout, " ok\n\n"); fprintf(stdout, " ok\n\n");
} }
if (ctx)
BN_CTX_free(ctx); BN_CTX_free(ctx);
BN_free(p); BN_free(p);
BN_free(a); BN_free(a);

View File

@ -2939,7 +2939,6 @@ static RSA *tmp_rsa_cb(SSL *s, int is_export, int keylength)
end: end:
printf("\n"); printf("\n");
} }
if (bn)
BN_free(bn); BN_free(bn);
return (rsa_tmp); return (rsa_tmp);
} }
@ -3106,7 +3105,6 @@ static int psk_key2bn(const char *pskkey, unsigned char *psk,
if (!ret) { if (!ret) {
BIO_printf(bio_err, "Could not convert PSK key '%s' to BIGNUM\n", BIO_printf(bio_err, "Could not convert PSK key '%s' to BIGNUM\n",
pskkey); pskkey);
if (bn)
BN_free(bn); BN_free(bn);
return 0; return 0;
} }