Debian Codegraph @

debian/rust-vendor/openssl-src/openssl/crypto/bio/bss_sock.c

raw
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 {
64 return &methods_sockp;
65 }
66
67 BIO *BIO_new_socket(int fd, int close_flag)
68 {
69 BIO *ret;
70
71 ret = BIO_new(BIO_s_socket());
72 if (ret == NULL)
73 return NULL;
74 BIO_set_fd(ret, fd, close_flag);
75 return ret;
76 }
77
78 static int sock_new(BIO *bi)
79 {
80 bi->init = 0;
81 bi->num = 0;
82 bi->flags = 0;
83 bi->ptr = OPENSSL_zalloc(sizeof(struct bss_sock_st));
84 if (bi->ptr == NULL)
85 return 0;
86 return 1;
87 }
88
89 static int sock_free(BIO *a)
90 {
91 if (a == NULL)
92 return 0;
93 if (a->shutdown) {
94 if (a->init) {
95 BIO_closesocket(a->num);
96 }
97 a->init = 0;
98 a->flags = 0;
99 }
100 OPENSSL_free(a->ptr);
101 a->ptr = NULL;
102 return 1;
103 }
104
105 static int sock_read(BIO *b, char *out, int outl)
106 {
107 int ret = 0;
108
109 if (out != NULL) {
110 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 ret = readsocket(b->num, out, outl);
117 BIO_clear_retry_flags(b);
118 if (ret <= 0) {
119 if (BIO_sock_should_retry(ret))
120 BIO_set_retry_read(b);
121 else if (ret == 0)
122 b->flags |= BIO_FLAGS_IN_EOF;
123 }
124 }
125 return ret;
126 }
127
128 static int sock_write(BIO *b, const char *in, int inl)
129 {
130 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 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 ret = writesocket(b->num, in, inl);
157 BIO_clear_retry_flags(b);
158 if (ret <= 0) {
159 if (BIO_sock_should_retry(ret))
160 BIO_set_retry_write(b);
161 }
162 return ret;
163 }
164
165 static long sock_ctrl(BIO *b, int cmd, long num, void *ptr)
166 {
167 long ret = 1;
168 int *ip;
169 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 switch (cmd) {
175 case BIO_C_SET_FD:
176 /* minimal sock_free() */
177 if (b->shutdown) {
178 if (b->init)
179 BIO_closesocket(b->num);
180 b->flags = 0;
181 }
182 b->num = *((int *)ptr);
183 b->shutdown = (int)num;
184 b->init = 1;
185 data->tfo_first = 0;
186 memset(&data->tfo_peer, 0, sizeof(data->tfo_peer));
187 break;
188 case BIO_C_GET_FD:
189 if (b->init) {
190 ip = (int *)ptr;
191 if (ip != NULL)
192 *ip = b->num;
193 ret = b->num;
194 } else
195 ret = -1;
196 break;
197 case BIO_CTRL_GET_CLOSE:
198 ret = b->shutdown;
199 break;
200 case BIO_CTRL_SET_CLOSE:
201 b->shutdown = (int)num;
202 break;
203 case BIO_CTRL_DUP:
204 case BIO_CTRL_FLUSH:
205 ret = 1;
206 break;
207 case BIO_CTRL_GET_RPOLL_DESCRIPTOR:
208 case BIO_CTRL_GET_WPOLL_DESCRIPTOR: {
209 BIO_POLL_DESCRIPTOR *pd = ptr;
210
211 if (!b->init) {
212 ret = 0;
213 break;
214 }
215
216 pd->type = BIO_POLL_DESCRIPTOR_TYPE_SOCK_FD;
217 pd->value.fd = b->num;
218 } 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 case BIO_CTRL_EOF:
246 ret = (b->flags & BIO_FLAGS_IN_EOF) != 0;
247 break;
248 case BIO_C_GET_CONNECT:
249 if (ptr != NULL && num == 2) {
250 const char **pptr = (const char **)ptr;
251
252 *pptr = (const char *)&data->tfo_peer;
253 } else {
254 ret = 0;
255 }
256 break;
257 case BIO_C_SET_CONNECT:
258 if (ptr != NULL && num == 2) {
259 ret = BIO_ADDR_make(&data->tfo_peer,
260 BIO_ADDR_sockaddr((const BIO_ADDR *)ptr));
261 if (ret)
262 data->tfo_first = 1;
263 } else {
264 ret = 0;
265 }
266 break;
267 default:
268 ret = 0;
269 break;
270 }
271 return ret;
272 }
273
274 static int sock_puts(BIO *bp, const char *str)
275 {
276 int ret;
277 size_t n = strlen(str);
278
279 if (n > INT_MAX)
280 return -1;
281 ret = sock_write(bp, str, (int)n);
282 return ret;
283 }
284
285 int BIO_sock_should_retry(int i)
286 {
287 int err;
288
289 if ((i == 0) || (i == -1)) {
290 err = get_last_socket_error();
291
292 return BIO_sock_non_fatal_error(err);
293 }
294 return 0;
295 }
296
297 int BIO_sock_non_fatal_error(int err)
298 {
299 switch (err) {
300 #if defined(OPENSSL_SYS_WINDOWS)
301 #if defined(WSAEWOULDBLOCK)
302 case WSAEWOULDBLOCK:
303 #endif
304 #endif
305
306 #ifdef EWOULDBLOCK
307 #ifdef WSAEWOULDBLOCK
308 #if WSAEWOULDBLOCK != EWOULDBLOCK
309 case EWOULDBLOCK:
310 #endif
311 #else
312 case EWOULDBLOCK:
313 #endif
314 #endif
315
316 #if defined(ENOTCONN)
317 case ENOTCONN:
318 #endif
319
320 #ifdef EINTR
321 case EINTR:
322 #endif
323
324 #ifdef EAGAIN
325 #if EWOULDBLOCK != EAGAIN
326 case EAGAIN:
327 #endif
328 #endif
329
330 #ifdef EPROTO
331 case EPROTO:
332 #endif
333
334 #ifdef EINPROGRESS
335 case EINPROGRESS:
336 #endif
337
338 #ifdef EALREADY
339 case EALREADY:
340 #endif
341 return 1;
342 default:
343 break;
344 }
345 return 0;
346 }
347
348 #endif /* #ifndef OPENSSL_NO_SOCK */