1 /* ====================================================================
2 * Copyright (c) 2015 The OpenSSL Project. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
16 * 3. All advertising materials mentioning features or use of this
17 * software must display the following acknowledgment:
18 * "This product includes software developed by the OpenSSL Project
19 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
21 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
22 * endorse or promote products derived from this software without
23 * prior written permission. For written permission, please contact
24 * licensing@OpenSSL.org.
26 * 5. Products derived from this software may not be called "OpenSSL"
27 * nor may "OpenSSL" appear in their names without prior written
28 * permission of the OpenSSL Project.
30 * 6. Redistributions of any form whatsoever must retain the following
32 * "This product includes software developed by the OpenSSL Project
33 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
35 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
36 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
38 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
41 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
42 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
44 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
46 * OF THE POSSIBILITY OF SUCH DAMAGE.
47 * ====================================================================
50 /* #define COMPILE_STANDALONE_TEST_DRIVER */
53 #if !defined(OPENSSL_SYS_MSDOS)
54 # include OPENSSL_UNISTD
61 #include <openssl/bio.h>
63 #define MAX_OPT_HELP_WIDTH 30
64 const char OPT_HELP_STR[] = "--";
65 const char OPT_MORE_STR[] = "---";
74 static const OPTIONS *unknown;
75 static const OPTIONS *opts;
79 * Return the simple name of the program; removing various platform gunk.
81 #if defined(OPENSSL_SYS_WIN32) || defined(OPENSSL_SYS_NETWARE)
82 char *opt_progname(const char *argv0)
88 /* find the last '/', '\' or ':' */
89 for (p = argv0 + strlen(argv0); --p > argv0;)
90 if (*p == '/' || *p == '\\' || *p == ':') {
95 /* Strip off trailing nonsense. */
98 (strcmp(&p[n - 4], ".exe") == 0 || strcmp(&p[n - 4], ".EXE") == 0))
100 #if defined(OPENSSL_SYS_NETWARE)
102 (strcmp(&p[n - 4], ".nlm") == 0 || strcmp(&p[n - 4], ".NLM") == 0))
106 /* Copy over the name, in lowercase. */
107 if (n > sizeof prog - 1)
109 for (q = prog, i = 0; i < n; i++, p++)
110 *q++ = isupper(*p) ? tolower(*p) : *p;
115 #elif defined(OPENSSL_SYS_VMS)
117 char *opt_progname(const char *argv0)
121 /* Find last special charcter sys:[foo.bar]openssl */
122 for (p = argv0 + strlen(argv0); --p > argv0;)
123 if (*p == ':' || *p == ']' || *p == '>') {
129 strncpy(prog, p, sizeof prog - 1);
130 prog[sizeof prog - 1] = '\0';
131 if (q != NULL && q - p < sizeof prog)
138 char *opt_progname(const char *argv0)
142 /* Could use strchr, but this is like the ones above. */
143 for (p = argv0 + strlen(argv0); --p > argv0;)
148 strncpy(prog, p, sizeof prog - 1);
149 prog[sizeof prog - 1] = '\0';
154 char *opt_getprog(void)
159 /* Set up the arg parsing. */
160 char *opt_init(int ac, char **av, const OPTIONS *o)
170 for (; o->name; ++o) {
176 if (o->name == OPT_HELP_STR || o->name == OPT_MORE_STR)
181 /* Make sure options are legit. */
182 assert(o->name[0] != '-');
183 assert(o->retval > 0);
185 case 0: case '-': case '/': case '<': case '>': case 'F': case 'M':
186 case 'L': case 'U': case 'f': case 'n': case 'p': case 's': case 'u':
192 /* Make sure there are no duplicates. */
193 for (next = o + 1; next->name; ++next) {
195 * Some compilers inline strcmp and the assert string is too long.
197 duplicated = strcmp(o->name, next->name) == 0;
201 if (o->name[0] == '\0') {
202 assert(unknown == NULL);
204 assert(unknown->valtype == 0 || unknown->valtype == '-');
210 static OPT_PAIR formats[] = {
211 {"PEM/DER", OPT_FMT_PEMDER},
212 {"pkcs12", OPT_FMT_PKCS12},
213 {"smime", OPT_FMT_SMIME},
214 {"engine", OPT_FMT_ENGINE},
215 {"msblob", OPT_FMT_MSBLOB},
216 {"netscape", OPT_FMT_NETSCAPE},
217 {"nss", OPT_FMT_NSS},
218 {"text", OPT_FMT_TEXT},
219 {"http", OPT_FMT_HTTP},
220 {"pvk", OPT_FMT_PVK},
224 /* Print an error message about a failed format parse. */
225 int opt_format_error(const char *s, unsigned long flags)
229 if (flags == OPT_FMT_PEMDER)
230 BIO_printf(bio_err, "%s: Bad format \"%s\"; must be pem or der\n",
233 BIO_printf(bio_err, "%s: Bad format \"%s\"; must be one of:\n",
235 for (ap = formats; ap->name; ap++)
236 if (flags & ap->retval)
237 BIO_printf(bio_err, " %s\n", ap->name);
242 /* Parse a format string, put it into *result; return 0 on failure, else 1. */
243 int opt_format(const char *s, unsigned long flags, int *result)
250 if ((flags & OPT_FMT_PEMDER) == 0)
251 return opt_format_error(s, flags);
252 *result = FORMAT_ASN1;
256 if ((flags & OPT_FMT_TEXT) == 0)
257 return opt_format_error(s, flags);
258 *result = FORMAT_TEXT;
262 if ((flags & OPT_FMT_NSS) == 0)
263 return opt_format_error(s, flags);
264 if (strcmp(s, "NSS") != 0 && strcmp(s, "nss") != 0)
265 return opt_format_error(s, flags);
266 *result = FORMAT_NSS;
270 if ((flags & OPT_FMT_SMIME) == 0)
271 return opt_format_error(s, flags);
272 *result = FORMAT_SMIME;
276 if ((flags & OPT_FMT_MSBLOB) == 0)
277 return opt_format_error(s, flags);
278 *result = FORMAT_MSBLOB;
282 if ((flags & OPT_FMT_ENGINE) == 0)
283 return opt_format_error(s, flags);
284 *result = FORMAT_ENGINE;
288 if ((flags & OPT_FMT_HTTP) == 0)
289 return opt_format_error(s, flags);
290 *result = FORMAT_HTTP;
293 if ((flags & OPT_FMT_PKCS12) == 0)
294 return opt_format_error(s, flags);
295 *result = FORMAT_PKCS12;
299 if (s[1] == '\0' || strcmp(s, "PEM") == 0 || strcmp(s, "pem") == 0) {
300 if ((flags & OPT_FMT_PEMDER) == 0)
301 return opt_format_error(s, flags);
302 *result = FORMAT_PEM;
303 } else if (strcmp(s, "PVK") == 0 || strcmp(s, "pvk") == 0) {
304 if ((flags & OPT_FMT_PVK) == 0)
305 return opt_format_error(s, flags);
306 *result = FORMAT_PVK;
307 } else if (strcmp(s, "P12") == 0 || strcmp(s, "p12") == 0
308 || strcmp(s, "PKCS12") == 0 || strcmp(s, "pkcs12") == 0) {
309 if ((flags & OPT_FMT_PKCS12) == 0)
310 return opt_format_error(s, flags);
311 *result = FORMAT_PKCS12;
319 /* Parse a cipher name, put it in *EVP_CIPHER; return 0 on failure, else 1. */
320 int opt_cipher(const char *name, const EVP_CIPHER **cipherp)
322 *cipherp = EVP_get_cipherbyname(name);
325 BIO_printf(bio_err, "%s: Unknown cipher %s\n", prog, name);
330 * Parse message digest name, put it in *EVP_MD; return 0 on failure, else 1.
332 int opt_md(const char *name, const EVP_MD **mdp)
334 *mdp = EVP_get_digestbyname(name);
337 BIO_printf(bio_err, "%s: Unknown digest %s\n", prog, name);
341 /* Look through a list of name/value pairs. */
342 int opt_pair(const char *name, const OPT_PAIR* pairs, int *result)
346 for (pp = pairs; pp->name; pp++)
347 if (strcmp(pp->name, name) == 0) {
348 *result = pp->retval;
351 BIO_printf(bio_err, "%s: Value must be one of:\n", prog);
352 for (pp = pairs; pp->name; pp++)
353 BIO_printf(bio_err, "\t%s\n", pp->name);
357 /* Parse an int, put it into *result; return 0 on failure, else 1. */
358 int opt_int(const char *value, int *result)
362 if (!opt_long(value, &l))
366 BIO_printf(bio_err, "%s: Value \"%s\" outside integer range\n",
373 /* Parse a long, put it into *result; return 0 on failure, else 1. */
374 int opt_long(const char *value, long *result)
380 l = strtol(value, &endp, 0);
383 || ((l == LONG_MAX || l == LONG_MIN) && errno == ERANGE)
384 || (l == 0 && errno != 0)) {
385 BIO_printf(bio_err, "%s: Can't parse \"%s\" as a number\n",
395 #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L && \
396 defined(INTMAX_MAX) && defined(UINTMAX_MAX)
398 /* Parse an intmax_t, put it into *result; return 0 on failure, else 1. */
399 int opt_imax(const char *value, intmax_t *result)
405 m = strtoimax(value, &endp, 0);
408 || ((m == INTMAX_MAX || m == INTMAX_MIN) && errno == ERANGE)
409 || (m == 0 && errno != 0)) {
410 BIO_printf(bio_err, "%s: Can't parse \"%s\" as a number\n",
420 /* Parse a uintmax_t, put it into *result; return 0 on failure, else 1. */
421 int opt_umax(const char *value, uintmax_t *result)
427 m = strtoumax(value, &endp, 0);
430 || (m == UINTMAX_MAX && errno == ERANGE)
431 || (m == 0 && errno != 0)) {
432 BIO_printf(bio_err, "%s: Can't parse \"%s\" as a number\n",
444 * Parse an unsigned long, put it into *result; return 0 on failure, else 1.
446 int opt_ulong(const char *value, unsigned long *result)
452 l = strtoul(value, &endptr, 0);
455 || ((l == ULONG_MAX) && errno == ERANGE)
456 || (l == 0 && errno != 0)) {
457 BIO_printf(bio_err, "%s: Can't parse \"%s\" as an unsigned number\n",
468 * We pass opt as an int but cast it to "enum range" so that all the
469 * items in the OPT_V_ENUM enumeration are caught; this makes -Wswitch
470 * in gcc do the right thing.
472 enum range { OPT_V_ENUM };
474 int opt_verify(int opt, X509_VERIFY_PARAM *vpm)
480 const X509_VERIFY_PARAM *vtmp;
483 assert(opt > OPT_V__FIRST);
484 assert(opt < OPT_V__LAST);
486 switch ((enum range)opt) {
491 otmp = OBJ_txt2obj(opt_arg(), 0);
493 BIO_printf(bio_err, "%s: Invalid Policy %s\n", prog, opt_arg());
496 X509_VERIFY_PARAM_add0_policy(vpm, otmp);
499 /* purpose name -> purpose index */
500 i = X509_PURPOSE_get_by_sname(opt_arg());
502 BIO_printf(bio_err, "%s: Invalid purpose %s\n", prog, opt_arg());
506 /* purpose index -> purpose object */
507 xptmp = X509_PURPOSE_get0(i);
509 /* purpose object -> purpose value */
510 i = X509_PURPOSE_get_id(xptmp);
512 if (!X509_VERIFY_PARAM_set_purpose(vpm, i)) {
514 "%s: Internal error setting purpose %s\n",
519 case OPT_V_VERIFY_NAME:
520 vtmp = X509_VERIFY_PARAM_lookup(opt_arg());
522 BIO_printf(bio_err, "%s: Invalid verify name %s\n",
526 X509_VERIFY_PARAM_set1(vpm, vtmp);
528 case OPT_V_VERIFY_DEPTH:
531 X509_VERIFY_PARAM_set_depth(vpm, i);
534 if (!opt_imax(opt_arg(), &t))
536 if (t != (time_t)t) {
537 BIO_printf(bio_err, "%s: epoch time out of range %s\n",
541 X509_VERIFY_PARAM_set_time(vpm, (time_t)t);
543 case OPT_V_VERIFY_HOSTNAME:
544 if (!X509_VERIFY_PARAM_set1_host(vpm, opt_arg(), 0))
547 case OPT_V_VERIFY_EMAIL:
548 if (!X509_VERIFY_PARAM_set1_email(vpm, opt_arg(), 0))
551 case OPT_V_VERIFY_IP:
552 if (!X509_VERIFY_PARAM_set1_ip_asc(vpm, opt_arg()))
555 case OPT_V_IGNORE_CRITICAL:
556 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_IGNORE_CRITICAL);
558 case OPT_V_ISSUER_CHECKS:
559 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_CB_ISSUER_CHECK);
561 case OPT_V_CRL_CHECK:
562 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_CRL_CHECK);
564 case OPT_V_CRL_CHECK_ALL:
565 X509_VERIFY_PARAM_set_flags(vpm,
566 X509_V_FLAG_CRL_CHECK |
567 X509_V_FLAG_CRL_CHECK_ALL);
569 case OPT_V_POLICY_CHECK:
570 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_POLICY_CHECK);
572 case OPT_V_EXPLICIT_POLICY:
573 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_EXPLICIT_POLICY);
575 case OPT_V_INHIBIT_ANY:
576 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_INHIBIT_ANY);
578 case OPT_V_INHIBIT_MAP:
579 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_INHIBIT_MAP);
581 case OPT_V_X509_STRICT:
582 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_X509_STRICT);
584 case OPT_V_EXTENDED_CRL:
585 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_EXTENDED_CRL_SUPPORT);
587 case OPT_V_USE_DELTAS:
588 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_USE_DELTAS);
590 case OPT_V_POLICY_PRINT:
591 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_NOTIFY_POLICY);
593 case OPT_V_CHECK_SS_SIG:
594 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_CHECK_SS_SIGNATURE);
596 case OPT_V_TRUSTED_FIRST:
597 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_TRUSTED_FIRST);
599 case OPT_V_SUITEB_128_ONLY:
600 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_SUITEB_128_LOS_ONLY);
602 case OPT_V_SUITEB_128:
603 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_SUITEB_128_LOS);
605 case OPT_V_SUITEB_192:
606 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_SUITEB_192_LOS);
608 case OPT_V_PARTIAL_CHAIN:
609 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_PARTIAL_CHAIN);
611 case OPT_V_NO_ALT_CHAINS:
612 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_NO_ALT_CHAINS);
614 case OPT_V_NO_CHECK_TIME:
615 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_NO_CHECK_TIME);
623 * Parse the next flag (and value if specified), return 0 if done, -1 on
624 * error, otherwise the flag's retval.
634 ossl_uintmax_t umval;
636 /* Look at current arg; at end of the list? */
642 /* If word doesn't start with a -, we're done. */
646 /* Hit "--" ? We're done. */
648 if (strcmp(p, "--") == 0)
651 /* Allow -nnn and --nnn */
656 /* If we have --flag=foo, snip it off */
657 if ((arg = strchr(p, '=')) != NULL)
659 for (o = opts; o->name; ++o) {
660 /* If not this option, move on to the next one. */
661 if (strcmp(p, o->name) != 0)
664 /* If it doesn't take a value, make sure none was given. */
665 if (o->valtype == 0 || o->valtype == '-') {
668 "%s: Option -%s does not take a value\n", prog, p);
674 /* Want a value; get the next param if =foo not used. */
676 if (argv[opt_index] == NULL) {
678 "%s: Option -%s needs a value\n", prog, o->name);
681 arg = argv[opt_index++];
684 /* Syntax-check value. */
685 switch (o->valtype) {
691 if (app_isdir(arg) >= 0)
693 BIO_printf(bio_err, "%s: Not a directory: %s\n", prog, arg);
697 if (strcmp(arg, "-") == 0 || app_access(arg, R_OK) >= 0)
700 "%s: Cannot open input file %s, %s\n",
701 prog, arg, strerror(errno));
705 if (strcmp(arg, "-") == 0 || app_access(arg, W_OK) >= 0 || errno == ENOENT)
708 "%s: Cannot open output file %s, %s\n",
709 prog, arg, strerror(errno));
713 if (!opt_int(arg, &ival)
714 || (o->valtype == 'p' && ival <= 0)) {
716 "%s: Non-positive number \"%s\" for -%s\n",
722 if (!opt_imax(arg, &imval)) {
724 "%s: Invalid number \"%s\" for -%s\n",
730 if (!opt_umax(arg, &umval)) {
732 "%s: Invalid number \"%s\" for -%s\n",
738 if (!opt_long(arg, &lval)) {
740 "%s: Invalid number \"%s\" for -%s\n",
746 if (!opt_ulong(arg, &ulval)) {
748 "%s: Invalid number \"%s\" for -%s\n",
756 o->valtype == 'F' ? OPT_FMT_PEMDER
757 : OPT_FMT_ANY, &ival))
760 "%s: Invalid format \"%s\" for -%s\n",
765 /* Return the flag value. */
768 if (unknown != NULL) {
770 return unknown->retval;
772 BIO_printf(bio_err, "%s: Option unknown option -%s\n", prog, p);
776 /* Return the most recent flag parameter. */
782 /* Return the most recent flag. */
788 /* Return the unknown option. */
789 char *opt_unknown(void)
794 /* Return the rest of the arguments after parsing flags. */
795 char **opt_rest(void)
797 return &argv[opt_index];
800 /* How many items in remaining args? */
801 int opt_num_rest(void)
806 for (pp = opt_rest(); *pp; pp++, i++)
811 /* Return a string describing the parameter type. */
812 static const char *valtype2param(const OPTIONS *o)
814 switch (o->valtype) {
839 void opt_help(const OPTIONS *list)
849 /* Starts with its own help message? */
850 standard_prolog = list[0].name != OPT_HELP_STR;
852 /* Find the widest help. */
853 for (o = list; o->name; o++) {
854 if (o->name == OPT_MORE_STR)
856 i = 2 + (int)strlen(o->name);
857 if (o->valtype != '-')
858 i += 1 + strlen(valtype2param(o));
859 if (i < MAX_OPT_HELP_WIDTH && i > width)
861 assert(i < (int)sizeof start);
865 BIO_printf(bio_err, "Usage: %s [options]\nValid options are:\n",
868 /* Now let's print. */
869 for (o = list; o->name; o++) {
870 help = o->helpstr ? o->helpstr : "(No additional info)";
871 if (o->name == OPT_HELP_STR) {
872 BIO_printf(bio_err, help, prog);
877 memset(start, ' ', sizeof(start) - 1);
878 start[sizeof start - 1] = '\0';
880 if (o->name == OPT_MORE_STR) {
881 /* Continuation of previous line; padd and print. */
883 BIO_printf(bio_err, "%s %s\n", start, help);
887 /* Build up the "-flag [param]" part. */
892 p += strlen(strcpy(p, o->name));
895 if (o->valtype != '-') {
897 p += strlen(strcpy(p, valtype2param(o)));
900 if ((int)(p - start) >= MAX_OPT_HELP_WIDTH) {
902 BIO_printf(bio_err, "%s\n", start);
903 memset(start, ' ', sizeof(start));
906 BIO_printf(bio_err, "%s %s\n", start, help);
910 #ifdef COMPILE_STANDALONE_TEST_DRIVER
911 # include <sys/stat.h>
913 typedef enum OPTION_choice {
914 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
915 OPT_IN, OPT_INFORM, OPT_OUT, OPT_COUNT, OPT_U, OPT_FLAG,
919 static OPTIONS options[] = {
920 {OPT_HELP_STR, 1, '-', "Usage: %s flags\n"},
921 {OPT_HELP_STR, 1, '-', "Valid options are:\n"},
922 {"help", OPT_HELP, '-', "Display this summary"},
923 {"in", OPT_IN, '<', "input file"},
924 {OPT_MORE_STR, 1, '-', "more detail about input"},
925 {"inform", OPT_INFORM, 'f', "input file format; defaults to pem"},
926 {"out", OPT_OUT, '>', "output file"},
927 {"count", OPT_COUNT, 'p', "a counter greater than zero"},
928 {"u", OPT_U, 'u', "an unsigned number"},
929 {"flag", OPT_FLAG, 0, "just some flag"},
930 {"str", OPT_STR, 's', "the magic word"},
931 {"areallyverylongoption", OPT_HELP, '-', "long way for help"},
937 int app_isdir(const char *name)
941 return name != NULL && stat(name, &sb) >= 0 && S_ISDIR(sb.st_mode);
944 int main(int ac, char **av)
950 bio_err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
952 prog = opt_init(ac, av, options);
953 while ((o = opt_next()) != OPT_EOF) {
958 printf("%s: Usage error; try -help.\n", prog);
964 printf("in %s\n", opt_arg());
967 printf("inform %s\n", opt_arg());
970 printf("out %s\n", opt_arg());
973 printf("count %s\n", opt_arg());
976 printf("u %s\n", opt_arg());
982 printf("str %s\n", opt_arg());
986 argc = opt_num_rest();
989 printf("args = %d\n", argc);
992 printf(" %s\n", *argv++);