]> git.ipfire.org Git - thirdparty/openssl.git/blob - test/params_api_test.c
Copyright year updates
[thirdparty/openssl.git] / test / params_api_test.c
1 /*
2 * Copyright 2019-2024 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
4 *
5 * Licensed under the Apache License 2.0 (the "License"). You may not use
6 * this file except in compliance with the License. You can obtain a copy
7 * in the file LICENSE in the source distribution or at
8 * https://www.openssl.org/source/license.html
9 */
10
11 #include <string.h>
12 #include "testutil.h"
13 #include "internal/nelem.h"
14 #include "internal/endian.h"
15 #include <openssl/params.h>
16 #include <openssl/bn.h>
17
18 /* The maximum size of the static buffers used to test most things */
19 #define MAX_LEN 20
20
21 static void swap_copy(unsigned char *out, const void *in, size_t len)
22 {
23 size_t j;
24
25 for (j = 0; j < len; j++)
26 out[j] = ((unsigned char *)in)[len - j - 1];
27 }
28
29 /*
30 * A memory copy that converts the native byte ordering either to or from
31 * little endian format.
32 *
33 * On a little endian machine copying either is just a memcpy(3), on a
34 * big endian machine copying from native to or from little endian involves
35 * byte reversal.
36 */
37 static void le_copy(unsigned char *out, size_t outlen,
38 const void *in, size_t inlen)
39 {
40 DECLARE_IS_ENDIAN;
41
42 if (IS_LITTLE_ENDIAN) {
43 memcpy(out, in, outlen);
44 } else {
45 if (outlen < inlen)
46 in = (const char *)in + inlen - outlen;
47 swap_copy(out, in, outlen);
48 }
49 }
50
51 static const struct {
52 size_t len;
53 unsigned char value[MAX_LEN];
54 } raw_values[] = {
55 { 1, { 0x47 } },
56 { 1, { 0xd0 } },
57 { 2, { 0x01, 0xe9 } },
58 { 2, { 0xff, 0x53 } },
59 { 3, { 0x16, 0xff, 0x7c } },
60 { 3, { 0xa8, 0x9c, 0x0e } },
61 { 4, { 0x38, 0x27, 0xbf, 0x3b } },
62 { 4, { 0x9f, 0x26, 0x48, 0x22 } },
63 { 5, { 0x30, 0x65, 0xfa, 0xe4, 0x81 } },
64 { 5, { 0xd1, 0x76, 0x01, 0x1b, 0xcd } },
65 { 8, { 0x59, 0xb2, 0x1a, 0xe9, 0x2a, 0xd8, 0x46, 0x40 } },
66 { 8, { 0xb4, 0xae, 0xbd, 0xb4, 0xdd, 0x04, 0xb1, 0x4c } },
67 { 16, { 0x61, 0xe8, 0x7e, 0x31, 0xe9, 0x33, 0x83, 0x3d,
68 0x87, 0x99, 0xc7, 0xd8, 0x5d, 0xa9, 0x8b, 0x42 } },
69 { 16, { 0xee, 0x6e, 0x8b, 0xc3, 0xec, 0xcf, 0x37, 0xcc,
70 0x89, 0x67, 0xf2, 0x68, 0x33, 0xa0, 0x14, 0xb0 } },
71 };
72
73 static int test_param_type_null(OSSL_PARAM *param)
74 {
75 int rc = 0;
76 uint64_t intval;
77 double dval;
78 BIGNUM *bn;
79
80 switch(param->data_type) {
81 case OSSL_PARAM_INTEGER:
82 if (param->data_size == sizeof(int32_t))
83 rc = OSSL_PARAM_get_int32(param, (int32_t *)&intval);
84 else if (param->data_size == sizeof(uint64_t))
85 rc = OSSL_PARAM_get_int64(param, (int64_t *)&intval);
86 else
87 return 1;
88 break;
89 case OSSL_PARAM_UNSIGNED_INTEGER:
90 if (param->data_size == sizeof(uint32_t))
91 rc = OSSL_PARAM_get_uint32(param, (uint32_t *)&intval);
92 else if (param->data_size == sizeof(uint64_t))
93 rc = OSSL_PARAM_get_uint64(param, &intval);
94 else
95 rc = OSSL_PARAM_get_BN(param, &bn);
96 break;
97 case OSSL_PARAM_REAL:
98 rc = OSSL_PARAM_get_double(param, &dval);
99 break;
100 case OSSL_PARAM_UTF8_STRING:
101 case OSSL_PARAM_OCTET_STRING:
102 case OSSL_PARAM_UTF8_PTR:
103 case OSSL_PARAM_OCTET_PTR:
104 /* these are allowed to be null */
105 return 1;
106 break;
107 }
108
109 /*
110 * we expect the various OSSL_PARAM_get functions above
111 * to return failure when the data is set to NULL
112 */
113 return rc == 0;
114 }
115
116 static int test_param_type_extra(OSSL_PARAM *param, const unsigned char *cmp,
117 size_t width)
118 {
119 int32_t i32;
120 int64_t i64;
121 size_t s, sz;
122 unsigned char buf[MAX_LEN];
123 const int bit32 = param->data_size <= sizeof(int32_t);
124 const int sizet = param->data_size <= sizeof(size_t);
125 const int signd = param->data_type == OSSL_PARAM_INTEGER;
126
127 /*
128 * Set the unmodified sentinel directly because there is no param array
129 * for these tests.
130 */
131 param->return_size = OSSL_PARAM_UNMODIFIED;
132 if (signd) {
133 if ((bit32 && !TEST_true(OSSL_PARAM_get_int32(param, &i32)))
134 || !TEST_true(OSSL_PARAM_get_int64(param, &i64)))
135 return 0;
136 } else {
137 if ((bit32
138 && !TEST_true(OSSL_PARAM_get_uint32(param, (uint32_t *)&i32)))
139 || !TEST_true(OSSL_PARAM_get_uint64(param, (uint64_t *)&i64))
140 || (sizet && !TEST_true(OSSL_PARAM_get_size_t(param, &s))))
141 return 0;
142 }
143 if (!TEST_false(OSSL_PARAM_modified(param)))
144 return 0;
145
146 /* Check signed types */
147 if (bit32) {
148 le_copy(buf, sizeof(i32), &i32, sizeof(i32));
149 sz = sizeof(i32) < width ? sizeof(i32) : width;
150 if (!TEST_mem_eq(buf, sz, cmp, sz))
151 return 0;
152 }
153 le_copy(buf, sizeof(i64), &i64, sizeof(i64));
154 sz = sizeof(i64) < width ? sizeof(i64) : width;
155 if (!TEST_mem_eq(buf, sz, cmp, sz))
156 return 0;
157 if (sizet && !signd) {
158 le_copy(buf, sizeof(s), &s, sizeof(s));
159 sz = sizeof(s) < width ? sizeof(s) : width;
160 if (!TEST_mem_eq(buf, sz, cmp, sz))
161 return 0;
162 }
163
164 /* Check a widening write if possible */
165 if (sizeof(size_t) > width) {
166 if (signd) {
167 if (!TEST_true(OSSL_PARAM_set_int32(param, 12345))
168 || !TEST_true(OSSL_PARAM_get_int64(param, &i64))
169 || !TEST_size_t_eq((size_t)i64, 12345))
170 return 0;
171 } else {
172 if (!TEST_true(OSSL_PARAM_set_uint32(param, 12345))
173 || !TEST_true(OSSL_PARAM_get_uint64(param, (uint64_t *)&i64))
174 || !TEST_size_t_eq((size_t)i64, 12345))
175 return 0;
176 }
177 if (!TEST_true(OSSL_PARAM_modified(param)))
178 return 0;
179 }
180 return 1;
181 }
182
183 /*
184 * The test cases for each of the bastic integral types are similar.
185 * For each type, a param of that type is set and an attempt to read it
186 * get is made. Finally, the above function is called to verify that
187 * the params can be read as other types.
188 *
189 * All the real work is done via byte buffers which are converted to machine
190 * byte order and to little endian for comparisons. Narrower values are best
191 * compared using little endian because their values and positions don't
192 * change.
193 */
194
195 static int test_param_int(int n)
196 {
197 int in, out;
198 unsigned char buf[MAX_LEN], cmp[sizeof(int)];
199 const size_t len = raw_values[n].len >= sizeof(int) ?
200 sizeof(int) : raw_values[n].len;
201 OSSL_PARAM param = OSSL_PARAM_int("a", NULL);
202
203 if (!TEST_int_eq(test_param_type_null(&param), 1))
204 return 0;
205
206 memset(buf, 0, sizeof(buf));
207 le_copy(buf, sizeof(in), raw_values[n].value, sizeof(in));
208 memcpy(&in, buf, sizeof(in));
209 param.data = &out;
210 if (!TEST_true(OSSL_PARAM_set_int(&param, in)))
211 return 0;
212 le_copy(cmp, sizeof(out), &out, sizeof(out));
213 if (!TEST_mem_eq(cmp, len, raw_values[n].value, len))
214 return 0;
215 in = 0;
216 if (!TEST_true(OSSL_PARAM_get_int(&param, &in)))
217 return 0;
218 le_copy(cmp, sizeof(in), &in, sizeof(in));
219 if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in)))
220 return 0;
221 param.data = &out;
222 return test_param_type_extra(&param, raw_values[n].value, sizeof(int));
223 }
224
225 static int test_param_long(int n)
226 {
227 long int in, out;
228 unsigned char buf[MAX_LEN], cmp[sizeof(long int)];
229 const size_t len = raw_values[n].len >= sizeof(long int)
230 ? sizeof(long int) : raw_values[n].len;
231 OSSL_PARAM param = OSSL_PARAM_long("a", NULL);
232
233 if (!TEST_int_eq(test_param_type_null(&param), 1))
234 return 0;
235
236 memset(buf, 0, sizeof(buf));
237 le_copy(buf, sizeof(in), raw_values[n].value, sizeof(in));
238 memcpy(&in, buf, sizeof(in));
239 param.data = &out;
240 if (!TEST_true(OSSL_PARAM_set_long(&param, in)))
241 return 0;
242 le_copy(cmp, sizeof(out), &out, sizeof(out));
243 if (!TEST_mem_eq(cmp, len, raw_values[n].value, len))
244 return 0;
245 in = 0;
246 if (!TEST_true(OSSL_PARAM_get_long(&param, &in)))
247 return 0;
248 le_copy(cmp, sizeof(in), &in, sizeof(in));
249 if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in)))
250 return 0;
251 param.data = &out;
252 return test_param_type_extra(&param, raw_values[n].value, sizeof(long int));
253 }
254
255 static int test_param_uint(int n)
256 {
257 unsigned int in, out;
258 unsigned char buf[MAX_LEN], cmp[sizeof(unsigned int)];
259 const size_t len = raw_values[n].len >= sizeof(unsigned int) ? sizeof(unsigned int) : raw_values[n].len;
260 OSSL_PARAM param = OSSL_PARAM_uint("a", NULL);
261
262 if (!TEST_int_eq(test_param_type_null(&param), 1))
263 return 0;
264
265 memset(buf, 0, sizeof(buf));
266 le_copy(buf, sizeof(in), raw_values[n].value, sizeof(in));
267 memcpy(&in, buf, sizeof(in));
268 param.data = &out;
269 if (!TEST_true(OSSL_PARAM_set_uint(&param, in)))
270 return 0;
271 le_copy(cmp, sizeof(out), &out, sizeof(out));
272 if (!TEST_mem_eq(cmp, len, raw_values[n].value, len))
273 return 0;
274 in = 0;
275 if (!TEST_true(OSSL_PARAM_get_uint(&param, &in)))
276 return 0;
277 le_copy(cmp, sizeof(in), &in, sizeof(in));
278 if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in)))
279 return 0;
280 param.data = &out;
281 return test_param_type_extra(&param, raw_values[n].value, sizeof(unsigned int));
282 }
283
284 static int test_param_ulong(int n)
285 {
286 unsigned long int in, out;
287 unsigned char buf[MAX_LEN], cmp[sizeof(unsigned long int)];
288 const size_t len = raw_values[n].len >= sizeof(unsigned long int)
289 ? sizeof(unsigned long int) : raw_values[n].len;
290 OSSL_PARAM param = OSSL_PARAM_ulong("a", NULL);
291
292 if (!TEST_int_eq(test_param_type_null(&param), 1))
293 return 0;
294
295 memset(buf, 0, sizeof(buf));
296 le_copy(buf, sizeof(in), raw_values[n].value, sizeof(in));
297 memcpy(&in, buf, sizeof(in));
298 param.data = &out;
299 if (!TEST_true(OSSL_PARAM_set_ulong(&param, in)))
300 return 0;
301 le_copy(cmp, sizeof(out), &out, sizeof(out));
302 if (!TEST_mem_eq(cmp, len, raw_values[n].value, len))
303 return 0;
304 in = 0;
305 if (!TEST_true(OSSL_PARAM_get_ulong(&param, &in)))
306 return 0;
307 le_copy(cmp, sizeof(in), &in, sizeof(in));
308 if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in)))
309 return 0;
310 param.data = &out;
311 return test_param_type_extra(&param, raw_values[n].value, sizeof(unsigned long int));
312 }
313
314 static int test_param_int32(int n)
315 {
316 int32_t in, out;
317 unsigned char buf[MAX_LEN], cmp[sizeof(int32_t)];
318 const size_t len = raw_values[n].len >= sizeof(int32_t)
319 ? sizeof(int32_t) : raw_values[n].len;
320 OSSL_PARAM param = OSSL_PARAM_int32("a", NULL);
321
322 if (!TEST_int_eq(test_param_type_null(&param), 1))
323 return 0;
324
325 memset(buf, 0, sizeof(buf));
326 le_copy(buf, sizeof(in), raw_values[n].value, sizeof(in));
327 memcpy(&in, buf, sizeof(in));
328 param.data = &out;
329 if (!TEST_true(OSSL_PARAM_set_int32(&param, in)))
330 return 0;
331 le_copy(cmp, sizeof(out), &out, sizeof(out));
332 if (!TEST_mem_eq(cmp, len, raw_values[n].value, len))
333 return 0;
334 in = 0;
335 if (!TEST_true(OSSL_PARAM_get_int32(&param, &in)))
336 return 0;
337 le_copy(cmp, sizeof(in), &in, sizeof(in));
338 if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in)))
339 return 0;
340 param.data = &out;
341 return test_param_type_extra(&param, raw_values[n].value, sizeof(int32_t));
342 }
343
344 static int test_param_uint32(int n)
345 {
346 uint32_t in, out;
347 unsigned char buf[MAX_LEN], cmp[sizeof(uint32_t)];
348 const size_t len = raw_values[n].len >= sizeof(uint32_t)
349 ? sizeof(uint32_t) : raw_values[n].len;
350 OSSL_PARAM param = OSSL_PARAM_uint32("a", NULL);
351
352 if (!TEST_int_eq(test_param_type_null(&param), 1))
353 return 0;
354
355 memset(buf, 0, sizeof(buf));
356 le_copy(buf, sizeof(in), raw_values[n].value, sizeof(in));
357 memcpy(&in, buf, sizeof(in));
358 param.data = &out;
359 if (!TEST_true(OSSL_PARAM_set_uint32(&param, in)))
360 return 0;
361 le_copy(cmp, sizeof(out), &out, sizeof(out));
362 if (!TEST_mem_eq(cmp, len, raw_values[n].value, len))
363 return 0;
364 in = 0;
365 if (!TEST_true(OSSL_PARAM_get_uint32(&param, &in)))
366 return 0;
367 le_copy(cmp, sizeof(in), &in, sizeof(in));
368 if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in)))
369 return 0;
370 param.data = &out;
371 return test_param_type_extra(&param, raw_values[n].value, sizeof(uint32_t));
372 }
373
374 static int test_param_int64(int n)
375 {
376 int64_t in, out;
377 unsigned char buf[MAX_LEN], cmp[sizeof(int64_t)];
378 const size_t len = raw_values[n].len >= sizeof(int64_t)
379 ? sizeof(int64_t) : raw_values[n].len;
380 OSSL_PARAM param = OSSL_PARAM_int64("a", NULL);
381
382 if (!TEST_int_eq(test_param_type_null(&param), 1))
383 return 0;
384
385 memset(buf, 0, sizeof(buf));
386 le_copy(buf, sizeof(in), raw_values[n].value, sizeof(in));
387 memcpy(&in, buf, sizeof(in));
388 param.data = &out;
389 if (!TEST_true(OSSL_PARAM_set_int64(&param, in)))
390 return 0;
391 le_copy(cmp, sizeof(out), &out, sizeof(out));
392 if (!TEST_mem_eq(cmp, len, raw_values[n].value, len))
393 return 0;
394 in = 0;
395 if (!TEST_true(OSSL_PARAM_get_int64(&param, &in)))
396 return 0;
397 le_copy(cmp, sizeof(in), &in, sizeof(in));
398 if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in)))
399 return 0;
400 param.data = &out;
401 return test_param_type_extra(&param, raw_values[n].value, sizeof(int64_t));
402 }
403
404 static int test_param_uint64(int n)
405 {
406 uint64_t in, out;
407 unsigned char buf[MAX_LEN], cmp[sizeof(uint64_t)];
408 const size_t len = raw_values[n].len >= sizeof(uint64_t)
409 ? sizeof(uint64_t) : raw_values[n].len;
410 OSSL_PARAM param = OSSL_PARAM_uint64("a", NULL);
411
412 if (!TEST_int_eq(test_param_type_null(&param), 1))
413 return 0;
414
415 memset(buf, 0, sizeof(buf));
416 le_copy(buf, sizeof(in), raw_values[n].value, sizeof(in));
417 memcpy(&in, buf, sizeof(in));
418 param.data = &out;
419 if (!TEST_true(OSSL_PARAM_set_uint64(&param, in)))
420 return 0;
421 le_copy(cmp, sizeof(out), &out, sizeof(out));
422 if (!TEST_mem_eq(cmp, len, raw_values[n].value, len))
423 return 0;
424 in = 0;
425 if (!TEST_true(OSSL_PARAM_get_uint64(&param, &in)))
426 return 0;
427 le_copy(cmp, sizeof(in), &in, sizeof(in));
428 if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in)))
429 return 0;
430 param.data = &out;
431 return test_param_type_extra(&param, raw_values[n].value, sizeof(uint64_t));
432 }
433
434 static int test_param_size_t(int n)
435 {
436 size_t in, out;
437 unsigned char buf[MAX_LEN], cmp[sizeof(size_t)];
438 const size_t len = raw_values[n].len >= sizeof(size_t)
439 ? sizeof(size_t) : raw_values[n].len;
440 OSSL_PARAM param = OSSL_PARAM_size_t("a", NULL);
441
442 if (!TEST_int_eq(test_param_type_null(&param), 1))
443 return 0;
444
445 memset(buf, 0, sizeof(buf));
446 le_copy(buf, sizeof(in), raw_values[n].value, sizeof(in));
447 memcpy(&in, buf, sizeof(in));
448 param.data = &out;
449 if (!TEST_true(OSSL_PARAM_set_size_t(&param, in)))
450 return 0;
451 le_copy(cmp, sizeof(out), &out, sizeof(out));
452 if (!TEST_mem_eq(cmp, len, raw_values[n].value, len))
453 return 0;
454 in = 0;
455 if (!TEST_true(OSSL_PARAM_get_size_t(&param, &in)))
456 return 0;
457 le_copy(cmp, sizeof(in), &in, sizeof(in));
458 if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in)))
459 return 0;
460 param.data = &out;
461 return test_param_type_extra(&param, raw_values[n].value, sizeof(size_t));
462 }
463
464 static int test_param_time_t(int n)
465 {
466 time_t in, out;
467 unsigned char buf[MAX_LEN], cmp[sizeof(time_t)];
468 const size_t len = raw_values[n].len >= sizeof(time_t)
469 ? sizeof(time_t) : raw_values[n].len;
470 OSSL_PARAM param = OSSL_PARAM_time_t("a", NULL);
471
472 if (!TEST_int_eq(test_param_type_null(&param), 1))
473 return 0;
474
475 memset(buf, 0, sizeof(buf));
476 le_copy(buf, sizeof(in), raw_values[n].value, sizeof(in));
477 memcpy(&in, buf, sizeof(in));
478 param.data = &out;
479 if (!TEST_true(OSSL_PARAM_set_time_t(&param, in)))
480 return 0;
481 le_copy(cmp, sizeof(out), &out, sizeof(out));
482 if (!TEST_mem_eq(cmp, len, raw_values[n].value, len))
483 return 0;
484 in = 0;
485 if (!TEST_true(OSSL_PARAM_get_time_t(&param, &in)))
486 return 0;
487 le_copy(cmp, sizeof(in), &in, sizeof(in));
488 if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in)))
489 return 0;
490 param.data = &out;
491 return test_param_type_extra(&param, raw_values[n].value, sizeof(size_t));
492 }
493
494 static int test_param_bignum(int n)
495 {
496 unsigned char buf[MAX_LEN], bnbuf[MAX_LEN];
497 const size_t len = raw_values[n].len;
498 BIGNUM *b = NULL, *c = NULL;
499 OSSL_PARAM param = OSSL_PARAM_DEFN("bn", OSSL_PARAM_UNSIGNED_INTEGER,
500 NULL, 0);
501 int ret = 0;
502
503 if (!TEST_int_eq(test_param_type_null(&param), 1))
504 return 0;
505
506 param.data = bnbuf;
507 param.data_size = sizeof(bnbuf);
508
509 if (!TEST_ptr(b = BN_lebin2bn(raw_values[n].value, (int)len, NULL)))
510 goto err;
511
512 if (!TEST_true(OSSL_PARAM_set_BN(&param, b)))
513 goto err;
514 le_copy(buf, len, bnbuf, sizeof(bnbuf));
515 if (!TEST_mem_eq(raw_values[n].value, len, buf, len))
516 goto err;
517 param.data_size = param.return_size;
518 if (!TEST_true(OSSL_PARAM_get_BN(&param, &c))
519 || !TEST_BN_eq(b, c))
520 goto err;
521
522 ret = 1;
523 err:
524 BN_free(b);
525 BN_free(c);
526 return ret;
527 }
528
529 static int test_param_signed_bignum(int n)
530 {
531 unsigned char buf[MAX_LEN], bnbuf[MAX_LEN];
532 const size_t len = raw_values[n].len;
533 BIGNUM *b = NULL, *c = NULL;
534 OSSL_PARAM param = OSSL_PARAM_DEFN("bn", OSSL_PARAM_INTEGER, NULL, 0);
535 int ret = 0;
536
537 if (!TEST_int_eq(test_param_type_null(&param), 1))
538 return 0;
539
540 param.data = bnbuf;
541 param.data_size = sizeof(bnbuf);
542
543 if (!TEST_ptr(b = BN_signed_lebin2bn(raw_values[n].value, (int)len, NULL)))
544 goto err;
545
546 /* raw_values are little endian */
547 if (!TEST_false(!!(raw_values[n].value[len - 1] & 0x80) ^ BN_is_negative(b)))
548 goto err;
549 if (!TEST_true(OSSL_PARAM_set_BN(&param, b)))
550 goto err;
551 le_copy(buf, len, bnbuf, sizeof(bnbuf));
552 if (!TEST_mem_eq(raw_values[n].value, len, buf, len))
553 goto err;
554 param.data_size = param.return_size;
555 if (!TEST_true(OSSL_PARAM_get_BN(&param, &c))
556 || !TEST_BN_eq(b, c)) {
557 BN_print_fp(stderr, c);
558 goto err;
559 }
560
561 ret = 1;
562 err:
563 BN_free(b);
564 BN_free(c);
565 return ret;
566 }
567
568 static int test_param_real(void)
569 {
570 double p;
571 OSSL_PARAM param = OSSL_PARAM_double("r", NULL);
572
573 if (!TEST_int_eq(test_param_type_null(&param), 1))
574 return 0;
575
576 param.data = &p;
577 return TEST_true(OSSL_PARAM_set_double(&param, 3.14159))
578 && TEST_double_eq(p, 3.14159);
579 }
580
581 static int test_param_construct(int tstid)
582 {
583 static const char *int_names[] = {
584 "int", "long", "int32", "int64"
585 };
586 static const char *uint_names[] = {
587 "uint", "ulong", "uint32", "uint64", "size_t"
588 };
589 static const unsigned char bn_val[16] = {
590 0xac, 0x75, 0x22, 0x7d, 0x81, 0x06, 0x7a, 0x23,
591 0xa6, 0xed, 0x87, 0xc7, 0xab, 0xf4, 0x73, 0x22
592 };
593 OSSL_PARAM *p = NULL, *p1 = NULL;
594 static const OSSL_PARAM params_empty[] = {
595 OSSL_PARAM_END
596 };
597 OSSL_PARAM params[20];
598 char buf[100], buf2[100], *bufp, *bufp2;
599 unsigned char ubuf[100];
600 void *vp, *vpn = NULL, *vp2;
601 OSSL_PARAM *cp;
602 int i, n = 0, ret = 0;
603 unsigned int u;
604 long int l;
605 unsigned long int ul;
606 int32_t i32;
607 uint32_t u32;
608 int64_t i64;
609 uint64_t u64;
610 size_t j, k, s;
611 double d, d2;
612 BIGNUM *bn = NULL, *bn2 = NULL;
613
614 params[n++] = OSSL_PARAM_construct_int("int", &i);
615 params[n++] = OSSL_PARAM_construct_uint("uint", &u);
616 params[n++] = OSSL_PARAM_construct_long("long", &l);
617 params[n++] = OSSL_PARAM_construct_ulong("ulong", &ul);
618 params[n++] = OSSL_PARAM_construct_int32("int32", &i32);
619 params[n++] = OSSL_PARAM_construct_int64("int64", &i64);
620 params[n++] = OSSL_PARAM_construct_uint32("uint32", &u32);
621 params[n++] = OSSL_PARAM_construct_uint64("uint64", &u64);
622 params[n++] = OSSL_PARAM_construct_size_t("size_t", &s);
623 params[n++] = OSSL_PARAM_construct_double("double", &d);
624 params[n++] = OSSL_PARAM_construct_BN("bignum", ubuf, sizeof(ubuf));
625 params[n++] = OSSL_PARAM_construct_utf8_string("utf8str", buf, sizeof(buf));
626 params[n++] = OSSL_PARAM_construct_octet_string("octstr", buf, sizeof(buf));
627 params[n++] = OSSL_PARAM_construct_utf8_ptr("utf8ptr", &bufp, 0);
628 params[n++] = OSSL_PARAM_construct_octet_ptr("octptr", &vp, 0);
629 params[n] = OSSL_PARAM_construct_end();
630
631 switch (tstid) {
632 case 0:
633 p = params;
634 break;
635 case 1:
636 p = OSSL_PARAM_merge(params, params_empty);
637 break;
638 case 2:
639 p = OSSL_PARAM_dup(params);
640 break;
641 default:
642 p1 = OSSL_PARAM_dup(params);
643 p = OSSL_PARAM_merge(p1, params_empty);
644 break;
645 }
646
647 /* Search failure */
648 if (!TEST_ptr_null(OSSL_PARAM_locate(p, "fnord")))
649 goto err;
650
651 /* All signed integral types */
652 for (j = 0; j < OSSL_NELEM(int_names); j++) {
653 if (!TEST_ptr(cp = OSSL_PARAM_locate(p, int_names[j]))
654 || !TEST_true(OSSL_PARAM_set_int32(cp, (int32_t)(3 + j)))
655 || !TEST_true(OSSL_PARAM_get_int64(cp, &i64))
656 || !TEST_size_t_eq(cp->data_size, cp->return_size)
657 || !TEST_size_t_eq((size_t)i64, 3 + j)) {
658 TEST_note("iteration %zu var %s", j + 1, int_names[j]);
659 goto err;
660 }
661 }
662 /* All unsigned integral types */
663 for (j = 0; j < OSSL_NELEM(uint_names); j++) {
664 if (!TEST_ptr(cp = OSSL_PARAM_locate(p, uint_names[j]))
665 || !TEST_true(OSSL_PARAM_set_uint32(cp, (uint32_t)(3 + j)))
666 || !TEST_true(OSSL_PARAM_get_uint64(cp, &u64))
667 || !TEST_size_t_eq(cp->data_size, cp->return_size)
668 || !TEST_size_t_eq((size_t)u64, 3 + j)) {
669 TEST_note("iteration %zu var %s", j + 1, uint_names[j]);
670 goto err;
671 }
672 }
673 /* Real */
674 if (!TEST_ptr(cp = OSSL_PARAM_locate(p, "double"))
675 || !TEST_true(OSSL_PARAM_set_double(cp, 3.14))
676 || !TEST_true(OSSL_PARAM_get_double(cp, &d2))
677 || !TEST_size_t_eq(cp->return_size, sizeof(double))
678 || !TEST_double_eq(d2, 3.14)
679 || (tstid <= 1 && !TEST_double_eq(d, d2)))
680 goto err;
681 /* UTF8 string */
682 bufp = NULL;
683 if (!TEST_ptr(cp = OSSL_PARAM_locate(p, "utf8str"))
684 || !TEST_true(OSSL_PARAM_set_utf8_string(cp, "abcdef"))
685 || !TEST_size_t_eq(cp->return_size, sizeof("abcdef") - 1)
686 || !TEST_true(OSSL_PARAM_get_utf8_string(cp, &bufp, 0))
687 || !TEST_str_eq(bufp, "abcdef")) {
688 OPENSSL_free(bufp);
689 goto err;
690 }
691 OPENSSL_free(bufp);
692 bufp = buf2;
693 if (!TEST_true(OSSL_PARAM_get_utf8_string(cp, &bufp, sizeof(buf2)))
694 || !TEST_str_eq(buf2, "abcdef"))
695 goto err;
696 /* UTF8 pointer */
697 /* Note that the size of a UTF8 string does *NOT* include the NUL byte */
698 bufp = buf;
699 if (!TEST_ptr(cp = OSSL_PARAM_locate(p, "utf8ptr"))
700 || !TEST_true(OSSL_PARAM_set_utf8_ptr(cp, "tuvwxyz"))
701 || !TEST_size_t_eq(cp->return_size, sizeof("tuvwxyz") - 1)
702 || !TEST_true(OSSL_PARAM_get_utf8_ptr(cp, (const char **)&bufp2))
703 || !TEST_str_eq(bufp2, "tuvwxyz")
704 || (tstid <= 1 && !TEST_ptr_eq(bufp2, bufp)))
705 goto err;
706 /* OCTET string */
707 if (!TEST_ptr(cp = OSSL_PARAM_locate(p, "octstr"))
708 || !TEST_true(OSSL_PARAM_set_octet_string(cp, "abcdefghi",
709 sizeof("abcdefghi")))
710 || !TEST_size_t_eq(cp->return_size, sizeof("abcdefghi")))
711 goto err;
712 /* Match the return size to avoid trailing garbage bytes */
713 cp->data_size = cp->return_size;
714 if (!TEST_true(OSSL_PARAM_get_octet_string(cp, &vpn, 0, &s))
715 || !TEST_size_t_eq(s, sizeof("abcdefghi"))
716 || !TEST_mem_eq(vpn, sizeof("abcdefghi"),
717 "abcdefghi", sizeof("abcdefghi")))
718 goto err;
719 vp = buf2;
720 if (!TEST_true(OSSL_PARAM_get_octet_string(cp, &vp, sizeof(buf2), &s))
721 || !TEST_size_t_eq(s, sizeof("abcdefghi"))
722 || !TEST_mem_eq(vp, sizeof("abcdefghi"),
723 "abcdefghi", sizeof("abcdefghi")))
724 goto err;
725 /* OCTET pointer */
726 vp = &l;
727 if (!TEST_ptr(cp = OSSL_PARAM_locate(p, "octptr"))
728 || !TEST_true(OSSL_PARAM_set_octet_ptr(cp, &ul, sizeof(ul)))
729 || !TEST_size_t_eq(cp->return_size, sizeof(ul))
730 || (tstid <= 1 && !TEST_ptr_eq(vp, &ul)))
731 goto err;
732 /* Match the return size to avoid trailing garbage bytes */
733 cp->data_size = cp->return_size;
734 if (!TEST_true(OSSL_PARAM_get_octet_ptr(cp, (const void **)&vp2, &k))
735 || !TEST_size_t_eq(k, sizeof(ul))
736 || (tstid <= 1 && !TEST_ptr_eq(vp2, vp)))
737 goto err;
738 /* BIGNUM */
739 if (!TEST_ptr(cp = OSSL_PARAM_locate(p, "bignum"))
740 || !TEST_ptr(bn = BN_lebin2bn(bn_val, (int)sizeof(bn_val), NULL))
741 || !TEST_true(OSSL_PARAM_set_BN(cp, bn))
742 || !TEST_size_t_eq(cp->data_size, cp->return_size))
743 goto err;
744 /* Match the return size to avoid trailing garbage bytes */
745 cp->data_size = cp->return_size;
746 if (!TEST_true(OSSL_PARAM_get_BN(cp, &bn2))
747 || !TEST_BN_eq(bn, bn2))
748 goto err;
749 ret = 1;
750 err:
751 if (p != params)
752 OPENSSL_free(p);
753 OPENSSL_free(p1);
754 OPENSSL_free(vpn);
755 BN_free(bn);
756 BN_free(bn2);
757 return ret;
758 }
759
760 static int test_param_modified(void)
761 {
762 OSSL_PARAM param[3] = { OSSL_PARAM_int("a", NULL),
763 OSSL_PARAM_int("b", NULL),
764 OSSL_PARAM_END };
765 int a, b;
766
767 param->data = &a;
768 param[1].data = &b;
769 if (!TEST_false(OSSL_PARAM_modified(param))
770 && !TEST_true(OSSL_PARAM_set_int32(param, 1234))
771 && !TEST_true(OSSL_PARAM_modified(param))
772 && !TEST_false(OSSL_PARAM_modified(param + 1))
773 && !TEST_true(OSSL_PARAM_set_int32(param + 1, 1))
774 && !TEST_true(OSSL_PARAM_modified(param + 1)))
775 return 0;
776 OSSL_PARAM_set_all_unmodified(param);
777 if (!TEST_false(OSSL_PARAM_modified(param))
778 && !TEST_true(OSSL_PARAM_set_int32(param, 4321))
779 && !TEST_true(OSSL_PARAM_modified(param))
780 && !TEST_false(OSSL_PARAM_modified(param + 1))
781 && !TEST_true(OSSL_PARAM_set_int32(param + 1, 2))
782 && !TEST_true(OSSL_PARAM_modified(param + 1)))
783 return 0;
784 return 1;
785 }
786
787 static int test_param_copy_null(void)
788 {
789 int ret, val;
790 int a = 1, b = 2, i = 0;
791 OSSL_PARAM *cp1 = NULL, *cp2 = NULL, *p;
792 OSSL_PARAM param[3];
793
794 param[i++] = OSSL_PARAM_construct_int("a", &a);
795 param[i++] = OSSL_PARAM_construct_int("b", &b);
796 param[i] = OSSL_PARAM_construct_end();
797
798 ret = TEST_ptr_null(OSSL_PARAM_dup(NULL))
799 && TEST_ptr(cp1 = OSSL_PARAM_merge(NULL, param))
800 && TEST_ptr(p = OSSL_PARAM_locate(cp1, "a"))
801 && TEST_true(OSSL_PARAM_get_int(p, &val))
802 && TEST_int_eq(val, 1)
803 && TEST_ptr(p = OSSL_PARAM_locate(cp1, "b"))
804 && TEST_true(OSSL_PARAM_get_int(p, &val))
805 && TEST_int_eq(val, 2)
806 && TEST_ptr(cp2 = OSSL_PARAM_merge(param, NULL))
807 && TEST_ptr(p = OSSL_PARAM_locate(cp2, "a"))
808 && TEST_true(OSSL_PARAM_get_int(p, &val))
809 && TEST_int_eq(val, 1)
810 && TEST_ptr(p = OSSL_PARAM_locate(cp2, "b"))
811 && TEST_true(OSSL_PARAM_get_int(p, &val))
812 && TEST_int_eq(val, 2)
813 && TEST_ptr_null(OSSL_PARAM_merge(NULL, NULL));
814 OSSL_PARAM_free(cp2);
815 OSSL_PARAM_free(cp1);
816 return ret;
817 }
818
819 int setup_tests(void)
820 {
821 ADD_ALL_TESTS(test_param_int, OSSL_NELEM(raw_values));
822 ADD_ALL_TESTS(test_param_long, OSSL_NELEM(raw_values));
823 ADD_ALL_TESTS(test_param_uint, OSSL_NELEM(raw_values));
824 ADD_ALL_TESTS(test_param_ulong, OSSL_NELEM(raw_values));
825 ADD_ALL_TESTS(test_param_int32, OSSL_NELEM(raw_values));
826 ADD_ALL_TESTS(test_param_uint32, OSSL_NELEM(raw_values));
827 ADD_ALL_TESTS(test_param_size_t, OSSL_NELEM(raw_values));
828 ADD_ALL_TESTS(test_param_time_t, OSSL_NELEM(raw_values));
829 ADD_ALL_TESTS(test_param_int64, OSSL_NELEM(raw_values));
830 ADD_ALL_TESTS(test_param_uint64, OSSL_NELEM(raw_values));
831 ADD_ALL_TESTS(test_param_bignum, OSSL_NELEM(raw_values));
832 ADD_ALL_TESTS(test_param_signed_bignum, OSSL_NELEM(raw_values));
833 ADD_TEST(test_param_real);
834 ADD_ALL_TESTS(test_param_construct, 4);
835 ADD_TEST(test_param_modified);
836 ADD_TEST(test_param_copy_null);
837 return 1;
838 }