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 DEFINE_STACK_OF_CSTRING()
25 typedef enum OPTION_choice {
26 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
27 OPT_V = 100, OPT_VV, OPT_VVV
30 const OPTIONS provider_options[] = {
31 {OPT_HELP_STR, 1, '-', "Usage: %s [options] [provider...]\n"},
33 OPT_SECTION("General"),
34 {"help", OPT_HELP, '-', "Display this summary"},
36 OPT_SECTION("Output"),
37 {"v", OPT_V, '-', "List the algorithm names of specified provider"},
38 {"vv", OPT_VV, '-', "List the algorithm names of specified providers,"},
39 {OPT_MORE_STR, 0, '-', "categorised by operation type"},
40 {"vvv", OPT_VVV, '-', "List the algorithm names of specified provider"},
41 {OPT_MORE_STR, 0, '-', "one at a time, and list all known parameters"},
44 {"provider", 0, 0, "Provider(s) to load"},
48 typedef struct info_st INFO;
49 typedef struct meta_st META;
52 void (*collect_names_fn)(void *method, STACK_OF(OPENSSL_CSTRING) *names);
54 const OSSL_PARAM *gettable_params;
55 const OSSL_PARAM *gettable_ctx_params;
56 const OSSL_PARAM *settable_ctx_params;
60 int first; /* For prints */
67 void (*fn)(META *meta, INFO *info);
70 static void collect_cipher_names(void *method,
71 STACK_OF(OPENSSL_CSTRING) *names)
73 EVP_CIPHER_names_do_all(method, collect_names, names);
76 static void collect_digest_names(void *method,
77 STACK_OF(OPENSSL_CSTRING) *names)
79 EVP_MD_names_do_all(method, collect_names, names);
82 static void collect_mac_names(void *method,
83 STACK_OF(OPENSSL_CSTRING) *names)
85 EVP_MAC_names_do_all(method, collect_names, names);
88 static void collect_keymgmt_names(void *method,
89 STACK_OF(OPENSSL_CSTRING) *names)
91 EVP_KEYMGMT_names_do_all(method, collect_names, names);
94 static void collect_keyexch_names(void *method,
95 STACK_OF(OPENSSL_CSTRING) *names)
97 EVP_KEYEXCH_names_do_all(method, collect_names, names);
100 static void collect_signature_names(void *method,
101 STACK_OF(OPENSSL_CSTRING) *names)
103 EVP_SIGNATURE_names_do_all(method, collect_names, names);
106 static void print_method_names(BIO *out, INFO *info)
108 STACK_OF(OPENSSL_CSTRING) *names = sk_OPENSSL_CSTRING_new(name_cmp);
110 info->collect_names_fn(info->method, names);
111 print_names(out, names);
112 sk_OPENSSL_CSTRING_free(names);
115 static void print_caps(META *meta, INFO *info)
117 switch (meta->verbose) {
120 BIO_printf(bio_out, "; ");
121 print_method_names(bio_out, info);
126 BIO_printf(bio_out, "\n");
127 BIO_printf(bio_out, "%*s%ss:", meta->indent, "", meta->label);
129 BIO_printf(bio_out, " ");
130 print_method_names(bio_out, info);
134 BIO_printf(bio_out, "%*s%s ", meta->indent, "", meta->label);
135 print_method_names(bio_out, info);
136 BIO_printf(bio_out, "\n");
137 print_param_types("retrievable algorithm parameters",
138 info->gettable_params, meta->subindent);
139 print_param_types("retrievable operation parameters",
140 info->gettable_ctx_params, meta->subindent);
141 print_param_types("settable operation parameters",
142 info->settable_ctx_params, meta->subindent);
148 static void do_method(void *method,
149 void (*collect_names_fn)(void *method,
150 STACK_OF(OPENSSL_CSTRING) *names),
151 const OSSL_PARAM *gettable_params,
152 const OSSL_PARAM *gettable_ctx_params,
153 const OSSL_PARAM *settable_ctx_params,
158 info.collect_names_fn = collect_names_fn;
159 info.method = method;
160 info.gettable_params = gettable_params;
161 info.gettable_ctx_params = gettable_ctx_params;
162 info.settable_ctx_params = settable_ctx_params;
163 meta->fn(meta, &info);
167 static void do_cipher(EVP_CIPHER *cipher, void *meta)
169 do_method(cipher, collect_cipher_names,
170 EVP_CIPHER_gettable_params(cipher),
171 EVP_CIPHER_gettable_ctx_params(cipher),
172 EVP_CIPHER_settable_ctx_params(cipher),
176 static void do_digest(EVP_MD *digest, void *meta)
178 do_method(digest, collect_digest_names,
179 EVP_MD_gettable_params(digest),
180 EVP_MD_gettable_ctx_params(digest),
181 EVP_MD_settable_ctx_params(digest),
185 static void do_mac(EVP_MAC *mac, void *meta)
187 do_method(mac, collect_mac_names,
188 EVP_MAC_gettable_params(mac),
189 EVP_MAC_gettable_ctx_params(mac),
190 EVP_MAC_settable_ctx_params(mac),
194 static void do_keymgmt(EVP_KEYMGMT *keymgmt, void *meta)
196 do_method(keymgmt, collect_keymgmt_names,
198 * TODO(3.0) Enable when KEYMGMT and KEYEXCH have gettables and settables
201 EVP_KEYMGMT_gettable_params(keymgmt),
202 EVP_KEYMGMT_gettable_ctx_params(keymgmt),
203 EVP_KEYMGMT_settable_ctx_params(keymgmt),
210 static void do_keyexch(EVP_KEYEXCH *keyexch, void *meta)
212 do_method(keyexch, collect_keyexch_names,
214 * TODO(3.0) Enable when KEYMGMT and KEYEXCH have gettables and settables
217 EVP_KEYEXCH_gettable_params(keyexch),
218 EVP_KEYEXCH_gettable_ctx_params(keyexch),
219 EVP_KEYEXCH_settable_ctx_params(keyexch),
226 static void do_signature(EVP_SIGNATURE *signature, void *meta)
228 do_method(signature, collect_signature_names,
230 * TODO(3.0) Enable when KEYMGMT and SIGNATURE have gettables and settables
233 EVP_SIGNATURE_gettable_params(signature),
234 EVP_SIGNATURE_gettable_ctx_params(signature),
235 EVP_SIGNATURE_settable_ctx_params(signature),
242 int provider_main(int argc, char **argv)
246 STACK_OF(OPENSSL_CSTRING) *providers = sk_OPENSSL_CSTRING_new_null();
250 prog = opt_init(argc, argv, provider_options);
251 while ((o = opt_next()) != OPT_EOF) {
253 default: /* Catching OPT_ERR & covering OPT_EOF which isn't possible */
254 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
257 opt_help(provider_options);
263 /* Convert to an integer from one to four. */
264 i = (int)(o - OPT_V) + 1;
271 /* Allow any trailing parameters as provider names. */
272 argc = opt_num_rest();
274 for ( ; *argv; argv++) {
275 /* This isn't necessary since -- is supported. */
277 BIO_printf(bio_err, "%s: Cannot mix flags and provider names.\n",
279 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
282 sk_OPENSSL_CSTRING_push(providers, *argv);
286 for (i = 0; i < sk_OPENSSL_CSTRING_num(providers); i++) {
287 const char *name = sk_OPENSSL_CSTRING_value(providers, i);
288 OSSL_PROVIDER *prov = OSSL_PROVIDER_load(NULL, name);
291 BIO_printf(bio_out, verbose == 0 ? "%s\n" : "[ %s ]\n", name);
298 data.verbose = verbose;
300 data.fn = print_caps;
304 BIO_printf(bio_out, " ");
318 data.label = "Cipher";
320 EVP_CIPHER_do_all_provided(NULL, do_cipher, &data);
323 data.label = "Digest";
325 EVP_MD_do_all_provided(NULL, do_digest, &data);
330 EVP_MAC_do_all_provided(NULL, do_mac, &data);
334 data.label = "Key manager";
336 EVP_KEYMGMT_do_all_provided(NULL, do_keymgmt, &data);
339 data.label = "Key exchange";
341 EVP_KEYEXCH_do_all_provided(NULL, do_keyexch, &data);
344 data.label = "Signature";
346 EVP_SIGNATURE_do_all_provided(NULL, do_signature, &data);
353 BIO_printf(bio_out, "\n");
357 OSSL_PROVIDER_unload(prov);
359 ERR_print_errors(bio_err);
362 * Just because one provider module failed, there's no reason to
363 * stop, if there are more to try.
370 ERR_print_errors(bio_err);
371 sk_OPENSSL_CSTRING_free(providers);