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
12 #include "ssltestlib.h"
14 static int tls_dump_new(BIO *bi);
15 static int tls_dump_free(BIO *a);
16 static int tls_dump_read(BIO *b, char *out, int outl);
17 static int tls_dump_write(BIO *b, const char *in, int inl);
18 static long tls_dump_ctrl(BIO *b, int cmd, long num, void *ptr);
19 static int tls_dump_gets(BIO *bp, char *buf, int size);
20 static int tls_dump_puts(BIO *bp, const char *str);
22 /* Choose a sufficiently large type likely to be unused for this custom BIO */
23 # define BIO_TYPE_TLS_DUMP_FILTER (0x80 | BIO_TYPE_FILTER)
25 # define BIO_TYPE_MEMPACKET_TEST 0x81
27 static BIO_METHOD *method_tls_dump = NULL;
28 static BIO_METHOD *method_mempacket_test = NULL;
30 /* Note: Not thread safe! */
31 const BIO_METHOD *bio_f_tls_dump_filter(void)
33 if (method_tls_dump == NULL) {
34 method_tls_dump = BIO_meth_new(BIO_TYPE_TLS_DUMP_FILTER,
36 if ( method_tls_dump == NULL
37 || !BIO_meth_set_write(method_tls_dump, tls_dump_write)
38 || !BIO_meth_set_read(method_tls_dump, tls_dump_read)
39 || !BIO_meth_set_puts(method_tls_dump, tls_dump_puts)
40 || !BIO_meth_set_gets(method_tls_dump, tls_dump_gets)
41 || !BIO_meth_set_ctrl(method_tls_dump, tls_dump_ctrl)
42 || !BIO_meth_set_create(method_tls_dump, tls_dump_new)
43 || !BIO_meth_set_destroy(method_tls_dump, tls_dump_free))
46 return method_tls_dump;
49 void bio_f_tls_dump_filter_free(void)
51 BIO_meth_free(method_tls_dump);
54 static int tls_dump_new(BIO *bio)
60 static int tls_dump_free(BIO *bio)
67 static void copy_flags(BIO *bio)
70 BIO *next = BIO_next(bio);
72 flags = BIO_test_flags(next, BIO_FLAGS_SHOULD_RETRY | BIO_FLAGS_RWS);
73 BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY | BIO_FLAGS_RWS);
74 BIO_set_flags(bio, flags);
77 #define RECORD_CONTENT_TYPE 0
78 #define RECORD_VERSION_HI 1
79 #define RECORD_VERSION_LO 2
80 #define RECORD_EPOCH_HI 3
81 #define RECORD_EPOCH_LO 4
82 #define RECORD_SEQUENCE_START 5
83 #define RECORD_SEQUENCE_END 10
84 #define RECORD_LEN_HI 11
85 #define RECORD_LEN_LO 12
93 #define MSG_FRAG_OFF_HI 6
94 #define MSG_FRAG_OFF_MID 7
95 #define MSG_FRAG_OFF_LO 8
96 #define MSG_FRAG_LEN_HI 9
97 #define MSG_FRAG_LEN_MID 10
98 #define MSG_FRAG_LEN_LO 11
101 static void dump_data(const char *data, int len)
103 int rem, i, content, reclen, msglen, fragoff, fraglen, epoch;
106 printf("---- START OF PACKET ----\n");
109 rec = (unsigned char *)data;
114 printf("*---- START OF RECORD ----\n");
115 if (rem < DTLS1_RT_HEADER_LENGTH) {
116 printf("*---- RECORD TRUNCATED ----\n");
119 content = rec[RECORD_CONTENT_TYPE];
120 printf("** Record Content-type: %d\n", content);
121 printf("** Record Version: %02x%02x\n",
122 rec[RECORD_VERSION_HI], rec[RECORD_VERSION_LO]);
123 epoch = (rec[RECORD_EPOCH_HI] << 8) | rec[RECORD_EPOCH_LO];
124 printf("** Record Epoch: %d\n", epoch);
125 printf("** Record Sequence: ");
126 for (i = RECORD_SEQUENCE_START; i <= RECORD_SEQUENCE_END; i++)
127 printf("%02x", rec[i]);
128 reclen = (rec[RECORD_LEN_HI] << 8) | rec[RECORD_LEN_LO];
129 printf("\n** Record Length: %d\n", reclen);
131 /* Now look at message */
132 rec += DTLS1_RT_HEADER_LENGTH;
133 rem -= DTLS1_RT_HEADER_LENGTH;
134 if (content == SSL3_RT_HANDSHAKE) {
135 printf("**---- START OF HANDSHAKE MESSAGE FRAGMENT ----\n");
137 printf("**---- HANDSHAKE MESSAGE FRAGMENT ENCRYPTED ----\n");
138 } else if (rem < DTLS1_HM_HEADER_LENGTH
139 || reclen < DTLS1_HM_HEADER_LENGTH) {
140 printf("**---- HANDSHAKE MESSAGE FRAGMENT TRUNCATED ----\n");
142 printf("*** Message Type: %d\n", rec[MSG_TYPE]);
143 msglen = (rec[MSG_LEN_HI] << 16) | (rec[MSG_LEN_MID] << 8)
145 printf("*** Message Length: %d\n", msglen);
146 printf("*** Message sequence: %d\n",
147 (rec[MSG_SEQ_HI] << 8) | rec[MSG_SEQ_LO]);
148 fragoff = (rec[MSG_FRAG_OFF_HI] << 16)
149 | (rec[MSG_FRAG_OFF_MID] << 8)
150 | rec[MSG_FRAG_OFF_LO];
151 printf("*** Message Fragment offset: %d\n", fragoff);
152 fraglen = (rec[MSG_FRAG_LEN_HI] << 16)
153 | (rec[MSG_FRAG_LEN_MID] << 8)
154 | rec[MSG_FRAG_LEN_LO];
155 printf("*** Message Fragment len: %d\n", fraglen);
156 if (fragoff + fraglen > msglen)
157 printf("***---- HANDSHAKE MESSAGE FRAGMENT INVALID ----\n");
158 else if(reclen < fraglen)
159 printf("**---- HANDSHAKE MESSAGE FRAGMENT TRUNCATED ----\n");
161 printf("**---- END OF HANDSHAKE MESSAGE FRAGMENT ----\n");
165 printf("*---- RECORD TRUNCATED ----\n");
170 printf("*---- END OF RECORD ----\n");
173 printf("---- END OF PACKET ----\n\n");
177 static int tls_dump_read(BIO *bio, char *out, int outl)
180 BIO *next = BIO_next(bio);
182 ret = BIO_read(next, out, outl);
192 static int tls_dump_write(BIO *bio, const char *in, int inl)
195 BIO *next = BIO_next(bio);
197 ret = BIO_write(next, in, inl);
203 static long tls_dump_ctrl(BIO *bio, int cmd, long num, void *ptr)
206 BIO *next = BIO_next(bio);
216 ret = BIO_ctrl(next, cmd, num, ptr);
222 static int tls_dump_gets(BIO *bio, char *buf, int size)
224 /* We don't support this - not needed anyway */
228 static int tls_dump_puts(BIO *bio, const char *str)
230 return tls_dump_write(bio, str, strlen(str));
234 struct mempacket_st {
241 static void mempacket_free(MEMPACKET *pkt)
243 if (pkt->data != NULL)
244 OPENSSL_free(pkt->data);
248 typedef struct mempacket_test_ctx_st {
249 STACK_OF(MEMPACKET) *pkts;
251 unsigned int currrec;
252 unsigned int currpkt;
253 unsigned int lastpkt;
254 unsigned int noinject;
255 } MEMPACKET_TEST_CTX;
257 static int mempacket_test_new(BIO *bi);
258 static int mempacket_test_free(BIO *a);
259 static int mempacket_test_read(BIO *b, char *out, int outl);
260 static int mempacket_test_write(BIO *b, const char *in, int inl);
261 static long mempacket_test_ctrl(BIO *b, int cmd, long num, void *ptr);
262 static int mempacket_test_gets(BIO *bp, char *buf, int size);
263 static int mempacket_test_puts(BIO *bp, const char *str);
265 const BIO_METHOD *bio_s_mempacket_test(void)
267 if (method_mempacket_test == NULL) {
268 method_mempacket_test = BIO_meth_new(BIO_TYPE_MEMPACKET_TEST,
270 if ( method_mempacket_test == NULL
271 || !BIO_meth_set_write(method_mempacket_test, mempacket_test_write)
272 || !BIO_meth_set_read(method_mempacket_test, mempacket_test_read)
273 || !BIO_meth_set_puts(method_mempacket_test, mempacket_test_puts)
274 || !BIO_meth_set_gets(method_mempacket_test, mempacket_test_gets)
275 || !BIO_meth_set_ctrl(method_mempacket_test, mempacket_test_ctrl)
276 || !BIO_meth_set_create(method_mempacket_test, mempacket_test_new)
277 || !BIO_meth_set_destroy(method_mempacket_test, mempacket_test_free))
280 return method_mempacket_test;
283 void bio_s_mempacket_test_free(void)
285 BIO_meth_free(method_mempacket_test);
288 static int mempacket_test_new(BIO *bio)
290 MEMPACKET_TEST_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
293 ctx->pkts = sk_MEMPACKET_new_null();
294 if (ctx->pkts == NULL) {
298 BIO_set_init(bio, 1);
299 BIO_set_data(bio, ctx);
303 static int mempacket_test_free(BIO *bio)
305 MEMPACKET_TEST_CTX *ctx = BIO_get_data(bio);
307 sk_MEMPACKET_pop_free(ctx->pkts, mempacket_free);
309 BIO_set_data(bio, NULL);
310 BIO_set_init(bio, 0);
315 /* Record Header values */
318 #define RECORD_SEQUENCE 10
319 #define RECORD_LEN_HI 11
320 #define RECORD_LEN_LO 12
322 #define STANDARD_PACKET 0
324 static int mempacket_test_read(BIO *bio, char *out, int outl)
326 MEMPACKET_TEST_CTX *ctx = BIO_get_data(bio);
330 unsigned int seq, offset, len, epoch;
332 BIO_clear_retry_flags(bio);
334 thispkt = sk_MEMPACKET_value(ctx->pkts, 0);
335 if (thispkt == NULL || thispkt->num != ctx->currpkt) {
336 /* Probably run out of data */
337 BIO_set_retry_read(bio);
340 (void)sk_MEMPACKET_shift(ctx->pkts);
343 if (outl > thispkt->len)
346 if (thispkt->type != INJECT_PACKET_IGNORE_REC_SEQ) {
348 * Overwrite the record sequence number. We strictly number them in
349 * the order received. Since we are actually a reliable transport
350 * we know that there won't be any re-ordering. We overwrite to deal
351 * with any packets that have been injected
356 if (rem < DTLS1_RT_HEADER_LENGTH) {
359 epoch = (rec[EPOCH_HI] << 8) | rec[EPOCH_LO];
360 if (epoch != ctx->epoch) {
367 rec[RECORD_SEQUENCE - offset] = seq & 0xFF;
373 len = ((rec[RECORD_LEN_HI] << 8) | rec[RECORD_LEN_LO])
374 + DTLS1_RT_HEADER_LENGTH;
381 memcpy(out, thispkt->data, outl);
383 mempacket_free(thispkt);
388 int mempacket_test_inject(BIO *bio, const char *in, int inl, int pktnum,
391 MEMPACKET_TEST_CTX *ctx = BIO_get_data(bio);
392 MEMPACKET *thispkt, *looppkt, *nextpkt;
398 /* We only allow injection before we've started writing any data */
406 thispkt = OPENSSL_malloc(sizeof(MEMPACKET));
410 thispkt->data = OPENSSL_malloc(inl);
411 if (thispkt->data == NULL) {
412 mempacket_free(thispkt);
416 memcpy(thispkt->data, in, inl);
418 thispkt->num = (pktnum >= 0) ? (unsigned int)pktnum : ctx->lastpkt;
419 thispkt->type = type;
421 for(i = 0; (looppkt = sk_MEMPACKET_value(ctx->pkts, i)) != NULL; i++) {
422 /* Check if we found the right place to insert this packet */
423 if (looppkt->num > thispkt->num) {
424 if (sk_MEMPACKET_insert(ctx->pkts, thispkt, i) == 0) {
425 mempacket_free(thispkt);
428 /* If we're doing up front injection then we're done */
432 * We need to do some accounting on lastpkt. We increment it first,
433 * but it might now equal the value of injected packets, so we need
439 nextpkt = sk_MEMPACKET_value(ctx->pkts, i);
440 if (nextpkt != NULL && nextpkt->num == ctx->lastpkt)
445 } else if(looppkt->num == thispkt->num) {
446 if (!ctx->noinject) {
447 /* We injected two packets with the same packet number! */
455 * We didn't find any packets with a packet number equal to or greater than
456 * this one, so we just add it onto the end
458 if (!sk_MEMPACKET_push(ctx->pkts, thispkt)) {
459 mempacket_free(thispkt);
469 static int mempacket_test_write(BIO *bio, const char *in, int inl)
471 return mempacket_test_inject(bio, in, inl, -1, STANDARD_PACKET);
474 static long mempacket_test_ctrl(BIO *bio, int cmd, long num, void *ptr)
477 MEMPACKET_TEST_CTX *ctx = BIO_get_data(bio);
482 ret = (long)(sk_MEMPACKET_num(ctx->pkts) == 0);
484 case BIO_CTRL_GET_CLOSE:
485 ret = BIO_get_shutdown(bio);
487 case BIO_CTRL_SET_CLOSE:
488 BIO_set_shutdown(bio, (int)num);
490 case BIO_CTRL_WPENDING:
493 case BIO_CTRL_PENDING:
494 thispkt = sk_MEMPACKET_value(ctx->pkts, 0);
514 static int mempacket_test_gets(BIO *bio, char *buf, int size)
516 /* We don't support this - not needed anyway */
520 static int mempacket_test_puts(BIO *bio, const char *str)
522 return mempacket_test_write(bio, str, strlen(str));
525 int create_ssl_ctx_pair(const SSL_METHOD *sm, const SSL_METHOD *cm,
526 SSL_CTX **sctx, SSL_CTX **cctx, char *certfile,
529 SSL_CTX *serverctx = NULL;
530 SSL_CTX *clientctx = NULL;
532 serverctx = SSL_CTX_new(sm);
533 clientctx = SSL_CTX_new(cm);
534 if (serverctx == NULL || clientctx == NULL) {
535 printf("Failed to create SSL_CTX\n");
539 if (SSL_CTX_use_certificate_file(serverctx, certfile,
540 SSL_FILETYPE_PEM) <= 0) {
541 printf("Failed to load server certificate\n");
544 if (SSL_CTX_use_PrivateKey_file(serverctx, privkeyfile,
545 SSL_FILETYPE_PEM) <= 0) {
546 printf("Failed to load server private key\n");
548 if (SSL_CTX_check_private_key(serverctx) <= 0) {
549 printf("Failed to check private key\n");
553 #ifndef OPENSSL_NO_DH
554 SSL_CTX_set_dh_auto(serverctx, 1);
562 SSL_CTX_free(serverctx);
563 SSL_CTX_free(clientctx);
567 #define MAXLOOPS 100000
570 * NOTE: Transfers control of the BIOs - this function will free them on error
572 int create_ssl_objects(SSL_CTX *serverctx, SSL_CTX *clientctx, SSL **sssl,
573 SSL **cssl, BIO *s_to_c_fbio, BIO *c_to_s_fbio)
575 SSL *serverssl, *clientssl;
576 BIO *s_to_c_bio = NULL, *c_to_s_bio = NULL;
579 serverssl = SSL_new(serverctx);
583 clientssl = SSL_new(clientctx);
587 if (serverssl == NULL || clientssl == NULL) {
588 printf("Failed to create SSL object\n");
592 if (SSL_is_dtls(clientssl)) {
593 s_to_c_bio = BIO_new(bio_s_mempacket_test());
594 c_to_s_bio = BIO_new(bio_s_mempacket_test());
596 s_to_c_bio = BIO_new(BIO_s_mem());
597 c_to_s_bio = BIO_new(BIO_s_mem());
599 if (s_to_c_bio == NULL || c_to_s_bio == NULL) {
600 printf("Failed to create mem BIOs\n");
604 if (s_to_c_fbio != NULL)
605 s_to_c_bio = BIO_push(s_to_c_fbio, s_to_c_bio);
606 if (c_to_s_fbio != NULL)
607 c_to_s_bio = BIO_push(c_to_s_fbio, c_to_s_bio);
608 if (s_to_c_bio == NULL || c_to_s_bio == NULL) {
609 printf("Failed to create chained BIOs\n");
613 /* Set Non-blocking IO behaviour */
614 BIO_set_mem_eof_return(s_to_c_bio, -1);
615 BIO_set_mem_eof_return(c_to_s_bio, -1);
617 /* Up ref these as we are passing them to two SSL objects */
618 BIO_up_ref(s_to_c_bio);
619 BIO_up_ref(c_to_s_bio);
621 SSL_set_bio(serverssl, c_to_s_bio, s_to_c_bio);
622 SSL_set_bio(clientssl, s_to_c_bio, c_to_s_bio);
624 /* BIOs will now be freed when SSL objects are freed */
625 s_to_c_bio = c_to_s_bio = NULL;
626 s_to_c_fbio = c_to_s_fbio = NULL;
636 BIO_free(s_to_c_bio);
637 BIO_free(c_to_s_bio);
638 BIO_free(s_to_c_fbio);
639 BIO_free(c_to_s_fbio);
644 int create_ssl_connection(SSL *serverssl, SSL *clientssl)
646 int retc = -1, rets = -1, err, abortctr = 0;
647 int clienterr = 0, servererr = 0;
650 err = SSL_ERROR_WANT_WRITE;
651 while (!clienterr && retc <= 0 && err == SSL_ERROR_WANT_WRITE) {
652 retc = SSL_connect(clientssl);
654 err = SSL_get_error(clientssl, retc);
657 if (!clienterr && retc <= 0 && err != SSL_ERROR_WANT_READ) {
658 printf("SSL_connect() failed %d, %d\n", retc, err);
662 err = SSL_ERROR_WANT_WRITE;
663 while (!servererr && rets <= 0 && err == SSL_ERROR_WANT_WRITE) {
664 rets = SSL_accept(serverssl);
666 err = SSL_get_error(serverssl, rets);
669 if (!servererr && rets <= 0 && err != SSL_ERROR_WANT_READ) {
670 printf("SSL_accept() failed %d, %d\n", retc, err);
673 if (clienterr && servererr)
675 if (++abortctr == MAXLOOPS) {
676 printf("No progress made\n");
679 } while (retc <=0 || rets <= 0);