1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
|
/* $OpenBSD: exdata_test.c,v 1.1 2023/12/27 12:34:32 jsing Exp $ */
/*
* Copyright (c) 2023 Joel Sing <jsing@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <stdio.h>
#include <string.h>
#include <openssl/crypto.h>
static int ex_new_calls;
static int ex_free_calls;
static int ex_dup_calls;
static int
ex_new(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl,
void *argp)
{
long *arg = argp;
if (argl != 1234 || *arg != 1234) {
fprintf(stderr, "FAIL: ex_new() with bad arguments\n");
return 0;
}
ex_new_calls++;
return 1;
}
static int
ex_dup(CRYPTO_EX_DATA *to, CRYPTO_EX_DATA *from, void *from_d,
int idx, long argl, void *argp)
{
long *arg = argp;
if (argl != 1234 || *arg != 1234) {
fprintf(stderr, "FAIL: ex_dup() with bad arguments\n");
return 0;
}
ex_dup_calls++;
return 1;
}
static void
ex_free(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx,
long argl, void *argp)
{
long *arg = argp;
if (argl != 1234 || *arg != 1234) {
fprintf(stderr, "FAIL: ex_free() with bad arguments\n");
return;
}
ex_free_calls++;
}
struct exdata {
CRYPTO_EX_DATA exdata;
int val;
};
static int
ex_data_test(void)
{
struct exdata exdata1, exdata2;
void *argp;
long argl;
int idx1, idx2;
int failed = 1;
memset(&exdata1, 0, sizeof(exdata1));
memset(&exdata2, 0, sizeof(exdata2));
argl = 1234;
argp = &argl;
if ((idx1 = CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_RSA, argl, argp,
ex_new, ex_dup, ex_free)) < 0) {
fprintf(stderr, "FAIL: CRYPTO_get_ex_new_index failed\n");
goto failure;
}
if (idx1 == 0) {
fprintf(stderr, "FAIL: CRYPTO_get_ex_new_index() returned 0 "
"(reserved for internal use)\n");
goto failure;
}
if ((idx2 = CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_RSA, 0, NULL,
NULL, NULL, NULL)) < 0) {
fprintf(stderr, "FAIL: CRYPTO_get_ex_new_index failed\n");
goto failure;
}
if (idx1 == idx2) {
fprintf(stderr, "FAIL: CRYPTO_get_ex_new_index() returned the "
"same value\n");
goto failure;
}
if (idx2 < idx1) {
fprintf(stderr, "FAIL: CRYPTO_get_ex_new_index() returned "
"idx2 < idx1\n");
goto failure;
}
if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_RSA, &exdata1, &exdata1.exdata)) {
fprintf(stderr, "FAIL: CRYPTO_new_ex_data() failed\n");
goto failure;
}
if (!CRYPTO_set_ex_data(&exdata1.exdata, idx2, &idx2)) {
fprintf(stderr, "FAIL: CRYPTO_set_ex_data() failed\n");
goto failure;
}
if (!CRYPTO_set_ex_data(&exdata1.exdata, idx1, &idx1)) {
fprintf(stderr, "FAIL: CRYPTO_set_ex_data() failed\n");
goto failure;
}
if (CRYPTO_get_ex_data(&exdata1.exdata, idx1) != &idx1) {
fprintf(stderr, "FAIL: CRYPTO_get_ex_data() failed\n");
goto failure;
}
if (CRYPTO_get_ex_data(&exdata1.exdata, idx2) != &idx2) {
fprintf(stderr, "FAIL: CRYPTO_get_ex_data() failed\n");
goto failure;
}
if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_RSA, &exdata2.exdata,
&exdata1.exdata)) {
fprintf(stderr, "FAIL: CRYPTO_get_ex_data() failed\n");
goto failure;
}
if (CRYPTO_get_ex_data(&exdata2.exdata, idx1) != &idx1) {
fprintf(stderr, "FAIL: CRYPTO_get_ex_data() failed\n");
goto failure;
}
if (CRYPTO_get_ex_data(&exdata2.exdata, idx2) != &idx2) {
fprintf(stderr, "FAIL: CRYPTO_get_ex_data() failed\n");
goto failure;
}
CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, &exdata1, &exdata1.exdata);
CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, &exdata2, &exdata2.exdata);
if (ex_new_calls != 1) {
fprintf(stderr, "FAIL: got %d ex_new calls, want %d\n",
ex_new_calls, 1);
goto failure;
}
if (ex_dup_calls != 1) {
fprintf(stderr, "FAIL: got %d ex_dup calls, want %d\n",
ex_dup_calls, 1);
goto failure;
}
if (ex_free_calls != 2) {
fprintf(stderr, "FAIL: got %d ex_free calls, want %d\n",
ex_free_calls, 2);
goto failure;
}
failed = 0;
failure:
CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, &exdata1, &exdata1.exdata);
CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, &exdata2, &exdata2.exdata);
return failed;
}
#if 0
/* This insanity currently succeeds... */
static int
ex_new_index_test(void)
{
int failed = 1;
int idx;
if ((idx = CRYPTO_get_ex_new_index(-1, 0, NULL, NULL, NULL,
NULL)) > 0) {
fprintf(stderr, "FAIL: CRYPTO_get_ex_new_index() succeeded with "
"negative class\n");
goto failure;
}
if ((idx = CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX__COUNT, 0,
NULL, NULL, NULL, NULL)) > 0) {
fprintf(stderr, "FAIL: CRYPTO_get_ex_new_index() succeeded with "
"class exceeding maximum\n");
goto failure;
}
failed = 0;
failure:
return failed;
}
#endif
int
main(int argc, char **argv)
{
int failed = 0;
failed |= ex_data_test();
#if 0
failed |= ex_new_index_test();
#endif
/* Force a clean up. */
CRYPTO_cleanup_all_ex_data();
return failed;
}
|