Skip to content

Commit d3aa11b

Browse files
committed
Place manual memio helpers in utils.c and add macro for them
1 parent ab560aa commit d3aa11b

3 files changed

Lines changed: 214 additions & 222 deletions

File tree

tests/api.c

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10717,7 +10717,8 @@ static int test_wolfSSL_SCR_Reconnect(void)
1071710717
EXPECT_DECLS;
1071810718
#if defined(HAVE_SECURE_RENEGOTIATION) && \
1071910719
defined(BUILD_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) && \
10720-
defined(BUILD_TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256)
10720+
defined(BUILD_TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256) && \
10721+
defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
1072110722
struct test_memio_ctx test_ctx;
1072210723
WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
1072310724
WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
@@ -61017,8 +61018,7 @@ static int test_wolfSSL_DTLS_fragment_buckets(void)
6101761018

6101861019
#if !defined(NO_FILESYSTEM) && \
6101961020
defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
61020-
!defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
61021-
!defined(NO_RSA)
61021+
defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
6102261022

6102361023
static int test_wolfSSL_dtls_stateless2(void)
6102461024
{
@@ -61241,9 +61241,8 @@ static int test_wolfSSL_dtls_stateless_downgrade(void)
6124161241
#endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
6124261242
!defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)*/
6124361243

61244-
#if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
61245-
!defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
61246-
!defined(NO_OLD_TLS) && !defined(NO_RSA)
61244+
#if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
61245+
!defined(NO_OLD_TLS) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
6124761246
static int test_WOLFSSL_dtls_version_alert(void)
6124861247
{
6124961248
EXPECT_DECLS;
@@ -61297,7 +61296,8 @@ static int test_WOLFSSL_dtls_version_alert(void)
6129761296

6129861297
#if defined(WOLFSSL_TICKET_NONCE_MALLOC) && defined(HAVE_SESSION_TICKET) \
6129961298
&& defined(WOLFSSL_TLS13) && \
61300-
(!defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
61299+
(!defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))\
61300+
&& defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
6130161301
static int send_new_session_ticket(WOLFSSL *ssl, byte nonceLength, byte filler)
6130261302
{
6130361303
struct test_memio_ctx *test_ctx;
@@ -61466,7 +61466,7 @@ static int test_ticket_nonce_malloc(void)
6146661466
!defined(WOLFSSL_TICKET_DECRYPT_NO_CREATE) && \
6146761467
!defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
6146861468
!defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && !defined(NO_RSA) && \
61469-
defined(HAVE_ECC)
61469+
defined(HAVE_ECC) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
6147061470

6147161471
static int test_ticket_ret_create(void)
6147261472
{
@@ -61924,7 +61924,7 @@ static int test_TLS_13_ticket_different_ciphers(void)
6192461924
}
6192561925
#endif
6192661926
#if defined(WOLFSSL_EXTRA_ALERTS) && !defined(WOLFSSL_NO_TLS12) && \
61927-
defined(HAVE_IO_TESTS_DEPENDENCIES)
61927+
defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
6192861928

6192961929
#define TEST_WRONG_CS_CLIENT "DHE-RSA-AES128-SHA"
6193061930
/* AKA TLS_DHE_RSA_WITH_AES_128_CBC_SHA */
@@ -61990,7 +61990,7 @@ static int test_extra_alerts_wrong_cs(void)
6199061990
#endif
6199161991

6199261992
#if !defined(WOLFSSL_NO_TLS12) && defined(WOLFSSL_EXTRA_ALERTS) && \
61993-
defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_SP_MATH)
61993+
defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_SP_MATH)
6199461994

6199561995
static void test_remove_msg(byte *msg, int tail_len, int *len, int msg_length)
6199661996
{
@@ -62149,8 +62149,8 @@ static int test_extra_alerts_skip_hs(void)
6214962149
}
6215062150
#endif
6215162151

62152-
#if !defined(WOLFSSL_NO_TLS12) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
62153-
defined(WOLFSSL_EXTRA_ALERTS) && !defined(NO_PSK) && !defined(NO_DH)
62152+
#if !defined(WOLFSSL_NO_TLS12) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)\
62153+
&& defined(WOLFSSL_EXTRA_ALERTS) && !defined(NO_PSK) && !defined(NO_DH)
6215462154

6215562155
static unsigned int test_server_psk_cb(WOLFSSL* ssl, const char* id,
6215662156
unsigned char* key, unsigned int key_max_len)
@@ -62401,7 +62401,7 @@ static int test_override_alt_cert_chain(void)
6240162401
}
6240262402
#endif
6240362403

62404-
#if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13)
62404+
#if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13)
6240562405

6240662406

6240762407
static int test_dtls13_bad_epoch_ch(void)
@@ -62544,8 +62544,8 @@ static int test_short_session_id(void)
6254462544
}
6254562545
#endif
6254662546

