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(OPENSSL_SYS_LINUX)
15 # include <linux/version.h>
20 # if LINUX_VERSION_CODE < KERNEL_VERSION(K_MAJ, K_MIN1, K_MIN2)
23 # warning "KTLS requires Kernel Headers >= 4.13.0"
24 # warning "Skipping Compilation of KTLS"
30 # define TLS_CIPHER_AES_GCM_128 51
31 # define TLS_CIPHER_AES_GCM_128_IV_SIZE 8
32 # define TLS_CIPHER_AES_GCM_128_KEY_SIZE 16
33 # define TLS_CIPHER_AES_GCM_128_SALT_SIZE 4
34 # define TLS_CIPHER_AES_GCM_128_TAG_SIZE 16
35 # define TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE 8
37 # define TLS_SET_RECORD_TYPE 1
39 struct tls_crypto_info {
40 unsigned short version;
41 unsigned short cipher_type;
44 struct tls12_crypto_info_aes_gcm_128 {
45 struct tls_crypto_info info;
46 unsigned char iv[TLS_CIPHER_AES_GCM_128_IV_SIZE];
47 unsigned char key[TLS_CIPHER_AES_GCM_128_KEY_SIZE];
48 unsigned char salt[TLS_CIPHER_AES_GCM_128_SALT_SIZE];
49 unsigned char rec_seq[TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE];
52 /* Dummy functions here */
53 static ossl_inline int ktls_enable(int fd)
58 static ossl_inline int ktls_start(int fd,
59 struct tls12_crypto_info_aes_gcm_128
60 *crypto_info, size_t len, int is_tx)
65 static ossl_inline int ktls_send_ctrl_message(int fd, unsigned char record_type,
66 const void *data, size_t length)
71 static ossl_inline int ktls_read_record(int fd, void *data, size_t length)
76 static ossl_inline ossl_ssize_t ktls_sendfile(int s, int fd, off_t off, size_t size, int flags)
81 # else /* KERNEL_VERSION */
83 # include <sys/sendfile.h>
84 # include <netinet/tcp.h>
85 # include <linux/tls.h>
86 # include <linux/socket.h>
87 # include "openssl/ssl3.h"
88 # include "openssl/tls1.h"
89 # include "openssl/evp.h"
104 * When successful, this socket option doesn't change the behaviour of the
105 * TCP socket, except changing the TCP setsockopt handler to enable the
106 * processing of SOL_TLS socket options. All other functionality remains the
109 static ossl_inline int ktls_enable(int fd)
111 return setsockopt(fd, SOL_TCP, TCP_ULP, "tls", sizeof("tls")) ? 0 : 1;
115 * The TLS_TX socket option changes the send/sendmsg handlers of the TCP socket.
116 * If successful, then data sent using this socket will be encrypted and
117 * encapsulated in TLS records using the crypto_info provided here.
118 * The TLS_RX socket option changes the recv/recvmsg handlers of the TCP socket.
119 * If successful, then data received using this socket will be decrypted,
120 * authenticated and decapsulated using the crypto_info provided here.
122 static ossl_inline int ktls_start(int fd,
123 struct tls12_crypto_info_aes_gcm_128
124 *crypto_info, size_t len, int is_tx)
126 return setsockopt(fd, SOL_TLS, is_tx ? TLS_TX : TLS_RX,
127 crypto_info, sizeof(*crypto_info)) ? 0 : 1;
131 * Send a TLS record using the crypto_info provided in ktls_start and use
132 * record_type instead of the default SSL3_RT_APPLICATION_DATA.
133 * When the socket is non-blocking, then this call either returns EAGAIN or
134 * the entire record is pushed to TCP. It is impossible to send a partial
135 * record using this control message.
137 static ossl_inline int ktls_send_ctrl_message(int fd, unsigned char record_type,
138 const void *data, size_t length)
141 int cmsg_len = sizeof(record_type);
142 struct cmsghdr *cmsg;
145 char buf[CMSG_SPACE(sizeof(unsigned char))];
147 struct iovec msg_iov; /* Vector of data to send/receive into */
149 memset(&msg, 0, sizeof(msg));
150 msg.msg_control = cmsgbuf.buf;
151 msg.msg_controllen = sizeof(cmsgbuf.buf);
152 cmsg = CMSG_FIRSTHDR(&msg);
153 cmsg->cmsg_level = SOL_TLS;
154 cmsg->cmsg_type = TLS_SET_RECORD_TYPE;
155 cmsg->cmsg_len = CMSG_LEN(cmsg_len);
156 *((unsigned char *)CMSG_DATA(cmsg)) = record_type;
157 msg.msg_controllen = cmsg->cmsg_len;
159 msg_iov.iov_base = (void *)data;
160 msg_iov.iov_len = length;
161 msg.msg_iov = &msg_iov;
164 return sendmsg(fd, &msg, 0);
168 * KTLS enables the sendfile system call to send data from a file over TLS.
169 * @flags are ignored on Linux. (placeholder for FreeBSD sendfile)
171 static ossl_inline ossl_ssize_t ktls_sendfile(int s, int fd, off_t off, size_t size, int flags)
173 return sendfile(s, fd, &off, size);
176 # define K_MIN1_RX 17
177 # if LINUX_VERSION_CODE < KERNEL_VERSION(K_MAJ, K_MIN1_RX, K_MIN2)
180 # warning "KTLS requires Kernel Headers >= 4.17.0 for receiving"
181 # warning "Skipping Compilation of KTLS receive data path"
184 static ossl_inline int ktls_read_record(int fd, void *data, size_t length)
192 * Receive a TLS record using the crypto_info provided in ktls_start.
193 * The kernel strips the TLS record header, IV and authentication tag,
194 * returning only the plaintext data or an error on failure.
195 * We add the TLS record header here to satisfy routines in rec_layer_s3.c
197 static ossl_inline int ktls_read_record(int fd, void *data, size_t length)
200 struct cmsghdr *cmsg;
203 char buf[CMSG_SPACE(sizeof(unsigned char))];
205 struct iovec msg_iov;
207 unsigned char *p = data;
208 const size_t prepend_length = SSL3_RT_HEADER_LENGTH;
210 if (length < prepend_length + EVP_GCM_TLS_TAG_LEN) {
215 memset(&msg, 0, sizeof(msg));
216 msg.msg_control = cmsgbuf.buf;
217 msg.msg_controllen = sizeof(cmsgbuf.buf);
219 msg_iov.iov_base = p + prepend_length;
220 msg_iov.iov_len = length - prepend_length - EVP_GCM_TLS_TAG_LEN;
221 msg.msg_iov = &msg_iov;
224 ret = recvmsg(fd, &msg, 0);
228 if (msg.msg_controllen > 0) {
229 cmsg = CMSG_FIRSTHDR(&msg);
230 if (cmsg->cmsg_type == TLS_GET_RECORD_TYPE) {
231 p[0] = *((unsigned char *)CMSG_DATA(cmsg));
232 p[1] = TLS1_2_VERSION_MAJOR;
233 p[2] = TLS1_2_VERSION_MINOR;
234 /* returned length is limited to msg_iov.iov_len above */
235 p[3] = (ret >> 8) & 0xff;
237 ret += prepend_length;