Replace OSSL_ITEM with OSSL_PARAM as parameter descriptor, everywhere
[openssl.git] / test / p_test.c
1 /*
2  * Copyright 2019 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 <openssl/core.h>
30 #include <openssl/core_numbers.h>
31
32 static OSSL_core_get_param_types_fn *c_get_param_types = NULL;
33 static OSSL_core_get_params_fn *c_get_params = NULL;
34
35 /* Tell the core what params we provide and what type they are */
36 static const OSSL_PARAM p_param_types[] = {
37     { "greeting", OSSL_PARAM_UTF8_STRING, NULL, 0, 0 },
38     { NULL, 0, NULL, 0, 0 }
39 };
40
41 /* This is a trick to ensure we define the provider functions correctly */
42 static OSSL_provider_get_param_types_fn p_get_param_types;
43 static OSSL_provider_get_params_fn p_get_params;
44
45 static const OSSL_PARAM *p_get_param_types(void *_)
46 {
47     return p_param_types;
48 }
49
50 static int p_get_params(void *vprov, OSSL_PARAM params[])
51 {
52     const OSSL_PROVIDER *prov = vprov;
53     OSSL_PARAM *p = params;
54     int ok = 1;
55
56     for (; ok && p->key != NULL; p++) {
57         if (strcmp(p->key, "greeting") == 0) {
58             static char *opensslv;
59             static char *provname;
60             static char *greeting;
61             static OSSL_PARAM counter_request[] = {
62                 /* Known libcrypto provided parameters */
63                 { "openssl-version", OSSL_PARAM_UTF8_PTR,
64                   &opensslv, sizeof(&opensslv), 0 },
65                 { "provider-name", OSSL_PARAM_UTF8_PTR,
66                   &provname, sizeof(&provname), 0},
67
68                 /* This might be present, if there's such a configuration */
69                 { "greeting", OSSL_PARAM_UTF8_PTR,
70                   &greeting, sizeof(&greeting), 0 },
71
72                 { NULL, 0, NULL, 0, 0 }
73             };
74             char buf[256];
75             size_t buf_l;
76
77             opensslv = provname = greeting = NULL;
78
79             if (c_get_params(prov, counter_request)) {
80                 if (greeting) {
81                     strcpy(buf, greeting);
82                 } else {
83                     const char *versionp = *(void **)counter_request[0].data;
84                     const char *namep = *(void **)counter_request[1].data;
85
86                     sprintf(buf, "Hello OpenSSL %.20s, greetings from %s!",
87                             versionp, namep);
88                 }
89             } else {
90                 sprintf(buf, "Howdy stranger...");
91             }
92
93             p->return_size = buf_l = strlen(buf) + 1;
94             if (p->data_size >= buf_l)
95                 strcpy(p->data, buf);
96             else
97                 ok = 0;
98         }
99     }
100     return ok;
101 }
102
103 static const OSSL_DISPATCH p_test_table[] = {
104     { OSSL_FUNC_PROVIDER_GET_PARAM_TYPES, (void (*)(void))p_get_param_types },
105     { OSSL_FUNC_PROVIDER_GET_PARAMS, (void (*)(void))p_get_params },
106     { 0, NULL }
107 };
108
109 int OSSL_provider_init(const OSSL_PROVIDER *provider,
110                        const OSSL_DISPATCH *in,
111                        const OSSL_DISPATCH **out,
112                        void **provctx)
113 {
114     for (; in->function_id != 0; in++) {
115         switch (in->function_id) {
116         case OSSL_FUNC_CORE_GET_PARAM_TYPES:
117             c_get_param_types = OSSL_get_core_get_param_types(in);
118             break;
119         case OSSL_FUNC_CORE_GET_PARAMS:
120             c_get_params = OSSL_get_core_get_params(in);
121             break;
122         default:
123             /* Just ignore anything we don't understand */
124             break;
125         }
126     }
127
128     /* Because we use this in get_params, we need to pass it back */
129     *provctx = (void *)provider;
130
131     *out = p_test_table;
132     return 1;
133 }