| 1 |
/* |
| 2 |
* Copyright 2016-2022 The OpenSSL Project Authors. All Rights Reserved. |
| 3 |
* |
| 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 |
| 8 |
*/ |
| 9 |
|
| 10 |
#include <stdio.h> |
| 11 |
#include <stdlib.h> |
| 12 |
#include <errno.h> |
| 13 |
|
| 14 |
#include "bio_local.h" |
| 15 |
#include "internal/ktls.h" |
| 16 |
#include "internal/bio_tfo.h" |
| 17 |
|
| 18 |
#include <openssl/err.h> |
| 19 |
|
| 20 |
#ifndef OPENSSL_NO_SOCK |
| 21 |
#ifdef SO_MAXCONN |
| 22 |
#define MAX_LISTEN SO_MAXCONN |
| 23 |
#elif defined(SOMAXCONN) |
| 24 |
#define MAX_LISTEN SOMAXCONN |
| 25 |
#else |
| 26 |
#define MAX_LISTEN 32 |
| 27 |
#endif |
| 28 |
|
| 29 |
/*- |
| 30 |
* BIO_socket - create a socket |
| 31 |
* @domain: the socket domain (AF_INET, AF_INET6, AF_UNIX, ...) |
| 32 |
* @socktype: the socket type (SOCK_STEAM, SOCK_DGRAM) |
| 33 |
* @protocol: the protocol to use (IPPROTO_TCP, IPPROTO_UDP) |
| 34 |
* @options: BIO socket options (currently unused) |
| 35 |
* |
| 36 |
* Creates a socket. This should be called before calling any |
| 37 |
* of BIO_connect and BIO_listen. |
| 38 |
* |
| 39 |
* Returns the file descriptor on success or INVALID_SOCKET on failure. On |
| 40 |
* failure errno is set, and a status is added to the OpenSSL error stack. |
| 41 |
*/ |
| 42 |
int BIO_socket(int domain, int socktype, int protocol, int options) |
| 43 |
{ |
| 44 |
int sock = -1; |
| 45 |
|
| 46 |
if (BIO_sock_init() != 1) |
| 47 |
return INVALID_SOCKET; |
| 48 |
|
| 49 |
sock = socket(domain, socktype, protocol); |
| 50 |
if (sock == -1) { |
| 51 |
ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(), |
| 52 |
"calling socket()"); |
| 53 |
ERR_raise(ERR_LIB_BIO, BIO_R_UNABLE_TO_CREATE_SOCKET); |
| 54 |
return INVALID_SOCKET; |
| 55 |
} |
| 56 |
|
| 57 |
return sock; |
| 58 |
} |
| 59 |
|
| 60 |
/*- |
| 61 |
* BIO_connect - connect to an address |
| 62 |
* @sock: the socket to connect with |
| 63 |
* @addr: the address to connect to |
| 64 |
* @options: BIO socket options |
| 65 |
* |
| 66 |
* Connects to the address using the given socket and options. |
| 67 |
* |
| 68 |
* Options can be a combination of the following: |
| 69 |
* - BIO_SOCK_KEEPALIVE: enable regularly sending keep-alive messages. |
| 70 |
* - BIO_SOCK_NONBLOCK: Make the socket non-blocking. |
| 71 |
* - BIO_SOCK_NODELAY: don't delay small messages. |
| 72 |
* - BIO_SOCK_TFO: use TCP Fast Open |
| 73 |
* |
| 74 |
* options holds BIO socket options that can be used |
| 75 |
* You should call this for every address returned by BIO_lookup |
| 76 |
* until the connection is successful. |
| 77 |
* |
| 78 |
* Returns 1 on success or 0 on failure. On failure errno is set |
| 79 |
* and an error status is added to the OpenSSL error stack. |
| 80 |
*/ |
| 81 |
int BIO_connect(int sock, const BIO_ADDR *addr, int options) |
| 82 |
{ |
| 83 |
const int on = 1; |
| 84 |
|
| 85 |
if (sock == -1) { |
| 86 |
ERR_raise(ERR_LIB_BIO, BIO_R_INVALID_SOCKET); |
| 87 |
return 0; |
| 88 |
} |
| 89 |
|
| 90 |
if (!BIO_socket_nbio(sock, (options & BIO_SOCK_NONBLOCK) != 0)) |
| 91 |
return 0; |
| 92 |
|
| 93 |
if (options & BIO_SOCK_KEEPALIVE) { |
| 94 |
if (setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, |
| 95 |
(const void *)&on, sizeof(on)) |
| 96 |
!= 0) { |
| 97 |
ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(), |
| 98 |
"calling setsockopt()"); |
| 99 |
ERR_raise(ERR_LIB_BIO, BIO_R_UNABLE_TO_KEEPALIVE); |
| 100 |
return 0; |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
if (options & BIO_SOCK_NODELAY) { |
| 105 |
if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, |
| 106 |
(const void *)&on, sizeof(on)) |
| 107 |
!= 0) { |
| 108 |
ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(), |
| 109 |
"calling setsockopt()"); |
| 110 |
ERR_raise(ERR_LIB_BIO, BIO_R_UNABLE_TO_NODELAY); |
| 111 |
return 0; |
| 112 |
} |
| 113 |
} |
| 114 |
if (options & BIO_SOCK_TFO) { |
| 115 |
#if defined(OSSL_TFO_CLIENT_FLAG) |
| 116 |
#if defined(OSSL_TFO_SYSCTL_CLIENT) |
| 117 |
int enabled = 0; |
| 118 |
size_t enabledlen = sizeof(enabled); |
| 119 |
|
| 120 |
/* Later FreeBSD */ |
| 121 |
if (sysctlbyname(OSSL_TFO_SYSCTL_CLIENT, &enabled, &enabledlen, NULL, 0) < 0) { |
| 122 |
ERR_raise(ERR_LIB_BIO, BIO_R_TFO_NO_KERNEL_SUPPORT); |
| 123 |
return 0; |
| 124 |
} |
| 125 |
/* Need to check for client flag */ |
| 126 |
if (!(enabled & OSSL_TFO_CLIENT_FLAG)) { |
| 127 |
ERR_raise(ERR_LIB_BIO, BIO_R_TFO_DISABLED); |
| 128 |
return 0; |
| 129 |
} |
| 130 |
#elif defined(OSSL_TFO_SYSCTL) |
| 131 |
int enabled = 0; |
| 132 |
size_t enabledlen = sizeof(enabled); |
| 133 |
|
| 134 |
/* macOS */ |
| 135 |
if (sysctlbyname(OSSL_TFO_SYSCTL, &enabled, &enabledlen, NULL, 0) < 0) { |
| 136 |
ERR_raise(ERR_LIB_BIO, BIO_R_TFO_NO_KERNEL_SUPPORT); |
| 137 |
return 0; |
| 138 |
} |
| 139 |
/* Need to check for client flag */ |
| 140 |
if (!(enabled & OSSL_TFO_CLIENT_FLAG)) { |
| 141 |
ERR_raise(ERR_LIB_BIO, BIO_R_TFO_DISABLED); |
| 142 |
return 0; |
| 143 |
} |
| 144 |
#endif |
| 145 |
#endif |
| 146 |
#if defined(OSSL_TFO_CONNECTX) |
| 147 |
sa_endpoints_t sae; |
| 148 |
|
| 149 |
memset(&sae, 0, sizeof(sae)); |
| 150 |
sae.sae_dstaddr = BIO_ADDR_sockaddr(addr); |
| 151 |
sae.sae_dstaddrlen = BIO_ADDR_sockaddr_size(addr); |
| 152 |
if (connectx(sock, &sae, SAE_ASSOCID_ANY, |
| 153 |
CONNECT_DATA_IDEMPOTENT | CONNECT_RESUME_ON_READ_WRITE, |
| 154 |
NULL, 0, NULL, NULL) |
| 155 |
== -1) { |
| 156 |
if (!BIO_sock_should_retry(-1)) { |
| 157 |
ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(), |
| 158 |
"calling connectx()"); |
| 159 |
ERR_raise(ERR_LIB_BIO, BIO_R_CONNECT_ERROR); |
| 160 |
} |
| 161 |
return 0; |
| 162 |
} |
| 163 |
#endif |
| 164 |
#if defined(OSSL_TFO_CLIENT_SOCKOPT) |
| 165 |
if (setsockopt(sock, IPPROTO_TCP, OSSL_TFO_CLIENT_SOCKOPT, |
| 166 |
(const void *)&on, sizeof(on)) |
| 167 |
!= 0) { |
| 168 |
ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(), |
| 169 |
"calling setsockopt()"); |
| 170 |
ERR_raise(ERR_LIB_BIO, BIO_R_UNABLE_TO_TFO); |
| 171 |
return 0; |
| 172 |
} |
| 173 |
#endif |
| 174 |
#if defined(OSSL_TFO_DO_NOT_CONNECT) |
| 175 |
return 1; |
| 176 |
#endif |
| 177 |
} |
| 178 |
|
| 179 |
if (connect(sock, BIO_ADDR_sockaddr(addr), |
| 180 |
BIO_ADDR_sockaddr_size(addr)) |
| 181 |
== -1) { |
| 182 |
if (!BIO_sock_should_retry(-1)) { |
| 183 |
ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(), |
| 184 |
"calling connect()"); |
| 185 |
ERR_raise(ERR_LIB_BIO, BIO_R_CONNECT_ERROR); |
| 186 |
} |
| 187 |
return 0; |
| 188 |
} |
| 189 |
return 1; |
| 190 |
} |
| 191 |
|
| 192 |
/*- |
| 193 |
* BIO_bind - bind socket to address |
| 194 |
* @sock: the socket to set |
| 195 |
* @addr: local address to bind to |
| 196 |
* @options: BIO socket options |
| 197 |
* |
| 198 |
* Binds to the address using the given socket and options. |
| 199 |
* |
| 200 |
* Options can be a combination of the following: |
| 201 |
* - BIO_SOCK_REUSEADDR: Try to reuse the address and port combination |
| 202 |
* for a recently closed port. |
| 203 |
* |
| 204 |
* When restarting the program it could be that the port is still in use. If |
| 205 |
* you set to BIO_SOCK_REUSEADDR option it will try to reuse the port anyway. |
| 206 |
* It's recommended that you use this. |
| 207 |
*/ |
| 208 |
int BIO_bind(int sock, const BIO_ADDR *addr, int options) |
| 209 |
{ |
| 210 |
#ifndef OPENSSL_SYS_WINDOWS |
| 211 |
int on = 1; |
| 212 |
#endif |
| 213 |
|
| 214 |
if (sock == -1) { |
| 215 |
ERR_raise(ERR_LIB_BIO, BIO_R_INVALID_SOCKET); |
| 216 |
return 0; |
| 217 |
} |
| 218 |
|
| 219 |
#ifndef OPENSSL_SYS_WINDOWS |
| 220 |
/* |
| 221 |
* SO_REUSEADDR has different behavior on Windows than on |
| 222 |
* other operating systems, don't set it there. |
| 223 |
*/ |
| 224 |
if (options & BIO_SOCK_REUSEADDR) { |
| 225 |
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, |
| 226 |
(const void *)&on, sizeof(on)) |
| 227 |
!= 0) { |
| 228 |
ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(), |
| 229 |
"calling setsockopt()"); |
| 230 |
ERR_raise(ERR_LIB_BIO, BIO_R_UNABLE_TO_REUSEADDR); |
| 231 |
return 0; |
| 232 |
} |
| 233 |
} |
| 234 |
#endif |
| 235 |
|
| 236 |
if (bind(sock, BIO_ADDR_sockaddr(addr), BIO_ADDR_sockaddr_size(addr)) != 0) { |
| 237 |
ERR_raise_data(ERR_LIB_SYS, get_last_socket_error() /* may be 0 */, |
| 238 |
"calling bind()"); |
| 239 |
ERR_raise(ERR_LIB_BIO, BIO_R_UNABLE_TO_BIND_SOCKET); |
| 240 |
return 0; |
| 241 |
} |
| 242 |
|
| 243 |
return 1; |
| 244 |
} |
| 245 |
|
| 246 |
/*- |
| 247 |
* BIO_listen - Creates a listen socket |
| 248 |
* @sock: the socket to listen with |
| 249 |
* @addr: local address to bind to |
| 250 |
* @options: BIO socket options |
| 251 |
* |
| 252 |
* Binds to the address using the given socket and options, then |
| 253 |
* starts listening for incoming connections. |
| 254 |
* |
| 255 |
* Options can be a combination of the following: |
| 256 |
* - BIO_SOCK_KEEPALIVE: enable regularly sending keep-alive messages. |
| 257 |
* - BIO_SOCK_NONBLOCK: Make the socket non-blocking. |
| 258 |
* - BIO_SOCK_NODELAY: don't delay small messages. |
| 259 |
* - BIO_SOCK_REUSEADDR: Try to reuse the address and port combination |
| 260 |
* for a recently closed port. |
| 261 |
* - BIO_SOCK_V6_ONLY: When creating an IPv6 socket, make it listen only |
| 262 |
* for IPv6 addresses and not IPv4 addresses mapped to IPv6. |
| 263 |
* - BIO_SOCK_TFO: accept TCP fast open (set TCP_FASTOPEN) |
| 264 |
* |
| 265 |
* It's recommended that you set up both an IPv6 and IPv4 listen socket, and |
| 266 |
* then check both for new clients that connect to it. You want to set up |
| 267 |
* the socket as non-blocking in that case since else it could hang. |
| 268 |
* |
| 269 |
* Not all operating systems support IPv4 addresses on an IPv6 socket, and for |
| 270 |
* others it's an option. If you pass the BIO_LISTEN_V6_ONLY it will try to |
| 271 |
* create the IPv6 sockets to only listen for IPv6 connection. |
| 272 |
* |
| 273 |
* It could be that the first BIO_listen() call will listen to all the IPv6 |
| 274 |
* and IPv4 addresses and that then trying to bind to the IPv4 address will |
| 275 |
* fail. We can't tell the difference between already listening ourself to |
| 276 |
* it and someone else listening to it when failing and errno is EADDRINUSE, so |
| 277 |
* it's recommended to not give an error in that case if the first call was |
| 278 |
* successful. |
| 279 |
* |
| 280 |
* When restarting the program it could be that the port is still in use. If |
| 281 |
* you set to BIO_SOCK_REUSEADDR option it will try to reuse the port anyway. |
| 282 |
* It's recommended that you use this. |
| 283 |
*/ |
| 284 |
int BIO_listen(int sock, const BIO_ADDR *addr, int options) |
| 285 |
{ |
| 286 |
int on = 1; |
| 287 |
int socktype; |
| 288 |
socklen_t socktype_len = sizeof(socktype); |
| 289 |
|
| 290 |
if (sock == -1) { |
| 291 |
ERR_raise(ERR_LIB_BIO, BIO_R_INVALID_SOCKET); |
| 292 |
return 0; |
| 293 |
} |
| 294 |
|
| 295 |
if (getsockopt(sock, SOL_SOCKET, SO_TYPE, |
| 296 |
(void *)&socktype, &socktype_len) |
| 297 |
!= 0 |
| 298 |
|| socktype_len != sizeof(socktype)) { |
| 299 |
ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(), |
| 300 |
"calling getsockopt()"); |
| 301 |
ERR_raise(ERR_LIB_BIO, BIO_R_GETTING_SOCKTYPE); |
| 302 |
return 0; |
| 303 |
} |
| 304 |
|
| 305 |
if (!BIO_socket_nbio(sock, (options & BIO_SOCK_NONBLOCK) != 0)) |
| 306 |
return 0; |
| 307 |
|
| 308 |
if (options & BIO_SOCK_KEEPALIVE) { |
| 309 |
if (setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, |
| 310 |
(const void *)&on, sizeof(on)) |
| 311 |
!= 0) { |
| 312 |
ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(), |
| 313 |
"calling setsockopt()"); |
| 314 |
ERR_raise(ERR_LIB_BIO, BIO_R_UNABLE_TO_KEEPALIVE); |
| 315 |
return 0; |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
if (options & BIO_SOCK_NODELAY) { |
| 320 |
if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, |
| 321 |
(const void *)&on, sizeof(on)) |
| 322 |
!= 0) { |
| 323 |
ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(), |
| 324 |
"calling setsockopt()"); |
| 325 |
ERR_raise(ERR_LIB_BIO, BIO_R_UNABLE_TO_NODELAY); |
| 326 |
return 0; |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
/* On OpenBSD it is always IPv6 only with IPv6 sockets thus read-only */ |
| 331 |
#if defined(IPV6_V6ONLY) && !defined(__OpenBSD__) |
| 332 |
if (BIO_ADDR_family(addr) == AF_INET6) { |
| 333 |
/* |
| 334 |
* Note: Windows default of IPV6_V6ONLY is ON, and Linux is OFF. |
| 335 |
* Therefore we always have to use setsockopt here. |
| 336 |
*/ |
| 337 |
on = options & BIO_SOCK_V6_ONLY ? 1 : 0; |
| 338 |
if (setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, |
| 339 |
(const void *)&on, sizeof(on)) |
| 340 |
!= 0) { |
| 341 |
ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(), |
| 342 |
"calling setsockopt()"); |
| 343 |
ERR_raise(ERR_LIB_BIO, BIO_R_LISTEN_V6_ONLY); |
| 344 |
return 0; |
| 345 |
} |
| 346 |
} |
| 347 |
#endif |
| 348 |
|
| 349 |
if (!BIO_bind(sock, addr, options)) |
| 350 |
return 0; |
| 351 |
|
| 352 |
if (socktype != SOCK_DGRAM && listen(sock, MAX_LISTEN) == -1) { |
| 353 |
ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(), |
| 354 |
"calling listen()"); |
| 355 |
ERR_raise(ERR_LIB_BIO, BIO_R_UNABLE_TO_LISTEN_SOCKET); |
| 356 |
return 0; |
| 357 |
} |
| 358 |
|
| 359 |
#if defined(OSSL_TFO_SERVER_SOCKOPT) |
| 360 |
/* |
| 361 |
* Must do it explicitly after listen() for macOS, still |
| 362 |
* works fine on other OS's |
| 363 |
*/ |
| 364 |
if ((options & BIO_SOCK_TFO) && socktype != SOCK_DGRAM) { |
| 365 |
int q = OSSL_TFO_SERVER_SOCKOPT_VALUE; |
| 366 |
#if defined(OSSL_TFO_CLIENT_FLAG) |
| 367 |
#if defined(OSSL_TFO_SYSCTL_SERVER) |
| 368 |
int enabled = 0; |
| 369 |
size_t enabledlen = sizeof(enabled); |
| 370 |
|
| 371 |
/* Later FreeBSD */ |
| 372 |
if (sysctlbyname(OSSL_TFO_SYSCTL_SERVER, &enabled, &enabledlen, NULL, 0) < 0) { |
| 373 |
ERR_raise(ERR_LIB_BIO, BIO_R_TFO_NO_KERNEL_SUPPORT); |
| 374 |
return 0; |
| 375 |
} |
| 376 |
/* Need to check for server flag */ |
| 377 |
if (!(enabled & OSSL_TFO_SERVER_FLAG)) { |
| 378 |
ERR_raise(ERR_LIB_BIO, BIO_R_TFO_DISABLED); |
| 379 |
return 0; |
| 380 |
} |
| 381 |
#elif defined(OSSL_TFO_SYSCTL) |
| 382 |
int enabled = 0; |
| 383 |
size_t enabledlen = sizeof(enabled); |
| 384 |
|
| 385 |
/* Early FreeBSD, macOS */ |
| 386 |
if (sysctlbyname(OSSL_TFO_SYSCTL, &enabled, &enabledlen, NULL, 0) < 0) { |
| 387 |
ERR_raise(ERR_LIB_BIO, BIO_R_TFO_NO_KERNEL_SUPPORT); |
| 388 |
return 0; |
| 389 |
} |
| 390 |
/* Need to check for server flag */ |
| 391 |
if (!(enabled & OSSL_TFO_SERVER_FLAG)) { |
| 392 |
ERR_raise(ERR_LIB_BIO, BIO_R_TFO_DISABLED); |
| 393 |
return 0; |
| 394 |
} |
| 395 |
#endif |
| 396 |
#endif |
| 397 |
if (setsockopt(sock, IPPROTO_TCP, OSSL_TFO_SERVER_SOCKOPT, |
| 398 |
(void *)&q, sizeof(q)) |
| 399 |
< 0) { |
| 400 |
ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(), |
| 401 |
"calling setsockopt()"); |
| 402 |
ERR_raise(ERR_LIB_BIO, BIO_R_UNABLE_TO_TFO); |
| 403 |
return 0; |
| 404 |
} |
| 405 |
} |
| 406 |
#endif |
| 407 |
|
| 408 |
return 1; |
| 409 |
} |
| 410 |
|
| 411 |
/*- |
| 412 |
* BIO_accept_ex - Accept new incoming connections |
| 413 |
* @sock: the listening socket |
| 414 |
* @addr: the BIO_ADDR to store the peer address in |
| 415 |
* @options: BIO socket options, applied on the accepted socket. |
| 416 |
* |
| 417 |
*/ |
| 418 |
int BIO_accept_ex(int accept_sock, BIO_ADDR *addr_, int options) |
| 419 |
{ |
| 420 |
socklen_t len; |
| 421 |
int accepted_sock; |
| 422 |
BIO_ADDR locaddr; |
| 423 |
BIO_ADDR *addr = addr_ == NULL ? &locaddr : addr_; |
| 424 |
|
| 425 |
len = sizeof(*addr); |
| 426 |
accepted_sock = accept(accept_sock, |
| 427 |
BIO_ADDR_sockaddr_noconst(addr), &len); |
| 428 |
if (accepted_sock == -1) { |
| 429 |
if (!BIO_sock_should_retry(accepted_sock)) { |
| 430 |
ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(), |
| 431 |
"calling accept()"); |
| 432 |
ERR_raise(ERR_LIB_BIO, BIO_R_ACCEPT_ERROR); |
| 433 |
} |
| 434 |
return INVALID_SOCKET; |
| 435 |
} |
| 436 |
|
| 437 |
if (!BIO_socket_nbio(accepted_sock, (options & BIO_SOCK_NONBLOCK) != 0)) { |
| 438 |
closesocket(accepted_sock); |
| 439 |
return INVALID_SOCKET; |
| 440 |
} |
| 441 |
|
| 442 |
return accepted_sock; |
| 443 |
} |
| 444 |
|
| 445 |
/*- |
| 446 |
* BIO_closesocket - Close a socket |
| 447 |
* @sock: the socket to close |
| 448 |
*/ |
| 449 |
int BIO_closesocket(int sock) |
| 450 |
{ |
| 451 |
if (sock < 0 || closesocket(sock) < 0) |
| 452 |
return 0; |
| 453 |
return 1; |
| 454 |
} |
| 455 |
#endif |