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/bio.h>
12 #include "quictestlib.h"
13 #include "../testutil.h"
14 #include "internal/quic_wire_pkt.h"
15 #include "internal/quic_record_tx.h"
16 #include "internal/quic_error.h"
17 #include "internal/packet.h"
19 #define GROWTH_ALLOWANCE 1024
21 struct ossl_quic_fault
{
24 /* Plain packet mutations */
25 /* Header for the plaintext packet */
26 QUIC_PKT_HDR pplainhdr
;
27 /* iovec for the plaintext packet data buffer */
28 OSSL_QTX_IOVEC pplainio
;
29 /* Allocted size of the plaintext packet data buffer */
30 size_t pplainbuf_alloc
;
31 ossl_quic_fault_on_packet_plain_cb pplaincb
;
34 /* Handshake message mutations */
35 /* Handshake message buffer */
36 unsigned char *handbuf
;
37 /* Allocated size of the handshake message buffer */
39 /* Actual length of the handshake message */
41 ossl_quic_fault_on_handshake_cb handshakecb
;
43 ossl_quic_fault_on_enc_ext_cb encextcb
;
46 /* Cipher packet mutations */
47 ossl_quic_fault_on_packet_cipher_cb pciphercb
;
50 /* Datagram mutations */
51 ossl_quic_fault_on_datagram_cb datagramcb
;
53 /* The currently processed message */
55 /* Allocated size of msg data buffer */
59 static void packet_plain_finish(void *arg
);
60 static void handshake_finish(void *arg
);
62 static BIO_METHOD
*get_bio_method(void);
64 int qtest_create_quic_objects(SSL_CTX
*clientctx
, char *certfile
, char *keyfile
,
65 QUIC_TSERVER
**qtserv
, SSL
**cssl
,
66 OSSL_QUIC_FAULT
**fault
)
68 /* ALPN value as recognised by QUIC_TSERVER */
69 unsigned char alpn
[] = { 8, 'o', 's', 's', 'l', 't', 'e', 's', 't' };
70 QUIC_TSERVER_ARGS tserver_args
= {0};
71 BIO
*cbio
= NULL
, *sbio
= NULL
, *fisbio
= NULL
;
72 BIO_ADDR
*peeraddr
= NULL
;
73 struct in_addr ina
= {0};
78 *cssl
= SSL_new(clientctx
);
82 if (!TEST_true(SSL_set_blocking_mode(*cssl
, 0)))
85 /* SSL_set_alpn_protos returns 0 for success! */
86 if (!TEST_false(SSL_set_alpn_protos(*cssl
, alpn
, sizeof(alpn
))))
89 if (!TEST_true(BIO_new_bio_dgram_pair(&cbio
, 0, &sbio
, 0)))
92 if (!TEST_true(BIO_dgram_set_caps(cbio
, BIO_DGRAM_CAP_HANDLES_DST_ADDR
))
93 || !TEST_true(BIO_dgram_set_caps(sbio
, BIO_DGRAM_CAP_HANDLES_DST_ADDR
)))
96 SSL_set_bio(*cssl
, cbio
, cbio
);
98 if (!TEST_ptr(peeraddr
= BIO_ADDR_new()))
101 /* Dummy server address */
102 if (!TEST_true(BIO_ADDR_rawmake(peeraddr
, AF_INET
, &ina
, sizeof(ina
),
106 if (!TEST_true(SSL_set_initial_peer_addr(*cssl
, peeraddr
)))
110 *fault
= OPENSSL_zalloc(sizeof(**fault
));
115 fisbio
= BIO_new(get_bio_method());
116 if (!TEST_ptr(fisbio
))
119 BIO_set_data(fisbio
, fault
== NULL
? NULL
: *fault
);
121 if (!TEST_ptr(BIO_push(fisbio
, sbio
)))
124 tserver_args
.net_rbio
= sbio
;
125 tserver_args
.net_wbio
= fisbio
;
127 if (!TEST_ptr(*qtserv
= ossl_quic_tserver_new(&tserver_args
, certfile
,
131 /* Ownership of fisbio and sbio is now held by *qtserv */
136 (*fault
)->qtserv
= *qtserv
;
138 BIO_ADDR_free(peeraddr
);
142 BIO_ADDR_free(peeraddr
);
147 ossl_quic_tserver_free(*qtserv
);
149 OPENSSL_free(*fault
);
154 #define MAXLOOPS 1000
156 int qtest_create_quic_connection(QUIC_TSERVER
*qtserv
, SSL
*clientssl
)
158 int retc
= -1, rets
= 0, err
, abortctr
= 0, ret
= 0;
159 int clienterr
= 0, servererr
= 0;
162 err
= SSL_ERROR_WANT_WRITE
;
163 while (!clienterr
&& retc
<= 0 && err
== SSL_ERROR_WANT_WRITE
) {
164 retc
= SSL_connect(clientssl
);
166 err
= SSL_get_error(clientssl
, retc
);
169 if (!clienterr
&& retc
<= 0 && err
!= SSL_ERROR_WANT_READ
) {
170 TEST_info("SSL_connect() failed %d, %d", retc
, err
);
171 TEST_openssl_errors();
176 * We're cheating. We don't take any notice of SSL_get_tick_timeout()
177 * and tick everytime around the loop anyway. This is inefficient. We
178 * can get away with it in test code because we control both ends of
179 * the communications and don't expect network delays. This shouldn't
180 * be done in a real application.
182 if (!clienterr
&& retc
<= 0)
184 if (!servererr
&& rets
<= 0) {
185 ossl_quic_tserver_tick(qtserv
);
186 servererr
= ossl_quic_tserver_is_term_any(qtserv
, NULL
);
188 rets
= ossl_quic_tserver_is_handshake_confirmed(qtserv
);
191 if (clienterr
&& servererr
)
194 if (++abortctr
== MAXLOOPS
) {
195 TEST_info("No progress made");
198 } while ((retc
<= 0 && !clienterr
) || (rets
<= 0 && !servererr
));
200 if (!clienterr
&& !servererr
)
206 int qtest_check_server_transport_err(QUIC_TSERVER
*qtserv
, uint64_t code
)
208 QUIC_TERMINATE_CAUSE cause
;
210 ossl_quic_tserver_tick(qtserv
);
213 * Check that the server has closed with the specified code from the client
215 if (!TEST_true(ossl_quic_tserver_is_term_any(qtserv
)))
218 cause
= ossl_quic_tserver_get_terminate_cause(qtserv
);
219 if (!TEST_true(cause
.remote
)
220 || !TEST_uint64_t_eq(cause
.error_code
, code
))
226 int qtest_check_server_protocol_err(QUIC_TSERVER
*qtserv
)
228 return qtest_check_server_transport_err(qtserv
, QUIC_ERR_PROTOCOL_VIOLATION
);
231 void ossl_quic_fault_free(OSSL_QUIC_FAULT
*fault
)
236 packet_plain_finish(fault
);
237 handshake_finish(fault
);
242 static int packet_plain_mutate(const QUIC_PKT_HDR
*hdrin
,
243 const OSSL_QTX_IOVEC
*iovecin
, size_t numin
,
244 QUIC_PKT_HDR
**hdrout
,
245 const OSSL_QTX_IOVEC
**iovecout
,
249 OSSL_QUIC_FAULT
*fault
= arg
;
253 /* Coalesce our data into a single buffer */
255 /* First calculate required buffer size */
256 for (i
= 0; i
< numin
; i
++)
257 bufsz
+= iovecin
[i
].buf_len
;
259 fault
->pplainio
.buf_len
= bufsz
;
261 /* Add an allowance for possible growth */
262 bufsz
+= GROWTH_ALLOWANCE
;
264 fault
->pplainio
.buf
= cur
= OPENSSL_malloc(bufsz
);
266 fault
->pplainio
.buf_len
= 0;
270 fault
->pplainbuf_alloc
= bufsz
;
272 /* Copy in the data from the input buffers */
273 for (i
= 0; i
< numin
; i
++) {
274 memcpy(cur
, iovecin
[i
].buf
, iovecin
[i
].buf_len
);
275 cur
+= iovecin
[i
].buf_len
;
278 fault
->pplainhdr
= *hdrin
;
280 /* Cast below is safe because we allocated the buffer */
281 if (fault
->pplaincb
!= NULL
282 && !fault
->pplaincb(fault
, &fault
->pplainhdr
,
283 (unsigned char *)fault
->pplainio
.buf
,
284 fault
->pplainio
.buf_len
, fault
->pplaincbarg
))
287 *hdrout
= &fault
->pplainhdr
;
288 *iovecout
= &fault
->pplainio
;
294 static void packet_plain_finish(void *arg
)
296 OSSL_QUIC_FAULT
*fault
= arg
;
298 /* Cast below is safe because we allocated the buffer */
299 OPENSSL_free((unsigned char *)fault
->pplainio
.buf
);
300 fault
->pplainio
.buf_len
= 0;
301 fault
->pplainbuf_alloc
= 0;
302 fault
->pplainio
.buf
= NULL
;
305 int ossl_quic_fault_set_packet_plain_listener(OSSL_QUIC_FAULT
*fault
,
306 ossl_quic_fault_on_packet_plain_cb pplaincb
,
309 fault
->pplaincb
= pplaincb
;
310 fault
->pplaincbarg
= pplaincbarg
;
312 return ossl_quic_tserver_set_plain_packet_mutator(fault
->qtserv
,
318 /* To be called from a packet_plain_listener callback */
319 int ossl_quic_fault_resize_plain_packet(OSSL_QUIC_FAULT
*fault
, size_t newlen
)
322 size_t oldlen
= fault
->pplainio
.buf_len
;
325 * Alloc'd size should always be non-zero, so if this fails we've been
328 if (fault
->pplainbuf_alloc
== 0)
331 if (newlen
> fault
->pplainbuf_alloc
) {
332 /* This exceeds our growth allowance. Fail */
336 /* Cast below is safe because we allocated the buffer */
337 buf
= (unsigned char *)fault
->pplainio
.buf
;
339 if (newlen
> oldlen
) {
340 /* Extend packet with 0 bytes */
341 memset(buf
+ oldlen
, 0, newlen
- oldlen
);
342 } /* else we're truncating or staying the same */
344 fault
->pplainio
.buf_len
= newlen
;
345 fault
->pplainhdr
.len
= newlen
;
351 * Prepend frame data into a packet. To be called from a packet_plain_listener
354 int ossl_quic_fault_prepend_frame(OSSL_QUIC_FAULT
*fault
, unsigned char *frame
,
361 * Alloc'd size should always be non-zero, so if this fails we've been
364 if (fault
->pplainbuf_alloc
== 0)
367 /* Cast below is safe because we allocated the buffer */
368 buf
= (unsigned char *)fault
->pplainio
.buf
;
369 old_len
= fault
->pplainio
.buf_len
;
371 /* Extend the size of the packet by the size of the new frame */
372 if (!TEST_true(ossl_quic_fault_resize_plain_packet(fault
,
373 old_len
+ frame_len
)))
376 memmove(buf
+ frame_len
, buf
, old_len
);
377 memcpy(buf
, frame
, frame_len
);
382 static int handshake_mutate(const unsigned char *msgin
, size_t msginlen
,
383 unsigned char **msgout
, size_t *msgoutlen
,
386 OSSL_QUIC_FAULT
*fault
= arg
;
388 unsigned long payloadlen
;
389 unsigned int msgtype
;
392 buf
= OPENSSL_malloc(msginlen
+ GROWTH_ALLOWANCE
);
396 fault
->handbuf
= buf
;
397 fault
->handbuflen
= msginlen
;
398 fault
->handbufalloc
= msginlen
+ GROWTH_ALLOWANCE
;
399 memcpy(buf
, msgin
, msginlen
);
401 if (!PACKET_buf_init(&pkt
, buf
, msginlen
)
402 || !PACKET_get_1(&pkt
, &msgtype
)
403 || !PACKET_get_net_3(&pkt
, &payloadlen
)
404 || PACKET_remaining(&pkt
) != payloadlen
)
407 /* Parse specific message types */
409 case SSL3_MT_ENCRYPTED_EXTENSIONS
:
411 OSSL_QF_ENCRYPTED_EXTENSIONS ee
;
413 if (fault
->encextcb
== NULL
)
417 * The EncryptedExtensions message is very simple. It just has an
418 * extensions block in it and nothing else.
420 ee
.extensions
= (unsigned char *)PACKET_data(&pkt
);
421 ee
.extensionslen
= payloadlen
;
422 if (!fault
->encextcb(fault
, &ee
, payloadlen
, fault
->encextcbarg
))
427 /* No specific handlers for these message types yet */
431 if (fault
->handshakecb
!= NULL
432 && !fault
->handshakecb(fault
, buf
, fault
->handbuflen
,
433 fault
->handshakecbarg
))
437 *msgoutlen
= fault
->handbuflen
;
442 static void handshake_finish(void *arg
)
444 OSSL_QUIC_FAULT
*fault
= arg
;
446 OPENSSL_free(fault
->handbuf
);
447 fault
->handbuf
= NULL
;
450 int ossl_quic_fault_set_handshake_listener(OSSL_QUIC_FAULT
*fault
,
451 ossl_quic_fault_on_handshake_cb handshakecb
,
452 void *handshakecbarg
)
454 fault
->handshakecb
= handshakecb
;
455 fault
->handshakecbarg
= handshakecbarg
;
457 return ossl_quic_tserver_set_handshake_mutator(fault
->qtserv
,
463 int ossl_quic_fault_set_hand_enc_ext_listener(OSSL_QUIC_FAULT
*fault
,
464 ossl_quic_fault_on_enc_ext_cb encextcb
,
467 fault
->encextcb
= encextcb
;
468 fault
->encextcbarg
= encextcbarg
;
470 return ossl_quic_tserver_set_handshake_mutator(fault
->qtserv
,
476 /* To be called from a handshake_listener callback */
477 int ossl_quic_fault_resize_handshake(OSSL_QUIC_FAULT
*fault
, size_t newlen
)
480 size_t oldlen
= fault
->handbuflen
;
483 * Alloc'd size should always be non-zero, so if this fails we've been
486 if (fault
->handbufalloc
== 0)
489 if (newlen
> fault
->handbufalloc
) {
490 /* This exceeds our growth allowance. Fail */
494 buf
= (unsigned char *)fault
->handbuf
;
496 if (newlen
> oldlen
) {
497 /* Extend packet with 0 bytes */
498 memset(buf
+ oldlen
, 0, newlen
- oldlen
);
499 } /* else we're truncating or staying the same */
501 fault
->handbuflen
= newlen
;
505 /* To be called from message specific listener callbacks */
506 int ossl_quic_fault_resize_message(OSSL_QUIC_FAULT
*fault
, size_t newlen
)
508 /* First resize the underlying message */
509 if (!ossl_quic_fault_resize_handshake(fault
, newlen
+ SSL3_HM_HEADER_LENGTH
))
512 /* Fixup the handshake message header */
513 fault
->handbuf
[1] = (unsigned char)((newlen
>> 16) & 0xff);
514 fault
->handbuf
[2] = (unsigned char)((newlen
>> 8) & 0xff);
515 fault
->handbuf
[3] = (unsigned char)((newlen
) & 0xff);
520 int ossl_quic_fault_delete_extension(OSSL_QUIC_FAULT
*fault
,
521 unsigned int exttype
, unsigned char *ext
,
524 PACKET pkt
, sub
, subext
;
526 const unsigned char *start
, *end
;
528 size_t msglen
= fault
->handbuflen
;
530 if (!PACKET_buf_init(&pkt
, ext
, *extlen
))
533 /* Extension block starts with 2 bytes for extension block length */
534 if (!PACKET_as_length_prefixed_2(&pkt
, &sub
))
538 start
= PACKET_data(&sub
);
539 if (!PACKET_get_net_2(&sub
, &type
)
540 || !PACKET_get_length_prefixed_2(&sub
, &subext
))
542 } while (type
!= exttype
);
545 end
= PACKET_data(&sub
);
548 * If we're not the last extension we need to move the rest earlier. The
549 * cast below is safe because we own the underlying buffer and we're no
550 * longer making PACKET calls.
552 if (end
< ext
+ *extlen
)
553 memmove((unsigned char *)start
, end
, end
- start
);
556 * Calculate new extensions payload length =
558 * - 2 extension block length bytes
559 * - length of removed extension
561 newlen
= *extlen
- 2 - (end
- start
);
563 /* Fixup the length bytes for the extension block */
564 ext
[0] = (unsigned char)((newlen
>> 8) & 0xff);
565 ext
[1] = (unsigned char)((newlen
) & 0xff);
568 * Length of the whole extension block is the new payload length plus the
569 * 2 bytes for the length
571 *extlen
= newlen
+ 2;
573 /* We can now resize the message */
574 if ((size_t)(end
- start
) + SSL3_HM_HEADER_LENGTH
> msglen
)
575 return 0; /* Should not happen */
576 msglen
-= (end
- start
) + SSL3_HM_HEADER_LENGTH
;
577 if (!ossl_quic_fault_resize_message(fault
, msglen
))
583 #define BIO_TYPE_CIPHER_PACKET_FILTER (0x80 | BIO_TYPE_FILTER)
585 static BIO_METHOD
*pcipherbiometh
= NULL
;
587 # define BIO_MSG_N(array, stride, n) (*(BIO_MSG *)((char *)(array) + (n)*(stride)))
589 static int pcipher_sendmmsg(BIO
*b
, BIO_MSG
*msg
, size_t stride
,
590 size_t num_msg
, uint64_t flags
,
591 size_t *num_processed
)
593 OSSL_QUIC_FAULT
*fault
;
594 BIO
*next
= BIO_next(b
);
595 ossl_ssize_t ret
= 0;
596 size_t i
= 0, tmpnump
;
599 unsigned char *tmpdata
;
604 fault
= BIO_get_data(b
);
606 || (fault
->pciphercb
== NULL
&& fault
->datagramcb
== NULL
))
607 return BIO_sendmmsg(next
, msg
, stride
, num_msg
, flags
, num_processed
);
614 for (i
= 0; i
< num_msg
; ++i
) {
615 fault
->msg
= BIO_MSG_N(msg
, stride
, i
);
617 /* Take a copy of the data so that callbacks can modify it */
618 tmpdata
= OPENSSL_malloc(fault
->msg
.data_len
+ GROWTH_ALLOWANCE
);
621 memcpy(tmpdata
, fault
->msg
.data
, fault
->msg
.data_len
);
622 fault
->msg
.data
= tmpdata
;
623 fault
->msgalloc
= fault
->msg
.data_len
+ GROWTH_ALLOWANCE
;
625 if (fault
->pciphercb
!= NULL
) {
626 if (!PACKET_buf_init(&pkt
, fault
->msg
.data
, fault
->msg
.data_len
))
630 if (!ossl_quic_wire_decode_pkt_hdr(&pkt
,
631 0 /* TODO(QUIC): Not sure how this should be set*/, 1,
636 * hdr.data is const - but its our buffer so casting away the
639 if (!fault
->pciphercb(fault
, &hdr
, (unsigned char *)hdr
.data
,
640 hdr
.len
, fault
->pciphercbarg
))
644 * TODO(QUIC): At the moment modifications to hdr by the callback
645 * are ignored. We might need to rewrite the QUIC header to
646 * enable tests to change this. We also don't yet have a
647 * mechanism for the callback to change the encrypted data
648 * length. It's not clear if that's needed or not.
650 } while (PACKET_remaining(&pkt
) > 0);
653 if (fault
->datagramcb
!= NULL
654 && !fault
->datagramcb(fault
, &fault
->msg
, stride
,
655 fault
->datagramcbarg
))
658 if (!BIO_sendmmsg(next
, &fault
->msg
, stride
, 1, flags
, &tmpnump
)) {
663 OPENSSL_free(fault
->msg
.data
);
664 fault
->msg
.data
= NULL
;
675 OPENSSL_free(fault
->msg
.data
);
676 fault
->msg
.data
= NULL
;
680 static long pcipher_ctrl(BIO
*b
, int cmd
, long larg
, void *parg
)
682 BIO
*next
= BIO_next(b
);
687 return BIO_ctrl(next
, cmd
, larg
, parg
);
690 static BIO_METHOD
*get_bio_method(void)
694 if (pcipherbiometh
!= NULL
)
695 return pcipherbiometh
;
697 tmp
= BIO_meth_new(BIO_TYPE_CIPHER_PACKET_FILTER
, "Cipher Packet Filter");
702 if (!TEST_true(BIO_meth_set_sendmmsg(tmp
, pcipher_sendmmsg
))
703 || !TEST_true(BIO_meth_set_ctrl(tmp
, pcipher_ctrl
)))
706 pcipherbiometh
= tmp
;
710 return pcipherbiometh
;
713 int ossl_quic_fault_set_packet_cipher_listener(OSSL_QUIC_FAULT
*fault
,
714 ossl_quic_fault_on_packet_cipher_cb pciphercb
,
717 fault
->pciphercb
= pciphercb
;
718 fault
->pciphercbarg
= pciphercbarg
;
723 int ossl_quic_fault_set_datagram_listener(OSSL_QUIC_FAULT
*fault
,
724 ossl_quic_fault_on_datagram_cb datagramcb
,
727 fault
->datagramcb
= datagramcb
;
728 fault
->datagramcbarg
= datagramcbarg
;
733 /* To be called from a datagram_listener callback */
734 int ossl_quic_fault_resize_datagram(OSSL_QUIC_FAULT
*fault
, size_t newlen
)
736 if (newlen
> fault
->msgalloc
)
739 if (newlen
> fault
->msg
.data_len
)
740 memset((unsigned char *)fault
->msg
.data
+ fault
->msg
.data_len
, 0,
741 newlen
- fault
->msg
.data_len
);
743 fault
->msg
.data_len
= newlen
;