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