]> git.ipfire.org Git - thirdparty/openssl.git/blame - apps/lib/opt.c
Fix X509_PUBKEY_cmp(), move to crypto/x509/x_pubkey.c, rename, export, and document it
[thirdparty/openssl.git] / apps / lib / opt.c
CommitLineData
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
29const char OPT_HELP_STR[] = "-H";
30const char OPT_MORE_STR[] = "-M";
31const char OPT_SECTION_STR[] = "-S";
32const char OPT_PARAM_STR[] = "-P";
7e1b7485
RS
33
34/* Our state */
35static char **argv;
36static int argc;
37static int opt_index;
38static char *arg;
39static char *flag;
40static char *dunno;
41static const OPTIONS *unknown;
42static const OPTIONS *opts;
43static 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
49char *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
79char *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
100char *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
116char *opt_getprog(void)
117{
118 return prog;
119}
120
121/* Set up the arg parsing. */
122char *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
176static 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. */
190int 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. */
208int 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
51c833ac
DO
285/* Return string representing the given format. */
286const char *format2str(int format)
287{
288 switch (format) {
289 default:
290 return "(undefined)";
291 case FORMAT_PEM:
292 return "PEM";
293 case FORMAT_ASN1:
294 return "DER";
295 case FORMAT_TEXT:
296 return "TEXT";
297 case FORMAT_NSS:
298 return "NSS";
299 case FORMAT_SMIME:
300 return "SMIME";
301 case FORMAT_MSBLOB:
302 return "MSBLOB";
303 case FORMAT_ENGINE:
304 return "ENGINE";
305 case FORMAT_HTTP:
306 return "HTTP";
307 case FORMAT_PKCS12:
308 return "P12";
309 case FORMAT_PVK:
310 return "PVK";
311 }
312}
313
314/* Print an error message about unsuitable/unsupported format requested. */
315void print_format_error(int format, unsigned long flags)
316{
317 (void)opt_format_error(format2str(format), flags);
318}
319
7e1b7485
RS
320/* Parse a cipher name, put it in *EVP_CIPHER; return 0 on failure, else 1. */
321int opt_cipher(const char *name, const EVP_CIPHER **cipherp)
322{
323 *cipherp = EVP_get_cipherbyname(name);
2234212c 324 if (*cipherp != NULL)
7e1b7485 325 return 1;
e0e68f9e 326 opt_printf_stderr("%s: Unknown cipher: %s\n", prog, name);
7e1b7485
RS
327 return 0;
328}
329
330/*
331 * Parse message digest name, put it in *EVP_MD; return 0 on failure, else 1.
332 */
333int opt_md(const char *name, const EVP_MD **mdp)
334{
335 *mdp = EVP_get_digestbyname(name);
2234212c 336 if (*mdp != NULL)
7e1b7485 337 return 1;
e0e68f9e 338 opt_printf_stderr("%s: Unknown message digest: %s\n", prog, name);
7e1b7485
RS
339 return 0;
340}
341
342/* Look through a list of name/value pairs. */
343int opt_pair(const char *name, const OPT_PAIR* pairs, int *result)
344{
345 const OPT_PAIR *pp;
346
347 for (pp = pairs; pp->name; pp++)
348 if (strcmp(pp->name, name) == 0) {
349 *result = pp->retval;
350 return 1;
351 }
a43ce58f 352 opt_printf_stderr("%s: Value must be one of:\n", prog);
7e1b7485 353 for (pp = pairs; pp->name; pp++)
a43ce58f 354 opt_printf_stderr("\t%s\n", pp->name);
7e1b7485
RS
355 return 0;
356}
357
7e1b7485
RS
358/* Parse an int, put it into *result; return 0 on failure, else 1. */
359int opt_int(const char *value, int *result)
360{
bd4850df
RS
361 long l;
362
363 if (!opt_long(value, &l))
364 return 0;
365 *result = (int)l;
366 if (*result != l) {
a43ce58f
SL
367 opt_printf_stderr("%s: Value \"%s\" outside integer range\n",
368 prog, value);
7e1b7485
RS
369 return 0;
370 }
371 return 1;
372}
373
7447c49f
PY
374static void opt_number_error(const char *v)
375{
376 size_t i = 0;
377 struct strstr_pair_st {
378 char *prefix;
379 char *name;
380 } b[] = {
381 {"0x", "a hexadecimal"},
382 {"0X", "a hexadecimal"},
383 {"0", "an octal"}
384 };
385
386 for (i = 0; i < OSSL_NELEM(b); i++) {
387 if (strncmp(v, b[i].prefix, strlen(b[i].prefix)) == 0) {
a43ce58f
SL
388 opt_printf_stderr("%s: Can't parse \"%s\" as %s number\n",
389 prog, v, b[i].name);
7447c49f
PY
390 return;
391 }
392 }
a43ce58f 393 opt_printf_stderr("%s: Can't parse \"%s\" as a number\n", prog, v);
7447c49f
PY
394 return;
395}
396
7e1b7485
RS
397/* Parse a long, put it into *result; return 0 on failure, else 1. */
398int opt_long(const char *value, long *result)
399{
bd4850df
RS
400 int oerrno = errno;
401 long l;
402 char *endp;
403
c3a7e0c5 404 errno = 0;
bd4850df
RS
405 l = strtol(value, &endp, 0);
406 if (*endp
407 || endp == value
408 || ((l == LONG_MAX || l == LONG_MIN) && errno == ERANGE)
409 || (l == 0 && errno != 0)) {
7447c49f 410 opt_number_error(value);
bd4850df 411 errno = oerrno;
7e1b7485
RS
412 return 0;
413 }
bd4850df
RS
414 *result = l;
415 errno = oerrno;
7e1b7485
RS
416 return 1;
417}
418
d94a1a70 419#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L && \
53f54696
SL
420 defined(INTMAX_MAX) && defined(UINTMAX_MAX) && \
421 !defined(OPENSSL_NO_INTTYPES_H)
03f887ca
VD
422
423/* Parse an intmax_t, put it into *result; return 0 on failure, else 1. */
424int opt_imax(const char *value, intmax_t *result)
425{
426 int oerrno = errno;
427 intmax_t m;
428 char *endp;
429
c3a7e0c5 430 errno = 0;
03f887ca
VD
431 m = strtoimax(value, &endp, 0);
432 if (*endp
433 || endp == value
434 || ((m == INTMAX_MAX || m == INTMAX_MIN) && errno == ERANGE)
435 || (m == 0 && errno != 0)) {
7447c49f 436 opt_number_error(value);
03f887ca
VD
437 errno = oerrno;
438 return 0;
439 }
440 *result = m;
441 errno = oerrno;
442 return 1;
443}
444
445/* Parse a uintmax_t, put it into *result; return 0 on failure, else 1. */
446int opt_umax(const char *value, uintmax_t *result)
447{
448 int oerrno = errno;
449 uintmax_t m;
450 char *endp;
451
c3a7e0c5 452 errno = 0;
03f887ca
VD
453 m = strtoumax(value, &endp, 0);
454 if (*endp
455 || endp == value
456 || (m == UINTMAX_MAX && errno == ERANGE)
457 || (m == 0 && errno != 0)) {
7447c49f 458 opt_number_error(value);
03f887ca
VD
459 errno = oerrno;
460 return 0;
461 }
462 *result = m;
463 errno = oerrno;
464 return 1;
465}
466#endif
467
7e1b7485
RS
468/*
469 * Parse an unsigned long, put it into *result; return 0 on failure, else 1.
470 */
471int opt_ulong(const char *value, unsigned long *result)
472{
bd4850df 473 int oerrno = errno;
7e1b7485 474 char *endptr;
bd4850df
RS
475 unsigned long l;
476
c3a7e0c5 477 errno = 0;
bd4850df
RS
478 l = strtoul(value, &endptr, 0);
479 if (*endptr
480 || endptr == value
481 || ((l == ULONG_MAX) && errno == ERANGE)
482 || (l == 0 && errno != 0)) {
7447c49f 483 opt_number_error(value);
bd4850df 484 errno = oerrno;
7e1b7485
RS
485 return 0;
486 }
bd4850df
RS
487 *result = l;
488 errno = oerrno;
7e1b7485
RS
489 return 1;
490}
491
492/*
493 * We pass opt as an int but cast it to "enum range" so that all the
494 * items in the OPT_V_ENUM enumeration are caught; this makes -Wswitch
495 * in gcc do the right thing.
496 */
497enum range { OPT_V_ENUM };
498
499int opt_verify(int opt, X509_VERIFY_PARAM *vpm)
500{
7e1b7485 501 int i;
03f887ca 502 ossl_intmax_t t = 0;
7e1b7485
RS
503 ASN1_OBJECT *otmp;
504 X509_PURPOSE *xptmp;
505 const X509_VERIFY_PARAM *vtmp;
506
a43ce58f
SL
507 OPENSSL_assert(vpm != NULL);
508 OPENSSL_assert(opt > OPT_V__FIRST);
509 OPENSSL_assert(opt < OPT_V__LAST);
7e1b7485
RS
510
511 switch ((enum range)opt) {
512 case OPT_V__FIRST:
513 case OPT_V__LAST:
514 return 0;
515 case OPT_V_POLICY:
516 otmp = OBJ_txt2obj(opt_arg(), 0);
517 if (otmp == NULL) {
a43ce58f 518 opt_printf_stderr("%s: Invalid Policy %s\n", prog, opt_arg());
7e1b7485
RS
519 return 0;
520 }
521 X509_VERIFY_PARAM_add0_policy(vpm, otmp);
522 break;
523 case OPT_V_PURPOSE:
0daccd4d 524 /* purpose name -> purpose index */
7e1b7485
RS
525 i = X509_PURPOSE_get_by_sname(opt_arg());
526 if (i < 0) {
a43ce58f 527 opt_printf_stderr("%s: Invalid purpose %s\n", prog, opt_arg());
7e1b7485
RS
528 return 0;
529 }
0daccd4d
VD
530
531 /* purpose index -> purpose object */
7e1b7485 532 xptmp = X509_PURPOSE_get0(i);
0daccd4d
VD
533
534 /* purpose object -> purpose value */
7e1b7485 535 i = X509_PURPOSE_get_id(xptmp);
0daccd4d
VD
536
537 if (!X509_VERIFY_PARAM_set_purpose(vpm, i)) {
a43ce58f
SL
538 opt_printf_stderr("%s: Internal error setting purpose %s\n",
539 prog, opt_arg());
0daccd4d
VD
540 return 0;
541 }
7e1b7485
RS
542 break;
543 case OPT_V_VERIFY_NAME:
544 vtmp = X509_VERIFY_PARAM_lookup(opt_arg());
545 if (vtmp == NULL) {
a43ce58f
SL
546 opt_printf_stderr("%s: Invalid verify name %s\n",
547 prog, opt_arg());
7e1b7485
RS
548 return 0;
549 }
550 X509_VERIFY_PARAM_set1(vpm, vtmp);
551 break;
552 case OPT_V_VERIFY_DEPTH:
553 i = atoi(opt_arg());
554 if (i >= 0)
555 X509_VERIFY_PARAM_set_depth(vpm, i);
556 break;
fbb82a60
VD
557 case OPT_V_VERIFY_AUTH_LEVEL:
558 i = atoi(opt_arg());
559 if (i >= 0)
560 X509_VERIFY_PARAM_set_auth_level(vpm, i);
561 break;
7e1b7485 562 case OPT_V_ATTIME:
03f887ca
VD
563 if (!opt_imax(opt_arg(), &t))
564 return 0;
565 if (t != (time_t)t) {
a43ce58f
SL
566 opt_printf_stderr("%s: epoch time out of range %s\n",
567 prog, opt_arg());
03f887ca
VD
568 return 0;
569 }
570 X509_VERIFY_PARAM_set_time(vpm, (time_t)t);
7e1b7485
RS
571 break;
572 case OPT_V_VERIFY_HOSTNAME:
573 if (!X509_VERIFY_PARAM_set1_host(vpm, opt_arg(), 0))
574 return 0;
575 break;
576 case OPT_V_VERIFY_EMAIL:
577 if (!X509_VERIFY_PARAM_set1_email(vpm, opt_arg(), 0))
578 return 0;
579 break;
580 case OPT_V_VERIFY_IP:
581 if (!X509_VERIFY_PARAM_set1_ip_asc(vpm, opt_arg()))
582 return 0;
583 break;
584 case OPT_V_IGNORE_CRITICAL:
585 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_IGNORE_CRITICAL);
586 break;
587 case OPT_V_ISSUER_CHECKS:
d33def66 588 /* NOP, deprecated */
7e1b7485
RS
589 break;
590 case OPT_V_CRL_CHECK:
591 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_CRL_CHECK);
592 break;
593 case OPT_V_CRL_CHECK_ALL:
594 X509_VERIFY_PARAM_set_flags(vpm,
595 X509_V_FLAG_CRL_CHECK |
596 X509_V_FLAG_CRL_CHECK_ALL);
597 break;
598 case OPT_V_POLICY_CHECK:
599 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_POLICY_CHECK);
600 break;
601 case OPT_V_EXPLICIT_POLICY:
602 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_EXPLICIT_POLICY);
603 break;
604 case OPT_V_INHIBIT_ANY:
605 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_INHIBIT_ANY);
606 break;
607 case OPT_V_INHIBIT_MAP:
608 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_INHIBIT_MAP);
609 break;
610 case OPT_V_X509_STRICT:
611 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_X509_STRICT);
612 break;
613 case OPT_V_EXTENDED_CRL:
614 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_EXTENDED_CRL_SUPPORT);
615 break;
616 case OPT_V_USE_DELTAS:
617 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_USE_DELTAS);
618 break;
619 case OPT_V_POLICY_PRINT:
620 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_NOTIFY_POLICY);
621 break;
622 case OPT_V_CHECK_SS_SIG:
623 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_CHECK_SS_SIGNATURE);
624 break;
625 case OPT_V_TRUSTED_FIRST:
626 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_TRUSTED_FIRST);
627 break;
628 case OPT_V_SUITEB_128_ONLY:
629 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_SUITEB_128_LOS_ONLY);
630 break;
631 case OPT_V_SUITEB_128:
632 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_SUITEB_128_LOS);
633 break;
634 case OPT_V_SUITEB_192:
635 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_SUITEB_192_LOS);
636 break;
637 case OPT_V_PARTIAL_CHAIN:
638 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_PARTIAL_CHAIN);
639 break;
640 case OPT_V_NO_ALT_CHAINS:
641 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_NO_ALT_CHAINS);
dccd20d1 642 break;
d35ff2c0
DW
643 case OPT_V_NO_CHECK_TIME:
644 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_NO_CHECK_TIME);
dccd20d1 645 break;
a392ef20
RL
646 case OPT_V_ALLOW_PROXY_CERTS:
647 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_ALLOW_PROXY_CERTS);
648 break;
7e1b7485
RS
649 }
650 return 1;
651
652}
653
a43ce58f
SL
654void opt_begin(void)
655{
656 opt_index = 1;
657 arg = NULL;
658 flag = NULL;
659}
660
7e1b7485
RS
661/*
662 * Parse the next flag (and value if specified), return 0 if done, -1 on
663 * error, otherwise the flag's retval.
664 */
665int opt_next(void)
666{
96de2e59 667 char *p;
7e1b7485 668 const OPTIONS *o;
bd4850df 669 int ival;
03f887ca
VD
670 long lval;
671 unsigned long ulval;
672 ossl_intmax_t imval;
673 ossl_uintmax_t umval;
7e1b7485
RS
674
675 /* Look at current arg; at end of the list? */
676 arg = NULL;
677 p = argv[opt_index];
678 if (p == NULL)
679 return 0;
680
681 /* If word doesn't start with a -, we're done. */
682 if (*p != '-')
683 return 0;
684
685 /* Hit "--" ? We're done. */
686 opt_index++;
687 if (strcmp(p, "--") == 0)
688 return 0;
689
690 /* Allow -nnn and --nnn */
691 if (*++p == '-')
692 p++;
693 flag = p - 1;
694
695 /* If we have --flag=foo, snip it off */
696 if ((arg = strchr(p, '=')) != NULL)
697 *arg++ = '\0';
698 for (o = opts; o->name; ++o) {
699 /* If not this option, move on to the next one. */
700 if (strcmp(p, o->name) != 0)
701 continue;
702
703 /* If it doesn't take a value, make sure none was given. */
704 if (o->valtype == 0 || o->valtype == '-') {
705 if (arg) {
a43ce58f
SL
706 opt_printf_stderr("%s: Option -%s does not take a value\n",
707 prog, p);
7e1b7485
RS
708 return -1;
709 }
710 return o->retval;
711 }
712
713 /* Want a value; get the next param if =foo not used. */
714 if (arg == NULL) {
715 if (argv[opt_index] == NULL) {
a43ce58f
SL
716 opt_printf_stderr("%s: Option -%s needs a value\n",
717 prog, o->name);
7e1b7485
RS
718 return -1;
719 }
720 arg = argv[opt_index++];
721 }
722
723 /* Syntax-check value. */
7e1b7485
RS
724 switch (o->valtype) {
725 default:
726 case 's':
fd3397fc 727 case ':':
7e1b7485
RS
728 /* Just a string. */
729 break;
730 case '/':
a43ce58f 731 if (opt_isdir(arg) > 0)
7e1b7485 732 break;
a43ce58f 733 opt_printf_stderr("%s: Not a directory: %s\n", prog, arg);
7e1b7485
RS
734 return -1;
735 case '<':
736 /* Input file. */
4522e130 737 break;
7e1b7485
RS
738 case '>':
739 /* Output file. */
4522e130 740 break;
7e1b7485
RS
741 case 'p':
742 case 'n':
bd4850df
RS
743 if (!opt_int(arg, &ival)
744 || (o->valtype == 'p' && ival <= 0)) {
a43ce58f
SL
745 opt_printf_stderr("%s: Non-positive number \"%s\" for -%s\n",
746 prog, arg, o->name);
bd4850df 747 return -1;
7e1b7485 748 }
bd4850df 749 break;
03f887ca
VD
750 case 'M':
751 if (!opt_imax(arg, &imval)) {
a43ce58f
SL
752 opt_printf_stderr("%s: Invalid number \"%s\" for -%s\n",
753 prog, arg, o->name);
03f887ca
VD
754 return -1;
755 }
756 break;
757 case 'U':
758 if (!opt_umax(arg, &umval)) {
a43ce58f
SL
759 opt_printf_stderr("%s: Invalid number \"%s\" for -%s\n",
760 prog, arg, o->name);
03f887ca
VD
761 return -1;
762 }
763 break;
0c20802c 764 case 'l':
03f887ca 765 if (!opt_long(arg, &lval)) {
a43ce58f
SL
766 opt_printf_stderr("%s: Invalid number \"%s\" for -%s\n",
767 prog, arg, o->name);
03f887ca
VD
768 return -1;
769 }
770 break;
7e1b7485 771 case 'u':
03f887ca 772 if (!opt_ulong(arg, &ulval)) {
a43ce58f
SL
773 opt_printf_stderr("%s: Invalid number \"%s\" for -%s\n",
774 prog, arg, o->name);
bd4850df
RS
775 return -1;
776 }
777 break;
f47e5647 778 case 'c':
0c20802c 779 case 'E':
7e1b7485 780 case 'F':
0c20802c 781 case 'f':
7e1b7485 782 if (opt_format(arg,
f47e5647 783 o->valtype == 'c' ? OPT_FMT_PDS :
0c20802c 784 o->valtype == 'E' ? OPT_FMT_PDE :
7e1b7485 785 o->valtype == 'F' ? OPT_FMT_PEMDER
bd4850df 786 : OPT_FMT_ANY, &ival))
7e1b7485 787 break;
a43ce58f
SL
788 opt_printf_stderr("%s: Invalid format \"%s\" for -%s\n",
789 prog, arg, o->name);
7e1b7485
RS
790 return -1;
791 }
792
793 /* Return the flag value. */
794 return o->retval;
795 }
796 if (unknown != NULL) {
797 dunno = p;
798 return unknown->retval;
799 }
e0e68f9e 800 opt_printf_stderr("%s: Unknown option: -%s\n", prog, p);
7e1b7485
RS
801 return -1;
802}
803
804/* Return the most recent flag parameter. */
805char *opt_arg(void)
806{
807 return arg;
808}
809
810/* Return the most recent flag. */
811char *opt_flag(void)
812{
813 return flag;
814}
815
816/* Return the unknown option. */
817char *opt_unknown(void)
818{
819 return dunno;
820}
821
822/* Return the rest of the arguments after parsing flags. */
823char **opt_rest(void)
824{
825 return &argv[opt_index];
826}
827
828/* How many items in remaining args? */
829int opt_num_rest(void)
830{
831 int i = 0;
832 char **pp;
833
834 for (pp = opt_rest(); *pp; pp++, i++)
835 continue;
836 return i;
837}
838
839/* Return a string describing the parameter type. */
840static const char *valtype2param(const OPTIONS *o)
841{
842 switch (o->valtype) {
6755ff11 843 case 0:
7e1b7485
RS
844 case '-':
845 return "";
fd3397fc
RL
846 case ':':
847 return "uri";
7e1b7485
RS
848 case 's':
849 return "val";
850 case '/':
851 return "dir";
852 case '<':
853 return "infile";
854 case '>':
855 return "outfile";
856 case 'p':
0c20802c 857 return "+int";
7e1b7485 858 case 'n':
0c20802c
VD
859 return "int";
860 case 'l':
861 return "long";
7e1b7485 862 case 'u':
0c20802c
VD
863 return "ulong";
864 case 'E':
865 return "PEM|DER|ENGINE";
7e1b7485 866 case 'F':
0c20802c 867 return "PEM|DER";
7e1b7485
RS
868 case 'f':
869 return "format";
0c20802c
VD
870 case 'M':
871 return "intmax";
872 case 'U':
873 return "uintmax";
7e1b7485
RS
874 }
875 return "parm";
876}
877
92de469f 878void opt_print(const OPTIONS *o, int doingparams, int width)
7e1b7485 879{
4ed83891 880 const char* help;
7e1b7485
RS
881 char start[80 + 1];
882 char *p;
7e1b7485 883
7e1b7485 884 help = o->helpstr ? o->helpstr : "(No additional info)";
5388f986
RS
885 if (o->name == OPT_HELP_STR) {
886 opt_printf_stderr(help, prog);
887 return;
888 }
889 if (o->name == OPT_SECTION_STR) {
890 opt_printf_stderr("\n");
a43ce58f 891 opt_printf_stderr(help, prog);
4ed83891 892 return;
7e1b7485 893 }
92de469f
RS
894 if (o->name == OPT_PARAM_STR) {
895 opt_printf_stderr("\nParameters:\n");
896 return;
897 }
7e1b7485
RS
898
899 /* Pad out prefix */
16f8d4eb 900 memset(start, ' ', sizeof(start) - 1);
cbe29648 901 start[sizeof(start) - 1] = '\0';
7e1b7485
RS
902
903 if (o->name == OPT_MORE_STR) {
0ad69cd6 904 /* Continuation of previous line; pad and print. */
7e1b7485 905 start[width] = '\0';
a43ce58f 906 opt_printf_stderr("%s %s\n", start, help);
4ed83891 907 return;
7e1b7485
RS
908 }
909
910 /* Build up the "-flag [param]" part. */
911 p = start;
912 *p++ = ' ';
92de469f
RS
913 if (!doingparams)
914 *p++ = '-';
7e1b7485
RS
915 if (o->name[0])
916 p += strlen(strcpy(p, o->name));
917 else
918 *p++ = '*';
919 if (o->valtype != '-') {
920 *p++ = ' ';
921 p += strlen(strcpy(p, valtype2param(o)));
922 }
923 *p = ' ';
924 if ((int)(p - start) >= MAX_OPT_HELP_WIDTH) {
925 *p = '\0';
a43ce58f 926 opt_printf_stderr("%s\n", start);
16f8d4eb 927 memset(start, ' ', sizeof(start));
7e1b7485
RS
928 }
929 start[width] = '\0';
a43ce58f 930 opt_printf_stderr("%s %s\n", start, help);
4ed83891
JS
931}
932
933void opt_help(const OPTIONS *list)
934{
935 const OPTIONS *o;
92de469f 936 int i, sawparams = 0, width = 5;
4ed83891 937 int standard_prolog;
4ed83891
JS
938 char start[80 + 1];
939
940 /* Starts with its own help message? */
941 standard_prolog = list[0].name != OPT_HELP_STR;
942
943 /* Find the widest help. */
944 for (o = list; o->name; o++) {
945 if (o->name == OPT_MORE_STR)
946 continue;
947 i = 2 + (int)strlen(o->name);
948 if (o->valtype != '-')
949 i += 1 + strlen(valtype2param(o));
950 if (i < MAX_OPT_HELP_WIDTH && i > width)
951 width = i;
952 OPENSSL_assert(i < (int)sizeof(start));
953 }
954
955 if (standard_prolog) {
956 opt_printf_stderr("Usage: %s [options]\n", prog);
957 if (list[0].name != OPT_SECTION_STR)
958 opt_printf_stderr("Valid options are:\n", prog);
959 }
960
961 /* Now let's print. */
962 for (o = list; o->name; o++) {
92de469f
RS
963 if (o->name == OPT_PARAM_STR)
964 sawparams = 1;
965 opt_print(o, sawparams, width);
7e1b7485
RS
966 }
967}
a43ce58f
SL
968
969/* opt_isdir section */
970#ifdef _WIN32
971# include <windows.h>
972int opt_isdir(const char *name)
973{
974 DWORD attr;
975# if defined(UNICODE) || defined(_UNICODE)
976 size_t i, len_0 = strlen(name) + 1;
977 WCHAR tempname[MAX_PATH];
978
979 if (len_0 > MAX_PATH)
980 return -1;
981
982# if !defined(_WIN32_WCE) || _WIN32_WCE>=101
983 if (!MultiByteToWideChar(CP_ACP, 0, name, len_0, tempname, MAX_PATH))
984# endif
985 for (i = 0; i < len_0; i++)
986 tempname[i] = (WCHAR)name[i];
987
988 attr = GetFileAttributes(tempname);
989# else
990 attr = GetFileAttributes(name);
991# endif
992 if (attr == INVALID_FILE_ATTRIBUTES)
993 return -1;
994 return ((attr & FILE_ATTRIBUTE_DIRECTORY) != 0);
995}
996#else
997# include <sys/stat.h>
998# ifndef S_ISDIR
999# if defined(_S_IFMT) && defined(_S_IFDIR)
1000# define S_ISDIR(a) (((a) & _S_IFMT) == _S_IFDIR)
1001# else
1002# define S_ISDIR(a) (((a) & S_IFMT) == S_IFDIR)
1003# endif
1004# endif
1005
1006int opt_isdir(const char *name)
1007{
1008# if defined(S_ISDIR)
1009 struct stat st;
1010
1011 if (stat(name, &st) == 0)
1012 return S_ISDIR(st.st_mode);
1013 else
1014 return -1;
1015# else
1016 return -1;
1017# endif
1018}
1019#endif