2 * Copyright 2019-2021 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 accommodate.
50 * Assumed data type OSSL_PARAM_UNSIGNED_INTEGER
54 * Documented as a C string.
55 * The data size must be large enough to accommodate.
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 obj->p4_l = strlen(obj->p4);
145 } else if (strcmp(params->key, "p5") == 0) {
147 * Protect obj->p5 against too much data. This should not
148 * happen, we don't use that long strings.
151 OPENSSL_strnlen(params->data, params->data_size);
153 if (!TEST_size_t_lt(data_length, sizeof(obj->p5)))
155 strncpy(obj->p5, params->data, data_length);
156 obj->p5[data_length] = '\0';
157 obj->p5_l = strlen(obj->p5);
158 } else if (strcmp(params->key, "p6") == 0) {
159 obj->p6 = *(const char **)params->data;
160 obj->p6_l = params->data_size;
166 static int raw_get_params(void *vobj, OSSL_PARAM *params)
168 struct object_st *obj = vobj;
170 for (; params->key != NULL; params++)
171 if (strcmp(params->key, "p1") == 0) {
172 params->return_size = sizeof(obj->p1);
173 *(int *)params->data = obj->p1;
174 } else if (strcmp(params->key, "p2") == 0) {
175 params->return_size = sizeof(obj->p2);
176 *(double *)params->data = obj->p2;
177 } else if (strcmp(params->key, "p3") == 0) {
178 params->return_size = BN_num_bytes(obj->p3);
179 if (!TEST_size_t_ge(params->data_size, params->return_size))
181 BN_bn2nativepad(obj->p3, params->data, params->return_size);
182 } else if (strcmp(params->key, "p4") == 0) {
183 params->return_size = strlen(obj->p4);
184 if (!TEST_size_t_gt(params->data_size, params->return_size))
186 strcpy(params->data, obj->p4);
187 } else if (strcmp(params->key, "p5") == 0) {
188 params->return_size = strlen(obj->p5);
189 if (!TEST_size_t_gt(params->data_size, params->return_size))
191 strcpy(params->data, obj->p5);
192 } else if (strcmp(params->key, "p6") == 0) {
193 params->return_size = strlen(obj->p6);
194 *(const char **)params->data = obj->p6;
201 * API provider, which handles the parameters using the API from params.h
204 static int api_set_params(void *vobj, const OSSL_PARAM *params)
206 struct object_st *obj = vobj;
207 const OSSL_PARAM *p = NULL;
209 if ((p = OSSL_PARAM_locate_const(params, "p1")) != NULL
210 && !TEST_true(OSSL_PARAM_get_int(p, &obj->p1)))
212 if ((p = OSSL_PARAM_locate_const(params, "p2")) != NULL
213 && !TEST_true(OSSL_PARAM_get_double(p, &obj->p2)))
215 if ((p = OSSL_PARAM_locate_const(params, "p3")) != NULL
216 && !TEST_true(OSSL_PARAM_get_BN(p, &obj->p3)))
218 if ((p = OSSL_PARAM_locate_const(params, "p4")) != NULL) {
219 OPENSSL_free(obj->p4);
221 /* If the value pointer is NULL, we get it automatically allocated */
222 if (!TEST_true(OSSL_PARAM_get_utf8_string(p, &obj->p4, 0)))
225 if ((p = OSSL_PARAM_locate_const(params, "p5")) != NULL) {
226 char *p5_ptr = obj->p5;
227 if (!TEST_true(OSSL_PARAM_get_utf8_string(p, &p5_ptr, sizeof(obj->p5))))
229 obj->p5_l = strlen(obj->p5);
231 if ((p = OSSL_PARAM_locate_const(params, "p6")) != NULL) {
232 if (!TEST_true(OSSL_PARAM_get_utf8_ptr(p, &obj->p6)))
234 obj->p6_l = strlen(obj->p6);
240 static int api_get_params(void *vobj, OSSL_PARAM *params)
242 struct object_st *obj = vobj;
243 OSSL_PARAM *p = NULL;
245 if ((p = OSSL_PARAM_locate(params, "p1")) != NULL
246 && !TEST_true(OSSL_PARAM_set_int(p, obj->p1)))
248 if ((p = OSSL_PARAM_locate(params, "p2")) != NULL
249 && !TEST_true(OSSL_PARAM_set_double(p, obj->p2)))
251 if ((p = OSSL_PARAM_locate(params, "p3")) != NULL
252 && !TEST_true(OSSL_PARAM_set_BN(p, obj->p3)))
254 if ((p = OSSL_PARAM_locate(params, "p4")) != NULL
255 && !TEST_true(OSSL_PARAM_set_utf8_string(p, obj->p4)))
257 if ((p = OSSL_PARAM_locate(params, "p5")) != NULL
258 && !TEST_true(OSSL_PARAM_set_utf8_string(p, obj->p5)))
260 if ((p = OSSL_PARAM_locate(params, "p6")) != NULL
261 && !TEST_true(OSSL_PARAM_set_utf8_ptr(p, obj->p6)))
268 * This structure only simulates a provider dispatch, the real deal is
269 * a bit more code that's not necessary in these tests.
271 struct provider_dispatch_st {
272 int (*set_params)(void *obj, const OSSL_PARAM *params);
273 int (*get_params)(void *obj, OSSL_PARAM *params);
277 static const struct provider_dispatch_st provider_raw = {
278 raw_set_params, raw_get_params
282 static const struct provider_dispatch_st provider_api = {
283 api_set_params, api_get_params
287 * APPLICATION SECTION
288 * ===================
291 /* In all our tests, these are variables that get manipulated as parameters
293 * These arrays consistently do nothing with the "p2" parameter, and
294 * always include a "foo" parameter. This is to check that the
295 * set_params and get_params calls ignore the lack of parameters that
296 * the application isn't interested in, as well as ignore parameters
297 * they don't understand (the application may have one big bag of
300 static int app_p1; /* "p1" */
301 static double app_p2; /* "p2" is ignored */
302 static BIGNUM *app_p3 = NULL; /* "p3" */
303 static unsigned char bignumbin[4096]; /* "p3" */
304 static char app_p4[256]; /* "p4" */
305 static char app_p5[256]; /* "p5" */
306 static const char *app_p6 = NULL; /* "p6" */
307 static unsigned char foo[1]; /* "foo" */
309 #define app_p1_init 17 /* A random number */
310 #define app_p2_init 47.11 /* Another random number */
311 #define app_p3_init "deadbeef" /* Classic */
312 #define app_p4_init "Hello"
313 #define app_p5_init "World"
314 #define app_p6_init "Cookie"
315 #define app_foo_init 'z'
317 static int cleanup_app_variables(void)
324 static int init_app_variables(void)
328 cleanup_app_variables();
330 app_p1 = app_p1_init;
331 app_p2 = app_p2_init;
332 if (!BN_hex2bn(&app_p3, app_p3_init)
333 || (l = BN_bn2nativepad(app_p3, bignumbin, sizeof(bignumbin))) < 0)
335 strcpy(app_p4, app_p4_init);
336 strcpy(app_p5, app_p5_init);
337 app_p6 = app_p6_init;
338 foo[0] = app_foo_init;
344 * Here, we define test OSSL_PARAM arrays
347 /* An array of OSSL_PARAM, specific in the most raw manner possible */
348 static OSSL_PARAM static_raw_params[] = {
349 { "p1", OSSL_PARAM_INTEGER, &app_p1, sizeof(app_p1), 0 },
350 { "p3", OSSL_PARAM_UNSIGNED_INTEGER, &bignumbin, sizeof(bignumbin), 0 },
351 { "p4", OSSL_PARAM_UTF8_STRING, &app_p4, sizeof(app_p4), 0 },
352 { "p5", OSSL_PARAM_UTF8_STRING, &app_p5, sizeof(app_p5), 0 },
353 /* sizeof(app_p6_init) - 1, because we know that's what we're using */
354 { "p6", OSSL_PARAM_UTF8_PTR, &app_p6, sizeof(app_p6_init) - 1, 0 },
355 { "foo", OSSL_PARAM_OCTET_STRING, &foo, sizeof(foo), 0 },
356 { NULL, 0, NULL, 0, 0 }
359 /* The same array of OSSL_PARAM, specified with the macros from params.h */
360 static OSSL_PARAM static_api_params[] = {
361 OSSL_PARAM_int("p1", &app_p1),
362 OSSL_PARAM_BN("p3", &bignumbin, sizeof(bignumbin)),
363 OSSL_PARAM_DEFN("p4", OSSL_PARAM_UTF8_STRING, &app_p4, sizeof(app_p4)),
364 OSSL_PARAM_DEFN("p5", OSSL_PARAM_UTF8_STRING, &app_p5, sizeof(app_p5)),
365 /* sizeof(app_p6_init), because we know that's what we're using */
366 OSSL_PARAM_DEFN("p6", OSSL_PARAM_UTF8_PTR, &app_p6,
367 sizeof(app_p6_init) - 1),
368 OSSL_PARAM_DEFN("foo", OSSL_PARAM_OCTET_STRING, &foo, sizeof(foo)),
373 * The same array again, but constructed at run-time
374 * This exercises the OSSL_PARAM constructor functions
376 static OSSL_PARAM *construct_api_params(void)
379 static OSSL_PARAM params[10];
381 params[n++] = OSSL_PARAM_construct_int("p1", &app_p1);
382 params[n++] = OSSL_PARAM_construct_BN("p3", bignumbin, sizeof(bignumbin));
383 params[n++] = OSSL_PARAM_construct_utf8_string("p4", app_p4,
385 params[n++] = OSSL_PARAM_construct_utf8_string("p5", app_p5,
387 /* sizeof(app_p6_init), because we know that's what we're using */
388 params[n++] = OSSL_PARAM_construct_utf8_ptr("p6", (char **)&app_p6,
389 sizeof(app_p6_init));
390 params[n++] = OSSL_PARAM_construct_octet_string("foo", &foo, sizeof(foo));
391 params[n++] = OSSL_PARAM_construct_end();
396 struct param_owner_st {
397 OSSL_PARAM *static_params;
398 OSSL_PARAM *(*constructed_params)(void);
401 static const struct param_owner_st raw_params = {
402 static_raw_params, NULL
405 static const struct param_owner_st api_params = {
406 static_api_params, construct_api_params
415 * Test cases to combine parameters with "provider side" functions
418 const struct provider_dispatch_st *prov;
419 const struct param_owner_st *app;
422 /* Tests within specific methods */
423 { &provider_raw, &raw_params, "raw provider vs raw params" },
424 { &provider_api, &api_params, "api provider vs api params" },
427 { &provider_raw, &api_params, "raw provider vs api params" },
428 { &provider_api, &raw_params, "api provider vs raw params" },
431 /* Generic tester of combinations of "providers" and params */
432 static int test_case_variant(OSSL_PARAM *params, const struct provider_dispatch_st *prov)
434 BIGNUM *verify_p3 = NULL;
442 if (!TEST_ptr(obj = init_object())
443 || !TEST_true(BN_hex2bn(&verify_p3, p3_init))) {
449 * Get parameters a first time, just to see that getting works and
450 * gets us the values we expect.
452 init_app_variables();
454 if (!TEST_true(prov->get_params(obj, params))
455 || !TEST_int_eq(app_p1, p1_init) /* "provider" value */
456 || !TEST_double_eq(app_p2, app_p2_init) /* Should remain untouched */
457 || !TEST_ptr(p = OSSL_PARAM_locate(params, "p3"))
458 || !TEST_ptr(BN_native2bn(bignumbin, p->return_size, app_p3))
459 || !TEST_BN_eq(app_p3, verify_p3) /* "provider" value */
460 || !TEST_str_eq(app_p4, p4_init) /* "provider" value */
461 || !TEST_ptr(p = OSSL_PARAM_locate(params, "p5"))
462 || !TEST_size_t_eq(p->return_size,
463 sizeof(p5_init) - 1) /* "provider" value */
464 || !TEST_str_eq(app_p5, p5_init) /* "provider" value */
465 || !TEST_ptr(p = OSSL_PARAM_locate(params, "p6"))
466 || !TEST_size_t_eq(p->return_size,
467 sizeof(p6_init) - 1) /* "provider" value */
468 || !TEST_str_eq(app_p6, p6_init) /* "provider" value */
469 || !TEST_char_eq(foo[0], app_foo_init) /* Should remain untouched */
470 || !TEST_ptr(p = OSSL_PARAM_locate(params, "foo")))
474 * Set parameters, then sneak into the object itself and check
475 * that its attributes got set (or ignored) properly.
477 init_app_variables();
479 if (!TEST_true(prov->set_params(obj, params))) {
482 struct object_st *sneakpeek = obj;
484 if (!TEST_int_eq(sneakpeek->p1, app_p1) /* app value set */
485 || !TEST_double_eq(sneakpeek->p2, p2_init) /* Should remain untouched */
486 || !TEST_BN_eq(sneakpeek->p3, app_p3) /* app value set */
487 || !TEST_str_eq(sneakpeek->p4, app_p4) /* app value set */
488 || !TEST_str_eq(sneakpeek->p5, app_p5) /* app value set */
489 || !TEST_str_eq(sneakpeek->p6, app_p6)) /* app value set */
494 * Get parameters again, checking that we get different values
495 * than earlier where relevant.
500 if (!TEST_true(BN_hex2bn(&verify_p3, app_p3_init))) {
505 if (!TEST_true(prov->get_params(obj, params))
506 || !TEST_int_eq(app_p1, app_p1_init) /* app value */
507 || !TEST_double_eq(app_p2, app_p2_init) /* Should remain untouched */
508 || !TEST_ptr(p = OSSL_PARAM_locate(params, "p3"))
509 || !TEST_ptr(BN_native2bn(bignumbin, p->return_size, app_p3))
510 || !TEST_BN_eq(app_p3, verify_p3) /* app value */
511 || !TEST_str_eq(app_p4, app_p4_init) /* app value */
512 || !TEST_ptr(p = OSSL_PARAM_locate(params, "p5"))
513 || !TEST_size_t_eq(p->return_size,
514 sizeof(app_p5_init) - 1) /* app value */
515 || !TEST_str_eq(app_p5, app_p5_init) /* app value */
516 || !TEST_ptr(p = OSSL_PARAM_locate(params, "p6"))
517 || !TEST_size_t_eq(p->return_size,
518 sizeof(app_p6_init) - 1) /* app value */
519 || !TEST_str_eq(app_p6, app_p6_init) /* app value */
520 || !TEST_char_eq(foo[0], app_foo_init) /* Should remain untouched */
521 || !TEST_ptr(p = OSSL_PARAM_locate(params, "foo")))
527 cleanup_app_variables();
533 static int test_case(int i)
535 TEST_info("Case: %s", test_cases[i].desc);
537 return test_case_variant(test_cases[i].app->static_params,
539 && (test_cases[i].app->constructed_params == NULL
540 || test_case_variant(test_cases[i].app->constructed_params(),
541 test_cases[i].prov));
545 * OSSL_PARAM_allocate_from_text() tests
546 * =====================================
549 static const OSSL_PARAM params_from_text[] = {
550 OSSL_PARAM_int32("int", NULL),
551 OSSL_PARAM_DEFN("short", OSSL_PARAM_INTEGER, NULL, sizeof(int16_t)),
552 OSSL_PARAM_DEFN("ushort", OSSL_PARAM_UNSIGNED_INTEGER, NULL, sizeof(uint16_t)),
556 struct int_from_text_test_st {
563 static struct int_from_text_test_st int_from_text_test_cases[] = {
565 { "int", "0", 0, 1 },
566 { "int", "101", 101, 1 },
567 { "int", "-102", -102, 1 },
568 { "int", "12A", 12, 1 }, /* incomplete */
569 { "int", "0x12B", 0x12B, 1 },
570 { "hexint", "12C", 0x12C, 1 },
571 { "hexint", "0x12D", 0, 1 }, /* zero */
572 /* test check of the target buffer size */
573 { "int", "0x7fffffff", INT32_MAX, 1 },
574 { "int", "2147483647", INT32_MAX, 1 },
575 { "int", "2147483648", 0, 0 }, /* too small buffer */
576 { "int", "-2147483648", INT32_MIN, 1 },
577 { "int", "-2147483649", 0, 0 }, /* too small buffer */
578 { "short", "0x7fff", INT16_MAX, 1 },
579 { "short", "32767", INT16_MAX, 1 },
580 { "short", "32768", 0, 0 }, /* too small buffer */
581 { "ushort", "0xffff", UINT16_MAX, 1 },
582 { "ushort", "65535", UINT16_MAX, 1 },
583 { "ushort", "65536", 0, 0 }, /* too small buffer */
586 static int check_int_from_text(const struct int_from_text_test_st a)
592 if (!OSSL_PARAM_allocate_from_text(¶m, params_from_text,
593 a.argname, a.strval, 0, NULL)) {
595 TEST_error("errant %s param \"%s\"", a.argname, a.strval);
599 res = OSSL_PARAM_get_long(¶m, &val);
600 OPENSSL_free(param.data);
602 if (res ^ a.res || val != a.intval) {
603 TEST_error("errant %s \"%s\" %li != %li",
604 a.argname, a.strval, a.intval, val);
611 static int test_allocate_from_text(int i)
613 return check_int_from_text(int_from_text_test_cases[i]);
616 int setup_tests(void)
618 ADD_ALL_TESTS(test_case, OSSL_NELEM(test_cases));
619 ADD_ALL_TESTS(test_allocate_from_text, OSSL_NELEM(int_from_text_test_cases));