]>
Commit | Line | Data |
---|---|---|
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 <openssl/params.h> | |
13 | #include "openssl/param_build.h" | |
14 | #include "internal/nelem.h" | |
15 | #include "testutil.h" | |
16 | ||
17 | static int template_public_test(void) | |
18 | { | |
19 | OSSL_PARAM_BLD *bld = OSSL_PARAM_BLD_new(); | |
20 | OSSL_PARAM *params = NULL, *p; | |
21 | BIGNUM *bn = NULL, *bn_res = NULL; | |
22 | int i; | |
23 | long int l; | |
24 | int32_t i32; | |
25 | int64_t i64; | |
26 | double d; | |
27 | time_t t; | |
28 | char *utf = NULL; | |
29 | const char *cutf; | |
30 | int res = 0; | |
31 | ||
32 | if (!TEST_ptr(bld) | |
33 | || !TEST_true(OSSL_PARAM_BLD_push_int(bld, "i", -6)) | |
34 | || !TEST_true(OSSL_PARAM_BLD_push_long(bld, "l", 42)) | |
35 | || !TEST_true(OSSL_PARAM_BLD_push_int32(bld, "i32", 1532)) | |
36 | || !TEST_true(OSSL_PARAM_BLD_push_int64(bld, "i64", -9999999)) | |
37 | || !TEST_true(OSSL_PARAM_BLD_push_time_t(bld, "t", 11224)) | |
38 | || !TEST_true(OSSL_PARAM_BLD_push_double(bld, "d", 1.61803398875)) | |
39 | || !TEST_ptr(bn = BN_new()) | |
40 | || !TEST_true(BN_set_word(bn, 1729)) | |
41 | || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, "bignumber", bn)) | |
42 | || !TEST_true(OSSL_PARAM_BLD_push_utf8_string(bld, "utf8_s", "foo", | |
43 | sizeof("foo"))) | |
44 | || !TEST_true(OSSL_PARAM_BLD_push_utf8_ptr(bld, "utf8_p", "bar-boom", | |
45 | 0)) | |
46 | || !TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld)) | |
47 | /* Check int */ | |
48 | || !TEST_ptr(p = OSSL_PARAM_locate(params, "i")) | |
49 | || !TEST_true(OSSL_PARAM_get_int(p, &i)) | |
50 | || !TEST_str_eq(p->key, "i") | |
51 | || !TEST_uint_eq(p->data_type, OSSL_PARAM_INTEGER) | |
52 | || !TEST_size_t_eq(p->data_size, sizeof(int)) | |
53 | || !TEST_int_eq(i, -6) | |
54 | /* Check int32 */ | |
55 | || !TEST_ptr(p = OSSL_PARAM_locate(params, "i32")) | |
56 | || !TEST_true(OSSL_PARAM_get_int32(p, &i32)) | |
57 | || !TEST_str_eq(p->key, "i32") | |
58 | || !TEST_uint_eq(p->data_type, OSSL_PARAM_INTEGER) | |
59 | || !TEST_size_t_eq(p->data_size, sizeof(int32_t)) | |
60 | || !TEST_int_eq((int)i32, 1532) | |
61 | /* Check int64 */ | |
62 | || !TEST_ptr(p = OSSL_PARAM_locate(params, "i64")) | |
63 | || !TEST_str_eq(p->key, "i64") | |
64 | || !TEST_uint_eq(p->data_type, OSSL_PARAM_INTEGER) | |
65 | || !TEST_size_t_eq(p->data_size, sizeof(int64_t)) | |
66 | || !TEST_true(OSSL_PARAM_get_int64(p, &i64)) | |
67 | || !TEST_long_eq((long)i64, -9999999) | |
68 | /* Check long */ | |
69 | || !TEST_ptr(p = OSSL_PARAM_locate(params, "l")) | |
70 | || !TEST_str_eq(p->key, "l") | |
71 | || !TEST_uint_eq(p->data_type, OSSL_PARAM_INTEGER) | |
72 | || !TEST_size_t_eq(p->data_size, sizeof(long int)) | |
73 | || !TEST_true(OSSL_PARAM_get_long(p, &l)) | |
74 | || !TEST_long_eq(l, 42) | |
75 | /* Check time_t */ | |
76 | || !TEST_ptr(p = OSSL_PARAM_locate(params, "t")) | |
77 | || !TEST_str_eq(p->key, "t") | |
78 | || !TEST_uint_eq(p->data_type, OSSL_PARAM_INTEGER) | |
79 | || !TEST_size_t_eq(p->data_size, sizeof(time_t)) | |
80 | || !TEST_true(OSSL_PARAM_get_time_t(p, &t)) | |
81 | || !TEST_time_t_eq(t, 11224) | |
82 | /* Check double */ | |
83 | || !TEST_ptr(p = OSSL_PARAM_locate(params, "d")) | |
84 | || !TEST_true(OSSL_PARAM_get_double(p, &d)) | |
85 | || !TEST_str_eq(p->key, "d") | |
86 | || !TEST_uint_eq(p->data_type, OSSL_PARAM_REAL) | |
87 | || !TEST_size_t_eq(p->data_size, sizeof(double)) | |
88 | || !TEST_double_eq(d, 1.61803398875) | |
89 | /* Check UTF8 string */ | |
90 | || !TEST_ptr(p = OSSL_PARAM_locate(params, "utf8_s")) | |
91 | || !TEST_str_eq(p->data, "foo") | |
92 | || !TEST_true(OSSL_PARAM_get_utf8_string(p, &utf, 0)) | |
93 | || !TEST_str_eq(utf, "foo") | |
94 | /* Check UTF8 pointer */ | |
95 | || !TEST_ptr(p = OSSL_PARAM_locate(params, "utf8_p")) | |
96 | || !TEST_true(OSSL_PARAM_get_utf8_ptr(p, &cutf)) | |
97 | || !TEST_str_eq(cutf, "bar-boom") | |
98 | /* Check BN */ | |
99 | || !TEST_ptr(p = OSSL_PARAM_locate(params, "bignumber")) | |
100 | || !TEST_str_eq(p->key, "bignumber") | |
101 | || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER) | |
102 | || !TEST_true(OSSL_PARAM_get_BN(p, &bn_res)) | |
103 | || !TEST_int_eq(BN_cmp(bn_res, bn), 0)) | |
104 | goto err; | |
105 | res = 1; | |
106 | err: | |
107 | OSSL_PARAM_BLD_free_params(params); | |
108 | OSSL_PARAM_BLD_free(bld); | |
109 | OPENSSL_free(utf); | |
110 | BN_free(bn); | |
111 | BN_free(bn_res); | |
112 | return res; | |
113 | } | |
114 | ||
115 | static int template_private_test(void) | |
116 | { | |
117 | int *data1 = NULL, *data2 = NULL, j; | |
118 | const int data1_num = 12; | |
119 | const int data1_size = data1_num * sizeof(int); | |
120 | const int data2_num = 5; | |
121 | const int data2_size = data2_num * sizeof(int); | |
122 | OSSL_PARAM_BLD *bld = NULL; | |
123 | OSSL_PARAM *params = NULL, *p; | |
124 | unsigned int i; | |
125 | unsigned long int l; | |
126 | uint32_t i32; | |
127 | uint64_t i64; | |
128 | size_t st; | |
129 | BIGNUM *bn = NULL, *bn_res = NULL; | |
130 | int res = 0; | |
131 | ||
132 | if (!TEST_ptr(data1 = OPENSSL_secure_malloc(data1_size)) | |
133 | || !TEST_ptr(data2 = OPENSSL_secure_malloc(data2_size)) | |
134 | || !TEST_ptr(bld = OSSL_PARAM_BLD_new())) | |
135 | goto err; | |
136 | ||
137 | for (j = 0; j < data1_num; j++) | |
138 | data1[j] = -16 * j; | |
139 | for (j = 0; j < data2_num; j++) | |
140 | data2[j] = 2 * j; | |
141 | ||
142 | if (!TEST_true(OSSL_PARAM_BLD_push_uint(bld, "i", 6)) | |
143 | || !TEST_true(OSSL_PARAM_BLD_push_ulong(bld, "l", 42)) | |
144 | || !TEST_true(OSSL_PARAM_BLD_push_uint32(bld, "i32", 1532)) | |
145 | || !TEST_true(OSSL_PARAM_BLD_push_uint64(bld, "i64", 9999999)) | |
146 | || !TEST_true(OSSL_PARAM_BLD_push_size_t(bld, "st", 65537)) | |
147 | || !TEST_ptr(bn = BN_secure_new()) | |
148 | || !TEST_true(BN_set_word(bn, 1729)) | |
149 | || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, "bignumber", bn)) | |
150 | || !TEST_true(OSSL_PARAM_BLD_push_octet_string(bld, "oct_s", data1, | |
151 | data1_size)) | |
152 | || !TEST_true(OSSL_PARAM_BLD_push_octet_ptr(bld, "oct_p", data2, | |
153 | data2_size)) | |
154 | || !TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld)) | |
155 | /* Check unsigned int */ | |
156 | || !TEST_ptr(p = OSSL_PARAM_locate(params, "i")) | |
157 | || !TEST_false(CRYPTO_secure_allocated(p->data)) | |
158 | || !TEST_true(OSSL_PARAM_get_uint(p, &i)) | |
159 | || !TEST_str_eq(p->key, "i") | |
160 | || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER) | |
161 | || !TEST_size_t_eq(p->data_size, sizeof(int)) | |
162 | || !TEST_uint_eq(i, 6) | |
163 | /* Check unsigned int32 */ | |
164 | || !TEST_ptr(p = OSSL_PARAM_locate(params, "i32")) | |
165 | || !TEST_false(CRYPTO_secure_allocated(p->data)) | |
166 | || !TEST_true(OSSL_PARAM_get_uint32(p, &i32)) | |
167 | || !TEST_str_eq(p->key, "i32") | |
168 | || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER) | |
169 | || !TEST_size_t_eq(p->data_size, sizeof(int32_t)) | |
170 | || !TEST_uint_eq((unsigned int)i32, 1532) | |
171 | /* Check unsigned int64 */ | |
172 | || !TEST_ptr(p = OSSL_PARAM_locate(params, "i64")) | |
173 | || !TEST_false(CRYPTO_secure_allocated(p->data)) | |
174 | || !TEST_str_eq(p->key, "i64") | |
175 | || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER) | |
176 | || !TEST_size_t_eq(p->data_size, sizeof(int64_t)) | |
177 | || !TEST_true(OSSL_PARAM_get_uint64(p, &i64)) | |
178 | || !TEST_ulong_eq((unsigned long)i64, 9999999) | |
179 | /* Check unsigned long int */ | |
180 | || !TEST_ptr(p = OSSL_PARAM_locate(params, "l")) | |
181 | || !TEST_false(CRYPTO_secure_allocated(p->data)) | |
182 | || !TEST_str_eq(p->key, "l") | |
183 | || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER) | |
184 | || !TEST_size_t_eq(p->data_size, sizeof(unsigned long int)) | |
185 | || !TEST_true(OSSL_PARAM_get_ulong(p, &l)) | |
186 | || !TEST_ulong_eq(l, 42) | |
187 | /* Check size_t */ | |
188 | || !TEST_ptr(p = OSSL_PARAM_locate(params, "st")) | |
189 | || !TEST_false(CRYPTO_secure_allocated(p->data)) | |
190 | || !TEST_str_eq(p->key, "st") | |
191 | || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER) | |
192 | || !TEST_size_t_eq(p->data_size, sizeof(size_t)) | |
193 | || !TEST_true(OSSL_PARAM_get_size_t(p, &st)) | |
194 | || !TEST_size_t_eq(st, 65537) | |
195 | /* Check octet string */ | |
196 | || !TEST_ptr(p = OSSL_PARAM_locate(params, "oct_s")) | |
197 | || !TEST_true(CRYPTO_secure_allocated(p->data)) | |
198 | || !TEST_str_eq(p->key, "oct_s") | |
199 | || !TEST_uint_eq(p->data_type, OSSL_PARAM_OCTET_STRING) | |
200 | || !TEST_mem_eq(p->data, p->data_size, data1, data1_size) | |
201 | /* Check octet pointer */ | |
202 | || !TEST_ptr(p = OSSL_PARAM_locate(params, "oct_p")) | |
203 | || !TEST_false(CRYPTO_secure_allocated(p->data)) | |
204 | || !TEST_true(CRYPTO_secure_allocated(*(void **)p->data)) | |
205 | || !TEST_str_eq(p->key, "oct_p") | |
206 | || !TEST_uint_eq(p->data_type, OSSL_PARAM_OCTET_PTR) | |
207 | || !TEST_mem_eq(*(void **)p->data, p->data_size, data2, data2_size) | |
208 | /* Check BN */ | |
209 | || !TEST_ptr(p = OSSL_PARAM_locate(params, "bignumber")) | |
210 | || !TEST_true(CRYPTO_secure_allocated(p->data)) | |
211 | || !TEST_str_eq(p->key, "bignumber") | |
212 | || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER) | |
213 | || !TEST_true(OSSL_PARAM_get_BN(p, &bn_res)) | |
214 | || !TEST_int_eq(BN_get_flags(bn, BN_FLG_SECURE), BN_FLG_SECURE) | |
215 | || !TEST_int_eq(BN_cmp(bn_res, bn), 0)) | |
216 | goto err; | |
217 | res = 1; | |
218 | err: | |
219 | OSSL_PARAM_BLD_free_params(params); | |
220 | OSSL_PARAM_BLD_free(bld); | |
221 | OPENSSL_secure_free(data1); | |
222 | OPENSSL_secure_free(data2); | |
223 | BN_free(bn); | |
224 | BN_free(bn_res); | |
225 | return res; | |
226 | } | |
227 | ||
228 | static int builder_limit_test(void) | |
229 | { | |
230 | const int n = 100; | |
231 | char names[100][3]; | |
232 | OSSL_PARAM_BLD *bld = OSSL_PARAM_BLD_new(); | |
233 | OSSL_PARAM *params = NULL; | |
234 | int i, res = 0; | |
235 | ||
236 | if (!TEST_ptr(bld)) | |
237 | goto err; | |
238 | ||
239 | for (i = 0; i < n; i++) { | |
240 | names[i][0] = 'A' + (i / 26) - 1; | |
241 | names[i][1] = 'a' + (i % 26) - 1; | |
242 | names[i][2] = '\0'; | |
243 | if (!TEST_true(OSSL_PARAM_BLD_push_int(bld, names[i], 3 * i + 1))) | |
244 | goto err; | |
245 | } | |
246 | if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld))) | |
247 | goto err; | |
248 | /* Count the elements in the params arrary, expecting n */ | |
249 | for (i = 0; params[i].key != NULL; i++); | |
250 | if (!TEST_int_eq(i, n)) | |
251 | goto err; | |
252 | ||
253 | /* Verify that the build, cleared the builder structure */ | |
254 | OSSL_PARAM_BLD_free_params(params); | |
255 | params = NULL; | |
256 | ||
257 | if (!TEST_true(OSSL_PARAM_BLD_push_int(bld, "g", 2)) | |
258 | || !TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld))) | |
259 | goto err; | |
260 | /* Count the elements in the params arrary, expecting 1 */ | |
261 | for (i = 0; params[i].key != NULL; i++); | |
262 | if (!TEST_int_eq(i, 1)) | |
263 | goto err; | |
264 | res = 1; | |
265 | err: | |
266 | OSSL_PARAM_BLD_free_params(params); | |
267 | OSSL_PARAM_BLD_free(bld); | |
268 | return res; | |
269 | } | |
270 | ||
271 | int setup_tests(void) | |
272 | { | |
273 | ADD_TEST(template_public_test); | |
274 | /* Only run the secure memory testing if we have secure memory available */ | |
275 | if (CRYPTO_secure_malloc_init(1<<16, 16)) | |
276 | ADD_TEST(template_private_test); | |
277 | ADD_TEST(builder_limit_test); | |
278 | return 1; | |
279 | } |