2 * Copyright 1995-2023 The OpenSSL Project Authors. All Rights Reserved.
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
12 #include "bio_local.h"
13 #include "internal/bio_tfo.h"
14 #include "internal/cryptlib.h"
15 #include "internal/ktls.h"
17 #ifndef OPENSSL_NO_SOCK
19 # include <openssl/bio.h>
22 /* Watt-32 uses same names */
26 # define sock_write SockWrite
27 # define sock_read SockRead
28 # define sock_puts SockPuts
34 #ifndef OPENSSL_NO_KTLS
35 unsigned char ktls_record_type;
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);
47 static const BIO_METHOD methods_sockp = {
55 NULL, /* sock_gets, */
59 NULL, /* sock_callback_ctrl */
62 const BIO_METHOD *BIO_s_socket(void)
64 return &methods_sockp;
67 BIO *BIO_new_socket(int fd, int close_flag)
71 ret = BIO_new(BIO_s_socket());
74 BIO_set_fd(ret, fd, close_flag);
75 # ifndef OPENSSL_NO_KTLS
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.
89 static int sock_new(BIO *bi)
94 bi->ptr = OPENSSL_zalloc(sizeof(struct bss_sock_st));
100 static int sock_free(BIO *a)
106 BIO_closesocket(a->num);
111 OPENSSL_free(a->ptr);
116 static int sock_read(BIO *b, char *out, int outl)
121 clear_socket_error();
122 # ifndef OPENSSL_NO_KTLS
123 if (BIO_get_ktls_recv(b))
124 ret = ktls_read_record(b->num, out, outl);
127 ret = readsocket(b->num, out, outl);
128 BIO_clear_retry_flags(b);
130 if (BIO_sock_should_retry(ret))
131 BIO_set_retry_read(b);
133 b->flags |= BIO_FLAGS_IN_EOF;
139 static int sock_write(BIO *b, const char *in, int inl)
142 # if !defined(OPENSSL_NO_KTLS) || defined(OSSL_TFO_SENDTO)
143 struct bss_sock_st *data = (struct bss_sock_st *)b->ptr;
146 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);
153 BIO_clear_ktls_ctrl_msg_flag(b);
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);
162 ret = sendto(b->num, in, inl, OSSL_TFO_SENDTO,
163 BIO_ADDR_sockaddr(&data->tfo_peer), peerlen);
167 ret = writesocket(b->num, in, inl);
168 BIO_clear_retry_flags(b);
170 if (BIO_sock_should_retry(ret))
171 BIO_set_retry_write(b);
176 static long sock_ctrl(BIO *b, int cmd, long num, void *ptr)
180 struct bss_sock_st *data = (struct bss_sock_st *)b->ptr;
181 # ifndef OPENSSL_NO_KTLS
182 ktls_crypto_info_t *crypto_info;
187 /* minimal sock_free() */
190 BIO_closesocket(b->num);
193 b->num = *((int *)ptr);
194 b->shutdown = (int)num;
197 memset(&data->tfo_peer, 0, sizeof(data->tfo_peer));
208 case BIO_CTRL_GET_CLOSE:
211 case BIO_CTRL_SET_CLOSE:
212 b->shutdown = (int)num;
218 case BIO_CTRL_GET_RPOLL_DESCRIPTOR:
219 case BIO_CTRL_GET_WPOLL_DESCRIPTOR:
221 BIO_POLL_DESCRIPTOR *pd = ptr;
228 pd->type = BIO_POLL_DESCRIPTOR_TYPE_SOCK_FD;
229 pd->value.fd = b->num;
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);
237 BIO_set_ktls_flag(b, num);
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;
248 case BIO_CTRL_CLEAR_KTLS_TX_CTRL_MSG:
249 BIO_clear_ktls_ctrl_msg_flag(b);
252 case BIO_CTRL_SET_KTLS_TX_ZEROCOPY_SENDFILE:
253 ret = ktls_enable_tx_zerocopy_sendfile(b->num);
255 BIO_set_ktls_zerocopy_sendfile_flag(b);
259 ret = (b->flags & BIO_FLAGS_IN_EOF) != 0;
261 case BIO_C_GET_CONNECT:
262 if (ptr != NULL && num == 2) {
263 const char **pptr = (const char **)ptr;
265 *pptr = (const char *)&data->tfo_peer;
270 case BIO_C_SET_CONNECT:
271 if (ptr != NULL && num == 2) {
272 ret = BIO_ADDR_make(&data->tfo_peer,
273 BIO_ADDR_sockaddr((const BIO_ADDR *)ptr));
287 static int sock_puts(BIO *bp, const char *str)
292 ret = sock_write(bp, str, n);
296 int BIO_sock_should_retry(int i)
300 if ((i == 0) || (i == -1)) {
301 err = get_last_socket_error();
303 return BIO_sock_non_fatal_error(err);
308 int BIO_sock_non_fatal_error(int err)
311 # if defined(OPENSSL_SYS_WINDOWS)
312 # if defined(WSAEWOULDBLOCK)
318 # ifdef WSAEWOULDBLOCK
319 # if WSAEWOULDBLOCK != EWOULDBLOCK
327 # if defined(ENOTCONN)
336 # if EWOULDBLOCK != EAGAIN
359 #endif /* #ifndef OPENSSL_NO_SOCK */