]> git.ipfire.org Git - thirdparty/openssl.git/commitdiff
Improve feedback on wrong format with new print_format_error() in apps/lib/opt.c
authorDavid von Oheimb <David.von.Oheimb@siemens.com>
Fri, 22 Dec 2017 18:50:17 +0000 (19:50 +0100)
committerDr. David von Oheimb <David.von.Oheimb@siemens.com>
Fri, 24 Apr 2020 16:00:24 +0000 (18:00 +0200)
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)

apps/include/opt.h
apps/lib/apps.c
apps/lib/opt.c

index 1a26f34649ef0c9c5d01f56c1d581e0e4b6a8191..1bf46215d5a276ed815d79dee773a53f5cb6a3ad 100644 (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, ...);
 
index d175e687baad1e04be8034fdf2536a77d6762950..80dafaf52d037b91081136f54b5917331755ebe3 100644 (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);
index d335d8e9db943d171bf246ec99b5eb3f668e0ec6..566de8ab7d273a7f7ba0fc441877afc4e38b8d55 100644 (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)
 {