2 * Copyright 2015-2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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 * This file is also used by the test suite. Do not #include "apps.h".
15 #include "internal/nelem.h"
17 #if !defined(OPENSSL_SYS_MSDOS)
25 #include <openssl/bio.h>
26 #include <openssl/x509v3.h>
28 #define MAX_OPT_HELP_WIDTH 30
29 const char OPT_HELP_STR[] = "-H";
30 const char OPT_MORE_STR[] = "-M";
31 const char OPT_SECTION_STR[] = "-S";
32 const char OPT_PARAM_STR[] = "-P";
41 static const OPTIONS *unknown;
42 static const OPTIONS *opts;
46 * Return the simple name of the program; removing various platform gunk.
48 #if defined(OPENSSL_SYS_WIN32)
49 char *opt_progname(const char *argv0)
55 /* find the last '/', '\' or ':' */
56 for (p = argv0 + strlen(argv0); --p > argv0;)
57 if (*p == '/' || *p == '\\' || *p == ':') {
62 /* Strip off trailing nonsense. */
65 (strcmp(&p[n - 4], ".exe") == 0 || strcmp(&p[n - 4], ".EXE") == 0))
68 /* Copy over the name, in lowercase. */
69 if (n > sizeof(prog) - 1)
71 for (q = prog, i = 0; i < n; i++, p++)
72 *q++ = tolower((unsigned char)*p);
77 #elif defined(OPENSSL_SYS_VMS)
79 char *opt_progname(const char *argv0)
83 /* Find last special character sys:[foo.bar]openssl */
84 for (p = argv0 + strlen(argv0); --p > argv0;)
85 if (*p == ':' || *p == ']' || *p == '>') {
91 strncpy(prog, p, sizeof(prog) - 1);
92 prog[sizeof(prog) - 1] = '\0';
93 if (q != NULL && q - p < sizeof(prog))
100 char *opt_progname(const char *argv0)
104 /* Could use strchr, but this is like the ones above. */
105 for (p = argv0 + strlen(argv0); --p > argv0;)
110 strncpy(prog, p, sizeof(prog) - 1);
111 prog[sizeof(prog) - 1] = '\0';
116 char *opt_getprog(void)
121 /* Set up the arg parsing. */
122 char *opt_init(int ac, char **av, const OPTIONS *o)
132 /* Check all options up until the PARAM marker (if present) */
133 for (; o->name != NULL && o->name != OPT_PARAM_STR; ++o) {
139 if (o->name == OPT_HELP_STR
140 || o->name == OPT_MORE_STR
141 || o->name == OPT_SECTION_STR)
146 /* Make sure options are legit. */
147 OPENSSL_assert(o->name[0] != '-');
148 OPENSSL_assert(o->retval > 0);
150 case 0: case '-': case '/': case '<': case '>': case 'E': case 'F':
151 case 'M': case 'U': case 'f': case 'l': case 'n': case 'p': case 's':
152 case 'u': case 'c': case ':':
158 /* Make sure there are no duplicates. */
159 for (next = o + 1; next->name; ++next) {
161 * Some compilers inline strcmp and the assert string is too long.
163 duplicated = strcmp(o->name, next->name) == 0;
164 OPENSSL_assert(!duplicated);
167 if (o->name[0] == '\0') {
168 OPENSSL_assert(unknown == NULL);
170 OPENSSL_assert(unknown->valtype == 0 || unknown->valtype == '-');
176 static OPT_PAIR formats[] = {
177 {"PEM/DER", OPT_FMT_PEMDER},
178 {"pkcs12", OPT_FMT_PKCS12},
179 {"smime", OPT_FMT_SMIME},
180 {"engine", OPT_FMT_ENGINE},
181 {"msblob", OPT_FMT_MSBLOB},
182 {"nss", OPT_FMT_NSS},
183 {"text", OPT_FMT_TEXT},
184 {"http", OPT_FMT_HTTP},
185 {"pvk", OPT_FMT_PVK},
189 /* Print an error message about a failed format parse. */
190 int opt_format_error(const char *s, unsigned long flags)
194 if (flags == OPT_FMT_PEMDER) {
195 opt_printf_stderr("%s: Bad format \"%s\"; must be pem or der\n",
198 opt_printf_stderr("%s: Bad format \"%s\"; must be one of:\n",
200 for (ap = formats; ap->name; ap++)
201 if (flags & ap->retval)
202 opt_printf_stderr(" %s\n", ap->name);
207 /* Parse a format string, put it into *result; return 0 on failure, else 1. */
208 int opt_format(const char *s, unsigned long flags, int *result)
215 if ((flags & OPT_FMT_PEMDER) == 0)
216 return opt_format_error(s, flags);
217 *result = FORMAT_ASN1;
221 if ((flags & OPT_FMT_TEXT) == 0)
222 return opt_format_error(s, flags);
223 *result = FORMAT_TEXT;
227 if ((flags & OPT_FMT_NSS) == 0)
228 return opt_format_error(s, flags);
229 if (strcmp(s, "NSS") != 0 && strcmp(s, "nss") != 0)
230 return opt_format_error(s, flags);
231 *result = FORMAT_NSS;
235 if ((flags & OPT_FMT_SMIME) == 0)
236 return opt_format_error(s, flags);
237 *result = FORMAT_SMIME;
241 if ((flags & OPT_FMT_MSBLOB) == 0)
242 return opt_format_error(s, flags);
243 *result = FORMAT_MSBLOB;
247 if ((flags & OPT_FMT_ENGINE) == 0)
248 return opt_format_error(s, flags);
249 *result = FORMAT_ENGINE;
253 if ((flags & OPT_FMT_HTTP) == 0)
254 return opt_format_error(s, flags);
255 *result = FORMAT_HTTP;
258 if ((flags & OPT_FMT_PKCS12) == 0)
259 return opt_format_error(s, flags);
260 *result = FORMAT_PKCS12;
264 if (s[1] == '\0' || strcmp(s, "PEM") == 0 || strcmp(s, "pem") == 0) {
265 if ((flags & OPT_FMT_PEMDER) == 0)
266 return opt_format_error(s, flags);
267 *result = FORMAT_PEM;
268 } else if (strcmp(s, "PVK") == 0 || strcmp(s, "pvk") == 0) {
269 if ((flags & OPT_FMT_PVK) == 0)
270 return opt_format_error(s, flags);
271 *result = FORMAT_PVK;
272 } else if (strcmp(s, "P12") == 0 || strcmp(s, "p12") == 0
273 || strcmp(s, "PKCS12") == 0 || strcmp(s, "pkcs12") == 0) {
274 if ((flags & OPT_FMT_PKCS12) == 0)
275 return opt_format_error(s, flags);
276 *result = FORMAT_PKCS12;
285 /* Return string representing the given format. */
286 const char *format2str(int format)
290 return "(undefined)";
314 /* Print an error message about unsuitable/unsupported format requested. */
315 void print_format_error(int format, unsigned long flags)
317 (void)opt_format_error(format2str(format), flags);
320 /* Parse a cipher name, put it in *EVP_CIPHER; return 0 on failure, else 1. */
321 int opt_cipher(const char *name, const EVP_CIPHER **cipherp)
323 *cipherp = EVP_get_cipherbyname(name);
324 if (*cipherp != NULL)
326 opt_printf_stderr("%s: Unknown cipher: %s\n", prog, name);
331 * Parse message digest name, put it in *EVP_MD; return 0 on failure, else 1.
333 int opt_md(const char *name, const EVP_MD **mdp)
335 *mdp = EVP_get_digestbyname(name);
338 opt_printf_stderr("%s: Unknown message digest: %s\n", prog, name);
342 /* Look through a list of name/value pairs. */
343 int opt_pair(const char *name, const OPT_PAIR* pairs, int *result)
347 for (pp = pairs; pp->name; pp++)
348 if (strcmp(pp->name, name) == 0) {
349 *result = pp->retval;
352 opt_printf_stderr("%s: Value must be one of:\n", prog);
353 for (pp = pairs; pp->name; pp++)
354 opt_printf_stderr("\t%s\n", pp->name);
358 /* Parse an int, put it into *result; return 0 on failure, else 1. */
359 int opt_int(const char *value, int *result)
363 if (!opt_long(value, &l))
367 opt_printf_stderr("%s: Value \"%s\" outside integer range\n",
374 static void opt_number_error(const char *v)
377 struct strstr_pair_st {
381 {"0x", "a hexadecimal"},
382 {"0X", "a hexadecimal"},
386 for (i = 0; i < OSSL_NELEM(b); i++) {
387 if (strncmp(v, b[i].prefix, strlen(b[i].prefix)) == 0) {
388 opt_printf_stderr("%s: Can't parse \"%s\" as %s number\n",
393 opt_printf_stderr("%s: Can't parse \"%s\" as a number\n", prog, v);
397 /* Parse a long, put it into *result; return 0 on failure, else 1. */
398 int opt_long(const char *value, long *result)
405 l = strtol(value, &endp, 0);
408 || ((l == LONG_MAX || l == LONG_MIN) && errno == ERANGE)
409 || (l == 0 && errno != 0)) {
410 opt_number_error(value);
419 #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L && \
420 defined(INTMAX_MAX) && defined(UINTMAX_MAX) && \
421 !defined(OPENSSL_NO_INTTYPES_H)
423 /* Parse an intmax_t, put it into *result; return 0 on failure, else 1. */
424 int opt_imax(const char *value, intmax_t *result)
431 m = strtoimax(value, &endp, 0);
434 || ((m == INTMAX_MAX || m == INTMAX_MIN) && errno == ERANGE)
435 || (m == 0 && errno != 0)) {
436 opt_number_error(value);
445 /* Parse a uintmax_t, put it into *result; return 0 on failure, else 1. */
446 int opt_umax(const char *value, uintmax_t *result)
453 m = strtoumax(value, &endp, 0);
456 || (m == UINTMAX_MAX && errno == ERANGE)
457 || (m == 0 && errno != 0)) {
458 opt_number_error(value);
469 * Parse an unsigned long, put it into *result; return 0 on failure, else 1.
471 int opt_ulong(const char *value, unsigned long *result)
478 l = strtoul(value, &endptr, 0);
481 || ((l == ULONG_MAX) && errno == ERANGE)
482 || (l == 0 && errno != 0)) {
483 opt_number_error(value);
493 * We pass opt as an int but cast it to "enum range" so that all the
494 * items in the OPT_V_ENUM enumeration are caught; this makes -Wswitch
495 * in gcc do the right thing.
497 enum range { OPT_V_ENUM };
499 int opt_verify(int opt, X509_VERIFY_PARAM *vpm)
505 const X509_VERIFY_PARAM *vtmp;
507 OPENSSL_assert(vpm != NULL);
508 OPENSSL_assert(opt > OPT_V__FIRST);
509 OPENSSL_assert(opt < OPT_V__LAST);
511 switch ((enum range)opt) {
516 otmp = OBJ_txt2obj(opt_arg(), 0);
518 opt_printf_stderr("%s: Invalid Policy %s\n", prog, opt_arg());
521 X509_VERIFY_PARAM_add0_policy(vpm, otmp);
524 /* purpose name -> purpose index */
525 i = X509_PURPOSE_get_by_sname(opt_arg());
527 opt_printf_stderr("%s: Invalid purpose %s\n", prog, opt_arg());
531 /* purpose index -> purpose object */
532 xptmp = X509_PURPOSE_get0(i);
534 /* purpose object -> purpose value */
535 i = X509_PURPOSE_get_id(xptmp);
537 if (!X509_VERIFY_PARAM_set_purpose(vpm, i)) {
538 opt_printf_stderr("%s: Internal error setting purpose %s\n",
543 case OPT_V_VERIFY_NAME:
544 vtmp = X509_VERIFY_PARAM_lookup(opt_arg());
546 opt_printf_stderr("%s: Invalid verify name %s\n",
550 X509_VERIFY_PARAM_set1(vpm, vtmp);
552 case OPT_V_VERIFY_DEPTH:
555 X509_VERIFY_PARAM_set_depth(vpm, i);
557 case OPT_V_VERIFY_AUTH_LEVEL:
560 X509_VERIFY_PARAM_set_auth_level(vpm, i);
563 if (!opt_imax(opt_arg(), &t))
565 if (t != (time_t)t) {
566 opt_printf_stderr("%s: epoch time out of range %s\n",
570 X509_VERIFY_PARAM_set_time(vpm, (time_t)t);
572 case OPT_V_VERIFY_HOSTNAME:
573 if (!X509_VERIFY_PARAM_set1_host(vpm, opt_arg(), 0))
576 case OPT_V_VERIFY_EMAIL:
577 if (!X509_VERIFY_PARAM_set1_email(vpm, opt_arg(), 0))
580 case OPT_V_VERIFY_IP:
581 if (!X509_VERIFY_PARAM_set1_ip_asc(vpm, opt_arg()))
584 case OPT_V_IGNORE_CRITICAL:
585 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_IGNORE_CRITICAL);
587 case OPT_V_ISSUER_CHECKS:
588 /* NOP, deprecated */
590 case OPT_V_CRL_CHECK:
591 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_CRL_CHECK);
593 case OPT_V_CRL_CHECK_ALL:
594 X509_VERIFY_PARAM_set_flags(vpm,
595 X509_V_FLAG_CRL_CHECK |
596 X509_V_FLAG_CRL_CHECK_ALL);
598 case OPT_V_POLICY_CHECK:
599 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_POLICY_CHECK);
601 case OPT_V_EXPLICIT_POLICY:
602 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_EXPLICIT_POLICY);
604 case OPT_V_INHIBIT_ANY:
605 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_INHIBIT_ANY);
607 case OPT_V_INHIBIT_MAP:
608 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_INHIBIT_MAP);
610 case OPT_V_X509_STRICT:
611 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_X509_STRICT);
613 case OPT_V_EXTENDED_CRL:
614 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_EXTENDED_CRL_SUPPORT);
616 case OPT_V_USE_DELTAS:
617 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_USE_DELTAS);
619 case OPT_V_POLICY_PRINT:
620 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_NOTIFY_POLICY);
622 case OPT_V_CHECK_SS_SIG:
623 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_CHECK_SS_SIGNATURE);
625 case OPT_V_TRUSTED_FIRST:
626 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_TRUSTED_FIRST);
628 case OPT_V_SUITEB_128_ONLY:
629 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_SUITEB_128_LOS_ONLY);
631 case OPT_V_SUITEB_128:
632 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_SUITEB_128_LOS);
634 case OPT_V_SUITEB_192:
635 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_SUITEB_192_LOS);
637 case OPT_V_PARTIAL_CHAIN:
638 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_PARTIAL_CHAIN);
640 case OPT_V_NO_ALT_CHAINS:
641 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_NO_ALT_CHAINS);
643 case OPT_V_NO_CHECK_TIME:
644 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_NO_CHECK_TIME);
646 case OPT_V_ALLOW_PROXY_CERTS:
647 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_ALLOW_PROXY_CERTS);
662 * Parse the next flag (and value if specified), return 0 if done, -1 on
663 * error, otherwise the flag's retval.
673 ossl_uintmax_t umval;
675 /* Look at current arg; at end of the list? */
681 /* If word doesn't start with a -, we're done. */
685 /* Hit "--" ? We're done. */
687 if (strcmp(p, "--") == 0)
690 /* Allow -nnn and --nnn */
695 /* If we have --flag=foo, snip it off */
696 if ((arg = strchr(p, '=')) != NULL)
698 for (o = opts; o->name; ++o) {
699 /* If not this option, move on to the next one. */
700 if (strcmp(p, o->name) != 0)
703 /* If it doesn't take a value, make sure none was given. */
704 if (o->valtype == 0 || o->valtype == '-') {
706 opt_printf_stderr("%s: Option -%s does not take a value\n",
713 /* Want a value; get the next param if =foo not used. */
715 if (argv[opt_index] == NULL) {
716 opt_printf_stderr("%s: Option -%s needs a value\n",
720 arg = argv[opt_index++];
723 /* Syntax-check value. */
724 switch (o->valtype) {
731 if (opt_isdir(arg) > 0)
733 opt_printf_stderr("%s: Not a directory: %s\n", prog, arg);
743 if (!opt_int(arg, &ival)
744 || (o->valtype == 'p' && ival <= 0)) {
745 opt_printf_stderr("%s: Non-positive number \"%s\" for -%s\n",
751 if (!opt_imax(arg, &imval)) {
752 opt_printf_stderr("%s: Invalid number \"%s\" for -%s\n",
758 if (!opt_umax(arg, &umval)) {
759 opt_printf_stderr("%s: Invalid number \"%s\" for -%s\n",
765 if (!opt_long(arg, &lval)) {
766 opt_printf_stderr("%s: Invalid number \"%s\" for -%s\n",
772 if (!opt_ulong(arg, &ulval)) {
773 opt_printf_stderr("%s: Invalid number \"%s\" for -%s\n",
783 o->valtype == 'c' ? OPT_FMT_PDS :
784 o->valtype == 'E' ? OPT_FMT_PDE :
785 o->valtype == 'F' ? OPT_FMT_PEMDER
786 : OPT_FMT_ANY, &ival))
788 opt_printf_stderr("%s: Invalid format \"%s\" for -%s\n",
793 /* Return the flag value. */
796 if (unknown != NULL) {
798 return unknown->retval;
800 opt_printf_stderr("%s: Unknown option: -%s\n", prog, p);
804 /* Return the most recent flag parameter. */
810 /* Return the most recent flag. */
816 /* Return the unknown option. */
817 char *opt_unknown(void)
822 /* Return the rest of the arguments after parsing flags. */
823 char **opt_rest(void)
825 return &argv[opt_index];
828 /* How many items in remaining args? */
829 int opt_num_rest(void)
834 for (pp = opt_rest(); *pp; pp++, i++)
839 /* Return a string describing the parameter type. */
840 static const char *valtype2param(const OPTIONS *o)
842 switch (o->valtype) {
865 return "PEM|DER|ENGINE";
878 void opt_print(const OPTIONS *o, int doingparams, int width)
884 help = o->helpstr ? o->helpstr : "(No additional info)";
885 if (o->name == OPT_HELP_STR) {
886 opt_printf_stderr(help, prog);
889 if (o->name == OPT_SECTION_STR) {
890 opt_printf_stderr("\n");
891 opt_printf_stderr(help, prog);
894 if (o->name == OPT_PARAM_STR) {
895 opt_printf_stderr("\nParameters:\n");
900 memset(start, ' ', sizeof(start) - 1);
901 start[sizeof(start) - 1] = '\0';
903 if (o->name == OPT_MORE_STR) {
904 /* Continuation of previous line; pad and print. */
906 opt_printf_stderr("%s %s\n", start, help);
910 /* Build up the "-flag [param]" part. */
916 p += strlen(strcpy(p, o->name));
919 if (o->valtype != '-') {
921 p += strlen(strcpy(p, valtype2param(o)));
924 if ((int)(p - start) >= MAX_OPT_HELP_WIDTH) {
926 opt_printf_stderr("%s\n", start);
927 memset(start, ' ', sizeof(start));
930 opt_printf_stderr("%s %s\n", start, help);
933 void opt_help(const OPTIONS *list)
936 int i, sawparams = 0, width = 5;
940 /* Starts with its own help message? */
941 standard_prolog = list[0].name != OPT_HELP_STR;
943 /* Find the widest help. */
944 for (o = list; o->name; o++) {
945 if (o->name == OPT_MORE_STR)
947 i = 2 + (int)strlen(o->name);
948 if (o->valtype != '-')
949 i += 1 + strlen(valtype2param(o));
950 if (i < MAX_OPT_HELP_WIDTH && i > width)
952 OPENSSL_assert(i < (int)sizeof(start));
955 if (standard_prolog) {
956 opt_printf_stderr("Usage: %s [options]\n", prog);
957 if (list[0].name != OPT_SECTION_STR)
958 opt_printf_stderr("Valid options are:\n", prog);
961 /* Now let's print. */
962 for (o = list; o->name; o++) {
963 if (o->name == OPT_PARAM_STR)
965 opt_print(o, sawparams, width);
969 /* opt_isdir section */
971 # include <windows.h>
972 int opt_isdir(const char *name)
975 # if defined(UNICODE) || defined(_UNICODE)
976 size_t i, len_0 = strlen(name) + 1;
977 WCHAR tempname[MAX_PATH];
979 if (len_0 > MAX_PATH)
982 # if !defined(_WIN32_WCE) || _WIN32_WCE>=101
983 if (!MultiByteToWideChar(CP_ACP, 0, name, len_0, tempname, MAX_PATH))
985 for (i = 0; i < len_0; i++)
986 tempname[i] = (WCHAR)name[i];
988 attr = GetFileAttributes(tempname);
990 attr = GetFileAttributes(name);
992 if (attr == INVALID_FILE_ATTRIBUTES)
994 return ((attr & FILE_ATTRIBUTE_DIRECTORY) != 0);
997 # include <sys/stat.h>
999 # if defined(_S_IFMT) && defined(_S_IFDIR)
1000 # define S_ISDIR(a) (((a) & _S_IFMT) == _S_IFDIR)
1002 # define S_ISDIR(a) (((a) & S_IFMT) == S_IFDIR)
1006 int opt_isdir(const char *name)
1008 # if defined(S_ISDIR)
1011 if (stat(name, &st) == 0)
1012 return S_ISDIR(st.st_mode);