2 * Copyright 2020 The OpenSSL Project Authors. All Rights Reserved.
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
11 * A filtering provider for test purposes. We pass all calls through to the
12 * default provider except where we want other behaviour for a test.
16 #include <openssl/core.h>
17 #include <openssl/core_numbers.h>
18 #include <openssl/provider.h>
19 #include <openssl/crypto.h>
21 OSSL_provider_init_fn filter_provider_init;
23 int filter_provider_set_filter(int operation, const char *name);
25 #define MAX_FILTERS 10
26 #define MAX_ALG_FILTERS 5
28 struct filter_prov_globals_st {
33 OSSL_ALGORITHM alg[MAX_ALG_FILTERS + 1];
34 } dispatch[MAX_FILTERS];
38 static struct filter_prov_globals_st ourglobals;
40 static struct filter_prov_globals_st *get_globals(void)
43 * Ideally we'd like to store this in the OPENSSL_CTX so that we can have
44 * more than one instance of the filter provider at a time. But for now we
45 * just make it simple.
50 static OSSL_provider_gettable_params_fn filter_gettable_params;
51 static OSSL_provider_get_params_fn filter_get_params;
52 static OSSL_provider_query_operation_fn filter_query;
53 static OSSL_provider_teardown_fn filter_teardown;
55 static const OSSL_PARAM *filter_gettable_params(void *provctx)
57 struct filter_prov_globals_st *globs = get_globals();
59 return OSSL_PROVIDER_gettable_params(globs->deflt);
62 static int filter_get_params(void *provctx, OSSL_PARAM params[])
64 struct filter_prov_globals_st *globs = get_globals();
66 return OSSL_PROVIDER_get_params(globs->deflt, params);
69 static const OSSL_ALGORITHM *filter_query(void *provctx,
73 struct filter_prov_globals_st *globs = get_globals();
76 for (i = 0; i < globs->num_dispatch; i++) {
77 if (globs->dispatch[i].operation == operation_id) {
79 return globs->dispatch[i].alg;
83 /* No filter set, so pass it down to the chained provider */
84 return OSSL_PROVIDER_query_operation(globs->deflt, operation_id, no_cache);
87 static void filter_teardown(void *provctx)
89 struct filter_prov_globals_st *globs = get_globals();
91 OSSL_PROVIDER_unload(globs->deflt);
92 OPENSSL_CTX_free(globs->libctx);
95 /* Functions we provide to the core */
96 static const OSSL_DISPATCH filter_dispatch_table[] = {
97 { OSSL_FUNC_PROVIDER_GETTABLE_PARAMS, (void (*)(void))filter_gettable_params },
98 { OSSL_FUNC_PROVIDER_GET_PARAMS, (void (*)(void))filter_get_params },
99 { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))filter_query },
100 { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))filter_teardown },
104 int filter_provider_init(const OSSL_CORE_HANDLE *handle,
105 const OSSL_DISPATCH *in,
106 const OSSL_DISPATCH **out,
109 memset(&ourglobals, 0, sizeof(ourglobals));
110 ourglobals.libctx = OPENSSL_CTX_new();
111 if (ourglobals.libctx == NULL)
114 ourglobals.deflt = OSSL_PROVIDER_load(ourglobals.libctx, "default");
115 if (ourglobals.deflt == NULL)
118 *provctx = OSSL_PROVIDER_get0_provider_ctx(ourglobals.deflt);
119 *out = filter_dispatch_table;
123 OSSL_PROVIDER_unload(ourglobals.deflt);
124 OPENSSL_CTX_free(ourglobals.libctx);
129 * Set a filter for the given operation id. The filter string is a colon
130 * separated list of algorithms that will be made available by this provider.
131 * Anything not in the filter will be suppressed. If a filter is not set for
132 * a given operation id then all algorithms are made available.
134 int filter_provider_set_filter(int operation, const char *filterstr)
137 int algnum = 0, last = 0, ret = 0;
138 struct filter_prov_globals_st *globs = get_globals();
140 char *filterstrtmp = OPENSSL_strdup(filterstr);
142 const OSSL_ALGORITHM *provalgs = OSSL_PROVIDER_query_operation(globs->deflt,
145 const OSSL_ALGORITHM *algs;
147 if (filterstrtmp == NULL)
150 /* We don't support no_cache */
154 /* Nothing to filter */
155 if (provalgs == NULL)
158 if (globs->num_dispatch >= MAX_FILTERS)
161 for (name = filterstrtmp; !last; name = sep + 1) {
162 sep = strstr(name, ":");
167 namelen = strlen(name);
169 for (algs = provalgs; algs->algorithm_names != NULL; algs++) {
170 const char *found = strstr(algs->algorithm_names, name);
174 if (found[namelen] != '\0' && found[namelen] != ':')
176 if (found != algs->algorithm_names && found[-1] != ':')
179 /* We found a match */
180 if (algnum >= MAX_ALG_FILTERS)
183 globs->dispatch[globs->num_dispatch].alg[algnum++] = *algs;
186 if (algs->algorithm_names == NULL) {
192 globs->dispatch[globs->num_dispatch].operation = operation;
193 globs->num_dispatch++;
197 OPENSSL_free(filterstrtmp);