|
2 | 2 |
|
3 | 3 | Kernel TLS (kTLS) support for [Compio](https://github.com/compio-rs/compio). |
4 | 4 |
|
| 5 | +[](README.zh.md) |
| 6 | +[](https://github.com/fantix/compio-ktls/actions/workflows/ci.yml) |
| 7 | +[](https://www.apache.org/licenses/LICENSE-2.0) |
| 8 | +[](https://license.coscl.org.cn/MulanPSL2/) |
| 9 | + |
| 10 | +## Overview |
| 11 | + |
| 12 | +- Built on top of [ktls-core](https://github.com/hanyu-dev/ktls) |
| 13 | +- Not tied to any specific Compio runtime implementation |
| 14 | +- Pluggable TLS implementations (currently supports Rustls) |
| 15 | +- Currently supports TLS 1.3 only |
| 16 | +- Supports NewSessionTicket, KeyUpdate, and Alert message handling |
| 17 | + |
| 18 | +## Features |
| 19 | + |
| 20 | +- `rustls` (default): Enable Rustls integration |
| 21 | +- `ring`: Use ring as the crypto backend |
| 22 | +- `app-write-with-empty-ancillary`: Use `write_with_ancillary()` instead of `write()` for |
| 23 | + application data writes. compio-rs/compio#756 introduced zero-copy writes for io-uring, |
| 24 | + which changed the default behavior of `write()` in a way that breaks on kTLS-enabled |
| 25 | + sockets. Enable this feature when using io-uring to work around the conflict between |
| 26 | + zero-copy writes and kTLS. |
| 27 | + |
| 28 | +## Usage |
| 29 | + |
| 30 | +```rust |
| 31 | +use compio_ktls::{KtlsConnector, KtlsAcceptor}; |
| 32 | + |
| 33 | +// Client side |
| 34 | +let connector = KtlsConnector::from(client_config); |
| 35 | +match connector.connect("example.com", tcp_stream).await? { |
| 36 | + Ok(stream) => { |
| 37 | + // kTLS enabled successfully |
| 38 | + } |
| 39 | + Err(stream) => { |
| 40 | + // kTLS unavailable, fallback to original stream |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +// Server side |
| 45 | +let acceptor = KtlsAcceptor::from(server_config); |
| 46 | +match acceptor.accept(tcp_stream).await? { |
| 47 | + Ok(stream) => { |
| 48 | + // kTLS enabled successfully |
| 49 | + } |
| 50 | + Err(stream) => { |
| 51 | + // kTLS unavailable, fallback to original stream |
| 52 | + } |
| 53 | +} |
| 54 | +``` |
| 55 | + |
| 56 | +## Requirements |
| 57 | + |
| 58 | +Requires Linux kernel with kTLS support, version 6.6 LTS or newer is recommended. |
| 59 | + |
| 60 | +Check if the kTLS module is loaded: |
| 61 | + |
| 62 | +```bash |
| 63 | +lsmod | grep tls |
| 64 | +``` |
| 65 | + |
| 66 | +If not loaded, you can manually load it: |
| 67 | + |
| 68 | +```bash |
| 69 | +sudo modprobe tls |
| 70 | +``` |
| 71 | + |
| 72 | +Also requires Rustls with `enable_secret_extraction` enabled: |
| 73 | + |
| 74 | +```rust |
| 75 | +use std::sync::Arc; |
| 76 | +use rustls::ClientConfig; |
| 77 | + |
| 78 | +let mut config = ClientConfig::builder() |
| 79 | + .dangerous() |
| 80 | + .with_custom_certificate_verifier(/* ... */) |
| 81 | + .with_no_client_auth(); |
| 82 | + |
| 83 | +config.enable_secret_extraction = true; |
| 84 | + |
| 85 | +let config = Arc::new(config); |
| 86 | +``` |
5 | 87 |
|
6 | 88 | ## License |
7 | 89 |
|
|
0 commit comments