filterprov.c 6.3 KB

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