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