Handle partial data re-sending on ktls/sendfile on FreeBSD
[openssl.git] / include / internal / ktls.h
1 /*
2  * Copyright 2018-2021 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 #if defined(OPENSSL_SYS_LINUX)
11 # ifndef OPENSSL_NO_KTLS
12 #  include <linux/version.h>
13 #  if LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0)
14 #   define OPENSSL_NO_KTLS
15 #   ifndef PEDANTIC
16 #    warning "KTLS requires Kernel Headers >= 4.13.0"
17 #    warning "Skipping Compilation of KTLS"
18 #   endif
19 #  endif
20 # endif
21 #endif
22
23 #ifndef HEADER_INTERNAL_KTLS
24 # define HEADER_INTERNAL_KTLS
25 # ifndef OPENSSL_NO_KTLS
26
27 #  if defined(__FreeBSD__)
28 #   include <sys/types.h>
29 #   include <sys/socket.h>
30 #   include <sys/ktls.h>
31 #   include <netinet/in.h>
32 #   include <netinet/tcp.h>
33 #   include "openssl/ssl3.h"
34
35 #   ifndef TCP_RXTLS_ENABLE
36 #    define OPENSSL_NO_KTLS_RX
37 #   endif
38 #   define OPENSSL_KTLS_AES_GCM_128
39 #   define OPENSSL_KTLS_AES_GCM_256
40 #   define OPENSSL_KTLS_TLS13
41
42 /*
43  * Only used by the tests in sslapitest.c.
44  */
45 #   define TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE             8
46 #   define TLS_CIPHER_AES_GCM_256_REC_SEQ_SIZE             8
47
48 typedef struct tls_enable ktls_crypto_info_t;
49
50 /*
51  * FreeBSD does not require any additional steps to enable KTLS before
52  * setting keys.
53  */
54 static ossl_inline int ktls_enable(int fd)
55 {
56     return 1;
57 }
58
59 /*
60  * The TCP_TXTLS_ENABLE socket option marks the outgoing socket buffer
61  * as using TLS.  If successful, then data sent using this socket will
62  * be encrypted and encapsulated in TLS records using the tls_en
63  * provided here.
64  *
65  * The TCP_RXTLS_ENABLE socket option marks the incoming socket buffer
66  * as using TLS.  If successful, then data received for this socket will
67  * be authenticated and decrypted using the tls_en provided here.
68  */
69 static ossl_inline int ktls_start(int fd, ktls_crypto_info_t *tls_en, int is_tx)
70 {
71     if (is_tx)
72         return setsockopt(fd, IPPROTO_TCP, TCP_TXTLS_ENABLE,
73                           tls_en, sizeof(*tls_en)) ? 0 : 1;
74 #   ifndef OPENSSL_NO_KTLS_RX
75     return setsockopt(fd, IPPROTO_TCP, TCP_RXTLS_ENABLE, tls_en,
76                       sizeof(*tls_en)) ? 0 : 1;
77 #   else
78     return 0;
79 #   endif
80 }
81
82 /*
83  * Send a TLS record using the tls_en provided in ktls_start and use
84  * record_type instead of the default SSL3_RT_APPLICATION_DATA.
85  * When the socket is non-blocking, then this call either returns EAGAIN or
86  * the entire record is pushed to TCP. It is impossible to send a partial
87  * record using this control message.
88  */
89 static ossl_inline int ktls_send_ctrl_message(int fd, unsigned char record_type,
90                                               const void *data, size_t length)
91 {
92     struct msghdr msg = { 0 };
93     int cmsg_len = sizeof(record_type);
94     struct cmsghdr *cmsg;
95     char buf[CMSG_SPACE(cmsg_len)];
96     struct iovec msg_iov;   /* Vector of data to send/receive into */
97
98     msg.msg_control = buf;
99     msg.msg_controllen = sizeof(buf);
100     cmsg = CMSG_FIRSTHDR(&msg);
101     cmsg->cmsg_level = IPPROTO_TCP;
102     cmsg->cmsg_type = TLS_SET_RECORD_TYPE;
103     cmsg->cmsg_len = CMSG_LEN(cmsg_len);
104     *((unsigned char *)CMSG_DATA(cmsg)) = record_type;
105     msg.msg_controllen = cmsg->cmsg_len;
106
107     msg_iov.iov_base = (void *)data;
108     msg_iov.iov_len = length;
109     msg.msg_iov = &msg_iov;
110     msg.msg_iovlen = 1;
111
112     return sendmsg(fd, &msg, 0);
113 }
114
115 #   ifdef OPENSSL_NO_KTLS_RX
116
117 static ossl_inline int ktls_read_record(int fd, void *data, size_t length)
118 {
119     return -1;
120 }
121
122 #   else /* !defined(OPENSSL_NO_KTLS_RX) */
123
124 /*
125  * Receive a TLS record using the tls_en provided in ktls_start.  The
126  * kernel strips any explicit IV and authentication tag, but provides
127  * the TLS record header via a control message.  If there is an error
128  * with the TLS record such as an invalid header, invalid padding, or
129  * authentication failure recvmsg() will fail with an error.
130  */
131 static ossl_inline int ktls_read_record(int fd, void *data, size_t length)
132 {
133     struct msghdr msg = { 0 };
134     int cmsg_len = sizeof(struct tls_get_record);
135     struct tls_get_record *tgr;
136     struct cmsghdr *cmsg;
137     char buf[CMSG_SPACE(cmsg_len)];
138     struct iovec msg_iov;   /* Vector of data to send/receive into */
139     int ret;
140     unsigned char *p = data;
141     const size_t prepend_length = SSL3_RT_HEADER_LENGTH;
142
143     if (length <= prepend_length) {
144         errno = EINVAL;
145         return -1;
146     }
147
148     msg.msg_control = buf;
149     msg.msg_controllen = sizeof(buf);
150
151     msg_iov.iov_base = p + prepend_length;
152     msg_iov.iov_len = length - prepend_length;
153     msg.msg_iov = &msg_iov;
154     msg.msg_iovlen = 1;
155
156     ret = recvmsg(fd, &msg, 0);
157     if (ret <= 0)
158         return ret;
159
160     if ((msg.msg_flags & (MSG_EOR | MSG_CTRUNC)) != MSG_EOR) {
161         errno = EMSGSIZE;
162         return -1;
163     }
164
165     if (msg.msg_controllen == 0) {
166         errno = EBADMSG;
167         return -1;
168     }
169
170     cmsg = CMSG_FIRSTHDR(&msg);
171     if (cmsg->cmsg_level != IPPROTO_TCP || cmsg->cmsg_type != TLS_GET_RECORD
172         || cmsg->cmsg_len != CMSG_LEN(cmsg_len)) {
173         errno = EBADMSG;
174         return -1;
175     }
176
177     tgr = (struct tls_get_record *)CMSG_DATA(cmsg);
178     p[0] = tgr->tls_type;
179     p[1] = tgr->tls_vmajor;
180     p[2] = tgr->tls_vminor;
181     *(uint16_t *)(p + 3) = htons(ret);
182
183     return ret + prepend_length;
184 }
185
186 #   endif /* OPENSSL_NO_KTLS_RX */
187
188 /*
189  * KTLS enables the sendfile system call to send data from a file over
190  * TLS.
191  */
192 static ossl_inline ossl_ssize_t ktls_sendfile(int s, int fd, off_t off,
193                                               size_t size, int flags)
194 {
195     off_t sbytes = 0;
196     int ret;
197
198     ret = sendfile(fd, s, off, size, NULL, &sbytes, flags);
199     if (ret == -1 && sbytes == 0)
200         return -1;
201     return sbytes;
202 }
203
204 #  endif                         /* __FreeBSD__ */
205
206 #  if defined(OPENSSL_SYS_LINUX)
207
208 #   include <linux/tls.h>
209 #   if LINUX_VERSION_CODE < KERNEL_VERSION(4, 17, 0)
210 #    define OPENSSL_NO_KTLS_RX
211 #    ifndef PEDANTIC
212 #     warning "KTLS requires Kernel Headers >= 4.17.0 for receiving"
213 #     warning "Skipping Compilation of KTLS receive data path"
214 #    endif
215 #   endif
216 #   define OPENSSL_KTLS_AES_GCM_128
217 #   if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 1, 0)
218 #    define OPENSSL_KTLS_AES_GCM_256
219 #    define OPENSSL_KTLS_TLS13
220 #    if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 2, 0)
221 #     define OPENSSL_KTLS_AES_CCM_128
222 #     if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 11, 0)
223 #      ifndef OPENSSL_NO_CHACHA
224 #       define OPENSSL_KTLS_CHACHA20_POLY1305
225 #      endif
226 #     endif
227 #    endif
228 #   endif
229
230 #   include <sys/sendfile.h>
231 #   include <netinet/tcp.h>
232 #   include <linux/socket.h>
233 #   include "openssl/ssl3.h"
234 #   include "openssl/tls1.h"
235 #   include "openssl/evp.h"
236
237 #   ifndef SOL_TLS
238 #    define SOL_TLS 282
239 #   endif
240
241 #   ifndef TCP_ULP
242 #    define TCP_ULP 31
243 #   endif
244
245 #   ifndef TLS_RX
246 #    define TLS_RX                  2
247 #   endif
248
249 struct tls_crypto_info_all {
250     union {
251 #   ifdef OPENSSL_KTLS_AES_GCM_128
252         struct tls12_crypto_info_aes_gcm_128 gcm128;
253 #   endif
254 #   ifdef OPENSSL_KTLS_AES_GCM_256
255         struct tls12_crypto_info_aes_gcm_256 gcm256;
256 #   endif
257 #   ifdef OPENSSL_KTLS_AES_CCM_128
258         struct tls12_crypto_info_aes_ccm_128 ccm128;
259 #   endif
260 #   ifdef OPENSSL_KTLS_CHACHA20_POLY1305
261         struct tls12_crypto_info_chacha20_poly1305 chacha20poly1305;
262 #   endif
263     };
264     size_t tls_crypto_info_len;
265 };
266
267 typedef struct tls_crypto_info_all ktls_crypto_info_t;
268
269 /*
270  * When successful, this socket option doesn't change the behaviour of the
271  * TCP socket, except changing the TCP setsockopt handler to enable the
272  * processing of SOL_TLS socket options. All other functionality remains the
273  * same.
274  */
275 static ossl_inline int ktls_enable(int fd)
276 {
277     return setsockopt(fd, SOL_TCP, TCP_ULP, "tls", sizeof("tls")) ? 0 : 1;
278 }
279
280 /*
281  * The TLS_TX socket option changes the send/sendmsg handlers of the TCP socket.
282  * If successful, then data sent using this socket will be encrypted and
283  * encapsulated in TLS records using the crypto_info provided here.
284  * The TLS_RX socket option changes the recv/recvmsg handlers of the TCP socket.
285  * If successful, then data received using this socket will be decrypted,
286  * authenticated and decapsulated using the crypto_info provided here.
287  */
288 static ossl_inline int ktls_start(int fd, ktls_crypto_info_t *crypto_info,
289                                   int is_tx)
290 {
291     return setsockopt(fd, SOL_TLS, is_tx ? TLS_TX : TLS_RX,
292                       crypto_info, crypto_info->tls_crypto_info_len) ? 0 : 1;
293 }
294
295 /*
296  * Send a TLS record using the crypto_info provided in ktls_start and use
297  * record_type instead of the default SSL3_RT_APPLICATION_DATA.
298  * When the socket is non-blocking, then this call either returns EAGAIN or
299  * the entire record is pushed to TCP. It is impossible to send a partial
300  * record using this control message.
301  */
302 static ossl_inline int ktls_send_ctrl_message(int fd, unsigned char record_type,
303                                               const void *data, size_t length)
304 {
305     struct msghdr msg;
306     int cmsg_len = sizeof(record_type);
307     struct cmsghdr *cmsg;
308     union {
309         struct cmsghdr hdr;
310         char buf[CMSG_SPACE(sizeof(unsigned char))];
311     } cmsgbuf;
312     struct iovec msg_iov;       /* Vector of data to send/receive into */
313
314     memset(&msg, 0, sizeof(msg));
315     msg.msg_control = cmsgbuf.buf;
316     msg.msg_controllen = sizeof(cmsgbuf.buf);
317     cmsg = CMSG_FIRSTHDR(&msg);
318     cmsg->cmsg_level = SOL_TLS;
319     cmsg->cmsg_type = TLS_SET_RECORD_TYPE;
320     cmsg->cmsg_len = CMSG_LEN(cmsg_len);
321     *((unsigned char *)CMSG_DATA(cmsg)) = record_type;
322     msg.msg_controllen = cmsg->cmsg_len;
323
324     msg_iov.iov_base = (void *)data;
325     msg_iov.iov_len = length;
326     msg.msg_iov = &msg_iov;
327     msg.msg_iovlen = 1;
328
329     return sendmsg(fd, &msg, 0);
330 }
331
332 /*
333  * KTLS enables the sendfile system call to send data from a file over TLS.
334  * @flags are ignored on Linux. (placeholder for FreeBSD sendfile)
335  * */
336 static ossl_inline ossl_ssize_t ktls_sendfile(int s, int fd, off_t off, size_t size, int flags)
337 {
338     return sendfile(s, fd, &off, size);
339 }
340
341 #   ifdef OPENSSL_NO_KTLS_RX
342
343
344 static ossl_inline int ktls_read_record(int fd, void *data, size_t length)
345 {
346     return -1;
347 }
348
349 #   else /* !defined(OPENSSL_NO_KTLS_RX) */
350
351 /*
352  * Receive a TLS record using the crypto_info provided in ktls_start.
353  * The kernel strips the TLS record header, IV and authentication tag,
354  * returning only the plaintext data or an error on failure.
355  * We add the TLS record header here to satisfy routines in rec_layer_s3.c
356  */
357 static ossl_inline int ktls_read_record(int fd, void *data, size_t length)
358 {
359     struct msghdr msg;
360     struct cmsghdr *cmsg;
361     union {
362         struct cmsghdr hdr;
363         char buf[CMSG_SPACE(sizeof(unsigned char))];
364     } cmsgbuf;
365     struct iovec msg_iov;
366     int ret;
367     unsigned char *p = data;
368     const size_t prepend_length = SSL3_RT_HEADER_LENGTH;
369
370     if (length < prepend_length + EVP_GCM_TLS_TAG_LEN) {
371         errno = EINVAL;
372         return -1;
373     }
374
375     memset(&msg, 0, sizeof(msg));
376     msg.msg_control = cmsgbuf.buf;
377     msg.msg_controllen = sizeof(cmsgbuf.buf);
378
379     msg_iov.iov_base = p + prepend_length;
380     msg_iov.iov_len = length - prepend_length - EVP_GCM_TLS_TAG_LEN;
381     msg.msg_iov = &msg_iov;
382     msg.msg_iovlen = 1;
383
384     ret = recvmsg(fd, &msg, 0);
385     if (ret < 0)
386         return ret;
387
388     if (msg.msg_controllen > 0) {
389         cmsg = CMSG_FIRSTHDR(&msg);
390         if (cmsg->cmsg_type == TLS_GET_RECORD_TYPE) {
391             p[0] = *((unsigned char *)CMSG_DATA(cmsg));
392             p[1] = TLS1_2_VERSION_MAJOR;
393             p[2] = TLS1_2_VERSION_MINOR;
394             /* returned length is limited to msg_iov.iov_len above */
395             p[3] = (ret >> 8) & 0xff;
396             p[4] = ret & 0xff;
397             ret += prepend_length;
398         }
399     }
400
401     return ret;
402 }
403
404 #   endif /* OPENSSL_NO_KTLS_RX */
405
406 #  endif /* OPENSSL_SYS_LINUX */
407 # endif /* OPENSSL_NO_KTLS */
408 #endif /* HEADER_INTERNAL_KTLS */