opt.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257
  1. /*
  2. * Copyright 2015-2022 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 "app_libctx.h"
  15. #include "internal/nelem.h"
  16. #include "internal/numbers.h"
  17. #include <string.h>
  18. #if !defined(OPENSSL_SYS_MSDOS)
  19. # include <unistd.h>
  20. #endif
  21. #include <stdlib.h>
  22. #include <errno.h>
  23. #include <ctype.h>
  24. #include <limits.h>
  25. #include <openssl/err.h>
  26. #include <openssl/bio.h>
  27. #include <openssl/x509v3.h>
  28. #define MAX_OPT_HELP_WIDTH 30
  29. const char OPT_HELP_STR[] = "-H";
  30. const char OPT_MORE_STR[] = "-M";
  31. const char OPT_SECTION_STR[] = "-S";
  32. const char OPT_PARAM_STR[] = "-P";
  33. /* Our state */
  34. static char **argv;
  35. static int argc;
  36. static int opt_index;
  37. static char *arg;
  38. static char *flag;
  39. static char *dunno;
  40. static const char *unknown_name;
  41. static const OPTIONS *unknown;
  42. static const OPTIONS *opts;
  43. static char prog[40];
  44. /*
  45. * Return the simple name of the program; removing various platform gunk.
  46. */
  47. #if defined(OPENSSL_SYS_WIN32)
  48. const char *opt_path_end(const char *filename)
  49. {
  50. const char *p;
  51. /* find the last '/', '\' or ':' */
  52. for (p = filename + strlen(filename); --p > filename; )
  53. if (*p == '/' || *p == '\\' || *p == ':') {
  54. p++;
  55. break;
  56. }
  57. return p;
  58. }
  59. char *opt_progname(const char *argv0)
  60. {
  61. size_t i, n;
  62. const char *p;
  63. char *q;
  64. p = opt_path_end(argv0);
  65. /* Strip off trailing nonsense. */
  66. n = strlen(p);
  67. if (n > 4 &&
  68. (strcmp(&p[n - 4], ".exe") == 0 || strcmp(&p[n - 4], ".EXE") == 0))
  69. n -= 4;
  70. /* Copy over the name, in lowercase. */
  71. if (n > sizeof(prog) - 1)
  72. n = sizeof(prog) - 1;
  73. for (q = prog, i = 0; i < n; i++, p++)
  74. *q++ = tolower((unsigned char)*p);
  75. *q = '\0';
  76. return prog;
  77. }
  78. #elif defined(OPENSSL_SYS_VMS)
  79. const char *opt_path_end(const char *filename)
  80. {
  81. const char *p;
  82. /* Find last special character sys:[foo.bar]openssl */
  83. for (p = filename + strlen(filename); --p > filename;)
  84. if (*p == ':' || *p == ']' || *p == '>') {
  85. p++;
  86. break;
  87. }
  88. return p;
  89. }
  90. char *opt_progname(const char *argv0)
  91. {
  92. const char *p, *q;
  93. /* Find last special character sys:[foo.bar]openssl */
  94. p = opt_path_end(argv0);
  95. q = strrchr(p, '.');
  96. if (prog != p)
  97. strncpy(prog, p, sizeof(prog) - 1);
  98. prog[sizeof(prog) - 1] = '\0';
  99. if (q != NULL && q - p < sizeof(prog))
  100. prog[q - p] = '\0';
  101. return prog;
  102. }
  103. #else
  104. const char *opt_path_end(const char *filename)
  105. {
  106. const char *p;
  107. /* Could use strchr, but this is like the ones above. */
  108. for (p = filename + strlen(filename); --p > filename;)
  109. if (*p == '/') {
  110. p++;
  111. break;
  112. }
  113. return p;
  114. }
  115. char *opt_progname(const char *argv0)
  116. {
  117. const char *p;
  118. p = opt_path_end(argv0);
  119. if (prog != p)
  120. strncpy(prog, p, sizeof(prog) - 1);
  121. prog[sizeof(prog) - 1] = '\0';
  122. return prog;
  123. }
  124. #endif
  125. char *opt_appname(const char *argv0)
  126. {
  127. size_t len = strlen(prog);
  128. if (argv0 != NULL)
  129. BIO_snprintf(prog + len, sizeof(prog) - len - 1, " %s", argv0);
  130. return prog;
  131. }
  132. char *opt_getprog(void)
  133. {
  134. return prog;
  135. }
  136. /* Set up the arg parsing. */
  137. char *opt_init(int ac, char **av, const OPTIONS *o)
  138. {
  139. /* Store state. */
  140. argc = ac;
  141. argv = av;
  142. opt_begin();
  143. opts = o;
  144. unknown = NULL;
  145. /* Make sure prog name is set for usage output */
  146. (void)opt_progname(argv[0]);
  147. /* Check all options up until the PARAM marker (if present) */
  148. for (; o->name != NULL && o->name != OPT_PARAM_STR; ++o) {
  149. #ifndef NDEBUG
  150. const OPTIONS *next;
  151. int duplicated, i;
  152. #endif
  153. if (o->name == OPT_HELP_STR
  154. || o->name == OPT_MORE_STR
  155. || o->name == OPT_SECTION_STR)
  156. continue;
  157. #ifndef NDEBUG
  158. i = o->valtype;
  159. /* Make sure options are legit. */
  160. OPENSSL_assert(o->name[0] != '-');
  161. if (o->valtype == '.')
  162. OPENSSL_assert(o->retval == OPT_PARAM);
  163. else
  164. OPENSSL_assert(o->retval == OPT_DUP || o->retval > OPT_PARAM);
  165. switch (i) {
  166. case 0: case '-': case '.':
  167. case '/': case '<': case '>': case 'E': case 'F':
  168. case 'M': case 'U': case 'f': case 'l': case 'n': case 'p': case 's':
  169. case 'u': case 'c': case ':': case 'N':
  170. break;
  171. default:
  172. OPENSSL_assert(0);
  173. }
  174. /* Make sure there are no duplicates. */
  175. for (next = o + 1; next->name; ++next) {
  176. /*
  177. * Some compilers inline strcmp and the assert string is too long.
  178. */
  179. duplicated = next->retval != OPT_DUP
  180. && strcmp(o->name, next->name) == 0;
  181. if (duplicated) {
  182. opt_printf_stderr("%s: Internal error: duplicate option %s\n",
  183. prog, o->name);
  184. OPENSSL_assert(!duplicated);
  185. }
  186. }
  187. #endif
  188. if (o->name[0] == '\0') {
  189. OPENSSL_assert(unknown_name != NULL);
  190. OPENSSL_assert(unknown == NULL);
  191. unknown = o;
  192. OPENSSL_assert(unknown->valtype == 0 || unknown->valtype == '-');
  193. }
  194. }
  195. return prog;
  196. }
  197. static OPT_PAIR formats[] = {
  198. {"PEM/DER", OPT_FMT_PEMDER},
  199. {"pkcs12", OPT_FMT_PKCS12},
  200. {"smime", OPT_FMT_SMIME},
  201. {"engine", OPT_FMT_ENGINE},
  202. {"msblob", OPT_FMT_MSBLOB},
  203. {"nss", OPT_FMT_NSS},
  204. {"text", OPT_FMT_TEXT},
  205. {"http", OPT_FMT_HTTP},
  206. {"pvk", OPT_FMT_PVK},
  207. {NULL}
  208. };
  209. void opt_set_unknown_name(const char *name)
  210. {
  211. unknown_name = name;
  212. }
  213. /* Print an error message about a failed format parse. */
  214. static int opt_format_error(const char *s, unsigned long flags)
  215. {
  216. OPT_PAIR *ap;
  217. if (flags == OPT_FMT_PEMDER) {
  218. opt_printf_stderr("%s: Bad format \"%s\"; must be pem or der\n",
  219. prog, s);
  220. } else {
  221. opt_printf_stderr("%s: Bad format \"%s\"; must be one of:\n",
  222. prog, s);
  223. for (ap = formats; ap->name; ap++)
  224. if (flags & ap->retval)
  225. opt_printf_stderr(" %s\n", ap->name);
  226. }
  227. return 0;
  228. }
  229. /* Parse a format string, put it into *result; return 0 on failure, else 1. */
  230. int opt_format(const char *s, unsigned long flags, int *result)
  231. {
  232. switch (*s) {
  233. default:
  234. opt_printf_stderr("%s: Bad format \"%s\"\n", prog, s);
  235. return 0;
  236. case 'D':
  237. case 'd':
  238. if ((flags & OPT_FMT_PEMDER) == 0)
  239. return opt_format_error(s, flags);
  240. *result = FORMAT_ASN1;
  241. break;
  242. case 'T':
  243. case 't':
  244. if ((flags & OPT_FMT_TEXT) == 0)
  245. return opt_format_error(s, flags);
  246. *result = FORMAT_TEXT;
  247. break;
  248. case 'N':
  249. case 'n':
  250. if ((flags & OPT_FMT_NSS) == 0)
  251. return opt_format_error(s, flags);
  252. if (strcmp(s, "NSS") != 0 && strcmp(s, "nss") != 0)
  253. return opt_format_error(s, flags);
  254. *result = FORMAT_NSS;
  255. break;
  256. case 'S':
  257. case 's':
  258. if ((flags & OPT_FMT_SMIME) == 0)
  259. return opt_format_error(s, flags);
  260. *result = FORMAT_SMIME;
  261. break;
  262. case 'M':
  263. case 'm':
  264. if ((flags & OPT_FMT_MSBLOB) == 0)
  265. return opt_format_error(s, flags);
  266. *result = FORMAT_MSBLOB;
  267. break;
  268. case 'E':
  269. case 'e':
  270. if ((flags & OPT_FMT_ENGINE) == 0)
  271. return opt_format_error(s, flags);
  272. *result = FORMAT_ENGINE;
  273. break;
  274. case 'H':
  275. case 'h':
  276. if ((flags & OPT_FMT_HTTP) == 0)
  277. return opt_format_error(s, flags);
  278. *result = FORMAT_HTTP;
  279. break;
  280. case '1':
  281. if ((flags & OPT_FMT_PKCS12) == 0)
  282. return opt_format_error(s, flags);
  283. *result = FORMAT_PKCS12;
  284. break;
  285. case 'P':
  286. case 'p':
  287. if (s[1] == '\0' || strcmp(s, "PEM") == 0 || strcmp(s, "pem") == 0) {
  288. if ((flags & OPT_FMT_PEMDER) == 0)
  289. return opt_format_error(s, flags);
  290. *result = FORMAT_PEM;
  291. } else if (strcmp(s, "PVK") == 0 || strcmp(s, "pvk") == 0) {
  292. if ((flags & OPT_FMT_PVK) == 0)
  293. return opt_format_error(s, flags);
  294. *result = FORMAT_PVK;
  295. } else if (strcmp(s, "P12") == 0 || strcmp(s, "p12") == 0
  296. || strcmp(s, "PKCS12") == 0 || strcmp(s, "pkcs12") == 0) {
  297. if ((flags & OPT_FMT_PKCS12) == 0)
  298. return opt_format_error(s, flags);
  299. *result = FORMAT_PKCS12;
  300. } else {
  301. opt_printf_stderr("%s: Bad format \"%s\"\n", prog, s);
  302. return 0;
  303. }
  304. break;
  305. }
  306. return 1;
  307. }
  308. /* Return string representing the given format. */
  309. static const char *format2str(int format)
  310. {
  311. switch (format) {
  312. default:
  313. return "(undefined)";
  314. case FORMAT_PEM:
  315. return "PEM";
  316. case FORMAT_ASN1:
  317. return "DER";
  318. case FORMAT_TEXT:
  319. return "TEXT";
  320. case FORMAT_NSS:
  321. return "NSS";
  322. case FORMAT_SMIME:
  323. return "SMIME";
  324. case FORMAT_MSBLOB:
  325. return "MSBLOB";
  326. case FORMAT_ENGINE:
  327. return "ENGINE";
  328. case FORMAT_HTTP:
  329. return "HTTP";
  330. case FORMAT_PKCS12:
  331. return "P12";
  332. case FORMAT_PVK:
  333. return "PVK";
  334. }
  335. }
  336. /* Print an error message about unsuitable/unsupported format requested. */
  337. void print_format_error(int format, unsigned long flags)
  338. {
  339. (void)opt_format_error(format2str(format), flags);
  340. }
  341. /*
  342. * Parse a cipher name, put it in *cipherp after freeing what was there, if
  343. * cipherp is not NULL. Return 0 on failure, else 1.
  344. */
  345. int opt_cipher_silent(const char *name, EVP_CIPHER **cipherp)
  346. {
  347. EVP_CIPHER *c;
  348. ERR_set_mark();
  349. if ((c = EVP_CIPHER_fetch(app_get0_libctx(), name,
  350. app_get0_propq())) != NULL
  351. || (opt_legacy_okay()
  352. && (c = (EVP_CIPHER *)EVP_get_cipherbyname(name)) != NULL)) {
  353. ERR_pop_to_mark();
  354. if (cipherp != NULL) {
  355. EVP_CIPHER_free(*cipherp);
  356. *cipherp = c;
  357. } else {
  358. EVP_CIPHER_free(c);
  359. }
  360. return 1;
  361. }
  362. ERR_clear_last_mark();
  363. return 0;
  364. }
  365. int opt_cipher_any(const char *name, EVP_CIPHER **cipherp)
  366. {
  367. int ret;
  368. if (name == NULL)
  369. return 1;
  370. if ((ret = opt_cipher_silent(name, cipherp)) == 0)
  371. opt_printf_stderr("%s: Unknown option or cipher: %s\n", prog, name);
  372. return ret;
  373. }
  374. int opt_cipher(const char *name, EVP_CIPHER **cipherp)
  375. {
  376. int mode, ret = 0;
  377. unsigned long int flags;
  378. EVP_CIPHER *c = NULL;
  379. if (name == NULL)
  380. return 1;
  381. if (opt_cipher_any(name, &c)) {
  382. mode = EVP_CIPHER_get_mode(c);
  383. flags = EVP_CIPHER_get_flags(c);
  384. if (mode == EVP_CIPH_XTS_MODE) {
  385. opt_printf_stderr("%s XTS ciphers not supported\n", prog);
  386. } else if ((flags & EVP_CIPH_FLAG_AEAD_CIPHER) != 0) {
  387. opt_printf_stderr("%s: AEAD ciphers not supported\n", prog);
  388. } else {
  389. ret = 1;
  390. if (cipherp != NULL)
  391. *cipherp = c;
  392. }
  393. }
  394. return ret;
  395. }
  396. /*
  397. * Parse message digest name, put it in *EVP_MD; return 0 on failure, else 1.
  398. */
  399. int opt_md_silent(const char *name, EVP_MD **mdp)
  400. {
  401. EVP_MD *md;
  402. ERR_set_mark();
  403. if ((md = EVP_MD_fetch(app_get0_libctx(), name, app_get0_propq())) != NULL
  404. || (opt_legacy_okay()
  405. && (md = (EVP_MD *)EVP_get_digestbyname(name)) != NULL)) {
  406. ERR_pop_to_mark();
  407. if (mdp != NULL) {
  408. EVP_MD_free(*mdp);
  409. *mdp = md;
  410. } else {
  411. EVP_MD_free(md);
  412. }
  413. return 1;
  414. }
  415. ERR_clear_last_mark();
  416. return 0;
  417. }
  418. int opt_md(const char *name, EVP_MD **mdp)
  419. {
  420. int ret;
  421. if (name == NULL)
  422. return 1;
  423. if ((ret = opt_md_silent(name, mdp)) == 0)
  424. opt_printf_stderr("%s: Unknown option or message digest: %s\n",
  425. prog, name);
  426. return ret;
  427. }
  428. int opt_check_md(const char *name)
  429. {
  430. if (opt_md(name, NULL))
  431. return 1;
  432. ERR_clear_error();
  433. return 0;
  434. }
  435. /* Look through a list of name/value pairs. */
  436. int opt_pair(const char *name, const OPT_PAIR* pairs, int *result)
  437. {
  438. const OPT_PAIR *pp;
  439. for (pp = pairs; pp->name; pp++)
  440. if (strcmp(pp->name, name) == 0) {
  441. *result = pp->retval;
  442. return 1;
  443. }
  444. opt_printf_stderr("%s: Value must be one of:\n", prog);
  445. for (pp = pairs; pp->name; pp++)
  446. opt_printf_stderr("\t%s\n", pp->name);
  447. return 0;
  448. }
  449. /* Look through a list of valid names */
  450. int opt_string(const char *name, const char **options)
  451. {
  452. const char **p;
  453. for (p = options; *p != NULL; p++)
  454. if (strcmp(*p, name) == 0)
  455. return 1;
  456. opt_printf_stderr("%s: Value must be one of:\n", prog);
  457. for (p = options; *p != NULL; p++)
  458. opt_printf_stderr("\t%s\n", *p);
  459. return 0;
  460. }
  461. /* Parse an int, put it into *result; return 0 on failure, else 1. */
  462. int opt_int(const char *value, int *result)
  463. {
  464. long l;
  465. if (!opt_long(value, &l))
  466. return 0;
  467. *result = (int)l;
  468. if (*result != l) {
  469. opt_printf_stderr("%s: Value \"%s\" outside integer range\n",
  470. prog, value);
  471. return 0;
  472. }
  473. return 1;
  474. }
  475. /* Parse and return an integer, assuming range has been checked before. */
  476. int opt_int_arg(void)
  477. {
  478. int result = -1;
  479. (void)opt_int(arg, &result);
  480. return result;
  481. }
  482. static void opt_number_error(const char *v)
  483. {
  484. size_t i = 0;
  485. struct strstr_pair_st {
  486. char *prefix;
  487. char *name;
  488. } b[] = {
  489. {"0x", "a hexadecimal"},
  490. {"0X", "a hexadecimal"},
  491. {"0", "an octal"}
  492. };
  493. for (i = 0; i < OSSL_NELEM(b); i++) {
  494. if (strncmp(v, b[i].prefix, strlen(b[i].prefix)) == 0) {
  495. opt_printf_stderr("%s: Can't parse \"%s\" as %s number\n",
  496. prog, v, b[i].name);
  497. return;
  498. }
  499. }
  500. opt_printf_stderr("%s: Can't parse \"%s\" as a number\n", prog, v);
  501. return;
  502. }
  503. /* Parse a long, put it into *result; return 0 on failure, else 1. */
  504. int opt_long(const char *value, long *result)
  505. {
  506. int oerrno = errno;
  507. long l;
  508. char *endp;
  509. errno = 0;
  510. l = strtol(value, &endp, 0);
  511. if (*endp
  512. || endp == value
  513. || ((l == LONG_MAX || l == LONG_MIN) && errno == ERANGE)
  514. || (l == 0 && errno != 0)) {
  515. opt_number_error(value);
  516. errno = oerrno;
  517. return 0;
  518. }
  519. *result = l;
  520. errno = oerrno;
  521. return 1;
  522. }
  523. #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L && \
  524. defined(INTMAX_MAX) && defined(UINTMAX_MAX) && \
  525. !defined(OPENSSL_NO_INTTYPES_H)
  526. /* Parse an intmax_t, put it into *result; return 0 on failure, else 1. */
  527. int opt_intmax(const char *value, ossl_intmax_t *result)
  528. {
  529. int oerrno = errno;
  530. intmax_t m;
  531. char *endp;
  532. errno = 0;
  533. m = strtoimax(value, &endp, 0);
  534. if (*endp
  535. || endp == value
  536. || ((m == INTMAX_MAX || m == INTMAX_MIN)
  537. && errno == ERANGE)
  538. || (m == 0 && errno != 0)) {
  539. opt_number_error(value);
  540. errno = oerrno;
  541. return 0;
  542. }
  543. /* Ensure that the value in |m| is never too big for |*result| */
  544. if (sizeof(m) > sizeof(*result)
  545. && (m < OSSL_INTMAX_MIN || m > OSSL_INTMAX_MAX)) {
  546. opt_number_error(value);
  547. return 0;
  548. }
  549. *result = (ossl_intmax_t)m;
  550. errno = oerrno;
  551. return 1;
  552. }
  553. /* Parse a uintmax_t, put it into *result; return 0 on failure, else 1. */
  554. int opt_uintmax(const char *value, ossl_uintmax_t *result)
  555. {
  556. int oerrno = errno;
  557. uintmax_t m;
  558. char *endp;
  559. errno = 0;
  560. m = strtoumax(value, &endp, 0);
  561. if (*endp
  562. || endp == value
  563. || (m == UINTMAX_MAX && errno == ERANGE)
  564. || (m == 0 && errno != 0)) {
  565. opt_number_error(value);
  566. errno = oerrno;
  567. return 0;
  568. }
  569. /* Ensure that the value in |m| is never too big for |*result| */
  570. if (sizeof(m) > sizeof(*result)
  571. && m > OSSL_UINTMAX_MAX) {
  572. opt_number_error(value);
  573. return 0;
  574. }
  575. *result = (ossl_intmax_t)m;
  576. errno = oerrno;
  577. return 1;
  578. }
  579. #else
  580. /* Fallback implementations based on long */
  581. int opt_intmax(const char *value, ossl_intmax_t *result)
  582. {
  583. long m;
  584. int ret;
  585. if ((ret = opt_long(value, &m)))
  586. *result = m;
  587. return ret;
  588. }
  589. int opt_uintmax(const char *value, ossl_uintmax_t *result)
  590. {
  591. unsigned long m;
  592. int ret;
  593. if ((ret = opt_ulong(value, &m)))
  594. *result = m;
  595. return ret;
  596. }
  597. #endif
  598. /*
  599. * Parse an unsigned long, put it into *result; return 0 on failure, else 1.
  600. */
  601. int opt_ulong(const char *value, unsigned long *result)
  602. {
  603. int oerrno = errno;
  604. char *endptr;
  605. unsigned long l;
  606. errno = 0;
  607. l = strtoul(value, &endptr, 0);
  608. if (*endptr
  609. || endptr == value
  610. || ((l == ULONG_MAX) && errno == ERANGE)
  611. || (l == 0 && errno != 0)) {
  612. opt_number_error(value);
  613. errno = oerrno;
  614. return 0;
  615. }
  616. *result = l;
  617. errno = oerrno;
  618. return 1;
  619. }
  620. /*
  621. * We pass opt as an int but cast it to "enum range" so that all the
  622. * items in the OPT_V_ENUM enumeration are caught; this makes -Wswitch
  623. * in gcc do the right thing.
  624. */
  625. enum range { OPT_V_ENUM };
  626. int opt_verify(int opt, X509_VERIFY_PARAM *vpm)
  627. {
  628. int i;
  629. ossl_intmax_t t = 0;
  630. ASN1_OBJECT *otmp;
  631. X509_PURPOSE *xptmp;
  632. const X509_VERIFY_PARAM *vtmp;
  633. OPENSSL_assert(vpm != NULL);
  634. OPENSSL_assert(opt > OPT_V__FIRST);
  635. OPENSSL_assert(opt < OPT_V__LAST);
  636. switch ((enum range)opt) {
  637. case OPT_V__FIRST:
  638. case OPT_V__LAST:
  639. return 0;
  640. case OPT_V_POLICY:
  641. otmp = OBJ_txt2obj(opt_arg(), 0);
  642. if (otmp == NULL) {
  643. opt_printf_stderr("%s: Invalid Policy %s\n", prog, opt_arg());
  644. return 0;
  645. }
  646. X509_VERIFY_PARAM_add0_policy(vpm, otmp);
  647. break;
  648. case OPT_V_PURPOSE:
  649. /* purpose name -> purpose index */
  650. i = X509_PURPOSE_get_by_sname(opt_arg());
  651. if (i < 0) {
  652. opt_printf_stderr("%s: Invalid purpose %s\n", prog, opt_arg());
  653. return 0;
  654. }
  655. /* purpose index -> purpose object */
  656. xptmp = X509_PURPOSE_get0(i);
  657. /* purpose object -> purpose value */
  658. i = X509_PURPOSE_get_id(xptmp);
  659. if (!X509_VERIFY_PARAM_set_purpose(vpm, i)) {
  660. opt_printf_stderr("%s: Internal error setting purpose %s\n",
  661. prog, opt_arg());
  662. return 0;
  663. }
  664. break;
  665. case OPT_V_VERIFY_NAME:
  666. vtmp = X509_VERIFY_PARAM_lookup(opt_arg());
  667. if (vtmp == NULL) {
  668. opt_printf_stderr("%s: Invalid verify name %s\n",
  669. prog, opt_arg());
  670. return 0;
  671. }
  672. X509_VERIFY_PARAM_set1(vpm, vtmp);
  673. break;
  674. case OPT_V_VERIFY_DEPTH:
  675. i = atoi(opt_arg());
  676. if (i >= 0)
  677. X509_VERIFY_PARAM_set_depth(vpm, i);
  678. break;
  679. case OPT_V_VERIFY_AUTH_LEVEL:
  680. i = atoi(opt_arg());
  681. if (i >= 0)
  682. X509_VERIFY_PARAM_set_auth_level(vpm, i);
  683. break;
  684. case OPT_V_ATTIME:
  685. if (!opt_intmax(opt_arg(), &t))
  686. return 0;
  687. if (t != (time_t)t) {
  688. opt_printf_stderr("%s: epoch time out of range %s\n",
  689. prog, opt_arg());
  690. return 0;
  691. }
  692. X509_VERIFY_PARAM_set_time(vpm, (time_t)t);
  693. break;
  694. case OPT_V_VERIFY_HOSTNAME:
  695. if (!X509_VERIFY_PARAM_set1_host(vpm, opt_arg(), 0))
  696. return 0;
  697. break;
  698. case OPT_V_VERIFY_EMAIL:
  699. if (!X509_VERIFY_PARAM_set1_email(vpm, opt_arg(), 0))
  700. return 0;
  701. break;
  702. case OPT_V_VERIFY_IP:
  703. if (!X509_VERIFY_PARAM_set1_ip_asc(vpm, opt_arg()))
  704. return 0;
  705. break;
  706. case OPT_V_IGNORE_CRITICAL:
  707. X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_IGNORE_CRITICAL);
  708. break;
  709. case OPT_V_ISSUER_CHECKS:
  710. /* NOP, deprecated */
  711. break;
  712. case OPT_V_CRL_CHECK:
  713. X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_CRL_CHECK);
  714. break;
  715. case OPT_V_CRL_CHECK_ALL:
  716. X509_VERIFY_PARAM_set_flags(vpm,
  717. X509_V_FLAG_CRL_CHECK |
  718. X509_V_FLAG_CRL_CHECK_ALL);
  719. break;
  720. case OPT_V_POLICY_CHECK:
  721. X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_POLICY_CHECK);
  722. break;
  723. case OPT_V_EXPLICIT_POLICY:
  724. X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_EXPLICIT_POLICY);
  725. break;
  726. case OPT_V_INHIBIT_ANY:
  727. X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_INHIBIT_ANY);
  728. break;
  729. case OPT_V_INHIBIT_MAP:
  730. X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_INHIBIT_MAP);
  731. break;
  732. case OPT_V_X509_STRICT:
  733. X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_X509_STRICT);
  734. break;
  735. case OPT_V_EXTENDED_CRL:
  736. X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_EXTENDED_CRL_SUPPORT);
  737. break;
  738. case OPT_V_USE_DELTAS:
  739. X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_USE_DELTAS);
  740. break;
  741. case OPT_V_POLICY_PRINT:
  742. X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_NOTIFY_POLICY);
  743. break;
  744. case OPT_V_CHECK_SS_SIG:
  745. X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_CHECK_SS_SIGNATURE);
  746. break;
  747. case OPT_V_TRUSTED_FIRST:
  748. X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_TRUSTED_FIRST);
  749. break;
  750. case OPT_V_SUITEB_128_ONLY:
  751. X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_SUITEB_128_LOS_ONLY);
  752. break;
  753. case OPT_V_SUITEB_128:
  754. X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_SUITEB_128_LOS);
  755. break;
  756. case OPT_V_SUITEB_192:
  757. X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_SUITEB_192_LOS);
  758. break;
  759. case OPT_V_PARTIAL_CHAIN:
  760. X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_PARTIAL_CHAIN);
  761. break;
  762. case OPT_V_NO_ALT_CHAINS:
  763. X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_NO_ALT_CHAINS);
  764. break;
  765. case OPT_V_NO_CHECK_TIME:
  766. X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_NO_CHECK_TIME);
  767. break;
  768. case OPT_V_ALLOW_PROXY_CERTS:
  769. X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_ALLOW_PROXY_CERTS);
  770. break;
  771. }
  772. return 1;
  773. }
  774. void opt_begin(void)
  775. {
  776. opt_index = 1;
  777. arg = NULL;
  778. flag = NULL;
  779. }
  780. /*
  781. * Parse the next flag (and value if specified), return 0 if done, -1 on
  782. * error, otherwise the flag's retval.
  783. */
  784. int opt_next(void)
  785. {
  786. char *p;
  787. const OPTIONS *o;
  788. int ival;
  789. long lval;
  790. unsigned long ulval;
  791. ossl_intmax_t imval;
  792. ossl_uintmax_t umval;
  793. /* Look at current arg; at end of the list? */
  794. arg = NULL;
  795. p = argv[opt_index];
  796. if (p == NULL)
  797. return 0;
  798. /* If word doesn't start with a -, we're done. */
  799. if (*p != '-')
  800. return 0;
  801. /* Hit "--" ? We're done. */
  802. opt_index++;
  803. if (strcmp(p, "--") == 0)
  804. return 0;
  805. /* Allow -nnn and --nnn */
  806. if (*++p == '-')
  807. p++;
  808. flag = p - 1;
  809. /* If we have --flag=foo, snip it off */
  810. if ((arg = strchr(p, '=')) != NULL)
  811. *arg++ = '\0';
  812. for (o = opts; o->name; ++o) {
  813. /* If not this option, move on to the next one. */
  814. if (!(strcmp(p, "h") == 0 && strcmp(o->name, "help") == 0)
  815. && strcmp(p, o->name) != 0)
  816. continue;
  817. /* If it doesn't take a value, make sure none was given. */
  818. if (o->valtype == 0 || o->valtype == '-') {
  819. if (arg) {
  820. opt_printf_stderr("%s: Option -%s does not take a value\n",
  821. prog, p);
  822. return -1;
  823. }
  824. return o->retval;
  825. }
  826. /* Want a value; get the next param if =foo not used. */
  827. if (arg == NULL) {
  828. if (argv[opt_index] == NULL) {
  829. opt_printf_stderr("%s: Option -%s needs a value\n",
  830. prog, o->name);
  831. return -1;
  832. }
  833. arg = argv[opt_index++];
  834. }
  835. /* Syntax-check value. */
  836. switch (o->valtype) {
  837. default:
  838. case 's':
  839. case ':':
  840. /* Just a string. */
  841. break;
  842. case '.':
  843. /* Parameters */
  844. break;
  845. case '/':
  846. if (opt_isdir(arg) > 0)
  847. break;
  848. opt_printf_stderr("%s: Not a directory: %s\n", prog, arg);
  849. return -1;
  850. case '<':
  851. /* Input file. */
  852. break;
  853. case '>':
  854. /* Output file. */
  855. break;
  856. case 'p':
  857. case 'n':
  858. case 'N':
  859. if (!opt_int(arg, &ival))
  860. return -1;
  861. if (o->valtype == 'p' && ival <= 0) {
  862. opt_printf_stderr("%s: Non-positive number \"%s\" for option -%s\n",
  863. prog, arg, o->name);
  864. return -1;
  865. }
  866. if (o->valtype == 'N' && ival < 0) {
  867. opt_printf_stderr("%s: Negative number \"%s\" for option -%s\n",
  868. prog, arg, o->name);
  869. return -1;
  870. }
  871. break;
  872. case 'M':
  873. if (!opt_intmax(arg, &imval))
  874. return -1;
  875. break;
  876. case 'U':
  877. if (!opt_uintmax(arg, &umval))
  878. return -1;
  879. break;
  880. case 'l':
  881. if (!opt_long(arg, &lval))
  882. return -1;
  883. break;
  884. case 'u':
  885. if (!opt_ulong(arg, &ulval))
  886. return -1;
  887. break;
  888. case 'c':
  889. case 'E':
  890. case 'F':
  891. case 'f':
  892. if (opt_format(arg,
  893. o->valtype == 'c' ? OPT_FMT_PDS :
  894. o->valtype == 'E' ? OPT_FMT_PDE :
  895. o->valtype == 'F' ? OPT_FMT_PEMDER
  896. : OPT_FMT_ANY, &ival))
  897. break;
  898. opt_printf_stderr("%s: Invalid format \"%s\" for option -%s\n",
  899. prog, arg, o->name);
  900. return -1;
  901. }
  902. /* Return the flag value. */
  903. return o->retval;
  904. }
  905. if (unknown != NULL) {
  906. if (dunno != NULL) {
  907. opt_printf_stderr("%s: Multiple %s or unknown options: -%s and -%s\n",
  908. prog, unknown_name, dunno, p);
  909. return -1;
  910. }
  911. dunno = p;
  912. return unknown->retval;
  913. }
  914. opt_printf_stderr("%s: Unknown option: -%s\n", prog, p);
  915. return -1;
  916. }
  917. /* Return the most recent flag parameter. */
  918. char *opt_arg(void)
  919. {
  920. return arg;
  921. }
  922. /* Return the most recent flag (option name including the preceding '-'). */
  923. char *opt_flag(void)
  924. {
  925. return flag;
  926. }
  927. /* Return the unknown option. */
  928. char *opt_unknown(void)
  929. {
  930. return dunno;
  931. }
  932. /* Reset the unknown option; needed by ocsp to allow multiple digest options. */
  933. void reset_unknown(void)
  934. {
  935. dunno = NULL;
  936. }
  937. /* Return the rest of the arguments after parsing flags. */
  938. char **opt_rest(void)
  939. {
  940. return &argv[opt_index];
  941. }
  942. /* How many items in remaining args? */
  943. int opt_num_rest(void)
  944. {
  945. int i = 0;
  946. char **pp;
  947. for (pp = opt_rest(); *pp; pp++, i++)
  948. continue;
  949. return i;
  950. }
  951. int opt_check_rest_arg(const char *expected)
  952. {
  953. char *opt = *opt_rest();
  954. if (opt == NULL || *opt == '\0') {
  955. if (expected == NULL)
  956. return 1;
  957. opt_printf_stderr("%s: Missing argument: %s\n", prog, expected);
  958. return 0;
  959. }
  960. if (expected != NULL)
  961. return 1;
  962. if (opt_unknown() == NULL)
  963. opt_printf_stderr("%s: Extra option: \"%s\"\n", prog, opt);
  964. else
  965. opt_printf_stderr("%s: Extra (unknown) options: \"%s\" \"%s\"\n",
  966. prog, opt_unknown(), opt);
  967. return 0;
  968. }
  969. /* Return a string describing the parameter type. */
  970. static const char *valtype2param(const OPTIONS *o)
  971. {
  972. switch (o->valtype) {
  973. case 0:
  974. case '-':
  975. return "";
  976. case ':':
  977. return "uri";
  978. case 's':
  979. return "val";
  980. case '/':
  981. return "dir";
  982. case '<':
  983. return "infile";
  984. case '>':
  985. return "outfile";
  986. case 'p':
  987. return "+int";
  988. case 'n':
  989. return "int";
  990. case 'l':
  991. return "long";
  992. case 'u':
  993. return "ulong";
  994. case 'E':
  995. return "PEM|DER|ENGINE";
  996. case 'F':
  997. return "PEM|DER";
  998. case 'f':
  999. return "format";
  1000. case 'M':
  1001. return "intmax";
  1002. case 'N':
  1003. return "nonneg";
  1004. case 'U':
  1005. return "uintmax";
  1006. }
  1007. return "parm";
  1008. }
  1009. static void opt_print(const OPTIONS *o, int doingparams, int width)
  1010. {
  1011. const char* help;
  1012. char start[80 + 1];
  1013. int linelen, printlen;
  1014. /* Avoid OOB if width is beyond the buffer size of start */
  1015. if (width >= (int)sizeof(start))
  1016. width = (int)sizeof(start) - 1;
  1017. help = o->helpstr ? o->helpstr : "(No additional info)";
  1018. if (o->name == OPT_HELP_STR) {
  1019. opt_printf_stderr(help, prog);
  1020. return;
  1021. } else if (o->name == OPT_SECTION_STR) {
  1022. opt_printf_stderr("\n");
  1023. opt_printf_stderr(help, prog);
  1024. return;
  1025. } else if (o->name == OPT_PARAM_STR) {
  1026. opt_printf_stderr("\nParameters:\n");
  1027. return;
  1028. }
  1029. /* Pad out prefix */
  1030. memset(start, ' ', sizeof(start) - 1);
  1031. start[sizeof(start) - 1] = '\0';
  1032. if (o->name == OPT_MORE_STR) {
  1033. /* Continuation of previous line; pad and print. */
  1034. start[width] = '\0';
  1035. opt_printf_stderr("%s %s\n", start, help);
  1036. return;
  1037. }
  1038. /* Build up the "-flag [param]" part. */
  1039. linelen = 0;
  1040. printlen = opt_printf_stderr(" %s", !doingparams ? "-" : "");
  1041. linelen += (printlen > 0) ? printlen : MAX_OPT_HELP_WIDTH;
  1042. printlen = opt_printf_stderr("%s" , o->name[0] ? o->name : "*");
  1043. linelen += (printlen > 0) ? printlen : MAX_OPT_HELP_WIDTH;
  1044. if (o->valtype != '-') {
  1045. printlen = opt_printf_stderr(" %s" , valtype2param(o));
  1046. linelen += (printlen > 0) ? printlen : MAX_OPT_HELP_WIDTH;
  1047. }
  1048. if (linelen >= MAX_OPT_HELP_WIDTH || linelen > width) {
  1049. opt_printf_stderr("%s", "\n");
  1050. memset(start, ' ', sizeof(start));
  1051. linelen = 0;
  1052. }
  1053. width -= linelen;
  1054. start[width] = '\0';
  1055. opt_printf_stderr("%s %s\n", start, help);
  1056. }
  1057. void opt_help(const OPTIONS *list)
  1058. {
  1059. const OPTIONS *o;
  1060. int i, sawparams = 0, width = 5;
  1061. int standard_prolog;
  1062. /* Starts with its own help message? */
  1063. standard_prolog = list[0].name != OPT_HELP_STR;
  1064. /* Find the widest help. */
  1065. for (o = list; o->name; o++) {
  1066. if (o->name == OPT_MORE_STR)
  1067. continue;
  1068. i = 2 + (int)strlen(o->name);
  1069. if (o->valtype != '-')
  1070. i += 1 + strlen(valtype2param(o));
  1071. if (i > width)
  1072. width = i;
  1073. }
  1074. if (width > MAX_OPT_HELP_WIDTH)
  1075. width = MAX_OPT_HELP_WIDTH;
  1076. if (standard_prolog) {
  1077. opt_printf_stderr("Usage: %s [options]\n", prog);
  1078. if (list[0].name != OPT_SECTION_STR)
  1079. opt_printf_stderr("Valid options are:\n", prog);
  1080. }
  1081. /* Now let's print. */
  1082. for (o = list; o->name; o++) {
  1083. if (o->name == OPT_PARAM_STR)
  1084. sawparams = 1;
  1085. opt_print(o, sawparams, width);
  1086. }
  1087. }
  1088. /* opt_isdir section */
  1089. #ifdef _WIN32
  1090. # include <windows.h>
  1091. int opt_isdir(const char *name)
  1092. {
  1093. DWORD attr;
  1094. # if defined(UNICODE) || defined(_UNICODE)
  1095. size_t i, len_0 = strlen(name) + 1;
  1096. WCHAR tempname[MAX_PATH];
  1097. if (len_0 > MAX_PATH)
  1098. return -1;
  1099. # if !defined(_WIN32_WCE) || _WIN32_WCE>=101
  1100. if (!MultiByteToWideChar(CP_ACP, 0, name, len_0, tempname, MAX_PATH))
  1101. # endif
  1102. for (i = 0; i < len_0; i++)
  1103. tempname[i] = (WCHAR)name[i];
  1104. attr = GetFileAttributes(tempname);
  1105. # else
  1106. attr = GetFileAttributes(name);
  1107. # endif
  1108. if (attr == INVALID_FILE_ATTRIBUTES)
  1109. return -1;
  1110. return ((attr & FILE_ATTRIBUTE_DIRECTORY) != 0);
  1111. }
  1112. #else
  1113. # include <sys/stat.h>
  1114. # ifndef S_ISDIR
  1115. # if defined(_S_IFMT) && defined(_S_IFDIR)
  1116. # define S_ISDIR(a) (((a) & _S_IFMT) == _S_IFDIR)
  1117. # else
  1118. # define S_ISDIR(a) (((a) & S_IFMT) == S_IFDIR)
  1119. # endif
  1120. # endif
  1121. int opt_isdir(const char *name)
  1122. {
  1123. # if defined(S_ISDIR)
  1124. struct stat st;
  1125. if (stat(name, &st) == 0)
  1126. return S_ISDIR(st.st_mode);
  1127. else
  1128. return -1;
  1129. # else
  1130. return -1;
  1131. # endif
  1132. }
  1133. #endif