provider_test.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*
  2. * Copyright 2019-2022 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 <openssl/provider.h>
  11. #include "testutil.h"
  12. extern OSSL_provider_init_fn PROVIDER_INIT_FUNCTION_NAME;
  13. static char buf[256];
  14. static OSSL_PARAM greeting_request[] = {
  15. { "greeting", OSSL_PARAM_UTF8_STRING, buf, sizeof(buf) },
  16. { NULL, 0, NULL, 0, 0 }
  17. };
  18. static unsigned int digestsuccess = 0;
  19. static OSSL_PARAM digest_check[] = {
  20. { "digest-check", OSSL_PARAM_UNSIGNED_INTEGER, &digestsuccess,
  21. sizeof(digestsuccess) },
  22. { NULL, 0, NULL, 0, 0 }
  23. };
  24. static unsigned int stopsuccess = 0;
  25. static OSSL_PARAM stop_property_mirror[] = {
  26. { "stop-property-mirror", OSSL_PARAM_UNSIGNED_INTEGER, &stopsuccess,
  27. sizeof(stopsuccess) },
  28. { NULL, 0, NULL, 0, 0 }
  29. };
  30. static int test_provider(OSSL_LIB_CTX **libctx, const char *name,
  31. OSSL_PROVIDER *legacy)
  32. {
  33. OSSL_PROVIDER *prov = NULL;
  34. const char *greeting = NULL;
  35. char expected_greeting[256];
  36. int ok = 0;
  37. long err;
  38. int dolegacycheck = (legacy != NULL);
  39. OSSL_PROVIDER *deflt = NULL, *base = NULL;
  40. BIO_snprintf(expected_greeting, sizeof(expected_greeting),
  41. "Hello OpenSSL %.20s, greetings from %s!",
  42. OPENSSL_VERSION_STR, name);
  43. /*
  44. * We set properties that we know the providers we are using don't have.
  45. * This should mean that the p_test provider will fail any fetches - which
  46. * is something we test inside the provider.
  47. */
  48. EVP_set_default_properties(*libctx, "fips=yes");
  49. /*
  50. * Check that it is possible to have a built-in provider mirrored in
  51. * a child lib ctx.
  52. */
  53. if (!TEST_ptr(base = OSSL_PROVIDER_load(*libctx, "base")))
  54. goto err;
  55. if (!TEST_ptr(prov = OSSL_PROVIDER_load(*libctx, name)))
  56. goto err;
  57. /*
  58. * Once the provider is loaded we clear the default properties and fetches
  59. * should start working again.
  60. */
  61. EVP_set_default_properties(*libctx, "");
  62. if (dolegacycheck) {
  63. if (!TEST_true(OSSL_PROVIDER_get_params(prov, digest_check))
  64. || !TEST_true(digestsuccess))
  65. goto err;
  66. /*
  67. * Check that a provider can prevent property mirroring if it sets its
  68. * own properties explicitly
  69. */
  70. if (!TEST_true(OSSL_PROVIDER_get_params(prov, stop_property_mirror))
  71. || !TEST_true(stopsuccess))
  72. goto err;
  73. EVP_set_default_properties(*libctx, "fips=yes");
  74. if (!TEST_true(OSSL_PROVIDER_get_params(prov, digest_check))
  75. || !TEST_true(digestsuccess))
  76. goto err;
  77. EVP_set_default_properties(*libctx, "");
  78. }
  79. if (!TEST_true(OSSL_PROVIDER_get_params(prov, greeting_request))
  80. || !TEST_ptr(greeting = greeting_request[0].data)
  81. || !TEST_size_t_gt(greeting_request[0].data_size, 0)
  82. || !TEST_str_eq(greeting, expected_greeting))
  83. goto err;
  84. /* Make sure we got the error we were expecting */
  85. err = ERR_peek_last_error();
  86. if (!TEST_int_gt(err, 0)
  87. || !TEST_int_eq(ERR_GET_REASON(err), 1))
  88. goto err;
  89. OSSL_PROVIDER_unload(legacy);
  90. legacy = NULL;
  91. if (dolegacycheck) {
  92. /* Legacy provider should also be unloaded from child libctx */
  93. if (!TEST_true(OSSL_PROVIDER_get_params(prov, digest_check))
  94. || !TEST_false(digestsuccess))
  95. goto err;
  96. /*
  97. * Loading the legacy provider again should make it available again in
  98. * the child libctx. Loading and unloading the default provider should
  99. * have no impact on the child because the child loads it explicitly
  100. * before this point.
  101. */
  102. legacy = OSSL_PROVIDER_load(*libctx, "legacy");
  103. deflt = OSSL_PROVIDER_load(*libctx, "default");
  104. if (!TEST_ptr(deflt)
  105. || !TEST_true(OSSL_PROVIDER_available(*libctx, "default")))
  106. goto err;
  107. OSSL_PROVIDER_unload(deflt);
  108. deflt = NULL;
  109. if (!TEST_ptr(legacy)
  110. || !TEST_false(OSSL_PROVIDER_available(*libctx, "default"))
  111. || !TEST_true(OSSL_PROVIDER_get_params(prov, digest_check))
  112. || !TEST_true(digestsuccess))
  113. goto err;
  114. OSSL_PROVIDER_unload(legacy);
  115. legacy = NULL;
  116. }
  117. if (!TEST_true(OSSL_PROVIDER_unload(base)))
  118. goto err;
  119. base = NULL;
  120. if (!TEST_true(OSSL_PROVIDER_unload(prov)))
  121. goto err;
  122. prov = NULL;
  123. /*
  124. * We must free the libctx to force the provider to really be unloaded from
  125. * memory
  126. */
  127. OSSL_LIB_CTX_free(*libctx);
  128. *libctx = NULL;
  129. /* We print out all the data to make sure it can still be accessed */
  130. ERR_print_errors_fp(stderr);
  131. ok = 1;
  132. err:
  133. OSSL_PROVIDER_unload(base);
  134. OSSL_PROVIDER_unload(deflt);
  135. OSSL_PROVIDER_unload(legacy);
  136. legacy = NULL;
  137. OSSL_PROVIDER_unload(prov);
  138. OSSL_LIB_CTX_free(*libctx);
  139. *libctx = NULL;
  140. return ok;
  141. }
  142. static int test_builtin_provider(void)
  143. {
  144. OSSL_LIB_CTX *libctx = OSSL_LIB_CTX_new();
  145. const char *name = "p_test_builtin";
  146. int ok;
  147. ok =
  148. TEST_ptr(libctx)
  149. && TEST_true(OSSL_PROVIDER_add_builtin(libctx, name,
  150. PROVIDER_INIT_FUNCTION_NAME))
  151. && test_provider(&libctx, name, NULL);
  152. OSSL_LIB_CTX_free(libctx);
  153. return ok;
  154. }
  155. /* Test relies on fetching the MD4 digest from the legacy provider */
  156. #ifndef OPENSSL_NO_MD4
  157. static int test_builtin_provider_with_child(void)
  158. {
  159. OSSL_LIB_CTX *libctx = OSSL_LIB_CTX_new();
  160. const char *name = "p_test";
  161. OSSL_PROVIDER *legacy;
  162. if (!TEST_ptr(libctx))
  163. return 0;
  164. legacy = OSSL_PROVIDER_load(libctx, "legacy");
  165. if (legacy == NULL) {
  166. /*
  167. * In this case we assume we've been built with "no-legacy" and skip
  168. * this test (there is no OPENSSL_NO_LEGACY)
  169. */
  170. OSSL_LIB_CTX_free(libctx);
  171. return 1;
  172. }
  173. if (!TEST_true(OSSL_PROVIDER_add_builtin(libctx, name,
  174. PROVIDER_INIT_FUNCTION_NAME))) {
  175. OSSL_LIB_CTX_free(libctx);
  176. return 0;
  177. }
  178. /* test_provider will free libctx and unload legacy as part of the test */
  179. return test_provider(&libctx, name, legacy);
  180. }
  181. #endif
  182. #ifndef NO_PROVIDER_MODULE
  183. static int test_loaded_provider(void)
  184. {
  185. OSSL_LIB_CTX *libctx = OSSL_LIB_CTX_new();
  186. const char *name = "p_test";
  187. if (!TEST_ptr(libctx))
  188. return 0;
  189. /* test_provider will free libctx as part of the test */
  190. return test_provider(&libctx, name, NULL);
  191. }
  192. #endif
  193. typedef enum OPTION_choice {
  194. OPT_ERR = -1,
  195. OPT_EOF = 0,
  196. OPT_LOADED,
  197. OPT_TEST_ENUM
  198. } OPTION_CHOICE;
  199. const OPTIONS *test_get_options(void)
  200. {
  201. static const OPTIONS test_options[] = {
  202. OPT_TEST_OPTIONS_DEFAULT_USAGE,
  203. { "loaded", OPT_LOADED, '-', "Run test with a loaded provider" },
  204. { NULL }
  205. };
  206. return test_options;
  207. }
  208. int setup_tests(void)
  209. {
  210. OPTION_CHOICE o;
  211. int loaded = 0;
  212. while ((o = opt_next()) != OPT_EOF) {
  213. switch (o) {
  214. case OPT_TEST_CASES:
  215. break;
  216. case OPT_LOADED:
  217. loaded = 1;
  218. break;
  219. default:
  220. return 0;
  221. }
  222. }
  223. if (!loaded) {
  224. ADD_TEST(test_builtin_provider);
  225. #ifndef OPENSSL_NO_MD4
  226. ADD_TEST(test_builtin_provider_with_child);
  227. #endif
  228. }
  229. #ifndef NO_PROVIDER_MODULE
  230. else {
  231. ADD_TEST(test_loaded_provider);
  232. }
  233. #endif
  234. return 1;
  235. }