2 * Copyright 2018 The OpenSSL Project Authors. All Rights Reserved.
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
10 #ifndef OPENSSL_NO_KTLS
11 # ifndef HEADER_INTERNAL_KTLS
12 # define HEADER_INTERNAL_KTLS
14 # if defined(__FreeBSD__)
15 # include <sys/types.h>
16 # include <sys/socket.h>
17 # include <sys/ktls.h>
18 # include <netinet/in.h>
19 # include <netinet/tcp.h>
20 # include <crypto/cryptodev.h>
23 * Only used by the tests in sslapitest.c.
25 # define TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE 8
28 * FreeBSD does not require any additional steps to enable KTLS before
31 static ossl_inline int ktls_enable(int fd)
37 * The TCP_TXTLS_ENABLE socket option marks the outgoing socket buffer
38 * as using TLS. If successful, then data sent using this socket will
39 * be encrypted and encapsulated in TLS records using the tls_en.
42 static ossl_inline int ktls_start(int fd,
43 struct tls_enable *tls_en,
44 size_t len, int is_tx)
47 return setsockopt(fd, IPPROTO_TCP, TCP_TXTLS_ENABLE,
48 tls_en, sizeof(*tls_en)) ? 0 : 1;
54 * Send a TLS record using the tls_en provided in ktls_start and use
55 * record_type instead of the default SSL3_RT_APPLICATION_DATA.
56 * When the socket is non-blocking, then this call either returns EAGAIN or
57 * the entire record is pushed to TCP. It is impossible to send a partial
58 * record using this control message.
60 static ossl_inline int ktls_send_ctrl_message(int fd, unsigned char record_type,
61 const void *data, size_t length)
63 struct msghdr msg = { 0 };
64 int cmsg_len = sizeof(record_type);
66 char buf[CMSG_SPACE(cmsg_len)];
67 struct iovec msg_iov; /* Vector of data to send/receive into */
69 msg.msg_control = buf;
70 msg.msg_controllen = sizeof(buf);
71 cmsg = CMSG_FIRSTHDR(&msg);
72 cmsg->cmsg_level = IPPROTO_TCP;
73 cmsg->cmsg_type = TLS_SET_RECORD_TYPE;
74 cmsg->cmsg_len = CMSG_LEN(cmsg_len);
75 *((unsigned char *)CMSG_DATA(cmsg)) = record_type;
76 msg.msg_controllen = cmsg->cmsg_len;
78 msg_iov.iov_base = (void *)data;
79 msg_iov.iov_len = length;
80 msg.msg_iov = &msg_iov;
83 return sendmsg(fd, &msg, 0);
86 static ossl_inline int ktls_read_record(int fd, void *data, size_t length)
92 * KTLS enables the sendfile system call to send data from a file over
95 static ossl_inline ossl_ssize_t ktls_sendfile(int s, int fd, off_t off,
96 size_t size, int flags)
101 ret = sendfile(fd, s, off, size, NULL, &sbytes, flags);
103 if (errno == EAGAIN && sbytes != 0)
109 # endif /* __FreeBSD__ */
111 # if defined(OPENSSL_SYS_LINUX)
112 # include <linux/version.h>
117 # if LINUX_VERSION_CODE < KERNEL_VERSION(K_MAJ, K_MIN1, K_MIN2)
120 # warning "KTLS requires Kernel Headers >= 4.13.0"
121 # warning "Skipping Compilation of KTLS"
127 # define TLS_CIPHER_AES_GCM_128 51
128 # define TLS_CIPHER_AES_GCM_128_IV_SIZE 8
129 # define TLS_CIPHER_AES_GCM_128_KEY_SIZE 16
130 # define TLS_CIPHER_AES_GCM_128_SALT_SIZE 4
131 # define TLS_CIPHER_AES_GCM_128_TAG_SIZE 16
132 # define TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE 8
134 # define TLS_SET_RECORD_TYPE 1
136 struct tls_crypto_info {
137 unsigned short version;
138 unsigned short cipher_type;
141 struct tls12_crypto_info_aes_gcm_128 {
142 struct tls_crypto_info info;
143 unsigned char iv[TLS_CIPHER_AES_GCM_128_IV_SIZE];
144 unsigned char key[TLS_CIPHER_AES_GCM_128_KEY_SIZE];
145 unsigned char salt[TLS_CIPHER_AES_GCM_128_SALT_SIZE];
146 unsigned char rec_seq[TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE];
149 /* Dummy functions here */
150 static ossl_inline int ktls_enable(int fd)
155 static ossl_inline int ktls_start(int fd,
156 struct tls12_crypto_info_aes_gcm_128
157 *crypto_info, size_t len, int is_tx)
162 static ossl_inline int ktls_send_ctrl_message(int fd, unsigned char record_type,
163 const void *data, size_t length)
168 static ossl_inline int ktls_read_record(int fd, void *data, size_t length)
173 static ossl_inline ossl_ssize_t ktls_sendfile(int s, int fd, off_t off, size_t size, int flags)
178 # else /* KERNEL_VERSION */
180 # include <sys/sendfile.h>
181 # include <netinet/tcp.h>
182 # include <linux/tls.h>
183 # include <linux/socket.h>
184 # include "openssl/ssl3.h"
185 # include "openssl/tls1.h"
186 # include "openssl/evp.h"
201 * When successful, this socket option doesn't change the behaviour of the
202 * TCP socket, except changing the TCP setsockopt handler to enable the
203 * processing of SOL_TLS socket options. All other functionality remains the
206 static ossl_inline int ktls_enable(int fd)
208 return setsockopt(fd, SOL_TCP, TCP_ULP, "tls", sizeof("tls")) ? 0 : 1;
212 * The TLS_TX socket option changes the send/sendmsg handlers of the TCP socket.
213 * If successful, then data sent using this socket will be encrypted and
214 * encapsulated in TLS records using the crypto_info provided here.
215 * The TLS_RX socket option changes the recv/recvmsg handlers of the TCP socket.
216 * If successful, then data received using this socket will be decrypted,
217 * authenticated and decapsulated using the crypto_info provided here.
219 static ossl_inline int ktls_start(int fd,
220 struct tls12_crypto_info_aes_gcm_128
221 *crypto_info, size_t len, int is_tx)
223 return setsockopt(fd, SOL_TLS, is_tx ? TLS_TX : TLS_RX,
224 crypto_info, sizeof(*crypto_info)) ? 0 : 1;
228 * Send a TLS record using the crypto_info provided in ktls_start and use
229 * record_type instead of the default SSL3_RT_APPLICATION_DATA.
230 * When the socket is non-blocking, then this call either returns EAGAIN or
231 * the entire record is pushed to TCP. It is impossible to send a partial
232 * record using this control message.
234 static ossl_inline int ktls_send_ctrl_message(int fd, unsigned char record_type,
235 const void *data, size_t length)
238 int cmsg_len = sizeof(record_type);
239 struct cmsghdr *cmsg;
242 char buf[CMSG_SPACE(sizeof(unsigned char))];
244 struct iovec msg_iov; /* Vector of data to send/receive into */
246 memset(&msg, 0, sizeof(msg));
247 msg.msg_control = cmsgbuf.buf;
248 msg.msg_controllen = sizeof(cmsgbuf.buf);
249 cmsg = CMSG_FIRSTHDR(&msg);
250 cmsg->cmsg_level = SOL_TLS;
251 cmsg->cmsg_type = TLS_SET_RECORD_TYPE;
252 cmsg->cmsg_len = CMSG_LEN(cmsg_len);
253 *((unsigned char *)CMSG_DATA(cmsg)) = record_type;
254 msg.msg_controllen = cmsg->cmsg_len;
256 msg_iov.iov_base = (void *)data;
257 msg_iov.iov_len = length;
258 msg.msg_iov = &msg_iov;
261 return sendmsg(fd, &msg, 0);
265 * KTLS enables the sendfile system call to send data from a file over TLS.
266 * @flags are ignored on Linux. (placeholder for FreeBSD sendfile)
268 static ossl_inline ossl_ssize_t ktls_sendfile(int s, int fd, off_t off, size_t size, int flags)
270 return sendfile(s, fd, &off, size);
273 # define K_MIN1_RX 17
274 # if LINUX_VERSION_CODE < KERNEL_VERSION(K_MAJ, K_MIN1_RX, K_MIN2)
277 # warning "KTLS requires Kernel Headers >= 4.17.0 for receiving"
278 # warning "Skipping Compilation of KTLS receive data path"
281 static ossl_inline int ktls_read_record(int fd, void *data, size_t length)
289 * Receive a TLS record using the crypto_info provided in ktls_start.
290 * The kernel strips the TLS record header, IV and authentication tag,
291 * returning only the plaintext data or an error on failure.
292 * We add the TLS record header here to satisfy routines in rec_layer_s3.c
294 static ossl_inline int ktls_read_record(int fd, void *data, size_t length)
297 struct cmsghdr *cmsg;
300 char buf[CMSG_SPACE(sizeof(unsigned char))];
302 struct iovec msg_iov;
304 unsigned char *p = data;
305 const size_t prepend_length = SSL3_RT_HEADER_LENGTH;
307 if (length < prepend_length + EVP_GCM_TLS_TAG_LEN) {
312 memset(&msg, 0, sizeof(msg));
313 msg.msg_control = cmsgbuf.buf;
314 msg.msg_controllen = sizeof(cmsgbuf.buf);
316 msg_iov.iov_base = p + prepend_length;
317 msg_iov.iov_len = length - prepend_length - EVP_GCM_TLS_TAG_LEN;
318 msg.msg_iov = &msg_iov;
321 ret = recvmsg(fd, &msg, 0);
325 if (msg.msg_controllen > 0) {
326 cmsg = CMSG_FIRSTHDR(&msg);
327 if (cmsg->cmsg_type == TLS_GET_RECORD_TYPE) {
328 p[0] = *((unsigned char *)CMSG_DATA(cmsg));
329 p[1] = TLS1_2_VERSION_MAJOR;
330 p[2] = TLS1_2_VERSION_MINOR;
331 /* returned length is limited to msg_iov.iov_len above */
332 p[3] = (ret >> 8) & 0xff;
334 ret += prepend_length;