2 * Copyright 2016-2018 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) {
21 usleep(millis * 1000);
26 static ossl_inline void ossl_sleep(unsigned int millis) {
30 /* Fallback to a busy wait */
31 static ossl_inline void ossl_sleep(unsigned int millis) {
32 struct timeval start, now;
33 unsigned int elapsedms;
35 gettimeofday(&start, NULL);
37 gettimeofday(&now, NULL);
38 elapsedms = (((now.tv_sec - start.tv_sec) * 1000000)
39 + now.tv_usec - start.tv_usec) / 1000;
40 } while (elapsedms < millis);
44 static int tls_dump_new(BIO *bi);
45 static int tls_dump_free(BIO *a);
46 static int tls_dump_read(BIO *b, char *out, int outl);
47 static int tls_dump_write(BIO *b, const char *in, int inl);
48 static long tls_dump_ctrl(BIO *b, int cmd, long num, void *ptr);
49 static int tls_dump_gets(BIO *bp, char *buf, int size);
50 static int tls_dump_puts(BIO *bp, const char *str);
52 /* Choose a sufficiently large type likely to be unused for this custom BIO */
53 #define BIO_TYPE_TLS_DUMP_FILTER (0x80 | BIO_TYPE_FILTER)
54 #define BIO_TYPE_MEMPACKET_TEST 0x81
56 static BIO_METHOD *method_tls_dump = NULL;
57 static BIO_METHOD *meth_mem = NULL;
59 /* Note: Not thread safe! */
60 const BIO_METHOD *bio_f_tls_dump_filter(void)
62 if (method_tls_dump == NULL) {
63 method_tls_dump = BIO_meth_new(BIO_TYPE_TLS_DUMP_FILTER,
65 if ( method_tls_dump == NULL
66 || !BIO_meth_set_write(method_tls_dump, tls_dump_write)
67 || !BIO_meth_set_read(method_tls_dump, tls_dump_read)
68 || !BIO_meth_set_puts(method_tls_dump, tls_dump_puts)
69 || !BIO_meth_set_gets(method_tls_dump, tls_dump_gets)
70 || !BIO_meth_set_ctrl(method_tls_dump, tls_dump_ctrl)
71 || !BIO_meth_set_create(method_tls_dump, tls_dump_new)
72 || !BIO_meth_set_destroy(method_tls_dump, tls_dump_free))
75 return method_tls_dump;
78 void bio_f_tls_dump_filter_free(void)
80 BIO_meth_free(method_tls_dump);
83 static int tls_dump_new(BIO *bio)
89 static int tls_dump_free(BIO *bio)
96 static void copy_flags(BIO *bio)
99 BIO *next = BIO_next(bio);
101 flags = BIO_test_flags(next, BIO_FLAGS_SHOULD_RETRY | BIO_FLAGS_RWS);
102 BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY | BIO_FLAGS_RWS);
103 BIO_set_flags(bio, flags);
106 #define RECORD_CONTENT_TYPE 0
107 #define RECORD_VERSION_HI 1
108 #define RECORD_VERSION_LO 2
109 #define RECORD_EPOCH_HI 3
110 #define RECORD_EPOCH_LO 4
111 #define RECORD_SEQUENCE_START 5
112 #define RECORD_SEQUENCE_END 10
113 #define RECORD_LEN_HI 11
114 #define RECORD_LEN_LO 12
118 #define MSG_LEN_MID 2
122 #define MSG_FRAG_OFF_HI 6
123 #define MSG_FRAG_OFF_MID 7
124 #define MSG_FRAG_OFF_LO 8
125 #define MSG_FRAG_LEN_HI 9
126 #define MSG_FRAG_LEN_MID 10
127 #define MSG_FRAG_LEN_LO 11
130 static void dump_data(const char *data, int len)
132 int rem, i, content, reclen, msglen, fragoff, fraglen, epoch;
135 printf("---- START OF PACKET ----\n");
138 rec = (unsigned char *)data;
143 printf("*---- START OF RECORD ----\n");
144 if (rem < DTLS1_RT_HEADER_LENGTH) {
145 printf("*---- RECORD TRUNCATED ----\n");
148 content = rec[RECORD_CONTENT_TYPE];
149 printf("** Record Content-type: %d\n", content);
150 printf("** Record Version: %02x%02x\n",
151 rec[RECORD_VERSION_HI], rec[RECORD_VERSION_LO]);
152 epoch = (rec[RECORD_EPOCH_HI] << 8) | rec[RECORD_EPOCH_LO];
153 printf("** Record Epoch: %d\n", epoch);
154 printf("** Record Sequence: ");
155 for (i = RECORD_SEQUENCE_START; i <= RECORD_SEQUENCE_END; i++)
156 printf("%02x", rec[i]);
157 reclen = (rec[RECORD_LEN_HI] << 8) | rec[RECORD_LEN_LO];
158 printf("\n** Record Length: %d\n", reclen);
160 /* Now look at message */
161 rec += DTLS1_RT_HEADER_LENGTH;
162 rem -= DTLS1_RT_HEADER_LENGTH;
163 if (content == SSL3_RT_HANDSHAKE) {
164 printf("**---- START OF HANDSHAKE MESSAGE FRAGMENT ----\n");
166 printf("**---- HANDSHAKE MESSAGE FRAGMENT ENCRYPTED ----\n");
167 } else if (rem < DTLS1_HM_HEADER_LENGTH
168 || reclen < DTLS1_HM_HEADER_LENGTH) {
169 printf("**---- HANDSHAKE MESSAGE FRAGMENT TRUNCATED ----\n");
171 printf("*** Message Type: %d\n", rec[MSG_TYPE]);
172 msglen = (rec[MSG_LEN_HI] << 16) | (rec[MSG_LEN_MID] << 8)
174 printf("*** Message Length: %d\n", msglen);
175 printf("*** Message sequence: %d\n",
176 (rec[MSG_SEQ_HI] << 8) | rec[MSG_SEQ_LO]);
177 fragoff = (rec[MSG_FRAG_OFF_HI] << 16)
178 | (rec[MSG_FRAG_OFF_MID] << 8)
179 | rec[MSG_FRAG_OFF_LO];
180 printf("*** Message Fragment offset: %d\n", fragoff);
181 fraglen = (rec[MSG_FRAG_LEN_HI] << 16)
182 | (rec[MSG_FRAG_LEN_MID] << 8)
183 | rec[MSG_FRAG_LEN_LO];
184 printf("*** Message Fragment len: %d\n", fraglen);
185 if (fragoff + fraglen > msglen)
186 printf("***---- HANDSHAKE MESSAGE FRAGMENT INVALID ----\n");
187 else if (reclen < fraglen)
188 printf("**---- HANDSHAKE MESSAGE FRAGMENT TRUNCATED ----\n");
190 printf("**---- END OF HANDSHAKE MESSAGE FRAGMENT ----\n");
194 printf("*---- RECORD TRUNCATED ----\n");
199 printf("*---- END OF RECORD ----\n");
202 printf("---- END OF PACKET ----\n\n");
206 static int tls_dump_read(BIO *bio, char *out, int outl)
209 BIO *next = BIO_next(bio);
211 ret = BIO_read(next, out, outl);
221 static int tls_dump_write(BIO *bio, const char *in, int inl)
224 BIO *next = BIO_next(bio);
226 ret = BIO_write(next, in, inl);
232 static long tls_dump_ctrl(BIO *bio, int cmd, long num, void *ptr)
235 BIO *next = BIO_next(bio);
245 ret = BIO_ctrl(next, cmd, num, ptr);
251 static int tls_dump_gets(BIO *bio, char *buf, int size)
253 /* We don't support this - not needed anyway */
257 static int tls_dump_puts(BIO *bio, const char *str)
259 return tls_dump_write(bio, str, strlen(str));
263 struct mempacket_st {
270 static void mempacket_free(MEMPACKET *pkt)
272 if (pkt->data != NULL)
273 OPENSSL_free(pkt->data);
277 typedef struct mempacket_test_ctx_st {
278 STACK_OF(MEMPACKET) *pkts;
280 unsigned int currrec;
281 unsigned int currpkt;
282 unsigned int lastpkt;
283 unsigned int injected;
284 unsigned int noinject;
285 unsigned int dropepoch;
287 } MEMPACKET_TEST_CTX;
289 static int mempacket_test_new(BIO *bi);
290 static int mempacket_test_free(BIO *a);
291 static int mempacket_test_read(BIO *b, char *out, int outl);
292 static int mempacket_test_write(BIO *b, const char *in, int inl);
293 static long mempacket_test_ctrl(BIO *b, int cmd, long num, void *ptr);
294 static int mempacket_test_gets(BIO *bp, char *buf, int size);
295 static int mempacket_test_puts(BIO *bp, const char *str);
297 const BIO_METHOD *bio_s_mempacket_test(void)
299 if (meth_mem == NULL) {
300 if (!TEST_ptr(meth_mem = BIO_meth_new(BIO_TYPE_MEMPACKET_TEST,
302 || !TEST_true(BIO_meth_set_write(meth_mem, mempacket_test_write))
303 || !TEST_true(BIO_meth_set_read(meth_mem, mempacket_test_read))
304 || !TEST_true(BIO_meth_set_puts(meth_mem, mempacket_test_puts))
305 || !TEST_true(BIO_meth_set_gets(meth_mem, mempacket_test_gets))
306 || !TEST_true(BIO_meth_set_ctrl(meth_mem, mempacket_test_ctrl))
307 || !TEST_true(BIO_meth_set_create(meth_mem, mempacket_test_new))
308 || !TEST_true(BIO_meth_set_destroy(meth_mem, mempacket_test_free)))
314 void bio_s_mempacket_test_free(void)
316 BIO_meth_free(meth_mem);
319 static int mempacket_test_new(BIO *bio)
321 MEMPACKET_TEST_CTX *ctx;
323 if (!TEST_ptr(ctx = OPENSSL_zalloc(sizeof(*ctx))))
325 if (!TEST_ptr(ctx->pkts = sk_MEMPACKET_new_null())) {
331 BIO_set_init(bio, 1);
332 BIO_set_data(bio, ctx);
336 static int mempacket_test_free(BIO *bio)
338 MEMPACKET_TEST_CTX *ctx = BIO_get_data(bio);
340 sk_MEMPACKET_pop_free(ctx->pkts, mempacket_free);
342 BIO_set_data(bio, NULL);
343 BIO_set_init(bio, 0);
347 /* Record Header values */
350 #define RECORD_SEQUENCE 10
351 #define RECORD_LEN_HI 11
352 #define RECORD_LEN_LO 12
354 #define STANDARD_PACKET 0
356 static int mempacket_test_read(BIO *bio, char *out, int outl)
358 MEMPACKET_TEST_CTX *ctx = BIO_get_data(bio);
362 unsigned int seq, offset, len, epoch;
364 BIO_clear_retry_flags(bio);
365 thispkt = sk_MEMPACKET_value(ctx->pkts, 0);
366 if (thispkt == NULL || thispkt->num != ctx->currpkt) {
367 /* Probably run out of data */
368 BIO_set_retry_read(bio);
371 (void)sk_MEMPACKET_shift(ctx->pkts);
374 if (outl > thispkt->len)
377 if (thispkt->type != INJECT_PACKET_IGNORE_REC_SEQ
378 && (ctx->injected || ctx->droprec >= 0)) {
380 * Overwrite the record sequence number. We strictly number them in
381 * the order received. Since we are actually a reliable transport
382 * we know that there won't be any re-ordering. We overwrite to deal
383 * with any packets that have been injected
385 for (rem = thispkt->len, rec = thispkt->data; rem > 0; rem -= len) {
386 if (rem < DTLS1_RT_HEADER_LENGTH)
388 epoch = (rec[EPOCH_HI] << 8) | rec[EPOCH_LO];
389 if (epoch != ctx->epoch) {
396 rec[RECORD_SEQUENCE - offset] = seq & 0xFF;
401 len = ((rec[RECORD_LEN_HI] << 8) | rec[RECORD_LEN_LO])
402 + DTLS1_RT_HEADER_LENGTH;
405 if (ctx->droprec == (int)ctx->currrec && ctx->dropepoch == epoch) {
407 memmove(rec, rec + len, rem - len);
411 BIO_set_retry_read(bio);
420 memcpy(out, thispkt->data, outl);
421 mempacket_free(thispkt);
425 int mempacket_test_inject(BIO *bio, const char *in, int inl, int pktnum,
428 MEMPACKET_TEST_CTX *ctx = BIO_get_data(bio);
429 MEMPACKET *thispkt, *looppkt, *nextpkt;
435 /* We only allow injection before we've started writing any data */
444 if (!TEST_ptr(thispkt = OPENSSL_malloc(sizeof(*thispkt))))
446 if (!TEST_ptr(thispkt->data = OPENSSL_malloc(inl))) {
447 mempacket_free(thispkt);
451 memcpy(thispkt->data, in, inl);
453 thispkt->num = (pktnum >= 0) ? (unsigned int)pktnum : ctx->lastpkt;
454 thispkt->type = type;
456 for(i = 0; (looppkt = sk_MEMPACKET_value(ctx->pkts, i)) != NULL; i++) {
457 /* Check if we found the right place to insert this packet */
458 if (looppkt->num > thispkt->num) {
459 if (sk_MEMPACKET_insert(ctx->pkts, thispkt, i) == 0) {
460 mempacket_free(thispkt);
463 /* If we're doing up front injection then we're done */
467 * We need to do some accounting on lastpkt. We increment it first,
468 * but it might now equal the value of injected packets, so we need
474 nextpkt = sk_MEMPACKET_value(ctx->pkts, i);
475 if (nextpkt != NULL && nextpkt->num == ctx->lastpkt)
480 } else if (looppkt->num == thispkt->num) {
481 if (!ctx->noinject) {
482 /* We injected two packets with the same packet number! */
490 * We didn't find any packets with a packet number equal to or greater than
491 * this one, so we just add it onto the end
493 if (!sk_MEMPACKET_push(ctx->pkts, thispkt)) {
494 mempacket_free(thispkt);
504 static int mempacket_test_write(BIO *bio, const char *in, int inl)
506 return mempacket_test_inject(bio, in, inl, -1, STANDARD_PACKET);
509 static long mempacket_test_ctrl(BIO *bio, int cmd, long num, void *ptr)
512 MEMPACKET_TEST_CTX *ctx = BIO_get_data(bio);
517 ret = (long)(sk_MEMPACKET_num(ctx->pkts) == 0);
519 case BIO_CTRL_GET_CLOSE:
520 ret = BIO_get_shutdown(bio);
522 case BIO_CTRL_SET_CLOSE:
523 BIO_set_shutdown(bio, (int)num);
525 case BIO_CTRL_WPENDING:
528 case BIO_CTRL_PENDING:
529 thispkt = sk_MEMPACKET_value(ctx->pkts, 0);
538 case MEMPACKET_CTRL_SET_DROP_EPOCH:
539 ctx->dropepoch = (unsigned int)num;
541 case MEMPACKET_CTRL_SET_DROP_REC:
542 ctx->droprec = (int)num;
544 case MEMPACKET_CTRL_GET_DROP_REC:
558 static int mempacket_test_gets(BIO *bio, char *buf, int size)
560 /* We don't support this - not needed anyway */
564 static int mempacket_test_puts(BIO *bio, const char *str)
566 return mempacket_test_write(bio, str, strlen(str));
569 int create_ssl_ctx_pair(const SSL_METHOD *sm, const SSL_METHOD *cm,
570 int min_proto_version, int max_proto_version,
571 SSL_CTX **sctx, SSL_CTX **cctx, char *certfile,
574 SSL_CTX *serverctx = NULL;
575 SSL_CTX *clientctx = NULL;
577 if (!TEST_ptr(serverctx = SSL_CTX_new(sm))
578 || (cctx != NULL && !TEST_ptr(clientctx = SSL_CTX_new(cm))))
581 if ((min_proto_version > 0
582 && !TEST_true(SSL_CTX_set_min_proto_version(serverctx,
584 || (max_proto_version > 0
585 && !TEST_true(SSL_CTX_set_max_proto_version(serverctx,
586 max_proto_version))))
588 if (clientctx != NULL
589 && ((min_proto_version > 0
590 && !TEST_true(SSL_CTX_set_min_proto_version(clientctx,
592 || (max_proto_version > 0
593 && !TEST_true(SSL_CTX_set_max_proto_version(clientctx,
594 max_proto_version)))))
597 if (certfile != NULL && privkeyfile != NULL) {
598 if (!TEST_int_eq(SSL_CTX_use_certificate_file(serverctx, certfile,
599 SSL_FILETYPE_PEM), 1)
600 || !TEST_int_eq(SSL_CTX_use_PrivateKey_file(serverctx,
602 SSL_FILETYPE_PEM), 1)
603 || !TEST_int_eq(SSL_CTX_check_private_key(serverctx), 1))
607 #ifndef OPENSSL_NO_DH
608 SSL_CTX_set_dh_auto(serverctx, 1);
617 SSL_CTX_free(serverctx);
618 SSL_CTX_free(clientctx);
622 #define MAXLOOPS 1000000
625 * NOTE: Transfers control of the BIOs - this function will free them on error
627 int create_ssl_objects(SSL_CTX *serverctx, SSL_CTX *clientctx, SSL **sssl,
628 SSL **cssl, BIO *s_to_c_fbio, BIO *c_to_s_fbio)
630 SSL *serverssl = NULL, *clientssl = NULL;
631 BIO *s_to_c_bio = NULL, *c_to_s_bio = NULL;
635 else if (!TEST_ptr(serverssl = SSL_new(serverctx)))
639 else if (!TEST_ptr(clientssl = SSL_new(clientctx)))
642 if (SSL_is_dtls(clientssl)) {
643 if (!TEST_ptr(s_to_c_bio = BIO_new(bio_s_mempacket_test()))
644 || !TEST_ptr(c_to_s_bio = BIO_new(bio_s_mempacket_test())))
647 if (!TEST_ptr(s_to_c_bio = BIO_new(BIO_s_mem()))
648 || !TEST_ptr(c_to_s_bio = BIO_new(BIO_s_mem())))
652 if (s_to_c_fbio != NULL
653 && !TEST_ptr(s_to_c_bio = BIO_push(s_to_c_fbio, s_to_c_bio)))
655 if (c_to_s_fbio != NULL
656 && !TEST_ptr(c_to_s_bio = BIO_push(c_to_s_fbio, c_to_s_bio)))
659 /* Set Non-blocking IO behaviour */
660 BIO_set_mem_eof_return(s_to_c_bio, -1);
661 BIO_set_mem_eof_return(c_to_s_bio, -1);
663 /* Up ref these as we are passing them to two SSL objects */
664 SSL_set_bio(serverssl, c_to_s_bio, s_to_c_bio);
665 BIO_up_ref(s_to_c_bio);
666 BIO_up_ref(c_to_s_bio);
667 SSL_set_bio(clientssl, s_to_c_bio, c_to_s_bio);
675 BIO_free(s_to_c_bio);
676 BIO_free(c_to_s_bio);
677 BIO_free(s_to_c_fbio);
678 BIO_free(c_to_s_fbio);
684 * Create an SSL connection, but does not ready any post-handshake
685 * NewSessionTicket messages.
687 int create_bare_ssl_connection(SSL *serverssl, SSL *clientssl, int want)
689 int retc = -1, rets = -1, err, abortctr = 0;
690 int clienterr = 0, servererr = 0;
691 int isdtls = SSL_is_dtls(serverssl);
694 err = SSL_ERROR_WANT_WRITE;
695 while (!clienterr && retc <= 0 && err == SSL_ERROR_WANT_WRITE) {
696 retc = SSL_connect(clientssl);
698 err = SSL_get_error(clientssl, retc);
701 if (!clienterr && retc <= 0 && err != SSL_ERROR_WANT_READ) {
702 TEST_info("SSL_connect() failed %d, %d", retc, err);
705 if (want != SSL_ERROR_NONE && err == want)
708 err = SSL_ERROR_WANT_WRITE;
709 while (!servererr && rets <= 0 && err == SSL_ERROR_WANT_WRITE) {
710 rets = SSL_accept(serverssl);
712 err = SSL_get_error(serverssl, rets);
715 if (!servererr && rets <= 0
716 && err != SSL_ERROR_WANT_READ
717 && err != SSL_ERROR_WANT_X509_LOOKUP) {
718 TEST_info("SSL_accept() failed %d, %d", rets, err);
721 if (want != SSL_ERROR_NONE && err == want)
723 if (clienterr && servererr)
726 if (rets > 0 && retc <= 0)
727 DTLSv1_handle_timeout(serverssl);
728 if (retc > 0 && rets <= 0)
729 DTLSv1_handle_timeout(clientssl);
731 if (++abortctr == MAXLOOPS) {
732 TEST_info("No progress made");
735 if (isdtls && abortctr <= 50 && (abortctr % 10) == 0) {
737 * It looks like we're just spinning. Pause for a short period to
738 * give the DTLS timer a chance to do something. We only do this for
739 * the first few times to prevent hangs.
743 } while (retc <=0 || rets <= 0);
749 * Create an SSL connection including any post handshake NewSessionTicket
752 int create_ssl_connection(SSL *serverssl, SSL *clientssl, int want)
758 if (!create_bare_ssl_connection(serverssl, clientssl, want))
762 * We attempt to read some data on the client side which we expect to fail.
763 * This will ensure we have received the NewSessionTicket in TLSv1.3 where
764 * appropriate. We do this twice because there are 2 NewSesionTickets.
766 for (i = 0; i < 2; i++) {
767 if (SSL_read_ex(clientssl, &buf, sizeof(buf), &readbytes) > 0) {
768 if (!TEST_ulong_eq(readbytes, 0))
770 } else if (!TEST_int_eq(SSL_get_error(clientssl, 0),
771 SSL_ERROR_WANT_READ)) {
779 void shutdown_ssl_connection(SSL *serverssl, SSL *clientssl)
781 SSL_shutdown(clientssl);
782 SSL_shutdown(serverssl);