]> git.ipfire.org Git - thirdparty/openssl.git/blame - apps/provider.c
Add test for non-default sized keys in variable key size ciphers
[thirdparty/openssl.git] / apps / provider.c
CommitLineData
18d307e9
RL
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"
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>
21#include <openssl/core_numbers.h>
22
23typedef enum OPTION_choice {
24 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
25 OPT_V = 100, OPT_VV, OPT_VVV
26} OPTION_CHOICE;
27
28const OPTIONS provider_options[] = {
29 {OPT_HELP_STR, 1, '-', "Usage: %s [options] provider...\n"},
30 {OPT_HELP_STR, 1, '-', " provider... Providers to load\n"},
5388f986
RS
31
32 OPT_SECTION("General"),
18d307e9 33 {"help", OPT_HELP, '-', "Display this summary"},
5388f986
RS
34
35 OPT_SECTION("Output"),
18d307e9
RL
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
44typedef struct info_st INFO;
45typedef struct meta_st META;
46
47struct info_st {
031873fe 48 void (*collect_names_fn)(void *method, STACK_OF(OPENSSL_CSTRING) *names);
18d307e9
RL
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
55struct 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
031873fe
RL
66static 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
72static 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
78static 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
84static 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
90static 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
96static 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
102static 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
18d307e9
RL
111static void print_caps(META *meta, INFO *info)
112{
113 switch (meta->verbose) {
114 case 1:
031873fe
RL
115 if (!meta->first)
116 BIO_printf(bio_out, "; ");
117 print_method_names(bio_out, info);
18d307e9
RL
118 break;
119 case 2:
120 if (meta->first) {
121 if (meta->total > 0)
122 BIO_printf(bio_out, "\n");
c92d0c5c 123 BIO_printf(bio_out, "%*s%ss:", meta->indent, "", meta->label);
18d307e9 124 }
031873fe
RL
125 BIO_printf(bio_out, " ");
126 print_method_names(bio_out, info);
18d307e9
RL
127 break;
128 case 3:
129 default:
031873fe
RL
130 BIO_printf(bio_out, "%*s%s ", meta->indent, "", meta->label);
131 print_method_names(bio_out, info);
132 BIO_printf(bio_out, "\n");
18d307e9
RL
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
031873fe
RL
144static void do_method(void *method,
145 void (*collect_names_fn)(void *method,
146 STACK_OF(OPENSSL_CSTRING) *names),
18d307e9
RL
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
031873fe 154 info.collect_names_fn = collect_names_fn;
18d307e9
RL
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
163static void do_cipher(EVP_CIPHER *cipher, void *meta)
164{
031873fe 165 do_method(cipher, collect_cipher_names,
18d307e9 166 EVP_CIPHER_gettable_params(cipher),
41f7ecf3
P
167 EVP_CIPHER_gettable_ctx_params(cipher),
168 EVP_CIPHER_settable_ctx_params(cipher),
18d307e9
RL
169 meta);
170}
171
172static void do_digest(EVP_MD *digest, void *meta)
173{
031873fe 174 do_method(digest, collect_digest_names,
18d307e9 175 EVP_MD_gettable_params(digest),
e6879a31
MC
176 EVP_MD_gettable_ctx_params(digest),
177 EVP_MD_settable_ctx_params(digest),
18d307e9
RL
178 meta);
179}
180
181static void do_mac(EVP_MAC *mac, void *meta)
182{
031873fe 183 do_method(mac, collect_mac_names,
18d307e9 184 EVP_MAC_gettable_params(mac),
41f7ecf3
P
185 EVP_MAC_gettable_ctx_params(mac),
186 EVP_MAC_settable_ctx_params(mac),
18d307e9
RL
187 meta);
188}
189
031873fe
RL
190static void do_keymgmt(EVP_KEYMGMT *keymgmt, void *meta)
191{
192 do_method(keymgmt, collect_keymgmt_names,
18d307e9
RL
193/*
194 * TODO(3.0) Enable when KEYMGMT and KEYEXCH have gettables and settables
195 */
196#if 0
18d307e9
RL
197 EVP_KEYMGMT_gettable_params(keymgmt),
198 EVP_KEYMGMT_gettable_ctx_params(keymgmt),
199 EVP_KEYMGMT_settable_ctx_params(keymgmt),
031873fe
RL
200#else
201 NULL, NULL, NULL,
202#endif
18d307e9
RL
203 meta);
204}
205
206static void do_keyexch(EVP_KEYEXCH *keyexch, void *meta)
207{
031873fe
RL
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
18d307e9
RL
213 EVP_KEYEXCH_gettable_params(keyexch),
214 EVP_KEYEXCH_gettable_ctx_params(keyexch),
215 EVP_KEYEXCH_settable_ctx_params(keyexch),
031873fe
RL
216#else
217 NULL, NULL, NULL,
218#endif
18d307e9
RL
219 meta);
220}
031873fe
RL
221
222static 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,
18d307e9 234#endif
031873fe
RL
235 meta);
236}
18d307e9
RL
237
238int 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) {
7964e370 249 default: /* Catching OPT_ERR & covering OPT_EOF which isn't possible */
18d307e9
RL
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 }
031873fe 315 EVP_CIPHER_do_all_provided(NULL, do_cipher, &data);
18d307e9
RL
316 if (verbose > 1) {
317 data.first = 1;
318 data.label = "Digest";
319 }
031873fe 320 EVP_MD_do_all_provided(NULL, do_digest, &data);
18d307e9
RL
321 if (verbose > 1) {
322 data.first = 1;
323 data.label = "MAC";
324 }
031873fe 325 EVP_MAC_do_all_provided(NULL, do_mac, &data);
18d307e9 326
18d307e9
RL
327 if (verbose > 1) {
328 data.first = 1;
329 data.label = "Key manager";
330 }
031873fe 331 EVP_KEYMGMT_do_all_provided(NULL, do_keymgmt, &data);
18d307e9
RL
332 if (verbose > 1) {
333 data.first = 1;
334 data.label = "Key exchange";
335 }
031873fe
RL
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);
18d307e9
RL
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}