provider.c 11 KB

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