2 * Copyright 2016-2019 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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
12 #include "internal/nelem.h"
13 #include "ssltestlib.h"
17 #ifdef OPENSSL_SYS_UNIX
19 #ifndef OPENSSL_NO_KTLS
20 # include <netinet/in.h>
21 # include <netinet/in.h>
22 # include <arpa/inet.h>
23 # include <sys/socket.h>
28 static ossl_inline void ossl_sleep(unsigned int millis)
30 # ifdef OPENSSL_SYS_VXWORKS
32 ts.tv_sec = (long int) (millis / 1000);
33 ts.tv_nsec = (long int) (millis % 1000) * 1000000ul;
36 usleep(millis * 1000);
42 static ossl_inline void ossl_sleep(unsigned int millis)
47 /* Fallback to a busy wait */
48 static ossl_inline void ossl_sleep(unsigned int millis)
50 struct timeval start, now;
51 unsigned int elapsedms;
53 gettimeofday(&start, NULL);
55 gettimeofday(&now, NULL);
56 elapsedms = (((now.tv_sec - start.tv_sec) * 1000000)
57 + now.tv_usec - start.tv_usec) / 1000;
58 } while (elapsedms < millis);
62 static int tls_dump_new(BIO *bi);
63 static int tls_dump_free(BIO *a);
64 static int tls_dump_read(BIO *b, char *out, int outl);
65 static int tls_dump_write(BIO *b, const char *in, int inl);
66 static long tls_dump_ctrl(BIO *b, int cmd, long num, void *ptr);
67 static int tls_dump_gets(BIO *bp, char *buf, int size);
68 static int tls_dump_puts(BIO *bp, const char *str);
70 /* Choose a sufficiently large type likely to be unused for this custom BIO */
71 #define BIO_TYPE_TLS_DUMP_FILTER (0x80 | BIO_TYPE_FILTER)
72 #define BIO_TYPE_MEMPACKET_TEST 0x81
73 #define BIO_TYPE_ALWAYS_RETRY 0x82
75 static BIO_METHOD *method_tls_dump = NULL;
76 static BIO_METHOD *meth_mem = NULL;
77 static BIO_METHOD *meth_always_retry = NULL;
79 /* Note: Not thread safe! */
80 const BIO_METHOD *bio_f_tls_dump_filter(void)
82 if (method_tls_dump == NULL) {
83 method_tls_dump = BIO_meth_new(BIO_TYPE_TLS_DUMP_FILTER,
85 if ( method_tls_dump == NULL
86 || !BIO_meth_set_write(method_tls_dump, tls_dump_write)
87 || !BIO_meth_set_read(method_tls_dump, tls_dump_read)
88 || !BIO_meth_set_puts(method_tls_dump, tls_dump_puts)
89 || !BIO_meth_set_gets(method_tls_dump, tls_dump_gets)
90 || !BIO_meth_set_ctrl(method_tls_dump, tls_dump_ctrl)
91 || !BIO_meth_set_create(method_tls_dump, tls_dump_new)
92 || !BIO_meth_set_destroy(method_tls_dump, tls_dump_free))
95 return method_tls_dump;
98 void bio_f_tls_dump_filter_free(void)
100 BIO_meth_free(method_tls_dump);
103 static int tls_dump_new(BIO *bio)
105 BIO_set_init(bio, 1);
109 static int tls_dump_free(BIO *bio)
111 BIO_set_init(bio, 0);
116 static void copy_flags(BIO *bio)
119 BIO *next = BIO_next(bio);
121 flags = BIO_test_flags(next, BIO_FLAGS_SHOULD_RETRY | BIO_FLAGS_RWS);
122 BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY | BIO_FLAGS_RWS);
123 BIO_set_flags(bio, flags);
126 #define RECORD_CONTENT_TYPE 0
127 #define RECORD_VERSION_HI 1
128 #define RECORD_VERSION_LO 2
129 #define RECORD_EPOCH_HI 3
130 #define RECORD_EPOCH_LO 4
131 #define RECORD_SEQUENCE_START 5
132 #define RECORD_SEQUENCE_END 10
133 #define RECORD_LEN_HI 11
134 #define RECORD_LEN_LO 12
138 #define MSG_LEN_MID 2
142 #define MSG_FRAG_OFF_HI 6
143 #define MSG_FRAG_OFF_MID 7
144 #define MSG_FRAG_OFF_LO 8
145 #define MSG_FRAG_LEN_HI 9
146 #define MSG_FRAG_LEN_MID 10
147 #define MSG_FRAG_LEN_LO 11
150 static void dump_data(const char *data, int len)
152 int rem, i, content, reclen, msglen, fragoff, fraglen, epoch;
155 printf("---- START OF PACKET ----\n");
158 rec = (unsigned char *)data;
163 printf("*---- START OF RECORD ----\n");
164 if (rem < DTLS1_RT_HEADER_LENGTH) {
165 printf("*---- RECORD TRUNCATED ----\n");
168 content = rec[RECORD_CONTENT_TYPE];
169 printf("** Record Content-type: %d\n", content);
170 printf("** Record Version: %02x%02x\n",
171 rec[RECORD_VERSION_HI], rec[RECORD_VERSION_LO]);
172 epoch = (rec[RECORD_EPOCH_HI] << 8) | rec[RECORD_EPOCH_LO];
173 printf("** Record Epoch: %d\n", epoch);
174 printf("** Record Sequence: ");
175 for (i = RECORD_SEQUENCE_START; i <= RECORD_SEQUENCE_END; i++)
176 printf("%02x", rec[i]);
177 reclen = (rec[RECORD_LEN_HI] << 8) | rec[RECORD_LEN_LO];
178 printf("\n** Record Length: %d\n", reclen);
180 /* Now look at message */
181 rec += DTLS1_RT_HEADER_LENGTH;
182 rem -= DTLS1_RT_HEADER_LENGTH;
183 if (content == SSL3_RT_HANDSHAKE) {
184 printf("**---- START OF HANDSHAKE MESSAGE FRAGMENT ----\n");
186 printf("**---- HANDSHAKE MESSAGE FRAGMENT ENCRYPTED ----\n");
187 } else if (rem < DTLS1_HM_HEADER_LENGTH
188 || reclen < DTLS1_HM_HEADER_LENGTH) {
189 printf("**---- HANDSHAKE MESSAGE FRAGMENT TRUNCATED ----\n");
191 printf("*** Message Type: %d\n", rec[MSG_TYPE]);
192 msglen = (rec[MSG_LEN_HI] << 16) | (rec[MSG_LEN_MID] << 8)
194 printf("*** Message Length: %d\n", msglen);
195 printf("*** Message sequence: %d\n",
196 (rec[MSG_SEQ_HI] << 8) | rec[MSG_SEQ_LO]);
197 fragoff = (rec[MSG_FRAG_OFF_HI] << 16)
198 | (rec[MSG_FRAG_OFF_MID] << 8)
199 | rec[MSG_FRAG_OFF_LO];
200 printf("*** Message Fragment offset: %d\n", fragoff);
201 fraglen = (rec[MSG_FRAG_LEN_HI] << 16)
202 | (rec[MSG_FRAG_LEN_MID] << 8)
203 | rec[MSG_FRAG_LEN_LO];
204 printf("*** Message Fragment len: %d\n", fraglen);
205 if (fragoff + fraglen > msglen)
206 printf("***---- HANDSHAKE MESSAGE FRAGMENT INVALID ----\n");
207 else if (reclen < fraglen)
208 printf("**---- HANDSHAKE MESSAGE FRAGMENT TRUNCATED ----\n");
210 printf("**---- END OF HANDSHAKE MESSAGE FRAGMENT ----\n");
214 printf("*---- RECORD TRUNCATED ----\n");
219 printf("*---- END OF RECORD ----\n");
222 printf("---- END OF PACKET ----\n\n");
226 static int tls_dump_read(BIO *bio, char *out, int outl)
229 BIO *next = BIO_next(bio);
231 ret = BIO_read(next, out, outl);
241 static int tls_dump_write(BIO *bio, const char *in, int inl)
244 BIO *next = BIO_next(bio);
246 ret = BIO_write(next, in, inl);
252 static long tls_dump_ctrl(BIO *bio, int cmd, long num, void *ptr)
255 BIO *next = BIO_next(bio);
265 ret = BIO_ctrl(next, cmd, num, ptr);
271 static int tls_dump_gets(BIO *bio, char *buf, int size)
273 /* We don't support this - not needed anyway */
277 static int tls_dump_puts(BIO *bio, const char *str)
279 return tls_dump_write(bio, str, strlen(str));
283 struct mempacket_st {
290 static void mempacket_free(MEMPACKET *pkt)
292 if (pkt->data != NULL)
293 OPENSSL_free(pkt->data);
297 typedef struct mempacket_test_ctx_st {
298 STACK_OF(MEMPACKET) *pkts;
300 unsigned int currrec;
301 unsigned int currpkt;
302 unsigned int lastpkt;
303 unsigned int injected;
304 unsigned int noinject;
305 unsigned int dropepoch;
308 } MEMPACKET_TEST_CTX;
310 static int mempacket_test_new(BIO *bi);
311 static int mempacket_test_free(BIO *a);
312 static int mempacket_test_read(BIO *b, char *out, int outl);
313 static int mempacket_test_write(BIO *b, const char *in, int inl);
314 static long mempacket_test_ctrl(BIO *b, int cmd, long num, void *ptr);
315 static int mempacket_test_gets(BIO *bp, char *buf, int size);
316 static int mempacket_test_puts(BIO *bp, const char *str);
318 const BIO_METHOD *bio_s_mempacket_test(void)
320 if (meth_mem == NULL) {
321 if (!TEST_ptr(meth_mem = BIO_meth_new(BIO_TYPE_MEMPACKET_TEST,
323 || !TEST_true(BIO_meth_set_write(meth_mem, mempacket_test_write))
324 || !TEST_true(BIO_meth_set_read(meth_mem, mempacket_test_read))
325 || !TEST_true(BIO_meth_set_puts(meth_mem, mempacket_test_puts))
326 || !TEST_true(BIO_meth_set_gets(meth_mem, mempacket_test_gets))
327 || !TEST_true(BIO_meth_set_ctrl(meth_mem, mempacket_test_ctrl))
328 || !TEST_true(BIO_meth_set_create(meth_mem, mempacket_test_new))
329 || !TEST_true(BIO_meth_set_destroy(meth_mem, mempacket_test_free)))
335 void bio_s_mempacket_test_free(void)
337 BIO_meth_free(meth_mem);
340 static int mempacket_test_new(BIO *bio)
342 MEMPACKET_TEST_CTX *ctx;
344 if (!TEST_ptr(ctx = OPENSSL_zalloc(sizeof(*ctx))))
346 if (!TEST_ptr(ctx->pkts = sk_MEMPACKET_new_null())) {
352 BIO_set_init(bio, 1);
353 BIO_set_data(bio, ctx);
357 static int mempacket_test_free(BIO *bio)
359 MEMPACKET_TEST_CTX *ctx = BIO_get_data(bio);
361 sk_MEMPACKET_pop_free(ctx->pkts, mempacket_free);
363 BIO_set_data(bio, NULL);
364 BIO_set_init(bio, 0);
368 /* Record Header values */
371 #define RECORD_SEQUENCE 10
372 #define RECORD_LEN_HI 11
373 #define RECORD_LEN_LO 12
375 #define STANDARD_PACKET 0
377 static int mempacket_test_read(BIO *bio, char *out, int outl)
379 MEMPACKET_TEST_CTX *ctx = BIO_get_data(bio);
383 unsigned int seq, offset, len, epoch;
385 BIO_clear_retry_flags(bio);
386 thispkt = sk_MEMPACKET_value(ctx->pkts, 0);
387 if (thispkt == NULL || thispkt->num != ctx->currpkt) {
388 /* Probably run out of data */
389 BIO_set_retry_read(bio);
392 (void)sk_MEMPACKET_shift(ctx->pkts);
395 if (outl > thispkt->len)
398 if (thispkt->type != INJECT_PACKET_IGNORE_REC_SEQ
399 && (ctx->injected || ctx->droprec >= 0)) {
401 * Overwrite the record sequence number. We strictly number them in
402 * the order received. Since we are actually a reliable transport
403 * we know that there won't be any re-ordering. We overwrite to deal
404 * with any packets that have been injected
406 for (rem = thispkt->len, rec = thispkt->data; rem > 0; rem -= len) {
407 if (rem < DTLS1_RT_HEADER_LENGTH)
409 epoch = (rec[EPOCH_HI] << 8) | rec[EPOCH_LO];
410 if (epoch != ctx->epoch) {
417 rec[RECORD_SEQUENCE - offset] = seq & 0xFF;
422 len = ((rec[RECORD_LEN_HI] << 8) | rec[RECORD_LEN_LO])
423 + DTLS1_RT_HEADER_LENGTH;
426 if (ctx->droprec == (int)ctx->currrec && ctx->dropepoch == epoch) {
428 memmove(rec, rec + len, rem - len);
432 BIO_set_retry_read(bio);
441 memcpy(out, thispkt->data, outl);
442 mempacket_free(thispkt);
446 int mempacket_test_inject(BIO *bio, const char *in, int inl, int pktnum,
449 MEMPACKET_TEST_CTX *ctx = BIO_get_data(bio);
450 MEMPACKET *thispkt = NULL, *looppkt, *nextpkt, *allpkts[3];
452 const unsigned char *inu = (const unsigned char *)in;
453 size_t len = ((inu[RECORD_LEN_HI] << 8) | inu[RECORD_LEN_LO])
454 + DTLS1_RT_HEADER_LENGTH;
459 if ((size_t)inl < len)
462 if ((size_t)inl == len)
465 duprec = ctx->duprec > 0;
467 /* We don't support arbitrary injection when duplicating records */
468 if (duprec && pktnum != -1)
471 /* We only allow injection before we've started writing any data */
480 for (i = 0; i < (duprec ? 3 : 1); i++) {
481 if (!TEST_ptr(allpkts[i] = OPENSSL_malloc(sizeof(*thispkt))))
483 thispkt = allpkts[i];
485 if (!TEST_ptr(thispkt->data = OPENSSL_malloc(inl)))
488 * If we are duplicating the packet, we duplicate it three times. The
489 * first two times we drop the first record if there are more than one.
490 * In this way we know that libssl will not be able to make progress
491 * until it receives the last packet, and hence will be forced to
492 * buffer these records.
494 if (duprec && i != 2) {
495 memcpy(thispkt->data, in + len, inl - len);
496 thispkt->len = inl - len;
498 memcpy(thispkt->data, in, inl);
501 thispkt->num = (pktnum >= 0) ? (unsigned int)pktnum : ctx->lastpkt + i;
502 thispkt->type = type;
505 for(i = 0; (looppkt = sk_MEMPACKET_value(ctx->pkts, i)) != NULL; i++) {
506 /* Check if we found the right place to insert this packet */
507 if (looppkt->num > thispkt->num) {
508 if (sk_MEMPACKET_insert(ctx->pkts, thispkt, i) == 0)
510 /* If we're doing up front injection then we're done */
514 * We need to do some accounting on lastpkt. We increment it first,
515 * but it might now equal the value of injected packets, so we need
521 nextpkt = sk_MEMPACKET_value(ctx->pkts, i);
522 if (nextpkt != NULL && nextpkt->num == ctx->lastpkt)
527 } else if (looppkt->num == thispkt->num) {
528 if (!ctx->noinject) {
529 /* We injected two packets with the same packet number! */
537 * We didn't find any packets with a packet number equal to or greater than
538 * this one, so we just add it onto the end
540 for (i = 0; i < (duprec ? 3 : 1); i++) {
541 thispkt = allpkts[i];
542 if (!sk_MEMPACKET_push(ctx->pkts, thispkt))
552 for (i = 0; i < (ctx->duprec > 0 ? 3 : 1); i++)
553 mempacket_free(allpkts[i]);
557 static int mempacket_test_write(BIO *bio, const char *in, int inl)
559 return mempacket_test_inject(bio, in, inl, -1, STANDARD_PACKET);
562 static long mempacket_test_ctrl(BIO *bio, int cmd, long num, void *ptr)
565 MEMPACKET_TEST_CTX *ctx = BIO_get_data(bio);
570 ret = (long)(sk_MEMPACKET_num(ctx->pkts) == 0);
572 case BIO_CTRL_GET_CLOSE:
573 ret = BIO_get_shutdown(bio);
575 case BIO_CTRL_SET_CLOSE:
576 BIO_set_shutdown(bio, (int)num);
578 case BIO_CTRL_WPENDING:
581 case BIO_CTRL_PENDING:
582 thispkt = sk_MEMPACKET_value(ctx->pkts, 0);
591 case MEMPACKET_CTRL_SET_DROP_EPOCH:
592 ctx->dropepoch = (unsigned int)num;
594 case MEMPACKET_CTRL_SET_DROP_REC:
595 ctx->droprec = (int)num;
597 case MEMPACKET_CTRL_GET_DROP_REC:
600 case MEMPACKET_CTRL_SET_DUPLICATE_REC:
601 ctx->duprec = (int)num;
614 static int mempacket_test_gets(BIO *bio, char *buf, int size)
616 /* We don't support this - not needed anyway */
620 static int mempacket_test_puts(BIO *bio, const char *str)
622 return mempacket_test_write(bio, str, strlen(str));
625 static int always_retry_new(BIO *bi);
626 static int always_retry_free(BIO *a);
627 static int always_retry_read(BIO *b, char *out, int outl);
628 static int always_retry_write(BIO *b, const char *in, int inl);
629 static long always_retry_ctrl(BIO *b, int cmd, long num, void *ptr);
630 static int always_retry_gets(BIO *bp, char *buf, int size);
631 static int always_retry_puts(BIO *bp, const char *str);
633 const BIO_METHOD *bio_s_always_retry(void)
635 if (meth_always_retry == NULL) {
636 if (!TEST_ptr(meth_always_retry = BIO_meth_new(BIO_TYPE_ALWAYS_RETRY,
638 || !TEST_true(BIO_meth_set_write(meth_always_retry,
640 || !TEST_true(BIO_meth_set_read(meth_always_retry,
642 || !TEST_true(BIO_meth_set_puts(meth_always_retry,
644 || !TEST_true(BIO_meth_set_gets(meth_always_retry,
646 || !TEST_true(BIO_meth_set_ctrl(meth_always_retry,
648 || !TEST_true(BIO_meth_set_create(meth_always_retry,
650 || !TEST_true(BIO_meth_set_destroy(meth_always_retry,
654 return meth_always_retry;
657 void bio_s_always_retry_free(void)
659 BIO_meth_free(meth_always_retry);
662 static int always_retry_new(BIO *bio)
664 BIO_set_init(bio, 1);
668 static int always_retry_free(BIO *bio)
670 BIO_set_data(bio, NULL);
671 BIO_set_init(bio, 0);
675 static int always_retry_read(BIO *bio, char *out, int outl)
677 BIO_set_retry_read(bio);
681 static int always_retry_write(BIO *bio, const char *in, int inl)
683 BIO_set_retry_write(bio);
687 static long always_retry_ctrl(BIO *bio, int cmd, long num, void *ptr)
693 BIO_set_retry_write(bio);
707 static int always_retry_gets(BIO *bio, char *buf, int size)
709 BIO_set_retry_read(bio);
713 static int always_retry_puts(BIO *bio, const char *str)
715 BIO_set_retry_write(bio);
719 int create_ssl_ctx_pair(const SSL_METHOD *sm, const SSL_METHOD *cm,
720 int min_proto_version, int max_proto_version,
721 SSL_CTX **sctx, SSL_CTX **cctx, char *certfile,
724 SSL_CTX *serverctx = NULL;
725 SSL_CTX *clientctx = NULL;
729 else if (!TEST_ptr(serverctx = SSL_CTX_new(sm)))
735 else if (!TEST_ptr(clientctx = SSL_CTX_new(cm)))
739 if ((min_proto_version > 0
740 && !TEST_true(SSL_CTX_set_min_proto_version(serverctx,
742 || (max_proto_version > 0
743 && !TEST_true(SSL_CTX_set_max_proto_version(serverctx,
744 max_proto_version))))
746 if (clientctx != NULL
747 && ((min_proto_version > 0
748 && !TEST_true(SSL_CTX_set_min_proto_version(clientctx,
750 || (max_proto_version > 0
751 && !TEST_true(SSL_CTX_set_max_proto_version(clientctx,
752 max_proto_version)))))
755 if (certfile != NULL && privkeyfile != NULL) {
756 if (!TEST_int_eq(SSL_CTX_use_certificate_file(serverctx, certfile,
757 SSL_FILETYPE_PEM), 1)
758 || !TEST_int_eq(SSL_CTX_use_PrivateKey_file(serverctx,
760 SSL_FILETYPE_PEM), 1)
761 || !TEST_int_eq(SSL_CTX_check_private_key(serverctx), 1))
765 #ifndef OPENSSL_NO_DH
766 SSL_CTX_set_dh_auto(serverctx, 1);
775 SSL_CTX_free(serverctx);
776 SSL_CTX_free(clientctx);
780 #define MAXLOOPS 1000000
782 #if !defined(OPENSSL_NO_KTLS) && !defined(OPENSSL_NO_SOCK)
783 static int set_nb(int fd)
787 flags = fcntl(fd,F_GETFL,0);
790 flags = fcntl(fd, F_SETFL, flags | O_NONBLOCK);
794 int create_test_sockets(int *cfd, int *sfd)
796 struct sockaddr_in sin;
797 const char *host = "127.0.0.1";
798 int cfd_connected = 0, ret = 0;
799 socklen_t slen = sizeof(sin);
805 memset ((char *) &sin, 0, sizeof(sin));
806 sin.sin_family = AF_INET;
807 sin.sin_addr.s_addr = inet_addr(host);
809 afd = socket(AF_INET, SOCK_STREAM, 0);
813 if (bind(afd, (struct sockaddr*)&sin, sizeof(sin)) < 0)
816 if (getsockname(afd, (struct sockaddr*)&sin, &slen) < 0)
819 if (listen(afd, 1) < 0)
822 *cfd = socket(AF_INET, SOCK_STREAM, 0);
826 if (set_nb(afd) == -1)
829 while (*sfd == -1 || !cfd_connected ) {
830 *sfd = accept(afd, NULL, 0);
831 if (*sfd == -1 && errno != EAGAIN)
834 if (!cfd_connected && connect(*cfd, (struct sockaddr*)&sin, sizeof(sin)) < 0)
840 if (set_nb(*cfd) == -1 || set_nb(*sfd) == -1)
856 int create_ssl_objects2(SSL_CTX *serverctx, SSL_CTX *clientctx, SSL **sssl,
857 SSL **cssl, int sfd, int cfd)
859 SSL *serverssl = NULL, *clientssl = NULL;
860 BIO *s_to_c_bio = NULL, *c_to_s_bio = NULL;
864 else if (!TEST_ptr(serverssl = SSL_new(serverctx)))
868 else if (!TEST_ptr(clientssl = SSL_new(clientctx)))
871 if (!TEST_ptr(s_to_c_bio = BIO_new_socket(sfd, BIO_NOCLOSE))
872 || !TEST_ptr(c_to_s_bio = BIO_new_socket(cfd, BIO_NOCLOSE)))
875 SSL_set_bio(clientssl, c_to_s_bio, c_to_s_bio);
876 SSL_set_bio(serverssl, s_to_c_bio, s_to_c_bio);
884 BIO_free(s_to_c_bio);
885 BIO_free(c_to_s_bio);
891 * NOTE: Transfers control of the BIOs - this function will free them on error
893 int create_ssl_objects(SSL_CTX *serverctx, SSL_CTX *clientctx, SSL **sssl,
894 SSL **cssl, BIO *s_to_c_fbio, BIO *c_to_s_fbio)
896 SSL *serverssl = NULL, *clientssl = NULL;
897 BIO *s_to_c_bio = NULL, *c_to_s_bio = NULL;
901 else if (!TEST_ptr(serverssl = SSL_new(serverctx)))
905 else if (!TEST_ptr(clientssl = SSL_new(clientctx)))
908 if (SSL_is_dtls(clientssl)) {
909 if (!TEST_ptr(s_to_c_bio = BIO_new(bio_s_mempacket_test()))
910 || !TEST_ptr(c_to_s_bio = BIO_new(bio_s_mempacket_test())))
913 if (!TEST_ptr(s_to_c_bio = BIO_new(BIO_s_mem()))
914 || !TEST_ptr(c_to_s_bio = BIO_new(BIO_s_mem())))
918 if (s_to_c_fbio != NULL
919 && !TEST_ptr(s_to_c_bio = BIO_push(s_to_c_fbio, s_to_c_bio)))
921 if (c_to_s_fbio != NULL
922 && !TEST_ptr(c_to_s_bio = BIO_push(c_to_s_fbio, c_to_s_bio)))
925 /* Set Non-blocking IO behaviour */
926 BIO_set_mem_eof_return(s_to_c_bio, -1);
927 BIO_set_mem_eof_return(c_to_s_bio, -1);
929 /* Up ref these as we are passing them to two SSL objects */
930 SSL_set_bio(serverssl, c_to_s_bio, s_to_c_bio);
931 BIO_up_ref(s_to_c_bio);
932 BIO_up_ref(c_to_s_bio);
933 SSL_set_bio(clientssl, s_to_c_bio, c_to_s_bio);
941 BIO_free(s_to_c_bio);
942 BIO_free(c_to_s_bio);
943 BIO_free(s_to_c_fbio);
944 BIO_free(c_to_s_fbio);
950 * Create an SSL connection, but does not ready any post-handshake
951 * NewSessionTicket messages.
952 * If |read| is set and we're using DTLS then we will attempt to SSL_read on
953 * the connection once we've completed one half of it, to ensure any retransmits
956 int create_bare_ssl_connection(SSL *serverssl, SSL *clientssl, int want,
959 int retc = -1, rets = -1, err, abortctr = 0;
960 int clienterr = 0, servererr = 0;
961 int isdtls = SSL_is_dtls(serverssl);
964 err = SSL_ERROR_WANT_WRITE;
965 while (!clienterr && retc <= 0 && err == SSL_ERROR_WANT_WRITE) {
966 retc = SSL_connect(clientssl);
968 err = SSL_get_error(clientssl, retc);
971 if (!clienterr && retc <= 0 && err != SSL_ERROR_WANT_READ) {
972 TEST_info("SSL_connect() failed %d, %d", retc, err);
975 if (want != SSL_ERROR_NONE && err == want)
978 err = SSL_ERROR_WANT_WRITE;
979 while (!servererr && rets <= 0 && err == SSL_ERROR_WANT_WRITE) {
980 rets = SSL_accept(serverssl);
982 err = SSL_get_error(serverssl, rets);
985 if (!servererr && rets <= 0
986 && err != SSL_ERROR_WANT_READ
987 && err != SSL_ERROR_WANT_X509_LOOKUP) {
988 TEST_info("SSL_accept() failed %d, %d", rets, err);
991 if (want != SSL_ERROR_NONE && err == want)
993 if (clienterr && servererr)
995 if (isdtls && read) {
996 unsigned char buf[20];
998 /* Trigger any retransmits that may be appropriate */
999 if (rets > 0 && retc <= 0) {
1000 if (SSL_read(serverssl, buf, sizeof(buf)) > 0) {
1001 /* We don't expect this to succeed! */
1002 TEST_info("Unexpected SSL_read() success!");
1006 if (retc > 0 && rets <= 0) {
1007 if (SSL_read(clientssl, buf, sizeof(buf)) > 0) {
1008 /* We don't expect this to succeed! */
1009 TEST_info("Unexpected SSL_read() success!");
1014 if (++abortctr == MAXLOOPS) {
1015 TEST_info("No progress made");
1018 if (isdtls && abortctr <= 50 && (abortctr % 10) == 0) {
1020 * It looks like we're just spinning. Pause for a short period to
1021 * give the DTLS timer a chance to do something. We only do this for
1022 * the first few times to prevent hangs.
1026 } while (retc <=0 || rets <= 0);
1032 * Create an SSL connection including any post handshake NewSessionTicket
1035 int create_ssl_connection(SSL *serverssl, SSL *clientssl, int want)
1041 if (!create_bare_ssl_connection(serverssl, clientssl, want, 1))
1045 * We attempt to read some data on the client side which we expect to fail.
1046 * This will ensure we have received the NewSessionTicket in TLSv1.3 where
1047 * appropriate. We do this twice because there are 2 NewSessionTickets.
1049 for (i = 0; i < 2; i++) {
1050 if (SSL_read_ex(clientssl, &buf, sizeof(buf), &readbytes) > 0) {
1051 if (!TEST_ulong_eq(readbytes, 0))
1053 } else if (!TEST_int_eq(SSL_get_error(clientssl, 0),
1054 SSL_ERROR_WANT_READ)) {
1062 void shutdown_ssl_connection(SSL *serverssl, SSL *clientssl)
1064 SSL_shutdown(clientssl);
1065 SSL_shutdown(serverssl);
1066 SSL_free(serverssl);
1067 SSL_free(clientssl);