storeutl.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. /*
  2. * Copyright 2016-2021 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);
  20. typedef enum OPTION_choice {
  21. OPT_COMMON,
  22. OPT_ENGINE, OPT_OUT, OPT_PASSIN,
  23. OPT_NOOUT, OPT_TEXT, OPT_RECURSIVE,
  24. OPT_SEARCHFOR_CERTS, OPT_SEARCHFOR_KEYS, OPT_SEARCHFOR_CRLS,
  25. OPT_CRITERION_SUBJECT, OPT_CRITERION_ISSUER, OPT_CRITERION_SERIAL,
  26. OPT_CRITERION_FINGERPRINT, OPT_CRITERION_ALIAS,
  27. OPT_MD, OPT_PROV_ENUM
  28. } OPTION_CHOICE;
  29. const OPTIONS storeutl_options[] = {
  30. {OPT_HELP_STR, 1, '-', "Usage: %s [options] uri\n"},
  31. OPT_SECTION("General"),
  32. {"help", OPT_HELP, '-', "Display this summary"},
  33. {"", OPT_MD, '-', "Any supported digest"},
  34. #ifndef OPENSSL_NO_ENGINE
  35. {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
  36. #endif
  37. OPT_SECTION("Search"),
  38. {"certs", OPT_SEARCHFOR_CERTS, '-', "Search for certificates only"},
  39. {"keys", OPT_SEARCHFOR_KEYS, '-', "Search for keys only"},
  40. {"crls", OPT_SEARCHFOR_CRLS, '-', "Search for CRLs only"},
  41. {"subject", OPT_CRITERION_SUBJECT, 's', "Search by subject"},
  42. {"issuer", OPT_CRITERION_ISSUER, 's', "Search by issuer and serial, issuer name"},
  43. {"serial", OPT_CRITERION_SERIAL, 's', "Search by issuer and serial, serial number"},
  44. {"fingerprint", OPT_CRITERION_FINGERPRINT, 's', "Search by public key fingerprint, given in hex"},
  45. {"alias", OPT_CRITERION_ALIAS, 's', "Search by alias"},
  46. {"r", OPT_RECURSIVE, '-', "Recurse through names"},
  47. OPT_SECTION("Input"),
  48. {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
  49. OPT_SECTION("Output"),
  50. {"out", OPT_OUT, '>', "Output file - default stdout"},
  51. {"text", OPT_TEXT, '-', "Print a text form of the objects"},
  52. {"noout", OPT_NOOUT, '-', "No PEM output, just status"},
  53. OPT_PROV_OPTIONS,
  54. OPT_PARAMETERS(),
  55. {"uri", 0, 0, "URI of the store object"},
  56. {NULL}
  57. };
  58. int storeutl_main(int argc, char *argv[])
  59. {
  60. int ret = 1, noout = 0, text = 0, recursive = 0;
  61. char *outfile = NULL, *passin = NULL, *passinarg = NULL;
  62. BIO *out = NULL;
  63. ENGINE *e = NULL;
  64. OPTION_CHOICE o;
  65. char *prog = opt_init(argc, argv, storeutl_options);
  66. PW_CB_DATA pw_cb_data;
  67. int expected = 0;
  68. int criterion = 0;
  69. X509_NAME *subject = NULL, *issuer = NULL;
  70. ASN1_INTEGER *serial = NULL;
  71. unsigned char *fingerprint = NULL;
  72. size_t fingerprintlen = 0;
  73. char *alias = NULL, *digestname = NULL;
  74. OSSL_STORE_SEARCH *search = NULL;
  75. EVP_MD *digest = NULL;
  76. OSSL_LIB_CTX *libctx = app_get0_libctx();
  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. digestname = opt_unknown();
  237. break;
  238. case OPT_PROV_CASES:
  239. if (!opt_provider(o))
  240. goto end;
  241. break;
  242. }
  243. }
  244. /* One argument, the URI */
  245. argc = opt_num_rest();
  246. argv = opt_rest();
  247. if (argc != 1)
  248. goto opthelp;
  249. if (digestname != NULL) {
  250. if (!opt_md(digestname, &digest))
  251. goto opthelp;
  252. }
  253. if (criterion != 0) {
  254. switch (criterion) {
  255. case OSSL_STORE_SEARCH_BY_NAME:
  256. if ((search = OSSL_STORE_SEARCH_by_name(subject)) == NULL) {
  257. ERR_print_errors(bio_err);
  258. goto end;
  259. }
  260. break;
  261. case OSSL_STORE_SEARCH_BY_ISSUER_SERIAL:
  262. if (issuer == NULL || serial == NULL) {
  263. BIO_printf(bio_err,
  264. "%s: both -issuer and -serial must be given.\n",
  265. prog);
  266. goto end;
  267. }
  268. if ((search = OSSL_STORE_SEARCH_by_issuer_serial(issuer, serial))
  269. == NULL) {
  270. ERR_print_errors(bio_err);
  271. goto end;
  272. }
  273. break;
  274. case OSSL_STORE_SEARCH_BY_KEY_FINGERPRINT:
  275. if ((search = OSSL_STORE_SEARCH_by_key_fingerprint(digest,
  276. fingerprint,
  277. fingerprintlen))
  278. == NULL) {
  279. ERR_print_errors(bio_err);
  280. goto end;
  281. }
  282. break;
  283. case OSSL_STORE_SEARCH_BY_ALIAS:
  284. if ((search = OSSL_STORE_SEARCH_by_alias(alias)) == NULL) {
  285. ERR_print_errors(bio_err);
  286. goto end;
  287. }
  288. break;
  289. }
  290. }
  291. if (!app_passwd(passinarg, NULL, &passin, NULL)) {
  292. BIO_printf(bio_err, "Error getting passwords\n");
  293. goto end;
  294. }
  295. pw_cb_data.password = passin;
  296. pw_cb_data.prompt_info = argv[0];
  297. out = bio_open_default(outfile, 'w', FORMAT_TEXT);
  298. if (out == NULL)
  299. goto end;
  300. ret = process(argv[0], get_ui_method(), &pw_cb_data,
  301. expected, criterion, search,
  302. text, noout, recursive, 0, out, prog, libctx);
  303. end:
  304. EVP_MD_free(digest);
  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)
  329. {
  330. OSSL_STORE_CTX *store_ctx = NULL;
  331. int ret = 1, items = 0;
  332. if ((store_ctx = OSSL_STORE_open_ex(uri, libctx, app_get0_propq(), uimeth, uidata,
  333. NULL, 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_error(store_ctx)) {
  366. if (recursive)
  367. ERR_clear_error();
  368. else
  369. ERR_print_errors(bio_err);
  370. if (OSSL_STORE_eof(store_ctx))
  371. break;
  372. ret++;
  373. continue;
  374. }
  375. if (OSSL_STORE_eof(store_ctx))
  376. break;
  377. BIO_printf(bio_err,
  378. "ERROR: OSSL_STORE_load() returned NULL without "
  379. "eof or error indications\n");
  380. BIO_printf(bio_err, " This is an error in the loader\n");
  381. ERR_print_errors(bio_err);
  382. ret++;
  383. break;
  384. }
  385. if (type == OSSL_STORE_INFO_NAME) {
  386. const char *name = OSSL_STORE_INFO_get0_NAME(info);
  387. const char *desc = OSSL_STORE_INFO_get0_NAME_description(info);
  388. indent_printf(indent, bio_out, "%d: %s: %s\n", items, infostr,
  389. name);
  390. if (desc != NULL)
  391. indent_printf(indent, bio_out, "%s\n", desc);
  392. } else {
  393. indent_printf(indent, bio_out, "%d: %s\n", items, infostr);
  394. }
  395. /*
  396. * Unfortunately, PEM_X509_INFO_write_bio() is sorely lacking in
  397. * functionality, so we must figure out how exactly to write things
  398. * ourselves...
  399. */
  400. switch (type) {
  401. case OSSL_STORE_INFO_NAME:
  402. if (recursive) {
  403. const char *suburi = OSSL_STORE_INFO_get0_NAME(info);
  404. ret += process(suburi, uimeth, uidata,
  405. expected, criterion, search,
  406. text, noout, recursive, indent + 2, out, prog,
  407. libctx);
  408. }
  409. break;
  410. case OSSL_STORE_INFO_PARAMS:
  411. if (text)
  412. EVP_PKEY_print_params(out, OSSL_STORE_INFO_get0_PARAMS(info),
  413. 0, NULL);
  414. if (!noout)
  415. PEM_write_bio_Parameters(out,
  416. OSSL_STORE_INFO_get0_PARAMS(info));
  417. break;
  418. case OSSL_STORE_INFO_PUBKEY:
  419. if (text)
  420. EVP_PKEY_print_public(out, OSSL_STORE_INFO_get0_PUBKEY(info),
  421. 0, NULL);
  422. if (!noout)
  423. PEM_write_bio_PUBKEY(out, OSSL_STORE_INFO_get0_PUBKEY(info));
  424. break;
  425. case OSSL_STORE_INFO_PKEY:
  426. if (text)
  427. EVP_PKEY_print_private(out, OSSL_STORE_INFO_get0_PKEY(info),
  428. 0, NULL);
  429. if (!noout)
  430. PEM_write_bio_PrivateKey(out, OSSL_STORE_INFO_get0_PKEY(info),
  431. NULL, NULL, 0, NULL, NULL);
  432. break;
  433. case OSSL_STORE_INFO_CERT:
  434. if (text)
  435. X509_print(out, OSSL_STORE_INFO_get0_CERT(info));
  436. if (!noout)
  437. PEM_write_bio_X509(out, OSSL_STORE_INFO_get0_CERT(info));
  438. break;
  439. case OSSL_STORE_INFO_CRL:
  440. if (text)
  441. X509_CRL_print(out, OSSL_STORE_INFO_get0_CRL(info));
  442. if (!noout)
  443. PEM_write_bio_X509_CRL(out, OSSL_STORE_INFO_get0_CRL(info));
  444. break;
  445. default:
  446. BIO_printf(bio_err, "!!! Unknown code\n");
  447. ret++;
  448. break;
  449. }
  450. items++;
  451. OSSL_STORE_INFO_free(info);
  452. }
  453. indent_printf(indent, out, "Total found: %d\n", items);
  454. end2:
  455. if (!OSSL_STORE_close(store_ctx)) {
  456. ERR_print_errors(bio_err);
  457. ret++;
  458. }
  459. return ret;
  460. }