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 "internal/nelem.h"
25 * Even though it's not necessarily ONLY providers doing this part,
26 * they are naturally going to be the most common users of
27 * set_params and get_params functions.
31 * In real use cases, setters and getters would take an object with
32 * which the parameters are associated. This structure is a cheap
37 * Documented as a native integer, of the size given by sizeof(int).
38 * Assumed data type OSSL_PARAM_INTEGER
42 * Documented as a native double, of the size given by sizeof(double).
43 * Assumed data type OSSL_PARAM_REAL
47 * Documented as an arbitrarly large unsigned integer.
48 * The data size must be large enough to accomodate.
49 * Assumed data type OSSL_PARAM_UNSIGNED_INTEGER
53 * Documented as a C string.
54 * The data size must be large enough to accomodate.
55 * Assumed data type OSSL_PARAM_UTF8_STRING
59 * Documented as a pointer to a constant C string.
60 * Assumed data type OSSL_PARAM_UTF8_STRING_PTR
65 #define p1_init 42 /* The ultimate answer */
66 #define p2_init 6.283 /* Magic number */
67 /* Stolen from evp_data, BLAKE2s256 test */
69 "4142434445464748494a4b4c4d4e4f50" \
70 "5152535455565758595a616263646566" \
71 "6768696a6b6c6d6e6f70717273747576" \
72 "7778797a30313233343536373839"
73 #define p4_init "BLAKE2s256" /* Random string */
74 #define p5_init OPENSSL_FULL_VERSION_STR /* Static string */
76 static void cleanup_object(void *vobj)
78 struct object_st *obj = vobj;
82 OPENSSL_free(obj->p4);
87 static void *init_object(void)
89 struct object_st *obj = OPENSSL_zalloc(sizeof(*obj));
93 if (!TEST_true(BN_hex2bn(&obj->p3, p3_init)))
95 if (!TEST_ptr(obj->p4 = OPENSSL_strdup(p4_init)))
108 * RAW provider, which handles the parameters in a very raw manner,
109 * with no fancy API and very minimal checking. The application that
110 * calls these to set or request parameters MUST get its OSSL_PARAM
114 static int raw_set_params(void *vobj, const OSSL_PARAM *params)
116 struct object_st *obj = vobj;
118 for (; params->key != NULL; params++)
119 if (strcmp(params->key, "p1") == 0) {
120 obj->p1 = *(int *)params->data;
121 } else if (strcmp(params->key, "p2") == 0) {
122 obj->p2 = *(double *)params->data;
123 } else if (strcmp(params->key, "p3") == 0) {
125 if (!TEST_ptr(obj->p3 = BN_native2bn(params->data,
126 params->data_size, NULL)))
128 } else if (strcmp(params->key, "p4") == 0) {
129 OPENSSL_free(obj->p4);
130 if (!TEST_ptr(obj->p4 = OPENSSL_strndup(params->data,
133 } else if (strcmp(params->key, "p5") == 0) {
134 obj->p5 = *(const char **)params->data;
140 static int raw_get_params(void *vobj, const OSSL_PARAM *params)
142 struct object_st *obj = vobj;
144 for (; params->key != NULL; params++)
145 if (strcmp(params->key, "p1") == 0) {
146 if (params->return_size != NULL)
147 *params->return_size = sizeof(obj->p1);
148 *(int *)params->data = obj->p1;
149 } else if (strcmp(params->key, "p2") == 0) {
150 if (params->return_size != NULL)
151 *params->return_size = sizeof(obj->p2);
152 *(double *)params->data = obj->p2;
153 } else if (strcmp(params->key, "p3") == 0) {
154 size_t bytes = BN_num_bytes(obj->p3);
156 if (params->return_size != NULL)
157 *params->return_size = bytes;
158 if (!TEST_size_t_ge(params->data_size, bytes))
160 BN_bn2nativepad(obj->p3, params->data, bytes);
161 } else if (strcmp(params->key, "p4") == 0) {
162 size_t bytes = strlen(obj->p4) + 1;
164 if (params->return_size != NULL)
165 *params->return_size = bytes;
166 if (!TEST_size_t_ge(params->data_size, bytes))
168 strcpy(params->data, obj->p4);
169 } else if (strcmp(params->key, "p5") == 0) {
171 * We COULD also use OPENSSL_FULL_VERSION_STR directly and
172 * use sizeof(OPENSSL_FULL_VERSION_STR) instead of calling
174 * The caller wouldn't know the difference.
176 size_t bytes = strlen(obj->p5) + 1;
178 if (params->return_size != NULL)
179 *params->return_size = bytes;
180 *(const char **)params->data = obj->p5;
187 * This structure only simulates a provider dispatch, the real deal is
188 * a bit more code that's not necessary in these tests.
190 struct provider_dispatch_st {
191 int (*set_params)(void *obj, const OSSL_PARAM *params);
192 int (*get_params)(void *obj, const OSSL_PARAM *params);
196 static const struct provider_dispatch_st provider_raw = {
197 raw_set_params, raw_get_params
201 * APPLICATION SECTION
202 * ===================
205 /* In all our tests, these are variables that get manipulated as parameters
207 * These arrays consistenly do nothing with the "p2" parameter, and
208 * always include a "foo" parameter. This is to check that the
209 * set_params and get_params calls ignore the lack of parameters that
210 * the application isn't interested in, as well as ignore parameters
211 * they don't understand (the application may have one big bag of
214 static int app_p1; /* "p1" */
215 static double app_p2; /* "p2" is ignored */
216 static BIGNUM *app_p3 = NULL; /* "p3" */
217 static unsigned char bignumbin[4096]; /* "p3" */
218 static size_t bignumbin_l; /* "p3" */
219 static char app_p4[256]; /* "p4" */
220 static size_t app_p4_l; /* "p4" */
221 static const char *app_p5 = NULL; /* "p5" */
222 static size_t app_p5_l; /* "p5" */
223 static unsigned char foo[1]; /* "foo" */
224 static size_t foo_l; /* "foo" */
226 #define app_p1_init 17 /* A random number */
227 #define app_p2_init 47.11 /* Another random number */
228 #define app_p3_init "deadbeef" /* Classic */
229 #define app_p4_init "Hello"
230 #define app_p5_init "World"
231 #define app_foo_init 'z'
233 static int cleanup_app_variables(void)
240 static int init_app_variables(void)
244 cleanup_app_variables();
246 app_p1 = app_p1_init;
247 app_p2 = app_p2_init;
248 if (!BN_hex2bn(&app_p3, app_p3_init)
249 || (l = BN_bn2nativepad(app_p3, bignumbin, sizeof(bignumbin))) < 0)
251 bignumbin_l = (size_t)l;
252 strcpy(app_p4, app_p4_init);
253 app_p4_l = sizeof(app_p4_init);
254 app_p5 = app_p5_init;
255 app_p5_l = sizeof(app_p5_init);
256 foo[0] = app_foo_init;
257 foo_l = sizeof(app_foo_init);
263 * Here, we define test OSSL_PARAM arrays
266 /* An array of OSSL_PARAM, specific in the most raw manner possible */
267 static const OSSL_PARAM raw_params[] = {
268 { "p1", OSSL_PARAM_INTEGER, &app_p1, sizeof(app_p1), NULL },
269 { "p3", OSSL_PARAM_UNSIGNED_INTEGER, &bignumbin, sizeof(bignumbin),
271 { "p4", OSSL_PARAM_UTF8_STRING, &app_p4, sizeof(app_p4), &app_p4_l },
272 { "p5", OSSL_PARAM_UTF8_STRING_PTR, &app_p5, sizeof(app_p5), &app_p5_l },
273 { "foo", OSSL_PARAM_OCTET_STRING, &foo, sizeof(foo), &foo_l },
274 { NULL, 0, NULL, 0, NULL }
283 * Test cases to combine parameters with "provider side" functions
286 const struct provider_dispatch_st *prov;
287 const OSSL_PARAM *params;
290 { &provider_raw, raw_params, "raw provider vs raw params" }
293 /* Generic tester of combinations of "providers" and params */
294 static int test_case(int i)
296 const struct provider_dispatch_st *prov = test_cases[i].prov;
297 const OSSL_PARAM *params = test_cases[i].params;
298 BIGNUM *verify_p3 = NULL;
302 TEST_info("Case: %s", test_cases[i].desc);
307 if (!TEST_ptr(obj = init_object())
308 || !TEST_true(BN_hex2bn(&verify_p3, p3_init))) {
314 * Get parameters a first time, just to see that getting works and
315 * gets us the values we expect.
317 init_app_variables();
319 if (!TEST_true(prov->get_params(obj, params))
320 || !TEST_int_eq(app_p1, p1_init) /* "provider" value */
321 || !TEST_ulong_eq(app_p2, app_p2_init) /* Should remain untouched */
322 || !TEST_ptr(BN_native2bn(bignumbin, bignumbin_l, app_p3))
323 || !TEST_BN_eq(app_p3, verify_p3) /* "provider" value */
324 || !TEST_str_eq(app_p4, p4_init) /* "provider" value */
325 || !TEST_str_eq(app_p5, p5_init) /* "provider" value */
326 || !TEST_char_eq(foo[0], app_foo_init) /* Should remain untouched */
327 || !TEST_int_eq(foo_l, sizeof(app_foo_init)))
331 * Set parameters, then sneak into the object itself and check
332 * that its attributes got set (or ignored) properly.
334 init_app_variables();
336 if (!TEST_true(prov->set_params(obj, params))) {
339 struct object_st *sneakpeek = obj;
341 if (!TEST_int_eq(sneakpeek->p1, app_p1) /* app value set */
342 || !TEST_ulong_eq(sneakpeek->p2, p2_init) /* Should remain untouched */
343 || !TEST_BN_eq(sneakpeek->p3, app_p3) /* app value set */
344 || !TEST_str_eq(sneakpeek->p4, app_p4) /* app value set */
345 || !TEST_str_eq(sneakpeek->p5, app_p5)) /* app value set */
350 * Get parameters again, checking that we get different values
351 * than earlier where relevant.
356 if (!TEST_true(BN_hex2bn(&verify_p3, app_p3_init))) {
361 if (!TEST_true(prov->get_params(obj, params))
362 || !TEST_int_eq(app_p1, app_p1_init) /* app value */
363 || !TEST_ulong_eq(app_p2, app_p2_init) /* Should remain untouched */
364 || !TEST_ptr(BN_native2bn(bignumbin, bignumbin_l, app_p3))
365 || !TEST_BN_eq(app_p3, verify_p3) /* app value */
366 || !TEST_str_eq(app_p4, app_p4_init) /* app value */
367 || !TEST_str_eq(app_p5, app_p5_init) /* app value */
368 || !TEST_char_eq(foo[0], app_foo_init) /* Should remain untouched */
369 || !TEST_int_eq(foo_l, sizeof(app_foo_init)))
375 cleanup_app_variables();
381 int setup_tests(void)
383 ADD_ALL_TESTS(test_case, OSSL_NELEM(test_cases));