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/bio.h>
12 #include <openssl/rand.h>
14 #include "internal/sockets.h"
15 #include "internal/bio_addr.h"
17 #if !defined(OPENSSL_NO_DGRAM) && !defined(OPENSSL_NO_SOCK)
19 static int compare_addr(const BIO_ADDR *a, const BIO_ADDR *b)
21 struct in_addr xa, xb;
23 struct in6_addr xa6, xb6;
28 if (BIO_ADDR_family(a) != BIO_ADDR_family(b))
31 if (BIO_ADDR_family(a) == AF_INET) {
37 else if (BIO_ADDR_family(a) == AF_INET6) {
48 if (!TEST_int_eq(BIO_ADDR_rawaddress(a, pa, &tmplen), 1))
52 if (!TEST_int_eq(BIO_ADDR_rawaddress(b, pb, &tmplen), 1))
55 if (!TEST_mem_eq(pa, slen, pb, slen))
58 if (!TEST_int_eq(BIO_ADDR_rawport(a), BIO_ADDR_rawport(b)))
64 static int do_sendmmsg(BIO *b, BIO_MSG *msg,
65 size_t num_msg, uint64_t flags,
66 size_t *num_processed)
70 for (done = 0; done < num_msg; ) {
71 if (!BIO_sendmmsg(b, msg + done, sizeof(BIO_MSG),
72 num_msg - done, flags, num_processed))
75 done += *num_processed;
78 *num_processed = done;
82 static int do_recvmmsg(BIO *b, BIO_MSG *msg,
83 size_t num_msg, uint64_t flags,
84 size_t *num_processed)
88 for (done = 0; done < num_msg; ) {
89 if (!BIO_recvmmsg(b, msg + done, sizeof(BIO_MSG),
90 num_msg - done, flags, num_processed))
93 done += *num_processed;
96 *num_processed = done;
100 static int test_bio_dgram_impl(int af, int use_local)
103 BIO *b1 = NULL, *b2 = NULL;
104 int fd1 = -1, fd2 = -1;
105 BIO_ADDR *addr1 = NULL, *addr2 = NULL, *addr3 = NULL, *addr4 = NULL,
106 *addr5 = NULL, *addr6 = NULL;
109 struct in6_addr ina6;
113 union BIO_sock_info_u info1 = {0}, info2 = {0};
114 char rx_buf[128], rx_buf2[128];
115 BIO_MSG tx_msg[128], rx_msg[128];
117 size_t num_processed = 0;
120 TEST_info("# Testing with AF_INET, local=%d\n", use_local);
125 else if (af == AF_INET6) {
126 TEST_info("# Testing with AF_INET6, local=%d\n", use_local);
135 memset(pina, 0, inal);
136 ina.s_addr = htonl(0x7f000001UL);
138 ina6.s6_addr[15] = 1;
141 addr1 = BIO_ADDR_new();
142 if (!TEST_ptr(addr1))
145 addr2 = BIO_ADDR_new();
146 if (!TEST_ptr(addr2))
149 addr3 = BIO_ADDR_new();
150 if (!TEST_ptr(addr3))
153 addr4 = BIO_ADDR_new();
154 if (!TEST_ptr(addr4))
157 addr5 = BIO_ADDR_new();
158 if (!TEST_ptr(addr5))
161 addr6 = BIO_ADDR_new();
162 if (!TEST_ptr(addr6))
165 if (!TEST_int_eq(BIO_ADDR_rawmake(addr1, af, pina, inal, 0), 1))
168 if (!TEST_int_eq(BIO_ADDR_rawmake(addr2, af, pina, inal, 0), 1))
171 fd1 = BIO_socket(af, SOCK_DGRAM, IPPROTO_UDP, 0);
172 if (!TEST_int_ge(fd1, 0))
175 fd2 = BIO_socket(af, SOCK_DGRAM, IPPROTO_UDP, 0);
176 if (!TEST_int_ge(fd2, 0))
179 if (BIO_bind(fd1, addr1, 0) <= 0
180 || BIO_bind(fd2, addr2, 0) <= 0) {
181 testresult = TEST_skip("BIO_bind() failed - assuming it's an unavailable address family");
186 if (!TEST_int_gt(BIO_sock_info(fd1, BIO_SOCK_INFO_ADDRESS, &info1), 0))
190 if (!TEST_int_gt(BIO_sock_info(fd2, BIO_SOCK_INFO_ADDRESS, &info2), 0))
193 if (!TEST_int_gt(BIO_ADDR_rawport(addr1), 0))
196 if (!TEST_int_gt(BIO_ADDR_rawport(addr2), 0))
199 b1 = BIO_new_dgram(fd1, 0);
203 b2 = BIO_new_dgram(fd2, 0);
207 if (!TEST_int_gt(BIO_dgram_set_peer(b1, addr2), 0))
210 if (!TEST_int_gt(BIO_write(b1, "hello", 5), 0))
213 /* Receiving automatically sets peer as source addr */
214 if (!TEST_int_eq(BIO_read(b2, rx_buf, sizeof(rx_buf)), 5))
217 if (!TEST_mem_eq(rx_buf, 5, "hello", 5))
220 if (!TEST_int_gt(BIO_dgram_get_peer(b2, addr3), 0))
223 if (!TEST_int_eq(compare_addr(addr3, addr1), 1))
227 if (!TEST_int_gt(BIO_ADDR_rawmake(addr3, af, pina, inal, 0), 0))
230 if (!TEST_int_gt(BIO_dgram_set_peer(b1, addr3), 0))
233 if (!TEST_int_gt(BIO_dgram_set_peer(b2, addr3), 0))
236 /* Now test using sendmmsg/recvmmsg with no peer set */
237 tx_msg[0].data = "apple";
238 tx_msg[0].data_len = 5;
239 tx_msg[0].peer = NULL;
240 tx_msg[0].local = NULL;
243 tx_msg[1].data = "orange";
244 tx_msg[1].data_len = 6;
245 tx_msg[1].peer = NULL;
246 tx_msg[1].local = NULL;
249 /* First effort should fail due to missing destination address */
250 if (!TEST_false(do_sendmmsg(b1, tx_msg, 2, 0, &num_processed))
251 || !TEST_size_t_eq(num_processed, 0))
255 * Second effort should fail due to local being requested
258 tx_msg[0].peer = addr2;
259 tx_msg[0].local = addr1;
260 tx_msg[1].peer = addr2;
261 tx_msg[1].local = addr1;
262 if (!TEST_false(do_sendmmsg(b1, tx_msg, 2, 0, &num_processed)
263 || !TEST_size_t_eq(num_processed, 0)))
266 /* Enable local if we are using it */
267 if (BIO_dgram_get_local_addr_cap(b1) > 0 && use_local) {
268 if (!TEST_int_eq(BIO_dgram_set_local_addr_enable(b1, 1), 1))
271 tx_msg[0].local = NULL;
272 tx_msg[1].local = NULL;
276 /* Third effort should succeed */
277 if (!TEST_true(do_sendmmsg(b1, tx_msg, 2, 0, &num_processed))
278 || !TEST_size_t_eq(num_processed, 2))
281 /* Now try receiving */
282 rx_msg[0].data = rx_buf;
283 rx_msg[0].data_len = sizeof(rx_buf);
284 rx_msg[0].peer = addr3;
285 rx_msg[0].local = addr4;
286 rx_msg[0].flags = (1UL<<31); /* undefined flag, should be erased */
287 memset(rx_buf, 0, sizeof(rx_buf));
289 rx_msg[1].data = rx_buf2;
290 rx_msg[1].data_len = sizeof(rx_buf2);
291 rx_msg[1].peer = addr5;
292 rx_msg[1].local = addr6;
293 rx_msg[1].flags = (1UL<<31); /* undefined flag, should be erased */
294 memset(rx_buf2, 0, sizeof(rx_buf2));
297 * Should fail at first due to local being requested when not
300 if (!TEST_false(do_recvmmsg(b2, rx_msg, 2, 0, &num_processed))
301 || !TEST_size_t_eq(num_processed, 0))
304 /* Fields have not been modified */
305 if (!TEST_int_eq((int)rx_msg[0].data_len, sizeof(rx_buf)))
308 if (!TEST_int_eq((int)rx_msg[1].data_len, sizeof(rx_buf2)))
311 if (!TEST_ulong_eq((unsigned long)rx_msg[0].flags, 1UL<<31))
314 if (!TEST_ulong_eq((unsigned long)rx_msg[1].flags, 1UL<<31))
317 /* Enable local if we are using it */
318 if (BIO_dgram_get_local_addr_cap(b2) > 0 && use_local) {
319 if (!TEST_int_eq(BIO_dgram_set_local_addr_enable(b2, 1), 1))
322 rx_msg[0].local = NULL;
323 rx_msg[1].local = NULL;
327 /* Do the receive. */
328 if (!TEST_true(do_recvmmsg(b2, rx_msg, 2, 0, &num_processed))
329 || !TEST_size_t_eq(num_processed, 2))
332 /* data_len should have been updated correctly */
333 if (!TEST_int_eq((int)rx_msg[0].data_len, 5))
336 if (!TEST_int_eq((int)rx_msg[1].data_len, 6))
339 /* flags should have been zeroed */
340 if (!TEST_int_eq((int)rx_msg[0].flags, 0))
343 if (!TEST_int_eq((int)rx_msg[1].flags, 0))
346 /* peer address should match expected */
347 if (!TEST_int_eq(compare_addr(addr3, addr1), 1))
350 if (!TEST_int_eq(compare_addr(addr5, addr1), 1))
354 * Do not test local address yet as some platforms do not reliably return
355 * local addresses for messages queued for RX before local address support
356 * was enabled. Instead, send some new messages and test they're received
357 * with the correct local addresses.
359 if (!TEST_true(do_sendmmsg(b1, tx_msg, 2, 0, &num_processed))
360 || !TEST_size_t_eq(num_processed, 2))
363 /* Receive the messages. */
364 rx_msg[0].data_len = sizeof(rx_buf);
365 rx_msg[1].data_len = sizeof(rx_buf2);
367 if (!TEST_true(do_recvmmsg(b2, rx_msg, 2, 0, &num_processed))
368 || !TEST_size_t_eq(num_processed, 2))
371 if (rx_msg[0].local != NULL) {
372 /* If we are using local, it should match expected */
373 if (!TEST_int_eq(compare_addr(addr4, addr2), 1))
376 if (!TEST_int_eq(compare_addr(addr6, addr2), 1))
381 * Try sending more than can be handled in one sendmmsg call (when using the
382 * sendmmsg implementation)
384 for (i = 0; i < OSSL_NELEM(tx_msg); ++i) {
386 tx_msg[i].data = tx_buf + i;
387 tx_msg[i].data_len = 1;
388 tx_msg[i].peer = addr2;
389 tx_msg[i].local = use_local ? addr1 : NULL;
392 if (!TEST_true(do_sendmmsg(b1, tx_msg, OSSL_NELEM(tx_msg), 0, &num_processed))
393 || !TEST_size_t_eq(num_processed, OSSL_NELEM(tx_msg)))
397 * Try receiving more than can be handled in one recvmmsg call (when using
398 * the recvmmsg implementation)
400 for (i = 0; i < OSSL_NELEM(rx_msg); ++i) {
402 rx_msg[i].data = rx_buf + i;
403 rx_msg[i].data_len = 1;
404 rx_msg[i].peer = NULL;
405 rx_msg[i].local = NULL;
408 if (!TEST_true(do_recvmmsg(b2, rx_msg, OSSL_NELEM(rx_msg), 0, &num_processed))
409 || !TEST_size_t_eq(num_processed, OSSL_NELEM(rx_msg)))
412 if (!TEST_mem_eq(tx_buf, OSSL_NELEM(tx_msg), rx_buf, OSSL_NELEM(tx_msg)))
420 BIO_closesocket(fd1);
422 BIO_closesocket(fd2);
423 BIO_ADDR_free(addr1);
424 BIO_ADDR_free(addr2);
425 BIO_ADDR_free(addr3);
426 BIO_ADDR_free(addr4);
427 BIO_ADDR_free(addr5);
428 BIO_ADDR_free(addr6);
432 struct bio_dgram_case {
436 static const struct bio_dgram_case bio_dgram_cases[] = {
437 /* Test without local */
442 /* Test with local */
449 static int test_bio_dgram(int idx)
451 return test_bio_dgram_impl(bio_dgram_cases[idx].af,
452 bio_dgram_cases[idx].local);
455 # if !defined(OPENSSL_NO_CHACHA)
456 static int random_data(const uint32_t *key, uint8_t *data, size_t data_len, size_t offset)
459 EVP_CIPHER_CTX *ctx = NULL;
460 EVP_CIPHER *cipher = NULL;
461 static const uint8_t zeroes[2048];
462 uint32_t counter[4] = {0};
464 counter[0] = (uint32_t)offset;
466 ctx = EVP_CIPHER_CTX_new();
470 cipher = EVP_CIPHER_fetch(NULL, "ChaCha20", NULL);
474 if (EVP_EncryptInit_ex2(ctx, cipher, (uint8_t *)key, (uint8_t *)counter, NULL) == 0)
477 while (data_len > 0) {
478 outl = data_len > sizeof(zeroes) ? (int)sizeof(zeroes) : (int)data_len;
479 if (EVP_EncryptUpdate(ctx, data, &outl, zeroes, outl) != 1)
488 EVP_CIPHER_CTX_free(ctx);
489 EVP_CIPHER_free(cipher);
493 static int test_bio_dgram_pair(int idx)
495 int testresult = 0, blen, mtu1, mtu2, r;
496 BIO *bio1 = NULL, *bio2 = NULL;
497 uint8_t scratch[2048 + 4], scratch2[2048];
499 size_t i, num_dgram, num_processed = 0;
500 BIO_MSG msgs[2], rmsgs[2];
501 BIO_ADDR *addr1 = NULL, *addr2 = NULL, *addr3 = NULL, *addr4 = NULL;
502 struct in_addr in_local;
504 const uint32_t ref_caps = BIO_DGRAM_CAP_HANDLES_SRC_ADDR
505 | BIO_DGRAM_CAP_HANDLES_DST_ADDR
506 | BIO_DGRAM_CAP_PROVIDES_SRC_ADDR
507 | BIO_DGRAM_CAP_PROVIDES_DST_ADDR;
509 memset(msgs, 0, sizeof(msgs));
510 memset(rmsgs, 0, sizeof(rmsgs));
512 in_local.s_addr = ntohl(0x7f000001);
514 for (i = 0; i < OSSL_NELEM(key); ++i)
515 key[i] = test_random();
518 if (!TEST_int_eq(BIO_new_bio_dgram_pair(&bio1, 0, &bio2, 0), 1))
521 if (!TEST_ptr(bio1 = bio2 = BIO_new(BIO_s_dgram_mem())))
525 mtu1 = BIO_dgram_get_mtu(bio1);
526 if (!TEST_int_ge(mtu1, 1280))
533 * Assume the header contains 2 BIO_ADDR structures and a length. We
534 * set a buffer big enough for 9 full sized datagrams.
536 bufsz = 9 * (mtu1 + (sizeof(BIO_ADDR) * 2) + sizeof(size_t));
537 if (!TEST_true(BIO_set_write_buf_size(bio1, bufsz)))
541 mtu2 = BIO_dgram_get_mtu(bio2);
542 if (!TEST_int_ge(mtu2, 1280))
545 if (!TEST_int_eq(mtu1, mtu2))
548 if (!TEST_int_le(mtu1, sizeof(scratch) - 4))
551 for (i = 0; total < 1 * 1024 * 1024; ++i) {
552 if (!TEST_int_eq(random_data(key, scratch, sizeof(scratch), i), 1))
555 blen = ((*(uint32_t*)scratch) % mtu1) + 1;
556 r = BIO_write(bio1, scratch + 4, blen);
560 if (!TEST_int_eq(r, blen))
566 if (idx <= 1 && !TEST_size_t_lt(total, 1 * 1024 * 1024))
569 if (idx == 2 && !TEST_size_t_ge(total, 1 * 1024 * 1024))
573 * The number of datagrams we can fit depends on the size of the default
574 * write buffer size, the size of the datagram header and the size of the
575 * payload data we send in each datagram. The max payload data is based on
576 * the mtu. The default write buffer size is 9 * (sizeof(header) + mtu) so
577 * we expect at least 9 maximally sized datagrams to fit in the buffer.
579 if (!TEST_int_ge(i, 9))
582 /* Check we read back the same data */
584 for (i = 0; i < num_dgram; ++i) {
585 if (!TEST_int_eq(random_data(key, scratch, sizeof(scratch), i), 1))
588 blen = ((*(uint32_t*)scratch) % mtu1) + 1;
589 r = BIO_read(bio2, scratch2, sizeof(scratch2));
590 if (!TEST_int_eq(r, blen))
593 if (!TEST_mem_eq(scratch + 4, blen, scratch2, blen))
597 /* Should now be out of data */
598 if (!TEST_int_eq(BIO_read(bio2, scratch2, sizeof(scratch2)), -1))
601 /* sendmmsg/recvmmsg */
602 if (!TEST_int_eq(random_data(key, scratch, sizeof(scratch), 0), 1))
605 msgs[0].data = scratch;
606 msgs[0].data_len = 19;
607 msgs[1].data = scratch + 19;
608 msgs[1].data_len = 46;
610 if (!TEST_true(BIO_sendmmsg(bio1, msgs, sizeof(BIO_MSG), OSSL_NELEM(msgs), 0,
612 || !TEST_size_t_eq(num_processed, 2))
615 rmsgs[0].data = scratch2;
616 rmsgs[0].data_len = 64;
617 rmsgs[1].data = scratch2 + 64;
618 rmsgs[1].data_len = 64;
619 if (!TEST_true(BIO_recvmmsg(bio2, rmsgs, sizeof(BIO_MSG), OSSL_NELEM(rmsgs), 0,
621 || !TEST_size_t_eq(num_processed, 2))
624 if (!TEST_mem_eq(rmsgs[0].data, rmsgs[0].data_len, scratch, 19))
627 if (!TEST_mem_eq(rmsgs[1].data, rmsgs[1].data_len, scratch + 19, 46))
630 /* sendmmsg/recvmmsg with peer */
631 addr1 = BIO_ADDR_new();
632 if (!TEST_ptr(addr1))
635 if (!TEST_int_eq(BIO_ADDR_rawmake(addr1, AF_INET, &in_local,
636 sizeof(in_local), 1234), 1))
639 addr2 = BIO_ADDR_new();
640 if (!TEST_ptr(addr2))
643 if (!TEST_int_eq(BIO_ADDR_rawmake(addr2, AF_INET, &in_local,
644 sizeof(in_local), 2345), 1))
647 addr3 = BIO_ADDR_new();
648 if (!TEST_ptr(addr3))
651 addr4 = BIO_ADDR_new();
652 if (!TEST_ptr(addr4))
655 msgs[0].peer = addr1;
657 /* fails due to lack of caps on peer */
658 if (!TEST_false(BIO_sendmmsg(bio1, msgs, sizeof(BIO_MSG),
659 OSSL_NELEM(msgs), 0, &num_processed))
660 || !TEST_size_t_eq(num_processed, 0))
663 if (!TEST_int_eq(BIO_dgram_set_caps(bio2, ref_caps), 1))
666 if (!TEST_int_eq(BIO_dgram_get_caps(bio2), ref_caps))
669 if (!TEST_int_eq(BIO_dgram_get_effective_caps(bio1), ref_caps))
672 if (idx == 0 && !TEST_int_eq(BIO_dgram_get_effective_caps(bio2), 0))
675 if (!TEST_int_eq(BIO_dgram_set_caps(bio1, ref_caps), 1))
678 /* succeeds with cap now available */
679 if (!TEST_true(BIO_sendmmsg(bio1, msgs, sizeof(BIO_MSG), 1, 0, &num_processed))
680 || !TEST_size_t_eq(num_processed, 1))
683 /* enable local addr support */
684 if (!TEST_int_eq(BIO_dgram_set_local_addr_enable(bio2, 1), 1))
687 rmsgs[0].data = scratch2;
688 rmsgs[0].data_len = 64;
689 rmsgs[0].peer = addr3;
690 rmsgs[0].local = addr4;
691 if (!TEST_true(BIO_recvmmsg(bio2, rmsgs, sizeof(BIO_MSG), OSSL_NELEM(rmsgs), 0,
693 || !TEST_size_t_eq(num_processed, 1))
696 if (!TEST_mem_eq(rmsgs[0].data, rmsgs[0].data_len, msgs[0].data, 19))
699 /* We didn't set the source address so this should be zero */
700 if (!TEST_int_eq(BIO_ADDR_family(addr3), 0))
703 if (!TEST_int_eq(BIO_ADDR_family(addr4), AF_INET))
706 if (!TEST_int_eq(BIO_ADDR_rawport(addr4), 1234))
709 /* test source address */
710 msgs[0].local = addr2;
712 if (!TEST_int_eq(BIO_dgram_set_local_addr_enable(bio1, 1), 1))
715 if (!TEST_true(BIO_sendmmsg(bio1, msgs, sizeof(BIO_MSG), 1, 0, &num_processed))
716 || !TEST_size_t_eq(num_processed, 1))
719 rmsgs[0].data = scratch2;
720 rmsgs[0].data_len = 64;
721 if (!TEST_true(BIO_recvmmsg(bio2, rmsgs, sizeof(BIO_MSG), OSSL_NELEM(rmsgs), 0, &num_processed))
722 || !TEST_size_t_eq(num_processed, 1))
725 if (!TEST_mem_eq(rmsgs[0].data, rmsgs[0].data_len,
726 msgs[0].data, msgs[0].data_len))
729 if (!TEST_int_eq(BIO_ADDR_family(addr3), AF_INET))
732 if (!TEST_int_eq(BIO_ADDR_rawport(addr3), 2345))
735 if (!TEST_int_eq(BIO_ADDR_family(addr4), AF_INET))
738 if (!TEST_int_eq(BIO_ADDR_rawport(addr4), 1234))
741 /* test truncation, pending */
742 r = BIO_write(bio1, scratch, 64);
743 if (!TEST_int_eq(r, 64))
746 memset(scratch2, 0, 64);
747 if (!TEST_int_eq(BIO_dgram_set_no_trunc(bio2, 1), 1))
750 if (!TEST_int_eq(BIO_read(bio2, scratch2, 32), -1))
753 if (!TEST_int_eq(BIO_pending(bio2), 64))
756 if (!TEST_int_eq(BIO_dgram_set_no_trunc(bio2, 0), 1))
759 if (!TEST_int_eq(BIO_read(bio2, scratch2, 32), 32))
762 if (!TEST_mem_eq(scratch, 32, scratch2, 32))
770 BIO_ADDR_free(addr1);
771 BIO_ADDR_free(addr2);
772 BIO_ADDR_free(addr3);
773 BIO_ADDR_free(addr4);
776 # endif /* !defined(OPENSSL_NO_CHACHA) */
777 #endif /* !defined(OPENSSL_NO_DGRAM) && !defined(OPENSSL_NO_SOCK) */
779 int setup_tests(void)
781 if (!test_skip_common_options()) {
782 TEST_error("Error parsing test options\n");
786 #if !defined(OPENSSL_NO_DGRAM) && !defined(OPENSSL_NO_SOCK)
787 ADD_ALL_TESTS(test_bio_dgram, OSSL_NELEM(bio_dgram_cases));
788 # if !defined(OPENSSL_NO_CHACHA)
789 ADD_ALL_TESTS(test_bio_dgram_pair, 3);