APPS: Improve diagnostics on missing/extra args and unknown cipher/digest
authorDr. David von Oheimb <David.von.Oheimb@siemens.com>
Fri, 27 Aug 2021 13:33:18 +0000 (15:33 +0200)
committerDr. David von Oheimb <dev@ddvo.net>
Tue, 7 Dec 2021 14:26:40 +0000 (15:26 +0100)
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/16450)

45 files changed:
apps/asn1parse.c
apps/ciphers.c
apps/cmp.c
apps/cms.c
apps/crl.c
apps/crl2pkcs7.c
apps/dhparam.c
apps/dsa.c
apps/dsaparam.c
apps/ec.c
apps/ecparam.c
apps/enc.c
apps/fipsinstall.c
apps/gendsa.c
apps/genpkey.c
apps/genrsa.c
apps/include/opt.h
apps/info.c
apps/lib/opt.c
apps/list.c
apps/mac.c
apps/nseq.c
apps/ocsp.c
apps/openssl.c
apps/pkcs12.c
apps/pkcs7.c
apps/pkcs8.c
apps/pkey.c
apps/pkeyparam.c
apps/pkeyutl.c
apps/prime.c
apps/rand.c
apps/req.c
apps/rsa.c
apps/rsautl.c
apps/s_client.c
apps/s_server.c
apps/s_time.c
apps/sess_id.c
apps/smime.c
apps/spkac.c
apps/storeutl.c
apps/ts.c
apps/version.c
apps/x509.c

index f0bfd1d45fc4233f2f10d821d5dbc95f84ff8f2d..b456f13d94ea9e580307f8506321038ab23163a8 100644 (file)
@@ -159,8 +159,7 @@ int asn1parse_main(int argc, char **argv)
     }
 
     /* No extra args. */
-    argc = opt_num_rest();
-    if (argc != 0)
+    if (!opt_check_rest_arg(NULL))
         goto opthelp;
 
     if (oidfile != NULL) {
index 9c494224a11f2793000ad893554f62b590a63f94..dcf0d3fa1e13458ac4c9dd1f241234dbcbe8ce1a 100644 (file)
@@ -174,10 +174,9 @@ int ciphers_main(int argc, char **argv)
 
     /* Optional arg is cipher name. */
     argv = opt_rest();
-    argc = opt_num_rest();
-    if (argc == 1)
+    if (opt_num_rest() == 1)
         ciphers = argv[0];
-    else if (argc != 0)
+    else if (!opt_check_rest_arg(NULL))
         goto opthelp;
 
     if (convert != NULL) {
index f646e3f7bc22195297e2573b1e97c059f3e553f7..5056d841d186c454c56dcc580e0c7b72bd1078fd 100644 (file)
@@ -2552,9 +2552,7 @@ static int get_opts(int argc, char **argv)
     }
 
     /* No extra args. */
-    argc = opt_num_rest();
-    argv = opt_rest();
-    if (argc != 0)
+    if (!opt_check_rest_arg(NULL))
         goto opthelp;
     return 1;
 }
index 76c7896719376f106754b15565a0014b92959dff..18671fdc308e843747b31917aae0b90ed85e288d 100644 (file)
@@ -697,10 +697,8 @@ int cms_main(int argc, char **argv)
         if (!opt_md(digestname, &sign_md))
             goto end;
     }
-    if (ciphername != NULL) {
-        if (!opt_cipher_any(ciphername, &cipher))
-            goto end;
-    }
+    if (!opt_cipher_any(ciphername, &cipher))
+        goto end;
     if (wrapname != NULL) {
         if (!opt_cipher_any(wrapname, &wrap_cipher))
             goto end;
index 2158a107e55174cc2623c60061bc6519aca8689c..8d353ff2af6599ddc39ddaea31e982d549f01ee5 100644 (file)
@@ -209,14 +209,11 @@ int crl_main(int argc, char **argv)
     }
 
     /* No remaining args. */
-    argc = opt_num_rest();
-    if (argc != 0)
+    if (!opt_check_rest_arg(NULL))
         goto opthelp;
 
