Skip to content

Commit d9f0735

Browse files
committed
APPS: Improve diagnostics on missing/extra args and unknown cipher/digest
Reviewed-by: Paul Dale <[email protected]> (Merged from openssl#16450)
1 parent b0be101 commit d9f0735

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+134
-147
lines changed

Diff for: apps/asn1parse.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,7 @@ int asn1parse_main(int argc, char **argv)
159159
}
160160

161161
/* No extra args. */
162-
argc = opt_num_rest();
163-
if (argc != 0)
162+
if (!opt_check_rest_arg(NULL))
164163
goto opthelp;
165164

166165
if (oidfile != NULL) {

Diff for: apps/ciphers.c

+2-3
Original file line numberDiff line numberDiff line change
@@ -174,10 +174,9 @@ int ciphers_main(int argc, char **argv)
174174

175175
/* Optional arg is cipher name. */
176176
argv = opt_rest();
177-
argc = opt_num_rest();
178-
if (argc == 1)
177+
if (opt_num_rest() == 1)
179178
ciphers = argv[0];
180-
else if (argc != 0)
179+
else if (!opt_check_rest_arg(NULL))
181180
goto opthelp;
182181

183182
if (convert != NULL) {

Diff for: apps/cmp.c

+1-3
Original file line numberDiff line numberDiff line change
@@ -2552,9 +2552,7 @@ static int get_opts(int argc, char **argv)
25522552
}
25532553

25542554
/* No extra args. */
2555-
argc = opt_num_rest();
2556-
argv = opt_rest();
2557-
if (argc != 0)
2555+
if (!opt_check_rest_arg(NULL))
25582556
goto opthelp;
25592557
return 1;
25602558
}

Diff for: apps/cms.c

+2-4
Original file line numberDiff line numberDiff line change
@@ -697,10 +697,8 @@ int cms_main(int argc, char **argv)
697697
if (!opt_md(digestname, &sign_md))
698698
goto end;
699699
}
700-
if (ciphername != NULL) {
701-
if (!opt_cipher_any(ciphername, &cipher))
702-
goto end;
703-
}
700+
if (!opt_cipher_any(ciphername, &cipher))
701+
goto end;
704702
if (wrapname != NULL) {
705703
if (!opt_cipher_any(wrapname, &wrap_cipher))
706704
goto end;

Diff for: apps/crl.c

+3-6
Original file line numberDiff line numberDiff line change
@@ -209,14 +209,11 @@ int crl_main(int argc, char **argv)
209209
}
210210

211211
/* No remaining args. */
212-
argc = opt_num_rest();
213-
if (argc != 0)
212+
if (!opt_check_rest_arg(NULL))
214213
goto opthelp;
215214

216-
if (digestname != NULL) {
217-
if (!opt_md(digestname, &digest))
218-
goto opthelp;
219-
}
215+
if (!opt_md(digestname, &digest))
216+
goto opthelp;
220217
x = load_crl(infile, informat, 1, "CRL");
221218
if (x == NULL)
222219
goto end;

Diff for: apps/crl2pkcs7.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,7 @@ int crl2pkcs7_main(int argc, char **argv)
104104
}
105105

106106
/* No remaining args. */
107-
argc = opt_num_rest();
108-
if (argc != 0)
107+
if (!opt_check_rest_arg(NULL))
109108
goto opthelp;
110109

111110
if (!nocrl) {

Diff for: apps/dhparam.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ int dhparam_main(int argc, char **argv)
155155
if (argc == 1) {
156156
if (!opt_int(argv[0], &num) || num <= 0)
157157
goto opthelp;
158-
} else if (argc != 0) {
158+
} else if (!opt_check_rest_arg(NULL)) {
159159
goto opthelp;
160160
}
161161
if (!app_RAND_load())

Diff for: apps/dsa.c

+3-6
Original file line numberDiff line numberDiff line change
@@ -161,14 +161,11 @@ int dsa_main(int argc, char **argv)
161161
}
162162

163163
/* No extra args. */
164-
argc = opt_num_rest();
165-
if (argc != 0)
164+
if (!opt_check_rest_arg(NULL))
166165
goto opthelp;
167166

