Update copyright year
[openssl.git] / apps / include / opt.h
1 /*
2  * Copyright 2018-2021 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9 #ifndef OSSL_APPS_OPT_H
10 #define OSSL_APPS_OPT_H
11
12 #include <sys/types.h>
13 #include <openssl/e_os2.h>
14 #include <openssl/types.h>
15 #include <stdarg.h>
16
17 /*
18  * Common verification options.
19  */
20 # define OPT_V_ENUM \
21         OPT_V__FIRST=2000, \
22         OPT_V_POLICY, OPT_V_PURPOSE, OPT_V_VERIFY_NAME, OPT_V_VERIFY_DEPTH, \
23         OPT_V_ATTIME, OPT_V_VERIFY_HOSTNAME, OPT_V_VERIFY_EMAIL, \
24         OPT_V_VERIFY_IP, OPT_V_IGNORE_CRITICAL, OPT_V_ISSUER_CHECKS, \
25         OPT_V_CRL_CHECK, OPT_V_CRL_CHECK_ALL, OPT_V_POLICY_CHECK, \
26         OPT_V_EXPLICIT_POLICY, OPT_V_INHIBIT_ANY, OPT_V_INHIBIT_MAP, \
27         OPT_V_X509_STRICT, OPT_V_EXTENDED_CRL, OPT_V_USE_DELTAS, \
28         OPT_V_POLICY_PRINT, OPT_V_CHECK_SS_SIG, OPT_V_TRUSTED_FIRST, \
29         OPT_V_SUITEB_128_ONLY, OPT_V_SUITEB_128, OPT_V_SUITEB_192, \
30         OPT_V_PARTIAL_CHAIN, OPT_V_NO_ALT_CHAINS, OPT_V_NO_CHECK_TIME, \
31         OPT_V_VERIFY_AUTH_LEVEL, OPT_V_ALLOW_PROXY_CERTS, \
32         OPT_V__LAST
33
34 # define OPT_V_OPTIONS \
35         OPT_SECTION("Validation"), \
36         { "policy", OPT_V_POLICY, 's', "adds policy to the acceptable policy set"}, \
37         { "purpose", OPT_V_PURPOSE, 's', \
38             "certificate chain purpose"}, \
39         { "verify_name", OPT_V_VERIFY_NAME, 's', "verification policy name"}, \
40         { "verify_depth", OPT_V_VERIFY_DEPTH, 'n', \
41             "chain depth limit" }, \
42         { "auth_level", OPT_V_VERIFY_AUTH_LEVEL, 'n', \
43             "chain authentication security level" }, \
44         { "attime", OPT_V_ATTIME, 'M', "verification epoch time" }, \
45         { "verify_hostname", OPT_V_VERIFY_HOSTNAME, 's', \
46             "expected peer hostname" }, \
47         { "verify_email", OPT_V_VERIFY_EMAIL, 's', \
48             "expected peer email" }, \
49         { "verify_ip", OPT_V_VERIFY_IP, 's', \
50             "expected peer IP address" }, \
51         { "ignore_critical", OPT_V_IGNORE_CRITICAL, '-', \
52             "permit unhandled critical extensions"}, \
53         { "issuer_checks", OPT_V_ISSUER_CHECKS, '-', "(deprecated)"}, \
54         { "crl_check", OPT_V_CRL_CHECK, '-', "check leaf certificate revocation" }, \
55         { "crl_check_all", OPT_V_CRL_CHECK_ALL, '-', "check full chain revocation" }, \
56         { "policy_check", OPT_V_POLICY_CHECK, '-', "perform rfc5280 policy checks"}, \
57         { "explicit_policy", OPT_V_EXPLICIT_POLICY, '-', \
58             "set policy variable require-explicit-policy"}, \
59         { "inhibit_any", OPT_V_INHIBIT_ANY, '-', \
60             "set policy variable inhibit-any-policy"}, \
61         { "inhibit_map", OPT_V_INHIBIT_MAP, '-', \
62             "set policy variable inhibit-policy-mapping"}, \
63         { "x509_strict", OPT_V_X509_STRICT, '-', \
64             "disable certificate compatibility work-arounds"}, \
65         { "extended_crl", OPT_V_EXTENDED_CRL, '-', \
66             "enable extended CRL features"}, \
67         { "use_deltas", OPT_V_USE_DELTAS, '-', \
68             "use delta CRLs"}, \
69         { "policy_print", OPT_V_POLICY_PRINT, '-', \
70             "print policy processing diagnostics"}, \
71         { "check_ss_sig", OPT_V_CHECK_SS_SIG, '-', \
72             "check root CA self-signatures"}, \
73         { "trusted_first", OPT_V_TRUSTED_FIRST, '-', \
74             "search trust store first (default)" }, \
75         { "suiteB_128_only", OPT_V_SUITEB_128_ONLY, '-', "Suite B 128-bit-only mode"}, \
76         { "suiteB_128", OPT_V_SUITEB_128, '-', \
77             "Suite B 128-bit mode allowing 192-bit algorithms"}, \
78         { "suiteB_192", OPT_V_SUITEB_192, '-', "Suite B 192-bit-only mode" }, \
79         { "partial_chain", OPT_V_PARTIAL_CHAIN, '-', \
80             "accept chains anchored by intermediate trust-store CAs"}, \
81         { "no_alt_chains", OPT_V_NO_ALT_CHAINS, '-', "(deprecated)" }, \
82         { "no_check_time", OPT_V_NO_CHECK_TIME, '-', "ignore certificate validity time" }, \
83         { "allow_proxy_certs", OPT_V_ALLOW_PROXY_CERTS, '-', "allow the use of proxy certificates" }
84
85 # define OPT_V_CASES \
86         OPT_V__FIRST: case OPT_V__LAST: break; \
87         case OPT_V_POLICY: \
88         case OPT_V_PURPOSE: \
89         case OPT_V_VERIFY_NAME: \
90         case OPT_V_VERIFY_DEPTH: \
91         case OPT_V_VERIFY_AUTH_LEVEL: \
92         case OPT_V_ATTIME: \
93         case OPT_V_VERIFY_HOSTNAME: \
94         case OPT_V_VERIFY_EMAIL: \
95         case OPT_V_VERIFY_IP: \
96         case OPT_V_IGNORE_CRITICAL: \
97         case OPT_V_ISSUER_CHECKS: \
98         case OPT_V_CRL_CHECK: \
99         case OPT_V_CRL_CHECK_ALL: \
100         case OPT_V_POLICY_CHECK: \
101         case OPT_V_EXPLICIT_POLICY: \
102         case OPT_V_INHIBIT_ANY: \
103         case OPT_V_INHIBIT_MAP: \
104         case OPT_V_X509_STRICT: \
105         case OPT_V_EXTENDED_CRL: \
106         case OPT_V_USE_DELTAS: \
107         case OPT_V_POLICY_PRINT: \
108         case OPT_V_CHECK_SS_SIG: \
109         case OPT_V_TRUSTED_FIRST: \
110         case OPT_V_SUITEB_128_ONLY: \
111         case OPT_V_SUITEB_128: \
112         case OPT_V_SUITEB_192: \
113         case OPT_V_PARTIAL_CHAIN: \
114         case OPT_V_NO_ALT_CHAINS: \
115         case OPT_V_NO_CHECK_TIME: \
116         case OPT_V_ALLOW_PROXY_CERTS
117
118 /*
119  * Common "extended validation" options.
120  */
121 # define OPT_X_ENUM \
122         OPT_X__FIRST=1000, \
123         OPT_X_KEY, OPT_X_CERT, OPT_X_CHAIN, OPT_X_CHAIN_BUILD, \
124         OPT_X_CERTFORM, OPT_X_KEYFORM, \
125         OPT_X__LAST
126
127 # define OPT_X_OPTIONS \
128         OPT_SECTION("Extended certificate"), \
129         { "xkey", OPT_X_KEY, '<', "key for Extended certificates"}, \
130         { "xcert", OPT_X_CERT, '<', "cert for Extended certificates"}, \
131         { "xchain", OPT_X_CHAIN, '<', "chain for Extended certificates"}, \
132         { "xchain_build", OPT_X_CHAIN_BUILD, '-', \
133             "build certificate chain for the extended certificates"}, \
134         { "xcertform", OPT_X_CERTFORM, 'F', \
135             "format of Extended certificate (PEM/DER/P12); has no effect" }, \
136         { "xkeyform", OPT_X_KEYFORM, 'F', \
137             "format of Extended certificate's key (DER/PEM/P12); has no effect"}
138
139 # define OPT_X_CASES \
140         OPT_X__FIRST: case OPT_X__LAST: break; \
141         case OPT_X_KEY: \
142         case OPT_X_CERT: \
143         case OPT_X_CHAIN: \
144         case OPT_X_CHAIN_BUILD: \
145         case OPT_X_CERTFORM: \
146         case OPT_X_KEYFORM
147
148 /*
149  * Common SSL options.
150  * Any changes here must be coordinated with ../ssl/ssl_conf.c
151  */
152 # define OPT_S_ENUM \
153         OPT_S__FIRST=3000, \
154         OPT_S_NOSSL3, OPT_S_NOTLS1, OPT_S_NOTLS1_1, OPT_S_NOTLS1_2, \
155         OPT_S_NOTLS1_3, OPT_S_BUGS, OPT_S_NO_COMP, OPT_S_NOTICKET, \
156         OPT_S_SERVERPREF, OPT_S_LEGACYRENEG, OPT_S_LEGACYCONN, \
157         OPT_S_ONRESUMP, OPT_S_NOLEGACYCONN, OPT_S_ALLOW_NO_DHE_KEX, \
158         OPT_S_PRIORITIZE_CHACHA, \
159         OPT_S_STRICT, OPT_S_SIGALGS, OPT_S_CLIENTSIGALGS, OPT_S_GROUPS, \
160         OPT_S_CURVES, OPT_S_NAMEDCURVE, OPT_S_CIPHER, OPT_S_CIPHERSUITES, \
161         OPT_S_RECORD_PADDING, OPT_S_DEBUGBROKE, OPT_S_COMP, \
162         OPT_S_MINPROTO, OPT_S_MAXPROTO, \
163         OPT_S_NO_RENEGOTIATION, OPT_S_NO_MIDDLEBOX, OPT_S__LAST
164
165 # define OPT_S_OPTIONS \
166         OPT_SECTION("TLS/SSL"), \
167         {"no_ssl3", OPT_S_NOSSL3, '-',"Just disable SSLv3" }, \
168         {"no_tls1", OPT_S_NOTLS1, '-', "Just disable TLSv1"}, \
169         {"no_tls1_1", OPT_S_NOTLS1_1, '-', "Just disable TLSv1.1" }, \
170         {"no_tls1_2", OPT_S_NOTLS1_2, '-', "Just disable TLSv1.2"}, \
171         {"no_tls1_3", OPT_S_NOTLS1_3, '-', "Just disable TLSv1.3"}, \
172         {"bugs", OPT_S_BUGS, '-', "Turn on SSL bug compatibility"}, \
173         {"no_comp", OPT_S_NO_COMP, '-', "Disable SSL/TLS compression (default)" }, \
174         {"comp", OPT_S_COMP, '-', "Use SSL/TLS-level compression" }, \
175         {"no_ticket", OPT_S_NOTICKET, '-', \
176             "Disable use of TLS session tickets"}, \
177         {"serverpref", OPT_S_SERVERPREF, '-', "Use server's cipher preferences"}, \
178         {"legacy_renegotiation", OPT_S_LEGACYRENEG, '-', \
179             "Enable use of legacy renegotiation (dangerous)"}, \
180         {"no_renegotiation", OPT_S_NO_RENEGOTIATION, '-', \
181             "Disable all renegotiation."}, \
182         {"legacy_server_connect", OPT_S_LEGACYCONN, '-', \
183             "Allow initial connection to servers that don't support RI"}, \
184         {"no_resumption_on_reneg", OPT_S_ONRESUMP, '-', \
185             "Disallow session resumption on renegotiation"}, \
186         {"no_legacy_server_connect", OPT_S_NOLEGACYCONN, '-', \
187             "Disallow initial connection to servers that don't support RI"}, \
188         {"allow_no_dhe_kex", OPT_S_ALLOW_NO_DHE_KEX, '-', \
189             "In TLSv1.3 allow non-(ec)dhe based key exchange on resumption"}, \
190         {"prioritize_chacha", OPT_S_PRIORITIZE_CHACHA, '-', \
191             "Prioritize ChaCha ciphers when preferred by clients"}, \
192         {"strict", OPT_S_STRICT, '-', \
193             "Enforce strict certificate checks as per TLS standard"}, \
194         {"sigalgs", OPT_S_SIGALGS, 's', \
195             "Signature algorithms to support (colon-separated list)" }, \
196         {"client_sigalgs", OPT_S_CLIENTSIGALGS, 's', \
197             "Signature algorithms to support for client certificate" \
198             " authentication (colon-separated list)" }, \
199         {"groups", OPT_S_GROUPS, 's', \
200             "Groups to advertise (colon-separated list)" }, \
201         {"curves", OPT_S_CURVES, 's', \
202             "Groups to advertise (colon-separated list)" }, \
203         {"named_curve", OPT_S_NAMEDCURVE, 's', \
204             "Elliptic curve used for ECDHE (server-side only)" }, \
205         {"cipher", OPT_S_CIPHER, 's', "Specify TLSv1.2 and below cipher list to be used"}, \
206         {"ciphersuites", OPT_S_CIPHERSUITES, 's', "Specify TLSv1.3 ciphersuites to be used"}, \
207         {"min_protocol", OPT_S_MINPROTO, 's', "Specify the minimum protocol version to be used"}, \
208         {"max_protocol", OPT_S_MAXPROTO, 's', "Specify the maximum protocol version to be used"}, \
209         {"record_padding", OPT_S_RECORD_PADDING, 's', \
210             "Block size to pad TLS 1.3 records to."}, \
211         {"debug_broken_protocol", OPT_S_DEBUGBROKE, '-', \
212             "Perform all sorts of protocol violations for testing purposes"}, \
213         {"no_middlebox", OPT_S_NO_MIDDLEBOX, '-', \
214             "Disable TLSv1.3 middlebox compat mode" }
215
216 # define OPT_S_CASES \
217         OPT_S__FIRST: case OPT_S__LAST: break; \
218         case OPT_S_NOSSL3: \
219         case OPT_S_NOTLS1: \
220         case OPT_S_NOTLS1_1: \
221         case OPT_S_NOTLS1_2: \
222         case OPT_S_NOTLS1_3: \
223         case OPT_S_BUGS: \
224         case OPT_S_NO_COMP: \
225         case OPT_S_COMP: \
226         case OPT_S_NOTICKET: \
227         case OPT_S_SERVERPREF: \
228         case OPT_S_LEGACYRENEG: \
229         case OPT_S_LEGACYCONN: \
230         case OPT_S_ONRESUMP: \
231         case OPT_S_NOLEGACYCONN: \
232         case OPT_S_ALLOW_NO_DHE_KEX: \
233         case OPT_S_PRIORITIZE_CHACHA: \
234         case OPT_S_STRICT: \
235         case OPT_S_SIGALGS: \
236         case OPT_S_CLIENTSIGALGS: \
237         case OPT_S_GROUPS: \
238         case OPT_S_CURVES: \
239         case OPT_S_NAMEDCURVE: \
240         case OPT_S_CIPHER: \
241         case OPT_S_CIPHERSUITES: \
242         case OPT_S_RECORD_PADDING: \
243         case OPT_S_NO_RENEGOTIATION: \
244         case OPT_S_MINPROTO: \
245         case OPT_S_MAXPROTO: \
246         case OPT_S_DEBUGBROKE: \
247         case OPT_S_NO_MIDDLEBOX
248
249 #define IS_NO_PROT_FLAG(o) \
250  (o == OPT_S_NOSSL3 || o == OPT_S_NOTLS1 || o == OPT_S_NOTLS1_1 \
251   || o == OPT_S_NOTLS1_2 || o == OPT_S_NOTLS1_3)
252
253 /*
254  * Random state options.
255  */
256 # define OPT_R_ENUM \
257         OPT_R__FIRST=1500, OPT_R_RAND, OPT_R_WRITERAND, OPT_R__LAST
258
259 # define OPT_R_OPTIONS \
260     OPT_SECTION("Random state"), \
261     {"rand", OPT_R_RAND, 's', "Load the file(s) into the random number generator"}, \
262     {"writerand", OPT_R_WRITERAND, '>', "Write random data to the specified file"}
263
264 # define OPT_R_CASES \
265         OPT_R__FIRST: case OPT_R__LAST: break; \
266         case OPT_R_RAND: case OPT_R_WRITERAND
267
268 /*
269  * Provider options.
270  */
271 # define OPT_PROV_ENUM \
272         OPT_PROV__FIRST=1600, \
273         OPT_PROV_PROVIDER, OPT_PROV_PROVIDER_PATH, \
274         OPT_PROV__LAST
275
276 # define OPT_CONFIG_OPTION \
277         { "config", OPT_CONFIG, '<', "Load a configuration file (this may load modules)" }
278
279 # define OPT_PROV_OPTIONS \
280         OPT_SECTION("Provider"), \
281         { "provider-path", OPT_PROV_PROVIDER_PATH, 's', "Provider load path (must be before 'provider' argument if required)" }, \
282         { "provider", OPT_PROV_PROVIDER, 's', "Provider to load (can be specified multiple times)" }
283
284 # define OPT_PROV_CASES \
285         OPT_PROV__FIRST: case OPT_PROV__LAST: break; \
286         case OPT_PROV_PROVIDER: \
287         case OPT_PROV_PROVIDER_PATH
288
289 /*
290  * Option parsing.
291  */
292 extern const char OPT_HELP_STR[];
293 extern const char OPT_MORE_STR[];
294 extern const char OPT_SECTION_STR[];
295 extern const char OPT_PARAM_STR[];
296
297 typedef struct options_st {
298     const char *name;
299     int retval;
300     /*
301      * value type: - no value (also the value zero), n number, p positive
302      * number, u unsigned, l long, s string, < input file, > output file,
303      * f any format, F der/pem format, E der/pem/engine format identifier.
304      * l, n and u include zero; p does not.
305      */
306     int valtype;
307     const char *helpstr;
308 } OPTIONS;
309
310 /*
311  * A string/int pairing; widely use for option value lookup, hence the
312  * name OPT_PAIR. But that name is misleading in s_cb.c, so we also use
313  * the "generic" name STRINT_PAIR.
314  */
315 typedef struct string_int_pair_st {
316     const char *name;
317     int retval;
318 } OPT_PAIR, STRINT_PAIR;
319
320 /* Flags to pass into opt_format; see FORMAT_xxx, below. */
321 # define OPT_FMT_PEMDER          (1L <<  1)
322 # define OPT_FMT_PKCS12          (1L <<  2)
323 # define OPT_FMT_SMIME           (1L <<  3)
324 # define OPT_FMT_ENGINE          (1L <<  4)
325 # define OPT_FMT_MSBLOB          (1L <<  5)
326 /* (1L <<  6) was OPT_FMT_NETSCAPE, but wasn't used */
327 # define OPT_FMT_NSS             (1L <<  7)
328 # define OPT_FMT_TEXT            (1L <<  8)
329 # define OPT_FMT_HTTP            (1L <<  9)
330 # define OPT_FMT_PVK             (1L << 10)
331 # define OPT_FMT_PDE     (OPT_FMT_PEMDER | OPT_FMT_ENGINE)
332 # define OPT_FMT_PDS     (OPT_FMT_PEMDER | OPT_FMT_SMIME)
333 # define OPT_FMT_ANY     ( \
334         OPT_FMT_PEMDER | OPT_FMT_PKCS12 | OPT_FMT_SMIME | \
335         OPT_FMT_ENGINE | OPT_FMT_MSBLOB | OPT_FMT_NSS   | \
336         OPT_FMT_TEXT   | OPT_FMT_HTTP   | OPT_FMT_PVK)
337
338 /* Divide options into sections when displaying usage */
339 #define OPT_SECTION(sec) { OPT_SECTION_STR, 1, '-', sec " options:\n" }
340 #define OPT_PARAMETERS() { OPT_PARAM_STR, 1, '-', "Parameters:\n" }
341
342 const char *opt_path_end(const char *filename);
343 char *opt_progname(const char *argv0);
344 char *opt_appname(const char *arg0);
345 char *opt_getprog(void);
346 char *opt_init(int ac, char **av, const OPTIONS * o);
347 int opt_next(void);
348 void opt_begin(void);
349 int opt_format(const char *s, unsigned long flags, int *result);
350 const char *format2str(int format);
351 int opt_int(const char *arg, int *result);
352 int opt_ulong(const char *arg, unsigned long *result);
353 int opt_long(const char *arg, long *result);
354 #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L && \
355     defined(INTMAX_MAX) && defined(UINTMAX_MAX) && \
356     !defined(OPENSSL_NO_INTTYPES_H)
357 int opt_imax(const char *arg, intmax_t *result);
358 int opt_umax(const char *arg, uintmax_t *result);
359 #else
360 # define opt_imax opt_long
361 # define opt_umax opt_ulong
362 # define intmax_t long
363 # define uintmax_t unsigned long
364 #endif
365 int opt_pair(const char *arg, const OPT_PAIR * pairs, int *result);
366 int opt_string(const char *name, const char **options);
367 int opt_cipher(const char *name, const EVP_CIPHER **cipherp);
368 int opt_md(const char *name, const EVP_MD **mdp);
369 char *opt_arg(void);
370 char *opt_flag(void);
371 char *opt_unknown(void);
372 char **opt_rest(void);
373 int opt_num_rest(void);
374 int opt_verify(int i, X509_VERIFY_PARAM *vpm);
375 int opt_rand(int i);
376 int opt_provider(int i);
377 void opt_help(const OPTIONS * list);
378 void opt_print(const OPTIONS * opt, int doingparams, int width);
379 int opt_format_error(const char *s, unsigned long flags);
380 void print_format_error(int format, unsigned long flags);
381 int opt_isdir(const char *name);
382 int opt_printf_stderr(const char *fmt, ...);
383
384 #endif /* OSSL_APPS_OPT_H */