test/params_test.c: Add API test case, and mixed methods
[oweals/openssl.git] / test / params_test.c
1 /*
2  * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * https://www.openssl.org/source/license.html
8  * or in the file LICENSE in the source distribution.
9  */
10
11 /*
12  * This program tests the use of OSSL_PARAM, currently in raw form.
13  */
14
15 #include <string.h>
16 #include <openssl/bn.h>
17 #include <openssl/core.h>
18 #include <openssl/params.h>
19 #include "internal/nelem.h"
20 #include "testutil.h"
21
22 /*-
23  * PROVIDER SECTION
24  * ================
25  *
26  * Even though it's not necessarily ONLY providers doing this part,
27  * they are naturally going to be the most common users of
28  * set_params and get_params functions.
29  */
30
31 /*
32  * In real use cases, setters and getters would take an object with
33  * which the parameters are associated.  This structure is a cheap
34  * simulation.
35  */
36 struct object_st {
37     /*
38      * Documented as a native integer, of the size given by sizeof(int).
39      * Assumed data type OSSL_PARAM_INTEGER
40      */
41     int p1;
42     /*
43      * Documented as a native double, of the size given by sizeof(double).
44      * Assumed data type OSSL_PARAM_REAL
45      */
46     double p2;
47     /*
48      * Documented as an arbitrarly large unsigned integer.
49      * The data size must be large enough to accomodate.
50      * Assumed data type OSSL_PARAM_UNSIGNED_INTEGER
51      */
52     BIGNUM *p3;
53     /*
54      * Documented as a C string.
55      * The data size must be large enough to accomodate.
56      * Assumed data type OSSL_PARAM_UTF8_STRING
57      */
58     char *p4;
59     size_t p4_l;
60     /*
61      * Documented as a pointer to a constant C string.
62      * Assumed data type OSSL_PARAM_UTF8_PTR
63      */
64     const char *p5;
65     size_t p5_l;
66 };
67
68 #define p1_init 42                              /* The ultimate answer */
69 #define p2_init 6.283                           /* Magic number */
70 /* Stolen from evp_data, BLAKE2s256 test */
71 #define p3_init                                 \
72     "4142434445464748494a4b4c4d4e4f50"          \
73     "5152535455565758595a616263646566"          \
74     "6768696a6b6c6d6e6f70717273747576"          \
75     "7778797a30313233343536373839"
76 #define p4_init "BLAKE2s256"                    /* Random string */
77 #define p5_init OPENSSL_FULL_VERSION_STR        /* Static string */
78
79 static void cleanup_object(void *vobj)
80 {
81     struct object_st *obj = vobj;
82
83     BN_free(obj->p3);
84     obj->p3 = NULL;
85     OPENSSL_free(obj->p4);
86     obj->p4 = NULL;
87     OPENSSL_free(obj);
88 }
89
90 static void *init_object(void)
91 {
92     struct object_st *obj = OPENSSL_zalloc(sizeof(*obj));
93
94     obj->p1 = p1_init;
95     obj->p2 = p2_init;
96     if (!TEST_true(BN_hex2bn(&obj->p3, p3_init)))
97         goto fail;
98     if (!TEST_ptr(obj->p4 = OPENSSL_strdup(p4_init)))
99         goto fail;
100     obj->p5 = p5_init;
101
102     return obj;
103  fail:
104     cleanup_object(obj);
105     obj = NULL;
106
107     return NULL;
108 }
109
110 /*
111  * RAW provider, which handles the parameters in a very raw manner,
112  * with no fancy API and very minimal checking.  The application that
113  * calls these to set or request parameters MUST get its OSSL_PARAM
114  * array right.
115  */
116
117 static int raw_set_params(void *vobj, const OSSL_PARAM *params)
118 {
119     struct object_st *obj = vobj;
120
121     for (; params->key != NULL; params++)
122         if (strcmp(params->key, "p1") == 0) {
123             obj->p1 = *(int *)params->data;
124         } else if (strcmp(params->key, "p2") == 0) {
125             obj->p2 = *(double *)params->data;
126         } else if (strcmp(params->key, "p3") == 0) {
127             BN_free(obj->p3);
128             if (!TEST_ptr(obj->p3 = BN_native2bn(params->data,
129                                                  params->data_size, NULL)))
130                 return 0;
131         } else if (strcmp(params->key, "p4") == 0) {
132             OPENSSL_free(obj->p4);
133             if (!TEST_ptr(obj->p4 = OPENSSL_strndup(params->data,
134                                                     params->data_size)))
135                 return 0;
136         } else if (strcmp(params->key, "p5") == 0) {
137             obj->p5 = *(const char **)params->data;
138         }
139
140     return 1;
141 }
142
143 static int raw_get_params(void *vobj, const OSSL_PARAM *params)
144 {
145     struct object_st *obj = vobj;
146
147     for (; params->key != NULL; params++)
148         if (strcmp(params->key, "p1") == 0) {
149             if (params->return_size != NULL)
150                 *params->return_size = sizeof(obj->p1);
151             *(int *)params->data = obj->p1;
152         } else if (strcmp(params->key, "p2") == 0) {
153             if (params->return_size != NULL)
154                 *params->return_size = sizeof(obj->p2);
155             *(double *)params->data = obj->p2;
156         } else if (strcmp(params->key, "p3") == 0) {
157             size_t bytes = BN_num_bytes(obj->p3);
158
159             if (params->return_size != NULL)
160                 *params->return_size = bytes;
161             if (!TEST_size_t_ge(params->data_size, bytes))
162                 return 0;
163             BN_bn2nativepad(obj->p3, params->data, bytes);
164         } else if (strcmp(params->key, "p4") == 0) {
165             size_t bytes = strlen(obj->p4) + 1;
166
167             if (params->return_size != NULL)
168                 *params->return_size = bytes;
169             if (!TEST_size_t_ge(params->data_size, bytes))
170                 return 0;
171             strcpy(params->data, obj->p4);
172         } else if (strcmp(params->key, "p5") == 0) {
173             /*
174              * We COULD also use OPENSSL_FULL_VERSION_STR directly and
175              * use sizeof(OPENSSL_FULL_VERSION_STR) instead of calling
176              * strlen().
177              * The caller wouldn't know the difference.
178              */
179             size_t bytes = strlen(obj->p5) + 1;
180
181             if (params->return_size != NULL)
182                 *params->return_size = bytes;
183             *(const char **)params->data = obj->p5;
184         }
185
186     return 1;
187 }
188
189 /*
190  * API provider, which handles the parameters using the API from params.h
191  */
192
193 static int api_set_params(void *vobj, const OSSL_PARAM *params)
194 {
195     struct object_st *obj = vobj;
196     const OSSL_PARAM *p = NULL;
197
198     if ((p = OSSL_PARAM_locate(params, "p1")) != NULL
199         && !TEST_true(OSSL_PARAM_get_int(p, &obj->p1)))
200         return 0;
201     if ((p = OSSL_PARAM_locate(params, "p2")) != NULL
202         && !TEST_true(OSSL_PARAM_get_double(p, &obj->p2)))
203         return 0;
204     if ((p = OSSL_PARAM_locate(params, "p3")) != NULL
205         && !TEST_true(OSSL_PARAM_get_BN(p, &obj->p3)))
206         return 0;
207     if ((p = OSSL_PARAM_locate(params, "p4")) != NULL) {
208         OPENSSL_free(obj->p4);
209         obj->p4 = NULL;
210         /* If the value pointer is NULL, we get it automatically allocated */
211         if (!TEST_true(OSSL_PARAM_get_utf8_string(p, &obj->p4, 0)))
212             return 0;
213     }
214     if ((p = OSSL_PARAM_locate(params, "p5")) != NULL
215         && !TEST_true(OSSL_PARAM_get_utf8_ptr(p, &obj->p5)))
216         return 0;
217
218     return 1;
219 }
220
221 static int api_get_params(void *vobj, const OSSL_PARAM *params)
222 {
223     struct object_st *obj = vobj;
224     const OSSL_PARAM *p = NULL;
225
226     if ((p = OSSL_PARAM_locate(params, "p1")) != NULL
227         && !TEST_true(OSSL_PARAM_set_int(p, obj->p1)))
228         return 0;
229     if ((p = OSSL_PARAM_locate(params, "p2")) != NULL
230         && !TEST_true(OSSL_PARAM_set_double(p, obj->p2)))
231         return 0;
232     if ((p = OSSL_PARAM_locate(params, "p3")) != NULL
233         && !TEST_true(OSSL_PARAM_set_BN(p, obj->p3)))
234         return 0;
235     if ((p = OSSL_PARAM_locate(params, "p4")) != NULL
236         && !TEST_true(OSSL_PARAM_set_utf8_string(p, obj->p4)))
237         return 0;
238     if ((p = OSSL_PARAM_locate(params, "p5")) != NULL
239         && !TEST_true(OSSL_PARAM_set_utf8_ptr(p, obj->p5)))
240         return 0;
241
242     return 1;
243 }
244
245 /*
246  * This structure only simulates a provider dispatch, the real deal is
247  * a bit more code that's not necessary in these tests.
248  */
249 struct provider_dispatch_st {
250     int (*set_params)(void *obj, const OSSL_PARAM *params);
251     int (*get_params)(void *obj, const OSSL_PARAM *params);
252 };
253
254 /* "raw" provider */
255 static const struct provider_dispatch_st provider_raw = {
256     raw_set_params, raw_get_params
257 };
258
259 /* "api" provider */
260 static const struct provider_dispatch_st provider_api = {
261     api_set_params, api_get_params
262 };
263
264 /*-
265  * APPLICATION SECTION
266  * ===================
267  */
268
269 /* In all our tests, these are variables that get manipulated as parameters
270  *
271  * These arrays consistenly do nothing with the "p2" parameter, and
272  * always include a "foo" parameter.  This is to check that the
273  * set_params and get_params calls ignore the lack of parameters that
274  * the application isn't interested in, as well as ignore parameters
275  * they don't understand (the application may have one big bag of
276  * parameters).
277  */
278 static int app_p1;                    /* "p1" */
279 static double app_p2;                 /* "p2" is ignored */
280 static BIGNUM *app_p3 = NULL;         /* "p3" */
281 static unsigned char bignumbin[4096]; /* "p3" */
282 static size_t bignumbin_l;            /* "p3" */
283 static char app_p4[256];              /* "p4" */
284 static size_t app_p4_l;               /* "p4" */
285 static const char *app_p5 = NULL;     /* "p5" */
286 static size_t app_p5_l;               /* "p5" */
287 static unsigned char foo[1];          /* "foo" */
288 static size_t foo_l;                  /* "foo" */
289
290 #define app_p1_init 17           /* A random number */
291 #define app_p2_init 47.11        /* Another random number */
292 #define app_p3_init "deadbeef"   /* Classic */
293 #define app_p4_init "Hello"
294 #define app_p5_init "World"
295 #define app_foo_init 'z'
296
297 static int cleanup_app_variables(void)
298 {
299     BN_free(app_p3);
300     app_p3 = NULL;
301     return 1;
302 }
303
304 static int init_app_variables(void)
305 {
306     int l = 0;
307
308     cleanup_app_variables();
309
310     app_p1 = app_p1_init;
311     app_p2 = app_p2_init;
312     if (!BN_hex2bn(&app_p3, app_p3_init)
313         || (l = BN_bn2nativepad(app_p3, bignumbin, sizeof(bignumbin))) < 0)
314         return 0;
315     bignumbin_l = (size_t)l;
316     strcpy(app_p4, app_p4_init);
317     app_p4_l = sizeof(app_p4_init);
318     app_p5 = app_p5_init;
319     app_p5_l = sizeof(app_p5_init);
320     foo[0] = app_foo_init;
321     foo_l = sizeof(app_foo_init);
322
323     return 1;
324 }
325
326 /*
327  * Here, we define test OSSL_PARAM arrays
328  */
329
330 /* An array of OSSL_PARAM, specific in the most raw manner possible */
331 static const OSSL_PARAM raw_params[] = {
332     { "p1", OSSL_PARAM_INTEGER, &app_p1, sizeof(app_p1), NULL },
333     { "p3", OSSL_PARAM_UNSIGNED_INTEGER, &bignumbin, sizeof(bignumbin),
334       &bignumbin_l },
335     { "p4", OSSL_PARAM_UTF8_STRING, &app_p4, sizeof(app_p4), &app_p4_l },
336     { "p5", OSSL_PARAM_UTF8_PTR, &app_p5, sizeof(app_p5), &app_p5_l },
337     { "foo", OSSL_PARAM_OCTET_STRING, &foo, sizeof(foo), &foo_l },
338     { NULL, 0, NULL, 0, NULL }
339 };
340
341 /* The same array of OSSL_PARAM, specified with the macros from params.h */
342 static const OSSL_PARAM api_params[] = {
343     OSSL_PARAM_int("p1", &app_p1),
344     OSSL_PARAM_SIZED_BN("p3", &bignumbin, sizeof(bignumbin), bignumbin_l),
345     OSSL_PARAM_DEFN("p4", OSSL_PARAM_UTF8_STRING, &app_p4, sizeof(app_p4),
346                     &app_p4_l),
347     OSSL_PARAM_DEFN("p5", OSSL_PARAM_UTF8_PTR, &app_p5, sizeof(app_p5),
348                     &app_p5_l),
349     OSSL_PARAM_DEFN("foo", OSSL_PARAM_OCTET_STRING, &foo, sizeof(foo), &foo_l),
350     OSSL_PARAM_END
351 };
352
353 /*-
354  * TESTING
355  * =======
356  */
357
358 /*
359  * Test cases to combine parameters with "provider side" functions
360  */
361 static struct {
362     const struct provider_dispatch_st *prov;
363     const OSSL_PARAM *params;
364     const char *desc;
365 } test_cases[] = {
366     /* Tests within specific methods */
367     { &provider_raw, raw_params, "raw provider vs raw params" },
368     { &provider_api, api_params, "api provider vs api params" },
369
370     /* Mixed methods */
371     { &provider_raw, api_params, "raw provider vs api params" },
372     { &provider_api, raw_params, "api provider vs raw params" },
373 };
374
375 /* Generic tester of combinations of "providers" and params */
376 static int test_case(int i)
377 {
378     const struct provider_dispatch_st *prov = test_cases[i].prov;
379     const OSSL_PARAM *params = test_cases[i].params;
380     BIGNUM *verify_p3 = NULL;
381     void *obj = NULL;
382     int errcnt = 0;
383
384     TEST_info("Case: %s", test_cases[i].desc);
385
386     /*
387      * Initialize
388      */
389     if (!TEST_ptr(obj = init_object())
390         || !TEST_true(BN_hex2bn(&verify_p3, p3_init))) {
391         errcnt++;
392         goto fin;
393     }
394
395     /*
396      * Get parameters a first time, just to see that getting works and
397      * gets us the values we expect.
398      */
399     init_app_variables();
400
401     if (!TEST_true(prov->get_params(obj, params))
402         || !TEST_int_eq(app_p1, p1_init)        /* "provider" value */
403         || !TEST_ulong_eq(app_p2, app_p2_init)  /* Should remain untouched */
404         || !TEST_ptr(BN_native2bn(bignumbin, bignumbin_l, app_p3))
405         || !TEST_BN_eq(app_p3, verify_p3)       /* "provider" value */
406         || !TEST_str_eq(app_p4, p4_init)        /* "provider" value */
407         || !TEST_str_eq(app_p5, p5_init)        /* "provider" value */
408         || !TEST_char_eq(foo[0], app_foo_init)  /* Should remain untouched */
409         || !TEST_int_eq(foo_l, sizeof(app_foo_init)))
410         errcnt++;
411
412     /*
413      * Set parameters, then sneak into the object itself and check
414      * that its attributes got set (or ignored) properly.
415      */
416     init_app_variables();
417
418     if (!TEST_true(prov->set_params(obj, params))) {
419         errcnt++;
420     } else {
421         struct object_st *sneakpeek = obj;
422
423         if (!TEST_int_eq(sneakpeek->p1, app_p1)         /* app value set */
424             || !TEST_ulong_eq(sneakpeek->p2, p2_init) /* Should remain untouched */
425             || !TEST_BN_eq(sneakpeek->p3, app_p3)       /* app value set */
426             || !TEST_str_eq(sneakpeek->p4, app_p4)      /* app value set */
427             || !TEST_str_eq(sneakpeek->p5, app_p5))     /* app value set */
428             errcnt++;
429     }
430
431     /*
432      * Get parameters again, checking that we get different values
433      * than earlier where relevant.
434      */
435     BN_free(verify_p3);
436     verify_p3 = NULL;
437
438     if (!TEST_true(BN_hex2bn(&verify_p3, app_p3_init))) {
439         errcnt++;
440         goto fin;
441     }
442
443     if (!TEST_true(prov->get_params(obj, params))
444         || !TEST_int_eq(app_p1, app_p1_init)    /* app value */
445         || !TEST_ulong_eq(app_p2, app_p2_init)  /* Should remain untouched */
446         || !TEST_ptr(BN_native2bn(bignumbin, bignumbin_l, app_p3))
447         || !TEST_BN_eq(app_p3, verify_p3)       /* app value */
448         || !TEST_str_eq(app_p4, app_p4_init)    /* app value */
449         || !TEST_str_eq(app_p5, app_p5_init)    /* app value */
450         || !TEST_char_eq(foo[0], app_foo_init)  /* Should remain untouched */
451         || !TEST_int_eq(foo_l, sizeof(app_foo_init)))
452         errcnt++;
453
454  fin:
455     BN_free(verify_p3);
456     verify_p3 = NULL;
457     cleanup_app_variables();
458     cleanup_object(obj);
459
460     return errcnt == 0;
461 }
462
463 int setup_tests(void)
464 {
465     ADD_ALL_TESTS(test_case, OSSL_NELEM(test_cases));
466     return 1;
467 }