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