]> git.ipfire.org Git - thirdparty/openssl.git/blame - test/ssltestlib.c
Delay setting the sig algs until after the cert_cb has been called
[thirdparty/openssl.git] / test / ssltestlib.c
CommitLineData
2cb4b5f6 1/*
b0edda11 2 * Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved.
2cb4b5f6
MC
3 *
4 * Licensed under the OpenSSL license (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
d82dec40
MC
10#include <string.h>
11
176db6dc 12#include "internal/nelem.h"
2cb4b5f6 13#include "ssltestlib.h"
8ed9a266 14#include "testutil.h"
61e96557
MC
15#include "e_os.h"
16
17#ifdef OPENSSL_SYS_UNIX
18# include <unistd.h>
19
20static ossl_inline void ossl_sleep(unsigned int millis) {
21 usleep(millis * 1000);
22}
23#elif defined(_WIN32)
24# include <windows.h>
25
26static ossl_inline void ossl_sleep(unsigned int millis) {
27 Sleep(millis);
28}
29#else
30/* Fallback to a busy wait */
31static ossl_inline void ossl_sleep(unsigned int millis) {
32 struct timeval start, now;
33 unsigned int elapsedms;
34
35 gettimeofday(&start, NULL);
36 do {
37 gettimeofday(&now, NULL);
38 elapsedms = (((now.tv_sec - start.tv_sec) * 1000000)
39 + now.tv_usec - start.tv_usec) / 1000;
40 } while (elapsedms < millis);
41}
42#endif
2cb4b5f6 43
d9a2e90b
MC
44static int tls_dump_new(BIO *bi);
45static int tls_dump_free(BIO *a);
46static int tls_dump_read(BIO *b, char *out, int outl);
47static int tls_dump_write(BIO *b, const char *in, int inl);
48static long tls_dump_ctrl(BIO *b, int cmd, long num, void *ptr);
49static int tls_dump_gets(BIO *bp, char *buf, int size);
50static int tls_dump_puts(BIO *bp, const char *str);
51
52/* Choose a sufficiently large type likely to be unused for this custom BIO */
8ed9a266
RS
53#define BIO_TYPE_TLS_DUMP_FILTER (0x80 | BIO_TYPE_FILTER)
54#define BIO_TYPE_MEMPACKET_TEST 0x81
d9a2e90b
MC
55
56static BIO_METHOD *method_tls_dump = NULL;
8ed9a266 57static BIO_METHOD *meth_mem = NULL;
d9a2e90b
MC
58
59/* Note: Not thread safe! */
60const BIO_METHOD *bio_f_tls_dump_filter(void)
61{
62 if (method_tls_dump == NULL) {
63 method_tls_dump = BIO_meth_new(BIO_TYPE_TLS_DUMP_FILTER,
64 "TLS dump filter");
65 if ( method_tls_dump == NULL
66 || !BIO_meth_set_write(method_tls_dump, tls_dump_write)
67 || !BIO_meth_set_read(method_tls_dump, tls_dump_read)
68 || !BIO_meth_set_puts(method_tls_dump, tls_dump_puts)
69 || !BIO_meth_set_gets(method_tls_dump, tls_dump_gets)
70 || !BIO_meth_set_ctrl(method_tls_dump, tls_dump_ctrl)
71 || !BIO_meth_set_create(method_tls_dump, tls_dump_new)
72 || !BIO_meth_set_destroy(method_tls_dump, tls_dump_free))
73 return NULL;
74 }
75 return method_tls_dump;
76}
77
78void bio_f_tls_dump_filter_free(void)
79{
80 BIO_meth_free(method_tls_dump);
81}
82
83static int tls_dump_new(BIO *bio)
84{
85 BIO_set_init(bio, 1);
86 return 1;
87}
88
89static int tls_dump_free(BIO *bio)
90{
91 BIO_set_init(bio, 0);
92
93 return 1;
94}
95
96static void copy_flags(BIO *bio)
97{
98 int flags;
99 BIO *next = BIO_next(bio);
100
101 flags = BIO_test_flags(next, BIO_FLAGS_SHOULD_RETRY | BIO_FLAGS_RWS);
102 BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY | BIO_FLAGS_RWS);
103 BIO_set_flags(bio, flags);
104}
105
106#define RECORD_CONTENT_TYPE 0
107#define RECORD_VERSION_HI 1
108#define RECORD_VERSION_LO 2
109#define RECORD_EPOCH_HI 3
110#define RECORD_EPOCH_LO 4
111#define RECORD_SEQUENCE_START 5
112#define RECORD_SEQUENCE_END 10
113#define RECORD_LEN_HI 11
114#define RECORD_LEN_LO 12
115
116#define MSG_TYPE 0
117#define MSG_LEN_HI 1
118#define MSG_LEN_MID 2
119#define MSG_LEN_LO 3
120#define MSG_SEQ_HI 4
121#define MSG_SEQ_LO 5
122#define MSG_FRAG_OFF_HI 6
123#define MSG_FRAG_OFF_MID 7
124#define MSG_FRAG_OFF_LO 8
125#define MSG_FRAG_LEN_HI 9
126#define MSG_FRAG_LEN_MID 10
127#define MSG_FRAG_LEN_LO 11
128
129
130static void dump_data(const char *data, int len)
131{
132 int rem, i, content, reclen, msglen, fragoff, fraglen, epoch;
133 unsigned char *rec;
134
135 printf("---- START OF PACKET ----\n");
136
137 rem = len;
138 rec = (unsigned char *)data;
139
140 while (rem > 0) {
141 if (rem != len)
142 printf("*\n");
143 printf("*---- START OF RECORD ----\n");
144 if (rem < DTLS1_RT_HEADER_LENGTH) {
145 printf("*---- RECORD TRUNCATED ----\n");
146 break;
147 }
148 content = rec[RECORD_CONTENT_TYPE];
149 printf("** Record Content-type: %d\n", content);
150 printf("** Record Version: %02x%02x\n",
151 rec[RECORD_VERSION_HI], rec[RECORD_VERSION_LO]);
152 epoch = (rec[RECORD_EPOCH_HI] << 8) | rec[RECORD_EPOCH_LO];
153 printf("** Record Epoch: %d\n", epoch);
154 printf("** Record Sequence: ");
155 for (i = RECORD_SEQUENCE_START; i <= RECORD_SEQUENCE_END; i++)
156 printf("%02x", rec[i]);
157 reclen = (rec[RECORD_LEN_HI] << 8) | rec[RECORD_LEN_LO];
158 printf("\n** Record Length: %d\n", reclen);
159
160 /* Now look at message */
161 rec += DTLS1_RT_HEADER_LENGTH;
162 rem -= DTLS1_RT_HEADER_LENGTH;
163 if (content == SSL3_RT_HANDSHAKE) {
164 printf("**---- START OF HANDSHAKE MESSAGE FRAGMENT ----\n");
165 if (epoch > 0) {
166 printf("**---- HANDSHAKE MESSAGE FRAGMENT ENCRYPTED ----\n");
167 } else if (rem < DTLS1_HM_HEADER_LENGTH
168 || reclen < DTLS1_HM_HEADER_LENGTH) {
169 printf("**---- HANDSHAKE MESSAGE FRAGMENT TRUNCATED ----\n");
170 } else {
171 printf("*** Message Type: %d\n", rec[MSG_TYPE]);
172 msglen = (rec[MSG_LEN_HI] << 16) | (rec[MSG_LEN_MID] << 8)
173 | rec[MSG_LEN_LO];
174 printf("*** Message Length: %d\n", msglen);
175 printf("*** Message sequence: %d\n",
176 (rec[MSG_SEQ_HI] << 8) | rec[MSG_SEQ_LO]);
177 fragoff = (rec[MSG_FRAG_OFF_HI] << 16)
178 | (rec[MSG_FRAG_OFF_MID] << 8)
179 | rec[MSG_FRAG_OFF_LO];
180 printf("*** Message Fragment offset: %d\n", fragoff);
181 fraglen = (rec[MSG_FRAG_LEN_HI] << 16)
182 | (rec[MSG_FRAG_LEN_MID] << 8)
183 | rec[MSG_FRAG_LEN_LO];
184 printf("*** Message Fragment len: %d\n", fraglen);
185 if (fragoff + fraglen > msglen)
186 printf("***---- HANDSHAKE MESSAGE FRAGMENT INVALID ----\n");
28b86f31 187 else if (reclen < fraglen)
d9a2e90b
MC
188 printf("**---- HANDSHAKE MESSAGE FRAGMENT TRUNCATED ----\n");
189 else
190 printf("**---- END OF HANDSHAKE MESSAGE FRAGMENT ----\n");
191 }
192 }
193 if (rem < reclen) {
194 printf("*---- RECORD TRUNCATED ----\n");
195 rem = 0;
196 } else {
197 rec += reclen;
198 rem -= reclen;
199 printf("*---- END OF RECORD ----\n");
200 }
201 }
202 printf("---- END OF PACKET ----\n\n");
203 fflush(stdout);
204}
205
206static int tls_dump_read(BIO *bio, char *out, int outl)
207{
208 int ret;
209 BIO *next = BIO_next(bio);
210
211 ret = BIO_read(next, out, outl);
212 copy_flags(bio);
213
214 if (ret > 0) {
215 dump_data(out, ret);
216 }
217
218 return ret;
219}
220
221static int tls_dump_write(BIO *bio, const char *in, int inl)
222{
223 int ret;
224 BIO *next = BIO_next(bio);
225
226 ret = BIO_write(next, in, inl);
227 copy_flags(bio);
228
229 return ret;
230}
231
232static long tls_dump_ctrl(BIO *bio, int cmd, long num, void *ptr)
233{
234 long ret;
235 BIO *next = BIO_next(bio);
236
237 if (next == NULL)
238 return 0;
239
240 switch (cmd) {
241 case BIO_CTRL_DUP:
242 ret = 0L;
243 break;
244 default:
245 ret = BIO_ctrl(next, cmd, num, ptr);
246 break;
247 }
248 return ret;
249}
250
251static int tls_dump_gets(BIO *bio, char *buf, int size)
252{
253 /* We don't support this - not needed anyway */
254 return -1;
255}
256
257static int tls_dump_puts(BIO *bio, const char *str)
258{
259 return tls_dump_write(bio, str, strlen(str));
260}
261
d82dec40 262
0556f2aa 263struct mempacket_st {
d82dec40
MC
264 unsigned char *data;
265 int len;
266 unsigned int num;
267 unsigned int type;
0556f2aa 268};
d82dec40 269
d82dec40
MC
270static void mempacket_free(MEMPACKET *pkt)
271{
272 if (pkt->data != NULL)
273 OPENSSL_free(pkt->data);
274 OPENSSL_free(pkt);
275}
276
277typedef struct mempacket_test_ctx_st {
278 STACK_OF(MEMPACKET) *pkts;
279 unsigned int epoch;
280 unsigned int currrec;
281 unsigned int currpkt;
282 unsigned int lastpkt;
61e96557 283 unsigned int injected;
d82dec40 284 unsigned int noinject;
61e96557
MC
285 unsigned int dropepoch;
286 int droprec;
d82dec40
MC
287} MEMPACKET_TEST_CTX;
288
289static int mempacket_test_new(BIO *bi);
290static int mempacket_test_free(BIO *a);
291static int mempacket_test_read(BIO *b, char *out, int outl);
292static int mempacket_test_write(BIO *b, const char *in, int inl);
293static long mempacket_test_ctrl(BIO *b, int cmd, long num, void *ptr);
294static int mempacket_test_gets(BIO *bp, char *buf, int size);
295static int mempacket_test_puts(BIO *bp, const char *str);
296
297const BIO_METHOD *bio_s_mempacket_test(void)
298{
8ed9a266
RS
299 if (meth_mem == NULL) {
300 if (!TEST_ptr(meth_mem = BIO_meth_new(BIO_TYPE_MEMPACKET_TEST,
301 "Mem Packet Test"))
302 || !TEST_true(BIO_meth_set_write(meth_mem, mempacket_test_write))
303 || !TEST_true(BIO_meth_set_read(meth_mem, mempacket_test_read))
304 || !TEST_true(BIO_meth_set_puts(meth_mem, mempacket_test_puts))
305 || !TEST_true(BIO_meth_set_gets(meth_mem, mempacket_test_gets))
306 || !TEST_true(BIO_meth_set_ctrl(meth_mem, mempacket_test_ctrl))
307 || !TEST_true(BIO_meth_set_create(meth_mem, mempacket_test_new))
308 || !TEST_true(BIO_meth_set_destroy(meth_mem, mempacket_test_free)))
d82dec40
MC
309 return NULL;
310 }
8ed9a266 311 return meth_mem;
d82dec40
MC
312}
313
314void bio_s_mempacket_test_free(void)
315{
8ed9a266 316 BIO_meth_free(meth_mem);
d82dec40
MC
317}
318
319static int mempacket_test_new(BIO *bio)
320{
8ed9a266 321 MEMPACKET_TEST_CTX *ctx;
bd91e3c8 322
8ed9a266 323 if (!TEST_ptr(ctx = OPENSSL_zalloc(sizeof(*ctx))))
d82dec40 324 return 0;
8ed9a266 325 if (!TEST_ptr(ctx->pkts = sk_MEMPACKET_new_null())) {
d82dec40
MC
326 OPENSSL_free(ctx);
327 return 0;
328 }
61e96557
MC
329 ctx->dropepoch = 0;
330 ctx->droprec = -1;
d82dec40
MC
331 BIO_set_init(bio, 1);
332 BIO_set_data(bio, ctx);
333 return 1;
334}
335
336static int mempacket_test_free(BIO *bio)
337{
338 MEMPACKET_TEST_CTX *ctx = BIO_get_data(bio);
339
340 sk_MEMPACKET_pop_free(ctx->pkts, mempacket_free);
341 OPENSSL_free(ctx);
342 BIO_set_data(bio, NULL);
343 BIO_set_init(bio, 0);
d82dec40
MC
344 return 1;
345}
346
347/* Record Header values */
61e96557
MC
348#define EPOCH_HI 3
349#define EPOCH_LO 4
d82dec40
MC
350#define RECORD_SEQUENCE 10
351#define RECORD_LEN_HI 11
352#define RECORD_LEN_LO 12
353
354#define STANDARD_PACKET 0
355
356static int mempacket_test_read(BIO *bio, char *out, int outl)
357{
358 MEMPACKET_TEST_CTX *ctx = BIO_get_data(bio);
359 MEMPACKET *thispkt;
360 unsigned char *rec;
361 int rem;
362 unsigned int seq, offset, len, epoch;
363
364 BIO_clear_retry_flags(bio);
d82dec40
MC
365 thispkt = sk_MEMPACKET_value(ctx->pkts, 0);
366 if (thispkt == NULL || thispkt->num != ctx->currpkt) {
367 /* Probably run out of data */
368 BIO_set_retry_read(bio);
369 return -1;
370 }
1c288878 371 (void)sk_MEMPACKET_shift(ctx->pkts);
d82dec40
MC
372 ctx->currpkt++;
373
374 if (outl > thispkt->len)
375 outl = thispkt->len;
376
61e96557
MC
377 if (thispkt->type != INJECT_PACKET_IGNORE_REC_SEQ
378 && (ctx->injected || ctx->droprec >= 0)) {
d82dec40
MC
379 /*
380 * Overwrite the record sequence number. We strictly number them in
381 * the order received. Since we are actually a reliable transport
382 * we know that there won't be any re-ordering. We overwrite to deal
383 * with any packets that have been injected
384 */
61e96557 385 for (rem = thispkt->len, rec = thispkt->data; rem > 0; rem -= len) {
8ed9a266 386 if (rem < DTLS1_RT_HEADER_LENGTH)
d82dec40 387 return -1;
d82dec40
MC
388 epoch = (rec[EPOCH_HI] << 8) | rec[EPOCH_LO];
389 if (epoch != ctx->epoch) {
390 ctx->epoch = epoch;
391 ctx->currrec = 0;
392 }
393 seq = ctx->currrec;
394 offset = 0;
395 do {
396 rec[RECORD_SEQUENCE - offset] = seq & 0xFF;
397 seq >>= 8;
398 offset++;
399 } while (seq > 0);
d82dec40
MC
400
401 len = ((rec[RECORD_LEN_HI] << 8) | rec[RECORD_LEN_LO])
402 + DTLS1_RT_HEADER_LENGTH;
61e96557
MC
403 if (rem < (int)len)
404 return -1;
405 if (ctx->droprec == (int)ctx->currrec && ctx->dropepoch == epoch) {
406 if (rem > (int)len)
407 memmove(rec, rec + len, rem - len);
408 outl -= len;
409 ctx->droprec = -1;
410 if (outl == 0)
411 BIO_set_retry_read(bio);
412 } else {
413 rec += len;
414 }
415
416 ctx->currrec++;
d82dec40
MC
417 }
418 }
419
420 memcpy(out, thispkt->data, outl);
d82dec40 421 mempacket_free(thispkt);
d82dec40
MC
422 return outl;
423}
424
425int mempacket_test_inject(BIO *bio, const char *in, int inl, int pktnum,
426 int type)
427{
428 MEMPACKET_TEST_CTX *ctx = BIO_get_data(bio);
429 MEMPACKET *thispkt, *looppkt, *nextpkt;
430 int i;
431
432 if (ctx == NULL)
433 return -1;
434
435 /* We only allow injection before we've started writing any data */
436 if (pktnum >= 0) {
437 if (ctx->noinject)
438 return -1;
61e96557 439 ctx->injected = 1;
d82dec40
MC
440 } else {
441 ctx->noinject = 1;
442 }
443
8ed9a266 444 if (!TEST_ptr(thispkt = OPENSSL_malloc(sizeof(*thispkt))))
d82dec40 445 return -1;
8ed9a266 446 if (!TEST_ptr(thispkt->data = OPENSSL_malloc(inl))) {
d82dec40
MC
447 mempacket_free(thispkt);
448 return -1;
449 }
450
451 memcpy(thispkt->data, in, inl);
452 thispkt->len = inl;
453 thispkt->num = (pktnum >= 0) ? (unsigned int)pktnum : ctx->lastpkt;
454 thispkt->type = type;
455
456 for(i = 0; (looppkt = sk_MEMPACKET_value(ctx->pkts, i)) != NULL; i++) {
457 /* Check if we found the right place to insert this packet */
458 if (looppkt->num > thispkt->num) {
459 if (sk_MEMPACKET_insert(ctx->pkts, thispkt, i) == 0) {
460 mempacket_free(thispkt);
461 return -1;
462 }
463 /* If we're doing up front injection then we're done */
464 if (pktnum >= 0)
465 return inl;
466 /*
467 * We need to do some accounting on lastpkt. We increment it first,
468 * but it might now equal the value of injected packets, so we need
469 * to skip over those
470 */
471 ctx->lastpkt++;
472 do {
473 i++;
474 nextpkt = sk_MEMPACKET_value(ctx->pkts, i);
475 if (nextpkt != NULL && nextpkt->num == ctx->lastpkt)
476 ctx->lastpkt++;
477 else
478 return inl;
479 } while(1);
28b86f31 480 } else if (looppkt->num == thispkt->num) {
d82dec40
MC
481 if (!ctx->noinject) {
482 /* We injected two packets with the same packet number! */
483 return -1;
484 }
485 ctx->lastpkt++;
486 thispkt->num++;
487 }
488 }
489 /*
490 * We didn't find any packets with a packet number equal to or greater than
491 * this one, so we just add it onto the end
492 */
493 if (!sk_MEMPACKET_push(ctx->pkts, thispkt)) {
494 mempacket_free(thispkt);
495 return -1;
496 }
497
498 if (pktnum < 0)
499 ctx->lastpkt++;
500
501 return inl;
502}
503
504static int mempacket_test_write(BIO *bio, const char *in, int inl)
505{
506 return mempacket_test_inject(bio, in, inl, -1, STANDARD_PACKET);
507}
508
509static long mempacket_test_ctrl(BIO *bio, int cmd, long num, void *ptr)
510{
511 long ret = 1;
512 MEMPACKET_TEST_CTX *ctx = BIO_get_data(bio);
513 MEMPACKET *thispkt;
514
515 switch (cmd) {
516 case BIO_CTRL_EOF:
517 ret = (long)(sk_MEMPACKET_num(ctx->pkts) == 0);
518 break;
519 case BIO_CTRL_GET_CLOSE:
520 ret = BIO_get_shutdown(bio);
521 break;
522 case BIO_CTRL_SET_CLOSE:
523 BIO_set_shutdown(bio, (int)num);
524 break;
525 case BIO_CTRL_WPENDING:
526 ret = 0L;
527 break;
528 case BIO_CTRL_PENDING:
529 thispkt = sk_MEMPACKET_value(ctx->pkts, 0);
530 if (thispkt == NULL)
531 ret = 0;
532 else
533 ret = thispkt->len;
534 break;
535 case BIO_CTRL_FLUSH:
536 ret = 1;
537 break;
61e96557
MC
538 case MEMPACKET_CTRL_SET_DROP_EPOCH:
539 ctx->dropepoch = (unsigned int)num;
540 break;
541 case MEMPACKET_CTRL_SET_DROP_REC:
542 ctx->droprec = (int)num;
543 break;
544 case MEMPACKET_CTRL_GET_DROP_REC:
545 ret = ctx->droprec;
546 break;
d82dec40
MC
547 case BIO_CTRL_RESET:
548 case BIO_CTRL_DUP:
549 case BIO_CTRL_PUSH:
550 case BIO_CTRL_POP:
551 default:
552 ret = 0;
553 break;
554 }
555 return ret;
556}
557
558static int mempacket_test_gets(BIO *bio, char *buf, int size)
559{
560 /* We don't support this - not needed anyway */
561 return -1;
562}
563
564static int mempacket_test_puts(BIO *bio, const char *str)
565{
566 return mempacket_test_write(bio, str, strlen(str));
567}
568
2cb4b5f6 569int create_ssl_ctx_pair(const SSL_METHOD *sm, const SSL_METHOD *cm,
7d7f6834 570 int min_proto_version, int max_proto_version,
2cb4b5f6
MC
571 SSL_CTX **sctx, SSL_CTX **cctx, char *certfile,
572 char *privkeyfile)
573{
574 SSL_CTX *serverctx = NULL;
575 SSL_CTX *clientctx = NULL;
576
8ed9a266 577 if (!TEST_ptr(serverctx = SSL_CTX_new(sm))
bb01ef3f 578 || (cctx != NULL && !TEST_ptr(clientctx = SSL_CTX_new(cm))))
2cb4b5f6 579 goto err;
2cb4b5f6 580
7d7f6834
RL
581 if ((min_proto_version > 0
582 && !TEST_true(SSL_CTX_set_min_proto_version(serverctx,
583 min_proto_version)))
584 || (max_proto_version > 0
585 && !TEST_true(SSL_CTX_set_max_proto_version(serverctx,
586 max_proto_version))))
587 goto err;
588 if (clientctx != NULL
589 && ((min_proto_version > 0
6021d8ec 590 && !TEST_true(SSL_CTX_set_min_proto_version(clientctx,
7d7f6834
RL
591 min_proto_version)))
592 || (max_proto_version > 0
6021d8ec 593 && !TEST_true(SSL_CTX_set_max_proto_version(clientctx,
7d7f6834
RL
594 max_proto_version)))))
595 goto err;
596
0d8da779
MC
597 if (certfile != NULL && privkeyfile != NULL) {
598 if (!TEST_int_eq(SSL_CTX_use_certificate_file(serverctx, certfile,
599 SSL_FILETYPE_PEM), 1)
600 || !TEST_int_eq(SSL_CTX_use_PrivateKey_file(serverctx,
601 privkeyfile,
602 SSL_FILETYPE_PEM), 1)
603 || !TEST_int_eq(SSL_CTX_check_private_key(serverctx), 1))
604 goto err;
605 }
2cb4b5f6 606
c5a56992
AP
607#ifndef OPENSSL_NO_DH
608 SSL_CTX_set_dh_auto(serverctx, 1);
609#endif
610
2cb4b5f6 611 *sctx = serverctx;
bb01ef3f
MC
612 if (cctx != NULL)
613 *cctx = clientctx;
2cb4b5f6 614 return 1;
8ed9a266 615
2cb4b5f6
MC
616 err:
617 SSL_CTX_free(serverctx);
618 SSL_CTX_free(clientctx);
619 return 0;
620}
621
9970290e 622#define MAXLOOPS 1000000
2cb4b5f6
MC
623
624/*
625 * NOTE: Transfers control of the BIOs - this function will free them on error
626 */
b4982125 627int create_ssl_objects(SSL_CTX *serverctx, SSL_CTX *clientctx, SSL **sssl,
2cb4b5f6
MC
628 SSL **cssl, BIO *s_to_c_fbio, BIO *c_to_s_fbio)
629{
8ed9a266 630 SSL *serverssl = NULL, *clientssl = NULL;
2cb4b5f6
MC
631 BIO *s_to_c_bio = NULL, *c_to_s_bio = NULL;
632
8ed9a266 633 if (*sssl != NULL)
eaa776da 634 serverssl = *sssl;
8ed9a266
RS
635 else if (!TEST_ptr(serverssl = SSL_new(serverctx)))
636 goto error;
637 if (*cssl != NULL)
eaa776da 638 clientssl = *cssl;
8ed9a266 639 else if (!TEST_ptr(clientssl = SSL_new(clientctx)))
2cb4b5f6 640 goto error;
2cb4b5f6 641
b4982125 642 if (SSL_is_dtls(clientssl)) {
8ed9a266
RS
643 if (!TEST_ptr(s_to_c_bio = BIO_new(bio_s_mempacket_test()))
644 || !TEST_ptr(c_to_s_bio = BIO_new(bio_s_mempacket_test())))
645 goto error;
b4982125 646 } else {
8ed9a266
RS
647 if (!TEST_ptr(s_to_c_bio = BIO_new(BIO_s_mem()))
648 || !TEST_ptr(c_to_s_bio = BIO_new(BIO_s_mem())))
649 goto error;
2cb4b5f6
MC
650 }
651
8ed9a266
RS
652 if (s_to_c_fbio != NULL
653 && !TEST_ptr(s_to_c_bio = BIO_push(s_to_c_fbio, s_to_c_bio)))
654 goto error;
655 if (c_to_s_fbio != NULL
656 && !TEST_ptr(c_to_s_bio = BIO_push(c_to_s_fbio, c_to_s_bio)))
2cb4b5f6 657 goto error;
2cb4b5f6
MC
658
659 /* Set Non-blocking IO behaviour */
660 BIO_set_mem_eof_return(s_to_c_bio, -1);
661 BIO_set_mem_eof_return(c_to_s_bio, -1);
662
663 /* Up ref these as we are passing them to two SSL objects */
8ed9a266 664 SSL_set_bio(serverssl, c_to_s_bio, s_to_c_bio);
2cb4b5f6
MC
665 BIO_up_ref(s_to_c_bio);
666 BIO_up_ref(c_to_s_bio);
2cb4b5f6 667 SSL_set_bio(clientssl, s_to_c_bio, c_to_s_bio);
b4982125
MC
668 *sssl = serverssl;
669 *cssl = clientssl;
b4982125
MC
670 return 1;
671
672 error:
673 SSL_free(serverssl);
674 SSL_free(clientssl);
675 BIO_free(s_to_c_bio);
676 BIO_free(c_to_s_bio);
677 BIO_free(s_to_c_fbio);
678 BIO_free(c_to_s_fbio);
679
680 return 0;
681}
682
c748834f
MC
683/*
684 * Create an SSL connection, but does not ready any post-handshake
685 * NewSessionTicket messages.
686 */
687int create_bare_ssl_connection(SSL *serverssl, SSL *clientssl, int want)
b4982125 688{
c748834f 689 int retc = -1, rets = -1, err, abortctr = 0;
b4982125 690 int clienterr = 0, servererr = 0;
61e96557 691 int isdtls = SSL_is_dtls(serverssl);
b4982125 692
2cb4b5f6
MC
693 do {
694 err = SSL_ERROR_WANT_WRITE;
eaa776da 695 while (!clienterr && retc <= 0 && err == SSL_ERROR_WANT_WRITE) {
2cb4b5f6
MC
696 retc = SSL_connect(clientssl);
697 if (retc <= 0)
698 err = SSL_get_error(clientssl, retc);
699 }
700
eaa776da 701 if (!clienterr && retc <= 0 && err != SSL_ERROR_WANT_READ) {
8ed9a266 702 TEST_info("SSL_connect() failed %d, %d", retc, err);
eaa776da 703 clienterr = 1;
2cb4b5f6 704 }
8e2236ef
BK
705 if (want != SSL_ERROR_NONE && err == want)
706 return 0;
2cb4b5f6
MC
707
708 err = SSL_ERROR_WANT_WRITE;
eaa776da 709 while (!servererr && rets <= 0 && err == SSL_ERROR_WANT_WRITE) {
2cb4b5f6
MC
710 rets = SSL_accept(serverssl);
711 if (rets <= 0)
712 err = SSL_get_error(serverssl, rets);
713 }
714
eaa776da 715 if (!servererr && rets <= 0 && err != SSL_ERROR_WANT_READ) {
8ed9a266 716 TEST_info("SSL_accept() failed %d, %d", rets, err);
eaa776da 717 servererr = 1;
2cb4b5f6 718 }
8e2236ef
BK
719 if (want != SSL_ERROR_NONE && err == want)
720 return 0;
eaa776da 721 if (clienterr && servererr)
b4982125 722 return 0;
61e96557
MC
723 if (isdtls) {
724 if (rets > 0 && retc <= 0)
725 DTLSv1_handle_timeout(serverssl);
726 if (retc > 0 && rets <= 0)
727 DTLSv1_handle_timeout(clientssl);
728 }
2cb4b5f6 729 if (++abortctr == MAXLOOPS) {
8ed9a266 730 TEST_info("No progress made");
b4982125 731 return 0;
2cb4b5f6 732 }
61e96557
MC
733 if (isdtls && abortctr <= 50 && (abortctr % 10) == 0) {
734 /*
735 * It looks like we're just spinning. Pause for a short period to
736 * give the DTLS timer a chance to do something. We only do this for
737 * the first few times to prevent hangs.
738 */
739 ossl_sleep(50);
740 }
2cb4b5f6
MC
741 } while (retc <=0 || rets <= 0);
742
c748834f
MC
743 return 1;
744}
745
746/*
747 * Create an SSL connection including any post handshake NewSessionTicket
748 * messages.
749 */
750int create_ssl_connection(SSL *serverssl, SSL *clientssl, int want)
751{
752 int i;
753 unsigned char buf;
754 size_t readbytes;
755
756 if (!create_bare_ssl_connection(serverssl, clientssl, want))
757 return 0;
758
59db06f1
MC
759 /*
760 * We attempt to read some data on the client side which we expect to fail.
761 * This will ensure we have received the NewSessionTicket in TLSv1.3 where
36ff232c 762 * appropriate. We do this twice because there are 2 NewSesionTickets.
59db06f1 763 */
36ff232c
MC
764 for (i = 0; i < 2; i++) {
765 if (SSL_read_ex(clientssl, &buf, sizeof(buf), &readbytes) > 0) {
766 if (!TEST_ulong_eq(readbytes, 0))
767 return 0;
768 } else if (!TEST_int_eq(SSL_get_error(clientssl, 0),
769 SSL_ERROR_WANT_READ)) {
59db06f1 770 return 0;
36ff232c 771 }
59db06f1
MC
772 }
773
2cb4b5f6 774 return 1;
2cb4b5f6 775}
ca8c71ba
MC
776
777void shutdown_ssl_connection(SSL *serverssl, SSL *clientssl)
778{
779 SSL_shutdown(clientssl);
780 SSL_shutdown(serverssl);
781 SSL_free(serverssl);
782 SSL_free(clientssl);
783}