2 * Copyright 2015-2021 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 "app_libctx.h"
16 #include "internal/nelem.h"
17 #include "internal/numbers.h"
19 #if !defined(OPENSSL_SYS_MSDOS)
27 #include <openssl/err.h>
28 #include <openssl/bio.h>
29 #include <openssl/x509v3.h>
31 #define MAX_OPT_HELP_WIDTH 30
32 const char OPT_HELP_STR
[] = "-H";
33 const char OPT_MORE_STR
[] = "-M";
34 const char OPT_SECTION_STR
[] = "-S";
35 const char OPT_PARAM_STR
[] = "-P";
44 static const char *unknown_name
;
45 static const OPTIONS
*unknown
;
46 static const OPTIONS
*opts
;
50 * Return the simple name of the program; removing various platform gunk.
52 #if defined(OPENSSL_SYS_WIN32)
54 const char *opt_path_end(const char *filename
)
58 /* find the last '/', '\' or ':' */
59 for (p
= filename
+ strlen(filename
); --p
> filename
; )
60 if (*p
== '/' || *p
== '\\' || *p
== ':') {
67 char *opt_progname(const char *argv0
)
73 p
= opt_path_end(argv0
);
75 /* Strip off trailing nonsense. */
78 (strcmp(&p
[n
- 4], ".exe") == 0 || strcmp(&p
[n
- 4], ".EXE") == 0))
81 /* Copy over the name, in lowercase. */
82 if (n
> sizeof(prog
) - 1)
84 for (q
= prog
, i
= 0; i
< n
; i
++, p
++)
85 *q
++ = tolower((unsigned char)*p
);
90 #elif defined(OPENSSL_SYS_VMS)
92 const char *opt_path_end(const char *filename
)
96 /* Find last special character sys:[foo.bar]openssl */
97 for (p
= filename
+ strlen(filename
); --p
> filename
;)
98 if (*p
== ':' || *p
== ']' || *p
== '>') {
105 char *opt_progname(const char *argv0
)
109 /* Find last special character sys:[foo.bar]openssl */
110 p
= opt_path_end(argv0
);
113 strncpy(prog
, p
, sizeof(prog
) - 1);
114 prog
[sizeof(prog
) - 1] = '\0';
115 if (q
!= NULL
&& q
- p
< sizeof(prog
))
122 const char *opt_path_end(const char *filename
)
126 /* Could use strchr, but this is like the ones above. */
127 for (p
= filename
+ strlen(filename
); --p
> filename
;)
135 char *opt_progname(const char *argv0
)
139 p
= opt_path_end(argv0
);
141 strncpy(prog
, p
, sizeof(prog
) - 1);
142 prog
[sizeof(prog
) - 1] = '\0';
147 char *opt_appname(const char *argv0
)
149 size_t len
= strlen(prog
);
152 BIO_snprintf(prog
+ len
, sizeof(prog
) - len
- 1, " %s", argv0
);
156 char *opt_getprog(void)
161 /* Set up the arg parsing. */
162 char *opt_init(int ac
, char **av
, const OPTIONS
*o
)
170 /* Make sure prog name is set for usage output */
171 (void)opt_progname(argv
[0]);
173 /* Check all options up until the PARAM marker (if present) */
174 for (; o
->name
!= NULL
&& o
->name
!= OPT_PARAM_STR
; ++o
) {
180 if (o
->name
== OPT_HELP_STR
181 || o
->name
== OPT_MORE_STR
182 || o
->name
== OPT_SECTION_STR
)
187 /* Make sure options are legit. */
188 OPENSSL_assert(o
->name
[0] != '-');
189 if (o
->valtype
== '.')
190 OPENSSL_assert(o
->retval
== OPT_PARAM
);
192 OPENSSL_assert(o
->retval
== OPT_DUP
|| o
->retval
> OPT_PARAM
);
194 case 0: case '-': case '.':
195 case '/': case '<': case '>': case 'E': case 'F':
196 case 'M': case 'U': case 'f': case 'l': case 'n': case 'p': case 's':
197 case 'u': case 'c': case ':': case 'N':
203 /* Make sure there are no duplicates. */
204 for (next
= o
+ 1; next
->name
; ++next
) {
206 * Some compilers inline strcmp and the assert string is too long.
208 duplicated
= next
->retval
!= OPT_DUP
209 && strcmp(o
->name
, next
->name
) == 0;
211 opt_printf_stderr("%s: Internal error: duplicate option %s\n",
213 OPENSSL_assert(!duplicated
);
217 if (o
->name
[0] == '\0') {
218 OPENSSL_assert(unknown_name
!= NULL
);
219 OPENSSL_assert(unknown
== NULL
);
221 OPENSSL_assert(unknown
->valtype
== 0 || unknown
->valtype
== '-');
227 static OPT_PAIR formats
[] = {
228 {"PEM/DER", OPT_FMT_PEMDER
},
229 {"pkcs12", OPT_FMT_PKCS12
},
230 {"smime", OPT_FMT_SMIME
},
231 {"engine", OPT_FMT_ENGINE
},
232 {"msblob", OPT_FMT_MSBLOB
},
233 {"nss", OPT_FMT_NSS
},
234 {"text", OPT_FMT_TEXT
},
235 {"http", OPT_FMT_HTTP
},
236 {"pvk", OPT_FMT_PVK
},
240 void opt_set_unknown_name(const char *name
)
245 /* Print an error message about a failed format parse. */
246 static int opt_format_error(const char *s
, unsigned long flags
)
250 if (flags
== OPT_FMT_PEMDER
) {
251 opt_printf_stderr("%s: Bad format \"%s\"; must be pem or der\n",
254 opt_printf_stderr("%s: Bad format \"%s\"; must be one of:\n",
256 for (ap
= formats
; ap
->name
; ap
++)
257 if (flags
& ap
->retval
)
258 opt_printf_stderr(" %s\n", ap
->name
);
263 /* Parse a format string, put it into *result; return 0 on failure, else 1. */
264 int opt_format(const char *s
, unsigned long flags
, int *result
)
268 opt_printf_stderr("%s: Bad format \"%s\"\n", prog
, s
);
272 if ((flags
& OPT_FMT_PEMDER
) == 0)
273 return opt_format_error(s
, flags
);
274 *result
= FORMAT_ASN1
;
278 if ((flags
& OPT_FMT_TEXT
) == 0)
279 return opt_format_error(s
, flags
);
280 *result
= FORMAT_TEXT
;
284 if ((flags
& OPT_FMT_NSS
) == 0)
285 return opt_format_error(s
, flags
);
286 if (strcmp(s
, "NSS") != 0 && strcmp(s
, "nss") != 0)
287 return opt_format_error(s
, flags
);
288 *result
= FORMAT_NSS
;
292 if ((flags
& OPT_FMT_SMIME
) == 0)
293 return opt_format_error(s
, flags
);
294 *result
= FORMAT_SMIME
;
298 if ((flags
& OPT_FMT_MSBLOB
) == 0)
299 return opt_format_error(s
, flags
);
300 *result
= FORMAT_MSBLOB
;
304 if ((flags
& OPT_FMT_ENGINE
) == 0)
305 return opt_format_error(s
, flags
);
306 *result
= FORMAT_ENGINE
;
310 if ((flags
& OPT_FMT_HTTP
) == 0)
311 return opt_format_error(s
, flags
);
312 *result
= FORMAT_HTTP
;
315 if ((flags
& OPT_FMT_PKCS12
) == 0)
316 return opt_format_error(s
, flags
);
317 *result
= FORMAT_PKCS12
;
321 if (s
[1] == '\0' || strcmp(s
, "PEM") == 0 || strcmp(s
, "pem") == 0) {
322 if ((flags
& OPT_FMT_PEMDER
) == 0)
323 return opt_format_error(s
, flags
);
324 *result
= FORMAT_PEM
;
325 } else if (strcmp(s
, "PVK") == 0 || strcmp(s
, "pvk") == 0) {
326 if ((flags
& OPT_FMT_PVK
) == 0)
327 return opt_format_error(s
, flags
);
328 *result
= FORMAT_PVK
;
329 } else if (strcmp(s
, "P12") == 0 || strcmp(s
, "p12") == 0
330 || strcmp(s
, "PKCS12") == 0 || strcmp(s
, "pkcs12") == 0) {
331 if ((flags
& OPT_FMT_PKCS12
) == 0)
332 return opt_format_error(s
, flags
);
333 *result
= FORMAT_PKCS12
;
335 opt_printf_stderr("%s: Bad format \"%s\"\n", prog
, s
);
343 /* Return string representing the given format. */
344 static const char *format2str(int format
)
348 return "(undefined)";
372 /* Print an error message about unsuitable/unsupported format requested. */
373 void print_format_error(int format
, unsigned long flags
)
375 (void)opt_format_error(format2str(format
), flags
);
379 * Parse a cipher name, put it in *cipherp after freeing what was there, if
380 * cipherp is not NULL. Return 0 on failure, else 1.
382 int opt_cipher_silent(const char *name
, EVP_CIPHER
**cipherp
)
387 if ((c
= EVP_CIPHER_fetch(app_get0_libctx(), name
,
388 app_get0_propq())) != NULL
389 || (opt_legacy_okay()
390 && (c
= (EVP_CIPHER
*)EVP_get_cipherbyname(name
)) != NULL
)) {
392 if (cipherp
!= NULL
) {
393 EVP_CIPHER_free(*cipherp
);
400 ERR_clear_last_mark();
404 int opt_cipher_any(const char *name
, EVP_CIPHER
**cipherp
)
410 if ((ret
= opt_cipher_silent(name
, cipherp
)) == 0)
411 opt_printf_stderr("%s: Unknown option or cipher: %s\n", prog
, name
);
415 int opt_cipher(const char *name
, EVP_CIPHER
**cipherp
)
418 unsigned long int flags
;
419 EVP_CIPHER
*c
= NULL
;
423 if (opt_cipher_any(name
, &c
)) {
424 mode
= EVP_CIPHER_get_mode(c
);
425 flags
= EVP_CIPHER_get_flags(c
);
426 if (mode
== EVP_CIPH_XTS_MODE
) {
427 opt_printf_stderr("%s XTS ciphers not supported\n", prog
);
428 } else if ((flags
& EVP_CIPH_FLAG_AEAD_CIPHER
) != 0) {
429 opt_printf_stderr("%s: AEAD ciphers not supported\n", prog
);
440 * Parse message digest name, put it in *EVP_MD; return 0 on failure, else 1.
442 int opt_md_silent(const char *name
, EVP_MD
**mdp
)
447 if ((md
= EVP_MD_fetch(app_get0_libctx(), name
, app_get0_propq())) != NULL
448 || (opt_legacy_okay()
449 && (md
= (EVP_MD
*)EVP_get_digestbyname(name
)) != NULL
)) {
459 ERR_clear_last_mark();
463 int opt_md(const char *name
, EVP_MD
**mdp
)
469 if ((ret
= opt_md_silent(name
, mdp
)) == 0)
470 opt_printf_stderr("%s: Unknown option or message digest: %s\n",
475 int opt_check_md(const char *name
)
477 if (opt_md(name
, NULL
))
483 /* Look through a list of name/value pairs. */
484 int opt_pair(const char *name
, const OPT_PAIR
* pairs
, int *result
)
488 for (pp
= pairs
; pp
->name
; pp
++)
489 if (strcmp(pp
->name
, name
) == 0) {
490 *result
= pp
->retval
;
493 opt_printf_stderr("%s: Value must be one of:\n", prog
);
494 for (pp
= pairs
; pp
->name
; pp
++)
495 opt_printf_stderr("\t%s\n", pp
->name
);
499 /* Look through a list of valid names */
500 int opt_string(const char *name
, const char **options
)
504 for (p
= options
; *p
!= NULL
; p
++)
505 if (strcmp(*p
, name
) == 0)
507 opt_printf_stderr("%s: Value must be one of:\n", prog
);
508 for (p
= options
; *p
!= NULL
; p
++)
509 opt_printf_stderr("\t%s\n", *p
);
513 /* Parse an int, put it into *result; return 0 on failure, else 1. */
514 int opt_int(const char *value
, int *result
)
518 if (!opt_long(value
, &l
))
522 opt_printf_stderr("%s: Value \"%s\" outside integer range\n",
529 /* Parse and return an integer, assuming range has been checked before. */
530 int opt_int_arg(void)
534 (void)opt_int(arg
, &result
);
538 static void opt_number_error(const char *v
)
541 struct strstr_pair_st
{
545 {"0x", "a hexadecimal"},
546 {"0X", "a hexadecimal"},
550 for (i
= 0; i
< OSSL_NELEM(b
); i
++) {
551 if (strncmp(v
, b
[i
].prefix
, strlen(b
[i
].prefix
)) == 0) {
552 opt_printf_stderr("%s: Can't parse \"%s\" as %s number\n",
557 opt_printf_stderr("%s: Can't parse \"%s\" as a number\n", prog
, v
);
561 /* Parse a long, put it into *result; return 0 on failure, else 1. */
562 int opt_long(const char *value
, long *result
)
569 l
= strtol(value
, &endp
, 0);
572 || ((l
== LONG_MAX
|| l
== LONG_MIN
) && errno
== ERANGE
)
573 || (l
== 0 && errno
!= 0)) {
574 opt_number_error(value
);
583 #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L && \
584 defined(INTMAX_MAX) && defined(UINTMAX_MAX) && \
585 !defined(OPENSSL_NO_INTTYPES_H)
587 /* Parse an intmax_t, put it into *result; return 0 on failure, else 1. */
588 int opt_intmax(const char *value
, ossl_intmax_t
*result
)
595 m
= strtoimax(value
, &endp
, 0);
598 || ((m
== INTMAX_MAX
|| m
== INTMAX_MIN
)
600 || (m
== 0 && errno
!= 0)) {
601 opt_number_error(value
);
605 /* Ensure that the value in |m| is never too big for |*result| */
606 if (sizeof(m
) > sizeof(*result
)
607 && (m
< OSSL_INTMAX_MIN
|| m
> OSSL_INTMAX_MAX
)) {
608 opt_number_error(value
);
611 *result
= (ossl_intmax_t
)m
;
616 /* Parse a uintmax_t, put it into *result; return 0 on failure, else 1. */
617 int opt_uintmax(const char *value
, ossl_uintmax_t
*result
)
624 m
= strtoumax(value
, &endp
, 0);
627 || (m
== UINTMAX_MAX
&& errno
== ERANGE
)
628 || (m
== 0 && errno
!= 0)) {
629 opt_number_error(value
);
633 /* Ensure that the value in |m| is never too big for |*result| */
634 if (sizeof(m
) > sizeof(*result
)
635 && m
> OSSL_UINTMAX_MAX
) {
636 opt_number_error(value
);
639 *result
= (ossl_intmax_t
)m
;
644 /* Fallback implementations based on long */
645 int opt_intmax(const char *value
, ossl_intmax_t
*result
)
650 if ((ret
= opt_long(value
, &m
)))
655 int opt_uintmax(const char *value
, ossl_uintmax_t
*result
)
660 if ((ret
= opt_ulong(value
, &m
)))
667 * Parse an unsigned long, put it into *result; return 0 on failure, else 1.
669 int opt_ulong(const char *value
, unsigned long *result
)
676 l
= strtoul(value
, &endptr
, 0);
679 || ((l
== ULONG_MAX
) && errno
== ERANGE
)
680 || (l
== 0 && errno
!= 0)) {
681 opt_number_error(value
);
691 * We pass opt as an int but cast it to "enum range" so that all the
692 * items in the OPT_V_ENUM enumeration are caught; this makes -Wswitch
693 * in gcc do the right thing.
695 enum range
{ OPT_V_ENUM
};
697 int opt_verify(int opt
, X509_VERIFY_PARAM
*vpm
)
703 const X509_VERIFY_PARAM
*vtmp
;
705 OPENSSL_assert(vpm
!= NULL
);
706 OPENSSL_assert(opt
> OPT_V__FIRST
);
707 OPENSSL_assert(opt
< OPT_V__LAST
);
709 switch ((enum range
)opt
) {
714 otmp
= OBJ_txt2obj(opt_arg(), 0);
716 opt_printf_stderr("%s: Invalid Policy %s\n", prog
, opt_arg());
719 X509_VERIFY_PARAM_add0_policy(vpm
, otmp
);
722 /* purpose name -> purpose index */
723 i
= X509_PURPOSE_get_by_sname(opt_arg());
725 opt_printf_stderr("%s: Invalid purpose %s\n", prog
, opt_arg());
729 /* purpose index -> purpose object */
730 xptmp
= X509_PURPOSE_get0(i
);
732 /* purpose object -> purpose value */
733 i
= X509_PURPOSE_get_id(xptmp
);
735 if (!X509_VERIFY_PARAM_set_purpose(vpm
, i
)) {
736 opt_printf_stderr("%s: Internal error setting purpose %s\n",
741 case OPT_V_VERIFY_NAME
:
742 vtmp
= X509_VERIFY_PARAM_lookup(opt_arg());
744 opt_printf_stderr("%s: Invalid verify name %s\n",
748 X509_VERIFY_PARAM_set1(vpm
, vtmp
);
750 case OPT_V_VERIFY_DEPTH
:
753 X509_VERIFY_PARAM_set_depth(vpm
, i
);
755 case OPT_V_VERIFY_AUTH_LEVEL
:
758 X509_VERIFY_PARAM_set_auth_level(vpm
, i
);
761 if (!opt_intmax(opt_arg(), &t
))
763 if (t
!= (time_t)t
) {
764 opt_printf_stderr("%s: epoch time out of range %s\n",
768 X509_VERIFY_PARAM_set_time(vpm
, (time_t)t
);
770 case OPT_V_VERIFY_HOSTNAME
:
771 if (!X509_VERIFY_PARAM_set1_host(vpm
, opt_arg(), 0))
774 case OPT_V_VERIFY_EMAIL
:
775 if (!X509_VERIFY_PARAM_set1_email(vpm
, opt_arg(), 0))
778 case OPT_V_VERIFY_IP
:
779 if (!X509_VERIFY_PARAM_set1_ip_asc(vpm
, opt_arg()))
782 case OPT_V_IGNORE_CRITICAL
:
783 X509_VERIFY_PARAM_set_flags(vpm
, X509_V_FLAG_IGNORE_CRITICAL
);
785 case OPT_V_ISSUER_CHECKS
:
786 /* NOP, deprecated */
788 case OPT_V_CRL_CHECK
:
789 X509_VERIFY_PARAM_set_flags(vpm
, X509_V_FLAG_CRL_CHECK
);
791 case OPT_V_CRL_CHECK_ALL
:
792 X509_VERIFY_PARAM_set_flags(vpm
,
793 X509_V_FLAG_CRL_CHECK
|
794 X509_V_FLAG_CRL_CHECK_ALL
);
796 case OPT_V_POLICY_CHECK
:
797 X509_VERIFY_PARAM_set_flags(vpm
, X509_V_FLAG_POLICY_CHECK
);
799 case OPT_V_EXPLICIT_POLICY
:
800 X509_VERIFY_PARAM_set_flags(vpm
, X509_V_FLAG_EXPLICIT_POLICY
);
802 case OPT_V_INHIBIT_ANY
:
803 X509_VERIFY_PARAM_set_flags(vpm
, X509_V_FLAG_INHIBIT_ANY
);
805 case OPT_V_INHIBIT_MAP
:
806 X509_VERIFY_PARAM_set_flags(vpm
, X509_V_FLAG_INHIBIT_MAP
);
808 case OPT_V_X509_STRICT
:
809 X509_VERIFY_PARAM_set_flags(vpm
, X509_V_FLAG_X509_STRICT
);
811 case OPT_V_EXTENDED_CRL
:
812 X509_VERIFY_PARAM_set_flags(vpm
, X509_V_FLAG_EXTENDED_CRL_SUPPORT
);
814 case OPT_V_USE_DELTAS
:
815 X509_VERIFY_PARAM_set_flags(vpm
, X509_V_FLAG_USE_DELTAS
);
817 case OPT_V_POLICY_PRINT
:
818 X509_VERIFY_PARAM_set_flags(vpm
, X509_V_FLAG_NOTIFY_POLICY
);
820 case OPT_V_CHECK_SS_SIG
:
821 X509_VERIFY_PARAM_set_flags(vpm
, X509_V_FLAG_CHECK_SS_SIGNATURE
);
823 case OPT_V_TRUSTED_FIRST
:
824 X509_VERIFY_PARAM_set_flags(vpm
, X509_V_FLAG_TRUSTED_FIRST
);
826 case OPT_V_SUITEB_128_ONLY
:
827 X509_VERIFY_PARAM_set_flags(vpm
, X509_V_FLAG_SUITEB_128_LOS_ONLY
);
829 case OPT_V_SUITEB_128
:
830 X509_VERIFY_PARAM_set_flags(vpm
, X509_V_FLAG_SUITEB_128_LOS
);
832 case OPT_V_SUITEB_192
:
833 X509_VERIFY_PARAM_set_flags(vpm
, X509_V_FLAG_SUITEB_192_LOS
);
835 case OPT_V_PARTIAL_CHAIN
:
836 X509_VERIFY_PARAM_set_flags(vpm
, X509_V_FLAG_PARTIAL_CHAIN
);
838 case OPT_V_NO_ALT_CHAINS
:
839 X509_VERIFY_PARAM_set_flags(vpm
, X509_V_FLAG_NO_ALT_CHAINS
);
841 case OPT_V_NO_CHECK_TIME
:
842 X509_VERIFY_PARAM_set_flags(vpm
, X509_V_FLAG_NO_CHECK_TIME
);
844 case OPT_V_ALLOW_PROXY_CERTS
:
845 X509_VERIFY_PARAM_set_flags(vpm
, X509_V_FLAG_ALLOW_PROXY_CERTS
);
860 * Parse the next flag (and value if specified), return 0 if done, -1 on
861 * error, otherwise the flag's retval.
871 ossl_uintmax_t umval
;
873 /* Look at current arg; at end of the list? */
879 /* If word doesn't start with a -, we're done. */
883 /* Hit "--" ? We're done. */
885 if (strcmp(p
, "--") == 0)
888 /* Allow -nnn and --nnn */
893 /* If we have --flag=foo, snip it off */
894 if ((arg
= strchr(p
, '=')) != NULL
)
896 for (o
= opts
; o
->name
; ++o
) {
897 /* If not this option, move on to the next one. */
898 if (!(strcmp(p
, "h") == 0 && strcmp(o
->name
, "help") == 0)
899 && strcmp(p
, o
->name
) != 0)
902 /* If it doesn't take a value, make sure none was given. */
903 if (o
->valtype
== 0 || o
->valtype
== '-') {
905 opt_printf_stderr("%s: Option -%s does not take a value\n",
912 /* Want a value; get the next param if =foo not used. */
914 if (argv
[opt_index
] == NULL
) {
915 opt_printf_stderr("%s: Option -%s needs a value\n",
919 arg
= argv
[opt_index
++];
922 /* Syntax-check value. */
923 switch (o
->valtype
) {
933 if (opt_isdir(arg
) > 0)
935 opt_printf_stderr("%s: Not a directory: %s\n", prog
, arg
);
946 if (!opt_int(arg
, &ival
))
948 if (o
->valtype
== 'p' && ival
<= 0) {
949 opt_printf_stderr("%s: Non-positive number \"%s\" for option -%s\n",
953 if (o
->valtype
== 'N' && ival
< 0) {
954 opt_printf_stderr("%s: Negative number \"%s\" for option -%s\n",
960 if (!opt_intmax(arg
, &imval
))
964 if (!opt_uintmax(arg
, &umval
))
968 if (!opt_long(arg
, &lval
))
972 if (!opt_ulong(arg
, &ulval
))
980 o
->valtype
== 'c' ? OPT_FMT_PDS
:
981 o
->valtype
== 'E' ? OPT_FMT_PDE
:
982 o
->valtype
== 'F' ? OPT_FMT_PEMDER
983 : OPT_FMT_ANY
, &ival
))
985 opt_printf_stderr("%s: Invalid format \"%s\" for option -%s\n",
990 /* Return the flag value. */
993 if (unknown
!= NULL
) {
995 opt_printf_stderr("%s: Multiple %s or unknown options: -%s and -%s\n",
996 prog
, unknown_name
, dunno
, p
);
1000 return unknown
->retval
;
1002 opt_printf_stderr("%s: Unknown option: -%s\n", prog
, p
);
1006 /* Return the most recent flag parameter. */
1012 /* Return the most recent flag (option name including the preceding '-'). */
1013 char *opt_flag(void)
1018 /* Return the unknown option. */
1019 char *opt_unknown(void)
1024 /* Reset the unknown option; needed by ocsp to allow multiple digest options. */
1025 void reset_unknown(void)
1030 /* Return the rest of the arguments after parsing flags. */
1031 char **opt_rest(void)
1033 return &argv
[opt_index
];
1036 /* How many items in remaining args? */
1037 int opt_num_rest(void)
1042 for (pp
= opt_rest(); *pp
; pp
++, i
++)
1047 int opt_check_rest_arg(const char *expected
)
1049 char *opt
= *opt_rest();
1051 if (opt
== NULL
|| *opt
== '\0') {
1052 if (expected
== NULL
)
1054 opt_printf_stderr("%s: Missing argument: %s\n", prog
, expected
);
1057 if (expected
!= NULL
)
1059 if (opt_unknown() == NULL
)
1060 opt_printf_stderr("%s: Extra option: \"%s\"\n", prog
, opt
);
1062 opt_printf_stderr("%s: Extra (unknown) options: \"%s\" \"%s\"\n",
1063 prog
, opt_unknown(), opt
);
1067 /* Return a string describing the parameter type. */
1068 static const char *valtype2param(const OPTIONS
*o
)
1070 switch (o
->valtype
) {
1093 return "PEM|DER|ENGINE";
1108 static void opt_print(const OPTIONS
*o
, int doingparams
, int width
)
1114 help
= o
->helpstr
? o
->helpstr
: "(No additional info)";
1115 if (o
->name
== OPT_HELP_STR
) {
1116 opt_printf_stderr(help
, prog
);
1119 if (o
->name
== OPT_SECTION_STR
) {
1120 opt_printf_stderr("\n");
1121 opt_printf_stderr(help
, prog
);
1124 if (o
->name
== OPT_PARAM_STR
) {
1125 opt_printf_stderr("\nParameters:\n");
1129 /* Pad out prefix */
1130 memset(start
, ' ', sizeof(start
) - 1);
1131 start
[sizeof(start
) - 1] = '\0';
1133 if (o
->name
== OPT_MORE_STR
) {
1134 /* Continuation of previous line; pad and print. */
1135 start
[width
] = '\0';
1136 opt_printf_stderr("%s %s\n", start
, help
);
1140 /* Build up the "-flag [param]" part. */
1146 p
+= strlen(strcpy(p
, o
->name
));
1149 if (o
->valtype
!= '-') {
1151 p
+= strlen(strcpy(p
, valtype2param(o
)));
1154 if ((int)(p
- start
) >= MAX_OPT_HELP_WIDTH
) {
1156 opt_printf_stderr("%s\n", start
);
1157 memset(start
, ' ', sizeof(start
));
1159 start
[width
] = '\0';
1160 opt_printf_stderr("%s %s\n", start
, help
);
1163 void opt_help(const OPTIONS
*list
)
1166 int i
, sawparams
= 0, width
= 5;
1167 int standard_prolog
;
1170 /* Starts with its own help message? */
1171 standard_prolog
= list
[0].name
!= OPT_HELP_STR
;
1173 /* Find the widest help. */
1174 for (o
= list
; o
->name
; o
++) {
1175 if (o
->name
== OPT_MORE_STR
)
1177 i
= 2 + (int)strlen(o
->name
);
1178 if (o
->valtype
!= '-')
1179 i
+= 1 + strlen(valtype2param(o
));
1180 if (i
< MAX_OPT_HELP_WIDTH
&& i
> width
)
1182 OPENSSL_assert(i
< (int)sizeof(start
));
1185 if (standard_prolog
) {
1186 opt_printf_stderr("Usage: %s [options]\n", prog
);
1187 if (list
[0].name
!= OPT_SECTION_STR
)
1188 opt_printf_stderr("Valid options are:\n", prog
);
1191 /* Now let's print. */
1192 for (o
= list
; o
->name
; o
++) {
1193 if (o
->name
== OPT_PARAM_STR
)
1195 opt_print(o
, sawparams
, width
);
1199 /* opt_isdir section */
1201 # include <windows.h>
1202 int opt_isdir(const char *name
)
1205 # if defined(UNICODE) || defined(_UNICODE)
1206 size_t i
, len_0
= strlen(name
) + 1;
1207 WCHAR tempname
[MAX_PATH
];
1209 if (len_0
> MAX_PATH
)
1212 # if !defined(_WIN32_WCE) || _WIN32_WCE>=101
1213 if (!MultiByteToWideChar(CP_ACP
, 0, name
, len_0
, tempname
, MAX_PATH
))
1215 for (i
= 0; i
< len_0
; i
++)
1216 tempname
[i
] = (WCHAR
)name
[i
];
1218 attr
= GetFileAttributes(tempname
);
1220 attr
= GetFileAttributes(name
);
1222 if (attr
== INVALID_FILE_ATTRIBUTES
)
1224 return ((attr
& FILE_ATTRIBUTE_DIRECTORY
) != 0);
1227 # include <sys/stat.h>
1229 # if defined(_S_IFMT) && defined(_S_IFDIR)
1230 # define S_ISDIR(a) (((a) & _S_IFMT) == _S_IFDIR)
1232 # define S_ISDIR(a) (((a) & S_IFMT) == S_IFDIR)
1236 int opt_isdir(const char *name
)
1238 # if defined(S_ISDIR)
1241 if (stat(name
, &st
) == 0)
1242 return S_ISDIR(st
.st_mode
);