Coverage Report

Created: 2025-12-10 06:24

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) {
110
0
        clear_socket_error();
111
#ifndef OPENSSL_NO_KTLS
112
        if (BIO_get_ktls_recv(b))
113
            ret = ktls_read_record(b->num, out, outl);
114
        else
115
#endif
116
0
            ret = readsocket(b->num, out, outl);
117
0
        BIO_clear_retry_flags(b);
118
0
        if (ret <= 0) {
119
0
            if (BIO_sock_should_retry(ret))
120
0
                BIO_set_retry_read(b);
121
0
            else if (ret == 0)
122
0
                b->flags |= BIO_FLAGS_IN_EOF;
123
0
        }
124
0
    }
125
0
    return ret;
126
0
}
127
128
static int sock_write(BIO *b, const char *in, int inl)
129
0
{
130
0
    int ret = 0;
131
#if !defined(OPENSSL_NO_KTLS) || defined(OSSL_TFO_SENDTO)
132
    struct bss_sock_st *data = (struct bss_sock_st *)b->ptr;
133
#endif
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);
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
        struct bss_sock_st *data = (struct bss_sock_st *)b->ptr;
149
        socklen_t peerlen = BIO_ADDR_sockaddr_size(&data->tfo_peer);
150
151
        ret = sendto(b->num, in, inl, OSSL_TFO_SENDTO,
152
            BIO_ADDR_sockaddr(&data->tfo_peer), peerlen);
153
        data->tfo_first = 0;
154
    } else
155
#endif
156
0
        ret = writesocket(b->num, in, inl);
157
0
    BIO_clear_retry_flags(b);
158
0
    if (ret <= 0) {
159
0
        if (BIO_sock_should_retry(ret))
160
0
            BIO_set_retry_write(b);
161
0
    }
162
0
    return ret;
