]>
Commit | Line | Data |
---|---|---|
7e1b7485 RS |
1 | /* ==================================================================== |
2 | * Copyright (c) 2015 The OpenSSL Project. All rights reserved. | |
3 | * | |
4 | * Redistribution and use in source and binary forms, with or without | |
5 | * modification, are permitted provided that the following conditions | |
6 | * are met: | |
7 | * | |
8 | * 1. Redistributions of source code must retain the above copyright | |
9 | * notice, this list of conditions and the following disclaimer. | |
10 | * | |
11 | * 2. Redistributions in binary form must reproduce the above copyright | |
12 | * notice, this list of conditions and the following disclaimer in | |
13 | * the documentation and/or other materials provided with the | |
14 | * distribution. | |
15 | * | |
16 | * 3. All advertising materials mentioning features or use of this | |
17 | * software must display the following acknowledgment: | |
18 | * "This product includes software developed by the OpenSSL Project | |
19 | * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" | |
20 | * | |
21 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | |
22 | * endorse or promote products derived from this software without | |
23 | * prior written permission. For written permission, please contact | |
24 | * licensing@OpenSSL.org. | |
25 | * | |
26 | * 5. Products derived from this software may not be called "OpenSSL" | |
27 | * nor may "OpenSSL" appear in their names without prior written | |
28 | * permission of the OpenSSL Project. | |
29 | * | |
30 | * 6. Redistributions of any form whatsoever must retain the following | |
31 | * acknowledgment: | |
32 | * "This product includes software developed by the OpenSSL Project | |
33 | * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" | |
34 | * | |
35 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | |
36 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
37 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
38 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | |
39 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
40 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
41 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
42 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
43 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
44 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
45 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
46 | * OF THE POSSIBILITY OF SUCH DAMAGE. | |
47 | * ==================================================================== | |
48 | */ | |
49 | ||
50 | /* #define COMPILE_STANDALONE_TEST_DRIVER */ | |
51 | #include "apps.h" | |
52 | #include <assert.h> | |
53 | #include <string.h> | |
54 | #if !defined(OPENSSL_SYS_MSDOS) | |
55 | # include OPENSSL_UNISTD | |
56 | #endif | |
a3ed492f | 57 | |
7e1b7485 RS |
58 | #include <stdlib.h> |
59 | #include <errno.h> | |
60 | #include <ctype.h> | |
61 | #include <openssl/bio.h> | |
62 | ||
63 | #define MAX_OPT_HELP_WIDTH 30 | |
64 | const char OPT_HELP_STR[] = "--"; | |
65 | const char OPT_MORE_STR[] = "---"; | |
66 | ||
67 | /* Our state */ | |
68 | static char **argv; | |
69 | static int argc; | |
70 | static int opt_index; | |
71 | static char *arg; | |
72 | static char *flag; | |
73 | static char *dunno; | |
74 | static const OPTIONS *unknown; | |
75 | static const OPTIONS *opts; | |
76 | static char prog[40]; | |
77 | ||
78 | /* | |
79 | * Return the simple name of the program; removing various platform gunk. | |
80 | */ | |
81 | #if defined(OPENSSL_SYS_WIN32) || defined(OPENSSL_SYS_NETWARE) | |
82 | char *opt_progname(const char *argv0) | |
83 | { | |
84 | int i; | |
85 | int n; | |
86 | const char *p; | |
87 | char *q; | |
88 | ||
89 | /* find the last '/', '\' or ':' */ | |
90 | for (p = argv0 + strlen(argv0); --p > argv0;) | |
91 | if (*p == '/' || *p == '\\' || *p == ':') { | |
92 | p++; | |
93 | break; | |
94 | } | |
95 | ||
96 | /* Strip off trailing nonsense. */ | |
97 | n = strlen(p); | |
98 | if (n > 4 && | |
a3ed492f | 99 | (strcmp(&p[n - 4], ".exe") == 0 || strcmp(&p[n - 4], ".EXE") == 0)) |
7e1b7485 RS |
100 | n -= 4; |
101 | #if defined(OPENSSL_SYS_NETWARE) | |
102 | if (n > 4 && | |
a3ed492f | 103 | (strcmp(&p[n - 4], ".nlm") == 0 || strcmp(&p[n - 4], ".NLM") == 0)) |
7e1b7485 RS |
104 | n -= 4; |
105 | #endif | |
106 | ||
107 | /* Copy over the name, in lowercase. */ | |
108 | if (n > sizeof prog - 1) | |
109 | n = sizeof prog - 1; | |
110 | for (q = prog, i = 0; i < n; i++, p++) | |
a3ed492f | 111 | *q++ = isupper(*p) ? tolower(*p) : *p; |
7e1b7485 RS |
112 | *q = '\0'; |
113 | return prog; | |
114 | } | |
115 | ||
116 | #elif defined(OPENSSL_SYS_VMS) | |
117 | ||
118 | char *opt_progname(const char *argv0) | |
119 | { | |
120 | const char *p, *q; | |
121 | ||
122 | /* Find last special charcter sys:[foo.bar]openssl */ | |
123 | for (p = argv0 + strlen(argv0); --p > argv0;) | |
124 | if (*p == ':' || *p == ']' || *p == '>') { | |
125 | p++; | |
126 | break; | |
127 | } | |
128 | ||
129 | q = strrchr(p, '.'); | |
130 | strncpy(prog, p, sizeof prog - 1); | |
131 | prog[sizeof prog - 1] = '\0'; | |
132 | if (q == NULL || q - p >= sizeof prog) | |
133 | prog[q - p] = '\0'; | |
134 | return prog; | |
135 | } | |
136 | ||
137 | #else | |
138 | ||
139 | char *opt_progname(const char *argv0) | |
140 | { | |
141 | const char *p; | |
142 | ||
143 | /* Could use strchr, but this is like the ones above. */ | |
144 | for (p = argv0 + strlen(argv0); --p > argv0;) | |
145 | if (*p == '/') { | |
146 | p++; | |
147 | break; | |
148 | } | |
149 | strncpy(prog, p, sizeof prog - 1); | |
150 | prog[sizeof prog - 1] = '\0'; | |
151 | return prog; | |
152 | } | |
153 | #endif | |
154 | ||
155 | char *opt_getprog(void) | |
156 | { | |
157 | return prog; | |
158 | } | |
159 | ||
160 | /* Set up the arg parsing. */ | |
161 | char *opt_init(int ac, char **av, const OPTIONS *o) | |
162 | { | |
163 | /* Store state. */ | |
164 | argc = ac; | |
165 | argv = av; | |
166 | opt_index = 1; | |
167 | opts = o; | |
168 | opt_progname(av[0]); | |
169 | unknown = NULL; | |
170 | ||
171 | for (; o->name; ++o) { | |
172 | const OPTIONS *next; | |
173 | #ifndef NDEBUG | |
88806cfc | 174 | int duplicated, i; |
7e1b7485 RS |
175 | #endif |
176 | ||
177 | if (o->name == OPT_HELP_STR || o->name == OPT_MORE_STR) | |
178 | continue; | |
179 | #ifndef NDEBUG | |
180 | i = o->valtype; | |
181 | ||
182 | /* Make sure options are legit. */ | |
183 | assert(o->name[0] != '-'); | |
184 | assert(o->retval > 0); | |
185 | assert(i == 0 || i == '-' | |
186 | || i == 'n' || i == 'p' || i == 'u' | |
187 | || i == 's' || i == '<' || i == '>' || i == '/' | |
188 | || i == 'f' || i == 'F'); | |
189 | ||
190 | /* Make sure there are no duplicates. */ | |
88806cfc | 191 | for (next = o + 1; next->name; ++next) { |
7e1b7485 | 192 | /* |
88806cfc | 193 | * Some compilers inline strcmp and the assert string is too long. |
7e1b7485 | 194 | */ |
88806cfc RS |
195 | duplicated = strcmp(o->name, next->name) == 0; |
196 | assert(!duplicated); | |
7e1b7485 RS |
197 | } |
198 | #endif | |
199 | if (o->name[0] == '\0') { | |
200 | assert(unknown == NULL); | |
201 | unknown = o; | |
202 | assert(unknown->valtype == 0 || unknown->valtype == '-'); | |
203 | } | |
204 | } | |
205 | return prog; | |
206 | } | |
207 | ||
208 | static OPT_PAIR formats[] = { | |
209 | {"PEM/DER", OPT_FMT_PEMDER}, | |
210 | {"pkcs12", OPT_FMT_PKCS12}, | |
211 | {"smime", OPT_FMT_SMIME}, | |
212 | {"engine", OPT_FMT_ENGINE}, | |
213 | {"msblob", OPT_FMT_MSBLOB}, | |
214 | {"netscape", OPT_FMT_NETSCAPE}, | |
215 | {"nss", OPT_FMT_NSS}, | |
216 | {"text", OPT_FMT_TEXT}, | |
217 | {"http", OPT_FMT_HTTP}, | |
218 | {"pvk", OPT_FMT_PVK}, | |
219 | {NULL} | |
220 | }; | |
221 | ||
222 | /* Print an error message about a failed format parse. */ | |
223 | int opt_format_error(const char *s, unsigned long flags) | |
224 | { | |
225 | OPT_PAIR *ap; | |
226 | ||
227 | if (flags == OPT_FMT_PEMDER) | |
228 | BIO_printf(bio_err, "%s: Bad format \"%s\"; must be pem or der\n", | |
229 | prog, s); | |
230 | else { | |
231 | BIO_printf(bio_err, "%s: Bad format \"%s\"; must be one of:\n", | |
232 | prog, s); | |
233 | for (ap = formats; ap->name; ap++) | |
234 | if (flags & ap->retval) | |
235 | BIO_printf(bio_err, " %s\n", ap->name); | |
236 | } | |
237 | return 0; | |
238 | } | |
239 | ||
240 | /* Parse a format string, put it into *result; return 0 on failure, else 1. */ | |
241 | int opt_format(const char *s, unsigned long flags, int *result) | |
242 | { | |
243 | switch (*s) { | |
244 | default: | |
245 | return 0; | |
246 | case 'D': | |
247 | case 'd': | |
248 | if ((flags & OPT_FMT_PEMDER) == 0) | |
249 | return opt_format_error(s, flags); | |
250 | *result = FORMAT_ASN1; | |
251 | break; | |
252 | case 'T': | |
253 | case 't': | |
254 | if ((flags & OPT_FMT_TEXT) == 0) | |
255 | return opt_format_error(s, flags); | |
256 | *result = FORMAT_TEXT; | |
257 | break; | |
258 | case 'N': | |
259 | case 'n': | |
260 | if (strcmp(s, "NSS") == 0 || strcmp(s, "nss") == 0) { | |
261 | if ((flags & OPT_FMT_NSS) == 0) | |
262 | return opt_format_error(s, flags); | |
263 | *result = FORMAT_NSS; | |
264 | } else { | |
265 | if ((flags & OPT_FMT_NETSCAPE) == 0) | |
266 | return opt_format_error(s, flags); | |
267 | *result = FORMAT_NETSCAPE; | |
268 | } | |
269 | break; | |
270 | case 'S': | |
271 | case 's': | |
272 | if ((flags & OPT_FMT_SMIME) == 0) | |
273 | return opt_format_error(s, flags); | |
274 | *result = FORMAT_SMIME; | |
275 | break; | |
276 | case 'M': | |
277 | case 'm': | |
278 | if ((flags & OPT_FMT_MSBLOB) == 0) | |
279 | return opt_format_error(s, flags); | |
280 | *result = FORMAT_MSBLOB; | |
281 | break; | |
282 | case 'E': | |
283 | case 'e': | |
284 | if ((flags & OPT_FMT_ENGINE) == 0) | |
285 | return opt_format_error(s, flags); | |
286 | *result = FORMAT_ENGINE; | |
287 | break; | |
288 | case 'H': | |
289 | case 'h': | |
290 | if ((flags & OPT_FMT_HTTP) == 0) | |
291 | return opt_format_error(s, flags); | |
292 | *result = FORMAT_HTTP; | |
293 | break; | |
294 | case '1': | |
295 | if ((flags & OPT_FMT_PKCS12) == 0) | |
296 | return opt_format_error(s, flags); | |
297 | *result = FORMAT_PKCS12; | |
298 | break; | |
299 | case 'P': | |
300 | case 'p': | |
301 | if (s[1] == '\0' || strcmp(s, "PEM") == 0 || strcmp(s, "pem") == 0) { | |
302 | if ((flags & OPT_FMT_PEMDER) == 0) | |
303 | return opt_format_error(s, flags); | |
304 | *result = FORMAT_PEM; | |
305 | } else if (strcmp(s, "PVK") == 0 || strcmp(s, "pvk") == 0) { | |
306 | if ((flags & OPT_FMT_PVK) == 0) | |
307 | return opt_format_error(s, flags); | |
308 | *result = FORMAT_PVK; | |
309 | } else if (strcmp(s, "P12") == 0 || strcmp(s, "p12") == 0 | |
310 | || strcmp(s, "PKCS12") == 0 || strcmp(s, "pkcs12") == 0) { | |
311 | if ((flags & OPT_FMT_PKCS12) == 0) | |
312 | return opt_format_error(s, flags); | |
313 | *result = FORMAT_PKCS12; | |
314 | } else | |
315 | return 0; | |
316 | break; | |
317 | } | |
318 | return 1; | |
319 | } | |
320 | ||
321 | /* Parse a cipher name, put it in *EVP_CIPHER; return 0 on failure, else 1. */ | |
322 | int opt_cipher(const char *name, const EVP_CIPHER **cipherp) | |
323 | { | |
324 | *cipherp = EVP_get_cipherbyname(name); | |
325 | if (*cipherp) | |
326 | return 1; | |
327 | BIO_printf(bio_err, "%s: Unknown cipher %s\n", prog, name); | |
328 | return 0; | |
329 | } | |
330 | ||
331 | /* | |
332 | * Parse message digest name, put it in *EVP_MD; return 0 on failure, else 1. | |
333 | */ | |
334 | int opt_md(const char *name, const EVP_MD **mdp) | |
335 | { | |
336 | *mdp = EVP_get_digestbyname(name); | |
337 | if (*mdp) | |
338 | return 1; | |
339 | BIO_printf(bio_err, "%s: Unknown digest %s\n", prog, name); | |
340 | return 0; | |
341 | } | |
342 | ||
343 | /* Look through a list of name/value pairs. */ | |
344 | int opt_pair(const char *name, const OPT_PAIR* pairs, int *result) | |
345 | { | |
346 | const OPT_PAIR *pp; | |
347 | ||
348 | for (pp = pairs; pp->name; pp++) | |
349 | if (strcmp(pp->name, name) == 0) { | |
350 | *result = pp->retval; | |
351 | return 1; | |
352 | } | |
353 | BIO_printf(bio_err, "%s: Value must be one of:\n", prog); | |
354 | for (pp = pairs; pp->name; pp++) | |
355 | BIO_printf(bio_err, "\t%s\n", pp->name); | |
356 | return 0; | |
357 | } | |
358 | ||
359 | /* See if cp looks like a hex number, in case user left off the 0x */ | |
360 | static int scanforhex(const char *cp) | |
361 | { | |
362 | if (*cp == '0' && (cp[1] == 'x' || cp[1] == 'X')) | |
363 | return 16; | |
364 | for (; *cp; cp++) | |
365 | /* Look for a hex digit that isn't a regular digit. */ | |
366 | if (isxdigit(*cp) && !isdigit(*cp)) | |
367 | return 16; | |
368 | return 0; | |
369 | } | |
370 | ||
371 | /* Parse an int, put it into *result; return 0 on failure, else 1. */ | |
372 | int opt_int(const char *value, int *result) | |
373 | { | |
374 | const char *fmt = "%d"; | |
375 | int base = scanforhex(value); | |
376 | ||
377 | if (base == 16) | |
378 | fmt = "%x"; | |
379 | else if (*value == '0') | |
380 | fmt = "%o"; | |
381 | if (sscanf(value, fmt, result) != 1) { | |
382 | BIO_printf(bio_err, "%s: Can't parse \"%s\" as a number\n", | |
383 | prog, value); | |
384 | return 0; | |
385 | } | |
386 | return 1; | |
387 | } | |
388 | ||
389 | /* Parse a long, put it into *result; return 0 on failure, else 1. */ | |
390 | int opt_long(const char *value, long *result) | |
391 | { | |
392 | char *endptr; | |
393 | int base = scanforhex(value); | |
394 | ||
395 | *result = strtol(value, &endptr, base); | |
396 | if (*endptr) { | |
397 | BIO_printf(bio_err, | |
398 | "%s: Bad char %c in number %s\n", prog, *endptr, value); | |
399 | return 0; | |
400 | } | |
401 | return 1; | |
402 | } | |
403 | ||
404 | /* | |
405 | * Parse an unsigned long, put it into *result; return 0 on failure, else 1. | |
406 | */ | |
407 | int opt_ulong(const char *value, unsigned long *result) | |
408 | { | |
409 | char *endptr; | |
410 | int base = scanforhex(value); | |
411 | ||
412 | *result = strtoul(value, &endptr, base); | |
413 | if (*endptr) { | |
414 | BIO_printf(bio_err, | |
415 | "%s: Bad char %c in number %s\n", prog, *endptr, value); | |
416 | return 0; | |
417 | } | |
418 | return 1; | |
419 | } | |
420 | ||
421 | /* | |
422 | * We pass opt as an int but cast it to "enum range" so that all the | |
423 | * items in the OPT_V_ENUM enumeration are caught; this makes -Wswitch | |
424 | * in gcc do the right thing. | |
425 | */ | |
426 | enum range { OPT_V_ENUM }; | |
427 | ||
428 | int opt_verify(int opt, X509_VERIFY_PARAM *vpm) | |
429 | { | |
430 | unsigned long ul; | |
431 | int i; | |
432 | ASN1_OBJECT *otmp; | |
433 | X509_PURPOSE *xptmp; | |
434 | const X509_VERIFY_PARAM *vtmp; | |
435 | ||
436 | assert(vpm != NULL); | |
437 | assert(opt > OPT_V__FIRST); | |
438 | assert(opt < OPT_V__LAST); | |
439 | ||
440 | switch ((enum range)opt) { | |
441 | case OPT_V__FIRST: | |
442 | case OPT_V__LAST: | |
443 | return 0; | |
444 | case OPT_V_POLICY: | |
445 | otmp = OBJ_txt2obj(opt_arg(), 0); | |
446 | if (otmp == NULL) { | |
447 | BIO_printf(bio_err, "%s: Invalid Policy %s\n", prog, opt_arg()); | |
448 | return 0; | |
449 | } | |
450 | X509_VERIFY_PARAM_add0_policy(vpm, otmp); | |
451 | break; | |
452 | case OPT_V_PURPOSE: | |
453 | i = X509_PURPOSE_get_by_sname(opt_arg()); | |
454 | if (i < 0) { | |
455 | BIO_printf(bio_err, "%s: Invalid purpose %s\n", prog, opt_arg()); | |
456 | return 0; | |
457 | } | |
458 | xptmp = X509_PURPOSE_get0(i); | |
459 | i = X509_PURPOSE_get_id(xptmp); | |
460 | X509_VERIFY_PARAM_set_purpose(vpm, i); | |
461 | break; | |
462 | case OPT_V_VERIFY_NAME: | |
463 | vtmp = X509_VERIFY_PARAM_lookup(opt_arg()); | |
464 | if (vtmp == NULL) { | |
465 | BIO_printf(bio_err, "%s: Invalid verify name %s\n", | |
466 | prog, opt_arg()); | |
467 | return 0; | |
468 | } | |
469 | X509_VERIFY_PARAM_set1(vpm, vtmp); | |
470 | break; | |
471 | case OPT_V_VERIFY_DEPTH: | |
472 | i = atoi(opt_arg()); | |
473 | if (i >= 0) | |
474 | X509_VERIFY_PARAM_set_depth(vpm, i); | |
475 | break; | |
476 | case OPT_V_ATTIME: | |
477 | opt_ulong(opt_arg(), &ul); | |
478 | if (ul) | |
479 | X509_VERIFY_PARAM_set_time(vpm, (time_t)ul); | |
480 | break; | |
481 | case OPT_V_VERIFY_HOSTNAME: | |
482 | if (!X509_VERIFY_PARAM_set1_host(vpm, opt_arg(), 0)) | |
483 | return 0; | |
484 | break; | |
485 | case OPT_V_VERIFY_EMAIL: | |
486 | if (!X509_VERIFY_PARAM_set1_email(vpm, opt_arg(), 0)) | |
487 | return 0; | |
488 | break; | |
489 | case OPT_V_VERIFY_IP: | |
490 | if (!X509_VERIFY_PARAM_set1_ip_asc(vpm, opt_arg())) | |
491 | return 0; | |
492 | break; | |
493 | case OPT_V_IGNORE_CRITICAL: | |
494 | X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_IGNORE_CRITICAL); | |
495 | break; | |
496 | case OPT_V_ISSUER_CHECKS: | |
497 | X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_CB_ISSUER_CHECK); | |
498 | break; | |
499 | case OPT_V_CRL_CHECK: | |
500 | X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_CRL_CHECK); | |
501 | break; | |
502 | case OPT_V_CRL_CHECK_ALL: | |
503 | X509_VERIFY_PARAM_set_flags(vpm, | |
504 | X509_V_FLAG_CRL_CHECK | | |
505 | X509_V_FLAG_CRL_CHECK_ALL); | |
506 | break; | |
507 | case OPT_V_POLICY_CHECK: | |
508 | X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_POLICY_CHECK); | |
509 | break; | |
510 | case OPT_V_EXPLICIT_POLICY: | |
511 | X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_EXPLICIT_POLICY); | |
512 | break; | |
513 | case OPT_V_INHIBIT_ANY: | |
514 | X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_INHIBIT_ANY); | |
515 | break; | |
516 | case OPT_V_INHIBIT_MAP: | |
517 | X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_INHIBIT_MAP); | |
518 | break; | |
519 | case OPT_V_X509_STRICT: | |
520 | X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_X509_STRICT); | |
521 | break; | |
522 | case OPT_V_EXTENDED_CRL: | |
523 | X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_EXTENDED_CRL_SUPPORT); | |
524 | break; | |
525 | case OPT_V_USE_DELTAS: | |
526 | X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_USE_DELTAS); | |
527 | break; | |
528 | case OPT_V_POLICY_PRINT: | |
529 | X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_NOTIFY_POLICY); | |
530 | break; | |
531 | case OPT_V_CHECK_SS_SIG: | |
532 | X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_CHECK_SS_SIGNATURE); | |
533 | break; | |
534 | case OPT_V_TRUSTED_FIRST: | |
535 | X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_TRUSTED_FIRST); | |
536 | break; | |
537 | case OPT_V_SUITEB_128_ONLY: | |
538 | X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_SUITEB_128_LOS_ONLY); | |
539 | break; | |
540 | case OPT_V_SUITEB_128: | |
541 | X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_SUITEB_128_LOS); | |
542 | break; | |
543 | case OPT_V_SUITEB_192: | |
544 | X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_SUITEB_192_LOS); | |
545 | break; | |
546 | case OPT_V_PARTIAL_CHAIN: | |
547 | X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_PARTIAL_CHAIN); | |
548 | break; | |
549 | case OPT_V_NO_ALT_CHAINS: | |
550 | X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_NO_ALT_CHAINS); | |
551 | } | |
552 | return 1; | |
553 | ||
554 | } | |
555 | ||
556 | /* | |
557 | * Parse the next flag (and value if specified), return 0 if done, -1 on | |
558 | * error, otherwise the flag's retval. | |
559 | */ | |
560 | int opt_next(void) | |
561 | { | |
562 | char *p; | |
563 | char *endptr; | |
564 | const OPTIONS *o; | |
565 | int dummy; | |
566 | int base; | |
567 | long val; | |
568 | ||
569 | /* Look at current arg; at end of the list? */ | |
570 | arg = NULL; | |
571 | p = argv[opt_index]; | |
572 | if (p == NULL) | |
573 | return 0; | |
574 | ||
575 | /* If word doesn't start with a -, we're done. */ | |
576 | if (*p != '-') | |
577 | return 0; | |
578 | ||
579 | /* Hit "--" ? We're done. */ | |
580 | opt_index++; | |
581 | if (strcmp(p, "--") == 0) | |
582 | return 0; | |
583 | ||
584 | /* Allow -nnn and --nnn */ | |
585 | if (*++p == '-') | |
586 | p++; | |
587 | flag = p - 1; | |
588 | ||
589 | /* If we have --flag=foo, snip it off */ | |
590 | if ((arg = strchr(p, '=')) != NULL) | |
591 | *arg++ = '\0'; | |
592 | for (o = opts; o->name; ++o) { | |
593 | /* If not this option, move on to the next one. */ | |
594 | if (strcmp(p, o->name) != 0) | |
595 | continue; | |
596 | ||
597 | /* If it doesn't take a value, make sure none was given. */ | |
598 | if (o->valtype == 0 || o->valtype == '-') { | |
599 | if (arg) { | |
600 | BIO_printf(bio_err, | |
601 | "%s: Option -%s does not take a value\n", prog, p); | |
602 | return -1; | |
603 | } | |
604 | return o->retval; | |
605 | } | |
606 | ||
607 | /* Want a value; get the next param if =foo not used. */ | |
608 | if (arg == NULL) { | |
609 | if (argv[opt_index] == NULL) { | |
610 | BIO_printf(bio_err, | |
611 | "%s: Option -%s needs a value\n", prog, o->name); | |
612 | return -1; | |
613 | } | |
614 | arg = argv[opt_index++]; | |
615 | } | |
616 | ||
617 | /* Syntax-check value. */ | |
618 | /* | |
619 | * Do some basic syntax-checking on the value. These tests aren't | |
620 | * perfect (ignore range overflow) but they catch common failures. | |
621 | */ | |
622 | switch (o->valtype) { | |
623 | default: | |
624 | case 's': | |
625 | /* Just a string. */ | |
626 | break; | |
627 | case '/': | |
628 | if (app_isdir(arg) >= 0) | |
629 | break; | |
630 | BIO_printf(bio_err, "%s: Not a directory: %s\n", prog, arg); | |
631 | return -1; | |
632 | case '<': | |
633 | /* Input file. */ | |
634 | if (strcmp(arg, "-") == 0 || app_access(arg, R_OK) >= 0) | |
635 | break; | |
636 | BIO_printf(bio_err, | |
637 | "%s: Cannot open input file %s, %s\n", | |
638 | prog, arg, strerror(errno)); | |
639 | return -1; | |
640 | case '>': | |
641 | /* Output file. */ | |
642 | if (strcmp(arg, "-") == 0 || app_access(arg, W_OK) >= 0 || errno == ENOENT) | |
643 | break; | |
644 | BIO_printf(bio_err, | |
645 | "%s: Cannot open output file %s, %s\n", | |
646 | prog, arg, strerror(errno)); | |
647 | return -1; | |
648 | case 'p': | |
649 | case 'n': | |
650 | base = scanforhex(arg); | |
651 | val = strtol(arg, &endptr, base); | |
652 | if (*endptr == '\0') { | |
653 | if (o->valtype == 'p' && val <= 0) { | |
654 | BIO_printf(bio_err, | |
655 | "%s: Non-positive number \"%s\" for -%s\n", | |
656 | prog, arg, o->name); | |
657 | return -1; | |
658 | } | |
659 | break; | |
660 | } | |
661 | BIO_printf(bio_err, | |
662 | "%s: Invalid number \"%s\" for -%s\n", | |
663 | prog, arg, o->name); | |
664 | return -1; | |
665 | case 'u': | |
666 | base = scanforhex(arg); | |
667 | strtoul(arg, &endptr, base); | |
668 | if (*endptr == '\0') | |
669 | break; | |
670 | BIO_printf(bio_err, | |
671 | "%s: Invalid number \"%s\" for -%s\n", | |
672 | prog, arg, o->name); | |
673 | return -1; | |
674 | case 'f': | |
675 | case 'F': | |
676 | if (opt_format(arg, | |
677 | o->valtype == 'F' ? OPT_FMT_PEMDER | |
678 | : OPT_FMT_ANY, &dummy)) | |
679 | break; | |
680 | BIO_printf(bio_err, | |
681 | "%s: Invalid format \"%s\" for -%s\n", | |
682 | prog, arg, o->name); | |
683 | return -1; | |
684 | } | |
685 | ||
686 | /* Return the flag value. */ | |
687 | return o->retval; | |
688 | } | |
689 | if (unknown != NULL) { | |
690 | dunno = p; | |
691 | return unknown->retval; | |
692 | } | |
693 | BIO_printf(bio_err, "%s: Option unknown option -%s\n", prog, p); | |
694 | return -1; | |
695 | } | |
696 | ||
697 | /* Return the most recent flag parameter. */ | |
698 | char *opt_arg(void) | |
699 | { | |
700 | return arg; | |
701 | } | |
702 | ||
703 | /* Return the most recent flag. */ | |
704 | char *opt_flag(void) | |
705 | { | |
706 | return flag; | |
707 | } | |
708 | ||
709 | /* Return the unknown option. */ | |
710 | char *opt_unknown(void) | |
711 | { | |
712 | return dunno; | |
713 | } | |
714 | ||
715 | /* Return the rest of the arguments after parsing flags. */ | |
716 | char **opt_rest(void) | |
717 | { | |
718 | return &argv[opt_index]; | |
719 | } | |
720 | ||
721 | /* How many items in remaining args? */ | |
722 | int opt_num_rest(void) | |
723 | { | |
724 | int i = 0; | |
725 | char **pp; | |
726 | ||
727 | for (pp = opt_rest(); *pp; pp++, i++) | |
728 | continue; | |
729 | return i; | |
730 | } | |
731 | ||
732 | /* Return a string describing the parameter type. */ | |
733 | static const char *valtype2param(const OPTIONS *o) | |
734 | { | |
735 | switch (o->valtype) { | |
736 | case '-': | |
737 | return ""; | |
738 | case 's': | |
739 | return "val"; | |
740 | case '/': | |
741 | return "dir"; | |
742 | case '<': | |
743 | return "infile"; | |
744 | case '>': | |
745 | return "outfile"; | |
746 | case 'p': | |
747 | return "pnum"; | |
748 | case 'n': | |
749 | return "num"; | |
750 | case 'u': | |
751 | return "unum"; | |
752 | case 'F': | |
753 | return "der/pem"; | |
754 | case 'f': | |
755 | return "format"; | |
756 | } | |
757 | return "parm"; | |
758 | } | |
759 | ||
760 | void opt_help(const OPTIONS *list) | |
761 | { | |
762 | const OPTIONS *o; | |
763 | int i; | |
764 | int standard_prolog; | |
765 | int width = 5; | |
766 | char start[80 + 1]; | |
767 | char *p; | |
768 | const char *help; | |
769 | ||
770 | /* Starts with its own help message? */ | |
771 | standard_prolog = list[0].name != OPT_HELP_STR; | |
772 | ||
773 | /* Find the widest help. */ | |
774 | for (o = list; o->name; o++) { | |
775 | if (o->name == OPT_MORE_STR) | |
776 | continue; | |
777 | i = 2 + (int)strlen(o->name); | |
778 | if (o->valtype != '-') | |
779 | i += 1 + strlen(valtype2param(o)); | |
780 | if (i < MAX_OPT_HELP_WIDTH && i > width) | |
781 | width = i; | |
782 | assert(i < (int)sizeof start); | |
783 | } | |
784 | ||
785 | if (standard_prolog) | |
786 | BIO_printf(bio_err, "Usage: %s [options]\nValid options are:\n", | |
787 | prog); | |
788 | ||
789 | /* Now let's print. */ | |
790 | for (o = list; o->name; o++) { | |
791 | help = o->helpstr ? o->helpstr : "(No additional info)"; | |
792 | if (o->name == OPT_HELP_STR) { | |
793 | BIO_printf(bio_err, help, prog); | |
794 | continue; | |
795 | } | |
796 | ||
797 | /* Pad out prefix */ | |
798 | memset(start, ' ', sizeof start - 1); | |
799 | start[sizeof start - 1] = '\0'; | |
800 | ||
801 | if (o->name == OPT_MORE_STR) { | |
802 | /* Continuation of previous line; padd and print. */ | |
803 | start[width] = '\0'; | |
804 | BIO_printf(bio_err, "%s %s\n", start, help); | |
805 | continue; | |
806 | } | |
807 | ||
808 | /* Build up the "-flag [param]" part. */ | |
809 | p = start; | |
810 | *p++ = ' '; | |
811 | *p++ = '-'; | |
812 | if (o->name[0]) | |
813 | p += strlen(strcpy(p, o->name)); | |
814 | else | |
815 | *p++ = '*'; | |
816 | if (o->valtype != '-') { | |
817 | *p++ = ' '; | |
818 | p += strlen(strcpy(p, valtype2param(o))); | |
819 | } | |
820 | *p = ' '; | |
821 | if ((int)(p - start) >= MAX_OPT_HELP_WIDTH) { | |
822 | *p = '\0'; | |
823 | BIO_printf(bio_err, "%s\n", start); | |
824 | memset(start, ' ', sizeof start); | |
825 | } | |
826 | start[width] = '\0'; | |
827 | BIO_printf(bio_err, "%s %s\n", start, help); | |
828 | } | |
829 | } | |
830 | ||
831 | #ifdef COMPILE_STANDALONE_TEST_DRIVER | |
832 | # include <sys/stat.h> | |
833 | ||
834 | typedef enum OPTION_choice { | |
835 | OPT_ERR = -1, OPT_EOF = 0, OPT_HELP, | |
836 | OPT_IN, OPT_INFORM, OPT_OUT, OPT_COUNT, OPT_U, OPT_FLAG, | |
837 | OPT_STR, OPT_NOTUSED | |
838 | } OPTION_CHOICE; | |
839 | ||
840 | static OPTIONS options[] = { | |
841 | {OPT_HELP_STR, 1, '-', "Usage: %s flags\n"}, | |
842 | {OPT_HELP_STR, 1, '-', "Valid options are:\n"}, | |
843 | {"help", OPT_HELP, '-', "Display this summary"}, | |
844 | {"in", OPT_IN, '<', "input file"}, | |
845 | {OPT_MORE_STR, 1, '-', "more detail about input"}, | |
846 | {"inform", OPT_INFORM, 'f', "input file format; defaults to pem"}, | |
847 | {"out", OPT_OUT, '>', "output file"}, | |
848 | {"count", OPT_COUNT, 'p', "a counter greater than zero"}, | |
849 | {"u", OPT_U, 'u', "an unsigned number"}, | |
850 | {"flag", OPT_FLAG, 0, "just some flag"}, | |
851 | {"str", OPT_STR, 's', "the magic word"}, | |
852 | {"areallyverylongoption", OPT_HELP, '-', "long way for help"}, | |
853 | {NULL} | |
854 | }; | |
855 | ||
856 | BIO *bio_err; | |
857 | ||
858 | int app_isdir(const char *name) | |
859 | { | |
860 | struct stat sb; | |
861 | ||
862 | return name != NULL && stat(name, &sb) >= 0 && S_ISDIR(sb.st_mode); | |
863 | } | |
864 | ||
865 | int main(int ac, char **av) | |
866 | { | |
867 | OPTION_CHOICE o; | |
868 | char **rest; | |
869 | char *prog; | |
870 | ||
871 | bio_err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT); | |
872 | ||
873 | prog = opt_init(ac, av, options); | |
874 | while ((o = opt_next()) != OPT_EOF) { | |
875 | switch (c) { | |
876 | case OPT_NOTUSED: | |
877 | case OPT_EOF: | |
878 | case OPT_ERR: | |
879 | printf("%s: Usage error; try -help.\n", prog); | |
880 | return 1; | |
881 | case OPT_HELP: | |
882 | opt_help(options); | |
883 | return 0; | |
884 | case OPT_IN: | |
885 | printf("in %s\n", opt_arg()); | |
886 | break; | |
887 | case OPT_INFORM: | |
888 | printf("inform %s\n", opt_arg()); | |
889 | break; | |
890 | case OPT_OUT: | |
891 | printf("out %s\n", opt_arg()); | |
892 | break; | |
893 | case OPT_COUNT: | |
894 | printf("count %s\n", opt_arg()); | |
895 | break; | |
896 | case OPT_U: | |
897 | printf("u %s\n", opt_arg()); | |
898 | break; | |
899 | case OPT_FLAG: | |
900 | printf("flag\n"); | |
901 | break; | |
902 | case OPT_STR: | |
903 | printf("str %s\n", opt_arg()); | |
904 | break; | |
905 | } | |
906 | } | |
907 | argc = opt_num_rest(); | |
908 | argv = opt_rest(); | |
909 | ||
910 | printf("args = %d\n", argc); | |
911 | if (argc) | |
912 | while (*argv) | |
913 | printf(" %s\n", *argv++); | |
914 | return 0; | |
915 | } | |
916 | #endif |