| 1 |
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 OR ISC |
| 3 |
|
| 4 |
#include <openssl/bio.h> |
| 5 |
|
| 6 |
#if !defined(OPENSSL_NO_SOCK) |
| 7 |
|
| 8 |
#include <openssl/mem.h> |
| 9 |
|
| 10 |
#include "../internal.h" |
| 11 |
#include "./internal.h" |
| 12 |
|
| 13 |
BIO_ADDR *BIO_ADDR_new(void) { |
| 14 |
BIO_ADDR *ret = OPENSSL_zalloc(sizeof(BIO_ADDR)); |
| 15 |
if (ret == NULL) { |
| 16 |
return NULL; |
| 17 |
} |
| 18 |
|
| 19 |
ret->sa.sa_family = AF_UNSPEC; |
| 20 |
return ret; |
| 21 |
} |
| 22 |
|
| 23 |
void BIO_ADDR_free(BIO_ADDR *ap) { OPENSSL_free(ap); } |
| 24 |
|
| 25 |
int BIO_ADDR_copy(BIO_ADDR *dst, const BIO_ADDR *src) { |
| 26 |
GUARD_PTR(dst); |
| 27 |
GUARD_PTR(src); |
| 28 |
|
| 29 |
if (src->sa.sa_family == AF_UNSPEC) { |
| 30 |
BIO_ADDR_clear(dst); |
| 31 |
return 1; |
| 32 |
} |
| 33 |
|
| 34 |
if (src->sa.sa_family == AF_INET) { |
| 35 |
OPENSSL_memcpy(&dst->s_in, &src->sa, sizeof(dst->s_in)); |
| 36 |
return 1; |
| 37 |
} |
| 38 |
#ifdef AF_INET6 |
| 39 |
if (src->sa.sa_family == AF_INET6) { |
| 40 |
OPENSSL_memcpy(&dst->s_in6, &src->sa, sizeof(dst->s_in6)); |
| 41 |
return 1; |
| 42 |
} |
| 43 |
#endif |
| 44 |
#ifdef AWS_LC_HAS_AF_UNIX |
| 45 |
if (src->sa.sa_family == AF_UNIX) { |
| 46 |
OPENSSL_memcpy(&dst->s_un, &src->sa, sizeof(dst->s_un)); |
| 47 |
return 1; |
| 48 |
} |
| 49 |
#endif |
| 50 |
|
| 51 |
return 0; |
| 52 |
} |
| 53 |
|
| 54 |
BIO_ADDR *BIO_ADDR_dup(const BIO_ADDR *ap) { |
| 55 |
GUARD_PTR(ap); |
| 56 |
|
| 57 |
BIO_ADDR *ret = BIO_ADDR_new(); |
| 58 |
if (ret == NULL) { |
| 59 |
return NULL; |
| 60 |
} |
| 61 |
|
| 62 |
if (1 != BIO_ADDR_copy(ret, ap)) { |
| 63 |
BIO_ADDR_free(ret); |
| 64 |
ret = NULL; |
| 65 |
} |
| 66 |
return ret; |
| 67 |
} |
| 68 |
|
| 69 |
void BIO_ADDR_clear(BIO_ADDR *ap) { |
| 70 |
if (ap == NULL) { |
| 71 |
return; |
| 72 |
} |
| 73 |
OPENSSL_cleanse(ap, sizeof(BIO_ADDR)); |
| 74 |
ap->sa.sa_family = AF_UNSPEC; |
| 75 |
} |
| 76 |
|
| 77 |
int BIO_ADDR_family(const BIO_ADDR *ap) { |
| 78 |
GUARD_PTR(ap); |
| 79 |
|
| 80 |
return ap->sa.sa_family; |
| 81 |
} |
| 82 |
|
| 83 |
int BIO_ADDR_rawmake(BIO_ADDR *ap, int family, const void *where, |
| 84 |
size_t wherelen, unsigned short port) { |
| 85 |
GUARD_PTR(ap); |
| 86 |
if (family == AF_INET) { |
| 87 |
if (wherelen != sizeof(struct in_addr)) { |
| 88 |
return 0; |
| 89 |
} |
| 90 |
OPENSSL_cleanse(&ap->s_in, sizeof(ap->s_in)); |
| 91 |
ap->s_in.sin_family = family; |
| 92 |
ap->s_in.sin_port = port; |
| 93 |
ap->s_in.sin_addr = *(struct in_addr *)where; |
| 94 |
return 1; |
| 95 |
} |
| 96 |
#ifdef AF_INET6 |
| 97 |
if (family == AF_INET6) { |
| 98 |
if (wherelen != sizeof(struct in6_addr)) { |
| 99 |
return 0; |
| 100 |
} |
| 101 |
OPENSSL_cleanse(&ap->s_in6, sizeof(ap->s_in6)); |
| 102 |
ap->s_in6.sin6_family = family; |
| 103 |
ap->s_in6.sin6_port = port; |
| 104 |
ap->s_in6.sin6_addr = *(struct in6_addr *)where; |
| 105 |
return 1; |
| 106 |
} |
| 107 |
#endif |
| 108 |
#ifdef AWS_LC_HAS_AF_UNIX |
| 109 |
if (family == AF_UNIX) { |
| 110 |
GUARD_PTR(where); |
| 111 |
// wherelen is expected to be the length of the path string |
| 112 |
// not including the terminating NUL. |
| 113 |
if (wherelen + 1 > sizeof(ap->s_un.sun_path)) { |
| 114 |
return 0; |
| 115 |
} |
| 116 |
OPENSSL_cleanse(&ap->s_un, sizeof(ap->s_un)); |
| 117 |
ap->s_un.sun_family = family; |
| 118 |
OPENSSL_strlcpy(ap->s_un.sun_path, where, wherelen); |
| 119 |
return 1; |
| 120 |
} |
| 121 |
#endif |
| 122 |
|
| 123 |
return 0; |
| 124 |
} |
| 125 |
|
| 126 |
int BIO_ADDR_rawaddress(const BIO_ADDR *ap, void *p, size_t *l) { |
| 127 |
size_t len = 0; |
| 128 |
const void *addrptr = NULL; |
| 129 |
|
| 130 |
GUARD_PTR(ap); |
| 131 |
|
| 132 |
if (ap->sa.sa_family == AF_INET) { |
| 133 |
len = sizeof(ap->s_in.sin_addr); |
| 134 |
addrptr = &ap->s_in.sin_addr; |
| 135 |
} |
| 136 |
#ifdef AF_INET6 |
| 137 |
else if (ap->sa.sa_family == AF_INET6) { |
| 138 |
len = sizeof(ap->s_in6.sin6_addr); |
| 139 |
addrptr = &ap->s_in6.sin6_addr; |
| 140 |
} |
| 141 |
#endif |
| 142 |
#ifdef AWS_LC_HAS_AF_UNIX |
| 143 |
else if (ap->sa.sa_family == AF_UNIX) { |
| 144 |
// len does not include the null |
| 145 |
len = OPENSSL_strnlen(ap->s_un.sun_path, sizeof(ap->s_un.sun_path)); |
| 146 |
addrptr = &ap->s_un.sun_path; |
| 147 |
} |
| 148 |
#endif |
| 149 |
|
| 150 |
if (addrptr == NULL) { |
| 151 |
return 0; |
| 152 |
} |
| 153 |
if (l != NULL) { |
| 154 |
*l = len; |
| 155 |
} |
| 156 |
|
| 157 |
if (p != NULL) { |
| 158 |
OPENSSL_memcpy(p, addrptr, len); |
| 159 |
if (ap->sa.sa_family == AF_UNIX) { |
| 160 |
// OpenSSL does not write the terminating NUL |
| 161 |
((char *)p)[len] = '\0'; |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
return 1; |
| 166 |
} |
| 167 |
|
| 168 |
unsigned short BIO_ADDR_rawport(const BIO_ADDR *ap) |
| 169 |
{ |
| 170 |
GUARD_PTR(ap); |
| 171 |
|
| 172 |
if (ap->sa.sa_family == AF_INET) { |
| 173 |
return ap->s_in.sin_port; |
| 174 |
} |
| 175 |
#ifdef AF_INET6 |
| 176 |
if (ap->sa.sa_family == AF_INET6) { |
| 177 |
return ap->s_in6.sin6_port; |
| 178 |
} |
| 179 |
#endif |
| 180 |
return 0; |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
#endif // !defined(OPENSSL_NO_SOCK) |