2 * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
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
10 #include <openssl/opensslconf.h>
13 #include "app_params.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>
23 typedef enum OPTION_choice {
24 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
25 OPT_V = 100, OPT_VV, OPT_VVV
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 {"help", OPT_HELP, '-', "Display this summary"},
32 {"v", OPT_V, '-', "List the algorithm names of specified provider"},
33 {"vv", OPT_VV, '-', "List the algorithm names of specified providers,"},
34 {OPT_MORE_STR, 0, '-', "categorised by operation type"},
35 {"vvv", OPT_VVV, '-', "List the algorithm names of specified provider"},
36 {OPT_MORE_STR, 0, '-', "one at a time, and list all known parameters"},
40 typedef struct info_st INFO;
41 typedef struct meta_st META;
44 void (*collect_names_fn)(void *method, STACK_OF(OPENSSL_CSTRING) *names);
46 const OSSL_PARAM *gettable_params;
47 const OSSL_PARAM *gettable_ctx_params;
48 const OSSL_PARAM *settable_ctx_params;
52 int first; /* For prints */
59 void (*fn)(META *meta, INFO *info);
62 static void collect_cipher_names(void *method,
63 STACK_OF(OPENSSL_CSTRING) *names)
65 EVP_CIPHER_names_do_all(method, collect_names, names);
68 static void collect_digest_names(void *method,
69 STACK_OF(OPENSSL_CSTRING) *names)
71 EVP_MD_names_do_all(method, collect_names, names);
74 static void collect_mac_names(void *method,
75 STACK_OF(OPENSSL_CSTRING) *names)
77 EVP_MAC_names_do_all(method, collect_names, names);
80 static void collect_keymgmt_names(void *method,
81 STACK_OF(OPENSSL_CSTRING) *names)
83 EVP_KEYMGMT_names_do_all(method, collect_names, names);
86 static void collect_keyexch_names(void *method,
87 STACK_OF(OPENSSL_CSTRING) *names)
89 EVP_KEYEXCH_names_do_all(method, collect_names, names);
92 static void collect_signature_names(void *method,
93 STACK_OF(OPENSSL_CSTRING) *names)
95 EVP_SIGNATURE_names_do_all(method, collect_names, names);
98 static void print_method_names(BIO *out, INFO *info)
100 STACK_OF(OPENSSL_CSTRING) *names = sk_OPENSSL_CSTRING_new(name_cmp);
102 info->collect_names_fn(info->method, names);
103 print_names(out, names);
104 sk_OPENSSL_CSTRING_free(names);
107 static void print_caps(META *meta, INFO *info)
109 switch (meta->verbose) {
112 BIO_printf(bio_out, "; ");
113 print_method_names(bio_out, info);
118 BIO_printf(bio_out, "\n");
119 BIO_printf(bio_out, "%*s%ss:", meta->indent, "", meta->label);
121 BIO_printf(bio_out, " ");
122 print_method_names(bio_out, info);
126 BIO_printf(bio_out, "%*s%s ", meta->indent, "", meta->label);
127 print_method_names(bio_out, info);
128 BIO_printf(bio_out, "\n");
129 print_param_types("retrievable algorithm parameters",
130 info->gettable_params, meta->subindent);
131 print_param_types("retrievable operation parameters",
132 info->gettable_ctx_params, meta->subindent);
133 print_param_types("settable operation parameters",
134 info->settable_ctx_params, meta->subindent);
140 static void do_method(void *method,
141 void (*collect_names_fn)(void *method,
142 STACK_OF(OPENSSL_CSTRING) *names),
143 const OSSL_PARAM *gettable_params,
144 const OSSL_PARAM *gettable_ctx_params,
145 const OSSL_PARAM *settable_ctx_params,
150 info.collect_names_fn = collect_names_fn;
151 info.method = method;
152 info.gettable_params = gettable_params;
153 info.gettable_ctx_params = gettable_ctx_params;
154 info.settable_ctx_params = settable_ctx_params;
155 meta->fn(meta, &info);
159 static void do_cipher(EVP_CIPHER *cipher, void *meta)
161 do_method(cipher, collect_cipher_names,
162 EVP_CIPHER_gettable_params(cipher),
163 EVP_CIPHER_gettable_ctx_params(cipher),
164 EVP_CIPHER_settable_ctx_params(cipher),
168 static void do_digest(EVP_MD *digest, void *meta)
170 do_method(digest, collect_digest_names,
171 EVP_MD_gettable_params(digest),
172 EVP_MD_gettable_ctx_params(digest),
173 EVP_MD_settable_ctx_params(digest),
177 static void do_mac(EVP_MAC *mac, void *meta)
179 do_method(mac, collect_mac_names,
180 EVP_MAC_gettable_params(mac),
181 EVP_MAC_gettable_ctx_params(mac),
182 EVP_MAC_settable_ctx_params(mac),
186 static void do_keymgmt(EVP_KEYMGMT *keymgmt, void *meta)
188 do_method(keymgmt, collect_keymgmt_names,
190 * TODO(3.0) Enable when KEYMGMT and KEYEXCH have gettables and settables
193 EVP_KEYMGMT_gettable_params(keymgmt),
194 EVP_KEYMGMT_gettable_ctx_params(keymgmt),
195 EVP_KEYMGMT_settable_ctx_params(keymgmt),
202 static void do_keyexch(EVP_KEYEXCH *keyexch, void *meta)
204 do_method(keyexch, collect_keyexch_names,
206 * TODO(3.0) Enable when KEYMGMT and KEYEXCH have gettables and settables
209 EVP_KEYEXCH_gettable_params(keyexch),
210 EVP_KEYEXCH_gettable_ctx_params(keyexch),
211 EVP_KEYEXCH_settable_ctx_params(keyexch),
218 static void do_signature(EVP_SIGNATURE *signature, void *meta)
220 do_method(signature, collect_signature_names,
222 * TODO(3.0) Enable when KEYMGMT and SIGNATURE have gettables and settables
225 EVP_SIGNATURE_gettable_params(signature),
226 EVP_SIGNATURE_gettable_ctx_params(signature),
227 EVP_SIGNATURE_settable_ctx_params(signature),
234 int provider_main(int argc, char **argv)
238 STACK_OF(OPENSSL_CSTRING) *providers = sk_OPENSSL_CSTRING_new_null();
242 prog = opt_init(argc, argv, provider_options);
243 while ((o = opt_next()) != OPT_EOF) {
245 default: /* Catching OPT_ERR & covering OPT_EOF which isn't possible */
246 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
249 opt_help(provider_options);
255 /* Convert to an integer from one to four. */
256 i = (int)(o - OPT_V) + 1;
263 /* Allow any trailing parameters as provider names. */
264 argc = opt_num_rest();
266 for ( ; *argv; argv++) {
268 BIO_printf(bio_err, "%s: Cannot mix flags and provider names.\n",
270 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
273 sk_OPENSSL_CSTRING_push(providers, *argv);
277 for (i = 0; i < sk_OPENSSL_CSTRING_num(providers); i++) {
278 const char *name = sk_OPENSSL_CSTRING_value(providers, i);
279 OSSL_PROVIDER *prov = OSSL_PROVIDER_load(NULL, name);
282 BIO_printf(bio_out, verbose == 0 ? "%s\n" : "[ %s ]\n", name);
289 data.verbose = verbose;
291 data.fn = print_caps;
295 BIO_printf(bio_out, " ");
309 data.label = "Cipher";
311 EVP_CIPHER_do_all_provided(NULL, do_cipher, &data);
314 data.label = "Digest";
316 EVP_MD_do_all_provided(NULL, do_digest, &data);
321 EVP_MAC_do_all_provided(NULL, do_mac, &data);
325 data.label = "Key manager";
327 EVP_KEYMGMT_do_all_provided(NULL, do_keymgmt, &data);
330 data.label = "Key exchange";
332 EVP_KEYEXCH_do_all_provided(NULL, do_keyexch, &data);
335 data.label = "Signature";
337 EVP_SIGNATURE_do_all_provided(NULL, do_signature, &data);
344 BIO_printf(bio_out, "\n");
348 OSSL_PROVIDER_unload(prov);
350 ERR_print_errors(bio_err);
353 * Just because one provider module failed, there's no reason to
354 * stop, if there are more to try.
361 ERR_print_errors(bio_err);
362 sk_OPENSSL_CSTRING_free(providers);