163
0
}
164
165
static long sock_ctrl(BIO *b, int cmd, long num, void *ptr)
166
0
{
167
0
    long ret = 1;
168
0
    int *ip;
169
0
    struct bss_sock_st *data = (struct bss_sock_st *)b->ptr;
170
#ifndef OPENSSL_NO_KTLS
171
    ktls_crypto_info_t *crypto_info;
172
#endif
173
174
0
    switch (cmd) {
175
0
    case BIO_C_SET_FD:
176
        /* minimal sock_free() */
177
0
        if (b->shutdown) {
178
0
            if (b->init)
179
0
                BIO_closesocket(b->num);
180
0
            b->flags = 0;
181
0
        }
182
0
        b->num = *((int *)ptr);
183
0
        b->shutdown = (int)num;
184
0
        b->init = 1;
185
0
        data->tfo_first = 0;
186
0
        memset(&data->tfo_peer, 0, sizeof(data->tfo_peer));
187
0
        break;
188
0
    case BIO_C_GET_FD:
189
0
        if (b->init) {
190
0
            ip = (int *)ptr;
191
0
            if (ip != NULL)
192
0
                *ip = b->num;
193
0
            ret = b->num;
194
0
        } else
195
0
            ret = -1;
196
0
        break;
197
0
    case BIO_CTRL_GET_CLOSE:
198
0
        ret = b->shutdown;
199
0
        break;
200
0
    case BIO_CTRL_SET_CLOSE:
201
0
        b->shutdown = (int)num;
202
0
        break;
203
0
    case BIO_CTRL_DUP:
204
0
    case BIO_CTRL_FLUSH:
205
0
        ret = 1;
206
0
        break;
207
0
    case BIO_CTRL_GET_RPOLL_DESCRIPTOR:
208
0
    case BIO_CTRL_GET_WPOLL_DESCRIPTOR: {
209
0
        BIO_POLL_DESCRIPTOR *pd = ptr;
210
211
0
        if (!b->init) {
212
0
            ret = 0;
213
0
            break;
214
0
        }
215
216
0
        pd->type = BIO_POLL_DESCRIPTOR_TYPE_SOCK_FD;
217
0
        pd->value.fd = b->num;
218
0
    } break;
219
#ifndef OPENSSL_NO_KTLS
220
    case BIO_CTRL_SET_KTLS:
221
        crypto_info = (ktls_crypto_info_t *)ptr;
222
        ret = ktls_start(b->num, crypto_info, num);
223
        if (ret)
224
            BIO_set_ktls_flag(b, num);
225
        break;
226
    case BIO_CTRL_GET_KTLS_SEND:
227
        return BIO_should_ktls_flag(b, 1) != 0;
228
    case BIO_CTRL_GET_KTLS_RECV:
229
        return BIO_should_ktls_flag(b, 0) != 0;
230
    case BIO_CTRL_SET_KTLS_TX_SEND_CTRL_MSG:
231
        BIO_set_ktls_ctrl_msg_flag(b);
232
        data->ktls_record_type = (unsigned char)num;
233
        ret = 0;
234
        break;
235
    case BIO_CTRL_CLEAR_KTLS_TX_CTRL_MSG:
236
        BIO_clear_ktls_ctrl_msg_flag(b);
237
        ret = 0;
238
        break;
239
    case BIO_CTRL_SET_KTLS_TX_ZEROCOPY_SENDFILE:
240
        ret = ktls_enable_tx_zerocopy_sendfile(b->num);
241
        if (ret)
242
            BIO_set_ktls_zerocopy_sendfile_flag(b);
243
        break;
244
#endif
245
0
    case BIO_CTRL_EOF:
246
0
        ret = (b->flags & BIO_FLAGS_IN_EOF) != 0;
247
0
        break;
248
0
    case BIO_C_GET_CONNECT:
249
0
        if (ptr != NULL && num == 2) {
250
0
            const char **pptr = (const char **)ptr;
251
252
0
            *pptr = (const char *)&data->tfo_peer;
253
0
        } else {
254
0
            ret = 0;
255
0
        }
256
0
        break;
257
0
    case BIO_C_SET_CONNECT:
258
0
        if (ptr != NULL && num == 2) {
259
0
            ret = BIO_ADDR_make(&data->tfo_peer,
260
0
                BIO_ADDR_sockaddr((const BIO_ADDR *)ptr));
261
0
            if (ret)
262
0
                data->tfo_first = 1;
263
0
        } else {
264
0
            ret = 0;
265
0
        }
266
0
        break;
267
0
    default:
268
0
        ret = 0;
269
0
        break;
270
0
    }
271
0
    return ret;
272
0
}
273
274
static int sock_puts(BIO *bp, const char *str)
275
0
{
276
0
    int ret;
277
0
    size_t n = strlen(str);
278
279
0
    if (n > INT_MAX)
280
0
        return -1;
281
0
    ret = sock_write(bp, str, (int)n);
282
0
    return ret;
283
0
}
284
285
int BIO_sock_should_retry(int i)
286
0
{
287
0
    int err;
288
289
0
    if ((i == 0) || (i == -1)) {
290
0
        err = get_last_socket_error();
291
292
0
        return BIO_sock_non_fatal_error(err);
293
0
    }
294
0
    return 0;
295
0
}
296
297
int BIO_sock_non_fatal_error(int err)
298
0
{
299
#if defined(OPENSSL_SYS_WINDOWS)
300
    return err == WSAEWOULDBLOCK
301
        || err == WSAENOTCONN
302
        || err == WSAEINTR
303
        || err == WSAEINPROGRESS
304
        || err == WSAEALREADY;
305
#else /* POSIX.1-2001 */
306
0
    return err == EWOULDBLOCK
307
0
        || err == EAGAIN
308
0
        || err == ENOTCONN
309
0
        || err == EINTR
310
0
#if !defined(__DJGPP__) && !defined(OPENSSL_SYS_TANDEM)
311
0
        || err == EPROTO
312
0
#endif
313
0
        || err == EINPROGRESS
314
0
        || err == EALREADY;
315
0
#endif
316
0
}
317
318
#endif /* #ifndef OPENSSL_NO_SOCK */