2 * Copyright 2015-2017 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 #if !defined(OPENSSL_SYS_MSDOS)
12 # include OPENSSL_UNISTD
19 #include <openssl/bio.h>
20 #include <openssl/x509v3.h>
22 #define MAX_OPT_HELP_WIDTH 30
23 const char OPT_HELP_STR[] = "--";
24 const char OPT_MORE_STR[] = "---";
33 static const OPTIONS *unknown;
34 static const OPTIONS *opts;
38 * Return the simple name of the program; removing various platform gunk.
40 #if defined(OPENSSL_SYS_WIN32)
41 char *opt_progname(const char *argv0)
47 /* find the last '/', '\' or ':' */
48 for (p = argv0 + strlen(argv0); --p > argv0;)
49 if (*p == '/' || *p == '\\' || *p == ':') {
54 /* Strip off trailing nonsense. */
57 (strcmp(&p[n - 4], ".exe") == 0 || strcmp(&p[n - 4], ".EXE") == 0))
60 /* Copy over the name, in lowercase. */
61 if (n > sizeof prog - 1)
63 for (q = prog, i = 0; i < n; i++, p++)
64 *q++ = isupper(*p) ? tolower(*p) : *p;
69 #elif defined(OPENSSL_SYS_VMS)
71 char *opt_progname(const char *argv0)
75 /* Find last special character sys:[foo.bar]openssl */
76 for (p = argv0 + strlen(argv0); --p > argv0;)
77 if (*p == ':' || *p == ']' || *p == '>') {
83 strncpy(prog, p, sizeof prog - 1);
84 prog[sizeof prog - 1] = '\0';
85 if (q != NULL && q - p < sizeof prog)
92 char *opt_progname(const char *argv0)
96 /* Could use strchr, but this is like the ones above. */
97 for (p = argv0 + strlen(argv0); --p > argv0;)
102 strncpy(prog, p, sizeof prog - 1);
103 prog[sizeof prog - 1] = '\0';
108 char *opt_getprog(void)
113 /* Set up the arg parsing. */
114 char *opt_init(int ac, char **av, const OPTIONS *o)
124 for (; o->name; ++o) {
130 if (o->name == OPT_HELP_STR || o->name == OPT_MORE_STR)
135 /* Make sure options are legit. */
136 assert(o->name[0] != '-');
137 assert(o->retval > 0);
139 case 0: case '-': case '/': case '<': case '>': case 'E': case 'F':
140 case 'M': case 'U': case 'f': case 'l': case 'n': case 'p': case 's':
147 /* Make sure there are no duplicates. */
148 for (next = o + 1; next->name; ++next) {
150 * Some compilers inline strcmp and the assert string is too long.
152 duplicated = strcmp(o->name, next->name) == 0;
156 if (o->name[0] == '\0') {
157 assert(unknown == NULL);
159 assert(unknown->valtype == 0 || unknown->valtype == '-');
165 static OPT_PAIR formats[] = {
166 {"PEM/DER", OPT_FMT_PEMDER},
167 {"pkcs12", OPT_FMT_PKCS12},
168 {"smime", OPT_FMT_SMIME},
169 {"engine", OPT_FMT_ENGINE},
170 {"msblob", OPT_FMT_MSBLOB},
171 {"netscape", OPT_FMT_NETSCAPE},
172 {"nss", OPT_FMT_NSS},
173 {"text", OPT_FMT_TEXT},
174 {"http", OPT_FMT_HTTP},
175 {"pvk", OPT_FMT_PVK},
179 /* Print an error message about a failed format parse. */
180 int opt_format_error(const char *s, unsigned long flags)
184 if (flags == OPT_FMT_PEMDER) {
185 BIO_printf(bio_err, "%s: Bad format \"%s\"; must be pem or der\n",
188 BIO_printf(bio_err, "%s: Bad format \"%s\"; must be one of:\n",
190 for (ap = formats; ap->name; ap++)
191 if (flags & ap->retval)
192 BIO_printf(bio_err, " %s\n", ap->name);
197 /* Parse a format string, put it into *result; return 0 on failure, else 1. */
198 int opt_format(const char *s, unsigned long flags, int *result)
205 if ((flags & OPT_FMT_PEMDER) == 0)
206 return opt_format_error(s, flags);
207 *result = FORMAT_ASN1;
211 if ((flags & OPT_FMT_TEXT) == 0)
212 return opt_format_error(s, flags);
213 *result = FORMAT_TEXT;
217 if ((flags & OPT_FMT_NSS) == 0)
218 return opt_format_error(s, flags);
219 if (strcmp(s, "NSS") != 0 && strcmp(s, "nss") != 0)
220 return opt_format_error(s, flags);
221 *result = FORMAT_NSS;
225 if ((flags & OPT_FMT_SMIME) == 0)
226 return opt_format_error(s, flags);
227 *result = FORMAT_SMIME;
231 if ((flags & OPT_FMT_MSBLOB) == 0)
232 return opt_format_error(s, flags);
233 *result = FORMAT_MSBLOB;
237 if ((flags & OPT_FMT_ENGINE) == 0)
238 return opt_format_error(s, flags);
239 *result = FORMAT_ENGINE;
243 if ((flags & OPT_FMT_HTTP) == 0)
244 return opt_format_error(s, flags);
245 *result = FORMAT_HTTP;
248 if ((flags & OPT_FMT_PKCS12) == 0)
249 return opt_format_error(s, flags);
250 *result = FORMAT_PKCS12;
254 if (s[1] == '\0' || strcmp(s, "PEM") == 0 || strcmp(s, "pem") == 0) {
255 if ((flags & OPT_FMT_PEMDER) == 0)
256 return opt_format_error(s, flags);
257 *result = FORMAT_PEM;
258 } else if (strcmp(s, "PVK") == 0 || strcmp(s, "pvk") == 0) {
259 if ((flags & OPT_FMT_PVK) == 0)
260 return opt_format_error(s, flags);
261 *result = FORMAT_PVK;
262 } else if (strcmp(s, "P12") == 0 || strcmp(s, "p12") == 0
263 || strcmp(s, "PKCS12") == 0 || strcmp(s, "pkcs12") == 0) {
264 if ((flags & OPT_FMT_PKCS12) == 0)
265 return opt_format_error(s, flags);
266 *result = FORMAT_PKCS12;
275 /* Parse a cipher name, put it in *EVP_CIPHER; return 0 on failure, else 1. */
276 int opt_cipher(const char *name, const EVP_CIPHER **cipherp)
278 *cipherp = EVP_get_cipherbyname(name);
279 if (*cipherp != NULL)
281 BIO_printf(bio_err, "%s: Unknown cipher %s\n", prog, name);
286 * Parse message digest name, put it in *EVP_MD; return 0 on failure, else 1.
288 int opt_md(const char *name, const EVP_MD **mdp)
290 *mdp = EVP_get_digestbyname(name);
293 BIO_printf(bio_err, "%s: Unknown digest %s\n", prog, name);
297 /* Look through a list of name/value pairs. */
298 int opt_pair(const char *name, const OPT_PAIR* pairs, int *result)
302 for (pp = pairs; pp->name; pp++)
303 if (strcmp(pp->name, name) == 0) {
304 *result = pp->retval;
307 BIO_printf(bio_err, "%s: Value must be one of:\n", prog);
308 for (pp = pairs; pp->name; pp++)
309 BIO_printf(bio_err, "\t%s\n", pp->name);
313 /* Parse an int, put it into *result; return 0 on failure, else 1. */
314 int opt_int(const char *value, int *result)
318 if (!opt_long(value, &l))
322 BIO_printf(bio_err, "%s: Value \"%s\" outside integer range\n",
329 /* Parse a long, put it into *result; return 0 on failure, else 1. */
330 int opt_long(const char *value, long *result)
337 l = strtol(value, &endp, 0);
340 || ((l == LONG_MAX || l == LONG_MIN) && errno == ERANGE)
341 || (l == 0 && errno != 0)) {
342 BIO_printf(bio_err, "%s: Can't parse \"%s\" as a number\n",
352 #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L && \
353 defined(INTMAX_MAX) && defined(UINTMAX_MAX)
355 /* Parse an intmax_t, put it into *result; return 0 on failure, else 1. */
356 int opt_imax(const char *value, intmax_t *result)
363 m = strtoimax(value, &endp, 0);
366 || ((m == INTMAX_MAX || m == INTMAX_MIN) && errno == ERANGE)
367 || (m == 0 && errno != 0)) {
368 BIO_printf(bio_err, "%s: Can't parse \"%s\" as a number\n",
378 /* Parse a uintmax_t, put it into *result; return 0 on failure, else 1. */
379 int opt_umax(const char *value, uintmax_t *result)
386 m = strtoumax(value, &endp, 0);
389 || (m == UINTMAX_MAX && errno == ERANGE)
390 || (m == 0 && errno != 0)) {
391 BIO_printf(bio_err, "%s: Can't parse \"%s\" as a number\n",
403 * Parse an unsigned long, put it into *result; return 0 on failure, else 1.
405 int opt_ulong(const char *value, unsigned long *result)
412 l = strtoul(value, &endptr, 0);
415 || ((l == ULONG_MAX) && errno == ERANGE)
416 || (l == 0 && errno != 0)) {
417 BIO_printf(bio_err, "%s: Can't parse \"%s\" as an unsigned number\n",
428 * We pass opt as an int but cast it to "enum range" so that all the
429 * items in the OPT_V_ENUM enumeration are caught; this makes -Wswitch
430 * in gcc do the right thing.
432 enum range { OPT_V_ENUM };
434 int opt_verify(int opt, X509_VERIFY_PARAM *vpm)
440 const X509_VERIFY_PARAM *vtmp;
443 assert(opt > OPT_V__FIRST);
444 assert(opt < OPT_V__LAST);
446 switch ((enum range)opt) {
451 otmp = OBJ_txt2obj(opt_arg(), 0);
453 BIO_printf(bio_err, "%s: Invalid Policy %s\n", prog, opt_arg());
456 X509_VERIFY_PARAM_add0_policy(vpm, otmp);
459 /* purpose name -> purpose index */
460 i = X509_PURPOSE_get_by_sname(opt_arg());
462 BIO_printf(bio_err, "%s: Invalid purpose %s\n", prog, opt_arg());
466 /* purpose index -> purpose object */
467 xptmp = X509_PURPOSE_get0(i);
469 /* purpose object -> purpose value */
470 i = X509_PURPOSE_get_id(xptmp);
472 if (!X509_VERIFY_PARAM_set_purpose(vpm, i)) {
474 "%s: Internal error setting purpose %s\n",
479 case OPT_V_VERIFY_NAME:
480 vtmp = X509_VERIFY_PARAM_lookup(opt_arg());
482 BIO_printf(bio_err, "%s: Invalid verify name %s\n",
486 X509_VERIFY_PARAM_set1(vpm, vtmp);
488 case OPT_V_VERIFY_DEPTH:
491 X509_VERIFY_PARAM_set_depth(vpm, i);
493 case OPT_V_VERIFY_AUTH_LEVEL:
496 X509_VERIFY_PARAM_set_auth_level(vpm, i);
499 if (!opt_imax(opt_arg(), &t))
501 if (t != (time_t)t) {
502 BIO_printf(bio_err, "%s: epoch time out of range %s\n",
506 X509_VERIFY_PARAM_set_time(vpm, (time_t)t);
508 case OPT_V_VERIFY_HOSTNAME:
509 if (!X509_VERIFY_PARAM_set1_host(vpm, opt_arg(), 0))
512 case OPT_V_VERIFY_EMAIL:
513 if (!X509_VERIFY_PARAM_set1_email(vpm, opt_arg(), 0))
516 case OPT_V_VERIFY_IP:
517 if (!X509_VERIFY_PARAM_set1_ip_asc(vpm, opt_arg()))
520 case OPT_V_IGNORE_CRITICAL:
521 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_IGNORE_CRITICAL);
523 case OPT_V_ISSUER_CHECKS:
524 /* NOP, deprecated */
526 case OPT_V_CRL_CHECK:
527 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_CRL_CHECK);
529 case OPT_V_CRL_CHECK_ALL:
530 X509_VERIFY_PARAM_set_flags(vpm,
531 X509_V_FLAG_CRL_CHECK |
532 X509_V_FLAG_CRL_CHECK_ALL);
534 case OPT_V_POLICY_CHECK:
535 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_POLICY_CHECK);
537 case OPT_V_EXPLICIT_POLICY:
538 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_EXPLICIT_POLICY);
540 case OPT_V_INHIBIT_ANY:
541 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_INHIBIT_ANY);
543 case OPT_V_INHIBIT_MAP:
544 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_INHIBIT_MAP);
546 case OPT_V_X509_STRICT:
547 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_X509_STRICT);
549 case OPT_V_EXTENDED_CRL:
550 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_EXTENDED_CRL_SUPPORT);
552 case OPT_V_USE_DELTAS:
553 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_USE_DELTAS);
555 case OPT_V_POLICY_PRINT:
556 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_NOTIFY_POLICY);
558 case OPT_V_CHECK_SS_SIG:
559 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_CHECK_SS_SIGNATURE);
561 case OPT_V_TRUSTED_FIRST:
562 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_TRUSTED_FIRST);
564 case OPT_V_SUITEB_128_ONLY:
565 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_SUITEB_128_LOS_ONLY);
567 case OPT_V_SUITEB_128:
568 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_SUITEB_128_LOS);
570 case OPT_V_SUITEB_192:
571 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_SUITEB_192_LOS);
573 case OPT_V_PARTIAL_CHAIN:
574 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_PARTIAL_CHAIN);
576 case OPT_V_NO_ALT_CHAINS:
577 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_NO_ALT_CHAINS);
579 case OPT_V_NO_CHECK_TIME:
580 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_NO_CHECK_TIME);
582 case OPT_V_ALLOW_PROXY_CERTS:
583 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_ALLOW_PROXY_CERTS);
591 * Parse the next flag (and value if specified), return 0 if done, -1 on
592 * error, otherwise the flag's retval.
602 ossl_uintmax_t umval;
604 /* Look at current arg; at end of the list? */
610 /* If word doesn't start with a -, we're done. */
614 /* Hit "--" ? We're done. */
616 if (strcmp(p, "--") == 0)
619 /* Allow -nnn and --nnn */
624 /* If we have --flag=foo, snip it off */
625 if ((arg = strchr(p, '=')) != NULL)
627 for (o = opts; o->name; ++o) {
628 /* If not this option, move on to the next one. */
629 if (strcmp(p, o->name) != 0)
632 /* If it doesn't take a value, make sure none was given. */
633 if (o->valtype == 0 || o->valtype == '-') {
636 "%s: Option -%s does not take a value\n", prog, p);
642 /* Want a value; get the next param if =foo not used. */
644 if (argv[opt_index] == NULL) {
646 "%s: Option -%s needs a value\n", prog, o->name);
649 arg = argv[opt_index++];
652 /* Syntax-check value. */
653 switch (o->valtype) {
659 if (app_isdir(arg) >= 0)
661 BIO_printf(bio_err, "%s: Not a directory: %s\n", prog, arg);
665 if (strcmp(arg, "-") == 0 || app_access(arg, R_OK) >= 0)
668 "%s: Cannot open input file %s, %s\n",
669 prog, arg, strerror(errno));
673 if (strcmp(arg, "-") == 0 || app_access(arg, W_OK) >= 0 || errno == ENOENT)
676 "%s: Cannot open output file %s, %s\n",
677 prog, arg, strerror(errno));
681 if (!opt_int(arg, &ival)
682 || (o->valtype == 'p' && ival <= 0)) {
684 "%s: Non-positive number \"%s\" for -%s\n",
690 if (!opt_imax(arg, &imval)) {
692 "%s: Invalid number \"%s\" for -%s\n",
698 if (!opt_umax(arg, &umval)) {
700 "%s: Invalid number \"%s\" for -%s\n",
706 if (!opt_long(arg, &lval)) {
708 "%s: Invalid number \"%s\" for -%s\n",
714 if (!opt_ulong(arg, &ulval)) {
716 "%s: Invalid number \"%s\" for -%s\n",
726 o->valtype == 'c' ? OPT_FMT_PDS :
727 o->valtype == 'E' ? OPT_FMT_PDE :
728 o->valtype == 'F' ? OPT_FMT_PEMDER
729 : OPT_FMT_ANY, &ival))
732 "%s: Invalid format \"%s\" for -%s\n",
737 /* Return the flag value. */
740 if (unknown != NULL) {
742 return unknown->retval;
744 BIO_printf(bio_err, "%s: Option unknown option -%s\n", prog, p);
748 /* Return the most recent flag parameter. */
754 /* Return the most recent flag. */
760 /* Return the unknown option. */
761 char *opt_unknown(void)
766 /* Return the rest of the arguments after parsing flags. */
767 char **opt_rest(void)
769 return &argv[opt_index];
772 /* How many items in remaining args? */
773 int opt_num_rest(void)
778 for (pp = opt_rest(); *pp; pp++, i++)
783 /* Return a string describing the parameter type. */
784 static const char *valtype2param(const OPTIONS *o)
786 switch (o->valtype) {
807 return "PEM|DER|ENGINE";
820 void opt_help(const OPTIONS *list)
830 /* Starts with its own help message? */
831 standard_prolog = list[0].name != OPT_HELP_STR;
833 /* Find the widest help. */
834 for (o = list; o->name; o++) {
835 if (o->name == OPT_MORE_STR)
837 i = 2 + (int)strlen(o->name);
838 if (o->valtype != '-')
839 i += 1 + strlen(valtype2param(o));
840 if (i < MAX_OPT_HELP_WIDTH && i > width)
842 assert(i < (int)sizeof start);
846 BIO_printf(bio_err, "Usage: %s [options]\nValid options are:\n",
849 /* Now let's print. */
850 for (o = list; o->name; o++) {
851 help = o->helpstr ? o->helpstr : "(No additional info)";
852 if (o->name == OPT_HELP_STR) {
853 BIO_printf(bio_err, help, prog);
858 memset(start, ' ', sizeof(start) - 1);
859 start[sizeof start - 1] = '\0';
861 if (o->name == OPT_MORE_STR) {
862 /* Continuation of previous line; pad and print. */
864 BIO_printf(bio_err, "%s %s\n", start, help);
868 /* Build up the "-flag [param]" part. */
873 p += strlen(strcpy(p, o->name));
876 if (o->valtype != '-') {
878 p += strlen(strcpy(p, valtype2param(o)));
881 if ((int)(p - start) >= MAX_OPT_HELP_WIDTH) {
883 BIO_printf(bio_err, "%s\n", start);
884 memset(start, ' ', sizeof(start));
887 BIO_printf(bio_err, "%s %s\n", start, help);