-    if (digestname != NULL) {
-        if (!opt_md(digestname, &digest))
-            goto opthelp;
-    }
+    if (!opt_md(digestname, &digest))
+        goto opthelp;
     x = load_crl(infile, informat, 1, "CRL");
     if (x == NULL)
         goto end;
index fe59e654270d07bef481a7083a5c4f54e778794b..681c60285f10b701a71cb594d17f403b42507c60 100644 (file)
@@ -104,8 +104,7 @@ int crl2pkcs7_main(int argc, char **argv)
     }
 
     /* No remaining args. */
-    argc = opt_num_rest();
-    if (argc != 0)
+    if (!opt_check_rest_arg(NULL))
         goto opthelp;
 
     if (!nocrl) {
index 0e90698cd69408c8bd84456999c6e72136b90565..9fe0eedfc207a3af1696ca41aff669d7f7d5eb64 100644 (file)
@@ -155,7 +155,7 @@ int dhparam_main(int argc, char **argv)
     if (argc == 1) {
         if (!opt_int(argv[0], &num) || num <= 0)
             goto opthelp;
-    } else if (argc != 0) {
+    } else if (!opt_check_rest_arg(NULL)) {
         goto opthelp;
     }
     if (!app_RAND_load())
index 51c02843539fadf6e4d9f3e514b672dc0cf3c005..9605ed81e7c319b3c226df49b0f613fcab00a459 100644 (file)
@@ -161,14 +161,11 @@ int dsa_main(int argc, char **argv)
     }
 
     /* No extra args. */
-    argc = opt_num_rest();
-    if (argc != 0)
+    if (!opt_check_rest_arg(NULL))
         goto opthelp;
 
-    if (ciphername != NULL) {
-        if (!opt_cipher(ciphername, &enc))
-            goto end;
-    }
+    if (!opt_cipher(ciphername, &enc))
+        goto end;
     private = pubin || pubout ? 0 : 1;
     if (text && !pubin)
         private = 1;
index 8025b8be67c830def6cd67c3cf2cf94e19b1b816..08f912340af7b90b0160b855c8d735e39b18dcee 100644 (file)
@@ -133,7 +133,7 @@ int dsaparam_main(int argc, char **argv)
     if (argc == 1) {
         if (!opt_int(argv[0], &num) || num < 0)
             goto opthelp;
-    } else if (argc != 0) {
+    } else if (!opt_check_rest_arg(NULL)) {
         goto opthelp;
     }
     if (!app_RAND_load())
index dcbef104eefeeca0fe48ca63ebd523b1d82d7d73..4573300a5e7eb3e54de49e78cea527a397198093 100644 (file)
--- a/apps/ec.c
+++ b/apps/ec.c
@@ -157,14 +157,11 @@ int ec_main(int argc, char **argv)
     }
 
     /* No extra arguments. */
-    argc = opt_num_rest();
-    if (argc != 0)
+    if (!opt_check_rest_arg(NULL))
         goto opthelp;
 
-    if (ciphername != NULL) {
-        if (!opt_cipher(ciphername, &enc))
-            goto opthelp;
-    }
+    if (!opt_cipher(ciphername, &enc))
+        goto opthelp;
     private = param_out || pubin || pubout ? 0 : 1;
     if (text && !pubin)
         private = 1;
index 12eed703de6960102336daa03939f312b47c2b3e..9910d8c17e3ffae54dfa14489702e2b1bf5def5f 100644 (file)
@@ -186,8 +186,7 @@ int ecparam_main(int argc, char **argv)
     }
 
     /* No extra args. */
-    argc = opt_num_rest();
-    if (argc != 0)
+    if (!opt_check_rest_arg(NULL))
         goto opthelp;
 
     if (!app_RAND_load())
index 3dd609856304eed7c4090e0d55e94ecd0ac94397..e71453c3c4adcb81b5bfb34ae0d08c301d0405f8 100644 (file)
@@ -289,17 +289,14 @@ int enc_main(int argc, char **argv)
     }
 
     /* No extra arguments. */
-    argc = opt_num_rest();
-    if (argc != 0)
+    if (!opt_check_rest_arg(NULL))
         goto opthelp;
     if (!app_RAND_load())
         goto end;
 
     /* Get the cipher name, either from progname (if set) or flag. */
