2
0

opt.c 28 KB

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