168-
if (ciphername != NULL) {
169-
if (!opt_cipher(ciphername, &enc))
170-
goto end;
171-
}
167+
if (!opt_cipher(ciphername, &enc))
168+
goto end;
172169
private = pubin || pubout ? 0 : 1;
173170
if (text && !pubin)
174171
private = 1;

Diff for: apps/dsaparam.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ int dsaparam_main(int argc, char **argv)
133133
if (argc == 1) {
134134
if (!opt_int(argv[0], &num) || num < 0)
135135
goto opthelp;
136-
} else if (argc != 0) {
136+
} else if (!opt_check_rest_arg(NULL)) {
137137
goto opthelp;
138138
}
139139
if (!app_RAND_load())

Diff for: apps/ec.c

+3-6
Original file line numberDiff line numberDiff line change
@@ -157,14 +157,11 @@ int ec_main(int argc, char **argv)
157157
}
158158

159159
/* No extra arguments. */
160-
argc = opt_num_rest();
161-
if (argc != 0)
160+
if (!opt_check_rest_arg(NULL))
162161
goto opthelp;
163162

164-
if (ciphername != NULL) {
165-
if (!opt_cipher(ciphername, &enc))
166-
goto opthelp;
167-
}
163+
if (!opt_cipher(ciphername, &enc))
164+
goto opthelp;
168165
private = param_out || pubin || pubout ? 0 : 1;
169166
if (text && !pubin)
170167
private = 1;

Diff for: apps/ecparam.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,7 @@ int ecparam_main(int argc, char **argv)
186186
}
187187

188188
/* No extra args. */
189-
argc = opt_num_rest();
190-
if (argc != 0)
189+
if (!opt_check_rest_arg(NULL))
191190
goto opthelp;
192191

193192
if (!app_RAND_load())

Diff for: apps/enc.c

+3-6
Original file line numberDiff line numberDiff line change
@@ -289,17 +289,14 @@ int enc_main(int argc, char **argv)
289289
}
290290

291291
/* No extra arguments. */
292-
argc = opt_num_rest();
293-
if (argc != 0)
292+
if (!opt_check_rest_arg(NULL))
294293
goto opthelp;
295294
if (!app_RAND_load())
296295
goto end;
297296

298297
/* Get the cipher name, either from progname (if set) or flag. */
299-
if (ciphername != NULL) {
300-
if (!opt_cipher(ciphername, &cipher))
301-
goto opthelp;
302-
}
298+
if (!opt_cipher(ciphername, &cipher))
299+
goto opthelp;
303300
if (digestname != NULL) {
304301
if (!opt_md(digestname, &dgst))
305302
goto opthelp;

Diff for: apps/fipsinstall.c

+5-2
Original file line numberDiff line numberDiff line change
@@ -382,9 +382,12 @@ int fipsinstall_main(int argc, char **argv)
382382
}
383383

384384
/* No extra arguments. */
385-
argc = opt_num_rest();
386-
if (argc != 0 || (verify && in_fname == NULL))
385+
if (!opt_check_rest_arg(NULL))
387386
goto opthelp;
387+
if (verify && in_fname == NULL) {
388+
BIO_printf(bio_err, "Missing -in option for -verify\n");
389+
goto opthelp;
390+
}
388391

389392
if (parent_config != NULL) {
390393
/* Test that a parent config can load the module */

Diff for: apps/gendsa.c

+4-7
Original file line numberDiff line numberDiff line change
@@ -101,19 +101,16 @@ int gendsa_main(int argc, char **argv)
101101
}
102102

103103
/* One argument, the params file. */
104-
argc = opt_num_rest();
105-
argv = opt_rest();
106-
if (argc != 1)
104+
if (!opt_check_rest_arg("params file"))
107105
goto opthelp;
106+
argv = opt_rest();
108107
dsaparams = argv[0];
109108

110109
if (!app_RAND_load())
111110
goto end;
112111

113-
if (ciphername != NULL) {
114-
if (!opt_cipher(ciphername, &enc))
115-
goto end;
116-
}
112+
if (!opt_cipher(ciphername, &enc))
113+
goto end;
117114
private = 1;
118115

