1*e0c4386eSCy Schubert /*
2*e0c4386eSCy Schubert * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
3*e0c4386eSCy Schubert *
4*e0c4386eSCy Schubert * Licensed under the Apache License 2.0 (the "License"). You may not use
5*e0c4386eSCy Schubert * this file except in compliance with the License. You can obtain a copy
6*e0c4386eSCy Schubert * in the file LICENSE in the source distribution or at
7*e0c4386eSCy Schubert * https://www.openssl.org/source/license.html
8*e0c4386eSCy Schubert */
9*e0c4386eSCy Schubert
10*e0c4386eSCy Schubert /*
11*e0c4386eSCy Schubert * This is a very simple provider that does absolutely nothing except respond
12*e0c4386eSCy Schubert * to provider global parameter requests. It does this by simply echoing back
13*e0c4386eSCy Schubert * a parameter request it makes to the loading library.
14*e0c4386eSCy Schubert */
15*e0c4386eSCy Schubert
16*e0c4386eSCy Schubert #include <string.h>
17*e0c4386eSCy Schubert #include <stdio.h>
18*e0c4386eSCy Schubert
19*e0c4386eSCy Schubert /*
20*e0c4386eSCy Schubert * When built as an object file to link the application with, we get the
21*e0c4386eSCy Schubert * init function name through the macro PROVIDER_INIT_FUNCTION_NAME. If
22*e0c4386eSCy Schubert * not defined, we use the standard init function name for the shared
23*e0c4386eSCy Schubert * object form.
24*e0c4386eSCy Schubert */
25*e0c4386eSCy Schubert #ifdef PROVIDER_INIT_FUNCTION_NAME
26*e0c4386eSCy Schubert # define OSSL_provider_init PROVIDER_INIT_FUNCTION_NAME
27*e0c4386eSCy Schubert #endif
28*e0c4386eSCy Schubert
29*e0c4386eSCy Schubert #include "e_os.h"
30*e0c4386eSCy Schubert #include <openssl/core.h>
31*e0c4386eSCy Schubert #include <openssl/core_dispatch.h>
32*e0c4386eSCy Schubert #include <openssl/err.h>
33*e0c4386eSCy Schubert #include <openssl/evp.h>
34*e0c4386eSCy Schubert #include <openssl/crypto.h>
35*e0c4386eSCy Schubert #include <openssl/provider.h>
36*e0c4386eSCy Schubert
37*e0c4386eSCy Schubert typedef struct p_test_ctx {
38*e0c4386eSCy Schubert char *thisfile;
39*e0c4386eSCy Schubert char *thisfunc;
40*e0c4386eSCy Schubert const OSSL_CORE_HANDLE *handle;
41*e0c4386eSCy Schubert OSSL_LIB_CTX *libctx;
42*e0c4386eSCy Schubert } P_TEST_CTX;
43*e0c4386eSCy Schubert
44*e0c4386eSCy Schubert static OSSL_FUNC_core_gettable_params_fn *c_gettable_params = NULL;
45*e0c4386eSCy Schubert static OSSL_FUNC_core_get_params_fn *c_get_params = NULL;
46*e0c4386eSCy Schubert static OSSL_FUNC_core_new_error_fn *c_new_error;
47*e0c4386eSCy Schubert static OSSL_FUNC_core_set_error_debug_fn *c_set_error_debug;
48*e0c4386eSCy Schubert static OSSL_FUNC_core_vset_error_fn *c_vset_error;
49*e0c4386eSCy Schubert
50*e0c4386eSCy Schubert /* Tell the core what params we provide and what type they are */
51*e0c4386eSCy Schubert static const OSSL_PARAM p_param_types[] = {
52*e0c4386eSCy Schubert { "greeting", OSSL_PARAM_UTF8_STRING, NULL, 0, 0 },
53*e0c4386eSCy Schubert { "digest-check", OSSL_PARAM_UNSIGNED_INTEGER, NULL, 0, 0},
54*e0c4386eSCy Schubert { NULL, 0, NULL, 0, 0 }
55*e0c4386eSCy Schubert };
56*e0c4386eSCy Schubert
57*e0c4386eSCy Schubert /* This is a trick to ensure we define the provider functions correctly */
58*e0c4386eSCy Schubert static OSSL_FUNC_provider_gettable_params_fn p_gettable_params;
59*e0c4386eSCy Schubert static OSSL_FUNC_provider_get_params_fn p_get_params;
60*e0c4386eSCy Schubert static OSSL_FUNC_provider_get_reason_strings_fn p_get_reason_strings;
61*e0c4386eSCy Schubert static OSSL_FUNC_provider_teardown_fn p_teardown;
62*e0c4386eSCy Schubert
p_set_error(int lib,int reason,const char * file,int line,const char * func,const char * fmt,...)63*e0c4386eSCy Schubert static void p_set_error(int lib, int reason, const char *file, int line,
64*e0c4386eSCy Schubert const char *func, const char *fmt, ...)
65*e0c4386eSCy Schubert {
66*e0c4386eSCy Schubert va_list ap;
67*e0c4386eSCy Schubert
68*e0c4386eSCy Schubert va_start(ap, fmt);
69*e0c4386eSCy Schubert c_new_error(NULL);
70*e0c4386eSCy Schubert c_set_error_debug(NULL, file, line, func);
71*e0c4386eSCy Schubert c_vset_error(NULL, ERR_PACK(lib, 0, reason), fmt, ap);
72*e0c4386eSCy Schubert va_end(ap);
73*e0c4386eSCy Schubert }
74*e0c4386eSCy Schubert
p_gettable_params(void * _)75*e0c4386eSCy Schubert static const OSSL_PARAM *p_gettable_params(void *_)
76*e0c4386eSCy Schubert {
77*e0c4386eSCy Schubert return p_param_types;
78*e0c4386eSCy Schubert }
79*e0c4386eSCy Schubert
p_get_params(void * provctx,OSSL_PARAM params[])80*e0c4386eSCy Schubert static int p_get_params(void *provctx, OSSL_PARAM params[])
81*e0c4386eSCy Schubert {
82*e0c4386eSCy Schubert P_TEST_CTX *ctx = (P_TEST_CTX *)provctx;
83*e0c4386eSCy Schubert const OSSL_CORE_HANDLE *hand = ctx->handle;
84*e0c4386eSCy Schubert OSSL_PARAM *p = params;
85*e0c4386eSCy Schubert int ok = 1;
86*e0c4386eSCy Schubert
87*e0c4386eSCy Schubert for (; ok && p->key != NULL; p++) {
88*e0c4386eSCy Schubert if (strcmp(p->key, "greeting") == 0) {
89*e0c4386eSCy Schubert static char *opensslv;
90*e0c4386eSCy Schubert static char *provname;
91*e0c4386eSCy Schubert static char *greeting;
92*e0c4386eSCy Schubert static OSSL_PARAM counter_request[] = {
93*e0c4386eSCy Schubert /* Known libcrypto provided parameters */
94*e0c4386eSCy Schubert { "openssl-version", OSSL_PARAM_UTF8_PTR,
95*e0c4386eSCy Schubert &opensslv, sizeof(&opensslv), 0 },
96*e0c4386eSCy Schubert { "provider-name", OSSL_PARAM_UTF8_PTR,
97*e0c4386eSCy Schubert &provname, sizeof(&provname), 0},
98*e0c4386eSCy Schubert
99*e0c4386eSCy Schubert /* This might be present, if there's such a configuration */
100*e0c4386eSCy Schubert { "greeting", OSSL_PARAM_UTF8_PTR,
101*e0c4386eSCy Schubert &greeting, sizeof(&greeting), 0 },
102*e0c4386eSCy Schubert
103*e0c4386eSCy Schubert { NULL, 0, NULL, 0, 0 }
104*e0c4386eSCy Schubert };
105*e0c4386eSCy Schubert char buf[256];
106*e0c4386eSCy Schubert size_t buf_l;
107*e0c4386eSCy Schubert
108*e0c4386eSCy Schubert opensslv = provname = greeting = NULL;
109*e0c4386eSCy Schubert
110*e0c4386eSCy Schubert if (c_get_params(hand, counter_request)) {
111*e0c4386eSCy Schubert if (greeting) {
112*e0c4386eSCy Schubert strcpy(buf, greeting);
113*e0c4386eSCy Schubert } else {
114*e0c4386eSCy Schubert const char *versionp = *(void **)counter_request[0].data;
115*e0c4386eSCy Schubert const char *namep = *(void **)counter_request[1].data;
116*e0c4386eSCy Schubert
117*e0c4386eSCy Schubert sprintf(buf, "Hello OpenSSL %.20s, greetings from %s!",
118*e0c4386eSCy Schubert versionp, namep);
119*e0c4386eSCy Schubert }
120*e0c4386eSCy Schubert } else {
121*e0c4386eSCy Schubert sprintf(buf, "Howdy stranger...");
122*e0c4386eSCy Schubert }
123*e0c4386eSCy Schubert
124*e0c4386eSCy Schubert p->return_size = buf_l = strlen(buf) + 1;
125*e0c4386eSCy Schubert if (p->data_size >= buf_l)
126*e0c4386eSCy Schubert strcpy(p->data, buf);
127*e0c4386eSCy Schubert else
128*e0c4386eSCy Schubert ok = 0;
129*e0c4386eSCy Schubert } else if (strcmp(p->key, "digest-check") == 0) {
130*e0c4386eSCy Schubert unsigned int digestsuccess = 0;
131*e0c4386eSCy Schubert
132*e0c4386eSCy Schubert /*
133*e0c4386eSCy Schubert * Test we can use an algorithm from another provider. We're using
134*e0c4386eSCy Schubert * legacy to check that legacy is actually available and we haven't
135*e0c4386eSCy Schubert * just fallen back to default.
136*e0c4386eSCy Schubert */
137*e0c4386eSCy Schubert #ifdef PROVIDER_INIT_FUNCTION_NAME
138*e0c4386eSCy Schubert EVP_MD *md4 = EVP_MD_fetch(ctx->libctx, "MD4", NULL);
139*e0c4386eSCy Schubert EVP_MD_CTX *mdctx = EVP_MD_CTX_new();
140*e0c4386eSCy Schubert const char *msg = "Hello world";
141*e0c4386eSCy Schubert unsigned char out[16];
142*e0c4386eSCy Schubert OSSL_PROVIDER *deflt;
143*e0c4386eSCy Schubert
144*e0c4386eSCy Schubert /*
145*e0c4386eSCy Schubert * "default" has not been loaded into the parent libctx. We should be able
146*e0c4386eSCy Schubert * to explicitly load it as a non-child provider.
147*e0c4386eSCy Schubert */
148*e0c4386eSCy Schubert deflt = OSSL_PROVIDER_load(ctx->libctx, "default");
149*e0c4386eSCy Schubert if (deflt == NULL
150*e0c4386eSCy Schubert || !OSSL_PROVIDER_available(ctx->libctx, "default")) {
151*e0c4386eSCy Schubert /* We set error "3" for a failure to load the default provider */
152*e0c4386eSCy Schubert p_set_error(ERR_LIB_PROV, 3, ctx->thisfile, OPENSSL_LINE,
153*e0c4386eSCy Schubert ctx->thisfunc, NULL);
154*e0c4386eSCy Schubert ok = 0;
155*e0c4386eSCy Schubert }
156*e0c4386eSCy Schubert
157*e0c4386eSCy Schubert /*
158*e0c4386eSCy Schubert * We should have the default provider available that we loaded
159*e0c4386eSCy Schubert * ourselves, and the base and legacy providers which we inherit
160*e0c4386eSCy Schubert * from the parent libctx. We should also have "this" provider
161*e0c4386eSCy Schubert * available.
162*e0c4386eSCy Schubert */
163*e0c4386eSCy Schubert if (ok
164*e0c4386eSCy Schubert && OSSL_PROVIDER_available(ctx->libctx, "default")
165*e0c4386eSCy Schubert && OSSL_PROVIDER_available(ctx->libctx, "base")
166*e0c4386eSCy Schubert && OSSL_PROVIDER_available(ctx->libctx, "legacy")
167*e0c4386eSCy Schubert && OSSL_PROVIDER_available(ctx->libctx, "p_test")
168*e0c4386eSCy Schubert && md4 != NULL
169*e0c4386eSCy Schubert && mdctx != NULL) {
170*e0c4386eSCy Schubert if (EVP_DigestInit_ex(mdctx, md4, NULL)
171*e0c4386eSCy Schubert && EVP_DigestUpdate(mdctx, (const unsigned char *)msg,
172*e0c4386eSCy Schubert strlen(msg))
173*e0c4386eSCy Schubert && EVP_DigestFinal(mdctx, out, NULL))
174*e0c4386eSCy Schubert digestsuccess = 1;
175*e0c4386eSCy Schubert }
176*e0c4386eSCy Schubert EVP_MD_CTX_free(mdctx);
177*e0c4386eSCy Schubert EVP_MD_free(md4);
178*e0c4386eSCy Schubert OSSL_PROVIDER_unload(deflt);
179*e0c4386eSCy Schubert #endif
180*e0c4386eSCy Schubert if (p->data_size >= sizeof(digestsuccess)) {
181*e0c4386eSCy Schubert *(unsigned int *)p->data = digestsuccess;
182*e0c4386eSCy Schubert p->return_size = sizeof(digestsuccess);
183*e0c4386eSCy Schubert } else {
184*e0c4386eSCy Schubert ok = 0;
185*e0c4386eSCy Schubert }
186*e0c4386eSCy Schubert } else if (strcmp(p->key, "stop-property-mirror") == 0) {
187*e0c4386eSCy Schubert /*
188*e0c4386eSCy Schubert * Setting the default properties explicitly should stop mirroring
189*e0c4386eSCy Schubert * of properties from the parent libctx.
190*e0c4386eSCy Schubert */
191*e0c4386eSCy Schubert unsigned int stopsuccess = 0;
192*e0c4386eSCy Schubert
193*e0c4386eSCy Schubert #ifdef PROVIDER_INIT_FUNCTION_NAME
194*e0c4386eSCy Schubert stopsuccess = EVP_set_default_properties(ctx->libctx, NULL);
195*e0c4386eSCy Schubert #endif
196*e0c4386eSCy Schubert if (p->data_size >= sizeof(stopsuccess)) {
197*e0c4386eSCy Schubert *(unsigned int *)p->data = stopsuccess;
198*e0c4386eSCy Schubert p->return_size = sizeof(stopsuccess);
199*e0c4386eSCy Schubert } else {
200*e0c4386eSCy Schubert ok = 0;
201*e0c4386eSCy Schubert }
202*e0c4386eSCy Schubert }
203*e0c4386eSCy Schubert }
204*e0c4386eSCy Schubert return ok;
205*e0c4386eSCy Schubert }
206*e0c4386eSCy Schubert
p_get_reason_strings(void * _)207*e0c4386eSCy Schubert static const OSSL_ITEM *p_get_reason_strings(void *_)
208*e0c4386eSCy Schubert {
209*e0c4386eSCy Schubert static const OSSL_ITEM reason_strings[] = {
210*e0c4386eSCy Schubert {1, "dummy reason string"},
211*e0c4386eSCy Schubert {2, "Can't create child library context"},
212*e0c4386eSCy Schubert {3, "Can't load default provider"},
213*e0c4386eSCy Schubert {0, NULL}
214*e0c4386eSCy Schubert };
215*e0c4386eSCy Schubert
216*e0c4386eSCy Schubert return reason_strings;
217*e0c4386eSCy Schubert }
218*e0c4386eSCy Schubert
219*e0c4386eSCy Schubert static const OSSL_DISPATCH p_test_table[] = {
220*e0c4386eSCy Schubert { OSSL_FUNC_PROVIDER_GETTABLE_PARAMS, (void (*)(void))p_gettable_params },
221*e0c4386eSCy Schubert { OSSL_FUNC_PROVIDER_GET_PARAMS, (void (*)(void))p_get_params },
222*e0c4386eSCy Schubert { OSSL_FUNC_PROVIDER_GET_REASON_STRINGS,
223*e0c4386eSCy Schubert (void (*)(void))p_get_reason_strings},
224*e0c4386eSCy Schubert { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))p_teardown },
225*e0c4386eSCy Schubert { 0, NULL }
226*e0c4386eSCy Schubert };
227*e0c4386eSCy Schubert
OSSL_provider_init(const OSSL_CORE_HANDLE * handle,const OSSL_DISPATCH * oin,const OSSL_DISPATCH ** out,void ** provctx)228*e0c4386eSCy Schubert int OSSL_provider_init(const OSSL_CORE_HANDLE *handle,
229*e0c4386eSCy Schubert const OSSL_DISPATCH *oin,
230*e0c4386eSCy Schubert const OSSL_DISPATCH **out,
231*e0c4386eSCy Schubert void **provctx)
232*e0c4386eSCy Schubert {
233*e0c4386eSCy Schubert P_TEST_CTX *ctx;
234*e0c4386eSCy Schubert const OSSL_DISPATCH *in = oin;
235*e0c4386eSCy Schubert
236*e0c4386eSCy Schubert for (; in->function_id != 0; in++) {
237*e0c4386eSCy Schubert switch (in->function_id) {
238*e0c4386eSCy Schubert case OSSL_FUNC_CORE_GETTABLE_PARAMS:
239*e0c4386eSCy Schubert c_gettable_params = OSSL_FUNC_core_gettable_params(in);
240*e0c4386eSCy Schubert break;
241*e0c4386eSCy Schubert case OSSL_FUNC_CORE_GET_PARAMS:
242*e0c4386eSCy Schubert c_get_params = OSSL_FUNC_core_get_params(in);
243*e0c4386eSCy Schubert break;
244*e0c4386eSCy Schubert case OSSL_FUNC_CORE_NEW_ERROR:
245*e0c4386eSCy Schubert c_new_error = OSSL_FUNC_core_new_error(in);
246*e0c4386eSCy Schubert break;
247*e0c4386eSCy Schubert case OSSL_FUNC_CORE_SET_ERROR_DEBUG:
248*e0c4386eSCy Schubert c_set_error_debug = OSSL_FUNC_core_set_error_debug(in);
249*e0c4386eSCy Schubert break;
250*e0c4386eSCy Schubert case OSSL_FUNC_CORE_VSET_ERROR:
251*e0c4386eSCy Schubert c_vset_error = OSSL_FUNC_core_vset_error(in);
252*e0c4386eSCy Schubert break;
253*e0c4386eSCy Schubert default:
254*e0c4386eSCy Schubert /* Just ignore anything we don't understand */
255*e0c4386eSCy Schubert break;
256*e0c4386eSCy Schubert }
257*e0c4386eSCy Schubert }
258*e0c4386eSCy Schubert
259*e0c4386eSCy Schubert /*
260*e0c4386eSCy Schubert * We want to test that libcrypto doesn't use the file and func pointers
261*e0c4386eSCy Schubert * that we provide to it via c_set_error_debug beyond the time that they
262*e0c4386eSCy Schubert * are valid for. Therefore we dynamically allocate these strings now and
263*e0c4386eSCy Schubert * free them again when the provider is torn down. If anything tries to
264*e0c4386eSCy Schubert * use those strings after that point there will be a use-after-free and
265*e0c4386eSCy Schubert * asan will complain (and hence the tests will fail).
266*e0c4386eSCy Schubert * This file isn't linked against libcrypto, so we use malloc and strdup
267*e0c4386eSCy Schubert * instead of OPENSSL_malloc and OPENSSL_strdup
268*e0c4386eSCy Schubert */
269*e0c4386eSCy Schubert ctx = malloc(sizeof(*ctx));
270*e0c4386eSCy Schubert if (ctx == NULL)
271*e0c4386eSCy Schubert return 0;
272*e0c4386eSCy Schubert ctx->thisfile = strdup(OPENSSL_FILE);
273*e0c4386eSCy Schubert ctx->thisfunc = strdup(OPENSSL_FUNC);
274*e0c4386eSCy Schubert ctx->handle = handle;
275*e0c4386eSCy Schubert #ifdef PROVIDER_INIT_FUNCTION_NAME
276*e0c4386eSCy Schubert /* We only do this if we are linked with libcrypto */
277*e0c4386eSCy Schubert ctx->libctx = OSSL_LIB_CTX_new_child(handle, oin);
278*e0c4386eSCy Schubert if (ctx->libctx == NULL) {
279*e0c4386eSCy Schubert /* We set error "2" for a failure to create the child libctx*/
280*e0c4386eSCy Schubert p_set_error(ERR_LIB_PROV, 2, ctx->thisfile, OPENSSL_LINE, ctx->thisfunc,
281*e0c4386eSCy Schubert NULL);
282*e0c4386eSCy Schubert p_teardown(ctx);
283*e0c4386eSCy Schubert return 0;
284*e0c4386eSCy Schubert }
285*e0c4386eSCy Schubert /*
286*e0c4386eSCy Schubert * The default provider is loaded - but the default properties should not
287*e0c4386eSCy Schubert * allow its use.
288*e0c4386eSCy Schubert */
289*e0c4386eSCy Schubert {
290*e0c4386eSCy Schubert EVP_MD *sha256 = EVP_MD_fetch(ctx->libctx, "SHA2-256", NULL);
291*e0c4386eSCy Schubert if (sha256 != NULL) {
292*e0c4386eSCy Schubert EVP_MD_free(sha256);
293*e0c4386eSCy Schubert p_teardown(ctx);
294*e0c4386eSCy Schubert return 0;
295*e0c4386eSCy Schubert }
296*e0c4386eSCy Schubert }
297*e0c4386eSCy Schubert #endif
298*e0c4386eSCy Schubert
299*e0c4386eSCy Schubert /*
300*e0c4386eSCy Schubert * Set a spurious error to check error handling works correctly. This will
301*e0c4386eSCy Schubert * be ignored
302*e0c4386eSCy Schubert */
303*e0c4386eSCy Schubert p_set_error(ERR_LIB_PROV, 1, ctx->thisfile, OPENSSL_LINE, ctx->thisfunc, NULL);
304*e0c4386eSCy Schubert
305*e0c4386eSCy Schubert *provctx = (void *)ctx;
306*e0c4386eSCy Schubert *out = p_test_table;
307*e0c4386eSCy Schubert return 1;
308*e0c4386eSCy Schubert }
309*e0c4386eSCy Schubert
p_teardown(void * provctx)310*e0c4386eSCy Schubert static void p_teardown(void *provctx)
311*e0c4386eSCy Schubert {
312*e0c4386eSCy Schubert P_TEST_CTX *ctx = (P_TEST_CTX *)provctx;
313*e0c4386eSCy Schubert
314*e0c4386eSCy Schubert #ifdef PROVIDER_INIT_FUNCTION_NAME
315*e0c4386eSCy Schubert OSSL_LIB_CTX_free(ctx->libctx);
316*e0c4386eSCy Schubert #endif
317*e0c4386eSCy Schubert free(ctx->thisfile);
318*e0c4386eSCy Schubert free(ctx->thisfunc);
319*e0c4386eSCy Schubert free(ctx);
320*e0c4386eSCy Schubert }
321