storeutl.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. /*
  2. * Copyright 2016-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 <openssl/opensslconf.h>
  10. #include "apps.h"
  11. #include "progs.h"
  12. #include <openssl/err.h>
  13. #include <openssl/pem.h>
  14. #include <openssl/store.h>
  15. #include <openssl/x509v3.h> /* s2i_ASN1_INTEGER */
  16. static int process(const char *uri, const UI_METHOD *uimeth, PW_CB_DATA *uidata,
  17. int expected, int criterion, OSSL_STORE_SEARCH *search,
  18. int text, int noout, int recursive, int indent, BIO *out,
  19. const char *prog);
  20. typedef enum OPTION_choice {
  21. OPT_ERR = -1, OPT_EOF = 0, OPT_HELP, OPT_ENGINE, OPT_OUT, OPT_PASSIN,
  22. OPT_NOOUT, OPT_TEXT, OPT_RECURSIVE,
  23. OPT_SEARCHFOR_CERTS, OPT_SEARCHFOR_KEYS, OPT_SEARCHFOR_CRLS,
  24. OPT_CRITERION_SUBJECT, OPT_CRITERION_ISSUER, OPT_CRITERION_SERIAL,
  25. OPT_CRITERION_FINGERPRINT, OPT_CRITERION_ALIAS,
  26. OPT_MD
  27. } OPTION_CHOICE;
  28. const OPTIONS storeutl_options[] = {
  29. {OPT_HELP_STR, 1, '-', "Usage: %s [options] uri\nValid options are:\n"},
  30. {"help", OPT_HELP, '-', "Display this summary"},
  31. {"out", OPT_OUT, '>', "Output file - default stdout"},
  32. {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
  33. {"text", OPT_TEXT, '-', "Print a text form of the objects"},
  34. {"noout", OPT_NOOUT, '-', "No PEM output, just status"},
  35. {"certs", OPT_SEARCHFOR_CERTS, '-', "Search for certificates only"},
  36. {"keys", OPT_SEARCHFOR_KEYS, '-', "Search for keys only"},
  37. {"crls", OPT_SEARCHFOR_CRLS, '-', "Search for CRLs only"},
  38. {"subject", OPT_CRITERION_SUBJECT, 's', "Search by subject"},
  39. {"issuer", OPT_CRITERION_ISSUER, 's', "Search by issuer and serial, issuer name"},
  40. {"serial", OPT_CRITERION_SERIAL, 's', "Search by issuer and serial, serial number"},
  41. {"fingerprint", OPT_CRITERION_FINGERPRINT, 's', "Search by public key fingerprint, given in hex"},
  42. {"alias", OPT_CRITERION_ALIAS, 's', "Search by alias"},
  43. {"", OPT_MD, '-', "Any supported digest"},
  44. #ifndef OPENSSL_NO_ENGINE
  45. {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
  46. #endif
  47. {"r", OPT_RECURSIVE, '-', "Recurse through names"},
  48. {NULL}
  49. };
  50. int storeutl_main(int argc, char *argv[])
  51. {
  52. int ret = 1, noout = 0, text = 0, recursive = 0;
  53. char *outfile = NULL, *passin = NULL, *passinarg = NULL;
  54. BIO *out = NULL;
  55. ENGINE *e = NULL;
  56. OPTION_CHOICE o;
  57. char *prog = opt_init(argc, argv, storeutl_options);
  58. PW_CB_DATA pw_cb_data;
  59. int expected = 0;
  60. int criterion = 0;
  61. X509_NAME *subject = NULL, *issuer = NULL;
  62. ASN1_INTEGER *serial = NULL;
  63. unsigned char *fingerprint = NULL;
  64. size_t fingerprintlen = 0;
  65. char *alias = NULL;
  66. OSSL_STORE_SEARCH *search = NULL;
  67. const EVP_MD *digest = NULL;
  68. while ((o = opt_next()) != OPT_EOF) {
  69. switch (o) {
  70. case OPT_EOF:
  71. case OPT_ERR:
  72. opthelp:
  73. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  74. goto end;
  75. case OPT_HELP:
  76. opt_help(storeutl_options);
  77. ret = 0;
  78. goto end;
  79. case OPT_OUT:
  80. outfile = opt_arg();
  81. break;
  82. case OPT_PASSIN:
  83. passinarg = opt_arg();
  84. break;
  85. case OPT_NOOUT:
  86. noout = 1;
  87. break;
  88. case OPT_TEXT:
  89. text = 1;
  90. break;
  91. case OPT_RECURSIVE:
  92. recursive = 1;
  93. break;
  94. case OPT_SEARCHFOR_CERTS:
  95. case OPT_SEARCHFOR_KEYS:
  96. case OPT_SEARCHFOR_CRLS:
  97. if (expected != 0) {
  98. BIO_printf(bio_err, "%s: only one search type can be given.\n",
  99. prog);
  100. goto end;
  101. }
  102. {
  103. static const struct {
  104. enum OPTION_choice choice;
  105. int type;
  106. } map[] = {
  107. {OPT_SEARCHFOR_CERTS, OSSL_STORE_INFO_CERT},
  108. {OPT_SEARCHFOR_KEYS, OSSL_STORE_INFO_PKEY},
  109. {OPT_SEARCHFOR_CRLS, OSSL_STORE_INFO_CRL},
  110. };
  111. size_t i;
  112. for (i = 0; i < OSSL_NELEM(map); i++) {
  113. if (o == map[i].choice) {
  114. expected = map[i].type;
  115. break;
  116. }
  117. }
  118. /*
  119. * If expected wasn't set at this point, it means the map
  120. * isn't syncronised with the possible options leading here.
  121. */
  122. OPENSSL_assert(expected != 0);
  123. }
  124. break;
  125. case OPT_CRITERION_SUBJECT:
  126. if (criterion != 0) {
  127. BIO_printf(bio_err, "%s: criterion already given.\n",
  128. prog);
  129. goto end;
  130. }
  131. criterion = OSSL_STORE_SEARCH_BY_NAME;
  132. if (subject != NULL) {
  133. BIO_printf(bio_err, "%s: subject already given.\n",
  134. prog);
  135. goto end;
  136. }
  137. if ((subject = parse_name(opt_arg(), MBSTRING_UTF8, 1)) == NULL) {
  138. BIO_printf(bio_err, "%s: can't parse subject argument.\n",
  139. prog);
  140. goto end;
  141. }
  142. break;
  143. case OPT_CRITERION_ISSUER:
  144. if (criterion != 0
  145. || (criterion == OSSL_STORE_SEARCH_BY_ISSUER_SERIAL
  146. && issuer != NULL)) {
  147. BIO_printf(bio_err, "%s: criterion already given.\n",
  148. prog);
  149. goto end;
  150. }
  151. criterion = OSSL_STORE_SEARCH_BY_ISSUER_SERIAL;
  152. if (issuer != NULL) {
  153. BIO_printf(bio_err, "%s: issuer already given.\n",
  154. prog);
  155. goto end;
  156. }
  157. if ((issuer = parse_name(opt_arg(), MBSTRING_UTF8, 1)) == NULL) {
  158. BIO_printf(bio_err, "%s: can't parse issuer argument.\n",
  159. prog);
  160. goto end;
  161. }
  162. break;
  163. case OPT_CRITERION_SERIAL:
  164. if (criterion != 0
  165. || (criterion == OSSL_STORE_SEARCH_BY_ISSUER_SERIAL
  166. && serial != NULL)) {
  167. BIO_printf(bio_err, "%s: criterion already given.\n",
  168. prog);
  169. goto end;
  170. }
  171. criterion = OSSL_STORE_SEARCH_BY_ISSUER_SERIAL;
  172. if (serial != NULL) {
  173. BIO_printf(bio_err, "%s: serial number already given.\n",
  174. prog);
  175. goto end;
  176. }
  177. if ((serial = s2i_ASN1_INTEGER(NULL, opt_arg())) == NULL) {
  178. BIO_printf(bio_err, "%s: can't parse serial number argument.\n",
  179. prog);
  180. goto end;
  181. }
  182. break;
  183. case OPT_CRITERION_FINGERPRINT:
  184. if (criterion != 0
  185. || (criterion == OSSL_STORE_SEARCH_BY_KEY_FINGERPRINT
  186. && fingerprint != NULL)) {
  187. BIO_printf(bio_err, "%s: criterion already given.\n",
  188. prog);
  189. goto end;
  190. }
  191. criterion = OSSL_STORE_SEARCH_BY_KEY_FINGERPRINT;
  192. if (fingerprint != NULL) {
  193. BIO_printf(bio_err, "%s: fingerprint already given.\n",
  194. prog);
  195. goto end;
  196. }
  197. {
  198. long tmplen = 0;
  199. if ((fingerprint = OPENSSL_hexstr2buf(opt_arg(), &tmplen))
  200. == NULL) {
  201. BIO_printf(bio_err,
  202. "%s: can't parse fingerprint argument.\n",
  203. prog);
  204. goto end;
  205. }
  206. fingerprintlen = (size_t)tmplen;
  207. }
  208. break;
  209. case OPT_CRITERION_ALIAS:
  210. if (criterion != 0) {
  211. BIO_printf(bio_err, "%s: criterion already given.\n",
  212. prog);
  213. goto end;
  214. }
  215. criterion = OSSL_STORE_SEARCH_BY_ALIAS;
  216. if (alias != NULL) {
  217. BIO_printf(bio_err, "%s: alias already given.\n",
  218. prog);
  219. goto end;
  220. }
  221. if ((alias = OPENSSL_strdup(opt_arg())) == NULL) {
  222. BIO_printf(bio_err, "%s: can't parse alias argument.\n",
  223. prog);
  224. goto end;
  225. }
  226. break;
  227. case OPT_ENGINE:
  228. e = setup_engine(opt_arg(), 0);
  229. break;
  230. case OPT_MD:
  231. if (!opt_md(opt_unknown(), &digest))
  232. goto opthelp;
  233. }
  234. }
  235. argc = opt_num_rest();
  236. argv = opt_rest();
  237. if (argc == 0) {
  238. BIO_printf(bio_err, "%s: No URI given, nothing to do...\n", prog);
  239. goto opthelp;
  240. }
  241. if (argc > 1) {
  242. BIO_printf(bio_err, "%s: Unknown extra parameters after URI\n", prog);
  243. goto opthelp;
  244. }
  245. if (criterion != 0) {
  246. switch (criterion) {
  247. case OSSL_STORE_SEARCH_BY_NAME:
  248. if ((search = OSSL_STORE_SEARCH_by_name(subject)) == NULL) {
  249. ERR_print_errors(bio_err);
  250. goto end;
  251. }
  252. break;
  253. case OSSL_STORE_SEARCH_BY_ISSUER_SERIAL:
  254. if (issuer == NULL || serial == NULL) {
  255. BIO_printf(bio_err,
  256. "%s: both -issuer and -serial must be given.\n",
  257. prog);
  258. goto end;
  259. }
  260. if ((search = OSSL_STORE_SEARCH_by_issuer_serial(issuer, serial))
  261. == NULL) {
  262. ERR_print_errors(bio_err);
  263. goto end;
  264. }
  265. break;
  266. case OSSL_STORE_SEARCH_BY_KEY_FINGERPRINT:
  267. if ((search = OSSL_STORE_SEARCH_by_key_fingerprint(digest,
  268. fingerprint,
  269. fingerprintlen))
  270. == NULL) {
  271. ERR_print_errors(bio_err);
  272. goto end;
  273. }
  274. break;
  275. case OSSL_STORE_SEARCH_BY_ALIAS:
  276. if ((search = OSSL_STORE_SEARCH_by_alias(alias)) == NULL) {
  277. ERR_print_errors(bio_err);
  278. goto end;
  279. }
  280. break;
  281. }
  282. }
  283. if (!app_passwd(passinarg, NULL, &passin, NULL)) {
  284. BIO_printf(bio_err, "Error getting passwords\n");
  285. goto end;
  286. }
  287. pw_cb_data.password = passin;
  288. pw_cb_data.prompt_info = argv[0];
  289. out = bio_open_default(outfile, 'w', FORMAT_TEXT);
  290. if (out == NULL)
  291. goto end;
  292. ret = process(argv[0], get_ui_method(), &pw_cb_data,
  293. expected, criterion, search,
  294. text, noout, recursive, 0, out, prog);
  295. end:
  296. OPENSSL_free(alias);
  297. ASN1_INTEGER_free(serial);
  298. X509_NAME_free(subject);
  299. X509_NAME_free(issuer);
  300. OSSL_STORE_SEARCH_free(search);
  301. BIO_free_all(out);
  302. OPENSSL_free(passin);
  303. release_engine(e);
  304. return ret;
  305. }
  306. static int indent_printf(int indent, BIO *bio, const char *format, ...)
  307. {
  308. va_list args;
  309. int ret;
  310. va_start(args, format);
  311. ret = BIO_printf(bio, "%*s", indent, "") + BIO_vprintf(bio, format, args);
  312. va_end(args);
  313. return ret;
  314. }
  315. static int process(const char *uri, const UI_METHOD *uimeth, PW_CB_DATA *uidata,
  316. int expected, int criterion, OSSL_STORE_SEARCH *search,
  317. int text, int noout, int recursive, int indent, BIO *out,
  318. const char *prog)
  319. {
  320. OSSL_STORE_CTX *store_ctx = NULL;
  321. int ret = 1, items = 0;
  322. if ((store_ctx = OSSL_STORE_open(uri, uimeth, uidata, NULL, NULL))
  323. == NULL) {
  324. BIO_printf(bio_err, "Couldn't open file or uri %s\n", uri);
  325. ERR_print_errors(bio_err);
  326. return ret;
  327. }
  328. if (expected != 0) {
  329. if (!OSSL_STORE_expect(store_ctx, expected)) {
  330. ERR_print_errors(bio_err);
  331. goto end2;
  332. }
  333. }
  334. if (criterion != 0) {
  335. if (!OSSL_STORE_supports_search(store_ctx, criterion)) {
  336. BIO_printf(bio_err,
  337. "%s: the store scheme doesn't support the given search criteria.\n",
  338. prog);
  339. goto end2;
  340. }
  341. if (!OSSL_STORE_find(store_ctx, search)) {
  342. ERR_print_errors(bio_err);
  343. goto end2;
  344. }
  345. }
  346. /* From here on, we count errors, and we'll return the count at the end */
  347. ret = 0;
  348. for (;;) {
  349. OSSL_STORE_INFO *info = OSSL_STORE_load(store_ctx);
  350. int type = info == NULL ? 0 : OSSL_STORE_INFO_get_type(info);
  351. const char *infostr =
  352. info == NULL ? NULL : OSSL_STORE_INFO_type_string(type);
  353. if (info == NULL) {
  354. if (OSSL_STORE_eof(store_ctx))
  355. break;
  356. if (OSSL_STORE_error(store_ctx)) {
  357. if (recursive)
  358. ERR_clear_error();
  359. else
  360. ERR_print_errors(bio_err);
  361. ret++;
  362. continue;
  363. }
  364. BIO_printf(bio_err,
  365. "ERROR: OSSL_STORE_load() returned NULL without "
  366. "eof or error indications\n");
  367. BIO_printf(bio_err, " This is an error in the loader\n");
  368. ERR_print_errors(bio_err);
  369. ret++;
  370. break;
  371. }
  372. if (type == OSSL_STORE_INFO_NAME) {
  373. const char *name = OSSL_STORE_INFO_get0_NAME(info);
  374. const char *desc = OSSL_STORE_INFO_get0_NAME_description(info);
  375. indent_printf(indent, bio_out, "%d: %s: %s\n", items, infostr,
  376. name);
  377. if (desc != NULL)
  378. indent_printf(indent, bio_out, "%s\n", desc);
  379. } else {
  380. indent_printf(indent, bio_out, "%d: %s\n", items, infostr);
  381. }
  382. /*
  383. * Unfortunately, PEM_X509_INFO_write_bio() is sorely lacking in
  384. * functionality, so we must figure out how exactly to write things
  385. * ourselves...
  386. */
  387. switch (type) {
  388. case OSSL_STORE_INFO_NAME:
  389. if (recursive) {
  390. const char *suburi = OSSL_STORE_INFO_get0_NAME(info);
  391. ret += process(suburi, uimeth, uidata,
  392. expected, criterion, search,
  393. text, noout, recursive, indent + 2, out, prog);
  394. }
  395. break;
  396. case OSSL_STORE_INFO_PARAMS:
  397. if (text)
  398. EVP_PKEY_print_params(out, OSSL_STORE_INFO_get0_PARAMS(info),
  399. 0, NULL);
  400. if (!noout)
  401. PEM_write_bio_Parameters(out,
  402. OSSL_STORE_INFO_get0_PARAMS(info));
  403. break;
  404. case OSSL_STORE_INFO_PKEY:
  405. if (text)
  406. EVP_PKEY_print_private(out, OSSL_STORE_INFO_get0_PKEY(info),
  407. 0, NULL);
  408. if (!noout)
  409. PEM_write_bio_PrivateKey(out, OSSL_STORE_INFO_get0_PKEY(info),
  410. NULL, NULL, 0, NULL, NULL);
  411. break;
  412. case OSSL_STORE_INFO_CERT:
  413. if (text)
  414. X509_print(out, OSSL_STORE_INFO_get0_CERT(info));
  415. if (!noout)
  416. PEM_write_bio_X509(out, OSSL_STORE_INFO_get0_CERT(info));
  417. break;
  418. case OSSL_STORE_INFO_CRL:
  419. if (text)
  420. X509_CRL_print(out, OSSL_STORE_INFO_get0_CRL(info));
  421. if (!noout)
  422. PEM_write_bio_X509_CRL(out, OSSL_STORE_INFO_get0_CRL(info));
  423. break;
  424. default:
  425. BIO_printf(bio_err, "!!! Unknown code\n");
  426. ret++;
  427. break;
  428. }
  429. items++;
  430. OSSL_STORE_INFO_free(info);
  431. }
  432. indent_printf(indent, out, "Total found: %d\n", items);
  433. end2:
  434. if (!OSSL_STORE_close(store_ctx)) {
  435. ERR_print_errors(bio_err);
  436. ret++;
  437. }
  438. return ret;
  439. }