2 * Copyright 2022-2023 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_ssl.h"
20 #include "internal/quic_wire_pkt.h"
21 #include "internal/quic_record_tx.h"
22 #include "internal/quic_error.h"
23 #include "internal/packet.h"
24 #include "internal/tsan_assist.h"
26 #define GROWTH_ALLOWANCE 1024
31 /* Plain packet mutations */
32 /* Header for the plaintext packet */
33 QUIC_PKT_HDR pplainhdr;
34 /* iovec for the plaintext packet data buffer */
35 OSSL_QTX_IOVEC pplainio;
36 /* Allocated size of the plaintext packet data buffer */
37 size_t pplainbuf_alloc;
38 qtest_fault_on_packet_plain_cb pplaincb;
41 /* Handshake message mutations */
42 /* Handshake message buffer */
43 unsigned char *handbuf;
44 /* Allocated size of the handshake message buffer */
46 /* Actual length of the handshake message */
48 qtest_fault_on_handshake_cb handshakecb;
50 qtest_fault_on_enc_ext_cb encextcb;
53 /* Cipher packet mutations */
54 qtest_fault_on_packet_cipher_cb pciphercb;
57 /* Datagram mutations */
58 qtest_fault_on_datagram_cb datagramcb;
60 /* The currently processed message */
62 /* Allocated size of msg data buffer */
66 static void packet_plain_finish(void *arg);
67 static void handshake_finish(void *arg);
69 static int using_fake_time = 0;
70 static OSSL_TIME fake_now;
72 static OSSL_TIME fake_now_cb(void *arg)
77 int qtest_create_quic_objects(OSSL_LIB_CTX *libctx, SSL_CTX *clientctx,
78 SSL_CTX *serverctx, char *certfile, char *keyfile,
79 int flags, QUIC_TSERVER **qtserv, SSL **cssl,
82 /* ALPN value as recognised by QUIC_TSERVER */
83 unsigned char alpn[] = { 8, 'o', 's', 's', 'l', 't', 'e', 's', 't' };
84 QUIC_TSERVER_ARGS tserver_args = {0};
85 BIO *cbio = NULL, *sbio = NULL, *fisbio = NULL;
86 BIO_ADDR *peeraddr = NULL;
87 struct in_addr ina = {0};
94 *cssl = SSL_new(clientctx);
99 /* SSL_set_alpn_protos returns 0 for success! */
100 if (!TEST_false(SSL_set_alpn_protos(*cssl, alpn, sizeof(alpn))))
103 if (!TEST_ptr(peeraddr = BIO_ADDR_new()))
106 if ((flags & QTEST_FLAG_BLOCK) != 0) {
107 #if !defined(OPENSSL_NO_POSIX_IO)
111 * For blocking mode we need to create actual sockets rather than doing
112 * everything in memory
114 if (!TEST_true(create_test_sockets(&cfd, &sfd, SOCK_DGRAM, peeraddr)))
116 cbio = BIO_new_dgram(cfd, 1);
117 if (!TEST_ptr(cbio)) {
122 sbio = BIO_new_dgram(sfd, 1);
123 if (!TEST_ptr(sbio)) {
131 if (!TEST_true(BIO_new_bio_dgram_pair(&cbio, 0, &sbio, 0)))
134 if (!TEST_true(BIO_dgram_set_caps(cbio, BIO_DGRAM_CAP_HANDLES_DST_ADDR))
135 || !TEST_true(BIO_dgram_set_caps(sbio, BIO_DGRAM_CAP_HANDLES_DST_ADDR)))
138 /* Dummy server address */
139 if (!TEST_true(BIO_ADDR_rawmake(peeraddr, AF_INET, &ina, sizeof(ina),
144 if ((flags & QTEST_FLAG_NOISE) != 0) {
145 BIO *noisebio = BIO_new(bio_f_noisy_dgram_filter());
147 if (!TEST_ptr(noisebio))
149 cbio = BIO_push(noisebio, cbio);
152 SSL_set_bio(*cssl, cbio, cbio);
154 if (!TEST_true(SSL_set_blocking_mode(*cssl,
155 (flags & QTEST_FLAG_BLOCK) != 0 ? 1 : 0)))
158 if (!TEST_true(SSL_set1_initial_peer_addr(*cssl, peeraddr)))
162 *fault = OPENSSL_zalloc(sizeof(**fault));
167 fisbio = BIO_new(qtest_get_bio_method());
168 if (!TEST_ptr(fisbio))
171 BIO_set_data(fisbio, fault == NULL ? NULL : *fault);
173 if (!TEST_ptr(BIO_push(fisbio, sbio)))
176 tserver_args.libctx = libctx;
177 tserver_args.net_rbio = sbio;
178 tserver_args.net_wbio = fisbio;
179 tserver_args.alpn = NULL;
180 if (serverctx != NULL && !TEST_true(SSL_CTX_up_ref(serverctx)))
182 tserver_args.ctx = serverctx;
183 if ((flags & QTEST_FLAG_FAKE_TIME) != 0) {
185 fake_now = ossl_time_zero();
186 /* zero time can have a special meaning, bump it */
188 tserver_args.now_cb = fake_now_cb;
189 (void)ossl_quic_conn_set_override_now_cb(*cssl, fake_now_cb, NULL);
194 if (!TEST_ptr(*qtserv = ossl_quic_tserver_new(&tserver_args, certfile,
198 /* Ownership of fisbio and sbio is now held by *qtserv */
203 (*fault)->qtserv = *qtserv;
205 BIO_ADDR_free(peeraddr);
209 SSL_CTX_free(tserver_args.ctx);
210 BIO_ADDR_free(peeraddr);
216 ossl_quic_tserver_free(*qtserv);
218 OPENSSL_free(*fault);
223 void qtest_add_time(uint64_t millis)
225 fake_now = ossl_time_add(fake_now, ossl_ms2time(millis));
228 QTEST_FAULT *qtest_create_injector(QUIC_TSERVER *ts)
232 f = OPENSSL_zalloc(sizeof(*f));
241 int qtest_supports_blocking(void)
243 #if !defined(OPENSSL_NO_POSIX_IO) && defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG)
250 #define MAXLOOPS 1000
252 #if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG)
253 static int globserverret = 0;
254 static TSAN_QUALIFIER int abortserverthread = 0;
255 static QUIC_TSERVER *globtserv;
256 static const thread_t thread_zero;
258 static void run_server_thread(void)
261 * This will operate in a busy loop because the server does not block,
262 * but should be acceptable because it is local and we expect this to be
265 globserverret = qtest_create_quic_connection(globtserv, NULL);
269 static int wait_for_timeout(SSL *s, QUIC_TSERVER *qtserv)
272 OSSL_TIME ctimeout, stimeout, mintimeout, now;
275 /* We don't need to wait in blocking mode */
276 if (s == NULL || qtserv == NULL)
279 /* Don't wait if either BIO has data waiting */
280 if (BIO_pending(SSL_get_rbio(s)) > 0
281 || BIO_pending(ossl_quic_tserver_get0_rbio(qtserv)) > 0)
285 * Neither endpoint has data waiting to be read. We assume data transmission
286 * is instantaneous due to using mem based BIOs, so there is no data "in
287 * flight" and no more data will be sent by either endpoint until some time
288 * based event has occurred. Therefore, wait for a timeout to occur. This
289 * might happen if we are using the noisy BIO and datagrams have been lost.
291 if (!SSL_get_event_timeout(s, &tv, &cinf))
296 now = ossl_time_now();
297 ctimeout = cinf ? ossl_time_infinite() : ossl_time_from_timeval(tv);
298 stimeout = ossl_time_subtract(ossl_quic_tserver_get_deadline(qtserv), now);
299 mintimeout = ossl_time_min(ctimeout, stimeout);
300 if (ossl_time_is_infinite(mintimeout))
303 fake_now = ossl_time_add(now, mintimeout);
305 OSSL_sleep(ossl_time2ms(mintimeout));
310 int qtest_create_quic_connection_ex(QUIC_TSERVER *qtserv, SSL *clientssl,
313 int retc = -1, rets = 0, abortctr = 0, ret = 0;
314 int clienterr = 0, servererr = 0;
315 #if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG)
317 * Pointless initialisation to avoid bogus compiler warnings about using
320 thread_t t = thread_zero;
322 if (clientssl != NULL)
323 abortserverthread = 0;
326 if (!TEST_ptr(qtserv)) {
328 } else if (clientssl == NULL) {
330 } else if (SSL_get_blocking_mode(clientssl) > 0) {
331 #if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG)
333 * clientssl is blocking. We will need a thread to complete the
337 if (!TEST_true(run_thread(&t, run_server_thread)))
343 TEST_error("No thread support in this build");
349 if (!clienterr && retc <= 0) {
352 retc = SSL_connect(clientssl);
354 err = SSL_get_error(clientssl, retc);
356 if (err == wanterr) {
358 #if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG)
359 if (qtserv == NULL && rets > 0)
360 tsan_store(&abortserverthread, 1);
365 if (err != SSL_ERROR_WANT_READ
366 && err != SSL_ERROR_WANT_WRITE) {
367 TEST_info("SSL_connect() failed %d, %d", retc, err);
368 TEST_openssl_errors();
375 if (!clienterr && retc <= 0)
376 SSL_handle_events(clientssl);
378 if (!servererr && rets <= 0) {
380 ossl_quic_tserver_tick(qtserv);
381 servererr = ossl_quic_tserver_is_term_any(qtserv);
383 rets = ossl_quic_tserver_is_handshake_confirmed(qtserv);
386 if (clienterr && servererr)
389 if (clientssl != NULL && ++abortctr == MAXLOOPS) {
390 TEST_info("No progress made");
394 if (!wait_for_timeout(clientssl, qtserv))
396 } while ((retc <= 0 && !clienterr)
397 || (rets <= 0 && !servererr
398 #if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG)
399 && !tsan_load(&abortserverthread)
403 if (qtserv == NULL && rets > 0) {
404 #if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG)
405 if (!TEST_true(wait_for_thread(t)) || !TEST_true(globserverret))
408 TEST_error("Should not happen");
413 if (!clienterr && !servererr)
419 int qtest_create_quic_connection(QUIC_TSERVER *qtserv, SSL *clientssl)
421 return qtest_create_quic_connection_ex(qtserv, clientssl, SSL_ERROR_NONE);
424 #if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG)
425 static TSAN_QUALIFIER int shutdowndone;
427 static void run_server_shutdown_thread(void)
430 * This will operate in a busy loop because the server does not block,
431 * but should be acceptable because it is local and we expect this to be
435 ossl_quic_tserver_tick(globtserv);
436 } while(!tsan_load(&shutdowndone));
440 int qtest_shutdown(QUIC_TSERVER *qtserv, SSL *clientssl)
444 #if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG)
446 * Pointless initialisation to avoid bogus compiler warnings about using
449 thread_t t = thread_zero;
452 if (SSL_get_blocking_mode(clientssl) > 0) {
453 #if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG)
455 * clientssl is blocking. We will need a thread to complete the
460 if (!TEST_true(run_thread(&t, run_server_shutdown_thread)))
465 TEST_error("No thread support in this build");
470 /* Busy loop in non-blocking mode. It should be quick because its local */
472 int rc = SSL_shutdown(clientssl);
483 ossl_quic_tserver_tick(qtserv);
486 #if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG)
487 tsan_store(&shutdowndone, 1);
489 if (!TEST_true(wait_for_thread(t)))
497 int qtest_check_server_transport_err(QUIC_TSERVER *qtserv, uint64_t code)
499 const QUIC_TERMINATE_CAUSE *cause;
501 ossl_quic_tserver_tick(qtserv);
504 * Check that the server has closed with the specified code from the client
506 if (!TEST_true(ossl_quic_tserver_is_term_any(qtserv)))
509 cause = ossl_quic_tserver_get_terminate_cause(qtserv);
511 || !TEST_true(cause->remote)
512 || !TEST_false(cause->app)
513 || !TEST_uint64_t_eq(cause->error_code, code))
519 int qtest_check_server_protocol_err(QUIC_TSERVER *qtserv)
521 return qtest_check_server_transport_err(qtserv, QUIC_ERR_PROTOCOL_VIOLATION);
524 int qtest_check_server_frame_encoding_err(QUIC_TSERVER *qtserv)
526 return qtest_check_server_transport_err(qtserv, QUIC_ERR_FRAME_ENCODING_ERROR);
529 void qtest_fault_free(QTEST_FAULT *fault)
534 packet_plain_finish(fault);
535 handshake_finish(fault);
540 static int packet_plain_mutate(const QUIC_PKT_HDR *hdrin,
541 const OSSL_QTX_IOVEC *iovecin, size_t numin,
542 QUIC_PKT_HDR **hdrout,
543 const OSSL_QTX_IOVEC **iovecout,
547 QTEST_FAULT *fault = arg;
551 /* Coalesce our data into a single buffer */
553 /* First calculate required buffer size */
554 for (i = 0; i < numin; i++)
555 bufsz += iovecin[i].buf_len;
557 fault->pplainio.buf_len = bufsz;
559 /* Add an allowance for possible growth */
560 bufsz += GROWTH_ALLOWANCE;
562 fault->pplainio.buf = cur = OPENSSL_malloc(bufsz);
564 fault->pplainio.buf_len = 0;
568 fault->pplainbuf_alloc = bufsz;
570 /* Copy in the data from the input buffers */
571 for (i = 0; i < numin; i++) {
572 memcpy(cur, iovecin[i].buf, iovecin[i].buf_len);
573 cur += iovecin[i].buf_len;
576 fault->pplainhdr = *hdrin;
578 /* Cast below is safe because we allocated the buffer */
579 if (fault->pplaincb != NULL
580 && !fault->pplaincb(fault, &fault->pplainhdr,
581 (unsigned char *)fault->pplainio.buf,
582 fault->pplainio.buf_len, fault->pplaincbarg))
585 *hdrout = &fault->pplainhdr;
586 *iovecout = &fault->pplainio;
592 static void packet_plain_finish(void *arg)
594 QTEST_FAULT *fault = arg;
596 /* Cast below is safe because we allocated the buffer */
597 OPENSSL_free((unsigned char *)fault->pplainio.buf);
598 fault->pplainio.buf_len = 0;
599 fault->pplainbuf_alloc = 0;
600 fault->pplainio.buf = NULL;
603 int qtest_fault_set_packet_plain_listener(QTEST_FAULT *fault,
604 qtest_fault_on_packet_plain_cb pplaincb,
607 fault->pplaincb = pplaincb;
608 fault->pplaincbarg = pplaincbarg;
610 return ossl_quic_tserver_set_plain_packet_mutator(fault->qtserv,
616 /* To be called from a packet_plain_listener callback */
617 int qtest_fault_resize_plain_packet(QTEST_FAULT *fault, size_t newlen)
620 size_t oldlen = fault->pplainio.buf_len;
623 * Alloc'd size should always be non-zero, so if this fails we've been
626 if (fault->pplainbuf_alloc == 0)
629 if (newlen > fault->pplainbuf_alloc) {
630 /* This exceeds our growth allowance. Fail */
634 /* Cast below is safe because we allocated the buffer */
635 buf = (unsigned char *)fault->pplainio.buf;
637 if (newlen > oldlen) {
638 /* Extend packet with 0 bytes */
639 memset(buf + oldlen, 0, newlen - oldlen);
640 } /* else we're truncating or staying the same */
642 fault->pplainio.buf_len = newlen;
643 fault->pplainhdr.len = newlen;
649 * Prepend frame data into a packet. To be called from a packet_plain_listener
652 int qtest_fault_prepend_frame(QTEST_FAULT *fault, const unsigned char *frame,
659 * Alloc'd size should always be non-zero, so if this fails we've been
662 if (fault->pplainbuf_alloc == 0)
665 /* Cast below is safe because we allocated the buffer */
666 buf = (unsigned char *)fault->pplainio.buf;
667 old_len = fault->pplainio.buf_len;
669 /* Extend the size of the packet by the size of the new frame */
670 if (!TEST_true(qtest_fault_resize_plain_packet(fault,
671 old_len + frame_len)))
674 memmove(buf + frame_len, buf, old_len);
675 memcpy(buf, frame, frame_len);
680 static int handshake_mutate(const unsigned char *msgin, size_t msginlen,
681 unsigned char **msgout, size_t *msgoutlen,
684 QTEST_FAULT *fault = arg;
686 unsigned long payloadlen;
687 unsigned int msgtype;
690 buf = OPENSSL_malloc(msginlen + GROWTH_ALLOWANCE);
694 fault->handbuf = buf;
695 fault->handbuflen = msginlen;
696 fault->handbufalloc = msginlen + GROWTH_ALLOWANCE;
697 memcpy(buf, msgin, msginlen);
699 if (!PACKET_buf_init(&pkt, buf, msginlen)
700 || !PACKET_get_1(&pkt, &msgtype)
701 || !PACKET_get_net_3(&pkt, &payloadlen)
702 || PACKET_remaining(&pkt) != payloadlen)
705 /* Parse specific message types */
707 case SSL3_MT_ENCRYPTED_EXTENSIONS:
709 QTEST_ENCRYPTED_EXTENSIONS ee;
711 if (fault->encextcb == NULL)
715 * The EncryptedExtensions message is very simple. It just has an
716 * extensions block in it and nothing else.
718 ee.extensions = (unsigned char *)PACKET_data(&pkt);
719 ee.extensionslen = payloadlen;
720 if (!fault->encextcb(fault, &ee, payloadlen, fault->encextcbarg))
725 /* No specific handlers for these message types yet */
729 if (fault->handshakecb != NULL
730 && !fault->handshakecb(fault, buf, fault->handbuflen,
731 fault->handshakecbarg))
735 *msgoutlen = fault->handbuflen;
740 static void handshake_finish(void *arg)
742 QTEST_FAULT *fault = arg;
744 OPENSSL_free(fault->handbuf);
745 fault->handbuf = NULL;
748 int qtest_fault_set_handshake_listener(QTEST_FAULT *fault,
749 qtest_fault_on_handshake_cb handshakecb,
750 void *handshakecbarg)
752 fault->handshakecb = handshakecb;
753 fault->handshakecbarg = handshakecbarg;
755 return ossl_quic_tserver_set_handshake_mutator(fault->qtserv,
761 int qtest_fault_set_hand_enc_ext_listener(QTEST_FAULT *fault,
762 qtest_fault_on_enc_ext_cb encextcb,
765 fault->encextcb = encextcb;
766 fault->encextcbarg = encextcbarg;
768 return ossl_quic_tserver_set_handshake_mutator(fault->qtserv,
774 /* To be called from a handshake_listener callback */
775 int qtest_fault_resize_handshake(QTEST_FAULT *fault, size_t newlen)
778 size_t oldlen = fault->handbuflen;
781 * Alloc'd size should always be non-zero, so if this fails we've been
784 if (fault->handbufalloc == 0)
787 if (newlen > fault->handbufalloc) {
788 /* This exceeds our growth allowance. Fail */
792 buf = (unsigned char *)fault->handbuf;
794 if (newlen > oldlen) {
795 /* Extend packet with 0 bytes */
796 memset(buf + oldlen, 0, newlen - oldlen);
797 } /* else we're truncating or staying the same */
799 fault->handbuflen = newlen;
803 /* To be called from message specific listener callbacks */
804 int qtest_fault_resize_message(QTEST_FAULT *fault, size_t newlen)
806 /* First resize the underlying message */
807 if (!qtest_fault_resize_handshake(fault, newlen + SSL3_HM_HEADER_LENGTH))
810 /* Fixup the handshake message header */
811 fault->handbuf[1] = (unsigned char)((newlen >> 16) & 0xff);
812 fault->handbuf[2] = (unsigned char)((newlen >> 8) & 0xff);
813 fault->handbuf[3] = (unsigned char)((newlen ) & 0xff);
818 int qtest_fault_delete_extension(QTEST_FAULT *fault,
819 unsigned int exttype, unsigned char *ext,
822 PACKET pkt, sub, subext;
824 const unsigned char *start, *end;
826 size_t msglen = fault->handbuflen;
828 if (!PACKET_buf_init(&pkt, ext, *extlen))
831 /* Extension block starts with 2 bytes for extension block length */
832 if (!PACKET_as_length_prefixed_2(&pkt, &sub))
836 start = PACKET_data(&sub);
837 if (!PACKET_get_net_2(&sub, &type)
838 || !PACKET_get_length_prefixed_2(&sub, &subext))
840 } while (type != exttype);
843 end = PACKET_data(&sub);
846 * If we're not the last extension we need to move the rest earlier. The
847 * cast below is safe because we own the underlying buffer and we're no
848 * longer making PACKET calls.
850 if (end < ext + *extlen)
851 memmove((unsigned char *)start, end, end - start);
854 * Calculate new extensions payload length =
856 * - 2 extension block length bytes
857 * - length of removed extension
859 newlen = *extlen - 2 - (end - start);
861 /* Fixup the length bytes for the extension block */
862 ext[0] = (unsigned char)((newlen >> 8) & 0xff);
863 ext[1] = (unsigned char)((newlen ) & 0xff);
866 * Length of the whole extension block is the new payload length plus the
867 * 2 bytes for the length
869 *extlen = newlen + 2;
871 /* We can now resize the message */
872 if ((size_t)(end - start) + SSL3_HM_HEADER_LENGTH > msglen)
873 return 0; /* Should not happen */
874 msglen -= (end - start) + SSL3_HM_HEADER_LENGTH;
875 if (!qtest_fault_resize_message(fault, msglen))
881 #define BIO_TYPE_CIPHER_PACKET_FILTER (0x80 | BIO_TYPE_FILTER)
883 static BIO_METHOD *pcipherbiometh = NULL;
885 # define BIO_MSG_N(array, stride, n) (*(BIO_MSG *)((char *)(array) + (n)*(stride)))
887 static int pcipher_sendmmsg(BIO *b, BIO_MSG *msg, size_t stride,
888 size_t num_msg, uint64_t flags,
889 size_t *num_processed)
892 BIO *next = BIO_next(b);
893 ossl_ssize_t ret = 0;
894 size_t i = 0, tmpnump;
897 unsigned char *tmpdata;
902 fault = BIO_get_data(b);
904 || (fault->pciphercb == NULL && fault->datagramcb == NULL))
905 return BIO_sendmmsg(next, msg, stride, num_msg, flags, num_processed);
912 for (i = 0; i < num_msg; ++i) {
913 fault->msg = BIO_MSG_N(msg, stride, i);
915 /* Take a copy of the data so that callbacks can modify it */
916 tmpdata = OPENSSL_malloc(fault->msg.data_len + GROWTH_ALLOWANCE);
919 memcpy(tmpdata, fault->msg.data, fault->msg.data_len);
920 fault->msg.data = tmpdata;
921 fault->msgalloc = fault->msg.data_len + GROWTH_ALLOWANCE;
923 if (fault->pciphercb != NULL) {
924 if (!PACKET_buf_init(&pkt, fault->msg.data, fault->msg.data_len))
928 if (!ossl_quic_wire_decode_pkt_hdr(&pkt,
931 * Needs to be set to the actual short header CID length
932 * when testing the server implementation.
940 * hdr.data is const - but its our buffer so casting away the
943 if (!fault->pciphercb(fault, &hdr, (unsigned char *)hdr.data,
944 hdr.len, fault->pciphercbarg))
948 * At the moment modifications to hdr by the callback
949 * are ignored. We might need to rewrite the QUIC header to
950 * enable tests to change this. We also don't yet have a
951 * mechanism for the callback to change the encrypted data
952 * length. It's not clear if that's needed or not.
954 } while (PACKET_remaining(&pkt) > 0);
957 if (fault->datagramcb != NULL
958 && !fault->datagramcb(fault, &fault->msg, stride,
959 fault->datagramcbarg))
962 if (!BIO_sendmmsg(next, &fault->msg, stride, 1, flags, &tmpnump)) {
967 OPENSSL_free(fault->msg.data);
968 fault->msg.data = NULL;
975 OPENSSL_free(fault->msg.data);
976 fault->msg.data = NULL;
980 static long pcipher_ctrl(BIO *b, int cmd, long larg, void *parg)
982 BIO *next = BIO_next(b);
987 return BIO_ctrl(next, cmd, larg, parg);
990 BIO_METHOD *qtest_get_bio_method(void)
994 if (pcipherbiometh != NULL)
995 return pcipherbiometh;
997 tmp = BIO_meth_new(BIO_TYPE_CIPHER_PACKET_FILTER, "Cipher Packet Filter");
1002 if (!TEST_true(BIO_meth_set_sendmmsg(tmp, pcipher_sendmmsg))
1003 || !TEST_true(BIO_meth_set_ctrl(tmp, pcipher_ctrl)))
1006 pcipherbiometh = tmp;
1010 return pcipherbiometh;
1013 int qtest_fault_set_packet_cipher_listener(QTEST_FAULT *fault,
1014 qtest_fault_on_packet_cipher_cb pciphercb,
1017 fault->pciphercb = pciphercb;
1018 fault->pciphercbarg = pciphercbarg;
1023 int qtest_fault_set_datagram_listener(QTEST_FAULT *fault,
1024 qtest_fault_on_datagram_cb datagramcb,
1025 void *datagramcbarg)
1027 fault->datagramcb = datagramcb;
1028 fault->datagramcbarg = datagramcbarg;
1033 /* To be called from a datagram_listener callback */
1034 int qtest_fault_resize_datagram(QTEST_FAULT *fault, size_t newlen)
1036 if (newlen > fault->msgalloc)
1039 if (newlen > fault->msg.data_len)
1040 memset((unsigned char *)fault->msg.data + fault->msg.data_len, 0,
1041 newlen - fault->msg.data_len);
1043 fault->msg.data_len = newlen;
1048 /* There isn't a public function to do BIO_ADDR_copy() so we create one */
1049 int bio_addr_copy(BIO_ADDR *dst, BIO_ADDR *src)
1056 if (src == NULL || dst == NULL)
1059 family = BIO_ADDR_family(src);
1060 if (family == AF_UNSPEC) {
1061 BIO_ADDR_clear(dst);
1065 if (!BIO_ADDR_rawaddress(src, NULL, &len))
1069 data = OPENSSL_malloc(len);
1070 if (!TEST_ptr(data))
1074 if (!BIO_ADDR_rawaddress(src, data, &len))
1077 if (!BIO_ADDR_rawmake(src, family, data, len, BIO_ADDR_rawport(src)))
1086 int bio_msg_copy(BIO_MSG *dst, BIO_MSG *src)
1089 * Note it is assumed that the originally allocated data sizes for dst and
1092 memcpy(dst->data, src->data, src->data_len);
1093 dst->data_len = src->data_len;
1094 dst->flags = src->flags;
1095 if (dst->local != NULL) {
1096 if (src->local != NULL) {
1097 if (!TEST_true(bio_addr_copy(dst->local, src->local)))
1100 BIO_ADDR_clear(dst->local);
1103 if (!TEST_true(bio_addr_copy(dst->peer, src->peer)))