-    if (ciphername != NULL) {
-        if (!opt_cipher(ciphername, &cipher))
-            goto opthelp;
-    }
+    if (!opt_cipher(ciphername, &cipher))
+        goto opthelp;
     if (digestname != NULL) {
         if (!opt_md(digestname, &dgst))
             goto opthelp;
index 363631112efca9ee9591d6afbf15b3405bb6185f..5af007083ac6b2df9826b0cd6befe9230db039b7 100644 (file)
@@ -382,9 +382,12 @@ opthelp:
     }
 
     /* No extra arguments. */
-    argc = opt_num_rest();
-    if (argc != 0 || (verify && in_fname == NULL))
+    if (!opt_check_rest_arg(NULL))
         goto opthelp;
+    if (verify && in_fname == NULL) {
+        BIO_printf(bio_err, "Missing -in option for -verify\n");
+        goto opthelp;
+    }
 
     if (parent_config != NULL) {
         /* Test that a parent config can load the module */
index e5c9bc22adaa209940a69a92aaffbfb8d3268dff..b9bc2f502b33f538e165511394aea7e05cd9622e 100644 (file)
@@ -101,19 +101,16 @@ int gendsa_main(int argc, char **argv)
     }
 
     /* One argument, the params file. */
-    argc = opt_num_rest();
-    argv = opt_rest();
-    if (argc != 1)
+    if (!opt_check_rest_arg("params file"))
         goto opthelp;
+    argv = opt_rest();
     dsaparams = argv[0];
 
     if (!app_RAND_load())
         goto end;
 
-    if (ciphername != NULL) {
-        if (!opt_cipher(ciphername, &enc))
-            goto end;
-    }
+    if (!opt_cipher(ciphername, &enc))
+        goto end;
     private = 1;
 
     if (!app_passwd(NULL, passoutarg, NULL, &passout)) {
index d00754eeaca09fb62e37f08e4a5f211435cd5c90..7f70a6baa28f3e19076cb7c3dbc7092fc38f0d8d 100644 (file)
@@ -139,8 +139,7 @@ int genpkey_main(int argc, char **argv)
     }
 
     /* No extra arguments. */
-    argc = opt_num_rest();
-    if (argc != 0)
+    if (!opt_check_rest_arg(NULL))
         goto opthelp;
 
     /* Fetch cipher, etc. */
@@ -163,9 +162,12 @@ int genpkey_main(int argc, char **argv)
             goto end;
         }
     }
-    if (ciphername != NULL)
-        if (!opt_cipher(ciphername, &cipher) || do_param == 1)
-            goto opthelp;
+    if (!opt_cipher(ciphername, &cipher))
+        goto opthelp;
+    if (ciphername != NULL && do_param == 1) {
+        BIO_printf(bio_err, "Cannot use cipher with -genparam option\n");
+        goto opthelp;
+    }
 
     private = do_param ? 0 : 1;
 
index e709ea38ce9933a74b187c0e525b039b53c4128a..1a6c67380f7ed13d66a3e89e0828563a0196f424 100644 (file)
@@ -157,8 +157,7 @@ opthelp:
                        "Warning: It is not recommended to use more than %d bit for RSA keys.\n"
                        "         Your key size is %d! Larger key size may behave not as expected.\n",
                        OPENSSL_RSA_MAX_MODULUS_BITS, num);
-    } else if (argc > 0) {
-        BIO_printf(bio_err, "Extra arguments given.\n");
+    } else if (!opt_check_rest_arg(NULL)) {
         goto opthelp;
     }
 
@@ -166,10 +165,8 @@ opthelp:
         goto end;
 
     private = 1;
