Coverage Report

Created: 2026-02-22 06:11

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