VirtualBox

source: vbox/trunk/src/libs/openssl-3.3.2/include/internal/ktls.h@ 108403

最後變更 在這個檔案從108403是 108206,由 vboxsync 提交於 5 週 前

openssl-3.3.2: Exported all files to OSE and removed .scm-settings ​bugref:10757

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

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