Add support for in-kernel TLS (KTLS) on FreeBSD.
[openssl.git] / crypto / bio / bss_sock.c
1 /*
2  * Copyright 1995-2016 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         }
122     }
123     return ret;
124 }
125
126 static int sock_write(BIO *b, const char *in, int inl)
127 {
128     int ret = 0;
129
130     clear_socket_error();
131 # ifndef OPENSSL_NO_KTLS
132     if (BIO_should_ktls_ctrl_msg_flag(b)) {
133         unsigned char record_type = (intptr_t)b->ptr;
134         ret = ktls_send_ctrl_message(b->num, record_type, in, inl);
135         if (ret >= 0) {
136             ret = inl;
137             BIO_clear_ktls_ctrl_msg_flag(b);
138         }
139     } else
140 # endif
141         ret = writesocket(b->num, in, inl);
142     BIO_clear_retry_flags(b);
143     if (ret <= 0) {
144         if (BIO_sock_should_retry(ret))
145             BIO_set_retry_write(b);
146     }
147     return ret;
148 }
149
150 static long sock_ctrl(BIO *b, int cmd, long num, void *ptr)
151 {
152     long ret = 1;
153     int *ip;
154 # ifndef OPENSSL_NO_KTLS
155 #  ifdef __FreeBSD__
156     struct tls_enable *crypto_info;
157 #  else
158     struct tls12_crypto_info_aes_gcm_128 *crypto_info;
159 #  endif
160 # endif
161
162     switch (cmd) {
163     case BIO_C_SET_FD:
164         sock_free(b);
165         b->num = *((int *)ptr);
166         b->shutdown = (int)num;
167         b->init = 1;
168         break;
169     case BIO_C_GET_FD:
170         if (b->init) {
171             ip = (int *)ptr;
172             if (ip != NULL)
173                 *ip = b->num;
174             ret = b->num;
175         } else
176             ret = -1;
177         break;
178     case BIO_CTRL_GET_CLOSE:
179         ret = b->shutdown;
180         break;
181     case BIO_CTRL_SET_CLOSE:
182         b->shutdown = (int)num;
183         break;
184     case BIO_CTRL_DUP:
185     case BIO_CTRL_FLUSH:
186         ret = 1;
187         break;
188 # ifndef OPENSSL_NO_KTLS
189     case BIO_CTRL_SET_KTLS:
190 #  ifdef __FreeBSD__
191         crypto_info = (struct tls_enable *)ptr;
192 #  else
193         crypto_info = (struct tls12_crypto_info_aes_gcm_128 *)ptr;
194 #  endif
195         ret = ktls_start(b->num, crypto_info, sizeof(*crypto_info), num);
196         if (ret)
197             BIO_set_ktls_flag(b, num);
198         break;
199     case BIO_CTRL_GET_KTLS_SEND:
200         return BIO_should_ktls_flag(b, 1);
201     case BIO_CTRL_GET_KTLS_RECV:
202         return BIO_should_ktls_flag(b, 0);
203     case BIO_CTRL_SET_KTLS_TX_SEND_CTRL_MSG:
204         BIO_set_ktls_ctrl_msg_flag(b);
205         b->ptr = (void *)num;
206         ret = 0;
207         break;
208     case BIO_CTRL_CLEAR_KTLS_TX_CTRL_MSG:
209         BIO_clear_ktls_ctrl_msg_flag(b);
210         ret = 0;
211         break;
212 # endif
213     default:
214         ret = 0;
215         break;
216     }
217     return ret;
218 }
219
220 static int sock_puts(BIO *bp, const char *str)
221 {
222     int n, ret;
223
224     n = strlen(str);
225     ret = sock_write(bp, str, n);
226     return ret;
227 }
228
229 int BIO_sock_should_retry(int i)
230 {
231     int err;
232
233     if ((i == 0) || (i == -1)) {
234         err = get_last_socket_error();
235
236         return BIO_sock_non_fatal_error(err);
237     }
238     return 0;
239 }
240
241 int BIO_sock_non_fatal_error(int err)
242 {
243     switch (err) {
244 # if defined(OPENSSL_SYS_WINDOWS)
245 #  if defined(WSAEWOULDBLOCK)
246     case WSAEWOULDBLOCK:
247 #  endif
248 # endif
249
250 # ifdef EWOULDBLOCK
251 #  ifdef WSAEWOULDBLOCK
252 #   if WSAEWOULDBLOCK != EWOULDBLOCK
253     case EWOULDBLOCK:
254 #   endif
255 #  else
256     case EWOULDBLOCK:
257 #  endif
258 # endif
259
260 # if defined(ENOTCONN)
261     case ENOTCONN:
262 # endif
263
264 # ifdef EINTR
265     case EINTR:
266 # endif
267
268 # ifdef EAGAIN
269 #  if EWOULDBLOCK != EAGAIN
270     case EAGAIN:
271 #  endif
272 # endif
273
274 # ifdef EPROTO
275     case EPROTO:
276 # endif
277
278 # ifdef EINPROGRESS
279     case EINPROGRESS:
280 # endif
281
282 # ifdef EALREADY
283     case EALREADY:
284 # endif
285         return 1;
286     default:
287         break;
288     }
289     return 0;
290 }
291
292 #endif                          /* #ifndef OPENSSL_NO_SOCK */