2 * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
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
12 #include <openssl/params.h>
13 #include "internal/param_build.h"
14 #include "internal/nelem.h"
17 static int template_public_test(void)
20 OSSL_PARAM *params = NULL, *p;
21 void *secure = (void *)"abc";
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",
39 || !TEST_true(ossl_param_bld_push_utf8_ptr(&bld, "utf8_p", "bar-boom",
41 || !TEST_ptr(params = ossl_param_bld_to_param(&bld, &secure))
42 || !TEST_ptr_null(secure)
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)
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)
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)
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)
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"))
91 OPENSSL_secure_free(secure);
96 static int template_private_test(void)
98 static int data1[] = { 2, 3, 5, 7, 11, 15, 17 };
99 static unsigned char data2[] = { 2, 4, 6, 8, 10 };
101 OSSL_PARAM *params = NULL, *p;
102 void *secure = (void *)"abc";
108 BIGNUM *bn = NULL, *bn_res = NULL;
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,
122 || !TEST_true(ossl_param_bld_push_octet_ptr(&bld, "oct_p", 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)
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))
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))
179 OPENSSL_secure_free(secure);
180 OPENSSL_free(params);
186 static int template_static_params_test(int n)
188 unsigned char data[1000], secure[500];
190 OSSL_PARAM params[20], *p;
191 BIGNUM *bn = NULL, *bn_r = NULL;
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",
203 || !TEST_ptr(ossl_param_bld_to_param_ex(&bld, params,
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)
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"))
235 static int template_static_fail_test(int n)
237 unsigned char data[10000], secure[500];
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",
251 || !TEST_ptr_null(ossl_param_bld_to_param_ex(&bld, NULL, 0, data,
252 sizeof(data), secure,
254 /* Short OSSL_PARAMS */
255 || !TEST_ptr_null(ossl_param_bld_to_param_ex(&bld, prms, 2,
257 secure, sizeof(secure)))
259 || !TEST_ptr_null(ossl_param_bld_to_param_ex(&bld, prms,
263 /* Not enough normal data */
264 || !TEST_ptr_null(ossl_param_bld_to_param_ex(&bld, prms,
271 if (!TEST_ptr_null(ossl_param_bld_to_param_ex(&bld, prms,
275 /* Not enough secure data */
276 || !TEST_ptr_null(ossl_param_bld_to_param_ex(&bld, prms,
288 int setup_tests(void)
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);