provider_status_test.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * Copyright 2020-2024 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 <stddef.h>
  10. #include <string.h>
  11. #include <openssl/provider.h>
  12. #include <openssl/params.h>
  13. #include <openssl/core_names.h>
  14. #include <openssl/self_test.h>
  15. #include <openssl/evp.h>
  16. #include <openssl/rsa.h>
  17. #include "testutil.h"
  18. typedef enum OPTION_choice {
  19. OPT_ERR = -1,
  20. OPT_EOF = 0,
  21. OPT_PROVIDER_NAME,
  22. OPT_CONFIG_FILE,
  23. OPT_TEST_ENUM
  24. } OPTION_CHOICE;
  25. struct self_test_arg {
  26. int count;
  27. };
  28. static OSSL_LIB_CTX *libctx = NULL;
  29. static char *provider_name = NULL;
  30. static struct self_test_arg self_test_args = { 0 };
  31. const OPTIONS *test_get_options(void)
  32. {
  33. static const OPTIONS test_options[] = {
  34. OPT_TEST_OPTIONS_DEFAULT_USAGE,
  35. { "provider_name", OPT_PROVIDER_NAME, 's',
  36. "The name of the provider to load" },
  37. { "config", OPT_CONFIG_FILE, '<',
  38. "The configuration file to use for the libctx" },
  39. { NULL }
  40. };
  41. return test_options;
  42. }
  43. static int self_test_events(const OSSL_PARAM params[], void *arg,
  44. const char *title, int corrupt)
  45. {
  46. struct self_test_arg *args = arg;
  47. const OSSL_PARAM *p = NULL;
  48. const char *phase = NULL, *type = NULL, *desc = NULL;
  49. int ret = 0;
  50. if (args->count == 0)
  51. BIO_printf(bio_out, "\n%s\n", title);
  52. args->count++;
  53. p = OSSL_PARAM_locate_const(params, OSSL_PROV_PARAM_SELF_TEST_PHASE);
  54. if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING)
  55. goto err;
  56. phase = (const char *)p->data;
  57. p = OSSL_PARAM_locate_const(params, OSSL_PROV_PARAM_SELF_TEST_DESC);
  58. if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING)
  59. goto err;
  60. desc = (const char *)p->data;
  61. p = OSSL_PARAM_locate_const(params, OSSL_PROV_PARAM_SELF_TEST_TYPE);
  62. if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING)
  63. goto err;
  64. type = (const char *)p->data;
  65. if (strcmp(phase, OSSL_SELF_TEST_PHASE_START) == 0)
  66. BIO_printf(bio_out, "%s : (%s) : ", desc, type);
  67. else if (strcmp(phase, OSSL_SELF_TEST_PHASE_PASS) == 0
  68. || strcmp(phase, OSSL_SELF_TEST_PHASE_FAIL) == 0)
  69. BIO_printf(bio_out, "%s\n", phase);
  70. /*
  71. * The self test code will internally corrupt the KAT test result if an
  72. * error is returned during the corrupt phase.
  73. */
  74. if (corrupt && strcmp(phase, OSSL_SELF_TEST_PHASE_CORRUPT) == 0)
  75. goto err;
  76. ret = 1;
  77. err:
  78. return ret;
  79. }
  80. static int self_test_on_demand_fail(const OSSL_PARAM params[], void *arg)
  81. {
  82. return self_test_events(params, arg, "On Demand Failure", 1);
  83. }
  84. static int self_test_on_demand(const OSSL_PARAM params[], void *arg)
  85. {
  86. return self_test_events(params, arg, "On Demand", 0);
  87. }
  88. static int self_test_on_load(const OSSL_PARAM params[], void *arg)
  89. {
  90. return self_test_events(params, arg, "On Loading", 0);
  91. }
  92. static int get_provider_params(const OSSL_PROVIDER *prov)
  93. {
  94. int ret = 0;
  95. OSSL_PARAM params[5];
  96. char *name, *version, *buildinfo;
  97. int status;
  98. const OSSL_PARAM *gettable, *p;
  99. if (!TEST_ptr(gettable = OSSL_PROVIDER_gettable_params(prov))
  100. || !TEST_ptr(p = OSSL_PARAM_locate_const(gettable, OSSL_PROV_PARAM_NAME))
  101. || !TEST_ptr(p = OSSL_PARAM_locate_const(gettable, OSSL_PROV_PARAM_VERSION))
  102. || !TEST_ptr(p = OSSL_PARAM_locate_const(gettable, OSSL_PROV_PARAM_STATUS))
  103. || !TEST_ptr(p = OSSL_PARAM_locate_const(gettable, OSSL_PROV_PARAM_BUILDINFO)))
  104. goto end;
  105. params[0] = OSSL_PARAM_construct_utf8_ptr(OSSL_PROV_PARAM_NAME, &name, 0);
  106. params[1] = OSSL_PARAM_construct_utf8_ptr(OSSL_PROV_PARAM_VERSION,
  107. &version, 0);
  108. params[2] = OSSL_PARAM_construct_int(OSSL_PROV_PARAM_STATUS, &status);
  109. params[3] = OSSL_PARAM_construct_utf8_ptr(OSSL_PROV_PARAM_BUILDINFO,
  110. &buildinfo, 0);
  111. params[4] = OSSL_PARAM_construct_end();
  112. OSSL_PARAM_set_all_unmodified(params);
  113. if (!TEST_true(OSSL_PROVIDER_get_params(prov, params)))
  114. goto end;
  115. if (!TEST_true(OSSL_PARAM_modified(params + 0))
  116. || !TEST_true(OSSL_PARAM_modified(params + 1))
  117. || !TEST_true(OSSL_PARAM_modified(params + 2))
  118. || !TEST_true(OSSL_PARAM_modified(params + 3))
  119. || !TEST_true(status == 1))
  120. goto end;
  121. ret = 1;
  122. end:
  123. return ret;
  124. }
  125. static int test_provider_status(void)
  126. {
  127. int ret = 0;
  128. unsigned int status = 0;
  129. OSSL_PROVIDER *prov = NULL;
  130. OSSL_PARAM params[2];
  131. EVP_MD *fetch = NULL;
  132. EVP_PKEY_CTX *pctx = NULL;
  133. EVP_PKEY *pkey = NULL;
  134. if (!TEST_ptr(prov = OSSL_PROVIDER_load(libctx, provider_name)))
  135. goto err;
  136. if (!get_provider_params(prov))
  137. goto err;
  138. /* Test that the provider status is ok */
  139. params[0] = OSSL_PARAM_construct_uint(OSSL_PROV_PARAM_STATUS, &status);
  140. params[1] = OSSL_PARAM_construct_end();
  141. if (!TEST_true(OSSL_PROVIDER_get_params(prov, params))
  142. || !TEST_true(status == 1))
  143. goto err;
  144. if (!TEST_ptr(fetch = EVP_MD_fetch(libctx, "SHA256", NULL)))
  145. goto err;
  146. EVP_MD_free(fetch);
  147. fetch = NULL;
  148. /* Use RNG before triggering on-demand self tests */
  149. if (!TEST_ptr((pctx = EVP_PKEY_CTX_new_from_name(libctx, "RSA", NULL)))
  150. || !TEST_int_gt(EVP_PKEY_keygen_init(pctx), 0)
  151. || !TEST_int_gt(EVP_PKEY_CTX_set_rsa_keygen_bits(pctx, 2048), 0)
  152. || !TEST_int_gt(EVP_PKEY_keygen(pctx, &pkey), 0))
  153. goto err;
  154. EVP_PKEY_free(pkey);
  155. EVP_PKEY_CTX_free(pctx);
  156. pkey = NULL;
  157. pctx = NULL;
  158. /* Test that the provider self test is ok */
  159. self_test_args.count = 0;
  160. OSSL_SELF_TEST_set_callback(libctx, self_test_on_demand, &self_test_args);
  161. if (!TEST_true(OSSL_PROVIDER_self_test(prov)))
  162. goto err;
  163. /* Setup a callback that corrupts the self tests and causes status failures */
  164. self_test_args.count = 0;
  165. OSSL_SELF_TEST_set_callback(libctx, self_test_on_demand_fail, &self_test_args);
  166. if (!TEST_false(OSSL_PROVIDER_self_test(prov)))
  167. goto err;
  168. if (!TEST_true(OSSL_PROVIDER_get_params(prov, params))
  169. || !TEST_uint_eq(status, 0))
  170. goto err;
  171. if (!TEST_ptr_null(fetch = EVP_MD_fetch(libctx, "SHA256", NULL)))
  172. goto err;
  173. ret = 1;
  174. err:
  175. EVP_MD_free(fetch);
  176. OSSL_PROVIDER_unload(prov);
  177. return ret;
  178. }
  179. static int test_provider_gettable_params(void)
  180. {
  181. OSSL_PROVIDER *prov;
  182. int ret;
  183. if (!TEST_ptr(prov = OSSL_PROVIDER_load(libctx, provider_name)))
  184. return 0;
  185. ret = get_provider_params(prov);
  186. OSSL_PROVIDER_unload(prov);
  187. return ret;
  188. }
  189. int setup_tests(void)
  190. {
  191. OPTION_CHOICE o;
  192. char *config_file = NULL;
  193. while ((o = opt_next()) != OPT_EOF) {
  194. switch (o) {
  195. case OPT_CONFIG_FILE:
  196. config_file = opt_arg();
  197. break;
  198. case OPT_PROVIDER_NAME:
  199. provider_name = opt_arg();
  200. break;
  201. case OPT_TEST_CASES:
  202. break;
  203. default:
  204. case OPT_ERR:
  205. return 0;
  206. }
  207. }
  208. libctx = OSSL_LIB_CTX_new();
  209. if (libctx == NULL)
  210. return 0;
  211. if (strcmp(provider_name, "fips") == 0) {
  212. self_test_args.count = 0;
  213. OSSL_SELF_TEST_set_callback(libctx, self_test_on_load, &self_test_args);
  214. if (!OSSL_LIB_CTX_load_config(libctx, config_file)) {
  215. opt_printf_stderr("Failed to load config\n");
  216. return 0;
  217. }
  218. ADD_TEST(test_provider_status);
  219. } else {
  220. ADD_TEST(test_provider_gettable_params);
  221. }
  222. return 1;
  223. }
  224. void cleanup_tests(void)
  225. {
  226. OSSL_LIB_CTX_free(libctx);
  227. }