2 * Copyright 2015-2016 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
10 /* #define COMPILE_STANDALONE_TEST_DRIVER */
13 #if !defined(OPENSSL_SYS_MSDOS)
14 # include OPENSSL_UNISTD
21 #include <openssl/bio.h>
22 #include <openssl/x509v3.h>
24 #define MAX_OPT_HELP_WIDTH 30
25 const char OPT_HELP_STR[] = "--";
26 const char OPT_MORE_STR[] = "---";
35 static const OPTIONS *unknown;
36 static const OPTIONS *opts;
40 * Return the simple name of the program; removing various platform gunk.
42 #if defined(OPENSSL_SYS_WIN32)
43 char *opt_progname(const char *argv0)
49 /* find the last '/', '\' or ':' */
50 for (p = argv0 + strlen(argv0); --p > argv0;)
51 if (*p == '/' || *p == '\\' || *p == ':') {
56 /* Strip off trailing nonsense. */
59 (strcmp(&p[n - 4], ".exe") == 0 || strcmp(&p[n - 4], ".EXE") == 0))
62 /* Copy over the name, in lowercase. */
63 if (n > sizeof prog - 1)
65 for (q = prog, i = 0; i < n; i++, p++)
66 *q++ = isupper(*p) ? tolower(*p) : *p;
71 #elif defined(OPENSSL_SYS_VMS)
73 char *opt_progname(const char *argv0)
77 /* Find last special character sys:[foo.bar]openssl */
78 for (p = argv0 + strlen(argv0); --p > argv0;)
79 if (*p == ':' || *p == ']' || *p == '>') {
85 strncpy(prog, p, sizeof prog - 1);
86 prog[sizeof prog - 1] = '\0';
87 if (q != NULL && q - p < sizeof prog)
94 char *opt_progname(const char *argv0)
98 /* Could use strchr, but this is like the ones above. */
99 for (p = argv0 + strlen(argv0); --p > argv0;)
104 strncpy(prog, p, sizeof prog - 1);
105 prog[sizeof prog - 1] = '\0';
110 char *opt_getprog(void)
115 /* Set up the arg parsing. */
116 char *opt_init(int ac, char **av, const OPTIONS *o)
126 for (; o->name; ++o) {
132 if (o->name == OPT_HELP_STR || o->name == OPT_MORE_STR)
137 /* Make sure options are legit. */
138 assert(o->name[0] != '-');
139 assert(o->retval > 0);
141 case 0: case '-': case '/': case '<': case '>': case 'E': case 'F':
142 case 'M': case 'U': case 'f': case 'l': case 'n': case 'p': case 's':
149 /* Make sure there are no duplicates. */
150 for (next = o + 1; next->name; ++next) {
152 * Some compilers inline strcmp and the assert string is too long.
154 duplicated = strcmp(o->name, next->name) == 0;
158 if (o->name[0] == '\0') {
159 assert(unknown == NULL);
161 assert(unknown->valtype == 0 || unknown->valtype == '-');
167 static OPT_PAIR formats[] = {
168 {"PEM/DER", OPT_FMT_PEMDER},
169 {"pkcs12", OPT_FMT_PKCS12},
170 {"smime", OPT_FMT_SMIME},
171 {"engine", OPT_FMT_ENGINE},
172 {"msblob", OPT_FMT_MSBLOB},
173 {"netscape", OPT_FMT_NETSCAPE},
174 {"nss", OPT_FMT_NSS},
175 {"text", OPT_FMT_TEXT},
176 {"http", OPT_FMT_HTTP},
177 {"pvk", OPT_FMT_PVK},
181 /* Print an error message about a failed format parse. */
182 int opt_format_error(const char *s, unsigned long flags)
186 if (flags == OPT_FMT_PEMDER)
187 BIO_printf(bio_err, "%s: Bad format \"%s\"; must be pem or der\n",
190 BIO_printf(bio_err, "%s: Bad format \"%s\"; must be one of:\n",
192 for (ap = formats; ap->name; ap++)
193 if (flags & ap->retval)
194 BIO_printf(bio_err, " %s\n", ap->name);
199 /* Parse a format string, put it into *result; return 0 on failure, else 1. */
200 int opt_format(const char *s, unsigned long flags, int *result)
207 if ((flags & OPT_FMT_PEMDER) == 0)
208 return opt_format_error(s, flags);
209 *result = FORMAT_ASN1;
213 if ((flags & OPT_FMT_TEXT) == 0)
214 return opt_format_error(s, flags);
215 *result = FORMAT_TEXT;
219 if ((flags & OPT_FMT_NSS) == 0)
220 return opt_format_error(s, flags);
221 if (strcmp(s, "NSS") != 0 && strcmp(s, "nss") != 0)
222 return opt_format_error(s, flags);
223 *result = FORMAT_NSS;
227 if ((flags & OPT_FMT_SMIME) == 0)
228 return opt_format_error(s, flags);
229 *result = FORMAT_SMIME;
233 if ((flags & OPT_FMT_MSBLOB) == 0)
234 return opt_format_error(s, flags);
235 *result = FORMAT_MSBLOB;
239 if ((flags & OPT_FMT_ENGINE) == 0)
240 return opt_format_error(s, flags);
241 *result = FORMAT_ENGINE;
245 if ((flags & OPT_FMT_HTTP) == 0)
246 return opt_format_error(s, flags);
247 *result = FORMAT_HTTP;
250 if ((flags & OPT_FMT_PKCS12) == 0)
251 return opt_format_error(s, flags);
252 *result = FORMAT_PKCS12;
256 if (s[1] == '\0' || strcmp(s, "PEM") == 0 || strcmp(s, "pem") == 0) {
257 if ((flags & OPT_FMT_PEMDER) == 0)
258 return opt_format_error(s, flags);
259 *result = FORMAT_PEM;
260 } else if (strcmp(s, "PVK") == 0 || strcmp(s, "pvk") == 0) {
261 if ((flags & OPT_FMT_PVK) == 0)
262 return opt_format_error(s, flags);
263 *result = FORMAT_PVK;
264 } else if (strcmp(s, "P12") == 0 || strcmp(s, "p12") == 0
265 || strcmp(s, "PKCS12") == 0 || strcmp(s, "pkcs12") == 0) {
266 if ((flags & OPT_FMT_PKCS12) == 0)
267 return opt_format_error(s, flags);
268 *result = FORMAT_PKCS12;
276 /* Parse a cipher name, put it in *EVP_CIPHER; return 0 on failure, else 1. */
277 int opt_cipher(const char *name, const EVP_CIPHER **cipherp)
279 *cipherp = EVP_get_cipherbyname(name);
282 BIO_printf(bio_err, "%s: Unknown cipher %s\n", prog, name);
287 * Parse message digest name, put it in *EVP_MD; return 0 on failure, else 1.
289 int opt_md(const char *name, const EVP_MD **mdp)
291 *mdp = EVP_get_digestbyname(name);
294 BIO_printf(bio_err, "%s: Unknown digest %s\n", prog, name);
298 /* Look through a list of name/value pairs. */
299 int opt_pair(const char *name, const OPT_PAIR* pairs, int *result)
303 for (pp = pairs; pp->name; pp++)
304 if (strcmp(pp->name, name) == 0) {
305 *result = pp->retval;
308 BIO_printf(bio_err, "%s: Value must be one of:\n", prog);
309 for (pp = pairs; pp->name; pp++)
310 BIO_printf(bio_err, "\t%s\n", pp->name);
314 /* Parse an int, put it into *result; return 0 on failure, else 1. */
315 int opt_int(const char *value, int *result)
319 if (!opt_long(value, &l))
323 BIO_printf(bio_err, "%s: Value \"%s\" outside integer range\n",
330 /* Parse a long, put it into *result; return 0 on failure, else 1. */
331 int opt_long(const char *value, long *result)
338 l = strtol(value, &endp, 0);
341 || ((l == LONG_MAX || l == LONG_MIN) && errno == ERANGE)
342 || (l == 0 && errno != 0)) {
343 BIO_printf(bio_err, "%s: Can't parse \"%s\" as a number\n",
353 #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L && \
354 defined(INTMAX_MAX) && defined(UINTMAX_MAX)
356 /* Parse an intmax_t, put it into *result; return 0 on failure, else 1. */
357 int opt_imax(const char *value, intmax_t *result)
364 m = strtoimax(value, &endp, 0);
367 || ((m == INTMAX_MAX || m == INTMAX_MIN) && errno == ERANGE)
368 || (m == 0 && errno != 0)) {
369 BIO_printf(bio_err, "%s: Can't parse \"%s\" as a number\n",
379 /* Parse a uintmax_t, put it into *result; return 0 on failure, else 1. */
380 int opt_umax(const char *value, uintmax_t *result)
387 m = strtoumax(value, &endp, 0);
390 || (m == UINTMAX_MAX && errno == ERANGE)
391 || (m == 0 && errno != 0)) {
392 BIO_printf(bio_err, "%s: Can't parse \"%s\" as a number\n",
404 * Parse an unsigned long, put it into *result; return 0 on failure, else 1.
406 int opt_ulong(const char *value, unsigned long *result)
413 l = strtoul(value, &endptr, 0);
416 || ((l == ULONG_MAX) && errno == ERANGE)
417 || (l == 0 && errno != 0)) {
418 BIO_printf(bio_err, "%s: Can't parse \"%s\" as an unsigned number\n",
429 * We pass opt as an int but cast it to "enum range" so that all the
430 * items in the OPT_V_ENUM enumeration are caught; this makes -Wswitch
431 * in gcc do the right thing.
433 enum range { OPT_V_ENUM };
435 int opt_verify(int opt, X509_VERIFY_PARAM *vpm)
441 const X509_VERIFY_PARAM *vtmp;
444 assert(opt > OPT_V__FIRST);
445 assert(opt < OPT_V__LAST);
447 switch ((enum range)opt) {
452 otmp = OBJ_txt2obj(opt_arg(), 0);
454 BIO_printf(bio_err, "%s: Invalid Policy %s\n", prog, opt_arg());
457 X509_VERIFY_PARAM_add0_policy(vpm, otmp);
460 /* purpose name -> purpose index */
461 i = X509_PURPOSE_get_by_sname(opt_arg());
463 BIO_printf(bio_err, "%s: Invalid purpose %s\n", prog, opt_arg());
467 /* purpose index -> purpose object */
468 xptmp = X509_PURPOSE_get0(i);
470 /* purpose object -> purpose value */
471 i = X509_PURPOSE_get_id(xptmp);
473 if (!X509_VERIFY_PARAM_set_purpose(vpm, i)) {
475 "%s: Internal error setting purpose %s\n",
480 case OPT_V_VERIFY_NAME:
481 vtmp = X509_VERIFY_PARAM_lookup(opt_arg());
483 BIO_printf(bio_err, "%s: Invalid verify name %s\n",
487 X509_VERIFY_PARAM_set1(vpm, vtmp);
489 case OPT_V_VERIFY_DEPTH:
492 X509_VERIFY_PARAM_set_depth(vpm, i);
494 case OPT_V_VERIFY_AUTH_LEVEL:
497 X509_VERIFY_PARAM_set_auth_level(vpm, i);
500 if (!opt_imax(opt_arg(), &t))
502 if (t != (time_t)t) {
503 BIO_printf(bio_err, "%s: epoch time out of range %s\n",
507 X509_VERIFY_PARAM_set_time(vpm, (time_t)t);
509 case OPT_V_VERIFY_HOSTNAME:
510 if (!X509_VERIFY_PARAM_set1_host(vpm, opt_arg(), 0))
513 case OPT_V_VERIFY_EMAIL:
514 if (!X509_VERIFY_PARAM_set1_email(vpm, opt_arg(), 0))
517 case OPT_V_VERIFY_IP:
518 if (!X509_VERIFY_PARAM_set1_ip_asc(vpm, opt_arg()))
521 case OPT_V_IGNORE_CRITICAL:
522 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_IGNORE_CRITICAL);
524 case OPT_V_ISSUER_CHECKS:
525 /* NOP, deprecated */
527 case OPT_V_CRL_CHECK:
528 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_CRL_CHECK);
530 case OPT_V_CRL_CHECK_ALL:
531 X509_VERIFY_PARAM_set_flags(vpm,
532 X509_V_FLAG_CRL_CHECK |
533 X509_V_FLAG_CRL_CHECK_ALL);
535 case OPT_V_POLICY_CHECK:
536 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_POLICY_CHECK);
538 case OPT_V_EXPLICIT_POLICY:
539 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_EXPLICIT_POLICY);
541 case OPT_V_INHIBIT_ANY:
542 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_INHIBIT_ANY);
544 case OPT_V_INHIBIT_MAP:
545 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_INHIBIT_MAP);
547 case OPT_V_X509_STRICT:
548 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_X509_STRICT);
550 case OPT_V_EXTENDED_CRL:
551 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_EXTENDED_CRL_SUPPORT);
553 case OPT_V_USE_DELTAS:
554 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_USE_DELTAS);
556 case OPT_V_POLICY_PRINT:
557 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_NOTIFY_POLICY);
559 case OPT_V_CHECK_SS_SIG:
560 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_CHECK_SS_SIGNATURE);
562 case OPT_V_TRUSTED_FIRST:
563 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_TRUSTED_FIRST);
565 case OPT_V_SUITEB_128_ONLY:
566 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_SUITEB_128_LOS_ONLY);
568 case OPT_V_SUITEB_128:
569 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_SUITEB_128_LOS);
571 case OPT_V_SUITEB_192:
572 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_SUITEB_192_LOS);
574 case OPT_V_PARTIAL_CHAIN:
575 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_PARTIAL_CHAIN);
577 case OPT_V_NO_ALT_CHAINS:
578 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_NO_ALT_CHAINS);
580 case OPT_V_NO_CHECK_TIME:
581 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_NO_CHECK_TIME);
583 case OPT_V_ALLOW_PROXY_CERTS:
584 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_ALLOW_PROXY_CERTS);
592 * Parse the next flag (and value if specified), return 0 if done, -1 on
593 * error, otherwise the flag's retval.
603 ossl_uintmax_t umval;
605 /* Look at current arg; at end of the list? */
611 /* If word doesn't start with a -, we're done. */
615 /* Hit "--" ? We're done. */
617 if (strcmp(p, "--") == 0)
620 /* Allow -nnn and --nnn */
625 /* If we have --flag=foo, snip it off */
626 if ((arg = strchr(p, '=')) != NULL)
628 for (o = opts; o->name; ++o) {
629 /* If not this option, move on to the next one. */
630 if (strcmp(p, o->name) != 0)
633 /* If it doesn't take a value, make sure none was given. */
634 if (o->valtype == 0 || o->valtype == '-') {
637 "%s: Option -%s does not take a value\n", prog, p);
643 /* Want a value; get the next param if =foo not used. */
645 if (argv[opt_index] == NULL) {
647 "%s: Option -%s needs a value\n", prog, o->name);
650 arg = argv[opt_index++];
653 /* Syntax-check value. */
654 switch (o->valtype) {
660 if (app_isdir(arg) >= 0)
662 BIO_printf(bio_err, "%s: Not a directory: %s\n", prog, arg);
666 if (strcmp(arg, "-") == 0 || app_access(arg, R_OK) >= 0)
669 "%s: Cannot open input file %s, %s\n",
670 prog, arg, strerror(errno));
674 if (strcmp(arg, "-") == 0 || app_access(arg, W_OK) >= 0 || errno == ENOENT)
677 "%s: Cannot open output file %s, %s\n",
678 prog, arg, strerror(errno));
682 if (!opt_int(arg, &ival)
683 || (o->valtype == 'p' && ival <= 0)) {
685 "%s: Non-positive number \"%s\" for -%s\n",
691 if (!opt_imax(arg, &imval)) {
693 "%s: Invalid number \"%s\" for -%s\n",
699 if (!opt_umax(arg, &umval)) {
701 "%s: Invalid number \"%s\" for -%s\n",
707 if (!opt_long(arg, &lval)) {
709 "%s: Invalid number \"%s\" for -%s\n",
715 if (!opt_ulong(arg, &ulval)) {
717 "%s: Invalid number \"%s\" for -%s\n",
727 o->valtype == 'c' ? OPT_FMT_PDS :
728 o->valtype == 'E' ? OPT_FMT_PDE :
729 o->valtype == 'F' ? OPT_FMT_PEMDER
730 : OPT_FMT_ANY, &ival))
733 "%s: Invalid format \"%s\" for -%s\n",
738 /* Return the flag value. */
741 if (unknown != NULL) {
743 return unknown->retval;
745 BIO_printf(bio_err, "%s: Option unknown option -%s\n", prog, p);
749 /* Return the most recent flag parameter. */
755 /* Return the most recent flag. */
761 /* Return the unknown option. */
762 char *opt_unknown(void)
767 /* Return the rest of the arguments after parsing flags. */
768 char **opt_rest(void)
770 return &argv[opt_index];
773 /* How many items in remaining args? */
774 int opt_num_rest(void)
779 for (pp = opt_rest(); *pp; pp++, i++)
784 /* Return a string describing the parameter type. */
785 static const char *valtype2param(const OPTIONS *o)
787 switch (o->valtype) {
808 return "PEM|DER|ENGINE";
821 void opt_help(const OPTIONS *list)
831 /* Starts with its own help message? */
832 standard_prolog = list[0].name != OPT_HELP_STR;
834 /* Find the widest help. */
835 for (o = list; o->name; o++) {
836 if (o->name == OPT_MORE_STR)
838 i = 2 + (int)strlen(o->name);
839 if (o->valtype != '-')
840 i += 1 + strlen(valtype2param(o));
841 if (i < MAX_OPT_HELP_WIDTH && i > width)
843 assert(i < (int)sizeof start);
847 BIO_printf(bio_err, "Usage: %s [options]\nValid options are:\n",
850 /* Now let's print. */
851 for (o = list; o->name; o++) {
852 help = o->helpstr ? o->helpstr : "(No additional info)";
853 if (o->name == OPT_HELP_STR) {
854 BIO_printf(bio_err, help, prog);
859 memset(start, ' ', sizeof(start) - 1);
860 start[sizeof start - 1] = '\0';
862 if (o->name == OPT_MORE_STR) {
863 /* Continuation of previous line; pad and print. */
865 BIO_printf(bio_err, "%s %s\n", start, help);
869 /* Build up the "-flag [param]" part. */
874 p += strlen(strcpy(p, o->name));
877 if (o->valtype != '-') {
879 p += strlen(strcpy(p, valtype2param(o)));
882 if ((int)(p - start) >= MAX_OPT_HELP_WIDTH) {
884 BIO_printf(bio_err, "%s\n", start);
885 memset(start, ' ', sizeof(start));
888 BIO_printf(bio_err, "%s %s\n", start, help);
892 #ifdef COMPILE_STANDALONE_TEST_DRIVER
893 # include <sys/stat.h>
895 typedef enum OPTION_choice {
896 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
897 OPT_IN, OPT_INFORM, OPT_OUT, OPT_COUNT, OPT_U, OPT_FLAG,
901 static OPTIONS options[] = {
902 {OPT_HELP_STR, 1, '-', "Usage: %s flags\n"},
903 {OPT_HELP_STR, 1, '-', "Valid options are:\n"},
904 {"help", OPT_HELP, '-', "Display this summary"},
905 {"in", OPT_IN, '<', "input file"},
906 {OPT_MORE_STR, 1, '-', "more detail about input"},
907 {"inform", OPT_INFORM, 'f', "input file format; defaults to pem"},
908 {"out", OPT_OUT, '>', "output file"},
909 {"count", OPT_COUNT, 'p', "a counter greater than zero"},
910 {"u", OPT_U, 'u', "an unsigned number"},
911 {"flag", OPT_FLAG, 0, "just some flag"},
912 {"str", OPT_STR, 's', "the magic word"},
913 {"areallyverylongoption", OPT_HELP, '-', "long way for help"},
919 int app_isdir(const char *name)
923 return name != NULL && stat(name, &sb) >= 0 && S_ISDIR(sb.st_mode);
926 int main(int ac, char **av)
932 bio_err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
934 prog = opt_init(ac, av, options);
935 while ((o = opt_next()) != OPT_EOF) {
940 printf("%s: Usage error; try -help.\n", prog);
946 printf("in %s\n", opt_arg());
949 printf("inform %s\n", opt_arg());
952 printf("out %s\n", opt_arg());
955 printf("count %s\n", opt_arg());
958 printf("u %s\n", opt_arg());
964 printf("str %s\n", opt_arg());
968 argc = opt_num_rest();
971 printf("args = %d\n", argc);
974 printf(" %s\n", *argv++);