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