Remove extra trailing semicolon
[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
34 typedef struct p_test_ctx {
35     char *thisfile;
36     char *thisfunc;
37     const OSSL_CORE_HANDLE *handle;
38 } P_TEST_CTX;
39
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;
45
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 }
50 };
51
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;
57
58 static const OSSL_PARAM *p_gettable_params(void *_)
59 {
60     return p_param_types;
61 }
62
63 static int p_get_params(void *provctx, OSSL_PARAM params[])
64 {
65     P_TEST_CTX *ctx = (P_TEST_CTX *)provctx;
66     const OSSL_CORE_HANDLE *hand = ctx->handle;
67     OSSL_PARAM *p = params;
68     int ok = 1;
69
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},
81
82                 /* This might be present, if there's such a configuration */
83                 { "greeting", OSSL_PARAM_UTF8_PTR,
84                   &greeting, sizeof(&greeting), 0 },
85
86                 { NULL, 0, NULL, 0, 0 }
87             };
88             char buf[256];
89             size_t buf_l;
90
91             opensslv = provname = greeting = NULL;
92
93             if (c_get_params(hand, counter_request)) {
94                 if (greeting) {
95                     strcpy(buf, greeting);
96                 } else {
97                     const char *versionp = *(void **)counter_request[0].data;
98                     const char *namep = *(void **)counter_request[1].data;
99
100                     sprintf(buf, "Hello OpenSSL %.20s, greetings from %s!",
101                             versionp, namep);
102                 }
103             } else {
104                 sprintf(buf, "Howdy stranger...");
105             }
106
107             p->return_size = buf_l = strlen(buf) + 1;
108             if (p->data_size >= buf_l)
109                 strcpy(p->data, buf);
110             else
111                 ok = 0;
112         }
113     }
114     return ok;
115 }
116
117 static void p_set_error(int lib, int reason, const char *file, int line,
118                         const char *func, const char *fmt, ...)
119 {
120     va_list ap;
121
122     va_start(ap, fmt);
123     c_new_error(NULL);
124     c_set_error_debug(NULL, file, line, func);
125     c_vset_error(NULL, ERR_PACK(lib, 0, reason), fmt, ap);
126     va_end(ap);
127 }
128
129 static const OSSL_ITEM *p_get_reason_strings(void *_)
130 {
131     static const OSSL_ITEM reason_strings[] = {
132         {1, "dummy reason string"},
133         {0, NULL}
134     };
135
136     return reason_strings;
137 }
138
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 },
145     { 0, NULL }
146 };
147
148 int OSSL_provider_init(const OSSL_CORE_HANDLE *handle,
149                        const OSSL_DISPATCH *in,
150                        const OSSL_DISPATCH **out,
151                        void **provctx)
152 {
153     P_TEST_CTX *ctx;
154
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);
159             break;
160         case OSSL_FUNC_CORE_GET_PARAMS:
161             c_get_params = OSSL_FUNC_core_get_params(in);
162             break;
163         case OSSL_FUNC_CORE_NEW_ERROR:
164             c_new_error = OSSL_FUNC_core_new_error(in);
165             break;
166         case OSSL_FUNC_CORE_SET_ERROR_DEBUG:
167             c_set_error_debug = OSSL_FUNC_core_set_error_debug(in);
168             break;
169         case OSSL_FUNC_CORE_VSET_ERROR:
170             c_vset_error = OSSL_FUNC_core_vset_error(in);
171             break;
172         default:
173             /* Just ignore anything we don't understand */
174             break;
175         }
176     }
177
178     /*
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
187      */
188     ctx = malloc(sizeof(*ctx));
189     if (ctx == NULL)
190         return 0;
191     ctx->thisfile = strdup(OPENSSL_FILE);
192     ctx->thisfunc = strdup(OPENSSL_FUNC);
193     ctx->handle = handle;
194
195     /*
196      * Set a spurious error to check error handling works correctly. This will
197      * be ignored
198      */
199     p_set_error(ERR_LIB_PROV, 1, ctx->thisfile, OPENSSL_LINE, ctx->thisfunc, NULL);
200
201     *provctx = (void *)ctx;
202     *out = p_test_table;
203     return 1;
204 }
205
206 static void p_teardown(void *provctx)
207 {
208     P_TEST_CTX *ctx = (P_TEST_CTX *)provctx;
209
210     free(ctx->thisfile);
211     free(ctx->thisfunc);
212     free(ctx);
213 }