filterprov.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * Copyright 2020-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. * A filtering provider for test purposes. We pass all calls through to the
  11. * default provider except where we want other behaviour for a test.
  12. */
  13. #include <string.h>
  14. #include <openssl/core.h>
  15. #include <openssl/provider.h>
  16. #include <openssl/crypto.h>
  17. #include "testutil.h"
  18. #include "filterprov.h"
  19. #define MAX_FILTERS 10
  20. #define MAX_ALG_FILTERS 5
  21. struct filter_prov_globals_st {
  22. OSSL_LIB_CTX *libctx;
  23. OSSL_PROVIDER *deflt;
  24. struct {
  25. int operation;
  26. OSSL_ALGORITHM alg[MAX_ALG_FILTERS + 1];
  27. } dispatch[MAX_FILTERS];
  28. int num_dispatch;
  29. int no_cache;
  30. unsigned long int query_count;
  31. int error;
  32. };
  33. static struct filter_prov_globals_st ourglobals;
  34. static struct filter_prov_globals_st *get_globals(void)
  35. {
  36. /*
  37. * Ideally we'd like to store this in the OSSL_LIB_CTX so that we can have
  38. * more than one instance of the filter provider at a time. But for now we
  39. * just make it simple.
  40. */
  41. return &ourglobals;
  42. }
  43. static OSSL_FUNC_provider_gettable_params_fn filter_gettable_params;
  44. static OSSL_FUNC_provider_get_params_fn filter_get_params;
  45. static OSSL_FUNC_provider_query_operation_fn filter_query;
  46. static OSSL_FUNC_provider_unquery_operation_fn filter_unquery;
  47. static OSSL_FUNC_provider_teardown_fn filter_teardown;
  48. static const OSSL_PARAM *filter_gettable_params(void *provctx)
  49. {
  50. struct filter_prov_globals_st *globs = get_globals();
  51. return OSSL_PROVIDER_gettable_params(globs->deflt);
  52. }
  53. static int filter_get_params(void *provctx, OSSL_PARAM params[])
  54. {
  55. struct filter_prov_globals_st *globs = get_globals();
  56. return OSSL_PROVIDER_get_params(globs->deflt, params);
  57. }
  58. static int filter_get_capabilities(void *provctx, const char *capability,
  59. OSSL_CALLBACK *cb, void *arg)
  60. {
  61. struct filter_prov_globals_st *globs = get_globals();
  62. return OSSL_PROVIDER_get_capabilities(globs->deflt, capability, cb, arg);
  63. }
  64. static const OSSL_ALGORITHM *filter_query(void *provctx,
  65. int operation_id,
  66. int *no_cache)
  67. {
  68. struct filter_prov_globals_st *globs = get_globals();
  69. int i;
  70. globs->query_count++;
  71. for (i = 0; i < globs->num_dispatch; i++) {
  72. if (globs->dispatch[i].operation == operation_id) {
  73. *no_cache = globs->no_cache;
  74. return globs->dispatch[i].alg;
  75. }
  76. }
  77. /* No filter set, so pass it down to the chained provider */
  78. return OSSL_PROVIDER_query_operation(globs->deflt, operation_id, no_cache);
  79. }
  80. static void filter_unquery(void *provctx, int operation_id,
  81. const OSSL_ALGORITHM *algs)
  82. {
  83. struct filter_prov_globals_st *globs = get_globals();
  84. int i;
  85. if (!TEST_ulong_gt(globs->query_count, 0))
  86. globs->error = 1;
  87. else
  88. globs->query_count--;
  89. for (i = 0; i < globs->num_dispatch; i++)
  90. if (globs->dispatch[i].alg == algs)
  91. return;
  92. OSSL_PROVIDER_unquery_operation(globs->deflt, operation_id, algs);
  93. }
  94. static void filter_teardown(void *provctx)
  95. {
  96. struct filter_prov_globals_st *globs = get_globals();
  97. OSSL_PROVIDER_unload(globs->deflt);
  98. OSSL_LIB_CTX_free(globs->libctx);
  99. memset(globs, 0, sizeof(*globs));
  100. }
  101. /* Functions we provide to the core */
  102. static const OSSL_DISPATCH filter_dispatch_table[] = {
  103. { OSSL_FUNC_PROVIDER_GETTABLE_PARAMS, (void (*)(void))filter_gettable_params },
  104. { OSSL_FUNC_PROVIDER_GET_PARAMS, (void (*)(void))filter_get_params },
  105. { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))filter_query },
  106. { OSSL_FUNC_PROVIDER_UNQUERY_OPERATION, (void (*)(void))filter_unquery },
  107. { OSSL_FUNC_PROVIDER_GET_CAPABILITIES, (void (*)(void))filter_get_capabilities },
  108. { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))filter_teardown },
  109. OSSL_DISPATCH_END
  110. };
  111. int filter_provider_init(const OSSL_CORE_HANDLE *handle,
  112. const OSSL_DISPATCH *in,
  113. const OSSL_DISPATCH **out,
  114. void **provctx)
  115. {
  116. memset(&ourglobals, 0, sizeof(ourglobals));
  117. ourglobals.libctx = OSSL_LIB_CTX_new();
  118. if (ourglobals.libctx == NULL)
  119. goto err;
  120. ourglobals.deflt = OSSL_PROVIDER_load(ourglobals.libctx, "default");
  121. if (ourglobals.deflt == NULL)
  122. goto err;
  123. *provctx = OSSL_PROVIDER_get0_provider_ctx(ourglobals.deflt);
  124. *out = filter_dispatch_table;
  125. return 1;
  126. err:
  127. OSSL_PROVIDER_unload(ourglobals.deflt);
  128. OSSL_LIB_CTX_free(ourglobals.libctx);
  129. return 0;
  130. }
  131. /*
  132. * Set a filter for the given operation id. The filter string is a colon
  133. * separated list of algorithms that will be made available by this provider.
  134. * Anything not in the filter will be suppressed. If a filter is not set for
  135. * a given operation id then all algorithms are made available.
  136. */
  137. int filter_provider_set_filter(int operation, const char *filterstr)
  138. {
  139. int no_cache = 0;
  140. int algnum = 0, last = 0, ret = 0;
  141. struct filter_prov_globals_st *globs = get_globals();
  142. size_t namelen;
  143. char *filterstrtmp = OPENSSL_strdup(filterstr);
  144. char *name, *sep;
  145. const OSSL_ALGORITHM *provalgs = OSSL_PROVIDER_query_operation(globs->deflt,
  146. operation,
  147. &no_cache);
  148. const OSSL_ALGORITHM *algs;
  149. if (filterstrtmp == NULL)
  150. goto err;
  151. /* Nothing to filter */
  152. if (provalgs == NULL)
  153. goto err;
  154. if (globs->num_dispatch >= MAX_FILTERS)
  155. goto err;
  156. for (name = filterstrtmp; !last; name = (sep == NULL ? NULL : sep + 1)) {
  157. sep = strstr(name, ":");
  158. if (sep != NULL)
  159. *sep = '\0';
  160. else
  161. last = 1;
  162. namelen = strlen(name);
  163. for (algs = provalgs; algs->algorithm_names != NULL; algs++) {
  164. const char *found = strstr(algs->algorithm_names, name);
  165. if (found == NULL)
  166. continue;
  167. if (found[namelen] != '\0' && found[namelen] != ':')
  168. continue;
  169. if (found != algs->algorithm_names && found[-1] != ':')
  170. continue;
  171. /* We found a match */
  172. if (algnum >= MAX_ALG_FILTERS)
  173. goto err;
  174. globs->dispatch[globs->num_dispatch].alg[algnum++] = *algs;
  175. break;
  176. }
  177. if (algs->algorithm_names == NULL) {
  178. /* No match found */
  179. goto err;
  180. }
  181. }
  182. globs->dispatch[globs->num_dispatch].operation = operation;
  183. globs->no_cache = no_cache;
  184. globs->num_dispatch++;
  185. ret = 1;
  186. err:
  187. OSSL_PROVIDER_unquery_operation(globs->deflt, operation, provalgs);
  188. OPENSSL_free(filterstrtmp);
  189. return ret;
  190. }
  191. /*
  192. * Test if a filter provider is in a clean finishing state.
  193. * If it is return 1, otherwise return 0.
  194. */
  195. int filter_provider_check_clean_finish(void)
  196. {
  197. struct filter_prov_globals_st *globs = get_globals();
  198. return TEST_ulong_eq(globs->query_count, 0) && !globs->error;
  199. }