opt.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898
  1. /*
  2. * Copyright 2015-2018 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++ = tolower((unsigned char)*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. {"nss", OPT_FMT_NSS},
  151. {"text", OPT_FMT_TEXT},
  152. {"http", OPT_FMT_HTTP},
  153. {"pvk", OPT_FMT_PVK},
  154. {NULL}
  155. };
  156. /* Print an error message about a failed format parse. */
  157. int opt_format_error(const char *s, unsigned long flags)
  158. {
  159. OPT_PAIR *ap;
  160. if (flags == OPT_FMT_PEMDER) {
  161. BIO_printf(bio_err, "%s: Bad format \"%s\"; must be pem or der\n",
  162. prog, s);
  163. } else {
  164. BIO_printf(bio_err, "%s: Bad format \"%s\"; must be one of:\n",
  165. prog, s);
  166. for (ap = formats; ap->name; ap++)
  167. if (flags & ap->retval)
  168. BIO_printf(bio_err, " %s\n", ap->name);
  169. }
  170. return 0;
  171. }
  172. /* Parse a format string, put it into *result; return 0 on failure, else 1. */
  173. int opt_format(const char *s, unsigned long flags, int *result)
  174. {
  175. switch (*s) {
  176. default:
  177. return 0;
  178. case 'D':
  179. case 'd':
  180. if ((flags & OPT_FMT_PEMDER) == 0)
  181. return opt_format_error(s, flags);
  182. *result = FORMAT_ASN1;
  183. break;
  184. case 'T':
  185. case 't':
  186. if ((flags & OPT_FMT_TEXT) == 0)
  187. return opt_format_error(s, flags);
  188. *result = FORMAT_TEXT;
  189. break;
  190. case 'N':
  191. case 'n':
  192. if ((flags & OPT_FMT_NSS) == 0)
  193. return opt_format_error(s, flags);
  194. if (strcmp(s, "NSS") != 0 && strcmp(s, "nss") != 0)
  195. return opt_format_error(s, flags);
  196. *result = FORMAT_NSS;
  197. break;
  198. case 'S':
  199. case 's':
  200. if ((flags & OPT_FMT_SMIME) == 0)
  201. return opt_format_error(s, flags);
  202. *result = FORMAT_SMIME;
  203. break;
  204. case 'M':
  205. case 'm':
  206. if ((flags & OPT_FMT_MSBLOB) == 0)
  207. return opt_format_error(s, flags);
  208. *result = FORMAT_MSBLOB;
  209. break;
  210. case 'E':
  211. case 'e':
  212. if ((flags & OPT_FMT_ENGINE) == 0)
  213. return opt_format_error(s, flags);
  214. *result = FORMAT_ENGINE;
  215. break;
  216. case 'H':
  217. case 'h':
  218. if ((flags & OPT_FMT_HTTP) == 0)
  219. return opt_format_error(s, flags);
  220. *result = FORMAT_HTTP;
  221. break;
  222. case '1':
  223. if ((flags & OPT_FMT_PKCS12) == 0)
  224. return opt_format_error(s, flags);
  225. *result = FORMAT_PKCS12;
  226. break;
  227. case 'P':
  228. case 'p':
  229. if (s[1] == '\0' || strcmp(s, "PEM") == 0 || strcmp(s, "pem") == 0) {
  230. if ((flags & OPT_FMT_PEMDER) == 0)
  231. return opt_format_error(s, flags);
  232. *result = FORMAT_PEM;
  233. } else if (strcmp(s, "PVK") == 0 || strcmp(s, "pvk") == 0) {
  234. if ((flags & OPT_FMT_PVK) == 0)
  235. return opt_format_error(s, flags);
  236. *result = FORMAT_PVK;
  237. } else if (strcmp(s, "P12") == 0 || strcmp(s, "p12") == 0
  238. || strcmp(s, "PKCS12") == 0 || strcmp(s, "pkcs12") == 0) {
  239. if ((flags & OPT_FMT_PKCS12) == 0)
  240. return opt_format_error(s, flags);
  241. *result = FORMAT_PKCS12;
  242. } else {
  243. return 0;
  244. }
  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 != NULL)
  254. return 1;
  255. BIO_printf(bio_err, "%s: Unrecognized flag %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 != NULL)
  265. return 1;
  266. BIO_printf(bio_err, "%s: Unrecognized flag %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. static void opt_number_error(const char *v)
  298. {
  299. size_t i = 0;
  300. struct strstr_pair_st {
  301. char *prefix;
  302. char *name;
  303. } b[] = {
  304. {"0x", "a hexadecimal"},
  305. {"0X", "a hexadecimal"},
  306. {"0", "an octal"}
  307. };
  308. for (i = 0; i < OSSL_NELEM(b); i++) {
  309. if (strncmp(v, b[i].prefix, strlen(b[i].prefix)) == 0) {
  310. BIO_printf(bio_err,
  311. "%s: Can't parse \"%s\" as %s number\n",
  312. prog, v, b[i].name);
  313. return;
  314. }
  315. }
  316. BIO_printf(bio_err, "%s: Can't parse \"%s\" as a number\n", prog, v);
  317. return;
  318. }
  319. /* Parse a long, put it into *result; return 0 on failure, else 1. */
  320. int opt_long(const char *value, long *result)
  321. {
  322. int oerrno = errno;
  323. long l;
  324. char *endp;
  325. errno = 0;
  326. l = strtol(value, &endp, 0);
  327. if (*endp
  328. || endp == value
  329. || ((l == LONG_MAX || l == LONG_MIN) && errno == ERANGE)
  330. || (l == 0 && errno != 0)) {
  331. opt_number_error(value);
  332. errno = oerrno;
  333. return 0;
  334. }
  335. *result = l;
  336. errno = oerrno;
  337. return 1;
  338. }
  339. #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L && \
  340. defined(INTMAX_MAX) && defined(UINTMAX_MAX)
  341. /* Parse an intmax_t, put it into *result; return 0 on failure, else 1. */
  342. int opt_imax(const char *value, intmax_t *result)
  343. {
  344. int oerrno = errno;
  345. intmax_t m;
  346. char *endp;
  347. errno = 0;
  348. m = strtoimax(value, &endp, 0);
  349. if (*endp
  350. || endp == value
  351. || ((m == INTMAX_MAX || m == INTMAX_MIN) && errno == ERANGE)
  352. || (m == 0 && errno != 0)) {
  353. opt_number_error(value);
  354. errno = oerrno;
  355. return 0;
  356. }
  357. *result = m;
  358. errno = oerrno;
  359. return 1;
  360. }
  361. /* Parse a uintmax_t, put it into *result; return 0 on failure, else 1. */
  362. int opt_umax(const char *value, uintmax_t *result)
  363. {
  364. int oerrno = errno;
  365. uintmax_t m;
  366. char *endp;
  367. errno = 0;
  368. m = strtoumax(value, &endp, 0);
  369. if (*endp
  370. || endp == value
  371. || (m == UINTMAX_MAX && errno == ERANGE)
  372. || (m == 0 && errno != 0)) {
  373. opt_number_error(value);
  374. errno = oerrno;
  375. return 0;
  376. }
  377. *result = m;
  378. errno = oerrno;
  379. return 1;
  380. }
  381. #endif
  382. /*
  383. * Parse an unsigned long, put it into *result; return 0 on failure, else 1.
  384. */
  385. int opt_ulong(const char *value, unsigned long *result)
  386. {
  387. int oerrno = errno;
  388. char *endptr;
  389. unsigned long l;
  390. errno = 0;
  391. l = strtoul(value, &endptr, 0);
  392. if (*endptr
  393. || endptr == value
  394. || ((l == ULONG_MAX) && errno == ERANGE)
  395. || (l == 0 && errno != 0)) {
  396. opt_number_error(value);
  397. errno = oerrno;
  398. return 0;
  399. }
  400. *result = l;
  401. errno = oerrno;
  402. return 1;
  403. }
  404. /*
  405. * We pass opt as an int but cast it to "enum range" so that all the
  406. * items in the OPT_V_ENUM enumeration are caught; this makes -Wswitch
  407. * in gcc do the right thing.
  408. */
  409. enum range { OPT_V_ENUM };
  410. int opt_verify(int opt, X509_VERIFY_PARAM *vpm)
  411. {
  412. int i;
  413. ossl_intmax_t t = 0;
  414. ASN1_OBJECT *otmp;
  415. X509_PURPOSE *xptmp;
  416. const X509_VERIFY_PARAM *vtmp;
  417. assert(vpm != NULL);
  418. assert(opt > OPT_V__FIRST);
  419. assert(opt < OPT_V__LAST);
  420. switch ((enum range)opt) {
  421. case OPT_V__FIRST:
  422. case OPT_V__LAST:
  423. return 0;
  424. case OPT_V_POLICY:
  425. otmp = OBJ_txt2obj(opt_arg(), 0);
  426. if (otmp == NULL) {
  427. BIO_printf(bio_err, "%s: Invalid Policy %s\n", prog, opt_arg());
  428. return 0;
  429. }
  430. X509_VERIFY_PARAM_add0_policy(vpm, otmp);
  431. break;
  432. case OPT_V_PURPOSE:
  433. /* purpose name -> purpose index */
  434. i = X509_PURPOSE_get_by_sname(opt_arg());
  435. if (i < 0) {
  436. BIO_printf(bio_err, "%s: Invalid purpose %s\n", prog, opt_arg());
  437. return 0;
  438. }
  439. /* purpose index -> purpose object */
  440. xptmp = X509_PURPOSE_get0(i);
  441. /* purpose object -> purpose value */
  442. i = X509_PURPOSE_get_id(xptmp);
  443. if (!X509_VERIFY_PARAM_set_purpose(vpm, i)) {
  444. BIO_printf(bio_err,
  445. "%s: Internal error setting purpose %s\n",
  446. prog, opt_arg());
  447. return 0;
  448. }
  449. break;
  450. case OPT_V_VERIFY_NAME:
  451. vtmp = X509_VERIFY_PARAM_lookup(opt_arg());
  452. if (vtmp == NULL) {
  453. BIO_printf(bio_err, "%s: Invalid verify name %s\n",
  454. prog, opt_arg());
  455. return 0;
  456. }
  457. X509_VERIFY_PARAM_set1(vpm, vtmp);
  458. break;
  459. case OPT_V_VERIFY_DEPTH:
  460. i = atoi(opt_arg());
  461. if (i >= 0)
  462. X509_VERIFY_PARAM_set_depth(vpm, i);
  463. break;
  464. case OPT_V_VERIFY_AUTH_LEVEL:
  465. i = atoi(opt_arg());
  466. if (i >= 0)
  467. X509_VERIFY_PARAM_set_auth_level(vpm, i);
  468. break;
  469. case OPT_V_ATTIME:
  470. if (!opt_imax(opt_arg(), &t))
  471. return 0;
  472. if (t != (time_t)t) {
  473. BIO_printf(bio_err, "%s: epoch time out of range %s\n",
  474. prog, opt_arg());
  475. return 0;
  476. }
  477. X509_VERIFY_PARAM_set_time(vpm, (time_t)t);
  478. break;
  479. case OPT_V_VERIFY_HOSTNAME:
  480. if (!X509_VERIFY_PARAM_set1_host(vpm, opt_arg(), 0))
  481. return 0;
  482. break;
  483. case OPT_V_VERIFY_EMAIL:
  484. if (!X509_VERIFY_PARAM_set1_email(vpm, opt_arg(), 0))
  485. return 0;
  486. break;
  487. case OPT_V_VERIFY_IP:
  488. if (!X509_VERIFY_PARAM_set1_ip_asc(vpm, opt_arg()))
  489. return 0;
  490. break;
  491. case OPT_V_IGNORE_CRITICAL:
  492. X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_IGNORE_CRITICAL);
  493. break;
  494. case OPT_V_ISSUER_CHECKS:
  495. /* NOP, deprecated */
  496. break;
  497. case OPT_V_CRL_CHECK:
  498. X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_CRL_CHECK);
  499. break;
  500. case OPT_V_CRL_CHECK_ALL:
  501. X509_VERIFY_PARAM_set_flags(vpm,
  502. X509_V_FLAG_CRL_CHECK |
  503. X509_V_FLAG_CRL_CHECK_ALL);
  504. break;
  505. case OPT_V_POLICY_CHECK:
  506. X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_POLICY_CHECK);
  507. break;
  508. case OPT_V_EXPLICIT_POLICY:
  509. X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_EXPLICIT_POLICY);
  510. break;
  511. case OPT_V_INHIBIT_ANY:
  512. X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_INHIBIT_ANY);
  513. break;
  514. case OPT_V_INHIBIT_MAP:
  515. X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_INHIBIT_MAP);
  516. break;
  517. case OPT_V_X509_STRICT:
  518. X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_X509_STRICT);
  519. break;
  520. case OPT_V_EXTENDED_CRL:
  521. X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_EXTENDED_CRL_SUPPORT);
  522. break;
  523. case OPT_V_USE_DELTAS:
  524. X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_USE_DELTAS);
  525. break;
  526. case OPT_V_POLICY_PRINT:
  527. X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_NOTIFY_POLICY);
  528. break;
  529. case OPT_V_CHECK_SS_SIG:
  530. X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_CHECK_SS_SIGNATURE);
  531. break;
  532. case OPT_V_TRUSTED_FIRST:
  533. X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_TRUSTED_FIRST);
  534. break;
  535. case OPT_V_SUITEB_128_ONLY:
  536. X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_SUITEB_128_LOS_ONLY);
  537. break;
  538. case OPT_V_SUITEB_128:
  539. X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_SUITEB_128_LOS);
  540. break;
  541. case OPT_V_SUITEB_192:
  542. X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_SUITEB_192_LOS);
  543. break;
  544. case OPT_V_PARTIAL_CHAIN:
  545. X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_PARTIAL_CHAIN);
  546. break;
  547. case OPT_V_NO_ALT_CHAINS:
  548. X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_NO_ALT_CHAINS);
  549. break;
  550. case OPT_V_NO_CHECK_TIME:
  551. X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_NO_CHECK_TIME);
  552. break;
  553. case OPT_V_ALLOW_PROXY_CERTS:
  554. X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_ALLOW_PROXY_CERTS);
  555. break;
  556. }
  557. return 1;
  558. }
  559. /*
  560. * Parse the next flag (and value if specified), return 0 if done, -1 on
  561. * error, otherwise the flag's retval.
  562. */
  563. int opt_next(void)
  564. {
  565. char *p;
  566. const OPTIONS *o;
  567. int ival;
  568. long lval;
  569. unsigned long ulval;
  570. ossl_intmax_t imval;
  571. ossl_uintmax_t umval;
  572. /* Look at current arg; at end of the list? */
  573. arg = NULL;
  574. p = argv[opt_index];
  575. if (p == NULL)
  576. return 0;
  577. /* If word doesn't start with a -, we're done. */
  578. if (*p != '-')
  579. return 0;
  580. /* Hit "--" ? We're done. */
  581. opt_index++;
  582. if (strcmp(p, "--") == 0)
  583. return 0;
  584. /* Allow -nnn and --nnn */
  585. if (*++p == '-')
  586. p++;
  587. flag = p - 1;
  588. /* If we have --flag=foo, snip it off */
  589. if ((arg = strchr(p, '=')) != NULL)
  590. *arg++ = '\0';
  591. for (o = opts; o->name; ++o) {
  592. /* If not this option, move on to the next one. */
  593. if (strcmp(p, o->name) != 0)
  594. continue;
  595. /* If it doesn't take a value, make sure none was given. */
  596. if (o->valtype == 0 || o->valtype == '-') {
  597. if (arg) {
  598. BIO_printf(bio_err,
  599. "%s: Option -%s does not take a value\n", prog, p);
  600. return -1;
  601. }
  602. return o->retval;
  603. }
  604. /* Want a value; get the next param if =foo not used. */
  605. if (arg == NULL) {
  606. if (argv[opt_index] == NULL) {
  607. BIO_printf(bio_err,
  608. "%s: Option -%s needs a value\n", prog, o->name);
  609. return -1;
  610. }
  611. arg = argv[opt_index++];
  612. }
  613. /* Syntax-check value. */
  614. switch (o->valtype) {
  615. default:
  616. case 's':
  617. /* Just a string. */
  618. break;
  619. case '/':
  620. if (app_isdir(arg) > 0)
  621. break;
  622. BIO_printf(bio_err, "%s: Not a directory: %s\n", prog, arg);
  623. return -1;
  624. case '<':
  625. /* Input file. */
  626. break;
  627. case '>':
  628. /* Output file. */
  629. break;
  630. case 'p':
  631. case 'n':
  632. if (!opt_int(arg, &ival)
  633. || (o->valtype == 'p' && ival <= 0)) {
  634. BIO_printf(bio_err,
  635. "%s: Non-positive number \"%s\" for -%s\n",
  636. prog, arg, o->name);
  637. return -1;
  638. }
  639. break;
  640. case 'M':
  641. if (!opt_imax(arg, &imval)) {
  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 'U':
  649. if (!opt_umax(arg, &umval)) {
  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 'l':
  657. if (!opt_long(arg, &lval)) {
  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 'u':
  665. if (!opt_ulong(arg, &ulval)) {
  666. BIO_printf(bio_err,
  667. "%s: Invalid number \"%s\" for -%s\n",
  668. prog, arg, o->name);
  669. return -1;
  670. }
  671. break;
  672. case 'c':
  673. case 'E':
  674. case 'F':
  675. case 'f':
  676. if (opt_format(arg,
  677. o->valtype == 'c' ? OPT_FMT_PDS :
  678. o->valtype == 'E' ? OPT_FMT_PDE :
  679. o->valtype == 'F' ? OPT_FMT_PEMDER
  680. : OPT_FMT_ANY, &ival))
  681. break;
  682. BIO_printf(bio_err,
  683. "%s: Invalid format \"%s\" for -%s\n",
  684. prog, arg, o->name);
  685. return -1;
  686. }
  687. /* Return the flag value. */
  688. return o->retval;
  689. }
  690. if (unknown != NULL) {
  691. dunno = p;
  692. return unknown->retval;
  693. }
  694. BIO_printf(bio_err, "%s: Option unknown option -%s\n", prog, p);
  695. return -1;
  696. }
  697. /* Return the most recent flag parameter. */
  698. char *opt_arg(void)
  699. {
  700. return arg;
  701. }
  702. /* Return the most recent flag. */
  703. char *opt_flag(void)
  704. {
  705. return flag;
  706. }
  707. /* Return the unknown option. */
  708. char *opt_unknown(void)
  709. {
  710. return dunno;
  711. }
  712. /* Return the rest of the arguments after parsing flags. */
  713. char **opt_rest(void)
  714. {
  715. return &argv[opt_index];
  716. }
  717. /* How many items in remaining args? */
  718. int opt_num_rest(void)
  719. {
  720. int i = 0;
  721. char **pp;
  722. for (pp = opt_rest(); *pp; pp++, i++)
  723. continue;
  724. return i;
  725. }
  726. /* Return a string describing the parameter type. */
  727. static const char *valtype2param(const OPTIONS *o)
  728. {
  729. switch (o->valtype) {
  730. case 0:
  731. case '-':
  732. return "";
  733. case 's':
  734. return "val";
  735. case '/':
  736. return "dir";
  737. case '<':
  738. return "infile";
  739. case '>':
  740. return "outfile";
  741. case 'p':
  742. return "+int";
  743. case 'n':
  744. return "int";
  745. case 'l':
  746. return "long";
  747. case 'u':
  748. return "ulong";
  749. case 'E':
  750. return "PEM|DER|ENGINE";
  751. case 'F':
  752. return "PEM|DER";
  753. case 'f':
  754. return "format";
  755. case 'M':
  756. return "intmax";
  757. case 'U':
  758. return "uintmax";
  759. }
  760. return "parm";
  761. }
  762. void opt_help(const OPTIONS *list)
  763. {
  764. const OPTIONS *o;
  765. int i;
  766. int standard_prolog;
  767. int width = 5;
  768. char start[80 + 1];
  769. char *p;
  770. const char *help;
  771. /* Starts with its own help message? */
  772. standard_prolog = list[0].name != OPT_HELP_STR;
  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. if (standard_prolog)
  785. BIO_printf(bio_err, "Usage: %s [options]\nValid options are:\n",
  786. prog);
  787. /* Now let's print. */
  788. for (o = list; o->name; o++) {
  789. help = o->helpstr ? o->helpstr : "(No additional info)";
  790. if (o->name == OPT_HELP_STR) {
  791. BIO_printf(bio_err, help, prog);
  792. continue;
  793. }
  794. /* Pad out prefix */
  795. memset(start, ' ', sizeof(start) - 1);
  796. start[sizeof(start) - 1] = '\0';
  797. if (o->name == OPT_MORE_STR) {
  798. /* Continuation of previous line; pad and print. */
  799. start[width] = '\0';
  800. BIO_printf(bio_err, "%s %s\n", start, help);
  801. continue;
  802. }
  803. /* Build up the "-flag [param]" part. */
  804. p = start;
  805. *p++ = ' ';
  806. *p++ = '-';
  807. if (o->name[0])
  808. p += strlen(strcpy(p, o->name));
  809. else
  810. *p++ = '*';
  811. if (o->valtype != '-') {
  812. *p++ = ' ';
  813. p += strlen(strcpy(p, valtype2param(o)));
  814. }
  815. *p = ' ';
  816. if ((int)(p - start) >= MAX_OPT_HELP_WIDTH) {
  817. *p = '\0';
  818. BIO_printf(bio_err, "%s\n", start);
  819. memset(start, ' ', sizeof(start));
  820. }
  821. start[width] = '\0';
  822. BIO_printf(bio_err, "%s %s\n", start, help);
  823. }
  824. }