]>
Commit | Line | Data |
---|---|---|
c403a1dd | 1 | /* |
6738bf14 | 2 | * Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved. |
c403a1dd RL |
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 | ||
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> | |
17 | ||
fb43ddce | 18 | static int process(const char *uri, const UI_METHOD *uimeth, PW_CB_DATA *uidata, |
9511d973 | 19 | int expected, |
fb43ddce RL |
20 | int text, int noout, int recursive, int indent, BIO *out); |
21 | ||
c403a1dd RL |
22 | typedef enum OPTION_choice { |
23 | OPT_ERR = -1, OPT_EOF = 0, OPT_HELP, OPT_ENGINE, OPT_OUT, OPT_PASSIN, | |
9511d973 RL |
24 | OPT_NOOUT, OPT_TEXT, OPT_RECURSIVE, |
25 | OPT_SEARCHFOR_CERTS, OPT_SEARCHFOR_KEYS, OPT_SEARCHFOR_CRLS | |
c403a1dd RL |
26 | } OPTION_CHOICE; |
27 | ||
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"}, | |
9511d973 RL |
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"}, | |
c403a1dd RL |
38 | #ifndef OPENSSL_NO_ENGINE |
39 | {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"}, | |
40 | #endif | |
fb43ddce | 41 | {"r", OPT_RECURSIVE, '-', "Recurse through names"}, |
c403a1dd RL |
42 | {NULL} |
43 | }; | |
44 | ||
45 | int storeutl_main(int argc, char *argv[]) | |
46 | { | |
fb43ddce | 47 | int ret = 1, noout = 0, text = 0, recursive = 0; |
c403a1dd RL |
48 | char *outfile = NULL, *passin = NULL, *passinarg = NULL; |
49 | BIO *out = NULL; | |
50 | ENGINE *e = NULL; | |
51 | OPTION_CHOICE o; | |
52 | char *prog = opt_init(argc, argv, storeutl_options); | |
53 | PW_CB_DATA pw_cb_data; | |
9511d973 | 54 | int expected = 0; |
c403a1dd RL |
55 | |
56 | while ((o = opt_next()) != OPT_EOF) { | |
57 | switch (o) { | |
58 | case OPT_EOF: | |
59 | case OPT_ERR: | |
60 | opthelp: | |
61 | BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); | |
62 | goto end; | |
63 | case OPT_HELP: | |
64 | opt_help(storeutl_options); | |
65 | ret = 0; | |
66 | goto end; | |
67 | case OPT_OUT: | |
68 | outfile = opt_arg(); | |
69 | break; | |
70 | case OPT_PASSIN: | |
71 | passinarg = opt_arg(); | |
72 | break; | |
73 | case OPT_NOOUT: | |
74 | noout = 1; | |
75 | break; | |
76 | case OPT_TEXT: | |
77 | text = 1; | |
78 | break; | |
fb43ddce RL |
79 | case OPT_RECURSIVE: |
80 | recursive = 1; | |
81 | break; | |
9511d973 RL |
82 | case OPT_SEARCHFOR_CERTS: |
83 | case OPT_SEARCHFOR_KEYS: | |
84 | case OPT_SEARCHFOR_CRLS: | |
85 | if (expected != 0) { | |
86 | BIO_printf(bio_err, "%s: only one search type can be given.\n", | |
87 | prog); | |
88 | goto end; | |
89 | } | |
90 | { | |
91 | static const struct { | |
92 | enum OPTION_choice choice; | |
93 | int type; | |
94 | } map[] = { | |
95 | {OPT_SEARCHFOR_CERTS, OSSL_STORE_INFO_CERT}, | |
96 | {OPT_SEARCHFOR_KEYS, OSSL_STORE_INFO_PKEY}, | |
97 | {OPT_SEARCHFOR_CRLS, OSSL_STORE_INFO_CRL}, | |
98 | }; | |
99 | size_t i; | |
100 | ||
101 | for (i = 0; i < OSSL_NELEM(map); i++) { | |
102 | if (o == map[i].choice) { | |
103 | expected = map[i].type; | |
104 | break; | |
105 | } | |
106 | } | |
107 | /* | |
108 | * If expected wasn't set at this point, it means the map | |
109 | * isn't syncronised with the possible options leading here. | |
110 | */ | |
111 | OPENSSL_assert(expected != 0); | |
112 | } | |
113 | break; | |
c403a1dd RL |
114 | case OPT_ENGINE: |
115 | e = setup_engine(opt_arg(), 0); | |
116 | break; | |
117 | } | |
118 | } | |
119 | argc = opt_num_rest(); | |
120 | argv = opt_rest(); | |
121 | ||
122 | if (argc == 0) { | |
123 | BIO_printf(bio_err, "%s: No URI given, nothing to do...\n", prog); | |
124 | goto opthelp; | |
125 | } | |
126 | if (argc > 1) { | |
127 | BIO_printf(bio_err, "%s: Unknown extra parameters after URI\n", prog); | |
128 | goto opthelp; | |
129 | } | |
130 | ||
131 | if (!app_passwd(passinarg, NULL, &passin, NULL)) { | |
132 | BIO_printf(bio_err, "Error getting passwords\n"); | |
133 | goto end; | |
134 | } | |
135 | pw_cb_data.password = passin; | |
136 | pw_cb_data.prompt_info = argv[0]; | |
137 | ||
138 | out = bio_open_default(outfile, 'w', FORMAT_TEXT); | |
139 | if (out == NULL) | |
140 | goto end; | |
141 | ||
9511d973 RL |
142 | ret = process(argv[0], get_ui_method(), &pw_cb_data, |
143 | expected, | |
144 | text, noout, recursive, 0, out); | |
fb43ddce RL |
145 | |
146 | end: | |
147 | BIO_free_all(out); | |
148 | OPENSSL_free(passin); | |
149 | release_engine(e); | |
150 | return ret; | |
151 | } | |
152 | ||
153 | static int indent_printf(int indent, BIO *bio, const char *format, ...) | |
154 | { | |
155 | va_list args; | |
156 | int ret; | |
157 | ||
158 | va_start(args, format); | |
159 | ||
160 | ret = BIO_printf(bio, "%*s", indent, "") + BIO_vprintf(bio, format, args); | |
161 | ||
162 | va_end(args); | |
163 | return ret; | |
164 | } | |
165 | ||
166 | static int process(const char *uri, const UI_METHOD *uimeth, PW_CB_DATA *uidata, | |
9511d973 | 167 | int expected, |
fb43ddce RL |
168 | int text, int noout, int recursive, int indent, BIO *out) |
169 | { | |
170 | OSSL_STORE_CTX *store_ctx = NULL; | |
171 | int ret = 1, items = 0; | |
172 | ||
173 | if ((store_ctx = OSSL_STORE_open(uri, uimeth, uidata, NULL, NULL)) | |
174 | == NULL) { | |
175 | BIO_printf(bio_err, "Couldn't open file or uri %s\n", uri); | |
c403a1dd | 176 | ERR_print_errors(bio_err); |
fb43ddce | 177 | return ret; |
c403a1dd RL |
178 | } |
179 | ||
9511d973 RL |
180 | if (expected != 0) { |
181 | if (!OSSL_STORE_expect(store_ctx, expected)) { | |
182 | ERR_print_errors(bio_err); | |
183 | goto end2; | |
184 | } | |
185 | } | |
186 | ||
c403a1dd RL |
187 | /* From here on, we count errors, and we'll return the count at the end */ |
188 | ret = 0; | |
189 | ||
190 | for (;;) { | |
191 | OSSL_STORE_INFO *info = OSSL_STORE_load(store_ctx); | |
192 | int type = info == NULL ? 0 : OSSL_STORE_INFO_get_type(info); | |
193 | const char *infostr = | |
194 | info == NULL ? NULL : OSSL_STORE_INFO_type_string(type); | |
195 | ||
196 | if (info == NULL) { | |
197 | if (OSSL_STORE_eof(store_ctx)) | |
198 | break; | |
199 | ||
200 | if (OSSL_STORE_error(store_ctx)) { | |
fb43ddce RL |
201 | if (recursive) |
202 | ERR_clear_error(); | |
203 | else | |
204 | ERR_print_errors(bio_err); | |
c403a1dd | 205 | ret++; |
6fc1d33c | 206 | continue; |
c403a1dd RL |
207 | } |
208 | ||
209 | BIO_printf(bio_err, | |
210 | "ERROR: OSSL_STORE_load() returned NULL without " | |
211 | "eof or error indications\n"); | |
212 | BIO_printf(bio_err, " This is an error in the loader\n"); | |
213 | ERR_print_errors(bio_err); | |
214 | ret++; | |
215 | break; | |
216 | } | |
217 | ||
218 | if (type == OSSL_STORE_INFO_NAME) { | |
219 | const char *name = OSSL_STORE_INFO_get0_NAME(info); | |
220 | const char *desc = OSSL_STORE_INFO_get0_NAME_description(info); | |
fb43ddce RL |
221 | indent_printf(indent, bio_out, "%d: %s: %s\n", items, infostr, |
222 | name); | |
c403a1dd | 223 | if (desc != NULL) |
fb43ddce | 224 | indent_printf(indent, bio_out, "%s\n", desc); |
c403a1dd | 225 | } else { |
fb43ddce | 226 | indent_printf(indent, bio_out, "%d: %s\n", items, infostr); |
c403a1dd RL |
227 | } |
228 | ||
229 | /* | |
230 | * Unfortunately, PEM_X509_INFO_write_bio() is sorely lacking in | |
231 | * functionality, so we must figure out how exactly to write things | |
232 | * ourselves... | |
233 | */ | |
234 | switch (type) { | |
235 | case OSSL_STORE_INFO_NAME: | |
fb43ddce RL |
236 | if (recursive) { |
237 | const char *suburi = OSSL_STORE_INFO_get0_NAME(info); | |
9511d973 RL |
238 | ret += process(suburi, uimeth, uidata, |
239 | expected, | |
240 | text, noout, recursive, indent + 2, out); | |
fb43ddce | 241 | } |
c403a1dd RL |
242 | break; |
243 | case OSSL_STORE_INFO_PARAMS: | |
244 | if (text) | |
245 | EVP_PKEY_print_params(out, OSSL_STORE_INFO_get0_PARAMS(info), | |
246 | 0, NULL); | |
247 | if (!noout) | |
248 | PEM_write_bio_Parameters(out, | |
249 | OSSL_STORE_INFO_get0_PARAMS(info)); | |
250 | break; | |
251 | case OSSL_STORE_INFO_PKEY: | |
252 | if (text) | |
253 | EVP_PKEY_print_private(out, OSSL_STORE_INFO_get0_PKEY(info), | |
254 | 0, NULL); | |
255 | if (!noout) | |
256 | PEM_write_bio_PrivateKey(out, OSSL_STORE_INFO_get0_PKEY(info), | |
257 | NULL, NULL, 0, NULL, NULL); | |
258 | break; | |
259 | case OSSL_STORE_INFO_CERT: | |
260 | if (text) | |
261 | X509_print(out, OSSL_STORE_INFO_get0_CERT(info)); | |
262 | if (!noout) | |
263 | PEM_write_bio_X509(out, OSSL_STORE_INFO_get0_CERT(info)); | |
264 | break; | |
265 | case OSSL_STORE_INFO_CRL: | |
266 | if (text) | |
267 | X509_CRL_print(out, OSSL_STORE_INFO_get0_CRL(info)); | |
268 | if (!noout) | |
269 | PEM_write_bio_X509_CRL(out, OSSL_STORE_INFO_get0_CRL(info)); | |
270 | break; | |
271 | default: | |
272 | BIO_printf(bio_err, "!!! Unknown code\n"); | |
273 | ret++; | |
274 | break; | |
275 | } | |
276 | items++; | |
277 | OSSL_STORE_INFO_free(info); | |
278 | } | |
fb43ddce | 279 | indent_printf(indent, out, "Total found: %d\n", items); |
c403a1dd | 280 | |
9511d973 | 281 | end2: |
c403a1dd RL |
282 | if (!OSSL_STORE_close(store_ctx)) { |
283 | ERR_print_errors(bio_err); | |
284 | ret++; | |
c403a1dd RL |
285 | } |
286 | ||
c403a1dd RL |
287 | return ret; |
288 | } |