Continue standardisation of malloc handling in apps

continue on from previous commits but in the apps directory

Reviewed-by: Kurt Roeckx <kurt@openssl.org>
This commit is contained in:
Matt Caswell 2015-10-30 11:18:04 +00:00
parent 90945fa31a
commit 96487cddd4
16 changed files with 46 additions and 38 deletions

View File

@ -646,7 +646,7 @@ int load_cert_crl_http(const char *url, X509 **pcert, X509_CRL **pcrl)
if (!bio || !BIO_set_conn_port(bio, port)) if (!bio || !BIO_set_conn_port(bio, port))
goto err; goto err;
rctx = OCSP_REQ_CTX_new(bio, 1024); rctx = OCSP_REQ_CTX_new(bio, 1024);
if (!rctx) if (rctx == NULL)
goto err; goto err;
if (!OCSP_REQ_CTX_http(rctx, "GET", path)) if (!OCSP_REQ_CTX_http(rctx, "GET", path))
goto err; goto err;
@ -856,7 +856,7 @@ EVP_PKEY *load_pubkey(const char *file, int format, int maybe_stdin,
rsa = d2i_RSAPublicKey_bio(key, NULL); rsa = d2i_RSAPublicKey_bio(key, NULL);
if (rsa) { if (rsa) {
pkey = EVP_PKEY_new(); pkey = EVP_PKEY_new();
if (pkey) if (pkey != NULL)
EVP_PKEY_set1_RSA(pkey, rsa); EVP_PKEY_set1_RSA(pkey, rsa);
RSA_free(rsa); RSA_free(rsa);
} else } else
@ -866,9 +866,9 @@ EVP_PKEY *load_pubkey(const char *file, int format, int maybe_stdin,
rsa = PEM_read_bio_RSAPublicKey(key, NULL, rsa = PEM_read_bio_RSAPublicKey(key, NULL,
(pem_password_cb *)password_callback, (pem_password_cb *)password_callback,
&cb_data); &cb_data);
if (rsa) { if (rsa != NULL) {
pkey = EVP_PKEY_new(); pkey = EVP_PKEY_new();
if (pkey) if (pkey != NULL)
EVP_PKEY_set1_RSA(pkey, rsa); EVP_PKEY_set1_RSA(pkey, rsa);
RSA_free(rsa); RSA_free(rsa);
} else } else
@ -1252,7 +1252,7 @@ X509_STORE *setup_verify(char *CAfile, char *CApath, int noCAfile, int noCApath)
X509_STORE *store = X509_STORE_new(); X509_STORE *store = X509_STORE_new();
X509_LOOKUP *lookup; X509_LOOKUP *lookup;
if (!store) if (store == NULL)
goto end; goto end;
if(CAfile != NULL || !noCAfile) { if(CAfile != NULL || !noCAfile) {
@ -1541,7 +1541,7 @@ int rand_serial(BIGNUM *b, ASN1_INTEGER *ai)
else else
btmp = BN_new(); btmp = BN_new();
if (!btmp) if (btmp == NULL)
return 0; return 0;
if (!BN_pseudo_rand(btmp, SERIAL_RAND_BITS, 0, 0)) if (!BN_pseudo_rand(btmp, SERIAL_RAND_BITS, 0, 0))
@ -1901,7 +1901,7 @@ int bio_to_mem(unsigned char **out, int maxlen, BIO *in)
int len, ret; int len, ret;
unsigned char tbuf[1024]; unsigned char tbuf[1024];
mem = BIO_new(BIO_s_mem()); mem = BIO_new(BIO_s_mem());
if (!mem) if (mem == NULL)
return -1; return -1;
for (;;) { for (;;) {
if ((maxlen != -1) && maxlen < 1024) if ((maxlen != -1) && maxlen < 1024)

View File

@ -1165,7 +1165,7 @@ end_of_options:
goto end; goto end;
tmptm = ASN1_TIME_new(); tmptm = ASN1_TIME_new();
if (!tmptm) if (tmptm == NULL)
goto end; goto end;
X509_gmtime_adj(tmptm, 0); X509_gmtime_adj(tmptm, 0);
X509_CRL_set_lastUpdate(crl, tmptm); X509_CRL_set_lastUpdate(crl, tmptm);
@ -2283,10 +2283,12 @@ static int do_updatedb(CA_DB *db)
char **rrow, *a_tm_s; char **rrow, *a_tm_s;
a_tm = ASN1_UTCTIME_new(); a_tm = ASN1_UTCTIME_new();
if (a_tm == NULL)
return -1;
/* get actual time and make a string */ /* get actual time and make a string */
a_tm = X509_gmtime_adj(a_tm, 0); a_tm = X509_gmtime_adj(a_tm, 0);
a_tm_s = (char *)OPENSSL_malloc(a_tm->length + 1); a_tm_s = (char *)app_malloc(a_tm->length + 1, "time string");
memcpy(a_tm_s, a_tm->data, a_tm->length); memcpy(a_tm_s, a_tm->data, a_tm->length);
a_tm_s[a_tm->length] = '\0'; a_tm_s[a_tm->length] = '\0';
@ -2470,7 +2472,7 @@ int make_revoked(X509_REVOKED *rev, const char *str)
if (rev && (reason_code != OCSP_REVOKED_STATUS_NOSTATUS)) { if (rev && (reason_code != OCSP_REVOKED_STATUS_NOSTATUS)) {
rtmp = ASN1_ENUMERATED_new(); rtmp = ASN1_ENUMERATED_new();
if (!rtmp || !ASN1_ENUMERATED_set(rtmp, reason_code)) if (rtmp == NULL || !ASN1_ENUMERATED_set(rtmp, reason_code))
goto end; goto end;
if (!X509_REVOKED_add1_ext_i2d(rev, NID_crl_reason, rtmp, 0, 0)) if (!X509_REVOKED_add1_ext_i2d(rev, NID_crl_reason, rtmp, 0, 0))
goto end; goto end;
@ -2576,7 +2578,7 @@ int unpack_revinfo(ASN1_TIME **prevtm, int *preason, ASN1_OBJECT **phold,
if (prevtm) { if (prevtm) {
*prevtm = ASN1_UTCTIME_new(); *prevtm = ASN1_UTCTIME_new();
if (!*prevtm) { if (*prevtm == NULL) {
BIO_printf(bio_err, "memory allocation failure\n"); BIO_printf(bio_err, "memory allocation failure\n");
goto end; goto end;
} }
@ -2622,7 +2624,7 @@ int unpack_revinfo(ASN1_TIME **prevtm, int *preason, ASN1_OBJECT **phold,
goto end; goto end;
} }
comp_time = ASN1_GENERALIZEDTIME_new(); comp_time = ASN1_GENERALIZEDTIME_new();
if (!comp_time) { if (comp_time == NULL) {
BIO_printf(bio_err, "memory allocation failure\n"); BIO_printf(bio_err, "memory allocation failure\n");
goto end; goto end;
} }

View File

@ -1269,7 +1269,7 @@ static STACK_OF(GENERAL_NAMES) *make_names_stack(STACK_OF(OPENSSL_STRING) *ns)
if (!gen) if (!gen)
goto err; goto err;
gens = GENERAL_NAMES_new(); gens = GENERAL_NAMES_new();
if (!gens) if (gens == NULL)
goto err; goto err;
if (!sk_GENERAL_NAME_push(gens, gen)) if (!sk_GENERAL_NAME_push(gens, gen))
goto err; goto err;

View File

@ -251,7 +251,7 @@ int dhparam_main(int argc, char **argv)
BN_GENCB *cb; BN_GENCB *cb;
cb = BN_GENCB_new(); cb = BN_GENCB_new();
if (!cb) { if (cb == NULL) {
ERR_print_errors(bio_err); ERR_print_errors(bio_err);
goto end; goto end;
} }
@ -271,7 +271,7 @@ int dhparam_main(int argc, char **argv)
BIO_printf(bio_err, BIO_printf(bio_err,
"Generating DSA parameters, %d bit long prime\n", num); "Generating DSA parameters, %d bit long prime\n", num);
if (!dsa if (dsa == NULL
|| !DSA_generate_parameters_ex(dsa, num, NULL, 0, NULL, NULL, || !DSA_generate_parameters_ex(dsa, num, NULL, 0, NULL, NULL,
cb)) { cb)) {
DSA_free(dsa); DSA_free(dsa);
@ -295,7 +295,7 @@ int dhparam_main(int argc, char **argv)
"Generating DH parameters, %d bit long safe prime, generator %d\n", "Generating DH parameters, %d bit long safe prime, generator %d\n",
num, g); num, g);
BIO_printf(bio_err, "This is going to take a long time\n"); BIO_printf(bio_err, "This is going to take a long time\n");
if (!dh || !DH_generate_parameters_ex(dh, num, g, cb)) { if (dh == NULL || !DH_generate_parameters_ex(dh, num, g, cb)) {
BN_GENCB_free(cb); BN_GENCB_free(cb);
ERR_print_errors(bio_err); ERR_print_errors(bio_err);
goto end; goto end;

View File

@ -208,14 +208,14 @@ int dsaparam_main(int argc, char **argv)
if (numbits > 0) { if (numbits > 0) {
cb = BN_GENCB_new(); cb = BN_GENCB_new();
if (!cb) { if (cb == NULL) {
BIO_printf(bio_err, "Error allocating BN_GENCB object\n"); BIO_printf(bio_err, "Error allocating BN_GENCB object\n");
goto end; goto end;
} }
BN_GENCB_set(cb, dsa_cb, bio_err); BN_GENCB_set(cb, dsa_cb, bio_err);
assert(need_rand); assert(need_rand);
dsa = DSA_new(); dsa = DSA_new();
if (!dsa) { if (dsa == NULL) {
BIO_printf(bio_err, "Error allocating DSA object\n"); BIO_printf(bio_err, "Error allocating DSA object\n");
goto end; goto end;
} }

View File

@ -269,7 +269,7 @@ static int init_keygen_file(EVP_PKEY_CTX **pctx, const char *file, ENGINE *e)
} }
ctx = EVP_PKEY_CTX_new(pkey, e); ctx = EVP_PKEY_CTX_new(pkey, e);
if (!ctx) if (ctx == NULL)
goto err; goto err;
if (EVP_PKEY_keygen_init(ctx) <= 0) if (EVP_PKEY_keygen_init(ctx) <= 0)
goto err; goto err;

View File

@ -114,7 +114,7 @@ int genrsa_main(int argc, char **argv)
char *inrand = NULL, *prog, *hexe, *dece; char *inrand = NULL, *prog, *hexe, *dece;
OPTION_CHOICE o; OPTION_CHOICE o;
if (!bn || !cb) if (bn == NULL || cb == NULL)
goto end; goto end;
BN_GENCB_set(cb, genrsa_cb, bio_err); BN_GENCB_set(cb, genrsa_cb, bio_err);
@ -185,7 +185,7 @@ int genrsa_main(int argc, char **argv)
BIO_printf(bio_err, "Generating RSA private key, %d bit long modulus\n", BIO_printf(bio_err, "Generating RSA private key, %d bit long modulus\n",
num); num);
rsa = e ? RSA_new_method(e) : RSA_new(); rsa = e ? RSA_new_method(e) : RSA_new();
if (!rsa) if (rsa == NULL)
goto end; goto end;
if (non_fips_allow) if (non_fips_allow)

View File

@ -118,8 +118,10 @@ int nseq_main(int argc, char **argv)
if (toseq) { if (toseq) {
seq = NETSCAPE_CERT_SEQUENCE_new(); seq = NETSCAPE_CERT_SEQUENCE_new();
if (seq == NULL)
goto end;
seq->certs = sk_X509_new_null(); seq->certs = sk_X509_new_null();
if (!seq->certs) if (seq->certs == NULL)
goto end; goto end;
while ((x509 = PEM_read_bio_X509(in, NULL, NULL, NULL))) while ((x509 = PEM_read_bio_X509(in, NULL, NULL, NULL)))
sk_X509_push(seq->certs, x509); sk_X509_push(seq->certs, x509);

View File

@ -783,9 +783,9 @@ static int add_ocsp_cert(OCSP_REQUEST **req, X509 *cert,
BIO_printf(bio_err, "No issuer certificate specified\n"); BIO_printf(bio_err, "No issuer certificate specified\n");
return 0; return 0;
} }
if (!*req) if (*req == NULL)
*req = OCSP_REQUEST_new(); *req = OCSP_REQUEST_new();
if (!*req) if (*req == NULL)
goto err; goto err;
id = OCSP_cert_to_id(cert_id_md, cert, issuer); id = OCSP_cert_to_id(cert_id_md, cert, issuer);
if (!id || !sk_OCSP_CERTID_push(ids, id)) if (!id || !sk_OCSP_CERTID_push(ids, id))
@ -811,9 +811,9 @@ static int add_ocsp_serial(OCSP_REQUEST **req, char *serial,
BIO_printf(bio_err, "No issuer certificate specified\n"); BIO_printf(bio_err, "No issuer certificate specified\n");
return 0; return 0;
} }
if (!*req) if (*req == NULL)
*req = OCSP_REQUEST_new(); *req = OCSP_REQUEST_new();
if (!*req) if (*req == NULL)
goto err; goto err;
iname = X509_get_subject_name(issuer); iname = X509_get_subject_name(issuer);
ikey = X509_get0_pubkey_bitstr(issuer); ikey = X509_get0_pubkey_bitstr(issuer);
@ -824,7 +824,7 @@ static int add_ocsp_serial(OCSP_REQUEST **req, char *serial,
} }
id = OCSP_cert_id_new(cert_id_md, iname, ikey, sno); id = OCSP_cert_id_new(cert_id_md, iname, ikey, sno);
ASN1_INTEGER_free(sno); ASN1_INTEGER_free(sno);
if (!id || !sk_OCSP_CERTID_push(ids, id)) if (id == NULL || !sk_OCSP_CERTID_push(ids, id))
goto err; goto err;
if (!OCSP_request_add0_id(*req, id)) if (!OCSP_request_add0_id(*req, id))
goto err; goto err;
@ -1029,7 +1029,7 @@ static BIO *init_responder(const char *port)
return NULL; return NULL;
# endif # endif
bufbio = BIO_new(BIO_f_buffer()); bufbio = BIO_new(BIO_f_buffer());
if (!bufbio) if (bufbio == NULL)
goto err; goto err;
acbio = BIO_new(BIO_s_accept()); acbio = BIO_new(BIO_s_accept());
if (acbio == NULL if (acbio == NULL
@ -1220,7 +1220,7 @@ static OCSP_RESPONSE *query_responder(BIO *cbio, const char *host,
} }
ctx = OCSP_sendreq_new(cbio, path, NULL, -1); ctx = OCSP_sendreq_new(cbio, path, NULL, -1);
if (!ctx) if (ctx == NULL)
return NULL; return NULL;
for (i = 0; i < sk_CONF_VALUE_num(headers); i++) { for (i = 0; i < sk_CONF_VALUE_num(headers); i++) {

View File

@ -376,7 +376,7 @@ static EVP_PKEY_CTX *init_ctx(int *pkeysize,
EVP_PKEY_free(pkey); EVP_PKEY_free(pkey);
if (!ctx) if (ctx == NULL)
goto end; goto end;
switch (pkey_op) { switch (pkey_op) {

View File

@ -1442,7 +1442,7 @@ static EVP_PKEY_CTX *set_keygen_ctx(const char *gstr,
} else } else
gctx = EVP_PKEY_CTX_new_id(*pkey_type, keygen_engine); gctx = EVP_PKEY_CTX_new_id(*pkey_type, keygen_engine);
if (!gctx) { if (gctx == NULL) {
BIO_puts(bio_err, "Error allocating keygen context\n"); BIO_puts(bio_err, "Error allocating keygen context\n");
ERR_print_errors(bio_err); ERR_print_errors(bio_err);
return NULL; return NULL;

View File

@ -1279,8 +1279,10 @@ int ssl_load_stores(SSL_CTX *ctx,
{ {
X509_STORE *vfy = NULL, *ch = NULL; X509_STORE *vfy = NULL, *ch = NULL;
int rv = 0; int rv = 0;
if (vfyCApath || vfyCAfile) { if (vfyCApath != NULL || vfyCAfile != NULL) {
vfy = X509_STORE_new(); vfy = X509_STORE_new();
if (vfy == NULL)
goto err;
if (!X509_STORE_load_locations(vfy, vfyCAfile, vfyCApath)) if (!X509_STORE_load_locations(vfy, vfyCAfile, vfyCApath))
goto err; goto err;
add_crls_store(vfy, crls); add_crls_store(vfy, crls);
@ -1288,8 +1290,10 @@ int ssl_load_stores(SSL_CTX *ctx,
if (crl_download) if (crl_download)
store_setup_crl_download(vfy); store_setup_crl_download(vfy);
} }
if (chCApath || chCAfile) { if (chCApath != NULL || chCAfile != NULL) {
ch = X509_STORE_new(); ch = X509_STORE_new();
if (ch == NULL)
goto err;
if (!X509_STORE_load_locations(ch, chCAfile, chCApath)) if (!X509_STORE_load_locations(ch, chCAfile, chCApath))
goto err; goto err;
SSL_CTX_set1_chain_cert_store(ctx, ch); SSL_CTX_set1_chain_cert_store(ctx, ch);

View File

@ -667,7 +667,7 @@ static int cert_status_cb(SSL *s, void *arg)
goto done; goto done;
} }
req = OCSP_REQUEST_new(); req = OCSP_REQUEST_new();
if (!req) if (req == NULL)
goto err; goto err;
id = OCSP_cert_to_id(NULL, x, obj.data.x509); id = OCSP_cert_to_id(NULL, x, obj.data.x509);
X509_free(obj.data.x509); X509_free(obj.data.x509);

View File

@ -189,7 +189,7 @@ int spkac_main(int argc, char **argv)
spkstr = NCONF_get_string(conf, spksect, spkac); spkstr = NCONF_get_string(conf, spksect, spkac);
if (!spkstr) { if (spkstr == NULL) {
BIO_printf(bio_err, "Can't find SPKAC called \"%s\"\n", spkac); BIO_printf(bio_err, "Can't find SPKAC called \"%s\"\n", spkac);
ERR_print_errors(bio_err); ERR_print_errors(bio_err);
goto end; goto end;

View File

@ -947,7 +947,7 @@ static X509_STORE *create_cert_store(char *CApath, char *CAfile)
cert_ctx = X509_STORE_new(); cert_ctx = X509_STORE_new();
X509_STORE_set_verify_cb(cert_ctx, verify_cb); X509_STORE_set_verify_cb(cert_ctx, verify_cb);
if (CApath) { if (CApath != NULL) {
lookup = X509_STORE_add_lookup(cert_ctx, X509_LOOKUP_hash_dir()); lookup = X509_STORE_add_lookup(cert_ctx, X509_LOOKUP_hash_dir());
if (lookup == NULL) { if (lookup == NULL) {
BIO_printf(bio_err, "memory allocation failure\n"); BIO_printf(bio_err, "memory allocation failure\n");
@ -960,7 +960,7 @@ static X509_STORE *create_cert_store(char *CApath, char *CAfile)
} }
} }
if (CAfile) { if (CAfile != NULL) {
lookup = X509_STORE_add_lookup(cert_ctx, X509_LOOKUP_file()); lookup = X509_STORE_add_lookup(cert_ctx, X509_LOOKUP_file());
if (lookup == NULL) { if (lookup == NULL) {
BIO_printf(bio_err, "memory allocation failure\n"); BIO_printf(bio_err, "memory allocation failure\n");

View File

@ -590,7 +590,7 @@ int x509_main(int argc, char **argv)
if (sno == NULL) { if (sno == NULL) {
sno = ASN1_INTEGER_new(); sno = ASN1_INTEGER_new();
if (!sno || !rand_serial(NULL, sno)) if (sno == NULL || !rand_serial(NULL, sno))
goto end; goto end;
if (!X509_set_serialNumber(x, sno)) if (!X509_set_serialNumber(x, sno))
goto end; goto end;