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