2 * Copyright 2016-2020 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 "internal/cryptlib.h" /* for ossl_sleep() */
14 #include "ssltestlib.h"
18 #ifdef OPENSSL_SYS_UNIX
20 # ifndef OPENSSL_NO_KTLS
21 # include <netinet/in.h>
22 # include <netinet/in.h>
23 # include <arpa/inet.h>
24 # include <sys/socket.h>
30 static int tls_dump_new(BIO *bi);
31 static int tls_dump_free(BIO *a);
32 static int tls_dump_read(BIO *b, char *out, int outl);
33 static int tls_dump_write(BIO *b, const char *in, int inl);
34 static long tls_dump_ctrl(BIO *b, int cmd, long num, void *ptr);
35 static int tls_dump_gets(BIO *bp, char *buf, int size);
36 static int tls_dump_puts(BIO *bp, const char *str);
38 /* Choose a sufficiently large type likely to be unused for this custom BIO */
39 #define BIO_TYPE_TLS_DUMP_FILTER (0x80 | BIO_TYPE_FILTER)
40 #define BIO_TYPE_MEMPACKET_TEST 0x81
41 #define BIO_TYPE_ALWAYS_RETRY 0x82
43 static BIO_METHOD *method_tls_dump = NULL;
44 static BIO_METHOD *meth_mem = NULL;
45 static BIO_METHOD *meth_always_retry = NULL;
47 /* Note: Not thread safe! */
48 const BIO_METHOD *bio_f_tls_dump_filter(void)
50 if (method_tls_dump == NULL) {
51 method_tls_dump = BIO_meth_new(BIO_TYPE_TLS_DUMP_FILTER,
53 if ( method_tls_dump == NULL
54 || !BIO_meth_set_write(method_tls_dump, tls_dump_write)
55 || !BIO_meth_set_read(method_tls_dump, tls_dump_read)
56 || !BIO_meth_set_puts(method_tls_dump, tls_dump_puts)
57 || !BIO_meth_set_gets(method_tls_dump, tls_dump_gets)
58 || !BIO_meth_set_ctrl(method_tls_dump, tls_dump_ctrl)
59 || !BIO_meth_set_create(method_tls_dump, tls_dump_new)
60 || !BIO_meth_set_destroy(method_tls_dump, tls_dump_free))
63 return method_tls_dump;
66 void bio_f_tls_dump_filter_free(void)
68 BIO_meth_free(method_tls_dump);
71 static int tls_dump_new(BIO *bio)
77 static int tls_dump_free(BIO *bio)
84 static void copy_flags(BIO *bio)
87 BIO *next = BIO_next(bio);
89 flags = BIO_test_flags(next, BIO_FLAGS_SHOULD_RETRY | BIO_FLAGS_RWS);
90 BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY | BIO_FLAGS_RWS);
91 BIO_set_flags(bio, flags);
94 #define RECORD_CONTENT_TYPE 0
95 #define RECORD_VERSION_HI 1
96 #define RECORD_VERSION_LO 2
97 #define RECORD_EPOCH_HI 3
98 #define RECORD_EPOCH_LO 4
99 #define RECORD_SEQUENCE_START 5
100 #define RECORD_SEQUENCE_END 10
101 #define RECORD_LEN_HI 11
102 #define RECORD_LEN_LO 12
106 #define MSG_LEN_MID 2
110 #define MSG_FRAG_OFF_HI 6
111 #define MSG_FRAG_OFF_MID 7
112 #define MSG_FRAG_OFF_LO 8
113 #define MSG_FRAG_LEN_HI 9
114 #define MSG_FRAG_LEN_MID 10
115 #define MSG_FRAG_LEN_LO 11
118 static void dump_data(const char *data, int len)
120 int rem, i, content, reclen, msglen, fragoff, fraglen, epoch;
123 printf("---- START OF PACKET ----\n");
126 rec = (unsigned char *)data;
131 printf("*---- START OF RECORD ----\n");
132 if (rem < DTLS1_RT_HEADER_LENGTH) {
133 printf("*---- RECORD TRUNCATED ----\n");
136 content = rec[RECORD_CONTENT_TYPE];
137 printf("** Record Content-type: %d\n", content);
138 printf("** Record Version: %02x%02x\n",
139 rec[RECORD_VERSION_HI], rec[RECORD_VERSION_LO]);
140 epoch = (rec[RECORD_EPOCH_HI] << 8) | rec[RECORD_EPOCH_LO];
141 printf("** Record Epoch: %d\n", epoch);
142 printf("** Record Sequence: ");
143 for (i = RECORD_SEQUENCE_START; i <= RECORD_SEQUENCE_END; i++)
144 printf("%02x", rec[i]);
145 reclen = (rec[RECORD_LEN_HI] << 8) | rec[RECORD_LEN_LO];
146 printf("\n** Record Length: %d\n", reclen);
148 /* Now look at message */
149 rec += DTLS1_RT_HEADER_LENGTH;
150 rem -= DTLS1_RT_HEADER_LENGTH;
151 if (content == SSL3_RT_HANDSHAKE) {
152 printf("**---- START OF HANDSHAKE MESSAGE FRAGMENT ----\n");
154 printf("**---- HANDSHAKE MESSAGE FRAGMENT ENCRYPTED ----\n");
155 } else if (rem < DTLS1_HM_HEADER_LENGTH
156 || reclen < DTLS1_HM_HEADER_LENGTH) {
157 printf("**---- HANDSHAKE MESSAGE FRAGMENT TRUNCATED ----\n");
159 printf("*** Message Type: %d\n", rec[MSG_TYPE]);
160 msglen = (rec[MSG_LEN_HI] << 16) | (rec[MSG_LEN_MID] << 8)
162 printf("*** Message Length: %d\n", msglen);
163 printf("*** Message sequence: %d\n",
164 (rec[MSG_SEQ_HI] << 8) | rec[MSG_SEQ_LO]);
165 fragoff = (rec[MSG_FRAG_OFF_HI] << 16)
166 | (rec[MSG_FRAG_OFF_MID] << 8)
167 | rec[MSG_FRAG_OFF_LO];
168 printf("*** Message Fragment offset: %d\n", fragoff);
169 fraglen = (rec[MSG_FRAG_LEN_HI] << 16)
170 | (rec[MSG_FRAG_LEN_MID] << 8)
171 | rec[MSG_FRAG_LEN_LO];
172 printf("*** Message Fragment len: %d\n", fraglen);
173 if (fragoff + fraglen > msglen)
174 printf("***---- HANDSHAKE MESSAGE FRAGMENT INVALID ----\n");
175 else if (reclen < fraglen)
176 printf("**---- HANDSHAKE MESSAGE FRAGMENT TRUNCATED ----\n");
178 printf("**---- END OF HANDSHAKE MESSAGE FRAGMENT ----\n");
182 printf("*---- RECORD TRUNCATED ----\n");
187 printf("*---- END OF RECORD ----\n");
190 printf("---- END OF PACKET ----\n\n");
194 static int tls_dump_read(BIO *bio, char *out, int outl)
197 BIO *next = BIO_next(bio);
199 ret = BIO_read(next, out, outl);
209 static int tls_dump_write(BIO *bio, const char *in, int inl)
212 BIO *next = BIO_next(bio);
214 ret = BIO_write(next, in, inl);
220 static long tls_dump_ctrl(BIO *bio, int cmd, long num, void *ptr)
223 BIO *next = BIO_next(bio);
233 ret = BIO_ctrl(next, cmd, num, ptr);
239 static int tls_dump_gets(BIO *bio, char *buf, int size)
241 /* We don't support this - not needed anyway */
245 static int tls_dump_puts(BIO *bio, const char *str)
247 return tls_dump_write(bio, str, strlen(str));
251 struct mempacket_st {
258 static void mempacket_free(MEMPACKET *pkt)
260 if (pkt->data != NULL)
261 OPENSSL_free(pkt->data);
265 typedef struct mempacket_test_ctx_st {
266 STACK_OF(MEMPACKET) *pkts;
268 unsigned int currrec;
269 unsigned int currpkt;
270 unsigned int lastpkt;
271 unsigned int injected;
272 unsigned int noinject;
273 unsigned int dropepoch;
276 } MEMPACKET_TEST_CTX;
278 static int mempacket_test_new(BIO *bi);
279 static int mempacket_test_free(BIO *a);
280 static int mempacket_test_read(BIO *b, char *out, int outl);
281 static int mempacket_test_write(BIO *b, const char *in, int inl);
282 static long mempacket_test_ctrl(BIO *b, int cmd, long num, void *ptr);
283 static int mempacket_test_gets(BIO *bp, char *buf, int size);
284 static int mempacket_test_puts(BIO *bp, const char *str);
286 const BIO_METHOD *bio_s_mempacket_test(void)
288 if (meth_mem == NULL) {
289 if (!TEST_ptr(meth_mem = BIO_meth_new(BIO_TYPE_MEMPACKET_TEST,
291 || !TEST_true(BIO_meth_set_write(meth_mem, mempacket_test_write))
292 || !TEST_true(BIO_meth_set_read(meth_mem, mempacket_test_read))
293 || !TEST_true(BIO_meth_set_puts(meth_mem, mempacket_test_puts))
294 || !TEST_true(BIO_meth_set_gets(meth_mem, mempacket_test_gets))
295 || !TEST_true(BIO_meth_set_ctrl(meth_mem, mempacket_test_ctrl))
296 || !TEST_true(BIO_meth_set_create(meth_mem, mempacket_test_new))
297 || !TEST_true(BIO_meth_set_destroy(meth_mem, mempacket_test_free)))
303 void bio_s_mempacket_test_free(void)
305 BIO_meth_free(meth_mem);
308 static int mempacket_test_new(BIO *bio)
310 MEMPACKET_TEST_CTX *ctx;
312 if (!TEST_ptr(ctx = OPENSSL_zalloc(sizeof(*ctx))))
314 if (!TEST_ptr(ctx->pkts = sk_MEMPACKET_new_null())) {
320 BIO_set_init(bio, 1);
321 BIO_set_data(bio, ctx);
325 static int mempacket_test_free(BIO *bio)
327 MEMPACKET_TEST_CTX *ctx = BIO_get_data(bio);
329 sk_MEMPACKET_pop_free(ctx->pkts, mempacket_free);
331 BIO_set_data(bio, NULL);
332 BIO_set_init(bio, 0);
336 /* Record Header values */
339 #define RECORD_SEQUENCE 10
340 #define RECORD_LEN_HI 11
341 #define RECORD_LEN_LO 12
343 #define STANDARD_PACKET 0
345 static int mempacket_test_read(BIO *bio, char *out, int outl)
347 MEMPACKET_TEST_CTX *ctx = BIO_get_data(bio);
351 unsigned int seq, offset, len, epoch;
353 BIO_clear_retry_flags(bio);
354 thispkt = sk_MEMPACKET_value(ctx->pkts, 0);
355 if (thispkt == NULL || thispkt->num != ctx->currpkt) {
356 /* Probably run out of data */
357 BIO_set_retry_read(bio);
360 (void)sk_MEMPACKET_shift(ctx->pkts);
363 if (outl > thispkt->len)
366 if (thispkt->type != INJECT_PACKET_IGNORE_REC_SEQ
367 && (ctx->injected || ctx->droprec >= 0)) {
369 * Overwrite the record sequence number. We strictly number them in
370 * the order received. Since we are actually a reliable transport
371 * we know that there won't be any re-ordering. We overwrite to deal
372 * with any packets that have been injected
374 for (rem = thispkt->len, rec = thispkt->data; rem > 0; rem -= len) {
375 if (rem < DTLS1_RT_HEADER_LENGTH)
377 epoch = (rec[EPOCH_HI] << 8) | rec[EPOCH_LO];
378 if (epoch != ctx->epoch) {
385 rec[RECORD_SEQUENCE - offset] = seq & 0xFF;
390 len = ((rec[RECORD_LEN_HI] << 8) | rec[RECORD_LEN_LO])
391 + DTLS1_RT_HEADER_LENGTH;
394 if (ctx->droprec == (int)ctx->currrec && ctx->dropepoch == epoch) {
396 memmove(rec, rec + len, rem - len);
400 BIO_set_retry_read(bio);
409 memcpy(out, thispkt->data, outl);
410 mempacket_free(thispkt);
414 int mempacket_test_inject(BIO *bio, const char *in, int inl, int pktnum,
417 MEMPACKET_TEST_CTX *ctx = BIO_get_data(bio);
418 MEMPACKET *thispkt = NULL, *looppkt, *nextpkt, *allpkts[3];
420 const unsigned char *inu = (const unsigned char *)in;
421 size_t len = ((inu[RECORD_LEN_HI] << 8) | inu[RECORD_LEN_LO])
422 + DTLS1_RT_HEADER_LENGTH;
427 if ((size_t)inl < len)
430 if ((size_t)inl == len)
433 duprec = ctx->duprec > 0;
435 /* We don't support arbitrary injection when duplicating records */
436 if (duprec && pktnum != -1)
439 /* We only allow injection before we've started writing any data */
448 for (i = 0; i < (duprec ? 3 : 1); i++) {
449 if (!TEST_ptr(allpkts[i] = OPENSSL_malloc(sizeof(*thispkt))))
451 thispkt = allpkts[i];
453 if (!TEST_ptr(thispkt->data = OPENSSL_malloc(inl)))
456 * If we are duplicating the packet, we duplicate it three times. The
457 * first two times we drop the first record if there are more than one.
458 * In this way we know that libssl will not be able to make progress
459 * until it receives the last packet, and hence will be forced to
460 * buffer these records.
462 if (duprec && i != 2) {
463 memcpy(thispkt->data, in + len, inl - len);
464 thispkt->len = inl - len;
466 memcpy(thispkt->data, in, inl);
469 thispkt->num = (pktnum >= 0) ? (unsigned int)pktnum : ctx->lastpkt + i;
470 thispkt->type = type;
473 for(i = 0; (looppkt = sk_MEMPACKET_value(ctx->pkts, i)) != NULL; i++) {
474 /* Check if we found the right place to insert this packet */
475 if (looppkt->num > thispkt->num) {
476 if (sk_MEMPACKET_insert(ctx->pkts, thispkt, i) == 0)
478 /* If we're doing up front injection then we're done */
482 * We need to do some accounting on lastpkt. We increment it first,
483 * but it might now equal the value of injected packets, so we need
489 nextpkt = sk_MEMPACKET_value(ctx->pkts, i);
490 if (nextpkt != NULL && nextpkt->num == ctx->lastpkt)
495 } else if (looppkt->num == thispkt->num) {
496 if (!ctx->noinject) {
497 /* We injected two packets with the same packet number! */
505 * We didn't find any packets with a packet number equal to or greater than
506 * this one, so we just add it onto the end
508 for (i = 0; i < (duprec ? 3 : 1); i++) {
509 thispkt = allpkts[i];
510 if (!sk_MEMPACKET_push(ctx->pkts, thispkt))
520 for (i = 0; i < (ctx->duprec > 0 ? 3 : 1); i++)
521 mempacket_free(allpkts[i]);
525 static int mempacket_test_write(BIO *bio, const char *in, int inl)
527 return mempacket_test_inject(bio, in, inl, -1, STANDARD_PACKET);
530 static long mempacket_test_ctrl(BIO *bio, int cmd, long num, void *ptr)
533 MEMPACKET_TEST_CTX *ctx = BIO_get_data(bio);
538 ret = (long)(sk_MEMPACKET_num(ctx->pkts) == 0);
540 case BIO_CTRL_GET_CLOSE:
541 ret = BIO_get_shutdown(bio);
543 case BIO_CTRL_SET_CLOSE:
544 BIO_set_shutdown(bio, (int)num);
546 case BIO_CTRL_WPENDING:
549 case BIO_CTRL_PENDING:
550 thispkt = sk_MEMPACKET_value(ctx->pkts, 0);
559 case MEMPACKET_CTRL_SET_DROP_EPOCH:
560 ctx->dropepoch = (unsigned int)num;
562 case MEMPACKET_CTRL_SET_DROP_REC:
563 ctx->droprec = (int)num;
565 case MEMPACKET_CTRL_GET_DROP_REC:
568 case MEMPACKET_CTRL_SET_DUPLICATE_REC:
569 ctx->duprec = (int)num;
582 static int mempacket_test_gets(BIO *bio, char *buf, int size)
584 /* We don't support this - not needed anyway */
588 static int mempacket_test_puts(BIO *bio, const char *str)
590 return mempacket_test_write(bio, str, strlen(str));
593 static int always_retry_new(BIO *bi);
594 static int always_retry_free(BIO *a);
595 static int always_retry_read(BIO *b, char *out, int outl);
596 static int always_retry_write(BIO *b, const char *in, int inl);
597 static long always_retry_ctrl(BIO *b, int cmd, long num, void *ptr);
598 static int always_retry_gets(BIO *bp, char *buf, int size);
599 static int always_retry_puts(BIO *bp, const char *str);
601 const BIO_METHOD *bio_s_always_retry(void)
603 if (meth_always_retry == NULL) {
604 if (!TEST_ptr(meth_always_retry = BIO_meth_new(BIO_TYPE_ALWAYS_RETRY,
606 || !TEST_true(BIO_meth_set_write(meth_always_retry,
608 || !TEST_true(BIO_meth_set_read(meth_always_retry,
610 || !TEST_true(BIO_meth_set_puts(meth_always_retry,
612 || !TEST_true(BIO_meth_set_gets(meth_always_retry,
614 || !TEST_true(BIO_meth_set_ctrl(meth_always_retry,
616 || !TEST_true(BIO_meth_set_create(meth_always_retry,
618 || !TEST_true(BIO_meth_set_destroy(meth_always_retry,
622 return meth_always_retry;
625 void bio_s_always_retry_free(void)
627 BIO_meth_free(meth_always_retry);
630 static int always_retry_new(BIO *bio)
632 BIO_set_init(bio, 1);
636 static int always_retry_free(BIO *bio)
638 BIO_set_data(bio, NULL);
639 BIO_set_init(bio, 0);
643 static int always_retry_read(BIO *bio, char *out, int outl)
645 BIO_set_retry_read(bio);
649 static int always_retry_write(BIO *bio, const char *in, int inl)
651 BIO_set_retry_write(bio);
655 static long always_retry_ctrl(BIO *bio, int cmd, long num, void *ptr)
661 BIO_set_retry_write(bio);
675 static int always_retry_gets(BIO *bio, char *buf, int size)
677 BIO_set_retry_read(bio);
681 static int always_retry_puts(BIO *bio, const char *str)
683 BIO_set_retry_write(bio);
687 int create_ssl_ctx_pair(OPENSSL_CTX *libctx, const SSL_METHOD *sm,
688 const SSL_METHOD *cm,
689 int min_proto_version, int max_proto_version,
690 SSL_CTX **sctx, SSL_CTX **cctx, char *certfile,
693 SSL_CTX *serverctx = NULL;
694 SSL_CTX *clientctx = NULL;
698 else if (!TEST_ptr(serverctx = SSL_CTX_new_with_libctx(libctx, NULL, sm)))
704 else if (!TEST_ptr(clientctx = SSL_CTX_new_with_libctx(libctx, NULL, cm)))
708 if ((min_proto_version > 0
709 && !TEST_true(SSL_CTX_set_min_proto_version(serverctx,
711 || (max_proto_version > 0
712 && !TEST_true(SSL_CTX_set_max_proto_version(serverctx,
713 max_proto_version))))
715 if (clientctx != NULL
716 && ((min_proto_version > 0
717 && !TEST_true(SSL_CTX_set_min_proto_version(clientctx,
719 || (max_proto_version > 0
720 && !TEST_true(SSL_CTX_set_max_proto_version(clientctx,
721 max_proto_version)))))
724 if (certfile != NULL && privkeyfile != NULL) {
725 if (!TEST_int_eq(SSL_CTX_use_certificate_file(serverctx, certfile,
726 SSL_FILETYPE_PEM), 1)
727 || !TEST_int_eq(SSL_CTX_use_PrivateKey_file(serverctx,
729 SSL_FILETYPE_PEM), 1)
730 || !TEST_int_eq(SSL_CTX_check_private_key(serverctx), 1))
734 #ifndef OPENSSL_NO_DH
735 SSL_CTX_set_dh_auto(serverctx, 1);
744 SSL_CTX_free(serverctx);
745 SSL_CTX_free(clientctx);
749 #define MAXLOOPS 1000000
751 #if !defined(OPENSSL_NO_KTLS) && !defined(OPENSSL_NO_SOCK)
752 static int set_nb(int fd)
756 flags = fcntl(fd,F_GETFL,0);
759 flags = fcntl(fd, F_SETFL, flags | O_NONBLOCK);
763 int create_test_sockets(int *cfd, int *sfd)
765 struct sockaddr_in sin;
766 const char *host = "127.0.0.1";
767 int cfd_connected = 0, ret = 0;
768 socklen_t slen = sizeof(sin);
774 memset ((char *) &sin, 0, sizeof(sin));
775 sin.sin_family = AF_INET;
776 sin.sin_addr.s_addr = inet_addr(host);
778 afd = socket(AF_INET, SOCK_STREAM, 0);
782 if (bind(afd, (struct sockaddr*)&sin, sizeof(sin)) < 0)
785 if (getsockname(afd, (struct sockaddr*)&sin, &slen) < 0)
788 if (listen(afd, 1) < 0)
791 *cfd = socket(AF_INET, SOCK_STREAM, 0);
795 if (set_nb(afd) == -1)
798 while (*sfd == -1 || !cfd_connected ) {
799 *sfd = accept(afd, NULL, 0);
800 if (*sfd == -1 && errno != EAGAIN)
803 if (!cfd_connected && connect(*cfd, (struct sockaddr*)&sin, sizeof(sin)) < 0)
809 if (set_nb(*cfd) == -1 || set_nb(*sfd) == -1)
825 int create_ssl_objects2(SSL_CTX *serverctx, SSL_CTX *clientctx, SSL **sssl,
826 SSL **cssl, int sfd, int cfd)
828 SSL *serverssl = NULL, *clientssl = NULL;
829 BIO *s_to_c_bio = NULL, *c_to_s_bio = NULL;
833 else if (!TEST_ptr(serverssl = SSL_new(serverctx)))
837 else if (!TEST_ptr(clientssl = SSL_new(clientctx)))
840 if (!TEST_ptr(s_to_c_bio = BIO_new_socket(sfd, BIO_NOCLOSE))
841 || !TEST_ptr(c_to_s_bio = BIO_new_socket(cfd, BIO_NOCLOSE)))
844 SSL_set_bio(clientssl, c_to_s_bio, c_to_s_bio);
845 SSL_set_bio(serverssl, s_to_c_bio, s_to_c_bio);
853 BIO_free(s_to_c_bio);
854 BIO_free(c_to_s_bio);
860 * NOTE: Transfers control of the BIOs - this function will free them on error
862 int create_ssl_objects(SSL_CTX *serverctx, SSL_CTX *clientctx, SSL **sssl,
863 SSL **cssl, BIO *s_to_c_fbio, BIO *c_to_s_fbio)
865 SSL *serverssl = NULL, *clientssl = NULL;
866 BIO *s_to_c_bio = NULL, *c_to_s_bio = NULL;
870 else if (!TEST_ptr(serverssl = SSL_new(serverctx)))
874 else if (!TEST_ptr(clientssl = SSL_new(clientctx)))
877 if (SSL_is_dtls(clientssl)) {
878 if (!TEST_ptr(s_to_c_bio = BIO_new(bio_s_mempacket_test()))
879 || !TEST_ptr(c_to_s_bio = BIO_new(bio_s_mempacket_test())))
882 if (!TEST_ptr(s_to_c_bio = BIO_new(BIO_s_mem()))
883 || !TEST_ptr(c_to_s_bio = BIO_new(BIO_s_mem())))
887 if (s_to_c_fbio != NULL
888 && !TEST_ptr(s_to_c_bio = BIO_push(s_to_c_fbio, s_to_c_bio)))
890 if (c_to_s_fbio != NULL
891 && !TEST_ptr(c_to_s_bio = BIO_push(c_to_s_fbio, c_to_s_bio)))
894 /* Set Non-blocking IO behaviour */
895 BIO_set_mem_eof_return(s_to_c_bio, -1);
896 BIO_set_mem_eof_return(c_to_s_bio, -1);
898 /* Up ref these as we are passing them to two SSL objects */
899 SSL_set_bio(serverssl, c_to_s_bio, s_to_c_bio);
900 BIO_up_ref(s_to_c_bio);
901 BIO_up_ref(c_to_s_bio);
902 SSL_set_bio(clientssl, s_to_c_bio, c_to_s_bio);
910 BIO_free(s_to_c_bio);
911 BIO_free(c_to_s_bio);
912 BIO_free(s_to_c_fbio);
913 BIO_free(c_to_s_fbio);
919 * Create an SSL connection, but does not read any post-handshake
920 * NewSessionTicket messages.
921 * If |read| is set and we're using DTLS then we will attempt to SSL_read on
922 * the connection once we've completed one half of it, to ensure any retransmits
924 * We stop the connection attempt (and return a failure value) if either peer
925 * has SSL_get_error() return the value in the |want| parameter. The connection
926 * attempt could be restarted by a subsequent call to this function.
928 int create_bare_ssl_connection(SSL *serverssl, SSL *clientssl, int want,
931 int retc = -1, rets = -1, err, abortctr = 0;
932 int clienterr = 0, servererr = 0;
933 int isdtls = SSL_is_dtls(serverssl);
936 err = SSL_ERROR_WANT_WRITE;
937 while (!clienterr && retc <= 0 && err == SSL_ERROR_WANT_WRITE) {
938 retc = SSL_connect(clientssl);
940 err = SSL_get_error(clientssl, retc);
943 if (!clienterr && retc <= 0 && err != SSL_ERROR_WANT_READ) {
944 TEST_info("SSL_connect() failed %d, %d", retc, err);
945 if (want != SSL_ERROR_SSL)
946 TEST_openssl_errors();
949 if (want != SSL_ERROR_NONE && err == want)
952 err = SSL_ERROR_WANT_WRITE;
953 while (!servererr && rets <= 0 && err == SSL_ERROR_WANT_WRITE) {
954 rets = SSL_accept(serverssl);
956 err = SSL_get_error(serverssl, rets);
959 if (!servererr && rets <= 0
960 && err != SSL_ERROR_WANT_READ
961 && err != SSL_ERROR_WANT_X509_LOOKUP) {
962 TEST_info("SSL_accept() failed %d, %d", rets, err);
963 if (want != SSL_ERROR_SSL)
964 TEST_openssl_errors();
967 if (want != SSL_ERROR_NONE && err == want)
969 if (clienterr && servererr)
971 if (isdtls && read) {
972 unsigned char buf[20];
974 /* Trigger any retransmits that may be appropriate */
975 if (rets > 0 && retc <= 0) {
976 if (SSL_read(serverssl, buf, sizeof(buf)) > 0) {
977 /* We don't expect this to succeed! */
978 TEST_info("Unexpected SSL_read() success!");
982 if (retc > 0 && rets <= 0) {
983 if (SSL_read(clientssl, buf, sizeof(buf)) > 0) {
984 /* We don't expect this to succeed! */
985 TEST_info("Unexpected SSL_read() success!");
990 if (++abortctr == MAXLOOPS) {
991 TEST_info("No progress made");
994 if (isdtls && abortctr <= 50 && (abortctr % 10) == 0) {
996 * It looks like we're just spinning. Pause for a short period to
997 * give the DTLS timer a chance to do something. We only do this for
998 * the first few times to prevent hangs.
1002 } while (retc <=0 || rets <= 0);
1008 * Create an SSL connection including any post handshake NewSessionTicket
1011 int create_ssl_connection(SSL *serverssl, SSL *clientssl, int want)
1017 if (!create_bare_ssl_connection(serverssl, clientssl, want, 1))
1021 * We attempt to read some data on the client side which we expect to fail.
1022 * This will ensure we have received the NewSessionTicket in TLSv1.3 where
1023 * appropriate. We do this twice because there are 2 NewSessionTickets.
1025 for (i = 0; i < 2; i++) {
1026 if (SSL_read_ex(clientssl, &buf, sizeof(buf), &readbytes) > 0) {
1027 if (!TEST_ulong_eq(readbytes, 0))
1029 } else if (!TEST_int_eq(SSL_get_error(clientssl, 0),
1030 SSL_ERROR_WANT_READ)) {
1038 void shutdown_ssl_connection(SSL *serverssl, SSL *clientssl)
1040 SSL_shutdown(clientssl);
1041 SSL_shutdown(serverssl);
1042 SSL_free(serverssl);
1043 SSL_free(clientssl);