provider_status_test.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * Copyright 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 <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 test_provider_status(void)
  92. {
  93. int ret = 0;
  94. unsigned int status = 0;
  95. OSSL_PROVIDER *prov = NULL;
  96. OSSL_PARAM params[2];
  97. EVP_MD *fetch = NULL;
  98. if (!TEST_ptr(prov = OSSL_PROVIDER_load(libctx, provider_name)))
  99. goto err;
  100. /* Test that the provider status is ok */
  101. params[0] = OSSL_PARAM_construct_uint(OSSL_PROV_PARAM_STATUS, &status);
  102. params[1] = OSSL_PARAM_construct_end();
  103. if (!TEST_true(OSSL_PROVIDER_get_params(prov, params))
  104. || !TEST_true(status == 1))
  105. goto err;
  106. if (!TEST_ptr(fetch = EVP_MD_fetch(libctx, "SHA256", NULL)))
  107. goto err;
  108. EVP_MD_free(fetch);
  109. fetch = NULL;
  110. /* Test that the provider self test is ok */
  111. self_test_args.count = 0;
  112. OSSL_SELF_TEST_set_callback(libctx, self_test_on_demand, &self_test_args);
  113. if (!TEST_true(OSSL_PROVIDER_self_test(prov)))
  114. goto err;
  115. /* Setup a callback that corrupts the self tests and causes status failures */
  116. self_test_args.count = 0;
  117. OSSL_SELF_TEST_set_callback(libctx, self_test_on_demand_fail, &self_test_args);
  118. if (!TEST_false(OSSL_PROVIDER_self_test(prov)))
  119. goto err;
  120. if (!TEST_true(OSSL_PROVIDER_get_params(prov, params))
  121. || !TEST_uint_eq(status, 0))
  122. goto err;
  123. if (!TEST_ptr_null(fetch = EVP_MD_fetch(libctx, "SHA256", NULL)))
  124. goto err;
  125. ret = 1;
  126. err:
  127. EVP_MD_free(fetch);
  128. OSSL_PROVIDER_unload(prov);
  129. return ret;
  130. }
  131. int setup_tests(void)
  132. {
  133. OPTION_CHOICE o;
  134. char *config_file = NULL;
  135. while ((o = opt_next()) != OPT_EOF) {
  136. switch (o) {
  137. case OPT_CONFIG_FILE:
  138. config_file = opt_arg();
  139. break;
  140. case OPT_PROVIDER_NAME:
  141. provider_name = opt_arg();
  142. break;
  143. case OPT_TEST_CASES:
  144. break;
  145. default:
  146. case OPT_ERR:
  147. return 0;
  148. }
  149. }
  150. libctx = OSSL_LIB_CTX_new();
  151. if (libctx == NULL)
  152. return 0;
  153. self_test_args.count = 0;
  154. OSSL_SELF_TEST_set_callback(libctx, self_test_on_load, &self_test_args);
  155. if (!OSSL_LIB_CTX_load_config(libctx, config_file)) {
  156. opt_printf_stderr("Failed to load config\n");
  157. return 0;
  158. }
  159. ADD_TEST(test_provider_status);
  160. return 1;
  161. }