free NULL cleanup

Start ensuring all OpenSSL "free" routines allow NULL, and remove
any if check before calling them.
This gets DH_free, DSA_free, RSA_free

Reviewed-by: Matt Caswell <matt@openssl.org>
This commit is contained in:
Rich Salz
2015-03-24 10:17:37 -04:00
parent 1300705a47
commit d64070838e
34 changed files with 58 additions and 108 deletions

View File

@@ -314,8 +314,7 @@ int MAIN(int argc, char **argv)
BIO_free(in); BIO_free(in);
if (out != NULL) if (out != NULL)
BIO_free_all(out); BIO_free_all(out);
if (dh != NULL) DH_free(dh);
DH_free(dh);
apps_shutdown(); apps_shutdown();
OPENSSL_EXIT(ret); OPENSSL_EXIT(ret);
} }

View File

@@ -319,8 +319,7 @@ int MAIN(int argc, char **argv)
if (!dsa if (!dsa
|| !DSA_generate_parameters_ex(dsa, num, NULL, 0, NULL, NULL, || !DSA_generate_parameters_ex(dsa, num, NULL, 0, NULL, NULL,
cb)) { cb)) {
if (dsa) DSA_free(dsa);
DSA_free(dsa);
BN_GENCB_free(cb); BN_GENCB_free(cb);
ERR_print_errors(bio_err); ERR_print_errors(bio_err);
goto end; goto end;
@@ -520,8 +519,7 @@ int MAIN(int argc, char **argv)
BIO_free(in); BIO_free(in);
if (out != NULL) if (out != NULL)
BIO_free_all(out); BIO_free_all(out);
if (dh != NULL) DH_free(dh);
DH_free(dh);
apps_shutdown(); apps_shutdown();
OPENSSL_EXIT(ret); OPENSSL_EXIT(ret);
} }

View File

@@ -360,8 +360,7 @@ int MAIN(int argc, char **argv)
BIO_free(in); BIO_free(in);
if (out != NULL) if (out != NULL)
BIO_free_all(out); BIO_free_all(out);
if (dsa != NULL) DSA_free(dsa);
DSA_free(dsa);
if (passin) if (passin)
OPENSSL_free(passin); OPENSSL_free(passin);
if (passout) if (passout)

View File

@@ -438,8 +438,7 @@ int MAIN(int argc, char **argv)
BIO_free(in); BIO_free(in);
if (out != NULL) if (out != NULL)
BIO_free_all(out); BIO_free_all(out);
if (dsa != NULL) DSA_free(dsa);
DSA_free(dsa);
apps_shutdown(); apps_shutdown();
OPENSSL_EXIT(ret); OPENSSL_EXIT(ret);
} }

View File

@@ -212,8 +212,7 @@ int MAIN(int argc, char **argv)
ERR_print_errors(bio_err); ERR_print_errors(bio_err);
if (out != NULL) if (out != NULL)
BIO_free_all(out); BIO_free_all(out);
if (dh != NULL) DH_free(dh);
DH_free(dh);
if (cb != NULL) if (cb != NULL)
BN_GENCB_free(cb); BN_GENCB_free(cb);
apps_shutdown(); apps_shutdown();

View File

@@ -271,8 +271,7 @@ int MAIN(int argc, char **argv)
BIO_free(in); BIO_free(in);
if (out != NULL) if (out != NULL)
BIO_free_all(out); BIO_free_all(out);
if (dsa != NULL) DSA_free(dsa);
DSA_free(dsa);
if (passout) if (passout)
OPENSSL_free(passout); OPENSSL_free(passout);
apps_shutdown(); apps_shutdown();

View File

@@ -311,8 +311,7 @@ int MAIN(int argc, char **argv)
BN_free(bn); BN_free(bn);
if (cb) if (cb)
BN_GENCB_free(cb); BN_GENCB_free(cb);
if (rsa) RSA_free(rsa);
RSA_free(rsa);
if (out) if (out)
BIO_free_all(out); BIO_free_all(out);
if (passout) if (passout)

View File

@@ -424,8 +424,7 @@ int MAIN(int argc, char **argv)
end: end:
if (out != NULL) if (out != NULL)
BIO_free_all(out); BIO_free_all(out);
if (rsa != NULL) RSA_free(rsa);
RSA_free(rsa);
if (passin) if (passin)
OPENSSL_free(passin); OPENSSL_free(passin);
if (passout) if (passout)

View File

