storeutl.c 17 KB

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