2 * Copyright 2016-2022 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"
14 #include "../testutil.h"
15 #include "internal/e_os.h" /* for ossl_sleep() etc. */
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>
29 static int tls_dump_new(BIO *bi);
30 static int tls_dump_free(BIO *a);
31 static int tls_dump_read(BIO *b, char *out, int outl);
32 static int tls_dump_write(BIO *b, const char *in, int inl);
33 static long tls_dump_ctrl(BIO *b, int cmd, long num, void *ptr);
34 static int tls_dump_gets(BIO *bp, char *buf, int size);
35 static int tls_dump_puts(BIO *bp, const char *str);
37 /* Choose a sufficiently large type likely to be unused for this custom BIO */
38 #define BIO_TYPE_TLS_DUMP_FILTER (0x80 | BIO_TYPE_FILTER)
39 #define BIO_TYPE_MEMPACKET_TEST 0x81
40 #define BIO_TYPE_ALWAYS_RETRY 0x82
42 static BIO_METHOD *method_tls_dump = NULL;
43 static BIO_METHOD *meth_mem = NULL;
44 static BIO_METHOD *meth_always_retry = NULL;
46 /* Note: Not thread safe! */
47 const BIO_METHOD *bio_f_tls_dump_filter(void)
49 if (method_tls_dump == NULL) {
50 method_tls_dump = BIO_meth_new(BIO_TYPE_TLS_DUMP_FILTER,
52 if (method_tls_dump == NULL
53 || !BIO_meth_set_write(method_tls_dump, tls_dump_write)
54 || !BIO_meth_set_read(method_tls_dump, tls_dump_read)
55 || !BIO_meth_set_puts(method_tls_dump, tls_dump_puts)
56 || !BIO_meth_set_gets(method_tls_dump, tls_dump_gets)
57 || !BIO_meth_set_ctrl(method_tls_dump, tls_dump_ctrl)
58 || !BIO_meth_set_create(method_tls_dump, tls_dump_new)
59 || !BIO_meth_set_destroy(method_tls_dump, tls_dump_free))
62 return method_tls_dump;
65 void bio_f_tls_dump_filter_free(void)
67 BIO_meth_free(method_tls_dump);
70 static int tls_dump_new(BIO *bio)
76 static int tls_dump_free(BIO *bio)
83 static void copy_flags(BIO *bio)
86 BIO *next = BIO_next(bio);
88 flags = BIO_test_flags(next, BIO_FLAGS_SHOULD_RETRY | BIO_FLAGS_RWS);
89 BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY | BIO_FLAGS_RWS);
90 BIO_set_flags(bio, flags);
93 #define RECORD_CONTENT_TYPE 0
94 #define RECORD_VERSION_HI 1
95 #define RECORD_VERSION_LO 2
96 #define RECORD_EPOCH_HI 3
97 #define RECORD_EPOCH_LO 4
98 #define RECORD_SEQUENCE_START 5
99 #define RECORD_SEQUENCE_END 10
100 #define RECORD_LEN_HI 11
101 #define RECORD_LEN_LO 12
105 #define MSG_LEN_MID 2
109 #define MSG_FRAG_OFF_HI 6
110 #define MSG_FRAG_OFF_MID 7
111 #define MSG_FRAG_OFF_LO 8
112 #define MSG_FRAG_LEN_HI 9
113 #define MSG_FRAG_LEN_MID 10
114 #define MSG_FRAG_LEN_LO 11
117 static void dump_data(const char *data, int len)
119 int rem, i, content, reclen, msglen, fragoff, fraglen, epoch;
122 printf("---- START OF PACKET ----\n");
125 rec = (unsigned char *)data;
130 printf("*---- START OF RECORD ----\n");
131 if (rem < DTLS1_RT_HEADER_LENGTH) {
132 printf("*---- RECORD TRUNCATED ----\n");
135 content = rec[RECORD_CONTENT_TYPE];
136 printf("** Record Content-type: %d\n", content);
137 printf("** Record Version: %02x%02x\n",
138 rec[RECORD_VERSION_HI], rec[RECORD_VERSION_LO]);
139 epoch = (rec[RECORD_EPOCH_HI] << 8) | rec[RECORD_EPOCH_LO];
140 printf("** Record Epoch: %d\n", epoch);
141 printf("** Record Sequence: ");
142 for (i = RECORD_SEQUENCE_START; i <= RECORD_SEQUENCE_END; i++)
143 printf("%02x", rec[i]);
144 reclen = (rec[RECORD_LEN_HI] << 8) | rec[RECORD_LEN_LO];
145 printf("\n** Record Length: %d\n", reclen);
147 /* Now look at message */
148 rec += DTLS1_RT_HEADER_LENGTH;
149 rem -= DTLS1_RT_HEADER_LENGTH;
150 if (content == SSL3_RT_HANDSHAKE) {
151 printf("**---- START OF HANDSHAKE MESSAGE FRAGMENT ----\n");
153 printf("**---- HANDSHAKE MESSAGE FRAGMENT ENCRYPTED ----\n");
154 } else if (rem < DTLS1_HM_HEADER_LENGTH
155 || reclen < DTLS1_HM_HEADER_LENGTH) {
156 printf("**---- HANDSHAKE MESSAGE FRAGMENT TRUNCATED ----\n");
158 printf("*** Message Type: %d\n", rec[MSG_TYPE]);
159 msglen = (rec[MSG_LEN_HI] << 16) | (rec[MSG_LEN_MID] << 8)
161 printf("*** Message Length: %d\n", msglen);
162 printf("*** Message sequence: %d\n",
163 (rec[MSG_SEQ_HI] << 8) | rec[MSG_SEQ_LO]);
164 fragoff = (rec[MSG_FRAG_OFF_HI] << 16)
165 | (rec[MSG_FRAG_OFF_MID] << 8)
166 | rec[MSG_FRAG_OFF_LO];
167 printf("*** Message Fragment offset: %d\n", fragoff);
168 fraglen = (rec[MSG_FRAG_LEN_HI] << 16)
169 | (rec[MSG_FRAG_LEN_MID] << 8)
170 | rec[MSG_FRAG_LEN_LO];
171 printf("*** Message Fragment len: %d\n", fraglen);
172 if (fragoff + fraglen > msglen)
173 printf("***---- HANDSHAKE MESSAGE FRAGMENT INVALID ----\n");
174 else if (reclen < fraglen)
175 printf("**---- HANDSHAKE MESSAGE FRAGMENT TRUNCATED ----\n");
177 printf("**---- END OF HANDSHAKE MESSAGE FRAGMENT ----\n");
181 printf("*---- RECORD TRUNCATED ----\n");
186 printf("*---- END OF RECORD ----\n");
189 printf("---- END OF PACKET ----\n\n");
193 static int tls_dump_read(BIO *bio, char *out, int outl)
196 BIO *next = BIO_next(bio);
198 ret = BIO_read(next, out, outl);
208 static int tls_dump_write(BIO *bio, const char *in, int inl)
211 BIO *next = BIO_next(bio);
213 ret = BIO_write(next, in, inl);
219 static long tls_dump_ctrl(BIO *bio, int cmd, long num, void *ptr)
222 BIO *next = BIO_next(bio);
232 ret = BIO_ctrl(next, cmd, num, ptr);
238 static int tls_dump_gets(BIO *bio, char *buf, int size)
240 /* We don't support this - not needed anyway */
244 static int tls_dump_puts(BIO *bio, const char *str)
246 return tls_dump_write(bio, str, strlen(str));
250 struct mempacket_st {
257 static void mempacket_free(MEMPACKET *pkt)
259 if (pkt->data != NULL)
260 OPENSSL_free(pkt->data);
264 typedef struct mempacket_test_ctx_st {
265 STACK_OF(MEMPACKET) *pkts;
267 unsigned int currrec;
268 unsigned int currpkt;
269 unsigned int lastpkt;
270 unsigned int injected;
271 unsigned int noinject;
272 unsigned int dropepoch;
275 } MEMPACKET_TEST_CTX;
277 static int mempacket_test_new(BIO *bi);
278 static int mempacket_test_free(BIO *a);
279 static int mempacket_test_read(BIO *b, char *out, int outl);
280 static int mempacket_test_write(BIO *b, const char *in, int inl);
281 static long mempacket_test_ctrl(BIO *b, int cmd, long num, void *ptr);
282 static int mempacket_test_gets(BIO *bp, char *buf, int size);
283 static int mempacket_test_puts(BIO *bp, const char *str);
285 const BIO_METHOD *bio_s_mempacket_test(void)
287 if (meth_mem == NULL) {
288 if (!TEST_ptr(meth_mem = BIO_meth_new(BIO_TYPE_MEMPACKET_TEST,
290 || !TEST_true(BIO_meth_set_write(meth_mem, mempacket_test_write))
291 || !TEST_true(BIO_meth_set_read(meth_mem, mempacket_test_read))
292 || !TEST_true(BIO_meth_set_puts(meth_mem, mempacket_test_puts))
293 || !TEST_true(BIO_meth_set_gets(meth_mem, mempacket_test_gets))
294 || !TEST_true(BIO_meth_set_ctrl(meth_mem, mempacket_test_ctrl))
295 || !TEST_true(BIO_meth_set_create(meth_mem, mempacket_test_new))
296 || !TEST_true(BIO_meth_set_destroy(meth_mem, mempacket_test_free)))
302 void bio_s_mempacket_test_free(void)
304 BIO_meth_free(meth_mem);
307 static int mempacket_test_new(BIO *bio)
309 MEMPACKET_TEST_CTX *ctx;
311 if (!TEST_ptr(ctx = OPENSSL_zalloc(sizeof(*ctx))))
313 if (!TEST_ptr(ctx->pkts = sk_MEMPACKET_new_null())) {
319 BIO_set_init(bio, 1);
320 BIO_set_data(bio, ctx);
324 static int mempacket_test_free(BIO *bio)
326 MEMPACKET_TEST_CTX *ctx = BIO_get_data(bio);
328 sk_MEMPACKET_pop_free(ctx->pkts, mempacket_free);
330 BIO_set_data(bio, NULL);
331 BIO_set_init(bio, 0);
335 /* Record Header values */
338 #define RECORD_SEQUENCE 10
339 #define RECORD_LEN_HI 11
340 #define RECORD_LEN_LO 12
342 #define STANDARD_PACKET 0
344 static int mempacket_test_read(BIO *bio, char *out, int outl)
346 MEMPACKET_TEST_CTX *ctx = BIO_get_data(bio);
350 unsigned int seq, offset, len, epoch;
352 BIO_clear_retry_flags(bio);
353 thispkt = sk_MEMPACKET_value(ctx->pkts, 0);
354 if (thispkt == NULL || thispkt->num != ctx->currpkt) {
355 /* Probably run out of data */
356 BIO_set_retry_read(bio);
359 (void)sk_MEMPACKET_shift(ctx->pkts);
362 if (outl > thispkt->len)
365 if (thispkt->type != INJECT_PACKET_IGNORE_REC_SEQ
366 && (ctx->injected || ctx->droprec >= 0)) {
368 * Overwrite the record sequence number. We strictly number them in
369 * the order received. Since we are actually a reliable transport
370 * we know that there won't be any re-ordering. We overwrite to deal
371 * with any packets that have been injected
373 for (rem = thispkt->len, rec = thispkt->data; rem > 0; rem -= len) {
374 if (rem < DTLS1_RT_HEADER_LENGTH)
376 epoch = (rec[EPOCH_HI] << 8) | rec[EPOCH_LO];
377 if (epoch != ctx->epoch) {
384 rec[RECORD_SEQUENCE - offset] = seq & 0xFF;
389 len = ((rec[RECORD_LEN_HI] << 8) | rec[RECORD_LEN_LO])
390 + DTLS1_RT_HEADER_LENGTH;
393 if (ctx->droprec == (int)ctx->currrec && ctx->dropepoch == epoch) {
395 memmove(rec, rec + len, rem - len);
399 BIO_set_retry_read(bio);
408 memcpy(out, thispkt->data, outl);
410 mempacket_free(thispkt);
415 * Look for records from different epochs and swap them around
417 int mempacket_swap_epoch(BIO *bio)
419 MEMPACKET_TEST_CTX *ctx = BIO_get_data(bio);
421 int rem, len, prevlen = 0, pktnum;
422 unsigned char *rec, *prevrec = NULL, *tmp;
424 int numpkts = sk_MEMPACKET_num(ctx->pkts);
430 * If there are multiple packets we only look in the last one. This should
431 * always be the one where any epoch change occurs.
433 thispkt = sk_MEMPACKET_value(ctx->pkts, numpkts - 1);
437 for (rem = thispkt->len, rec = thispkt->data; rem > 0; rem -= len, rec += len) {
438 if (rem < DTLS1_RT_HEADER_LENGTH)
440 epoch = (rec[EPOCH_HI] << 8) | rec[EPOCH_LO];
441 len = ((rec[RECORD_LEN_HI] << 8) | rec[RECORD_LEN_LO])
442 + DTLS1_RT_HEADER_LENGTH;
446 /* Assumes the epoch change does not happen on the first record */
447 if (epoch != ctx->epoch) {
452 * We found 2 records with different epochs. Take a copy of the
455 tmp = OPENSSL_malloc(prevlen);
459 memcpy(tmp, prevrec, prevlen);
461 * Move everything from this record onwards, including any trailing
462 * records, and overwrite the earlier record
464 memmove(prevrec, rec, rem);
465 thispkt->len -= prevlen;
466 pktnum = thispkt->num;
469 * Create a new packet for the earlier record that we took out and
470 * add it to the end of the packet list.
472 thispkt = OPENSSL_malloc(sizeof(*thispkt));
473 if (thispkt == NULL) {
477 thispkt->type = INJECT_PACKET;
479 thispkt->len = prevlen;
480 thispkt->num = pktnum + 1;
481 if (sk_MEMPACKET_insert(ctx->pkts, thispkt, numpkts) <= 0) {
483 OPENSSL_free(thispkt);
496 int mempacket_test_inject(BIO *bio, const char *in, int inl, int pktnum,
499 MEMPACKET_TEST_CTX *ctx = BIO_get_data(bio);
500 MEMPACKET *thispkt = NULL, *looppkt, *nextpkt, *allpkts[3];
502 const unsigned char *inu = (const unsigned char *)in;
503 size_t len = ((inu[RECORD_LEN_HI] << 8) | inu[RECORD_LEN_LO])
504 + DTLS1_RT_HEADER_LENGTH;
509 if ((size_t)inl < len)
512 if ((size_t)inl == len)
515 duprec = ctx->duprec > 0;
517 /* We don't support arbitrary injection when duplicating records */
518 if (duprec && pktnum != -1)
521 /* We only allow injection before we've started writing any data */
530 for (i = 0; i < (duprec ? 3 : 1); i++) {
531 if (!TEST_ptr(allpkts[i] = OPENSSL_malloc(sizeof(*thispkt))))
533 thispkt = allpkts[i];
535 if (!TEST_ptr(thispkt->data = OPENSSL_malloc(inl)))
538 * If we are duplicating the packet, we duplicate it three times. The
539 * first two times we drop the first record if there are more than one.
540 * In this way we know that libssl will not be able to make progress
541 * until it receives the last packet, and hence will be forced to
542 * buffer these records.
544 if (duprec && i != 2) {
545 memcpy(thispkt->data, in + len, inl - len);
546 thispkt->len = inl - len;
548 memcpy(thispkt->data, in, inl);
551 thispkt->num = (pktnum >= 0) ? (unsigned int)pktnum : ctx->lastpkt + i;
552 thispkt->type = type;
555 for (i = 0; (looppkt = sk_MEMPACKET_value(ctx->pkts, i)) != NULL; i++) {
556 /* Check if we found the right place to insert this packet */
557 if (looppkt->num > thispkt->num) {
558 if (sk_MEMPACKET_insert(ctx->pkts, thispkt, i) == 0)
560 /* If we're doing up front injection then we're done */
564 * We need to do some accounting on lastpkt. We increment it first,
565 * but it might now equal the value of injected packets, so we need
571 nextpkt = sk_MEMPACKET_value(ctx->pkts, i);
572 if (nextpkt != NULL && nextpkt->num == ctx->lastpkt)
577 } else if (looppkt->num == thispkt->num) {
578 if (!ctx->noinject) {
579 /* We injected two packets with the same packet number! */
587 * We didn't find any packets with a packet number equal to or greater than
588 * this one, so we just add it onto the end
590 for (i = 0; i < (duprec ? 3 : 1); i++) {
591 thispkt = allpkts[i];
592 if (!sk_MEMPACKET_push(ctx->pkts, thispkt))
602 for (i = 0; i < (ctx->duprec > 0 ? 3 : 1); i++)
603 mempacket_free(allpkts[i]);
607 static int mempacket_test_write(BIO *bio, const char *in, int inl)
609 return mempacket_test_inject(bio, in, inl, -1, STANDARD_PACKET);
612 static long mempacket_test_ctrl(BIO *bio, int cmd, long num, void *ptr)
615 MEMPACKET_TEST_CTX *ctx = BIO_get_data(bio);
620 ret = (long)(sk_MEMPACKET_num(ctx->pkts) == 0);
622 case BIO_CTRL_GET_CLOSE:
623 ret = BIO_get_shutdown(bio);
625 case BIO_CTRL_SET_CLOSE:
626 BIO_set_shutdown(bio, (int)num);
628 case BIO_CTRL_WPENDING:
631 case BIO_CTRL_PENDING:
632 thispkt = sk_MEMPACKET_value(ctx->pkts, 0);
641 case MEMPACKET_CTRL_SET_DROP_EPOCH:
642 ctx->dropepoch = (unsigned int)num;
644 case MEMPACKET_CTRL_SET_DROP_REC:
645 ctx->droprec = (int)num;
647 case MEMPACKET_CTRL_GET_DROP_REC:
650 case MEMPACKET_CTRL_SET_DUPLICATE_REC:
651 ctx->duprec = (int)num;
664 static int mempacket_test_gets(BIO *bio, char *buf, int size)
666 /* We don't support this - not needed anyway */
670 static int mempacket_test_puts(BIO *bio, const char *str)
672 return mempacket_test_write(bio, str, strlen(str));
675 static int always_retry_new(BIO *bi);
676 static int always_retry_free(BIO *a);
677 static int always_retry_read(BIO *b, char *out, int outl);
678 static int always_retry_write(BIO *b, const char *in, int inl);
679 static long always_retry_ctrl(BIO *b, int cmd, long num, void *ptr);
680 static int always_retry_gets(BIO *bp, char *buf, int size);
681 static int always_retry_puts(BIO *bp, const char *str);
683 const BIO_METHOD *bio_s_always_retry(void)
685 if (meth_always_retry == NULL) {
686 if (!TEST_ptr(meth_always_retry = BIO_meth_new(BIO_TYPE_ALWAYS_RETRY,
688 || !TEST_true(BIO_meth_set_write(meth_always_retry,
690 || !TEST_true(BIO_meth_set_read(meth_always_retry,
692 || !TEST_true(BIO_meth_set_puts(meth_always_retry,
694 || !TEST_true(BIO_meth_set_gets(meth_always_retry,
696 || !TEST_true(BIO_meth_set_ctrl(meth_always_retry,
698 || !TEST_true(BIO_meth_set_create(meth_always_retry,
700 || !TEST_true(BIO_meth_set_destroy(meth_always_retry,
704 return meth_always_retry;
707 void bio_s_always_retry_free(void)
709 BIO_meth_free(meth_always_retry);
712 static int always_retry_new(BIO *bio)
714 BIO_set_init(bio, 1);
718 static int always_retry_free(BIO *bio)
720 BIO_set_data(bio, NULL);
721 BIO_set_init(bio, 0);
725 static int always_retry_read(BIO *bio, char *out, int outl)
727 BIO_set_retry_read(bio);
731 static int always_retry_write(BIO *bio, const char *in, int inl)
733 BIO_set_retry_write(bio);
737 static long always_retry_ctrl(BIO *bio, int cmd, long num, void *ptr)
743 BIO_set_retry_write(bio);
757 static int always_retry_gets(BIO *bio, char *buf, int size)
759 BIO_set_retry_read(bio);
763 static int always_retry_puts(BIO *bio, const char *str)
765 BIO_set_retry_write(bio);
769 int create_ssl_ctx_pair(OSSL_LIB_CTX *libctx, const SSL_METHOD *sm,
770 const SSL_METHOD *cm, int min_proto_version,
771 int max_proto_version, SSL_CTX **sctx, SSL_CTX **cctx,
772 char *certfile, char *privkeyfile)
774 SSL_CTX *serverctx = NULL;
775 SSL_CTX *clientctx = NULL;
780 else if (!TEST_ptr(serverctx = SSL_CTX_new_ex(libctx, NULL, sm))
781 || !TEST_true(SSL_CTX_set_options(serverctx,
782 SSL_OP_ALLOW_CLIENT_RENEGOTIATION)))
789 else if (!TEST_ptr(clientctx = SSL_CTX_new_ex(libctx, NULL, cm)))
793 #if !defined(OPENSSL_NO_TLS1_3) \
794 && defined(OPENSSL_NO_EC) \
795 && defined(OPENSSL_NO_DH)
797 * There are no usable built-in TLSv1.3 groups if ec and dh are both
800 if (max_proto_version == 0
801 && (sm == TLS_server_method() || cm == TLS_client_method()))
802 max_proto_version = TLS1_2_VERSION;
805 if (serverctx != NULL
806 && ((min_proto_version > 0
807 && !TEST_true(SSL_CTX_set_min_proto_version(serverctx,
809 || (max_proto_version > 0
810 && !TEST_true(SSL_CTX_set_max_proto_version(serverctx,
811 max_proto_version)))))
813 if (clientctx != NULL
814 && ((min_proto_version > 0
815 && !TEST_true(SSL_CTX_set_min_proto_version(clientctx,
817 || (max_proto_version > 0
818 && !TEST_true(SSL_CTX_set_max_proto_version(clientctx,
819 max_proto_version)))))
822 if (serverctx != NULL && certfile != NULL && privkeyfile != NULL) {
823 if (!TEST_int_eq(SSL_CTX_use_certificate_file(serverctx, certfile,
824 SSL_FILETYPE_PEM), 1)
825 || !TEST_int_eq(SSL_CTX_use_PrivateKey_file(serverctx,
827 SSL_FILETYPE_PEM), 1)
828 || !TEST_int_eq(SSL_CTX_check_private_key(serverctx), 1))
839 if (sctx != NULL && *sctx == NULL)
840 SSL_CTX_free(serverctx);
841 if (cctx != NULL && *cctx == NULL)
842 SSL_CTX_free(clientctx);
846 #define MAXLOOPS 1000000
848 #if !defined(OPENSSL_NO_KTLS) && !defined(OPENSSL_NO_SOCK)
849 static int set_nb(int fd)
853 flags = fcntl(fd, F_GETFL, 0);
856 flags = fcntl(fd, F_SETFL, flags | O_NONBLOCK);
860 int create_test_sockets(int *cfdp, int *sfdp)
862 struct sockaddr_in sin;
863 const char *host = "127.0.0.1";
864 int cfd_connected = 0, ret = 0;
865 socklen_t slen = sizeof(sin);
866 int afd = -1, cfd = -1, sfd = -1;
868 memset ((char *) &sin, 0, sizeof(sin));
869 sin.sin_family = AF_INET;
870 sin.sin_addr.s_addr = inet_addr(host);
872 afd = socket(AF_INET, SOCK_STREAM, 0);
876 if (bind(afd, (struct sockaddr*)&sin, sizeof(sin)) < 0)
879 if (getsockname(afd, (struct sockaddr*)&sin, &slen) < 0)
882 if (listen(afd, 1) < 0)
885 cfd = socket(AF_INET, SOCK_STREAM, 0);
889 if (set_nb(afd) == -1)
892 while (sfd == -1 || !cfd_connected) {
893 sfd = accept(afd, NULL, 0);
894 if (sfd == -1 && errno != EAGAIN)
897 if (!cfd_connected && connect(cfd, (struct sockaddr*)&sin, sizeof(sin)) < 0)
903 if (set_nb(cfd) == -1 || set_nb(sfd) == -1)
921 int create_ssl_objects2(SSL_CTX *serverctx, SSL_CTX *clientctx, SSL **sssl,
922 SSL **cssl, int sfd, int cfd)
924 SSL *serverssl = NULL, *clientssl = NULL;
925 BIO *s_to_c_bio = NULL, *c_to_s_bio = NULL;
929 else if (!TEST_ptr(serverssl = SSL_new(serverctx)))
933 else if (!TEST_ptr(clientssl = SSL_new(clientctx)))
936 if (!TEST_ptr(s_to_c_bio = BIO_new_socket(sfd, BIO_NOCLOSE))
937 || !TEST_ptr(c_to_s_bio = BIO_new_socket(cfd, BIO_NOCLOSE)))
940 SSL_set_bio(clientssl, c_to_s_bio, c_to_s_bio);
941 SSL_set_bio(serverssl, s_to_c_bio, s_to_c_bio);
949 BIO_free(s_to_c_bio);
950 BIO_free(c_to_s_bio);
956 * NOTE: Transfers control of the BIOs - this function will free them on error
958 int create_ssl_objects(SSL_CTX *serverctx, SSL_CTX *clientctx, SSL **sssl,
959 SSL **cssl, BIO *s_to_c_fbio, BIO *c_to_s_fbio)
961 SSL *serverssl = NULL, *clientssl = NULL;
962 BIO *s_to_c_bio = NULL, *c_to_s_bio = NULL;
966 else if (!TEST_ptr(serverssl = SSL_new(serverctx)))
970 else if (!TEST_ptr(clientssl = SSL_new(clientctx)))
973 if (SSL_is_dtls(clientssl)) {
974 if (!TEST_ptr(s_to_c_bio = BIO_new(bio_s_mempacket_test()))
975 || !TEST_ptr(c_to_s_bio = BIO_new(bio_s_mempacket_test())))
978 if (!TEST_ptr(s_to_c_bio = BIO_new(BIO_s_mem()))
979 || !TEST_ptr(c_to_s_bio = BIO_new(BIO_s_mem())))
983 if (s_to_c_fbio != NULL
984 && !TEST_ptr(s_to_c_bio = BIO_push(s_to_c_fbio, s_to_c_bio)))
986 if (c_to_s_fbio != NULL
987 && !TEST_ptr(c_to_s_bio = BIO_push(c_to_s_fbio, c_to_s_bio)))
990 /* Set Non-blocking IO behaviour */
991 BIO_set_mem_eof_return(s_to_c_bio, -1);
992 BIO_set_mem_eof_return(c_to_s_bio, -1);
994 /* Up ref these as we are passing them to two SSL objects */
995 SSL_set_bio(serverssl, c_to_s_bio, s_to_c_bio);
996 BIO_up_ref(s_to_c_bio);
997 BIO_up_ref(c_to_s_bio);
998 SSL_set_bio(clientssl, s_to_c_bio, c_to_s_bio);
1004 SSL_free(serverssl);
1005 SSL_free(clientssl);
1006 BIO_free(s_to_c_bio);
1007 BIO_free(c_to_s_bio);
1008 BIO_free(s_to_c_fbio);
1009 BIO_free(c_to_s_fbio);
1015 * Create an SSL connection, but does not read any post-handshake
1016 * NewSessionTicket messages.
1017 * If |read| is set and we're using DTLS then we will attempt to SSL_read on
1018 * the connection once we've completed one half of it, to ensure any retransmits
1020 * We stop the connection attempt (and return a failure value) if either peer
1021 * has SSL_get_error() return the value in the |want| parameter. The connection
1022 * attempt could be restarted by a subsequent call to this function.
1024 int create_bare_ssl_connection(SSL *serverssl, SSL *clientssl, int want,
1027 int retc = -1, rets = -1, err, abortctr = 0;
1028 int clienterr = 0, servererr = 0;
1029 int isdtls = SSL_is_dtls(serverssl);
1032 err = SSL_ERROR_WANT_WRITE;
1033 while (!clienterr && retc <= 0 && err == SSL_ERROR_WANT_WRITE) {
1034 retc = SSL_connect(clientssl);
1036 err = SSL_get_error(clientssl, retc);
1039 if (!clienterr && retc <= 0 && err != SSL_ERROR_WANT_READ) {
1040 TEST_info("SSL_connect() failed %d, %d", retc, err);
1041 if (want != SSL_ERROR_SSL)
1042 TEST_openssl_errors();
1045 if (want != SSL_ERROR_NONE && err == want)
1048 err = SSL_ERROR_WANT_WRITE;
1049 while (!servererr && rets <= 0 && err == SSL_ERROR_WANT_WRITE) {
1050 rets = SSL_accept(serverssl);
1052 err = SSL_get_error(serverssl, rets);
1055 if (!servererr && rets <= 0
1056 && err != SSL_ERROR_WANT_READ
1057 && err != SSL_ERROR_WANT_X509_LOOKUP) {
1058 TEST_info("SSL_accept() failed %d, %d", rets, err);
1059 if (want != SSL_ERROR_SSL)
1060 TEST_openssl_errors();
1063 if (want != SSL_ERROR_NONE && err == want)
1065 if (clienterr && servererr)
1067 if (isdtls && read) {
1068 unsigned char buf[20];
1070 /* Trigger any retransmits that may be appropriate */
1071 if (rets > 0 && retc <= 0) {
1072 if (SSL_read(serverssl, buf, sizeof(buf)) > 0) {
1073 /* We don't expect this to succeed! */
1074 TEST_info("Unexpected SSL_read() success!");
1078 if (retc > 0 && rets <= 0) {
1079 if (SSL_read(clientssl, buf, sizeof(buf)) > 0) {
1080 /* We don't expect this to succeed! */
1081 TEST_info("Unexpected SSL_read() success!");
1086 if (++abortctr == MAXLOOPS) {
1087 TEST_info("No progress made");
1090 if (isdtls && abortctr <= 50 && (abortctr % 10) == 0) {
1092 * It looks like we're just spinning. Pause for a short period to
1093 * give the DTLS timer a chance to do something. We only do this for
1094 * the first few times to prevent hangs.
1098 } while (retc <=0 || rets <= 0);
1104 * Create an SSL connection including any post handshake NewSessionTicket
1107 int create_ssl_connection(SSL *serverssl, SSL *clientssl, int want)
1113 if (!create_bare_ssl_connection(serverssl, clientssl, want, 1))
1117 * We attempt to read some data on the client side which we expect to fail.
1118 * This will ensure we have received the NewSessionTicket in TLSv1.3 where
1119 * appropriate. We do this twice because there are 2 NewSessionTickets.
1121 for (i = 0; i < 2; i++) {
1122 if (SSL_read_ex(clientssl, &buf, sizeof(buf), &readbytes) > 0) {
1123 if (!TEST_ulong_eq(readbytes, 0))
1125 } else if (!TEST_int_eq(SSL_get_error(clientssl, 0),
1126 SSL_ERROR_WANT_READ)) {
1134 void shutdown_ssl_connection(SSL *serverssl, SSL *clientssl)
1136 SSL_shutdown(clientssl);
1137 SSL_shutdown(serverssl);
1138 SSL_free(serverssl);
1139 SSL_free(clientssl);