-    if (ciphername != NULL) {
-        if (!opt_cipher(ciphername, &enc))
-            goto end;
-    }
+    if (!opt_cipher(ciphername, &enc))
+        goto end;
     if (!app_passwd(NULL, passoutarg, NULL, &passout)) {
         BIO_printf(bio_err, "Error getting password\n");
         goto end;
index 4f83a0ed53c939c6e2dc5be5fd5ffc04de3694a3..9493901c44d377fe2b6730b655ff5b06d195a627 100644 (file)
@@ -368,6 +368,7 @@ char *opt_unknown(void);
 int opt_cipher(const char *name, EVP_CIPHER **cipherp);
 int opt_cipher_any(const char *name, EVP_CIPHER **cipherp);
 int opt_cipher_silent(const char *name, EVP_CIPHER **cipherp);
+int opt_check_md(const char *name);
 int opt_md(const char *name, EVP_MD **mdp);
 int opt_md_silent(const char *name, EVP_MD **mdp);
 
@@ -392,6 +393,7 @@ int opt_provider_option_given(void);
 
 char **opt_rest(void);
 int opt_num_rest(void);
+int opt_check_rest_arg(const char *expected);
 
 /* Returns non-zero if legacy paths are still available */
 int opt_legacy_okay(void);
index c68603652f218b6976a47e55f8697ca9e1e02496..befc62dac10470e5b53455b494ead25b40c48e4e 100644 (file)
@@ -86,7 +86,7 @@ opthelp:
             break;
         }
     }
-    if (opt_num_rest() != 0)
+    if (!opt_check_rest_arg(NULL))
         goto opthelp;
     if (dirty > 1) {
         BIO_printf(bio_err, "%s: Only one item allowed\n", prog);
index 157367982d2fa9f71a596d927aceda38e5fe35e0..3925ec96c3d275f8db6304b547c0e6ddfba6ab16 100644 (file)
@@ -399,8 +399,10 @@ int opt_cipher_any(const char *name, EVP_CIPHER **cipherp)
 {
     int ret;
 
+    if (name == NULL)
+         return 1;
     if ((ret = opt_cipher_silent(name, cipherp)) == 0)
-        opt_printf_stderr("%s: Unknown cipher: %s\n", prog, name);
+        opt_printf_stderr("%s: Unknown option or cipher: %s\n", prog, name);
     return ret;
 }
 
@@ -410,6 +412,8 @@ int opt_cipher(const char *name, EVP_CIPHER **cipherp)
      unsigned long int flags;
      EVP_CIPHER *c = NULL;
 
+    if (name == NULL)
+         return 1;
      if (opt_cipher_any(name, &c)) {
         mode = EVP_CIPHER_get_mode(c);
         flags = EVP_CIPHER_get_flags(c);
@@ -454,12 +458,22 @@ int opt_md(const char *name, EVP_MD **mdp)
 {
     int ret;
 
+    if (name == NULL)
+        return 1;
     if ((ret = opt_md_silent(name, mdp)) == 0)
-        opt_printf_stderr("%s: Unknown option or message digest: %s\n", prog,
-                          name != NULL ? name : "\"\"");
+        opt_printf_stderr("%s: Unknown option or message digest: %s\n",
+                          prog, name);
     return ret;
 }
 
+int opt_check_md(const char *name)
+{
+    if (opt_md(name, NULL))
+        return 1;
+    ERR_clear_error();
+    return 0;
+}
+
 /* Look through a list of name/value pairs. */
 int opt_pair(const char *name, const OPT_PAIR* pairs, int *result)
 {
@@ -1013,6 +1027,26 @@ int opt_num_rest(void)
     return i;
 }
 
+int opt_check_rest_arg(const char *expected)
+{
+    char *opt = *opt_rest();
+
+    if (opt == NULL || *opt == '\0') {
+        if (expected == NULL)
+            return 1;
+        opt_printf_stderr("%s: Missing argument: %s\n", prog, expected);
+        return 0;
+    } else if (expected != NULL) {
+        return 1;
+    }
+    if (opt_unknown() == NULL)
+        opt_printf_stderr("%s: Extra option: \"%s\"\n", prog, opt);
+    else
+        opt_printf_stderr("%s: Extra (unknown) options: \"%s\" \"%s\"\n",
+                          prog, opt_unknown(), opt != NULL ? opt : "");
+    return 0;
+}
+
 /* Return a string describing the parameter type. */
 static const char *valtype2param(const OPTIONS *o)
 {
index 9732d6625a05c1690a3f41f73ef401d274fdb40a..30bf2be919f6e580b03c0cda52c6971f83313f91 100644 (file)
@@ -1647,7 +1647,7 @@ opthelp:
     }
 
     /* No extra arguments. */
-    if (opt_num_rest() != 0)
+    if (!opt_check_rest_arg(NULL))
         goto opthelp;
 
     if (todo.commands)
index 08f06be867859bd760ea5212ca8dade9a7853941..8bd7ce23977bf66654eaffaff230a9b9fe08afbf 100644 (file)
@@ -137,10 +137,9 @@ opthelp:
     }
 
     /* One argument, the MAC name. */
-    argc = opt_num_rest();
-    argv = opt_rest();
-    if (argc != 1)
+    if (!opt_check_rest_arg("MAC name"))
         goto opthelp;
+    argv = opt_rest();
 
     mac = EVP_MAC_fetch(app_get0_libctx(), argv[0], app_get0_propq());
     if (mac == NULL) {
index d5524370f26c850571ebc7c2b0a79dc4cf55a555..e66b58d95705b267fa74be79369133973a1b3daf 100644 (file)
@@ -73,8 +73,7 @@ int nseq_main(int argc, char **argv)
     }
 
     /* No extra arguments. */
-    argc = opt_num_rest();
-    if (argc != 0)
+    if (!opt_check_rest_arg(NULL))
         goto opthelp;
 
     in = bio_open_default(infile, 'r', FORMAT_PEM);
index 841b5f7b8141d21f60f1c4b6aa14e3157f6f2c50..b0d030a940a2b46760331be1b84371f91da89bff 100644 (file)
@@ -535,8 +535,7 @@ int ocsp_main(int argc, char **argv)
     }
 
     /* No extra arguments. */