119116
if (!app_passwd(NULL, passoutarg, NULL, &passout)) {

Diff for: apps/genpkey.c

+7-5
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,7 @@ int genpkey_main(int argc, char **argv)
139139
}
140140

141141
/* No extra arguments. */
142-
argc = opt_num_rest();
143-
if (argc != 0)
142+
if (!opt_check_rest_arg(NULL))
144143
goto opthelp;
145144

146145
/* Fetch cipher, etc. */
@@ -163,9 +162,12 @@ int genpkey_main(int argc, char **argv)
163162
goto end;
164163
}
165164
}
166-
if (ciphername != NULL)
167-
if (!opt_cipher(ciphername, &cipher) || do_param == 1)
168-
goto opthelp;
165+
if (!opt_cipher(ciphername, &cipher))
166+
goto opthelp;
167+
if (ciphername != NULL && do_param == 1) {
168+
BIO_printf(bio_err, "Cannot use cipher with -genparam option\n");
169+
goto opthelp;
170+
}
169171

170172
private = do_param ? 0 : 1;
171173

Diff for: apps/genrsa.c

+3-6
Original file line numberDiff line numberDiff line change
@@ -157,19 +157,16 @@ int genrsa_main(int argc, char **argv)
157157
"Warning: It is not recommended to use more than %d bit for RSA keys.\n"
158158
" Your key size is %d! Larger key size may behave not as expected.\n",
159159
OPENSSL_RSA_MAX_MODULUS_BITS, num);
160-
} else if (argc > 0) {
161-
BIO_printf(bio_err, "Extra arguments given.\n");
160+
} else if (!opt_check_rest_arg(NULL)) {
162161
goto opthelp;
163162
}
164163

165164
if (!app_RAND_load())
166165
goto end;
167166

