In-kernel TLS Framing and Encryp6on for FreeBSD John Baldwin - - PowerPoint PPT Presentation

in kernel tls framing and encryp6on for freebsd
SMART_READER_LITE
LIVE PREVIEW

In-kernel TLS Framing and Encryp6on for FreeBSD John Baldwin - - PowerPoint PPT Presentation

In-kernel TLS Framing and Encryp6on for FreeBSD John Baldwin BSDCan June 2020 Overview What is KTLS? TLS Transmit TLS Receive Current Status What is TLS? Transport Layer Security (TLS) is an applica6on layer protocol


slide-1
SLIDE 1

In-kernel TLS Framing and Encryp6on for FreeBSD

John Baldwin BSDCan June 2020

slide-2
SLIDE 2

Overview

  • What is KTLS?
  • TLS Transmit
  • TLS Receive
  • Current Status
slide-3
SLIDE 3

What is TLS?

  • Transport Layer Security (TLS) is an applica6on

layer protocol

  • Provides authen6ca6on and privacy
  • Structured as a stream of records, or frames, sent

and received over a transport protocol

  • Includes handshake messages to nego6ate

session keys and applica6on data messages to tunnel applica6on data

slide-4
SLIDE 4

What is KTLS?

  • In-kernel TLS (KTLS) handles TLS framing and

encryp6on/decryp6on in the kernel

  • KTLS does not handle session key nego6a6on

– Userland library such as OpenSSL supplies session keys to kernel aQer handshake

slide-5
SLIDE 5

Why KTLS?

Two reasons to handle TLS in the kernel

  • 1. Enable zero-copy send over TLS via

sendfile()

  • 2. Support TLS offload in NICs
slide-6
SLIDE 6

TLS Sessions

  • TLS Sessions describe session keys

– Ciphersuite (AES-GCM, AES-CBC with HMAC) – Cipher and MAC keys

  • SSL library provides session keys via

setsockopt()

  • TLS Sessions are associated with socket buffers

– Separate sessions for transmit and receive

slide-7
SLIDE 7

TLS Transmit

  • All data wri\en on a socket using KTLS transmit is

encrypted by the kernel

  • Userland can send individual TLS records with a

specific record type and length via sendmsg()

– TLS_SET_RECORD_TYPE control message

  • Kernel chooses framing and uses “applica6on

data” record type for all other data

slide-8
SLIDE 8

TLS Transmit

  • TLS records stored in a special type of mbuf

– TLS header and trailer stored inline in mbuf – Payload data referenced via physical address pointers

  • Not-yet-encrypted TLS record mbufs hold a

reference to a TLS session

– Session reference inherited from socket buffer

slide-9
SLIDE 9

TLS Transmit: SW KTLS

NIC Userland Kernel write() Applica6on Data Socket Buffer TCP Packet Unencrypted (M_NOTREADY) Encrypted

slide-10
SLIDE 10

TLS Transmit: SW KTLS

NIC Userland Kernel sendfile() Socket Buffer TCP Packet Disk Per-socket Copies

slide-11
SLIDE 11

TLS Transmit: NIC/TOE KTLS

NIC Userland Kernel write() Applica6on Data Socket Buffer TCP Packet Unencrypted

slide-12
SLIDE 12

TLS Transmit: NIC/TOE KTLS

NIC Userland Kernel Socket Buffer TCP Packet sendfile() Disk No Copies!

slide-13
SLIDE 13

TLS Receive

  • All data received on a socket using KTLS receive is

decrypted by the kernel

  • Userland receives individual TLS records via

recvmsg()

– TLS_GET_RECORD control message

  • Socket buffer holds a list of TLS records like a

datagram socket even though TCP is a stream socket

slide-14
SLIDE 14

TLS Receive: TOE KTLS

NIC Userland Kernel recvmsg() Applica6on Data Socket Buffer TLS PDU Decrypted

slide-15
SLIDE 15

TLS Receive: SW KTLS

NIC Userland Kernel recvmsg() Applica6on Data Socket Buffer Decrypted Encrypted (M_NOTREADY) TCP Packet Encrypted

slide-16
SLIDE 16

TLS & Socket Send Buffers

  • TLS uses send socket buffer in the “usual” way. It

is a single “record” holding a stream of TLS mbufs.

– Each mbuf describes a single TLS record – Unencrypted records are marked as M_NOTREADY – Both unencrypted and encrypted TLS records live in the same stream – To mark a record as encrypted, clear M_NOTREADY

slide-17
SLIDE 17

TLS & Socket Receive Buffers

  • TLS uses receive socket buffer differently

– Decrypted TLS records are stored as “records” in socket buffer consis6ng of control message mbuf holding TLS header followed by decrypted data in “normal” mbufs. No trailer. – Encrypted TLS records received from TCP are just “normal” mbufs with TLS header and trailer data in the mbuf payload – Can’t simply flip M_NOTREADY bit to convert from encrypted to decrypted

slide-18
SLIDE 18

Decryp6ng TLS Records

  • Wait for full TLS record to be received
  • Decrypt TLS record payload
  • Allocate control message and copy TLS header into

message

  • Discard TLS header and trailer from “normal” mbufs

holding TLS record

  • Ensure the mbufs holding TLS record aren’t freed out from

under decryp6on handler via sbcut(), sbdrop(), or sbflush()

  • Ensure socket buffer accoun6ng is accurate
slide-19
SLIDE 19

Splifng the Receive Buffer

sb_mb sb_mtls Socket Buffer

slide-20
SLIDE 20

Decryp6ng a TLS Record

sb_mb sb_mtls

slide-21
SLIDE 21

TLS Receive: NIC TLS (Sketch)

sb_mb sb_mtls Decrypted TLS Record Out of order encrypted data

slide-22
SLIDE 22

Current Status: Transmit

  • KTLS Transmit for TLS 1.0-1.3 merged to FreeBSD 13.0-

CURRENT

– Includes SW TLS, NIC TLS, TOE TLS – ktls_ocf.ko and security/ktls_isa-l_crypto-kmod port/ package

  • KTLS Transmit for TLS 1.0-1.2 merged to OpenSSL

master (will ship in 3.0)

  • TLS 1.3 for OpenSSL pending review

– h\ps://github.com/openssl/openssl/pull/10626

slide-23
SLIDE 23

Current Status: Receive

  • KTLS Receive for TLS 1.1-1.2 via TOE merged to

FreeBSD 13.0-CURRENT

  • KTLS Receive for TLS 1.1-1.2 via SW in progress

– h\ps://reviews.freebsd.org/D24628

  • KTLS Receive for TLS 1.1-1.2 for OpenSSL pending

review

– h\ps://github.com/openssl/openssl/pull/11679

slide-24
SLIDE 24

Current Status: nginx

  • nginx patches to support SSL_sendfile()

– h\ps://github.com/nginx/nginx/compare/ branches/stable-1.14...bsdjhb:ktls-1.14-openssl- master – h\ps://github.com/nginx/nginx/compare/ branches/stable-1.16...bsdjhb:ktls-1.16

slide-25
SLIDE 25

Further WIP

  • Improving KTLS performance using OCF

– Goal is to bring aesni.ko and ktls_ocf.ko on par with security/ktls_isa-l_crypto-kmod

  • Adding support for TLS 1.1 (and maybe 1.0)

transmit to SW KTLS via OCF

  • Adding support for TLS 1.3 receive to SW KTLS
  • Making OpenSSL KTLS available via base or ports
  • Adding SSL_sendfile() support to nginx port