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