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 <openssl/rand.h>
14 #include "internal/sockets.h"
16 #if !defined(OPENSSL_NO_DGRAM) && !defined(OPENSSL_NO_SOCK)
18 static int compare_addr(const BIO_ADDR *a, const BIO_ADDR *b)
20 struct in_addr xa, xb;
22 struct in6_addr xa6, xb6;
27 if (BIO_ADDR_family(a) != BIO_ADDR_family(b))
30 if (BIO_ADDR_family(a) == AF_INET) {
36 else if (BIO_ADDR_family(a) == AF_INET6) {
47 if (!TEST_int_eq(BIO_ADDR_rawaddress(a, pa, &tmplen), 1))
51 if (!TEST_int_eq(BIO_ADDR_rawaddress(b, pb, &tmplen), 1))
54 if (!TEST_mem_eq(pa, slen, pb, slen))
57 if (!TEST_int_eq(BIO_ADDR_rawport(a), BIO_ADDR_rawport(b)))
63 static int do_sendmmsg(BIO *b, BIO_MSG *msg,
64 size_t num_msg, uint64_t flags,
65 size_t *num_processed)
69 for (done = 0; done < num_msg; ) {
70 if (!BIO_sendmmsg(b, msg + done, sizeof(BIO_MSG),
71 num_msg - done, flags, num_processed))
74 done += *num_processed;
77 *num_processed = done;
81 static int do_recvmmsg(BIO *b, BIO_MSG *msg,
82 size_t num_msg, uint64_t flags,
83 size_t *num_processed)
87 for (done = 0; done < num_msg; ) {
88 if (!BIO_recvmmsg(b, msg + done, sizeof(BIO_MSG),
89 num_msg - done, flags, num_processed))
92 done += *num_processed;
95 *num_processed = done;
99 static int test_bio_dgram_impl(int af, int use_local)
102 BIO *b1 = NULL, *b2 = NULL;
103 int fd1 = -1, fd2 = -1;
104 BIO_ADDR *addr1 = NULL, *addr2 = NULL, *addr3 = NULL, *addr4 = NULL,
105 *addr5 = NULL, *addr6 = NULL;
108 struct in6_addr ina6;
112 union BIO_sock_info_u info1 = {0}, info2 = {0};
113 char rx_buf[128], rx_buf2[128];
114 BIO_MSG tx_msg[128], rx_msg[128];
116 size_t num_processed = 0;
119 TEST_info("# Testing with AF_INET, local=%d\n", use_local);
124 else if (af == AF_INET6) {
125 TEST_info("# Testing with AF_INET6, local=%d\n", use_local);
134 memset(pina, 0, inal);
135 ina.s_addr = htonl(0x7f000001UL);
137 ina6.s6_addr[15] = 1;
140 addr1 = BIO_ADDR_new();
141 if (!TEST_ptr(addr1))
144 addr2 = BIO_ADDR_new();
145 if (!TEST_ptr(addr2))
148 addr3 = BIO_ADDR_new();
149 if (!TEST_ptr(addr3))
152 addr4 = BIO_ADDR_new();
153 if (!TEST_ptr(addr4))
156 addr5 = BIO_ADDR_new();
157 if (!TEST_ptr(addr5))
160 addr6 = BIO_ADDR_new();
161 if (!TEST_ptr(addr6))
164 if (!TEST_int_eq(BIO_ADDR_rawmake(addr1, af, pina, inal, 0), 1))
167 if (!TEST_int_eq(BIO_ADDR_rawmake(addr2, af, pina, inal, 0), 1))
170 fd1 = BIO_socket(af, SOCK_DGRAM, IPPROTO_UDP, 0);
171 if (!TEST_int_ge(fd1, 0))
174 fd2 = BIO_socket(af, SOCK_DGRAM, IPPROTO_UDP, 0);
175 if (!TEST_int_ge(fd2, 0))
178 if (!TEST_int_gt(BIO_bind(fd1, addr1, 0), 0))
181 if (!TEST_int_gt(BIO_bind(fd2, addr2, 0), 0))
185 if (!TEST_int_gt(BIO_sock_info(fd1, BIO_SOCK_INFO_ADDRESS, &info1), 0))
189 if (!TEST_int_gt(BIO_sock_info(fd2, BIO_SOCK_INFO_ADDRESS, &info2), 0))
192 if (!TEST_int_gt(BIO_ADDR_rawport(addr1), 0))
195 if (!TEST_int_gt(BIO_ADDR_rawport(addr2), 0))
198 b1 = BIO_new_dgram(fd1, 0);
202 b2 = BIO_new_dgram(fd2, 0);
206 if (!TEST_int_gt(BIO_dgram_set_peer(b1, addr2), 0))
209 if (!TEST_int_gt(BIO_write(b1, "hello", 5), 0))
212 /* Receiving automatically sets peer as source addr */
213 if (!TEST_int_eq(BIO_read(b2, rx_buf, sizeof(rx_buf)), 5))
216 if (!TEST_mem_eq(rx_buf, 5, "hello", 5))
219 if (!TEST_int_gt(BIO_dgram_get_peer(b2, addr3), 0))
222 if (!TEST_int_eq(compare_addr(addr3, addr1), 1))
226 if (!TEST_int_gt(BIO_ADDR_rawmake(addr3, af, pina, inal, 0), 0))
229 if (!TEST_int_gt(BIO_dgram_set_peer(b1, addr3), 0))
232 if (!TEST_int_gt(BIO_dgram_set_peer(b2, addr3), 0))
235 /* Now test using sendmmsg/recvmmsg with no peer set */
236 tx_msg[0].data = "apple";
237 tx_msg[0].data_len = 5;
238 tx_msg[0].peer = NULL;
239 tx_msg[0].local = NULL;
242 tx_msg[1].data = "orange";
243 tx_msg[1].data_len = 6;
244 tx_msg[1].peer = NULL;
245 tx_msg[1].local = NULL;
248 /* First effort should fail due to missing destination address */
249 if (!TEST_false(do_sendmmsg(b1, tx_msg, 2, 0, &num_processed))
250 || !TEST_size_t_eq(num_processed, 0))
254 * Second effort should fail due to local being requested
257 tx_msg[0].peer = addr2;
258 tx_msg[0].local = addr1;
259 tx_msg[1].peer = addr2;
260 tx_msg[1].local = addr1;
261 if (!TEST_false(do_sendmmsg(b1, tx_msg, 2, 0, &num_processed)
262 || !TEST_size_t_eq(num_processed, 0)))
265 /* Enable local if we are using it */
266 if (BIO_dgram_get_local_addr_cap(b1) > 0 && use_local) {
267 if (!TEST_int_eq(BIO_dgram_set_local_addr_enable(b1, 1), 1))
270 tx_msg[0].local = NULL;
271 tx_msg[1].local = NULL;
275 /* Third effort should succeed */
276 if (!TEST_true(do_sendmmsg(b1, tx_msg, 2, 0, &num_processed))
277 || !TEST_size_t_eq(num_processed, 2))
280 /* Now try receiving */
281 rx_msg[0].data = rx_buf;
282 rx_msg[0].data_len = sizeof(rx_buf);
283 rx_msg[0].peer = addr3;
284 rx_msg[0].local = addr4;
285 rx_msg[0].flags = (1UL<<31); /* undefined flag, should be erased */
286 memset(rx_buf, 0, sizeof(rx_buf));
288 rx_msg[1].data = rx_buf2;
289 rx_msg[1].data_len = sizeof(rx_buf2);
290 rx_msg[1].peer = addr5;
291 rx_msg[1].local = addr6;
292 rx_msg[1].flags = (1UL<<31); /* undefined flag, should be erased */
293 memset(rx_buf2, 0, sizeof(rx_buf2));
296 * Should fail at first due to local being requested when not
299 if (!TEST_false(do_recvmmsg(b2, rx_msg, 2, 0, &num_processed))
300 || !TEST_size_t_eq(num_processed, 0))
303 /* Fields have not been modified */
304 if (!TEST_int_eq((int)rx_msg[0].data_len, sizeof(rx_buf)))
307 if (!TEST_int_eq((int)rx_msg[1].data_len, sizeof(rx_buf2)))
310 if (!TEST_ulong_eq((unsigned long)rx_msg[0].flags, 1UL<<31))
313 if (!TEST_ulong_eq((unsigned long)rx_msg[1].flags, 1UL<<31))
316 /* Enable local if we are using it */
317 if (BIO_dgram_get_local_addr_cap(b2) > 0 && use_local) {
318 if (!TEST_int_eq(BIO_dgram_set_local_addr_enable(b2, 1), 1))
321 rx_msg[0].local = NULL;
322 rx_msg[1].local = NULL;
326 /* Do the receive. */
327 if (!TEST_true(do_recvmmsg(b2, rx_msg, 2, 0, &num_processed))
328 || !TEST_size_t_eq(num_processed, 2))
331 /* data_len should have been updated correctly */
332 if (!TEST_int_eq((int)rx_msg[0].data_len, 5))
335 if (!TEST_int_eq((int)rx_msg[1].data_len, 6))
338 /* flags should have been zeroed */
339 if (!TEST_int_eq((int)rx_msg[0].flags, 0))
342 if (!TEST_int_eq((int)rx_msg[1].flags, 0))
345 /* peer address should match expected */
346 if (!TEST_int_eq(compare_addr(addr3, addr1), 1))
349 if (!TEST_int_eq(compare_addr(addr5, addr1), 1))
353 * Do not test local address yet as some platforms do not reliably return
354 * local addresses for messages queued for RX before local address support
355 * was enabled. Instead, send some new messages and test they're received
356 * with the correct local addresses.
358 if (!TEST_true(do_sendmmsg(b1, tx_msg, 2, 0, &num_processed))
359 || !TEST_size_t_eq(num_processed, 2))
362 /* Receive the messages. */
363 rx_msg[0].data_len = sizeof(rx_buf);
364 rx_msg[1].data_len = sizeof(rx_buf2);
366 if (!TEST_true(do_recvmmsg(b2, rx_msg, 2, 0, &num_processed))
367 || !TEST_size_t_eq(num_processed, 2))
370 if (rx_msg[0].local != NULL) {
371 /* If we are using local, it should match expected */
372 if (!TEST_int_eq(compare_addr(addr4, addr2), 1))
375 if (!TEST_int_eq(compare_addr(addr6, addr2), 1))
380 * Try sending more than can be handled in one sendmmsg call (when using the
381 * sendmmsg implementation)
383 for (i = 0; i < OSSL_NELEM(tx_msg); ++i) {
385 tx_msg[i].data = tx_buf + i;
386 tx_msg[i].data_len = 1;
387 tx_msg[i].peer = addr2;
388 tx_msg[i].local = use_local ? addr1 : NULL;
391 if (!TEST_true(do_sendmmsg(b1, tx_msg, OSSL_NELEM(tx_msg), 0, &num_processed))
392 || !TEST_size_t_eq(num_processed, OSSL_NELEM(tx_msg)))
396 * Try receiving more than can be handled in one recvmmsg call (when using
397 * the recvmmsg implementation)
399 for (i = 0; i < OSSL_NELEM(rx_msg); ++i) {
401 rx_msg[i].data = rx_buf + i;
402 rx_msg[i].data_len = 1;
403 rx_msg[i].peer = NULL;
404 rx_msg[i].local = NULL;
407 if (!TEST_true(do_recvmmsg(b2, rx_msg, OSSL_NELEM(rx_msg), 0, &num_processed))
408 || !TEST_size_t_eq(num_processed, OSSL_NELEM(rx_msg)))
411 if (!TEST_mem_eq(tx_buf, OSSL_NELEM(tx_msg), rx_buf, OSSL_NELEM(tx_msg)))
419 BIO_closesocket(fd1);
421 BIO_closesocket(fd2);
422 BIO_ADDR_free(addr1);
423 BIO_ADDR_free(addr2);
424 BIO_ADDR_free(addr3);
425 BIO_ADDR_free(addr4);
426 BIO_ADDR_free(addr5);
427 BIO_ADDR_free(addr6);
431 struct bio_dgram_case {
435 static const struct bio_dgram_case bio_dgram_cases[] = {
436 /* Test without local */
441 /* Test with local */
448 static int test_bio_dgram(int idx)
450 return test_bio_dgram_impl(bio_dgram_cases[idx].af,
451 bio_dgram_cases[idx].local);
454 # if !defined(OPENSSL_NO_CHACHA)
455 static int random_data(const uint32_t *key, uint8_t *data, size_t data_len, size_t offset)
458 EVP_CIPHER_CTX *ctx = NULL;
459 EVP_CIPHER *cipher = NULL;
460 static const uint8_t zeroes[2048];
461 uint32_t counter[4] = {0};
463 counter[0] = (uint32_t)offset;
465 ctx = EVP_CIPHER_CTX_new();
469 cipher = EVP_CIPHER_fetch(NULL, "ChaCha20", NULL);
473 if (EVP_EncryptInit_ex2(ctx, cipher, (uint8_t *)key, (uint8_t *)counter, NULL) == 0)
476 while (data_len > 0) {
477 outl = data_len > sizeof(zeroes) ? (int)sizeof(zeroes) : (int)data_len;
478 if (EVP_EncryptUpdate(ctx, data, &outl, zeroes, outl) != 1)
487 EVP_CIPHER_CTX_free(ctx);
488 EVP_CIPHER_free(cipher);
492 static int test_bio_dgram_pair(void)
494 int testresult = 0, blen, mtu1, mtu2, r;
495 BIO *bio1 = NULL, *bio2 = NULL;
496 uint8_t scratch[2048 + 4], scratch2[2048];
498 size_t i, num_dgram, num_processed = 0;
499 BIO_MSG msgs[2], rmsgs[2];
500 BIO_ADDR *addr1 = NULL, *addr2 = NULL, *addr3 = NULL, *addr4 = NULL;
501 struct in_addr in_local;
503 const uint32_t ref_caps = BIO_DGRAM_CAP_HANDLES_SRC_ADDR
504 | BIO_DGRAM_CAP_HANDLES_DST_ADDR
505 | BIO_DGRAM_CAP_PROVIDES_SRC_ADDR
506 | BIO_DGRAM_CAP_PROVIDES_DST_ADDR;
508 memset(msgs, 0, sizeof(msgs));
509 memset(rmsgs, 0, sizeof(rmsgs));
511 in_local.s_addr = ntohl(0x7f000001);
513 for (i = 0; i < OSSL_NELEM(key); ++i)
514 key[i] = test_random();
516 if (!TEST_int_eq(BIO_new_bio_dgram_pair(&bio1, 0, &bio2, 0), 1))
519 mtu1 = BIO_dgram_get_mtu(bio1);
520 if (!TEST_int_ge(mtu1, 1280))
523 mtu2 = BIO_dgram_get_mtu(bio2);
524 if (!TEST_int_ge(mtu2, 1280))
527 if (!TEST_int_eq(mtu1, mtu2))
530 if (!TEST_int_le(mtu1, sizeof(scratch) - 4))
534 if (!TEST_int_eq(random_data(key, scratch, sizeof(scratch), i), 1))
537 blen = ((*(uint32_t*)scratch) % mtu1) + 1;
538 r = BIO_write(bio1, scratch + 4, blen);
542 if (!TEST_int_eq(r, blen))
546 if (!TEST_size_t_lt(total, 1 * 1024 * 1024))
551 * Should be able to fit at least 9 datagrams in default write buffer size
554 if (!TEST_int_ge(i, 9))
557 /* Check we read back the same data */
559 for (i = 0; i < num_dgram; ++i) {
560 if (!TEST_int_eq(random_data(key, scratch, sizeof(scratch), i), 1))
563 blen = ((*(uint32_t*)scratch) % mtu1) + 1;
564 r = BIO_read(bio2, scratch2, sizeof(scratch2));
565 if (!TEST_int_eq(r, blen))
568 if (!TEST_mem_eq(scratch + 4, blen, scratch2, blen))
572 /* Should now be out of data */
573 if (!TEST_int_eq(BIO_read(bio2, scratch2, sizeof(scratch2)), -1))
576 /* sendmmsg/recvmmsg */
577 if (!TEST_int_eq(random_data(key, scratch, sizeof(scratch), 0), 1))
580 msgs[0].data = scratch;
581 msgs[0].data_len = 19;
582 msgs[1].data = scratch + 19;
583 msgs[1].data_len = 46;
585 if (!TEST_true(BIO_sendmmsg(bio1, msgs, sizeof(BIO_MSG), OSSL_NELEM(msgs), 0,
587 || !TEST_size_t_eq(num_processed, 2))
590 rmsgs[0].data = scratch2;
591 rmsgs[0].data_len = 64;
592 rmsgs[1].data = scratch2 + 64;
593 rmsgs[1].data_len = 64;
594 if (!TEST_true(BIO_recvmmsg(bio2, rmsgs, sizeof(BIO_MSG), OSSL_NELEM(rmsgs), 0,
596 || !TEST_size_t_eq(num_processed, 2))
599 if (!TEST_mem_eq(rmsgs[0].data, rmsgs[0].data_len, scratch, 19))
602 if (!TEST_mem_eq(rmsgs[1].data, rmsgs[1].data_len, scratch + 19, 46))
605 /* sendmmsg/recvmmsg with peer */
606 addr1 = BIO_ADDR_new();
607 if (!TEST_ptr(addr1))
610 if (!TEST_int_eq(BIO_ADDR_rawmake(addr1, AF_INET, &in_local,
611 sizeof(in_local), 1234), 1))
614 addr2 = BIO_ADDR_new();
615 if (!TEST_ptr(addr2))
618 if (!TEST_int_eq(BIO_ADDR_rawmake(addr2, AF_INET, &in_local,
619 sizeof(in_local), 2345), 1))
622 addr3 = BIO_ADDR_new();
623 if (!TEST_ptr(addr3))
626 addr4 = BIO_ADDR_new();
627 if (!TEST_ptr(addr4))
630 msgs[0].peer = addr1;
632 /* fails due to lack of caps on peer */
633 if (!TEST_false(BIO_sendmmsg(bio1, msgs, sizeof(BIO_MSG), OSSL_NELEM(msgs),
635 || !TEST_size_t_eq(num_processed, 0))
638 if (!TEST_int_eq(BIO_dgram_set_caps(bio2, ref_caps), 1))
641 if (!TEST_int_eq(BIO_dgram_get_caps(bio2), ref_caps))
644 if (!TEST_int_eq(BIO_dgram_get_effective_caps(bio1), ref_caps))
647 if (!TEST_int_eq(BIO_dgram_get_effective_caps(bio2), 0))
650 if (!TEST_int_eq(BIO_dgram_set_caps(bio1, ref_caps), 1))
653 /* succeeds with cap now available */
654 if (!TEST_true(BIO_sendmmsg(bio1, msgs, sizeof(BIO_MSG), 1, 0, &num_processed))
655 || !TEST_size_t_eq(num_processed, 1))
658 /* enable local addr support */
659 if (!TEST_int_eq(BIO_dgram_set_local_addr_enable(bio2, 1), 1))
662 rmsgs[0].data = scratch2;
663 rmsgs[0].data_len = 64;
664 rmsgs[0].peer = addr3;
665 rmsgs[0].local = addr4;
666 if (!TEST_true(BIO_recvmmsg(bio2, rmsgs, sizeof(BIO_MSG), OSSL_NELEM(rmsgs), 0,
668 || !TEST_size_t_eq(num_processed, 1))
671 if (!TEST_mem_eq(rmsgs[0].data, rmsgs[0].data_len, msgs[0].data, 19))
674 /* We didn't set the source address so this should be zero */
675 if (!TEST_int_eq(BIO_ADDR_family(addr3), 0))
678 if (!TEST_int_eq(BIO_ADDR_family(addr4), AF_INET))
681 if (!TEST_int_eq(BIO_ADDR_rawport(addr4), 1234))
684 /* test source address */
685 msgs[0].local = addr2;
687 if (!TEST_int_eq(BIO_dgram_set_local_addr_enable(bio1, 1), 1))
690 if (!TEST_true(BIO_sendmmsg(bio1, msgs, sizeof(BIO_MSG), 1, 0, &num_processed))
691 || !TEST_size_t_eq(num_processed, 1))
694 rmsgs[0].data = scratch2;
695 rmsgs[0].data_len = 64;
696 if (!TEST_true(BIO_recvmmsg(bio2, rmsgs, sizeof(BIO_MSG), OSSL_NELEM(rmsgs), 0, &num_processed))
697 || !TEST_size_t_eq(num_processed, 1))
700 if (!TEST_mem_eq(rmsgs[0].data, rmsgs[0].data_len,
701 msgs[0].data, msgs[0].data_len))
704 if (!TEST_int_eq(BIO_ADDR_family(addr3), AF_INET))
707 if (!TEST_int_eq(BIO_ADDR_rawport(addr3), 2345))
710 if (!TEST_int_eq(BIO_ADDR_family(addr4), AF_INET))
713 if (!TEST_int_eq(BIO_ADDR_rawport(addr4), 1234))
716 /* test truncation, pending */
717 r = BIO_write(bio1, scratch, 64);
718 if (!TEST_int_eq(r, 64))
721 memset(scratch2, 0, 64);
722 if (!TEST_int_eq(BIO_dgram_set_no_trunc(bio2, 1), 1))
725 if (!TEST_int_eq(BIO_read(bio2, scratch2, 32), -1))
728 if (!TEST_int_eq(BIO_pending(bio2), 64))
731 if (!TEST_int_eq(BIO_dgram_set_no_trunc(bio2, 0), 1))
734 if (!TEST_int_eq(BIO_read(bio2, scratch2, 32), 32))
737 if (!TEST_mem_eq(scratch, 32, scratch2, 32))
744 BIO_ADDR_free(addr1);
745 BIO_ADDR_free(addr2);
746 BIO_ADDR_free(addr3);
747 BIO_ADDR_free(addr4);
750 # endif /* !defined(OPENSSL_NO_CHACHA) */
751 #endif /* !defined(OPENSSL_NO_DGRAM) && !defined(OPENSSL_NO_SOCK) */
753 int setup_tests(void)
755 if (!test_skip_common_options()) {
756 TEST_error("Error parsing test options\n");
760 #if !defined(OPENSSL_NO_DGRAM) && !defined(OPENSSL_NO_SOCK)
761 ADD_ALL_TESTS(test_bio_dgram, OSSL_NELEM(bio_dgram_cases));
762 # if !defined(OPENSSL_NO_CHACHA)
763 ADD_TEST(test_bio_dgram_pair);