Add a test for OSSL_LIB_CTX_new_child()
[openssl.git] / test / p_test.c
1 /*
2  * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
10 /*
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.
14  */
15
16 #include <string.h>
17 #include <stdio.h>
18
19 /*
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
23  * object form.
24  */
25 #ifdef PROVIDER_INIT_FUNCTION_NAME
26 # define OSSL_provider_init PROVIDER_INIT_FUNCTION_NAME
27 #endif
28
29 #include "e_os.h"
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>
35
36 typedef struct p_test_ctx {
37     char *thisfile;
38     char *thisfunc;
39     const OSSL_CORE_HANDLE *handle;
40     OSSL_LIB_CTX *libctx;
41 } P_TEST_CTX;
42
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;
48
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 }
54 };
55
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;
61
62 static const OSSL_PARAM *p_gettable_params(void *_)
63 {
64     return p_param_types;
65 }
66
67 static int p_get_params(void *provctx, OSSL_PARAM params[])
68 {
69     P_TEST_CTX *ctx = (P_TEST_CTX *)provctx;
70     const OSSL_CORE_HANDLE *hand = ctx->handle;
71     OSSL_PARAM *p = params;
72     int ok = 1;
73
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},
85
86                 /* This might be present, if there's such a configuration */
87                 { "greeting", OSSL_PARAM_UTF8_PTR,
88                   &greeting, sizeof(&greeting), 0 },
89
90                 { NULL, 0, NULL, 0, 0 }
91             };
92             char buf[256];
93             size_t buf_l;
94
95             opensslv = provname = greeting = NULL;
96
97             if (c_get_params(hand, counter_request)) {
98                 if (greeting) {
99                     strcpy(buf, greeting);
100                 } else {
101                     const char *versionp = *(void **)counter_request[0].data;
102                     const char *namep = *(void **)counter_request[1].data;
103
104                     sprintf(buf, "Hello OpenSSL %.20s, greetings from %s!",
105                             versionp, namep);
106                 }
107             } else {
108                 sprintf(buf, "Howdy stranger...");
109             }
110
111             p->return_size = buf_l = strlen(buf) + 1;
112             if (p->data_size >= buf_l)
113                 strcpy(p->data, buf);
114             else
115                 ok = 0;
116         } else if (strcmp(p->key, "digest-check") == 0) {
117             unsigned int digestsuccess = 0;
118
119             /*
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.
123              */
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];
129
130             if (md4 != NULL && mdctx != NULL) {
131                 if (EVP_DigestInit_ex(mdctx, md4, NULL)
132                         && EVP_DigestUpdate(mdctx, (const unsigned char *)msg,
133                                             strlen(msg))
134                         &&EVP_DigestFinal(mdctx, out, NULL))
135                     digestsuccess = 1;
136             }
137             EVP_MD_CTX_free(mdctx);
138             EVP_MD_free(md4);
139 #endif
140             if (p->data_size >= sizeof(digestsuccess)) {
141                 *(unsigned int *)p->data = digestsuccess;
142                 p->return_size = sizeof(digestsuccess);
143             } else {
144                 ok = 0;
145             }
146         }
147     }
148     return ok;
149 }
150
151 static void p_set_error(int lib, int reason, const char *file, int line,
152                         const char *func, const char *fmt, ...)
153 {
154     va_list ap;
155
156     va_start(ap, fmt);
157     c_new_error(NULL);
158     c_set_error_debug(NULL, file, line, func);
159     c_vset_error(NULL, ERR_PACK(lib, 0, reason), fmt, ap);
160     va_end(ap);
161 }
162
163 static const OSSL_ITEM *p_get_reason_strings(void *_)
164 {
165     static const OSSL_ITEM reason_strings[] = {
166         {1, "dummy reason string"},
167         {0, NULL}
168     };
169
170     return reason_strings;
171 }
172
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 },
179     { 0, NULL }
180 };
181
182 int OSSL_provider_init(const OSSL_CORE_HANDLE *handle,
183                        const OSSL_DISPATCH *oin,
184                        const OSSL_DISPATCH **out,
185                        void **provctx)
186 {
187     P_TEST_CTX *ctx;
188     const OSSL_DISPATCH *in = oin;
189
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);
194             break;
195         case OSSL_FUNC_CORE_GET_PARAMS:
196             c_get_params = OSSL_FUNC_core_get_params(in);
197             break;
198         case OSSL_FUNC_CORE_NEW_ERROR:
199             c_new_error = OSSL_FUNC_core_new_error(in);
200             break;
201         case OSSL_FUNC_CORE_SET_ERROR_DEBUG:
202             c_set_error_debug = OSSL_FUNC_core_set_error_debug(in);
203             break;
204         case OSSL_FUNC_CORE_VSET_ERROR:
205             c_vset_error = OSSL_FUNC_core_vset_error(in);
206             break;
207         default:
208             /* Just ignore anything we don't understand */
209             break;
210         }
211     }
212
213     /*
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
222      */
223     ctx = malloc(sizeof(*ctx));
224     if (ctx == NULL)
225         return 0;
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) {
233         p_teardown(ctx);
234         return 0;
235     }
236 #endif
237
238     /*
239      * Set a spurious error to check error handling works correctly. This will
240      * be ignored
241      */
242     p_set_error(ERR_LIB_PROV, 1, ctx->thisfile, OPENSSL_LINE, ctx->thisfunc, NULL);
243
244     *provctx = (void *)ctx;
245     *out = p_test_table;
246     return 1;
247 }
248
249 static void p_teardown(void *provctx)
250 {
251     P_TEST_CTX *ctx = (P_TEST_CTX *)provctx;
252
253 #ifdef PROVIDER_INIT_FUNCTION_NAME
254     OSSL_LIB_CTX_free(ctx->libctx);
255 #endif
256     free(ctx->thisfile);
257     free(ctx->thisfunc);
258     free(ctx);
259 }