2 * Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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
11 #include <openssl/configuration.h>
12 #include <openssl/bio.h>
13 #include "quictestlib.h"
14 #include "ssltestlib.h"
15 #include "../testutil.h"
16 #if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG)
17 # include "../threadstest.h"
19 #include "internal/quic_wire_pkt.h"
20 #include "internal/quic_record_tx.h"
21 #include "internal/quic_error.h"
22 #include "internal/packet.h"
24 #define GROWTH_ALLOWANCE 1024
29 /* Plain packet mutations */
30 /* Header for the plaintext packet */
31 QUIC_PKT_HDR pplainhdr
;
32 /* iovec for the plaintext packet data buffer */
33 OSSL_QTX_IOVEC pplainio
;
34 /* Allocted size of the plaintext packet data buffer */
35 size_t pplainbuf_alloc
;
36 qtest_fault_on_packet_plain_cb pplaincb
;
39 /* Handshake message mutations */
40 /* Handshake message buffer */
41 unsigned char *handbuf
;
42 /* Allocated size of the handshake message buffer */
44 /* Actual length of the handshake message */
46 qtest_fault_on_handshake_cb handshakecb
;
48 qtest_fault_on_enc_ext_cb encextcb
;
51 /* Cipher packet mutations */
52 qtest_fault_on_packet_cipher_cb pciphercb
;
55 /* Datagram mutations */
56 qtest_fault_on_datagram_cb datagramcb
;
58 /* The currently processed message */
60 /* Allocated size of msg data buffer */
64 static void packet_plain_finish(void *arg
);
65 static void handshake_finish(void *arg
);
67 static BIO_METHOD
*get_bio_method(void);
69 int qtest_create_quic_objects(OSSL_LIB_CTX
*libctx
, SSL_CTX
*clientctx
,
70 char *certfile
, char *keyfile
,
71 int block
, QUIC_TSERVER
**qtserv
, SSL
**cssl
,
74 /* ALPN value as recognised by QUIC_TSERVER */
75 unsigned char alpn
[] = { 8, 'o', 's', 's', 'l', 't', 'e', 's', 't' };
76 QUIC_TSERVER_ARGS tserver_args
= {0};
77 BIO
*cbio
= NULL
, *sbio
= NULL
, *fisbio
= NULL
;
78 BIO_ADDR
*peeraddr
= NULL
;
79 struct in_addr ina
= {0};
84 *cssl
= SSL_new(clientctx
);
88 /* SSL_set_alpn_protos returns 0 for success! */
89 if (!TEST_false(SSL_set_alpn_protos(*cssl
, alpn
, sizeof(alpn
))))
92 if (!TEST_ptr(peeraddr
= BIO_ADDR_new()))
96 #if !defined(OPENSSL_NO_POSIX_IO)
100 * For blocking mode we need to create actual sockets rather than doing
101 * everything in memory
103 if (!TEST_true(create_test_sockets(&cfd
, &sfd
, SOCK_DGRAM
, peeraddr
)))
105 cbio
= BIO_new_dgram(cfd
, 1);
106 if (!TEST_ptr(cbio
)) {
111 sbio
= BIO_new_dgram(sfd
, 1);
112 if (!TEST_ptr(sbio
)) {
120 if (!TEST_true(BIO_new_bio_dgram_pair(&cbio
, 0, &sbio
, 0)))
123 if (!TEST_true(BIO_dgram_set_caps(cbio
, BIO_DGRAM_CAP_HANDLES_DST_ADDR
))
124 || !TEST_true(BIO_dgram_set_caps(sbio
, BIO_DGRAM_CAP_HANDLES_DST_ADDR
)))
127 /* Dummy server address */
128 if (!TEST_true(BIO_ADDR_rawmake(peeraddr
, AF_INET
, &ina
, sizeof(ina
),
133 SSL_set_bio(*cssl
, cbio
, cbio
);
135 if (!TEST_true(SSL_set_blocking_mode(*cssl
, block
)))
138 if (!TEST_true(SSL_set_initial_peer_addr(*cssl
, peeraddr
)))
142 *fault
= OPENSSL_zalloc(sizeof(**fault
));
147 fisbio
= BIO_new(get_bio_method());
148 if (!TEST_ptr(fisbio
))
151 BIO_set_data(fisbio
, fault
== NULL
? NULL
: *fault
);
153 if (!TEST_ptr(BIO_push(fisbio
, sbio
)))
156 tserver_args
.libctx
= libctx
;
157 tserver_args
.net_rbio
= sbio
;
158 tserver_args
.net_wbio
= fisbio
;
160 if (!TEST_ptr(*qtserv
= ossl_quic_tserver_new(&tserver_args
, certfile
,
164 /* Ownership of fisbio and sbio is now held by *qtserv */
169 (*fault
)->qtserv
= *qtserv
;
171 BIO_ADDR_free(peeraddr
);
175 BIO_ADDR_free(peeraddr
);
181 ossl_quic_tserver_free(*qtserv
);
183 OPENSSL_free(*fault
);
188 int qtest_supports_blocking(void)
190 #if !defined(OPENSSL_NO_POSIX_IO) && defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG)
197 #define MAXLOOPS 1000
199 #if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG)
200 static int globserverret
= 0;
201 static QUIC_TSERVER
*globtserv
;
202 static const thread_t thread_zero
;
204 static void run_server_thread(void)
207 * This will operate in a busy loop because the server does not block,
208 * but should be acceptable because it is local and we expect this to be
211 globserverret
= qtest_create_quic_connection(globtserv
, NULL
);
215 int qtest_create_quic_connection(QUIC_TSERVER
*qtserv
, SSL
*clientssl
)
217 int retc
= -1, rets
= 0, err
, abortctr
= 0, ret
= 0;
218 int clienterr
= 0, servererr
= 0;
219 #if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG)
221 * Pointless initialisation to avoid bogus compiler warnings about using
224 thread_t t
= thread_zero
;
227 if (!TEST_ptr(qtserv
)) {
229 } else if (clientssl
== NULL
) {
231 } else if (SSL_get_blocking_mode(clientssl
) > 0) {
232 #if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG)
234 * clientssl is blocking. We will need a thread to complete the
238 if (!TEST_true(run_thread(&t
, run_server_thread
)))
244 TEST_error("No thread support in this build");
250 err
= SSL_ERROR_WANT_WRITE
;
251 while (!clienterr
&& retc
<= 0 && err
== SSL_ERROR_WANT_WRITE
) {
252 retc
= SSL_connect(clientssl
);
254 err
= SSL_get_error(clientssl
, retc
);
257 if (!clienterr
&& retc
<= 0 && err
!= SSL_ERROR_WANT_READ
) {
258 TEST_info("SSL_connect() failed %d, %d", retc
, err
);
259 TEST_openssl_errors();
264 * We're cheating. We don't take any notice of SSL_get_tick_timeout()
265 * and tick everytime around the loop anyway. This is inefficient. We
266 * can get away with it in test code because we control both ends of
267 * the communications and don't expect network delays. This shouldn't
268 * be done in a real application.
270 if (!clienterr
&& retc
<= 0)
272 if (!servererr
&& rets
<= 0) {
273 ossl_quic_tserver_tick(qtserv
);
274 servererr
= ossl_quic_tserver_is_term_any(qtserv
);
276 rets
= ossl_quic_tserver_is_handshake_confirmed(qtserv
);
279 if (clienterr
&& servererr
)
282 if (clientssl
!= NULL
&& ++abortctr
== MAXLOOPS
) {
283 TEST_info("No progress made");
286 } while ((retc
<= 0 && !clienterr
) || (rets
<= 0 && !servererr
));
288 if (qtserv
== NULL
&& rets
> 0) {
289 #if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG)
290 if (!TEST_true(wait_for_thread(t
)) || !TEST_true(globserverret
))
293 TEST_error("Should not happen");
298 if (!clienterr
&& !servererr
)
304 int qtest_shutdown(QUIC_TSERVER
*qtserv
, SSL
*clientssl
)
306 /* Busy loop in non-blocking mode. It should be quick because its local */
307 while (SSL_shutdown(clientssl
) != 1)
308 ossl_quic_tserver_tick(qtserv
);
313 int qtest_check_server_transport_err(QUIC_TSERVER
*qtserv
, uint64_t code
)
315 QUIC_TERMINATE_CAUSE cause
;
317 ossl_quic_tserver_tick(qtserv
);
320 * Check that the server has closed with the specified code from the client
322 if (!TEST_true(ossl_quic_tserver_is_term_any(qtserv
)))
325 cause
= ossl_quic_tserver_get_terminate_cause(qtserv
);
326 if (!TEST_true(cause
.remote
)
327 || !TEST_uint64_t_eq(cause
.error_code
, code
))
333 int qtest_check_server_protocol_err(QUIC_TSERVER
*qtserv
)
335 return qtest_check_server_transport_err(qtserv
, QUIC_ERR_PROTOCOL_VIOLATION
);
338 void qtest_fault_free(QTEST_FAULT
*fault
)
343 packet_plain_finish(fault
);
344 handshake_finish(fault
);
349 static int packet_plain_mutate(const QUIC_PKT_HDR
*hdrin
,
350 const OSSL_QTX_IOVEC
*iovecin
, size_t numin
,
351 QUIC_PKT_HDR
**hdrout
,
352 const OSSL_QTX_IOVEC
**iovecout
,
356 QTEST_FAULT
*fault
= arg
;
360 /* Coalesce our data into a single buffer */
362 /* First calculate required buffer size */
363 for (i
= 0; i
< numin
; i
++)
364 bufsz
+= iovecin
[i
].buf_len
;
366 fault
->pplainio
.buf_len
= bufsz
;
368 /* Add an allowance for possible growth */
369 bufsz
+= GROWTH_ALLOWANCE
;
371 fault
->pplainio
.buf
= cur
= OPENSSL_malloc(bufsz
);
373 fault
->pplainio
.buf_len
= 0;
377 fault
->pplainbuf_alloc
= bufsz
;
379 /* Copy in the data from the input buffers */
380 for (i
= 0; i
< numin
; i
++) {
381 memcpy(cur
, iovecin
[i
].buf
, iovecin
[i
].buf_len
);
382 cur
+= iovecin
[i
].buf_len
;
385 fault
->pplainhdr
= *hdrin
;
387 /* Cast below is safe because we allocated the buffer */
388 if (fault
->pplaincb
!= NULL
389 && !fault
->pplaincb(fault
, &fault
->pplainhdr
,
390 (unsigned char *)fault
->pplainio
.buf
,
391 fault
->pplainio
.buf_len
, fault
->pplaincbarg
))
394 *hdrout
= &fault
->pplainhdr
;
395 *iovecout
= &fault
->pplainio
;
401 static void packet_plain_finish(void *arg
)
403 QTEST_FAULT
*fault
= arg
;
405 /* Cast below is safe because we allocated the buffer */
406 OPENSSL_free((unsigned char *)fault
->pplainio
.buf
);
407 fault
->pplainio
.buf_len
= 0;
408 fault
->pplainbuf_alloc
= 0;
409 fault
->pplainio
.buf
= NULL
;
412 int qtest_fault_set_packet_plain_listener(QTEST_FAULT
*fault
,
413 qtest_fault_on_packet_plain_cb pplaincb
,
416 fault
->pplaincb
= pplaincb
;
417 fault
->pplaincbarg
= pplaincbarg
;
419 return ossl_quic_tserver_set_plain_packet_mutator(fault
->qtserv
,
425 /* To be called from a packet_plain_listener callback */
426 int qtest_fault_resize_plain_packet(QTEST_FAULT
*fault
, size_t newlen
)
429 size_t oldlen
= fault
->pplainio
.buf_len
;
432 * Alloc'd size should always be non-zero, so if this fails we've been
435 if (fault
->pplainbuf_alloc
== 0)
438 if (newlen
> fault
->pplainbuf_alloc
) {
439 /* This exceeds our growth allowance. Fail */
443 /* Cast below is safe because we allocated the buffer */
444 buf
= (unsigned char *)fault
->pplainio
.buf
;
446 if (newlen
> oldlen
) {
447 /* Extend packet with 0 bytes */
448 memset(buf
+ oldlen
, 0, newlen
- oldlen
);
449 } /* else we're truncating or staying the same */
451 fault
->pplainio
.buf_len
= newlen
;
452 fault
->pplainhdr
.len
= newlen
;
458 * Prepend frame data into a packet. To be called from a packet_plain_listener
461 int qtest_fault_prepend_frame(QTEST_FAULT
*fault
, unsigned char *frame
,
468 * Alloc'd size should always be non-zero, so if this fails we've been
471 if (fault
->pplainbuf_alloc
== 0)
474 /* Cast below is safe because we allocated the buffer */
475 buf
= (unsigned char *)fault
->pplainio
.buf
;
476 old_len
= fault
->pplainio
.buf_len
;
478 /* Extend the size of the packet by the size of the new frame */
479 if (!TEST_true(qtest_fault_resize_plain_packet(fault
,
480 old_len
+ frame_len
)))
483 memmove(buf
+ frame_len
, buf
, old_len
);
484 memcpy(buf
, frame
, frame_len
);
489 static int handshake_mutate(const unsigned char *msgin
, size_t msginlen
,
490 unsigned char **msgout
, size_t *msgoutlen
,
493 QTEST_FAULT
*fault
= arg
;
495 unsigned long payloadlen
;
496 unsigned int msgtype
;
499 buf
= OPENSSL_malloc(msginlen
+ GROWTH_ALLOWANCE
);
503 fault
->handbuf
= buf
;
504 fault
->handbuflen
= msginlen
;
505 fault
->handbufalloc
= msginlen
+ GROWTH_ALLOWANCE
;
506 memcpy(buf
, msgin
, msginlen
);
508 if (!PACKET_buf_init(&pkt
, buf
, msginlen
)
509 || !PACKET_get_1(&pkt
, &msgtype
)
510 || !PACKET_get_net_3(&pkt
, &payloadlen
)
511 || PACKET_remaining(&pkt
) != payloadlen
)
514 /* Parse specific message types */
516 case SSL3_MT_ENCRYPTED_EXTENSIONS
:
518 QTEST_ENCRYPTED_EXTENSIONS ee
;
520 if (fault
->encextcb
== NULL
)
524 * The EncryptedExtensions message is very simple. It just has an
525 * extensions block in it and nothing else.
527 ee
.extensions
= (unsigned char *)PACKET_data(&pkt
);
528 ee
.extensionslen
= payloadlen
;
529 if (!fault
->encextcb(fault
, &ee
, payloadlen
, fault
->encextcbarg
))
534 /* No specific handlers for these message types yet */
538 if (fault
->handshakecb
!= NULL
539 && !fault
->handshakecb(fault
, buf
, fault
->handbuflen
,
540 fault
->handshakecbarg
))
544 *msgoutlen
= fault
->handbuflen
;
549 static void handshake_finish(void *arg
)
551 QTEST_FAULT
*fault
= arg
;
553 OPENSSL_free(fault
->handbuf
);
554 fault
->handbuf
= NULL
;
557 int qtest_fault_set_handshake_listener(QTEST_FAULT
*fault
,
558 qtest_fault_on_handshake_cb handshakecb
,
559 void *handshakecbarg
)
561 fault
->handshakecb
= handshakecb
;
562 fault
->handshakecbarg
= handshakecbarg
;
564 return ossl_quic_tserver_set_handshake_mutator(fault
->qtserv
,
570 int qtest_fault_set_hand_enc_ext_listener(QTEST_FAULT
*fault
,
571 qtest_fault_on_enc_ext_cb encextcb
,
574 fault
->encextcb
= encextcb
;
575 fault
->encextcbarg
= encextcbarg
;
577 return ossl_quic_tserver_set_handshake_mutator(fault
->qtserv
,
583 /* To be called from a handshake_listener callback */
584 int qtest_fault_resize_handshake(QTEST_FAULT
*fault
, size_t newlen
)
587 size_t oldlen
= fault
->handbuflen
;
590 * Alloc'd size should always be non-zero, so if this fails we've been
593 if (fault
->handbufalloc
== 0)
596 if (newlen
> fault
->handbufalloc
) {
597 /* This exceeds our growth allowance. Fail */
601 buf
= (unsigned char *)fault
->handbuf
;
603 if (newlen
> oldlen
) {
604 /* Extend packet with 0 bytes */
605 memset(buf
+ oldlen
, 0, newlen
- oldlen
);
606 } /* else we're truncating or staying the same */
608 fault
->handbuflen
= newlen
;
612 /* To be called from message specific listener callbacks */
613 int qtest_fault_resize_message(QTEST_FAULT
*fault
, size_t newlen
)
615 /* First resize the underlying message */
616 if (!qtest_fault_resize_handshake(fault
, newlen
+ SSL3_HM_HEADER_LENGTH
))
619 /* Fixup the handshake message header */
620 fault
->handbuf
[1] = (unsigned char)((newlen
>> 16) & 0xff);
621 fault
->handbuf
[2] = (unsigned char)((newlen
>> 8) & 0xff);
622 fault
->handbuf
[3] = (unsigned char)((newlen
) & 0xff);
627 int qtest_fault_delete_extension(QTEST_FAULT
*fault
,
628 unsigned int exttype
, unsigned char *ext
,
631 PACKET pkt
, sub
, subext
;
633 const unsigned char *start
, *end
;
635 size_t msglen
= fault
->handbuflen
;
637 if (!PACKET_buf_init(&pkt
, ext
, *extlen
))
640 /* Extension block starts with 2 bytes for extension block length */
641 if (!PACKET_as_length_prefixed_2(&pkt
, &sub
))
645 start
= PACKET_data(&sub
);
646 if (!PACKET_get_net_2(&sub
, &type
)
647 || !PACKET_get_length_prefixed_2(&sub
, &subext
))
649 } while (type
!= exttype
);
652 end
= PACKET_data(&sub
);
655 * If we're not the last extension we need to move the rest earlier. The
656 * cast below is safe because we own the underlying buffer and we're no
657 * longer making PACKET calls.
659 if (end
< ext
+ *extlen
)
660 memmove((unsigned char *)start
, end
, end
- start
);
663 * Calculate new extensions payload length =
665 * - 2 extension block length bytes
666 * - length of removed extension
668 newlen
= *extlen
- 2 - (end
- start
);
670 /* Fixup the length bytes for the extension block */
671 ext
[0] = (unsigned char)((newlen
>> 8) & 0xff);
672 ext
[1] = (unsigned char)((newlen
) & 0xff);
675 * Length of the whole extension block is the new payload length plus the
676 * 2 bytes for the length
678 *extlen
= newlen
+ 2;
680 /* We can now resize the message */
681 if ((size_t)(end
- start
) + SSL3_HM_HEADER_LENGTH
> msglen
)
682 return 0; /* Should not happen */
683 msglen
-= (end
- start
) + SSL3_HM_HEADER_LENGTH
;
684 if (!qtest_fault_resize_message(fault
, msglen
))
690 #define BIO_TYPE_CIPHER_PACKET_FILTER (0x80 | BIO_TYPE_FILTER)
692 static BIO_METHOD
*pcipherbiometh
= NULL
;
694 # define BIO_MSG_N(array, stride, n) (*(BIO_MSG *)((char *)(array) + (n)*(stride)))
696 static int pcipher_sendmmsg(BIO
*b
, BIO_MSG
*msg
, size_t stride
,
697 size_t num_msg
, uint64_t flags
,
698 size_t *num_processed
)
701 BIO
*next
= BIO_next(b
);
702 ossl_ssize_t ret
= 0;
703 size_t i
= 0, tmpnump
;
706 unsigned char *tmpdata
;
711 fault
= BIO_get_data(b
);
713 || (fault
->pciphercb
== NULL
&& fault
->datagramcb
== NULL
))
714 return BIO_sendmmsg(next
, msg
, stride
, num_msg
, flags
, num_processed
);
721 for (i
= 0; i
< num_msg
; ++i
) {
722 fault
->msg
= BIO_MSG_N(msg
, stride
, i
);
724 /* Take a copy of the data so that callbacks can modify it */
725 tmpdata
= OPENSSL_malloc(fault
->msg
.data_len
+ GROWTH_ALLOWANCE
);
728 memcpy(tmpdata
, fault
->msg
.data
, fault
->msg
.data_len
);
729 fault
->msg
.data
= tmpdata
;
730 fault
->msgalloc
= fault
->msg
.data_len
+ GROWTH_ALLOWANCE
;
732 if (fault
->pciphercb
!= NULL
) {
733 if (!PACKET_buf_init(&pkt
, fault
->msg
.data
, fault
->msg
.data_len
))
737 if (!ossl_quic_wire_decode_pkt_hdr(&pkt
,
738 0 /* TODO(QUIC): Not sure how this should be set*/, 1,
743 * hdr.data is const - but its our buffer so casting away the
746 if (!fault
->pciphercb(fault
, &hdr
, (unsigned char *)hdr
.data
,
747 hdr
.len
, fault
->pciphercbarg
))
751 * TODO(QUIC): At the moment modifications to hdr by the callback
752 * are ignored. We might need to rewrite the QUIC header to
753 * enable tests to change this. We also don't yet have a
754 * mechanism for the callback to change the encrypted data
755 * length. It's not clear if that's needed or not.
757 } while (PACKET_remaining(&pkt
) > 0);
760 if (fault
->datagramcb
!= NULL
761 && !fault
->datagramcb(fault
, &fault
->msg
, stride
,
762 fault
->datagramcbarg
))
765 if (!BIO_sendmmsg(next
, &fault
->msg
, stride
, 1, flags
, &tmpnump
)) {
770 OPENSSL_free(fault
->msg
.data
);
771 fault
->msg
.data
= NULL
;
778 OPENSSL_free(fault
->msg
.data
);
779 fault
->msg
.data
= NULL
;
783 static long pcipher_ctrl(BIO
*b
, int cmd
, long larg
, void *parg
)
785 BIO
*next
= BIO_next(b
);
790 return BIO_ctrl(next
, cmd
, larg
, parg
);
793 static BIO_METHOD
*get_bio_method(void)
797 if (pcipherbiometh
!= NULL
)
798 return pcipherbiometh
;
800 tmp
= BIO_meth_new(BIO_TYPE_CIPHER_PACKET_FILTER
, "Cipher Packet Filter");
805 if (!TEST_true(BIO_meth_set_sendmmsg(tmp
, pcipher_sendmmsg
))
806 || !TEST_true(BIO_meth_set_ctrl(tmp
, pcipher_ctrl
)))
809 pcipherbiometh
= tmp
;
813 return pcipherbiometh
;
816 int qtest_fault_set_packet_cipher_listener(QTEST_FAULT
*fault
,
817 qtest_fault_on_packet_cipher_cb pciphercb
,
820 fault
->pciphercb
= pciphercb
;
821 fault
->pciphercbarg
= pciphercbarg
;
826 int qtest_fault_set_datagram_listener(QTEST_FAULT
*fault
,
827 qtest_fault_on_datagram_cb datagramcb
,
830 fault
->datagramcb
= datagramcb
;
831 fault
->datagramcbarg
= datagramcbarg
;
836 /* To be called from a datagram_listener callback */
837 int qtest_fault_resize_datagram(QTEST_FAULT
*fault
, size_t newlen
)
839 if (newlen
> fault
->msgalloc
)
842 if (newlen
> fault
->msg
.data_len
)
843 memset((unsigned char *)fault
->msg
.data
+ fault
->msg
.data_len
, 0,
844 newlen
- fault
->msg
.data_len
);
846 fault
->msg
.data_len
= newlen
;