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)
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))
101 /* Copy over the name, in lowercase. */
102 if (n > sizeof prog - 1)
104 for (q = prog, i = 0; i < n; i++, p++)
105 *q++ = isupper(*p) ? tolower(*p) : *p;
110 #elif defined(OPENSSL_SYS_VMS)
112 char *opt_progname(const char *argv0)
116 /* Find last special charcter sys:[foo.bar]openssl */
117 for (p = argv0 + strlen(argv0); --p > argv0;)
118 if (*p == ':' || *p == ']' || *p == '>') {
124 strncpy(prog, p, sizeof prog - 1);
125 prog[sizeof prog - 1] = '\0';
126 if (q != NULL && q - p < sizeof prog)
133 char *opt_progname(const char *argv0)
137 /* Could use strchr, but this is like the ones above. */
138 for (p = argv0 + strlen(argv0); --p > argv0;)
143 strncpy(prog, p, sizeof prog - 1);
144 prog[sizeof prog - 1] = '\0';
149 char *opt_getprog(void)
154 /* Set up the arg parsing. */
155 char *opt_init(int ac, char **av, const OPTIONS *o)
165 for (; o->name; ++o) {
171 if (o->name == OPT_HELP_STR || o->name == OPT_MORE_STR)
176 /* Make sure options are legit. */
177 assert(o->name[0] != '-');
178 assert(o->retval > 0);
180 case 0: case '-': case '/': case '<': case '>': case 'E': case 'F':
181 case 'M': case 'U': case 'f': case 'l': case 'n': case 'p': case 's':
188 /* Make sure there are no duplicates. */
189 for (next = o + 1; next->name; ++next) {
191 * Some compilers inline strcmp and the assert string is too long.
193 duplicated = strcmp(o->name, next->name) == 0;
197 if (o->name[0] == '\0') {
198 assert(unknown == NULL);
200 assert(unknown->valtype == 0 || unknown->valtype == '-');
206 static OPT_PAIR formats[] = {
207 {"PEM/DER", OPT_FMT_PEMDER},
208 {"pkcs12", OPT_FMT_PKCS12},
209 {"smime", OPT_FMT_SMIME},
210 {"engine", OPT_FMT_ENGINE},
211 {"msblob", OPT_FMT_MSBLOB},
212 {"netscape", OPT_FMT_NETSCAPE},
213 {"nss", OPT_FMT_NSS},
214 {"text", OPT_FMT_TEXT},
215 {"http", OPT_FMT_HTTP},
216 {"pvk", OPT_FMT_PVK},
220 /* Print an error message about a failed format parse. */
221 int opt_format_error(const char *s, unsigned long flags)
225 if (flags == OPT_FMT_PEMDER)
226 BIO_printf(bio_err, "%s: Bad format \"%s\"; must be pem or der\n",
229 BIO_printf(bio_err, "%s: Bad format \"%s\"; must be one of:\n",
231 for (ap = formats; ap->name; ap++)
232 if (flags & ap->retval)
233 BIO_printf(bio_err, " %s\n", ap->name);
238 /* Parse a format string, put it into *result; return 0 on failure, else 1. */
239 int opt_format(const char *s, unsigned long flags, int *result)
246 if ((flags & OPT_FMT_PEMDER) == 0)
247 return opt_format_error(s, flags);
248 *result = FORMAT_ASN1;
252 if ((flags & OPT_FMT_TEXT) == 0)
253 return opt_format_error(s, flags);
254 *result = FORMAT_TEXT;
258 if ((flags & OPT_FMT_NSS) == 0)
259 return opt_format_error(s, flags);
260 if (strcmp(s, "NSS") != 0 && strcmp(s, "nss") != 0)
261 return opt_format_error(s, flags);
262 *result = FORMAT_NSS;
266 if ((flags & OPT_FMT_SMIME) == 0)
267 return opt_format_error(s, flags);
268 *result = FORMAT_SMIME;
272 if ((flags & OPT_FMT_MSBLOB) == 0)
273 return opt_format_error(s, flags);
274 *result = FORMAT_MSBLOB;
278 if ((flags & OPT_FMT_ENGINE) == 0)
279 return opt_format_error(s, flags);
280 *result = FORMAT_ENGINE;
284 if ((flags & OPT_FMT_HTTP) == 0)
285 return opt_format_error(s, flags);
286 *result = FORMAT_HTTP;
289 if ((flags & OPT_FMT_PKCS12) == 0)
290 return opt_format_error(s, flags);
291 *result = FORMAT_PKCS12;
295 if (s[1] == '\0' || strcmp(s, "PEM") == 0 || strcmp(s, "pem") == 0) {
296 if ((flags & OPT_FMT_PEMDER) == 0)
297 return opt_format_error(s, flags);
298 *result = FORMAT_PEM;
299 } else if (strcmp(s, "PVK") == 0 || strcmp(s, "pvk") == 0) {
300 if ((flags & OPT_FMT_PVK) == 0)
301 return opt_format_error(s, flags);
302 *result = FORMAT_PVK;
303 } else if (strcmp(s, "P12") == 0 || strcmp(s, "p12") == 0
304 || strcmp(s, "PKCS12") == 0 || strcmp(s, "pkcs12") == 0) {
305 if ((flags & OPT_FMT_PKCS12) == 0)
306 return opt_format_error(s, flags);
307 *result = FORMAT_PKCS12;
315 /* Parse a cipher name, put it in *EVP_CIPHER; return 0 on failure, else 1. */
316 int opt_cipher(const char *name, const EVP_CIPHER **cipherp)
318 *cipherp = EVP_get_cipherbyname(name);
321 BIO_printf(bio_err, "%s: Unknown cipher %s\n", prog, name);
326 * Parse message digest name, put it in *EVP_MD; return 0 on failure, else 1.
328 int opt_md(const char *name, const EVP_MD **mdp)
330 *mdp = EVP_get_digestbyname(name);
333 BIO_printf(bio_err, "%s: Unknown digest %s\n", prog, name);
337 /* Look through a list of name/value pairs. */
338 int opt_pair(const char *name, const OPT_PAIR* pairs, int *result)
342 for (pp = pairs; pp->name; pp++)
343 if (strcmp(pp->name, name) == 0) {
344 *result = pp->retval;
347 BIO_printf(bio_err, "%s: Value must be one of:\n", prog);
348 for (pp = pairs; pp->name; pp++)
349 BIO_printf(bio_err, "\t%s\n", pp->name);
353 /* Parse an int, put it into *result; return 0 on failure, else 1. */
354 int opt_int(const char *value, int *result)
358 if (!opt_long(value, &l))
362 BIO_printf(bio_err, "%s: Value \"%s\" outside integer range\n",
369 /* Parse a long, put it into *result; return 0 on failure, else 1. */
370 int opt_long(const char *value, long *result)
376 l = strtol(value, &endp, 0);
379 || ((l == LONG_MAX || l == LONG_MIN) && errno == ERANGE)
380 || (l == 0 && errno != 0)) {
381 BIO_printf(bio_err, "%s: Can't parse \"%s\" as a number\n",
391 #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L && \
392 defined(INTMAX_MAX) && defined(UINTMAX_MAX)
394 /* Parse an intmax_t, put it into *result; return 0 on failure, else 1. */
395 int opt_imax(const char *value, intmax_t *result)
401 m = strtoimax(value, &endp, 0);
404 || ((m == INTMAX_MAX || m == INTMAX_MIN) && errno == ERANGE)
405 || (m == 0 && errno != 0)) {
406 BIO_printf(bio_err, "%s: Can't parse \"%s\" as a number\n",
416 /* Parse a uintmax_t, put it into *result; return 0 on failure, else 1. */
417 int opt_umax(const char *value, uintmax_t *result)
423 m = strtoumax(value, &endp, 0);
426 || (m == UINTMAX_MAX && errno == ERANGE)
427 || (m == 0 && errno != 0)) {
428 BIO_printf(bio_err, "%s: Can't parse \"%s\" as a number\n",
440 * Parse an unsigned long, put it into *result; return 0 on failure, else 1.
442 int opt_ulong(const char *value, unsigned long *result)
448 l = strtoul(value, &endptr, 0);
451 || ((l == ULONG_MAX) && errno == ERANGE)
452 || (l == 0 && errno != 0)) {
453 BIO_printf(bio_err, "%s: Can't parse \"%s\" as an unsigned number\n",
464 * We pass opt as an int but cast it to "enum range" so that all the
465 * items in the OPT_V_ENUM enumeration are caught; this makes -Wswitch
466 * in gcc do the right thing.
468 enum range { OPT_V_ENUM };
470 int opt_verify(int opt, X509_VERIFY_PARAM *vpm)
476 const X509_VERIFY_PARAM *vtmp;
479 assert(opt > OPT_V__FIRST);
480 assert(opt < OPT_V__LAST);
482 switch ((enum range)opt) {
487 otmp = OBJ_txt2obj(opt_arg(), 0);
489 BIO_printf(bio_err, "%s: Invalid Policy %s\n", prog, opt_arg());
492 X509_VERIFY_PARAM_add0_policy(vpm, otmp);
495 /* purpose name -> purpose index */
496 i = X509_PURPOSE_get_by_sname(opt_arg());
498 BIO_printf(bio_err, "%s: Invalid purpose %s\n", prog, opt_arg());
502 /* purpose index -> purpose object */
503 xptmp = X509_PURPOSE_get0(i);
505 /* purpose object -> purpose value */
506 i = X509_PURPOSE_get_id(xptmp);
508 if (!X509_VERIFY_PARAM_set_purpose(vpm, i)) {
510 "%s: Internal error setting purpose %s\n",
515 case OPT_V_VERIFY_NAME:
516 vtmp = X509_VERIFY_PARAM_lookup(opt_arg());
518 BIO_printf(bio_err, "%s: Invalid verify name %s\n",
522 X509_VERIFY_PARAM_set1(vpm, vtmp);
524 case OPT_V_VERIFY_DEPTH:
527 X509_VERIFY_PARAM_set_depth(vpm, i);
529 case OPT_V_VERIFY_AUTH_LEVEL:
532 X509_VERIFY_PARAM_set_auth_level(vpm, i);
535 if (!opt_imax(opt_arg(), &t))
537 if (t != (time_t)t) {
538 BIO_printf(bio_err, "%s: epoch time out of range %s\n",
542 X509_VERIFY_PARAM_set_time(vpm, (time_t)t);
544 case OPT_V_VERIFY_HOSTNAME:
545 if (!X509_VERIFY_PARAM_set1_host(vpm, opt_arg(), 0))
548 case OPT_V_VERIFY_EMAIL:
549 if (!X509_VERIFY_PARAM_set1_email(vpm, opt_arg(), 0))
552 case OPT_V_VERIFY_IP:
553 if (!X509_VERIFY_PARAM_set1_ip_asc(vpm, opt_arg()))
556 case OPT_V_IGNORE_CRITICAL:
557 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_IGNORE_CRITICAL);
559 case OPT_V_ISSUER_CHECKS:
560 /* NOP, deprecated */
562 case OPT_V_CRL_CHECK:
563 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_CRL_CHECK);
565 case OPT_V_CRL_CHECK_ALL:
566 X509_VERIFY_PARAM_set_flags(vpm,
567 X509_V_FLAG_CRL_CHECK |
568 X509_V_FLAG_CRL_CHECK_ALL);
570 case OPT_V_POLICY_CHECK:
571 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_POLICY_CHECK);
573 case OPT_V_EXPLICIT_POLICY:
574 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_EXPLICIT_POLICY);
576 case OPT_V_INHIBIT_ANY:
577 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_INHIBIT_ANY);
579 case OPT_V_INHIBIT_MAP:
580 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_INHIBIT_MAP);
582 case OPT_V_X509_STRICT:
583 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_X509_STRICT);
585 case OPT_V_EXTENDED_CRL:
586 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_EXTENDED_CRL_SUPPORT);
588 case OPT_V_USE_DELTAS:
589 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_USE_DELTAS);
591 case OPT_V_POLICY_PRINT:
592 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_NOTIFY_POLICY);
594 case OPT_V_CHECK_SS_SIG:
595 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_CHECK_SS_SIGNATURE);
597 case OPT_V_TRUSTED_FIRST:
598 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_TRUSTED_FIRST);
600 case OPT_V_SUITEB_128_ONLY:
601 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_SUITEB_128_LOS_ONLY);
603 case OPT_V_SUITEB_128:
604 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_SUITEB_128_LOS);
606 case OPT_V_SUITEB_192:
607 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_SUITEB_192_LOS);
609 case OPT_V_PARTIAL_CHAIN:
610 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_PARTIAL_CHAIN);
612 case OPT_V_NO_ALT_CHAINS:
613 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_NO_ALT_CHAINS);
615 case OPT_V_NO_CHECK_TIME:
616 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_NO_CHECK_TIME);
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 == '-') {
669 "%s: Option -%s does not take a value\n", prog, p);
675 /* Want a value; get the next param if =foo not used. */
677 if (argv[opt_index] == NULL) {
679 "%s: Option -%s needs a value\n", prog, o->name);
682 arg = argv[opt_index++];
685 /* Syntax-check value. */
686 switch (o->valtype) {
692 if (app_isdir(arg) >= 0)
694 BIO_printf(bio_err, "%s: Not a directory: %s\n", prog, arg);
698 if (strcmp(arg, "-") == 0 || app_access(arg, R_OK) >= 0)
701 "%s: Cannot open input file %s, %s\n",
702 prog, arg, strerror(errno));
706 if (strcmp(arg, "-") == 0 || app_access(arg, W_OK) >= 0 || errno == ENOENT)
709 "%s: Cannot open output file %s, %s\n",
710 prog, arg, strerror(errno));
714 if (!opt_int(arg, &ival)
715 || (o->valtype == 'p' && ival <= 0)) {
717 "%s: Non-positive number \"%s\" for -%s\n",
723 if (!opt_imax(arg, &imval)) {
725 "%s: Invalid number \"%s\" for -%s\n",
731 if (!opt_umax(arg, &umval)) {
733 "%s: Invalid number \"%s\" for -%s\n",
739 if (!opt_long(arg, &lval)) {
741 "%s: Invalid number \"%s\" for -%s\n",
747 if (!opt_ulong(arg, &ulval)) {
749 "%s: Invalid number \"%s\" for -%s\n",
758 o->valtype == 'E' ? OPT_FMT_PDE :
759 o->valtype == 'F' ? OPT_FMT_PEMDER
760 : OPT_FMT_ANY, &ival))
763 "%s: Invalid format \"%s\" for -%s\n",
768 /* Return the flag value. */
771 if (unknown != NULL) {
773 return unknown->retval;
775 BIO_printf(bio_err, "%s: Option unknown option -%s\n", prog, p);
779 /* Return the most recent flag parameter. */
785 /* Return the most recent flag. */
791 /* Return the unknown option. */
792 char *opt_unknown(void)
797 /* Return the rest of the arguments after parsing flags. */
798 char **opt_rest(void)
800 return &argv[opt_index];
803 /* How many items in remaining args? */
804 int opt_num_rest(void)
809 for (pp = opt_rest(); *pp; pp++, i++)
814 /* Return a string describing the parameter type. */
815 static const char *valtype2param(const OPTIONS *o)
817 switch (o->valtype) {
838 return "PEM|DER|ENGINE";
851 void opt_help(const OPTIONS *list)
861 /* Starts with its own help message? */
862 standard_prolog = list[0].name != OPT_HELP_STR;
864 /* Find the widest help. */
865 for (o = list; o->name; o++) {
866 if (o->name == OPT_MORE_STR)
868 i = 2 + (int)strlen(o->name);
869 if (o->valtype != '-')
870 i += 1 + strlen(valtype2param(o));
871 if (i < MAX_OPT_HELP_WIDTH && i > width)
873 assert(i < (int)sizeof start);
877 BIO_printf(bio_err, "Usage: %s [options]\nValid options are:\n",
880 /* Now let's print. */
881 for (o = list; o->name; o++) {
882 help = o->helpstr ? o->helpstr : "(No additional info)";
883 if (o->name == OPT_HELP_STR) {
884 BIO_printf(bio_err, help, prog);
889 memset(start, ' ', sizeof(start) - 1);
890 start[sizeof start - 1] = '\0';
892 if (o->name == OPT_MORE_STR) {
893 /* Continuation of previous line; padd and print. */
895 BIO_printf(bio_err, "%s %s\n", start, help);
899 /* Build up the "-flag [param]" part. */
904 p += strlen(strcpy(p, o->name));
907 if (o->valtype != '-') {
909 p += strlen(strcpy(p, valtype2param(o)));
912 if ((int)(p - start) >= MAX_OPT_HELP_WIDTH) {
914 BIO_printf(bio_err, "%s\n", start);
915 memset(start, ' ', sizeof(start));
918 BIO_printf(bio_err, "%s %s\n", start, help);
922 #ifdef COMPILE_STANDALONE_TEST_DRIVER
923 # include <sys/stat.h>
925 typedef enum OPTION_choice {
926 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
927 OPT_IN, OPT_INFORM, OPT_OUT, OPT_COUNT, OPT_U, OPT_FLAG,
931 static OPTIONS options[] = {
932 {OPT_HELP_STR, 1, '-', "Usage: %s flags\n"},
933 {OPT_HELP_STR, 1, '-', "Valid options are:\n"},
934 {"help", OPT_HELP, '-', "Display this summary"},
935 {"in", OPT_IN, '<', "input file"},
936 {OPT_MORE_STR, 1, '-', "more detail about input"},
937 {"inform", OPT_INFORM, 'f', "input file format; defaults to pem"},
938 {"out", OPT_OUT, '>', "output file"},
939 {"count", OPT_COUNT, 'p', "a counter greater than zero"},
940 {"u", OPT_U, 'u', "an unsigned number"},
941 {"flag", OPT_FLAG, 0, "just some flag"},
942 {"str", OPT_STR, 's', "the magic word"},
943 {"areallyverylongoption", OPT_HELP, '-', "long way for help"},
949 int app_isdir(const char *name)
953 return name != NULL && stat(name, &sb) >= 0 && S_ISDIR(sb.st_mode);
956 int main(int ac, char **av)
962 bio_err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
964 prog = opt_init(ac, av, options);
965 while ((o = opt_next()) != OPT_EOF) {
970 printf("%s: Usage error; try -help.\n", prog);
976 printf("in %s\n", opt_arg());
979 printf("inform %s\n", opt_arg());
982 printf("out %s\n", opt_arg());
985 printf("count %s\n", opt_arg());
988 printf("u %s\n", opt_arg());
994 printf("str %s\n", opt_arg());
998 argc = opt_num_rest();
1001 printf("args = %d\n", argc);
1004 printf(" %s\n", *argv++);