2 * Copyright 2016-2019 The OpenSSL Project Authors. All Rights Reserved.
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
12 #include "internal/nelem.h"
13 #include "ssltestlib.h"
17 #ifdef OPENSSL_SYS_UNIX
20 static ossl_inline
void ossl_sleep(unsigned int millis
)
22 # ifdef OPENSSL_SYS_VXWORKS
24 ts
.tv_sec
= (long int) (millis
/ 1000);
25 ts
.tv_nsec
= (long int) (millis
% 1000) * 1000000ul;
28 usleep(millis
* 1000);
34 static ossl_inline
void ossl_sleep(unsigned int millis
)
39 /* Fallback to a busy wait */
40 static ossl_inline
void ossl_sleep(unsigned int millis
)
42 struct timeval start
, now
;
43 unsigned int elapsedms
;
45 gettimeofday(&start
, NULL
);
47 gettimeofday(&now
, NULL
);
48 elapsedms
= (((now
.tv_sec
- start
.tv_sec
) * 1000000)
49 + now
.tv_usec
- start
.tv_usec
) / 1000;
50 } while (elapsedms
< millis
);
54 static int tls_dump_new(BIO
*bi
);
55 static int tls_dump_free(BIO
*a
);
56 static int tls_dump_read(BIO
*b
, char *out
, int outl
);
57 static int tls_dump_write(BIO
*b
, const char *in
, int inl
);
58 static long tls_dump_ctrl(BIO
*b
, int cmd
, long num
, void *ptr
);
59 static int tls_dump_gets(BIO
*bp
, char *buf
, int size
);
60 static int tls_dump_puts(BIO
*bp
, const char *str
);
62 /* Choose a sufficiently large type likely to be unused for this custom BIO */
63 #define BIO_TYPE_TLS_DUMP_FILTER (0x80 | BIO_TYPE_FILTER)
64 #define BIO_TYPE_MEMPACKET_TEST 0x81
65 #define BIO_TYPE_ALWAYS_RETRY 0x82
67 static BIO_METHOD
*method_tls_dump
= NULL
;
68 static BIO_METHOD
*meth_mem
= NULL
;
69 static BIO_METHOD
*meth_always_retry
= NULL
;
71 /* Note: Not thread safe! */
72 const BIO_METHOD
*bio_f_tls_dump_filter(void)
74 if (method_tls_dump
== NULL
) {
75 method_tls_dump
= BIO_meth_new(BIO_TYPE_TLS_DUMP_FILTER
,
77 if ( method_tls_dump
== NULL
78 || !BIO_meth_set_write(method_tls_dump
, tls_dump_write
)
79 || !BIO_meth_set_read(method_tls_dump
, tls_dump_read
)
80 || !BIO_meth_set_puts(method_tls_dump
, tls_dump_puts
)
81 || !BIO_meth_set_gets(method_tls_dump
, tls_dump_gets
)
82 || !BIO_meth_set_ctrl(method_tls_dump
, tls_dump_ctrl
)
83 || !BIO_meth_set_create(method_tls_dump
, tls_dump_new
)
84 || !BIO_meth_set_destroy(method_tls_dump
, tls_dump_free
))
87 return method_tls_dump
;
90 void bio_f_tls_dump_filter_free(void)
92 BIO_meth_free(method_tls_dump
);
95 static int tls_dump_new(BIO
*bio
)
101 static int tls_dump_free(BIO
*bio
)
103 BIO_set_init(bio
, 0);
108 static void copy_flags(BIO
*bio
)
111 BIO
*next
= BIO_next(bio
);
113 flags
= BIO_test_flags(next
, BIO_FLAGS_SHOULD_RETRY
| BIO_FLAGS_RWS
);
114 BIO_clear_flags(bio
, BIO_FLAGS_SHOULD_RETRY
| BIO_FLAGS_RWS
);
115 BIO_set_flags(bio
, flags
);
118 #define RECORD_CONTENT_TYPE 0
119 #define RECORD_VERSION_HI 1
120 #define RECORD_VERSION_LO 2
121 #define RECORD_EPOCH_HI 3
122 #define RECORD_EPOCH_LO 4
123 #define RECORD_SEQUENCE_START 5
124 #define RECORD_SEQUENCE_END 10
125 #define RECORD_LEN_HI 11
126 #define RECORD_LEN_LO 12
130 #define MSG_LEN_MID 2
134 #define MSG_FRAG_OFF_HI 6
135 #define MSG_FRAG_OFF_MID 7
136 #define MSG_FRAG_OFF_LO 8
137 #define MSG_FRAG_LEN_HI 9
138 #define MSG_FRAG_LEN_MID 10
139 #define MSG_FRAG_LEN_LO 11
142 static void dump_data(const char *data
, int len
)
144 int rem
, i
, content
, reclen
, msglen
, fragoff
, fraglen
, epoch
;
147 printf("---- START OF PACKET ----\n");
150 rec
= (unsigned char *)data
;
155 printf("*---- START OF RECORD ----\n");
156 if (rem
< DTLS1_RT_HEADER_LENGTH
) {
157 printf("*---- RECORD TRUNCATED ----\n");
160 content
= rec
[RECORD_CONTENT_TYPE
];
161 printf("** Record Content-type: %d\n", content
);
162 printf("** Record Version: %02x%02x\n",
163 rec
[RECORD_VERSION_HI
], rec
[RECORD_VERSION_LO
]);
164 epoch
= (rec
[RECORD_EPOCH_HI
] << 8) | rec
[RECORD_EPOCH_LO
];
165 printf("** Record Epoch: %d\n", epoch
);
166 printf("** Record Sequence: ");
167 for (i
= RECORD_SEQUENCE_START
; i
<= RECORD_SEQUENCE_END
; i
++)
168 printf("%02x", rec
[i
]);
169 reclen
= (rec
[RECORD_LEN_HI
] << 8) | rec
[RECORD_LEN_LO
];
170 printf("\n** Record Length: %d\n", reclen
);
172 /* Now look at message */
173 rec
+= DTLS1_RT_HEADER_LENGTH
;
174 rem
-= DTLS1_RT_HEADER_LENGTH
;
175 if (content
== SSL3_RT_HANDSHAKE
) {
176 printf("**---- START OF HANDSHAKE MESSAGE FRAGMENT ----\n");
178 printf("**---- HANDSHAKE MESSAGE FRAGMENT ENCRYPTED ----\n");
179 } else if (rem
< DTLS1_HM_HEADER_LENGTH
180 || reclen
< DTLS1_HM_HEADER_LENGTH
) {
181 printf("**---- HANDSHAKE MESSAGE FRAGMENT TRUNCATED ----\n");
183 printf("*** Message Type: %d\n", rec
[MSG_TYPE
]);
184 msglen
= (rec
[MSG_LEN_HI
] << 16) | (rec
[MSG_LEN_MID
] << 8)
186 printf("*** Message Length: %d\n", msglen
);
187 printf("*** Message sequence: %d\n",
188 (rec
[MSG_SEQ_HI
] << 8) | rec
[MSG_SEQ_LO
]);
189 fragoff
= (rec
[MSG_FRAG_OFF_HI
] << 16)
190 | (rec
[MSG_FRAG_OFF_MID
] << 8)
191 | rec
[MSG_FRAG_OFF_LO
];
192 printf("*** Message Fragment offset: %d\n", fragoff
);
193 fraglen
= (rec
[MSG_FRAG_LEN_HI
] << 16)
194 | (rec
[MSG_FRAG_LEN_MID
] << 8)
195 | rec
[MSG_FRAG_LEN_LO
];
196 printf("*** Message Fragment len: %d\n", fraglen
);
197 if (fragoff
+ fraglen
> msglen
)
198 printf("***---- HANDSHAKE MESSAGE FRAGMENT INVALID ----\n");
199 else if (reclen
< fraglen
)
200 printf("**---- HANDSHAKE MESSAGE FRAGMENT TRUNCATED ----\n");
202 printf("**---- END OF HANDSHAKE MESSAGE FRAGMENT ----\n");
206 printf("*---- RECORD TRUNCATED ----\n");
211 printf("*---- END OF RECORD ----\n");
214 printf("---- END OF PACKET ----\n\n");
218 static int tls_dump_read(BIO
*bio
, char *out
, int outl
)
221 BIO
*next
= BIO_next(bio
);
223 ret
= BIO_read(next
, out
, outl
);
233 static int tls_dump_write(BIO
*bio
, const char *in
, int inl
)
236 BIO
*next
= BIO_next(bio
);
238 ret
= BIO_write(next
, in
, inl
);
244 static long tls_dump_ctrl(BIO
*bio
, int cmd
, long num
, void *ptr
)
247 BIO
*next
= BIO_next(bio
);
257 ret
= BIO_ctrl(next
, cmd
, num
, ptr
);
263 static int tls_dump_gets(BIO
*bio
, char *buf
, int size
)
265 /* We don't support this - not needed anyway */
269 static int tls_dump_puts(BIO
*bio
, const char *str
)
271 return tls_dump_write(bio
, str
, strlen(str
));
275 struct mempacket_st
{
282 static void mempacket_free(MEMPACKET
*pkt
)
284 if (pkt
->data
!= NULL
)
285 OPENSSL_free(pkt
->data
);
289 typedef struct mempacket_test_ctx_st
{
290 STACK_OF(MEMPACKET
) *pkts
;
292 unsigned int currrec
;
293 unsigned int currpkt
;
294 unsigned int lastpkt
;
295 unsigned int injected
;
296 unsigned int noinject
;
297 unsigned int dropepoch
;
300 } MEMPACKET_TEST_CTX
;
302 static int mempacket_test_new(BIO
*bi
);
303 static int mempacket_test_free(BIO
*a
);
304 static int mempacket_test_read(BIO
*b
, char *out
, int outl
);
305 static int mempacket_test_write(BIO
*b
, const char *in
, int inl
);
306 static long mempacket_test_ctrl(BIO
*b
, int cmd
, long num
, void *ptr
);
307 static int mempacket_test_gets(BIO
*bp
, char *buf
, int size
);
308 static int mempacket_test_puts(BIO
*bp
, const char *str
);
310 const BIO_METHOD
*bio_s_mempacket_test(void)
312 if (meth_mem
== NULL
) {
313 if (!TEST_ptr(meth_mem
= BIO_meth_new(BIO_TYPE_MEMPACKET_TEST
,
315 || !TEST_true(BIO_meth_set_write(meth_mem
, mempacket_test_write
))
316 || !TEST_true(BIO_meth_set_read(meth_mem
, mempacket_test_read
))
317 || !TEST_true(BIO_meth_set_puts(meth_mem
, mempacket_test_puts
))
318 || !TEST_true(BIO_meth_set_gets(meth_mem
, mempacket_test_gets
))
319 || !TEST_true(BIO_meth_set_ctrl(meth_mem
, mempacket_test_ctrl
))
320 || !TEST_true(BIO_meth_set_create(meth_mem
, mempacket_test_new
))
321 || !TEST_true(BIO_meth_set_destroy(meth_mem
, mempacket_test_free
)))
327 void bio_s_mempacket_test_free(void)
329 BIO_meth_free(meth_mem
);
332 static int mempacket_test_new(BIO
*bio
)
334 MEMPACKET_TEST_CTX
*ctx
;
336 if (!TEST_ptr(ctx
= OPENSSL_zalloc(sizeof(*ctx
))))
338 if (!TEST_ptr(ctx
->pkts
= sk_MEMPACKET_new_null())) {
344 BIO_set_init(bio
, 1);
345 BIO_set_data(bio
, ctx
);
349 static int mempacket_test_free(BIO
*bio
)
351 MEMPACKET_TEST_CTX
*ctx
= BIO_get_data(bio
);
353 sk_MEMPACKET_pop_free(ctx
->pkts
, mempacket_free
);
355 BIO_set_data(bio
, NULL
);
356 BIO_set_init(bio
, 0);
360 /* Record Header values */
363 #define RECORD_SEQUENCE 10
364 #define RECORD_LEN_HI 11
365 #define RECORD_LEN_LO 12
367 #define STANDARD_PACKET 0
369 static int mempacket_test_read(BIO
*bio
, char *out
, int outl
)
371 MEMPACKET_TEST_CTX
*ctx
= BIO_get_data(bio
);
375 unsigned int seq
, offset
, len
, epoch
;
377 BIO_clear_retry_flags(bio
);
378 thispkt
= sk_MEMPACKET_value(ctx
->pkts
, 0);
379 if (thispkt
== NULL
|| thispkt
->num
!= ctx
->currpkt
) {
380 /* Probably run out of data */
381 BIO_set_retry_read(bio
);
384 (void)sk_MEMPACKET_shift(ctx
->pkts
);
387 if (outl
> thispkt
->len
)
390 if (thispkt
->type
!= INJECT_PACKET_IGNORE_REC_SEQ
391 && (ctx
->injected
|| ctx
->droprec
>= 0)) {
393 * Overwrite the record sequence number. We strictly number them in
394 * the order received. Since we are actually a reliable transport
395 * we know that there won't be any re-ordering. We overwrite to deal
396 * with any packets that have been injected
398 for (rem
= thispkt
->len
, rec
= thispkt
->data
; rem
> 0; rem
-= len
) {
399 if (rem
< DTLS1_RT_HEADER_LENGTH
)
401 epoch
= (rec
[EPOCH_HI
] << 8) | rec
[EPOCH_LO
];
402 if (epoch
!= ctx
->epoch
) {
409 rec
[RECORD_SEQUENCE
- offset
] = seq
& 0xFF;
414 len
= ((rec
[RECORD_LEN_HI
] << 8) | rec
[RECORD_LEN_LO
])
415 + DTLS1_RT_HEADER_LENGTH
;
418 if (ctx
->droprec
== (int)ctx
->currrec
&& ctx
->dropepoch
== epoch
) {
420 memmove(rec
, rec
+ len
, rem
- len
);
424 BIO_set_retry_read(bio
);
433 memcpy(out
, thispkt
->data
, outl
);
434 mempacket_free(thispkt
);
438 int mempacket_test_inject(BIO
*bio
, const char *in
, int inl
, int pktnum
,
441 MEMPACKET_TEST_CTX
*ctx
= BIO_get_data(bio
);
442 MEMPACKET
*thispkt
= NULL
, *looppkt
, *nextpkt
, *allpkts
[3];
444 const unsigned char *inu
= (const unsigned char *)in
;
445 size_t len
= ((inu
[RECORD_LEN_HI
] << 8) | inu
[RECORD_LEN_LO
])
446 + DTLS1_RT_HEADER_LENGTH
;
451 if ((size_t)inl
< len
)
454 if ((size_t)inl
== len
)
457 duprec
= ctx
->duprec
> 0;
459 /* We don't support arbitrary injection when duplicating records */
460 if (duprec
&& pktnum
!= -1)
463 /* We only allow injection before we've started writing any data */
472 for (i
= 0; i
< (duprec
? 3 : 1); i
++) {
473 if (!TEST_ptr(allpkts
[i
] = OPENSSL_malloc(sizeof(*thispkt
))))
475 thispkt
= allpkts
[i
];
477 if (!TEST_ptr(thispkt
->data
= OPENSSL_malloc(inl
)))
480 * If we are duplicating the packet, we duplicate it three times. The
481 * first two times we drop the first record if there are more than one.
482 * In this way we know that libssl will not be able to make progress
483 * until it receives the last packet, and hence will be forced to
484 * buffer these records.
486 if (duprec
&& i
!= 2) {
487 memcpy(thispkt
->data
, in
+ len
, inl
- len
);
488 thispkt
->len
= inl
- len
;
490 memcpy(thispkt
->data
, in
, inl
);
493 thispkt
->num
= (pktnum
>= 0) ? (unsigned int)pktnum
: ctx
->lastpkt
+ i
;
494 thispkt
->type
= type
;
497 for(i
= 0; (looppkt
= sk_MEMPACKET_value(ctx
->pkts
, i
)) != NULL
; i
++) {
498 /* Check if we found the right place to insert this packet */
499 if (looppkt
->num
> thispkt
->num
) {
500 if (sk_MEMPACKET_insert(ctx
->pkts
, thispkt
, i
) == 0)
502 /* If we're doing up front injection then we're done */
506 * We need to do some accounting on lastpkt. We increment it first,
507 * but it might now equal the value of injected packets, so we need
513 nextpkt
= sk_MEMPACKET_value(ctx
->pkts
, i
);
514 if (nextpkt
!= NULL
&& nextpkt
->num
== ctx
->lastpkt
)
519 } else if (looppkt
->num
== thispkt
->num
) {
520 if (!ctx
->noinject
) {
521 /* We injected two packets with the same packet number! */
529 * We didn't find any packets with a packet number equal to or greater than
530 * this one, so we just add it onto the end
532 for (i
= 0; i
< (duprec
? 3 : 1); i
++) {
533 thispkt
= allpkts
[i
];
534 if (!sk_MEMPACKET_push(ctx
->pkts
, thispkt
))
544 for (i
= 0; i
< (ctx
->duprec
> 0 ? 3 : 1); i
++)
545 mempacket_free(allpkts
[i
]);
549 static int mempacket_test_write(BIO
*bio
, const char *in
, int inl
)
551 return mempacket_test_inject(bio
, in
, inl
, -1, STANDARD_PACKET
);
554 static long mempacket_test_ctrl(BIO
*bio
, int cmd
, long num
, void *ptr
)
557 MEMPACKET_TEST_CTX
*ctx
= BIO_get_data(bio
);
562 ret
= (long)(sk_MEMPACKET_num(ctx
->pkts
) == 0);
564 case BIO_CTRL_GET_CLOSE
:
565 ret
= BIO_get_shutdown(bio
);
567 case BIO_CTRL_SET_CLOSE
:
568 BIO_set_shutdown(bio
, (int)num
);
570 case BIO_CTRL_WPENDING
:
573 case BIO_CTRL_PENDING
:
574 thispkt
= sk_MEMPACKET_value(ctx
->pkts
, 0);
583 case MEMPACKET_CTRL_SET_DROP_EPOCH
:
584 ctx
->dropepoch
= (unsigned int)num
;
586 case MEMPACKET_CTRL_SET_DROP_REC
:
587 ctx
->droprec
= (int)num
;
589 case MEMPACKET_CTRL_GET_DROP_REC
:
592 case MEMPACKET_CTRL_SET_DUPLICATE_REC
:
593 ctx
->duprec
= (int)num
;
606 static int mempacket_test_gets(BIO
*bio
, char *buf
, int size
)
608 /* We don't support this - not needed anyway */
612 static int mempacket_test_puts(BIO
*bio
, const char *str
)
614 return mempacket_test_write(bio
, str
, strlen(str
));
617 static int always_retry_new(BIO
*bi
);
618 static int always_retry_free(BIO
*a
);
619 static int always_retry_read(BIO
*b
, char *out
, int outl
);
620 static int always_retry_write(BIO
*b
, const char *in
, int inl
);
621 static long always_retry_ctrl(BIO
*b
, int cmd
, long num
, void *ptr
);
622 static int always_retry_gets(BIO
*bp
, char *buf
, int size
);
623 static int always_retry_puts(BIO
*bp
, const char *str
);
625 const BIO_METHOD
*bio_s_always_retry(void)
627 if (meth_always_retry
== NULL
) {
628 if (!TEST_ptr(meth_always_retry
= BIO_meth_new(BIO_TYPE_ALWAYS_RETRY
,
630 || !TEST_true(BIO_meth_set_write(meth_always_retry
,
632 || !TEST_true(BIO_meth_set_read(meth_always_retry
,
634 || !TEST_true(BIO_meth_set_puts(meth_always_retry
,
636 || !TEST_true(BIO_meth_set_gets(meth_always_retry
,
638 || !TEST_true(BIO_meth_set_ctrl(meth_always_retry
,
640 || !TEST_true(BIO_meth_set_create(meth_always_retry
,
642 || !TEST_true(BIO_meth_set_destroy(meth_always_retry
,
646 return meth_always_retry
;
649 void bio_s_always_retry_free(void)
651 BIO_meth_free(meth_always_retry
);
654 static int always_retry_new(BIO
*bio
)
656 BIO_set_init(bio
, 1);
660 static int always_retry_free(BIO
*bio
)
662 BIO_set_data(bio
, NULL
);
663 BIO_set_init(bio
, 0);
667 static int always_retry_read(BIO
*bio
, char *out
, int outl
)
669 BIO_set_retry_read(bio
);
673 static int always_retry_write(BIO
*bio
, const char *in
, int inl
)
675 BIO_set_retry_write(bio
);
679 static long always_retry_ctrl(BIO
*bio
, int cmd
, long num
, void *ptr
)
685 BIO_set_retry_write(bio
);
699 static int always_retry_gets(BIO
*bio
, char *buf
, int size
)
701 BIO_set_retry_read(bio
);
705 static int always_retry_puts(BIO
*bio
, const char *str
)
707 BIO_set_retry_write(bio
);
711 int create_ssl_ctx_pair(const SSL_METHOD
*sm
, const SSL_METHOD
*cm
,
712 int min_proto_version
, int max_proto_version
,
713 SSL_CTX
**sctx
, SSL_CTX
**cctx
, char *certfile
,
716 SSL_CTX
*serverctx
= NULL
;
717 SSL_CTX
*clientctx
= NULL
;
719 if (!TEST_ptr(serverctx
= SSL_CTX_new(sm
))
720 || (cctx
!= NULL
&& !TEST_ptr(clientctx
= SSL_CTX_new(cm
))))
723 if ((min_proto_version
> 0
724 && !TEST_true(SSL_CTX_set_min_proto_version(serverctx
,
726 || (max_proto_version
> 0
727 && !TEST_true(SSL_CTX_set_max_proto_version(serverctx
,
728 max_proto_version
))))
730 if (clientctx
!= NULL
731 && ((min_proto_version
> 0
732 && !TEST_true(SSL_CTX_set_min_proto_version(clientctx
,
734 || (max_proto_version
> 0
735 && !TEST_true(SSL_CTX_set_max_proto_version(clientctx
,
736 max_proto_version
)))))
739 if (certfile
!= NULL
&& privkeyfile
!= NULL
) {
740 if (!TEST_int_eq(SSL_CTX_use_certificate_file(serverctx
, certfile
,
741 SSL_FILETYPE_PEM
), 1)
742 || !TEST_int_eq(SSL_CTX_use_PrivateKey_file(serverctx
,
744 SSL_FILETYPE_PEM
), 1)
745 || !TEST_int_eq(SSL_CTX_check_private_key(serverctx
), 1))
749 #ifndef OPENSSL_NO_DH
750 SSL_CTX_set_dh_auto(serverctx
, 1);
759 SSL_CTX_free(serverctx
);
760 SSL_CTX_free(clientctx
);
764 #define MAXLOOPS 1000000
767 * NOTE: Transfers control of the BIOs - this function will free them on error
769 int create_ssl_objects(SSL_CTX
*serverctx
, SSL_CTX
*clientctx
, SSL
**sssl
,
770 SSL
**cssl
, BIO
*s_to_c_fbio
, BIO
*c_to_s_fbio
)
772 SSL
*serverssl
= NULL
, *clientssl
= NULL
;
773 BIO
*s_to_c_bio
= NULL
, *c_to_s_bio
= NULL
;
777 else if (!TEST_ptr(serverssl
= SSL_new(serverctx
)))
781 else if (!TEST_ptr(clientssl
= SSL_new(clientctx
)))
784 if (SSL_is_dtls(clientssl
)) {
785 if (!TEST_ptr(s_to_c_bio
= BIO_new(bio_s_mempacket_test()))
786 || !TEST_ptr(c_to_s_bio
= BIO_new(bio_s_mempacket_test())))
789 if (!TEST_ptr(s_to_c_bio
= BIO_new(BIO_s_mem()))
790 || !TEST_ptr(c_to_s_bio
= BIO_new(BIO_s_mem())))
794 if (s_to_c_fbio
!= NULL
795 && !TEST_ptr(s_to_c_bio
= BIO_push(s_to_c_fbio
, s_to_c_bio
)))
797 if (c_to_s_fbio
!= NULL
798 && !TEST_ptr(c_to_s_bio
= BIO_push(c_to_s_fbio
, c_to_s_bio
)))
801 /* Set Non-blocking IO behaviour */
802 BIO_set_mem_eof_return(s_to_c_bio
, -1);
803 BIO_set_mem_eof_return(c_to_s_bio
, -1);
805 /* Up ref these as we are passing them to two SSL objects */
806 SSL_set_bio(serverssl
, c_to_s_bio
, s_to_c_bio
);
807 BIO_up_ref(s_to_c_bio
);
808 BIO_up_ref(c_to_s_bio
);
809 SSL_set_bio(clientssl
, s_to_c_bio
, c_to_s_bio
);
817 BIO_free(s_to_c_bio
);
818 BIO_free(c_to_s_bio
);
819 BIO_free(s_to_c_fbio
);
820 BIO_free(c_to_s_fbio
);
826 * Create an SSL connection, but does not ready any post-handshake
827 * NewSessionTicket messages.
828 * If |read| is set and we're using DTLS then we will attempt to SSL_read on
829 * the connection once we've completed one half of it, to ensure any retransmits
832 int create_bare_ssl_connection(SSL
*serverssl
, SSL
*clientssl
, int want
,
835 int retc
= -1, rets
= -1, err
, abortctr
= 0;
836 int clienterr
= 0, servererr
= 0;
837 int isdtls
= SSL_is_dtls(serverssl
);
840 err
= SSL_ERROR_WANT_WRITE
;
841 while (!clienterr
&& retc
<= 0 && err
== SSL_ERROR_WANT_WRITE
) {
842 retc
= SSL_connect(clientssl
);
844 err
= SSL_get_error(clientssl
, retc
);
847 if (!clienterr
&& retc
<= 0 && err
!= SSL_ERROR_WANT_READ
) {
848 TEST_info("SSL_connect() failed %d, %d", retc
, err
);
851 if (want
!= SSL_ERROR_NONE
&& err
== want
)
854 err
= SSL_ERROR_WANT_WRITE
;
855 while (!servererr
&& rets
<= 0 && err
== SSL_ERROR_WANT_WRITE
) {
856 rets
= SSL_accept(serverssl
);
858 err
= SSL_get_error(serverssl
, rets
);
861 if (!servererr
&& rets
<= 0
862 && err
!= SSL_ERROR_WANT_READ
863 && err
!= SSL_ERROR_WANT_X509_LOOKUP
) {
864 TEST_info("SSL_accept() failed %d, %d", rets
, err
);
867 if (want
!= SSL_ERROR_NONE
&& err
== want
)
869 if (clienterr
&& servererr
)
871 if (isdtls
&& read
) {
872 unsigned char buf
[20];
874 /* Trigger any retransmits that may be appropriate */
875 if (rets
> 0 && retc
<= 0) {
876 if (SSL_read(serverssl
, buf
, sizeof(buf
)) > 0) {
877 /* We don't expect this to succeed! */
878 TEST_info("Unexpected SSL_read() success!");
882 if (retc
> 0 && rets
<= 0) {
883 if (SSL_read(clientssl
, buf
, sizeof(buf
)) > 0) {
884 /* We don't expect this to succeed! */
885 TEST_info("Unexpected SSL_read() success!");
890 if (++abortctr
== MAXLOOPS
) {
891 TEST_info("No progress made");
894 if (isdtls
&& abortctr
<= 50 && (abortctr
% 10) == 0) {
896 * It looks like we're just spinning. Pause for a short period to
897 * give the DTLS timer a chance to do something. We only do this for
898 * the first few times to prevent hangs.
902 } while (retc
<=0 || rets
<= 0);
908 * Create an SSL connection including any post handshake NewSessionTicket
911 int create_ssl_connection(SSL
*serverssl
, SSL
*clientssl
, int want
)
917 if (!create_bare_ssl_connection(serverssl
, clientssl
, want
, 1))
921 * We attempt to read some data on the client side which we expect to fail.
922 * This will ensure we have received the NewSessionTicket in TLSv1.3 where
923 * appropriate. We do this twice because there are 2 NewSessionTickets.
925 for (i
= 0; i
< 2; i
++) {
926 if (SSL_read_ex(clientssl
, &buf
, sizeof(buf
), &readbytes
) > 0) {
927 if (!TEST_ulong_eq(readbytes
, 0))
929 } else if (!TEST_int_eq(SSL_get_error(clientssl
, 0),
930 SSL_ERROR_WANT_READ
)) {
938 void shutdown_ssl_connection(SSL
*serverssl
, SSL
*clientssl
)
940 SSL_shutdown(clientssl
);
941 SSL_shutdown(serverssl
);