@@ -3180,8 +3180,7 @@ static RSA *tmp_rsa_cb(SSL *s, int is_export, int keylength)
} }
if (!BN_set_word(bn, RSA_F4) || ((rsa_tmp = RSA_new()) == NULL) || if (!BN_set_word(bn, RSA_F4) || ((rsa_tmp = RSA_new()) == NULL) ||
!RSA_generate_key_ex(rsa_tmp, keylength, bn, NULL)) { !RSA_generate_key_ex(rsa_tmp, keylength, bn, NULL)) {
if (rsa_tmp) RSA_free(rsa_tmp);
RSA_free(rsa_tmp);
rsa_tmp = NULL; rsa_tmp = NULL;
} }
if (!s_quiet) { if (!s_quiet) {

View File

@@ -2448,13 +2448,11 @@ int MAIN(int argc, char **argv)
OPENSSL_free(buf2_malloc); OPENSSL_free(buf2_malloc);
#ifndef OPENSSL_NO_RSA #ifndef OPENSSL_NO_RSA
for (i = 0; i < RSA_NUM; i++) for (i = 0; i < RSA_NUM; i++)
if (rsa_key[i] != NULL) RSA_free(rsa_key[i]);
RSA_free(rsa_key[i]);
#endif #endif
#ifndef OPENSSL_NO_DSA #ifndef OPENSSL_NO_DSA
for (i = 0; i < DSA_NUM; i++) for (i = 0; i < DSA_NUM; i++)
if (dsa_key[i] != NULL) DSA_free(dsa_key[i]);
DSA_free(dsa_key[i]);
#endif #endif
#ifndef OPENSSL_NO_EC #ifndef OPENSSL_NO_EC

View File

@@ -142,8 +142,7 @@ static int dh_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey)
err: err:
if (public_key) if (public_key)
ASN1_INTEGER_free(public_key); ASN1_INTEGER_free(public_key);
if (dh) DH_free(dh);
DH_free(dh);
return 0; return 0;
} }
@@ -711,8 +710,7 @@ static int dh_cms_set_peerkey(EVP_PKEY_CTX *pctx,
ASN1_INTEGER_free(public_key); ASN1_INTEGER_free(public_key);
if (pkpeer) if (pkpeer)
EVP_PKEY_free(pkpeer); EVP_PKEY_free(pkpeer);
if (dhpeer) DH_free(dhpeer);
DH_free(dhpeer);
return rv; return rv;
} }

View File

@@ -142,8 +142,7 @@ DH *d2i_DHxparams(DH **a, const unsigned char **pp, long length)
} }
if (a) { if (a) {
if (*a) DH_free(*a);
DH_free(*a);
*a = dh; *a = dh;
} }

View File

@@ -170,6 +170,7 @@ DH *DH_new_method(ENGINE *engine)
void DH_free(DH *r) void DH_free(DH *r)
{ {
int i; int i;
if (r == NULL) if (r == NULL)
return; return;
i = CRYPTO_add(&r->references, -1, CRYPTO_LOCK_DH); i = CRYPTO_add(&r->references, -1, CRYPTO_LOCK_DH);

View File

@@ -199,10 +199,8 @@ int main(int argc, char *argv[])
OPENSSL_free(abuf); OPENSSL_free(abuf);
if (bbuf != NULL) if (bbuf != NULL)
OPENSSL_free(bbuf); OPENSSL_free(bbuf);
if (b != NULL) DH_free(b);
DH_free(b); DH_free(a);
if (a != NULL)
DH_free(a);
if (_cb) if (_cb)
BN_GENCB_free(_cb); BN_GENCB_free(_cb);
BIO_free(out); BIO_free(out);

View File

@@ -120,8 +120,7 @@ static int dsa_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey)
err: err:
if (public_key) if (public_key)
ASN1_INTEGER_free(public_key); ASN1_INTEGER_free(public_key);
if (dsa) DSA_free(dsa);
DSA_free(dsa);
return 0; return 0;
} }

View File

@@ -89,10 +89,8 @@ DSA *DSA_generate_parameters(int bits,
if ((ret = DSA_new()) == NULL) if ((ret = DSA_new()) == NULL)
return NULL; return NULL;
cb = BN_GENCB_new(); cb = BN_GENCB_new();
if (!cb) { if (!cb)
DSA_free(ret); goto err;
return NULL;
}
BN_GENCB_set_old(cb, callback, cb_arg); BN_GENCB_set_old(cb, callback, cb_arg);
@@ -102,6 +100,7 @@ DSA *DSA_generate_parameters(int bits,
return ret; return ret;
} }
BN_GENCB_free(cb); BN_GENCB_free(cb);
err:
DSA_free(ret); DSA_free(ret);
return NULL; return NULL;
} }

View File

