2 * Copyright 2016 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
13 #include "ssltestlib.h"
15 static int tls_dump_new(BIO *bi);
16 static int tls_dump_free(BIO *a);
17 static int tls_dump_read(BIO *b, char *out, int outl);
18 static int tls_dump_write(BIO *b, const char *in, int inl);
19 static long tls_dump_ctrl(BIO *b, int cmd, long num, void *ptr);
20 static int tls_dump_gets(BIO *bp, char *buf, int size);
21 static int tls_dump_puts(BIO *bp, const char *str);
23 /* Choose a sufficiently large type likely to be unused for this custom BIO */
24 # define BIO_TYPE_TLS_DUMP_FILTER (0x80 | BIO_TYPE_FILTER)
26 # define BIO_TYPE_MEMPACKET_TEST 0x81
28 static BIO_METHOD *method_tls_dump = NULL;
29 static BIO_METHOD *method_mempacket_test = NULL;
31 /* Note: Not thread safe! */
32 const BIO_METHOD *bio_f_tls_dump_filter(void)
34 if (method_tls_dump == NULL) {
35 method_tls_dump = BIO_meth_new(BIO_TYPE_TLS_DUMP_FILTER,
37 if ( method_tls_dump == NULL
38 || !BIO_meth_set_write(method_tls_dump, tls_dump_write)
39 || !BIO_meth_set_read(method_tls_dump, tls_dump_read)
40 || !BIO_meth_set_puts(method_tls_dump, tls_dump_puts)
41 || !BIO_meth_set_gets(method_tls_dump, tls_dump_gets)
42 || !BIO_meth_set_ctrl(method_tls_dump, tls_dump_ctrl)
43 || !BIO_meth_set_create(method_tls_dump, tls_dump_new)
44 || !BIO_meth_set_destroy(method_tls_dump, tls_dump_free))
47 return method_tls_dump;
50 void bio_f_tls_dump_filter_free(void)
52 BIO_meth_free(method_tls_dump);
55 static int tls_dump_new(BIO *bio)
61 static int tls_dump_free(BIO *bio)
68 static void copy_flags(BIO *bio)
71 BIO *next = BIO_next(bio);
73 flags = BIO_test_flags(next, BIO_FLAGS_SHOULD_RETRY | BIO_FLAGS_RWS);
74 BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY | BIO_FLAGS_RWS);
75 BIO_set_flags(bio, flags);
78 #define RECORD_CONTENT_TYPE 0
79 #define RECORD_VERSION_HI 1
80 #define RECORD_VERSION_LO 2
81 #define RECORD_EPOCH_HI 3
82 #define RECORD_EPOCH_LO 4
83 #define RECORD_SEQUENCE_START 5
84 #define RECORD_SEQUENCE_END 10
85 #define RECORD_LEN_HI 11
86 #define RECORD_LEN_LO 12
94 #define MSG_FRAG_OFF_HI 6
95 #define MSG_FRAG_OFF_MID 7
96 #define MSG_FRAG_OFF_LO 8
97 #define MSG_FRAG_LEN_HI 9
98 #define MSG_FRAG_LEN_MID 10
99 #define MSG_FRAG_LEN_LO 11
102 static void dump_data(const char *data, int len)
104 int rem, i, content, reclen, msglen, fragoff, fraglen, epoch;
107 printf("---- START OF PACKET ----\n");
110 rec = (unsigned char *)data;
115 printf("*---- START OF RECORD ----\n");
116 if (rem < DTLS1_RT_HEADER_LENGTH) {
117 printf("*---- RECORD TRUNCATED ----\n");
120 content = rec[RECORD_CONTENT_TYPE];
121 printf("** Record Content-type: %d\n", content);
122 printf("** Record Version: %02x%02x\n",
123 rec[RECORD_VERSION_HI], rec[RECORD_VERSION_LO]);
124 epoch = (rec[RECORD_EPOCH_HI] << 8) | rec[RECORD_EPOCH_LO];
125 printf("** Record Epoch: %d\n", epoch);
126 printf("** Record Sequence: ");
127 for (i = RECORD_SEQUENCE_START; i <= RECORD_SEQUENCE_END; i++)
128 printf("%02x", rec[i]);
129 reclen = (rec[RECORD_LEN_HI] << 8) | rec[RECORD_LEN_LO];
130 printf("\n** Record Length: %d\n", reclen);
132 /* Now look at message */
133 rec += DTLS1_RT_HEADER_LENGTH;
134 rem -= DTLS1_RT_HEADER_LENGTH;
135 if (content == SSL3_RT_HANDSHAKE) {
136 printf("**---- START OF HANDSHAKE MESSAGE FRAGMENT ----\n");
138 printf("**---- HANDSHAKE MESSAGE FRAGMENT ENCRYPTED ----\n");
139 } else if (rem < DTLS1_HM_HEADER_LENGTH
140 || reclen < DTLS1_HM_HEADER_LENGTH) {
141 printf("**---- HANDSHAKE MESSAGE FRAGMENT TRUNCATED ----\n");
143 printf("*** Message Type: %d\n", rec[MSG_TYPE]);
144 msglen = (rec[MSG_LEN_HI] << 16) | (rec[MSG_LEN_MID] << 8)
146 printf("*** Message Length: %d\n", msglen);
147 printf("*** Message sequence: %d\n",
148 (rec[MSG_SEQ_HI] << 8) | rec[MSG_SEQ_LO]);
149 fragoff = (rec[MSG_FRAG_OFF_HI] << 16)
150 | (rec[MSG_FRAG_OFF_MID] << 8)
151 | rec[MSG_FRAG_OFF_LO];
152 printf("*** Message Fragment offset: %d\n", fragoff);
153 fraglen = (rec[MSG_FRAG_LEN_HI] << 16)
154 | (rec[MSG_FRAG_LEN_MID] << 8)
155 | rec[MSG_FRAG_LEN_LO];
156 printf("*** Message Fragment len: %d\n", fraglen);
157 if (fragoff + fraglen > msglen)
158 printf("***---- HANDSHAKE MESSAGE FRAGMENT INVALID ----\n");
159 else if(reclen < fraglen)
160 printf("**---- HANDSHAKE MESSAGE FRAGMENT TRUNCATED ----\n");
162 printf("**---- END OF HANDSHAKE MESSAGE FRAGMENT ----\n");
166 printf("*---- RECORD TRUNCATED ----\n");
171 printf("*---- END OF RECORD ----\n");
174 printf("---- END OF PACKET ----\n\n");
178 static int tls_dump_read(BIO *bio, char *out, int outl)
181 BIO *next = BIO_next(bio);
183 ret = BIO_read(next, out, outl);
193 static int tls_dump_write(BIO *bio, const char *in, int inl)
196 BIO *next = BIO_next(bio);
198 ret = BIO_write(next, in, inl);
204 static long tls_dump_ctrl(BIO *bio, int cmd, long num, void *ptr)
207 BIO *next = BIO_next(bio);
217 ret = BIO_ctrl(next, cmd, num, ptr);
223 static int tls_dump_gets(BIO *bio, char *buf, int size)
225 /* We don't support this - not needed anyway */
229 static int tls_dump_puts(BIO *bio, const char *str)
231 return tls_dump_write(bio, str, strlen(str));
235 struct mempacket_st {
242 static void mempacket_free(MEMPACKET *pkt)
244 if (pkt->data != NULL)
245 OPENSSL_free(pkt->data);
249 typedef struct mempacket_test_ctx_st {
250 STACK_OF(MEMPACKET) *pkts;
252 unsigned int currrec;
253 unsigned int currpkt;
254 unsigned int lastpkt;
255 unsigned int noinject;
256 } MEMPACKET_TEST_CTX;
258 static int mempacket_test_new(BIO *bi);
259 static int mempacket_test_free(BIO *a);
260 static int mempacket_test_read(BIO *b, char *out, int outl);
261 static int mempacket_test_write(BIO *b, const char *in, int inl);
262 static long mempacket_test_ctrl(BIO *b, int cmd, long num, void *ptr);
263 static int mempacket_test_gets(BIO *bp, char *buf, int size);
264 static int mempacket_test_puts(BIO *bp, const char *str);
266 const BIO_METHOD *bio_s_mempacket_test(void)
268 if (method_mempacket_test == NULL) {
269 method_mempacket_test = BIO_meth_new(BIO_TYPE_MEMPACKET_TEST,
271 if ( method_mempacket_test == NULL
272 || !BIO_meth_set_write(method_mempacket_test, mempacket_test_write)
273 || !BIO_meth_set_read(method_mempacket_test, mempacket_test_read)
274 || !BIO_meth_set_puts(method_mempacket_test, mempacket_test_puts)
275 || !BIO_meth_set_gets(method_mempacket_test, mempacket_test_gets)
276 || !BIO_meth_set_ctrl(method_mempacket_test, mempacket_test_ctrl)
277 || !BIO_meth_set_create(method_mempacket_test, mempacket_test_new)
278 || !BIO_meth_set_destroy(method_mempacket_test, mempacket_test_free))
281 return method_mempacket_test;
284 void bio_s_mempacket_test_free(void)
286 BIO_meth_free(method_mempacket_test);
289 static int mempacket_test_new(BIO *bio)
291 MEMPACKET_TEST_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
294 ctx->pkts = sk_MEMPACKET_new_null();
295 if (ctx->pkts == NULL) {
299 BIO_set_init(bio, 1);
300 BIO_set_data(bio, ctx);
304 static int mempacket_test_free(BIO *bio)
306 MEMPACKET_TEST_CTX *ctx = BIO_get_data(bio);
308 sk_MEMPACKET_pop_free(ctx->pkts, mempacket_free);
310 BIO_set_data(bio, NULL);
311 BIO_set_init(bio, 0);
316 /* Record Header values */
319 #define RECORD_SEQUENCE 10
320 #define RECORD_LEN_HI 11
321 #define RECORD_LEN_LO 12
323 #define STANDARD_PACKET 0
325 static int mempacket_test_read(BIO *bio, char *out, int outl)
327 MEMPACKET_TEST_CTX *ctx = BIO_get_data(bio);
331 unsigned int seq, offset, len, epoch;
333 BIO_clear_retry_flags(bio);
335 thispkt = sk_MEMPACKET_value(ctx->pkts, 0);
336 if (thispkt == NULL || thispkt->num != ctx->currpkt) {
337 /* Probably run out of data */
338 BIO_set_retry_read(bio);
341 (void)sk_MEMPACKET_shift(ctx->pkts);
344 if (outl > thispkt->len)
347 if (thispkt->type != INJECT_PACKET_IGNORE_REC_SEQ) {
349 * Overwrite the record sequence number. We strictly number them in
350 * the order received. Since we are actually a reliable transport
351 * we know that there won't be any re-ordering. We overwrite to deal
352 * with any packets that have been injected
357 if (rem < DTLS1_RT_HEADER_LENGTH) {
360 epoch = (rec[EPOCH_HI] << 8) | rec[EPOCH_LO];
361 if (epoch != ctx->epoch) {
368 rec[RECORD_SEQUENCE - offset] = seq & 0xFF;
374 len = ((rec[RECORD_LEN_HI] << 8) | rec[RECORD_LEN_LO])
375 + DTLS1_RT_HEADER_LENGTH;
382 memcpy(out, thispkt->data, outl);
384 mempacket_free(thispkt);
389 int mempacket_test_inject(BIO *bio, const char *in, int inl, int pktnum,
392 MEMPACKET_TEST_CTX *ctx = BIO_get_data(bio);
393 MEMPACKET *thispkt, *looppkt, *nextpkt;
399 /* We only allow injection before we've started writing any data */
407 thispkt = OPENSSL_malloc(sizeof(MEMPACKET));
411 thispkt->data = OPENSSL_malloc(inl);
412 if (thispkt->data == NULL) {
413 mempacket_free(thispkt);
417 memcpy(thispkt->data, in, inl);
419 thispkt->num = (pktnum >= 0) ? (unsigned int)pktnum : ctx->lastpkt;
420 thispkt->type = type;
422 for(i = 0; (looppkt = sk_MEMPACKET_value(ctx->pkts, i)) != NULL; i++) {
423 /* Check if we found the right place to insert this packet */
424 if (looppkt->num > thispkt->num) {
425 if (sk_MEMPACKET_insert(ctx->pkts, thispkt, i) == 0) {
426 mempacket_free(thispkt);
429 /* If we're doing up front injection then we're done */
433 * We need to do some accounting on lastpkt. We increment it first,
434 * but it might now equal the value of injected packets, so we need
440 nextpkt = sk_MEMPACKET_value(ctx->pkts, i);
441 if (nextpkt != NULL && nextpkt->num == ctx->lastpkt)
446 } else if(looppkt->num == thispkt->num) {
447 if (!ctx->noinject) {
448 /* We injected two packets with the same packet number! */
456 * We didn't find any packets with a packet number equal to or greater than
457 * this one, so we just add it onto the end
459 if (!sk_MEMPACKET_push(ctx->pkts, thispkt)) {
460 mempacket_free(thispkt);
470 static int mempacket_test_write(BIO *bio, const char *in, int inl)
472 return mempacket_test_inject(bio, in, inl, -1, STANDARD_PACKET);
475 static long mempacket_test_ctrl(BIO *bio, int cmd, long num, void *ptr)
478 MEMPACKET_TEST_CTX *ctx = BIO_get_data(bio);
483 ret = (long)(sk_MEMPACKET_num(ctx->pkts) == 0);
485 case BIO_CTRL_GET_CLOSE:
486 ret = BIO_get_shutdown(bio);
488 case BIO_CTRL_SET_CLOSE:
489 BIO_set_shutdown(bio, (int)num);
491 case BIO_CTRL_WPENDING:
494 case BIO_CTRL_PENDING:
495 thispkt = sk_MEMPACKET_value(ctx->pkts, 0);
515 static int mempacket_test_gets(BIO *bio, char *buf, int size)
517 /* We don't support this - not needed anyway */
521 static int mempacket_test_puts(BIO *bio, const char *str)
523 return mempacket_test_write(bio, str, strlen(str));
526 int create_ssl_ctx_pair(const SSL_METHOD *sm, const SSL_METHOD *cm,
527 SSL_CTX **sctx, SSL_CTX **cctx, char *certfile,
530 SSL_CTX *serverctx = NULL;
531 SSL_CTX *clientctx = NULL;
533 serverctx = SSL_CTX_new(sm);
534 clientctx = SSL_CTX_new(cm);
535 if (serverctx == NULL || clientctx == NULL) {
536 printf("Failed to create SSL_CTX\n");
540 if (SSL_CTX_use_certificate_file(serverctx, certfile,
541 SSL_FILETYPE_PEM) <= 0) {
542 printf("Failed to load server certificate\n");
545 if (SSL_CTX_use_PrivateKey_file(serverctx, privkeyfile,
546 SSL_FILETYPE_PEM) <= 0) {
547 printf("Failed to load server private key\n");
549 if (SSL_CTX_check_private_key(serverctx) <= 0) {
550 printf("Failed to check private key\n");
554 #ifndef OPENSSL_NO_DH
555 SSL_CTX_set_dh_auto(serverctx, 1);
563 SSL_CTX_free(serverctx);
564 SSL_CTX_free(clientctx);
568 #define MAXLOOPS 100000
571 * NOTE: Transfers control of the BIOs - this function will free them on error
573 int create_ssl_objects(SSL_CTX *serverctx, SSL_CTX *clientctx, SSL **sssl,
574 SSL **cssl, BIO *s_to_c_fbio, BIO *c_to_s_fbio)
576 SSL *serverssl, *clientssl;
577 BIO *s_to_c_bio = NULL, *c_to_s_bio = NULL;
580 serverssl = SSL_new(serverctx);
584 clientssl = SSL_new(clientctx);
588 if (serverssl == NULL || clientssl == NULL) {
589 printf("Failed to create SSL object\n");
593 if (SSL_is_dtls(clientssl)) {
594 s_to_c_bio = BIO_new(bio_s_mempacket_test());
595 c_to_s_bio = BIO_new(bio_s_mempacket_test());
597 s_to_c_bio = BIO_new(BIO_s_mem());
598 c_to_s_bio = BIO_new(BIO_s_mem());
600 if (s_to_c_bio == NULL || c_to_s_bio == NULL) {
601 printf("Failed to create mem BIOs\n");
605 if (s_to_c_fbio != NULL)
606 s_to_c_bio = BIO_push(s_to_c_fbio, s_to_c_bio);
607 if (c_to_s_fbio != NULL)
608 c_to_s_bio = BIO_push(c_to_s_fbio, c_to_s_bio);
609 if (s_to_c_bio == NULL || c_to_s_bio == NULL) {
610 printf("Failed to create chained BIOs\n");
614 /* Set Non-blocking IO behaviour */
615 BIO_set_mem_eof_return(s_to_c_bio, -1);
616 BIO_set_mem_eof_return(c_to_s_bio, -1);
618 /* Up ref these as we are passing them to two SSL objects */
619 BIO_up_ref(s_to_c_bio);
620 BIO_up_ref(c_to_s_bio);
622 SSL_set_bio(serverssl, c_to_s_bio, s_to_c_bio);
623 SSL_set_bio(clientssl, s_to_c_bio, c_to_s_bio);
625 /* BIOs will now be freed when SSL objects are freed */
626 s_to_c_bio = c_to_s_bio = NULL;
627 s_to_c_fbio = c_to_s_fbio = NULL;
637 BIO_free(s_to_c_bio);
638 BIO_free(c_to_s_bio);
639 BIO_free(s_to_c_fbio);
640 BIO_free(c_to_s_fbio);
645 int create_ssl_connection(SSL *serverssl, SSL *clientssl)
647 int retc = -1, rets = -1, err, abortctr = 0;
648 int clienterr = 0, servererr = 0;
651 err = SSL_ERROR_WANT_WRITE;
652 while (!clienterr && retc <= 0 && err == SSL_ERROR_WANT_WRITE) {
653 retc = SSL_connect(clientssl);
655 err = SSL_get_error(clientssl, retc);
658 if (!clienterr && retc <= 0 && err != SSL_ERROR_WANT_READ) {
659 printf("SSL_connect() failed %d, %d\n", retc, err);
663 err = SSL_ERROR_WANT_WRITE;
664 while (!servererr && rets <= 0 && err == SSL_ERROR_WANT_WRITE) {
665 rets = SSL_accept(serverssl);
667 err = SSL_get_error(serverssl, rets);
670 if (!servererr && rets <= 0 && err != SSL_ERROR_WANT_READ) {
671 printf("SSL_accept() failed %d, %d\n", retc, err);
674 if (clienterr && servererr)
676 if (++abortctr == MAXLOOPS) {
677 printf("No progress made\n");
680 } while (retc <=0 || rets <= 0);