opt.c 24 KB

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