Path: blob/main/crypto/openssl/include/internal/ktls.h
106840 views
/*1* Copyright 2018-2024 The OpenSSL Project Authors. All Rights Reserved.2*3* Licensed under the Apache License 2.0 (the "License"). You may not use4* this file except in compliance with the License. You can obtain a copy5* in the file LICENSE in the source distribution or at6* https://www.openssl.org/source/license.html7*/89#if defined(OPENSSL_SYS_LINUX)10#ifndef OPENSSL_NO_KTLS11#include <linux/version.h>12#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0)13#define OPENSSL_NO_KTLS14#ifndef PEDANTIC15#warning "KTLS requires Kernel Headers >= 4.13.0"16#warning "Skipping Compilation of KTLS"17#endif18#endif19#endif20#endif2122#ifndef HEADER_INTERNAL_KTLS23#define HEADER_INTERNAL_KTLS24#pragma once2526#ifndef OPENSSL_NO_KTLS2728#if defined(__FreeBSD__)29#include <sys/types.h>30#include <sys/socket.h>31#include <sys/ktls.h>32#include <netinet/in.h>33#include <netinet/tcp.h>34#include <openssl/ssl3.h>3536#ifndef TCP_RXTLS_ENABLE37#define OPENSSL_NO_KTLS_RX38#endif39#define OPENSSL_KTLS_AES_GCM_12840#define OPENSSL_KTLS_AES_GCM_25641#define OPENSSL_KTLS_TLS1342#ifdef TLS_CHACHA20_IV_LEN43#ifndef OPENSSL_NO_CHACHA44#define OPENSSL_KTLS_CHACHA20_POLY130545#endif46#endif4748typedef struct tls_enable ktls_crypto_info_t;4950/*51* FreeBSD does not require any additional steps to enable KTLS before52* setting keys.53*/54static ossl_inline int ktls_enable(int fd)55{56return 1;57}5859/*60* The TCP_TXTLS_ENABLE socket option marks the outgoing socket buffer61* as using TLS. If successful, then data sent using this socket will62* be encrypted and encapsulated in TLS records using the tls_en63* provided here.64*65* The TCP_RXTLS_ENABLE socket option marks the incoming socket buffer66* as using TLS. If successful, then data received for this socket will67* be authenticated and decrypted using the tls_en provided here.68*/69static ossl_inline int ktls_start(int fd, ktls_crypto_info_t *tls_en, int is_tx)70{71if (is_tx)72return setsockopt(fd, IPPROTO_TCP, TCP_TXTLS_ENABLE,73tls_en, sizeof(*tls_en))74? 075: 1;76#ifndef OPENSSL_NO_KTLS_RX77return setsockopt(fd, IPPROTO_TCP, TCP_RXTLS_ENABLE, tls_en,78sizeof(*tls_en))79? 080: 1;81#else82return 0;83#endif84}8586/* Not supported on FreeBSD */87static ossl_inline int ktls_enable_tx_zerocopy_sendfile(int fd)88{89return 0;90}9192/*93* Send a TLS record using the tls_en provided in ktls_start and use94* record_type instead of the default SSL3_RT_APPLICATION_DATA.95* When the socket is non-blocking, then this call either returns EAGAIN or96* the entire record is pushed to TCP. It is impossible to send a partial97* record using this control message.98*/99static ossl_inline int ktls_send_ctrl_message(int fd, unsigned char record_type,100const void *data, size_t length)101{102struct msghdr msg = { 0 };103int cmsg_len = sizeof(record_type);104struct cmsghdr *cmsg;105char buf[CMSG_SPACE(cmsg_len)];106struct iovec msg_iov; /* Vector of data to send/receive into */107108msg.msg_control = buf;109msg.msg_controllen = sizeof(buf);110cmsg = CMSG_FIRSTHDR(&msg);111cmsg->cmsg_level = IPPROTO_TCP;112cmsg->cmsg_type = TLS_SET_RECORD_TYPE;113cmsg->cmsg_len = CMSG_LEN(cmsg_len);114*((unsigned char *)CMSG_DATA(cmsg)) = record_type;115msg.msg_controllen = cmsg->cmsg_len;116117msg_iov.iov_base = (void *)data;118msg_iov.iov_len = length;119msg.msg_iov = &msg_iov;120msg.msg_iovlen = 1;121122return sendmsg(fd, &msg, 0);123}124125#ifdef OPENSSL_NO_KTLS_RX126127static ossl_inline int ktls_read_record(int fd, void *data, size_t length)128{129return -1;130}131132#else /* !defined(OPENSSL_NO_KTLS_RX) */133134/*135* Receive a TLS record using the tls_en provided in ktls_start. The136* kernel strips any explicit IV and authentication tag, but provides137* the TLS record header via a control message. If there is an error138* with the TLS record such as an invalid header, invalid padding, or139* authentication failure recvmsg() will fail with an error.140*/141static ossl_inline int ktls_read_record(int fd, void *data, size_t length)142{143struct msghdr msg = { 0 };144int cmsg_len = sizeof(struct tls_get_record);145struct tls_get_record *tgr;146struct cmsghdr *cmsg;147char buf[CMSG_SPACE(cmsg_len)];148struct iovec msg_iov; /* Vector of data to send/receive into */149int ret;150unsigned char *p = data;151const size_t prepend_length = SSL3_RT_HEADER_LENGTH;152153if (length <= prepend_length) {154errno = EINVAL;155return -1;156}157158msg.msg_control = buf;159msg.msg_controllen = sizeof(buf);160161msg_iov.iov_base = p + prepend_length;162msg_iov.iov_len = length - prepend_length;163msg.msg_iov = &msg_iov;164msg.msg_iovlen = 1;165166ret = recvmsg(fd, &msg, 0);167if (ret <= 0)168return ret;169170if ((msg.msg_flags & (MSG_EOR | MSG_CTRUNC)) != MSG_EOR) {171errno = EMSGSIZE;172return -1;173}174175if (msg.msg_controllen == 0) {176errno = EBADMSG;177return -1;178}179180cmsg = CMSG_FIRSTHDR(&msg);181if (cmsg->cmsg_level != IPPROTO_TCP || cmsg->cmsg_type != TLS_GET_RECORD182|| cmsg->cmsg_len != CMSG_LEN(cmsg_len)) {183errno = EBADMSG;184return -1;185}186187tgr = (struct tls_get_record *)CMSG_DATA(cmsg);188p[0] = tgr->tls_type;189p[1] = tgr->tls_vmajor;190p[2] = tgr->tls_vminor;191p[3] = (ret >> 8) & 0xff;192p[4] = ret & 0xff;193194return ret + prepend_length;195}196197#endif /* OPENSSL_NO_KTLS_RX */198199/*200* KTLS enables the sendfile system call to send data from a file over201* TLS.202*/203static ossl_inline ossl_ssize_t ktls_sendfile(int s, int fd, off_t off,204size_t size, int flags)205{206off_t sbytes = 0;207int ret;208209ret = sendfile(fd, s, off, size, NULL, &sbytes, flags);210if (ret == -1 && sbytes == 0)211return -1;212return sbytes;213}214215#endif /* __FreeBSD__ */216217#if defined(OPENSSL_SYS_LINUX)218219#include <linux/tls.h>220#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 17, 0)221#define OPENSSL_NO_KTLS_RX222#ifndef PEDANTIC223#warning "KTLS requires Kernel Headers >= 4.17.0 for receiving"224#warning "Skipping Compilation of KTLS receive data path"225#endif226#endif227#if LINUX_VERSION_CODE < KERNEL_VERSION(5, 19, 0)228#define OPENSSL_NO_KTLS_ZC_TX229#ifndef PEDANTIC230#warning "KTLS requires Kernel Headers >= 5.19.0 for zerocopy sendfile"231#warning "Skipping Compilation of KTLS zerocopy sendfile"232#endif233#endif234#define OPENSSL_KTLS_AES_GCM_128235#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 1, 0)236#define OPENSSL_KTLS_AES_GCM_256237#define OPENSSL_KTLS_TLS13238#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 2, 0)239#define OPENSSL_KTLS_AES_CCM_128240#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 11, 0)241#ifndef OPENSSL_NO_CHACHA242#define OPENSSL_KTLS_CHACHA20_POLY1305243#endif244#endif245#endif246#endif247248#include <sys/sendfile.h>249#include <netinet/tcp.h>250#include <linux/socket.h>251#include <openssl/ssl3.h>252#include <openssl/tls1.h>253#include <openssl/evp.h>254255#ifndef SOL_TLS256#define SOL_TLS 282257#endif258259#ifndef TCP_ULP260#define TCP_ULP 31261#endif262263#ifndef TLS_RX264#define TLS_RX 2265#endif266267struct tls_crypto_info_all {268union {269#ifdef OPENSSL_KTLS_AES_GCM_128270struct tls12_crypto_info_aes_gcm_128 gcm128;271#endif272#ifdef OPENSSL_KTLS_AES_GCM_256273struct tls12_crypto_info_aes_gcm_256 gcm256;274#endif275#ifdef OPENSSL_KTLS_AES_CCM_128276struct tls12_crypto_info_aes_ccm_128 ccm128;277#endif278#ifdef OPENSSL_KTLS_CHACHA20_POLY1305279struct tls12_crypto_info_chacha20_poly1305 chacha20poly1305;280#endif281};282size_t tls_crypto_info_len;283};284285typedef struct tls_crypto_info_all ktls_crypto_info_t;286287/*288* When successful, this socket option doesn't change the behaviour of the289* TCP socket, except changing the TCP setsockopt handler to enable the290* processing of SOL_TLS socket options. All other functionality remains the291* same.292*/293static ossl_inline int ktls_enable(int fd)294{295return setsockopt(fd, SOL_TCP, TCP_ULP, "tls", sizeof("tls")) ? 0 : 1;296}297298/*299* The TLS_TX socket option changes the send/sendmsg handlers of the TCP socket.300* If successful, then data sent using this socket will be encrypted and301* encapsulated in TLS records using the crypto_info provided here.302* The TLS_RX socket option changes the recv/recvmsg handlers of the TCP socket.303* If successful, then data received using this socket will be decrypted,304* authenticated and decapsulated using the crypto_info provided here.305*/306static ossl_inline int ktls_start(int fd, ktls_crypto_info_t *crypto_info,307int is_tx)308{309/*310* Socket must be in TCP established state to enable KTLS.311* Further calls to enable ktls will return EEXIST312*/313ktls_enable(fd);314315return setsockopt(fd, SOL_TLS, is_tx ? TLS_TX : TLS_RX,316crypto_info, crypto_info->tls_crypto_info_len)317? 0318: 1;319}320321static ossl_inline int ktls_enable_tx_zerocopy_sendfile(int fd)322{323#ifndef OPENSSL_NO_KTLS_ZC_TX324int enable = 1;325326return setsockopt(fd, SOL_TLS, TLS_TX_ZEROCOPY_RO,327&enable, sizeof(enable))328? 0329: 1;330#else331return 0;332#endif333}334335/*336* Send a TLS record using the crypto_info provided in ktls_start and use337* record_type instead of the default SSL3_RT_APPLICATION_DATA.338* When the socket is non-blocking, then this call either returns EAGAIN or339* the entire record is pushed to TCP. It is impossible to send a partial340* record using this control message.341*/342static ossl_inline int ktls_send_ctrl_message(int fd, unsigned char record_type,343const void *data, size_t length)344{345struct msghdr msg;346int cmsg_len = sizeof(record_type);347struct cmsghdr *cmsg;348union {349struct cmsghdr hdr;350char buf[CMSG_SPACE(sizeof(unsigned char))];351} cmsgbuf;352struct iovec msg_iov; /* Vector of data to send/receive into */353354memset(&msg, 0, sizeof(msg));355msg.msg_control = cmsgbuf.buf;356msg.msg_controllen = sizeof(cmsgbuf.buf);357cmsg = CMSG_FIRSTHDR(&msg);358cmsg->cmsg_level = SOL_TLS;359cmsg->cmsg_type = TLS_SET_RECORD_TYPE;360cmsg->cmsg_len = CMSG_LEN(cmsg_len);361*((unsigned char *)CMSG_DATA(cmsg)) = record_type;362msg.msg_controllen = cmsg->cmsg_len;363364msg_iov.iov_base = (void *)data;365msg_iov.iov_len = length;366msg.msg_iov = &msg_iov;367msg.msg_iovlen = 1;368369return sendmsg(fd, &msg, 0);370}371372/*373* KTLS enables the sendfile system call to send data from a file over TLS.374* @flags are ignored on Linux. (placeholder for FreeBSD sendfile)375* */376static ossl_inline ossl_ssize_t ktls_sendfile(int s, int fd, off_t off, size_t size, int flags)377{378return sendfile(s, fd, &off, size);379}380381#ifdef OPENSSL_NO_KTLS_RX382383static ossl_inline int ktls_read_record(int fd, void *data, size_t length)384{385return -1;386}387388#else /* !defined(OPENSSL_NO_KTLS_RX) */389390/*391* Receive a TLS record using the crypto_info provided in ktls_start.392* The kernel strips the TLS record header, IV and authentication tag,393* returning only the plaintext data or an error on failure.394* We add the TLS record header here to satisfy routines in rec_layer_s3.c395*/396static ossl_inline int ktls_read_record(int fd, void *data, size_t length)397{398struct msghdr msg;399struct cmsghdr *cmsg;400union {401struct cmsghdr hdr;402char buf[CMSG_SPACE(sizeof(unsigned char))];403} cmsgbuf;404struct iovec msg_iov;405int ret;406unsigned char *p = data;407const size_t prepend_length = SSL3_RT_HEADER_LENGTH;408409if (length < prepend_length + EVP_GCM_TLS_TAG_LEN) {410errno = EINVAL;411return -1;412}413414memset(&msg, 0, sizeof(msg));415msg.msg_control = cmsgbuf.buf;416msg.msg_controllen = sizeof(cmsgbuf.buf);417418msg_iov.iov_base = p + prepend_length;419msg_iov.iov_len = length - prepend_length - EVP_GCM_TLS_TAG_LEN;420msg.msg_iov = &msg_iov;421msg.msg_iovlen = 1;422423ret = recvmsg(fd, &msg, 0);424if (ret < 0)425return ret;426427if (msg.msg_controllen > 0) {428cmsg = CMSG_FIRSTHDR(&msg);429if (cmsg->cmsg_type == TLS_GET_RECORD_TYPE) {430p[0] = *((unsigned char *)CMSG_DATA(cmsg));431p[1] = TLS1_2_VERSION_MAJOR;432p[2] = TLS1_2_VERSION_MINOR;433/* returned length is limited to msg_iov.iov_len above */434p[3] = (ret >> 8) & 0xff;435p[4] = ret & 0xff;436ret += prepend_length;437}438}439440return ret;441}442443#endif /* OPENSSL_NO_KTLS_RX */444445#endif /* OPENSSL_SYS_LINUX */446#endif /* OPENSSL_NO_KTLS */447#endif /* HEADER_INTERNAL_KTLS */448449450