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
66 static BIO_METHOD *method_tls_dump = NULL;
67 static BIO_METHOD *meth_mem = NULL;
69 /* Note: Not thread safe! */
70 const BIO_METHOD *bio_f_tls_dump_filter(void)
72 if (method_tls_dump == NULL) {
73 method_tls_dump = BIO_meth_new(BIO_TYPE_TLS_DUMP_FILTER,
75 if ( method_tls_dump == NULL
76 || !BIO_meth_set_write(method_tls_dump, tls_dump_write)
77 || !BIO_meth_set_read(method_tls_dump, tls_dump_read)
78 || !BIO_meth_set_puts(method_tls_dump, tls_dump_puts)
79 || !BIO_meth_set_gets(method_tls_dump, tls_dump_gets)
80 || !BIO_meth_set_ctrl(method_tls_dump, tls_dump_ctrl)
81 || !BIO_meth_set_create(method_tls_dump, tls_dump_new)
82 || !BIO_meth_set_destroy(method_tls_dump, tls_dump_free))
85 return method_tls_dump;
88 void bio_f_tls_dump_filter_free(void)
90 BIO_meth_free(method_tls_dump);
93 static int tls_dump_new(BIO *bio)
99 static int tls_dump_free(BIO *bio)
101 BIO_set_init(bio, 0);
106 static void copy_flags(BIO *bio)
109 BIO *next = BIO_next(bio);
111 flags = BIO_test_flags(next, BIO_FLAGS_SHOULD_RETRY | BIO_FLAGS_RWS);
112 BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY | BIO_FLAGS_RWS);
113 BIO_set_flags(bio, flags);
116 #define RECORD_CONTENT_TYPE 0
117 #define RECORD_VERSION_HI 1
118 #define RECORD_VERSION_LO 2
119 #define RECORD_EPOCH_HI 3
120 #define RECORD_EPOCH_LO 4
121 #define RECORD_SEQUENCE_START 5
122 #define RECORD_SEQUENCE_END 10
123 #define RECORD_LEN_HI 11
124 #define RECORD_LEN_LO 12
128 #define MSG_LEN_MID 2
132 #define MSG_FRAG_OFF_HI 6
133 #define MSG_FRAG_OFF_MID 7
134 #define MSG_FRAG_OFF_LO 8
135 #define MSG_FRAG_LEN_HI 9
136 #define MSG_FRAG_LEN_MID 10
137 #define MSG_FRAG_LEN_LO 11
140 static void dump_data(const char *data, int len)
142 int rem, i, content, reclen, msglen, fragoff, fraglen, epoch;
145 printf("---- START OF PACKET ----\n");
148 rec = (unsigned char *)data;
153 printf("*---- START OF RECORD ----\n");
154 if (rem < DTLS1_RT_HEADER_LENGTH) {
155 printf("*---- RECORD TRUNCATED ----\n");
158 content = rec[RECORD_CONTENT_TYPE];
159 printf("** Record Content-type: %d\n", content);
160 printf("** Record Version: %02x%02x\n",
161 rec[RECORD_VERSION_HI], rec[RECORD_VERSION_LO]);
162 epoch = (rec[RECORD_EPOCH_HI] << 8) | rec[RECORD_EPOCH_LO];
163 printf("** Record Epoch: %d\n", epoch);
164 printf("** Record Sequence: ");
165 for (i = RECORD_SEQUENCE_START; i <= RECORD_SEQUENCE_END; i++)
166 printf("%02x", rec[i]);
167 reclen = (rec[RECORD_LEN_HI] << 8) | rec[RECORD_LEN_LO];
168 printf("\n** Record Length: %d\n", reclen);
170 /* Now look at message */
171 rec += DTLS1_RT_HEADER_LENGTH;
172 rem -= DTLS1_RT_HEADER_LENGTH;
173 if (content == SSL3_RT_HANDSHAKE) {
174 printf("**---- START OF HANDSHAKE MESSAGE FRAGMENT ----\n");
176 printf("**---- HANDSHAKE MESSAGE FRAGMENT ENCRYPTED ----\n");
177 } else if (rem < DTLS1_HM_HEADER_LENGTH
178 || reclen < DTLS1_HM_HEADER_LENGTH) {
179 printf("**---- HANDSHAKE MESSAGE FRAGMENT TRUNCATED ----\n");
181 printf("*** Message Type: %d\n", rec[MSG_TYPE]);
182 msglen = (rec[MSG_LEN_HI] << 16) | (rec[MSG_LEN_MID] << 8)
184 printf("*** Message Length: %d\n", msglen);
185 printf("*** Message sequence: %d\n",
186 (rec[MSG_SEQ_HI] << 8) | rec[MSG_SEQ_LO]);
187 fragoff = (rec[MSG_FRAG_OFF_HI] << 16)
188 | (rec[MSG_FRAG_OFF_MID] << 8)
189 | rec[MSG_FRAG_OFF_LO];
190 printf("*** Message Fragment offset: %d\n", fragoff);
191 fraglen = (rec[MSG_FRAG_LEN_HI] << 16)
192 | (rec[MSG_FRAG_LEN_MID] << 8)
193 | rec[MSG_FRAG_LEN_LO];
194 printf("*** Message Fragment len: %d\n", fraglen);
195 if (fragoff + fraglen > msglen)
196 printf("***---- HANDSHAKE MESSAGE FRAGMENT INVALID ----\n");
197 else if (reclen < fraglen)
198 printf("**---- HANDSHAKE MESSAGE FRAGMENT TRUNCATED ----\n");
200 printf("**---- END OF HANDSHAKE MESSAGE FRAGMENT ----\n");
204 printf("*---- RECORD TRUNCATED ----\n");
209 printf("*---- END OF RECORD ----\n");
212 printf("---- END OF PACKET ----\n\n");
216 static int tls_dump_read(BIO *bio, char *out, int outl)
219 BIO *next = BIO_next(bio);
221 ret = BIO_read(next, out, outl);
231 static int tls_dump_write(BIO *bio, const char *in, int inl)
234 BIO *next = BIO_next(bio);
236 ret = BIO_write(next, in, inl);
242 static long tls_dump_ctrl(BIO *bio, int cmd, long num, void *ptr)
245 BIO *next = BIO_next(bio);
255 ret = BIO_ctrl(next, cmd, num, ptr);
261 static int tls_dump_gets(BIO *bio, char *buf, int size)
263 /* We don't support this - not needed anyway */
267 static int tls_dump_puts(BIO *bio, const char *str)
269 return tls_dump_write(bio, str, strlen(str));
273 struct mempacket_st {
280 static void mempacket_free(MEMPACKET *pkt)
282 if (pkt->data != NULL)
283 OPENSSL_free(pkt->data);
287 typedef struct mempacket_test_ctx_st {
288 STACK_OF(MEMPACKET) *pkts;
290 unsigned int currrec;
291 unsigned int currpkt;
292 unsigned int lastpkt;
293 unsigned int injected;
294 unsigned int noinject;
295 unsigned int dropepoch;
298 } MEMPACKET_TEST_CTX;
300 static int mempacket_test_new(BIO *bi);
301 static int mempacket_test_free(BIO *a);
302 static int mempacket_test_read(BIO *b, char *out, int outl);
303 static int mempacket_test_write(BIO *b, const char *in, int inl);
304 static long mempacket_test_ctrl(BIO *b, int cmd, long num, void *ptr);
305 static int mempacket_test_gets(BIO *bp, char *buf, int size);
306 static int mempacket_test_puts(BIO *bp, const char *str);
308 const BIO_METHOD *bio_s_mempacket_test(void)
310 if (meth_mem == NULL) {
311 if (!TEST_ptr(meth_mem = BIO_meth_new(BIO_TYPE_MEMPACKET_TEST,
313 || !TEST_true(BIO_meth_set_write(meth_mem, mempacket_test_write))
314 || !TEST_true(BIO_meth_set_read(meth_mem, mempacket_test_read))
315 || !TEST_true(BIO_meth_set_puts(meth_mem, mempacket_test_puts))
316 || !TEST_true(BIO_meth_set_gets(meth_mem, mempacket_test_gets))
317 || !TEST_true(BIO_meth_set_ctrl(meth_mem, mempacket_test_ctrl))
318 || !TEST_true(BIO_meth_set_create(meth_mem, mempacket_test_new))
319 || !TEST_true(BIO_meth_set_destroy(meth_mem, mempacket_test_free)))
325 void bio_s_mempacket_test_free(void)
327 BIO_meth_free(meth_mem);
330 static int mempacket_test_new(BIO *bio)
332 MEMPACKET_TEST_CTX *ctx;
334 if (!TEST_ptr(ctx = OPENSSL_zalloc(sizeof(*ctx))))
336 if (!TEST_ptr(ctx->pkts = sk_MEMPACKET_new_null())) {
342 BIO_set_init(bio, 1);
343 BIO_set_data(bio, ctx);
347 static int mempacket_test_free(BIO *bio)
349 MEMPACKET_TEST_CTX *ctx = BIO_get_data(bio);
351 sk_MEMPACKET_pop_free(ctx->pkts, mempacket_free);
353 BIO_set_data(bio, NULL);
354 BIO_set_init(bio, 0);
358 /* Record Header values */
361 #define RECORD_SEQUENCE 10
362 #define RECORD_LEN_HI 11
363 #define RECORD_LEN_LO 12
365 #define STANDARD_PACKET 0
367 static int mempacket_test_read(BIO *bio, char *out, int outl)
369 MEMPACKET_TEST_CTX *ctx = BIO_get_data(bio);
373 unsigned int seq, offset, len, epoch;
375 BIO_clear_retry_flags(bio);
376 thispkt = sk_MEMPACKET_value(ctx->pkts, 0);
377 if (thispkt == NULL || thispkt->num != ctx->currpkt) {
378 /* Probably run out of data */
379 BIO_set_retry_read(bio);
382 (void)sk_MEMPACKET_shift(ctx->pkts);
385 if (outl > thispkt->len)
388 if (thispkt->type != INJECT_PACKET_IGNORE_REC_SEQ
389 && (ctx->injected || ctx->droprec >= 0)) {
391 * Overwrite the record sequence number. We strictly number them in
392 * the order received. Since we are actually a reliable transport
393 * we know that there won't be any re-ordering. We overwrite to deal
394 * with any packets that have been injected
396 for (rem = thispkt->len, rec = thispkt->data; rem > 0; rem -= len) {
397 if (rem < DTLS1_RT_HEADER_LENGTH)
399 epoch = (rec[EPOCH_HI] << 8) | rec[EPOCH_LO];
400 if (epoch != ctx->epoch) {
407 rec[RECORD_SEQUENCE - offset] = seq & 0xFF;
412 len = ((rec[RECORD_LEN_HI] << 8) | rec[RECORD_LEN_LO])
413 + DTLS1_RT_HEADER_LENGTH;
416 if (ctx->droprec == (int)ctx->currrec && ctx->dropepoch == epoch) {
418 memmove(rec, rec + len, rem - len);
422 BIO_set_retry_read(bio);
431 memcpy(out, thispkt->data, outl);
432 mempacket_free(thispkt);
436 int mempacket_test_inject(BIO *bio, const char *in, int inl, int pktnum,
439 MEMPACKET_TEST_CTX *ctx = BIO_get_data(bio);
440 MEMPACKET *thispkt = NULL, *looppkt, *nextpkt, *allpkts[3];
442 const unsigned char *inu = (const unsigned char *)in;
443 size_t len = ((inu[RECORD_LEN_HI] << 8) | inu[RECORD_LEN_LO])
444 + DTLS1_RT_HEADER_LENGTH;
449 if ((size_t)inl < len)
452 if ((size_t)inl == len)
455 duprec = ctx->duprec > 0;
457 /* We don't support arbitrary injection when duplicating records */
458 if (duprec && pktnum != -1)
461 /* We only allow injection before we've started writing any data */
470 for (i = 0; i < (duprec ? 3 : 1); i++) {
471 if (!TEST_ptr(allpkts[i] = OPENSSL_malloc(sizeof(*thispkt))))
473 thispkt = allpkts[i];
475 if (!TEST_ptr(thispkt->data = OPENSSL_malloc(inl)))
478 * If we are duplicating the packet, we duplicate it three times. The
479 * first two times we drop the first record if there are more than one.
480 * In this way we know that libssl will not be able to make progress
481 * until it receives the last packet, and hence will be forced to
482 * buffer these records.
484 if (duprec && i != 2) {
485 memcpy(thispkt->data, in + len, inl - len);
486 thispkt->len = inl - len;
488 memcpy(thispkt->data, in, inl);
491 thispkt->num = (pktnum >= 0) ? (unsigned int)pktnum : ctx->lastpkt + i;
492 thispkt->type = type;
495 for(i = 0; (looppkt = sk_MEMPACKET_value(ctx->pkts, i)) != NULL; i++) {
496 /* Check if we found the right place to insert this packet */
497 if (looppkt->num > thispkt->num) {
498 if (sk_MEMPACKET_insert(ctx->pkts, thispkt, i) == 0)
500 /* If we're doing up front injection then we're done */
504 * We need to do some accounting on lastpkt. We increment it first,
505 * but it might now equal the value of injected packets, so we need
511 nextpkt = sk_MEMPACKET_value(ctx->pkts, i);
512 if (nextpkt != NULL && nextpkt->num == ctx->lastpkt)
517 } else if (looppkt->num == thispkt->num) {
518 if (!ctx->noinject) {
519 /* We injected two packets with the same packet number! */
527 * We didn't find any packets with a packet number equal to or greater than
528 * this one, so we just add it onto the end
530 for (i = 0; i < (duprec ? 3 : 1); i++) {
531 thispkt = allpkts[i];
532 if (!sk_MEMPACKET_push(ctx->pkts, thispkt))
542 for (i = 0; i < (ctx->duprec > 0 ? 3 : 1); i++)
543 mempacket_free(allpkts[i]);
547 static int mempacket_test_write(BIO *bio, const char *in, int inl)
549 return mempacket_test_inject(bio, in, inl, -1, STANDARD_PACKET);
552 static long mempacket_test_ctrl(BIO *bio, int cmd, long num, void *ptr)
555 MEMPACKET_TEST_CTX *ctx = BIO_get_data(bio);
560 ret = (long)(sk_MEMPACKET_num(ctx->pkts) == 0);
562 case BIO_CTRL_GET_CLOSE:
563 ret = BIO_get_shutdown(bio);
565 case BIO_CTRL_SET_CLOSE:
566 BIO_set_shutdown(bio, (int)num);
568 case BIO_CTRL_WPENDING:
571 case BIO_CTRL_PENDING:
572 thispkt = sk_MEMPACKET_value(ctx->pkts, 0);
581 case MEMPACKET_CTRL_SET_DROP_EPOCH:
582 ctx->dropepoch = (unsigned int)num;
584 case MEMPACKET_CTRL_SET_DROP_REC:
585 ctx->droprec = (int)num;
587 case MEMPACKET_CTRL_GET_DROP_REC:
590 case MEMPACKET_CTRL_SET_DUPLICATE_REC:
591 ctx->duprec = (int)num;
604 static int mempacket_test_gets(BIO *bio, char *buf, int size)
606 /* We don't support this - not needed anyway */
610 static int mempacket_test_puts(BIO *bio, const char *str)
612 return mempacket_test_write(bio, str, strlen(str));
615 int create_ssl_ctx_pair(const SSL_METHOD *sm, const SSL_METHOD *cm,
616 int min_proto_version, int max_proto_version,
617 SSL_CTX **sctx, SSL_CTX **cctx, char *certfile,
620 SSL_CTX *serverctx = NULL;
621 SSL_CTX *clientctx = NULL;
623 if (!TEST_ptr(serverctx = SSL_CTX_new(sm))
624 || (cctx != NULL && !TEST_ptr(clientctx = SSL_CTX_new(cm))))
627 if ((min_proto_version > 0
628 && !TEST_true(SSL_CTX_set_min_proto_version(serverctx,
630 || (max_proto_version > 0
631 && !TEST_true(SSL_CTX_set_max_proto_version(serverctx,
632 max_proto_version))))
634 if (clientctx != NULL
635 && ((min_proto_version > 0
636 && !TEST_true(SSL_CTX_set_min_proto_version(clientctx,
638 || (max_proto_version > 0
639 && !TEST_true(SSL_CTX_set_max_proto_version(clientctx,
640 max_proto_version)))))
643 if (certfile != NULL && privkeyfile != NULL) {
644 if (!TEST_int_eq(SSL_CTX_use_certificate_file(serverctx, certfile,
645 SSL_FILETYPE_PEM), 1)
646 || !TEST_int_eq(SSL_CTX_use_PrivateKey_file(serverctx,
648 SSL_FILETYPE_PEM), 1)
649 || !TEST_int_eq(SSL_CTX_check_private_key(serverctx), 1))
653 #ifndef OPENSSL_NO_DH
654 SSL_CTX_set_dh_auto(serverctx, 1);
663 SSL_CTX_free(serverctx);
664 SSL_CTX_free(clientctx);
668 #define MAXLOOPS 1000000
671 * NOTE: Transfers control of the BIOs - this function will free them on error
673 int create_ssl_objects(SSL_CTX *serverctx, SSL_CTX *clientctx, SSL **sssl,
674 SSL **cssl, BIO *s_to_c_fbio, BIO *c_to_s_fbio)
676 SSL *serverssl = NULL, *clientssl = NULL;
677 BIO *s_to_c_bio = NULL, *c_to_s_bio = NULL;
681 else if (!TEST_ptr(serverssl = SSL_new(serverctx)))
685 else if (!TEST_ptr(clientssl = SSL_new(clientctx)))
688 if (SSL_is_dtls(clientssl)) {
689 if (!TEST_ptr(s_to_c_bio = BIO_new(bio_s_mempacket_test()))
690 || !TEST_ptr(c_to_s_bio = BIO_new(bio_s_mempacket_test())))
693 if (!TEST_ptr(s_to_c_bio = BIO_new(BIO_s_mem()))
694 || !TEST_ptr(c_to_s_bio = BIO_new(BIO_s_mem())))
698 if (s_to_c_fbio != NULL
699 && !TEST_ptr(s_to_c_bio = BIO_push(s_to_c_fbio, s_to_c_bio)))
701 if (c_to_s_fbio != NULL
702 && !TEST_ptr(c_to_s_bio = BIO_push(c_to_s_fbio, c_to_s_bio)))
705 /* Set Non-blocking IO behaviour */
706 BIO_set_mem_eof_return(s_to_c_bio, -1);
707 BIO_set_mem_eof_return(c_to_s_bio, -1);
709 /* Up ref these as we are passing them to two SSL objects */
710 SSL_set_bio(serverssl, c_to_s_bio, s_to_c_bio);
711 BIO_up_ref(s_to_c_bio);
712 BIO_up_ref(c_to_s_bio);
713 SSL_set_bio(clientssl, s_to_c_bio, c_to_s_bio);
721 BIO_free(s_to_c_bio);
722 BIO_free(c_to_s_bio);
723 BIO_free(s_to_c_fbio);
724 BIO_free(c_to_s_fbio);
730 * Create an SSL connection, but does not ready any post-handshake
731 * NewSessionTicket messages.
732 * If |read| is set and we're using DTLS then we will attempt to SSL_read on
733 * the connection once we've completed one half of it, to ensure any retransmits
736 int create_bare_ssl_connection(SSL *serverssl, SSL *clientssl, int want,
739 int retc = -1, rets = -1, err, abortctr = 0;
740 int clienterr = 0, servererr = 0;
741 int isdtls = SSL_is_dtls(serverssl);
744 err = SSL_ERROR_WANT_WRITE;
745 while (!clienterr && retc <= 0 && err == SSL_ERROR_WANT_WRITE) {
746 retc = SSL_connect(clientssl);
748 err = SSL_get_error(clientssl, retc);
751 if (!clienterr && retc <= 0 && err != SSL_ERROR_WANT_READ) {
752 TEST_info("SSL_connect() failed %d, %d", retc, err);
755 if (want != SSL_ERROR_NONE && err == want)
758 err = SSL_ERROR_WANT_WRITE;
759 while (!servererr && rets <= 0 && err == SSL_ERROR_WANT_WRITE) {
760 rets = SSL_accept(serverssl);
762 err = SSL_get_error(serverssl, rets);
765 if (!servererr && rets <= 0
766 && err != SSL_ERROR_WANT_READ
767 && err != SSL_ERROR_WANT_X509_LOOKUP) {
768 TEST_info("SSL_accept() failed %d, %d", rets, err);
771 if (want != SSL_ERROR_NONE && err == want)
773 if (clienterr && servererr)
775 if (isdtls && read) {
776 unsigned char buf[20];
778 /* Trigger any retransmits that may be appropriate */
779 if (rets > 0 && retc <= 0) {
780 if (SSL_read(serverssl, buf, sizeof(buf)) > 0) {
781 /* We don't expect this to succeed! */
782 TEST_info("Unexpected SSL_read() success!");
786 if (retc > 0 && rets <= 0) {
787 if (SSL_read(clientssl, buf, sizeof(buf)) > 0) {
788 /* We don't expect this to succeed! */
789 TEST_info("Unexpected SSL_read() success!");
794 if (++abortctr == MAXLOOPS) {
795 TEST_info("No progress made");
798 if (isdtls && abortctr <= 50 && (abortctr % 10) == 0) {
800 * It looks like we're just spinning. Pause for a short period to
801 * give the DTLS timer a chance to do something. We only do this for
802 * the first few times to prevent hangs.
806 } while (retc <=0 || rets <= 0);
812 * Create an SSL connection including any post handshake NewSessionTicket
815 int create_ssl_connection(SSL *serverssl, SSL *clientssl, int want)
821 if (!create_bare_ssl_connection(serverssl, clientssl, want, 1))
825 * We attempt to read some data on the client side which we expect to fail.
826 * This will ensure we have received the NewSessionTicket in TLSv1.3 where
827 * appropriate. We do this twice because there are 2 NewSesionTickets.
829 for (i = 0; i < 2; i++) {
830 if (SSL_read_ex(clientssl, &buf, sizeof(buf), &readbytes) > 0) {
831 if (!TEST_ulong_eq(readbytes, 0))
833 } else if (!TEST_int_eq(SSL_get_error(clientssl, 0),
834 SSL_ERROR_WANT_READ)) {
842 void shutdown_ssl_connection(SSL *serverssl, SSL *clientssl)
844 SSL_shutdown(clientssl);
845 SSL_shutdown(serverssl);