Improve feedback on wrong format with new print_format_error() in apps/lib/opt.c

Reviewed-by: Tomas Mraz <tmraz@fedoraproject.org>
Reviewed-by: David von Oheimb <david.von.oheimb@siemens.com>
(Merged from https://github.com/openssl/openssl/pull/4940)
This commit is contained in:
David von Oheimb 2017-12-22 19:50:17 +01:00 committed by Dr. David von Oheimb
parent 9d5aca6553
commit 51c833ac2d
3 changed files with 62 additions and 5 deletions

View File

@ -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, ...);

View File

@ -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);

View File

@ -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)
{