-    argc = opt_num_rest();
-    if (argc != 0)
+    if (!opt_check_rest_arg(NULL))
         goto opthelp;
 
     if (trailing_md) {
index d61acbbc5472d69485b9b075a60a96621bbb7c82..3497d51f4df6be3663c485414ba47e4e4342a83e 100644 (file)
@@ -357,7 +357,7 @@ int help_main(int argc, char **argv)
         new_argv[2] = NULL;
         return do_cmd(prog_init(), 2, new_argv);
     }
-    if (opt_num_rest() != 0) {
+    if (!opt_check_rest_arg(NULL)) {
         BIO_printf(bio_err, "Usage: %s\n", prog);
         return 1;
     }
index acc45c405a1555c7de94420109d58b2ce8d6e765..65dcdad38ac4d7207ea26aabd558454839273036 100644 (file)
@@ -356,17 +356,14 @@ int pkcs12_main(int argc, char **argv)
     }
 
     /* No extra arguments. */
-    argc = opt_num_rest();
-    if (argc != 0)
+    if (!opt_check_rest_arg(NULL))
         goto opthelp;
 
     if (!app_RAND_load())
         goto end;
 
-    if (ciphername != NULL) {
-        if (!opt_cipher_any(ciphername, &enc))
-            goto opthelp;
-    }
+    if (!opt_cipher_any(ciphername, &enc))
+        goto opthelp;
     if (export_pkcs12) {
         if ((options & INFO) != 0)
             WARN_EXPORT("info");
index ba11e8151ae931f57f569db5f625d89222556e0a..ac2dec152a46f800bf57cd43c90bdb8f2ea4ddeb 100644 (file)
@@ -111,8 +111,7 @@ int pkcs7_main(int argc, char **argv)
     }
 
     /* No extra arguments. */
-    argc = opt_num_rest();
-    if (argc != 0)
+    if (!opt_check_rest_arg(NULL))
         goto opthelp;
 
     in = bio_open_default(infile, 'r', informat);
index 6b09b909eb7a0f03d55cf737a03c2c2dfd1956ea..e3932245f39db33292dd0745aac923fec391bea5 100644 (file)
@@ -193,8 +193,7 @@ int pkcs8_main(int argc, char **argv)
     }
 
     /* No extra arguments. */
-    argc = opt_num_rest();
-    if (argc != 0)
+    if (!opt_check_rest_arg(NULL))
         goto opthelp;
 
     private = 1;
