opt.c 25 KB

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