Coverage Report

Created: 2025-12-10 06:24

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/crypto/bio/bio_sock2.c
Line
Count
Source
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
0
#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
0
{
44
0
    int sock = -1;
45
46
0
    if (BIO_sock_init() != 1)
47
0
        return INVALID_SOCKET;
48
49
0
    sock = socket(domain, socktype, protocol);
50
0
    if (sock == -1) {
51
0
        ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
52
0
            "calling socket()");
53
0
        ERR_raise(ERR_LIB_BIO, BIO_R_UNABLE_TO_CREATE_SOCKET);
54
0
        return INVALID_SOCKET;
55
0
    }
56
57
0
    return sock;
58
0
}
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
0
{
83
0
    const int on = 1;
84
85
0
    if (sock == -1) {
86
0
        ERR_raise(ERR_LIB_BIO, BIO_R_INVALID_SOCKET);
87
0
        return 0;
88
0
    }
89
90
0
    if (!BIO_socket_nbio(sock, (options & BIO_SOCK_NONBLOCK) != 0))
91
0
        return 0;
92
93
0
    if (options & BIO_SOCK_KEEPALIVE) {
94
0
        if (setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
95
0
                (const void *)&on, sizeof(on))
96
0
            != 0) {
97
0
            ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
98
0
                "calling setsockopt()");
99
0
            ERR_raise(ERR_LIB_BIO, BIO_R_UNABLE_TO_KEEPALIVE);
100
0
            return 0;
101
0
        }
102
0
    }
103
104
0
    if (options & BIO_SOCK_NODELAY) {
105
0
        if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY,
106
0
                (const void *)&on, sizeof(on))
107
0
            != 0) {
108
0
            ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
109
0
                "calling setsockopt()");
110
0
            ERR_raise(ERR_LIB_BIO, BIO_R_UNABLE_TO_NODELAY);
111
0
            return 0;
112
0
        }
113
0
    }
114
0
    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
0
    }
178
179
0
    if (connect(sock, BIO_ADDR_sockaddr(addr),
180
0
            BIO_ADDR_sockaddr_size(addr))
181
0
        == -1) {
182
0
        if (!BIO_sock_should_retry(-1)) {
183
0
            ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
184
0
                "calling connect()");
185
0
            ERR_raise(ERR_LIB_BIO, BIO_R_CONNECT_ERROR);
186
0
        }
187
0
        return 0;
188
0
    }
189
0
    return 1;
190
0
}
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
0
{
210
0
#ifndef OPENSSL_SYS_WINDOWS
211
0
    int on = 1;
212
0
#endif
213
214
0
    if (sock == -1) {
215
0
        ERR_raise(ERR_LIB_BIO, BIO_R_INVALID_SOCKET);
216
0
        return 0;
217
0
    }
218
219
0
#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
0
    if (options & BIO_SOCK_REUSEADDR) {
225
0
        if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
226
0
                (const void *)&on, sizeof(on))
227
0
            != 0) {
228
0
            ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
229
0
                "calling setsockopt()");
230
0
            ERR_raise(ERR_LIB_BIO, BIO_R_UNABLE_TO_REUSEADDR);
231
0
            return 0;
232
0
        }
233
0
    }
234
0
#endif
235
236
0
    if (bind(sock, BIO_ADDR_sockaddr(addr), BIO_ADDR_sockaddr_size(addr)) != 0) {
237
0
        ERR_raise_data(ERR_LIB_SYS, get_last_socket_error() /* may be 0 */,
238
0
            "calling bind()");
239
0
        ERR_raise(ERR_LIB_BIO, BIO_R_UNABLE_TO_BIND_SOCKET);
240
0
        return 0;
241
0
    }
242
243
0
    return 1;
