kTLS: add support for AES_CCM128 and AES_GCM256
[openssl.git] / crypto / bio / bss_sock.c
1 /*
2  * Copyright 1995-2020 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     /* TODO: Convert to new style write function */
42     bwrite_conv,
43     sock_write,
44     /* TODO: Convert to new style read function */
45     bread_conv,
46     sock_read,
47     sock_puts,
48     NULL,                       /* sock_gets,         */
49     sock_ctrl,
50     sock_new,
51     sock_free,
52     NULL,                       /* sock_callback_ctrl */
53 };
54
55 const BIO_METHOD *BIO_s_socket(void)
56 {
57     return &methods_sockp;
58 }
59
60 BIO *BIO_new_socket(int fd, int close_flag)
61 {
62     BIO *ret;
63
64     ret = BIO_new(BIO_s_socket());
65     if (ret == NULL)
66         return NULL;
67     BIO_set_fd(ret, fd, close_flag);
68 # ifndef OPENSSL_NO_KTLS
69     {
70         /*
71          * The new socket is created successfully regardless of ktls_enable.
72          * ktls_enable doesn't change any functionality of the socket, except
73          * changing the setsockopt to enable the processing of ktls_start.
74          * Thus, it is not a problem to call it for non-TLS sockets.
75          */
76         ktls_enable(fd);
77     }
78 # endif
79     return ret;
80 }
81
82 static int sock_new(BIO *bi)
83 {
84     bi->init = 0;
85     bi->num = 0;
86     bi->ptr = NULL;
87     bi->flags = 0;
88     return 1;
89 }
90
91 static int sock_free(BIO *a)
92 {
93     if (a == NULL)
94         return 0;
95     if (a->shutdown) {
96         if (a->init) {
97             BIO_closesocket(a->num);
98         }
99         a->init = 0;
100         a->flags = 0;
101     }
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
132     clear_socket_error();
133 # ifndef OPENSSL_NO_KTLS
134     if (BIO_should_ktls_ctrl_msg_flag(b)) {
135         unsigned char record_type = (intptr_t)b->ptr;
136         ret = ktls_send_ctrl_message(b->num, record_type, in, inl);
137         if (ret >= 0) {
138             ret = inl;
139             BIO_clear_ktls_ctrl_msg_flag(b);
140         }
141     } else
142 # endif
143         ret = writesocket(b->num, in, inl);
144     BIO_clear_retry_flags(b);
145     if (ret <= 0) {
146         if (BIO_sock_should_retry(ret))
147             BIO_set_retry_write(b);
148     }
149     return ret;
150 }
151
152 static long sock_ctrl(BIO *b, int cmd, long num, void *ptr)
153 {
154     long ret = 1;
155     int *ip;
156 # ifndef OPENSSL_NO_KTLS
157     size_t crypto_info_len;
158 #  ifdef __FreeBSD__
159     struct tls_enable *crypto_info;
160 #  else
161     struct tls_crypto_info_all *crypto_info;
162 #  endif
163 # endif
164
165     switch (cmd) {
166     case BIO_C_SET_FD:
167         sock_free(b);
168         b->num = *((int *)ptr);
169         b->shutdown = (int)num;
170         b->init = 1;
171         break;
172     case BIO_C_GET_FD:
173         if (b->init) {
174             ip = (int *)ptr;
175             if (ip != NULL)
176                 *ip = b->num;
177             ret = b->num;
178         } else
179             ret = -1;
180         break;
181     case BIO_CTRL_GET_CLOSE:
182         ret = b->shutdown;
183         break;
184     case BIO_CTRL_SET_CLOSE:
185         b->shutdown = (int)num;
186         break;
187     case BIO_CTRL_DUP:
188     case BIO_CTRL_FLUSH:
189         ret = 1;
190         break;
191 # ifndef OPENSSL_NO_KTLS
192     case BIO_CTRL_SET_KTLS:
193 #  ifdef __FreeBSD__
194         crypto_info = (struct tls_enable *)ptr;
195         crypto_info_len = sizeof(*crypto_info);
196 #  else
197         crypto_info = (struct tls_crypto_info_all *)ptr;
198         crypto_info_len = crypto_info->tls_crypto_info_len;
199 #  endif
200         ret = ktls_start(b->num, crypto_info, crypto_info_len, num);
201         if (ret)
202             BIO_set_ktls_flag(b, num);
203         break;
204     case BIO_CTRL_GET_KTLS_SEND:
205         return BIO_should_ktls_flag(b, 1);
206     case BIO_CTRL_GET_KTLS_RECV:
207         return BIO_should_ktls_flag(b, 0);
208     case BIO_CTRL_SET_KTLS_TX_SEND_CTRL_MSG:
209         BIO_set_ktls_ctrl_msg_flag(b);
210         b->ptr = (void *)num;
211         ret = 0;
212         break;
213     case BIO_CTRL_CLEAR_KTLS_TX_CTRL_MSG:
214         BIO_clear_ktls_ctrl_msg_flag(b);
215         ret = 0;
216         break;
217 # endif
218     case BIO_CTRL_EOF:
219         ret = (b->flags & BIO_FLAGS_IN_EOF) != 0 ? 1 : 0;
220         break;
221     default:
222         ret = 0;
223         break;
224     }
225     return ret;
226 }
227
228 static int sock_puts(BIO *bp, const char *str)
229 {
230     int n, ret;
231
232     n = strlen(str);
233     ret = sock_write(bp, str, n);
234     return ret;
235 }
236
237 int BIO_sock_should_retry(int i)
238 {
239     int err;
240
241     if ((i == 0) || (i == -1)) {
242         err = get_last_socket_error();
243
244         return BIO_sock_non_fatal_error(err);
245     }
246     return 0;
247 }
248
249 int BIO_sock_non_fatal_error(int err)
250 {
251     switch (err) {
252 # if defined(OPENSSL_SYS_WINDOWS)
253 #  if defined(WSAEWOULDBLOCK)
254     case WSAEWOULDBLOCK:
255 #  endif
256 # endif
257
258 # ifdef EWOULDBLOCK
259 #  ifdef WSAEWOULDBLOCK
260 #   if WSAEWOULDBLOCK != EWOULDBLOCK
261     case EWOULDBLOCK:
262 #   endif
263 #  else
264     case EWOULDBLOCK:
265 #  endif
266 # endif
267
268 # if defined(ENOTCONN)
269     case ENOTCONN:
270 # endif
271
272 # ifdef EINTR
273     case EINTR:
274 # endif
275
276 # ifdef EAGAIN
277 #  if EWOULDBLOCK != EAGAIN
278     case EAGAIN:
279 #  endif
280 # endif
281
282 # ifdef EPROTO
283     case EPROTO:
284 # endif
285
286 # ifdef EINPROGRESS
287     case EINPROGRESS:
288 # endif
289
290 # ifdef EALREADY
291     case EALREADY:
292 # endif
293         return 1;
294     default:
295         break;
296     }
297     return 0;
298 }
299
300 #endif                          /* #ifndef OPENSSL_NO_SOCK */