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