@@ -315,8 +315,7 @@ DH *DSA_dup_DH(const DSA *r)
return ret; return ret;
err: err:
if (ret != NULL) DH_free(ret);
DH_free(ret);
return NULL; return NULL;
} }
#endif #endif

View File

@@ -211,8 +211,7 @@ int main(int argc, char **argv)
end: end:
if (!ret) if (!ret)
ERR_print_errors(bio_err); ERR_print_errors(bio_err);
if (dsa != NULL) DSA_free(dsa);
DSA_free(dsa);
if (cb != NULL) if (cb != NULL)
BN_GENCB_free(cb); BN_GENCB_free(cb);
CRYPTO_cleanup_all_ex_data(); CRYPTO_cleanup_all_ex_data();

View File

@@ -268,9 +268,7 @@ static EVP_PKEY *load_example_rsa_key(void)
if (pkey) { if (pkey) {
EVP_PKEY_free(pkey); EVP_PKEY_free(pkey);
} }
if (rsa) { RSA_free(rsa);
RSA_free(rsa);
}
return ret; return ret;
} }

View File

@@ -335,8 +335,7 @@ static EVP_PKEY *b2i_dss(const unsigned char **in, unsigned int length,
memerr: memerr:
PEMerr(PEM_F_B2I_DSS, ERR_R_MALLOC_FAILURE); PEMerr(PEM_F_B2I_DSS, ERR_R_MALLOC_FAILURE);
if (dsa) DSA_free(dsa);
DSA_free(dsa);
if (ret) if (ret)
EVP_PKEY_free(ret); EVP_PKEY_free(ret);
if (ctx) if (ctx)
@@ -385,8 +384,7 @@ static EVP_PKEY *b2i_rsa(const unsigned char **in, unsigned int length,
return ret; return ret;
memerr: memerr:
PEMerr(PEM_F_B2I_RSA, ERR_R_MALLOC_FAILURE); PEMerr(PEM_F_B2I_RSA, ERR_R_MALLOC_FAILURE);
if (rsa) RSA_free(rsa);
RSA_free(rsa);
if (ret) if (ret)
EVP_PKEY_free(ret); EVP_PKEY_free(ret);
return NULL; return NULL;

View File

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

View File

@@ -637,8 +637,7 @@ void tls_set_dhe1024(int i, void *apparg)
tls_openssl_errors("", "", NULL, apparg); tls_openssl_errors("", "", NULL, apparg);
return; return;
} }
if (tls_dhe1024 != NULL) DH_free(tls_dhe1024);
DH_free(tls_dhe1024);
tls_dhe1024 = dhparams; tls_dhe1024 = dhparams;
} }

View File

@@ -18,6 +18,7 @@ DH_new() allocates and initializes a B<DH> structure.
DH_free() frees the B<DH> structure and its components. The values are DH_free() frees the B<DH> structure and its components. The values are
erased before the memory is returned to the system. erased before the memory is returned to the system.
If B<dh> is NULL nothing is done.
=head1 RETURN VALUES =head1 RETURN VALUES

View File

@@ -19,6 +19,7 @@ calling DSA_new_method(NULL).
DSA_free() frees the B<DSA> structure and its components. The values are DSA_free() frees the B<DSA> structure and its components. The values are
erased before the memory is returned to the system. erased before the memory is returned to the system.
If B<dsa> is NULL nothing is done.
=head1 RETURN VALUES =head1 RETURN VALUES

View File

@@ -19,6 +19,7 @@ calling RSA_new_method(NULL).
RSA_free() frees the B<RSA> structure and its components. The key is RSA_free() frees the B<RSA> structure and its components. The key is
erased before the memory is returned to the system. erased before the memory is returned to the system.
If B<rsa> is NULL nothing is done.
=head1 RETURN VALUES =head1 RETURN VALUES

View File

@@ -276,9 +276,7 @@ static int pkey_ctrl_gost(EVP_PKEY *pkey, int op, long arg1, void *arg2)
/* --------------------- free functions * ------------------------------*/ /* --------------------- free functions * ------------------------------*/
static void pkey_free_gost94(EVP_PKEY *key) static void pkey_free_gost94(EVP_PKEY *key)
{ {
if (key->pkey.dsa) { DSA_free(key->pkey.dsa);
DSA_free(key->pkey.dsa);
}
} }
static void pkey_free_gost01(EVP_PKEY *key) static void pkey_free_gost01(EVP_PKEY *key)

View File

