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)
212 opt_printf_stderr("%s: Bad format \"%s\"\n", prog, s);
216 if ((flags & OPT_FMT_PEMDER) == 0)
217 return opt_format_error(s, flags);
218 *result = FORMAT_ASN1;
222 if ((flags & OPT_FMT_TEXT) == 0)
223 return opt_format_error(s, flags);
224 *result = FORMAT_TEXT;
228 if ((flags & OPT_FMT_NSS) == 0)
229 return opt_format_error(s, flags);
230 if (strcmp(s, "NSS") != 0 && strcmp(s, "nss") != 0)
231 return opt_format_error(s, flags);
232 *result = FORMAT_NSS;
236 if ((flags & OPT_FMT_SMIME) == 0)
237 return opt_format_error(s, flags);
238 *result = FORMAT_SMIME;
242 if ((flags & OPT_FMT_MSBLOB) == 0)
243 return opt_format_error(s, flags);
244 *result = FORMAT_MSBLOB;
248 if ((flags & OPT_FMT_ENGINE) == 0)
249 return opt_format_error(s, flags);
250 *result = FORMAT_ENGINE;
254 if ((flags & OPT_FMT_HTTP) == 0)
255 return opt_format_error(s, flags);
256 *result = FORMAT_HTTP;
259 if ((flags & OPT_FMT_PKCS12) == 0)
260 return opt_format_error(s, flags);
261 *result = FORMAT_PKCS12;
265 if (s[1] == '\0' || strcmp(s, "PEM") == 0 || strcmp(s, "pem") == 0) {
266 if ((flags & OPT_FMT_PEMDER) == 0)
267 return opt_format_error(s, flags);
268 *result = FORMAT_PEM;
269 } else if (strcmp(s, "PVK") == 0 || strcmp(s, "pvk") == 0) {
270 if ((flags & OPT_FMT_PVK) == 0)
271 return opt_format_error(s, flags);
272 *result = FORMAT_PVK;
273 } else if (strcmp(s, "P12") == 0 || strcmp(s, "p12") == 0
274 || strcmp(s, "PKCS12") == 0 || strcmp(s, "pkcs12") == 0) {
275 if ((flags & OPT_FMT_PKCS12) == 0)
276 return opt_format_error(s, flags);
277 *result = FORMAT_PKCS12;
279 opt_printf_stderr("%s: Bad format \"%s\"\n", prog, s);
287 /* Return string representing the given format. */
288 const char *format2str(int format)
292 return "(undefined)";
316 /* Print an error message about unsuitable/unsupported format requested. */
317 void print_format_error(int format, unsigned long flags)
319 (void)opt_format_error(format2str(format), flags);
322 /* Parse a cipher name, put it in *EVP_CIPHER; return 0 on failure, else 1. */
323 int opt_cipher(const char *name, const EVP_CIPHER **cipherp)
325 *cipherp = EVP_get_cipherbyname(name);
326 if (*cipherp != NULL)
328 opt_printf_stderr("%s: Unknown cipher: %s\n", prog, name);
333 * Parse message digest name, put it in *EVP_MD; return 0 on failure, else 1.
335 int opt_md(const char *name, const EVP_MD **mdp)
337 *mdp = EVP_get_digestbyname(name);
340 opt_printf_stderr("%s: Unknown message digest: %s\n", prog, name);
344 /* Look through a list of name/value pairs. */
345 int opt_pair(const char *name, const OPT_PAIR* pairs, int *result)
349 for (pp = pairs; pp->name; pp++)
350 if (strcmp(pp->name, name) == 0) {
351 *result = pp->retval;
354 opt_printf_stderr("%s: Value must be one of:\n", prog);
355 for (pp = pairs; pp->name; pp++)
356 opt_printf_stderr("\t%s\n", pp->name);
360 /* Parse an int, put it into *result; return 0 on failure, else 1. */
361 int opt_int(const char *value, int *result)
365 if (!opt_long(value, &l))
369 opt_printf_stderr("%s: Value \"%s\" outside integer range\n",
376 static void opt_number_error(const char *v)
379 struct strstr_pair_st {
383 {"0x", "a hexadecimal"},
384 {"0X", "a hexadecimal"},
388 for (i = 0; i < OSSL_NELEM(b); i++) {
389 if (strncmp(v, b[i].prefix, strlen(b[i].prefix)) == 0) {
390 opt_printf_stderr("%s: Can't parse \"%s\" as %s number\n",
395 opt_printf_stderr("%s: Can't parse \"%s\" as a number\n", prog, v);
399 /* Parse a long, put it into *result; return 0 on failure, else 1. */
400 int opt_long(const char *value, long *result)
407 l = strtol(value, &endp, 0);
410 || ((l == LONG_MAX || l == LONG_MIN) && errno == ERANGE)
411 || (l == 0 && errno != 0)) {
412 opt_number_error(value);
421 #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L && \
422 defined(INTMAX_MAX) && defined(UINTMAX_MAX) && \
423 !defined(OPENSSL_NO_INTTYPES_H)
425 /* Parse an intmax_t, put it into *result; return 0 on failure, else 1. */
426 int opt_imax(const char *value, intmax_t *result)
433 m = strtoimax(value, &endp, 0);
436 || ((m == INTMAX_MAX || m == INTMAX_MIN) && errno == ERANGE)
437 || (m == 0 && errno != 0)) {
438 opt_number_error(value);
447 /* Parse a uintmax_t, put it into *result; return 0 on failure, else 1. */
448 int opt_umax(const char *value, uintmax_t *result)
455 m = strtoumax(value, &endp, 0);
458 || (m == UINTMAX_MAX && errno == ERANGE)
459 || (m == 0 && errno != 0)) {
460 opt_number_error(value);
471 * Parse an unsigned long, put it into *result; return 0 on failure, else 1.
473 int opt_ulong(const char *value, unsigned long *result)
480 l = strtoul(value, &endptr, 0);
483 || ((l == ULONG_MAX) && errno == ERANGE)
484 || (l == 0 && errno != 0)) {
485 opt_number_error(value);
495 * We pass opt as an int but cast it to "enum range" so that all the
496 * items in the OPT_V_ENUM enumeration are caught; this makes -Wswitch
497 * in gcc do the right thing.
499 enum range { OPT_V_ENUM };
501 int opt_verify(int opt, X509_VERIFY_PARAM *vpm)
507 const X509_VERIFY_PARAM *vtmp;
509 OPENSSL_assert(vpm != NULL);
510 OPENSSL_assert(opt > OPT_V__FIRST);
511 OPENSSL_assert(opt < OPT_V__LAST);
513 switch ((enum range)opt) {
518 otmp = OBJ_txt2obj(opt_arg(), 0);
520 opt_printf_stderr("%s: Invalid Policy %s\n", prog, opt_arg());
523 X509_VERIFY_PARAM_add0_policy(vpm, otmp);
526 /* purpose name -> purpose index */
527 i = X509_PURPOSE_get_by_sname(opt_arg());
529 opt_printf_stderr("%s: Invalid purpose %s\n", prog, opt_arg());
533 /* purpose index -> purpose object */
534 xptmp = X509_PURPOSE_get0(i);
536 /* purpose object -> purpose value */
537 i = X509_PURPOSE_get_id(xptmp);
539 if (!X509_VERIFY_PARAM_set_purpose(vpm, i)) {
540 opt_printf_stderr("%s: Internal error setting purpose %s\n",
545 case OPT_V_VERIFY_NAME:
546 vtmp = X509_VERIFY_PARAM_lookup(opt_arg());
548 opt_printf_stderr("%s: Invalid verify name %s\n",
552 X509_VERIFY_PARAM_set1(vpm, vtmp);
554 case OPT_V_VERIFY_DEPTH:
557 X509_VERIFY_PARAM_set_depth(vpm, i);
559 case OPT_V_VERIFY_AUTH_LEVEL:
562 X509_VERIFY_PARAM_set_auth_level(vpm, i);
565 if (!opt_imax(opt_arg(), &t))
567 if (t != (time_t)t) {
568 opt_printf_stderr("%s: epoch time out of range %s\n",
572 X509_VERIFY_PARAM_set_time(vpm, (time_t)t);
574 case OPT_V_VERIFY_HOSTNAME:
575 if (!X509_VERIFY_PARAM_set1_host(vpm, opt_arg(), 0))
578 case OPT_V_VERIFY_EMAIL:
579 if (!X509_VERIFY_PARAM_set1_email(vpm, opt_arg(), 0))
582 case OPT_V_VERIFY_IP:
583 if (!X509_VERIFY_PARAM_set1_ip_asc(vpm, opt_arg()))
586 case OPT_V_IGNORE_CRITICAL:
587 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_IGNORE_CRITICAL);
589 case OPT_V_ISSUER_CHECKS:
590 /* NOP, deprecated */
592 case OPT_V_CRL_CHECK:
593 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_CRL_CHECK);
595 case OPT_V_CRL_CHECK_ALL:
596 X509_VERIFY_PARAM_set_flags(vpm,
597 X509_V_FLAG_CRL_CHECK |
598 X509_V_FLAG_CRL_CHECK_ALL);
600 case OPT_V_POLICY_CHECK:
601 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_POLICY_CHECK);
603 case OPT_V_EXPLICIT_POLICY:
604 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_EXPLICIT_POLICY);
606 case OPT_V_INHIBIT_ANY:
607 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_INHIBIT_ANY);
609 case OPT_V_INHIBIT_MAP:
610 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_INHIBIT_MAP);
612 case OPT_V_X509_STRICT:
613 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_X509_STRICT);
615 case OPT_V_EXTENDED_CRL:
616 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_EXTENDED_CRL_SUPPORT);
618 case OPT_V_USE_DELTAS:
619 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_USE_DELTAS);
621 case OPT_V_POLICY_PRINT:
622 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_NOTIFY_POLICY);
624 case OPT_V_CHECK_SS_SIG:
625 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_CHECK_SS_SIGNATURE);
627 case OPT_V_TRUSTED_FIRST:
628 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_TRUSTED_FIRST);
630 case OPT_V_SUITEB_128_ONLY:
631 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_SUITEB_128_LOS_ONLY);
633 case OPT_V_SUITEB_128:
634 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_SUITEB_128_LOS);
636 case OPT_V_SUITEB_192:
637 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_SUITEB_192_LOS);
639 case OPT_V_PARTIAL_CHAIN:
640 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_PARTIAL_CHAIN);
642 case OPT_V_NO_ALT_CHAINS:
643 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_NO_ALT_CHAINS);
645 case OPT_V_NO_CHECK_TIME:
646 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_NO_CHECK_TIME);
648 case OPT_V_ALLOW_PROXY_CERTS:
649 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_ALLOW_PROXY_CERTS);
664 * Parse the next flag (and value if specified), return 0 if done, -1 on
665 * error, otherwise the flag's retval.
675 ossl_uintmax_t umval;
677 /* Look at current arg; at end of the list? */
683 /* If word doesn't start with a -, we're done. */
687 /* Hit "--" ? We're done. */
689 if (strcmp(p, "--") == 0)
692 /* Allow -nnn and --nnn */
697 /* If we have --flag=foo, snip it off */
698 if ((arg = strchr(p, '=')) != NULL)
700 for (o = opts; o->name; ++o) {
701 /* If not this option, move on to the next one. */
702 if (strcmp(p, o->name) != 0)
705 /* If it doesn't take a value, make sure none was given. */
706 if (o->valtype == 0 || o->valtype == '-') {
708 opt_printf_stderr("%s: Option -%s does not take a value\n",
715 /* Want a value; get the next param if =foo not used. */
717 if (argv[opt_index] == NULL) {
718 opt_printf_stderr("%s: Option -%s needs a value\n",
722 arg = argv[opt_index++];
725 /* Syntax-check value. */
726 switch (o->valtype) {
733 if (opt_isdir(arg) > 0)
735 opt_printf_stderr("%s: Not a directory: %s\n", prog, arg);
745 if (!opt_int(arg, &ival))
747 if (o->valtype == 'p' && ival <= 0) {
748 opt_printf_stderr("%s: Non-positive number \"%s\" for -%s\n",
754 if (!opt_imax(arg, &imval))
758 if (!opt_umax(arg, &umval))
762 if (!opt_long(arg, &lval))
766 if (!opt_ulong(arg, &ulval))
774 o->valtype == 'c' ? OPT_FMT_PDS :
775 o->valtype == 'E' ? OPT_FMT_PDE :
776 o->valtype == 'F' ? OPT_FMT_PEMDER
777 : OPT_FMT_ANY, &ival))
779 opt_printf_stderr("%s: Invalid format \"%s\" for -%s\n",
784 /* Return the flag value. */
787 if (unknown != NULL) {
789 return unknown->retval;
791 opt_printf_stderr("%s: Unknown option: -%s\n", prog, p);
795 /* Return the most recent flag parameter. */
801 /* Return the most recent flag. */
807 /* Return the unknown option. */
808 char *opt_unknown(void)
813 /* Return the rest of the arguments after parsing flags. */
814 char **opt_rest(void)
816 return &argv[opt_index];
819 /* How many items in remaining args? */
820 int opt_num_rest(void)
825 for (pp = opt_rest(); *pp; pp++, i++)
830 /* Return a string describing the parameter type. */
831 static const char *valtype2param(const OPTIONS *o)
833 switch (o->valtype) {
856 return "PEM|DER|ENGINE";
869 void opt_print(const OPTIONS *o, int doingparams, int width)
875 help = o->helpstr ? o->helpstr : "(No additional info)";
876 if (o->name == OPT_HELP_STR) {
877 opt_printf_stderr(help, prog);
880 if (o->name == OPT_SECTION_STR) {
881 opt_printf_stderr("\n");
882 opt_printf_stderr(help, prog);
885 if (o->name == OPT_PARAM_STR) {
886 opt_printf_stderr("\nParameters:\n");
891 memset(start, ' ', sizeof(start) - 1);
892 start[sizeof(start) - 1] = '\0';
894 if (o->name == OPT_MORE_STR) {
895 /* Continuation of previous line; pad and print. */
897 opt_printf_stderr("%s %s\n", start, help);
901 /* Build up the "-flag [param]" part. */
907 p += strlen(strcpy(p, o->name));
910 if (o->valtype != '-') {
912 p += strlen(strcpy(p, valtype2param(o)));
915 if ((int)(p - start) >= MAX_OPT_HELP_WIDTH) {
917 opt_printf_stderr("%s\n", start);
918 memset(start, ' ', sizeof(start));
921 opt_printf_stderr("%s %s\n", start, help);
924 void opt_help(const OPTIONS *list)
927 int i, sawparams = 0, width = 5;
931 /* Starts with its own help message? */
932 standard_prolog = list[0].name != OPT_HELP_STR;
934 /* Find the widest help. */
935 for (o = list; o->name; o++) {
936 if (o->name == OPT_MORE_STR)
938 i = 2 + (int)strlen(o->name);
939 if (o->valtype != '-')
940 i += 1 + strlen(valtype2param(o));
941 if (i < MAX_OPT_HELP_WIDTH && i > width)
943 OPENSSL_assert(i < (int)sizeof(start));
946 if (standard_prolog) {
947 opt_printf_stderr("Usage: %s [options]\n", prog);
948 if (list[0].name != OPT_SECTION_STR)
949 opt_printf_stderr("Valid options are:\n", prog);
952 /* Now let's print. */
953 for (o = list; o->name; o++) {
954 if (o->name == OPT_PARAM_STR)
956 opt_print(o, sawparams, width);
960 /* opt_isdir section */
962 # include <windows.h>
963 int opt_isdir(const char *name)
966 # if defined(UNICODE) || defined(_UNICODE)
967 size_t i, len_0 = strlen(name) + 1;
968 WCHAR tempname[MAX_PATH];
970 if (len_0 > MAX_PATH)
973 # if !defined(_WIN32_WCE) || _WIN32_WCE>=101
974 if (!MultiByteToWideChar(CP_ACP, 0, name, len_0, tempname, MAX_PATH))
976 for (i = 0; i < len_0; i++)
977 tempname[i] = (WCHAR)name[i];
979 attr = GetFileAttributes(tempname);
981 attr = GetFileAttributes(name);
983 if (attr == INVALID_FILE_ATTRIBUTES)
985 return ((attr & FILE_ATTRIBUTE_DIRECTORY) != 0);
988 # include <sys/stat.h>
990 # if defined(_S_IFMT) && defined(_S_IFDIR)
991 # define S_ISDIR(a) (((a) & _S_IFMT) == _S_IFDIR)
993 # define S_ISDIR(a) (((a) & S_IFMT) == S_IFDIR)
997 int opt_isdir(const char *name)
999 # if defined(S_ISDIR)
1002 if (stat(name, &st) == 0)
1003 return S_ISDIR(st.st_mode);