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