@@ -754,10 +754,8 @@ static EVP_PKEY *capi_get_pkey(ENGINE *eng, CAPI_KEY * key)
if (pubkey) if (pubkey)
OPENSSL_free(pubkey); OPENSSL_free(pubkey);
if (!ret) { if (!ret) {
if (rkey) RSA_free(rkey);
RSA_free(rkey); DSA_free(dkey);
if (dkey)
DSA_free(dkey);
} }
return ret; return ret;

View File

@@ -849,8 +849,7 @@ static EVP_PKEY *hwcrhk_load_privkey(ENGINE *eng, const char *key_id,
return res; return res;
err: err:
# ifndef OPENSSL_NO_RSA # ifndef OPENSSL_NO_RSA
if (rtmp) RSA_free(rtmp);
RSA_free(rtmp);
# endif # endif
return NULL; return NULL;
} }

View File

@@ -801,12 +801,10 @@ static EVP_PKEY *sureware_load_public(ENGINE *e, const char *key_id,
return res; return res;
err: err:
# ifndef OPENSSL_NO_RSA # ifndef OPENSSL_NO_RSA
if (rsatmp) RSA_free(rsatmp);
RSA_free(rsatmp);
# endif # endif
# ifndef OPENSSL_NO_DSA # ifndef OPENSSL_NO_DSA
if (dsatmp) DSA_free(dsatmp);
DSA_free(dsatmp);
# endif # endif
return NULL; return NULL;
} }

View File

@@ -1380,16 +1380,12 @@ int ssl3_get_key_exchange(SSL *s)
param = p = (unsigned char *)s->init_msg; param = p = (unsigned char *)s->init_msg;
if (s->session->sess_cert != NULL) { if (s->session->sess_cert != NULL) {
#ifndef OPENSSL_NO_RSA #ifndef OPENSSL_NO_RSA
if (s->session->sess_cert->peer_rsa_tmp != NULL) { RSA_free(s->session->sess_cert->peer_rsa_tmp);
RSA_free(s->session->sess_cert->peer_rsa_tmp); s->session->sess_cert->peer_rsa_tmp = NULL;
s->session->sess_cert->peer_rsa_tmp = NULL;
}
#endif #endif
#ifndef OPENSSL_NO_DH #ifndef OPENSSL_NO_DH
if (s->session->sess_cert->peer_dh_tmp) { DH_free(s->session->sess_cert->peer_dh_tmp);
DH_free(s->session->sess_cert->peer_dh_tmp); s->session->sess_cert->peer_dh_tmp = NULL;
s->session->sess_cert->peer_dh_tmp = NULL;
}
#endif #endif
#ifndef OPENSSL_NO_EC #ifndef OPENSSL_NO_EC
if (s->session->sess_cert->peer_ecdh_tmp) { if (s->session->sess_cert->peer_ecdh_tmp) {
@@ -1955,12 +1951,10 @@ int ssl3_get_key_exchange(SSL *s)
err: err:
EVP_PKEY_free(pkey); EVP_PKEY_free(pkey);
#ifndef OPENSSL_NO_RSA #ifndef OPENSSL_NO_RSA
if (rsa != NULL) RSA_free(rsa);
RSA_free(rsa);
#endif #endif
#ifndef OPENSSL_NO_DH #ifndef OPENSSL_NO_DH
if (dh != NULL) DH_free(dh);
DH_free(dh);
#endif #endif
#ifndef OPENSSL_NO_EC #ifndef OPENSSL_NO_EC
BN_CTX_free(bn_ctx); BN_CTX_free(bn_ctx);

View File

@@ -3138,8 +3138,7 @@ void ssl3_free(SSL *s)
if (s->s3->rrec.comp != NULL) if (s->s3->rrec.comp != NULL)
OPENSSL_free(s->s3->rrec.comp); OPENSSL_free(s->s3->rrec.comp);
#ifndef OPENSSL_NO_DH #ifndef OPENSSL_NO_DH
if (s->s3->tmp.dh != NULL) DH_free(s->s3->tmp.dh);
DH_free(s->s3->tmp.dh);
#endif #endif
#ifndef OPENSSL_NO_EC #ifndef OPENSSL_NO_EC
if (s->s3->tmp.ecdh != NULL) if (s->s3->tmp.ecdh != NULL)
@@ -3181,10 +3180,8 @@ void ssl3_clear(SSL *s)
s->s3->rrec.comp = NULL; s->s3->rrec.comp = NULL;
} }
#ifndef OPENSSL_NO_DH #ifndef OPENSSL_NO_DH
if (s->s3->tmp.dh != NULL) { DH_free(s->s3->tmp.dh);
DH_free(s->s3->tmp.dh); s->s3->tmp.dh = NULL;
s->s3->tmp.dh = NULL;
}
#endif #endif
#ifndef OPENSSL_NO_EC #ifndef OPENSSL_NO_EC
if (s->s3->tmp.ecdh != NULL) { if (s->s3->tmp.ecdh != NULL) {
@@ -3293,8 +3290,7 @@ long ssl3_ctrl(SSL *s, int cmd, long larg, void *parg)
SSLerr(SSL_F_SSL3_CTRL, ERR_R_RSA_LIB); SSLerr(SSL_F_SSL3_CTRL, ERR_R_RSA_LIB);
return (ret); return (ret);
} }
if (s->cert->rsa_tmp != NULL) RSA_free(s->cert->rsa_tmp);
RSA_free(s->cert->rsa_tmp);
s->cert->rsa_tmp = rsa; s->cert->rsa_tmp = rsa;
ret = 1; ret = 1;
} }
@@ -3329,8 +3325,7 @@ long ssl3_ctrl(SSL *s, int cmd, long larg, void *parg)
return (ret); return (ret);
} }
} }
if (s->cert->dh_tmp != NULL) DH_free(s->cert->dh_tmp);
DH_free(s->cert->dh_tmp);
s->cert->dh_tmp = dh; s->cert->dh_tmp = dh;
ret = 1; ret = 1;
} }
@@ -3766,8 +3761,7 @@ long ssl3_ctx_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg)
SSLerr(SSL_F_SSL3_CTX_CTRL, ERR_R_RSA_LIB); SSLerr(SSL_F_SSL3_CTX_CTRL, ERR_R_RSA_LIB);
return (0); return (0);
} else { } else {
if (cert->rsa_tmp != NULL) RSA_free(cert->rsa_tmp);
RSA_free(cert->rsa_tmp);
cert->rsa_tmp = rsa; cert->rsa_tmp = rsa;
return (1); return (1);
} }
@@ -3801,8 +3795,7 @@ long ssl3_ctx_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg)
return 0; return 0;
} }
} }
if (cert->dh_tmp != NULL) DH_free(cert->dh_tmp);
DH_free(cert->dh_tmp);
cert->dh_tmp = new; cert->dh_tmp = new;
return 1; return 1;
} }

