p_test.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /*
  2. * Copyright 2019-2023 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. /*
  10. * This is a very simple provider that does absolutely nothing except respond
  11. * to provider global parameter requests. It does this by simply echoing back
  12. * a parameter request it makes to the loading library.
  13. */
  14. #include <string.h>
  15. #include <stdio.h>
  16. #include <stdarg.h>
  17. /*
  18. * When built as an object file to link the application with, we get the
  19. * init function name through the macro PROVIDER_INIT_FUNCTION_NAME. If
  20. * not defined, we use the standard init function name for the shared
  21. * object form.
  22. */
  23. #ifdef PROVIDER_INIT_FUNCTION_NAME
  24. # define OSSL_provider_init PROVIDER_INIT_FUNCTION_NAME
  25. #endif
  26. #include "internal/e_os.h"
  27. #include <openssl/core.h>
  28. #include <openssl/core_dispatch.h>
  29. #include <openssl/err.h>
  30. #include <openssl/evp.h>
  31. #include <openssl/crypto.h>
  32. #include <openssl/provider.h>
  33. typedef struct p_test_ctx {
  34. char *thisfile;
  35. char *thisfunc;
  36. const OSSL_CORE_HANDLE *handle;
  37. OSSL_LIB_CTX *libctx;
  38. } P_TEST_CTX;
  39. static OSSL_FUNC_core_gettable_params_fn *c_gettable_params = NULL;
  40. static OSSL_FUNC_core_get_params_fn *c_get_params = NULL;
  41. static OSSL_FUNC_core_new_error_fn *c_new_error;
  42. static OSSL_FUNC_core_set_error_debug_fn *c_set_error_debug;
  43. static OSSL_FUNC_core_vset_error_fn *c_vset_error;
  44. static OSSL_FUNC_BIO_vsnprintf_fn *c_BIO_vsnprintf;
  45. /* Tell the core what params we provide and what type they are */
  46. static const OSSL_PARAM p_param_types[] = {
  47. { "greeting", OSSL_PARAM_UTF8_STRING, NULL, 0, 0 },
  48. { "digest-check", OSSL_PARAM_UNSIGNED_INTEGER, NULL, 0, 0},
  49. { NULL, 0, NULL, 0, 0 }
  50. };
  51. /* This is a trick to ensure we define the provider functions correctly */
  52. static OSSL_FUNC_provider_gettable_params_fn p_gettable_params;
  53. static OSSL_FUNC_provider_get_params_fn p_get_params;
  54. static OSSL_FUNC_provider_get_reason_strings_fn p_get_reason_strings;
  55. static OSSL_FUNC_provider_teardown_fn p_teardown;
  56. static int local_snprintf(char *buf, size_t n, const char *format, ...)
  57. {
  58. va_list args;
  59. int ret;
  60. va_start(args, format);
  61. ret = (*c_BIO_vsnprintf)(buf, n, format, args);
  62. va_end(args);
  63. return ret;
  64. }
  65. static void p_set_error(int lib, int reason, const char *file, int line,
  66. const char *func, const char *fmt, ...)
  67. {
  68. va_list ap;
  69. va_start(ap, fmt);
  70. c_new_error(NULL);
  71. c_set_error_debug(NULL, file, line, func);
  72. c_vset_error(NULL, ERR_PACK(lib, 0, reason), fmt, ap);
  73. va_end(ap);
  74. }
  75. static const OSSL_PARAM *p_gettable_params(void *_)
  76. {
  77. return p_param_types;
  78. }
  79. static int p_get_params(void *provctx, OSSL_PARAM params[])
  80. {
  81. P_TEST_CTX *ctx = (P_TEST_CTX *)provctx;
  82. const OSSL_CORE_HANDLE *hand = ctx->handle;
  83. OSSL_PARAM *p = params;
  84. int ok = 1;
  85. for (; ok && p->key != NULL; p++) {
  86. if (strcmp(p->key, "greeting") == 0) {
  87. static char *opensslv;
  88. static char *provname;
  89. static char *greeting;
  90. static OSSL_PARAM counter_request[] = {
  91. /* Known libcrypto provided parameters */
  92. { "openssl-version", OSSL_PARAM_UTF8_PTR,
  93. &opensslv, sizeof(&opensslv), 0 },
  94. { "provider-name", OSSL_PARAM_UTF8_PTR,
  95. &provname, sizeof(&provname), 0},
  96. /* This might be present, if there's such a configuration */
  97. { "greeting", OSSL_PARAM_UTF8_PTR,
  98. &greeting, sizeof(&greeting), 0 },
  99. { NULL, 0, NULL, 0, 0 }
  100. };
  101. char buf[256];
  102. size_t buf_l;
  103. opensslv = provname = greeting = NULL;
  104. if (c_get_params(hand, counter_request)) {
  105. if (greeting) {
  106. strcpy(buf, greeting);
  107. } else {
  108. const char *versionp = *(void **)counter_request[0].data;
  109. const char *namep = *(void **)counter_request[1].data;
  110. local_snprintf(buf, sizeof(buf), "Hello OpenSSL %.20s, greetings from %s!",
  111. versionp, namep);
  112. }
  113. } else {
  114. local_snprintf(buf, sizeof(buf), "Howdy stranger...");
  115. }
  116. p->return_size = buf_l = strlen(buf) + 1;
  117. if (p->data_size >= buf_l)
  118. strcpy(p->data, buf);
  119. else
  120. ok = 0;
  121. } else if (strcmp(p->key, "digest-check") == 0) {
  122. unsigned int digestsuccess = 0;
  123. /*
  124. * Test we can use an algorithm from another provider. We're using
  125. * legacy to check that legacy is actually available and we haven't
  126. * just fallen back to default.
  127. */
  128. #ifdef PROVIDER_INIT_FUNCTION_NAME
  129. EVP_MD *md4 = EVP_MD_fetch(ctx->libctx, "MD4", NULL);
  130. EVP_MD_CTX *mdctx = EVP_MD_CTX_new();
  131. const char *msg = "Hello world";
  132. unsigned char out[16];
  133. OSSL_PROVIDER *deflt;
  134. /*
  135. * "default" has not been loaded into the parent libctx. We should be able
  136. * to explicitly load it as a non-child provider.
  137. */
  138. deflt = OSSL_PROVIDER_load(ctx->libctx, "default");
  139. if (deflt == NULL
  140. || !OSSL_PROVIDER_available(ctx->libctx, "default")) {
  141. /* We set error "3" for a failure to load the default provider */
  142. p_set_error(ERR_LIB_PROV, 3, ctx->thisfile, OPENSSL_LINE,
  143. ctx->thisfunc, NULL);
  144. ok = 0;
  145. }
  146. /*
  147. * We should have the default provider available that we loaded
  148. * ourselves, and the base and legacy providers which we inherit
  149. * from the parent libctx. We should also have "this" provider
  150. * available.
  151. */
  152. if (ok
  153. && OSSL_PROVIDER_available(ctx->libctx, "default")
  154. && OSSL_PROVIDER_available(ctx->libctx, "base")
  155. && OSSL_PROVIDER_available(ctx->libctx, "legacy")
  156. && OSSL_PROVIDER_available(ctx->libctx, "p_test")
  157. && md4 != NULL
  158. && mdctx != NULL) {
  159. if (EVP_DigestInit_ex(mdctx, md4, NULL)
  160. && EVP_DigestUpdate(mdctx, (const unsigned char *)msg,
  161. strlen(msg))
  162. && EVP_DigestFinal(mdctx, out, NULL))
  163. digestsuccess = 1;
  164. }
  165. EVP_MD_CTX_free(mdctx);
  166. EVP_MD_free(md4);
  167. OSSL_PROVIDER_unload(deflt);
  168. #endif
  169. if (p->data_size >= sizeof(digestsuccess)) {
  170. *(unsigned int *)p->data = digestsuccess;
  171. p->return_size = sizeof(digestsuccess);
  172. } else {
  173. ok = 0;
  174. }
  175. } else if (strcmp(p->key, "stop-property-mirror") == 0) {
  176. /*
  177. * Setting the default properties explicitly should stop mirroring
  178. * of properties from the parent libctx.
  179. */
  180. unsigned int stopsuccess = 0;
  181. #ifdef PROVIDER_INIT_FUNCTION_NAME
  182. stopsuccess = EVP_set_default_properties(ctx->libctx, NULL);
  183. #endif
  184. if (p->data_size >= sizeof(stopsuccess)) {
  185. *(unsigned int *)p->data = stopsuccess;
  186. p->return_size = sizeof(stopsuccess);
  187. } else {
  188. ok = 0;
  189. }
  190. }
  191. }
  192. return ok;
  193. }
  194. static const OSSL_ITEM *p_get_reason_strings(void *_)
  195. {
  196. static const OSSL_ITEM reason_strings[] = {
  197. {1, "dummy reason string"},
  198. {2, "Can't create child library context"},
  199. {3, "Can't load default provider"},
  200. {0, NULL}
  201. };
  202. return reason_strings;
  203. }
  204. static const OSSL_ALGORITHM *p_query(OSSL_PROVIDER *prov,
  205. int operation_id,
  206. int *no_cache)
  207. {
  208. *no_cache = 1;
  209. return NULL;
  210. }
  211. static const OSSL_DISPATCH p_test_table[] = {
  212. { OSSL_FUNC_PROVIDER_GETTABLE_PARAMS, (void (*)(void))p_gettable_params },
  213. { OSSL_FUNC_PROVIDER_GET_PARAMS, (void (*)(void))p_get_params },
  214. { OSSL_FUNC_PROVIDER_GET_REASON_STRINGS,
  215. (void (*)(void))p_get_reason_strings},
  216. { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))p_teardown },
  217. { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))p_query },
  218. OSSL_DISPATCH_END
  219. };
  220. int OSSL_provider_init(const OSSL_CORE_HANDLE *handle,
  221. const OSSL_DISPATCH *oin,
  222. const OSSL_DISPATCH **out,
  223. void **provctx)
  224. {
  225. P_TEST_CTX *ctx;
  226. const OSSL_DISPATCH *in = oin;
  227. for (; in->function_id != 0; in++) {
  228. switch (in->function_id) {
  229. case OSSL_FUNC_CORE_GETTABLE_PARAMS:
  230. c_gettable_params = OSSL_FUNC_core_gettable_params(in);
  231. break;
  232. case OSSL_FUNC_CORE_GET_PARAMS:
  233. c_get_params = OSSL_FUNC_core_get_params(in);
  234. break;
  235. case OSSL_FUNC_CORE_NEW_ERROR:
  236. c_new_error = OSSL_FUNC_core_new_error(in);
  237. break;
  238. case OSSL_FUNC_CORE_SET_ERROR_DEBUG:
  239. c_set_error_debug = OSSL_FUNC_core_set_error_debug(in);
  240. break;
  241. case OSSL_FUNC_CORE_VSET_ERROR:
  242. c_vset_error = OSSL_FUNC_core_vset_error(in);
  243. break;
  244. case OSSL_FUNC_BIO_VSNPRINTF:
  245. c_BIO_vsnprintf = OSSL_FUNC_BIO_vsnprintf(in);
  246. break;
  247. default:
  248. /* Just ignore anything we don't understand */
  249. break;
  250. }
  251. }
  252. /*
  253. * We want to test that libcrypto doesn't use the file and func pointers
  254. * that we provide to it via c_set_error_debug beyond the time that they
  255. * are valid for. Therefore we dynamically allocate these strings now and
  256. * free them again when the provider is torn down. If anything tries to
  257. * use those strings after that point there will be a use-after-free and
  258. * asan will complain (and hence the tests will fail).
  259. * This file isn't linked against libcrypto, so we use malloc and strdup
  260. * instead of OPENSSL_malloc and OPENSSL_strdup
  261. */
  262. ctx = malloc(sizeof(*ctx));
  263. if (ctx == NULL)
  264. return 0;
  265. ctx->thisfile = strdup(OPENSSL_FILE);
  266. ctx->thisfunc = strdup(OPENSSL_FUNC);
  267. ctx->handle = handle;
  268. #ifdef PROVIDER_INIT_FUNCTION_NAME
  269. /* We only do this if we are linked with libcrypto */
  270. ctx->libctx = OSSL_LIB_CTX_new_child(handle, oin);
  271. if (ctx->libctx == NULL) {
  272. /* We set error "2" for a failure to create the child libctx*/
  273. p_set_error(ERR_LIB_PROV, 2, ctx->thisfile, OPENSSL_LINE, ctx->thisfunc,
  274. NULL);
  275. p_teardown(ctx);
  276. return 0;
  277. }
  278. /*
  279. * The default provider is loaded - but the default properties should not
  280. * allow its use.
  281. */
  282. {
  283. EVP_MD *sha256 = EVP_MD_fetch(ctx->libctx, "SHA2-256", NULL);
  284. if (sha256 != NULL) {
  285. EVP_MD_free(sha256);
  286. p_teardown(ctx);
  287. return 0;
  288. }
  289. }
  290. #endif
  291. /*
  292. * Set a spurious error to check error handling works correctly. This will
  293. * be ignored
  294. */
  295. p_set_error(ERR_LIB_PROV, 1, ctx->thisfile, OPENSSL_LINE, ctx->thisfunc, NULL);
  296. *provctx = (void *)ctx;
  297. *out = p_test_table;
  298. return 1;
  299. }
  300. static void p_teardown(void *provctx)
  301. {
  302. P_TEST_CTX *ctx = (P_TEST_CTX *)provctx;
  303. #ifdef PROVIDER_INIT_FUNCTION_NAME
  304. OSSL_LIB_CTX_free(ctx->libctx);
  305. #endif
  306. free(ctx->thisfile);
  307. free(ctx->thisfunc);
  308. free(ctx);
  309. }