]>
Commit | Line | Data |
---|---|---|
846e33c7 | 1 | /* |
33388b44 | 2 | * Copyright 2015-2020 The OpenSSL Project Authors. All Rights Reserved. |
7e1b7485 | 3 | * |
dffa7520 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
846e33c7 RS |
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 | |
7e1b7485 | 8 | */ |
a43ce58f SL |
9 | |
10 | /* | |
11 | * This file is also used by the test suite. Do not #include "apps.h". | |
12 | */ | |
13 | #include "opt.h" | |
14 | #include "fmt.h" | |
15 | #include "internal/nelem.h" | |
7e1b7485 RS |
16 | #include <string.h> |
17 | #if !defined(OPENSSL_SYS_MSDOS) | |
6b10d29c | 18 | # include <unistd.h> |
7e1b7485 | 19 | #endif |
a3ed492f | 20 | |
7e1b7485 RS |
21 | #include <stdlib.h> |
22 | #include <errno.h> | |
23 | #include <ctype.h> | |
bd4850df | 24 | #include <limits.h> |
7e1b7485 | 25 | #include <openssl/bio.h> |
5951e840 | 26 | #include <openssl/x509v3.h> |
7e1b7485 RS |
27 | |
28 | #define MAX_OPT_HELP_WIDTH 30 | |
92de469f RS |
29 | const char OPT_HELP_STR[] = "-H"; |
30 | const char OPT_MORE_STR[] = "-M"; | |
31 | const char OPT_SECTION_STR[] = "-S"; | |
32 | const char OPT_PARAM_STR[] = "-P"; | |
7e1b7485 RS |
33 | |
34 | /* Our state */ | |
35 | static char **argv; | |
36 | static int argc; | |
37 | static int opt_index; | |
38 | static char *arg; | |
39 | static char *flag; | |
40 | static char *dunno; | |
41 | static const OPTIONS *unknown; | |
42 | static const OPTIONS *opts; | |
43 | static char prog[40]; | |
44 | ||
45 | /* | |
46 | * Return the simple name of the program; removing various platform gunk. | |
47 | */ | |
1fbab1dc | 48 | #if defined(OPENSSL_SYS_WIN32) |
7e1b7485 RS |
49 | char *opt_progname(const char *argv0) |
50 | { | |
45f13518 | 51 | size_t i, n; |
7e1b7485 RS |
52 | const char *p; |
53 | char *q; | |
54 | ||
55 | /* find the last '/', '\' or ':' */ | |
56 | for (p = argv0 + strlen(argv0); --p > argv0;) | |
57 | if (*p == '/' || *p == '\\' || *p == ':') { | |
58 | p++; | |
59 | break; | |
60 | } | |
61 | ||
62 | /* Strip off trailing nonsense. */ | |
63 | n = strlen(p); | |
64 | if (n > 4 && | |
a3ed492f | 65 | (strcmp(&p[n - 4], ".exe") == 0 || strcmp(&p[n - 4], ".EXE") == 0)) |
7e1b7485 | 66 | n -= 4; |
7e1b7485 RS |
67 | |
68 | /* Copy over the name, in lowercase. */ | |
cbe29648 RS |
69 | if (n > sizeof(prog) - 1) |
70 | n = sizeof(prog) - 1; | |
7e1b7485 | 71 | for (q = prog, i = 0; i < n; i++, p++) |
00dfbaad | 72 | *q++ = tolower((unsigned char)*p); |
7e1b7485 RS |
73 | *q = '\0'; |
74 | return prog; | |
75 | } | |
76 | ||
77 | #elif defined(OPENSSL_SYS_VMS) | |
78 | ||
79 | char *opt_progname(const char *argv0) | |
80 | { | |
81 | const char *p, *q; | |
82 | ||
0ad69cd6 | 83 | /* Find last special character sys:[foo.bar]openssl */ |
7e1b7485 RS |
84 | for (p = argv0 + strlen(argv0); --p > argv0;) |
85 | if (*p == ':' || *p == ']' || *p == '>') { | |
86 | p++; | |
87 | break; | |
88 | } | |
89 | ||
90 | q = strrchr(p, '.'); | |
cbe29648 RS |
91 | strncpy(prog, p, sizeof(prog) - 1); |
92 | prog[sizeof(prog) - 1] = '\0'; | |
93 | if (q != NULL && q - p < sizeof(prog)) | |
7e1b7485 RS |
94 | prog[q - p] = '\0'; |
95 | return prog; | |
96 | } | |
97 | ||
98 | #else | |
99 | ||
100 | char *opt_progname(const char *argv0) | |
101 | { | |
102 | const char *p; | |
103 | ||
104 | /* Could use strchr, but this is like the ones above. */ | |
105 | for (p = argv0 + strlen(argv0); --p > argv0;) | |
106 | if (*p == '/') { | |
107 | p++; | |
108 | break; | |
109 | } | |
cbe29648 RS |
110 | strncpy(prog, p, sizeof(prog) - 1); |
111 | prog[sizeof(prog) - 1] = '\0'; | |
7e1b7485 RS |
112 | return prog; |
113 | } | |
114 | #endif | |
115 | ||
116 | char *opt_getprog(void) | |
117 | { | |
118 | return prog; | |
119 | } | |
120 | ||
121 | /* Set up the arg parsing. */ | |
122 | char *opt_init(int ac, char **av, const OPTIONS *o) | |
123 | { | |
124 | /* Store state. */ | |
125 | argc = ac; | |
126 | argv = av; | |
a43ce58f | 127 | opt_begin(); |
7e1b7485 RS |
128 | opts = o; |
129 | opt_progname(av[0]); | |
130 | unknown = NULL; | |
131 | ||
92de469f RS |
132 | /* Check all options up until the PARAM marker (if present) */ |
133 | for (; o->name != NULL && o->name != OPT_PARAM_STR; ++o) { | |
7e1b7485 | 134 | #ifndef NDEBUG |
b286cb8e | 135 | const OPTIONS *next; |
88806cfc | 136 | int duplicated, i; |
7e1b7485 RS |
137 | #endif |
138 | ||
5388f986 RS |
139 | if (o->name == OPT_HELP_STR |
140 | || o->name == OPT_MORE_STR | |
141 | || o->name == OPT_SECTION_STR) | |
7e1b7485 RS |
142 | continue; |
143 | #ifndef NDEBUG | |
144 | i = o->valtype; | |
145 | ||
146 | /* Make sure options are legit. */ | |
a43ce58f SL |
147 | OPENSSL_assert(o->name[0] != '-'); |
148 | OPENSSL_assert(o->retval > 0); | |
03f887ca | 149 | switch (i) { |
0c20802c VD |
150 | case 0: case '-': case '/': case '<': case '>': case 'E': case 'F': |
151 | case 'M': case 'U': case 'f': case 'l': case 'n': case 'p': case 's': | |
fd3397fc | 152 | case 'u': case 'c': case ':': |
03f887ca VD |
153 | break; |
154 | default: | |
a43ce58f | 155 | OPENSSL_assert(0); |
03f887ca | 156 | } |
7e1b7485 RS |
157 | |
158 | /* Make sure there are no duplicates. */ | |
88806cfc | 159 | for (next = o + 1; next->name; ++next) { |
7e1b7485 | 160 | /* |
88806cfc | 161 | * Some compilers inline strcmp and the assert string is too long. |
7e1b7485 | 162 | */ |
88806cfc | 163 | duplicated = strcmp(o->name, next->name) == 0; |
a43ce58f | 164 | OPENSSL_assert(!duplicated); |
7e1b7485 RS |
165 | } |
166 | #endif | |
167 | if (o->name[0] == '\0') { | |
a43ce58f | 168 | OPENSSL_assert(unknown == NULL); |
7e1b7485 | 169 | unknown = o; |
a43ce58f | 170 | OPENSSL_assert(unknown->valtype == 0 || unknown->valtype == '-'); |
7e1b7485 RS |
171 | } |
172 | } | |
173 | return prog; | |
174 | } | |
175 | ||
176 | static OPT_PAIR formats[] = { | |
177 | {"PEM/DER", OPT_FMT_PEMDER}, | |
178 | {"pkcs12", OPT_FMT_PKCS12}, | |
179 | {"smime", OPT_FMT_SMIME}, | |
180 | {"engine", OPT_FMT_ENGINE}, | |
181 | {"msblob", OPT_FMT_MSBLOB}, | |
7e1b7485 RS |
182 | {"nss", OPT_FMT_NSS}, |
183 | {"text", OPT_FMT_TEXT}, | |
184 | {"http", OPT_FMT_HTTP}, | |
185 | {"pvk", OPT_FMT_PVK}, | |
186 | {NULL} | |
187 | }; | |
188 | ||
189 | /* Print an error message about a failed format parse. */ | |
190 | int opt_format_error(const char *s, unsigned long flags) | |
191 | { | |
192 | OPT_PAIR *ap; | |
193 | ||
2234212c | 194 | if (flags == OPT_FMT_PEMDER) { |
a43ce58f SL |
195 | opt_printf_stderr("%s: Bad format \"%s\"; must be pem or der\n", |
196 | prog, s); | |
2234212c | 197 | } else { |
a43ce58f SL |
198 | opt_printf_stderr("%s: Bad format \"%s\"; must be one of:\n", |
199 | prog, s); | |
7e1b7485 RS |
200 | for (ap = formats; ap->name; ap++) |
201 | if (flags & ap->retval) | |
a43ce58f | 202 | opt_printf_stderr(" %s\n", ap->name); |
7e1b7485 RS |
203 | } |
204 | return 0; | |
205 | } | |
206 | ||
207 | /* Parse a format string, put it into *result; return 0 on failure, else 1. */ | |
208 | int opt_format(const char *s, unsigned long flags, int *result) | |
209 | { | |
210 | switch (*s) { | |
211 | default: | |
212 | return 0; | |
213 | case 'D': | |
214 | case 'd': | |
215 | if ((flags & OPT_FMT_PEMDER) == 0) | |
216 | return opt_format_error(s, flags); | |
217 | *result = FORMAT_ASN1; | |
218 | break; | |
219 | case 'T': | |
220 | case 't': | |
221 | if ((flags & OPT_FMT_TEXT) == 0) | |
222 | return opt_format_error(s, flags); | |
223 | *result = FORMAT_TEXT; | |
224 | break; | |
225 | case 'N': | |
226 | case 'n': | |
0bc2f365 RS |
227 | if ((flags & OPT_FMT_NSS) == 0) |
228 | return opt_format_error(s, flags); | |
229 | if (strcmp(s, "NSS") != 0 && strcmp(s, "nss") != 0) | |
230 | return opt_format_error(s, flags); | |
231 | *result = FORMAT_NSS; | |
7e1b7485 RS |
232 | break; |
233 | case 'S': | |
234 | case 's': | |
235 | if ((flags & OPT_FMT_SMIME) == 0) | |
236 | return opt_format_error(s, flags); | |
237 | *result = FORMAT_SMIME; | |
238 | break; | |
239 | case 'M': | |
240 | case 'm': | |
241 | if ((flags & OPT_FMT_MSBLOB) == 0) | |
242 | return opt_format_error(s, flags); | |
243 | *result = FORMAT_MSBLOB; | |
244 | break; | |
245 | case 'E': | |
246 | case 'e': | |
247 | if ((flags & OPT_FMT_ENGINE) == 0) | |
248 | return opt_format_error(s, flags); | |
249 | *result = FORMAT_ENGINE; | |
250 | break; | |
251 | case 'H': | |
252 | case 'h': | |
253 | if ((flags & OPT_FMT_HTTP) == 0) | |
254 | return opt_format_error(s, flags); | |
255 | *result = FORMAT_HTTP; | |
256 | break; | |
257 | case '1': | |
258 | if ((flags & OPT_FMT_PKCS12) == 0) | |
259 | return opt_format_error(s, flags); | |
260 | *result = FORMAT_PKCS12; | |
261 | break; | |
262 | case 'P': | |
263 | case 'p': | |
264 | if (s[1] == '\0' || strcmp(s, "PEM") == 0 || strcmp(s, "pem") == 0) { | |
265 | if ((flags & OPT_FMT_PEMDER) == 0) | |
266 | return opt_format_error(s, flags); | |
267 | *result = FORMAT_PEM; | |
268 | } else if (strcmp(s, "PVK") == 0 || strcmp(s, "pvk") == 0) { | |
269 | if ((flags & OPT_FMT_PVK) == 0) | |
270 | return opt_format_error(s, flags); | |
271 | *result = FORMAT_PVK; | |
272 | } else if (strcmp(s, "P12") == 0 || strcmp(s, "p12") == 0 | |
273 | || strcmp(s, "PKCS12") == 0 || strcmp(s, "pkcs12") == 0) { | |
274 | if ((flags & OPT_FMT_PKCS12) == 0) | |
275 | return opt_format_error(s, flags); | |
276 | *result = FORMAT_PKCS12; | |
2234212c | 277 | } else { |
7e1b7485 | 278 | return 0; |
2234212c | 279 | } |
7e1b7485 RS |
280 | break; |
281 | } | |
282 | return 1; | |
283 | } | |
284 | ||
285 | /* Parse a cipher name, put it in *EVP_CIPHER; return 0 on failure, else 1. */ | |
286 | int opt_cipher(const char *name, const EVP_CIPHER **cipherp) | |
287 | { | |
288 | *cipherp = EVP_get_cipherbyname(name); | |
2234212c | 289 | if (*cipherp != NULL) |
7e1b7485 | 290 | return 1; |
e0e68f9e | 291 | opt_printf_stderr("%s: Unknown cipher: %s\n", prog, name); |
7e1b7485 RS |
292 | return 0; |
293 | } | |
294 | ||
295 | /* | |
296 | * Parse message digest name, put it in *EVP_MD; return 0 on failure, else 1. | |
297 | */ | |
298 | int opt_md(const char *name, const EVP_MD **mdp) | |
299 | { | |
300 | *mdp = EVP_get_digestbyname(name); | |
2234212c | 301 | if (*mdp != NULL) |
7e1b7485 | 302 | return 1; |
e0e68f9e | 303 | opt_printf_stderr("%s: Unknown message digest: %s\n", prog, name); |
7e1b7485 RS |
304 | return 0; |
305 | } | |
306 | ||
307 | /* Look through a list of name/value pairs. */ | |
308 | int opt_pair(const char *name, const OPT_PAIR* pairs, int *result) | |
309 | { | |
310 | const OPT_PAIR *pp; | |
311 | ||
312 | for (pp = pairs; pp->name; pp++) | |
313 | if (strcmp(pp->name, name) == 0) { | |
314 | *result = pp->retval; | |
315 | return 1; | |
316 | } | |
a43ce58f | 317 | opt_printf_stderr("%s: Value must be one of:\n", prog); |
7e1b7485 | 318 | for (pp = pairs; pp->name; pp++) |
a43ce58f | 319 | opt_printf_stderr("\t%s\n", pp->name); |
7e1b7485 RS |
320 | return 0; |
321 | } | |
322 | ||
7e1b7485 RS |
323 | /* Parse an int, put it into *result; return 0 on failure, else 1. */ |
324 | int opt_int(const char *value, int *result) | |
325 | { | |
bd4850df RS |
326 | long l; |
327 | ||
328 | if (!opt_long(value, &l)) | |
329 | return 0; | |
330 | *result = (int)l; | |
331 | if (*result != l) { | |
a43ce58f SL |
332 | opt_printf_stderr("%s: Value \"%s\" outside integer range\n", |
333 | prog, value); | |
7e1b7485 RS |
334 | return 0; |
335 | } | |
336 | return 1; | |
337 | } | |
338 | ||
7447c49f PY |
339 | static void opt_number_error(const char *v) |
340 | { | |
341 | size_t i = 0; | |
342 | struct strstr_pair_st { | |
343 | char *prefix; | |
344 | char *name; | |
345 | } b[] = { | |
346 | {"0x", "a hexadecimal"}, | |
347 | {"0X", "a hexadecimal"}, | |
348 | {"0", "an octal"} | |
349 | }; | |
350 | ||
351 | for (i = 0; i < OSSL_NELEM(b); i++) { | |
352 | if (strncmp(v, b[i].prefix, strlen(b[i].prefix)) == 0) { | |
a43ce58f SL |
353 | opt_printf_stderr("%s: Can't parse \"%s\" as %s number\n", |
354 | prog, v, b[i].name); | |
7447c49f PY |
355 | return; |
356 | } | |
357 | } | |
a43ce58f | 358 | opt_printf_stderr("%s: Can't parse \"%s\" as a number\n", prog, v); |
7447c49f PY |
359 | return; |
360 | } | |
361 | ||
7e1b7485 RS |
362 | /* Parse a long, put it into *result; return 0 on failure, else 1. */ |
363 | int opt_long(const char *value, long *result) | |
364 | { | |
bd4850df RS |
365 | int oerrno = errno; |
366 | long l; | |
367 | char *endp; | |
368 | ||
c3a7e0c5 | 369 | errno = 0; |
bd4850df RS |
370 | l = strtol(value, &endp, 0); |
371 | if (*endp | |
372 | || endp == value | |
373 | || ((l == LONG_MAX || l == LONG_MIN) && errno == ERANGE) | |
374 | || (l == 0 && errno != 0)) { | |
7447c49f | 375 | opt_number_error(value); |
bd4850df | 376 | errno = oerrno; |
7e1b7485 RS |
377 | return 0; |
378 | } | |
bd4850df RS |
379 | *result = l; |
380 | errno = oerrno; | |
7e1b7485 RS |
381 | return 1; |
382 | } | |
383 | ||
d94a1a70 | 384 | #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L && \ |
53f54696 SL |
385 | defined(INTMAX_MAX) && defined(UINTMAX_MAX) && \ |
386 | !defined(OPENSSL_NO_INTTYPES_H) | |
03f887ca VD |
387 | |
388 | /* Parse an intmax_t, put it into *result; return 0 on failure, else 1. */ | |
389 | int opt_imax(const char *value, intmax_t *result) | |
390 | { | |
391 | int oerrno = errno; | |
392 | intmax_t m; | |
393 | char *endp; | |
394 | ||
c3a7e0c5 | 395 | errno = 0; |
03f887ca VD |
396 | m = strtoimax(value, &endp, 0); |
397 | if (*endp | |
398 | || endp == value | |
399 | || ((m == INTMAX_MAX || m == INTMAX_MIN) && errno == ERANGE) | |
400 | || (m == 0 && errno != 0)) { | |
7447c49f | 401 | opt_number_error(value); |
03f887ca VD |
402 | errno = oerrno; |
403 | return 0; | |
404 | } | |
405 | *result = m; | |
406 | errno = oerrno; | |
407 | return 1; | |
408 | } | |
409 | ||
410 | /* Parse a uintmax_t, put it into *result; return 0 on failure, else 1. */ | |
411 | int opt_umax(const char *value, uintmax_t *result) | |
412 | { | |
413 | int oerrno = errno; | |
414 | uintmax_t m; | |
415 | char *endp; | |
416 | ||
c3a7e0c5 | 417 | errno = 0; |
03f887ca VD |
418 | m = strtoumax(value, &endp, 0); |
419 | if (*endp | |
420 | || endp == value | |
421 | || (m == UINTMAX_MAX && errno == ERANGE) | |
422 | || (m == 0 && errno != 0)) { | |
7447c49f | 423 | opt_number_error(value); |
03f887ca VD |
424 | errno = oerrno; |
425 | return 0; | |
426 | } | |
427 | *result = m; | |
428 | errno = oerrno; | |
429 | return 1; | |
430 | } | |
431 | #endif | |
432 | ||
7e1b7485 RS |
433 | /* |
434 | * Parse an unsigned long, put it into *result; return 0 on failure, else 1. | |
435 | */ | |
436 | int opt_ulong(const char *value, unsigned long *result) | |
437 | { | |
bd4850df | 438 | int oerrno = errno; |
7e1b7485 | 439 | char *endptr; |
bd4850df RS |
440 | unsigned long l; |
441 | ||
c3a7e0c5 | 442 | errno = 0; |
bd4850df RS |
443 | l = strtoul(value, &endptr, 0); |
444 | if (*endptr | |
445 | || endptr == value | |
446 | || ((l == ULONG_MAX) && errno == ERANGE) | |
447 | || (l == 0 && errno != 0)) { | |
7447c49f | 448 | opt_number_error(value); |
bd4850df | 449 | errno = oerrno; |
7e1b7485 RS |
450 | return 0; |
451 | } | |
bd4850df RS |
452 | *result = l; |
453 | errno = oerrno; | |
7e1b7485 RS |
454 | return 1; |
455 | } | |
456 | ||
457 | /* | |
458 | * We pass opt as an int but cast it to "enum range" so that all the | |
459 | * items in the OPT_V_ENUM enumeration are caught; this makes -Wswitch | |
460 | * in gcc do the right thing. | |
461 | */ | |
462 | enum range { OPT_V_ENUM }; | |
463 | ||
464 | int opt_verify(int opt, X509_VERIFY_PARAM *vpm) | |
465 | { | |
7e1b7485 | 466 | int i; |
03f887ca | 467 | ossl_intmax_t t = 0; |
7e1b7485 RS |
468 | ASN1_OBJECT *otmp; |
469 | X509_PURPOSE *xptmp; | |
470 | const X509_VERIFY_PARAM *vtmp; | |
471 | ||
a43ce58f SL |
472 | OPENSSL_assert(vpm != NULL); |
473 | OPENSSL_assert(opt > OPT_V__FIRST); | |
474 | OPENSSL_assert(opt < OPT_V__LAST); | |
7e1b7485 RS |
475 | |
476 | switch ((enum range)opt) { | |
477 | case OPT_V__FIRST: | |
478 | case OPT_V__LAST: | |
479 | return 0; | |
480 | case OPT_V_POLICY: | |
481 | otmp = OBJ_txt2obj(opt_arg(), 0); | |
482 | if (otmp == NULL) { | |
a43ce58f | 483 | opt_printf_stderr("%s: Invalid Policy %s\n", prog, opt_arg()); |
7e1b7485 RS |
484 | return 0; |
485 | } | |
486 | X509_VERIFY_PARAM_add0_policy(vpm, otmp); | |
487 | break; | |
488 | case OPT_V_PURPOSE: | |
0daccd4d | 489 | /* purpose name -> purpose index */ |
7e1b7485 RS |
490 | i = X509_PURPOSE_get_by_sname(opt_arg()); |
491 | if (i < 0) { | |
a43ce58f | 492 | opt_printf_stderr("%s: Invalid purpose %s\n", prog, opt_arg()); |
7e1b7485 RS |
493 | return 0; |
494 | } | |
0daccd4d VD |
495 | |
496 | /* purpose index -> purpose object */ | |
7e1b7485 | 497 | xptmp = X509_PURPOSE_get0(i); |
0daccd4d VD |
498 | |
499 | /* purpose object -> purpose value */ | |
7e1b7485 | 500 | i = X509_PURPOSE_get_id(xptmp); |
0daccd4d VD |
501 | |
502 | if (!X509_VERIFY_PARAM_set_purpose(vpm, i)) { | |
a43ce58f SL |
503 | opt_printf_stderr("%s: Internal error setting purpose %s\n", |
504 | prog, opt_arg()); | |
0daccd4d VD |
505 | return 0; |
506 | } | |
7e1b7485 RS |
507 | break; |
508 | case OPT_V_VERIFY_NAME: | |
509 | vtmp = X509_VERIFY_PARAM_lookup(opt_arg()); | |
510 | if (vtmp == NULL) { | |
a43ce58f SL |
511 | opt_printf_stderr("%s: Invalid verify name %s\n", |
512 | prog, opt_arg()); | |
7e1b7485 RS |
513 | return 0; |
514 | } | |
515 | X509_VERIFY_PARAM_set1(vpm, vtmp); | |
516 | break; | |
517 | case OPT_V_VERIFY_DEPTH: | |
518 | i = atoi(opt_arg()); | |
519 | if (i >= 0) | |
520 | X509_VERIFY_PARAM_set_depth(vpm, i); | |
521 | break; | |
fbb82a60 VD |
522 | case OPT_V_VERIFY_AUTH_LEVEL: |
523 | i = atoi(opt_arg()); | |
524 | if (i >= 0) | |
525 | X509_VERIFY_PARAM_set_auth_level(vpm, i); | |
526 | break; | |
7e1b7485 | 527 | case OPT_V_ATTIME: |
03f887ca VD |
528 | if (!opt_imax(opt_arg(), &t)) |
529 | return 0; | |
530 | if (t != (time_t)t) { | |
a43ce58f SL |
531 | opt_printf_stderr("%s: epoch time out of range %s\n", |
532 | prog, opt_arg()); | |
03f887ca VD |
533 | return 0; |
534 | } | |
535 | X509_VERIFY_PARAM_set_time(vpm, (time_t)t); | |
7e1b7485 RS |
536 | break; |
537 | case OPT_V_VERIFY_HOSTNAME: | |
538 | if (!X509_VERIFY_PARAM_set1_host(vpm, opt_arg(), 0)) | |
539 | return 0; | |
540 | break; | |
541 | case OPT_V_VERIFY_EMAIL: | |
542 | if (!X509_VERIFY_PARAM_set1_email(vpm, opt_arg(), 0)) | |
543 | return 0; | |
544 | break; | |
545 | case OPT_V_VERIFY_IP: | |
546 | if (!X509_VERIFY_PARAM_set1_ip_asc(vpm, opt_arg())) | |
547 | return 0; | |
548 | break; | |
549 | case OPT_V_IGNORE_CRITICAL: | |
550 | X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_IGNORE_CRITICAL); | |
551 | break; | |
552 | case OPT_V_ISSUER_CHECKS: | |
d33def66 | 553 | /* NOP, deprecated */ |
7e1b7485 RS |
554 | break; |
555 | case OPT_V_CRL_CHECK: | |
556 | X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_CRL_CHECK); | |
557 | break; | |
558 | case OPT_V_CRL_CHECK_ALL: | |
559 | X509_VERIFY_PARAM_set_flags(vpm, | |
560 | X509_V_FLAG_CRL_CHECK | | |
561 | X509_V_FLAG_CRL_CHECK_ALL); | |
562 | break; | |
563 | case OPT_V_POLICY_CHECK: | |
564 | X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_POLICY_CHECK); | |
565 | break; | |
566 | case OPT_V_EXPLICIT_POLICY: | |
567 | X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_EXPLICIT_POLICY); | |
568 | break; | |
569 | case OPT_V_INHIBIT_ANY: | |
570 | X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_INHIBIT_ANY); | |
571 | break; | |
572 | case OPT_V_INHIBIT_MAP: | |
573 | X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_INHIBIT_MAP); | |
574 | break; | |
575 | case OPT_V_X509_STRICT: | |
576 | X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_X509_STRICT); | |
577 | break; | |
578 | case OPT_V_EXTENDED_CRL: | |
579 | X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_EXTENDED_CRL_SUPPORT); | |
580 | break; | |
581 | case OPT_V_USE_DELTAS: | |
582 | X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_USE_DELTAS); | |
583 | break; | |
584 | case OPT_V_POLICY_PRINT: | |
585 | X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_NOTIFY_POLICY); | |
586 | break; | |
587 | case OPT_V_CHECK_SS_SIG: | |
588 | X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_CHECK_SS_SIGNATURE); | |
589 | break; | |
590 | case OPT_V_TRUSTED_FIRST: | |
591 | X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_TRUSTED_FIRST); | |
592 | break; | |
593 | case OPT_V_SUITEB_128_ONLY: | |
594 | X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_SUITEB_128_LOS_ONLY); | |
595 | break; | |
596 | case OPT_V_SUITEB_128: | |
597 | X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_SUITEB_128_LOS); | |
598 | break; | |
599 | case OPT_V_SUITEB_192: | |
600 | X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_SUITEB_192_LOS); | |
601 | break; | |
602 | case OPT_V_PARTIAL_CHAIN: | |
603 | X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_PARTIAL_CHAIN); | |
604 | break; | |
605 | case OPT_V_NO_ALT_CHAINS: | |
606 | X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_NO_ALT_CHAINS); | |
dccd20d1 | 607 | break; |
d35ff2c0 DW |
608 | case OPT_V_NO_CHECK_TIME: |
609 | X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_NO_CHECK_TIME); | |
dccd20d1 | 610 | break; |
a392ef20 RL |
611 | case OPT_V_ALLOW_PROXY_CERTS: |
612 | X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_ALLOW_PROXY_CERTS); | |
613 | break; | |
7e1b7485 RS |
614 | } |
615 | return 1; | |
616 | ||
617 | } | |
618 | ||
a43ce58f SL |
619 | void opt_begin(void) |
620 | { | |
621 | opt_index = 1; | |
622 | arg = NULL; | |
623 | flag = NULL; | |
624 | } | |
625 | ||
7e1b7485 RS |
626 | /* |
627 | * Parse the next flag (and value if specified), return 0 if done, -1 on | |
628 | * error, otherwise the flag's retval. | |
629 | */ | |
630 | int opt_next(void) | |
631 | { | |
96de2e59 | 632 | char *p; |
7e1b7485 | 633 | const OPTIONS *o; |
bd4850df | 634 | int ival; |
03f887ca VD |
635 | long lval; |
636 | unsigned long ulval; | |
637 | ossl_intmax_t imval; | |
638 | ossl_uintmax_t umval; | |
7e1b7485 RS |
639 | |
640 | /* Look at current arg; at end of the list? */ | |
641 | arg = NULL; | |
642 | p = argv[opt_index]; | |
643 | if (p == NULL) | |
644 | return 0; | |
645 | ||
646 | /* If word doesn't start with a -, we're done. */ | |
647 | if (*p != '-') | |
648 | return 0; | |
649 | ||
650 | /* Hit "--" ? We're done. */ | |
651 | opt_index++; | |
652 | if (strcmp(p, "--") == 0) | |
653 | return 0; | |
654 | ||
655 | /* Allow -nnn and --nnn */ | |
656 | if (*++p == '-') | |
657 | p++; | |
658 | flag = p - 1; | |
659 | ||
660 | /* If we have --flag=foo, snip it off */ | |
661 | if ((arg = strchr(p, '=')) != NULL) | |
662 | *arg++ = '\0'; | |
663 | for (o = opts; o->name; ++o) { | |
664 | /* If not this option, move on to the next one. */ | |
665 | if (strcmp(p, o->name) != 0) | |
666 | continue; | |
667 | ||
668 | /* If it doesn't take a value, make sure none was given. */ | |
669 | if (o->valtype == 0 || o->valtype == '-') { | |
670 | if (arg) { | |
a43ce58f SL |
671 | opt_printf_stderr("%s: Option -%s does not take a value\n", |
672 | prog, p); | |
7e1b7485 RS |
673 | return -1; |
674 | } | |
675 | return o->retval; | |
676 | } | |
677 | ||
678 | /* Want a value; get the next param if =foo not used. */ | |
679 | if (arg == NULL) { | |
680 | if (argv[opt_index] == NULL) { | |
a43ce58f SL |
681 | opt_printf_stderr("%s: Option -%s needs a value\n", |
682 | prog, o->name); | |
7e1b7485 RS |
683 | return -1; |
684 | } | |
685 | arg = argv[opt_index++]; | |
686 | } | |
687 | ||
688 | /* Syntax-check value. */ | |
7e1b7485 RS |
689 | switch (o->valtype) { |
690 | default: | |
691 | case 's': | |
fd3397fc | 692 | case ':': |
7e1b7485 RS |
693 | /* Just a string. */ |
694 | break; | |
695 | case '/': | |
a43ce58f | 696 | if (opt_isdir(arg) > 0) |
7e1b7485 | 697 | break; |
a43ce58f | 698 | opt_printf_stderr("%s: Not a directory: %s\n", prog, arg); |
7e1b7485 RS |
699 | return -1; |
700 | case '<': | |
701 | /* Input file. */ | |
4522e130 | 702 | break; |
7e1b7485 RS |
703 | case '>': |
704 | /* Output file. */ | |
4522e130 | 705 | break; |
7e1b7485 RS |
706 | case 'p': |
707 | case 'n': | |
bd4850df RS |
708 | if (!opt_int(arg, &ival) |
709 | || (o->valtype == 'p' && ival <= 0)) { | |
a43ce58f SL |
710 | opt_printf_stderr("%s: Non-positive number \"%s\" for -%s\n", |
711 | prog, arg, o->name); | |
bd4850df | 712 | return -1; |
7e1b7485 | 713 | } |
bd4850df | 714 | break; |
03f887ca VD |
715 | case 'M': |
716 | if (!opt_imax(arg, &imval)) { | |
a43ce58f SL |
717 | opt_printf_stderr("%s: Invalid number \"%s\" for -%s\n", |
718 | prog, arg, o->name); | |
03f887ca VD |
719 | return -1; |
720 | } | |
721 | break; | |
722 | case 'U': | |
723 | if (!opt_umax(arg, &umval)) { | |
a43ce58f SL |
724 | opt_printf_stderr("%s: Invalid number \"%s\" for -%s\n", |
725 | prog, arg, o->name); | |
03f887ca VD |
726 | return -1; |
727 | } | |
728 | break; | |
0c20802c | 729 | case 'l': |
03f887ca | 730 | if (!opt_long(arg, &lval)) { |
a43ce58f SL |
731 | opt_printf_stderr("%s: Invalid number \"%s\" for -%s\n", |
732 | prog, arg, o->name); | |
03f887ca VD |
733 | return -1; |
734 | } | |
735 | break; | |
7e1b7485 | 736 | case 'u': |
03f887ca | 737 | if (!opt_ulong(arg, &ulval)) { |
a43ce58f SL |
738 | opt_printf_stderr("%s: Invalid number \"%s\" for -%s\n", |
739 | prog, arg, o->name); | |
bd4850df RS |
740 | return -1; |
741 | } | |
742 | break; | |
f47e5647 | 743 | case 'c': |
0c20802c | 744 | case 'E': |
7e1b7485 | 745 | case 'F': |
0c20802c | 746 | case 'f': |
7e1b7485 | 747 | if (opt_format(arg, |
f47e5647 | 748 | o->valtype == 'c' ? OPT_FMT_PDS : |
0c20802c | 749 | o->valtype == 'E' ? OPT_FMT_PDE : |
7e1b7485 | 750 | o->valtype == 'F' ? OPT_FMT_PEMDER |
bd4850df | 751 | : OPT_FMT_ANY, &ival)) |
7e1b7485 | 752 | break; |
a43ce58f SL |
753 | opt_printf_stderr("%s: Invalid format \"%s\" for -%s\n", |
754 | prog, arg, o->name); | |
7e1b7485 RS |
755 | return -1; |
756 | } | |
757 | ||
758 | /* Return the flag value. */ | |
759 | return o->retval; | |
760 | } | |
761 | if (unknown != NULL) { | |
762 | dunno = p; | |
763 | return unknown->retval; | |
764 | } | |
e0e68f9e | 765 | opt_printf_stderr("%s: Unknown option: -%s\n", prog, p); |
7e1b7485 RS |
766 | return -1; |
767 | } | |
768 | ||
769 | /* Return the most recent flag parameter. */ | |
770 | char *opt_arg(void) | |
771 | { | |
772 | return arg; | |
773 | } | |
774 | ||
775 | /* Return the most recent flag. */ | |
776 | char *opt_flag(void) | |
777 | { | |
778 | return flag; | |
779 | } | |
780 | ||
781 | /* Return the unknown option. */ | |
782 | char *opt_unknown(void) | |
783 | { | |
784 | return dunno; | |
785 | } | |
786 | ||
787 | /* Return the rest of the arguments after parsing flags. */ | |
788 | char **opt_rest(void) | |
789 | { | |
790 | return &argv[opt_index]; | |
791 | } | |
792 | ||
793 | /* How many items in remaining args? */ | |
794 | int opt_num_rest(void) | |
795 | { | |
796 | int i = 0; | |
797 | char **pp; | |
798 | ||
799 | for (pp = opt_rest(); *pp; pp++, i++) | |
800 | continue; | |
801 | return i; | |
802 | } | |
803 | ||
804 | /* Return a string describing the parameter type. */ | |
805 | static const char *valtype2param(const OPTIONS *o) | |
806 | { | |
807 | switch (o->valtype) { | |
6755ff11 | 808 | case 0: |
7e1b7485 RS |
809 | case '-': |
810 | return ""; | |
fd3397fc RL |
811 | case ':': |
812 | return "uri"; | |
7e1b7485 RS |
813 | case 's': |
814 | return "val"; | |
815 | case '/': | |
816 | return "dir"; | |
817 | case '<': | |
818 | return "infile"; | |
819 | case '>': | |
820 | return "outfile"; | |
821 | case 'p': | |
0c20802c | 822 | return "+int"; |
7e1b7485 | 823 | case 'n': |
0c20802c VD |
824 | return "int"; |
825 | case 'l': | |
826 | return "long"; | |
7e1b7485 | 827 | case 'u': |
0c20802c VD |
828 | return "ulong"; |
829 | case 'E': | |
830 | return "PEM|DER|ENGINE"; | |
7e1b7485 | 831 | case 'F': |
0c20802c | 832 | return "PEM|DER"; |
7e1b7485 RS |
833 | case 'f': |
834 | return "format"; | |
0c20802c VD |
835 | case 'M': |
836 | return "intmax"; | |
837 | case 'U': | |
838 | return "uintmax"; | |
7e1b7485 RS |
839 | } |
840 | return "parm"; | |
841 | } | |
842 | ||
92de469f | 843 | void opt_print(const OPTIONS *o, int doingparams, int width) |
7e1b7485 | 844 | { |
4ed83891 | 845 | const char* help; |
7e1b7485 RS |
846 | char start[80 + 1]; |
847 | char *p; | |
7e1b7485 | 848 | |
7e1b7485 | 849 | help = o->helpstr ? o->helpstr : "(No additional info)"; |
5388f986 RS |
850 | if (o->name == OPT_HELP_STR) { |
851 | opt_printf_stderr(help, prog); | |
852 | return; | |
853 | } | |
854 | if (o->name == OPT_SECTION_STR) { | |
855 | opt_printf_stderr("\n"); | |
a43ce58f | 856 | opt_printf_stderr(help, prog); |
4ed83891 | 857 | return; |
7e1b7485 | 858 | } |
92de469f RS |
859 | if (o->name == OPT_PARAM_STR) { |
860 | opt_printf_stderr("\nParameters:\n"); | |
861 | return; | |
862 | } | |
7e1b7485 RS |
863 | |
864 | /* Pad out prefix */ | |
16f8d4eb | 865 | memset(start, ' ', sizeof(start) - 1); |
cbe29648 | 866 | start[sizeof(start) - 1] = '\0'; |
7e1b7485 RS |
867 | |
868 | if (o->name == OPT_MORE_STR) { | |
0ad69cd6 | 869 | /* Continuation of previous line; pad and print. */ |
7e1b7485 | 870 | start[width] = '\0'; |
a43ce58f | 871 | opt_printf_stderr("%s %s\n", start, help); |
4ed83891 | 872 | return; |
7e1b7485 RS |
873 | } |
874 | ||
875 | /* Build up the "-flag [param]" part. */ | |
876 | p = start; | |
877 | *p++ = ' '; | |
92de469f RS |
878 | if (!doingparams) |
879 | *p++ = '-'; | |
7e1b7485 RS |
880 | if (o->name[0]) |
881 | p += strlen(strcpy(p, o->name)); | |
882 | else | |
883 | *p++ = '*'; | |
884 | if (o->valtype != '-') { | |
885 | *p++ = ' '; | |
886 | p += strlen(strcpy(p, valtype2param(o))); | |
887 | } | |
888 | *p = ' '; | |
889 | if ((int)(p - start) >= MAX_OPT_HELP_WIDTH) { | |
890 | *p = '\0'; | |
a43ce58f | 891 | opt_printf_stderr("%s\n", start); |
16f8d4eb | 892 | memset(start, ' ', sizeof(start)); |
7e1b7485 RS |
893 | } |
894 | start[width] = '\0'; | |
a43ce58f | 895 | opt_printf_stderr("%s %s\n", start, help); |
4ed83891 JS |
896 | } |
897 | ||
898 | void opt_help(const OPTIONS *list) | |
899 | { | |
900 | const OPTIONS *o; | |
92de469f | 901 | int i, sawparams = 0, width = 5; |
4ed83891 | 902 | int standard_prolog; |
4ed83891 JS |
903 | char start[80 + 1]; |
904 | ||
905 | /* Starts with its own help message? */ | |
906 | standard_prolog = list[0].name != OPT_HELP_STR; | |
907 | ||
908 | /* Find the widest help. */ | |
909 | for (o = list; o->name; o++) { | |
910 | if (o->name == OPT_MORE_STR) | |
911 | continue; | |
912 | i = 2 + (int)strlen(o->name); | |
913 | if (o->valtype != '-') | |
914 | i += 1 + strlen(valtype2param(o)); | |
915 | if (i < MAX_OPT_HELP_WIDTH && i > width) | |
916 | width = i; | |
917 | OPENSSL_assert(i < (int)sizeof(start)); | |
918 | } | |
919 | ||
920 | if (standard_prolog) { | |
921 | opt_printf_stderr("Usage: %s [options]\n", prog); | |
922 | if (list[0].name != OPT_SECTION_STR) | |
923 | opt_printf_stderr("Valid options are:\n", prog); | |
924 | } | |
925 | ||
926 | /* Now let's print. */ | |
927 | for (o = list; o->name; o++) { | |
92de469f RS |
928 | if (o->name == OPT_PARAM_STR) |
929 | sawparams = 1; | |
930 | opt_print(o, sawparams, width); | |
7e1b7485 RS |
931 | } |
932 | } | |
a43ce58f SL |
933 | |
934 | /* opt_isdir section */ | |
935 | #ifdef _WIN32 | |
936 | # include <windows.h> | |
937 | int opt_isdir(const char *name) | |
938 | { | |
939 | DWORD attr; | |
940 | # if defined(UNICODE) || defined(_UNICODE) | |
941 | size_t i, len_0 = strlen(name) + 1; | |
942 | WCHAR tempname[MAX_PATH]; | |
943 | ||
944 | if (len_0 > MAX_PATH) | |
945 | return -1; | |
946 | ||
947 | # if !defined(_WIN32_WCE) || _WIN32_WCE>=101 | |
948 | if (!MultiByteToWideChar(CP_ACP, 0, name, len_0, tempname, MAX_PATH)) | |
949 | # endif | |
950 | for (i = 0; i < len_0; i++) | |
951 | tempname[i] = (WCHAR)name[i]; | |
952 | ||
953 | attr = GetFileAttributes(tempname); | |
954 | # else | |
955 | attr = GetFileAttributes(name); | |
956 | # endif | |
957 | if (attr == INVALID_FILE_ATTRIBUTES) | |
958 | return -1; | |
959 | return ((attr & FILE_ATTRIBUTE_DIRECTORY) != 0); | |
960 | } | |
961 | #else | |
962 | # include <sys/stat.h> | |
963 | # ifndef S_ISDIR | |
964 | # if defined(_S_IFMT) && defined(_S_IFDIR) | |
965 | # define S_ISDIR(a) (((a) & _S_IFMT) == _S_IFDIR) | |
966 | # else | |
967 | # define S_ISDIR(a) (((a) & S_IFMT) == S_IFDIR) | |
968 | # endif | |
969 | # endif | |
970 | ||
971 | int opt_isdir(const char *name) | |
972 | { | |
973 | # if defined(S_ISDIR) | |
974 | struct stat st; | |
975 | ||
976 | if (stat(name, &st) == 0) | |
977 | return S_ISDIR(st.st_mode); | |
978 | else | |
979 | return -1; | |
980 | # else | |
981 | return -1; | |
982 | # endif | |
983 | } | |
984 | #endif |