VirtualBox

source: vbox/trunk/src/libs/openssl-3.1.3/include/internal/ktls.h@ 102210

Last change on this file since 102210 was 101211, checked in by vboxsync, 16 months ago

openssl-3.1.3: Applied and adjusted our OpenSSL changes to 3.1.2. bugref:10527

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette