1 1.1 christos /* 2 1.1 christos * Copyright 2023 The OpenSSL Project Authors. All Rights Reserved. 3 1.1 christos * 4 1.1 christos * Licensed under the Apache License 2.0 (the "License"). You may not use 5 1.1 christos * this file except in compliance with the License. You can obtain a copy 6 1.1 christos * in the file LICENSE in the source distribution or at 7 1.1 christos * https://www.openssl.org/source/license.html 8 1.1 christos */ 9 1.1 christos 10 1.1 christos #include <openssl/bio.h> 11 1.1 christos #include "internal/e_os.h" 12 1.1 christos #include "internal/sockets.h" 13 1.1 christos #include "testutil.h" 14 1.1 christos 15 1.1 christos static int families[] = { 16 1.1 christos AF_INET, 17 1.1 christos #if OPENSSL_USE_IPV6 18 1.1 christos AF_INET6, 19 1.1 christos #endif 20 1.1 christos #ifndef OPENSSL_NO_UNIX_SOCK 21 1.1 christos AF_UNIX 22 1.1 christos #endif 23 1.1 christos }; 24 1.1 christos 25 1.1 christos static BIO_ADDR *make_dummy_addr(int family) 26 1.1 christos { 27 1.1 christos BIO_ADDR *addr; 28 1.1 christos union { 29 1.1 christos struct sockaddr_in sin; 30 1.1 christos #if OPENSSL_USE_IPV6 31 1.1 christos struct sockaddr_in6 sin6; 32 1.1 christos #endif 33 1.1 christos #ifndef OPENSSL_NO_UNIX_SOCK 34 1.1 christos struct sockaddr_un sunaddr; 35 1.1 christos #endif 36 1.1 christos } sa; 37 1.1 christos void *where; 38 1.1 christos size_t wherelen; 39 1.1 christos 40 1.1 christos /* Fill with a dummy address */ 41 1.1.1.2 christos switch (family) { 42 1.1 christos case AF_INET: 43 1.1 christos where = &(sa.sin.sin_addr); 44 1.1 christos wherelen = sizeof(sa.sin.sin_addr); 45 1.1 christos break; 46 1.1 christos #if OPENSSL_USE_IPV6 47 1.1 christos case AF_INET6: 48 1.1 christos where = &(sa.sin6.sin6_addr); 49 1.1 christos wherelen = sizeof(sa.sin6.sin6_addr); 50 1.1 christos break; 51 1.1 christos #endif 52 1.1 christos #ifndef OPENSSL_NO_UNIX_SOCK 53 1.1 christos case AF_UNIX: 54 1.1 christos where = &(sa.sunaddr.sun_path); 55 1.1 christos /* BIO_ADDR_rawmake needs an extra byte for a NUL-terminator*/ 56 1.1 christos wherelen = sizeof(sa.sunaddr.sun_path) - 1; 57 1.1 christos break; 58 1.1 christos #endif 59 1.1 christos default: 60 1.1 christos TEST_error("Unsupported address family"); 61 1.1 christos return 0; 62 1.1 christos } 63 1.1 christos /* 64 1.1 christos * Could be any data, but we make it printable because BIO_ADDR_rawmake 65 1.1 christos * expects the AF_UNIX address to be a string. 66 1.1 christos */ 67 1.1 christos memset(where, 'a', wherelen); 68 1.1 christos 69 1.1 christos addr = BIO_ADDR_new(); 70 1.1 christos if (!TEST_ptr(addr)) 71 1.1 christos return NULL; 72 1.1 christos 73 1.1 christos if (!TEST_true(BIO_ADDR_rawmake(addr, family, where, wherelen, 1000))) { 74 1.1 christos BIO_ADDR_free(addr); 75 1.1 christos return NULL; 76 1.1 christos } 77 1.1 christos 78 1.1 christos return addr; 79 1.1 christos } 80 1.1 christos 81 1.1 christos static int bio_addr_is_eq(const BIO_ADDR *a, const BIO_ADDR *b) 82 1.1 christos { 83 1.1 christos unsigned char *adata = NULL, *bdata = NULL; 84 1.1 christos size_t alen, blen; 85 1.1 christos int ret = 0; 86 1.1 christos 87 1.1 christos /* True even if a and b are NULL */ 88 1.1 christos if (a == b) 89 1.1 christos return 1; 90 1.1 christos 91 1.1 christos /* If one is NULL the other cannot be due to the test above */ 92 1.1 christos if (a == NULL || b == NULL) 93 1.1 christos return 0; 94 1.1 christos 95 1.1 christos if (BIO_ADDR_family(a) != BIO_ADDR_family(b)) 96 1.1 christos return 0; 97 1.1 christos 98 1.1 christos /* Works even with AF_UNIX/AF_UNSPEC which just returns 0 */ 99 1.1 christos if (BIO_ADDR_rawport(a) != BIO_ADDR_rawport(b)) 100 1.1 christos return 0; 101 1.1 christos 102 1.1 christos if (!BIO_ADDR_rawaddress(a, NULL, &alen)) 103 1.1 christos return 0; 104 1.1 christos 105 1.1 christos if (!BIO_ADDR_rawaddress(b, NULL, &blen)) 106 1.1 christos goto err; 107 1.1 christos 108 1.1 christos if (alen != blen) 109 1.1 christos return 0; 110 1.1 christos 111 1.1 christos if (alen == 0) 112 1.1 christos return 1; 113 1.1 christos 114 1.1 christos adata = OPENSSL_malloc(alen); 115 1.1 christos if (!TEST_ptr(adata) 116 1.1.1.2 christos || !BIO_ADDR_rawaddress(a, adata, &alen)) 117 1.1 christos goto err; 118 1.1 christos 119 1.1 christos bdata = OPENSSL_malloc(blen); 120 1.1 christos if (!TEST_ptr(bdata) 121 1.1.1.2 christos || !BIO_ADDR_rawaddress(b, bdata, &blen)) 122 1.1 christos goto err; 123 1.1 christos 124 1.1 christos ret = (memcmp(adata, bdata, alen) == 0); 125 1.1 christos 126 1.1.1.2 christos err: 127 1.1 christos OPENSSL_free(adata); 128 1.1 christos OPENSSL_free(bdata); 129 1.1 christos return ret; 130 1.1 christos } 131 1.1 christos 132 1.1 christos static int test_bio_addr_copy_dup(int idx) 133 1.1 christos { 134 1.1 christos BIO_ADDR *src = NULL, *dst = NULL; 135 1.1 christos int ret = 0; 136 1.1 christos int docopy = idx & 1; 137 1.1 christos 138 1.1 christos idx >>= 1; 139 1.1 christos 140 1.1 christos src = make_dummy_addr(families[idx]); 141 1.1 christos if (!TEST_ptr(src)) 142 1.1 christos return 0; 143 1.1 christos 144 1.1 christos if (docopy) { 145 1.1 christos dst = BIO_ADDR_new(); 146 1.1 christos if (!TEST_ptr(dst)) 147 1.1 christos goto err; 148 1.1 christos 149 1.1 christos if (!TEST_true(BIO_ADDR_copy(dst, src))) 150 1.1 christos goto err; 151 1.1 christos } else { 152 1.1 christos dst = BIO_ADDR_dup(src); 153 1.1 christos if (!TEST_ptr(dst)) 154 1.1 christos goto err; 155 1.1 christos } 156 1.1 christos 157 1.1 christos if (!TEST_true(bio_addr_is_eq(src, dst))) 158 1.1 christos goto err; 159 1.1 christos 160 1.1 christos ret = 1; 161 1.1.1.2 christos err: 162 1.1 christos BIO_ADDR_free(src); 163 1.1 christos BIO_ADDR_free(dst); 164 1.1 christos return ret; 165 1.1 christos } 166 1.1 christos 167 1.1 christos int setup_tests(void) 168 1.1 christos { 169 1.1 christos if (!test_skip_common_options()) { 170 1.1 christos TEST_error("Error parsing test options\n"); 171 1.1 christos return 0; 172 1.1 christos } 173 1.1 christos 174 1.1 christos ADD_ALL_TESTS(test_bio_addr_copy_dup, OSSL_NELEM(families) * 2); 175 1.1 christos return 1; 176 1.1 christos } 177