Coverage Report

Created: 2025-11-16 06:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl30/crypto/bio/bss_sock.c
Line
Count
Source
1
/*
2
 * Copyright 1995-2021 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/cryptlib.h"
14
#include "internal/ktls.h"
15
16
#ifndef OPENSSL_NO_SOCK
17
18
# include <openssl/bio.h>
19
20
# ifdef WATT32
21
/* Watt-32 uses same names */
22
#  undef sock_write
23
#  undef sock_read
24
#  undef sock_puts
25
#  define sock_write SockWrite
26
#  define sock_read  SockRead
27
#  define sock_puts  SockPuts
28
# endif
29
30
static int sock_write(BIO *h, const char *buf, int num);
31
static int sock_read(BIO *h, char *buf, int size);
32
static int sock_puts(BIO *h, const char *str);
33
static long sock_ctrl(BIO *h, int cmd, long arg1, void *arg2);
34
static int sock_new(BIO *h);
35
static int sock_free(BIO *data);
36
int BIO_sock_should_retry(int s);
37
38
static const BIO_METHOD methods_sockp = {
39
    BIO_TYPE_SOCKET,
40
    "socket",
41
    bwrite_conv,
42
    sock_write,
43
    bread_conv,
44
    sock_read,
45
    sock_puts,
46
    NULL,                       /* sock_gets,         */
47
    sock_ctrl,
48
    sock_new,
49
    sock_free,
50
    NULL,                       /* sock_callback_ctrl */
51
};
52
53
const BIO_METHOD *BIO_s_socket(void)
54
0
{
55
0
    return &methods_sockp;
56
0
}
57
58
BIO *BIO_new_socket(int fd, int close_flag)
59
0
{
60
0
    BIO *ret;
61
62
0
    ret = BIO_new(BIO_s_socket());
63
0
    if (ret == NULL)
64
0
        return NULL;
65
0
    BIO_set_fd(ret, fd, close_flag);
66
0
    return ret;
67
0
}
68
69
static int sock_new(BIO *bi)
70
0
{
71
0
    bi->init = 0;
72
0
    bi->num = 0;
73
0
    bi->ptr = NULL;
74
0
    bi->flags = 0;
75
0
    return 1;
76
0
}
77
78
static int sock_free(BIO *a)
79
0
{
80
0
    if (a == NULL)
81
0
        return 0;
82
0
    if (a->shutdown) {
83
0
        if (a->init) {
84
0
            BIO_closesocket(a->num);
85
0
        }
86
0
        a->init = 0;
87
0
        a->flags = 0;
88
0
    }
89
0
    return 1;
90
0
}
91
92
static int sock_read(BIO *b, char *out, int outl)
93
0
{
94
0
    int ret = 0;
95
96
0
    if (out != NULL) {
97
0
        clear_socket_error();
98
# ifndef OPENSSL_NO_KTLS
99
        if (BIO_get_ktls_recv(b))
100
            ret = ktls_read_record(b->num, out, outl);
101
        else
102
# endif
103
0
            ret = readsocket(b->num, out, outl);
104
0
        BIO_clear_retry_flags(b);
105
0
        if (ret <= 0) {
106
0
            if (BIO_sock_should_retry(ret))
107
0
                BIO_set_retry_read(b);
108
0
            else if (ret == 0)
109
0
                b->flags |= BIO_FLAGS_IN_EOF;
110
0
        }
111
0
    }
112
0
    return ret;
113
0
}
114
115
static int sock_write(BIO *b, const char *in, int inl)
116
0
{
117
0
    int ret = 0;
118
119
0
    clear_socket_error();
120
# ifndef OPENSSL_NO_KTLS
121
    if (BIO_should_ktls_ctrl_msg_flag(b)) {
122
        unsigned char record_type = (intptr_t)b->ptr;
123
        ret = ktls_send_ctrl_message(b->num, record_type, in, inl);
124
        if (ret >= 0) {
125
            ret = inl;
126
            BIO_clear_ktls_ctrl_msg_flag(b);
127
        }
128
    } else
129
# endif
130
0
        ret = writesocket(b->num, in, inl);
131
0
    BIO_clear_retry_flags(b);
132
0
    if (ret <= 0) {
133
0
        if (BIO_sock_should_retry(ret))
134
0
            BIO_set_retry_write(b);
135
0
    }
136
0
    return ret;
137
0
}
138
139
static long sock_ctrl(BIO *b, int cmd, long num, void *ptr)
140
0
{
141
0
    long ret = 1;
142
0
    int *ip;
143
# ifndef OPENSSL_NO_KTLS
144
    ktls_crypto_info_t *crypto_info;
145
# endif
146
147
0
    switch (cmd) {
148
0
    case BIO_C_SET_FD:
149
0
        sock_free(b);
150
0
        b->num = *((int *)ptr);
151
0
        b->shutdown = (int)num;
152
0
        b->init = 1;
153
0
        break;
154
0
    case BIO_C_GET_FD:
155
0
        if (b->init) {
156
0
            ip = (int *)ptr;
157
0
            if (ip != NULL)
158
0
                *ip = b->num;
159
0
            ret = b->num;
160
0
        } else
161
0
            ret = -1;
162
0
        break;
163
0
    case BIO_CTRL_GET_CLOSE:
164
0
        ret = b->shutdown;
165
0
        break;
166
0
    case BIO_CTRL_SET_CLOSE:
167
0
        b->shutdown = (int)num;
168
0
        break;
169
0
    case BIO_CTRL_DUP:
170
0
    case BIO_CTRL_FLUSH:
171
0
        ret = 1;
172
0
        break;
173
# ifndef OPENSSL_NO_KTLS
174
    case BIO_CTRL_SET_KTLS:
175
        crypto_info = (ktls_crypto_info_t *)ptr;
176
        ret = ktls_start(b->num, crypto_info, num);
177
        if (ret)
178
            BIO_set_ktls_flag(b, num);
179
        break;
180
    case BIO_CTRL_GET_KTLS_SEND:
181
        return BIO_should_ktls_flag(b, 1) != 0;
182
    case BIO_CTRL_GET_KTLS_RECV:
183
        return BIO_should_ktls_flag(b, 0) != 0;
184
    case BIO_CTRL_SET_KTLS_TX_SEND_CTRL_MSG:
185
        BIO_set_ktls_ctrl_msg_flag(b);
186
        b->ptr = (void *)num;
187
        ret = 0;
188
        break;
189
    case BIO_CTRL_CLEAR_KTLS_TX_CTRL_MSG:
190
        BIO_clear_ktls_ctrl_msg_flag(b);
191
        ret = 0;
192
        break;
193
# endif
194
0
    case BIO_CTRL_EOF:
195
0
        ret = (b->flags & BIO_FLAGS_IN_EOF) != 0;
196
0
        break;
197
0
    default:
198
0
        ret = 0;
199
0
        break;
200
0
    }
201
0
    return ret;
202
0
}
203
204
static int sock_puts(BIO *bp, const char *str)
205
0
{
206
0
    int n, ret;
207
208
0
    n = strlen(str);
209
0
    ret = sock_write(bp, str, n);
210
0
    return ret;
211
0
}
212
213
int BIO_sock_should_retry(int i)
214
0
{
215
0
    int err;
216
217
0
    if ((i == 0) || (i == -1)) {
218
0
        err = get_last_socket_error();
219
220
0
        return BIO_sock_non_fatal_error(err);
221
0
    }
222
0
    return 0;
223
0
}
224
225
int BIO_sock_non_fatal_error(int err)
226
0
{
227
0
    switch (err) {
228
# if defined(OPENSSL_SYS_WINDOWS)
229
#  if defined(WSAEWOULDBLOCK)
230
    case WSAEWOULDBLOCK:
231
#  endif
232
# endif
233
234
0
# ifdef EWOULDBLOCK
235
#  ifdef WSAEWOULDBLOCK
236
#   if WSAEWOULDBLOCK != EWOULDBLOCK
237
    case EWOULDBLOCK:
238
#   endif
239
#  else
240
0
    case EWOULDBLOCK:
241
0
#  endif
242
0
# endif
243
244
0
# if defined(ENOTCONN)
245
0
    case ENOTCONN:
246
0
# endif
247
248
0
# ifdef EINTR
249
0
    case EINTR:
250
0
# endif
251
252
0
# ifdef EAGAIN
253
#  if EWOULDBLOCK != EAGAIN
254
    case EAGAIN:
255
#  endif
256
0
# endif
257
258
0
# ifdef EPROTO
259
0
    case EPROTO:
260
0
# endif
261
262
0
# ifdef EINPROGRESS
263
0
    case EINPROGRESS:
264
0
# endif
265
266
0
# ifdef EALREADY
267
0
    case EALREADY:
268
0
# endif
269
0
        return 1;
270
0
    default:
271
0
        break;
272
0
    }
273
0
    return 0;
274
0
}
275
276
#endif                          /* #ifndef OPENSSL_NO_SOCK */