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