/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 | { |
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 | } |
220 | 0 | break; |
221 | | # ifndef OPENSSL_NO_KTLS |
222 | | case BIO_CTRL_SET_KTLS: |
223 | | crypto_info = (ktls_crypto_info_t *)ptr; |
224 | | ret = ktls_start(b->num, crypto_info, num); |
225 | | if (ret) |
226 | | BIO_set_ktls_flag(b, num); |
227 | | break; |
228 | | case BIO_CTRL_GET_KTLS_SEND: |
229 | | return BIO_should_ktls_flag(b, 1) != 0; |
230 | | case BIO_CTRL_GET_KTLS_RECV: |
231 | | return BIO_should_ktls_flag(b, 0) != 0; |
232 | | case BIO_CTRL_SET_KTLS_TX_SEND_CTRL_MSG: |
233 | | BIO_set_ktls_ctrl_msg_flag(b); |
234 | | data->ktls_record_type = (unsigned char)num; |
235 | | ret = 0; |
236 | | break; |
237 | | case BIO_CTRL_CLEAR_KTLS_TX_CTRL_MSG: |
238 | | BIO_clear_ktls_ctrl_msg_flag(b); |
239 | | ret = 0; |
240 | | break; |
241 | | case BIO_CTRL_SET_KTLS_TX_ZEROCOPY_SENDFILE: |
242 | | ret = ktls_enable_tx_zerocopy_sendfile(b->num); |
243 | | if (ret) |
244 | | BIO_set_ktls_zerocopy_sendfile_flag(b); |
245 | | break; |
246 | | # endif |
247 | 0 | case BIO_CTRL_EOF: |
248 | 0 | ret = (b->flags & BIO_FLAGS_IN_EOF) != 0; |
249 | 0 | break; |
250 | 0 | case BIO_C_GET_CONNECT: |
251 | 0 | if (ptr != NULL && num == 2) { |
252 | 0 | const char **pptr = (const char **)ptr; |
253 | |
|
254 | 0 | *pptr = (const char *)&data->tfo_peer; |
255 | 0 | } else { |
256 | 0 | ret = 0; |
257 | 0 | } |
258 | 0 | break; |
259 | 0 | case BIO_C_SET_CONNECT: |
260 | 0 | if (ptr != NULL && num == 2) { |
261 | 0 | ret = BIO_ADDR_make(&data->tfo_peer, |
262 | 0 | BIO_ADDR_sockaddr((const BIO_ADDR *)ptr)); |
263 | 0 | if (ret) |
264 | 0 | data->tfo_first = 1; |
265 | 0 | } else { |
266 | 0 | ret = 0; |
267 | 0 | } |
268 | 0 | break; |
269 | 0 | default: |
270 | 0 | ret = 0; |
271 | 0 | break; |
272 | 0 | } |
273 | 0 | return ret; |
274 | 0 | } |
275 | | |
276 | | static int sock_puts(BIO *bp, const char *str) |
277 | 0 | { |
278 | 0 | int ret; |
279 | 0 | size_t n = strlen(str); |
280 | |
|
281 | 0 | if (n > INT_MAX) |
282 | 0 | return -1; |
283 | 0 | ret = sock_write(bp, str, (int)n); |
284 | 0 | return ret; |
285 | 0 | } |
286 | | |
287 | | int BIO_sock_should_retry(int i) |
288 | 0 | { |
289 | 0 | int err; |
290 | |
|
291 | 0 | if ((i == 0) || (i == -1)) { |
292 | 0 | err = get_last_socket_error(); |
293 | |
|
294 | 0 | return BIO_sock_non_fatal_error(err); |
295 | 0 | } |
296 | 0 | return 0; |
297 | 0 | } |
298 | | |
299 | | int BIO_sock_non_fatal_error(int err) |
300 | 0 | { |
301 | | # if defined(OPENSSL_SYS_WINDOWS) |
302 | | return err == WSAEWOULDBLOCK |
303 | | || err == WSAENOTCONN |
304 | | || err == WSAEINTR |
305 | | || err == WSAEINPROGRESS |
306 | | || err == WSAEALREADY; |
307 | | # else /* POSIX.1-2001 */ |
308 | 0 | return err == EWOULDBLOCK |
309 | 0 | || err == EAGAIN |
310 | 0 | || err == ENOTCONN |
311 | 0 | || err == EINTR |
312 | 0 | # if !defined(__DJGPP__) && !defined(OPENSSL_SYS_TANDEM) |
313 | 0 | || err == EPROTO |
314 | 0 | # endif |
315 | 0 | || err == EINPROGRESS |
316 | 0 | || err == EALREADY; |
317 | 0 | # endif |
318 | 0 | } |
319 | | |
320 | | #endif /* #ifndef OPENSSL_NO_SOCK */ |