]> git.ipfire.org Git - thirdparty/openssl.git/blame - test/p_test.c
Make sure we use the libctx when creating an EVP_PKEY_CTX in libssl
[thirdparty/openssl.git] / test / p_test.c
CommitLineData
021a6552
RL
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
dca97d00 32static OSSL_core_gettable_params_fn *c_gettable_params = NULL;
021a6552
RL
33static OSSL_core_get_params_fn *c_get_params = NULL;
34
35/* Tell the core what params we provide and what type they are */
26175013
RL
36static const OSSL_PARAM p_param_types[] = {
37 { "greeting", OSSL_PARAM_UTF8_STRING, NULL, 0, 0 },
38 { NULL, 0, NULL, 0, 0 }
021a6552
RL
39};
40
a39eb840 41/* This is a trick to ensure we define the provider functions correctly */
dca97d00 42static OSSL_provider_gettable_params_fn p_gettable_params;
a39eb840 43static OSSL_provider_get_params_fn p_get_params;
551543e5 44static OSSL_provider_get_reason_strings_fn p_get_reason_strings;
a39eb840 45
dca97d00 46static const OSSL_PARAM *p_gettable_params(void *_)
021a6552
RL
47{
48 return p_param_types;
49}
50
4e7991b4 51static int p_get_params(void *vprov, OSSL_PARAM params[])
021a6552 52{
a39eb840 53 const OSSL_PROVIDER *prov = vprov;
4e7991b4 54 OSSL_PARAM *p = params;
021a6552
RL
55 int ok = 1;
56
57 for (; ok && p->key != NULL; p++) {
58 if (strcmp(p->key, "greeting") == 0) {
1ccf4973
RL
59 static char *opensslv;
60 static char *provname;
61 static char *greeting;
4e7991b4 62 static OSSL_PARAM counter_request[] = {
6d872a83 63 /* Known libcrypto provided parameters */
e2146e12 64 { "openssl-version", OSSL_PARAM_UTF8_PTR,
4e7991b4 65 &opensslv, sizeof(&opensslv), 0 },
e2146e12 66 { "provider-name", OSSL_PARAM_UTF8_PTR,
4e7991b4 67 &provname, sizeof(&provname), 0},
6d872a83
RL
68
69 /* This might be present, if there's such a configuration */
70 { "greeting", OSSL_PARAM_UTF8_PTR,
4e7991b4 71 &greeting, sizeof(&greeting), 0 },
6d872a83 72
4e7991b4 73 { NULL, 0, NULL, 0, 0 }
021a6552
RL
74 };
75 char buf[256];
76 size_t buf_l;
77
1ccf4973
RL
78 opensslv = provname = greeting = NULL;
79
021a6552 80 if (c_get_params(prov, counter_request)) {
6d872a83
RL
81 if (greeting) {
82 strcpy(buf, greeting);
83 } else {
84 const char *versionp = *(void **)counter_request[0].data;
85 const char *namep = *(void **)counter_request[1].data;
86
87 sprintf(buf, "Hello OpenSSL %.20s, greetings from %s!",
88 versionp, namep);
89 }
021a6552
RL
90 } else {
91 sprintf(buf, "Howdy stranger...");
92 }
93
4e7991b4 94 p->return_size = buf_l = strlen(buf) + 1;
8c4412ed 95 if (p->data_size >= buf_l)
f663ddc7 96 strcpy(p->data, buf);
021a6552
RL
97 else
98 ok = 0;
99 }
100 }
101 return ok;
102}
103
551543e5
NT
104static const OSSL_ITEM *p_get_reason_strings(void *_)
105{
106 static const OSSL_ITEM reason_strings[] = {
107 {1, "dummy reason string"},
108 {0, NULL}
109 };
110
111 return reason_strings;
112}
113
021a6552 114static const OSSL_DISPATCH p_test_table[] = {
dca97d00 115 { OSSL_FUNC_PROVIDER_GETTABLE_PARAMS, (void (*)(void))p_gettable_params },
021a6552 116 { OSSL_FUNC_PROVIDER_GET_PARAMS, (void (*)(void))p_get_params },
551543e5
NT
117 { OSSL_FUNC_PROVIDER_GET_REASON_STRINGS,
118 (void (*)(void))p_get_reason_strings},
021a6552
RL
119 { 0, NULL }
120};
121
122int OSSL_provider_init(const OSSL_PROVIDER *provider,
123 const OSSL_DISPATCH *in,
a39eb840
RL
124 const OSSL_DISPATCH **out,
125 void **provctx)
021a6552
RL
126{
127 for (; in->function_id != 0; in++) {
128 switch (in->function_id) {
dca97d00
RL
129 case OSSL_FUNC_CORE_GETTABLE_PARAMS:
130 c_gettable_params = OSSL_get_core_gettable_params(in);
021a6552
RL
131 break;
132 case OSSL_FUNC_CORE_GET_PARAMS:
133 c_get_params = OSSL_get_core_get_params(in);
134 break;
135 default:
136 /* Just ignore anything we don't understand */
137 break;
138 }
139 }
140
a39eb840
RL
141 /* Because we use this in get_params, we need to pass it back */
142 *provctx = (void *)provider;
143
021a6552
RL
144 *out = p_test_table;
145 return 1;
146}