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