Coverage Report

Created: 2026-03-09 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/crypto/bio/bss_sock.c
Line
Count
Source
1
/*
2
 * Copyright 1995-2025 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 <errno.h>
12
#include "bio_local.h"
13
#include "internal/bio_tfo.h"
14
#include "internal/cryptlib.h"
15
#include "internal/ktls.h"
16
17
#ifndef OPENSSL_NO_SOCK
18
19
#include <openssl/bio.h>
20
21
#ifdef WATT32
22
/* Watt-32 uses same names */
23
#undef sock_write
24
#undef sock_read
25
#undef sock_puts
26
#define sock_write SockWrite
27
#define sock_read SockRead
28
#define sock_puts SockPuts
29
#endif
30
31
struct bss_sock_st {
32
    BIO_ADDR tfo_peer;
33
    int tfo_first;
34
#ifndef OPENSSL_NO_KTLS
35
    unsigned char ktls_record_type;
36
#endif
37
    int sflags;
38
};
39
40
static int sock_write(BIO *h, const char *buf, int num);
41
static int sock_read(BIO *h, char *buf, int size);
42
static int sock_puts(BIO *h, const char *str);
43
static long sock_ctrl(BIO *h, int cmd, long arg1, void *arg2);
44
static int sock_new(BIO *h);
45
static int sock_free(BIO *data);
46
int BIO_sock_should_retry(int s);
47
48
static const BIO_METHOD methods_sockp = {
49
    BIO_TYPE_SOCKET,
50
    "socket",
51
    bwrite_conv,
52
    sock_write,
53
    bread_conv,
54
    sock_read,
55
    sock_puts,
56
    NULL, /* sock_gets,         */
57
    sock_ctrl,
58
    sock_new,
59
    sock_free,
60
    NULL, /* sock_callback_ctrl */
61
};
62
63
const BIO_METHOD *BIO_s_socket(void)
64
0
{
65
0
    return &methods_sockp;
66
0
}
67
68
BIO *BIO_new_socket(int fd, int close_flag)
69
0
{
70
0
    BIO *ret;
71
72
0
    ret = BIO_new(BIO_s_socket());
73
0
    if (ret == NULL)
74
0
        return NULL;
75
0
    BIO_set_fd(ret, fd, close_flag);
76
0
    return ret;
77
0
}
78
79
static int sock_new(BIO *bi)
80
0
{
81
0
    bi->init = 0;
82
0
    bi->num = 0;
83
0
    bi->flags = 0;
84
0
    bi->ptr = OPENSSL_zalloc(sizeof(struct bss_sock_st));
85
0
    if (bi->ptr == NULL)
86
0
        return 0;
87
0
    return 1;
88
0
}
89
90
static int sock_free(BIO *a)
91
0
{
92
0
    if (a == NULL)
93
0
        return 0;
94
0
    if (a->shutdown) {
95
0
        if (a->init) {
96
0
            BIO_closesocket(a->num);
97
0
        }
98
0
        a->init = 0;
99
0
        a->flags = 0;
100
0
    }
101
0
    OPENSSL_free(a->ptr);
102
0
    a->ptr = NULL;
103
0
    return 1;
104
0
}
105
106
static int sock_read(BIO *b, char *out, int outl)
107
0
{
108
0
    int ret = 0;
109
110
0
    if (out != NULL && outl > 0) {
111
0
        clear_socket_error();
112
0
        b->flags &= ~BIO_FLAGS_IN_EOF;
113
#ifndef OPENSSL_NO_KTLS
114
        if (BIO_get_ktls_recv(b))
115
            ret = ktls_read_record(b->num, out, outl);
116
        else
117
#endif
118
0
            ret = readsocket(b->num, out, outl);
119
0
        BIO_clear_retry_flags(b);
120
0
        if (ret <= 0) {
121
0
            if (BIO_sock_should_retry(ret))
122
0
                BIO_set_retry_read(b);
123
0
            else if (ret == 0)
124
0
                b->flags |= BIO_FLAGS_IN_EOF;
125
0
        }
126
0
    }
127
0
    return ret;
128
0
}
129
130
static int sock_write(BIO *b, const char *in, int inl)
131
0
{
132
0
    int ret = 0;
133
0
    struct bss_sock_st *data = (struct bss_sock_st *)b->ptr;
134
135
0
    clear_socket_error();
136
#ifndef OPENSSL_NO_KTLS
137
    if (BIO_should_ktls_ctrl_msg_flag(b)) {
138
        unsigned char record_type = data->ktls_record_type;
139
        ret = ktls_send_ctrl_message(b->num, record_type, in, inl, data->sflags);
140
        if (ret >= 0) {
141
            ret = inl;
142
            BIO_clear_ktls_ctrl_msg_flag(b);
143
        }
144
    } else
145
#endif
146
#if defined(OSSL_TFO_SENDTO)
147
        if (data->tfo_first) {
148
        socklen_t peerlen = BIO_ADDR_sockaddr_size(&data->tfo_peer);
149
150
        ret = sendto(b->num, in, inl, OSSL_TFO_SENDTO | data->sflags,
151
            BIO_ADDR_sockaddr(&data->tfo_peer), peerlen);
152
        data->tfo_first = 0;
153
    } else
154
#endif
155
0
        ret = writesocket_ex(b->num, in, inl, data->sflags);
156
0
    BIO_clear_retry_flags(b);
157
0
    if (ret <= 0) {
158
0
        if (BIO_sock_should_retry(ret))
159
0
            BIO_set_retry_write(b);
160
0
    }
161
0
    return ret;
162
0
}
163
164
static long sock_ctrl(BIO *b, int cmd, long num, void *ptr)
165
0
{
166
0
    long ret = 1;
167
0
    int *ip;
168
0
    struct bss_sock_st *data = (struct bss_sock_st *)b->ptr;
169
#ifndef OPENSSL_NO_KTLS
170
    ktls_crypto_info_t *crypto_info;
171
#endif
172
173
0
    switch (cmd) {
174
0
    case BIO_C_SET_FD:
175
        /* minimal sock_free() */
176
0
        if (b->shutdown) {
177
0
            if (b->init)
178
0
                BIO_closesocket(b->num);
179
0
            b->flags = 0;
180
0
        }
181
0
        b->num = *((int *)ptr);
182
0
        b->shutdown = (int)num;
183
0
        b->init = 1;
184
0
        data->tfo_first = 0;
185
0
        memset(&data->tfo_peer, 0, sizeof(data->tfo_peer));
186
0
        break;
187
0
    case BIO_C_GET_FD:
188
0
        if (b->init) {
189
0
            ip = (int *)ptr;
190
0
            if (ip != NULL)
191
0
                *ip = b->num;
192
0
            ret = b->num;
193
0
        } else
194
0
            ret = -1;
195
0
        break;
196
0
    case BIO_CTRL_GET_CLOSE:
197
0
        ret = b->shutdown;
198
0
        break;
199
0
    case BIO_CTRL_SET_CLOSE:
200
0
        b->shutdown = (int)num;
201
0
        break;
202
0
    case BIO_CTRL_DUP:
203
0
    case BIO_CTRL_FLUSH:
204
0
        ret = 1;
205
0
        break;
206
0
    case BIO_CTRL_GET_RPOLL_DESCRIPTOR:
207
0
    case BIO_CTRL_GET_WPOLL_DESCRIPTOR: {
208
0
        BIO_POLL_DESCRIPTOR *pd = ptr;
209
210
0
        if (!b->init) {
211
0
            ret = 0;
212
0
            break;
213
0
        }
214
215
0
        pd->type = BIO_POLL_DESCRIPTOR_TYPE_SOCK_FD;
216
0
        pd->value.fd = b->num;
217
0
    } break;
218
#ifndef OPENSSL_NO_KTLS
219
    case BIO_CTRL_SET_KTLS:
220
        crypto_info = (ktls_crypto_info_t *)ptr;
221
        ret = ktls_start(b->num, crypto_info, num);
222
        if (ret)
223
            BIO_set_ktls_flag(b, num);
224
        break;
225
    case BIO_CTRL_GET_KTLS_SEND:
226
        return BIO_should_ktls_flag(b, 1) != 0;
227
    case BIO_CTRL_GET_KTLS_RECV:
228
        return BIO_should_ktls_flag(b, 0) != 0;
229
    case BIO_CTRL_SET_KTLS_TX_SEND_CTRL_MSG:
230
        BIO_set_ktls_ctrl_msg_flag(b);
231
        data->ktls_record_type = (unsigned char)num;
232
        ret = 0;
233
        break;
234
    case BIO_CTRL_CLEAR_KTLS_TX_CTRL_MSG:
235
        BIO_clear_ktls_ctrl_msg_flag(b);
236
        ret = 0;
237
        break;
238
    case BIO_CTRL_SET_KTLS_TX_ZEROCOPY_SENDFILE:
239
        ret = ktls_enable_tx_zerocopy_sendfile(b->num);
240
        if (ret)
241
            BIO_set_ktls_zerocopy_sendfile_flag(b);
242
        break;
243
#endif
244
0
    case BIO_CTRL_EOF:
245
0
        ret = (b->flags & BIO_FLAGS_IN_EOF) != 0;
246
0
        break;
247
0
    case BIO_C_GET_CONNECT:
248
0
        if (ptr != NULL && num == 2) {
249
0
            const char **pptr = (const char **)ptr;
250
251
0
            *pptr = (const char *)&data->tfo_peer;
252
0
        } else {
253
0
            ret = 0;
254
0
        }
255
0
        break;
256
0
    case BIO_C_SET_CONNECT:
257
0
        if (ptr != NULL && num == 2) {
258
0
            ret = BIO_ADDR_make(&data->tfo_peer,
259
0
                BIO_ADDR_sockaddr((const BIO_ADDR *)ptr));
260
0
            if (ret)
261
0
                data->tfo_first = 1;
262
0
        } else {
263
0
            ret = 0;
264
0
        }
265
0
        break;
266
0
    case BIO_C_SET_SEND_FLAGS:
267
0
        if (num > INT_MAX || num < INT_MIN) {
268
0
            ERR_raise(ERR_LIB_BIO, BIO_R_INVALID_ARGUMENT);
269
0
            return 0;
270
0
        }
271
#if defined(OPENSSL_SYS_VXWORKS) || defined(OPENSSL_SYS_TANDEM)
272
        if (num != 0) {
273
            ERR_raise(ERR_LIB_BIO, BIO_R_INVALID_ARGUMENT);
274
            return 0;
275
        }
276
#endif
277
0
        data->sflags = (int)num;
278
0
        ret = 1;
279
0
        break;
280
0
    default:
281
0
        ret = 0;
282
0
        break;
283
0
    }
284
0
    return ret;
285
0
}
286
287
static int sock_puts(BIO *bp, const char *str)
288
0
{
289
0
    int ret;
290
0
    size_t n = strlen(str);
291
292
0
    if (n > INT_MAX)
293
0
        return -1;
294
0
    ret = sock_write(bp, str, (int)n);
295
0
    return ret;
296
0
}
297
298
int BIO_sock_should_retry(int i)
299
0
{
300
0
    int err;
301
302
0
    if ((i == 0) || (i == -1)) {
303
0
        err = get_last_socket_error();
304
305
0
        return BIO_sock_non_fatal_error(err);
306
0
    }
307
0
    return 0;
308
0
}
309
310
int BIO_sock_non_fatal_error(int err)
311
0
{
312
#if defined(OPENSSL_SYS_WINDOWS)
313
    return err == WSAEWOULDBLOCK
314
        || err == WSAENOTCONN
315
        || err == WSAEINTR
316
        || err == WSAEINPROGRESS
317
        || err == WSAEALREADY;
318
#else /* POSIX.1-2001 */
319
0
    return err == EWOULDBLOCK
320
0
        || err == EAGAIN
321
0
        || err == ENOTCONN
322
0
        || err == EINTR
323
0
#if !defined(__DJGPP__) && !defined(OPENSSL_SYS_TANDEM)
324
0
        || err == EPROTO
325
0
#endif
326
#ifdef __FreeBSD__
327
        || err == EBUSY
328
#endif
329
0
        || err == EINPROGRESS
330
0
        || err == EALREADY;
331
0
#endif
332
0
}
333
334
#endif /* #ifndef OPENSSL_NO_SOCK */