| 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 |
#if !defined(OPENSSL_WINDOWS) |
| 14 |
#include <sys/time.h> |
| 15 |
static int closesocket(const int sock) { return close(sock); } |
| 16 |
#else |
| 17 |
OPENSSL_MSVC_PRAGMA(warning(push, 3)) |
| 18 |
#include <winsock2.h> |
| 19 |
OPENSSL_MSVC_PRAGMA(warning(pop)) |
| 20 |
#endif |
| 21 |
|
| 22 |
typedef struct bio_dgram_data_st { |
| 23 |
BIO_ADDR peer; |
| 24 |
unsigned int connected; |
| 25 |
unsigned int _errno; |
| 26 |
} bio_dgram_data; |
| 27 |
|
| 28 |
static socklen_t BIO_ADDR_sockaddr_size(const BIO_ADDR *bap) { |
| 29 |
GUARD_PTR(bap); |
| 30 |
if (bap->sa.sa_family == AF_INET) { |
| 31 |
return sizeof(bap->s_in); |
| 32 |
} |
| 33 |
#ifdef AF_INET6 |
| 34 |
if (bap->sa.sa_family == AF_INET6) { |
| 35 |
return sizeof(bap->s_in6); |
| 36 |
} |
| 37 |
#endif |
| 38 |
#ifdef AWS_LC_HAS_AF_UNIX |
| 39 |
if (bap->sa.sa_family == AF_UNIX) { |
| 40 |
return sizeof(bap->s_un); |
| 41 |
} |
| 42 |
#endif |
| 43 |
return sizeof(*bap); |
| 44 |
} |
| 45 |
|
| 46 |
static struct sockaddr *BIO_ADDR_sockaddr_noconst(BIO_ADDR *bap) { |
| 47 |
GUARD_PTR(bap); |
| 48 |
return &bap->sa; |
| 49 |
} |
| 50 |
|
| 51 |
static const struct sockaddr *BIO_ADDR_sockaddr(const BIO_ADDR *bap) { |
| 52 |
GUARD_PTR(bap); |
| 53 |
return &bap->sa; |
| 54 |
} |
| 55 |
|
| 56 |
static int dgram_get_errno(void) { |
| 57 |
#if defined(OPENSSL_WINDOWS) |
| 58 |
return WSAGetLastError(); |
| 59 |
#else |
| 60 |
return errno; |
| 61 |
#endif |
| 62 |
} |
| 63 |
|
| 64 |
static int dgram_write(BIO *bp, const char *in, const int in_len) { |
| 65 |
GUARD_PTR(bp); |
| 66 |
GUARD_PTR(in); |
| 67 |
|
| 68 |
ssize_t result; |
| 69 |
bio_dgram_data *data = bp->ptr; |
| 70 |
GUARD_PTR(data); |
| 71 |
|
| 72 |
if (in_len < 0) { |
| 73 |
OPENSSL_PUT_ERROR(BIO, BIO_R_INVALID_ARGUMENT); |
| 74 |
return -1; |
| 75 |
} |
| 76 |
|
| 77 |
bio_clear_socket_error(bp->num); |
| 78 |
if (data->connected) { |
| 79 |
// With a zero flags argument, send() is equivalent to write(2). |
| 80 |
result = send(bp->num, in, in_len, 0); |
| 81 |
} else { |
| 82 |
// If a peer address has been pre-specified, sendto may return -1 and set |
| 83 |
// errno to[EISCONN]. |
| 84 |
const socklen_t peerlen = BIO_ADDR_sockaddr_size(&data->peer); |
| 85 |
result = |
| 86 |
sendto(bp->num, in, in_len, 0, BIO_ADDR_sockaddr(&data->peer), peerlen); |
| 87 |
} |
| 88 |
|
| 89 |
if (result < INT_MIN || result > INT_MAX) { |
| 90 |
OPENSSL_PUT_ERROR(BIO, BIO_R_SYS_LIB); |
| 91 |
return -1; |
| 92 |
} |
| 93 |
const int ret = result; |
| 94 |
|
| 95 |
BIO_clear_retry_flags(bp); |
| 96 |
if (ret <= 0) { |
| 97 |
if (bio_socket_should_retry(ret)) { |
| 98 |
BIO_set_retry_write(bp); |
| 99 |
} |
| 100 |
data->_errno = dgram_get_errno(); |
| 101 |
} |
| 102 |
return ret; |
| 103 |
} |
| 104 |
|
| 105 |
static int dgram_read(BIO *bp, char *out, const int out_len) { |
| 106 |
GUARD_PTR(bp); |
| 107 |
GUARD_PTR(out); |
| 108 |
GUARD_PTR(bp->ptr); |
| 109 |
|
| 110 |
BIO_ADDR peer; |
| 111 |
// Might be modified by call to `recvfrom`. |
| 112 |
socklen_t len = sizeof(peer); |
| 113 |
|
| 114 |
bio_dgram_data *data = bp->ptr; |
| 115 |
bio_clear_socket_error(bp->num); |
| 116 |
|
| 117 |
if (out_len < 0) { |
| 118 |
// out_len is cast to `size_t` below. |
| 119 |
OPENSSL_PUT_ERROR(BIO, BIO_R_INVALID_ARGUMENT); |
| 120 |
return -1; |
| 121 |
} |
| 122 |
|
| 123 |
// recvfrom may be used to receive data on a socket regardless of whether |
| 124 |
// it's connection-oriented. |
| 125 |
const ssize_t result = recvfrom(bp->num, out, out_len, 0, |
| 126 |
BIO_ADDR_sockaddr_noconst(&peer), &len); |
| 127 |
|
| 128 |
if (result < INT_MIN || result > INT_MAX) { |
| 129 |
OPENSSL_PUT_ERROR(BIO, BIO_R_SYS_LIB); |
| 130 |
return -1; |
| 131 |
} |
| 132 |
const int ret = result; |
| 133 |
|
| 134 |
if (!data->connected && ret >= 0) { |
| 135 |
if (1 != BIO_dgram_set_peer(bp, &peer)) { |
| 136 |
// The operation does not fail if peer not set. |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
BIO_clear_retry_flags(bp); |
| 141 |
if (ret < 0) { |
| 142 |
if (bio_socket_should_retry(ret)) { |
| 143 |
BIO_set_retry_read(bp); |
| 144 |
} |
| 145 |
data->_errno = dgram_get_errno(); |
| 146 |
} |
| 147 |
|
| 148 |
return ret; |
| 149 |
} |
| 150 |
|
| 151 |
static int dgram_puts(BIO *bp, const char *str) { |
| 152 |
GUARD_PTR(bp); |
| 153 |
GUARD_PTR(str); |
| 154 |
|
| 155 |
const int len = OPENSSL_strnlen(str, INT_MAX); |
| 156 |
return dgram_write(bp, str, len); |
| 157 |
} |
| 158 |
|
| 159 |
static int dgram_free(BIO *bp) { |
| 160 |
GUARD_PTR(bp); |
| 161 |
if (bp->shutdown && bp->init) { |
| 162 |
if (0 != closesocket(bp->num)) { |
| 163 |
// the show must go on |
| 164 |
} |
| 165 |
} |
| 166 |
bp->init = 0; |
| 167 |
bp->num = -1; |
| 168 |
bp->flags = 0; |
| 169 |
OPENSSL_free(bp->ptr); |
| 170 |
bp->ptr = NULL; |
| 171 |
return 1; |
| 172 |
} |
| 173 |
|
| 174 |
static int dgram_new(BIO *bio) { |
| 175 |
bio->init = 0; |
| 176 |
bio->num = -1; |
| 177 |
bio->ptr = OPENSSL_zalloc(sizeof(bio_dgram_data)); |
| 178 |
return bio->ptr != NULL; |
| 179 |
} |
| 180 |
|
| 181 |
static long dgram_ctrl(BIO *bp, const int cmd, const long num, void *ptr) { |
| 182 |
GUARD_PTR(bp); |
| 183 |
bio_dgram_data *data = bp->ptr; |
| 184 |
|
| 185 |
long ret = 1; |
| 186 |
|
| 187 |
switch (cmd) { |
| 188 |
case BIO_C_SET_FD: |
| 189 |
GUARD_PTR(ptr); |
| 190 |
int fd = *(int *)ptr; |
| 191 |
if (fd < 0) { |
| 192 |
// file descriptors must be non-negative. |
| 193 |
OPENSSL_PUT_ERROR(BIO, BIO_R_INVALID_ARGUMENT); |
| 194 |
return 0; |
| 195 |
} |
| 196 |
dgram_free(bp); |
| 197 |
dgram_new(bp); |
| 198 |
bp->num = fd; |
| 199 |
bp->shutdown = (int)num; |
| 200 |
bp->init = 1; |
| 201 |
break; |
| 202 |
case BIO_C_GET_FD: |
| 203 |
if (bp->init) { |
| 204 |
int *ip = ptr; |
| 205 |
if (ip) { |
| 206 |
*ip = bp->num; |
| 207 |
} |
| 208 |
ret = bp->num; |
| 209 |
} else { |
| 210 |
ret = -1; |
| 211 |
} |
| 212 |
break; |
| 213 |
case BIO_CTRL_GET_CLOSE: |
| 214 |
ret = bp->shutdown; |
| 215 |
break; |
| 216 |
case BIO_CTRL_SET_CLOSE: |
| 217 |
bp->shutdown = num != 0; |
| 218 |
break; |
| 219 |
case BIO_CTRL_FLUSH: |
| 220 |
ret = 1; |
| 221 |
break; |
| 222 |
case BIO_CTRL_DGRAM_SET_CONNECTED: |
| 223 |
GUARD_PTR(data); |
| 224 |
if (ptr != NULL) { |
| 225 |
data->connected = 1; |
| 226 |
ret = BIO_ADDR_copy(&data->peer, ptr); |
| 227 |
} else { |
| 228 |
data->connected = 0; |
| 229 |
OPENSSL_cleanse(&data->peer, sizeof(data->peer)); |
| 230 |
ret = 1; |
| 231 |
} |
| 232 |
break; |
| 233 |
case BIO_CTRL_DGRAM_GET_PEER: { |
| 234 |
GUARD_PTR(data); |
| 235 |
GUARD_PTR(ptr); |
| 236 |
const socklen_t size = BIO_ADDR_sockaddr_size(&data->peer); |
| 237 |
if (num == 0 || num >= (long)size) { |
| 238 |
OPENSSL_memcpy(ptr, &data->peer, size); |
| 239 |
ret = size; |
| 240 |
} else { |
| 241 |
ret = 0; |
| 242 |
} |
| 243 |
break; |
| 244 |
} |
| 245 |
case BIO_CTRL_DGRAM_CONNECT: |
| 246 |
case BIO_CTRL_DGRAM_SET_PEER: |
| 247 |
GUARD_PTR(data); |
| 248 |
GUARD_PTR(ptr); |
| 249 |
ret = BIO_ADDR_copy(&data->peer, ptr); |
| 250 |
break; |
| 251 |
case BIO_CTRL_DGRAM_GET_SEND_TIMER_EXP: |
| 252 |
case BIO_CTRL_DGRAM_GET_RECV_TIMER_EXP: { |
| 253 |
GUARD_PTR(data); |
| 254 |
int d_errno = 0; |
| 255 |
#ifdef OPENSSL_WINDOWS |
| 256 |
d_errno = (data->_errno == WSAETIMEDOUT); |
| 257 |
#else |
| 258 |
/* |
| 259 |
* if no data has been transferred and the timeout has been reached, |
| 260 |
* then -1 is returned with errno set to EAGAIN or EWOULDBLOCK, |
| 261 |
* or EINPROGRESS (for connect(2)) just as if the socket was specified |
| 262 |
* to be nonblocking. |
| 263 |
*/ |
| 264 |
d_errno = data->_errno == EAGAIN || data->_errno == EWOULDBLOCK || |
| 265 |
data->_errno == EINPROGRESS; |
| 266 |
#endif |
| 267 |
if (d_errno) { |
| 268 |
ret = 1; |
| 269 |
data->_errno = 0; |
| 270 |
} else { |
| 271 |
ret = 0; |
| 272 |
} |
| 273 |
break; |
| 274 |
} |
| 275 |
#if defined(SO_RCVTIMEO) |
| 276 |
case BIO_CTRL_DGRAM_SET_RECV_TIMEOUT: { |
| 277 |
GUARD_PTR(ptr); |
| 278 |
struct timeval *tv = (struct timeval *)ptr; |
| 279 |
// Check for negative values in timeval |
| 280 |
if (tv->tv_sec < 0 || tv->tv_usec < 0) { |
| 281 |
OPENSSL_PUT_ERROR(BIO, BIO_R_INVALID_ARGUMENT); |
| 282 |
ret = -1; |
| 283 |
break; |
| 284 |
} |
| 285 |
#ifdef OPENSSL_WINDOWS |
| 286 |
DWORD timeout = (DWORD)(tv->tv_sec * 1000 + tv->tv_usec / 1000); |
| 287 |
if (setsockopt(bp->num, SOL_SOCKET, SO_RCVTIMEO, |
| 288 |
(void *)&timeout, sizeof(timeout)) != 0) { |
| 289 |
OPENSSL_PUT_ERROR(BIO, BIO_R_SYS_LIB); |
| 290 |
ret = -1; |
| 291 |
} |
| 292 |
#else |
| 293 |
if (setsockopt(bp->num, SOL_SOCKET, SO_RCVTIMEO, ptr, |
| 294 |
sizeof(struct timeval)) < 0) { |
| 295 |
OPENSSL_PUT_ERROR(BIO, BIO_R_SYS_LIB); |
| 296 |
ret = -1; |
| 297 |
} |
| 298 |
#endif |
| 299 |
break; |
| 300 |
} |
| 301 |
case BIO_CTRL_DGRAM_GET_RECV_TIMEOUT: { |
| 302 |
GUARD_PTR(ptr); |
| 303 |
union { |
| 304 |
size_t s; |
| 305 |
int i; |
| 306 |
} sz = {0}; |
| 307 |
#ifdef OPENSSL_WINDOWS |
| 308 |
int timeout; |
| 309 |
struct timeval *tv = (struct timeval *)ptr; |
| 310 |
|
| 311 |
sz.i = sizeof(timeout); |
| 312 |
if (getsockopt(bp->num, SOL_SOCKET, SO_RCVTIMEO, |
| 313 |
(void *)&timeout, &sz.i) != 0) { |
| 314 |
OPENSSL_PUT_ERROR(BIO, BIO_R_SYS_LIB); |
| 315 |
ret = -1; |
| 316 |
} else { |
| 317 |
tv->tv_sec = timeout / 1000; |
| 318 |
tv->tv_usec = (timeout % 1000) * 1000; |
| 319 |
ret = sizeof(*tv); |
| 320 |
} |
| 321 |
#else |
| 322 |
sz.i = sizeof(struct timeval); |
| 323 |
if (getsockopt(bp->num, SOL_SOCKET, SO_RCVTIMEO, |
| 324 |
ptr, (void *)&sz) < 0) { |
| 325 |
OPENSSL_PUT_ERROR(BIO, BIO_R_SYS_LIB); |
| 326 |
ret = -1; |
| 327 |
} else if (sizeof(sz.s) != sizeof(sz.i) && sz.i == 0) { |
| 328 |
ret = (int)sz.s; |
| 329 |
} else { |
| 330 |
ret = sz.i; |
| 331 |
} |
| 332 |
#endif |
| 333 |
break; |
| 334 |
} |
| 335 |
#endif |
| 336 |
#if defined(SO_SNDTIMEO) |
| 337 |
case BIO_CTRL_DGRAM_SET_SEND_TIMEOUT: { |
| 338 |
GUARD_PTR(ptr); |
| 339 |
struct timeval *tv = (struct timeval *)ptr; |
| 340 |
// Check for negative values in timeval |
| 341 |
if (tv->tv_sec < 0 || tv->tv_usec < 0) { |
| 342 |
OPENSSL_PUT_ERROR(BIO, BIO_R_INVALID_ARGUMENT); |
| 343 |
ret = -1; |
| 344 |
break; |
| 345 |
} |
| 346 |
#ifdef OPENSSL_WINDOWS |
| 347 |
DWORD timeout = (DWORD)(tv->tv_sec * 1000 + tv->tv_usec / 1000); |
| 348 |
if (setsockopt(bp->num, SOL_SOCKET, SO_SNDTIMEO, |
| 349 |
(void *)&timeout, sizeof(timeout)) != 0) { |
| 350 |
OPENSSL_PUT_ERROR(BIO, BIO_R_SYS_LIB); |
| 351 |
ret = -1; |
| 352 |
} |
| 353 |
#else |
| 354 |
if (setsockopt(bp->num, SOL_SOCKET, SO_SNDTIMEO, ptr, |
| 355 |
sizeof(struct timeval)) < 0) { |
| 356 |
OPENSSL_PUT_ERROR(BIO, BIO_R_SYS_LIB); |
| 357 |
ret = -1; |
| 358 |
} |
| 359 |
#endif |
| 360 |
break; |
| 361 |
} |
| 362 |
case BIO_CTRL_DGRAM_GET_SEND_TIMEOUT: { |
| 363 |
GUARD_PTR(ptr); |
| 364 |
union { |
| 365 |
size_t s; |
| 366 |
int i; |
| 367 |
} sz = {0}; |
| 368 |
#ifdef OPENSSL_WINDOWS |
| 369 |
int timeout; |
| 370 |
struct timeval *tv = (struct timeval *)ptr; |
| 371 |
|
| 372 |
sz.i = sizeof(timeout); |
| 373 |
if (getsockopt(bp->num, SOL_SOCKET, SO_SNDTIMEO, |
| 374 |
(void *)&timeout, &sz.i) != 0) { |
| 375 |
OPENSSL_PUT_ERROR(BIO, BIO_R_SYS_LIB); |
| 376 |
ret = -1; |
| 377 |
} else { |
| 378 |
tv->tv_sec = timeout / 1000; |
| 379 |
tv->tv_usec = (timeout % 1000) * 1000; |
| 380 |
ret = sizeof(*tv); |
| 381 |
} |
| 382 |
#else |
| 383 |
sz.i = sizeof(struct timeval); |
| 384 |
if (getsockopt(bp->num, SOL_SOCKET, SO_SNDTIMEO, |
| 385 |
ptr, (void *)&sz) < 0) { |
| 386 |
OPENSSL_PUT_ERROR(BIO, BIO_R_SYS_LIB); |
| 387 |
ret = -1; |
| 388 |
} else if (sizeof(sz.s) != sizeof(sz.i) && sz.i == 0) { |
| 389 |
ret = (int)sz.s; |
| 390 |
} else { |
| 391 |
ret = sz.i; |
| 392 |
} |
| 393 |
#endif |
| 394 |
break; |
| 395 |
} |
| 396 |
#endif |
| 397 |
default: |
| 398 |
ret = 0; |
| 399 |
break; |
| 400 |
} |
| 401 |
return ret; |
| 402 |
} |
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
static const BIO_METHOD methods_dgramp = { |
| 407 |
.type = BIO_TYPE_DGRAM, |
| 408 |
.name = "datagram socket", |
| 409 |
.bwrite = dgram_write, |
| 410 |
.bread = dgram_read, |
| 411 |
.bputs = dgram_puts, |
| 412 |
.bgets = NULL, |
| 413 |
.ctrl = dgram_ctrl, |
| 414 |
.create = dgram_new, |
| 415 |
.destroy = dgram_free, |
| 416 |
.callback_ctrl = NULL, |
| 417 |
}; |
| 418 |
|
| 419 |
const BIO_METHOD *BIO_s_datagram(void) { return &methods_dgramp; } |
| 420 |
|
| 421 |
BIO *BIO_new_dgram(int fd, int close_flag) { |
| 422 |
BIO *ret = BIO_new(BIO_s_datagram()); |
| 423 |
if (ret == NULL) { |
| 424 |
return NULL; |
| 425 |
} |
| 426 |
int result = BIO_set_fd(ret, fd, close_flag); |
| 427 |
if (result <= 0) { |
| 428 |
BIO_free(ret); |
| 429 |
return NULL; |
| 430 |
} |
| 431 |
return ret; |
| 432 |
} |
| 433 |
|
| 434 |
int BIO_ctrl_dgram_connect(BIO *bp, const BIO_ADDR *peer) { |
| 435 |
const long ret = BIO_ctrl(bp, BIO_CTRL_DGRAM_CONNECT, 0, (BIO_ADDR *)peer); |
| 436 |
if (ret < INT_MIN || ret > INT_MAX) { |
| 437 |
return 0; |
| 438 |
} |
| 439 |
return ret; |
| 440 |
} |
| 441 |
|
| 442 |
int BIO_ctrl_set_connected(BIO *bp, const BIO_ADDR *peer) { |
| 443 |
const long ret = |
| 444 |
BIO_ctrl(bp, BIO_CTRL_DGRAM_SET_CONNECTED, 0, (BIO_ADDR *)peer); |
| 445 |
if (ret < INT_MIN || ret > INT_MAX) { |
| 446 |
return 0; |
| 447 |
} |
| 448 |
return ret; |
| 449 |
} |
| 450 |
|
| 451 |
int BIO_dgram_recv_timedout(BIO *bp) { |
| 452 |
const long ret = BIO_ctrl(bp, BIO_CTRL_DGRAM_GET_RECV_TIMER_EXP, 0, NULL); |
| 453 |
if (ret < INT_MIN || ret > INT_MAX) { |
| 454 |
return 0; |
| 455 |
} |
| 456 |
return ret; |
| 457 |
} |
| 458 |
|
| 459 |
int BIO_dgram_send_timedout(BIO *bp) { |
| 460 |
const long ret = BIO_ctrl(bp, BIO_CTRL_DGRAM_GET_SEND_TIMER_EXP, 0, NULL); |
| 461 |
if (ret < INT_MIN || ret > INT_MAX) { |
| 462 |
return 0; |
| 463 |
} |
| 464 |
return ret; |
| 465 |
} |
| 466 |
|
| 467 |
int BIO_dgram_get_peer(BIO *bp, BIO_ADDR *peer) { |
| 468 |
const long ret = BIO_ctrl(bp, BIO_CTRL_DGRAM_GET_PEER, 0, peer); |
| 469 |
if (ret < INT_MIN || ret > INT_MAX) { |
| 470 |
return 0; |
| 471 |
} |
| 472 |
return ret; |
| 473 |
} |
| 474 |
|
| 475 |
int BIO_dgram_set_peer(BIO *bp, const BIO_ADDR *peer) { |
| 476 |
const long ret = BIO_ctrl(bp, BIO_CTRL_DGRAM_SET_PEER, 0, (BIO_ADDR *)peer); |
| 477 |
if (ret < INT_MIN || ret > INT_MAX) { |
| 478 |
return 0; |
| 479 |
} |
| 480 |
return ret; |
| 481 |
} |
| 482 |
|
| 483 |
#endif |