index fb3899b08ec841418ecf52d9d601652e522aad98..41a4c29897a611aa72b5ecdb3d33f2ce7089a962 100644 (file)
@@ -171,8 +171,7 @@ int pkey_main(int argc, char **argv)
     }
 
     /* No extra arguments. */
-    argc = opt_num_rest();
-    if (argc != 0)
+    if (!opt_check_rest_arg(NULL))
         goto opthelp;
 
     if (text && text_pub)
@@ -190,10 +189,8 @@ int pkey_main(int argc, char **argv)
 
     private = (!noout && !pubout) || (text && !text_pub);
 
-    if (ciphername != NULL) {
-        if (!opt_cipher(ciphername, &cipher))
-            goto opthelp;
-    }
+    if (!opt_cipher(ciphername, &cipher))
+        goto opthelp;
     if (cipher == NULL) {
         if (passoutarg != NULL)
             BIO_printf(bio_err,
index 45647341ce874edd4a17795d1f4c4b36b8e6f3de..3722be4bf67bd7f3a4cb5486307a71fd2fa48352 100644 (file)
@@ -91,8 +91,7 @@ int pkeyparam_main(int argc, char **argv)
     }
 
     /* No extra arguments. */
-    argc = opt_num_rest();
-    if (argc != 0)
+    if (!opt_check_rest_arg(NULL))
         goto opthelp;
 
     in = bio_open_default(infile, 'r', FORMAT_PEM);
index d7290f7d48cc81e4ed061269fa80a6ba045db1c9..9e18dfc0e926dcdf00628408ad3d5746a8dfd1d1 100644 (file)
@@ -253,8 +253,7 @@ int pkeyutl_main(int argc, char **argv)
     }
 
     /* No extra arguments. */
-    argc = opt_num_rest();
-    if (argc != 0)
+    if (!opt_check_rest_arg(NULL))
         goto opthelp;
 
     if (!app_RAND_load())
index e269493d5cd7b0df8f7d425fe6a4d3e9e3e3217a..190254d90e4c386adbd960030c68bf981680afd7 100644 (file)
@@ -83,12 +83,12 @@ opthelp:
     }
 
     /* Optional arguments are numbers to check. */
+    if (generate && !opt_check_rest_arg(NULL))
+        goto opthelp;
     argc = opt_num_rest();
     argv = opt_rest();
-    if (generate) {
-        if (argc != 0)
-            goto opthelp;
-    } else if (argc == 0) {
+    if (!generate && argc == 0) {
+        BIO_printf(bio_err, "Missing number (s) to check\n");
         goto opthelp;
     }
 
index cbf495d5bc5378278991a6993b61841b42f37a43..f99c91dbbf45b73375c6027d81cdfd16b690e4b4 100644 (file)
@@ -95,7 +95,7 @@ int rand_main(int argc, char **argv)
     if (argc == 1) {
         if (!opt_int(argv[0], &num) || num <= 0)
             goto opthelp;
-    } else if (argc != 0) {
+    } else if (!opt_check_rest_arg(NULL)) {
         goto opthelp;
     }
 
index 274f839902458732173f014c19bbee25e4a4ffae..36ac4938078b3c5ab97c6f59075dd176221bbea0 100644 (file)
@@ -241,7 +241,6 @@ int req_main(int argc, char **argv)
     X509 *new_x509 = NULL, *CAcert = NULL;
     X509_REQ *req = NULL;
     EVP_CIPHER *cipher = NULL;
-    EVP_MD *md = NULL;
     int ext_copy = EXT_COPY_UNSET;
     BIO *addext_bio = NULL;
     char *extsect = NULL;
@@ -473,8 +472,7 @@ int req_main(int argc, char **argv)
     }
 
     /* No extra arguments. */
-    argc = opt_num_rest();
-    if (argc != 0)
+    if (!opt_check_rest_arg(NULL))
         goto opthelp;
 
     if (!app_RAND_load())
