xref: /freebsd-src/crypto/openssl/test/params_test.c (revision e0c4386e7e71d93b0edc0c8fa156263fc4a8b0b6)
1*e0c4386eSCy Schubert /*
2*e0c4386eSCy Schubert  * Copyright 2019-2024 The OpenSSL Project Authors. All Rights Reserved.
3*e0c4386eSCy Schubert  *
4*e0c4386eSCy Schubert  * Licensed under the Apache License 2.0 (the "License");
5*e0c4386eSCy Schubert  * you may not use this file except in compliance with the License.
6*e0c4386eSCy Schubert  * You may obtain a copy of the License at
7*e0c4386eSCy Schubert  * https://www.openssl.org/source/license.html
8*e0c4386eSCy Schubert  * or in the file LICENSE in the source distribution.
9*e0c4386eSCy Schubert  */
10*e0c4386eSCy Schubert 
11*e0c4386eSCy Schubert /*
12*e0c4386eSCy Schubert  * This program tests the use of OSSL_PARAM, currently in raw form.
13*e0c4386eSCy Schubert  */
14*e0c4386eSCy Schubert 
15*e0c4386eSCy Schubert #include <string.h>
16*e0c4386eSCy Schubert #include <openssl/bn.h>
17*e0c4386eSCy Schubert #include <openssl/core.h>
18*e0c4386eSCy Schubert #include <openssl/err.h>
19*e0c4386eSCy Schubert #include <openssl/params.h>
20*e0c4386eSCy Schubert #include "internal/numbers.h"
21*e0c4386eSCy Schubert #include "internal/nelem.h"
22*e0c4386eSCy Schubert #include "testutil.h"
23*e0c4386eSCy Schubert 
24*e0c4386eSCy Schubert /*-
25*e0c4386eSCy Schubert  * PROVIDER SECTION
26*e0c4386eSCy Schubert  * ================
27*e0c4386eSCy Schubert  *
28*e0c4386eSCy Schubert  * Even though it's not necessarily ONLY providers doing this part,
29*e0c4386eSCy Schubert  * they are naturally going to be the most common users of
30*e0c4386eSCy Schubert  * set_params and get_params functions.
31*e0c4386eSCy Schubert  */
32*e0c4386eSCy Schubert 
33*e0c4386eSCy Schubert /*
34*e0c4386eSCy Schubert  * In real use cases, setters and getters would take an object with
35*e0c4386eSCy Schubert  * which the parameters are associated.  This structure is a cheap
36*e0c4386eSCy Schubert  * simulation.
37*e0c4386eSCy Schubert  */
38*e0c4386eSCy Schubert struct object_st {
39*e0c4386eSCy Schubert     /*
40*e0c4386eSCy Schubert      * Documented as a native integer, of the size given by sizeof(int).
41*e0c4386eSCy Schubert      * Assumed data type OSSL_PARAM_INTEGER
42*e0c4386eSCy Schubert      */
43*e0c4386eSCy Schubert     int p1;
44*e0c4386eSCy Schubert     /*
45*e0c4386eSCy Schubert      * Documented as a native double, of the size given by sizeof(double).
46*e0c4386eSCy Schubert      * Assumed data type OSSL_PARAM_REAL
47*e0c4386eSCy Schubert      */
48*e0c4386eSCy Schubert     double p2;
49*e0c4386eSCy Schubert     /*
50*e0c4386eSCy Schubert      * Documented as an arbitrarly large unsigned integer.
51*e0c4386eSCy Schubert      * The data size must be large enough to accommodate.
52*e0c4386eSCy Schubert      * Assumed data type OSSL_PARAM_UNSIGNED_INTEGER
53*e0c4386eSCy Schubert      */
54*e0c4386eSCy Schubert     BIGNUM *p3;
55*e0c4386eSCy Schubert     /*
56*e0c4386eSCy Schubert      * Documented as a C string.
57*e0c4386eSCy Schubert      * The data size must be large enough to accommodate.
58*e0c4386eSCy Schubert      * Assumed data type OSSL_PARAM_UTF8_STRING
59*e0c4386eSCy Schubert      */
60*e0c4386eSCy Schubert     char *p4;
61*e0c4386eSCy Schubert     size_t p4_l;
62*e0c4386eSCy Schubert     /*
63*e0c4386eSCy Schubert      * Documented as a C string.
64*e0c4386eSCy Schubert      * Assumed data type OSSL_PARAM_UTF8_STRING
65*e0c4386eSCy Schubert      */
66*e0c4386eSCy Schubert     char p5[256];
67*e0c4386eSCy Schubert     size_t p5_l;
68*e0c4386eSCy Schubert     /*
69*e0c4386eSCy Schubert      * Documented as a pointer to a constant C string.
70*e0c4386eSCy Schubert      * Assumed data type OSSL_PARAM_UTF8_PTR
71*e0c4386eSCy Schubert      */
72*e0c4386eSCy Schubert     const char *p6;
73*e0c4386eSCy Schubert     size_t p6_l;
74*e0c4386eSCy Schubert };
75*e0c4386eSCy Schubert 
76*e0c4386eSCy Schubert #define p1_init 42                              /* The ultimate answer */
77*e0c4386eSCy Schubert #define p2_init 6.283                           /* Magic number */
78*e0c4386eSCy Schubert /* Stolen from evp_data, BLAKE2s256 test */
79*e0c4386eSCy Schubert #define p3_init                                 \
80*e0c4386eSCy Schubert     "4142434445464748494a4b4c4d4e4f50"          \
81*e0c4386eSCy Schubert     "5152535455565758595a616263646566"          \
82*e0c4386eSCy Schubert     "6768696a6b6c6d6e6f70717273747576"          \
83*e0c4386eSCy Schubert     "7778797a30313233343536373839"
84*e0c4386eSCy Schubert #define p4_init "BLAKE2s256"                    /* Random string */
85*e0c4386eSCy Schubert #define p5_init "Hellow World"                  /* Random string */
86*e0c4386eSCy Schubert #define p6_init OPENSSL_FULL_VERSION_STR        /* Static string */
87*e0c4386eSCy Schubert 
cleanup_object(void * vobj)88*e0c4386eSCy Schubert static void cleanup_object(void *vobj)
89*e0c4386eSCy Schubert {
90*e0c4386eSCy Schubert     struct object_st *obj = vobj;
91*e0c4386eSCy Schubert 
92*e0c4386eSCy Schubert     BN_free(obj->p3);
93*e0c4386eSCy Schubert     obj->p3 = NULL;
94*e0c4386eSCy Schubert     OPENSSL_free(obj->p4);
95*e0c4386eSCy Schubert     obj->p4 = NULL;
96*e0c4386eSCy Schubert     OPENSSL_free(obj);
97*e0c4386eSCy Schubert }
98*e0c4386eSCy Schubert 
init_object(void)99*e0c4386eSCy Schubert static void *init_object(void)
100*e0c4386eSCy Schubert {
101*e0c4386eSCy Schubert     struct object_st *obj;
102*e0c4386eSCy Schubert 
103*e0c4386eSCy Schubert     if (!TEST_ptr(obj = OPENSSL_zalloc(sizeof(*obj))))
104*e0c4386eSCy Schubert         return NULL;
105*e0c4386eSCy Schubert 
106*e0c4386eSCy Schubert     obj->p1 = p1_init;
107*e0c4386eSCy Schubert     obj->p2 = p2_init;
108*e0c4386eSCy Schubert     if (!TEST_true(BN_hex2bn(&obj->p3, p3_init)))
109*e0c4386eSCy Schubert         goto fail;
110*e0c4386eSCy Schubert     if (!TEST_ptr(obj->p4 = OPENSSL_strdup(p4_init)))
111*e0c4386eSCy Schubert         goto fail;
112*e0c4386eSCy Schubert     strcpy(obj->p5, p5_init);
113*e0c4386eSCy Schubert     obj->p6 = p6_init;
114*e0c4386eSCy Schubert 
115*e0c4386eSCy Schubert     return obj;
116*e0c4386eSCy Schubert  fail:
117*e0c4386eSCy Schubert     cleanup_object(obj);
118*e0c4386eSCy Schubert     obj = NULL;
119*e0c4386eSCy Schubert 
120*e0c4386eSCy Schubert     return NULL;
121*e0c4386eSCy Schubert }
122*e0c4386eSCy Schubert 
123*e0c4386eSCy Schubert /*
124*e0c4386eSCy Schubert  * RAW provider, which handles the parameters in a very raw manner,
125*e0c4386eSCy Schubert  * with no fancy API and very minimal checking.  The application that
126*e0c4386eSCy Schubert  * calls these to set or request parameters MUST get its OSSL_PARAM
127*e0c4386eSCy Schubert  * array right.
128*e0c4386eSCy Schubert  */
129*e0c4386eSCy Schubert 
raw_set_params(void * vobj,const OSSL_PARAM * params)130*e0c4386eSCy Schubert static int raw_set_params(void *vobj, const OSSL_PARAM *params)
131*e0c4386eSCy Schubert {
132*e0c4386eSCy Schubert     struct object_st *obj = vobj;
133*e0c4386eSCy Schubert 
134*e0c4386eSCy Schubert     for (; params->key != NULL; params++)
135*e0c4386eSCy Schubert         if (strcmp(params->key, "p1") == 0) {
136*e0c4386eSCy Schubert             obj->p1 = *(int *)params->data;
137*e0c4386eSCy Schubert         } else if (strcmp(params->key, "p2") == 0) {
138*e0c4386eSCy Schubert             obj->p2 = *(double *)params->data;
139*e0c4386eSCy Schubert         } else if (strcmp(params->key, "p3") == 0) {
140*e0c4386eSCy Schubert             BN_free(obj->p3);
141*e0c4386eSCy Schubert             if (!TEST_ptr(obj->p3 = BN_native2bn(params->data,
142*e0c4386eSCy Schubert                                                  params->data_size, NULL)))
143*e0c4386eSCy Schubert                 return 0;
144*e0c4386eSCy Schubert         } else if (strcmp(params->key, "p4") == 0) {
145*e0c4386eSCy Schubert             OPENSSL_free(obj->p4);
146*e0c4386eSCy Schubert             if (!TEST_ptr(obj->p4 = OPENSSL_strndup(params->data,
147*e0c4386eSCy Schubert                                                     params->data_size)))
148*e0c4386eSCy Schubert                 return 0;
149*e0c4386eSCy Schubert             obj->p4_l = strlen(obj->p4);
150*e0c4386eSCy Schubert         } else if (strcmp(params->key, "p5") == 0) {
151*e0c4386eSCy Schubert             /*
152*e0c4386eSCy Schubert              * Protect obj->p5 against too much data.  This should not
153*e0c4386eSCy Schubert              * happen, we don't use that long strings.
154*e0c4386eSCy Schubert              */
155*e0c4386eSCy Schubert             size_t data_length =
156*e0c4386eSCy Schubert                 OPENSSL_strnlen(params->data, params->data_size);
157*e0c4386eSCy Schubert 
158*e0c4386eSCy Schubert             if (!TEST_size_t_lt(data_length, sizeof(obj->p5)))
159*e0c4386eSCy Schubert                 return 0;
160*e0c4386eSCy Schubert             strncpy(obj->p5, params->data, data_length);
161*e0c4386eSCy Schubert             obj->p5[data_length] = '\0';
162*e0c4386eSCy Schubert             obj->p5_l = strlen(obj->p5);
163*e0c4386eSCy Schubert         } else if (strcmp(params->key, "p6") == 0) {
164*e0c4386eSCy Schubert             obj->p6 = *(const char **)params->data;
165*e0c4386eSCy Schubert             obj->p6_l = params->data_size;
166*e0c4386eSCy Schubert         }
167*e0c4386eSCy Schubert 
168*e0c4386eSCy Schubert     return 1;
169*e0c4386eSCy Schubert }
170*e0c4386eSCy Schubert 
raw_get_params(void * vobj,OSSL_PARAM * params)171*e0c4386eSCy Schubert static int raw_get_params(void *vobj, OSSL_PARAM *params)
172*e0c4386eSCy Schubert {
173*e0c4386eSCy Schubert     struct object_st *obj = vobj;
174*e0c4386eSCy Schubert 
175*e0c4386eSCy Schubert     for (; params->key != NULL; params++)
176*e0c4386eSCy Schubert         if (strcmp(params->key, "p1") == 0) {
177*e0c4386eSCy Schubert             params->return_size = sizeof(obj->p1);
178*e0c4386eSCy Schubert             *(int *)params->data = obj->p1;
179*e0c4386eSCy Schubert         } else if (strcmp(params->key, "p2") == 0) {
180*e0c4386eSCy Schubert             params->return_size = sizeof(obj->p2);
181*e0c4386eSCy Schubert             *(double *)params->data = obj->p2;
182*e0c4386eSCy Schubert         } else if (strcmp(params->key, "p3") == 0) {
183*e0c4386eSCy Schubert             params->return_size = BN_num_bytes(obj->p3);
184*e0c4386eSCy Schubert             if (!TEST_size_t_ge(params->data_size, params->return_size))
185*e0c4386eSCy Schubert                 return 0;
186*e0c4386eSCy Schubert             BN_bn2nativepad(obj->p3, params->data, params->return_size);
187*e0c4386eSCy Schubert         } else if (strcmp(params->key, "p4") == 0) {
188*e0c4386eSCy Schubert             params->return_size = strlen(obj->p4);
189*e0c4386eSCy Schubert             if (!TEST_size_t_gt(params->data_size, params->return_size))
190*e0c4386eSCy Schubert                 return 0;
191*e0c4386eSCy Schubert             strcpy(params->data, obj->p4);
192*e0c4386eSCy Schubert         } else if (strcmp(params->key, "p5") == 0) {
193*e0c4386eSCy Schubert             params->return_size = strlen(obj->p5);
194*e0c4386eSCy Schubert             if (!TEST_size_t_gt(params->data_size, params->return_size))
195*e0c4386eSCy Schubert                 return 0;
196*e0c4386eSCy Schubert             strcpy(params->data, obj->p5);
197*e0c4386eSCy Schubert         } else if (strcmp(params->key, "p6") == 0) {
198*e0c4386eSCy Schubert             params->return_size = strlen(obj->p6);
199*e0c4386eSCy Schubert             *(const char **)params->data = obj->p6;
200*e0c4386eSCy Schubert         }
201*e0c4386eSCy Schubert 
202*e0c4386eSCy Schubert     return 1;
203*e0c4386eSCy Schubert }
204*e0c4386eSCy Schubert 
205*e0c4386eSCy Schubert /*
206*e0c4386eSCy Schubert  * API provider, which handles the parameters using the API from params.h
207*e0c4386eSCy Schubert  */
208*e0c4386eSCy Schubert 
api_set_params(void * vobj,const OSSL_PARAM * params)209*e0c4386eSCy Schubert static int api_set_params(void *vobj, const OSSL_PARAM *params)
210*e0c4386eSCy Schubert {
211*e0c4386eSCy Schubert     struct object_st *obj = vobj;
212*e0c4386eSCy Schubert     const OSSL_PARAM *p = NULL;
213*e0c4386eSCy Schubert 
214*e0c4386eSCy Schubert     if ((p = OSSL_PARAM_locate_const(params, "p1")) != NULL
215*e0c4386eSCy Schubert         && !TEST_true(OSSL_PARAM_get_int(p, &obj->p1)))
216*e0c4386eSCy Schubert         return 0;
217*e0c4386eSCy Schubert     if ((p = OSSL_PARAM_locate_const(params, "p2")) != NULL
218*e0c4386eSCy Schubert         && !TEST_true(OSSL_PARAM_get_double(p, &obj->p2)))
219*e0c4386eSCy Schubert         return 0;
220*e0c4386eSCy Schubert     if ((p = OSSL_PARAM_locate_const(params, "p3")) != NULL
221*e0c4386eSCy Schubert         && !TEST_true(OSSL_PARAM_get_BN(p, &obj->p3)))
222*e0c4386eSCy Schubert         return 0;
223*e0c4386eSCy Schubert     if ((p = OSSL_PARAM_locate_const(params, "p4")) != NULL) {
224*e0c4386eSCy Schubert         OPENSSL_free(obj->p4);
225*e0c4386eSCy Schubert         obj->p4 = NULL;
226*e0c4386eSCy Schubert         /* If the value pointer is NULL, we get it automatically allocated */
227*e0c4386eSCy Schubert         if (!TEST_true(OSSL_PARAM_get_utf8_string(p, &obj->p4, 0)))
228*e0c4386eSCy Schubert             return 0;
229*e0c4386eSCy Schubert     }
230*e0c4386eSCy Schubert     if ((p = OSSL_PARAM_locate_const(params, "p5")) != NULL) {
231*e0c4386eSCy Schubert         char *p5_ptr = obj->p5;
232*e0c4386eSCy Schubert         if (!TEST_true(OSSL_PARAM_get_utf8_string(p, &p5_ptr, sizeof(obj->p5))))
233*e0c4386eSCy Schubert             return 0;
234*e0c4386eSCy Schubert         obj->p5_l = strlen(obj->p5);
235*e0c4386eSCy Schubert     }
236*e0c4386eSCy Schubert     if ((p = OSSL_PARAM_locate_const(params, "p6")) != NULL) {
237*e0c4386eSCy Schubert         if (!TEST_true(OSSL_PARAM_get_utf8_ptr(p, &obj->p6)))
238*e0c4386eSCy Schubert             return 0;
239*e0c4386eSCy Schubert         obj->p6_l = strlen(obj->p6);
240*e0c4386eSCy Schubert     }
241*e0c4386eSCy Schubert 
242*e0c4386eSCy Schubert     return 1;
243*e0c4386eSCy Schubert }
244*e0c4386eSCy Schubert 
api_get_params(void * vobj,OSSL_PARAM * params)245*e0c4386eSCy Schubert static int api_get_params(void *vobj, OSSL_PARAM *params)
246*e0c4386eSCy Schubert {
247*e0c4386eSCy Schubert     struct object_st *obj = vobj;
248*e0c4386eSCy Schubert     OSSL_PARAM *p = NULL;
249*e0c4386eSCy Schubert 
250*e0c4386eSCy Schubert     if ((p = OSSL_PARAM_locate(params, "p1")) != NULL
251*e0c4386eSCy Schubert         && !TEST_true(OSSL_PARAM_set_int(p, obj->p1)))
252*e0c4386eSCy Schubert         return 0;
253*e0c4386eSCy Schubert     if ((p = OSSL_PARAM_locate(params, "p2")) != NULL
254*e0c4386eSCy Schubert         && !TEST_true(OSSL_PARAM_set_double(p, obj->p2)))
255*e0c4386eSCy Schubert         return 0;
256*e0c4386eSCy Schubert     if ((p = OSSL_PARAM_locate(params, "p3")) != NULL
257*e0c4386eSCy Schubert         && !TEST_true(OSSL_PARAM_set_BN(p, obj->p3)))
258*e0c4386eSCy Schubert         return 0;
259*e0c4386eSCy Schubert     if ((p = OSSL_PARAM_locate(params, "p4")) != NULL
260*e0c4386eSCy Schubert         && !TEST_true(OSSL_PARAM_set_utf8_string(p, obj->p4)))
261*e0c4386eSCy Schubert         return 0;
262*e0c4386eSCy Schubert     if ((p = OSSL_PARAM_locate(params, "p5")) != NULL
263*e0c4386eSCy Schubert         && !TEST_true(OSSL_PARAM_set_utf8_string(p, obj->p5)))
264*e0c4386eSCy Schubert         return 0;
265*e0c4386eSCy Schubert     if ((p = OSSL_PARAM_locate(params, "p6")) != NULL
266*e0c4386eSCy Schubert         && !TEST_true(OSSL_PARAM_set_utf8_ptr(p, obj->p6)))
267*e0c4386eSCy Schubert         return 0;
268*e0c4386eSCy Schubert 
269*e0c4386eSCy Schubert     return 1;
270*e0c4386eSCy Schubert }
271*e0c4386eSCy Schubert 
272*e0c4386eSCy Schubert /*
273*e0c4386eSCy Schubert  * This structure only simulates a provider dispatch, the real deal is
274*e0c4386eSCy Schubert  * a bit more code that's not necessary in these tests.
275*e0c4386eSCy Schubert  */
276*e0c4386eSCy Schubert struct provider_dispatch_st {
277*e0c4386eSCy Schubert     int (*set_params)(void *obj, const OSSL_PARAM *params);
278*e0c4386eSCy Schubert     int (*get_params)(void *obj, OSSL_PARAM *params);
279*e0c4386eSCy Schubert };
280*e0c4386eSCy Schubert 
281*e0c4386eSCy Schubert /* "raw" provider */
282*e0c4386eSCy Schubert static const struct provider_dispatch_st provider_raw = {
283*e0c4386eSCy Schubert     raw_set_params, raw_get_params
284*e0c4386eSCy Schubert };
285*e0c4386eSCy Schubert 
286*e0c4386eSCy Schubert /* "api" provider */
287*e0c4386eSCy Schubert static const struct provider_dispatch_st provider_api = {
288*e0c4386eSCy Schubert     api_set_params, api_get_params
289*e0c4386eSCy Schubert };
290*e0c4386eSCy Schubert 
291*e0c4386eSCy Schubert /*-
292*e0c4386eSCy Schubert  * APPLICATION SECTION
293*e0c4386eSCy Schubert  * ===================
294*e0c4386eSCy Schubert  */
295*e0c4386eSCy Schubert 
296*e0c4386eSCy Schubert /* In all our tests, these are variables that get manipulated as parameters
297*e0c4386eSCy Schubert  *
298*e0c4386eSCy Schubert  * These arrays consistently do nothing with the "p2" parameter, and
299*e0c4386eSCy Schubert  * always include a "foo" parameter.  This is to check that the
300*e0c4386eSCy Schubert  * set_params and get_params calls ignore the lack of parameters that
301*e0c4386eSCy Schubert  * the application isn't interested in, as well as ignore parameters
302*e0c4386eSCy Schubert  * they don't understand (the application may have one big bag of
303*e0c4386eSCy Schubert  * parameters).
304*e0c4386eSCy Schubert  */
305*e0c4386eSCy Schubert static int app_p1;                    /* "p1" */
306*e0c4386eSCy Schubert static double app_p2;                 /* "p2" is ignored */
307*e0c4386eSCy Schubert static BIGNUM *app_p3 = NULL;         /* "p3" */
308*e0c4386eSCy Schubert static unsigned char bignumbin[4096]; /* "p3" */
309*e0c4386eSCy Schubert static char app_p4[256];              /* "p4" */
310*e0c4386eSCy Schubert static char app_p5[256];              /* "p5" */
311*e0c4386eSCy Schubert static const char *app_p6 = NULL;     /* "p6" */
312*e0c4386eSCy Schubert static unsigned char foo[1];          /* "foo" */
313*e0c4386eSCy Schubert 
314*e0c4386eSCy Schubert #define app_p1_init 17           /* A random number */
315*e0c4386eSCy Schubert #define app_p2_init 47.11        /* Another random number */
316*e0c4386eSCy Schubert #define app_p3_init "deadbeef"   /* Classic */
317*e0c4386eSCy Schubert #define app_p4_init "Hello"
318*e0c4386eSCy Schubert #define app_p5_init "World"
319*e0c4386eSCy Schubert #define app_p6_init "Cookie"
320*e0c4386eSCy Schubert #define app_foo_init 'z'
321*e0c4386eSCy Schubert 
cleanup_app_variables(void)322*e0c4386eSCy Schubert static int cleanup_app_variables(void)
323*e0c4386eSCy Schubert {
324*e0c4386eSCy Schubert     BN_free(app_p3);
325*e0c4386eSCy Schubert     app_p3 = NULL;
326*e0c4386eSCy Schubert     return 1;
327*e0c4386eSCy Schubert }
328*e0c4386eSCy Schubert 
init_app_variables(void)329*e0c4386eSCy Schubert static int init_app_variables(void)
330*e0c4386eSCy Schubert {
331*e0c4386eSCy Schubert     int l = 0;
332*e0c4386eSCy Schubert 
333*e0c4386eSCy Schubert     cleanup_app_variables();
334*e0c4386eSCy Schubert 
335*e0c4386eSCy Schubert     app_p1 = app_p1_init;
336*e0c4386eSCy Schubert     app_p2 = app_p2_init;
337*e0c4386eSCy Schubert     if (!BN_hex2bn(&app_p3, app_p3_init)
338*e0c4386eSCy Schubert         || (l = BN_bn2nativepad(app_p3, bignumbin, sizeof(bignumbin))) < 0)
339*e0c4386eSCy Schubert         return 0;
340*e0c4386eSCy Schubert     strcpy(app_p4, app_p4_init);
341*e0c4386eSCy Schubert     strcpy(app_p5, app_p5_init);
342*e0c4386eSCy Schubert     app_p6 = app_p6_init;
343*e0c4386eSCy Schubert     foo[0] = app_foo_init;
344*e0c4386eSCy Schubert 
345*e0c4386eSCy Schubert     return 1;
346*e0c4386eSCy Schubert }
347*e0c4386eSCy Schubert 
348*e0c4386eSCy Schubert /*
349*e0c4386eSCy Schubert  * Here, we define test OSSL_PARAM arrays
350*e0c4386eSCy Schubert  */
351*e0c4386eSCy Schubert 
352*e0c4386eSCy Schubert /* An array of OSSL_PARAM, specific in the most raw manner possible */
353*e0c4386eSCy Schubert static OSSL_PARAM static_raw_params[] = {
354*e0c4386eSCy Schubert     { "p1", OSSL_PARAM_INTEGER, &app_p1, sizeof(app_p1), 0 },
355*e0c4386eSCy Schubert     { "p3", OSSL_PARAM_UNSIGNED_INTEGER, &bignumbin, sizeof(bignumbin), 0 },
356*e0c4386eSCy Schubert     { "p4", OSSL_PARAM_UTF8_STRING, &app_p4, sizeof(app_p4), 0 },
357*e0c4386eSCy Schubert     { "p5", OSSL_PARAM_UTF8_STRING, &app_p5, sizeof(app_p5), 0 },
358*e0c4386eSCy Schubert     /* sizeof(app_p6_init) - 1, because we know that's what we're using */
359*e0c4386eSCy Schubert     { "p6", OSSL_PARAM_UTF8_PTR, &app_p6, sizeof(app_p6_init) - 1, 0 },
360*e0c4386eSCy Schubert     { "foo", OSSL_PARAM_OCTET_STRING, &foo, sizeof(foo), 0 },
361*e0c4386eSCy Schubert     { NULL, 0, NULL, 0, 0 }
362*e0c4386eSCy Schubert };
363*e0c4386eSCy Schubert 
364*e0c4386eSCy Schubert /* The same array of OSSL_PARAM, specified with the macros from params.h */
365*e0c4386eSCy Schubert static OSSL_PARAM static_api_params[] = {
366*e0c4386eSCy Schubert     OSSL_PARAM_int("p1", &app_p1),
367*e0c4386eSCy Schubert     OSSL_PARAM_BN("p3", &bignumbin, sizeof(bignumbin)),
368*e0c4386eSCy Schubert     OSSL_PARAM_DEFN("p4", OSSL_PARAM_UTF8_STRING, &app_p4, sizeof(app_p4)),
369*e0c4386eSCy Schubert     OSSL_PARAM_DEFN("p5", OSSL_PARAM_UTF8_STRING, &app_p5, sizeof(app_p5)),
370*e0c4386eSCy Schubert     /* sizeof(app_p6_init), because we know that's what we're using */
371*e0c4386eSCy Schubert     OSSL_PARAM_DEFN("p6", OSSL_PARAM_UTF8_PTR, &app_p6,
372*e0c4386eSCy Schubert                     sizeof(app_p6_init) - 1),
373*e0c4386eSCy Schubert     OSSL_PARAM_DEFN("foo", OSSL_PARAM_OCTET_STRING, &foo, sizeof(foo)),
374*e0c4386eSCy Schubert     OSSL_PARAM_END
375*e0c4386eSCy Schubert };
376*e0c4386eSCy Schubert 
377*e0c4386eSCy Schubert /*
378*e0c4386eSCy Schubert  * The same array again, but constructed at run-time
379*e0c4386eSCy Schubert  * This exercises the OSSL_PARAM constructor functions
380*e0c4386eSCy Schubert  */
construct_api_params(void)381*e0c4386eSCy Schubert static OSSL_PARAM *construct_api_params(void)
382*e0c4386eSCy Schubert {
383*e0c4386eSCy Schubert     size_t n = 0;
384*e0c4386eSCy Schubert     static OSSL_PARAM params[10];
385*e0c4386eSCy Schubert 
386*e0c4386eSCy Schubert     params[n++] = OSSL_PARAM_construct_int("p1", &app_p1);
387*e0c4386eSCy Schubert     params[n++] = OSSL_PARAM_construct_BN("p3", bignumbin, sizeof(bignumbin));
388*e0c4386eSCy Schubert     params[n++] = OSSL_PARAM_construct_utf8_string("p4", app_p4,
389*e0c4386eSCy Schubert                                                    sizeof(app_p4));
390*e0c4386eSCy Schubert     params[n++] = OSSL_PARAM_construct_utf8_string("p5", app_p5,
391*e0c4386eSCy Schubert                                                    sizeof(app_p5));
392*e0c4386eSCy Schubert     /* sizeof(app_p6_init), because we know that's what we're using */
393*e0c4386eSCy Schubert     params[n++] = OSSL_PARAM_construct_utf8_ptr("p6", (char **)&app_p6,
394*e0c4386eSCy Schubert                                                 sizeof(app_p6_init));
395*e0c4386eSCy Schubert     params[n++] = OSSL_PARAM_construct_octet_string("foo", &foo, sizeof(foo));
396*e0c4386eSCy Schubert     params[n++] = OSSL_PARAM_construct_end();
397*e0c4386eSCy Schubert 
398*e0c4386eSCy Schubert     return params;
399*e0c4386eSCy Schubert }
400*e0c4386eSCy Schubert 
401*e0c4386eSCy Schubert struct param_owner_st {
402*e0c4386eSCy Schubert     OSSL_PARAM *static_params;
403*e0c4386eSCy Schubert     OSSL_PARAM *(*constructed_params)(void);
404*e0c4386eSCy Schubert };
405*e0c4386eSCy Schubert 
406*e0c4386eSCy Schubert static const struct param_owner_st raw_params = {
407*e0c4386eSCy Schubert     static_raw_params, NULL
408*e0c4386eSCy Schubert };
409*e0c4386eSCy Schubert 
410*e0c4386eSCy Schubert static const struct param_owner_st api_params = {
411*e0c4386eSCy Schubert     static_api_params, construct_api_params
412*e0c4386eSCy Schubert };
413*e0c4386eSCy Schubert 
414*e0c4386eSCy Schubert /*-
415*e0c4386eSCy Schubert  * TESTING
416*e0c4386eSCy Schubert  * =======
417*e0c4386eSCy Schubert  */
418*e0c4386eSCy Schubert 
419*e0c4386eSCy Schubert /*
420*e0c4386eSCy Schubert  * Test cases to combine parameters with "provider side" functions
421*e0c4386eSCy Schubert  */
422*e0c4386eSCy Schubert static struct {
423*e0c4386eSCy Schubert     const struct provider_dispatch_st *prov;
424*e0c4386eSCy Schubert     const struct param_owner_st *app;
425*e0c4386eSCy Schubert     const char *desc;
426*e0c4386eSCy Schubert } test_cases[] = {
427*e0c4386eSCy Schubert     /* Tests within specific methods */
428*e0c4386eSCy Schubert     { &provider_raw, &raw_params, "raw provider vs raw params" },
429*e0c4386eSCy Schubert     { &provider_api, &api_params, "api provider vs api params" },
430*e0c4386eSCy Schubert 
431*e0c4386eSCy Schubert     /* Mixed methods */
432*e0c4386eSCy Schubert     { &provider_raw, &api_params, "raw provider vs api params" },
433*e0c4386eSCy Schubert     { &provider_api, &raw_params, "api provider vs raw params" },
434*e0c4386eSCy Schubert };
435*e0c4386eSCy Schubert 
436*e0c4386eSCy Schubert /* Generic tester of combinations of "providers" and params */
test_case_variant(OSSL_PARAM * params,const struct provider_dispatch_st * prov)437*e0c4386eSCy Schubert static int test_case_variant(OSSL_PARAM *params, const struct provider_dispatch_st *prov)
438*e0c4386eSCy Schubert {
439*e0c4386eSCy Schubert     BIGNUM *verify_p3 = NULL;
440*e0c4386eSCy Schubert     void *obj = NULL;
441*e0c4386eSCy Schubert     int errcnt = 0;
442*e0c4386eSCy Schubert     OSSL_PARAM *p;
443*e0c4386eSCy Schubert 
444*e0c4386eSCy Schubert     /*
445*e0c4386eSCy Schubert      * Initialize
446*e0c4386eSCy Schubert      */
447*e0c4386eSCy Schubert     if (!TEST_ptr(obj = init_object())
448*e0c4386eSCy Schubert         || !TEST_true(BN_hex2bn(&verify_p3, p3_init))) {
449*e0c4386eSCy Schubert         errcnt++;
450*e0c4386eSCy Schubert         goto fin;
451*e0c4386eSCy Schubert     }
452*e0c4386eSCy Schubert 
453*e0c4386eSCy Schubert     /*
454*e0c4386eSCy Schubert      * Get parameters a first time, just to see that getting works and
455*e0c4386eSCy Schubert      * gets us the values we expect.
456*e0c4386eSCy Schubert      */
457*e0c4386eSCy Schubert     init_app_variables();
458*e0c4386eSCy Schubert 
459*e0c4386eSCy Schubert     if (!TEST_true(prov->get_params(obj, params))
460*e0c4386eSCy Schubert         || !TEST_int_eq(app_p1, p1_init)        /* "provider" value */
461*e0c4386eSCy Schubert         || !TEST_double_eq(app_p2, app_p2_init) /* Should remain untouched */
462*e0c4386eSCy Schubert         || !TEST_ptr(p = OSSL_PARAM_locate(params, "p3"))
463*e0c4386eSCy Schubert         || !TEST_ptr(BN_native2bn(bignumbin, p->return_size, app_p3))
464*e0c4386eSCy Schubert         || !TEST_BN_eq(app_p3, verify_p3)       /* "provider" value */
465*e0c4386eSCy Schubert         || !TEST_str_eq(app_p4, p4_init)        /* "provider" value */
466*e0c4386eSCy Schubert         || !TEST_ptr(p = OSSL_PARAM_locate(params, "p5"))
467*e0c4386eSCy Schubert         || !TEST_size_t_eq(p->return_size,
468*e0c4386eSCy Schubert                            sizeof(p5_init) - 1) /* "provider" value */
469*e0c4386eSCy Schubert         || !TEST_str_eq(app_p5, p5_init)        /* "provider" value */
470*e0c4386eSCy Schubert         || !TEST_ptr(p = OSSL_PARAM_locate(params, "p6"))
471*e0c4386eSCy Schubert         || !TEST_size_t_eq(p->return_size,
472*e0c4386eSCy Schubert                            sizeof(p6_init) - 1) /* "provider" value */
473*e0c4386eSCy Schubert         || !TEST_str_eq(app_p6, p6_init)        /* "provider" value */
474*e0c4386eSCy Schubert         || !TEST_char_eq(foo[0], app_foo_init)  /* Should remain untouched */
475*e0c4386eSCy Schubert         || !TEST_ptr(p = OSSL_PARAM_locate(params, "foo")))
476*e0c4386eSCy Schubert         errcnt++;
477*e0c4386eSCy Schubert 
478*e0c4386eSCy Schubert     /*
479*e0c4386eSCy Schubert      * Set parameters, then sneak into the object itself and check
480*e0c4386eSCy Schubert      * that its attributes got set (or ignored) properly.
481*e0c4386eSCy Schubert      */
482*e0c4386eSCy Schubert     init_app_variables();
483*e0c4386eSCy Schubert 
484*e0c4386eSCy Schubert     if (!TEST_true(prov->set_params(obj, params))) {
485*e0c4386eSCy Schubert         errcnt++;
486*e0c4386eSCy Schubert     } else {
487*e0c4386eSCy Schubert         struct object_st *sneakpeek = obj;
488*e0c4386eSCy Schubert 
489*e0c4386eSCy Schubert         if (!TEST_int_eq(sneakpeek->p1, app_p1)         /* app value set */
490*e0c4386eSCy Schubert             || !TEST_double_eq(sneakpeek->p2, p2_init)  /* Should remain untouched */
491*e0c4386eSCy Schubert             || !TEST_BN_eq(sneakpeek->p3, app_p3)       /* app value set */
492*e0c4386eSCy Schubert             || !TEST_str_eq(sneakpeek->p4, app_p4)      /* app value set */
493*e0c4386eSCy Schubert             || !TEST_str_eq(sneakpeek->p5, app_p5)      /* app value set */
494*e0c4386eSCy Schubert             || !TEST_str_eq(sneakpeek->p6, app_p6))     /* app value set */
495*e0c4386eSCy Schubert             errcnt++;
496*e0c4386eSCy Schubert     }
497*e0c4386eSCy Schubert 
498*e0c4386eSCy Schubert     /*
499*e0c4386eSCy Schubert      * Get parameters again, checking that we get different values
500*e0c4386eSCy Schubert      * than earlier where relevant.
501*e0c4386eSCy Schubert      */
502*e0c4386eSCy Schubert     BN_free(verify_p3);
503*e0c4386eSCy Schubert     verify_p3 = NULL;
504*e0c4386eSCy Schubert 
505*e0c4386eSCy Schubert     if (!TEST_true(BN_hex2bn(&verify_p3, app_p3_init))) {
506*e0c4386eSCy Schubert         errcnt++;
507*e0c4386eSCy Schubert         goto fin;
508*e0c4386eSCy Schubert     }
509*e0c4386eSCy Schubert 
510*e0c4386eSCy Schubert     if (!TEST_true(prov->get_params(obj, params))
511*e0c4386eSCy Schubert         || !TEST_int_eq(app_p1, app_p1_init)    /* app value */
512*e0c4386eSCy Schubert         || !TEST_double_eq(app_p2, app_p2_init) /* Should remain untouched */
513*e0c4386eSCy Schubert         || !TEST_ptr(p = OSSL_PARAM_locate(params, "p3"))
514*e0c4386eSCy Schubert         || !TEST_ptr(BN_native2bn(bignumbin, p->return_size, app_p3))
515*e0c4386eSCy Schubert         || !TEST_BN_eq(app_p3, verify_p3)       /* app value */
516*e0c4386eSCy Schubert         || !TEST_str_eq(app_p4, app_p4_init)    /* app value */
517*e0c4386eSCy Schubert         || !TEST_ptr(p = OSSL_PARAM_locate(params, "p5"))
518*e0c4386eSCy Schubert         || !TEST_size_t_eq(p->return_size,
519*e0c4386eSCy Schubert                            sizeof(app_p5_init) - 1) /* app value */
520*e0c4386eSCy Schubert         || !TEST_str_eq(app_p5, app_p5_init)    /* app value */
521*e0c4386eSCy Schubert         || !TEST_ptr(p = OSSL_PARAM_locate(params, "p6"))
522*e0c4386eSCy Schubert         || !TEST_size_t_eq(p->return_size,
523*e0c4386eSCy Schubert                            sizeof(app_p6_init) - 1) /* app value */
524*e0c4386eSCy Schubert         || !TEST_str_eq(app_p6, app_p6_init)    /* app value */
525*e0c4386eSCy Schubert         || !TEST_char_eq(foo[0], app_foo_init)  /* Should remain untouched */
526*e0c4386eSCy Schubert         || !TEST_ptr(p = OSSL_PARAM_locate(params, "foo")))
527*e0c4386eSCy Schubert         errcnt++;
528*e0c4386eSCy Schubert 
529*e0c4386eSCy Schubert  fin:
530*e0c4386eSCy Schubert     BN_free(verify_p3);
531*e0c4386eSCy Schubert     verify_p3 = NULL;
532*e0c4386eSCy Schubert     cleanup_app_variables();
533*e0c4386eSCy Schubert     cleanup_object(obj);
534*e0c4386eSCy Schubert 
535*e0c4386eSCy Schubert     return errcnt == 0;
536*e0c4386eSCy Schubert }
537*e0c4386eSCy Schubert 
test_case(int i)538*e0c4386eSCy Schubert static int test_case(int i)
539*e0c4386eSCy Schubert {
540*e0c4386eSCy Schubert     TEST_info("Case: %s", test_cases[i].desc);
541*e0c4386eSCy Schubert 
542*e0c4386eSCy Schubert     return test_case_variant(test_cases[i].app->static_params,
543*e0c4386eSCy Schubert                              test_cases[i].prov)
544*e0c4386eSCy Schubert         && (test_cases[i].app->constructed_params == NULL
545*e0c4386eSCy Schubert             || test_case_variant(test_cases[i].app->constructed_params(),
546*e0c4386eSCy Schubert                                  test_cases[i].prov));
547*e0c4386eSCy Schubert }
548*e0c4386eSCy Schubert 
549*e0c4386eSCy Schubert /*-
550*e0c4386eSCy Schubert  * OSSL_PARAM_allocate_from_text() tests
551*e0c4386eSCy Schubert  * =====================================
552*e0c4386eSCy Schubert  */
553*e0c4386eSCy Schubert 
554*e0c4386eSCy Schubert static const OSSL_PARAM params_from_text[] = {
555*e0c4386eSCy Schubert     /* Fixed size buffer */
556*e0c4386eSCy Schubert     OSSL_PARAM_int32("int", NULL),
557*e0c4386eSCy Schubert     OSSL_PARAM_DEFN("short", OSSL_PARAM_INTEGER, NULL, sizeof(int16_t)),
558*e0c4386eSCy Schubert     OSSL_PARAM_DEFN("ushort", OSSL_PARAM_UNSIGNED_INTEGER, NULL, sizeof(uint16_t)),
559*e0c4386eSCy Schubert     /* Arbitrary size buffer.  Make sure the result fits in a long */
560*e0c4386eSCy Schubert     OSSL_PARAM_DEFN("num", OSSL_PARAM_INTEGER, NULL, 0),
561*e0c4386eSCy Schubert     OSSL_PARAM_DEFN("unum", OSSL_PARAM_UNSIGNED_INTEGER, NULL, 0),
562*e0c4386eSCy Schubert     OSSL_PARAM_DEFN("octets", OSSL_PARAM_OCTET_STRING, NULL, 0),
563*e0c4386eSCy Schubert     OSSL_PARAM_END,
564*e0c4386eSCy Schubert };
565*e0c4386eSCy Schubert 
566*e0c4386eSCy Schubert struct int_from_text_test_st {
567*e0c4386eSCy Schubert     const char *argname;
568*e0c4386eSCy Schubert     const char *strval;
569*e0c4386eSCy Schubert     long int expected_intval;
570*e0c4386eSCy Schubert     int expected_res;
571*e0c4386eSCy Schubert     size_t expected_bufsize;
572*e0c4386eSCy Schubert };
573*e0c4386eSCy Schubert 
574*e0c4386eSCy Schubert static struct int_from_text_test_st int_from_text_test_cases[] = {
575*e0c4386eSCy Schubert     { "int",               "",          0, 0, 0 },
576*e0c4386eSCy Schubert     { "int",              "0",          0, 1, 4 },
577*e0c4386eSCy Schubert     { "int",            "101",        101, 1, 4 },
578*e0c4386eSCy Schubert     { "int",           "-102",       -102, 1, 4 },
579*e0c4386eSCy Schubert     { "int",            "12A",         12, 1, 4 }, /* incomplete */
580*e0c4386eSCy Schubert     { "int",          "0x12B",      0x12B, 1, 4 },
581*e0c4386eSCy Schubert     { "hexint",         "12C",      0x12C, 1, 4 },
582*e0c4386eSCy Schubert     { "hexint",       "0x12D",          0, 1, 4 }, /* zero */
583*e0c4386eSCy Schubert     /* test check of the target buffer size */
584*e0c4386eSCy Schubert     { "int",     "0x7fffffff",  INT32_MAX, 1, 4 },
585*e0c4386eSCy Schubert     { "int",     "2147483647",  INT32_MAX, 1, 4 },
586*e0c4386eSCy Schubert     { "int",     "2147483648",          0, 0, 0 }, /* too small buffer */
587*e0c4386eSCy Schubert     { "int",    "-2147483648",  INT32_MIN, 1, 4 },
588*e0c4386eSCy Schubert     { "int",    "-2147483649",          0, 0, 4 }, /* too small buffer */
589*e0c4386eSCy Schubert     { "short",       "0x7fff",  INT16_MAX, 1, 2 },
590*e0c4386eSCy Schubert     { "short",        "32767",  INT16_MAX, 1, 2 },
591*e0c4386eSCy Schubert     { "short",        "32768",          0, 0, 0 }, /* too small buffer */
592*e0c4386eSCy Schubert     { "ushort",      "0xffff", UINT16_MAX, 1, 2 },
593*e0c4386eSCy Schubert     { "ushort",       "65535", UINT16_MAX, 1, 2 },
594*e0c4386eSCy Schubert     { "ushort",       "65536",          0, 0, 0 }, /* too small buffer */
595*e0c4386eSCy Schubert     /* test check of sign extension in arbitrary size results */
596*e0c4386eSCy Schubert     { "num",              "0",          0, 1, 1 },
597*e0c4386eSCy Schubert     { "num",              "0",          0, 1, 1 },
598*e0c4386eSCy Schubert     { "num",           "0xff",       0xff, 1, 2 }, /* sign extension */
599*e0c4386eSCy Schubert     { "num",          "-0xff",      -0xff, 1, 2 }, /* sign extension */
600*e0c4386eSCy Schubert     { "num",           "0x7f",       0x7f, 1, 1 }, /* no sign extension */
601*e0c4386eSCy Schubert     { "num",          "-0x7f",      -0x7f, 1, 1 }, /* no sign extension */
602*e0c4386eSCy Schubert     { "num",           "0x80",       0x80, 1, 2 }, /* sign extension */
603*e0c4386eSCy Schubert     { "num",          "-0x80",      -0x80, 1, 1 }, /* no sign extension */
604*e0c4386eSCy Schubert     { "num",           "0x81",       0x81, 1, 2 }, /* sign extension */
605*e0c4386eSCy Schubert     { "num",          "-0x81",      -0x81, 1, 2 }, /* sign extension */
606*e0c4386eSCy Schubert     { "unum",          "0xff",       0xff, 1, 1 },
607*e0c4386eSCy Schubert     { "unum",         "-0xff",      -0xff, 0, 0 }, /* invalid neg number */
608*e0c4386eSCy Schubert     { "unum",          "0x7f",       0x7f, 1, 1 },
609*e0c4386eSCy Schubert     { "unum",         "-0x7f",      -0x7f, 0, 0 }, /* invalid neg number */
610*e0c4386eSCy Schubert     { "unum",          "0x80",       0x80, 1, 1 },
611*e0c4386eSCy Schubert     { "unum",         "-0x80",      -0x80, 0, 0 }, /* invalid neg number */
612*e0c4386eSCy Schubert     { "unum",          "0x81",       0x81, 1, 1 },
613*e0c4386eSCy Schubert     { "unum",         "-0x81",      -0x81, 0, 0 }, /* invalid neg number */
614*e0c4386eSCy Schubert };
615*e0c4386eSCy Schubert 
check_int_from_text(const struct int_from_text_test_st a)616*e0c4386eSCy Schubert static int check_int_from_text(const struct int_from_text_test_st a)
617*e0c4386eSCy Schubert {
618*e0c4386eSCy Schubert     OSSL_PARAM param;
619*e0c4386eSCy Schubert     long int val = 0;
620*e0c4386eSCy Schubert     int res;
621*e0c4386eSCy Schubert 
622*e0c4386eSCy Schubert     if (!OSSL_PARAM_allocate_from_text(&param, params_from_text,
623*e0c4386eSCy Schubert                                        a.argname, a.strval, 0, NULL)) {
624*e0c4386eSCy Schubert         if (a.expected_res)
625*e0c4386eSCy Schubert             TEST_error("unexpected OSSL_PARAM_allocate_from_text() return for %s \"%s\"",
626*e0c4386eSCy Schubert                        a.argname, a.strval);
627*e0c4386eSCy Schubert         return !a.expected_res;
628*e0c4386eSCy Schubert     }
629*e0c4386eSCy Schubert 
630*e0c4386eSCy Schubert     /* For data size zero, OSSL_PARAM_get_long() may crash */
631*e0c4386eSCy Schubert     if (param.data_size == 0) {
632*e0c4386eSCy Schubert         OPENSSL_free(param.data);
633*e0c4386eSCy Schubert         TEST_error("unexpected zero size for %s \"%s\"",
634*e0c4386eSCy Schubert                    a.argname, a.strval);
635*e0c4386eSCy Schubert         return 0;
636*e0c4386eSCy Schubert     }
637*e0c4386eSCy Schubert     res = OSSL_PARAM_get_long(&param, &val);
638*e0c4386eSCy Schubert     OPENSSL_free(param.data);
639*e0c4386eSCy Schubert 
640*e0c4386eSCy Schubert     if (res ^ a.expected_res) {
641*e0c4386eSCy Schubert         TEST_error("unexpected OSSL_PARAM_get_long() return for %s \"%s\": "
642*e0c4386eSCy Schubert                    "%d != %d", a.argname, a.strval, a.expected_res, res);
643*e0c4386eSCy Schubert         return 0;
644*e0c4386eSCy Schubert     }
645*e0c4386eSCy Schubert     if (val != a.expected_intval) {
646*e0c4386eSCy Schubert         TEST_error("unexpected result for %s \"%s\":  %li != %li",
647*e0c4386eSCy Schubert                    a.argname, a.strval, a.expected_intval, val);
648*e0c4386eSCy Schubert         return 0;
649*e0c4386eSCy Schubert     }
650*e0c4386eSCy Schubert     if (param.data_size != a.expected_bufsize) {
651*e0c4386eSCy Schubert         TEST_error("unexpected size for %s \"%s\":  %d != %d",
652*e0c4386eSCy Schubert                    a.argname, a.strval,
653*e0c4386eSCy Schubert                    (int)a.expected_bufsize, (int)param.data_size);
654*e0c4386eSCy Schubert         return 0;
655*e0c4386eSCy Schubert     }
656*e0c4386eSCy Schubert 
657*e0c4386eSCy Schubert     return a.expected_res;
658*e0c4386eSCy Schubert }
659*e0c4386eSCy Schubert 
check_octetstr_from_hexstr(void)660*e0c4386eSCy Schubert static int check_octetstr_from_hexstr(void)
661*e0c4386eSCy Schubert {
662*e0c4386eSCy Schubert     OSSL_PARAM param;
663*e0c4386eSCy Schubert     static const char *values[] = { "", "F", "FF", "FFF", "FFFF", NULL };
664*e0c4386eSCy Schubert     int i;
665*e0c4386eSCy Schubert     int errcnt = 0;
666*e0c4386eSCy Schubert 
667*e0c4386eSCy Schubert     /* Test odd vs even number of hex digits */
668*e0c4386eSCy Schubert     for (i = 0; values[i] != NULL; i++) {
669*e0c4386eSCy Schubert         int expected = (strlen(values[i]) % 2) != 1;
670*e0c4386eSCy Schubert         int result;
671*e0c4386eSCy Schubert 
672*e0c4386eSCy Schubert         ERR_clear_error();
673*e0c4386eSCy Schubert         memset(&param, 0, sizeof(param));
674*e0c4386eSCy Schubert         if (expected)
675*e0c4386eSCy Schubert             result =
676*e0c4386eSCy Schubert                 TEST_true(OSSL_PARAM_allocate_from_text(&param,
677*e0c4386eSCy Schubert                                                         params_from_text,
678*e0c4386eSCy Schubert                                                         "hexoctets", values[i], 0,
679*e0c4386eSCy Schubert                                                         NULL));
680*e0c4386eSCy Schubert         else
681*e0c4386eSCy Schubert             result =
682*e0c4386eSCy Schubert                 TEST_false(OSSL_PARAM_allocate_from_text(&param,
683*e0c4386eSCy Schubert                                                          params_from_text,
684*e0c4386eSCy Schubert                                                          "hexoctets", values[i], 0,
685*e0c4386eSCy Schubert                                                          NULL));
686*e0c4386eSCy Schubert         if (!result) {
687*e0c4386eSCy Schubert             TEST_error("unexpected OSSL_PARAM_allocate_from_text() %s for 'octets' \"%s\"",
688*e0c4386eSCy Schubert                        (expected ? "failure" : "success"), values[i]);
689*e0c4386eSCy Schubert             errcnt++;
690*e0c4386eSCy Schubert         }
691*e0c4386eSCy Schubert         OPENSSL_free(param.data);
692*e0c4386eSCy Schubert     }
693*e0c4386eSCy Schubert     return errcnt == 0;
694*e0c4386eSCy Schubert }
695*e0c4386eSCy Schubert 
test_allocate_from_text(int i)696*e0c4386eSCy Schubert static int test_allocate_from_text(int i)
697*e0c4386eSCy Schubert {
698*e0c4386eSCy Schubert     return check_int_from_text(int_from_text_test_cases[i]);
699*e0c4386eSCy Schubert }
700*e0c4386eSCy Schubert 
test_more_allocate_from_text(void)701*e0c4386eSCy Schubert static int test_more_allocate_from_text(void)
702*e0c4386eSCy Schubert {
703*e0c4386eSCy Schubert     return check_octetstr_from_hexstr();
704*e0c4386eSCy Schubert }
705*e0c4386eSCy Schubert 
setup_tests(void)706*e0c4386eSCy Schubert int setup_tests(void)
707*e0c4386eSCy Schubert {
708*e0c4386eSCy Schubert     ADD_ALL_TESTS(test_case, OSSL_NELEM(test_cases));
709*e0c4386eSCy Schubert     ADD_ALL_TESTS(test_allocate_from_text, OSSL_NELEM(int_from_text_test_cases));
710*e0c4386eSCy Schubert     ADD_TEST(test_more_allocate_from_text);
711*e0c4386eSCy Schubert     return 1;
712*e0c4386eSCy Schubert }
713