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"},
32 OPT_SECTION("General"),
33 {"help", OPT_HELP, '-', "Display this summary"},
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"},
44 typedef struct info_st INFO;
45 typedef struct meta_st META;
48 void (*collect_names_fn)(void *method, STACK_OF(OPENSSL_CSTRING) *names);
50 const OSSL_PARAM *gettable_params;
51 const OSSL_PARAM *gettable_ctx_params;
52 const OSSL_PARAM *settable_ctx_params;
56 int first; /* For prints */
63 void (*fn)(META *meta, INFO *info);
66 static void collect_cipher_names(void *method,
67 STACK_OF(OPENSSL_CSTRING) *names)
69 EVP_CIPHER_names_do_all(method, collect_names, names);
72 static void collect_digest_names(void *method,
73 STACK_OF(OPENSSL_CSTRING) *names)
75 EVP_MD_names_do_all(method, collect_names, names);
78 static void collect_mac_names(void *method,
79 STACK_OF(OPENSSL_CSTRING) *names)
81 EVP_MAC_names_do_all(method, collect_names, names);
84 static void collect_keymgmt_names(void *method,
85 STACK_OF(OPENSSL_CSTRING) *names)
87 EVP_KEYMGMT_names_do_all(method, collect_names, names);
90 static void collect_keyexch_names(void *method,
91 STACK_OF(OPENSSL_CSTRING) *names)
93 EVP_KEYEXCH_names_do_all(method, collect_names, names);
96 static void collect_signature_names(void *method,
97 STACK_OF(OPENSSL_CSTRING) *names)
99 EVP_SIGNATURE_names_do_all(method, collect_names, names);
102 static void print_method_names(BIO *out, INFO *info)
104 STACK_OF(OPENSSL_CSTRING) *names = sk_OPENSSL_CSTRING_new(name_cmp);
106 info->collect_names_fn(info->method, names);
107 print_names(out, names);
108 sk_OPENSSL_CSTRING_free(names);
111 static void print_caps(META *meta, INFO *info)
113 switch (meta->verbose) {
116 BIO_printf(bio_out, "; ");
117 print_method_names(bio_out, info);
122 BIO_printf(bio_out, "\n");
123 BIO_printf(bio_out, "%*s%ss:", meta->indent, "", meta->label);
125 BIO_printf(bio_out, " ");
126 print_method_names(bio_out, info);
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);
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,
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);
163 static void do_cipher(EVP_CIPHER *cipher, void *meta)
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),
172 static void do_digest(EVP_MD *digest, void *meta)
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),
181 static void do_mac(EVP_MAC *mac, void *meta)
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),
190 static void do_keymgmt(EVP_KEYMGMT *keymgmt, void *meta)
192 do_method(keymgmt, collect_keymgmt_names,
194 * TODO(3.0) Enable when KEYMGMT and KEYEXCH have gettables and settables
197 EVP_KEYMGMT_gettable_params(keymgmt),
198 EVP_KEYMGMT_gettable_ctx_params(keymgmt),
199 EVP_KEYMGMT_settable_ctx_params(keymgmt),
206 static void do_keyexch(EVP_KEYEXCH *keyexch, void *meta)
208 do_method(keyexch, collect_keyexch_names,
210 * TODO(3.0) Enable when KEYMGMT and KEYEXCH have gettables and settables
213 EVP_KEYEXCH_gettable_params(keyexch),
214 EVP_KEYEXCH_gettable_ctx_params(keyexch),
215 EVP_KEYEXCH_settable_ctx_params(keyexch),
222 static void do_signature(EVP_SIGNATURE *signature, void *meta)
224 do_method(signature, collect_signature_names,
226 * TODO(3.0) Enable when KEYMGMT and SIGNATURE have gettables and settables
229 EVP_SIGNATURE_gettable_params(signature),
230 EVP_SIGNATURE_gettable_ctx_params(signature),
231 EVP_SIGNATURE_settable_ctx_params(signature),
238 int provider_main(int argc, char **argv)
242 STACK_OF(OPENSSL_CSTRING) *providers = sk_OPENSSL_CSTRING_new_null();
246 prog = opt_init(argc, argv, provider_options);
247 while ((o = opt_next()) != OPT_EOF) {
249 default: /* Catching OPT_ERR & covering OPT_EOF which isn't possible */
250 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
253 opt_help(provider_options);
259 /* Convert to an integer from one to four. */
260 i = (int)(o - OPT_V) + 1;
267 /* Allow any trailing parameters as provider names. */
268 argc = opt_num_rest();
270 for ( ; *argv; argv++) {
272 BIO_printf(bio_err, "%s: Cannot mix flags and provider names.\n",
274 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
277 sk_OPENSSL_CSTRING_push(providers, *argv);
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);
286 BIO_printf(bio_out, verbose == 0 ? "%s\n" : "[ %s ]\n", name);
293 data.verbose = verbose;
295 data.fn = print_caps;
299 BIO_printf(bio_out, " ");
313 data.label = "Cipher";
315 EVP_CIPHER_do_all_provided(NULL, do_cipher, &data);
318 data.label = "Digest";
320 EVP_MD_do_all_provided(NULL, do_digest, &data);
325 EVP_MAC_do_all_provided(NULL, do_mac, &data);
329 data.label = "Key manager";
331 EVP_KEYMGMT_do_all_provided(NULL, do_keymgmt, &data);
334 data.label = "Key exchange";
336 EVP_KEYEXCH_do_all_provided(NULL, do_keyexch, &data);
339 data.label = "Signature";
341 EVP_SIGNATURE_do_all_provided(NULL, do_signature, &data);
348 BIO_printf(bio_out, "\n");
352 OSSL_PROVIDER_unload(prov);
354 ERR_print_errors(bio_err);
357 * Just because one provider module failed, there's no reason to
358 * stop, if there are more to try.
365 ERR_print_errors(bio_err);
366 sk_OPENSSL_CSTRING_free(providers);