244
0
}
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
0
{
286
0
    int on = 1;
287
0
    int socktype;
288
0
    socklen_t socktype_len = sizeof(socktype);
289
290
0
    if (sock == -1) {
291
0
        ERR_raise(ERR_LIB_BIO, BIO_R_INVALID_SOCKET);
292
0
        return 0;
293
0
    }
294
295
0
    if (getsockopt(sock, SOL_SOCKET, SO_TYPE,
296
0
            (void *)&socktype, &socktype_len)
297
0
            != 0
298
0
        || socktype_len != sizeof(socktype)) {
299
0
        ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
300
0
            "calling getsockopt()");
301
0
        ERR_raise(ERR_LIB_BIO, BIO_R_GETTING_SOCKTYPE);
302
0
        return 0;
303
0
    }
304
305
0
    if (!BIO_socket_nbio(sock, (options & BIO_SOCK_NONBLOCK) != 0))
306
0
        return 0;
307
308
0
    if (options & BIO_SOCK_KEEPALIVE) {
309
0
        if (setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
310
0
                (const void *)&on, sizeof(on))
311
0
            != 0) {
312
0
            ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
313
0
                "calling setsockopt()");
314
0
            ERR_raise(ERR_LIB_BIO, BIO_R_UNABLE_TO_KEEPALIVE);
315
0
            return 0;
316
0
        }
317
0
    }
318
319
0
    if (options & BIO_SOCK_NODELAY) {
320
0
        if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY,
321
0
                (const void *)&on, sizeof(on))
322
0
            != 0) {
323
0
            ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
324
0
                "calling setsockopt()");
325
0
            ERR_raise(ERR_LIB_BIO, BIO_R_UNABLE_TO_NODELAY);
326
0
            return 0;
327
0
        }
328
0
    }
329
330
    /* On OpenBSD it is always IPv6 only with IPv6 sockets thus read-only */
331
0
#if defined(IPV6_V6ONLY) && !defined(__OpenBSD__)
332
0
    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
0
        on = options & BIO_SOCK_V6_ONLY ? 1 : 0;
338
0
        if (setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY,
339
0
                (const void *)&on, sizeof(on))
340
0
            != 0) {
341
0
            ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
342
0
                "calling setsockopt()");
343
0
            ERR_raise(ERR_LIB_BIO, BIO_R_LISTEN_V6_ONLY);
344
0
            return 0;
345
0
        }
346
0
    }
347
0
#endif
348
349
0
    if (!BIO_bind(sock, addr, options))
350
0
        return 0;
351
352
0
    if (socktype != SOCK_DGRAM && listen(sock, MAX_LISTEN) == -1) {
353
0
        ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
354
0
            "calling listen()");
355
0
        ERR_raise(ERR_LIB_BIO, BIO_R_UNABLE_TO_LISTEN_SOCKET);
356
0
        return 0;
357
0
    }
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
0
    return 1;
409
0
}
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
0
{
420
0
    socklen_t len;
421
0
    int accepted_sock;
422
0
    BIO_ADDR locaddr;
423
0
    BIO_ADDR *addr = addr_ == NULL ? &locaddr : addr_;
424
425
0
    len = sizeof(*addr);
426
0
    accepted_sock = accept(accept_sock,
427
0
        BIO_ADDR_sockaddr_noconst(addr), &len);
428
0
    if (accepted_sock == -1) {
429
0
        if (!BIO_sock_should_retry(accepted_sock)) {
430
0
            ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
431
0
                "calling accept()");
432
0
            ERR_raise(ERR_LIB_BIO, BIO_R_ACCEPT_ERROR);
433
0
        }
434
0
        return INVALID_SOCKET;
435
0
    }
436
437
0
    if (!BIO_socket_nbio(accepted_sock, (options & BIO_SOCK_NONBLOCK) != 0)) {
438
0
        closesocket(accepted_sock);
439
0
        return INVALID_SOCKET;
440
0
    }
441
442
0
    return accepted_sock;
443
0
}
444
445
/*-
446
 * BIO_closesocket - Close a socket
447
 * @sock: the socket to close
448
 */
449
int BIO_closesocket(int sock)
450
0
{
451
0
    if (sock < 0 || closesocket(sock) < 0)
452
0
        return 0;
453
0
    return 1;
454
0
}
455
#endif