2 * Copyright 2015-2018 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[] = "--";
30 const char OPT_MORE_STR[] = "---";
31 const char OPT_SECTION_STR[] = "----";
40 static const OPTIONS *unknown;
41 static const OPTIONS *opts;
45 * Return the simple name of the program; removing various platform gunk.
47 #if defined(OPENSSL_SYS_WIN32)
48 char *opt_progname(const char *argv0)
54 /* find the last '/', '\' or ':' */
55 for (p = argv0 + strlen(argv0); --p > argv0;)
56 if (*p == '/' || *p == '\\' || *p == ':') {
61 /* Strip off trailing nonsense. */
64 (strcmp(&p[n - 4], ".exe") == 0 || strcmp(&p[n - 4], ".EXE") == 0))
67 /* Copy over the name, in lowercase. */
68 if (n > sizeof(prog) - 1)
70 for (q = prog, i = 0; i < n; i++, p++)
71 *q++ = tolower((unsigned char)*p);
76 #elif defined(OPENSSL_SYS_VMS)
78 char *opt_progname(const char *argv0)
82 /* Find last special character sys:[foo.bar]openssl */
83 for (p = argv0 + strlen(argv0); --p > argv0;)
84 if (*p == ':' || *p == ']' || *p == '>') {
90 strncpy(prog, p, sizeof(prog) - 1);
91 prog[sizeof(prog) - 1] = '\0';
92 if (q != NULL && q - p < sizeof(prog))
99 char *opt_progname(const char *argv0)
103 /* Could use strchr, but this is like the ones above. */
104 for (p = argv0 + strlen(argv0); --p > argv0;)
109 strncpy(prog, p, sizeof(prog) - 1);
110 prog[sizeof(prog) - 1] = '\0';
115 char *opt_getprog(void)
120 /* Set up the arg parsing. */
121 char *opt_init(int ac, char **av, const OPTIONS *o)
131 for (; o->name; ++o) {
137 if (o->name == OPT_HELP_STR || o->name == OPT_MORE_STR ||
138 o->name == OPT_SECTION_STR)
143 /* Make sure options are legit. */
144 OPENSSL_assert(o->name[0] != '-');
145 OPENSSL_assert(o->retval > 0);
147 case 0: case '-': case '/': case '<': case '>': case 'E': case 'F':
148 case 'M': case 'U': case 'f': case 'l': case 'n': case 'p': case 's':
155 /* Make sure there are no duplicates. */
156 for (next = o + 1; next->name; ++next) {
158 * Some compilers inline strcmp and the assert string is too long.
160 duplicated = strcmp(o->name, next->name) == 0;
161 OPENSSL_assert(!duplicated);
164 if (o->name[0] == '\0') {
165 OPENSSL_assert(unknown == NULL);
167 OPENSSL_assert(unknown->valtype == 0 || unknown->valtype == '-');
173 static OPT_PAIR formats[] = {
174 {"PEM/DER", OPT_FMT_PEMDER},
175 {"pkcs12", OPT_FMT_PKCS12},
176 {"smime", OPT_FMT_SMIME},
177 {"engine", OPT_FMT_ENGINE},
178 {"msblob", OPT_FMT_MSBLOB},
179 {"nss", OPT_FMT_NSS},
180 {"text", OPT_FMT_TEXT},
181 {"http", OPT_FMT_HTTP},
182 {"pvk", OPT_FMT_PVK},
186 /* Print an error message about a failed format parse. */
187 int opt_format_error(const char *s, unsigned long flags)
191 if (flags == OPT_FMT_PEMDER) {
192 opt_printf_stderr("%s: Bad format \"%s\"; must be pem or der\n",
195 opt_printf_stderr("%s: Bad format \"%s\"; must be one of:\n",
197 for (ap = formats; ap->name; ap++)
198 if (flags & ap->retval)
199 opt_printf_stderr(" %s\n", ap->name);
204 /* Parse a format string, put it into *result; return 0 on failure, else 1. */
205 int opt_format(const char *s, unsigned long flags, int *result)
212 if ((flags & OPT_FMT_PEMDER) == 0)
213 return opt_format_error(s, flags);
214 *result = FORMAT_ASN1;
218 if ((flags & OPT_FMT_TEXT) == 0)
219 return opt_format_error(s, flags);
220 *result = FORMAT_TEXT;
224 if ((flags & OPT_FMT_NSS) == 0)
225 return opt_format_error(s, flags);
226 if (strcmp(s, "NSS") != 0 && strcmp(s, "nss") != 0)
227 return opt_format_error(s, flags);
228 *result = FORMAT_NSS;
232 if ((flags & OPT_FMT_SMIME) == 0)
233 return opt_format_error(s, flags);
234 *result = FORMAT_SMIME;
238 if ((flags & OPT_FMT_MSBLOB) == 0)
239 return opt_format_error(s, flags);
240 *result = FORMAT_MSBLOB;
244 if ((flags & OPT_FMT_ENGINE) == 0)
245 return opt_format_error(s, flags);
246 *result = FORMAT_ENGINE;
250 if ((flags & OPT_FMT_HTTP) == 0)
251 return opt_format_error(s, flags);
252 *result = FORMAT_HTTP;
255 if ((flags & OPT_FMT_PKCS12) == 0)
256 return opt_format_error(s, flags);
257 *result = FORMAT_PKCS12;
261 if (s[1] == '\0' || strcmp(s, "PEM") == 0 || strcmp(s, "pem") == 0) {
262 if ((flags & OPT_FMT_PEMDER) == 0)
263 return opt_format_error(s, flags);
264 *result = FORMAT_PEM;
265 } else if (strcmp(s, "PVK") == 0 || strcmp(s, "pvk") == 0) {
266 if ((flags & OPT_FMT_PVK) == 0)
267 return opt_format_error(s, flags);
268 *result = FORMAT_PVK;
269 } else if (strcmp(s, "P12") == 0 || strcmp(s, "p12") == 0
270 || strcmp(s, "PKCS12") == 0 || strcmp(s, "pkcs12") == 0) {
271 if ((flags & OPT_FMT_PKCS12) == 0)
272 return opt_format_error(s, flags);
273 *result = FORMAT_PKCS12;
282 /* Parse a cipher name, put it in *EVP_CIPHER; return 0 on failure, else 1. */
283 int opt_cipher(const char *name, const EVP_CIPHER **cipherp)
285 *cipherp = EVP_get_cipherbyname(name);
286 if (*cipherp != NULL)
288 opt_printf_stderr("%s: Unrecognized flag %s\n", prog, name);
293 * Parse message digest name, put it in *EVP_MD; return 0 on failure, else 1.
295 int opt_md(const char *name, const EVP_MD **mdp)
297 *mdp = EVP_get_digestbyname(name);
300 opt_printf_stderr("%s: Unrecognized flag %s\n", prog, name);
304 /* Look through a list of name/value pairs. */
305 int opt_pair(const char *name, const OPT_PAIR* pairs, int *result)
309 for (pp = pairs; pp->name; pp++)
310 if (strcmp(pp->name, name) == 0) {
311 *result = pp->retval;
314 opt_printf_stderr("%s: Value must be one of:\n", prog);
315 for (pp = pairs; pp->name; pp++)
316 opt_printf_stderr("\t%s\n", pp->name);
320 /* Parse an int, put it into *result; return 0 on failure, else 1. */
321 int opt_int(const char *value, int *result)
325 if (!opt_long(value, &l))
329 opt_printf_stderr("%s: Value \"%s\" outside integer range\n",
336 static void opt_number_error(const char *v)
339 struct strstr_pair_st {
343 {"0x", "a hexadecimal"},
344 {"0X", "a hexadecimal"},
348 for (i = 0; i < OSSL_NELEM(b); i++) {
349 if (strncmp(v, b[i].prefix, strlen(b[i].prefix)) == 0) {
350 opt_printf_stderr("%s: Can't parse \"%s\" as %s number\n",
355 opt_printf_stderr("%s: Can't parse \"%s\" as a number\n", prog, v);
359 /* Parse a long, put it into *result; return 0 on failure, else 1. */
360 int opt_long(const char *value, long *result)
367 l = strtol(value, &endp, 0);
370 || ((l == LONG_MAX || l == LONG_MIN) && errno == ERANGE)
371 || (l == 0 && errno != 0)) {
372 opt_number_error(value);
381 #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L && \
382 defined(INTMAX_MAX) && defined(UINTMAX_MAX) && \
383 !defined(OPENSSL_NO_INTTYPES_H)
385 /* Parse an intmax_t, put it into *result; return 0 on failure, else 1. */
386 int opt_imax(const char *value, intmax_t *result)
393 m = strtoimax(value, &endp, 0);
396 || ((m == INTMAX_MAX || m == INTMAX_MIN) && errno == ERANGE)
397 || (m == 0 && errno != 0)) {
398 opt_number_error(value);
407 /* Parse a uintmax_t, put it into *result; return 0 on failure, else 1. */
408 int opt_umax(const char *value, uintmax_t *result)
415 m = strtoumax(value, &endp, 0);
418 || (m == UINTMAX_MAX && errno == ERANGE)
419 || (m == 0 && errno != 0)) {
420 opt_number_error(value);
431 * Parse an unsigned long, put it into *result; return 0 on failure, else 1.
433 int opt_ulong(const char *value, unsigned long *result)
440 l = strtoul(value, &endptr, 0);
443 || ((l == ULONG_MAX) && errno == ERANGE)
444 || (l == 0 && errno != 0)) {
445 opt_number_error(value);
455 * We pass opt as an int but cast it to "enum range" so that all the
456 * items in the OPT_V_ENUM enumeration are caught; this makes -Wswitch
457 * in gcc do the right thing.
459 enum range { OPT_V_ENUM };
461 int opt_verify(int opt, X509_VERIFY_PARAM *vpm)
467 const X509_VERIFY_PARAM *vtmp;
469 OPENSSL_assert(vpm != NULL);
470 OPENSSL_assert(opt > OPT_V__FIRST);
471 OPENSSL_assert(opt < OPT_V__LAST);
473 switch ((enum range)opt) {
478 otmp = OBJ_txt2obj(opt_arg(), 0);
480 opt_printf_stderr("%s: Invalid Policy %s\n", prog, opt_arg());
483 X509_VERIFY_PARAM_add0_policy(vpm, otmp);
486 /* purpose name -> purpose index */
487 i = X509_PURPOSE_get_by_sname(opt_arg());
489 opt_printf_stderr("%s: Invalid purpose %s\n", prog, opt_arg());
493 /* purpose index -> purpose object */
494 xptmp = X509_PURPOSE_get0(i);
496 /* purpose object -> purpose value */
497 i = X509_PURPOSE_get_id(xptmp);
499 if (!X509_VERIFY_PARAM_set_purpose(vpm, i)) {
500 opt_printf_stderr("%s: Internal error setting purpose %s\n",
505 case OPT_V_VERIFY_NAME:
506 vtmp = X509_VERIFY_PARAM_lookup(opt_arg());
508 opt_printf_stderr("%s: Invalid verify name %s\n",
512 X509_VERIFY_PARAM_set1(vpm, vtmp);
514 case OPT_V_VERIFY_DEPTH:
517 X509_VERIFY_PARAM_set_depth(vpm, i);
519 case OPT_V_VERIFY_AUTH_LEVEL:
522 X509_VERIFY_PARAM_set_auth_level(vpm, i);
525 if (!opt_imax(opt_arg(), &t))
527 if (t != (time_t)t) {
528 opt_printf_stderr("%s: epoch time out of range %s\n",
532 X509_VERIFY_PARAM_set_time(vpm, (time_t)t);
534 case OPT_V_VERIFY_HOSTNAME:
535 if (!X509_VERIFY_PARAM_set1_host(vpm, opt_arg(), 0))
538 case OPT_V_VERIFY_EMAIL:
539 if (!X509_VERIFY_PARAM_set1_email(vpm, opt_arg(), 0))
542 case OPT_V_VERIFY_IP:
543 if (!X509_VERIFY_PARAM_set1_ip_asc(vpm, opt_arg()))
546 case OPT_V_IGNORE_CRITICAL:
547 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_IGNORE_CRITICAL);
549 case OPT_V_ISSUER_CHECKS:
550 /* NOP, deprecated */
552 case OPT_V_CRL_CHECK:
553 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_CRL_CHECK);
555 case OPT_V_CRL_CHECK_ALL:
556 X509_VERIFY_PARAM_set_flags(vpm,
557 X509_V_FLAG_CRL_CHECK |
558 X509_V_FLAG_CRL_CHECK_ALL);
560 case OPT_V_POLICY_CHECK:
561 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_POLICY_CHECK);
563 case OPT_V_EXPLICIT_POLICY:
564 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_EXPLICIT_POLICY);
566 case OPT_V_INHIBIT_ANY:
567 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_INHIBIT_ANY);
569 case OPT_V_INHIBIT_MAP:
570 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_INHIBIT_MAP);
572 case OPT_V_X509_STRICT:
573 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_X509_STRICT);
575 case OPT_V_EXTENDED_CRL:
576 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_EXTENDED_CRL_SUPPORT);
578 case OPT_V_USE_DELTAS:
579 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_USE_DELTAS);
581 case OPT_V_POLICY_PRINT:
582 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_NOTIFY_POLICY);
584 case OPT_V_CHECK_SS_SIG:
585 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_CHECK_SS_SIGNATURE);
587 case OPT_V_TRUSTED_FIRST:
588 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_TRUSTED_FIRST);
590 case OPT_V_SUITEB_128_ONLY:
591 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_SUITEB_128_LOS_ONLY);
593 case OPT_V_SUITEB_128:
594 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_SUITEB_128_LOS);
596 case OPT_V_SUITEB_192:
597 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_SUITEB_192_LOS);
599 case OPT_V_PARTIAL_CHAIN:
600 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_PARTIAL_CHAIN);
602 case OPT_V_NO_ALT_CHAINS:
603 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_NO_ALT_CHAINS);
605 case OPT_V_NO_CHECK_TIME:
606 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_NO_CHECK_TIME);
608 case OPT_V_ALLOW_PROXY_CERTS:
609 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_ALLOW_PROXY_CERTS);
624 * Parse the next flag (and value if specified), return 0 if done, -1 on
625 * error, otherwise the flag's retval.
635 ossl_uintmax_t umval;
637 /* Look at current arg; at end of the list? */
643 /* If word doesn't start with a -, we're done. */
647 /* Hit "--" ? We're done. */
649 if (strcmp(p, "--") == 0)
652 /* Allow -nnn and --nnn */
657 /* If we have --flag=foo, snip it off */
658 if ((arg = strchr(p, '=')) != NULL)
660 for (o = opts; o->name; ++o) {
661 /* If not this option, move on to the next one. */
662 if (strcmp(p, o->name) != 0)
665 /* If it doesn't take a value, make sure none was given. */
666 if (o->valtype == 0 || o->valtype == '-') {
668 opt_printf_stderr("%s: Option -%s does not take a value\n",
675 /* Want a value; get the next param if =foo not used. */
677 if (argv[opt_index] == NULL) {
678 opt_printf_stderr("%s: Option -%s needs a value\n",
682 arg = argv[opt_index++];
685 /* Syntax-check value. */
686 switch (o->valtype) {
692 if (opt_isdir(arg) > 0)
694 opt_printf_stderr("%s: Not a directory: %s\n", prog, arg);
704 if (!opt_int(arg, &ival)
705 || (o->valtype == 'p' && ival <= 0)) {
706 opt_printf_stderr("%s: Non-positive number \"%s\" for -%s\n",
712 if (!opt_imax(arg, &imval)) {
713 opt_printf_stderr("%s: Invalid number \"%s\" for -%s\n",
719 if (!opt_umax(arg, &umval)) {
720 opt_printf_stderr("%s: Invalid number \"%s\" for -%s\n",
726 if (!opt_long(arg, &lval)) {
727 opt_printf_stderr("%s: Invalid number \"%s\" for -%s\n",
733 if (!opt_ulong(arg, &ulval)) {
734 opt_printf_stderr("%s: Invalid number \"%s\" for -%s\n",
744 o->valtype == 'c' ? OPT_FMT_PDS :
745 o->valtype == 'E' ? OPT_FMT_PDE :
746 o->valtype == 'F' ? OPT_FMT_PEMDER
747 : OPT_FMT_ANY, &ival))
749 opt_printf_stderr("%s: Invalid format \"%s\" for -%s\n",
754 /* Return the flag value. */
757 if (unknown != NULL) {
759 return unknown->retval;
761 opt_printf_stderr("%s: Option unknown option -%s\n", prog, p);
765 /* Return the most recent flag parameter. */
771 /* Return the most recent flag. */
777 /* Return the unknown option. */
778 char *opt_unknown(void)
783 /* Return the rest of the arguments after parsing flags. */
784 char **opt_rest(void)
786 return &argv[opt_index];
789 /* How many items in remaining args? */
790 int opt_num_rest(void)
795 for (pp = opt_rest(); *pp; pp++, i++)
800 /* Return a string describing the parameter type. */
801 static const char *valtype2param(const OPTIONS *o)
803 switch (o->valtype) {
824 return "PEM|DER|ENGINE";
837 void opt_print(const OPTIONS *o, int width)
843 help = o->helpstr ? o->helpstr : "(No additional info)";
844 if (o->name == OPT_HELP_STR || o->name == OPT_SECTION_STR) {
845 opt_printf_stderr(help, prog);
850 memset(start, ' ', sizeof(start) - 1);
851 start[sizeof(start) - 1] = '\0';
853 if (o->name == OPT_MORE_STR) {
854 /* Continuation of previous line; pad and print. */
856 opt_printf_stderr("%s %s\n", start, help);
860 /* Build up the "-flag [param]" part. */
865 p += strlen(strcpy(p, o->name));
868 if (o->valtype != '-') {
870 p += strlen(strcpy(p, valtype2param(o)));
873 if ((int)(p - start) >= MAX_OPT_HELP_WIDTH) {
875 opt_printf_stderr("%s\n", start);
876 memset(start, ' ', sizeof(start));
879 opt_printf_stderr("%s %s\n", start, help);
882 void opt_help(const OPTIONS *list)
890 /* Starts with its own help message? */
891 standard_prolog = list[0].name != OPT_HELP_STR;
893 /* Find the widest help. */
894 for (o = list; o->name; o++) {
895 if (o->name == OPT_MORE_STR)
897 i = 2 + (int)strlen(o->name);
898 if (o->valtype != '-')
899 i += 1 + strlen(valtype2param(o));
900 if (i < MAX_OPT_HELP_WIDTH && i > width)
902 OPENSSL_assert(i < (int)sizeof(start));
905 if (standard_prolog) {
906 opt_printf_stderr("Usage: %s [options]\n", prog);
907 if (list[0].name != OPT_SECTION_STR)
908 opt_printf_stderr("Valid options are:\n", prog);
911 /* Now let's print. */
912 for (o = list; o->name; o++) {
917 /* opt_isdir section */
919 # include <windows.h>
920 int opt_isdir(const char *name)
923 # if defined(UNICODE) || defined(_UNICODE)
924 size_t i, len_0 = strlen(name) + 1;
925 WCHAR tempname[MAX_PATH];
927 if (len_0 > MAX_PATH)
930 # if !defined(_WIN32_WCE) || _WIN32_WCE>=101
931 if (!MultiByteToWideChar(CP_ACP, 0, name, len_0, tempname, MAX_PATH))
933 for (i = 0; i < len_0; i++)
934 tempname[i] = (WCHAR)name[i];
936 attr = GetFileAttributes(tempname);
938 attr = GetFileAttributes(name);
940 if (attr == INVALID_FILE_ATTRIBUTES)
942 return ((attr & FILE_ATTRIBUTE_DIRECTORY) != 0);
945 # include <sys/stat.h>
947 # if defined(_S_IFMT) && defined(_S_IFDIR)
948 # define S_ISDIR(a) (((a) & _S_IFMT) == _S_IFDIR)
950 # define S_ISDIR(a) (((a) & S_IFMT) == S_IFDIR)
954 int opt_isdir(const char *name)
956 # if defined(S_ISDIR)
959 if (stat(name, &st) == 0)
960 return S_ISDIR(st.st_mode);