Add app for fips installation
[openssl.git] / test / exdatatest.c
1 /*
2  * Copyright 2015-2018 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <stdio.h>
11 #include <string.h>
12 #include <stdlib.h>
13 #include <openssl/crypto.h>
14
15 #include "testutil.h"
16
17 static long saved_argl;
18 static void *saved_argp;
19 static int saved_idx;
20 static int saved_idx2;
21 static int saved_idx3;
22 static int gbl_result;
23
24 /*
25  * SIMPLE EX_DATA IMPLEMENTATION
26  * Apps explicitly set/get ex_data as needed
27  */
28
29 static void exnew(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
30           int idx, long argl, void *argp)
31 {
32     if (!TEST_int_eq(idx, saved_idx)
33         || !TEST_long_eq(argl, saved_argl)
34         || !TEST_ptr_eq(argp, saved_argp)
35         || !TEST_ptr_null(ptr))
36         gbl_result = 0;
37 }
38
39 static int exdup(CRYPTO_EX_DATA *to, const CRYPTO_EX_DATA *from,
40           void *from_d, int idx, long argl, void *argp)
41 {
42     if (!TEST_int_eq(idx, saved_idx)
43         || !TEST_long_eq(argl, saved_argl)
44         || !TEST_ptr_eq(argp, saved_argp)
45         || !TEST_ptr(from_d))
46         gbl_result = 0;
47     return 1;
48 }
49
50 static void exfree(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
51             int idx, long argl, void *argp)
52 {
53     if (!TEST_int_eq(idx, saved_idx)
54         || !TEST_long_eq(argl, saved_argl)
55         || !TEST_ptr_eq(argp, saved_argp))
56         gbl_result = 0;
57 }
58
59 /*
60  * PRE-ALLOCATED EX_DATA IMPLEMENTATION
61  * Extended data structure is allocated in exnew2/freed in exfree2
62  * Data is stored inside extended data structure
63  */
64
65 typedef struct myobj_ex_data_st {
66     char *hello;
67     int new;
68     int dup;
69 } MYOBJ_EX_DATA;
70
71 static void exnew2(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
72           int idx, long argl, void *argp)
73 {
74     MYOBJ_EX_DATA *ex_data = OPENSSL_zalloc(sizeof(*ex_data));
75
76     if (!TEST_true(idx == saved_idx2 || idx == saved_idx3)
77         || !TEST_long_eq(argl, saved_argl)
78         || !TEST_ptr_eq(argp, saved_argp)
79         || !TEST_ptr_null(ptr)
80         || !TEST_ptr(ex_data)
81         || !TEST_true(CRYPTO_set_ex_data(ad, idx, ex_data))) {
82         gbl_result = 0;
83         OPENSSL_free(ex_data);
84     } else {
85         ex_data->new = 1;
86     }
87 }
88
89 static int exdup2(CRYPTO_EX_DATA *to, const CRYPTO_EX_DATA *from,
90           void *from_d, int idx, long argl, void *argp)
91 {
92     MYOBJ_EX_DATA **update_ex_data = (MYOBJ_EX_DATA**)from_d;
93     MYOBJ_EX_DATA *ex_data = NULL;
94
95     if (!TEST_true(idx == saved_idx2 || idx == saved_idx3)
96         || !TEST_long_eq(argl, saved_argl)
97         || !TEST_ptr_eq(argp, saved_argp)
98         || !TEST_ptr(from_d)
99         || !TEST_ptr(*update_ex_data)
100         || !TEST_ptr(ex_data = CRYPTO_get_ex_data(to, idx))
101         || !TEST_true(ex_data->new)) {
102         gbl_result = 0;
103     } else {
104         /* Copy hello over */
105         ex_data->hello = (*update_ex_data)->hello;
106         /* indicate this is a dup */
107         ex_data->dup = 1;
108         /* Keep my original ex_data */
109         *update_ex_data = ex_data;
110     }
111     return 1;
112 }
113
114 static void exfree2(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
115             int idx, long argl, void *argp)
116 {
117     MYOBJ_EX_DATA *ex_data = CRYPTO_get_ex_data(ad, idx);
118
119     if (!TEST_true(idx == saved_idx2 || idx == saved_idx3)
120         || !TEST_long_eq(argl, saved_argl)
121         || !TEST_ptr_eq(argp, saved_argp)
122         || !TEST_true(CRYPTO_set_ex_data(ad, idx, NULL)))
123         gbl_result = 0;
124     OPENSSL_free(ex_data);
125 }
126
127 typedef struct myobj_st {
128     CRYPTO_EX_DATA ex_data;
129     int id;
130     int st;
131 } MYOBJ;
132
133 static MYOBJ *MYOBJ_new(void)
134 {
135     static int count = 0;
136     MYOBJ *obj = OPENSSL_malloc(sizeof(*obj));
137
138     obj->id = ++count;
139     obj->st = CRYPTO_new_ex_data(CRYPTO_EX_INDEX_APP, obj, &obj->ex_data);
140     return obj;
141 }
142
143 static void MYOBJ_sethello(MYOBJ *obj, char *cp)
144 {
145     obj->st = CRYPTO_set_ex_data(&obj->ex_data, saved_idx, cp);
146     if (!TEST_int_eq(obj->st, 1))
147         gbl_result = 0;
148 }
149
150 static char *MYOBJ_gethello(MYOBJ *obj)
151 {
152     return CRYPTO_get_ex_data(&obj->ex_data, saved_idx);
153 }
154
155 static void MYOBJ_sethello2(MYOBJ *obj, char *cp)
156 {
157     MYOBJ_EX_DATA* ex_data = CRYPTO_get_ex_data(&obj->ex_data, saved_idx2);
158
159     if (TEST_ptr(ex_data))
160         ex_data->hello = cp;
161     else
162         obj->st = gbl_result = 0;
163 }
164
165 static char *MYOBJ_gethello2(MYOBJ *obj)
166 {
167     MYOBJ_EX_DATA* ex_data = CRYPTO_get_ex_data(&obj->ex_data, saved_idx2);
168
169     if (TEST_ptr(ex_data))
170         return ex_data->hello;
171
172     obj->st = gbl_result = 0;
173     return NULL;
174 }
175
176 static void MYOBJ_allochello3(MYOBJ *obj, char *cp)
177 {
178     MYOBJ_EX_DATA* ex_data = NULL;
179
180     if (TEST_ptr_null(ex_data = CRYPTO_get_ex_data(&obj->ex_data, saved_idx3))
181         && TEST_true(CRYPTO_alloc_ex_data(CRYPTO_EX_INDEX_APP, obj,
182                                           &obj->ex_data, saved_idx3))
183         && TEST_ptr(ex_data = CRYPTO_get_ex_data(&obj->ex_data, saved_idx3)))
184         ex_data->hello = cp;
185     else
186         obj->st = gbl_result = 0;
187 }
188
189 static char *MYOBJ_gethello3(MYOBJ *obj)
190 {
191     MYOBJ_EX_DATA* ex_data = CRYPTO_get_ex_data(&obj->ex_data, saved_idx3);
192
193     if (TEST_ptr(ex_data))
194         return ex_data->hello;
195
196     obj->st = gbl_result = 0;
197     return NULL;
198 }
199
200 static void MYOBJ_free(MYOBJ *obj)
201 {
202     CRYPTO_free_ex_data(CRYPTO_EX_INDEX_APP, obj, &obj->ex_data);
203     OPENSSL_free(obj);
204 }
205
206 static MYOBJ *MYOBJ_dup(MYOBJ *in)
207 {
208     MYOBJ *obj = MYOBJ_new();
209
210     obj->st |= CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_APP, &obj->ex_data,
211                                  &in->ex_data);
212     return obj;
213 }
214
215 static int test_exdata(void)
216 {
217     MYOBJ *t1, *t2, *t3;
218     MYOBJ_EX_DATA *ex_data;
219     const char *cp;
220     char *p;
221
222     gbl_result = 1;
223
224     p = OPENSSL_strdup("hello world");
225     saved_argl = 21;
226     saved_argp = OPENSSL_malloc(1);
227     saved_idx = CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_APP,
228                                         saved_argl, saved_argp,
229                                         exnew, exdup, exfree);
230     saved_idx2 = CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_APP,
231                                          saved_argl, saved_argp,
232                                          exnew2, exdup2, exfree2);
233     t1 = MYOBJ_new();
234     t2 = MYOBJ_new();
235     if (!TEST_int_eq(t1->st, 1) || !TEST_int_eq(t2->st, 1))
236         return 0;
237     if (!TEST_ptr(CRYPTO_get_ex_data(&t1->ex_data, saved_idx2)))
238         return 0;
239
240     /*
241      * saved_idx3 differs from other indexes by being created after the exdata
242      * was initialized.
243      */
244     saved_idx3 = CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_APP,
245                                          saved_argl, saved_argp,
246                                          exnew2, exdup2, exfree2);
247     if (!TEST_ptr_null(CRYPTO_get_ex_data(&t1->ex_data, saved_idx3)))
248         return 0;
249
250     MYOBJ_sethello(t1, p);
251     cp = MYOBJ_gethello(t1);
252     if (!TEST_ptr_eq(cp, p))
253         return 0;
254
255     MYOBJ_sethello2(t1, p);
256     cp = MYOBJ_gethello2(t1);
257     if (!TEST_ptr_eq(cp, p))
258         return 0;
259
260     MYOBJ_allochello3(t1, p);
261     cp = MYOBJ_gethello3(t1);
262     if (!TEST_ptr_eq(cp, p))
263         return 0;
264
265     cp = MYOBJ_gethello(t2);
266     if (!TEST_ptr_null(cp))
267         return 0;
268
269     cp = MYOBJ_gethello2(t2);
270     if (!TEST_ptr_null(cp))
271         return 0;
272
273     t3 = MYOBJ_dup(t1);
274     if (!TEST_int_eq(t3->st, 1))
275         return 0;
276
277     ex_data = CRYPTO_get_ex_data(&t3->ex_data, saved_idx2);
278     if (!TEST_ptr(ex_data))
279         return 0;
280     if (!TEST_int_eq(ex_data->dup, 1))
281         return 0;
282
283     cp = MYOBJ_gethello(t3);
284     if (!TEST_ptr_eq(cp, p))
285         return 0;
286
287     cp = MYOBJ_gethello2(t3);
288     if (!TEST_ptr_eq(cp, p))
289         return 0;
290
291     cp = MYOBJ_gethello3(t3);
292     if (!TEST_ptr_eq(cp, p))
293         return 0;
294
295     MYOBJ_free(t1);
296     MYOBJ_free(t2);
297     MYOBJ_free(t3);
298     OPENSSL_free(saved_argp);
299     OPENSSL_free(p);
300
301     if (gbl_result)
302       return 1;
303     else
304       return 0;
305 }
306
307 int setup_tests(void)
308 {
309     ADD_TEST(test_exdata);
310     return 1;
311 }