168167
private = 1;
169-
if (ciphername != NULL) {
170-
if (!opt_cipher(ciphername, &enc))
171-
goto end;
172-
}
168+
if (!opt_cipher(ciphername, &enc))
169+
goto end;
173170
if (!app_passwd(NULL, passoutarg, NULL, &passout)) {
174171
BIO_printf(bio_err, "Error getting password\n");
175172
goto end;

Diff for: apps/include/opt.h

+2
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,7 @@ char *opt_unknown(void);
368368
int opt_cipher(const char *name, EVP_CIPHER **cipherp);
369369
int opt_cipher_any(const char *name, EVP_CIPHER **cipherp);
370370
int opt_cipher_silent(const char *name, EVP_CIPHER **cipherp);
371+
int opt_check_md(const char *name);
371372
int opt_md(const char *name, EVP_MD **mdp);
372373
int opt_md_silent(const char *name, EVP_MD **mdp);
373374

@@ -392,6 +393,7 @@ int opt_provider_option_given(void);
392393

393394
char **opt_rest(void);
394395
int opt_num_rest(void);
396+
int opt_check_rest_arg(const char *expected);
395397

396398
/* Returns non-zero if legacy paths are still available */
397399
int opt_legacy_okay(void);

Diff for: apps/info.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ int info_main(int argc, char **argv)
8686
break;
8787
}
8888
}
89-
if (opt_num_rest() != 0)
89+
if (!opt_check_rest_arg(NULL))
9090
goto opthelp;
9191
if (dirty > 1) {
9292
BIO_printf(bio_err, "%s: Only one item allowed\n", prog);

Diff for: apps/lib/opt.c

+37-3
Original file line numberDiff line numberDiff line change
@@ -399,8 +399,10 @@ int opt_cipher_any(const char *name, EVP_CIPHER **cipherp)
399399
{
400400
int ret;
401401

402+
if (name == NULL)
403+
return 1;
402404
if ((ret = opt_cipher_silent(name, cipherp)) == 0)
403-
opt_printf_stderr("%s: Unknown cipher: %s\n", prog, name);
405+
opt_printf_stderr("%s: Unknown option or cipher: %s\n", prog, name);
404406
return ret;
405407
}
406408

@@ -410,6 +412,8 @@ int opt_cipher(const char *name, EVP_CIPHER **cipherp)
410412
unsigned long int flags;
411413
EVP_CIPHER *c = NULL;
412414

415+
if (name == NULL)
416+
return 1;
413417
if (opt_cipher_any(name, &c)) {
414418
mode = EVP_CIPHER_get_mode(c);
415419
flags = EVP_CIPHER_get_flags(c);
@@ -454,12 +458,22 @@ int opt_md(const char *name, EVP_MD **mdp)
454458
{
455459
int ret;
456460

461+
if (name == NULL)
462+
return 1;
457463
if ((ret = opt_md_silent(name, mdp)) == 0)
458-
opt_printf_stderr("%s: Unknown option or message digest: %s\n", prog,
459-
name != NULL ? name : "\"\"");
464+
opt_printf_stderr("%s: Unknown option or message digest: %s\n",
465+
prog, name);
460466
return ret;
461467
}
462468

469+
int opt_check_md(const char *name)
470+
{
471+
if (opt_md(name, NULL))
472+
return 1;
473+
ERR_clear_error();
474+
return 0;
475+
}
476+
463477
/* Look through a list of name/value pairs. */
464478
int opt_pair(const char *name, const OPT_PAIR* pairs, int *result)
465479
{
@@ -1013,6 +1027,26 @@ int opt_num_rest(void)
10131027
return i;
10141028
}
10151029

1030+
int opt_check_rest_arg(const char *expected)
1031+
{
1032+
char *opt = *opt_rest();
1033+
1034+
if (opt == NULL || *opt == '\0') {
1035+
if (expected == NULL)
1036+
return 1;
1037+
opt_printf_stderr("%s: Missing argument: %s\n", prog, expected);
1038+
return 0;
1039+
} else if (expected != NULL) {
1040+
return 1;
1041+
}
1042+
if (opt_unknown() == NULL)
1043+
opt_printf_stderr("%s: Extra option: \"%s\"\n", prog, opt);
1044+
else
1045+
opt_printf_stderr("%s: Extra (unknown) options: \"%s\" \"%s\"\n",
1046+
prog, opt_unknown(), opt != NULL ? opt : "");
1047+
return 0;
1048+
}
1049+
10161050
/* Return a string describing the parameter type. */
10171051
static const char *valtype2param(const OPTIONS *o)
10181052
{

Diff for: apps/list.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1647,7 +1647,7 @@ int list_main(int argc, char **argv)
16471647
}
16481648

16491649
/* No extra arguments. */
1650-
if (opt_num_rest() != 0)
1650+
if (!opt_check_rest_arg(NULL))
16511651
goto opthelp;
16521652

16531653
if (todo.commands)

Diff for: apps/mac.c

+2-3
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,9 @@ int mac_main(int argc, char **argv)
137137
}
138138

139139
/* One argument, the MAC name. */
140-
argc = opt_num_rest();
141-
argv = opt_rest();
142-
if (argc != 1)
140+
if (!opt_check_rest_arg("MAC name"))
143141
goto opthelp;
142+
argv = opt_rest();
144143

145144
mac = EVP_MAC_fetch(app_get0_libctx(), argv[0], app_get0_propq());
146145
if (mac == NULL) {

Diff for: apps/nseq.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,7 @@ int nseq_main(int argc, char **argv)
7373
}
7474

7575
/* No extra arguments. */
76-
argc = opt_num_rest();
77-
if (argc != 0)
76+
if (!opt_check_rest_arg(NULL))
7877
goto opthelp;
7978

8079
in = bio_open_default(infile, 'r', FORMAT_PEM);

Diff for: apps/ocsp.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -535,8 +535,7 @@ int ocsp_main(int argc, char **argv)
535535
}
536536

537537
/* No extra arguments. */
538-
argc = opt_num_rest();
539-
if (argc != 0)
538+
if (!opt_check_rest_arg(NULL))
540539
goto opthelp;
541540

542541
if (trailing_md) {

Diff for: apps/openssl.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ int help_main(int argc, char **argv)
357357
new_argv[2] = NULL;
358358
return do_cmd(prog_init(), 2, new_argv);
359359
}
360-
if (opt_num_rest() != 0) {
360+
if (!opt_check_rest_arg(NULL)) {
361361
BIO_printf(bio_err, "Usage: %s\n", prog);
362362
return 1;
363363
}

0 commit comments

Comments
 (0)