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