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
74 static BIO_METHOD
*method_tls_dump
= NULL
;
75 static BIO_METHOD
*meth_mem
= NULL
;
77 /* Note: Not thread safe! */
78 const BIO_METHOD
*bio_f_tls_dump_filter(void)
80 if (method_tls_dump
== NULL
) {
81 method_tls_dump
= BIO_meth_new(BIO_TYPE_TLS_DUMP_FILTER
,
83 if ( method_tls_dump
== NULL
84 || !BIO_meth_set_write(method_tls_dump
, tls_dump_write
)
85 || !BIO_meth_set_read(method_tls_dump
, tls_dump_read
)
86 || !BIO_meth_set_puts(method_tls_dump
, tls_dump_puts
)
87 || !BIO_meth_set_gets(method_tls_dump
, tls_dump_gets
)
88 || !BIO_meth_set_ctrl(method_tls_dump
, tls_dump_ctrl
)
89 || !BIO_meth_set_create(method_tls_dump
, tls_dump_new
)
90 || !BIO_meth_set_destroy(method_tls_dump
, tls_dump_free
))
93 return method_tls_dump
;
96 void bio_f_tls_dump_filter_free(void)
98 BIO_meth_free(method_tls_dump
);
101 static int tls_dump_new(BIO
*bio
)
103 BIO_set_init(bio
, 1);
107 static int tls_dump_free(BIO
*bio
)
109 BIO_set_init(bio
, 0);
114 static void copy_flags(BIO
*bio
)
117 BIO
*next
= BIO_next(bio
);
119 flags
= BIO_test_flags(next
, BIO_FLAGS_SHOULD_RETRY
| BIO_FLAGS_RWS
);
120 BIO_clear_flags(bio
, BIO_FLAGS_SHOULD_RETRY
| BIO_FLAGS_RWS
);
121 BIO_set_flags(bio
, flags
);
124 #define RECORD_CONTENT_TYPE 0
125 #define RECORD_VERSION_HI 1
126 #define RECORD_VERSION_LO 2
127 #define RECORD_EPOCH_HI 3
128 #define RECORD_EPOCH_LO 4
129 #define RECORD_SEQUENCE_START 5
130 #define RECORD_SEQUENCE_END 10
131 #define RECORD_LEN_HI 11
132 #define RECORD_LEN_LO 12
136 #define MSG_LEN_MID 2
140 #define MSG_FRAG_OFF_HI 6
141 #define MSG_FRAG_OFF_MID 7
142 #define MSG_FRAG_OFF_LO 8
143 #define MSG_FRAG_LEN_HI 9
144 #define MSG_FRAG_LEN_MID 10
145 #define MSG_FRAG_LEN_LO 11
148 static void dump_data(const char *data
, int len
)
150 int rem
, i
, content
, reclen
, msglen
, fragoff
, fraglen
, epoch
;
153 printf("---- START OF PACKET ----\n");
156 rec
= (unsigned char *)data
;
161 printf("*---- START OF RECORD ----\n");
162 if (rem
< DTLS1_RT_HEADER_LENGTH
) {
163 printf("*---- RECORD TRUNCATED ----\n");
166 content
= rec
[RECORD_CONTENT_TYPE
];
167 printf("** Record Content-type: %d\n", content
);
168 printf("** Record Version: %02x%02x\n",
169 rec
[RECORD_VERSION_HI
], rec
[RECORD_VERSION_LO
]);
170 epoch
= (rec
[RECORD_EPOCH_HI
] << 8) | rec
[RECORD_EPOCH_LO
];
171 printf("** Record Epoch: %d\n", epoch
);
172 printf("** Record Sequence: ");
173 for (i
= RECORD_SEQUENCE_START
; i
<= RECORD_SEQUENCE_END
; i
++)
174 printf("%02x", rec
[i
]);
175 reclen
= (rec
[RECORD_LEN_HI
] << 8) | rec
[RECORD_LEN_LO
];
176 printf("\n** Record Length: %d\n", reclen
);
178 /* Now look at message */
179 rec
+= DTLS1_RT_HEADER_LENGTH
;
180 rem
-= DTLS1_RT_HEADER_LENGTH
;
181 if (content
== SSL3_RT_HANDSHAKE
) {
182 printf("**---- START OF HANDSHAKE MESSAGE FRAGMENT ----\n");
184 printf("**---- HANDSHAKE MESSAGE FRAGMENT ENCRYPTED ----\n");
185 } else if (rem
< DTLS1_HM_HEADER_LENGTH
186 || reclen
< DTLS1_HM_HEADER_LENGTH
) {
187 printf("**---- HANDSHAKE MESSAGE FRAGMENT TRUNCATED ----\n");
189 printf("*** Message Type: %d\n", rec
[MSG_TYPE
]);
190 msglen
= (rec
[MSG_LEN_HI
] << 16) | (rec
[MSG_LEN_MID
] << 8)
192 printf("*** Message Length: %d\n", msglen
);
193 printf("*** Message sequence: %d\n",
194 (rec
[MSG_SEQ_HI
] << 8) | rec
[MSG_SEQ_LO
]);
195 fragoff
= (rec
[MSG_FRAG_OFF_HI
] << 16)
196 | (rec
[MSG_FRAG_OFF_MID
] << 8)
197 | rec
[MSG_FRAG_OFF_LO
];
198 printf("*** Message Fragment offset: %d\n", fragoff
);
199 fraglen
= (rec
[MSG_FRAG_LEN_HI
] << 16)
200 | (rec
[MSG_FRAG_LEN_MID
] << 8)
201 | rec
[MSG_FRAG_LEN_LO
];
202 printf("*** Message Fragment len: %d\n", fraglen
);
203 if (fragoff
+ fraglen
> msglen
)
204 printf("***---- HANDSHAKE MESSAGE FRAGMENT INVALID ----\n");
205 else if (reclen
< fraglen
)
206 printf("**---- HANDSHAKE MESSAGE FRAGMENT TRUNCATED ----\n");
208 printf("**---- END OF HANDSHAKE MESSAGE FRAGMENT ----\n");
212 printf("*---- RECORD TRUNCATED ----\n");
217 printf("*---- END OF RECORD ----\n");
220 printf("---- END OF PACKET ----\n\n");
224 static int tls_dump_read(BIO
*bio
, char *out
, int outl
)
227 BIO
*next
= BIO_next(bio
);
229 ret
= BIO_read(next
, out
, outl
);
239 static int tls_dump_write(BIO
*bio
, const char *in
, int inl
)
242 BIO
*next
= BIO_next(bio
);
244 ret
= BIO_write(next
, in
, inl
);
250 static long tls_dump_ctrl(BIO
*bio
, int cmd
, long num
, void *ptr
)
253 BIO
*next
= BIO_next(bio
);
263 ret
= BIO_ctrl(next
, cmd
, num
, ptr
);
269 static int tls_dump_gets(BIO
*bio
, char *buf
, int size
)
271 /* We don't support this - not needed anyway */
275 static int tls_dump_puts(BIO
*bio
, const char *str
)
277 return tls_dump_write(bio
, str
, strlen(str
));
281 struct mempacket_st
{
288 static void mempacket_free(MEMPACKET
*pkt
)
290 if (pkt
->data
!= NULL
)
291 OPENSSL_free(pkt
->data
);
295 typedef struct mempacket_test_ctx_st
{
296 STACK_OF(MEMPACKET
) *pkts
;
298 unsigned int currrec
;
299 unsigned int currpkt
;
300 unsigned int lastpkt
;
301 unsigned int injected
;
302 unsigned int noinject
;
303 unsigned int dropepoch
;
306 } MEMPACKET_TEST_CTX
;
308 static int mempacket_test_new(BIO
*bi
);
309 static int mempacket_test_free(BIO
*a
);
310 static int mempacket_test_read(BIO
*b
, char *out
, int outl
);
311 static int mempacket_test_write(BIO
*b
, const char *in
, int inl
);
312 static long mempacket_test_ctrl(BIO
*b
, int cmd
, long num
, void *ptr
);
313 static int mempacket_test_gets(BIO
*bp
, char *buf
, int size
);
314 static int mempacket_test_puts(BIO
*bp
, const char *str
);
316 const BIO_METHOD
*bio_s_mempacket_test(void)
318 if (meth_mem
== NULL
) {
319 if (!TEST_ptr(meth_mem
= BIO_meth_new(BIO_TYPE_MEMPACKET_TEST
,
321 || !TEST_true(BIO_meth_set_write(meth_mem
, mempacket_test_write
))
322 || !TEST_true(BIO_meth_set_read(meth_mem
, mempacket_test_read
))
323 || !TEST_true(BIO_meth_set_puts(meth_mem
, mempacket_test_puts
))
324 || !TEST_true(BIO_meth_set_gets(meth_mem
, mempacket_test_gets
))
325 || !TEST_true(BIO_meth_set_ctrl(meth_mem
, mempacket_test_ctrl
))
326 || !TEST_true(BIO_meth_set_create(meth_mem
, mempacket_test_new
))
327 || !TEST_true(BIO_meth_set_destroy(meth_mem
, mempacket_test_free
)))
333 void bio_s_mempacket_test_free(void)
335 BIO_meth_free(meth_mem
);
338 static int mempacket_test_new(BIO
*bio
)
340 MEMPACKET_TEST_CTX
*ctx
;
342 if (!TEST_ptr(ctx
= OPENSSL_zalloc(sizeof(*ctx
))))
344 if (!TEST_ptr(ctx
->pkts
= sk_MEMPACKET_new_null())) {
350 BIO_set_init(bio
, 1);
351 BIO_set_data(bio
, ctx
);
355 static int mempacket_test_free(BIO
*bio
)
357 MEMPACKET_TEST_CTX
*ctx
= BIO_get_data(bio
);
359 sk_MEMPACKET_pop_free(ctx
->pkts
, mempacket_free
);
361 BIO_set_data(bio
, NULL
);
362 BIO_set_init(bio
, 0);
366 /* Record Header values */
369 #define RECORD_SEQUENCE 10
370 #define RECORD_LEN_HI 11
371 #define RECORD_LEN_LO 12
373 #define STANDARD_PACKET 0
375 static int mempacket_test_read(BIO
*bio
, char *out
, int outl
)
377 MEMPACKET_TEST_CTX
*ctx
= BIO_get_data(bio
);
381 unsigned int seq
, offset
, len
, epoch
;
383 BIO_clear_retry_flags(bio
);
384 thispkt
= sk_MEMPACKET_value(ctx
->pkts
, 0);
385 if (thispkt
== NULL
|| thispkt
->num
!= ctx
->currpkt
) {
386 /* Probably run out of data */
387 BIO_set_retry_read(bio
);
390 (void)sk_MEMPACKET_shift(ctx
->pkts
);
393 if (outl
> thispkt
->len
)
396 if (thispkt
->type
!= INJECT_PACKET_IGNORE_REC_SEQ
397 && (ctx
->injected
|| ctx
->droprec
>= 0)) {
399 * Overwrite the record sequence number. We strictly number them in
400 * the order received. Since we are actually a reliable transport
401 * we know that there won't be any re-ordering. We overwrite to deal
402 * with any packets that have been injected
404 for (rem
= thispkt
->len
, rec
= thispkt
->data
; rem
> 0; rem
-= len
) {
405 if (rem
< DTLS1_RT_HEADER_LENGTH
)
407 epoch
= (rec
[EPOCH_HI
] << 8) | rec
[EPOCH_LO
];
408 if (epoch
!= ctx
->epoch
) {
415 rec
[RECORD_SEQUENCE
- offset
] = seq
& 0xFF;
420 len
= ((rec
[RECORD_LEN_HI
] << 8) | rec
[RECORD_LEN_LO
])
421 + DTLS1_RT_HEADER_LENGTH
;
424 if (ctx
->droprec
== (int)ctx
->currrec
&& ctx
->dropepoch
== epoch
) {
426 memmove(rec
, rec
+ len
, rem
- len
);
430 BIO_set_retry_read(bio
);
439 memcpy(out
, thispkt
->data
, outl
);
440 mempacket_free(thispkt
);
444 int mempacket_test_inject(BIO
*bio
, const char *in
, int inl
, int pktnum
,
447 MEMPACKET_TEST_CTX
*ctx
= BIO_get_data(bio
);
448 MEMPACKET
*thispkt
= NULL
, *looppkt
, *nextpkt
, *allpkts
[3];
450 const unsigned char *inu
= (const unsigned char *)in
;
451 size_t len
= ((inu
[RECORD_LEN_HI
] << 8) | inu
[RECORD_LEN_LO
])
452 + DTLS1_RT_HEADER_LENGTH
;
457 if ((size_t)inl
< len
)
460 if ((size_t)inl
== len
)
463 duprec
= ctx
->duprec
> 0;
465 /* We don't support arbitrary injection when duplicating records */
466 if (duprec
&& pktnum
!= -1)
469 /* We only allow injection before we've started writing any data */
478 for (i
= 0; i
< (duprec
? 3 : 1); i
++) {
479 if (!TEST_ptr(allpkts
[i
] = OPENSSL_malloc(sizeof(*thispkt
))))
481 thispkt
= allpkts
[i
];
483 if (!TEST_ptr(thispkt
->data
= OPENSSL_malloc(inl
)))
486 * If we are duplicating the packet, we duplicate it three times. The
487 * first two times we drop the first record if there are more than one.
488 * In this way we know that libssl will not be able to make progress
489 * until it receives the last packet, and hence will be forced to
490 * buffer these records.
492 if (duprec
&& i
!= 2) {
493 memcpy(thispkt
->data
, in
+ len
, inl
- len
);
494 thispkt
->len
= inl
- len
;
496 memcpy(thispkt
->data
, in
, inl
);
499 thispkt
->num
= (pktnum
>= 0) ? (unsigned int)pktnum
: ctx
->lastpkt
+ i
;
500 thispkt
->type
= type
;
503 for(i
= 0; (looppkt
= sk_MEMPACKET_value(ctx
->pkts
, i
)) != NULL
; i
++) {
504 /* Check if we found the right place to insert this packet */
505 if (looppkt
->num
> thispkt
->num
) {
506 if (sk_MEMPACKET_insert(ctx
->pkts
, thispkt
, i
) == 0)
508 /* If we're doing up front injection then we're done */
512 * We need to do some accounting on lastpkt. We increment it first,
513 * but it might now equal the value of injected packets, so we need
519 nextpkt
= sk_MEMPACKET_value(ctx
->pkts
, i
);
520 if (nextpkt
!= NULL
&& nextpkt
->num
== ctx
->lastpkt
)
525 } else if (looppkt
->num
== thispkt
->num
) {
526 if (!ctx
->noinject
) {
527 /* We injected two packets with the same packet number! */
535 * We didn't find any packets with a packet number equal to or greater than
536 * this one, so we just add it onto the end
538 for (i
= 0; i
< (duprec
? 3 : 1); i
++) {
539 thispkt
= allpkts
[i
];
540 if (!sk_MEMPACKET_push(ctx
->pkts
, thispkt
))
550 for (i
= 0; i
< (ctx
->duprec
> 0 ? 3 : 1); i
++)
551 mempacket_free(allpkts
[i
]);
555 static int mempacket_test_write(BIO
*bio
, const char *in
, int inl
)
557 return mempacket_test_inject(bio
, in
, inl
, -1, STANDARD_PACKET
);
560 static long mempacket_test_ctrl(BIO
*bio
, int cmd
, long num
, void *ptr
)
563 MEMPACKET_TEST_CTX
*ctx
= BIO_get_data(bio
);
568 ret
= (long)(sk_MEMPACKET_num(ctx
->pkts
) == 0);
570 case BIO_CTRL_GET_CLOSE
:
571 ret
= BIO_get_shutdown(bio
);
573 case BIO_CTRL_SET_CLOSE
:
574 BIO_set_shutdown(bio
, (int)num
);
576 case BIO_CTRL_WPENDING
:
579 case BIO_CTRL_PENDING
:
580 thispkt
= sk_MEMPACKET_value(ctx
->pkts
, 0);
589 case MEMPACKET_CTRL_SET_DROP_EPOCH
:
590 ctx
->dropepoch
= (unsigned int)num
;
592 case MEMPACKET_CTRL_SET_DROP_REC
:
593 ctx
->droprec
= (int)num
;
595 case MEMPACKET_CTRL_GET_DROP_REC
:
598 case MEMPACKET_CTRL_SET_DUPLICATE_REC
:
599 ctx
->duprec
= (int)num
;
612 static int mempacket_test_gets(BIO
*bio
, char *buf
, int size
)
614 /* We don't support this - not needed anyway */
618 static int mempacket_test_puts(BIO
*bio
, const char *str
)
620 return mempacket_test_write(bio
, str
, strlen(str
));
623 int create_ssl_ctx_pair(const SSL_METHOD
*sm
, const SSL_METHOD
*cm
,
624 int min_proto_version
, int max_proto_version
,
625 SSL_CTX
**sctx
, SSL_CTX
**cctx
, char *certfile
,
628 SSL_CTX
*serverctx
= NULL
;
629 SSL_CTX
*clientctx
= NULL
;
631 if (!TEST_ptr(serverctx
= SSL_CTX_new(sm
))
632 || (cctx
!= NULL
&& !TEST_ptr(clientctx
= SSL_CTX_new(cm
))))
635 if ((min_proto_version
> 0
636 && !TEST_true(SSL_CTX_set_min_proto_version(serverctx
,
638 || (max_proto_version
> 0
639 && !TEST_true(SSL_CTX_set_max_proto_version(serverctx
,
640 max_proto_version
))))
642 if (clientctx
!= NULL
643 && ((min_proto_version
> 0
644 && !TEST_true(SSL_CTX_set_min_proto_version(clientctx
,
646 || (max_proto_version
> 0
647 && !TEST_true(SSL_CTX_set_max_proto_version(clientctx
,
648 max_proto_version
)))))
651 if (certfile
!= NULL
&& privkeyfile
!= NULL
) {
652 if (!TEST_int_eq(SSL_CTX_use_certificate_file(serverctx
, certfile
,
653 SSL_FILETYPE_PEM
), 1)
654 || !TEST_int_eq(SSL_CTX_use_PrivateKey_file(serverctx
,
656 SSL_FILETYPE_PEM
), 1)
657 || !TEST_int_eq(SSL_CTX_check_private_key(serverctx
), 1))
661 #ifndef OPENSSL_NO_DH
662 SSL_CTX_set_dh_auto(serverctx
, 1);
671 SSL_CTX_free(serverctx
);
672 SSL_CTX_free(clientctx
);
676 #define MAXLOOPS 1000000
678 #if !defined(OPENSSL_NO_KTLS) && !defined(OPENSSL_NO_SOCK)
679 static int set_nb(int fd
)
683 flags
= fcntl(fd
,F_GETFL
,0);
686 flags
= fcntl(fd
, F_SETFL
, flags
| O_NONBLOCK
);
690 int create_test_sockets(int *cfd
, int *sfd
)
692 struct sockaddr_in sin
;
693 const char *host
= "127.0.0.1";
694 int cfd_connected
= 0, ret
= 0;
695 socklen_t slen
= sizeof(sin
);
701 memset ((char *) &sin
, 0, sizeof(sin
));
702 sin
.sin_family
= AF_INET
;
703 sin
.sin_addr
.s_addr
= inet_addr(host
);
705 afd
= socket(AF_INET
, SOCK_STREAM
, 0);
709 if (bind(afd
, (struct sockaddr
*)&sin
, sizeof(sin
)) < 0)
712 if (getsockname(afd
, (struct sockaddr
*)&sin
, &slen
) < 0)
715 if (listen(afd
, 1) < 0)
718 *cfd
= socket(AF_INET
, SOCK_STREAM
, 0);
722 if (set_nb(afd
) == -1)
725 while (*sfd
== -1 || !cfd_connected
) {
726 *sfd
= accept(afd
, NULL
, 0);
727 if (*sfd
== -1 && errno
!= EAGAIN
)
730 if (!cfd_connected
&& connect(*cfd
, (struct sockaddr
*)&sin
, sizeof(sin
)) < 0)
736 if (set_nb(*cfd
) == -1 || set_nb(*sfd
) == -1)
752 int create_ssl_objects2(SSL_CTX
*serverctx
, SSL_CTX
*clientctx
, SSL
**sssl
,
753 SSL
**cssl
, int sfd
, int cfd
)
755 SSL
*serverssl
= NULL
, *clientssl
= NULL
;
756 BIO
*s_to_c_bio
= NULL
, *c_to_s_bio
= NULL
;
760 else if (!TEST_ptr(serverssl
= SSL_new(serverctx
)))
764 else if (!TEST_ptr(clientssl
= SSL_new(clientctx
)))
767 if (!TEST_ptr(s_to_c_bio
= BIO_new_socket(sfd
, BIO_NOCLOSE
))
768 || !TEST_ptr(c_to_s_bio
= BIO_new_socket(cfd
, BIO_NOCLOSE
)))
771 SSL_set_bio(clientssl
, c_to_s_bio
, c_to_s_bio
);
772 SSL_set_bio(serverssl
, s_to_c_bio
, s_to_c_bio
);
780 BIO_free(s_to_c_bio
);
781 BIO_free(c_to_s_bio
);
787 * NOTE: Transfers control of the BIOs - this function will free them on error
789 int create_ssl_objects(SSL_CTX
*serverctx
, SSL_CTX
*clientctx
, SSL
**sssl
,
790 SSL
**cssl
, BIO
*s_to_c_fbio
, BIO
*c_to_s_fbio
)
792 SSL
*serverssl
= NULL
, *clientssl
= NULL
;
793 BIO
*s_to_c_bio
= NULL
, *c_to_s_bio
= NULL
;
797 else if (!TEST_ptr(serverssl
= SSL_new(serverctx
)))
801 else if (!TEST_ptr(clientssl
= SSL_new(clientctx
)))
804 if (SSL_is_dtls(clientssl
)) {
805 if (!TEST_ptr(s_to_c_bio
= BIO_new(bio_s_mempacket_test()))
806 || !TEST_ptr(c_to_s_bio
= BIO_new(bio_s_mempacket_test())))
809 if (!TEST_ptr(s_to_c_bio
= BIO_new(BIO_s_mem()))
810 || !TEST_ptr(c_to_s_bio
= BIO_new(BIO_s_mem())))
814 if (s_to_c_fbio
!= NULL
815 && !TEST_ptr(s_to_c_bio
= BIO_push(s_to_c_fbio
, s_to_c_bio
)))
817 if (c_to_s_fbio
!= NULL
818 && !TEST_ptr(c_to_s_bio
= BIO_push(c_to_s_fbio
, c_to_s_bio
)))
821 /* Set Non-blocking IO behaviour */
822 BIO_set_mem_eof_return(s_to_c_bio
, -1);
823 BIO_set_mem_eof_return(c_to_s_bio
, -1);
825 /* Up ref these as we are passing them to two SSL objects */
826 SSL_set_bio(serverssl
, c_to_s_bio
, s_to_c_bio
);
827 BIO_up_ref(s_to_c_bio
);
828 BIO_up_ref(c_to_s_bio
);
829 SSL_set_bio(clientssl
, s_to_c_bio
, c_to_s_bio
);
837 BIO_free(s_to_c_bio
);
838 BIO_free(c_to_s_bio
);
839 BIO_free(s_to_c_fbio
);
840 BIO_free(c_to_s_fbio
);
846 * Create an SSL connection, but does not ready any post-handshake
847 * NewSessionTicket messages.
848 * If |read| is set and we're using DTLS then we will attempt to SSL_read on
849 * the connection once we've completed one half of it, to ensure any retransmits
852 int create_bare_ssl_connection(SSL
*serverssl
, SSL
*clientssl
, int want
,
855 int retc
= -1, rets
= -1, err
, abortctr
= 0;
856 int clienterr
= 0, servererr
= 0;
857 int isdtls
= SSL_is_dtls(serverssl
);
860 err
= SSL_ERROR_WANT_WRITE
;
861 while (!clienterr
&& retc
<= 0 && err
== SSL_ERROR_WANT_WRITE
) {
862 retc
= SSL_connect(clientssl
);
864 err
= SSL_get_error(clientssl
, retc
);
867 if (!clienterr
&& retc
<= 0 && err
!= SSL_ERROR_WANT_READ
) {
868 TEST_info("SSL_connect() failed %d, %d", retc
, err
);
871 if (want
!= SSL_ERROR_NONE
&& err
== want
)
874 err
= SSL_ERROR_WANT_WRITE
;
875 while (!servererr
&& rets
<= 0 && err
== SSL_ERROR_WANT_WRITE
) {
876 rets
= SSL_accept(serverssl
);
878 err
= SSL_get_error(serverssl
, rets
);
881 if (!servererr
&& rets
<= 0
882 && err
!= SSL_ERROR_WANT_READ
883 && err
!= SSL_ERROR_WANT_X509_LOOKUP
) {
884 TEST_info("SSL_accept() failed %d, %d", rets
, err
);
887 if (want
!= SSL_ERROR_NONE
&& err
== want
)
889 if (clienterr
&& servererr
)
891 if (isdtls
&& read
) {
892 unsigned char buf
[20];
894 /* Trigger any retransmits that may be appropriate */
895 if (rets
> 0 && retc
<= 0) {
896 if (SSL_read(serverssl
, buf
, sizeof(buf
)) > 0) {
897 /* We don't expect this to succeed! */
898 TEST_info("Unexpected SSL_read() success!");
902 if (retc
> 0 && rets
<= 0) {
903 if (SSL_read(clientssl
, buf
, sizeof(buf
)) > 0) {
904 /* We don't expect this to succeed! */
905 TEST_info("Unexpected SSL_read() success!");
910 if (++abortctr
== MAXLOOPS
) {
911 TEST_info("No progress made");
914 if (isdtls
&& abortctr
<= 50 && (abortctr
% 10) == 0) {
916 * It looks like we're just spinning. Pause for a short period to
917 * give the DTLS timer a chance to do something. We only do this for
918 * the first few times to prevent hangs.
922 } while (retc
<=0 || rets
<= 0);
928 * Create an SSL connection including any post handshake NewSessionTicket
931 int create_ssl_connection(SSL
*serverssl
, SSL
*clientssl
, int want
)
937 if (!create_bare_ssl_connection(serverssl
, clientssl
, want
, 1))
941 * We attempt to read some data on the client side which we expect to fail.
942 * This will ensure we have received the NewSessionTicket in TLSv1.3 where
943 * appropriate. We do this twice because there are 2 NewSesionTickets.
945 for (i
= 0; i
< 2; i
++) {
946 if (SSL_read_ex(clientssl
, &buf
, sizeof(buf
), &readbytes
) > 0) {
947 if (!TEST_ulong_eq(readbytes
, 0))
949 } else if (!TEST_int_eq(SSL_get_error(clientssl
, 0),
950 SSL_ERROR_WANT_READ
)) {
958 void shutdown_ssl_connection(SSL
*serverssl
, SSL
*clientssl
)
960 SSL_shutdown(clientssl
);
961 SSL_shutdown(serverssl
);