/src/openssl/crypto/bio/bss_sock.c
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /*  | 
2  |  |  * Copyright 1995-2023 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  |  | # ifndef OPENSSL_NO_KTLS  | 
76  |  |     { | 
77  |  |         /*  | 
78  |  |          * The new socket is created successfully regardless of ktls_enable.  | 
79  |  |          * ktls_enable doesn't change any functionality of the socket, except  | 
80  |  |          * changing the setsockopt to enable the processing of ktls_start.  | 
81  |  |          * Thus, it is not a problem to call it for non-TLS sockets.  | 
82  |  |          */  | 
83  |  |         ktls_enable(fd);  | 
84  |  |     }  | 
85  |  | # endif  | 
86  | 0  |     return ret;  | 
87  | 0  | }  | 
88  |  |  | 
89  |  | static int sock_new(BIO *bi)  | 
90  | 0  | { | 
91  | 0  |     bi->init = 0;  | 
92  | 0  |     bi->num = 0;  | 
93  | 0  |     bi->flags = 0;  | 
94  | 0  |     bi->ptr = OPENSSL_zalloc(sizeof(struct bss_sock_st));  | 
95  | 0  |     if (bi->ptr == NULL)  | 
96  | 0  |         return 0;  | 
97  | 0  |     return 1;  | 
98  | 0  | }  | 
99  |  |  | 
100  |  | static int sock_free(BIO *a)  | 
101  | 0  | { | 
102  | 0  |     if (a == NULL)  | 
103  | 0  |         return 0;  | 
104  | 0  |     if (a->shutdown) { | 
105  | 0  |         if (a->init) { | 
106  | 0  |             BIO_closesocket(a->num);  | 
107  | 0  |         }  | 
108  | 0  |         a->init = 0;  | 
109  | 0  |         a->flags = 0;  | 
110  | 0  |     }  | 
111  | 0  |     OPENSSL_free(a->ptr);  | 
112  | 0  |     a->ptr = NULL;  | 
113  | 0  |     return 1;  | 
114  | 0  | }  | 
115  |  |  | 
116  |  | static int sock_read(BIO *b, char *out, int outl)  | 
117  | 0  | { | 
118  | 0  |     int ret = 0;  | 
119  |  | 
  | 
120  | 0  |     if (out != NULL) { | 
121  | 0  |         clear_socket_error();  | 
122  |  | # ifndef OPENSSL_NO_KTLS  | 
123  |  |         if (BIO_get_ktls_recv(b))  | 
124  |  |             ret = ktls_read_record(b->num, out, outl);  | 
125  |  |         else  | 
126  |  | # endif  | 
127  | 0  |             ret = readsocket(b->num, out, outl);  | 
128  | 0  |         BIO_clear_retry_flags(b);  | 
129  | 0  |         if (ret <= 0) { | 
130  | 0  |             if (BIO_sock_should_retry(ret))  | 
131  | 0  |                 BIO_set_retry_read(b);  | 
132  | 0  |             else if (ret == 0)  | 
133  | 0  |                 b->flags |= BIO_FLAGS_IN_EOF;  | 
134  | 0  |         }  | 
135  | 0  |     }  | 
136  | 0  |     return ret;  | 
137  | 0  | }  | 
138  |  |  | 
139  |  | static int sock_write(BIO *b, const char *in, int inl)  | 
140  | 0  | { | 
141  | 0  |     int ret = 0;  | 
142  |  | # if !defined(OPENSSL_NO_KTLS) || defined(OSSL_TFO_SENDTO)  | 
143  |  |     struct bss_sock_st *data = (struct bss_sock_st *)b->ptr;  | 
144  |  | # endif  | 
145  |  | 
  | 
146  | 0  |     clear_socket_error();  | 
147  |  | # ifndef OPENSSL_NO_KTLS  | 
148  |  |     if (BIO_should_ktls_ctrl_msg_flag(b)) { | 
149  |  |         unsigned char record_type = data->ktls_record_type;  | 
150  |  |         ret = ktls_send_ctrl_message(b->num, record_type, in, inl);  | 
151  |  |         if (ret >= 0) { | 
152  |  |             ret = inl;  | 
153  |  |             BIO_clear_ktls_ctrl_msg_flag(b);  | 
154  |  |         }  | 
155  |  |     } else  | 
156  |  | # endif  | 
157  |  | # if defined(OSSL_TFO_SENDTO)  | 
158  |  |     if (data->tfo_first) { | 
159  |  |         struct bss_sock_st *data = (struct bss_sock_st *)b->ptr;  | 
160  |  |         socklen_t peerlen = BIO_ADDR_sockaddr_size(&data->tfo_peer);  | 
161  |  |  | 
162  |  |         ret = sendto(b->num, in, inl, OSSL_TFO_SENDTO,  | 
163  |  |                      BIO_ADDR_sockaddr(&data->tfo_peer), peerlen);  | 
164  |  |         data->tfo_first = 0;  | 
165  |  |     } else  | 
166  |  | # endif  | 
167  | 0  |         ret = writesocket(b->num, in, inl);  | 
168  | 0  |     BIO_clear_retry_flags(b);  | 
169  | 0  |     if (ret <= 0) { | 
170  | 0  |         if (BIO_sock_should_retry(ret))  | 
171  | 0  |             BIO_set_retry_write(b);  | 
172  | 0  |     }  | 
173  | 0  |     return ret;  | 
174  | 0  | }  | 
175  |  |  | 
176  |  | static long sock_ctrl(BIO *b, int cmd, long num, void *ptr)  | 
177  | 0  | { | 
178  | 0  |     long ret = 1;  | 
179  | 0  |     int *ip;  | 
180  | 0  |     struct bss_sock_st *data = (struct bss_sock_st *)b->ptr;  | 
181  |  | # ifndef OPENSSL_NO_KTLS  | 
182  |  |     ktls_crypto_info_t *crypto_info;  | 
183  |  | # endif  | 
184  |  | 
  | 
185  | 0  |     switch (cmd) { | 
186  | 0  |     case BIO_C_SET_FD:  | 
187  |  |         /* minimal sock_free() */  | 
188  | 0  |         if (b->shutdown) { | 
189  | 0  |             if (b->init)  | 
190  | 0  |                 BIO_closesocket(b->num);  | 
191  | 0  |             b->flags = 0;  | 
192  | 0  |         }  | 
193  | 0  |         b->num = *((int *)ptr);  | 
194  | 0  |         b->shutdown = (int)num;  | 
195  | 0  |         b->init = 1;  | 
196  | 0  |         data->tfo_first = 0;  | 
197  | 0  |         memset(&data->tfo_peer, 0, sizeof(data->tfo_peer));  | 
198  | 0  |         break;  | 
199  | 0  |     case BIO_C_GET_FD:  | 
200  | 0  |         if (b->init) { | 
201  | 0  |             ip = (int *)ptr;  | 
202  | 0  |             if (ip != NULL)  | 
203  | 0  |                 *ip = b->num;  | 
204  | 0  |             ret = b->num;  | 
205  | 0  |         } else  | 
206  | 0  |             ret = -1;  | 
207  | 0  |         break;  | 
208  | 0  |     case BIO_CTRL_GET_CLOSE:  | 
209  | 0  |         ret = b->shutdown;  | 
210  | 0  |         break;  | 
211  | 0  |     case BIO_CTRL_SET_CLOSE:  | 
212  | 0  |         b->shutdown = (int)num;  | 
213  | 0  |         break;  | 
214  | 0  |     case BIO_CTRL_DUP:  | 
215  | 0  |     case BIO_CTRL_FLUSH:  | 
216  | 0  |         ret = 1;  | 
217  | 0  |         break;  | 
218  | 0  |     case BIO_CTRL_GET_RPOLL_DESCRIPTOR:  | 
219  | 0  |     case BIO_CTRL_GET_WPOLL_DESCRIPTOR:  | 
220  | 0  |         { | 
221  | 0  |             BIO_POLL_DESCRIPTOR *pd = ptr;  | 
222  |  | 
  | 
223  | 0  |             if (!b->init) { | 
224  | 0  |                 ret = 0;  | 
225  | 0  |                 break;  | 
226  | 0  |             }  | 
227  |  |  | 
228  | 0  |             pd->type        = BIO_POLL_DESCRIPTOR_TYPE_SOCK_FD;  | 
229  | 0  |             pd->value.fd    = b->num;  | 
230  | 0  |         }  | 
231  | 0  |         break;  | 
232  |  | # ifndef OPENSSL_NO_KTLS  | 
233  |  |     case BIO_CTRL_SET_KTLS:  | 
234  |  |         crypto_info = (ktls_crypto_info_t *)ptr;  | 
235  |  |         ret = ktls_start(b->num, crypto_info, num);  | 
236  |  |         if (ret)  | 
237  |  |             BIO_set_ktls_flag(b, num);  | 
238  |  |         break;  | 
239  |  |     case BIO_CTRL_GET_KTLS_SEND:  | 
240  |  |         return BIO_should_ktls_flag(b, 1) != 0;  | 
241  |  |     case BIO_CTRL_GET_KTLS_RECV:  | 
242  |  |         return BIO_should_ktls_flag(b, 0) != 0;  | 
243  |  |     case BIO_CTRL_SET_KTLS_TX_SEND_CTRL_MSG:  | 
244  |  |         BIO_set_ktls_ctrl_msg_flag(b);  | 
245  |  |         data->ktls_record_type = (unsigned char)num;  | 
246  |  |         ret = 0;  | 
247  |  |         break;  | 
248  |  |     case BIO_CTRL_CLEAR_KTLS_TX_CTRL_MSG:  | 
249  |  |         BIO_clear_ktls_ctrl_msg_flag(b);  | 
250  |  |         ret = 0;  | 
251  |  |         break;  | 
252  |  |     case BIO_CTRL_SET_KTLS_TX_ZEROCOPY_SENDFILE:  | 
253  |  |         ret = ktls_enable_tx_zerocopy_sendfile(b->num);  | 
254  |  |         if (ret)  | 
255  |  |             BIO_set_ktls_zerocopy_sendfile_flag(b);  | 
256  |  |         break;  | 
257  |  | # endif  | 
258  | 0  |     case BIO_CTRL_EOF:  | 
259  | 0  |         ret = (b->flags & BIO_FLAGS_IN_EOF) != 0;  | 
260  | 0  |         break;  | 
261  | 0  |     case BIO_C_GET_CONNECT:  | 
262  | 0  |         if (ptr != NULL && num == 2) { | 
263  | 0  |             const char **pptr = (const char **)ptr;  | 
264  |  | 
  | 
265  | 0  |             *pptr = (const char *)&data->tfo_peer;  | 
266  | 0  |         } else { | 
267  | 0  |             ret = 0;  | 
268  | 0  |         }  | 
269  | 0  |         break;  | 
270  | 0  |     case BIO_C_SET_CONNECT:  | 
271  | 0  |         if (ptr != NULL && num == 2) { | 
272  | 0  |             ret = BIO_ADDR_make(&data->tfo_peer,  | 
273  | 0  |                                 BIO_ADDR_sockaddr((const BIO_ADDR *)ptr));  | 
274  | 0  |             if (ret)  | 
275  | 0  |                 data->tfo_first = 1;  | 
276  | 0  |         } else { | 
277  | 0  |             ret = 0;  | 
278  | 0  |         }  | 
279  | 0  |         break;  | 
280  | 0  |     default:  | 
281  | 0  |         ret = 0;  | 
282  | 0  |         break;  | 
283  | 0  |     }  | 
284  | 0  |     return ret;  | 
285  | 0  | }  | 
286  |  |  | 
287  |  | static int sock_puts(BIO *bp, const char *str)  | 
288  | 0  | { | 
289  | 0  |     int n, ret;  | 
290  |  | 
  | 
291  | 0  |     n = strlen(str);  | 
292  | 0  |     ret = sock_write(bp, str, n);  | 
293  | 0  |     return ret;  | 
294  | 0  | }  | 
295  |  |  | 
296  |  | int BIO_sock_should_retry(int i)  | 
297  | 0  | { | 
298  | 0  |     int err;  | 
299  |  | 
  | 
300  | 0  |     if ((i == 0) || (i == -1)) { | 
301  | 0  |         err = get_last_socket_error();  | 
302  |  | 
  | 
303  | 0  |         return BIO_sock_non_fatal_error(err);  | 
304  | 0  |     }  | 
305  | 0  |     return 0;  | 
306  | 0  | }  | 
307  |  |  | 
308  |  | int BIO_sock_non_fatal_error(int err)  | 
309  | 0  | { | 
310  | 0  |     switch (err) { | 
311  |  | # if defined(OPENSSL_SYS_WINDOWS)  | 
312  |  | #  if defined(WSAEWOULDBLOCK)  | 
313  |  |     case WSAEWOULDBLOCK:  | 
314  |  | #  endif  | 
315  |  | # endif  | 
316  |  |  | 
317  | 0  | # ifdef EWOULDBLOCK  | 
318  |  | #  ifdef WSAEWOULDBLOCK  | 
319  |  | #   if WSAEWOULDBLOCK != EWOULDBLOCK  | 
320  |  |     case EWOULDBLOCK:  | 
321  |  | #   endif  | 
322  |  | #  else  | 
323  | 0  |     case EWOULDBLOCK:  | 
324  | 0  | #  endif  | 
325  | 0  | # endif  | 
326  |  | 
  | 
327  | 0  | # if defined(ENOTCONN)  | 
328  | 0  |     case ENOTCONN:  | 
329  | 0  | # endif  | 
330  |  | 
  | 
331  | 0  | # ifdef EINTR  | 
332  | 0  |     case EINTR:  | 
333  | 0  | # endif  | 
334  |  | 
  | 
335  | 0  | # ifdef EAGAIN  | 
336  |  | #  if EWOULDBLOCK != EAGAIN  | 
337  |  |     case EAGAIN:  | 
338  |  | #  endif  | 
339  | 0  | # endif  | 
340  |  | 
  | 
341  | 0  | # ifdef EPROTO  | 
342  | 0  |     case EPROTO:  | 
343  | 0  | # endif  | 
344  |  | 
  | 
345  | 0  | # ifdef EINPROGRESS  | 
346  | 0  |     case EINPROGRESS:  | 
347  | 0  | # endif  | 
348  |  | 
  | 
349  | 0  | # ifdef EALREADY  | 
350  | 0  |     case EALREADY:  | 
351  | 0  | # endif  | 
352  | 0  |         return 1;  | 
353  | 0  |     default:  | 
354  | 0  |         break;  | 
355  | 0  |     }  | 
356  | 0  |     return 0;  | 
357  | 0  | }  | 
358  |  |  | 
359  |  | #endif                          /* #ifndef OPENSSL_NO_SOCK */  |