@@ -533,11 +531,8 @@ int req_main(int argc, char **argv)
 
     /* Check that any specified digest is fetchable */
     if (digest != NULL) {
-        if (!opt_md(digest, &md)) {
-            ERR_clear_error();
+        if (!opt_check_md(digest))
             goto opthelp;
-        }
-        EVP_MD_free(md);
     } else {
         /* No digest specified, default to configuration */
         p = NCONF_get_string(req_conf, section, "default_md");
index 05a091ce4be0f397d265f2f45c870edace997973..fb73173428cdb6eedd2ba687b6256b8e90827ab7 100644 (file)
@@ -217,14 +217,11 @@ int rsa_main(int argc, char **argv)
     }
 
     /* No extra arguments. */
-    argc = opt_num_rest();
-    if (argc != 0)
+    if (!opt_check_rest_arg(NULL))
         goto opthelp;
 
-    if (ciphername != NULL) {
-        if (!opt_cipher(ciphername, &enc))
-            goto opthelp;
-    }
+    if (!opt_cipher(ciphername, &enc))
+        goto opthelp;
     private = (text && !pubin) || (!pubout && !noout) ? 1 : 0;
 
     if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
index ae0206014d82face05d91230c4618109ddb7779c..c428bf18b4fae78379bfd5c04ffea60820908f4a 100644 (file)
@@ -169,8 +169,7 @@ int rsautl_main(int argc, char **argv)
     }
 
     /* No extra arguments. */
-    argc = opt_num_rest();
-    if (argc != 0)
+    if (!opt_check_rest_arg(NULL))
         goto opthelp;
 
     if (!app_RAND_load())
index e0748496de3efaaa254e69ce68804f694dc1a206..b905fbd3ec93a207546246d7233703c7b568c1da 100644 (file)
@@ -1473,8 +1473,7 @@ int s_client_main(int argc, char **argv)
     }
 
     /* Optional argument is connect string if -connect not used. */
-    argc = opt_num_rest();
-    if (argc == 1) {
+    if (opt_num_rest() == 1) {
         /* Don't allow -connect and a separate argument. */
         if (connectstr != NULL) {
             BIO_printf(bio_err,
@@ -1484,7 +1483,7 @@ int s_client_main(int argc, char **argv)
         }
         connect_type = use_inet;
         freeandcopy(&connectstr, *opt_rest());
-    } else if (argc != 0) {
+    } else if (!opt_check_rest_arg(NULL)) {
         goto opthelp;
     }
     if (!app_RAND_load())
index d60a1f3c852fbbb830d227c64d1cded6121db935..6b0e013ca78af0d6bb3cc687edc0e8a30f1a1c93 100644 (file)
@@ -1648,8 +1648,7 @@ int s_server_main(int argc, char *argv[])
     }
 
     /* No extra arguments. */
-    argc = opt_num_rest();
-    if (argc != 0)
+    if (!opt_check_rest_arg(NULL))
         goto opthelp;
 
     if (!app_RAND_load())
index 1a58e19de53fe7eeaa3c469df6aa54534e32ca35..1c6ed78b2cfd456de4eeaa01d3228be858809b06 100644 (file)
@@ -234,8 +234,7 @@ int s_time_main(int argc, char **argv)
     }
 
     /* No extra arguments. */
-    argc = opt_num_rest();
-    if (argc != 0)
+    if (!opt_check_rest_arg(NULL))
         goto opthelp;
 
     if (cipher == NULL)
index 714c0f77877e17894349aad659c11631e1e326d2..54b3d055634b0ec8291383fd3784f8cdfcd84b20 100644 (file)
@@ -98,8 +98,7 @@ int sess_id_main(int argc, char **argv)
     }
 
     /* No extra arguments. */
-    argc = opt_num_rest();
-    if (argc != 0)
+    if (!opt_check_rest_arg(NULL))
         goto opthelp;
 
     x = load_sess_id(infile, informat);
index a2ff0b5be75c834894be863a74155e1f09b39f68..6cf6ab3a452d864708f360ac1f8abe82eecb565c 100644 (file)
@@ -366,10 +366,8 @@ int smime_main(int argc, char **argv)
         if (!opt_md(digestname, &sign_md))
             goto opthelp;
     }