View File

@@ -443,12 +443,10 @@ void ssl_cert_free(CERT *c)
#endif #endif
#ifndef OPENSSL_NO_RSA #ifndef OPENSSL_NO_RSA
if (c->rsa_tmp) RSA_free(c->rsa_tmp);
RSA_free(c->rsa_tmp);
#endif #endif
#ifndef OPENSSL_NO_DH #ifndef OPENSSL_NO_DH
if (c->dh_tmp) DH_free(c->dh_tmp);
DH_free(c->dh_tmp);
#endif #endif
#ifndef OPENSSL_NO_EC #ifndef OPENSSL_NO_EC
if (c->ecdh_tmp) if (c->ecdh_tmp)
@@ -651,12 +649,10 @@ void ssl_sess_cert_free(SESS_CERT *sc)
} }
#ifndef OPENSSL_NO_RSA #ifndef OPENSSL_NO_RSA
if (sc->peer_rsa_tmp != NULL) RSA_free(sc->peer_rsa_tmp);
RSA_free(sc->peer_rsa_tmp);
#endif #endif
#ifndef OPENSSL_NO_DH #ifndef OPENSSL_NO_DH
if (sc->peer_dh_tmp != NULL) DH_free(sc->peer_dh_tmp);
DH_free(sc->peer_dh_tmp);
#endif #endif
#ifndef OPENSSL_NO_EC #ifndef OPENSSL_NO_EC
if (sc->peer_ecdh_tmp != NULL) if (sc->peer_ecdh_tmp != NULL)

View File

@@ -421,8 +421,7 @@ static int cmd_DHParameters(SSL_CONF_CTX *cctx, const char *value)
if (cctx->ssl) if (cctx->ssl)
rv = SSL_set_tmp_dh(cctx->ssl, dh); rv = SSL_set_tmp_dh(cctx->ssl, dh);
end: end:
if (dh) DH_free(dh);
DH_free(dh);
if (in) if (in)
BIO_free(in); BIO_free(in);
return rv > 0; return rv > 0;

View File

@@ -2968,10 +2968,8 @@ static RSA *tmp_rsa_cb(SSL *s, int is_export, int keylength)
static void free_tmp_rsa(void) static void free_tmp_rsa(void)
{ {
if (rsa_tmp != NULL) { RSA_free(rsa_tmp);
RSA_free(rsa_tmp); rsa_tmp = NULL;
rsa_tmp = NULL;
}
} }
#endif #endif