2 * Copyright 2019-2021 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 * This is a very simple provider that does absolutely nothing except respond
12 * to provider global parameter requests. It does this by simply echoing back
13 * a parameter request it makes to the loading library.
20 * When built as an object file to link the application with, we get the
21 * init function name through the macro PROVIDER_INIT_FUNCTION_NAME. If
22 * not defined, we use the standard init function name for the shared
25 #ifdef PROVIDER_INIT_FUNCTION_NAME
26 # define OSSL_provider_init PROVIDER_INIT_FUNCTION_NAME
30 #include <openssl/core.h>
31 #include <openssl/core_dispatch.h>
32 #include <openssl/err.h>
33 #include <openssl/evp.h>
34 #include <openssl/crypto.h>
36 typedef struct p_test_ctx {
39 const OSSL_CORE_HANDLE *handle;
43 static OSSL_FUNC_core_gettable_params_fn *c_gettable_params = NULL;
44 static OSSL_FUNC_core_get_params_fn *c_get_params = NULL;
45 static OSSL_FUNC_core_new_error_fn *c_new_error;
46 static OSSL_FUNC_core_set_error_debug_fn *c_set_error_debug;
47 static OSSL_FUNC_core_vset_error_fn *c_vset_error;
49 /* Tell the core what params we provide and what type they are */
50 static const OSSL_PARAM p_param_types[] = {
51 { "greeting", OSSL_PARAM_UTF8_STRING, NULL, 0, 0 },
52 { "digest-check", OSSL_PARAM_UNSIGNED_INTEGER, NULL, 0, 0},
53 { NULL, 0, NULL, 0, 0 }
56 /* This is a trick to ensure we define the provider functions correctly */
57 static OSSL_FUNC_provider_gettable_params_fn p_gettable_params;
58 static OSSL_FUNC_provider_get_params_fn p_get_params;
59 static OSSL_FUNC_provider_get_reason_strings_fn p_get_reason_strings;
60 static OSSL_FUNC_provider_teardown_fn p_teardown;
62 static const OSSL_PARAM *p_gettable_params(void *_)
67 static int p_get_params(void *provctx, OSSL_PARAM params[])
69 P_TEST_CTX *ctx = (P_TEST_CTX *)provctx;
70 const OSSL_CORE_HANDLE *hand = ctx->handle;
71 OSSL_PARAM *p = params;
74 for (; ok && p->key != NULL; p++) {
75 if (strcmp(p->key, "greeting") == 0) {
76 static char *opensslv;
77 static char *provname;
78 static char *greeting;
79 static OSSL_PARAM counter_request[] = {
80 /* Known libcrypto provided parameters */
81 { "openssl-version", OSSL_PARAM_UTF8_PTR,
82 &opensslv, sizeof(&opensslv), 0 },
83 { "provider-name", OSSL_PARAM_UTF8_PTR,
84 &provname, sizeof(&provname), 0},
86 /* This might be present, if there's such a configuration */
87 { "greeting", OSSL_PARAM_UTF8_PTR,
88 &greeting, sizeof(&greeting), 0 },
90 { NULL, 0, NULL, 0, 0 }
95 opensslv = provname = greeting = NULL;
97 if (c_get_params(hand, counter_request)) {
99 strcpy(buf, greeting);
101 const char *versionp = *(void **)counter_request[0].data;
102 const char *namep = *(void **)counter_request[1].data;
104 sprintf(buf, "Hello OpenSSL %.20s, greetings from %s!",
108 sprintf(buf, "Howdy stranger...");
111 p->return_size = buf_l = strlen(buf) + 1;
112 if (p->data_size >= buf_l)
113 strcpy(p->data, buf);
116 } else if (strcmp(p->key, "digest-check") == 0) {
117 unsigned int digestsuccess = 0;
120 * Test we can use an algorithm from another provider. We're using
121 * legacy to check that legacy is actually available and we haven't
122 * just fallen back to default.
124 #ifdef PROVIDER_INIT_FUNCTION_NAME
125 EVP_MD *md4 = EVP_MD_fetch(ctx->libctx, "MD4", NULL);
126 EVP_MD_CTX *mdctx = EVP_MD_CTX_new();
127 const char *msg = "Hello world";
128 unsigned char out[16];
130 if (md4 != NULL && mdctx != NULL) {
131 if (EVP_DigestInit_ex(mdctx, md4, NULL)
132 && EVP_DigestUpdate(mdctx, (const unsigned char *)msg,
134 &&EVP_DigestFinal(mdctx, out, NULL))
137 EVP_MD_CTX_free(mdctx);
140 if (p->data_size >= sizeof(digestsuccess)) {
141 *(unsigned int *)p->data = digestsuccess;
142 p->return_size = sizeof(digestsuccess);
151 static void p_set_error(int lib, int reason, const char *file, int line,
152 const char *func, const char *fmt, ...)
158 c_set_error_debug(NULL, file, line, func);
159 c_vset_error(NULL, ERR_PACK(lib, 0, reason), fmt, ap);
163 static const OSSL_ITEM *p_get_reason_strings(void *_)
165 static const OSSL_ITEM reason_strings[] = {
166 {1, "dummy reason string"},
170 return reason_strings;
173 static const OSSL_DISPATCH p_test_table[] = {
174 { OSSL_FUNC_PROVIDER_GETTABLE_PARAMS, (void (*)(void))p_gettable_params },
175 { OSSL_FUNC_PROVIDER_GET_PARAMS, (void (*)(void))p_get_params },
176 { OSSL_FUNC_PROVIDER_GET_REASON_STRINGS,
177 (void (*)(void))p_get_reason_strings},
178 { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))p_teardown },
182 int OSSL_provider_init(const OSSL_CORE_HANDLE *handle,
183 const OSSL_DISPATCH *oin,
184 const OSSL_DISPATCH **out,
188 const OSSL_DISPATCH *in = oin;
190 for (; in->function_id != 0; in++) {
191 switch (in->function_id) {
192 case OSSL_FUNC_CORE_GETTABLE_PARAMS:
193 c_gettable_params = OSSL_FUNC_core_gettable_params(in);
195 case OSSL_FUNC_CORE_GET_PARAMS:
196 c_get_params = OSSL_FUNC_core_get_params(in);
198 case OSSL_FUNC_CORE_NEW_ERROR:
199 c_new_error = OSSL_FUNC_core_new_error(in);
201 case OSSL_FUNC_CORE_SET_ERROR_DEBUG:
202 c_set_error_debug = OSSL_FUNC_core_set_error_debug(in);
204 case OSSL_FUNC_CORE_VSET_ERROR:
205 c_vset_error = OSSL_FUNC_core_vset_error(in);
208 /* Just ignore anything we don't understand */
214 * We want to test that libcrypto doesn't use the file and func pointers
215 * that we provide to it via c_set_error_debug beyond the time that they
216 * are valid for. Therefore we dynamically allocate these strings now and
217 * free them again when the provider is torn down. If anything tries to
218 * use those strings after that point there will be a use-after-free and
219 * asan will complain (and hence the tests will fail).
220 * This file isn't linked against libcrypto, so we use malloc and strdup
221 * instead of OPENSSL_malloc and OPENSSL_strdup
223 ctx = malloc(sizeof(*ctx));
226 ctx->thisfile = strdup(OPENSSL_FILE);
227 ctx->thisfunc = strdup(OPENSSL_FUNC);
228 ctx->handle = handle;
229 #ifdef PROVIDER_INIT_FUNCTION_NAME
230 /* We only do this if we are linked with libcrypto */
231 ctx->libctx = OSSL_LIB_CTX_new_child(handle, oin);
232 if (ctx->libctx == NULL) {
239 * Set a spurious error to check error handling works correctly. This will
242 p_set_error(ERR_LIB_PROV, 1, ctx->thisfile, OPENSSL_LINE, ctx->thisfunc, NULL);
244 *provctx = (void *)ctx;
249 static void p_teardown(void *provctx)
251 P_TEST_CTX *ctx = (P_TEST_CTX *)provctx;
253 #ifdef PROVIDER_INIT_FUNCTION_NAME
254 OSSL_LIB_CTX_free(ctx->libctx);