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 pointer to a constant C string.
62 * Assumed data type OSSL_PARAM_UTF8_PTR
68 #define p1_init 42 /* The ultimate answer */
69 #define p2_init 6.283 /* Magic number */
70 /* Stolen from evp_data, BLAKE2s256 test */
72 "4142434445464748494a4b4c4d4e4f50" \
73 "5152535455565758595a616263646566" \
74 "6768696a6b6c6d6e6f70717273747576" \
75 "7778797a30313233343536373839"
76 #define p4_init "BLAKE2s256" /* Random string */
77 #define p5_init OPENSSL_FULL_VERSION_STR /* Static string */
79 static void cleanup_object(void *vobj
)
81 struct object_st
*obj
= vobj
;
85 OPENSSL_free(obj
->p4
);
90 static void *init_object(void)
92 struct object_st
*obj
= OPENSSL_zalloc(sizeof(*obj
));
96 if (!TEST_true(BN_hex2bn(&obj
->p3
, p3_init
)))
98 if (!TEST_ptr(obj
->p4
= OPENSSL_strdup(p4_init
)))
111 * RAW provider, which handles the parameters in a very raw manner,
112 * with no fancy API and very minimal checking. The application that
113 * calls these to set or request parameters MUST get its OSSL_PARAM
117 static int raw_set_params(void *vobj
, const OSSL_PARAM
*params
)
119 struct object_st
*obj
= vobj
;
121 for (; params
->key
!= NULL
; params
++)
122 if (strcmp(params
->key
, "p1") == 0) {
123 obj
->p1
= *(int *)params
->data
;
124 } else if (strcmp(params
->key
, "p2") == 0) {
125 obj
->p2
= *(double *)params
->data
;
126 } else if (strcmp(params
->key
, "p3") == 0) {
128 if (!TEST_ptr(obj
->p3
= BN_native2bn(params
->data
,
129 params
->data_size
, NULL
)))
131 } else if (strcmp(params
->key
, "p4") == 0) {
132 OPENSSL_free(obj
->p4
);
133 if (!TEST_ptr(obj
->p4
= OPENSSL_strndup(params
->data
,
136 } else if (strcmp(params
->key
, "p5") == 0) {
137 obj
->p5
= *(const char **)params
->data
;
143 static int raw_get_params(void *vobj
, const OSSL_PARAM
*params
)
145 struct object_st
*obj
= vobj
;
147 for (; params
->key
!= NULL
; params
++)
148 if (strcmp(params
->key
, "p1") == 0) {
149 if (params
->return_size
!= NULL
)
150 *params
->return_size
= sizeof(obj
->p1
);
151 *(int *)params
->data
= obj
->p1
;
152 } else if (strcmp(params
->key
, "p2") == 0) {
153 if (params
->return_size
!= NULL
)
154 *params
->return_size
= sizeof(obj
->p2
);
155 *(double *)params
->data
= obj
->p2
;
156 } else if (strcmp(params
->key
, "p3") == 0) {
157 size_t bytes
= BN_num_bytes(obj
->p3
);
159 if (params
->return_size
!= NULL
)
160 *params
->return_size
= bytes
;
161 if (!TEST_size_t_ge(params
->data_size
, bytes
))
163 BN_bn2nativepad(obj
->p3
, params
->data
, bytes
);
164 } else if (strcmp(params
->key
, "p4") == 0) {
165 size_t bytes
= strlen(obj
->p4
) + 1;
167 if (params
->return_size
!= NULL
)
168 *params
->return_size
= bytes
;
169 if (!TEST_size_t_ge(params
->data_size
, bytes
))
171 strcpy(params
->data
, obj
->p4
);
172 } else if (strcmp(params
->key
, "p5") == 0) {
174 * We COULD also use OPENSSL_FULL_VERSION_STR directly and
175 * use sizeof(OPENSSL_FULL_VERSION_STR) instead of calling
177 * The caller wouldn't know the difference.
179 size_t bytes
= strlen(obj
->p5
) + 1;
181 if (params
->return_size
!= NULL
)
182 *params
->return_size
= bytes
;
183 *(const char **)params
->data
= obj
->p5
;
190 * API provider, which handles the parameters using the API from params.h
193 static int api_set_params(void *vobj
, const OSSL_PARAM
*params
)
195 struct object_st
*obj
= vobj
;
196 const OSSL_PARAM
*p
= NULL
;
198 if ((p
= OSSL_PARAM_locate(params
, "p1")) != NULL
199 && !TEST_true(OSSL_PARAM_get_int(p
, &obj
->p1
)))
201 if ((p
= OSSL_PARAM_locate(params
, "p2")) != NULL
202 && !TEST_true(OSSL_PARAM_get_double(p
, &obj
->p2
)))
204 if ((p
= OSSL_PARAM_locate(params
, "p3")) != NULL
205 && !TEST_true(OSSL_PARAM_get_BN(p
, &obj
->p3
)))
207 if ((p
= OSSL_PARAM_locate(params
, "p4")) != NULL
) {
208 OPENSSL_free(obj
->p4
);
210 /* If the value pointer is NULL, we get it automatically allocated */
211 if (!TEST_true(OSSL_PARAM_get_utf8_string(p
, &obj
->p4
, 0)))
214 if ((p
= OSSL_PARAM_locate(params
, "p5")) != NULL
215 && !TEST_true(OSSL_PARAM_get_utf8_ptr(p
, &obj
->p5
)))
221 static int api_get_params(void *vobj
, const OSSL_PARAM
*params
)
223 struct object_st
*obj
= vobj
;
224 const OSSL_PARAM
*p
= NULL
;
226 if ((p
= OSSL_PARAM_locate(params
, "p1")) != NULL
227 && !TEST_true(OSSL_PARAM_set_int(p
, obj
->p1
)))
229 if ((p
= OSSL_PARAM_locate(params
, "p2")) != NULL
230 && !TEST_true(OSSL_PARAM_set_double(p
, obj
->p2
)))
232 if ((p
= OSSL_PARAM_locate(params
, "p3")) != NULL
233 && !TEST_true(OSSL_PARAM_set_BN(p
, obj
->p3
)))
235 if ((p
= OSSL_PARAM_locate(params
, "p4")) != NULL
236 && !TEST_true(OSSL_PARAM_set_utf8_string(p
, obj
->p4
)))
238 if ((p
= OSSL_PARAM_locate(params
, "p5")) != NULL
239 && !TEST_true(OSSL_PARAM_set_utf8_ptr(p
, obj
->p5
)))
246 * This structure only simulates a provider dispatch, the real deal is
247 * a bit more code that's not necessary in these tests.
249 struct provider_dispatch_st
{
250 int (*set_params
)(void *obj
, const OSSL_PARAM
*params
);
251 int (*get_params
)(void *obj
, const OSSL_PARAM
*params
);
255 static const struct provider_dispatch_st provider_raw
= {
256 raw_set_params
, raw_get_params
260 static const struct provider_dispatch_st provider_api
= {
261 api_set_params
, api_get_params
265 * APPLICATION SECTION
266 * ===================
269 /* In all our tests, these are variables that get manipulated as parameters
271 * These arrays consistenly do nothing with the "p2" parameter, and
272 * always include a "foo" parameter. This is to check that the
273 * set_params and get_params calls ignore the lack of parameters that
274 * the application isn't interested in, as well as ignore parameters
275 * they don't understand (the application may have one big bag of
278 static int app_p1
; /* "p1" */
279 static double app_p2
; /* "p2" is ignored */
280 static BIGNUM
*app_p3
= NULL
; /* "p3" */
281 static unsigned char bignumbin
[4096]; /* "p3" */
282 static size_t bignumbin_l
; /* "p3" */
283 static char app_p4
[256]; /* "p4" */
284 static size_t app_p4_l
; /* "p4" */
285 static const char *app_p5
= NULL
; /* "p5" */
286 static size_t app_p5_l
; /* "p5" */
287 static unsigned char foo
[1]; /* "foo" */
288 static size_t foo_l
; /* "foo" */
290 #define app_p1_init 17 /* A random number */
291 #define app_p2_init 47.11 /* Another random number */
292 #define app_p3_init "deadbeef" /* Classic */
293 #define app_p4_init "Hello"
294 #define app_p5_init "World"
295 #define app_foo_init 'z'
297 static int cleanup_app_variables(void)
304 static int init_app_variables(void)
308 cleanup_app_variables();
310 app_p1
= app_p1_init
;
311 app_p2
= app_p2_init
;
312 if (!BN_hex2bn(&app_p3
, app_p3_init
)
313 || (l
= BN_bn2nativepad(app_p3
, bignumbin
, sizeof(bignumbin
))) < 0)
315 bignumbin_l
= (size_t)l
;
316 strcpy(app_p4
, app_p4_init
);
317 app_p4_l
= sizeof(app_p4_init
);
318 app_p5
= app_p5_init
;
319 app_p5_l
= sizeof(app_p5_init
);
320 foo
[0] = app_foo_init
;
321 foo_l
= sizeof(app_foo_init
);
327 * Here, we define test OSSL_PARAM arrays
330 /* An array of OSSL_PARAM, specific in the most raw manner possible */
331 static const OSSL_PARAM raw_params
[] = {
332 { "p1", OSSL_PARAM_INTEGER
, &app_p1
, sizeof(app_p1
), NULL
},
333 { "p3", OSSL_PARAM_UNSIGNED_INTEGER
, &bignumbin
, sizeof(bignumbin
),
335 { "p4", OSSL_PARAM_UTF8_STRING
, &app_p4
, sizeof(app_p4
), &app_p4_l
},
336 { "p5", OSSL_PARAM_UTF8_PTR
, &app_p5
, sizeof(app_p5
), &app_p5_l
},
337 { "foo", OSSL_PARAM_OCTET_STRING
, &foo
, sizeof(foo
), &foo_l
},
338 { NULL
, 0, NULL
, 0, NULL
}
341 /* The same array of OSSL_PARAM, specified with the macros from params.h */
342 static const OSSL_PARAM api_params
[] = {
343 OSSL_PARAM_int("p1", &app_p1
),
344 OSSL_PARAM_SIZED_BN("p3", &bignumbin
, sizeof(bignumbin
), bignumbin_l
),
345 OSSL_PARAM_DEFN("p4", OSSL_PARAM_UTF8_STRING
, &app_p4
, sizeof(app_p4
),
347 OSSL_PARAM_DEFN("p5", OSSL_PARAM_UTF8_PTR
, &app_p5
, sizeof(app_p5
),
349 OSSL_PARAM_DEFN("foo", OSSL_PARAM_OCTET_STRING
, &foo
, sizeof(foo
), &foo_l
),
359 * Test cases to combine parameters with "provider side" functions
362 const struct provider_dispatch_st
*prov
;
363 const OSSL_PARAM
*params
;
366 /* Tests within specific methods */
367 { &provider_raw
, raw_params
, "raw provider vs raw params" },
368 { &provider_api
, api_params
, "api provider vs api params" },
371 { &provider_raw
, api_params
, "raw provider vs api params" },
372 { &provider_api
, raw_params
, "api provider vs raw params" },
375 /* Generic tester of combinations of "providers" and params */
376 static int test_case(int i
)
378 const struct provider_dispatch_st
*prov
= test_cases
[i
].prov
;
379 const OSSL_PARAM
*params
= test_cases
[i
].params
;
380 BIGNUM
*verify_p3
= NULL
;
384 TEST_info("Case: %s", test_cases
[i
].desc
);
389 if (!TEST_ptr(obj
= init_object())
390 || !TEST_true(BN_hex2bn(&verify_p3
, p3_init
))) {
396 * Get parameters a first time, just to see that getting works and
397 * gets us the values we expect.
399 init_app_variables();
401 if (!TEST_true(prov
->get_params(obj
, params
))
402 || !TEST_int_eq(app_p1
, p1_init
) /* "provider" value */
403 || !TEST_ulong_eq(app_p2
, app_p2_init
) /* Should remain untouched */
404 || !TEST_ptr(BN_native2bn(bignumbin
, bignumbin_l
, app_p3
))
405 || !TEST_BN_eq(app_p3
, verify_p3
) /* "provider" value */
406 || !TEST_str_eq(app_p4
, p4_init
) /* "provider" value */
407 || !TEST_str_eq(app_p5
, p5_init
) /* "provider" value */
408 || !TEST_char_eq(foo
[0], app_foo_init
) /* Should remain untouched */
409 || !TEST_int_eq(foo_l
, sizeof(app_foo_init
)))
413 * Set parameters, then sneak into the object itself and check
414 * that its attributes got set (or ignored) properly.
416 init_app_variables();
418 if (!TEST_true(prov
->set_params(obj
, params
))) {
421 struct object_st
*sneakpeek
= obj
;
423 if (!TEST_int_eq(sneakpeek
->p1
, app_p1
) /* app value set */
424 || !TEST_ulong_eq(sneakpeek
->p2
, p2_init
) /* Should remain untouched */
425 || !TEST_BN_eq(sneakpeek
->p3
, app_p3
) /* app value set */
426 || !TEST_str_eq(sneakpeek
->p4
, app_p4
) /* app value set */
427 || !TEST_str_eq(sneakpeek
->p5
, app_p5
)) /* app value set */
432 * Get parameters again, checking that we get different values
433 * than earlier where relevant.
438 if (!TEST_true(BN_hex2bn(&verify_p3
, app_p3_init
))) {
443 if (!TEST_true(prov
->get_params(obj
, params
))
444 || !TEST_int_eq(app_p1
, app_p1_init
) /* app value */
445 || !TEST_ulong_eq(app_p2
, app_p2_init
) /* Should remain untouched */
446 || !TEST_ptr(BN_native2bn(bignumbin
, bignumbin_l
, app_p3
))
447 || !TEST_BN_eq(app_p3
, verify_p3
) /* app value */
448 || !TEST_str_eq(app_p4
, app_p4_init
) /* app value */
449 || !TEST_str_eq(app_p5
, app_p5_init
) /* app value */
450 || !TEST_char_eq(foo
[0], app_foo_init
) /* Should remain untouched */
451 || !TEST_int_eq(foo_l
, sizeof(app_foo_init
)))
457 cleanup_app_variables();
463 int setup_tests(void)
465 ADD_ALL_TESTS(test_case
, OSSL_NELEM(test_cases
));