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>
34 typedef struct p_test_ctx {
37 const OSSL_CORE_HANDLE *handle;
40 static OSSL_FUNC_core_gettable_params_fn *c_gettable_params = NULL;
41 static OSSL_FUNC_core_get_params_fn *c_get_params = NULL;
42 static OSSL_FUNC_core_new_error_fn *c_new_error;
43 static OSSL_FUNC_core_set_error_debug_fn *c_set_error_debug;
44 static OSSL_FUNC_core_vset_error_fn *c_vset_error;
46 /* Tell the core what params we provide and what type they are */
47 static const OSSL_PARAM p_param_types[] = {
48 { "greeting", OSSL_PARAM_UTF8_STRING, NULL, 0, 0 },
49 { NULL, 0, NULL, 0, 0 }
52 /* This is a trick to ensure we define the provider functions correctly */
53 static OSSL_FUNC_provider_gettable_params_fn p_gettable_params;
54 static OSSL_FUNC_provider_get_params_fn p_get_params;
55 static OSSL_FUNC_provider_get_reason_strings_fn p_get_reason_strings;
56 static OSSL_FUNC_provider_teardown_fn p_teardown;
58 static const OSSL_PARAM *p_gettable_params(void *_)
63 static int p_get_params(void *provctx, OSSL_PARAM params[])
65 P_TEST_CTX *ctx = (P_TEST_CTX *)provctx;
66 const OSSL_CORE_HANDLE *hand = ctx->handle;
67 OSSL_PARAM *p = params;
70 for (; ok && p->key != NULL; p++) {
71 if (strcmp(p->key, "greeting") == 0) {
72 static char *opensslv;
73 static char *provname;
74 static char *greeting;
75 static OSSL_PARAM counter_request[] = {
76 /* Known libcrypto provided parameters */
77 { "openssl-version", OSSL_PARAM_UTF8_PTR,
78 &opensslv, sizeof(&opensslv), 0 },
79 { "provider-name", OSSL_PARAM_UTF8_PTR,
80 &provname, sizeof(&provname), 0},
82 /* This might be present, if there's such a configuration */
83 { "greeting", OSSL_PARAM_UTF8_PTR,
84 &greeting, sizeof(&greeting), 0 },
86 { NULL, 0, NULL, 0, 0 }
91 opensslv = provname = greeting = NULL;
93 if (c_get_params(hand, counter_request)) {
95 strcpy(buf, greeting);
97 const char *versionp = *(void **)counter_request[0].data;
98 const char *namep = *(void **)counter_request[1].data;
100 sprintf(buf, "Hello OpenSSL %.20s, greetings from %s!",
104 sprintf(buf, "Howdy stranger...");
107 p->return_size = buf_l = strlen(buf) + 1;
108 if (p->data_size >= buf_l)
109 strcpy(p->data, buf);
117 static void p_set_error(int lib, int reason, const char *file, int line,
118 const char *func, const char *fmt, ...)
124 c_set_error_debug(NULL, file, line, func);
125 c_vset_error(NULL, ERR_PACK(lib, 0, reason), fmt, ap);
129 static const OSSL_ITEM *p_get_reason_strings(void *_)
131 static const OSSL_ITEM reason_strings[] = {
132 {1, "dummy reason string"},
136 return reason_strings;
139 static const OSSL_DISPATCH p_test_table[] = {
140 { OSSL_FUNC_PROVIDER_GETTABLE_PARAMS, (void (*)(void))p_gettable_params },
141 { OSSL_FUNC_PROVIDER_GET_PARAMS, (void (*)(void))p_get_params },
142 { OSSL_FUNC_PROVIDER_GET_REASON_STRINGS,
143 (void (*)(void))p_get_reason_strings},
144 { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))p_teardown },
148 int OSSL_provider_init(const OSSL_CORE_HANDLE *handle,
149 const OSSL_DISPATCH *in,
150 const OSSL_DISPATCH **out,
155 for (; in->function_id != 0; in++) {
156 switch (in->function_id) {
157 case OSSL_FUNC_CORE_GETTABLE_PARAMS:
158 c_gettable_params = OSSL_FUNC_core_gettable_params(in);
160 case OSSL_FUNC_CORE_GET_PARAMS:
161 c_get_params = OSSL_FUNC_core_get_params(in);
163 case OSSL_FUNC_CORE_NEW_ERROR:
164 c_new_error = OSSL_FUNC_core_new_error(in);
166 case OSSL_FUNC_CORE_SET_ERROR_DEBUG:
167 c_set_error_debug = OSSL_FUNC_core_set_error_debug(in);
169 case OSSL_FUNC_CORE_VSET_ERROR:
170 c_vset_error = OSSL_FUNC_core_vset_error(in);
173 /* Just ignore anything we don't understand */
179 * We want to test that libcrypto doesn't use the file and func pointers
180 * that we provide to it via c_set_error_debug beyond the time that they
181 * are valid for. Therefore we dynamically allocate these strings now and
182 * free them again when the provider is torn down. If anything tries to
183 * use those strings after that point there will be a use-after-free and
184 * asan will complain (and hence the tests will fail).
185 * This file isn't linked against libcrypto, so we use malloc and strdup
186 * instead of OPENSSL_malloc and OPENSSL_strdup
188 ctx = malloc(sizeof(*ctx));
191 ctx->thisfile = strdup(OPENSSL_FILE);
192 ctx->thisfunc = strdup(OPENSSL_FUNC);
193 ctx->handle = handle;
196 * Set a spurious error to check error handling works correctly. This will
199 p_set_error(ERR_LIB_PROV, 1, ctx->thisfile, OPENSSL_LINE, ctx->thisfunc, NULL);
201 *provctx = (void *)ctx;
206 static void p_teardown(void *provctx)
208 P_TEST_CTX *ctx = (P_TEST_CTX *)provctx;