]> git.ipfire.org Git - thirdparty/openssl.git/blame - test/params_test.c
Params: add OSSL_PARAM_construct_end()
[thirdparty/openssl.git] / test / params_test.c
CommitLineData
9ad41d24
RL
1/*
2 * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 * https://www.openssl.org/source/license.html
8 * or in the file LICENSE in the source distribution.
9 */
10
11/*
12 * This program tests the use of OSSL_PARAM, currently in raw form.
13 */
14
15#include <string.h>
16#include <openssl/bn.h>
17#include <openssl/core.h>
bc1e0be7 18#include <openssl/params.h>
9ad41d24
RL
19#include "internal/nelem.h"
20#include "testutil.h"
21
22/*-
23 * PROVIDER SECTION
24 * ================
25 *
26 * Even though it's not necessarily ONLY providers doing this part,
27 * they are naturally going to be the most common users of
28 * set_params and get_params functions.
29 */
30
31/*
32 * In real use cases, setters and getters would take an object with
33 * which the parameters are associated. This structure is a cheap
34 * simulation.
35 */
36struct object_st {
37 /*
38 * Documented as a native integer, of the size given by sizeof(int).
39 * Assumed data type OSSL_PARAM_INTEGER
40 */
41 int p1;
42 /*
43 * Documented as a native double, of the size given by sizeof(double).
44 * Assumed data type OSSL_PARAM_REAL
45 */
46 double p2;
47 /*
48 * Documented as an arbitrarly large unsigned integer.
49 * The data size must be large enough to accomodate.
50 * Assumed data type OSSL_PARAM_UNSIGNED_INTEGER
51 */
52 BIGNUM *p3;
53 /*
54 * Documented as a C string.
55 * The data size must be large enough to accomodate.
56 * Assumed data type OSSL_PARAM_UTF8_STRING
57 */
58 char *p4;
bc1e0be7 59 size_t p4_l;
fff68416
RL
60 /*
61 * Documented as a C string.
62 * Assumed data type OSSL_PARAM_UTF8_STRING
63 */
64 char p5[256];
65 size_t p5_l;
9ad41d24
RL
66 /*
67 * Documented as a pointer to a constant C string.
bc1e0be7 68 * Assumed data type OSSL_PARAM_UTF8_PTR
9ad41d24 69 */
fff68416
RL
70 const char *p6;
71 size_t p6_l;
9ad41d24
RL
72};
73
74#define p1_init 42 /* The ultimate answer */
75#define p2_init 6.283 /* Magic number */
76/* Stolen from evp_data, BLAKE2s256 test */
77#define p3_init \
78 "4142434445464748494a4b4c4d4e4f50" \
79 "5152535455565758595a616263646566" \
80 "6768696a6b6c6d6e6f70717273747576" \
81 "7778797a30313233343536373839"
82#define p4_init "BLAKE2s256" /* Random string */
fff68416
RL
83#define p5_init "Hellow World" /* Random string */
84#define p6_init OPENSSL_FULL_VERSION_STR /* Static string */
9ad41d24
RL
85
86static void cleanup_object(void *vobj)
87{
88 struct object_st *obj = vobj;
89
90 BN_free(obj->p3);
91 obj->p3 = NULL;
92 OPENSSL_free(obj->p4);
93 obj->p4 = NULL;
94 OPENSSL_free(obj);
95}
96
97static void *init_object(void)
98{
99 struct object_st *obj = OPENSSL_zalloc(sizeof(*obj));
100
101 obj->p1 = p1_init;
102 obj->p2 = p2_init;
103 if (!TEST_true(BN_hex2bn(&obj->p3, p3_init)))
104 goto fail;
105 if (!TEST_ptr(obj->p4 = OPENSSL_strdup(p4_init)))
106 goto fail;
fff68416
RL
107 strcpy(obj->p5, p5_init);
108 obj->p6 = p6_init;
9ad41d24
RL
109
110 return obj;
111 fail:
112 cleanup_object(obj);
113 obj = NULL;
114
115 return NULL;
116}
117
118/*
119 * RAW provider, which handles the parameters in a very raw manner,
120 * with no fancy API and very minimal checking. The application that
121 * calls these to set or request parameters MUST get its OSSL_PARAM
122 * array right.
123 */
124
125static int raw_set_params(void *vobj, const OSSL_PARAM *params)
126{
127 struct object_st *obj = vobj;
128
129 for (; params->key != NULL; params++)
130 if (strcmp(params->key, "p1") == 0) {
131 obj->p1 = *(int *)params->data;
132 } else if (strcmp(params->key, "p2") == 0) {
133 obj->p2 = *(double *)params->data;
134 } else if (strcmp(params->key, "p3") == 0) {
135 BN_free(obj->p3);
136 if (!TEST_ptr(obj->p3 = BN_native2bn(params->data,
137 params->data_size, NULL)))
138 return 0;
139 } else if (strcmp(params->key, "p4") == 0) {
140 OPENSSL_free(obj->p4);
141 if (!TEST_ptr(obj->p4 = OPENSSL_strndup(params->data,
142 params->data_size)))
143 return 0;
144 } else if (strcmp(params->key, "p5") == 0) {
fff68416
RL
145 strncpy(obj->p5, params->data, params->data_size);
146 } else if (strcmp(params->key, "p6") == 0) {
147 obj->p6 = *(const char **)params->data;
9ad41d24
RL
148 }
149
150 return 1;
151}
152
153static int raw_get_params(void *vobj, const OSSL_PARAM *params)
154{
155 struct object_st *obj = vobj;
156
157 for (; params->key != NULL; params++)
158 if (strcmp(params->key, "p1") == 0) {
159 if (params->return_size != NULL)
160 *params->return_size = sizeof(obj->p1);
161 *(int *)params->data = obj->p1;
162 } else if (strcmp(params->key, "p2") == 0) {
163 if (params->return_size != NULL)
164 *params->return_size = sizeof(obj->p2);
165 *(double *)params->data = obj->p2;
166 } else if (strcmp(params->key, "p3") == 0) {
167 size_t bytes = BN_num_bytes(obj->p3);
168
169 if (params->return_size != NULL)
170 *params->return_size = bytes;
171 if (!TEST_size_t_ge(params->data_size, bytes))
172 return 0;
173 BN_bn2nativepad(obj->p3, params->data, bytes);
174 } else if (strcmp(params->key, "p4") == 0) {
175 size_t bytes = strlen(obj->p4) + 1;
176
177 if (params->return_size != NULL)
178 *params->return_size = bytes;
179 if (!TEST_size_t_ge(params->data_size, bytes))
180 return 0;
181 strcpy(params->data, obj->p4);
182 } else if (strcmp(params->key, "p5") == 0) {
fff68416
RL
183 size_t bytes = strlen(obj->p5) + 1;
184
185 if (params->return_size != NULL)
186 *params->return_size = bytes;
187 if (!TEST_size_t_ge(params->data_size, bytes))
188 return 0;
189 strcpy(params->data, obj->p5);
190 } else if (strcmp(params->key, "p6") == 0) {
9ad41d24
RL
191 /*
192 * We COULD also use OPENSSL_FULL_VERSION_STR directly and
193 * use sizeof(OPENSSL_FULL_VERSION_STR) instead of calling
194 * strlen().
195 * The caller wouldn't know the difference.
196 */
fff68416 197 size_t bytes = strlen(obj->p6) + 1;
9ad41d24
RL
198
199 if (params->return_size != NULL)
200 *params->return_size = bytes;
fff68416 201 *(const char **)params->data = obj->p6;
9ad41d24
RL
202 }
203
204 return 1;
205}
206
bc1e0be7
RL
207/*
208 * API provider, which handles the parameters using the API from params.h
209 */
210
211static int api_set_params(void *vobj, const OSSL_PARAM *params)
212{
213 struct object_st *obj = vobj;
214 const OSSL_PARAM *p = NULL;
215
216 if ((p = OSSL_PARAM_locate(params, "p1")) != NULL
217 && !TEST_true(OSSL_PARAM_get_int(p, &obj->p1)))
218 return 0;
219 if ((p = OSSL_PARAM_locate(params, "p2")) != NULL
220 && !TEST_true(OSSL_PARAM_get_double(p, &obj->p2)))
221 return 0;
222 if ((p = OSSL_PARAM_locate(params, "p3")) != NULL
223 && !TEST_true(OSSL_PARAM_get_BN(p, &obj->p3)))
224 return 0;
225 if ((p = OSSL_PARAM_locate(params, "p4")) != NULL) {
226 OPENSSL_free(obj->p4);
227 obj->p4 = NULL;
228 /* If the value pointer is NULL, we get it automatically allocated */
229 if (!TEST_true(OSSL_PARAM_get_utf8_string(p, &obj->p4, 0)))
230 return 0;
231 }
fff68416
RL
232 if ((p = OSSL_PARAM_locate(params, "p5")) != NULL) {
233 char *p5_ptr = obj->p5;
234 if (!TEST_true(OSSL_PARAM_get_utf8_string(p, &p5_ptr, sizeof(obj->p5))))
235 return 0;
236 }
237 if ((p = OSSL_PARAM_locate(params, "p6")) != NULL
238 && !TEST_true(OSSL_PARAM_get_utf8_ptr(p, &obj->p6)))
bc1e0be7
RL
239 return 0;
240
241 return 1;
242}
243
244static int api_get_params(void *vobj, const OSSL_PARAM *params)
245{
246 struct object_st *obj = vobj;
247 const OSSL_PARAM *p = NULL;
248
249 if ((p = OSSL_PARAM_locate(params, "p1")) != NULL
250 && !TEST_true(OSSL_PARAM_set_int(p, obj->p1)))
251 return 0;
252 if ((p = OSSL_PARAM_locate(params, "p2")) != NULL
253 && !TEST_true(OSSL_PARAM_set_double(p, obj->p2)))
254 return 0;
255 if ((p = OSSL_PARAM_locate(params, "p3")) != NULL
256 && !TEST_true(OSSL_PARAM_set_BN(p, obj->p3)))
257 return 0;
258 if ((p = OSSL_PARAM_locate(params, "p4")) != NULL
259 && !TEST_true(OSSL_PARAM_set_utf8_string(p, obj->p4)))
260 return 0;
261 if ((p = OSSL_PARAM_locate(params, "p5")) != NULL
fff68416
RL
262 && !TEST_true(OSSL_PARAM_set_utf8_string(p, obj->p5)))
263 return 0;
264 if ((p = OSSL_PARAM_locate(params, "p6")) != NULL
265 && !TEST_true(OSSL_PARAM_set_utf8_ptr(p, obj->p6)))
bc1e0be7
RL
266 return 0;
267
268 return 1;
269}
270
9ad41d24
RL
271/*
272 * This structure only simulates a provider dispatch, the real deal is
273 * a bit more code that's not necessary in these tests.
274 */
275struct provider_dispatch_st {
276 int (*set_params)(void *obj, const OSSL_PARAM *params);
277 int (*get_params)(void *obj, const OSSL_PARAM *params);
278};
279
280/* "raw" provider */
281static const struct provider_dispatch_st provider_raw = {
282 raw_set_params, raw_get_params
283};
284
bc1e0be7
RL
285/* "api" provider */
286static const struct provider_dispatch_st provider_api = {
287 api_set_params, api_get_params
288};
289
9ad41d24
RL
290/*-
291 * APPLICATION SECTION
292 * ===================
293 */
294
295/* In all our tests, these are variables that get manipulated as parameters
296 *
297 * These arrays consistenly do nothing with the "p2" parameter, and
298 * always include a "foo" parameter. This is to check that the
299 * set_params and get_params calls ignore the lack of parameters that
300 * the application isn't interested in, as well as ignore parameters
301 * they don't understand (the application may have one big bag of
302 * parameters).
303 */
304static int app_p1; /* "p1" */
305static double app_p2; /* "p2" is ignored */
306static BIGNUM *app_p3 = NULL; /* "p3" */
307static unsigned char bignumbin[4096]; /* "p3" */
308static size_t bignumbin_l; /* "p3" */
309static char app_p4[256]; /* "p4" */
310static size_t app_p4_l; /* "p4" */
fff68416 311static char app_p5[256]; /* "p5" */
9ad41d24 312static size_t app_p5_l; /* "p5" */
fff68416
RL
313static const char *app_p6 = NULL; /* "p6" */
314static size_t app_p6_l; /* "p6" */
9ad41d24
RL
315static unsigned char foo[1]; /* "foo" */
316static size_t foo_l; /* "foo" */
317
318#define app_p1_init 17 /* A random number */
319#define app_p2_init 47.11 /* Another random number */
320#define app_p3_init "deadbeef" /* Classic */
321#define app_p4_init "Hello"
322#define app_p5_init "World"
fff68416 323#define app_p6_init "Cookie"
9ad41d24
RL
324#define app_foo_init 'z'
325
326static int cleanup_app_variables(void)
327{
328 BN_free(app_p3);
329 app_p3 = NULL;
330 return 1;
331}
332
333static int init_app_variables(void)
334{
335 int l = 0;
336
337 cleanup_app_variables();
338
339 app_p1 = app_p1_init;
340 app_p2 = app_p2_init;
341 if (!BN_hex2bn(&app_p3, app_p3_init)
342 || (l = BN_bn2nativepad(app_p3, bignumbin, sizeof(bignumbin))) < 0)
343 return 0;
344 bignumbin_l = (size_t)l;
345 strcpy(app_p4, app_p4_init);
346 app_p4_l = sizeof(app_p4_init);
fff68416 347 strcpy(app_p5, app_p5_init);
9ad41d24 348 app_p5_l = sizeof(app_p5_init);
fff68416 349 app_p6 = app_p6_init;
9ad41d24
RL
350 foo[0] = app_foo_init;
351 foo_l = sizeof(app_foo_init);
352
353 return 1;
354}
355
356/*
357 * Here, we define test OSSL_PARAM arrays
358 */
359
360/* An array of OSSL_PARAM, specific in the most raw manner possible */
fff68416 361static const OSSL_PARAM static_raw_params[] = {
9ad41d24
RL
362 { "p1", OSSL_PARAM_INTEGER, &app_p1, sizeof(app_p1), NULL },
363 { "p3", OSSL_PARAM_UNSIGNED_INTEGER, &bignumbin, sizeof(bignumbin),
364 &bignumbin_l },
365 { "p4", OSSL_PARAM_UTF8_STRING, &app_p4, sizeof(app_p4), &app_p4_l },
fff68416
RL
366 { "p5", OSSL_PARAM_UTF8_STRING, &app_p5, sizeof(app_p5), &app_p5_l },
367 { "p6", OSSL_PARAM_UTF8_PTR, &app_p6, sizeof(app_p6), &app_p6_l },
9ad41d24
RL
368 { "foo", OSSL_PARAM_OCTET_STRING, &foo, sizeof(foo), &foo_l },
369 { NULL, 0, NULL, 0, NULL }
370};
371
bc1e0be7 372/* The same array of OSSL_PARAM, specified with the macros from params.h */
fff68416 373static const OSSL_PARAM static_api_params[] = {
bc1e0be7
RL
374 OSSL_PARAM_int("p1", &app_p1),
375 OSSL_PARAM_SIZED_BN("p3", &bignumbin, sizeof(bignumbin), bignumbin_l),
fff68416
RL
376 OSSL_PARAM_DEFN("p4", OSSL_PARAM_UTF8_STRING,
377 &app_p4, sizeof(app_p4), &app_p4_l),
378 OSSL_PARAM_DEFN("p5", OSSL_PARAM_UTF8_STRING,
379 &app_p5, sizeof(app_p5), &app_p5_l),
380 OSSL_PARAM_DEFN("p6", OSSL_PARAM_UTF8_PTR,
381 &app_p6, sizeof(app_p6), &app_p6_l),
bc1e0be7
RL
382 OSSL_PARAM_DEFN("foo", OSSL_PARAM_OCTET_STRING, &foo, sizeof(foo), &foo_l),
383 OSSL_PARAM_END
384};
385
fff68416
RL
386/*
387 * The same array again, but constructed at run-time
388 * This exercises the OSSL_PARAM constructor functions
389 */
84727507 390static OSSL_PARAM *construct_api_params(void)
fff68416
RL
391{
392 size_t n = 0;
393 static OSSL_PARAM params[10];
fff68416
RL
394
395 params[n++] = OSSL_PARAM_construct_int("p1", &app_p1, NULL);
396 params[n++] = OSSL_PARAM_construct_BN("p3", bignumbin, sizeof(bignumbin),
397 &bignumbin_l);
398 params[n++] = OSSL_PARAM_construct_utf8_string("p4", app_p4, sizeof(app_p4),
399 &app_p4_l);
400 params[n++] = OSSL_PARAM_construct_utf8_string("p5", app_p5,
401 sizeof(app_p5), &app_p5_l);
402 params[n++] = OSSL_PARAM_construct_utf8_ptr("p6", (char **)&app_p6,
403 &app_p6_l);
404 params[n++] = OSSL_PARAM_construct_octet_string("foo", &foo, sizeof(foo),
405 &foo_l);
195852fe 406 params[n++] = OSSL_PARAM_construct_end();
fff68416
RL
407
408 return params;
409}
410
411struct param_owner_st {
412 const OSSL_PARAM *static_params;
413 OSSL_PARAM *(*constructed_params)(void);
414};
415
932c3d0f 416static const struct param_owner_st raw_params = {
fff68416
RL
417 static_raw_params, NULL
418};
419
932c3d0f 420static const struct param_owner_st api_params = {
fff68416
RL
421 static_api_params, construct_api_params
422};
423
9ad41d24
RL
424/*-
425 * TESTING
426 * =======
427 */
428
429/*
430 * Test cases to combine parameters with "provider side" functions
431 */
432static struct {
433 const struct provider_dispatch_st *prov;
fff68416 434 const struct param_owner_st *app;
9ad41d24
RL
435 const char *desc;
436} test_cases[] = {
bc1e0be7 437 /* Tests within specific methods */
fff68416
RL
438 { &provider_raw, &raw_params, "raw provider vs raw params" },
439 { &provider_api, &api_params, "api provider vs api params" },
bc1e0be7
RL
440
441 /* Mixed methods */
fff68416
RL
442 { &provider_raw, &api_params, "raw provider vs api params" },
443 { &provider_api, &raw_params, "api provider vs raw params" },
9ad41d24
RL
444};
445
446/* Generic tester of combinations of "providers" and params */
fff68416
RL
447static int test_case_variant(const OSSL_PARAM *params,
448 const struct provider_dispatch_st *prov)
9ad41d24 449{
9ad41d24
RL
450 BIGNUM *verify_p3 = NULL;
451 void *obj = NULL;
452 int errcnt = 0;
453
9ad41d24
RL
454 /*
455 * Initialize
456 */
457 if (!TEST_ptr(obj = init_object())
458 || !TEST_true(BN_hex2bn(&verify_p3, p3_init))) {
459 errcnt++;
460 goto fin;
461 }
462
463 /*
464 * Get parameters a first time, just to see that getting works and
465 * gets us the values we expect.
466 */
467 init_app_variables();
468
469 if (!TEST_true(prov->get_params(obj, params))
470 || !TEST_int_eq(app_p1, p1_init) /* "provider" value */
d3620841 471 || !TEST_double_eq(app_p2, app_p2_init) /* Should remain untouched */
9ad41d24
RL
472 || !TEST_ptr(BN_native2bn(bignumbin, bignumbin_l, app_p3))
473 || !TEST_BN_eq(app_p3, verify_p3) /* "provider" value */
474 || !TEST_str_eq(app_p4, p4_init) /* "provider" value */
475 || !TEST_str_eq(app_p5, p5_init) /* "provider" value */
fff68416 476 || !TEST_str_eq(app_p6, p6_init) /* "provider" value */
9ad41d24
RL
477 || !TEST_char_eq(foo[0], app_foo_init) /* Should remain untouched */
478 || !TEST_int_eq(foo_l, sizeof(app_foo_init)))
479 errcnt++;
480
481 /*
482 * Set parameters, then sneak into the object itself and check
483 * that its attributes got set (or ignored) properly.
484 */
485 init_app_variables();
486
487 if (!TEST_true(prov->set_params(obj, params))) {
488 errcnt++;
489 } else {
490 struct object_st *sneakpeek = obj;
491
492 if (!TEST_int_eq(sneakpeek->p1, app_p1) /* app value set */
d3620841 493 || !TEST_double_eq(sneakpeek->p2, p2_init) /* Should remain untouched */
9ad41d24
RL
494 || !TEST_BN_eq(sneakpeek->p3, app_p3) /* app value set */
495 || !TEST_str_eq(sneakpeek->p4, app_p4) /* app value set */
496 || !TEST_str_eq(sneakpeek->p5, app_p5)) /* app value set */
497 errcnt++;
498 }
499
500 /*
501 * Get parameters again, checking that we get different values
502 * than earlier where relevant.
503 */
504 BN_free(verify_p3);
505 verify_p3 = NULL;
506
507 if (!TEST_true(BN_hex2bn(&verify_p3, app_p3_init))) {
508 errcnt++;
509 goto fin;
510 }
511
512 if (!TEST_true(prov->get_params(obj, params))
513 || !TEST_int_eq(app_p1, app_p1_init) /* app value */
d3620841 514 || !TEST_double_eq(app_p2, app_p2_init) /* Should remain untouched */
9ad41d24
RL
515 || !TEST_ptr(BN_native2bn(bignumbin, bignumbin_l, app_p3))
516 || !TEST_BN_eq(app_p3, verify_p3) /* app value */
517 || !TEST_str_eq(app_p4, app_p4_init) /* app value */
518 || !TEST_str_eq(app_p5, app_p5_init) /* app value */
fff68416 519 || !TEST_str_eq(app_p6, app_p6_init) /* app value */
9ad41d24
RL
520 || !TEST_char_eq(foo[0], app_foo_init) /* Should remain untouched */
521 || !TEST_int_eq(foo_l, sizeof(app_foo_init)))
522 errcnt++;
523
524 fin:
525 BN_free(verify_p3);
526 verify_p3 = NULL;
527 cleanup_app_variables();
528 cleanup_object(obj);
529
530 return errcnt == 0;
531}
532
fff68416
RL
533static int test_case(int i)
534{
535 TEST_info("Case: %s", test_cases[i].desc);
536
537 return test_case_variant(test_cases[i].app->static_params,
538 test_cases[i].prov)
539 && (test_cases[i].app->constructed_params == NULL
540 || test_case_variant(test_cases[i].app->constructed_params(),
541 test_cases[i].prov));
542}
543
9ad41d24
RL
544int setup_tests(void)
545{
546 ADD_ALL_TESTS(test_case, OSSL_NELEM(test_cases));
547 return 1;
548}