]>
git.ipfire.org Git - thirdparty/openssl.git/blob - apps/provider.c
2 * Copyright 2019-2020 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_dispatch.h>
24 # include <string.h> /* memset */
27 typedef enum OPTION_choice
{
28 OPT_ERR
= -1, OPT_EOF
= 0, OPT_HELP
,
29 OPT_V
= 100, OPT_VV
, OPT_VVV
32 const OPTIONS provider_options
[] = {
33 {OPT_HELP_STR
, 1, '-', "Usage: %s [options] [provider...]\n"},
35 OPT_SECTION("General"),
36 {"help", OPT_HELP
, '-', "Display this summary"},
38 OPT_SECTION("Output"),
39 {"v", OPT_V
, '-', "List the algorithm names of specified provider"},
40 {"vv", OPT_VV
, '-', "List the algorithm names of specified providers,"},
41 {OPT_MORE_STR
, 0, '-', "categorised by operation type"},
42 {"vvv", OPT_VVV
, '-', "List the algorithm names of specified provider"},
43 {OPT_MORE_STR
, 0, '-', "one at a time, and list all known parameters"},
46 {"provider", 0, 0, "Provider(s) to load"},
50 typedef struct info_st INFO
;
51 typedef struct meta_st META
;
54 void (*collect_names_fn
)(void *method
, STACK_OF(OPENSSL_CSTRING
) *names
);
56 const OSSL_PARAM
*gettable_params
;
57 const OSSL_PARAM
*settable_params
;
58 const OSSL_PARAM
*gettable_ctx_params
;
59 const OSSL_PARAM
*settable_ctx_params
;
60 const OSSL_PARAM
*gen_settable_params
;
64 int first
; /* For prints */
71 void (*fn
)(META
*meta
, INFO
*info
);
74 static void collect_cipher_names(void *method
,
75 STACK_OF(OPENSSL_CSTRING
) *names
)
77 EVP_CIPHER_names_do_all(method
, collect_names
, names
);
80 static void collect_digest_names(void *method
,
81 STACK_OF(OPENSSL_CSTRING
) *names
)
83 EVP_MD_names_do_all(method
, collect_names
, names
);
86 static void collect_mac_names(void *method
,
87 STACK_OF(OPENSSL_CSTRING
) *names
)
89 EVP_MAC_names_do_all(method
, collect_names
, names
);
92 static void collect_keymgmt_names(void *method
,
93 STACK_OF(OPENSSL_CSTRING
) *names
)
95 EVP_KEYMGMT_names_do_all(method
, collect_names
, names
);
98 static void collect_keyexch_names(void *method
,
99 STACK_OF(OPENSSL_CSTRING
) *names
)
101 EVP_KEYEXCH_names_do_all(method
, collect_names
, names
);
104 static void collect_signature_names(void *method
,
105 STACK_OF(OPENSSL_CSTRING
) *names
)
107 EVP_SIGNATURE_names_do_all(method
, collect_names
, names
);
110 static void print_method_names(BIO
*out
, INFO
*info
)
112 STACK_OF(OPENSSL_CSTRING
) *names
= sk_OPENSSL_CSTRING_new(name_cmp
);
114 info
->collect_names_fn(info
->method
, names
);
115 print_names(out
, names
);
116 sk_OPENSSL_CSTRING_free(names
);
119 static void print_caps(META
*meta
, INFO
*info
)
121 switch (meta
->verbose
) {
124 BIO_printf(bio_out
, "; ");
125 print_method_names(bio_out
, info
);
130 BIO_printf(bio_out
, "\n");
131 BIO_printf(bio_out
, "%*s%ss:", meta
->indent
, "", meta
->label
);
133 BIO_printf(bio_out
, " ");
134 print_method_names(bio_out
, info
);
138 BIO_printf(bio_out
, "%*s%s ", meta
->indent
, "", meta
->label
);
139 print_method_names(bio_out
, info
);
140 BIO_printf(bio_out
, "\n");
141 print_param_types("settable keygen parameters",
142 info
->gen_settable_params
, meta
->subindent
);
143 print_param_types("settable algorithm parameters",
144 info
->settable_params
, meta
->subindent
);
145 print_param_types("retrievable algorithm parameters",
146 info
->gettable_params
, meta
->subindent
);
147 print_param_types("settable operation parameters",
148 info
->settable_ctx_params
, meta
->subindent
);
149 print_param_types("retrievable operation parameters",
150 info
->gettable_ctx_params
, meta
->subindent
);
156 static void do_method(void *method
,
157 void (*collect_names_fn
)(void *method
,
158 STACK_OF(OPENSSL_CSTRING
) *names
),
159 const OSSL_PARAM
*gettable_params
,
160 const OSSL_PARAM
*gettable_ctx_params
,
161 const OSSL_PARAM
*settable_ctx_params
,
166 memset(&info
, 0, sizeof(info
));
167 info
.collect_names_fn
= collect_names_fn
;
168 info
.method
= method
;
169 info
.gettable_params
= gettable_params
;
170 info
.gettable_ctx_params
= gettable_ctx_params
;
171 info
.settable_ctx_params
= settable_ctx_params
;
172 meta
->fn(meta
, &info
);
176 static void do_keymgmt_method(void *method
,
177 void (*collect_names_fn
)(void *method
,
178 STACK_OF(OPENSSL_CSTRING
)
180 const OSSL_PARAM
*gettable_params
,
181 const OSSL_PARAM
*settable_params
,
182 const OSSL_PARAM
*gen_settable_params
,
187 memset(&info
, 0, sizeof(info
));
188 info
.collect_names_fn
= collect_names_fn
;
189 info
.method
= method
;
190 info
.gettable_params
= gettable_params
;
191 info
.settable_params
= settable_params
;
192 info
.gen_settable_params
= gen_settable_params
;
193 meta
->fn(meta
, &info
);
197 static void do_cipher(EVP_CIPHER
*cipher
, void *meta
)
199 do_method(cipher
, collect_cipher_names
,
200 EVP_CIPHER_gettable_params(cipher
),
201 EVP_CIPHER_gettable_ctx_params(cipher
),
202 EVP_CIPHER_settable_ctx_params(cipher
),
206 static void do_digest(EVP_MD
*digest
, void *meta
)
208 do_method(digest
, collect_digest_names
,
209 EVP_MD_gettable_params(digest
),
210 EVP_MD_gettable_ctx_params(digest
),
211 EVP_MD_settable_ctx_params(digest
),
215 static void do_mac(EVP_MAC
*mac
, void *meta
)
217 do_method(mac
, collect_mac_names
,
218 EVP_MAC_gettable_params(mac
),
219 EVP_MAC_gettable_ctx_params(mac
),
220 EVP_MAC_settable_ctx_params(mac
),
224 static void do_keymgmt(EVP_KEYMGMT
*keymgmt
, void *meta
)
226 do_keymgmt_method(keymgmt
, collect_keymgmt_names
,
227 EVP_KEYMGMT_gettable_params(keymgmt
),
228 EVP_KEYMGMT_settable_params(keymgmt
),
229 EVP_KEYMGMT_gen_settable_params(keymgmt
),
233 static void do_keyexch(EVP_KEYEXCH
*keyexch
, void *meta
)
235 do_method(keyexch
, collect_keyexch_names
,
237 EVP_KEYEXCH_gettable_ctx_params(keyexch
),
238 EVP_KEYEXCH_settable_ctx_params(keyexch
),
242 static void do_signature(EVP_SIGNATURE
*signature
, void *meta
)
244 do_method(signature
, collect_signature_names
,
246 EVP_SIGNATURE_gettable_ctx_params(signature
),
247 EVP_SIGNATURE_settable_ctx_params(signature
),
251 int provider_main(int argc
, char **argv
)
255 STACK_OF(OPENSSL_CSTRING
) *providers
= sk_OPENSSL_CSTRING_new_null();
259 prog
= opt_init(argc
, argv
, provider_options
);
260 while ((o
= opt_next()) != OPT_EOF
) {
262 default: /* Catching OPT_ERR & covering OPT_EOF which isn't possible */
263 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
266 opt_help(provider_options
);
272 /* Convert to an integer from one to four. */
273 i
= (int)(o
- OPT_V
) + 1;
280 /* Allow any trailing parameters as provider names. */
281 argc
= opt_num_rest();
283 for ( ; *argv
; argv
++) {
284 /* This isn't necessary since -- is supported. */
286 BIO_printf(bio_err
, "%s: Cannot mix flags and provider names.\n",
288 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
291 sk_OPENSSL_CSTRING_push(providers
, *argv
);
295 for (i
= 0; i
< sk_OPENSSL_CSTRING_num(providers
); i
++) {
296 const char *name
= sk_OPENSSL_CSTRING_value(providers
, i
);
297 OSSL_PROVIDER
*prov
= OSSL_PROVIDER_load(NULL
, name
);
300 BIO_printf(bio_out
, verbose
== 0 ? "%s\n" : "[ %s ]\n", name
);
307 data
.verbose
= verbose
;
309 data
.fn
= print_caps
;
313 BIO_printf(bio_out
, " ");
327 data
.label
= "Cipher";
329 EVP_CIPHER_do_all_provided(NULL
, do_cipher
, &data
);
332 data
.label
= "Digest";
334 EVP_MD_do_all_provided(NULL
, do_digest
, &data
);
339 EVP_MAC_do_all_provided(NULL
, do_mac
, &data
);
343 data
.label
= "Key manager";
345 EVP_KEYMGMT_do_all_provided(NULL
, do_keymgmt
, &data
);
348 data
.label
= "Key exchange";
350 EVP_KEYEXCH_do_all_provided(NULL
, do_keyexch
, &data
);
353 data
.label
= "Signature";
355 EVP_SIGNATURE_do_all_provided(NULL
, do_signature
, &data
);
362 BIO_printf(bio_out
, "\n");
366 OSSL_PROVIDER_unload(prov
);
368 ERR_print_errors(bio_err
);
371 * Just because one provider module failed, there's no reason to
372 * stop, if there are more to try.
379 ERR_print_errors(bio_err
);
380 sk_OPENSSL_CSTRING_free(providers
);