From: Dr. David von Oheimb Date: Wed, 22 Apr 2020 12:58:41 +0000 (+0200) Subject: Clean up the use of ERR_print_errors() in apps.c and in four apps X-Git-Tag: openssl-3.0.0-alpha2~167 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=01c12100f7d54db29da3fd47dc40c9d0e08c0ab0;p=oweals%2Fopenssl.git Clean up the use of ERR_print_errors() in apps.c and in four apps Also make sure that all error messages in apps.c consistently begin upper-case. Changed files: apps/lib/apps.c and apps/{req.c,s_client.c,s_server.c,x509.c} Reviewed-by: Tomas Mraz Reviewed-by: David von Oheimb (Merged from https://github.com/openssl/openssl/pull/4940) --- diff --git a/apps/lib/apps.c b/apps/lib/apps.c index e627057887..1a23ae0846 100644 --- a/apps/lib/apps.c +++ b/apps/lib/apps.c @@ -492,7 +492,7 @@ X509 *load_cert(const char *file, int format, const char *desc) end: if (x == NULL && desc != NULL) { - BIO_printf(bio_err, "unable to load %s\n", desc); + BIO_printf(bio_err, "Unable to load %s\n", desc); ERR_print_errors(bio_err); } BIO_free(cert); @@ -523,7 +523,7 @@ X509_CRL *load_crl(const char *infile, int format, const char *desc) end: if (x == NULL && desc != NULL) { - BIO_printf(bio_err, "unable to load %s\n", desc); + BIO_printf(bio_err, "Unable to load %s\n", desc); ERR_print_errors(bio_err); } BIO_free(in); @@ -548,7 +548,7 @@ X509_REQ *load_csr(const char *file, int format, const char *desc) end: if (req == NULL && desc != NULL) { - BIO_printf(bio_err, "unable to load %s\n", desc); + BIO_printf(bio_err, "Unable to load %s\n", desc); ERR_print_errors(bio_err); } BIO_free(in); @@ -566,12 +566,12 @@ EVP_PKEY *load_key(const char *file, int format, int maybe_stdin, cb_data.prompt_info = file; if (file == NULL && (!maybe_stdin || format == FORMAT_ENGINE)) { - BIO_printf(bio_err, "no keyfile specified\n"); + BIO_printf(bio_err, "No keyfile specified\n"); goto end; } if (format == FORMAT_ENGINE) { if (e == NULL) { - BIO_printf(bio_err, "no engine specified\n"); + BIO_printf(bio_err, "No engine specified\n"); } else { #ifndef OPENSSL_NO_ENGINE if (ENGINE_init(e)) { @@ -581,11 +581,11 @@ EVP_PKEY *load_key(const char *file, int format, int maybe_stdin, ENGINE_finish(e); } if (pkey == NULL && desc != NULL) { - BIO_printf(bio_err, "cannot load %s from engine\n", desc); + BIO_printf(bio_err, "Cannot load %s from engine\n", desc); ERR_print_errors(bio_err); } #else - BIO_printf(bio_err, "engines not supported\n"); + BIO_printf(bio_err, "Engines not supported\n"); #endif } goto end; @@ -627,7 +627,7 @@ EVP_PKEY *load_key(const char *file, int format, int maybe_stdin, end: BIO_free(key); if (pkey == NULL && desc != NULL) { - BIO_printf(bio_err, "unable to load %s\n", desc); + BIO_printf(bio_err, "Unable to load %s\n", desc); ERR_print_errors(bio_err); } return pkey; @@ -644,22 +644,22 @@ EVP_PKEY *load_pubkey(const char *file, int format, int maybe_stdin, cb_data.prompt_info = file; if (file == NULL && (!maybe_stdin || format == FORMAT_ENGINE)) { - BIO_printf(bio_err, "no keyfile specified\n"); + BIO_printf(bio_err, "No keyfile specified\n"); goto end; } if (format == FORMAT_ENGINE) { if (e == NULL) { - BIO_printf(bio_err, "no engine specified\n"); + BIO_printf(bio_err, "No engine specified\n"); } else { #ifndef OPENSSL_NO_ENGINE pkey = ENGINE_load_public_key(e, file, (UI_METHOD *)get_ui_method(), &cb_data); if (pkey == NULL && desc != NULL) { - BIO_printf(bio_err, "cannot load %s from engine\n", desc); + BIO_printf(bio_err, "Cannot load %s from engine\n", desc); ERR_print_errors(bio_err); } #else - BIO_printf(bio_err, "engines not supported\n"); + BIO_printf(bio_err, "Engines not supported\n"); #endif } goto end; @@ -722,7 +722,7 @@ EVP_PKEY *load_pubkey(const char *file, int format, int maybe_stdin, end: BIO_free(key); if (pkey == NULL && desc != NULL) { - BIO_printf(bio_err, "unable to load %s\n", desc); + BIO_printf(bio_err, "Unable to load %s\n", desc); ERR_print_errors(bio_err); } return pkey; @@ -744,7 +744,7 @@ static int load_certs_crls(const char *file, int format, cb_data.prompt_info = file; if (format != FORMAT_PEM) { - BIO_printf(bio_err, "bad input format specified for %s\n", desc); + BIO_printf(bio_err, "Bad input format specified for %s\n", desc); return 0; } @@ -804,7 +804,7 @@ static int load_certs_crls(const char *file, int format, *pcrls = NULL; } if (desc != NULL) { - BIO_printf(bio_err, "unable to load %s for %s\n", + BIO_printf(bio_err, "Unable to load %s for %s\n", pcerts ? "certificates" : "CRLs", desc); ERR_print_errors(bio_err); } @@ -1139,6 +1139,7 @@ X509_STORE *setup_verify(const char *CAfile, int noCAfile, ERR_clear_error(); return store; end: + ERR_print_errors(bio_err); X509_STORE_free(store); return NULL; } @@ -1166,13 +1167,13 @@ ENGINE *setup_engine(const char *engine, int debug) #ifndef OPENSSL_NO_ENGINE if (engine != NULL) { if (strcmp(engine, "auto") == 0) { - BIO_printf(bio_err, "enabling auto ENGINE support\n"); + BIO_printf(bio_err, "Enabling auto ENGINE support\n"); ENGINE_register_all_complete(); return NULL; } if ((e = ENGINE_by_id(engine)) == NULL && (e = try_load_engine(engine)) == NULL) { - BIO_printf(bio_err, "invalid engine \"%s\"\n", engine); + BIO_printf(bio_err, "Invalid engine \"%s\"\n", engine); ERR_print_errors(bio_err); return NULL; } @@ -1182,13 +1183,13 @@ ENGINE *setup_engine(const char *engine, int debug) ENGINE_ctrl_cmd(e, "SET_USER_INTERFACE", 0, (void *)get_ui_method(), 0, 1); if (!ENGINE_set_default(e, ENGINE_METHOD_ALL)) { - BIO_printf(bio_err, "can't use that engine\n"); + BIO_printf(bio_err, "Cannot use engine \"%s\"\n", ENGINE_get_id(e)); ERR_print_errors(bio_err); ENGINE_free(e); return NULL; } - BIO_printf(bio_err, "engine \"%s\" set.\n", ENGINE_get_id(e)); + BIO_printf(bio_err, "Engine \"%s\" set.\n", ENGINE_get_id(e)); } #endif return e; @@ -1267,14 +1268,13 @@ BIGNUM *load_serial(const char *serialfile, int create, ASN1_INTEGER **retai) BIO_printf(bio_err, "Out of memory\n"); } else { if (!a2i_ASN1_INTEGER(in, ai, buf, 1024)) { - BIO_printf(bio_err, "unable to load number from %s\n", + BIO_printf(bio_err, "Unable to load number from %s\n", serialfile); goto err; } ret = ASN1_INTEGER_to_BN(ai, NULL); if (ret == NULL) { - BIO_printf(bio_err, - "error converting number from bin to BIGNUM\n"); + BIO_printf(bio_err, "Error converting number from bin to BIGNUM\n"); goto err; } } @@ -1284,6 +1284,7 @@ BIGNUM *load_serial(const char *serialfile, int create, ASN1_INTEGER **retai) ai = NULL; } err: + ERR_print_errors(bio_err); BIO_free(in); ASN1_INTEGER_free(ai); return ret; @@ -1303,7 +1304,7 @@ int save_serial(const char *serialfile, const char *suffix, const BIGNUM *serial else j = strlen(serialfile) + strlen(suffix) + 1; if (j >= BSIZE) { - BIO_printf(bio_err, "file name too long\n"); + BIO_printf(bio_err, "File name too long\n"); goto err; } @@ -1318,7 +1319,6 @@ int save_serial(const char *serialfile, const char *suffix, const BIGNUM *serial } out = BIO_new_file(buf[0], "w"); if (out == NULL) { - ERR_print_errors(bio_err); goto err; } @@ -1334,6 +1334,8 @@ int save_serial(const char *serialfile, const char *suffix, const BIGNUM *serial ai = NULL; } err: + if (!ret) + ERR_print_errors(bio_err); BIO_free_all(out); ASN1_INTEGER_free(ai); return ret; @@ -1350,7 +1352,7 @@ int rotate_serial(const char *serialfile, const char *new_suffix, if (i > j) j = i; if (j + 1 >= BSIZE) { - BIO_printf(bio_err, "file name too long\n"); + BIO_printf(bio_err, "File name too long\n"); goto err; } #ifndef OPENSSL_SYS_VMS @@ -1366,19 +1368,20 @@ int rotate_serial(const char *serialfile, const char *new_suffix, #endif ) { BIO_printf(bio_err, - "unable to rename %s to %s\n", serialfile, buf[1]); + "Unable to rename %s to %s\n", serialfile, buf[1]); perror("reason"); goto err; } if (rename(buf[0], serialfile) < 0) { BIO_printf(bio_err, - "unable to rename %s to %s\n", buf[0], serialfile); + "Unable to rename %s to %s\n", buf[0], serialfile); perror("reason"); rename(buf[1], serialfile); goto err; } return 1; err: + ERR_print_errors(bio_err); return 0; } @@ -1419,17 +1422,14 @@ CA_DB *load_index(const char *dbfile, DB_ATTR *db_attr) #endif in = BIO_new_file(dbfile, "r"); - if (in == NULL) { - ERR_print_errors(bio_err); + if (in == NULL) goto err; - } #ifndef OPENSSL_NO_POSIX_IO BIO_get_fp(in, &dbfp); if (fstat(fileno(dbfp), &dbst) == -1) { ERR_raise_data(ERR_LIB_SYS, errno, "calling fstat(%s)", dbfile); - ERR_print_errors(bio_err); goto err; } #endif @@ -1466,6 +1466,7 @@ CA_DB *load_index(const char *dbfile, DB_ATTR *db_attr) #endif err: + ERR_print_errors(bio_err); NCONF_free(dbattr_conf); TXT_DB_free(tmpdb); BIO_free_all(in); @@ -1481,20 +1482,23 @@ int index_index(CA_DB *db) LHASH_HASH_FN(index_serial), LHASH_COMP_FN(index_serial))) { BIO_printf(bio_err, - "error creating serial number index:(%ld,%ld,%ld)\n", + "Error creating serial number index:(%ld,%ld,%ld)\n", db->db->error, db->db->arg1, db->db->arg2); - return 0; + goto err; } if (db->attributes.unique_subject && !TXT_DB_create_index(db->db, DB_name, index_name_qual, LHASH_HASH_FN(index_name), LHASH_COMP_FN(index_name))) { - BIO_printf(bio_err, "error creating name index:(%ld,%ld,%ld)\n", + BIO_printf(bio_err, "Error creating name index:(%ld,%ld,%ld)\n", db->db->error, db->db->arg1, db->db->arg2); - return 0; + goto err; } return 1; + err: + ERR_print_errors(bio_err); + return 0; } int save_index(const char *dbfile, const char *suffix, CA_DB *db) @@ -1505,7 +1509,7 @@ int save_index(const char *dbfile, const char *suffix, CA_DB *db) j = strlen(dbfile) + strlen(suffix); if (j + 6 >= BSIZE) { - BIO_printf(bio_err, "file name too long\n"); + BIO_printf(bio_err, "File name too long\n"); goto err; } #ifndef OPENSSL_SYS_VMS @@ -1520,7 +1524,7 @@ int save_index(const char *dbfile, const char *suffix, CA_DB *db) out = BIO_new_file(buf[0], "w"); if (out == NULL) { perror(dbfile); - BIO_printf(bio_err, "unable to open '%s'\n", dbfile); + BIO_printf(bio_err, "Unable to open '%s'\n", dbfile); goto err; } j = TXT_DB_write(out, db->db); @@ -1531,7 +1535,7 @@ int save_index(const char *dbfile, const char *suffix, CA_DB *db) out = BIO_new_file(buf[1], "w"); if (out == NULL) { perror(buf[2]); - BIO_printf(bio_err, "unable to open '%s'\n", buf[2]); + BIO_printf(bio_err, "Unable to open '%s'\n", buf[2]); goto err; } BIO_printf(out, "unique_subject = %s\n", @@ -1540,6 +1544,7 @@ int save_index(const char *dbfile, const char *suffix, CA_DB *db) return 1; err: + ERR_print_errors(bio_err); return 0; } @@ -1554,7 +1559,7 @@ int rotate_index(const char *dbfile, const char *new_suffix, if (i > j) j = i; if (j + 6 >= BSIZE) { - BIO_printf(bio_err, "file name too long\n"); + BIO_printf(bio_err, "File name too long\n"); goto err; } #ifndef OPENSSL_SYS_VMS @@ -1575,12 +1580,12 @@ int rotate_index(const char *dbfile, const char *new_suffix, && errno != ENOTDIR #endif ) { - BIO_printf(bio_err, "unable to rename %s to %s\n", dbfile, buf[1]); + BIO_printf(bio_err, "Unable to rename %s to %s\n", dbfile, buf[1]); perror("reason"); goto err; } if (rename(buf[0], dbfile) < 0) { - BIO_printf(bio_err, "unable to rename %s to %s\n", buf[0], dbfile); + BIO_printf(bio_err, "Unable to rename %s to %s\n", buf[0], dbfile); perror("reason"); rename(buf[1], dbfile); goto err; @@ -1590,14 +1595,14 @@ int rotate_index(const char *dbfile, const char *new_suffix, && errno != ENOTDIR #endif ) { - BIO_printf(bio_err, "unable to rename %s to %s\n", buf[4], buf[3]); + BIO_printf(bio_err, "Unable to rename %s to %s\n", buf[4], buf[3]); perror("reason"); rename(dbfile, buf[0]); rename(buf[1], dbfile); goto err; } if (rename(buf[2], buf[4]) < 0) { - BIO_printf(bio_err, "unable to rename %s to %s\n", buf[2], buf[4]); + BIO_printf(bio_err, "Unable to rename %s to %s\n", buf[2], buf[4]); perror("reason"); rename(buf[3], buf[4]); rename(dbfile, buf[0]); @@ -1606,6 +1611,7 @@ int rotate_index(const char *dbfile, const char *new_suffix, } return 1; err: + ERR_print_errors(bio_err); return 0; } @@ -1696,7 +1702,7 @@ X509_NAME *parse_name(const char *cp, long chtype, int canmulti) } if (*cp == '\\' && *++cp == '\0') { BIO_printf(bio_err, - "%s: escape character at end of string\n", + "%s: Escape character at end of string\n", opt_getprog()); goto err; } diff --git a/apps/req.c b/apps/req.c index 6740f21c35..cba6952cad 100644 --- a/apps/req.c +++ b/apps/req.c @@ -590,12 +590,9 @@ int req_main(int argc, char **argv) if (keyfile != NULL) { pkey = load_key(keyfile, keyform, 0, passin, e, "Private Key"); - if (pkey == NULL) { - /* load_key() has already printed an appropriate message */ + if (pkey == NULL) goto end; - } else { - app_RAND_load_conf(req_conf, section); - } + app_RAND_load_conf(req_conf, section); } if (newreq && (pkey == NULL)) { diff --git a/apps/s_client.c b/apps/s_client.c index 1596cb2f1e..eb4dbdcaa2 100644 --- a/apps/s_client.c +++ b/apps/s_client.c @@ -1714,18 +1714,14 @@ int s_client_main(int argc, char **argv) if (key_file != NULL) { key = load_key(key_file, key_format, 0, pass, e, "client certificate private key file"); - if (key == NULL) { - ERR_print_errors(bio_err); + if (key == NULL) goto end; - } } if (cert_file != NULL) { cert = load_cert(cert_file, cert_format, "client certificate file"); - if (cert == NULL) { - ERR_print_errors(bio_err); + if (cert == NULL) goto end; - } } if (chain_file != NULL) { diff --git a/apps/s_server.c b/apps/s_server.c index 8e2d73e622..23c762ba9f 100644 --- a/apps/s_server.c +++ b/apps/s_server.c @@ -1736,18 +1736,14 @@ int s_server_main(int argc, char *argv[]) if (nocert == 0) { s_key = load_key(s_key_file, s_key_format, 0, pass, engine, "server certificate private key file"); - if (s_key == NULL) { - ERR_print_errors(bio_err); + if (s_key == NULL) goto end; - } s_cert = load_cert(s_cert_file, s_cert_format, "server certificate file"); - if (s_cert == NULL) { - ERR_print_errors(bio_err); + if (s_cert == NULL) goto end; - } if (s_chain_file != NULL) { if (!load_certs(s_chain_file, &s_chain, FORMAT_PEM, NULL, "server certificate chain")) @@ -1757,18 +1753,14 @@ int s_server_main(int argc, char *argv[]) if (tlsextcbp.servername != NULL) { s_key2 = load_key(s_key_file2, s_key_format, 0, pass, engine, "second server certificate private key file"); - if (s_key2 == NULL) { - ERR_print_errors(bio_err); + if (s_key2 == NULL) goto end; - } s_cert2 = load_cert(s_cert_file2, s_cert_format, "second server certificate file"); - if (s_cert2 == NULL) { - ERR_print_errors(bio_err); + if (s_cert2 == NULL) goto end; - } } } #if !defined(OPENSSL_NO_NEXTPROTONEG) @@ -1806,10 +1798,8 @@ int s_server_main(int argc, char *argv[]) s_dkey = load_key(s_dkey_file, s_dkey_format, 0, dpass, engine, "second certificate private key file"); - if (s_dkey == NULL) { - ERR_print_errors(bio_err); + if (s_dkey == NULL) goto end; - } s_dcert = load_cert(s_dcert_file, s_dcert_format, "second server certificate file"); diff --git a/apps/x509.c b/apps/x509.c index d891b42f92..a2a52e41b1 100644 --- a/apps/x509.c +++ b/apps/x509.c @@ -574,10 +574,8 @@ int x509_main(int argc, char **argv) EVP_PKEY *pkey; req = load_csr(infile, informat, "certificate request input"); - if (req == NULL) { - ERR_print_errors(bio_err); + if (req == NULL) goto end; - } if ((pkey = X509_REQ_get0_pubkey(req)) == NULL) { BIO_printf(bio_err, "error unpacking public key\n");