provider.c 11 KB

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