-    if (ciphername != NULL) {
-        if (!opt_cipher_any(ciphername, &cipher))
+    if (!opt_cipher_any(ciphername, &cipher))
             goto opthelp;
-    }
     if (!(operation & SMIME_SIGNERS) && (skkeys != NULL || sksigners != NULL)) {
         BIO_puts(bio_err, "Multiple signers or keys not allowed\n");
         goto opthelp;
index d92be7d6450ea19ac695f415a39f319234620dc5..b389d9afce21632082e56e1eea6be40583f5979b 100644 (file)
@@ -133,8 +133,7 @@ int spkac_main(int argc, char **argv)
     }
 
     /* No extra arguments. */
-    argc = opt_num_rest();
-    if (argc != 0)
+    if (!opt_check_rest_arg(NULL))
         goto opthelp;
 
     if (!app_passwd(passinarg, NULL, &passin, NULL)) {
index 1368caae92df17d74a8e72d22b535680b895e684..8d1ce3cea338393bcf9f5a203b18df7f054f8f9e 100644 (file)
@@ -258,15 +258,12 @@ int storeutl_main(int argc, char *argv[])
     }
 
     /* One argument, the URI */
-    argc = opt_num_rest();
-    argv = opt_rest();
-    if (argc != 1)
+    if (!opt_check_rest_arg("URI"))
         goto opthelp;
+    argv = opt_rest();
 
-    if (digestname != NULL) {
-        if (!opt_md(digestname, &digest))
-            goto opthelp;
-    }
+    if (!opt_md(digestname, &digest))
+        goto opthelp;
 
     if (criterion != 0) {
         switch (criterion) {
index e65d223348d6d4605b979f0e5e3c0e54f43a8ac0..8e58ef00b4d66500228d9a6676770c9d62bb88cc 100644 (file)
--- a/apps/ts.c
+++ b/apps/ts.c
@@ -204,8 +204,10 @@ int ts_main(int argc, char **argv)
         case OPT_QUERY:
         case OPT_REPLY:
         case OPT_VERIFY:
-            if (mode != OPT_ERR)
+            if (mode != OPT_ERR) {
+                BIO_printf(bio_err, "%s: Must give only one of -query, -reply, or -verify\n", prog);
                 goto opthelp;
+            }
             mode = o;
             break;
         case OPT_DATA:
@@ -288,17 +290,18 @@ int ts_main(int argc, char **argv)
     }
 
     /* No extra arguments. */
-    argc = opt_num_rest();
-    if (argc != 0 || mode == OPT_ERR)
+    if (!opt_check_rest_arg(NULL))
+        goto opthelp;
+    if (mode == OPT_ERR) {
+        BIO_printf(bio_err, "%s: Must give one of -query, -reply, or -verify\n", prog);
         goto opthelp;
+    }
 
     if (!app_RAND_load())
         goto end;
 
-    if (digestname != NULL) {
-        if (!opt_md(digestname, &md))
-            goto opthelp;
-    }
+    if (!opt_md(digestname, &md))
+        goto opthelp;
     if (mode == OPT_REPLY && passin &&
         !app_passwd(passin, NULL, &password, NULL)) {
         BIO_printf(bio_err, "Error getting password.\n");
index cab17a46bf18a0f1d17af8c884d55ffec6052e52..7185e9edcd1ddba29af5d7947132e471c21d022d 100644 (file)
@@ -99,8 +99,7 @@ opthelp:
     }
 
     /* No extra arguments. */
-    argc = opt_num_rest();
-    if (argc != 0)
+    if (!opt_check_rest_arg(NULL))
         goto opthelp;
 
     if (!dirty)
index 28fa769a01ea7bcdd75c7c70b376b79899229bf8..188bc17a09ff87333f2737166afb2fa399ad09b9 100644 (file)
@@ -594,13 +594,15 @@ int x509_main(int argc, char **argv)
     }
 
     /* No extra arguments. */
-    argc = opt_num_rest();
-    if (argc != 0)
+    if (!opt_check_rest_arg(NULL))
         goto opthelp;
 
     if (!app_RAND_load())
         goto end;
 
+    if (!opt_check_md(digest))
+        goto opthelp;
+
     if (preserve_dates && days != UNSET_DAYS) {
         BIO_printf(bio_err, "Cannot use -preserve_dates with -days option\n");
         goto err;