33f194c33226041ef9ddbc9ff9dadae164bb3700
[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 <openssl/err.h>
16 #include <openssl/evp.h>
17 #include <openssl/safestack.h>
18 #include <openssl/provider.h>
19 #include <openssl/core.h>
20 #include <openssl/core_numbers.h>
21
22 typedef enum OPTION_choice {
23     OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
24     OPT_V = 100, OPT_VV, OPT_VVV
25 } OPTION_CHOICE;
26
27 const OPTIONS provider_options[] = {
28     {OPT_HELP_STR, 1, '-', "Usage: %s [options] provider...\n"},
29     {OPT_HELP_STR, 1, '-', "  provider... Providers to load\n"},
30     {"help", OPT_HELP, '-', "Display this summary"},
31     {"v", OPT_V, '-', "List the algorithm names of specified provider"},
32     {"vv", OPT_VV, '-', "List the algorithm names of specified providers,"},
33     {OPT_MORE_STR, 0, '-', "categorised by operation type"},
34     {"vvv", OPT_VVV, '-', "List the algorithm names of specified provider"},
35     {OPT_MORE_STR, 0, '-', "one at a time, and list all known parameters"},
36     {NULL}
37 };
38
39 typedef struct info_st INFO;
40 typedef struct meta_st META;
41
42 struct info_st {
43     const char *name;
44     void *method;
45     const OSSL_PARAM *gettable_params;
46     const OSSL_PARAM *gettable_ctx_params;
47     const OSSL_PARAM *settable_ctx_params;
48 };
49
50 struct meta_st {
51     int first;                   /* For prints */
52     int total;
53     int indent;
54     int subindent;
55     int verbose;
56     const char *label;
57     OSSL_PROVIDER *prov;
58     void (*fn)(META *meta, INFO *info);
59 };
60
61 static void print_caps(META *meta, INFO *info)
62 {
63     switch (meta->verbose) {
64     case 1:
65         BIO_printf(bio_out, meta->first ? "%s" : " %s", info->name);
66         break;
67     case 2:
68         if (meta->first) {
69             if (meta->total > 0)
70                 BIO_printf(bio_out, "\n");
71             BIO_printf(bio_out, "%*s%ss:", meta->indent, " ", meta->label);
72         }
73         BIO_printf(bio_out, " %s", info->name);
74         break;
75     case 3:
76     default:
77         BIO_printf(bio_out, "%*s%s %s\n", meta->indent, " ", meta->label,
78                    info->name);
79         print_param_types("retrievable algorithm parameters",
80                           info->gettable_params, meta->subindent);
81         print_param_types("retrievable operation parameters",
82                           info->gettable_ctx_params, meta->subindent);
83         print_param_types("settable operation parameters",
84                           info->settable_ctx_params, meta->subindent);
85         break;
86     }
87     meta->first = 0;
88 }
89
90 static void do_method(void *method, const char *name,
91                       const OSSL_PARAM *gettable_params,
92                       const OSSL_PARAM *gettable_ctx_params,
93                       const OSSL_PARAM *settable_ctx_params,
94                       META *meta)
95 {
96     INFO info;
97
98     info.name = name;
99     info.method = method;
100     info.gettable_params = gettable_params;
101     info.gettable_ctx_params = gettable_ctx_params;
102     info.settable_ctx_params = settable_ctx_params;
103     meta->fn(meta, &info);
104     meta->total++;
105 }
106
107 static void do_cipher(EVP_CIPHER *cipher, void *meta)
108 {
109     do_method(cipher, EVP_CIPHER_name(cipher),
110               EVP_CIPHER_gettable_params(cipher),
111               EVP_CIPHER_CTX_gettable_params(cipher),
112               EVP_CIPHER_CTX_settable_params(cipher),
113               meta);
114 }
115
116 static void do_digest(EVP_MD *digest, void *meta)
117 {
118     do_method(digest, EVP_MD_name(digest),
119               EVP_MD_gettable_params(digest),
120               EVP_MD_CTX_gettable_params(digest),
121               EVP_MD_CTX_settable_params(digest),
122               meta);
123 }
124
125 static void do_mac(EVP_MAC *mac, void *meta)
126 {
127     do_method(mac, EVP_MAC_name(mac),
128               EVP_MAC_gettable_params(mac),
129               EVP_MAC_CTX_gettable_params(mac),
130               EVP_MAC_CTX_settable_params(mac),
131               meta);
132 }
133
134 /*
135  * TODO(3.0) Enable when KEYMGMT and KEYEXCH have gettables and settables
136  */
137 #if 0
138 static void do_keymgmt(EVP_KEYMGMT *keymgmt, void *meta)
139 {
140     do_method(keymgmt, EVP_KEYMGMT_name(keymgmt),
141               EVP_KEYMGMT_gettable_params(keymgmt),
142               EVP_KEYMGMT_gettable_ctx_params(keymgmt),
143               EVP_KEYMGMT_settable_ctx_params(keymgmt),
144               meta);
145 }
146
147 static void do_keyexch(EVP_KEYEXCH *keyexch, void *meta)
148 {
149     do_method(keyexch, EVP_KEYEXCH_name(keyexch),
150               EVP_KEYEXCH_gettable_params(keyexch),
151               EVP_KEYEXCH_gettable_ctx_params(keyexch),
152               EVP_KEYEXCH_settable_ctx_params(keyexch),
153               meta);
154 }
155 #endif
156
157 int provider_main(int argc, char **argv)
158 {
159     int ret = 1, i;
160     int verbose = 0;
161     STACK_OF(OPENSSL_CSTRING) *providers = sk_OPENSSL_CSTRING_new_null();
162     OPTION_CHOICE o;
163     char *prog;
164
165     prog = opt_init(argc, argv, provider_options);
166     while ((o = opt_next()) != OPT_EOF) {
167         switch (o) {
168         case OPT_EOF:
169         case OPT_ERR:
170             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
171             goto end;
172         case OPT_HELP:
173             opt_help(provider_options);
174             ret = 0;
175             goto end;
176         case OPT_VVV:
177         case OPT_VV:
178         case OPT_V:
179             /* Convert to an integer from one to four. */
180             i = (int)(o - OPT_V) + 1;
181             if (verbose < i)
182                 verbose = i;
183             break;
184         }
185     }
186
187     /* Allow any trailing parameters as provider names. */
188     argc = opt_num_rest();
189     argv = opt_rest();
190     for ( ; *argv; argv++) {
191         if (**argv == '-') {
192             BIO_printf(bio_err, "%s: Cannot mix flags and provider names.\n",
193                        prog);
194             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
195             goto end;
196         }
197         sk_OPENSSL_CSTRING_push(providers, *argv);
198     }
199
200     ret = 0;
201     for (i = 0; i < sk_OPENSSL_CSTRING_num(providers); i++) {
202         const char *name = sk_OPENSSL_CSTRING_value(providers, i);
203         OSSL_PROVIDER *prov = OSSL_PROVIDER_load(NULL, name);
204
205         if (prov != NULL) {
206             BIO_printf(bio_out, verbose == 0 ? "%s\n" :  "[ %s ]\n", name);
207
208             if (verbose > 0) {
209                 META data;
210
211                 data.total = 0;
212                 data.first = 1;
213                 data.verbose = verbose;
214                 data.prov = prov;
215                 data.fn = print_caps;
216
217                 switch (verbose) {
218                 case 1:
219                     BIO_printf(bio_out, "    ");
220                     break;
221                 case 2:
222                     data.indent = 4;
223                     break;
224                 case 3:
225                 default:
226                     data.indent = 4;
227                     data.subindent = 10;
228                     break;
229                 }
230
231                 if (verbose > 1) {
232                     data.first = 1;
233                     data.label = "Cipher";
234                 }
235                 EVP_CIPHER_do_all_ex(NULL, do_cipher, &data);
236                 if (verbose > 1) {
237                     data.first = 1;
238                     data.label = "Digest";
239                 }
240                 EVP_MD_do_all_ex(NULL, do_digest, &data);
241                 if (verbose > 1) {
242                     data.first = 1;
243                     data.label = "MAC";
244                 }
245                 EVP_MAC_do_all_ex(NULL, do_mac, &data);
246
247 /*
248  * TODO(3.0) Enable when KEYMGMT and KEYEXCH have do_all_ex functions
249  */
250 #if 0
251                 if (verbose > 1) {
252                     data.first = 1;
253                     data.label = "Key manager";
254                 }
255                 EVP_KEYMGMT_do_all_ex(NULL, do_keymgmt, &data);
256                 if (verbose > 1) {
257                     data.first = 1;
258                     data.label = "Key exchange";
259                 }
260                 EVP_KEYEXCH_do_all_ex(NULL, do_keyexch, &data);
261 #endif
262
263                 switch (verbose) {
264                 default:
265                     break;
266                 case 2:
267                 case 1:
268                     BIO_printf(bio_out, "\n");
269                     break;
270                 }
271             }
272             OSSL_PROVIDER_unload(prov);
273         } else {
274             ERR_print_errors(bio_err);
275             ret = 1;
276             /*
277              * Just because one provider module failed, there's no reason to
278              * stop, if there are more to try.
279              */
280         }
281     }
282
283  end:
284
285     ERR_print_errors(bio_err);
286     sk_OPENSSL_CSTRING_free(providers);
287     return ret;
288 }