2 * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
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.
12 * This program tests the use of OSSL_PARAM, currently in raw form.
16 #include <openssl/bn.h>
17 #include <openssl/core.h>
18 #include <openssl/params.h>
19 #include "internal/nelem.h"
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.
32 * In real use cases, setters and getters would take an object with
33 * which the parameters are associated. This structure is a cheap
38 * Documented as a native integer, of the size given by sizeof(int).
39 * Assumed data type OSSL_PARAM_INTEGER
43 * Documented as a native double, of the size given by sizeof(double).
44 * Assumed data type OSSL_PARAM_REAL
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
54 * Documented as a C string.
55 * The data size must be large enough to accomodate.
56 * Assumed data type OSSL_PARAM_UTF8_STRING
61 * Documented as a C string.
62 * Assumed data type OSSL_PARAM_UTF8_STRING
67 * Documented as a pointer to a constant C string.
68 * Assumed data type OSSL_PARAM_UTF8_PTR
74 #define p1_init 42 /* The ultimate answer */
75 #define p2_init 6.283 /* Magic number */
76 /* Stolen from evp_data, BLAKE2s256 test */
78 "4142434445464748494a4b4c4d4e4f50" \
79 "5152535455565758595a616263646566" \
80 "6768696a6b6c6d6e6f70717273747576" \
81 "7778797a30313233343536373839"
82 #define p4_init "BLAKE2s256" /* Random string */
83 #define p5_init "Hellow World" /* Random string */
84 #define p6_init OPENSSL_FULL_VERSION_STR /* Static string */
86 static void cleanup_object(void *vobj)
88 struct object_st *obj = vobj;
92 OPENSSL_free(obj->p4);
97 static void *init_object(void)
99 struct object_st *obj = OPENSSL_zalloc(sizeof(*obj));
103 if (!TEST_true(BN_hex2bn(&obj->p3, p3_init)))
105 if (!TEST_ptr(obj->p4 = OPENSSL_strdup(p4_init)))
107 strcpy(obj->p5, p5_init);
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
125 static int raw_set_params(void *vobj, const OSSL_PARAM *params)
127 struct object_st *obj = vobj;
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) {
136 if (!TEST_ptr(obj->p3 = BN_native2bn(params->data,
137 params->data_size, NULL)))
139 } else if (strcmp(params->key, "p4") == 0) {
140 OPENSSL_free(obj->p4);
141 if (!TEST_ptr(obj->p4 = OPENSSL_strndup(params->data,
144 } else if (strcmp(params->key, "p5") == 0) {
145 strncpy(obj->p5, params->data, params->data_size);
146 obj->p5_l = strlen(obj->p5) + 1;
147 } else if (strcmp(params->key, "p6") == 0) {
148 obj->p6 = *(const char **)params->data;
149 obj->p6_l = params->data_size;
155 static int raw_get_params(void *vobj, const OSSL_PARAM *params)
157 struct object_st *obj = vobj;
159 for (; params->key != NULL; params++)
160 if (strcmp(params->key, "p1") == 0) {
161 if (params->return_size != NULL)
162 *params->return_size = sizeof(obj->p1);
163 *(int *)params->data = obj->p1;
164 } else if (strcmp(params->key, "p2") == 0) {
165 if (params->return_size != NULL)
166 *params->return_size = sizeof(obj->p2);
167 *(double *)params->data = obj->p2;
168 } else if (strcmp(params->key, "p3") == 0) {
169 size_t bytes = BN_num_bytes(obj->p3);
171 if (params->return_size != NULL)
172 *params->return_size = bytes;
173 if (!TEST_size_t_ge(params->data_size, bytes))
175 BN_bn2nativepad(obj->p3, params->data, bytes);
176 } else if (strcmp(params->key, "p4") == 0) {
177 size_t bytes = strlen(obj->p4) + 1;
179 if (params->return_size != NULL)
180 *params->return_size = bytes;
181 if (!TEST_size_t_ge(params->data_size, bytes))
183 strcpy(params->data, obj->p4);
184 } else if (strcmp(params->key, "p5") == 0) {
185 size_t bytes = strlen(obj->p5) + 1;
187 if (params->return_size != NULL)
188 *params->return_size = bytes;
189 if (!TEST_size_t_ge(params->data_size, bytes))
191 strcpy(params->data, obj->p5);
192 } else if (strcmp(params->key, "p6") == 0) {
194 * We COULD also use OPENSSL_FULL_VERSION_STR directly and
195 * use sizeof(OPENSSL_FULL_VERSION_STR) instead of calling
197 * The caller wouldn't know the difference.
199 size_t bytes = strlen(obj->p6) + 1;
201 if (params->return_size != NULL)
202 *params->return_size = bytes;
203 *(const char **)params->data = obj->p6;
210 * API provider, which handles the parameters using the API from params.h
213 static int api_set_params(void *vobj, const OSSL_PARAM *params)
215 struct object_st *obj = vobj;
216 const OSSL_PARAM *p = NULL;
218 if ((p = OSSL_PARAM_locate(params, "p1")) != NULL
219 && !TEST_true(OSSL_PARAM_get_int(p, &obj->p1)))
221 if ((p = OSSL_PARAM_locate(params, "p2")) != NULL
222 && !TEST_true(OSSL_PARAM_get_double(p, &obj->p2)))
224 if ((p = OSSL_PARAM_locate(params, "p3")) != NULL
225 && !TEST_true(OSSL_PARAM_get_BN(p, &obj->p3)))
227 if ((p = OSSL_PARAM_locate(params, "p4")) != NULL) {
228 OPENSSL_free(obj->p4);
230 /* If the value pointer is NULL, we get it automatically allocated */
231 if (!TEST_true(OSSL_PARAM_get_utf8_string(p, &obj->p4, 0)))
234 if ((p = OSSL_PARAM_locate(params, "p5")) != NULL) {
235 char *p5_ptr = obj->p5;
236 if (!TEST_true(OSSL_PARAM_get_utf8_string(p, &p5_ptr, sizeof(obj->p5))))
238 obj->p5_l = strlen(obj->p5) + 1;
240 if ((p = OSSL_PARAM_locate(params, "p6")) != NULL) {
241 if (!TEST_true(OSSL_PARAM_get_utf8_ptr(p, &obj->p6)))
243 obj->p6_l = strlen(obj->p6) + 1;
249 static int api_get_params(void *vobj, const OSSL_PARAM *params)
251 struct object_st *obj = vobj;
252 const OSSL_PARAM *p = NULL;
254 if ((p = OSSL_PARAM_locate(params, "p1")) != NULL
255 && !TEST_true(OSSL_PARAM_set_int(p, obj->p1)))
257 if ((p = OSSL_PARAM_locate(params, "p2")) != NULL
258 && !TEST_true(OSSL_PARAM_set_double(p, obj->p2)))
260 if ((p = OSSL_PARAM_locate(params, "p3")) != NULL
261 && !TEST_true(OSSL_PARAM_set_BN(p, obj->p3)))
263 if ((p = OSSL_PARAM_locate(params, "p4")) != NULL
264 && !TEST_true(OSSL_PARAM_set_utf8_string(p, obj->p4)))
266 if ((p = OSSL_PARAM_locate(params, "p5")) != NULL
267 && !TEST_true(OSSL_PARAM_set_utf8_string(p, obj->p5)))
269 if ((p = OSSL_PARAM_locate(params, "p6")) != NULL
270 && !TEST_true(OSSL_PARAM_set_utf8_ptr(p, obj->p6)))
277 * This structure only simulates a provider dispatch, the real deal is
278 * a bit more code that's not necessary in these tests.
280 struct provider_dispatch_st {
281 int (*set_params)(void *obj, const OSSL_PARAM *params);
282 int (*get_params)(void *obj, const OSSL_PARAM *params);
286 static const struct provider_dispatch_st provider_raw = {
287 raw_set_params, raw_get_params
291 static const struct provider_dispatch_st provider_api = {
292 api_set_params, api_get_params
296 * APPLICATION SECTION
297 * ===================
300 /* In all our tests, these are variables that get manipulated as parameters
302 * These arrays consistenly do nothing with the "p2" parameter, and
303 * always include a "foo" parameter. This is to check that the
304 * set_params and get_params calls ignore the lack of parameters that
305 * the application isn't interested in, as well as ignore parameters
306 * they don't understand (the application may have one big bag of
309 static int app_p1; /* "p1" */
310 static double app_p2; /* "p2" is ignored */
311 static BIGNUM *app_p3 = NULL; /* "p3" */
312 static unsigned char bignumbin[4096]; /* "p3" */
313 static size_t bignumbin_l; /* "p3" */
314 static char app_p4[256]; /* "p4" */
315 static size_t app_p4_l; /* "p4" */
316 static char app_p5[256]; /* "p5" */
317 static size_t app_p5_l; /* "p5" */
318 static const char *app_p6 = NULL; /* "p6" */
319 static size_t app_p6_l; /* "p6" */
320 static unsigned char foo[1]; /* "foo" */
321 static size_t foo_l; /* "foo" */
323 #define app_p1_init 17 /* A random number */
324 #define app_p2_init 47.11 /* Another random number */
325 #define app_p3_init "deadbeef" /* Classic */
326 #define app_p4_init "Hello"
327 #define app_p5_init "World"
328 #define app_p6_init "Cookie"
329 #define app_foo_init 'z'
331 static int cleanup_app_variables(void)
338 static int init_app_variables(void)
342 cleanup_app_variables();
344 app_p1 = app_p1_init;
345 app_p2 = app_p2_init;
346 if (!BN_hex2bn(&app_p3, app_p3_init)
347 || (l = BN_bn2nativepad(app_p3, bignumbin, sizeof(bignumbin))) < 0)
349 bignumbin_l = (size_t)l;
350 strcpy(app_p4, app_p4_init);
351 app_p4_l = sizeof(app_p4_init);
352 strcpy(app_p5, app_p5_init);
353 app_p5_l = sizeof(app_p5_init);
354 app_p6 = app_p6_init;
355 foo[0] = app_foo_init;
356 foo_l = sizeof(app_foo_init);
362 * Here, we define test OSSL_PARAM arrays
365 /* An array of OSSL_PARAM, specific in the most raw manner possible */
366 static const OSSL_PARAM static_raw_params[] = {
367 { "p1", OSSL_PARAM_INTEGER, &app_p1, sizeof(app_p1), NULL },
368 { "p3", OSSL_PARAM_UNSIGNED_INTEGER, &bignumbin, sizeof(bignumbin),
370 { "p4", OSSL_PARAM_UTF8_STRING, &app_p4, sizeof(app_p4), &app_p4_l },
371 { "p5", OSSL_PARAM_UTF8_STRING, &app_p5, sizeof(app_p5), &app_p5_l },
372 /* sizeof(app_p6_init), because we know that's what we're using */
373 { "p6", OSSL_PARAM_UTF8_PTR, &app_p6, sizeof(app_p6_init), &app_p6_l },
374 { "foo", OSSL_PARAM_OCTET_STRING, &foo, sizeof(foo), &foo_l },
375 { NULL, 0, NULL, 0, NULL }
378 /* The same array of OSSL_PARAM, specified with the macros from params.h */
379 static const OSSL_PARAM static_api_params[] = {
380 OSSL_PARAM_int("p1", &app_p1),
381 OSSL_PARAM_SIZED_BN("p3", &bignumbin, sizeof(bignumbin), bignumbin_l),
382 OSSL_PARAM_DEFN("p4", OSSL_PARAM_UTF8_STRING,
383 &app_p4, sizeof(app_p4), &app_p4_l),
384 OSSL_PARAM_DEFN("p5", OSSL_PARAM_UTF8_STRING,
385 &app_p5, sizeof(app_p5), &app_p5_l),
386 /* sizeof(app_p6_init), because we know that's what we're using */
387 OSSL_PARAM_DEFN("p6", OSSL_PARAM_UTF8_PTR,
388 &app_p6, sizeof(app_p6_init), &app_p6_l),
389 OSSL_PARAM_DEFN("foo", OSSL_PARAM_OCTET_STRING, &foo, sizeof(foo), &foo_l),
394 * The same array again, but constructed at run-time
395 * This exercises the OSSL_PARAM constructor functions
397 static OSSL_PARAM *construct_api_params(void)
400 static OSSL_PARAM params[10];
402 params[n++] = OSSL_PARAM_construct_int("p1", &app_p1, NULL);
403 params[n++] = OSSL_PARAM_construct_BN("p3", bignumbin, sizeof(bignumbin),
405 params[n++] = OSSL_PARAM_construct_utf8_string("p4", app_p4, sizeof(app_p4),
407 params[n++] = OSSL_PARAM_construct_utf8_string("p5", app_p5,
408 sizeof(app_p5), &app_p5_l);
409 /* sizeof(app_p6_init), because we know that's what we're using */
410 params[n++] = OSSL_PARAM_construct_utf8_ptr("p6", (char **)&app_p6,
411 sizeof(app_p6_init), &app_p6_l);
412 params[n++] = OSSL_PARAM_construct_octet_string("foo", &foo, sizeof(foo),
414 params[n++] = OSSL_PARAM_construct_end();
419 struct param_owner_st {
420 const OSSL_PARAM *static_params;
421 OSSL_PARAM *(*constructed_params)(void);
424 static const struct param_owner_st raw_params = {
425 static_raw_params, NULL
428 static const struct param_owner_st api_params = {
429 static_api_params, construct_api_params
438 * Test cases to combine parameters with "provider side" functions
441 const struct provider_dispatch_st *prov;
442 const struct param_owner_st *app;
445 /* Tests within specific methods */
446 { &provider_raw, &raw_params, "raw provider vs raw params" },
447 { &provider_api, &api_params, "api provider vs api params" },
450 { &provider_raw, &api_params, "raw provider vs api params" },
451 { &provider_api, &raw_params, "api provider vs raw params" },
454 /* Generic tester of combinations of "providers" and params */
455 static int test_case_variant(const OSSL_PARAM *params,
456 const struct provider_dispatch_st *prov)
458 BIGNUM *verify_p3 = NULL;
465 if (!TEST_ptr(obj = init_object())
466 || !TEST_true(BN_hex2bn(&verify_p3, p3_init))) {
472 * Get parameters a first time, just to see that getting works and
473 * gets us the values we expect.
475 init_app_variables();
477 if (!TEST_true(prov->get_params(obj, params))
478 || !TEST_int_eq(app_p1, p1_init) /* "provider" value */
479 || !TEST_double_eq(app_p2, app_p2_init) /* Should remain untouched */
480 || !TEST_ptr(BN_native2bn(bignumbin, bignumbin_l, app_p3))
481 || !TEST_BN_eq(app_p3, verify_p3) /* "provider" value */
482 || !TEST_str_eq(app_p4, p4_init) /* "provider" value */
483 || !TEST_size_t_eq(app_p5_l, sizeof(p5_init)) /* "provider" value */
484 || !TEST_str_eq(app_p5, p5_init) /* "provider" value */
485 || !TEST_size_t_eq(app_p6_l, sizeof(p6_init)) /* "provider" value */
486 || !TEST_str_eq(app_p6, p6_init) /* "provider" value */
487 || !TEST_char_eq(foo[0], app_foo_init) /* Should remain untouched */
488 || !TEST_int_eq(foo_l, sizeof(app_foo_init)))
492 * Set parameters, then sneak into the object itself and check
493 * that its attributes got set (or ignored) properly.
495 init_app_variables();
497 if (!TEST_true(prov->set_params(obj, params))) {
500 struct object_st *sneakpeek = obj;
502 if (!TEST_int_eq(sneakpeek->p1, app_p1) /* app value set */
503 || !TEST_double_eq(sneakpeek->p2, p2_init) /* Should remain untouched */
504 || !TEST_BN_eq(sneakpeek->p3, app_p3) /* app value set */
505 || !TEST_str_eq(sneakpeek->p4, app_p4) /* app value set */
506 || !TEST_size_t_eq(sneakpeek->p5_l, app_p5_l) /* app value set */
507 || !TEST_str_eq(sneakpeek->p5, app_p5) /* app value set */
508 || !TEST_size_t_eq(sneakpeek->p6_l,
509 sizeof(app_p6_init)) /* app value set */
510 || !TEST_str_eq(sneakpeek->p6, app_p6)) /* app value set */
515 * Get parameters again, checking that we get different values
516 * than earlier where relevant.
521 if (!TEST_true(BN_hex2bn(&verify_p3, app_p3_init))) {
526 if (!TEST_true(prov->get_params(obj, params))
527 || !TEST_int_eq(app_p1, app_p1_init) /* app value */
528 || !TEST_double_eq(app_p2, app_p2_init) /* Should remain untouched */
529 || !TEST_ptr(BN_native2bn(bignumbin, bignumbin_l, app_p3))
530 || !TEST_BN_eq(app_p3, verify_p3) /* app value */
531 || !TEST_str_eq(app_p4, app_p4_init) /* app value */
532 || !TEST_size_t_eq(app_p5_l,
533 sizeof(app_p5_init)) /* app value */
534 || !TEST_str_eq(app_p5, app_p5_init) /* app value */
535 || !TEST_size_t_eq(app_p6_l,
536 sizeof(app_p6_init)) /* app value */
537 || !TEST_str_eq(app_p6, app_p6_init) /* app value */
538 || !TEST_char_eq(foo[0], app_foo_init) /* Should remain untouched */
539 || !TEST_int_eq(foo_l, sizeof(app_foo_init)))
545 cleanup_app_variables();
551 static int test_case(int i)
553 TEST_info("Case: %s", test_cases[i].desc);
555 return test_case_variant(test_cases[i].app->static_params,
557 && (test_cases[i].app->constructed_params == NULL
558 || test_case_variant(test_cases[i].app->constructed_params(),
559 test_cases[i].prov));
562 int setup_tests(void)
564 ADD_ALL_TESTS(test_case, OSSL_NELEM(test_cases));