Parameter building utilities.
[openssl.git] / test / param_build_test.c
1 /*
2  * Copyright 2019 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 "internal/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;
20     OSSL_PARAM *params = NULL, *p;
21     void *secure = (void *)"abc";
22     int i;
23     long int l;
24     int32_t i32;
25     int64_t i64;
26     double d;
27     char *utf = NULL;
28     const char *cutf;
29     int res = 0;
30
31     ossl_param_bld_init(&bld);
32     if (!TEST_true(ossl_param_bld_push_int(&bld, "i", -6))
33         || !TEST_true(ossl_param_bld_push_long(&bld, "l", 42))
34         || !TEST_true(ossl_param_bld_push_int32(&bld, "i32", 1532))
35         || !TEST_true(ossl_param_bld_push_int64(&bld, "i64", -9999999))
36         || !TEST_true(ossl_param_bld_push_double(&bld, "d", 1.61803398875))
37         || !TEST_true(ossl_param_bld_push_utf8_string(&bld, "utf8_s", "foo",
38                                                       sizeof("foo")))
39         || !TEST_true(ossl_param_bld_push_utf8_ptr(&bld, "utf8_p", "bar-boom",
40                                                    0))
41         || !TEST_ptr(params = ossl_param_bld_to_param(&bld, &secure))
42         || !TEST_ptr_null(secure)
43         /* Check int */
44         || !TEST_ptr(p = OSSL_PARAM_locate(params, "i"))
45         || !TEST_true(OSSL_PARAM_get_int(p, &i))
46         || !TEST_str_eq(p->key, "i")
47         || !TEST_uint_eq(p->data_type, OSSL_PARAM_INTEGER)
48         || !TEST_size_t_eq(p->data_size, sizeof(int))
49         || !TEST_int_eq(i, -6)
50         /* Check int32 */
51         || !TEST_ptr(p = OSSL_PARAM_locate(params, "i32"))
52         || !TEST_true(OSSL_PARAM_get_int32(p, &i32))
53         || !TEST_str_eq(p->key, "i32")
54         || !TEST_uint_eq(p->data_type, OSSL_PARAM_INTEGER)
55         || !TEST_size_t_eq(p->data_size, sizeof(int32_t))
56         || !TEST_int_eq((int)i32, 1532)
57         /* Check int64 */
58         || !TEST_ptr(p = OSSL_PARAM_locate(params, "i64"))
59         || !TEST_str_eq(p->key, "i64")
60         || !TEST_uint_eq(p->data_type, OSSL_PARAM_INTEGER)
61         || !TEST_size_t_eq(p->data_size, sizeof(int64_t))
62         || !TEST_true(OSSL_PARAM_get_int64(p, &i64))
63         || !TEST_long_eq((long)i64, -9999999)
64         /* Check long */
65         || !TEST_ptr(p = OSSL_PARAM_locate(params, "l"))
66         || !TEST_str_eq(p->key, "l")
67         || !TEST_uint_eq(p->data_type, OSSL_PARAM_INTEGER)
68         || !TEST_size_t_eq(p->data_size, sizeof(long int))
69         || !TEST_true(OSSL_PARAM_get_long(p, &l))
70         || !TEST_long_eq(l, 42)
71         /* Check double */
72         || !TEST_ptr(p = OSSL_PARAM_locate(params, "d"))
73         || !TEST_true(OSSL_PARAM_get_double(p, &d))
74         || !TEST_str_eq(p->key, "d")
75         || !TEST_uint_eq(p->data_type, OSSL_PARAM_REAL)
76         || !TEST_size_t_eq(p->data_size, sizeof(double))
77         || !TEST_double_eq(d, 1.61803398875)
78         /* Check UTF8 string */
79         || !TEST_ptr(p = OSSL_PARAM_locate(params, "utf8_s"))
80         || !TEST_str_eq(p->data, "foo")
81         || !TEST_true(OSSL_PARAM_get_utf8_string(p, &utf, 0))
82         || !TEST_str_eq(utf, "foo")
83         /* Check UTF8 pointer */
84         || !TEST_ptr(p = OSSL_PARAM_locate(params, "utf8_p"))
85         || !TEST_true(OSSL_PARAM_get_utf8_ptr(p, &cutf))
86         || !TEST_str_eq(cutf, "bar-boom"))
87         goto err;
88     res = 1;
89 err:
90     OPENSSL_free(params);
91     OPENSSL_secure_free(secure);
92     OPENSSL_free(utf);
93     return res;
94 }
95
96 static int template_private_test(void)
97 {
98     static int data1[] = { 2, 3, 5, 7, 11, 15, 17 };
99     static unsigned char data2[] = { 2, 4, 6, 8, 10 };
100     OSSL_PARAM_BLD bld;
101     OSSL_PARAM *params = NULL, *p;
102     void *secure = (void *)"abc";
103     unsigned int i;
104     unsigned long int l;
105     uint32_t i32;
106     uint64_t i64;
107     size_t st;
108     BIGNUM *bn = NULL, *bn_res = NULL;
109     int res = 0;
110
111     ossl_param_bld_init(&bld);
112     if (!TEST_true(ossl_param_bld_push_uint(&bld, "i", 6))
113         || !TEST_true(ossl_param_bld_push_ulong(&bld, "l", 42))
114         || !TEST_true(ossl_param_bld_push_uint32(&bld, "i32", 1532))
115         || !TEST_true(ossl_param_bld_push_uint64(&bld, "i64", 9999999))
116         || !TEST_true(ossl_param_bld_push_size_t(&bld, "st", 65537))
117         || !TEST_ptr(bn = BN_new())
118         || !TEST_true(BN_set_word(bn, 1729))
119         || !TEST_true(ossl_param_bld_push_BN(&bld, "bignumber", bn))
120         || !TEST_true(ossl_param_bld_push_octet_string(&bld, "oct_s", data1,
121                                                        sizeof(data1)))
122         || !TEST_true(ossl_param_bld_push_octet_ptr(&bld, "oct_p", data2,
123                                                     sizeof(data2)))
124         || !TEST_ptr(params = ossl_param_bld_to_param(&bld, &secure))
125         /* Check unsigned int */
126         || !TEST_ptr(p = OSSL_PARAM_locate(params, "i"))
127         || !TEST_true(OSSL_PARAM_get_uint(p, &i))
128         || !TEST_str_eq(p->key, "i")
129         || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
130         || !TEST_size_t_eq(p->data_size, sizeof(int))
131         || !TEST_uint_eq(i, 6)
132         /* Check unsigned int32 */
133         || !TEST_ptr(p = OSSL_PARAM_locate(params, "i32"))
134         || !TEST_true(OSSL_PARAM_get_uint32(p, &i32))
135         || !TEST_str_eq(p->key, "i32")
136         || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
137         || !TEST_size_t_eq(p->data_size, sizeof(int32_t))
138         || !TEST_uint_eq((unsigned int)i32, 1532)
139         /* Check unsigned int64 */
140         || !TEST_ptr(p = OSSL_PARAM_locate(params, "i64"))
141         || !TEST_str_eq(p->key, "i64")
142         || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
143         || !TEST_size_t_eq(p->data_size, sizeof(int64_t))
144         || !TEST_true(OSSL_PARAM_get_uint64(p, &i64))
145         || !TEST_ulong_eq((unsigned long)i64, 9999999)
146         /* Check unsigned long int */
147         || !TEST_ptr(p = OSSL_PARAM_locate(params, "l"))
148         || !TEST_str_eq(p->key, "l")
149         || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
150         || !TEST_size_t_eq(p->data_size, sizeof(unsigned long int))
151         || !TEST_true(OSSL_PARAM_get_ulong(p, &l))
152         || !TEST_ulong_eq(l, 42)
153         /* Check size_t */
154         || !TEST_ptr(p = OSSL_PARAM_locate(params, "st"))
155         || !TEST_str_eq(p->key, "st")
156         || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
157         || !TEST_size_t_eq(p->data_size, sizeof(size_t))
158         || !TEST_true(OSSL_PARAM_get_size_t(p, &st))
159         || !TEST_size_t_eq(st, 65537)
160         /* Check octet string */
161         || !TEST_ptr(p = OSSL_PARAM_locate(params, "oct_s"))
162         || !TEST_str_eq(p->key, "oct_s")
163         || !TEST_uint_eq(p->data_type, OSSL_PARAM_OCTET_STRING)
164         || !TEST_mem_eq(p->data, p->data_size, data1, sizeof(data1))
165         /* Check octet pointer */
166         || !TEST_ptr(p = OSSL_PARAM_locate(params, "oct_p"))
167         || !TEST_str_eq(p->key, "oct_p")
168         || !TEST_uint_eq(p->data_type, OSSL_PARAM_OCTET_PTR)
169         || !TEST_mem_eq(*(void **)p->data, p->data_size, data2, sizeof(data2))
170         /* Check BN */
171         || !TEST_ptr(p = OSSL_PARAM_locate(params, "bignumber"))
172         || !TEST_str_eq(p->key, "bignumber")
173         || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
174         || !TEST_true(OSSL_PARAM_get_BN(p, &bn_res))
175         || !TEST_int_eq(BN_cmp(bn_res, bn), 0))
176         goto err;
177     res = 1;
178 err:
179     OPENSSL_secure_free(secure);
180     OPENSSL_free(params);
181     BN_free(bn);
182     BN_free(bn_res);
183     return res;
184 }
185
186 static int template_static_params_test(int n)
187 {
188     unsigned char data[1000], secure[500];
189     OSSL_PARAM_BLD bld;
190     OSSL_PARAM params[20], *p;
191     BIGNUM *bn = NULL, *bn_r = NULL;
192     unsigned int i;
193     char *utf = NULL;
194     int res = 0;
195
196     ossl_param_bld_init(&bld);
197     if (!TEST_true(ossl_param_bld_push_uint(&bld, "i", 6))
198         || !TEST_ptr(bn = (n & 1) == 0 ? BN_new() : BN_secure_new())
199         || !TEST_true(BN_set_word(bn, 1337))
200         || !TEST_true(ossl_param_bld_push_BN(&bld, "bn", bn))
201         || !TEST_true(ossl_param_bld_push_utf8_string(&bld, "utf8_s", "bar",
202                                                       0))
203         || !TEST_ptr(ossl_param_bld_to_param_ex(&bld, params,
204                                                 OSSL_NELEM(params),
205                                                 data, sizeof(data),
206                                                 secure, sizeof(secure)))
207         /* Check unsigned int */
208         || !TEST_ptr(p = OSSL_PARAM_locate(params, "i"))
209         || !TEST_true(OSSL_PARAM_get_uint(p, &i))
210         || !TEST_str_eq(p->key, "i")
211         || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
212         || !TEST_size_t_eq(p->data_size, sizeof(int))
213         || !TEST_uint_eq(i, 6)
214         /* Check BIGNUM */
215         || !TEST_ptr(p = OSSL_PARAM_locate(params, "bn"))
216         || !TEST_true(OSSL_PARAM_get_BN(p, &bn_r))
217         || !TEST_str_eq(p->key, "bn")
218         || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
219         || !TEST_size_t_le(p->data_size, sizeof(BN_ULONG))
220         || !TEST_uint_eq((unsigned int)BN_get_word(bn_r), 1337)
221         /* Check UTF8 string */
222         || !TEST_ptr(p = OSSL_PARAM_locate(params, "utf8_s"))
223         || !TEST_str_eq(p->data, "bar")
224         || !TEST_true(OSSL_PARAM_get_utf8_string(p, &utf, 0))
225         || !TEST_str_eq(utf, "bar"))
226         goto err;
227     res = 1;
228 err:
229     OPENSSL_free(utf);
230     BN_free(bn);
231     BN_free(bn_r);
232     return res;
233 }
234
235 static int template_static_fail_test(int n)
236 {
237     unsigned char data[10000], secure[500];
238     OSSL_PARAM_BLD bld;
239     OSSL_PARAM prms[20];
240     BIGNUM *bn = NULL;
241     int res = 0;
242
243     ossl_param_bld_init(&bld);
244     if (!TEST_true(ossl_param_bld_push_uint(&bld, "i", 6))
245         || !TEST_ptr(bn = (n & 1) == 0 ? BN_new() : BN_secure_new())
246         || !TEST_true(BN_hex2bn(&bn, "ABCDEF78901234567890ABCDEF0987987654321"))
247         || !TEST_true(ossl_param_bld_push_BN(&bld, "bn", bn))
248         || !TEST_true(ossl_param_bld_push_utf8_string(&bld, "utf8_s", "abc",
249                                                       1000))
250         /* No OSSL_PARAMS */
251         || !TEST_ptr_null(ossl_param_bld_to_param_ex(&bld, NULL, 0, data,
252                                                      sizeof(data), secure,
253                                                      sizeof(secure)))
254         /* Short OSSL_PARAMS */
255         || !TEST_ptr_null(ossl_param_bld_to_param_ex(&bld, prms, 2,
256                                                      data, sizeof(data),
257                                                      secure, sizeof(secure)))
258         /* No normal data */
259         || !TEST_ptr_null(ossl_param_bld_to_param_ex(&bld, prms,
260                                                      OSSL_NELEM(prms),
261                                                      NULL, 0, secure,
262                                                      sizeof(secure)))
263         /* Not enough normal data */
264         || !TEST_ptr_null(ossl_param_bld_to_param_ex(&bld, prms,
265                                                      OSSL_NELEM(prms),
266                                                      data, 50, secure,
267                                                      sizeof(secure))))
268             goto err;
269         if ((n & 1) == 1) {
270             /* No secure data */
271             if (!TEST_ptr_null(ossl_param_bld_to_param_ex(&bld, prms,
272                                                           OSSL_NELEM(prms),
273                                                           data, sizeof(data),
274                                                           NULL, 0))
275             /* Not enough secure data */
276             || !TEST_ptr_null(ossl_param_bld_to_param_ex(&bld, prms,
277                                                          OSSL_NELEM(prms),
278                                                          data, sizeof(data),
279                                                          secure, 4)))
280                 goto err;
281         }
282     res = 1;
283 err:
284     BN_free(bn);
285     return res;
286 }
287
288 int setup_tests(void)
289 {
290     ADD_TEST(template_public_test);
291     ADD_TEST(template_private_test);
292     ADD_ALL_TESTS(template_static_params_test, 2);
293     ADD_ALL_TESTS(template_static_fail_test, 2);
294     return 1;
295 }