In apps, malloc or die

No point in proceeding if you're out of memory.  So change
*all* OPENSSL_malloc calls in apps to use the new routine which
prints a message and exits.

Reviewed-by: Richard Levitte <levitte@openssl.org>
This commit is contained in:
Rich Salz 2015-04-30 17:48:31 -04:00
parent 222561fe8e
commit 68dc682499
24 changed files with 126 additions and 310 deletions

View File

@ -180,7 +180,7 @@ int chopup_args(ARGS *arg, char *buf)
arg->argc = 0; arg->argc = 0;
if (arg->size == 0) { if (arg->size == 0) {
arg->size = 20; arg->size = 20;
arg->argv = OPENSSL_malloc(sizeof(char *) * arg->size); arg->argv = app_malloc(sizeof(char *) * arg->size, "argv space");
if (arg->argv == NULL) if (arg->argv == NULL)
return 0; return 0;
} }
@ -367,13 +367,7 @@ int password_callback(char *buf, int bufsiz, int verify, PW_CB_DATA *cb_tmp)
ok = UI_add_input_string(ui, prompt, ui_flags, buf, ok = UI_add_input_string(ui, prompt, ui_flags, buf,
PW_MIN_LENGTH, bufsiz - 1); PW_MIN_LENGTH, bufsiz - 1);
if (ok >= 0 && verify) { if (ok >= 0 && verify) {
buff = OPENSSL_malloc(bufsiz); buff = app_malloc(bufsiz, "password buffer");
if (!buff) {
BIO_printf(bio_err, "Out of memory\n");
UI_free(ui);
OPENSSL_free(prompt);
return 0;
}
ok = UI_add_verify_string(ui, prompt, ui_flags, buff, ok = UI_add_verify_string(ui, prompt, ui_flags, buff,
PW_MIN_LENGTH, bufsiz - 1, buf); PW_MIN_LENGTH, bufsiz - 1, buf);
} }
@ -989,6 +983,21 @@ static int load_certs_crls(const char *file, int format,
return rv; return rv;
} }
void* app_malloc(int sz, const char *what)
{
void *vp = OPENSSL_malloc(sz);
if (vp == NULL) {
BIO_printf(bio_err, "%s: Could not allocate %d bytes for %s\n",
opt_getprog(), sz, what);
ERR_print_errors(bio_err);
exit(1);
}
return vp;
}
STACK_OF(X509) *load_certs(const char *file, int format, STACK_OF(X509) *load_certs(const char *file, int format,
const char *pass, ENGINE *e, const char *desc) const char *pass, ENGINE *e, const char *desc)
{ {
@ -1585,11 +1594,7 @@ CA_DB *load_index(char *dbfile, DB_ATTR *db_attr)
} }
} }
if ((retdb = OPENSSL_malloc(sizeof(CA_DB))) == NULL) { retdb = app_malloc(sizeof *retdb, "new DB");
fprintf(stderr, "Out of memory\n");
goto err;
}
retdb->db = tmpdb; retdb->db = tmpdb;
tmpdb = NULL; tmpdb = NULL;
if (db_attr) if (db_attr)
@ -2230,10 +2235,7 @@ unsigned char *next_protos_parse(unsigned short *outlen, const char *in)
if (len >= 65535) if (len >= 65535)
return NULL; return NULL;
out = OPENSSL_malloc(strlen(in) + 1); out = app_malloc(strlen(in) + 1, "NPN buffer");
if (!out)
return NULL;
for (i = 0; i <= len; ++i) { for (i = 0; i <= len; ++i) {
if (i == len || in[i] == ',') { if (i == len || in[i] == ',') {
if (i - start > 255) { if (i - start > 255) {

View File

@ -469,6 +469,7 @@ typedef struct ca_db_st {
TXT_DB *db; TXT_DB *db;
} CA_DB; } CA_DB;
void* app_malloc(int sz, const char *what);
BIGNUM *load_serial(char *serialfile, int create, ASN1_INTEGER **retai); BIGNUM *load_serial(char *serialfile, int create, ASN1_INTEGER **retai);
int save_serial(char *serialfile, char *suffix, BIGNUM *serial, int save_serial(char *serialfile, char *suffix, BIGNUM *serial,
ASN1_INTEGER **retai); ASN1_INTEGER **retai);

View File

@ -491,21 +491,11 @@ end_of_options:
const char *s = X509_get_default_cert_area(); const char *s = X509_get_default_cert_area();
size_t len; size_t len;
len = strlen(s) + 1 + sizeof(CONFIG_FILE);
tofree = app_malloc(len, "config filename");
#ifdef OPENSSL_SYS_VMS #ifdef OPENSSL_SYS_VMS
len = strlen(s) + sizeof(CONFIG_FILE);
tofree = OPENSSL_malloc(len);
if (!tofree) {
BIO_printf(bio_err, "Out of memory\n");
goto end;
}
strcpy(tofree, s); strcpy(tofree, s);
#else #else
len = strlen(s) + sizeof(CONFIG_FILE) + 1;
tofree = OPENSSL_malloc(len);
if (!tofree) {
BIO_printf(bio_err, "Out of memory\n");
goto end;
}
BUF_strlcpy(tofree, s, len); BUF_strlcpy(tofree, s, len);
BUF_strlcat(tofree, "/", len); BUF_strlcat(tofree, "/", len);
#endif #endif
@ -1975,17 +1965,17 @@ static int do_body(X509 **xret, EVP_PKEY *pkey, X509 *x509,
goto end; goto end;
/* We now just add it to the database */ /* We now just add it to the database */
row[DB_type] = OPENSSL_malloc(2); row[DB_type] = app_malloc(2, "row db type");
tm = X509_get_notAfter(ret); tm = X509_get_notAfter(ret);
row[DB_exp_date] = OPENSSL_malloc(tm->length + 1); row[DB_exp_date] = app_malloc(tm->length + 1, "row expdate");
memcpy(row[DB_exp_date], tm->data, tm->length); memcpy(row[DB_exp_date], tm->data, tm->length);
row[DB_exp_date][tm->length] = '\0'; row[DB_exp_date][tm->length] = '\0';
row[DB_rev_date] = NULL; row[DB_rev_date] = NULL;
/* row[DB_serial] done already */ /* row[DB_serial] done already */
row[DB_file] = OPENSSL_malloc(8); row[DB_file] = app_malloc(8, "row file");
row[DB_name] = X509_NAME_oneline(X509_get_subject_name(ret), NULL, 0); row[DB_name] = X509_NAME_oneline(X509_get_subject_name(ret), NULL, 0);
if ((row[DB_type] == NULL) || (row[DB_exp_date] == NULL) || if ((row[DB_type] == NULL) || (row[DB_exp_date] == NULL) ||
@ -1997,11 +1987,7 @@ static int do_body(X509 **xret, EVP_PKEY *pkey, X509 *x509,
row[DB_type][0] = 'V'; row[DB_type][0] = 'V';
row[DB_type][1] = '\0'; row[DB_type][1] = '\0';
if ((irow = OPENSSL_malloc(sizeof(char *) * (DB_NUMBER + 1))) == NULL) { irow = app_malloc(sizeof(char *) * (DB_NUMBER + 1), "row space");
BIO_printf(bio_err, "Memory allocation failure\n");
goto end;
}
for (i = 0; i < DB_NUMBER; i++) { for (i = 0; i < DB_NUMBER; i++) {
irow[i] = row[i]; irow[i] = row[i];
row[i] = NULL; row[i] = NULL;
@ -2223,34 +2209,25 @@ static int do_revoke(X509 *x509, CA_DB *db, int type, char *value)
row[DB_serial], row[DB_name]); row[DB_serial], row[DB_name]);
/* We now just add it to the database */ /* We now just add it to the database */
row[DB_type] = OPENSSL_malloc(2); row[DB_type] = app_malloc(2, "row type");
tm = X509_get_notAfter(x509); tm = X509_get_notAfter(x509);
row[DB_exp_date] = OPENSSL_malloc(tm->length + 1); row[DB_exp_date] = app_malloc(tm->length + 1, "row exp_data");
memcpy(row[DB_exp_date], tm->data, tm->length); memcpy(row[DB_exp_date], tm->data, tm->length);
row[DB_exp_date][tm->length] = '\0'; row[DB_exp_date][tm->length] = '\0';
row[DB_rev_date] = NULL; row[DB_rev_date] = NULL;
/* row[DB_serial] done already */ /* row[DB_serial] done already */
row[DB_file] = OPENSSL_malloc(8); row[DB_file] = app_malloc(8, "row filename");
/* row[DB_name] done already */ /* row[DB_name] done already */
if ((row[DB_type] == NULL) || (row[DB_exp_date] == NULL) ||
(row[DB_file] == NULL)) {
BIO_printf(bio_err, "Memory allocation failure\n");
goto end;
}
BUF_strlcpy(row[DB_file], "unknown", 8); BUF_strlcpy(row[DB_file], "unknown", 8);
row[DB_type][0] = 'V'; row[DB_type][0] = 'V';
row[DB_type][1] = '\0'; row[DB_type][1] = '\0';
if ((irow = OPENSSL_malloc(sizeof(char *) * (DB_NUMBER + 1))) == NULL) { irow = app_malloc(sizeof(char *) * (DB_NUMBER + 1), "row ptr");
BIO_printf(bio_err, "Memory allocation failure\n");
goto end;
}
for (i = 0; i < DB_NUMBER; i++) { for (i = 0; i < DB_NUMBER; i++) {
irow[i] = row[i]; irow[i] = row[i];
row[i] = NULL; row[i] = NULL;
@ -2312,11 +2289,7 @@ static int get_certificate_status(const char *serial, CA_DB *db)
row[i] = NULL; row[i] = NULL;
/* Malloc needed char spaces */ /* Malloc needed char spaces */
row[DB_serial] = OPENSSL_malloc(strlen(serial) + 2); row[DB_serial] = app_malloc(strlen(serial) + 2, "row serial#");
if (row[DB_serial] == NULL) {
BIO_printf(bio_err, "Malloc failure\n");
goto end;
}
if (strlen(serial) % 2) { if (strlen(serial) % 2) {
/* /*
@ -2385,11 +2358,7 @@ static int do_updatedb(CA_DB *db)
/* 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 = OPENSSL_malloc(a_tm->length + 1); a_tm_s = (char *)OPENSSL_malloc(a_tm->length + 1);
if (a_tm_s == NULL) {
cnt = -1;
goto end;
}
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';
@ -2429,11 +2398,8 @@ static int do_updatedb(CA_DB *db)
} }
} }
end:
ASN1_UTCTIME_free(a_tm); ASN1_UTCTIME_free(a_tm);
OPENSSL_free(a_tm_s); OPENSSL_free(a_tm_s);
return (cnt); return (cnt);
} }
@ -2533,11 +2499,7 @@ char *make_revocation_str(int rev_type, char *rev_arg)
if (other) if (other)
i += strlen(other) + 1; i += strlen(other) + 1;
str = OPENSSL_malloc(i); str = app_malloc(i, "revocation reason");
if (!str)
return NULL;
BUF_strlcpy(str, (char *)revtm->data, i); BUF_strlcpy(str, (char *)revtm->data, i);
if (reason) { if (reason) {
BUF_strlcat(str, ",", i); BUF_strlcat(str, ",", i);

View File

@ -570,11 +570,7 @@ int cms_main(int argc, char **argv)
} }
if (key_param == NULL || key_param->idx != keyidx) { if (key_param == NULL || key_param->idx != keyidx) {
cms_key_param *nparam; cms_key_param *nparam;
nparam = OPENSSL_malloc(sizeof(cms_key_param)); nparam = app_malloc(sizeof *nparam, "key param buffer");
if (!nparam) {
BIO_printf(bio_err, "Out of memory\n");
goto end;
}
nparam->idx = keyidx; nparam->idx = keyidx;
if ((nparam->param = sk_OPENSSL_STRING_new_null()) == NULL) if ((nparam->param = sk_OPENSSL_STRING_new_null()) == NULL)
goto end; goto end;

View File

@ -139,10 +139,7 @@ int dgst_main(int argc, char **argv)
int engine_impl = 0; int engine_impl = 0;
prog = opt_progname(argv[0]); prog = opt_progname(argv[0]);
if ((buf = OPENSSL_malloc(BUFSIZE)) == NULL) { buf = app_malloc(BUFSIZE, "I/O buffer");
BIO_printf(bio_err, "%s: out of memory\n", prog);
goto end;
}
md = EVP_get_digestbyname(prog); md = EVP_get_digestbyname(prog);
prog = opt_init(argc, argv, dgst_options); prog = opt_init(argc, argv, dgst_options);
@ -394,11 +391,7 @@ int dgst_main(int argc, char **argv)
goto end; goto end;
} }
siglen = EVP_PKEY_size(sigkey); siglen = EVP_PKEY_size(sigkey);
sigbuf = OPENSSL_malloc(siglen); sigbuf = app_malloc(siglen, "signature buffer");
if (!sigbuf) {
BIO_printf(bio_err, "Out of memory\n");
goto end;
}
siglen = BIO_read(sigbio, sigbuf, siglen); siglen = BIO_read(sigbio, sigbuf, siglen);
BIO_free(sigbio); BIO_free(sigbio);
if (siglen <= 0) { if (siglen <= 0) {

View File

@ -379,11 +379,7 @@ int dhparam_main(int argc, char **argv)
len = BN_num_bytes(dh->p); len = BN_num_bytes(dh->p);
bits = BN_num_bits(dh->p); bits = BN_num_bits(dh->p);
data = OPENSSL_malloc(len); data = app_malloc(len, "print a BN");
if (data == NULL) {
perror("OPENSSL_malloc");
goto end;
}
BIO_printf(out, "#ifndef HEADER_DH_H\n" BIO_printf(out, "#ifndef HEADER_DH_H\n"
"# include <openssl/dh.h>\n" "# include <openssl/dh.h>\n"
"#endif\n" "#endif\n"

View File

@ -268,16 +268,9 @@ int dsaparam_main(int argc, char **argv)
} }
if (C) { if (C) {
unsigned char *data; int len = BN_num_bytes(dsa->p);
int len, bits_p; int bits_p = BN_num_bits(dsa->p);
unsigned char *data = app_malloc(len + 20, "BN space");
len = BN_num_bytes(dsa->p);
bits_p = BN_num_bits(dsa->p);
data = OPENSSL_malloc(len + 20);
if (data == NULL) {
perror("OPENSSL_malloc");
goto end;
}
BIO_printf(bio_out, "DSA *get_dsa%d()\n{\n", bits_p); BIO_printf(bio_out, "DSA *get_dsa%d()\n{\n", bits_p);
print_bignum_var(bio_out, dsa->p, "dsap", len, data); print_bignum_var(bio_out, dsa->p, "dsap", len, data);

View File

@ -229,16 +229,10 @@ int ecparam_main(int argc, char **argv)
if (list_curves) { if (list_curves) {
EC_builtin_curve *curves = NULL; EC_builtin_curve *curves = NULL;
size_t crv_len = 0; size_t crv_len = EC_get_builtin_curves(NULL, 0);
size_t n = 0; size_t n;
crv_len = EC_get_builtin_curves(NULL, 0);
curves = OPENSSL_malloc((int)(sizeof(EC_builtin_curve) * crv_len));
if (curves == NULL)
goto end;
curves = app_malloc((int)(sizeof *curves * crv_len), "list curves");
if (!EC_get_builtin_curves(curves, crv_len)) { if (!EC_get_builtin_curves(curves, crv_len)) {
OPENSSL_free(curves); OPENSSL_free(curves);
goto end; goto end;
@ -346,7 +340,7 @@ int ecparam_main(int argc, char **argv)
|| (ec_gen = BN_new()) == NULL || (ec_gen = BN_new()) == NULL
|| (ec_order = BN_new()) == NULL || (ec_order = BN_new()) == NULL
|| (ec_cofactor = BN_new()) == NULL) { || (ec_cofactor = BN_new()) == NULL) {
perror("OPENSSL_malloc"); perror("Can't allocate BN");
goto end; goto end;
} }
@ -388,11 +382,7 @@ int ecparam_main(int argc, char **argv)
if ((tmp_len = (size_t)BN_num_bytes(ec_cofactor)) > buf_len) if ((tmp_len = (size_t)BN_num_bytes(ec_cofactor)) > buf_len)
buf_len = tmp_len; buf_len = tmp_len;
buffer = OPENSSL_malloc(buf_len); buffer = app_malloc(buf_len, "BN buffer");
if (buffer == NULL) {
perror("OPENSSL_malloc");
goto end;
}
BIO_printf(out, "EC_GROUP *get_ec_group_%d(void)\n{\n", len); BIO_printf(out, "EC_GROUP *get_ec_group_%d(void)\n{\n", len);
print_bignum_var(out, ec_p, "ec_p", len, buffer); print_bignum_var(out, ec_p, "ec_p", len, buffer);

View File

@ -313,13 +313,8 @@ int enc_main(int argc, char **argv)
if (verbose) if (verbose)
BIO_printf(bio_err, "bufsize=%d\n", bsize); BIO_printf(bio_err, "bufsize=%d\n", bsize);
strbuf = OPENSSL_malloc(SIZE); strbuf = app_malloc(SIZE, "strbuf");
buff = OPENSSL_malloc(EVP_ENCODE_LENGTH(bsize)); buff = app_malloc(EVP_ENCODE_LENGTH(bsize), "evp buffer");
if ((buff == NULL) || (strbuf == NULL)) {
BIO_printf(bio_err, "OPENSSL_malloc failure %ld\n",
(long)EVP_ENCODE_LENGTH(bsize));
goto end;
}
if (debug) { if (debug) {
BIO_set_callback(in, BIO_debug_callback); BIO_set_callback(in, BIO_debug_callback);

View File

@ -98,9 +98,7 @@ static int append_buf(char **buf, const char *s, int *size, int step)
if (*buf == NULL) { if (*buf == NULL) {
*size = step; *size = step;
*buf = OPENSSL_malloc(*size); *buf = app_malloc(*size, "engine buffer");
if (*buf == NULL)
return 0;
**buf = '\0'; **buf = '\0';
} }
@ -211,8 +209,7 @@ static int util_verbose(ENGINE *e, int verbose, BIO *out, const char *indent)
if ((len = ENGINE_ctrl(e, ENGINE_CTRL_GET_NAME_LEN_FROM_CMD, num, if ((len = ENGINE_ctrl(e, ENGINE_CTRL_GET_NAME_LEN_FROM_CMD, num,
NULL, NULL)) <= 0) NULL, NULL)) <= 0)
goto err; goto err;
if ((name = OPENSSL_malloc(len + 1)) == NULL) name = app_malloc(len + 1, "name buffer");
goto err;
if (ENGINE_ctrl(e, ENGINE_CTRL_GET_NAME_FROM_CMD, num, name, if (ENGINE_ctrl(e, ENGINE_CTRL_GET_NAME_FROM_CMD, num, name,
NULL) <= 0) NULL) <= 0)
goto err; goto err;
@ -221,8 +218,7 @@ static int util_verbose(ENGINE *e, int verbose, BIO *out, const char *indent)
NULL, NULL)) < 0) NULL, NULL)) < 0)
goto err; goto err;
if (len > 0) { if (len > 0) {
if ((desc = OPENSSL_malloc(len + 1)) == NULL) desc = app_malloc(len + 1, "description buffer");
goto err;
if (ENGINE_ctrl(e, ENGINE_CTRL_GET_DESC_FROM_CMD, num, desc, if (ENGINE_ctrl(e, ENGINE_CTRL_GET_DESC_FROM_CMD, num, desc,
NULL) <= 0) NULL) <= 0)
goto err; goto err;

View File

@ -204,9 +204,7 @@ static char *make_config_name()
char *p; char *p;
len = strlen(t) + strlen(OPENSSL_CONF) + 2; len = strlen(t) + strlen(OPENSSL_CONF) + 2;
p = OPENSSL_malloc(len); p = app_malloc(len, "config filename buffer");
if (p == NULL)
return NULL;
BUF_strlcpy(p, t, len); BUF_strlcpy(p, t, len);
#ifndef OPENSSL_SYS_VMS #ifndef OPENSSL_SYS_VMS
BUF_strlcat(p, "/", len); BUF_strlcat(p, "/", len);

View File

@ -221,12 +221,9 @@ int passwd_main(int argc, char **argv)
/* no passwords on the command line */ /* no passwords on the command line */
passwd_malloc_size = pw_maxlen + 2; passwd_malloc_size = pw_maxlen + 2;
/* /* longer than necessary so that we can warn about truncation */
* longer than necessary so that we can warn about truncation passwd = passwd_malloc =
*/ app_malloc(passwd_malloc_size, "password buffer");
passwd = passwd_malloc = OPENSSL_malloc(passwd_malloc_size);
if (passwd_malloc == NULL)
goto end;
} }
if ((in == NULL) && (passwds == NULL)) { if ((in == NULL) && (passwds == NULL)) {
@ -426,9 +423,7 @@ static int do_passwd(int passed_salt, char **salt_p, char **salt_malloc_p,
# ifndef OPENSSL_NO_DES # ifndef OPENSSL_NO_DES
if (usecrypt) { if (usecrypt) {
if (*salt_malloc_p == NULL) { if (*salt_malloc_p == NULL) {
*salt_p = *salt_malloc_p = OPENSSL_malloc(3); *salt_p = *salt_malloc_p = app_malloc(3, "salt buffer");
if (*salt_malloc_p == NULL)
goto end;
} }
if (RAND_bytes((unsigned char *)*salt_p, 2) <= 0) if (RAND_bytes((unsigned char *)*salt_p, 2) <= 0)
goto end; goto end;
@ -447,9 +442,7 @@ static int do_passwd(int passed_salt, char **salt_p, char **salt_malloc_p,
int i; int i;
if (*salt_malloc_p == NULL) { if (*salt_malloc_p == NULL) {
*salt_p = *salt_malloc_p = OPENSSL_malloc(9); *salt_p = *salt_malloc_p = app_malloc(9, "salt buffer");
if (*salt_malloc_p == NULL)
goto end;
} }
if (RAND_bytes((unsigned char *)*salt_p, 8) <= 0) if (RAND_bytes((unsigned char *)*salt_p, 8) <= 0)
goto end; goto end;

View File

@ -299,13 +299,10 @@ int pkeyutl_main(int argc, char **argv)
rv = do_keyop(ctx, pkey_op, NULL, (size_t *)&buf_outlen, rv = do_keyop(ctx, pkey_op, NULL, (size_t *)&buf_outlen,
buf_in, (size_t)buf_inlen); buf_in, (size_t)buf_inlen);
if (rv > 0) { if (rv > 0) {
buf_out = OPENSSL_malloc(buf_outlen); buf_out = app_malloc(buf_outlen, "buffer output");
if (!buf_out) rv = do_keyop(ctx, pkey_op,
rv = -1; buf_out, (size_t *)&buf_outlen,
else buf_in, (size_t)buf_inlen);
rv = do_keyop(ctx, pkey_op,
buf_out, (size_t *)&buf_outlen,
buf_in, (size_t)buf_inlen);
} }
if (rv <= 0) { if (rv <= 0) {
ERR_print_errors(bio_err); ERR_print_errors(bio_err);

View File

@ -344,19 +344,14 @@ int rsa_main(int argc, char **argv)
} }
# ifndef OPENSSL_NO_RC4 # ifndef OPENSSL_NO_RC4
else if (outformat == FORMAT_NETSCAPE) { else if (outformat == FORMAT_NETSCAPE) {
unsigned char *p, *pp; unsigned char *p, *save;
int size; int size = i2d_RSA_NET(rsa, NULL, NULL, 0);
i = 1; save = p = app_malloc(size, "RSA i2d buffer");
size = i2d_RSA_NET(rsa, NULL, NULL, 0);
if ((p = OPENSSL_malloc(size)) == NULL) {
BIO_printf(bio_err, "Memory allocation failure\n");
goto end;
}
pp = p;
i2d_RSA_NET(rsa, &p, NULL, 0); i2d_RSA_NET(rsa, &p, NULL, 0);
BIO_write(out, (char *)pp, size); BIO_write(out, (char *)save, size);
OPENSSL_free(pp); OPENSSL_free(save);
i = 1;
} }
# endif # endif
else if (outformat == FORMAT_PEM) { else if (outformat == FORMAT_PEM) {

View File

@ -257,12 +257,8 @@ int rsautl_main(int argc, char **argv)
keysize = RSA_size(rsa); keysize = RSA_size(rsa);
rsa_in = OPENSSL_malloc(keysize * 2); rsa_in = app_malloc(keysize * 2, "hold rsa key");
rsa_out = OPENSSL_malloc(keysize); rsa_out = app_malloc(keysize, "output rsa key");
if (!rsa_in || !rsa_out) {
BIO_printf(bio_err, "Out of memory\n");
goto end;
}
/* Read the input data */ /* Read the input data */
rsa_inlen = BIO_read(in, rsa_in, keysize * 2); rsa_inlen = BIO_read(in, rsa_in, keysize * 2);

View File

@ -439,11 +439,7 @@ int ssl_print_curves(BIO *out, SSL *s, int noshared)
ncurves = SSL_get1_curves(s, NULL); ncurves = SSL_get1_curves(s, NULL);
if (ncurves <= 0) if (ncurves <= 0)
return 1; return 1;
curves = OPENSSL_malloc(ncurves * sizeof(int)); curves = app_malloc(ncurves * sizeof(int), "curves to print");
if (!curves) {
BIO_printf(out, "Out of memory\n");
return 0;
}
SSL_get1_curves(s, curves); SSL_get1_curves(s, curves);
BIO_puts(out, "Supported Elliptic Curves: "); BIO_puts(out, "Supported Elliptic Curves: ");
@ -955,12 +951,7 @@ int generate_cookie_callback(SSL *ssl, unsigned char *cookie,
OPENSSL_assert(0); OPENSSL_assert(0);
break; break;
} }
buffer = OPENSSL_malloc(length); buffer = app_malloc(length, "cookie generate buffer");
if (buffer == NULL) {
BIO_printf(bio_err, "out of memory\n");
return 0;
}
switch (peer.sa.sa_family) { switch (peer.sa.sa_family) {
case AF_INET: case AF_INET:
@ -1028,12 +1019,7 @@ int verify_cookie_callback(SSL *ssl, unsigned char *cookie,
OPENSSL_assert(0); OPENSSL_assert(0);
break; break;
} }
buffer = OPENSSL_malloc(length); buffer = app_malloc(length, "cookie verify buffer");
if (buffer == NULL) {
BIO_printf(bio_err, "out of memory\n");
return 0;
}
switch (peer.sa.sa_family) { switch (peer.sa.sa_family) {
case AF_INET: case AF_INET:
@ -1187,10 +1173,8 @@ void ssl_ctx_set_excert(SSL_CTX *ctx, SSL_EXCERT *exc)
static int ssl_excert_prepend(SSL_EXCERT **pexc) static int ssl_excert_prepend(SSL_EXCERT **pexc)
{ {
SSL_EXCERT *exc; SSL_EXCERT *exc = app_malloc(sizeof *exc, "prepend cert");
exc = OPENSSL_malloc(sizeof(SSL_EXCERT));
if (!exc)
return 0;
exc->certfile = NULL; exc->certfile = NULL;
exc->keyfile = NULL; exc->keyfile = NULL;
exc->chainfile = NULL; exc->chainfile = NULL;

View File

@ -385,14 +385,10 @@ static int ssl_srp_verify_param_cb(SSL *s, void *arg)
static char *ssl_give_srp_client_pwd_cb(SSL *s, void *arg) static char *ssl_give_srp_client_pwd_cb(SSL *s, void *arg)
{ {
SRP_ARG *srp_arg = (SRP_ARG *)arg; SRP_ARG *srp_arg = (SRP_ARG *)arg;
char *pass = OPENSSL_malloc(PWD_STRLEN + 1); char *pass = app_malloc(PWD_STRLEN + 1, "SRP password buffer");
PW_CB_DATA cb_tmp; PW_CB_DATA cb_tmp;
int l; int l;
if (!pass) {
BIO_printf(bio_err, "Out of memory\n");
return NULL;
}
cb_tmp.password = (char *)srp_arg->srppassin; cb_tmp.password = (char *)srp_arg->srppassin;
cb_tmp.prompt_info = "SRP user"; cb_tmp.prompt_info = "SRP user";
if ((l = password_callback(pass, PWD_STRLEN, 0, &cb_tmp)) < 0) { if ((l = password_callback(pass, PWD_STRLEN, 0, &cb_tmp)) < 0) {
@ -712,13 +708,12 @@ int s_client_main(int argc, char **argv)
verify_depth = 0; verify_depth = 0;
verify_error = X509_V_OK; verify_error = X509_V_OK;
vpm = X509_VERIFY_PARAM_new(); vpm = X509_VERIFY_PARAM_new();
cbuf = OPENSSL_malloc(BUFSIZZ); cbuf = app_malloc(BUFSIZZ, "cbuf");
sbuf = OPENSSL_malloc(BUFSIZZ); sbuf = app_malloc(BUFSIZZ, "sbuf");
mbuf = OPENSSL_malloc(BUFSIZZ); mbuf = app_malloc(BUFSIZZ, "mbuf");
cctx = SSL_CONF_CTX_new(); cctx = SSL_CONF_CTX_new();
if (vpm == NULL || cctx == NULL if (vpm == NULL || cctx == NULL) {
|| cbuf == NULL || sbuf == NULL || mbuf == NULL) {
BIO_printf(bio_err, "%s: out of memory\n", prog); BIO_printf(bio_err, "%s: out of memory\n", prog);
goto end; goto end;
} }
@ -2176,22 +2171,20 @@ static void print_stuff(BIO *bio, SSL *s, int full)
BIO_printf(bio, "Keying material exporter:\n"); BIO_printf(bio, "Keying material exporter:\n");
BIO_printf(bio, " Label: '%s'\n", keymatexportlabel); BIO_printf(bio, " Label: '%s'\n", keymatexportlabel);
BIO_printf(bio, " Length: %i bytes\n", keymatexportlen); BIO_printf(bio, " Length: %i bytes\n", keymatexportlen);
exportedkeymat = OPENSSL_malloc(keymatexportlen); exportedkeymat = app_malloc(keymatexportlen, "export key");
if (exportedkeymat != NULL) { if (!SSL_export_keying_material(s, exportedkeymat,
if (!SSL_export_keying_material(s, exportedkeymat, keymatexportlen,
keymatexportlen, keymatexportlabel,
keymatexportlabel, strlen(keymatexportlabel),
strlen(keymatexportlabel), NULL, 0, 0)) {
NULL, 0, 0)) { BIO_printf(bio, " Error\n");
BIO_printf(bio, " Error\n"); } else {
} else { BIO_printf(bio, " Keying material: ");
BIO_printf(bio, " Keying material: "); for (i = 0; i < keymatexportlen; i++)
for (i = 0; i < keymatexportlen; i++) BIO_printf(bio, "%02X", exportedkeymat[i]);
BIO_printf(bio, "%02X", exportedkeymat[i]); BIO_printf(bio, "\n");
BIO_printf(bio, "\n");
}
OPENSSL_free(exportedkeymat);
} }
OPENSSL_free(exportedkeymat);
} }
BIO_printf(bio, "---\n"); BIO_printf(bio, "---\n");
X509_free(peer); X509_free(peer);

View File

@ -447,6 +447,7 @@ static BIO_METHOD methods_ebcdic = {
ebcdic_free, ebcdic_free,
}; };
/* This struct is "unwarranted chumminess with the compiler." */
typedef struct { typedef struct {
size_t alloced; size_t alloced;
char buff[1]; char buff[1];
@ -461,9 +462,7 @@ static int ebcdic_new(BIO *bi)
{ {
EBCDIC_OUTBUFF *wbuf; EBCDIC_OUTBUFF *wbuf;
wbuf = OPENSSL_malloc(sizeof(EBCDIC_OUTBUFF) + 1024); wbuf = app_malloc(sizeof(EBCDIC_OUTBUFF) + 1024, "ebcdef wbuf");
if (!wbuf)
return 0;
wbuf->alloced = 1024; wbuf->alloced = 1024;
wbuf->buff[0] = '\0'; wbuf->buff[0] = '\0';
@ -518,9 +517,7 @@ static int ebcdic_write(BIO *b, const char *in, int inl)
num = num + num; /* double the size */ num = num + num; /* double the size */
if (num < inl) if (num < inl)
num = inl; num = inl;
wbuf = OPENSSL_malloc(sizeof(EBCDIC_OUTBUFF) + num); wbuf = app_malloc(sizeof(EBCDIC_OUTBUFF) + num, "grow ebcdic wbuf");
if (!wbuf)
return 0;
OPENSSL_free(b->ptr); OPENSSL_free(b->ptr);
wbuf->alloced = num; wbuf->alloced = num;
@ -2018,10 +2015,7 @@ static int sv_body(char *hostname, int s, int stype, unsigned char *context)
struct timeval *timeoutp; struct timeval *timeoutp;
#endif #endif
if ((buf = OPENSSL_malloc(bufsize)) == NULL) { buf = app_malloc(bufsize, "server buffer");
BIO_printf(bio_err, "out of memory\n");
goto err;
}
#ifdef FIONBIO #ifdef FIONBIO
if (s_nbio) { if (s_nbio) {
unsigned long sl = 1; unsigned long sl = 1;
@ -2542,22 +2536,20 @@ static int init_ssl_connection(SSL *con)
BIO_printf(bio_s_out, "Keying material exporter:\n"); BIO_printf(bio_s_out, "Keying material exporter:\n");
BIO_printf(bio_s_out, " Label: '%s'\n", keymatexportlabel); BIO_printf(bio_s_out, " Label: '%s'\n", keymatexportlabel);
BIO_printf(bio_s_out, " Length: %i bytes\n", keymatexportlen); BIO_printf(bio_s_out, " Length: %i bytes\n", keymatexportlen);
exportedkeymat = OPENSSL_malloc(keymatexportlen); exportedkeymat = app_malloc(keymatexportlen, "export key");
if (exportedkeymat != NULL) { if (!SSL_export_keying_material(con, exportedkeymat,
if (!SSL_export_keying_material(con, exportedkeymat, keymatexportlen,
keymatexportlen, keymatexportlabel,
keymatexportlabel, strlen(keymatexportlabel),
strlen(keymatexportlabel), NULL, 0, 0)) {
NULL, 0, 0)) { BIO_printf(bio_s_out, " Error\n");
BIO_printf(bio_s_out, " Error\n"); } else {
} else { BIO_printf(bio_s_out, " Keying material: ");
BIO_printf(bio_s_out, " Keying material: "); for (i = 0; i < keymatexportlen; i++)
for (i = 0; i < keymatexportlen; i++) BIO_printf(bio_s_out, "%02X", exportedkeymat[i]);
BIO_printf(bio_s_out, "%02X", exportedkeymat[i]); BIO_printf(bio_s_out, "\n");
BIO_printf(bio_s_out, "\n");
}
OPENSSL_free(exportedkeymat);
} }
OPENSSL_free(exportedkeymat);
} }
return (1); return (1);
@ -2593,9 +2585,7 @@ static int www_body(char *hostname, int s, int stype, unsigned char *context)
int total_bytes = 0; int total_bytes = 0;
#endif #endif
buf = OPENSSL_malloc(bufsize); buf = app_malloc(bufsize, "server www buffer");
if (buf == NULL)
return (0);
io = BIO_new(BIO_f_buffer()); io = BIO_new(BIO_f_buffer());
ssl_bio = BIO_new(BIO_f_ssl()); ssl_bio = BIO_new(BIO_f_ssl());
if ((io == NULL) || (ssl_bio == NULL)) if ((io == NULL) || (ssl_bio == NULL))
@ -2962,9 +2952,7 @@ static int rev_body(char *hostname, int s, int stype, unsigned char *context)
KSSL_CTX *kctx; KSSL_CTX *kctx;
#endif #endif
buf = OPENSSL_malloc(bufsize); buf = app_malloc(bufsize, "server rev buffer");
if (buf == NULL)
return (0);
io = BIO_new(BIO_f_buffer()); io = BIO_new(BIO_f_buffer());
ssl_bio = BIO_new(BIO_f_ssl()); ssl_bio = BIO_new(BIO_f_ssl());
if ((io == NULL) || (ssl_bio == NULL)) if ((io == NULL) || (ssl_bio == NULL))
@ -3161,15 +3149,9 @@ static simple_ssl_session *first = NULL;
static int add_session(SSL *ssl, SSL_SESSION *session) static int add_session(SSL *ssl, SSL_SESSION *session)
{ {
simple_ssl_session *sess; simple_ssl_session *sess = app_malloc(sizeof *sess, "get session");
unsigned char *p; unsigned char *p;
sess = OPENSSL_malloc(sizeof(simple_ssl_session));
if (!sess) {
BIO_printf(bio_err, "Out of memory adding to external cache\n");
return 0;
}
SSL_SESSION_get_id(session, &sess->idlen); SSL_SESSION_get_id(session, &sess->idlen);
sess->derlen = i2d_SSL_SESSION(session, NULL); sess->derlen = i2d_SSL_SESSION(session, NULL);
if (sess->derlen < 0) { if (sess->derlen < 0) {
@ -3179,8 +3161,8 @@ static int add_session(SSL *ssl, SSL_SESSION *session)
} }
sess->id = BUF_memdup(SSL_SESSION_get_id(session, NULL), sess->idlen); sess->id = BUF_memdup(SSL_SESSION_get_id(session, NULL), sess->idlen);
sess->der = OPENSSL_malloc(sess->derlen); sess->der = app_malloc(sess->derlen, "get session buffer");
if (!sess->id || !sess->der) { if (!sess->id) {
BIO_printf(bio_err, "Out of memory adding to external cache\n"); BIO_printf(bio_err, "Out of memory adding to external cache\n");
OPENSSL_free(sess->id); OPENSSL_free(sess->id);
OPENSSL_free(sess->der); OPENSSL_free(sess->der);

View File

@ -562,11 +562,7 @@ static int do_accept(int acc_sock, int *sock, char **host)
*host = NULL; *host = NULL;
/* return(0); */ /* return(0); */
} else { } else {
if ((*host = OPENSSL_malloc(strlen(h1->h_name) + 1)) == NULL) { *host = app_malloc(strlen(h1->h_name) + 1, "copy hostname");
perror("OPENSSL_malloc");
closesocket(ret);
return (0);
}
BUF_strlcpy(*host, h1->h_name, strlen(h1->h_name) + 1); BUF_strlcpy(*host, h1->h_name, strlen(h1->h_name) + 1);
h2 = GetHostByName(*host); h2 = GetHostByName(*host);

View File

@ -791,17 +791,9 @@ int speed_main(int argc, char **argv)
ecdh_doit[i] = 0; ecdh_doit[i] = 0;
#endif #endif
if ((buf_malloc = OPENSSL_malloc((int)BUFSIZE + misalign)) == NULL) { buf = buf_malloc = app_malloc((int)BUFSIZE + misalign, "input buffer");
BIO_printf(bio_err, "out of memory\n"); buf2 = buf2_malloc = app_malloc((int)BUFSIZE + misalign, "output buffer");
goto end;
}
if ((buf2_malloc = OPENSSL_malloc((int)BUFSIZE + misalign)) == NULL) {
BIO_printf(bio_err, "out of memory\n");
goto end;
}
misalign = 0; misalign = 0;
buf = buf_malloc;
buf2 = buf2_malloc;
prog = opt_init(argc, argv, speed_options); prog = opt_init(argc, argv, speed_options);
while ((o = opt_next()) != OPT_EOF) { while ((o = opt_next()) != OPT_EOF) {
@ -2452,13 +2444,8 @@ static void multiblock_speed(const EVP_CIPHER *evp_cipher)
EVP_CIPHER_CTX ctx; EVP_CIPHER_CTX ctx;
double d = 0.0; double d = 0.0;
inp = OPENSSL_malloc(mblengths[num - 1]); inp = app_malloc(mblengths[num - 1], "multiblock input buffer");
out = OPENSSL_malloc(mblengths[num - 1] + 1024); out = app_malloc(mblengths[num - 1] + 1024, "multiblock output buffer");
if (!inp || !out) {
BIO_printf(bio_err, "Out of memory\n");
goto end;
}
EVP_CIPHER_CTX_init(&ctx); EVP_CIPHER_CTX_init(&ctx);
EVP_EncryptInit_ex(&ctx, evp_cipher, NULL, no_key, no_iv); EVP_EncryptInit_ex(&ctx, evp_cipher, NULL, no_key, no_iv);
EVP_CIPHER_CTX_ctrl(&ctx, EVP_CTRL_AEAD_SET_MAC_KEY, sizeof(no_key), EVP_CIPHER_CTX_ctrl(&ctx, EVP_CTRL_AEAD_SET_MAC_KEY, sizeof(no_key),
@ -2541,7 +2528,6 @@ static void multiblock_speed(const EVP_CIPHER *evp_cipher)
fprintf(stdout, "\n"); fprintf(stdout, "\n");
} }
end:
if (inp) if (inp)
OPENSSL_free(inp); OPENSSL_free(inp);
if (out) if (out)

View File

@ -138,11 +138,7 @@ static int update_index(CA_DB *db, char **row)
char **irow; char **irow;
int i; int i;
if ((irow = OPENSSL_malloc(sizeof(char *) * (DB_NUMBER + 1))) == NULL) { irow = app_malloc(sizeof(char *) * (DB_NUMBER + 1), "row pointers");
BIO_printf(bio_err, "Memory allocation failure\n");
return 0;
}
for (i = 0; i < DB_NUMBER; i++) { for (i = 0; i < DB_NUMBER; i++) {
irow[i] = row[i]; irow[i] = row[i];
row[i] = NULL; row[i] = NULL;
@ -363,23 +359,12 @@ int srp_main(int argc, char **argv)
configfile = getenv("SSLEAY_CONF"); configfile = getenv("SSLEAY_CONF");
if (configfile == NULL) { if (configfile == NULL) {
const char *s = X509_get_default_cert_area(); const char *s = X509_get_default_cert_area();
size_t len; size_t len = strlen(s) + 1 + sizeof(CONFIG_FILE);
tofree = app_malloc(len, "config filename space");
# ifdef OPENSSL_SYS_VMS # ifdef OPENSSL_SYS_VMS
len = strlen(s) + sizeof(CONFIG_FILE);
tofree = OPENSSL_malloc(len);
if (!tofree) {
BIO_printf(bio_err, "Out of memory\n");
goto end;
}
strcpy(tofree, s); strcpy(tofree, s);
# else # else
len = strlen(s) + sizeof(CONFIG_FILE) + 1;
tofree = OPENSSL_malloc(len);
if (!tofree) {
BIO_printf(bio_err, "Out of memory\n");
goto end;
}
BUF_strlcpy(tofree, s, len); BUF_strlcpy(tofree, s, len);
BUF_strlcat(tofree, "/", len); BUF_strlcat(tofree, "/", len);
# endif # endif

View File

@ -576,10 +576,7 @@ static int create_digest(BIO *input, char *digest, const EVP_MD *md,
unsigned char buffer[4096]; unsigned char buffer[4096];
int length; int length;
*md_value = OPENSSL_malloc(md_value_len); *md_value = app_malloc(md_value_len, "digest buffer");
if (*md_value == 0)
goto err;
EVP_DigestInit(&md_ctx, md); EVP_DigestInit(&md_ctx, md);
while ((length = BIO_read(input, buffer, sizeof(buffer))) > 0) { while ((length = BIO_read(input, buffer, sizeof(buffer))) > 0) {
EVP_DigestUpdate(&md_ctx, buffer, length); EVP_DigestUpdate(&md_ctx, buffer, length);
@ -624,8 +621,7 @@ static ASN1_INTEGER *create_nonce(int bits)
OPENSSL_free(nonce->data); OPENSSL_free(nonce->data);
/* Allocate at least one byte. */ /* Allocate at least one byte. */
nonce->length = len - i; nonce->length = len - i;
if (!(nonce->data = OPENSSL_malloc(nonce->length + 1))) nonce->data = app_malloc(nonce->length + 1, "nonce buffer");
goto err;
memcpy(nonce->data, buf + i, nonce->length); memcpy(nonce->data, buf + i, nonce->length);
return nonce; return nonce;

View File

@ -130,7 +130,7 @@ char **copy_argv(int *argc, char *argv[])
*/ */
int i, count = *argc; int i, count = *argc;
char **newargv = OPENSSL_malloc((count + 1) * sizeof *newargv); char **newargv = app_malloc((count + 1) * sizeof *newargv, "argv copy");
for (i = 0; i < count; i++) for (i = 0; i < count; i++)
newargv[i] = argv[i]; newargv[i] = argv[i];

View File

@ -783,12 +783,7 @@ int x509_main(int argc, char **argv)
" */\n", buf); " */\n", buf);
len = i2d_X509(x, NULL); len = i2d_X509(x, NULL);
m = OPENSSL_malloc(len); m = app_malloc(len, "x509 name buffer");
if (!m) {
BIO_printf(bio_err, "Out of memory\n");
goto end;
}
d = (unsigned char *)m; d = (unsigned char *)m;
len = i2d_X509_NAME(X509_get_subject_name(x), &d); len = i2d_X509_NAME(X509_get_subject_name(x), &d);
print_array(out, "the_subject_name", len, (unsigned char *)m); print_array(out, "the_subject_name", len, (unsigned char *)m);
@ -976,11 +971,7 @@ static ASN1_INTEGER *x509_load_serial(char *CAfile, char *serialfile,
len = ((serialfile == NULL) len = ((serialfile == NULL)
? (strlen(CAfile) + strlen(POSTFIX) + 1) ? (strlen(CAfile) + strlen(POSTFIX) + 1)
: (strlen(serialfile))) + 1; : (strlen(serialfile))) + 1;
buf = OPENSSL_malloc(len); buf = app_malloc(len, "serial# buffer");
if (buf == NULL) {
BIO_printf(bio_err, "out of mem\n");
goto end;
}
if (serialfile == NULL) { if (serialfile == NULL) {
BUF_strlcpy(buf, CAfile, len); BUF_strlcpy(buf, CAfile, len);
for (p = buf; *p; p++) for (p = buf; *p; p++)