From: David von Oheimb Date: Fri, 22 Dec 2017 18:50:17 +0000 (+0100) Subject: Improve feedback on wrong format with new print_format_error() in apps/lib/opt.c X-Git-Tag: openssl-3.0.0-alpha2~169 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=51c833ac2d46653c8124a25def4df0b3d1a832b5;p=oweals%2Fopenssl.git Improve feedback on wrong format with new print_format_error() in apps/lib/opt.c Reviewed-by: Tomas Mraz Reviewed-by: David von Oheimb (Merged from https://github.com/openssl/openssl/pull/4940) --- diff --git a/apps/include/opt.h b/apps/include/opt.h index 1a26f34649..1bf46215d5 100644 --- a/apps/include/opt.h +++ b/apps/include/opt.h @@ -342,6 +342,7 @@ char *opt_init(int ac, char **av, const OPTIONS * o); int opt_next(void); void opt_begin(void); int opt_format(const char *s, unsigned long flags, int *result); +const char *format2str(int format); int opt_int(const char *arg, int *result); int opt_ulong(const char *arg, unsigned long *result); int opt_long(const char *arg, long *result); @@ -370,6 +371,7 @@ int opt_provider(int i); void opt_help(const OPTIONS * list); void opt_print(const OPTIONS * opt, int doingparams, int width); int opt_format_error(const char *s, unsigned long flags); +void print_format_error(int format, unsigned long flags); int opt_isdir(const char *name); int opt_printf_stderr(const char *fmt, ...); diff --git a/apps/lib/apps.c b/apps/lib/apps.c index d175e687ba..80dafaf52d 100644 --- a/apps/lib/apps.c +++ b/apps/lib/apps.c @@ -483,9 +483,14 @@ X509 *load_cert(const char *file, int format, const char *desc) if (!load_pkcs12(cert, desc, NULL, NULL, NULL, &x, NULL)) goto end; } else { - BIO_printf(bio_err, "bad input format specified for %s\n", cert_descrip); + print_format_error(format, +#if !defined(OPENSSL_NO_OCSP) && !defined(OPENSSL_NO_SOCK) + OPT_FMT_HTTP | +#endif + OPT_FMT_PEMDER | OPT_FMT_PKCS12); goto end; } + end: if (x == NULL && desc != NULL) { BIO_printf(bio_err, "unable to load %s\n", desc); @@ -515,7 +520,7 @@ X509_CRL *load_crl(const char *infile, int format, const char *desc) } else if (format == FORMAT_PEM) { x = PEM_read_bio_X509_CRL(in, NULL, NULL, NULL); } else { - BIO_printf(bio_err, "bad input format specified for input crl\n"); + print_format_error(format, OPT_FMT_PEMDER); goto end; } @@ -542,8 +547,10 @@ X509_REQ *load_csr(const char *file, int format, const char *desc) req = d2i_X509_REQ_bio(in, NULL); else if (format == FORMAT_PEM) req = PEM_read_bio_X509_REQ(in, NULL, NULL, NULL); - else if (desc) - BIO_printf(bio_err, "unsupported format for loading %s\n", desc); + else { + print_format_error(format, OPT_FMT_PEMDER); + goto end; + } end: if (req == NULL && desc != NULL) { @@ -613,7 +620,14 @@ EVP_PKEY *load_key(const char *file, int format, int maybe_stdin, pkey = b2i_PVK_bio(key, wrap_password_callback, &cb_data); #endif } else { - BIO_printf(bio_err, "bad input format specified for key file\n"); + print_format_error(format, OPT_FMT_PEMDER | OPT_FMT_PKCS12 +#if !defined(OPENSSL_NO_RSA) && !defined(OPENSSL_NO_DSA) && !defined (OPENSSL_NO_RC4) + | OPT_FMT_MSBLOB | FORMAT_PVK +#endif +#ifndef OPENSSL_NO_ENGINE + | OPT_FMT_ENGINE +#endif + ); goto end; } end: @@ -704,6 +718,12 @@ EVP_PKEY *load_pubkey(const char *file, int format, int maybe_stdin, } else if (format == FORMAT_MSBLOB) { pkey = b2i_PublicKey_bio(key); #endif + } else { + print_format_error(format, OPT_FMT_PEMDER +#if !defined(OPENSSL_NO_RSA) && !defined(OPENSSL_NO_DSA) + | OPT_FMT_MSBLOB +#endif + ); } end: BIO_free(key); diff --git a/apps/lib/opt.c b/apps/lib/opt.c index d335d8e9db..566de8ab7d 100644 --- a/apps/lib/opt.c +++ b/apps/lib/opt.c @@ -282,6 +282,41 @@ int opt_format(const char *s, unsigned long flags, int *result) return 1; } +/* Return string representing the given format. */ +const char *format2str(int format) +{ + switch (format) { + default: + return "(undefined)"; + case FORMAT_PEM: + return "PEM"; + case FORMAT_ASN1: + return "DER"; + case FORMAT_TEXT: + return "TEXT"; + case FORMAT_NSS: + return "NSS"; + case FORMAT_SMIME: + return "SMIME"; + case FORMAT_MSBLOB: + return "MSBLOB"; + case FORMAT_ENGINE: + return "ENGINE"; + case FORMAT_HTTP: + return "HTTP"; + case FORMAT_PKCS12: + return "P12"; + case FORMAT_PVK: + return "PVK"; + } +} + +/* Print an error message about unsuitable/unsupported format requested. */ +void print_format_error(int format, unsigned long flags) +{ + (void)opt_format_error(format2str(format), flags); +} + /* Parse a cipher name, put it in *EVP_CIPHER; return 0 on failure, else 1. */ int opt_cipher(const char *name, const EVP_CIPHER **cipherp) {