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 # else /* KERNEL_VERSION */
78 # include <netinet/tcp.h>
79 # include <linux/tls.h>
80 # include <linux/socket.h>
81 # include "openssl/ssl3.h"
82 # include "openssl/tls1.h"
83 # include "openssl/evp.h"
94 * When successful, this socket option doesn't change the behaviour of the
95 * TCP socket, except changing the TCP setsockopt handler to enable the
96 * processing of SOL_TLS socket options. All other functionality remains the
99 static ossl_inline int ktls_enable(int fd)
101 return setsockopt(fd, SOL_TCP, TCP_ULP, "tls", sizeof("tls")) ? 0 : 1;
105 * The TLS_TX socket option changes the send/sendmsg handlers of the TCP socket.
106 * If successful, then data sent using this socket will be encrypted and
107 * encapsulated in TLS records using the crypto_info provided here.
108 * The TLS_RX socket option changes the recv/recvmsg handlers of the TCP socket.
109 * If successful, then data received using this socket will be decrypted,
110 * authenticated and decapsulated using the crypto_info provided here.
112 static ossl_inline int ktls_start(int fd,
113 struct tls12_crypto_info_aes_gcm_128
114 *crypto_info, size_t len, int is_tx)
116 return setsockopt(fd, SOL_TLS, is_tx ? TLS_TX : TLS_RX,
117 crypto_info, sizeof(*crypto_info)) ? 0 : 1;
121 * Send a TLS record using the crypto_info provided in ktls_start and use
122 * record_type instead of the default SSL3_RT_APPLICATION_DATA.
123 * When the socket is non-blocking, then this call either returns EAGAIN or
124 * the entire record is pushed to TCP. It is impossible to send a partial
125 * record using this control message.
127 static ossl_inline int ktls_send_ctrl_message(int fd, unsigned char record_type,
128 const void *data, size_t length)
131 int cmsg_len = sizeof(record_type);
132 struct cmsghdr *cmsg;
135 char buf[CMSG_SPACE(sizeof(unsigned char))];
137 struct iovec msg_iov; /* Vector of data to send/receive into */
139 memset(&msg, 0, sizeof(msg));
140 msg.msg_control = cmsgbuf.buf;
141 msg.msg_controllen = sizeof(cmsgbuf.buf);
142 cmsg = CMSG_FIRSTHDR(&msg);
143 cmsg->cmsg_level = SOL_TLS;
144 cmsg->cmsg_type = TLS_SET_RECORD_TYPE;
145 cmsg->cmsg_len = CMSG_LEN(cmsg_len);
146 *((unsigned char *)CMSG_DATA(cmsg)) = record_type;
147 msg.msg_controllen = cmsg->cmsg_len;
149 msg_iov.iov_base = (void *)data;
150 msg_iov.iov_len = length;
151 msg.msg_iov = &msg_iov;
154 return sendmsg(fd, &msg, 0);
157 # define K_MIN1_RX 17
158 # if LINUX_VERSION_CODE < KERNEL_VERSION(K_MAJ, K_MIN1_RX, K_MIN2)
161 # warning "KTLS requires Kernel Headers >= 4.17.0 for receiving"
162 # warning "Skipping Compilation of KTLS receive data path"
165 static ossl_inline int ktls_read_record(int fd, void *data, size_t length)
173 * Receive a TLS record using the crypto_info provided in ktls_start.
174 * The kernel strips the TLS record header, IV and authentication tag,
175 * returning only the plaintext data or an error on failure.
176 * We add the TLS record header here to satisfy routines in rec_layer_s3.c
178 static ossl_inline int ktls_read_record(int fd, void *data, size_t length)
181 struct cmsghdr *cmsg;
184 char buf[CMSG_SPACE(sizeof(unsigned char))];
186 struct iovec msg_iov;
188 unsigned char *p = data;
189 const size_t prepend_length = SSL3_RT_HEADER_LENGTH;
191 if (length < prepend_length + EVP_GCM_TLS_TAG_LEN) {
196 memset(&msg, 0, sizeof(msg));
197 msg.msg_control = cmsgbuf.buf;
198 msg.msg_controllen = sizeof(cmsgbuf.buf);
200 msg_iov.iov_base = p + prepend_length;
201 msg_iov.iov_len = length - prepend_length - EVP_GCM_TLS_TAG_LEN;
202 msg.msg_iov = &msg_iov;
205 ret = recvmsg(fd, &msg, 0);
209 if (msg.msg_controllen > 0) {
210 cmsg = CMSG_FIRSTHDR(&msg);
211 if (cmsg->cmsg_type == TLS_GET_RECORD_TYPE) {
212 p[0] = *((unsigned char *)CMSG_DATA(cmsg));
213 p[1] = TLS1_2_VERSION_MAJOR;
214 p[2] = TLS1_2_VERSION_MINOR;
215 /* returned length is limited to msg_iov.iov_len above */
216 p[3] = (ret >> 8) & 0xff;
218 ret += prepend_length;