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