1 | /*
|
---|
2 | * Copyright 2019-2022 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, 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 |
|
---|
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 sentinel 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 |
|
---|
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(¶m, 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(¶m, &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(¶m, raw_values[n].value, sizeof(int));
|
---|
173 | }
|
---|
174 |
|
---|
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(¶m, 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(¶m, &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(¶m, raw_values[n].value, sizeof(long int));
|
---|
200 | }
|
---|
201 |
|
---|
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(¶m, 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(¶m, &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(¶m, raw_values[n].value, sizeof(unsigned int));
|
---|
226 | }
|
---|
227 |
|
---|
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(¶m, 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(¶m, &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(¶m, raw_values[n].value, sizeof(unsigned long int));
|
---|
253 | }
|
---|
254 |
|
---|
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(¶m, 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(¶m, &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(¶m, raw_values[n].value, sizeof(int32_t));
|
---|
280 | }
|
---|
281 |
|
---|
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(¶m, 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(¶m, &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(¶m, raw_values[n].value, sizeof(uint32_t));
|
---|
307 | }
|
---|
308 |
|
---|
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(¶m, 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(¶m, &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(¶m, raw_values[n].value, sizeof(int64_t));
|
---|
334 | }
|
---|
335 |
|
---|
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(¶m, 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(¶m, &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(¶m, raw_values[n].value, sizeof(uint64_t));
|
---|
361 | }
|
---|
362 |
|
---|
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(¶m, 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(¶m, &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(¶m, raw_values[n].value, sizeof(size_t));
|
---|
388 | }
|
---|
389 |
|
---|
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(¶m, 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(¶m, &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(¶m, raw_values[n].value, sizeof(size_t));
|
---|
415 | }
|
---|
416 |
|
---|
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 = sizeof(bnbuf);
|
---|
428 |
|
---|
429 | if (!TEST_ptr(b = BN_lebin2bn(raw_values[n].value, (int)len, NULL)))
|
---|
430 | goto err;
|
---|
431 |
|
---|
432 | if (!TEST_true(OSSL_PARAM_set_BN(¶m, b)))
|
---|
433 | goto err;
|
---|
434 | le_copy(buf, bnbuf, sizeof(bnbuf));
|
---|
435 | if (!TEST_mem_eq(raw_values[n].value, len, buf, len))
|
---|
436 | goto err;
|
---|
437 | param.data_size = param.return_size;
|
---|
438 | if (!TEST_true(OSSL_PARAM_get_BN(¶m, &c))
|
---|
439 | || !TEST_BN_eq(b, c))
|
---|
440 | goto err;
|
---|
441 |
|
---|
442 | ret = 1;
|
---|
443 | err:
|
---|
444 | BN_free(b);
|
---|
445 | BN_free(c);
|
---|
446 | return ret;
|
---|
447 | }
|
---|
448 |
|
---|
449 | static int test_param_real(void)
|
---|
450 | {
|
---|
451 | double p;
|
---|
452 | OSSL_PARAM param = OSSL_PARAM_double("r", NULL);
|
---|
453 |
|
---|
454 | param.data = &p;
|
---|
455 | return TEST_true(OSSL_PARAM_set_double(¶m, 3.14159))
|
---|
456 | && TEST_double_eq(p, 3.14159);
|
---|
457 | }
|
---|
458 |
|
---|
459 | static int test_param_construct(int tstid)
|
---|
460 | {
|
---|
461 | static const char *int_names[] = {
|
---|
462 | "int", "long", "int32", "int64"
|
---|
463 | };
|
---|
464 | static const char *uint_names[] = {
|
---|
465 | "uint", "ulong", "uint32", "uint64", "size_t"
|
---|
466 | };
|
---|
467 | static const unsigned char bn_val[16] = {
|
---|
468 | 0xac, 0x75, 0x22, 0x7d, 0x81, 0x06, 0x7a, 0x23,
|
---|
469 | 0xa6, 0xed, 0x87, 0xc7, 0xab, 0xf4, 0x73, 0x22
|
---|
470 | };
|
---|
471 | OSSL_PARAM *p = NULL, *p1 = NULL;
|
---|
472 | static const OSSL_PARAM params_empty[] = {
|
---|
473 | OSSL_PARAM_END
|
---|
474 | };
|
---|
475 | OSSL_PARAM params[20];
|
---|
476 | char buf[100], buf2[100], *bufp, *bufp2;
|
---|
477 | unsigned char ubuf[100];
|
---|
478 | void *vp, *vpn = NULL, *vp2;
|
---|
479 | OSSL_PARAM *cp;
|
---|
480 | int i, n = 0, ret = 0;
|
---|
481 | unsigned int u;
|
---|
482 | long int l;
|
---|
483 | unsigned long int ul;
|
---|
484 | int32_t i32;
|
---|
485 | uint32_t u32;
|
---|
486 | int64_t i64;
|
---|
487 | uint64_t u64;
|
---|
488 | size_t j, k, s;
|
---|
489 | double d, d2;
|
---|
490 | BIGNUM *bn = NULL, *bn2 = NULL;
|
---|
491 |
|
---|
492 | params[n++] = OSSL_PARAM_construct_int("int", &i);
|
---|
493 | params[n++] = OSSL_PARAM_construct_uint("uint", &u);
|
---|
494 | params[n++] = OSSL_PARAM_construct_long("long", &l);
|
---|
495 | params[n++] = OSSL_PARAM_construct_ulong("ulong", &ul);
|
---|
496 | params[n++] = OSSL_PARAM_construct_int32("int32", &i32);
|
---|
497 | params[n++] = OSSL_PARAM_construct_int64("int64", &i64);
|
---|
498 | params[n++] = OSSL_PARAM_construct_uint32("uint32", &u32);
|
---|
499 | params[n++] = OSSL_PARAM_construct_uint64("uint64", &u64);
|
---|
500 | params[n++] = OSSL_PARAM_construct_size_t("size_t", &s);
|
---|
501 | params[n++] = OSSL_PARAM_construct_double("double", &d);
|
---|
502 | params[n++] = OSSL_PARAM_construct_BN("bignum", ubuf, sizeof(ubuf));
|
---|
503 | params[n++] = OSSL_PARAM_construct_utf8_string("utf8str", buf, sizeof(buf));
|
---|
504 | params[n++] = OSSL_PARAM_construct_octet_string("octstr", buf, sizeof(buf));
|
---|
505 | params[n++] = OSSL_PARAM_construct_utf8_ptr("utf8ptr", &bufp, 0);
|
---|
506 | params[n++] = OSSL_PARAM_construct_octet_ptr("octptr", &vp, 0);
|
---|
507 | params[n] = OSSL_PARAM_construct_end();
|
---|
508 |
|
---|
509 | switch(tstid) {
|
---|
510 | case 0:
|
---|
511 | p = params;
|
---|
512 | break;
|
---|
513 | case 1:
|
---|
514 | p = OSSL_PARAM_merge(params, params_empty);
|
---|
515 | break;
|
---|
516 | case 2:
|
---|
517 | p = OSSL_PARAM_dup(params);
|
---|
518 | break;
|
---|
519 | default:
|
---|
520 | p1 = OSSL_PARAM_dup(params);
|
---|
521 | p = OSSL_PARAM_merge(p1, params_empty);
|
---|
522 | break;
|
---|
523 | }
|
---|
524 |
|
---|
525 | /* Search failure */
|
---|
526 | if (!TEST_ptr_null(OSSL_PARAM_locate(p, "fnord")))
|
---|
527 | goto err;
|
---|
528 |
|
---|
529 | /* All signed integral types */
|
---|
530 | for (j = 0; j < OSSL_NELEM(int_names); j++) {
|
---|
531 | if (!TEST_ptr(cp = OSSL_PARAM_locate(p, int_names[j]))
|
---|
532 | || !TEST_true(OSSL_PARAM_set_int32(cp, (int32_t)(3 + j)))
|
---|
533 | || !TEST_true(OSSL_PARAM_get_int64(cp, &i64))
|
---|
534 | || !TEST_size_t_eq(cp->data_size, cp->return_size)
|
---|
535 | || !TEST_size_t_eq((size_t)i64, 3 + j)) {
|
---|
536 | TEST_note("iteration %zu var %s", j + 1, int_names[j]);
|
---|
537 | goto err;
|
---|
538 | }
|
---|
539 | }
|
---|
540 | /* All unsigned integral types */
|
---|
541 | for (j = 0; j < OSSL_NELEM(uint_names); j++) {
|
---|
542 | if (!TEST_ptr(cp = OSSL_PARAM_locate(p, uint_names[j]))
|
---|
543 | || !TEST_true(OSSL_PARAM_set_uint32(cp, (uint32_t)(3 + j)))
|
---|
544 | || !TEST_true(OSSL_PARAM_get_uint64(cp, &u64))
|
---|
545 | || !TEST_size_t_eq(cp->data_size, cp->return_size)
|
---|
546 | || !TEST_size_t_eq((size_t)u64, 3 + j)) {
|
---|
547 | TEST_note("iteration %zu var %s", j + 1, uint_names[j]);
|
---|
548 | goto err;
|
---|
549 | }
|
---|
550 | }
|
---|
551 | /* Real */
|
---|
552 | if (!TEST_ptr(cp = OSSL_PARAM_locate(p, "double"))
|
---|
553 | || !TEST_true(OSSL_PARAM_set_double(cp, 3.14))
|
---|
554 | || !TEST_true(OSSL_PARAM_get_double(cp, &d2))
|
---|
555 | || !TEST_size_t_eq(cp->return_size, sizeof(double))
|
---|
556 | || !TEST_double_eq(d2, 3.14)
|
---|
557 | || (tstid <= 1 && !TEST_double_eq(d, d2)))
|
---|
558 | goto err;
|
---|
559 | /* UTF8 string */
|
---|
560 | bufp = NULL;
|
---|
561 | if (!TEST_ptr(cp = OSSL_PARAM_locate(p, "utf8str"))
|
---|
562 | || !TEST_true(OSSL_PARAM_set_utf8_string(cp, "abcdef"))
|
---|
563 | || !TEST_size_t_eq(cp->return_size, sizeof("abcdef") - 1)
|
---|
564 | || !TEST_true(OSSL_PARAM_get_utf8_string(cp, &bufp, 0))
|
---|
565 | || !TEST_str_eq(bufp, "abcdef")) {
|
---|
566 | OPENSSL_free(bufp);
|
---|
567 | goto err;
|
---|
568 | }
|
---|
569 | OPENSSL_free(bufp);
|
---|
570 | bufp = buf2;
|
---|
571 | if (!TEST_true(OSSL_PARAM_get_utf8_string(cp, &bufp, sizeof(buf2)))
|
---|
572 | || !TEST_str_eq(buf2, "abcdef"))
|
---|
573 | goto err;
|
---|
574 | /* UTF8 pointer */
|
---|
575 | /* Note that the size of a UTF8 string does *NOT* include the NUL byte */
|
---|
576 | bufp = buf;
|
---|
577 | if (!TEST_ptr(cp = OSSL_PARAM_locate(p, "utf8ptr"))
|
---|
578 | || !TEST_true(OSSL_PARAM_set_utf8_ptr(cp, "tuvwxyz"))
|
---|
579 | || !TEST_size_t_eq(cp->return_size, sizeof("tuvwxyz") - 1)
|
---|
580 | || !TEST_true(OSSL_PARAM_get_utf8_ptr(cp, (const char **)&bufp2))
|
---|
581 | || !TEST_str_eq(bufp2, "tuvwxyz")
|
---|
582 | || (tstid <= 1 && !TEST_ptr_eq(bufp2, bufp)))
|
---|
583 | goto err;
|
---|
584 | /* OCTET string */
|
---|
585 | if (!TEST_ptr(cp = OSSL_PARAM_locate(p, "octstr"))
|
---|
586 | || !TEST_true(OSSL_PARAM_set_octet_string(cp, "abcdefghi",
|
---|
587 | sizeof("abcdefghi")))
|
---|
588 | || !TEST_size_t_eq(cp->return_size, sizeof("abcdefghi")))
|
---|
589 | goto err;
|
---|
590 | /* Match the return size to avoid trailing garbage bytes */
|
---|
591 | cp->data_size = cp->return_size;
|
---|
592 | if (!TEST_true(OSSL_PARAM_get_octet_string(cp, &vpn, 0, &s))
|
---|
593 | || !TEST_size_t_eq(s, sizeof("abcdefghi"))
|
---|
594 | || !TEST_mem_eq(vpn, sizeof("abcdefghi"),
|
---|
595 | "abcdefghi", sizeof("abcdefghi")))
|
---|
596 | goto err;
|
---|
597 | vp = buf2;
|
---|
598 | if (!TEST_true(OSSL_PARAM_get_octet_string(cp, &vp, sizeof(buf2), &s))
|
---|
599 | || !TEST_size_t_eq(s, sizeof("abcdefghi"))
|
---|
600 | || !TEST_mem_eq(vp, sizeof("abcdefghi"),
|
---|
601 | "abcdefghi", sizeof("abcdefghi")))
|
---|
602 | goto err;
|
---|
603 | /* OCTET pointer */
|
---|
604 | vp = &l;
|
---|
605 | if (!TEST_ptr(cp = OSSL_PARAM_locate(p, "octptr"))
|
---|
606 | || !TEST_true(OSSL_PARAM_set_octet_ptr(cp, &ul, sizeof(ul)))
|
---|
607 | || !TEST_size_t_eq(cp->return_size, sizeof(ul))
|
---|
608 | || (tstid <= 1 && !TEST_ptr_eq(vp, &ul)))
|
---|
609 | goto err;
|
---|
610 | /* Match the return size to avoid trailing garbage bytes */
|
---|
611 | cp->data_size = cp->return_size;
|
---|
612 | if (!TEST_true(OSSL_PARAM_get_octet_ptr(cp, (const void **)&vp2, &k))
|
---|
613 | || !TEST_size_t_eq(k, sizeof(ul))
|
---|
614 | || (tstid <= 1 && !TEST_ptr_eq(vp2, vp)))
|
---|
615 | goto err;
|
---|
616 | /* BIGNUM */
|
---|
617 | if (!TEST_ptr(cp = OSSL_PARAM_locate(p, "bignum"))
|
---|
618 | || !TEST_ptr(bn = BN_lebin2bn(bn_val, (int)sizeof(bn_val), NULL))
|
---|
619 | || !TEST_true(OSSL_PARAM_set_BN(cp, bn))
|
---|
620 | || !TEST_size_t_eq(cp->data_size, cp->return_size))
|
---|
621 | goto err;
|
---|
622 | /* Match the return size to avoid trailing garbage bytes */
|
---|
623 | cp->data_size = cp->return_size;
|
---|
624 | if(!TEST_true(OSSL_PARAM_get_BN(cp, &bn2))
|
---|
625 | || !TEST_BN_eq(bn, bn2))
|
---|
626 | goto err;
|
---|
627 | ret = 1;
|
---|
628 | err:
|
---|
629 | if (p != params)
|
---|
630 | OPENSSL_free(p);
|
---|
631 | OPENSSL_free(p1);
|
---|
632 | OPENSSL_free(vpn);
|
---|
633 | BN_free(bn);
|
---|
634 | BN_free(bn2);
|
---|
635 | return ret;
|
---|
636 | }
|
---|
637 |
|
---|
638 | static int test_param_modified(void)
|
---|
639 | {
|
---|
640 | OSSL_PARAM param[3] = { OSSL_PARAM_int("a", NULL),
|
---|
641 | OSSL_PARAM_int("b", NULL),
|
---|
642 | OSSL_PARAM_END };
|
---|
643 | int a, b;
|
---|
644 |
|
---|
645 | param->data = &a;
|
---|
646 | param[1].data = &b;
|
---|
647 | if (!TEST_false(OSSL_PARAM_modified(param))
|
---|
648 | && !TEST_true(OSSL_PARAM_set_int32(param, 1234))
|
---|
649 | && !TEST_true(OSSL_PARAM_modified(param))
|
---|
650 | && !TEST_false(OSSL_PARAM_modified(param + 1))
|
---|
651 | && !TEST_true(OSSL_PARAM_set_int32(param + 1, 1))
|
---|
652 | && !TEST_true(OSSL_PARAM_modified(param + 1)))
|
---|
653 | return 0;
|
---|
654 | OSSL_PARAM_set_all_unmodified(param);
|
---|
655 | if (!TEST_false(OSSL_PARAM_modified(param))
|
---|
656 | && !TEST_true(OSSL_PARAM_set_int32(param, 4321))
|
---|
657 | && !TEST_true(OSSL_PARAM_modified(param))
|
---|
658 | && !TEST_false(OSSL_PARAM_modified(param + 1))
|
---|
659 | && !TEST_true(OSSL_PARAM_set_int32(param + 1, 2))
|
---|
660 | && !TEST_true(OSSL_PARAM_modified(param + 1)))
|
---|
661 | return 0;
|
---|
662 | return 1;
|
---|
663 | }
|
---|
664 |
|
---|
665 | static int test_param_copy_null(void)
|
---|
666 | {
|
---|
667 | int ret, val;
|
---|
668 | int a = 1, b = 2, i = 0;
|
---|
669 | OSSL_PARAM *cp1 = NULL, *cp2 = NULL, *p;
|
---|
670 | OSSL_PARAM param[3];
|
---|
671 |
|
---|
672 | param[i++] = OSSL_PARAM_construct_int("a", &a);
|
---|
673 | param[i++] = OSSL_PARAM_construct_int("b", &b);
|
---|
674 | param[i] = OSSL_PARAM_construct_end();
|
---|
675 |
|
---|
676 | ret = TEST_ptr_null(OSSL_PARAM_dup(NULL))
|
---|
677 | && TEST_ptr(cp1 = OSSL_PARAM_merge(NULL, param))
|
---|
678 | && TEST_ptr(p = OSSL_PARAM_locate(cp1, "a"))
|
---|
679 | && TEST_true(OSSL_PARAM_get_int(p, &val))
|
---|
680 | && TEST_int_eq(val, 1)
|
---|
681 | && TEST_ptr(p = OSSL_PARAM_locate(cp1, "b"))
|
---|
682 | && TEST_true(OSSL_PARAM_get_int(p, &val))
|
---|
683 | && TEST_int_eq(val, 2)
|
---|
684 | && TEST_ptr(cp2 = OSSL_PARAM_merge(param, NULL))
|
---|
685 | && TEST_ptr(p = OSSL_PARAM_locate(cp2, "a"))
|
---|
686 | && TEST_true(OSSL_PARAM_get_int(p, &val))
|
---|
687 | && TEST_int_eq(val, 1)
|
---|
688 | && TEST_ptr(p = OSSL_PARAM_locate(cp2, "b"))
|
---|
689 | && TEST_true(OSSL_PARAM_get_int(p, &val))
|
---|
690 | && TEST_int_eq(val, 2)
|
---|
691 | && TEST_ptr_null(OSSL_PARAM_merge(NULL, NULL));
|
---|
692 | OSSL_PARAM_free(cp2);
|
---|
693 | OSSL_PARAM_free(cp1);
|
---|
694 | return ret;
|
---|
695 | }
|
---|
696 |
|
---|
697 | int setup_tests(void)
|
---|
698 | {
|
---|
699 | ADD_ALL_TESTS(test_param_int, OSSL_NELEM(raw_values));
|
---|
700 | ADD_ALL_TESTS(test_param_long, OSSL_NELEM(raw_values));
|
---|
701 | ADD_ALL_TESTS(test_param_uint, OSSL_NELEM(raw_values));
|
---|
702 | ADD_ALL_TESTS(test_param_ulong, OSSL_NELEM(raw_values));
|
---|
703 | ADD_ALL_TESTS(test_param_int32, OSSL_NELEM(raw_values));
|
---|
704 | ADD_ALL_TESTS(test_param_uint32, OSSL_NELEM(raw_values));
|
---|
705 | ADD_ALL_TESTS(test_param_size_t, OSSL_NELEM(raw_values));
|
---|
706 | ADD_ALL_TESTS(test_param_time_t, OSSL_NELEM(raw_values));
|
---|
707 | ADD_ALL_TESTS(test_param_int64, OSSL_NELEM(raw_values));
|
---|
708 | ADD_ALL_TESTS(test_param_uint64, OSSL_NELEM(raw_values));
|
---|
709 | ADD_ALL_TESTS(test_param_bignum, OSSL_NELEM(raw_values));
|
---|
710 | ADD_TEST(test_param_real);
|
---|
711 | ADD_ALL_TESTS(test_param_construct, 4);
|
---|
712 | ADD_TEST(test_param_modified);
|
---|
713 | ADD_TEST(test_param_copy_null);
|
---|
714 | return 1;
|
---|
715 | }
|
---|