1b0d17251Schristos /*
2*0e2e28bcSchristos * Copyright 2019-2024 The OpenSSL Project Authors. All Rights Reserved.
3b0d17251Schristos *
4b0d17251Schristos * Licensed under the Apache License 2.0 (the "License");
5b0d17251Schristos * you may not use this file except in compliance with the License.
6b0d17251Schristos * You may obtain a copy of the License at
7b0d17251Schristos * https://www.openssl.org/source/license.html
8b0d17251Schristos * or in the file LICENSE in the source distribution.
9b0d17251Schristos */
10b0d17251Schristos
11b0d17251Schristos /*
12b0d17251Schristos * This program tests the use of OSSL_PARAM, currently in raw form.
13b0d17251Schristos */
14b0d17251Schristos
15b0d17251Schristos #include <string.h>
16b0d17251Schristos #include <openssl/bn.h>
17b0d17251Schristos #include <openssl/core.h>
18*0e2e28bcSchristos #include <openssl/err.h>
19b0d17251Schristos #include <openssl/params.h>
20b0d17251Schristos #include "internal/numbers.h"
21b0d17251Schristos #include "internal/nelem.h"
22b0d17251Schristos #include "testutil.h"
23b0d17251Schristos
24b0d17251Schristos /*-
25b0d17251Schristos * PROVIDER SECTION
26b0d17251Schristos * ================
27b0d17251Schristos *
28b0d17251Schristos * Even though it's not necessarily ONLY providers doing this part,
29b0d17251Schristos * they are naturally going to be the most common users of
30b0d17251Schristos * set_params and get_params functions.
31b0d17251Schristos */
32b0d17251Schristos
33b0d17251Schristos /*
34b0d17251Schristos * In real use cases, setters and getters would take an object with
35b0d17251Schristos * which the parameters are associated. This structure is a cheap
36b0d17251Schristos * simulation.
37b0d17251Schristos */
38b0d17251Schristos struct object_st {
39b0d17251Schristos /*
40b0d17251Schristos * Documented as a native integer, of the size given by sizeof(int).
41b0d17251Schristos * Assumed data type OSSL_PARAM_INTEGER
42b0d17251Schristos */
43b0d17251Schristos int p1;
44b0d17251Schristos /*
45b0d17251Schristos * Documented as a native double, of the size given by sizeof(double).
46b0d17251Schristos * Assumed data type OSSL_PARAM_REAL
47b0d17251Schristos */
48b0d17251Schristos double p2;
49b0d17251Schristos /*
50b0d17251Schristos * Documented as an arbitrarly large unsigned integer.
51b0d17251Schristos * The data size must be large enough to accommodate.
52b0d17251Schristos * Assumed data type OSSL_PARAM_UNSIGNED_INTEGER
53b0d17251Schristos */
54b0d17251Schristos BIGNUM *p3;
55b0d17251Schristos /*
56b0d17251Schristos * Documented as a C string.
57b0d17251Schristos * The data size must be large enough to accommodate.
58b0d17251Schristos * Assumed data type OSSL_PARAM_UTF8_STRING
59b0d17251Schristos */
60b0d17251Schristos char *p4;
61b0d17251Schristos size_t p4_l;
62b0d17251Schristos /*
63b0d17251Schristos * Documented as a C string.
64b0d17251Schristos * Assumed data type OSSL_PARAM_UTF8_STRING
65b0d17251Schristos */
66b0d17251Schristos char p5[256];
67b0d17251Schristos size_t p5_l;
68b0d17251Schristos /*
69b0d17251Schristos * Documented as a pointer to a constant C string.
70b0d17251Schristos * Assumed data type OSSL_PARAM_UTF8_PTR
71b0d17251Schristos */
72b0d17251Schristos const char *p6;
73b0d17251Schristos size_t p6_l;
74b0d17251Schristos };
75b0d17251Schristos
76b0d17251Schristos #define p1_init 42 /* The ultimate answer */
77b0d17251Schristos #define p2_init 6.283 /* Magic number */
78b0d17251Schristos /* Stolen from evp_data, BLAKE2s256 test */
79b0d17251Schristos #define p3_init \
80b0d17251Schristos "4142434445464748494a4b4c4d4e4f50" \
81b0d17251Schristos "5152535455565758595a616263646566" \
82b0d17251Schristos "6768696a6b6c6d6e6f70717273747576" \
83b0d17251Schristos "7778797a30313233343536373839"
84b0d17251Schristos #define p4_init "BLAKE2s256" /* Random string */
85b0d17251Schristos #define p5_init "Hellow World" /* Random string */
86b0d17251Schristos #define p6_init OPENSSL_FULL_VERSION_STR /* Static string */
87b0d17251Schristos
cleanup_object(void * vobj)88b0d17251Schristos static void cleanup_object(void *vobj)
89b0d17251Schristos {
90b0d17251Schristos struct object_st *obj = vobj;
91b0d17251Schristos
92b0d17251Schristos BN_free(obj->p3);
93b0d17251Schristos obj->p3 = NULL;
94b0d17251Schristos OPENSSL_free(obj->p4);
95b0d17251Schristos obj->p4 = NULL;
96b0d17251Schristos OPENSSL_free(obj);
97b0d17251Schristos }
98b0d17251Schristos
init_object(void)99b0d17251Schristos static void *init_object(void)
100b0d17251Schristos {
101b0d17251Schristos struct object_st *obj;
102b0d17251Schristos
103b0d17251Schristos if (!TEST_ptr(obj = OPENSSL_zalloc(sizeof(*obj))))
104b0d17251Schristos return NULL;
105b0d17251Schristos
106b0d17251Schristos obj->p1 = p1_init;
107b0d17251Schristos obj->p2 = p2_init;
108b0d17251Schristos if (!TEST_true(BN_hex2bn(&obj->p3, p3_init)))
109b0d17251Schristos goto fail;
110b0d17251Schristos if (!TEST_ptr(obj->p4 = OPENSSL_strdup(p4_init)))
111b0d17251Schristos goto fail;
112b0d17251Schristos strcpy(obj->p5, p5_init);
113b0d17251Schristos obj->p6 = p6_init;
114b0d17251Schristos
115b0d17251Schristos return obj;
116b0d17251Schristos fail:
117b0d17251Schristos cleanup_object(obj);
118b0d17251Schristos obj = NULL;
119b0d17251Schristos
120b0d17251Schristos return NULL;
121b0d17251Schristos }
122b0d17251Schristos
123b0d17251Schristos /*
124b0d17251Schristos * RAW provider, which handles the parameters in a very raw manner,
125b0d17251Schristos * with no fancy API and very minimal checking. The application that
126b0d17251Schristos * calls these to set or request parameters MUST get its OSSL_PARAM
127b0d17251Schristos * array right.
128b0d17251Schristos */
129b0d17251Schristos
raw_set_params(void * vobj,const OSSL_PARAM * params)130b0d17251Schristos static int raw_set_params(void *vobj, const OSSL_PARAM *params)
131b0d17251Schristos {
132b0d17251Schristos struct object_st *obj = vobj;
133b0d17251Schristos
134b0d17251Schristos for (; params->key != NULL; params++)
135b0d17251Schristos if (strcmp(params->key, "p1") == 0) {
136b0d17251Schristos obj->p1 = *(int *)params->data;
137b0d17251Schristos } else if (strcmp(params->key, "p2") == 0) {
138b0d17251Schristos obj->p2 = *(double *)params->data;
139b0d17251Schristos } else if (strcmp(params->key, "p3") == 0) {
140b0d17251Schristos BN_free(obj->p3);
141b0d17251Schristos if (!TEST_ptr(obj->p3 = BN_native2bn(params->data,
142b0d17251Schristos params->data_size, NULL)))
143b0d17251Schristos return 0;
144b0d17251Schristos } else if (strcmp(params->key, "p4") == 0) {
145b0d17251Schristos OPENSSL_free(obj->p4);
146b0d17251Schristos if (!TEST_ptr(obj->p4 = OPENSSL_strndup(params->data,
147b0d17251Schristos params->data_size)))
148b0d17251Schristos return 0;
149b0d17251Schristos obj->p4_l = strlen(obj->p4);
150b0d17251Schristos } else if (strcmp(params->key, "p5") == 0) {
151b0d17251Schristos /*
152b0d17251Schristos * Protect obj->p5 against too much data. This should not
153b0d17251Schristos * happen, we don't use that long strings.
154b0d17251Schristos */
155b0d17251Schristos size_t data_length =
156b0d17251Schristos OPENSSL_strnlen(params->data, params->data_size);
157b0d17251Schristos
158b0d17251Schristos if (!TEST_size_t_lt(data_length, sizeof(obj->p5)))
159b0d17251Schristos return 0;
160b0d17251Schristos strncpy(obj->p5, params->data, data_length);
161b0d17251Schristos obj->p5[data_length] = '\0';
162b0d17251Schristos obj->p5_l = strlen(obj->p5);
163b0d17251Schristos } else if (strcmp(params->key, "p6") == 0) {
164b0d17251Schristos obj->p6 = *(const char **)params->data;
165b0d17251Schristos obj->p6_l = params->data_size;
166b0d17251Schristos }
167b0d17251Schristos
168b0d17251Schristos return 1;
169b0d17251Schristos }
170b0d17251Schristos
raw_get_params(void * vobj,OSSL_PARAM * params)171b0d17251Schristos static int raw_get_params(void *vobj, OSSL_PARAM *params)
172b0d17251Schristos {
173b0d17251Schristos struct object_st *obj = vobj;
174b0d17251Schristos
175b0d17251Schristos for (; params->key != NULL; params++)
176b0d17251Schristos if (strcmp(params->key, "p1") == 0) {
177b0d17251Schristos params->return_size = sizeof(obj->p1);
178b0d17251Schristos *(int *)params->data = obj->p1;
179b0d17251Schristos } else if (strcmp(params->key, "p2") == 0) {
180b0d17251Schristos params->return_size = sizeof(obj->p2);
181b0d17251Schristos *(double *)params->data = obj->p2;
182b0d17251Schristos } else if (strcmp(params->key, "p3") == 0) {
183b0d17251Schristos params->return_size = BN_num_bytes(obj->p3);
184b0d17251Schristos if (!TEST_size_t_ge(params->data_size, params->return_size))
185b0d17251Schristos return 0;
186b0d17251Schristos BN_bn2nativepad(obj->p3, params->data, params->return_size);
187b0d17251Schristos } else if (strcmp(params->key, "p4") == 0) {
188b0d17251Schristos params->return_size = strlen(obj->p4);
189b0d17251Schristos if (!TEST_size_t_gt(params->data_size, params->return_size))
190b0d17251Schristos return 0;
191b0d17251Schristos strcpy(params->data, obj->p4);
192b0d17251Schristos } else if (strcmp(params->key, "p5") == 0) {
193b0d17251Schristos params->return_size = strlen(obj->p5);
194b0d17251Schristos if (!TEST_size_t_gt(params->data_size, params->return_size))
195b0d17251Schristos return 0;
196b0d17251Schristos strcpy(params->data, obj->p5);
197b0d17251Schristos } else if (strcmp(params->key, "p6") == 0) {
198b0d17251Schristos params->return_size = strlen(obj->p6);
199b0d17251Schristos *(const char **)params->data = obj->p6;
200b0d17251Schristos }
201b0d17251Schristos
202b0d17251Schristos return 1;
203b0d17251Schristos }
204b0d17251Schristos
205b0d17251Schristos /*
206b0d17251Schristos * API provider, which handles the parameters using the API from params.h
207b0d17251Schristos */
208b0d17251Schristos
api_set_params(void * vobj,const OSSL_PARAM * params)209b0d17251Schristos static int api_set_params(void *vobj, const OSSL_PARAM *params)
210b0d17251Schristos {
211b0d17251Schristos struct object_st *obj = vobj;
212b0d17251Schristos const OSSL_PARAM *p = NULL;
213b0d17251Schristos
214b0d17251Schristos if ((p = OSSL_PARAM_locate_const(params, "p1")) != NULL
215b0d17251Schristos && !TEST_true(OSSL_PARAM_get_int(p, &obj->p1)))
216b0d17251Schristos return 0;
217b0d17251Schristos if ((p = OSSL_PARAM_locate_const(params, "p2")) != NULL
218b0d17251Schristos && !TEST_true(OSSL_PARAM_get_double(p, &obj->p2)))
219b0d17251Schristos return 0;
220b0d17251Schristos if ((p = OSSL_PARAM_locate_const(params, "p3")) != NULL
221b0d17251Schristos && !TEST_true(OSSL_PARAM_get_BN(p, &obj->p3)))
222b0d17251Schristos return 0;
223b0d17251Schristos if ((p = OSSL_PARAM_locate_const(params, "p4")) != NULL) {
224b0d17251Schristos OPENSSL_free(obj->p4);
225b0d17251Schristos obj->p4 = NULL;
226b0d17251Schristos /* If the value pointer is NULL, we get it automatically allocated */
227b0d17251Schristos if (!TEST_true(OSSL_PARAM_get_utf8_string(p, &obj->p4, 0)))
228b0d17251Schristos return 0;
229b0d17251Schristos }
230b0d17251Schristos if ((p = OSSL_PARAM_locate_const(params, "p5")) != NULL) {
231b0d17251Schristos char *p5_ptr = obj->p5;
232b0d17251Schristos if (!TEST_true(OSSL_PARAM_get_utf8_string(p, &p5_ptr, sizeof(obj->p5))))
233b0d17251Schristos return 0;
234b0d17251Schristos obj->p5_l = strlen(obj->p5);
235b0d17251Schristos }
236b0d17251Schristos if ((p = OSSL_PARAM_locate_const(params, "p6")) != NULL) {
237b0d17251Schristos if (!TEST_true(OSSL_PARAM_get_utf8_ptr(p, &obj->p6)))
238b0d17251Schristos return 0;
239b0d17251Schristos obj->p6_l = strlen(obj->p6);
240b0d17251Schristos }
241b0d17251Schristos
242b0d17251Schristos return 1;
243b0d17251Schristos }
244b0d17251Schristos
api_get_params(void * vobj,OSSL_PARAM * params)245b0d17251Schristos static int api_get_params(void *vobj, OSSL_PARAM *params)
246b0d17251Schristos {
247b0d17251Schristos struct object_st *obj = vobj;
248b0d17251Schristos OSSL_PARAM *p = NULL;
249b0d17251Schristos
250b0d17251Schristos if ((p = OSSL_PARAM_locate(params, "p1")) != NULL
251b0d17251Schristos && !TEST_true(OSSL_PARAM_set_int(p, obj->p1)))
252b0d17251Schristos return 0;
253b0d17251Schristos if ((p = OSSL_PARAM_locate(params, "p2")) != NULL
254b0d17251Schristos && !TEST_true(OSSL_PARAM_set_double(p, obj->p2)))
255b0d17251Schristos return 0;
256b0d17251Schristos if ((p = OSSL_PARAM_locate(params, "p3")) != NULL
257b0d17251Schristos && !TEST_true(OSSL_PARAM_set_BN(p, obj->p3)))
258b0d17251Schristos return 0;
259b0d17251Schristos if ((p = OSSL_PARAM_locate(params, "p4")) != NULL
260b0d17251Schristos && !TEST_true(OSSL_PARAM_set_utf8_string(p, obj->p4)))
261b0d17251Schristos return 0;
262b0d17251Schristos if ((p = OSSL_PARAM_locate(params, "p5")) != NULL
263b0d17251Schristos && !TEST_true(OSSL_PARAM_set_utf8_string(p, obj->p5)))
264b0d17251Schristos return 0;
265b0d17251Schristos if ((p = OSSL_PARAM_locate(params, "p6")) != NULL
266b0d17251Schristos && !TEST_true(OSSL_PARAM_set_utf8_ptr(p, obj->p6)))
267b0d17251Schristos return 0;
268b0d17251Schristos
269b0d17251Schristos return 1;
270b0d17251Schristos }
271b0d17251Schristos
272b0d17251Schristos /*
273b0d17251Schristos * This structure only simulates a provider dispatch, the real deal is
274b0d17251Schristos * a bit more code that's not necessary in these tests.
275b0d17251Schristos */
276b0d17251Schristos struct provider_dispatch_st {
277b0d17251Schristos int (*set_params)(void *obj, const OSSL_PARAM *params);
278b0d17251Schristos int (*get_params)(void *obj, OSSL_PARAM *params);
279b0d17251Schristos };
280b0d17251Schristos
281b0d17251Schristos /* "raw" provider */
282b0d17251Schristos static const struct provider_dispatch_st provider_raw = {
283b0d17251Schristos raw_set_params, raw_get_params
284b0d17251Schristos };
285b0d17251Schristos
286b0d17251Schristos /* "api" provider */
287b0d17251Schristos static const struct provider_dispatch_st provider_api = {
288b0d17251Schristos api_set_params, api_get_params
289b0d17251Schristos };
290b0d17251Schristos
291b0d17251Schristos /*-
292b0d17251Schristos * APPLICATION SECTION
293b0d17251Schristos * ===================
294b0d17251Schristos */
295b0d17251Schristos
296b0d17251Schristos /* In all our tests, these are variables that get manipulated as parameters
297b0d17251Schristos *
298b0d17251Schristos * These arrays consistently do nothing with the "p2" parameter, and
299b0d17251Schristos * always include a "foo" parameter. This is to check that the
300b0d17251Schristos * set_params and get_params calls ignore the lack of parameters that
301b0d17251Schristos * the application isn't interested in, as well as ignore parameters
302b0d17251Schristos * they don't understand (the application may have one big bag of
303b0d17251Schristos * parameters).
304b0d17251Schristos */
305b0d17251Schristos static int app_p1; /* "p1" */
306b0d17251Schristos static double app_p2; /* "p2" is ignored */
307b0d17251Schristos static BIGNUM *app_p3 = NULL; /* "p3" */
308b0d17251Schristos static unsigned char bignumbin[4096]; /* "p3" */
309b0d17251Schristos static char app_p4[256]; /* "p4" */
310b0d17251Schristos static char app_p5[256]; /* "p5" */
311b0d17251Schristos static const char *app_p6 = NULL; /* "p6" */
312b0d17251Schristos static unsigned char foo[1]; /* "foo" */
313b0d17251Schristos
314b0d17251Schristos #define app_p1_init 17 /* A random number */
315b0d17251Schristos #define app_p2_init 47.11 /* Another random number */
316b0d17251Schristos #define app_p3_init "deadbeef" /* Classic */
317b0d17251Schristos #define app_p4_init "Hello"
318b0d17251Schristos #define app_p5_init "World"
319b0d17251Schristos #define app_p6_init "Cookie"
320b0d17251Schristos #define app_foo_init 'z'
321b0d17251Schristos
cleanup_app_variables(void)322b0d17251Schristos static int cleanup_app_variables(void)
323b0d17251Schristos {
324b0d17251Schristos BN_free(app_p3);
325b0d17251Schristos app_p3 = NULL;
326b0d17251Schristos return 1;
327b0d17251Schristos }
328b0d17251Schristos
init_app_variables(void)329b0d17251Schristos static int init_app_variables(void)
330b0d17251Schristos {
331b0d17251Schristos int l = 0;
332b0d17251Schristos
333b0d17251Schristos cleanup_app_variables();
334b0d17251Schristos
335b0d17251Schristos app_p1 = app_p1_init;
336b0d17251Schristos app_p2 = app_p2_init;
337b0d17251Schristos if (!BN_hex2bn(&app_p3, app_p3_init)
338b0d17251Schristos || (l = BN_bn2nativepad(app_p3, bignumbin, sizeof(bignumbin))) < 0)
339b0d17251Schristos return 0;
340b0d17251Schristos strcpy(app_p4, app_p4_init);
341b0d17251Schristos strcpy(app_p5, app_p5_init);
342b0d17251Schristos app_p6 = app_p6_init;
343b0d17251Schristos foo[0] = app_foo_init;
344b0d17251Schristos
345b0d17251Schristos return 1;
346b0d17251Schristos }
347b0d17251Schristos
348b0d17251Schristos /*
349b0d17251Schristos * Here, we define test OSSL_PARAM arrays
350b0d17251Schristos */
351b0d17251Schristos
352b0d17251Schristos /* An array of OSSL_PARAM, specific in the most raw manner possible */
353b0d17251Schristos static OSSL_PARAM static_raw_params[] = {
354b0d17251Schristos { "p1", OSSL_PARAM_INTEGER, &app_p1, sizeof(app_p1), 0 },
355b0d17251Schristos { "p3", OSSL_PARAM_UNSIGNED_INTEGER, &bignumbin, sizeof(bignumbin), 0 },
356b0d17251Schristos { "p4", OSSL_PARAM_UTF8_STRING, &app_p4, sizeof(app_p4), 0 },
357b0d17251Schristos { "p5", OSSL_PARAM_UTF8_STRING, &app_p5, sizeof(app_p5), 0 },
358b0d17251Schristos /* sizeof(app_p6_init) - 1, because we know that's what we're using */
359b0d17251Schristos { "p6", OSSL_PARAM_UTF8_PTR, &app_p6, sizeof(app_p6_init) - 1, 0 },
360b0d17251Schristos { "foo", OSSL_PARAM_OCTET_STRING, &foo, sizeof(foo), 0 },
361b0d17251Schristos { NULL, 0, NULL, 0, 0 }
362b0d17251Schristos };
363b0d17251Schristos
364b0d17251Schristos /* The same array of OSSL_PARAM, specified with the macros from params.h */
365b0d17251Schristos static OSSL_PARAM static_api_params[] = {
366b0d17251Schristos OSSL_PARAM_int("p1", &app_p1),
367b0d17251Schristos OSSL_PARAM_BN("p3", &bignumbin, sizeof(bignumbin)),
368b0d17251Schristos OSSL_PARAM_DEFN("p4", OSSL_PARAM_UTF8_STRING, &app_p4, sizeof(app_p4)),
369b0d17251Schristos OSSL_PARAM_DEFN("p5", OSSL_PARAM_UTF8_STRING, &app_p5, sizeof(app_p5)),
370b0d17251Schristos /* sizeof(app_p6_init), because we know that's what we're using */
371b0d17251Schristos OSSL_PARAM_DEFN("p6", OSSL_PARAM_UTF8_PTR, &app_p6,
372b0d17251Schristos sizeof(app_p6_init) - 1),
373b0d17251Schristos OSSL_PARAM_DEFN("foo", OSSL_PARAM_OCTET_STRING, &foo, sizeof(foo)),
374b0d17251Schristos OSSL_PARAM_END
375b0d17251Schristos };
376b0d17251Schristos
377b0d17251Schristos /*
378b0d17251Schristos * The same array again, but constructed at run-time
379b0d17251Schristos * This exercises the OSSL_PARAM constructor functions
380b0d17251Schristos */
construct_api_params(void)381b0d17251Schristos static OSSL_PARAM *construct_api_params(void)
382b0d17251Schristos {
383b0d17251Schristos size_t n = 0;
384b0d17251Schristos static OSSL_PARAM params[10];
385b0d17251Schristos
386b0d17251Schristos params[n++] = OSSL_PARAM_construct_int("p1", &app_p1);
387b0d17251Schristos params[n++] = OSSL_PARAM_construct_BN("p3", bignumbin, sizeof(bignumbin));
388b0d17251Schristos params[n++] = OSSL_PARAM_construct_utf8_string("p4", app_p4,
389b0d17251Schristos sizeof(app_p4));
390b0d17251Schristos params[n++] = OSSL_PARAM_construct_utf8_string("p5", app_p5,
391b0d17251Schristos sizeof(app_p5));
392b0d17251Schristos /* sizeof(app_p6_init), because we know that's what we're using */
393b0d17251Schristos params[n++] = OSSL_PARAM_construct_utf8_ptr("p6", (char **)&app_p6,
394b0d17251Schristos sizeof(app_p6_init));
395b0d17251Schristos params[n++] = OSSL_PARAM_construct_octet_string("foo", &foo, sizeof(foo));
396b0d17251Schristos params[n++] = OSSL_PARAM_construct_end();
397b0d17251Schristos
398b0d17251Schristos return params;
399b0d17251Schristos }
400b0d17251Schristos
401b0d17251Schristos struct param_owner_st {
402b0d17251Schristos OSSL_PARAM *static_params;
403b0d17251Schristos OSSL_PARAM *(*constructed_params)(void);
404b0d17251Schristos };
405b0d17251Schristos
406b0d17251Schristos static const struct param_owner_st raw_params = {
407b0d17251Schristos static_raw_params, NULL
408b0d17251Schristos };
409b0d17251Schristos
410b0d17251Schristos static const struct param_owner_st api_params = {
411b0d17251Schristos static_api_params, construct_api_params
412b0d17251Schristos };
413b0d17251Schristos
414b0d17251Schristos /*-
415b0d17251Schristos * TESTING
416b0d17251Schristos * =======
417b0d17251Schristos */
418b0d17251Schristos
419b0d17251Schristos /*
420b0d17251Schristos * Test cases to combine parameters with "provider side" functions
421b0d17251Schristos */
422b0d17251Schristos static struct {
423b0d17251Schristos const struct provider_dispatch_st *prov;
424b0d17251Schristos const struct param_owner_st *app;
425b0d17251Schristos const char *desc;
426b0d17251Schristos } test_cases[] = {
427b0d17251Schristos /* Tests within specific methods */
428b0d17251Schristos { &provider_raw, &raw_params, "raw provider vs raw params" },
429b0d17251Schristos { &provider_api, &api_params, "api provider vs api params" },
430b0d17251Schristos
431b0d17251Schristos /* Mixed methods */
432b0d17251Schristos { &provider_raw, &api_params, "raw provider vs api params" },
433b0d17251Schristos { &provider_api, &raw_params, "api provider vs raw params" },
434b0d17251Schristos };
435b0d17251Schristos
436b0d17251Schristos /* Generic tester of combinations of "providers" and params */
test_case_variant(OSSL_PARAM * params,const struct provider_dispatch_st * prov)437b0d17251Schristos static int test_case_variant(OSSL_PARAM *params, const struct provider_dispatch_st *prov)
438b0d17251Schristos {
439b0d17251Schristos BIGNUM *verify_p3 = NULL;
440b0d17251Schristos void *obj = NULL;
441b0d17251Schristos int errcnt = 0;
442b0d17251Schristos OSSL_PARAM *p;
443b0d17251Schristos
444b0d17251Schristos /*
445b0d17251Schristos * Initialize
446b0d17251Schristos */
447b0d17251Schristos if (!TEST_ptr(obj = init_object())
448b0d17251Schristos || !TEST_true(BN_hex2bn(&verify_p3, p3_init))) {
449b0d17251Schristos errcnt++;
450b0d17251Schristos goto fin;
451b0d17251Schristos }
452b0d17251Schristos
453b0d17251Schristos /*
454b0d17251Schristos * Get parameters a first time, just to see that getting works and
455b0d17251Schristos * gets us the values we expect.
456b0d17251Schristos */
457b0d17251Schristos init_app_variables();
458b0d17251Schristos
459b0d17251Schristos if (!TEST_true(prov->get_params(obj, params))
460b0d17251Schristos || !TEST_int_eq(app_p1, p1_init) /* "provider" value */
461b0d17251Schristos || !TEST_double_eq(app_p2, app_p2_init) /* Should remain untouched */
462b0d17251Schristos || !TEST_ptr(p = OSSL_PARAM_locate(params, "p3"))
463b0d17251Schristos || !TEST_ptr(BN_native2bn(bignumbin, p->return_size, app_p3))
464b0d17251Schristos || !TEST_BN_eq(app_p3, verify_p3) /* "provider" value */
465b0d17251Schristos || !TEST_str_eq(app_p4, p4_init) /* "provider" value */
466b0d17251Schristos || !TEST_ptr(p = OSSL_PARAM_locate(params, "p5"))
467b0d17251Schristos || !TEST_size_t_eq(p->return_size,
468b0d17251Schristos sizeof(p5_init) - 1) /* "provider" value */
469b0d17251Schristos || !TEST_str_eq(app_p5, p5_init) /* "provider" value */
470b0d17251Schristos || !TEST_ptr(p = OSSL_PARAM_locate(params, "p6"))
471b0d17251Schristos || !TEST_size_t_eq(p->return_size,
472b0d17251Schristos sizeof(p6_init) - 1) /* "provider" value */
473b0d17251Schristos || !TEST_str_eq(app_p6, p6_init) /* "provider" value */
474b0d17251Schristos || !TEST_char_eq(foo[0], app_foo_init) /* Should remain untouched */
475b0d17251Schristos || !TEST_ptr(p = OSSL_PARAM_locate(params, "foo")))
476b0d17251Schristos errcnt++;
477b0d17251Schristos
478b0d17251Schristos /*
479b0d17251Schristos * Set parameters, then sneak into the object itself and check
480b0d17251Schristos * that its attributes got set (or ignored) properly.
481b0d17251Schristos */
482b0d17251Schristos init_app_variables();
483b0d17251Schristos
484b0d17251Schristos if (!TEST_true(prov->set_params(obj, params))) {
485b0d17251Schristos errcnt++;
486b0d17251Schristos } else {
487b0d17251Schristos struct object_st *sneakpeek = obj;
488b0d17251Schristos
489b0d17251Schristos if (!TEST_int_eq(sneakpeek->p1, app_p1) /* app value set */
490b0d17251Schristos || !TEST_double_eq(sneakpeek->p2, p2_init) /* Should remain untouched */
491b0d17251Schristos || !TEST_BN_eq(sneakpeek->p3, app_p3) /* app value set */
492b0d17251Schristos || !TEST_str_eq(sneakpeek->p4, app_p4) /* app value set */
493b0d17251Schristos || !TEST_str_eq(sneakpeek->p5, app_p5) /* app value set */
494b0d17251Schristos || !TEST_str_eq(sneakpeek->p6, app_p6)) /* app value set */
495b0d17251Schristos errcnt++;
496b0d17251Schristos }
497b0d17251Schristos
498b0d17251Schristos /*
499b0d17251Schristos * Get parameters again, checking that we get different values
500b0d17251Schristos * than earlier where relevant.
501b0d17251Schristos */
502b0d17251Schristos BN_free(verify_p3);
503b0d17251Schristos verify_p3 = NULL;
504b0d17251Schristos
505b0d17251Schristos if (!TEST_true(BN_hex2bn(&verify_p3, app_p3_init))) {
506b0d17251Schristos errcnt++;
507b0d17251Schristos goto fin;
508b0d17251Schristos }
509b0d17251Schristos
510b0d17251Schristos if (!TEST_true(prov->get_params(obj, params))
511b0d17251Schristos || !TEST_int_eq(app_p1, app_p1_init) /* app value */
512b0d17251Schristos || !TEST_double_eq(app_p2, app_p2_init) /* Should remain untouched */
513b0d17251Schristos || !TEST_ptr(p = OSSL_PARAM_locate(params, "p3"))
514b0d17251Schristos || !TEST_ptr(BN_native2bn(bignumbin, p->return_size, app_p3))
515b0d17251Schristos || !TEST_BN_eq(app_p3, verify_p3) /* app value */
516b0d17251Schristos || !TEST_str_eq(app_p4, app_p4_init) /* app value */
517b0d17251Schristos || !TEST_ptr(p = OSSL_PARAM_locate(params, "p5"))
518b0d17251Schristos || !TEST_size_t_eq(p->return_size,
519b0d17251Schristos sizeof(app_p5_init) - 1) /* app value */
520b0d17251Schristos || !TEST_str_eq(app_p5, app_p5_init) /* app value */
521b0d17251Schristos || !TEST_ptr(p = OSSL_PARAM_locate(params, "p6"))
522b0d17251Schristos || !TEST_size_t_eq(p->return_size,
523b0d17251Schristos sizeof(app_p6_init) - 1) /* app value */
524b0d17251Schristos || !TEST_str_eq(app_p6, app_p6_init) /* app value */
525b0d17251Schristos || !TEST_char_eq(foo[0], app_foo_init) /* Should remain untouched */
526b0d17251Schristos || !TEST_ptr(p = OSSL_PARAM_locate(params, "foo")))
527b0d17251Schristos errcnt++;
528b0d17251Schristos
529b0d17251Schristos fin:
530b0d17251Schristos BN_free(verify_p3);
531b0d17251Schristos verify_p3 = NULL;
532b0d17251Schristos cleanup_app_variables();
533b0d17251Schristos cleanup_object(obj);
534b0d17251Schristos
535b0d17251Schristos return errcnt == 0;
536b0d17251Schristos }
537b0d17251Schristos
test_case(int i)538b0d17251Schristos static int test_case(int i)
539b0d17251Schristos {
540b0d17251Schristos TEST_info("Case: %s", test_cases[i].desc);
541b0d17251Schristos
542b0d17251Schristos return test_case_variant(test_cases[i].app->static_params,
543b0d17251Schristos test_cases[i].prov)
544b0d17251Schristos && (test_cases[i].app->constructed_params == NULL
545b0d17251Schristos || test_case_variant(test_cases[i].app->constructed_params(),
546b0d17251Schristos test_cases[i].prov));
547b0d17251Schristos }
548b0d17251Schristos
549b0d17251Schristos /*-
550b0d17251Schristos * OSSL_PARAM_allocate_from_text() tests
551b0d17251Schristos * =====================================
552b0d17251Schristos */
553b0d17251Schristos
554b0d17251Schristos static const OSSL_PARAM params_from_text[] = {
555b0d17251Schristos /* Fixed size buffer */
556b0d17251Schristos OSSL_PARAM_int32("int", NULL),
557b0d17251Schristos OSSL_PARAM_DEFN("short", OSSL_PARAM_INTEGER, NULL, sizeof(int16_t)),
558b0d17251Schristos OSSL_PARAM_DEFN("ushort", OSSL_PARAM_UNSIGNED_INTEGER, NULL, sizeof(uint16_t)),
559b0d17251Schristos /* Arbitrary size buffer. Make sure the result fits in a long */
560b0d17251Schristos OSSL_PARAM_DEFN("num", OSSL_PARAM_INTEGER, NULL, 0),
561b0d17251Schristos OSSL_PARAM_DEFN("unum", OSSL_PARAM_UNSIGNED_INTEGER, NULL, 0),
562*0e2e28bcSchristos OSSL_PARAM_DEFN("octets", OSSL_PARAM_OCTET_STRING, NULL, 0),
563b0d17251Schristos OSSL_PARAM_END,
564b0d17251Schristos };
565b0d17251Schristos
566b0d17251Schristos struct int_from_text_test_st {
567b0d17251Schristos const char *argname;
568b0d17251Schristos const char *strval;
569b0d17251Schristos long int expected_intval;
570b0d17251Schristos int expected_res;
571b0d17251Schristos size_t expected_bufsize;
572b0d17251Schristos };
573b0d17251Schristos
574b0d17251Schristos static struct int_from_text_test_st int_from_text_test_cases[] = {
575b0d17251Schristos { "int", "", 0, 0, 0 },
576b0d17251Schristos { "int", "0", 0, 1, 4 },
577b0d17251Schristos { "int", "101", 101, 1, 4 },
578b0d17251Schristos { "int", "-102", -102, 1, 4 },
579b0d17251Schristos { "int", "12A", 12, 1, 4 }, /* incomplete */
580b0d17251Schristos { "int", "0x12B", 0x12B, 1, 4 },
581b0d17251Schristos { "hexint", "12C", 0x12C, 1, 4 },
582b0d17251Schristos { "hexint", "0x12D", 0, 1, 4 }, /* zero */
583b0d17251Schristos /* test check of the target buffer size */
584b0d17251Schristos { "int", "0x7fffffff", INT32_MAX, 1, 4 },
585b0d17251Schristos { "int", "2147483647", INT32_MAX, 1, 4 },
586b0d17251Schristos { "int", "2147483648", 0, 0, 0 }, /* too small buffer */
587b0d17251Schristos { "int", "-2147483648", INT32_MIN, 1, 4 },
588b0d17251Schristos { "int", "-2147483649", 0, 0, 4 }, /* too small buffer */
589b0d17251Schristos { "short", "0x7fff", INT16_MAX, 1, 2 },
590b0d17251Schristos { "short", "32767", INT16_MAX, 1, 2 },
591b0d17251Schristos { "short", "32768", 0, 0, 0 }, /* too small buffer */
592b0d17251Schristos { "ushort", "0xffff", UINT16_MAX, 1, 2 },
593b0d17251Schristos { "ushort", "65535", UINT16_MAX, 1, 2 },
594b0d17251Schristos { "ushort", "65536", 0, 0, 0 }, /* too small buffer */
595b0d17251Schristos /* test check of sign extension in arbitrary size results */
596b0d17251Schristos { "num", "0", 0, 1, 1 },
597b0d17251Schristos { "num", "0", 0, 1, 1 },
598b0d17251Schristos { "num", "0xff", 0xff, 1, 2 }, /* sign extension */
599b0d17251Schristos { "num", "-0xff", -0xff, 1, 2 }, /* sign extension */
600b0d17251Schristos { "num", "0x7f", 0x7f, 1, 1 }, /* no sign extension */
601b0d17251Schristos { "num", "-0x7f", -0x7f, 1, 1 }, /* no sign extension */
602b0d17251Schristos { "num", "0x80", 0x80, 1, 2 }, /* sign extension */
603b0d17251Schristos { "num", "-0x80", -0x80, 1, 1 }, /* no sign extension */
604b0d17251Schristos { "num", "0x81", 0x81, 1, 2 }, /* sign extension */
605b0d17251Schristos { "num", "-0x81", -0x81, 1, 2 }, /* sign extension */
606b0d17251Schristos { "unum", "0xff", 0xff, 1, 1 },
607b0d17251Schristos { "unum", "-0xff", -0xff, 0, 0 }, /* invalid neg number */
608b0d17251Schristos { "unum", "0x7f", 0x7f, 1, 1 },
609b0d17251Schristos { "unum", "-0x7f", -0x7f, 0, 0 }, /* invalid neg number */
610b0d17251Schristos { "unum", "0x80", 0x80, 1, 1 },
611b0d17251Schristos { "unum", "-0x80", -0x80, 0, 0 }, /* invalid neg number */
612b0d17251Schristos { "unum", "0x81", 0x81, 1, 1 },
613b0d17251Schristos { "unum", "-0x81", -0x81, 0, 0 }, /* invalid neg number */
614b0d17251Schristos };
615b0d17251Schristos
check_int_from_text(const struct int_from_text_test_st a)616b0d17251Schristos static int check_int_from_text(const struct int_from_text_test_st a)
617b0d17251Schristos {
618b0d17251Schristos OSSL_PARAM param;
619b0d17251Schristos long int val = 0;
620b0d17251Schristos int res;
621b0d17251Schristos
622b0d17251Schristos if (!OSSL_PARAM_allocate_from_text(¶m, params_from_text,
623b0d17251Schristos a.argname, a.strval, 0, NULL)) {
624b0d17251Schristos if (a.expected_res)
625b0d17251Schristos TEST_error("unexpected OSSL_PARAM_allocate_from_text() return for %s \"%s\"",
626b0d17251Schristos a.argname, a.strval);
627b0d17251Schristos return !a.expected_res;
628b0d17251Schristos }
629b0d17251Schristos
630b0d17251Schristos /* For data size zero, OSSL_PARAM_get_long() may crash */
631b0d17251Schristos if (param.data_size == 0) {
632b0d17251Schristos OPENSSL_free(param.data);
633b0d17251Schristos TEST_error("unexpected zero size for %s \"%s\"",
634b0d17251Schristos a.argname, a.strval);
635b0d17251Schristos return 0;
636b0d17251Schristos }
637b0d17251Schristos res = OSSL_PARAM_get_long(¶m, &val);
638b0d17251Schristos OPENSSL_free(param.data);
639b0d17251Schristos
640b0d17251Schristos if (res ^ a.expected_res) {
641b0d17251Schristos TEST_error("unexpected OSSL_PARAM_get_long() return for %s \"%s\": "
642b0d17251Schristos "%d != %d", a.argname, a.strval, a.expected_res, res);
643b0d17251Schristos return 0;
644b0d17251Schristos }
645b0d17251Schristos if (val != a.expected_intval) {
646b0d17251Schristos TEST_error("unexpected result for %s \"%s\": %li != %li",
647b0d17251Schristos a.argname, a.strval, a.expected_intval, val);
648b0d17251Schristos return 0;
649b0d17251Schristos }
650b0d17251Schristos if (param.data_size != a.expected_bufsize) {
651b0d17251Schristos TEST_error("unexpected size for %s \"%s\": %d != %d",
652b0d17251Schristos a.argname, a.strval,
653b0d17251Schristos (int)a.expected_bufsize, (int)param.data_size);
654b0d17251Schristos return 0;
655b0d17251Schristos }
656b0d17251Schristos
657b0d17251Schristos return a.expected_res;
658b0d17251Schristos }
659b0d17251Schristos
check_octetstr_from_hexstr(void)660*0e2e28bcSchristos static int check_octetstr_from_hexstr(void)
661*0e2e28bcSchristos {
662*0e2e28bcSchristos OSSL_PARAM param;
663*0e2e28bcSchristos static const char *values[] = { "", "F", "FF", "FFF", "FFFF", NULL };
664*0e2e28bcSchristos int i;
665*0e2e28bcSchristos int errcnt = 0;
666*0e2e28bcSchristos
667*0e2e28bcSchristos /* Test odd vs even number of hex digits */
668*0e2e28bcSchristos for (i = 0; values[i] != NULL; i++) {
669*0e2e28bcSchristos int expected = (strlen(values[i]) % 2) != 1;
670*0e2e28bcSchristos int result;
671*0e2e28bcSchristos
672*0e2e28bcSchristos ERR_clear_error();
673*0e2e28bcSchristos memset(¶m, 0, sizeof(param));
674*0e2e28bcSchristos if (expected)
675*0e2e28bcSchristos result =
676*0e2e28bcSchristos TEST_true(OSSL_PARAM_allocate_from_text(¶m,
677*0e2e28bcSchristos params_from_text,
678*0e2e28bcSchristos "hexoctets", values[i], 0,
679*0e2e28bcSchristos NULL));
680*0e2e28bcSchristos else
681*0e2e28bcSchristos result =
682*0e2e28bcSchristos TEST_false(OSSL_PARAM_allocate_from_text(¶m,
683*0e2e28bcSchristos params_from_text,
684*0e2e28bcSchristos "hexoctets", values[i], 0,
685*0e2e28bcSchristos NULL));
686*0e2e28bcSchristos if (!result) {
687*0e2e28bcSchristos TEST_error("unexpected OSSL_PARAM_allocate_from_text() %s for 'octets' \"%s\"",
688*0e2e28bcSchristos (expected ? "failure" : "success"), values[i]);
689*0e2e28bcSchristos errcnt++;
690*0e2e28bcSchristos }
691*0e2e28bcSchristos OPENSSL_free(param.data);
692*0e2e28bcSchristos }
693*0e2e28bcSchristos return errcnt == 0;
694*0e2e28bcSchristos }
695*0e2e28bcSchristos
test_allocate_from_text(int i)696b0d17251Schristos static int test_allocate_from_text(int i)
697b0d17251Schristos {
698b0d17251Schristos return check_int_from_text(int_from_text_test_cases[i]);
699b0d17251Schristos }
700b0d17251Schristos
test_more_allocate_from_text(void)701*0e2e28bcSchristos static int test_more_allocate_from_text(void)
702*0e2e28bcSchristos {
703*0e2e28bcSchristos return check_octetstr_from_hexstr();
704*0e2e28bcSchristos }
705*0e2e28bcSchristos
setup_tests(void)706b0d17251Schristos int setup_tests(void)
707b0d17251Schristos {
708b0d17251Schristos ADD_ALL_TESTS(test_case, OSSL_NELEM(test_cases));
709b0d17251Schristos ADD_ALL_TESTS(test_allocate_from_text, OSSL_NELEM(int_from_text_test_cases));
710*0e2e28bcSchristos ADD_TEST(test_more_allocate_from_text);
711b0d17251Schristos return 1;
712b0d17251Schristos }
713