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"
16 static int tls_dump_new(BIO *bi);
17 static int tls_dump_free(BIO *a);
18 static int tls_dump_read(BIO *b, char *out, int outl);
19 static int tls_dump_write(BIO *b, const char *in, int inl);
20 static long tls_dump_ctrl(BIO *b, int cmd, long num, void *ptr);
21 static int tls_dump_gets(BIO *bp, char *buf, int size);
22 static int tls_dump_puts(BIO *bp, const char *str);
24 /* Choose a sufficiently large type likely to be unused for this custom BIO */
25 #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 *meth_mem = 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 (meth_mem == NULL) {
269 if (!TEST_ptr(meth_mem = BIO_meth_new(BIO_TYPE_MEMPACKET_TEST,
271 || !TEST_true(BIO_meth_set_write(meth_mem, mempacket_test_write))
272 || !TEST_true(BIO_meth_set_read(meth_mem, mempacket_test_read))
273 || !TEST_true(BIO_meth_set_puts(meth_mem, mempacket_test_puts))
274 || !TEST_true(BIO_meth_set_gets(meth_mem, mempacket_test_gets))
275 || !TEST_true(BIO_meth_set_ctrl(meth_mem, mempacket_test_ctrl))
276 || !TEST_true(BIO_meth_set_create(meth_mem, mempacket_test_new))
277 || !TEST_true(BIO_meth_set_destroy(meth_mem, mempacket_test_free)))
283 void bio_s_mempacket_test_free(void)
285 BIO_meth_free(meth_mem);
288 static int mempacket_test_new(BIO *bio)
290 MEMPACKET_TEST_CTX *ctx;
292 if (!TEST_ptr(ctx = OPENSSL_zalloc(sizeof(*ctx))))
294 if (!TEST_ptr(ctx->pkts = sk_MEMPACKET_new_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);
314 /* Record Header values */
317 #define RECORD_SEQUENCE 10
318 #define RECORD_LEN_HI 11
319 #define RECORD_LEN_LO 12
321 #define STANDARD_PACKET 0
323 static int mempacket_test_read(BIO *bio, char *out, int outl)
325 MEMPACKET_TEST_CTX *ctx = BIO_get_data(bio);
329 unsigned int seq, offset, len, epoch;
331 BIO_clear_retry_flags(bio);
332 thispkt = sk_MEMPACKET_value(ctx->pkts, 0);
333 if (thispkt == NULL || thispkt->num != ctx->currpkt) {
334 /* Probably run out of data */
335 BIO_set_retry_read(bio);
338 (void)sk_MEMPACKET_shift(ctx->pkts);
341 if (outl > thispkt->len)
344 if (thispkt->type != INJECT_PACKET_IGNORE_REC_SEQ) {
346 * Overwrite the record sequence number. We strictly number them in
347 * the order received. Since we are actually a reliable transport
348 * we know that there won't be any re-ordering. We overwrite to deal
349 * with any packets that have been injected
351 for (rem = thispkt->len, rec = thispkt->data
352 ; rem > 0; rec += len, rem -= len) {
353 if (rem < DTLS1_RT_HEADER_LENGTH)
355 epoch = (rec[EPOCH_HI] << 8) | rec[EPOCH_LO];
356 if (epoch != ctx->epoch) {
363 rec[RECORD_SEQUENCE - offset] = seq & 0xFF;
369 len = ((rec[RECORD_LEN_HI] << 8) | rec[RECORD_LEN_LO])
370 + DTLS1_RT_HEADER_LENGTH;
374 memcpy(out, thispkt->data, outl);
375 mempacket_free(thispkt);
379 int mempacket_test_inject(BIO *bio, const char *in, int inl, int pktnum,
382 MEMPACKET_TEST_CTX *ctx = BIO_get_data(bio);
383 MEMPACKET *thispkt, *looppkt, *nextpkt;
389 /* We only allow injection before we've started writing any data */
397 if (!TEST_ptr(thispkt = OPENSSL_malloc(sizeof(*thispkt))))
399 if (!TEST_ptr(thispkt->data = OPENSSL_malloc(inl))) {
400 mempacket_free(thispkt);
404 memcpy(thispkt->data, in, inl);
406 thispkt->num = (pktnum >= 0) ? (unsigned int)pktnum : ctx->lastpkt;
407 thispkt->type = type;
409 for(i = 0; (looppkt = sk_MEMPACKET_value(ctx->pkts, i)) != NULL; i++) {
410 /* Check if we found the right place to insert this packet */
411 if (looppkt->num > thispkt->num) {
412 if (sk_MEMPACKET_insert(ctx->pkts, thispkt, i) == 0) {
413 mempacket_free(thispkt);
416 /* If we're doing up front injection then we're done */
420 * We need to do some accounting on lastpkt. We increment it first,
421 * but it might now equal the value of injected packets, so we need
427 nextpkt = sk_MEMPACKET_value(ctx->pkts, i);
428 if (nextpkt != NULL && nextpkt->num == ctx->lastpkt)
433 } else if (looppkt->num == thispkt->num) {
434 if (!ctx->noinject) {
435 /* We injected two packets with the same packet number! */
443 * We didn't find any packets with a packet number equal to or greater than
444 * this one, so we just add it onto the end
446 if (!sk_MEMPACKET_push(ctx->pkts, thispkt)) {
447 mempacket_free(thispkt);
457 static int mempacket_test_write(BIO *bio, const char *in, int inl)
459 return mempacket_test_inject(bio, in, inl, -1, STANDARD_PACKET);
462 static long mempacket_test_ctrl(BIO *bio, int cmd, long num, void *ptr)
465 MEMPACKET_TEST_CTX *ctx = BIO_get_data(bio);
470 ret = (long)(sk_MEMPACKET_num(ctx->pkts) == 0);
472 case BIO_CTRL_GET_CLOSE:
473 ret = BIO_get_shutdown(bio);
475 case BIO_CTRL_SET_CLOSE:
476 BIO_set_shutdown(bio, (int)num);
478 case BIO_CTRL_WPENDING:
481 case BIO_CTRL_PENDING:
482 thispkt = sk_MEMPACKET_value(ctx->pkts, 0);
502 static int mempacket_test_gets(BIO *bio, char *buf, int size)
504 /* We don't support this - not needed anyway */
508 static int mempacket_test_puts(BIO *bio, const char *str)
510 return mempacket_test_write(bio, str, strlen(str));
513 int create_ssl_ctx_pair(const SSL_METHOD *sm, const SSL_METHOD *cm,
514 SSL_CTX **sctx, SSL_CTX **cctx, char *certfile,
517 SSL_CTX *serverctx = NULL;
518 SSL_CTX *clientctx = NULL;
520 if (!TEST_ptr(serverctx = SSL_CTX_new(sm))
521 || !TEST_ptr(clientctx = SSL_CTX_new(cm)))
524 if (!TEST_int_eq(SSL_CTX_use_certificate_file(serverctx, certfile,
525 SSL_FILETYPE_PEM), 1)
526 || !TEST_int_eq(SSL_CTX_use_PrivateKey_file(serverctx, privkeyfile,
527 SSL_FILETYPE_PEM), 1)
528 || !TEST_int_eq(SSL_CTX_check_private_key(serverctx), 1))
531 #ifndef OPENSSL_NO_DH
532 SSL_CTX_set_dh_auto(serverctx, 1);
540 SSL_CTX_free(serverctx);
541 SSL_CTX_free(clientctx);
545 #define MAXLOOPS 1000000
548 * NOTE: Transfers control of the BIOs - this function will free them on error
550 int create_ssl_objects(SSL_CTX *serverctx, SSL_CTX *clientctx, SSL **sssl,
551 SSL **cssl, BIO *s_to_c_fbio, BIO *c_to_s_fbio)
553 SSL *serverssl = NULL, *clientssl = NULL;
554 BIO *s_to_c_bio = NULL, *c_to_s_bio = NULL;
558 else if (!TEST_ptr(serverssl = SSL_new(serverctx)))
562 else if (!TEST_ptr(clientssl = SSL_new(clientctx)))
565 if (SSL_is_dtls(clientssl)) {
566 if (!TEST_ptr(s_to_c_bio = BIO_new(bio_s_mempacket_test()))
567 || !TEST_ptr(c_to_s_bio = BIO_new(bio_s_mempacket_test())))
570 if (!TEST_ptr(s_to_c_bio = BIO_new(BIO_s_mem()))
571 || !TEST_ptr(c_to_s_bio = BIO_new(BIO_s_mem())))
575 if (s_to_c_fbio != NULL
576 && !TEST_ptr(s_to_c_bio = BIO_push(s_to_c_fbio, s_to_c_bio)))
578 if (c_to_s_fbio != NULL
579 && !TEST_ptr(c_to_s_bio = BIO_push(c_to_s_fbio, c_to_s_bio)))
582 /* Set Non-blocking IO behaviour */
583 BIO_set_mem_eof_return(s_to_c_bio, -1);
584 BIO_set_mem_eof_return(c_to_s_bio, -1);
586 /* Up ref these as we are passing them to two SSL objects */
587 SSL_set_bio(serverssl, c_to_s_bio, s_to_c_bio);
588 BIO_up_ref(s_to_c_bio);
589 BIO_up_ref(c_to_s_bio);
590 SSL_set_bio(clientssl, s_to_c_bio, c_to_s_bio);
598 BIO_free(s_to_c_bio);
599 BIO_free(c_to_s_bio);
600 BIO_free(s_to_c_fbio);
601 BIO_free(c_to_s_fbio);
606 int create_ssl_connection(SSL *serverssl, SSL *clientssl, int want)
608 int retc = -1, rets = -1, err, abortctr = 0;
609 int clienterr = 0, servererr = 0;
614 err = SSL_ERROR_WANT_WRITE;
615 while (!clienterr && retc <= 0 && err == SSL_ERROR_WANT_WRITE) {
616 retc = SSL_connect(clientssl);
618 err = SSL_get_error(clientssl, retc);
621 if (!clienterr && retc <= 0 && err != SSL_ERROR_WANT_READ) {
622 TEST_info("SSL_connect() failed %d, %d", retc, err);
625 if (want != SSL_ERROR_NONE && err == want)
628 err = SSL_ERROR_WANT_WRITE;
629 while (!servererr && rets <= 0 && err == SSL_ERROR_WANT_WRITE) {
630 rets = SSL_accept(serverssl);
632 err = SSL_get_error(serverssl, rets);
635 if (!servererr && rets <= 0 && err != SSL_ERROR_WANT_READ) {
636 TEST_info("SSL_accept() failed %d, %d", rets, err);
639 if (want != SSL_ERROR_NONE && err == want)
641 if (clienterr && servererr)
643 if (++abortctr == MAXLOOPS) {
644 TEST_info("No progress made");
647 } while (retc <=0 || rets <= 0);
650 * We attempt to read some data on the client side which we expect to fail.
651 * This will ensure we have received the NewSessionTicket in TLSv1.3 where
654 if (SSL_read_ex(clientssl, &buf, sizeof(buf), &readbytes) > 0) {
655 if (!TEST_ulong_eq(readbytes, 0))
657 } else if (!TEST_int_eq(SSL_get_error(clientssl, 0), SSL_ERROR_WANT_READ)) {