diff options
author | jsing <> | 2024-03-25 10:41:36 +0000 |
---|---|---|
committer | jsing <> | 2024-03-25 10:41:36 +0000 |
commit | 20a39d2a3c5047f76ad6b865804f321ccc27e6c1 (patch) | |
tree | aea387a16dad649e3e02fdda96cd918f16dccdff | |
parent | 719fa17092ec7c60b7951197bba5eb1902544525 (diff) | |
download | openbsd-20a39d2a3c5047f76ad6b865804f321ccc27e6c1.tar.gz openbsd-20a39d2a3c5047f76ad6b865804f321ccc27e6c1.tar.bz2 openbsd-20a39d2a3c5047f76ad6b865804f321ccc27e6c1.zip |
Codify more insane CRYPTO_EX_DATA API.
The current CRYPTO_EX_DATA implementation allows for data to be set without
calling new, indexes can be used without allocation, new can be called without
getting an index and dup can be called after new or without calling new.
-rw-r--r-- | src/regress/lib/libcrypto/exdata/exdata_test.c | 68 |
1 files changed, 59 insertions, 9 deletions
diff --git a/src/regress/lib/libcrypto/exdata/exdata_test.c b/src/regress/lib/libcrypto/exdata/exdata_test.c index d90418610b..22483f7061 100644 --- a/src/regress/lib/libcrypto/exdata/exdata_test.c +++ b/src/regress/lib/libcrypto/exdata/exdata_test.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: exdata_test.c,v 1.1 2023/12/27 12:34:32 jsing Exp $ */ | 1 | /* $OpenBSD: exdata_test.c,v 1.2 2024/03/25 10:41:36 jsing Exp $ */ |
2 | /* | 2 | /* |
3 | * Copyright (c) 2023 Joel Sing <jsing@openbsd.org> | 3 | * Copyright (c) 2023 Joel Sing <jsing@openbsd.org> |
4 | * | 4 | * |
@@ -30,6 +30,10 @@ ex_new(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, | |||
30 | { | 30 | { |
31 | long *arg = argp; | 31 | long *arg = argp; |
32 | 32 | ||
33 | if (ptr != NULL) { | ||
34 | fprintf(stderr, "FAIL: ex_new() called with ptr != NULL\n"); | ||
35 | return 0; | ||
36 | } | ||
33 | if (argl != 1234 || *arg != 1234) { | 37 | if (argl != 1234 || *arg != 1234) { |
34 | fprintf(stderr, "FAIL: ex_new() with bad arguments\n"); | 38 | fprintf(stderr, "FAIL: ex_new() with bad arguments\n"); |
35 | return 0; | 39 | return 0; |
@@ -78,7 +82,7 @@ struct exdata { | |||
78 | static int | 82 | static int |
79 | ex_data_test(void) | 83 | ex_data_test(void) |
80 | { | 84 | { |
81 | struct exdata exdata1, exdata2; | 85 | struct exdata exdata1, exdata2, exdata3, exdata4; |
82 | void *argp; | 86 | void *argp; |
83 | long argl; | 87 | long argl; |
84 | int idx1, idx2; | 88 | int idx1, idx2; |
@@ -86,6 +90,8 @@ ex_data_test(void) | |||
86 | 90 | ||
87 | memset(&exdata1, 0, sizeof(exdata1)); | 91 | memset(&exdata1, 0, sizeof(exdata1)); |
88 | memset(&exdata2, 0, sizeof(exdata2)); | 92 | memset(&exdata2, 0, sizeof(exdata2)); |
93 | memset(&exdata3, 0, sizeof(exdata3)); | ||
94 | memset(&exdata4, 0, sizeof(exdata4)); | ||
89 | 95 | ||
90 | argl = 1234; | 96 | argl = 1234; |
91 | argp = &argl; | 97 | argp = &argl; |
@@ -123,33 +129,33 @@ ex_data_test(void) | |||
123 | } | 129 | } |
124 | 130 | ||
125 | if (!CRYPTO_set_ex_data(&exdata1.exdata, idx2, &idx2)) { | 131 | if (!CRYPTO_set_ex_data(&exdata1.exdata, idx2, &idx2)) { |
126 | fprintf(stderr, "FAIL: CRYPTO_set_ex_data() failed\n"); | 132 | fprintf(stderr, "FAIL: CRYPTO_set_ex_data() failed with index 2\n"); |
127 | goto failure; | 133 | goto failure; |
128 | } | 134 | } |
129 | if (!CRYPTO_set_ex_data(&exdata1.exdata, idx1, &idx1)) { | 135 | if (!CRYPTO_set_ex_data(&exdata1.exdata, idx1, &idx1)) { |
130 | fprintf(stderr, "FAIL: CRYPTO_set_ex_data() failed\n"); | 136 | fprintf(stderr, "FAIL: CRYPTO_set_ex_data() failed with index 1\n"); |
131 | goto failure; | 137 | goto failure; |
132 | } | 138 | } |
133 | if (CRYPTO_get_ex_data(&exdata1.exdata, idx1) != &idx1) { | 139 | if (CRYPTO_get_ex_data(&exdata1.exdata, idx1) != &idx1) { |
134 | fprintf(stderr, "FAIL: CRYPTO_get_ex_data() failed\n"); | 140 | fprintf(stderr, "FAIL: CRYPTO_get_ex_data() failed with index 1\n"); |
135 | goto failure; | 141 | goto failure; |
136 | } | 142 | } |
137 | if (CRYPTO_get_ex_data(&exdata1.exdata, idx2) != &idx2) { | 143 | if (CRYPTO_get_ex_data(&exdata1.exdata, idx2) != &idx2) { |
138 | fprintf(stderr, "FAIL: CRYPTO_get_ex_data() failed\n"); | 144 | fprintf(stderr, "FAIL: CRYPTO_get_ex_data() failed with index 2\n"); |
139 | goto failure; | 145 | goto failure; |
140 | } | 146 | } |
141 | 147 | ||
142 | if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_RSA, &exdata2.exdata, | 148 | if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_RSA, &exdata2.exdata, |
143 | &exdata1.exdata)) { | 149 | &exdata1.exdata)) { |
144 | fprintf(stderr, "FAIL: CRYPTO_get_ex_data() failed\n"); | 150 | fprintf(stderr, "FAIL: CRYPTO_dup_ex_data() failed\n"); |
145 | goto failure; | 151 | goto failure; |
146 | } | 152 | } |
147 | if (CRYPTO_get_ex_data(&exdata2.exdata, idx1) != &idx1) { | 153 | if (CRYPTO_get_ex_data(&exdata2.exdata, idx1) != &idx1) { |
148 | fprintf(stderr, "FAIL: CRYPTO_get_ex_data() failed\n"); | 154 | fprintf(stderr, "FAIL: CRYPTO_get_ex_data() failed after dup\n"); |
149 | goto failure; | 155 | goto failure; |
150 | } | 156 | } |
151 | if (CRYPTO_get_ex_data(&exdata2.exdata, idx2) != &idx2) { | 157 | if (CRYPTO_get_ex_data(&exdata2.exdata, idx2) != &idx2) { |
152 | fprintf(stderr, "FAIL: CRYPTO_get_ex_data() failed\n"); | 158 | fprintf(stderr, "FAIL: CRYPTO_get_ex_data() failed after dup\n"); |
153 | goto failure; | 159 | goto failure; |
154 | } | 160 | } |
155 | 161 | ||
@@ -172,11 +178,55 @@ ex_data_test(void) | |||
172 | goto failure; | 178 | goto failure; |
173 | } | 179 | } |
174 | 180 | ||
181 | /* The current implementation allows for data to be set without new. */ | ||
182 | if (!CRYPTO_set_ex_data(&exdata3.exdata, idx1, &idx1)) { | ||
183 | fprintf(stderr, "FAIL: CRYPTO_set_ex_data() failed with index " | ||
184 | "1 (without new)\n"); | ||
185 | goto failure; | ||
186 | } | ||
187 | if (CRYPTO_get_ex_data(&exdata3.exdata, idx1) != &idx1) { | ||
188 | fprintf(stderr, "FAIL: CRYPTO_get_ex_data() failed with index " | ||
189 | "1 (without new)\n"); | ||
190 | goto failure; | ||
191 | } | ||
192 | |||
193 | /* And indexes can be used without allocation. */ | ||
194 | if (!CRYPTO_set_ex_data(&exdata3.exdata, idx2 + 1, &idx2)) { | ||
195 | fprintf(stderr, "FAIL: CRYPTO_set_ex_data() failed with index " | ||
196 | "%d (unallocated)\n", idx2 + 1); | ||
197 | goto failure; | ||
198 | } | ||
199 | if (CRYPTO_get_ex_data(&exdata3.exdata, idx2 + 1) != &idx2) { | ||
200 | fprintf(stderr, "FAIL: CRYPTO_get_ex_data() failed with index " | ||
201 | "%d\n", idx2 + 1); | ||
202 | goto failure; | ||
203 | } | ||
204 | |||
205 | /* And new can be called without getting any index first. */ | ||
206 | if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_BIO, &exdata4, &exdata4.exdata)) { | ||
207 | fprintf(stderr, "FAIL: CRYPTO_new_ex_data() failed with " | ||
208 | "uninitialised index\n"); | ||
209 | goto failure; | ||
210 | } | ||
211 | |||
212 | /* And dup can be called after new or without new... */ | ||
213 | if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_RSA, &exdata1, &exdata1.exdata)) { | ||
214 | fprintf(stderr, "FAIL: CRYPTO_new_ex_data() failed\n"); | ||
215 | goto failure; | ||
216 | } | ||
217 | if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_RSA, &exdata2.exdata, | ||
218 | &exdata1.exdata)) { | ||
219 | fprintf(stderr, "FAIL: CRYPTO_dup_ex_data() after new failed\n"); | ||
220 | goto failure; | ||
221 | } | ||
222 | |||
175 | failed = 0; | 223 | failed = 0; |
176 | 224 | ||
177 | failure: | 225 | failure: |
178 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, &exdata1, &exdata1.exdata); | 226 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, &exdata1, &exdata1.exdata); |
179 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, &exdata2, &exdata2.exdata); | 227 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, &exdata2, &exdata2.exdata); |
228 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, &exdata3, &exdata3.exdata); | ||
229 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, &exdata4, &exdata4.exdata); | ||
180 | 230 | ||
181 | return failed; | 231 | return failed; |
182 | } | 232 | } |