Add answers for EVP_PKEY_get_default_digest_name() in RSA and DSA keymgmt
[openssl.git] / apps / storeutl.c
1 /*
2  * Copyright 2016-2018 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
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 #include <openssl/x509v3.h>      /* s2i_ASN1_INTEGER */
18
19 static int process(const char *uri, const UI_METHOD *uimeth, PW_CB_DATA *uidata,
20                    int expected, int criterion, OSSL_STORE_SEARCH *search,
21                    int text, int noout, int recursive, int indent, BIO *out,
22                    const char *prog);
23
24 typedef enum OPTION_choice {
25     OPT_ERR = -1, OPT_EOF = 0, OPT_HELP, OPT_ENGINE, OPT_OUT, OPT_PASSIN,
26     OPT_NOOUT, OPT_TEXT, OPT_RECURSIVE,
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
31 } OPTION_CHOICE;
32
33 const OPTIONS storeutl_options[] = {
34     {OPT_HELP_STR, 1, '-', "Usage: %s [options] uri\n"},
35
36     OPT_SECTION("General"),
37     {"help", OPT_HELP, '-', "Display this summary"},
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"),
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"},
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"},
52     {"r", OPT_RECURSIVE, '-', "Recurse through names"},
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"},
61
62     OPT_PARAMETERS(),
63     {"uri", 0, 0, "URI of the store object"},
64     {NULL}
65 };
66
67 int storeutl_main(int argc, char *argv[])
68 {
69     int ret = 1, noout = 0, text = 0, recursive = 0;
70     char *outfile = NULL, *passin = NULL, *passinarg = NULL;
71     BIO *out = NULL;
72     ENGINE *e = NULL;
73     OPTION_CHOICE o;
74     char *prog = opt_init(argc, argv, storeutl_options);
75     PW_CB_DATA pw_cb_data;
76     int expected = 0;
77     int criterion = 0;
78     X509_NAME *subject = NULL, *issuer = NULL;
79     ASN1_INTEGER *serial = NULL;
80     unsigned char *fingerprint = NULL;
81     size_t fingerprintlen = 0;
82     char *alias = NULL;
83     OSSL_STORE_SEARCH *search = NULL;
84     const EVP_MD *digest = NULL;
85
86     while ((o = opt_next()) != OPT_EOF) {
87         switch (o) {
88         case OPT_EOF:
89         case OPT_ERR:
90  opthelp:
91             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
92             goto end;
93         case OPT_HELP:
94             opt_help(storeutl_options);
95             ret = 0;
96             goto end;
97         case OPT_OUT:
98             outfile = opt_arg();
99             break;
100         case OPT_PASSIN:
101             passinarg = opt_arg();
102             break;
103         case OPT_NOOUT:
104             noout = 1;
105             break;
106         case OPT_TEXT:
107             text = 1;
108             break;
109         case OPT_RECURSIVE:
110             recursive = 1;
111             break;
112         case OPT_SEARCHFOR_CERTS:
113         case OPT_SEARCHFOR_KEYS:
114         case OPT_SEARCHFOR_CRLS:
115             if (expected != 0) {
116                 BIO_printf(bio_err, "%s: only one search type can be given.\n",
117                            prog);
118                 goto end;
119             }
120             {
121                 static const struct {
122                     enum OPTION_choice choice;
123                     int type;
124                 } map[] = {
125                     {OPT_SEARCHFOR_CERTS, OSSL_STORE_INFO_CERT},
126                     {OPT_SEARCHFOR_KEYS, OSSL_STORE_INFO_PKEY},
127                     {OPT_SEARCHFOR_CRLS, OSSL_STORE_INFO_CRL},
128                 };
129                 size_t i;
130
131                 for (i = 0; i < OSSL_NELEM(map); i++) {
132                     if (o == map[i].choice) {
133                         expected = map[i].type;
134                         break;
135                     }
136                 }
137                 /*
138                  * If expected wasn't set at this point, it means the map
139                  * isn't synchronised with the possible options leading here.
140                  */
141                 OPENSSL_assert(expected != 0);
142             }
143             break;
144         case OPT_CRITERION_SUBJECT:
145             if (criterion != 0) {
146                 BIO_printf(bio_err, "%s: criterion already given.\n",
147                            prog);
148                 goto end;
149             }
150             criterion = OSSL_STORE_SEARCH_BY_NAME;
151             if (subject != NULL) {
152                 BIO_printf(bio_err, "%s: subject already given.\n",
153                            prog);
154                 goto end;
155             }
156             if ((subject = parse_name(opt_arg(), MBSTRING_UTF8, 1)) == NULL) {
157                 BIO_printf(bio_err, "%s: can't parse subject argument.\n",
158                            prog);
159                 goto end;
160             }
161             break;
162         case OPT_CRITERION_ISSUER:
163             if (criterion != 0
164                 || (criterion == OSSL_STORE_SEARCH_BY_ISSUER_SERIAL
165                     && issuer != NULL)) {
166                 BIO_printf(bio_err, "%s: criterion already given.\n",
167                            prog);
168                 goto end;
169             }
170             criterion = OSSL_STORE_SEARCH_BY_ISSUER_SERIAL;
171             if (issuer != NULL) {
172                 BIO_printf(bio_err, "%s: issuer already given.\n",
173                            prog);
174                 goto end;
175             }
176             if ((issuer = parse_name(opt_arg(), MBSTRING_UTF8, 1)) == NULL) {
177                 BIO_printf(bio_err, "%s: can't parse issuer argument.\n",
178                            prog);
179                 goto end;
180             }
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;
247         case OPT_ENGINE:
248             e = setup_engine(opt_arg(), 0);
249             break;
250         case OPT_MD:
251             if (!opt_md(opt_unknown(), &digest))
252                 goto opthelp;
253         }
254     }
255     argc = opt_num_rest();
256     argv = opt_rest();
257
258     if (argc == 0) {
259         BIO_printf(bio_err, "%s: No URI given, nothing to do...\n", prog);
260         goto opthelp;
261     }
262     if (argc > 1) {
263         BIO_printf(bio_err, "%s: Unknown extra parameters after URI\n", prog);
264         goto opthelp;
265     }
266
267     if (criterion != 0) {
268         switch (criterion) {
269         case OSSL_STORE_SEARCH_BY_NAME:
270             if ((search = OSSL_STORE_SEARCH_by_name(subject)) == NULL) {
271                 ERR_print_errors(bio_err);
272                 goto end;
273             }
274             break;
275         case OSSL_STORE_SEARCH_BY_ISSUER_SERIAL:
276             if (issuer == NULL || serial == NULL) {
277                 BIO_printf(bio_err,
278                            "%s: both -issuer and -serial must be given.\n",
279                            prog);
280                 goto end;
281             }
282             if ((search = OSSL_STORE_SEARCH_by_issuer_serial(issuer, serial))
283                 == NULL) {
284                 ERR_print_errors(bio_err);
285                 goto end;
286             }
287             break;
288         case OSSL_STORE_SEARCH_BY_KEY_FINGERPRINT:
289             if ((search = OSSL_STORE_SEARCH_by_key_fingerprint(digest,
290                                                                fingerprint,
291                                                                fingerprintlen))
292                 == NULL) {
293                 ERR_print_errors(bio_err);
294                 goto end;
295             }
296             break;
297         case OSSL_STORE_SEARCH_BY_ALIAS:
298             if ((search = OSSL_STORE_SEARCH_by_alias(alias)) == NULL) {
299                 ERR_print_errors(bio_err);
300                 goto end;
301             }
302             break;
303         }
304     }
305
306     if (!app_passwd(passinarg, NULL, &passin, NULL)) {
307         BIO_printf(bio_err, "Error getting passwords\n");
308         goto end;
309     }
310     pw_cb_data.password = passin;
311     pw_cb_data.prompt_info = argv[0];
312
313     out = bio_open_default(outfile, 'w', FORMAT_TEXT);
314     if (out == NULL)
315         goto end;
316
317     ret = process(argv[0], get_ui_method(), &pw_cb_data,
318                   expected, criterion, search,
319                   text, noout, recursive, 0, out, prog);
320
321  end:
322     OPENSSL_free(fingerprint);
323     OPENSSL_free(alias);
324     ASN1_INTEGER_free(serial);
325     X509_NAME_free(subject);
326     X509_NAME_free(issuer);
327     OSSL_STORE_SEARCH_free(search);
328     BIO_free_all(out);
329     OPENSSL_free(passin);
330     release_engine(e);
331     return ret;
332 }
333
334 static int indent_printf(int indent, BIO *bio, const char *format, ...)
335 {
336     va_list args;
337     int ret;
338
339     va_start(args, format);
340
341     ret = BIO_printf(bio, "%*s", indent, "") + BIO_vprintf(bio, format, args);
342
343     va_end(args);
344     return ret;
345 }
346
347 static int process(const char *uri, const UI_METHOD *uimeth, PW_CB_DATA *uidata,
348                    int expected, int criterion, OSSL_STORE_SEARCH *search,
349                    int text, int noout, int recursive, int indent, BIO *out,
350                    const char *prog)
351 {
352     OSSL_STORE_CTX *store_ctx = NULL;
353     int ret = 1, items = 0;
354
355     if ((store_ctx = OSSL_STORE_open(uri, uimeth, uidata, NULL, NULL))
356         == NULL) {
357         BIO_printf(bio_err, "Couldn't open file or uri %s\n", uri);
358         ERR_print_errors(bio_err);
359         return ret;
360     }
361
362     if (expected != 0) {
363         if (!OSSL_STORE_expect(store_ctx, expected)) {
364             ERR_print_errors(bio_err);
365             goto end2;
366         }
367     }
368
369     if (criterion != 0) {
370         if (!OSSL_STORE_supports_search(store_ctx, criterion)) {
371             BIO_printf(bio_err,
372                        "%s: the store scheme doesn't support the given search criteria.\n",
373                        prog);
374             goto end2;
375         }
376
377         if (!OSSL_STORE_find(store_ctx, search)) {
378             ERR_print_errors(bio_err);
379             goto end2;
380         }
381     }
382
383     /* From here on, we count errors, and we'll return the count at the end */
384     ret = 0;
385
386     for (;;) {
387         OSSL_STORE_INFO *info = OSSL_STORE_load(store_ctx);
388         int type = info == NULL ? 0 : OSSL_STORE_INFO_get_type(info);
389         const char *infostr =
390             info == NULL ? NULL : OSSL_STORE_INFO_type_string(type);
391
392         if (info == NULL) {
393             if (OSSL_STORE_eof(store_ctx))
394                 break;
395
396             if (OSSL_STORE_error(store_ctx)) {
397                 if (recursive)
398                     ERR_clear_error();
399                 else
400                     ERR_print_errors(bio_err);
401                 ret++;
402                 continue;
403             }
404
405             BIO_printf(bio_err,
406                        "ERROR: OSSL_STORE_load() returned NULL without "
407                        "eof or error indications\n");
408             BIO_printf(bio_err, "       This is an error in the loader\n");
409             ERR_print_errors(bio_err);
410             ret++;
411             break;
412         }
413
414         if (type == OSSL_STORE_INFO_NAME) {
415             const char *name = OSSL_STORE_INFO_get0_NAME(info);
416             const char *desc = OSSL_STORE_INFO_get0_NAME_description(info);
417             indent_printf(indent, bio_out, "%d: %s: %s\n", items, infostr,
418                           name);
419             if (desc != NULL)
420                 indent_printf(indent, bio_out, "%s\n", desc);
421         } else {
422             indent_printf(indent, bio_out, "%d: %s\n", items, infostr);
423         }
424
425         /*
426          * Unfortunately, PEM_X509_INFO_write_bio() is sorely lacking in
427          * functionality, so we must figure out how exactly to write things
428          * ourselves...
429          */
430         switch (type) {
431         case OSSL_STORE_INFO_NAME:
432             if (recursive) {
433                 const char *suburi = OSSL_STORE_INFO_get0_NAME(info);
434                 ret += process(suburi, uimeth, uidata,
435                                expected, criterion, search,
436                                text, noout, recursive, indent + 2, out, prog);
437             }
438             break;
439         case OSSL_STORE_INFO_PARAMS:
440             if (text)
441                 EVP_PKEY_print_params(out, OSSL_STORE_INFO_get0_PARAMS(info),
442                                       0, NULL);
443             if (!noout)
444                 PEM_write_bio_Parameters(out,
445                                          OSSL_STORE_INFO_get0_PARAMS(info));
446             break;
447         case OSSL_STORE_INFO_PKEY:
448             if (text)
449                 EVP_PKEY_print_private(out, OSSL_STORE_INFO_get0_PKEY(info),
450                                        0, NULL);
451             if (!noout)
452                 PEM_write_bio_PrivateKey(out, OSSL_STORE_INFO_get0_PKEY(info),
453                                          NULL, NULL, 0, NULL, NULL);
454             break;
455         case OSSL_STORE_INFO_CERT:
456             if (text)
457                 X509_print(out, OSSL_STORE_INFO_get0_CERT(info));
458             if (!noout)
459                 PEM_write_bio_X509(out, OSSL_STORE_INFO_get0_CERT(info));
460             break;
461         case OSSL_STORE_INFO_CRL:
462             if (text)
463                 X509_CRL_print(out, OSSL_STORE_INFO_get0_CRL(info));
464             if (!noout)
465                 PEM_write_bio_X509_CRL(out, OSSL_STORE_INFO_get0_CRL(info));
466             break;
467         default:
468             BIO_printf(bio_err, "!!! Unknown code\n");
469             ret++;
470             break;
471         }
472         items++;
473         OSSL_STORE_INFO_free(info);
474     }
475     indent_printf(indent, out, "Total found: %d\n", items);
476
477  end2:
478     if (!OSSL_STORE_close(store_ctx)) {
479         ERR_print_errors(bio_err);
480         ret++;
481     }
482
483     return ret;
484 }