62547-
#if defined(HAVE_NULL_CIPHER) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
62548-
defined(WOLFSSL_DTLS13)
62547+
#if defined(HAVE_NULL_CIPHER) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) \
62548+
&& defined(WOLFSSL_DTLS13)
6254962549
static byte* test_find_string(const char *string,
6255062550
byte *buf, int buf_size)
6255162551
{
@@ -62858,7 +62858,7 @@ static int test_wolfSSL_configure_args(void)
6285862858
static int test_dtls_no_extensions(void)
6285962859
{
6286062860
EXPECT_DECLS;
62861-
#if defined(WOLFSSL_DTLS) && !defined(NO_FILESYSTEM)
62861+
#if defined(WOLFSSL_DTLS) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
6286262862
WOLFSSL *ssl_s = NULL;
6286362863
WOLFSSL_CTX *ctx_s = NULL;
6286462864
struct test_memio_ctx test_ctx;

tests/utils.c

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,4 +119,202 @@ int copy_file(const char* in, const char* out)
119119
}
120120
#endif /* !NO_FILESYSTEM */
121121

122+
#if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
123+
!defined(NO_WOLFSSL_SERVER) && !defined(NO_WOLFSSL_CLIENT)
124+
125+
/* This set of memio functions allows for more fine tuned control of the TLS
126+
* connection operations. For new tests, try to use ssl_memio first. */
127+
128+
#define HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES
129+
130+
#define TEST_MEMIO_BUF_SZ (64 * 1024)
131+
struct test_memio_ctx
132+
{
133+
byte c_buff[TEST_MEMIO_BUF_SZ];
134+
int c_len;
135+
const char* c_ciphers;
136+
byte s_buff[TEST_MEMIO_BUF_SZ];
137+
int s_len;
138+
const char* s_ciphers;
139+
};
140+
141+
int test_memio_do_handshake(WOLFSSL *ssl_c, WOLFSSL *ssl_s,
142+
int max_rounds, int *rounds);
143+
int test_memio_setup(struct test_memio_ctx *ctx,
144+
WOLFSSL_CTX **ctx_c, WOLFSSL_CTX **ctx_s, WOLFSSL **ssl_c, WOLFSSL **ssl_s,
145+
method_provider method_c, method_provider method_s);
146+
147+
static WC_INLINE int test_memio_write_cb(WOLFSSL *ssl, char *data, int sz,
148+
void *ctx)
149+
{
150+
struct test_memio_ctx *test_ctx;
151+
byte *buf;
152+
int *len;
153+
154+
test_ctx = (struct test_memio_ctx*)ctx;
155+
156+
if (wolfSSL_GetSide(ssl) == WOLFSSL_SERVER_END) {
157+
buf = test_ctx->c_buff;
158+
len = &test_ctx->c_len;
159+
}
160+
else {
161+
buf = test_ctx->s_buff;
162+
len = &test_ctx->s_len;
163+
}
164+
165+
if ((unsigned)(*len + sz) > TEST_MEMIO_BUF_SZ)
166+
return WOLFSSL_CBIO_ERR_WANT_READ;
167+
168+
XMEMCPY(buf + *len, data, sz);
169+
*len += sz;
170+
171+
return sz;
172+
}
173+
174+
static WC_INLINE int test_memio_read_cb(WOLFSSL *ssl, char *data, int sz,
175+
void *ctx)
176+
{
177+
struct test_memio_ctx *test_ctx;
178+
int read_sz;
179+
byte *buf;
180+
int *len;
181+
182+
test_ctx = (struct test_memio_ctx*)ctx;
183+
184+
if (wolfSSL_GetSide(ssl) == WOLFSSL_SERVER_END) {
185+
buf = test_ctx->s_buff;
186+
len = &test_ctx->s_len;
187+
}
188+
else {
189+
buf = test_ctx->c_buff;
190+
len = &test_ctx->c_len;
191+
}
192+
193+
if (*len == 0)
194+
return WOLFSSL_CBIO_ERR_WANT_READ;
195+
196+
read_sz = sz < *len ? sz : *len;
197+
198+
XMEMCPY(data, buf, read_sz);
199+
XMEMMOVE(buf, buf + read_sz, *len - read_sz);
200+
201+
*len -= read_sz;
202+
203+
return read_sz;
204+
}
205+
206+
int test_memio_do_handshake(WOLFSSL *ssl_c, WOLFSSL *ssl_s,
207+
int max_rounds, int *rounds)
208+
{
209+
byte handshake_complete = 0, hs_c = 0, hs_s = 0;
210+
int ret, err;
211+
212+
if (rounds != NULL)
213+
*rounds = 0;
214+
while (!handshake_complete && max_rounds > 0) {
215+
if (!hs_c) {
216+
ret = wolfSSL_connect(ssl_c);
217+
if (ret == WOLFSSL_SUCCESS) {
218+
hs_c = 1;
219+
}
220+
else {
221+
err = wolfSSL_get_error(ssl_c, ret);
222+
if (err != WOLFSSL_ERROR_WANT_READ &&
223+
err != WOLFSSL_ERROR_WANT_WRITE)
224+
return -1;
225+
}
226+
}
227+
if (!hs_s) {
228+
ret = wolfSSL_accept(ssl_s);
229+
if (ret == WOLFSSL_SUCCESS) {
230+
hs_s = 1;
231+
}
232+
else {
233+
err = wolfSSL_get_error(ssl_s, ret);
234+
if (err != WOLFSSL_ERROR_WANT_READ &&
235+
err != WOLFSSL_ERROR_WANT_WRITE)
236+
return -1;
237+
}
238+
}
239+
handshake_complete = hs_c && hs_s;
240+
max_rounds--;
241+
if (rounds != NULL)
242+
*rounds = *rounds + 1;
243+
}
244+
245+
if (!handshake_complete)
246+
return -1;
247+
248+
return 0;
249+
}
250+
251+
int test_memio_setup(struct test_memio_ctx *ctx,
252+
WOLFSSL_CTX **ctx_c, WOLFSSL_CTX **ctx_s, WOLFSSL **ssl_c, WOLFSSL **ssl_s,
253+
method_provider method_c, method_provider method_s)
254+
{
255+
int ret;
256+
257+
if (ctx_c != NULL && *ctx_c == NULL) {
258+
*ctx_c = wolfSSL_CTX_new(method_c());
259+
if (*ctx_c == NULL)
260+
return -1;
261+
#ifndef NO_CERTS
262+
ret = wolfSSL_CTX_load_verify_locations(*ctx_c, caCertFile, 0);
263+
if (ret != WOLFSSL_SUCCESS)
264+
return -1;
265+
#endif /* NO_CERTS */
266+
wolfSSL_SetIORecv(*ctx_c, test_memio_read_cb);
267+
wolfSSL_SetIOSend(*ctx_c, test_memio_write_cb);
268+
if (ctx->c_ciphers != NULL) {
269+
ret = wolfSSL_CTX_set_cipher_list(*ctx_c, ctx->c_ciphers);
270+
if (ret != WOLFSSL_SUCCESS)
271+
return -1;
272+
}
273+
}
274+
275+
if (ctx_s != NULL && *ctx_s == NULL) {
276+
*ctx_s = wolfSSL_CTX_new(method_s());
277+
if (*ctx_s == NULL)
278+
return -1;
279+
#ifndef NO_CERTS
280+
ret = wolfSSL_CTX_use_PrivateKey_file(*ctx_s, svrKeyFile,
281+
WOLFSSL_FILETYPE_PEM);
282+
if (ret != WOLFSSL_SUCCESS)
283+
return- -1;
284+
ret = wolfSSL_CTX_use_certificate_file(*ctx_s, svrCertFile,
285+
WOLFSSL_FILETYPE_PEM);
286+
if (ret != WOLFSSL_SUCCESS)
287+
return -1;
288+
#endif
289+
wolfSSL_SetIORecv(*ctx_s, test_memio_read_cb);
290+
wolfSSL_SetIOSend(*ctx_s, test_memio_write_cb);
291+
if (ctx->s_ciphers != NULL) {
292+
ret = wolfSSL_CTX_set_cipher_list(*ctx_s, ctx->s_ciphers);
293+
if (ret != WOLFSSL_SUCCESS)
294+
return -1;
295+
}
296+
}
297+
298+
if (ctx_c != NULL && ssl_c != NULL) {
299+
*ssl_c = wolfSSL_new(*ctx_c);
300+
if (*ssl_c == NULL)
301+
return -1;
302+
wolfSSL_SetIOWriteCtx(*ssl_c, ctx);
303+
wolfSSL_SetIOReadCtx(*ssl_c, ctx);
304+
}
305+
if (ctx_s != NULL && ssl_s != NULL) {
306+
*ssl_s = wolfSSL_new(*ctx_s);
307+
if (*ssl_s == NULL)
308+
return -1;
309+
wolfSSL_SetIOWriteCtx(*ssl_s, ctx);
310+
wolfSSL_SetIOReadCtx(*ssl_s, ctx);
311+
#if !defined(NO_DH)
312+
SetDH(*ssl_s);
313+
#endif
314+
}
315+
316+
return 0;
317+
}
318+
#endif
319+
122320
#endif /* WOLFSSL_TEST_UTILS_INCLUDED */

0 commit comments

Comments
 (0)