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 <openssl/params.h>
13 #include "testutil.h"
14 
15 /* On machines that dont support <inttypes.h> just disable the tests */
16 #if !defined(OPENSSL_NO_INTTYPES_H)
17 
18 # ifdef OPENSSL_SYS_VMS
19 #  define strtoumax strtoull
20 #  define strtoimax strtoll
21 # endif
22 
23 typedef struct {
24     OSSL_PARAM *param;
25     int32_t i32;
26     int64_t i64;
27     uint32_t u32;
28     uint64_t u64;
29     double d;
30     int valid_i32, valid_i64, valid_u32, valid_u64, valid_d;
31     void *ref, *datum;
32     size_t size;
33 } PARAM_CONVERSION;
34 
35 static int param_conversion_load_stanza(PARAM_CONVERSION *pc, const STANZA *s)
36 {
37 
38     static int32_t datum_i32, ref_i32;
39     static int64_t datum_i64, ref_i64;
40     static uint32_t datum_u32, ref_u32;
41     static uint64_t datum_u64, ref_u64;
42     static double datum_d, ref_d;
43     static OSSL_PARAM params[] = {
44         OSSL_PARAM_int32("int32",   &datum_i32),
45         OSSL_PARAM_int64("int64",   &datum_i64),
46         OSSL_PARAM_uint32("uint32", &datum_u32),
47         OSSL_PARAM_uint64("uint64", &datum_u64),
48         OSSL_PARAM_double("double", &datum_d),
49         OSSL_PARAM_END
50     };
51     int def_i32 = 0, def_i64 = 0, def_u32 = 0, def_u64 = 0, def_d = 0;
52     const PAIR *pp = s->pairs;
53     const char *type = NULL;
54     char *p;
55     int i;
56 
57     memset(pc, 0, sizeof(*pc));
58 
59     for (i = 0; i < s->numpairs; i++, pp++) {
60         p = "";
61         if (OPENSSL_strcasecmp(pp->key, "type") == 0) {
62             if (type != NULL) {
63                 TEST_info("Line %d: multiple type lines", s->curr);
64                 return 0;
65             }
66             pc->param = OSSL_PARAM_locate(params, type = pp->value);
67             if (pc->param == NULL) {
68                 TEST_info("Line %d: unknown type line", s->curr);
69                 return 0;
70             }
71         } else if (OPENSSL_strcasecmp(pp->key, "int32") == 0) {
72             if (def_i32++) {
73                 TEST_info("Line %d: multiple int32 lines", s->curr);
74                 return 0;
75             }
76             if (OPENSSL_strcasecmp(pp->value, "invalid") != 0) {
77                 pc->valid_i32 = 1;
78                 pc->i32 = (int32_t)strtoimax(pp->value, &p, 10);
79             }
80         } else if (OPENSSL_strcasecmp(pp->key, "int64") == 0) {
81             if (def_i64++) {
82                 TEST_info("Line %d: multiple int64 lines", s->curr);
83                 return 0;
84             }
85             if (OPENSSL_strcasecmp(pp->value, "invalid") != 0) {
86                 pc->valid_i64 = 1;
87                 pc->i64 = (int64_t)strtoimax(pp->value, &p, 10);
88             }
89         } else if (OPENSSL_strcasecmp(pp->key, "uint32") == 0) {
90             if (def_u32++) {
91                 TEST_info("Line %d: multiple uint32 lines", s->curr);
92                 return 0;
93             }
94             if (OPENSSL_strcasecmp(pp->value, "invalid") != 0) {
95                 pc->valid_u32 = 1;
96                 pc->u32 = (uint32_t)strtoumax(pp->value, &p, 10);
97             }
98         } else if (OPENSSL_strcasecmp(pp->key, "uint64") == 0) {
99             if (def_u64++) {
100                 TEST_info("Line %d: multiple uint64 lines", s->curr);
101                 return 0;
102             }
103             if (OPENSSL_strcasecmp(pp->value, "invalid") != 0) {
104                 pc->valid_u64 = 1;
105                 pc->u64 = (uint64_t)strtoumax(pp->value, &p, 10);
106             }
107         } else if (OPENSSL_strcasecmp(pp->key, "double") == 0) {
108             if (def_d++) {
109                 TEST_info("Line %d: multiple double lines", s->curr);
110                 return 0;
111             }
112             if (OPENSSL_strcasecmp(pp->value, "invalid") != 0) {
113                 pc->valid_d = 1;
114                 pc->d = strtod(pp->value, &p);
115             }
116         } else {
117             TEST_info("Line %d: unknown keyword %s", s->curr, pp->key);
118             return 0;
119         }
120         if (*p != '\0') {
121             TEST_info("Line %d: extra characters at end '%s' for %s",
122                       s->curr, p, pp->key);
123             return 0;
124         }
125     }
126 
127     if (!TEST_ptr(type)) {
128         TEST_info("Line %d: type not found", s->curr);
129         return 0;
130     }
131 
132     if (OPENSSL_strcasecmp(type, "int32") == 0) {
133         if (!TEST_true(def_i32) || !TEST_true(pc->valid_i32)) {
134             TEST_note("errant int32 on line %d", s->curr);
135             return 0;
136         }
137         datum_i32 = ref_i32 = pc->i32;
138         pc->datum = &datum_i32;
139         pc->ref = &ref_i32;
140         pc->size = sizeof(ref_i32);
141     } else if (OPENSSL_strcasecmp(type, "int64") == 0) {
142         if (!TEST_true(def_i64) || !TEST_true(pc->valid_i64)) {
143             TEST_note("errant int64 on line %d", s->curr);
144             return 0;
145         }
146         datum_i64 = ref_i64 = pc->i64;
147         pc->datum = &datum_i64;
148         pc->ref = &ref_i64;
149         pc->size = sizeof(ref_i64);
150     } else if (OPENSSL_strcasecmp(type, "uint32") == 0) {
151         if (!TEST_true(def_u32) || !TEST_true(pc->valid_u32)) {
152             TEST_note("errant uint32 on line %d", s->curr);
153             return 0;
154         }
155         datum_u32 = ref_u32 = pc->u32;
156         pc->datum = &datum_u32;
157         pc->ref = &ref_u32;
158         pc->size = sizeof(ref_u32);
159     } else if (OPENSSL_strcasecmp(type, "uint64") == 0) {
160         if (!TEST_true(def_u64) || !TEST_true(pc->valid_u64)) {
161             TEST_note("errant uint64 on line %d", s->curr);
162             return 0;
163         }
164         datum_u64 = ref_u64 = pc->u64;
165         pc->datum = &datum_u64;
166         pc->ref = &ref_u64;
167         pc->size = sizeof(ref_u64);
168     } else if (OPENSSL_strcasecmp(type, "double") == 0) {
169         if (!TEST_true(def_d) || !TEST_true(pc->valid_d)) {
170             TEST_note("errant double on line %d", s->curr);
171             return 0;
172         }
173         datum_d = ref_d = pc->d;
174         pc->datum = &datum_d;
175         pc->ref = &ref_d;
176         pc->size = sizeof(ref_d);
177     } else {
178         TEST_error("type unknown at line %d", s->curr);
179         return 0;
180     }
181     return 1;
182 }
183 
184 static int param_conversion_test(const PARAM_CONVERSION *pc, int line)
185 {
186     int32_t i32;
187     int64_t i64;
188     uint32_t u32;
189     uint64_t u64;
190     double d;
191 
192     if (!pc->valid_i32) {
193         if (!TEST_false(OSSL_PARAM_get_int32(pc->param, &i32))) {
194             TEST_note("unexpected valid conversion to int32 on line %d", line);
195             return 0;
196         }
197     } else {
198         if (!TEST_true(OSSL_PARAM_get_int32(pc->param, &i32))
199             || !TEST_true(i32 == pc->i32)) {
200             TEST_note("unexpected conversion to int32 on line %d", line);
201             return 0;
202         }
203         memset(pc->datum, 44, pc->size);
204         if (!TEST_true(OSSL_PARAM_set_int32(pc->param, i32))
205             || !TEST_mem_eq(pc->datum, pc->size, pc->ref, pc->size)) {
206             TEST_note("unexpected valid conversion from int32 on line %d",
207                       line);
208             return 0;
209         }
210     }
211 
212     if (!pc->valid_i64) {
213         if (!TEST_false(OSSL_PARAM_get_int64(pc->param, &i64))) {
214             TEST_note("unexpected valid conversion to int64 on line %d", line);
215             return 0;
216         }
217     } else {
218         if (!TEST_true(OSSL_PARAM_get_int64(pc->param, &i64))
219             || !TEST_true(i64 == pc->i64)) {
220             TEST_note("unexpected conversion to int64 on line %d", line);
221             return 0;
222         }
223         memset(pc->datum, 44, pc->size);
224         if (!TEST_true(OSSL_PARAM_set_int64(pc->param, i64))
225             || !TEST_mem_eq(pc->datum, pc->size, pc->ref, pc->size)) {
226             TEST_note("unexpected valid conversion from int64 on line %d",
227                       line);
228             return 0;
229         }
230     }
231 
232     if (!pc->valid_u32) {
233         if (!TEST_false(OSSL_PARAM_get_uint32(pc->param, &u32))) {
234             TEST_note("unexpected valid conversion to uint32 on line %d", line);
235             return 0;
236         }
237     } else {
238         if (!TEST_true(OSSL_PARAM_get_uint32(pc->param, &u32))
239             || !TEST_true(u32 == pc->u32)) {
240             TEST_note("unexpected conversion to uint32 on line %d", line);
241             return 0;
242         }
243         memset(pc->datum, 44, pc->size);
244         if (!TEST_true(OSSL_PARAM_set_uint32(pc->param, u32))
245             || !TEST_mem_eq(pc->datum, pc->size, pc->ref, pc->size)) {
246             TEST_note("unexpected valid conversion from uint32 on line %d",
247                       line);
248             return 0;
249         }
250     }
251 
252     if (!pc->valid_u64) {
253         if (!TEST_false(OSSL_PARAM_get_uint64(pc->param, &u64))) {
254             TEST_note("unexpected valid conversion to uint64 on line %d", line);
255             return 0;
256         }
257     } else {
258         if (!TEST_true(OSSL_PARAM_get_uint64(pc->param, &u64))
259             || !TEST_true(u64 == pc->u64)) {
260             TEST_note("unexpected conversion to uint64 on line %d", line);
261             return 0;
262         }
263         memset(pc->datum, 44, pc->size);
264         if (!TEST_true(OSSL_PARAM_set_uint64(pc->param, u64))
265             || !TEST_mem_eq(pc->datum, pc->size, pc->ref, pc->size)) {
266             TEST_note("unexpected valid conversion from uint64 on line %d",
267                       line);
268             return 0;
269         }
270     }
271 
272     if (!pc->valid_d) {
273         if (!TEST_false(OSSL_PARAM_get_double(pc->param, &d))) {
274             TEST_note("unexpected valid conversion to double on line %d", line);
275             return 0;
276         }
277     } else {
278         if (!TEST_true(OSSL_PARAM_get_double(pc->param, &d))) {
279             TEST_note("unable to convert to double on line %d", line);
280             return 0;
281         }
282         /*
283          * Check for not a number (NaN) without using the libm functions.
284          * When d is a NaN, the standard requires d == d to be false.
285          * It's less clear if d != d should be true even though it generally is.
286          * Hence we use the equality test and a not.
287          */
288         if (!(d == d)) {
289             /*
290              * We've encountered a NaN so check it's really meant to be a NaN.
291              * We ignore the case where the two values are both different NaN,
292              * that's not resolvable without knowing the underlying format
293              * or using libm functions.
294              */
295             if (!TEST_false(pc->d == pc->d)) {
296                 TEST_note("unexpected NaN on line %d", line);
297                 return 0;
298             }
299         } else if (!TEST_true(d == pc->d)) {
300             TEST_note("unexpected conversion to double on line %d", line);
301             return 0;
302         }
303         memset(pc->datum, 44, pc->size);
304         if (!TEST_true(OSSL_PARAM_set_double(pc->param, d))
305             || !TEST_mem_eq(pc->datum, pc->size, pc->ref, pc->size)) {
306             TEST_note("unexpected valid conversion from double on line %d",
307                       line);
308             return 0;
309         }
310     }
311 
312     return 1;
313 }
314 
315 static int run_param_file_tests(int i)
316 {
317     STANZA *s;
318     PARAM_CONVERSION pc;
319     const char *testfile = test_get_argument(i);
320     int res = 1;
321 
322     if (!TEST_ptr(s = OPENSSL_zalloc(sizeof(*s))))
323         return 0;
324     if (!test_start_file(s, testfile)) {
325         OPENSSL_free(s);
326         return 0;
327     }
328 
329     while (!BIO_eof(s->fp)) {
330         if (!test_readstanza(s)) {
331             res = 0;
332             goto end;
333         }
334         if (s->numpairs != 0)
335             if (!param_conversion_load_stanza(&pc, s)
336                 || !param_conversion_test(&pc, s->curr))
337                 res = 0;
338         test_clearstanza(s);
339     }
340 end:
341     test_end_file(s);
342     OPENSSL_free(s);
343     return res;
344 }
345 
346 #endif /* OPENSSL_NO_INTTYPES_H */
347 
348 OPT_TEST_DECLARE_USAGE("file...\n")
349 
350 int setup_tests(void)
351 {
352     size_t n;
353 
354     if (!test_skip_common_options()) {
355         TEST_error("Error parsing test options\n");
356         return 0;
357     }
358 
359     n = test_get_argument_count();
360     if (n == 0)
361         return 0;
362 
363 #if !defined(OPENSSL_NO_INTTYPES_H)
364     ADD_ALL_TESTS(run_param_file_tests, n);
365 #endif /* OPENSSL_NO_INTTYPES_H */
366 
367     return 1;
368 }
369