]> git.ipfire.org Git - thirdparty/openssl.git/blob - apps/provider.c
Fix stacks of OPENSSL_STRING, OPENSSL_CSTRING and OPENSSL_BLOCK
[thirdparty/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 #ifdef __TANDEM
24 # include <string.h> /* memset */
25 #endif
26
27 typedef enum OPTION_choice {
28 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
29 OPT_V = 100, OPT_VV, OPT_VVV
30 } OPTION_CHOICE;
31
32 const OPTIONS provider_options[] = {
33 {OPT_HELP_STR, 1, '-', "Usage: %s [options] [provider...]\n"},
34
35 OPT_SECTION("General"),
36 {"help", OPT_HELP, '-', "Display this summary"},
37
38 OPT_SECTION("Output"),
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"},
44
45 OPT_PARAMETERS(),
46 {"provider", 0, 0, "Provider(s) to load"},
47 {NULL}
48 };
49
50 typedef struct info_st INFO;
51 typedef struct meta_st META;
52
53 struct info_st {
54 void (*collect_names_fn)(void *method, STACK_OF(OPENSSL_CSTRING) *names);
55 void *method;
56 const OSSL_PARAM *gettable_params;
57 const OSSL_PARAM *settable_params;
58 const OSSL_PARAM *gettable_ctx_params;
59 const OSSL_PARAM *settable_ctx_params;
60 const OSSL_PARAM *gen_settable_params;
61 };
62
63 struct 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
74 static 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
80 static 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
86 static 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
92 static 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
98 static 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
104 static 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
110 static 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
119 static void print_caps(META *meta, INFO *info)
120 {
121 switch (meta->verbose) {
122 case 1:
123 if (!meta->first)
124 BIO_printf(bio_out, "; ");
125 print_method_names(bio_out, info);
126 break;
127 case 2:
128 if (meta->first) {
129 if (meta->total > 0)
130 BIO_printf(bio_out, "\n");
131 BIO_printf(bio_out, "%*s%ss:", meta->indent, "", meta->label);
132 }
133 BIO_printf(bio_out, " ");
134 print_method_names(bio_out, info);
135 break;
136 case 3:
137 default:
138 BIO_printf(bio_out, "%*s%s ", meta->indent, "", meta->label);
139 print_method_names(bio_out, info);
140 BIO_printf(bio_out, "\n");
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);
145 print_param_types("retrievable algorithm parameters",
146 info->gettable_params, meta->subindent);
147 print_param_types("settable operation parameters",
148 info->settable_ctx_params, meta->subindent);
149 print_param_types("retrievable operation parameters",
150 info->gettable_ctx_params, meta->subindent);
151 break;
152 }
153 meta->first = 0;
154 }
155
156 static void do_method(void *method,
157 void (*collect_names_fn)(void *method,
158 STACK_OF(OPENSSL_CSTRING) *names),
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
166 memset(&info, 0, sizeof(info));
167 info.collect_names_fn = collect_names_fn;
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
176 static 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
197 static void do_cipher(EVP_CIPHER *cipher, void *meta)
198 {
199 do_method(cipher, collect_cipher_names,
200 EVP_CIPHER_gettable_params(cipher),
201 EVP_CIPHER_gettable_ctx_params(cipher),
202 EVP_CIPHER_settable_ctx_params(cipher),
203 meta);
204 }
205
206 static void do_digest(EVP_MD *digest, void *meta)
207 {
208 do_method(digest, collect_digest_names,
209 EVP_MD_gettable_params(digest),
210 EVP_MD_gettable_ctx_params(digest),
211 EVP_MD_settable_ctx_params(digest),
212 meta);
213 }
214
215 static void do_mac(EVP_MAC *mac, void *meta)
216 {
217 do_method(mac, collect_mac_names,
218 EVP_MAC_gettable_params(mac),
219 EVP_MAC_gettable_ctx_params(mac),
220 EVP_MAC_settable_ctx_params(mac),
221 meta);
222 }
223
224 static void do_keymgmt(EVP_KEYMGMT *keymgmt, void *meta)
225 {
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);
231 }
232
233 static void do_keyexch(EVP_KEYEXCH *keyexch, void *meta)
234 {
235 do_method(keyexch, collect_keyexch_names,
236 NULL,
237 EVP_KEYEXCH_gettable_ctx_params(keyexch),
238 EVP_KEYEXCH_settable_ctx_params(keyexch),
239 meta);
240 }
241
242 static void do_signature(EVP_SIGNATURE *signature, void *meta)
243 {
244 do_method(signature, collect_signature_names,
245 NULL,
246 EVP_SIGNATURE_gettable_ctx_params(signature),
247 EVP_SIGNATURE_settable_ctx_params(signature),
248 meta);
249 }
250
251 int 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) {
262 default: /* Catching OPT_ERR & covering OPT_EOF which isn't possible */
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++) {
284 /* This isn't necessary since -- is supported. */
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 }
329 EVP_CIPHER_do_all_provided(NULL, do_cipher, &data);
330 if (verbose > 1) {
331 data.first = 1;
332 data.label = "Digest";
333 }
334 EVP_MD_do_all_provided(NULL, do_digest, &data);
335 if (verbose > 1) {
336 data.first = 1;
337 data.label = "MAC";
338 }
339 EVP_MAC_do_all_provided(NULL, do_mac, &data);
340
341 if (verbose > 1) {
342 data.first = 1;
343 data.label = "Key manager";
344 }
345 EVP_KEYMGMT_do_all_provided(NULL, do_keymgmt, &data);
346 if (verbose > 1) {
347 data.first = 1;
348 data.label = "Key exchange";
349 }
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);
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 }