provider_status_test.c 7.2 KB

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