DOC: Modify one example in EVP_PKEY_fromdata(3)
[openssl.git] / apps / provider.c
1 /*
2  * Copyright 2019-2020 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 "app_params.h"
14 #include "progs.h"
15 #include "names.h"
16 #include <openssl/err.h>
17 #include <openssl/evp.h>
18 #include <openssl/safestack.h>
19 #include <openssl/provider.h>
20 #include <openssl/core.h>
21 #include <openssl/core_dispatch.h>
22
23 DEFINE_STACK_OF_CSTRING()
24
25 typedef enum OPTION_choice {
26     OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
27     OPT_V = 100, OPT_VV, OPT_VVV
28 } OPTION_CHOICE;
29
30 const OPTIONS provider_options[] = {
31     {OPT_HELP_STR, 1, '-', "Usage: %s [options] [provider...]\n"},
32
33     OPT_SECTION("General"),
34     {"help", OPT_HELP, '-', "Display this summary"},
35
36     OPT_SECTION("Output"),
37     {"v", OPT_V, '-', "List the algorithm names of specified provider"},
38     {"vv", OPT_VV, '-', "List the algorithm names of specified providers,"},
39     {OPT_MORE_STR, 0, '-', "categorised by operation type"},
40     {"vvv", OPT_VVV, '-', "List the algorithm names of specified provider"},
41     {OPT_MORE_STR, 0, '-', "one at a time, and list all known parameters"},
42
43     OPT_PARAMETERS(),
44     {"provider", 0, 0, "Provider(s) to load"},
45     {NULL}
46 };
47
48 typedef struct info_st INFO;
49 typedef struct meta_st META;
50
51 struct info_st {
52     void (*collect_names_fn)(void *method, STACK_OF(OPENSSL_CSTRING) *names);
53     void *method;
54     const OSSL_PARAM *gettable_params;
55     const OSSL_PARAM *settable_params;
56     const OSSL_PARAM *gettable_ctx_params;
57     const OSSL_PARAM *settable_ctx_params;
58     const OSSL_PARAM *gen_settable_params;
59 };
60
61 struct meta_st {
62     int first;                   /* For prints */
63     int total;
64     int indent;
65     int subindent;
66     int verbose;
67     const char *label;
68     OSSL_PROVIDER *prov;
69     void (*fn)(META *meta, INFO *info);
70 };
71
72 static void collect_cipher_names(void *method,
73                                  STACK_OF(OPENSSL_CSTRING) *names)
74 {
75     EVP_CIPHER_names_do_all(method, collect_names, names);
76 }
77
78 static void collect_digest_names(void *method,
79                                  STACK_OF(OPENSSL_CSTRING) *names)
80 {
81     EVP_MD_names_do_all(method, collect_names, names);
82 }
83
84 static void collect_mac_names(void *method,
85                               STACK_OF(OPENSSL_CSTRING) *names)
86 {
87     EVP_MAC_names_do_all(method, collect_names, names);
88 }
89
90 static void collect_keymgmt_names(void *method,
91                                   STACK_OF(OPENSSL_CSTRING) *names)
92 {
93     EVP_KEYMGMT_names_do_all(method, collect_names, names);
94 }
95
96 static void collect_keyexch_names(void *method,
97                                   STACK_OF(OPENSSL_CSTRING) *names)
98 {
99     EVP_KEYEXCH_names_do_all(method, collect_names, names);
100 }
101
102 static void collect_signature_names(void *method,
103                                   STACK_OF(OPENSSL_CSTRING) *names)
104 {
105     EVP_SIGNATURE_names_do_all(method, collect_names, names);
106 }
107
108 static void print_method_names(BIO *out, INFO *info)
109 {
110     STACK_OF(OPENSSL_CSTRING) *names = sk_OPENSSL_CSTRING_new(name_cmp);
111
112     info->collect_names_fn(info->method, names);
113     print_names(out, names);
114     sk_OPENSSL_CSTRING_free(names);
115 }
116
117 static void print_caps(META *meta, INFO *info)
118 {
119     switch (meta->verbose) {
120     case 1:
121         if (!meta->first)
122             BIO_printf(bio_out, "; ");
123         print_method_names(bio_out, info);
124         break;
125     case 2:
126         if (meta->first) {
127             if (meta->total > 0)
128                 BIO_printf(bio_out, "\n");
129             BIO_printf(bio_out, "%*s%ss:", meta->indent, "", meta->label);
130         }
131         BIO_printf(bio_out, " ");
132         print_method_names(bio_out, info);
133         break;
134     case 3:
135     default:
136         BIO_printf(bio_out, "%*s%s ", meta->indent, "", meta->label);
137         print_method_names(bio_out, info);
138         BIO_printf(bio_out, "\n");
139         print_param_types("settable keygen parameters",
140                           info->gen_settable_params, meta->subindent);
141         print_param_types("settable algorithm parameters",
142                           info->settable_params, meta->subindent);
143         print_param_types("retrievable algorithm parameters",
144                           info->gettable_params, meta->subindent);
145         print_param_types("settable operation parameters",
146                           info->settable_ctx_params, meta->subindent);
147         print_param_types("retrievable operation parameters",
148                           info->gettable_ctx_params, meta->subindent);
149         break;
150     }
151     meta->first = 0;
152 }
153
154 static void do_method(void *method,
155                       void (*collect_names_fn)(void *method,
156                                                STACK_OF(OPENSSL_CSTRING) *names),
157                       const OSSL_PARAM *gettable_params,
158                       const OSSL_PARAM *gettable_ctx_params,
159                       const OSSL_PARAM *settable_ctx_params,
160                       META *meta)
161 {
162     INFO info;
163
164     memset(&info, 0, sizeof(info));
165     info.collect_names_fn = collect_names_fn;
166     info.method = method;
167     info.gettable_params = gettable_params;
168     info.gettable_ctx_params = gettable_ctx_params;
169     info.settable_ctx_params = settable_ctx_params;
170     meta->fn(meta, &info);
171     meta->total++;
172 }
173
174 static void do_keymgmt_method(void *method,
175                               void (*collect_names_fn)(void *method,
176                                                        STACK_OF(OPENSSL_CSTRING)
177                                                        *names),
178                               const OSSL_PARAM *gettable_params,
179                               const OSSL_PARAM *settable_params,
180                               const OSSL_PARAM *gen_settable_params,
181                               META *meta)
182 {
183     INFO info;
184
185     memset(&info, 0, sizeof(info));
186     info.collect_names_fn = collect_names_fn;
187     info.method = method;
188     info.gettable_params = gettable_params;
189     info.settable_params = settable_params;
190     info.gen_settable_params = gen_settable_params;
191     meta->fn(meta, &info);
192     meta->total++;
193 }
194
195 static void do_cipher(EVP_CIPHER *cipher, void *meta)
196 {
197     do_method(cipher, collect_cipher_names,
198               EVP_CIPHER_gettable_params(cipher),
199               EVP_CIPHER_gettable_ctx_params(cipher),
200               EVP_CIPHER_settable_ctx_params(cipher),
201               meta);
202 }
203
204 static void do_digest(EVP_MD *digest, void *meta)
205 {
206     do_method(digest, collect_digest_names,
207               EVP_MD_gettable_params(digest),
208               EVP_MD_gettable_ctx_params(digest),
209               EVP_MD_settable_ctx_params(digest),
210               meta);
211 }
212
213 static void do_mac(EVP_MAC *mac, void *meta)
214 {
215     do_method(mac, collect_mac_names,
216               EVP_MAC_gettable_params(mac),
217               EVP_MAC_gettable_ctx_params(mac),
218               EVP_MAC_settable_ctx_params(mac),
219               meta);
220 }
221
222 static void do_keymgmt(EVP_KEYMGMT *keymgmt, void *meta)
223 {
224     do_keymgmt_method(keymgmt, collect_keymgmt_names,
225                       EVP_KEYMGMT_gettable_params(keymgmt),
226                       EVP_KEYMGMT_settable_params(keymgmt),
227                       EVP_KEYMGMT_gen_settable_params(keymgmt),
228                       meta);
229 }
230
231 static void do_keyexch(EVP_KEYEXCH *keyexch, void *meta)
232 {
233     do_method(keyexch, collect_keyexch_names,
234               NULL,
235               EVP_KEYEXCH_gettable_ctx_params(keyexch),
236               EVP_KEYEXCH_settable_ctx_params(keyexch),
237               meta);
238 }
239
240 static void do_signature(EVP_SIGNATURE *signature, void *meta)
241 {
242     do_method(signature, collect_signature_names,
243               NULL,
244               EVP_SIGNATURE_gettable_ctx_params(signature),
245               EVP_SIGNATURE_settable_ctx_params(signature),
246               meta);
247 }
248
249 int provider_main(int argc, char **argv)
250 {
251     int ret = 1, i;
252     int verbose = 0;
253     STACK_OF(OPENSSL_CSTRING) *providers = sk_OPENSSL_CSTRING_new_null();
254     OPTION_CHOICE o;
255     char *prog;
256
257     prog = opt_init(argc, argv, provider_options);
258     while ((o = opt_next()) != OPT_EOF) {
259         switch (o) {
260         default: /* Catching OPT_ERR & covering OPT_EOF which isn't possible */
261             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
262             goto end;
263         case OPT_HELP:
264             opt_help(provider_options);
265             ret = 0;
266             goto end;
267         case OPT_VVV:
268         case OPT_VV:
269         case OPT_V:
270             /* Convert to an integer from one to four. */
271             i = (int)(o - OPT_V) + 1;
272             if (verbose < i)
273                 verbose = i;
274             break;
275         }
276     }
277
278     /* Allow any trailing parameters as provider names. */
279     argc = opt_num_rest();
280     argv = opt_rest();
281     for ( ; *argv; argv++) {
282         /* This isn't necessary since -- is supported. */
283         if (**argv == '-') {
284             BIO_printf(bio_err, "%s: Cannot mix flags and provider names.\n",
285                        prog);
286             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
287             goto end;
288         }
289         sk_OPENSSL_CSTRING_push(providers, *argv);
290     }
291
292     ret = 0;
293     for (i = 0; i < sk_OPENSSL_CSTRING_num(providers); i++) {
294         const char *name = sk_OPENSSL_CSTRING_value(providers, i);
295         OSSL_PROVIDER *prov = OSSL_PROVIDER_load(NULL, name);
296
297         if (prov != NULL) {
298             BIO_printf(bio_out, verbose == 0 ? "%s\n" :  "[ %s ]\n", name);
299
300             if (verbose > 0) {
301                 META data;
302
303                 data.total = 0;
304                 data.first = 1;
305                 data.verbose = verbose;
306                 data.prov = prov;
307                 data.fn = print_caps;
308
309                 switch (verbose) {
310                 case 1:
311                     BIO_printf(bio_out, "    ");
312                     break;
313                 case 2:
314                     data.indent = 4;
315                     break;
316                 case 3:
317                 default:
318                     data.indent = 4;
319                     data.subindent = 10;
320                     break;
321                 }
322
323                 if (verbose > 1) {
324                     data.first = 1;
325                     data.label = "Cipher";
326                 }
327                 EVP_CIPHER_do_all_provided(NULL, do_cipher, &data);
328                 if (verbose > 1) {
329                     data.first = 1;
330                     data.label = "Digest";
331                 }
332                 EVP_MD_do_all_provided(NULL, do_digest, &data);
333                 if (verbose > 1) {
334                     data.first = 1;
335                     data.label = "MAC";
336                 }
337                 EVP_MAC_do_all_provided(NULL, do_mac, &data);
338
339                 if (verbose > 1) {
340                     data.first = 1;
341                     data.label = "Key manager";
342                 }
343                 EVP_KEYMGMT_do_all_provided(NULL, do_keymgmt, &data);
344                 if (verbose > 1) {
345                     data.first = 1;
346                     data.label = "Key exchange";
347                 }
348                 EVP_KEYEXCH_do_all_provided(NULL, do_keyexch, &data);
349                 if (verbose > 1) {
350                     data.first = 1;
351                     data.label = "Signature";
352                 }
353                 EVP_SIGNATURE_do_all_provided(NULL, do_signature, &data);
354
355                 switch (verbose) {
356                 default:
357                     break;
358                 case 2:
359                 case 1:
360                     BIO_printf(bio_out, "\n");
361                     break;
362                 }
363             }
364             OSSL_PROVIDER_unload(prov);
365         } else {
366             ERR_print_errors(bio_err);
367             ret = 1;
368             /*
369              * Just because one provider module failed, there's no reason to
370              * stop, if there are more to try.
371              */
372         }
373     }
374
375  end:
376
377     ERR_print_errors(bio_err);
378     sk_OPENSSL_CSTRING_free(providers);
379     return ret;
380 }