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