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