provider.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /*
  2. * Copyright 2019-2020 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. #include <openssl/opensslconf.h>
  10. #include "apps.h"
  11. #include "app_params.h"
  12. #include "progs.h"
  13. #include "names.h"
  14. #include <openssl/err.h>
  15. #include <openssl/evp.h>
  16. #include <openssl/safestack.h>
  17. #include <openssl/provider.h>
  18. #include <openssl/core.h>
  19. #include <openssl/core_dispatch.h>
  20. #ifdef __TANDEM
  21. # include <string.h> /* memset */
  22. #endif
  23. DEFINE_STACK_OF_CSTRING()
  24. typedef enum OPTION_choice {
  25. OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
  26. OPT_V = 100, OPT_VV, OPT_VVV
  27. } OPTION_CHOICE;
  28. const OPTIONS provider_options[] = {
  29. {OPT_HELP_STR, 1, '-', "Usage: %s [options] [provider...]\n"},
  30. OPT_SECTION("General"),
  31. {"help", OPT_HELP, '-', "Display this summary"},
  32. OPT_SECTION("Output"),
  33. {"v", OPT_V, '-', "List the algorithm names of specified provider"},
  34. {"vv", OPT_VV, '-', "List the algorithm names of specified providers,"},
  35. {OPT_MORE_STR, 0, '-', "categorised by operation type"},
  36. {"vvv", OPT_VVV, '-', "List the algorithm names of specified provider"},
  37. {OPT_MORE_STR, 0, '-', "one at a time, and list all known parameters"},
  38. OPT_PARAMETERS(),
  39. {"provider", 0, 0, "Provider(s) to load"},
  40. {NULL}
  41. };
  42. typedef struct info_st INFO;
  43. typedef struct meta_st META;
  44. struct info_st {
  45. void (*collect_names_fn)(void *method, STACK_OF(OPENSSL_CSTRING) *names);
  46. void *method;
  47. const OSSL_PARAM *gettable_params;
  48. const OSSL_PARAM *settable_params;
  49. const OSSL_PARAM *gettable_ctx_params;
  50. const OSSL_PARAM *settable_ctx_params;
  51. const OSSL_PARAM *gen_settable_params;
  52. };
  53. struct meta_st {
  54. int first; /* For prints */
  55. int total;
  56. int indent;
  57. int subindent;
  58. int verbose;
  59. const char *label;
  60. OSSL_PROVIDER *prov;
  61. void (*fn)(META *meta, INFO *info);
  62. };
  63. static void collect_cipher_names(void *method,
  64. STACK_OF(OPENSSL_CSTRING) *names)
  65. {
  66. EVP_CIPHER_names_do_all(method, collect_names, names);
  67. }
  68. static void collect_digest_names(void *method,
  69. STACK_OF(OPENSSL_CSTRING) *names)
  70. {
  71. EVP_MD_names_do_all(method, collect_names, names);
  72. }
  73. static void collect_mac_names(void *method,
  74. STACK_OF(OPENSSL_CSTRING) *names)
  75. {
  76. EVP_MAC_names_do_all(method, collect_names, names);
  77. }
  78. static void collect_keymgmt_names(void *method,
  79. STACK_OF(OPENSSL_CSTRING) *names)
  80. {
  81. EVP_KEYMGMT_names_do_all(method, collect_names, names);
  82. }
  83. static void collect_keyexch_names(void *method,
  84. STACK_OF(OPENSSL_CSTRING) *names)
  85. {
  86. EVP_KEYEXCH_names_do_all(method, collect_names, names);
  87. }
  88. static void collect_signature_names(void *method,
  89. STACK_OF(OPENSSL_CSTRING) *names)
  90. {
  91. EVP_SIGNATURE_names_do_all(method, collect_names, names);
  92. }
  93. static void print_method_names(BIO *out, INFO *info)
  94. {
  95. STACK_OF(OPENSSL_CSTRING) *names = sk_OPENSSL_CSTRING_new(name_cmp);
  96. info->collect_names_fn(info->method, names);
  97. print_names(out, names);
  98. sk_OPENSSL_CSTRING_free(names);
  99. }
  100. static void print_caps(META *meta, INFO *info)
  101. {
  102. switch (meta->verbose) {
  103. case 1:
  104. if (!meta->first)
  105. BIO_printf(bio_out, "; ");
  106. print_method_names(bio_out, info);
  107. break;
  108. case 2:
  109. if (meta->first) {
  110. if (meta->total > 0)
  111. BIO_printf(bio_out, "\n");
  112. BIO_printf(bio_out, "%*s%ss:", meta->indent, "", meta->label);
  113. }
  114. BIO_printf(bio_out, " ");
  115. print_method_names(bio_out, info);
  116. break;
  117. case 3:
  118. default:
  119. BIO_printf(bio_out, "%*s%s ", meta->indent, "", meta->label);
  120. print_method_names(bio_out, info);
  121. BIO_printf(bio_out, "\n");
  122. print_param_types("settable keygen parameters",
  123. info->gen_settable_params, meta->subindent);
  124. print_param_types("settable algorithm parameters",
  125. info->settable_params, meta->subindent);
  126. print_param_types("retrievable algorithm parameters",
  127. info->gettable_params, meta->subindent);
  128. print_param_types("settable operation parameters",
  129. info->settable_ctx_params, meta->subindent);
  130. print_param_types("retrievable operation parameters",
  131. info->gettable_ctx_params, meta->subindent);
  132. break;
  133. }
  134. meta->first = 0;
  135. }
  136. static void do_method(void *method,
  137. void (*collect_names_fn)(void *method,
  138. STACK_OF(OPENSSL_CSTRING) *names),
  139. const OSSL_PARAM *gettable_params,
  140. const OSSL_PARAM *gettable_ctx_params,
  141. const OSSL_PARAM *settable_ctx_params,
  142. META *meta)
  143. {
  144. INFO info;
  145. memset(&info, 0, sizeof(info));
  146. info.collect_names_fn = collect_names_fn;
  147. info.method = method;
  148. info.gettable_params = gettable_params;
  149. info.gettable_ctx_params = gettable_ctx_params;
  150. info.settable_ctx_params = settable_ctx_params;
  151. meta->fn(meta, &info);
  152. meta->total++;
  153. }
  154. static void do_keymgmt_method(void *method,
  155. void (*collect_names_fn)(void *method,
  156. STACK_OF(OPENSSL_CSTRING)
  157. *names),
  158. const OSSL_PARAM *gettable_params,
  159. const OSSL_PARAM *settable_params,
  160. const OSSL_PARAM *gen_settable_params,
  161. META *meta)
  162. {
  163. INFO info;
  164. memset(&info, 0, sizeof(info));
  165. info.collect_names_fn = collect_names_fn;
  166. info.method = method;
  167. info.gettable_params = gettable_params;
  168. info.settable_params = settable_params;
  169. info.gen_settable_params = gen_settable_params;
  170. meta->fn(meta, &info);
  171. meta->total++;
  172. }
  173. static void do_cipher(EVP_CIPHER *cipher, void *meta)
  174. {
  175. do_method(cipher, collect_cipher_names,
  176. EVP_CIPHER_gettable_params(cipher),
  177. EVP_CIPHER_gettable_ctx_params(cipher),
  178. EVP_CIPHER_settable_ctx_params(cipher),
  179. meta);
  180. }
  181. static void do_digest(EVP_MD *digest, void *meta)
  182. {
  183. do_method(digest, collect_digest_names,
  184. EVP_MD_gettable_params(digest),
  185. EVP_MD_gettable_ctx_params(digest),
  186. EVP_MD_settable_ctx_params(digest),
  187. meta);
  188. }
  189. static void do_mac(EVP_MAC *mac, void *meta)
  190. {
  191. do_method(mac, collect_mac_names,
  192. EVP_MAC_gettable_params(mac),
  193. EVP_MAC_gettable_ctx_params(mac),
  194. EVP_MAC_settable_ctx_params(mac),
  195. meta);
  196. }
  197. static void do_keymgmt(EVP_KEYMGMT *keymgmt, void *meta)
  198. {
  199. do_keymgmt_method(keymgmt, collect_keymgmt_names,
  200. EVP_KEYMGMT_gettable_params(keymgmt),
  201. EVP_KEYMGMT_settable_params(keymgmt),
  202. EVP_KEYMGMT_gen_settable_params(keymgmt),
  203. meta);
  204. }
  205. static void do_keyexch(EVP_KEYEXCH *keyexch, void *meta)
  206. {
  207. do_method(keyexch, collect_keyexch_names,
  208. NULL,
  209. EVP_KEYEXCH_gettable_ctx_params(keyexch),
  210. EVP_KEYEXCH_settable_ctx_params(keyexch),
  211. meta);
  212. }
  213. static void do_signature(EVP_SIGNATURE *signature, void *meta)
  214. {
  215. do_method(signature, collect_signature_names,
  216. NULL,
  217. EVP_SIGNATURE_gettable_ctx_params(signature),
  218. EVP_SIGNATURE_settable_ctx_params(signature),
  219. meta);
  220. }
  221. int provider_main(int argc, char **argv)
  222. {
  223. int ret = 1, i;
  224. int verbose = 0;
  225. STACK_OF(OPENSSL_CSTRING) *providers = sk_OPENSSL_CSTRING_new_null();
  226. OPTION_CHOICE o;
  227. char *prog;
  228. prog = opt_init(argc, argv, provider_options);
  229. while ((o = opt_next()) != OPT_EOF) {
  230. switch (o) {
  231. default: /* Catching OPT_ERR & covering OPT_EOF which isn't possible */
  232. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  233. goto end;
  234. case OPT_HELP:
  235. opt_help(provider_options);
  236. ret = 0;
  237. goto end;
  238. case OPT_VVV:
  239. case OPT_VV:
  240. case OPT_V:
  241. /* Convert to an integer from one to four. */
  242. i = (int)(o - OPT_V) + 1;
  243. if (verbose < i)
  244. verbose = i;
  245. break;
  246. }
  247. }
  248. /* Allow any trailing parameters as provider names. */
  249. argc = opt_num_rest();
  250. argv = opt_rest();
  251. for ( ; *argv; argv++) {
  252. /* This isn't necessary since -- is supported. */
  253. if (**argv == '-') {
  254. BIO_printf(bio_err, "%s: Cannot mix flags and provider names.\n",
  255. prog);
  256. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  257. goto end;
  258. }
  259. sk_OPENSSL_CSTRING_push(providers, *argv);
  260. }
  261. ret = 0;
  262. for (i = 0; i < sk_OPENSSL_CSTRING_num(providers); i++) {
  263. const char *name = sk_OPENSSL_CSTRING_value(providers, i);
  264. OSSL_PROVIDER *prov = OSSL_PROVIDER_load(NULL, name);
  265. if (prov != NULL) {
  266. BIO_printf(bio_out, verbose == 0 ? "%s\n" : "[ %s ]\n", name);
  267. if (verbose > 0) {
  268. META data;
  269. data.total = 0;
  270. data.first = 1;
  271. data.verbose = verbose;
  272. data.prov = prov;
  273. data.fn = print_caps;
  274. switch (verbose) {
  275. case 1:
  276. BIO_printf(bio_out, " ");
  277. break;
  278. case 2:
  279. data.indent = 4;
  280. break;
  281. case 3:
  282. default:
  283. data.indent = 4;
  284. data.subindent = 10;
  285. break;
  286. }
  287. if (verbose > 1) {
  288. data.first = 1;
  289. data.label = "Cipher";
  290. }
  291. EVP_CIPHER_do_all_provided(NULL, do_cipher, &data);
  292. if (verbose > 1) {
  293. data.first = 1;
  294. data.label = "Digest";
  295. }
  296. EVP_MD_do_all_provided(NULL, do_digest, &data);
  297. if (verbose > 1) {
  298. data.first = 1;
  299. data.label = "MAC";
  300. }
  301. EVP_MAC_do_all_provided(NULL, do_mac, &data);
  302. if (verbose > 1) {
  303. data.first = 1;
  304. data.label = "Key manager";
  305. }
  306. EVP_KEYMGMT_do_all_provided(NULL, do_keymgmt, &data);
  307. if (verbose > 1) {
  308. data.first = 1;
  309. data.label = "Key exchange";
  310. }
  311. EVP_KEYEXCH_do_all_provided(NULL, do_keyexch, &data);
  312. if (verbose > 1) {
  313. data.first = 1;
  314. data.label = "Signature";
  315. }
  316. EVP_SIGNATURE_do_all_provided(NULL, do_signature, &data);
  317. switch (verbose) {
  318. default:
  319. break;
  320. case 2:
  321. case 1:
  322. BIO_printf(bio_out, "\n");
  323. break;
  324. }
  325. }
  326. OSSL_PROVIDER_unload(prov);
  327. } else {
  328. ERR_print_errors(bio_err);
  329. ret = 1;
  330. /*
  331. * Just because one provider module failed, there's no reason to
  332. * stop, if there are more to try.
  333. */
  334. }
  335. }
  336. end:
  337. ERR_print_errors(bio_err);
  338. sk_OPENSSL_CSTRING_free(providers);
  339. return ret;
  340. }