Skip to content

Commit 51aaaf2

Browse files
committed
epic: first committed version
1 parent 1be4d9b commit 51aaaf2

21 files changed

Lines changed: 2551 additions & 1 deletion

.github/workflows/ci.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
env:
10+
CARGO_TERM_COLOR: always
11+
RUST_BACKTRACE: 1
12+
13+
jobs:
14+
check:
15+
name: Check ${{ matrix.arch }}
16+
runs-on: ${{ matrix.runs-on }}
17+
strategy:
18+
matrix:
19+
include:
20+
- arch: x86_64
21+
runs-on: ubuntu-24.04
22+
- arch: aarch64
23+
runs-on: ubuntu-24.04-arm
24+
steps:
25+
- uses: actions/checkout@v4
26+
- uses: dtolnay/rust-toolchain@nightly
27+
with:
28+
components: rustfmt
29+
- uses: dtolnay/rust-toolchain@stable
30+
with:
31+
components: clippy
32+
- name: Check kTLS environment
33+
run: bash scripts/check-ktls.sh
34+
- name: Run all checks
35+
run: bash scripts/check.sh

.github/workflows/release.yml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
name: Release
2+
3+
on:
4+
push:
5+
tags:
6+
- 'v*'
7+
8+
permissions:
9+
contents: write
10+
11+
jobs:
12+
publish:
13+
name: Publish to crates.io
14+
runs-on: ubuntu-latest
15+
steps:
16+
- uses: actions/checkout@v4
17+
- uses: dtolnay/rust-toolchain@stable
18+
- name: Publish
19+
run: cargo publish --token ${{ secrets.CARGO_REGISTRY_TOKEN }}
20+
21+
github-release:
22+
name: Create GitHub Release
23+
runs-on: ubuntu-latest
24+
steps:
25+
- uses: actions/checkout@v4
26+
- name: Extract release notes
27+
id: extract-release-notes
28+
run: |
29+
VERSION=${GITHUB_REF#refs/tags/v}
30+
echo "version=$VERSION" >> $GITHUB_OUTPUT
31+
- name: Create Release
32+
uses: softprops/action-gh-release@v1
33+
with:
34+
generate_release_notes: true

CONTRIBUTING.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Contributing to compio-ktls
2+
3+
Thank you for your interest in contributing to compio-ktls!
4+
5+
## Getting Started
6+
7+
1. Fork the repository
8+
2. Clone your fork: `git clone https://github.com/YOUR_USERNAME/compio-ktls.git`
9+
3. Create a new branch: `git checkout -b my-feature`
10+
11+
## Development Setup
12+
13+
### Prerequisites
14+
15+
- Linux kernel 6.6 LTS or newer (for running tests)
16+
- kTLS kernel module loaded: `sudo modprobe tls`
17+
18+
Run the environment check script:
19+
20+
```bash
21+
./scripts/check-ktls.sh
22+
```
23+
24+
### Running Tests
25+
26+
```bash
27+
# Run all checks (fmt, clippy, tests, doc)
28+
./scripts/check.sh
29+
30+
# Or run individually
31+
cargo +nightly fmt --all -- --check
32+
cargo clippy --all-targets --all-features -- -D warnings
33+
cargo test --all-features
34+
cargo doc --all-features --no-deps
35+
```
36+
37+
## Making Changes
38+
39+
1. **Code Style**: Follow the project's code style. Run `cargo fmt` before committing.
40+
2. **Tests**: Add tests for new functionality. Ensure all tests pass.
41+
3. **Documentation**: Update documentation for public APIs.
42+
4. **Commit Messages**: Write clear, concise commit messages.
43+
44+
## Pull Request Process
45+
46+
1. Ensure all tests pass locally
47+
2. Update documentation if needed
48+
3. Create a pull request with a clear description of changes
49+
4. Address any feedback from reviewers
50+
51+
## Code of Conduct
52+
53+
- Be respectful and inclusive
54+
- Focus on constructive feedback
55+
- Help create a welcoming environment for all contributors
56+
57+
## License
58+
59+
By contributing, you agree that your contributions will be licensed under the same licenses as the project (Apache-2.0 OR MulanPSL-2.0).
60+

Cargo.toml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,46 @@ edition = "2024"
55
description = "Kernel TLS (kTLS) support for Compio"
66
repository = "https://github.com/fantix/compio-ktls"
77
license = "Apache-2.0 OR MulanPSL-2.0"
8+
authors = ["Fantix King <fantix.king@gmail.com>"]
89
keywords = ["ktls", "tls", "compio", "async", "kernel"]
910
categories = ["asynchronous", "network-programming", "cryptography"]
1011
readme = "README.md"
1112

1213
[dependencies]
14+
rustls = { version = "0.23", default-features = false, optional = true }
15+
16+
[dependencies.compio-io]
17+
version = "0.9"
18+
default-features = false
19+
features = ["ancillary", "bytemuck"]
20+
21+
[dependencies.compio-buf]
22+
version = "0.8"
23+
default-features = false
24+
25+
[target."cfg(target_os = \"linux\")".dependencies]
26+
libc = "0.2"
27+
ktls-core = "0.0.5"
28+
29+
[features]
30+
default = ["rustls"]
31+
rustls = ["dep:rustls", "ktls-core/shim-rustls"]
32+
ring = ["rustls", "rustls/ring"]
33+
app-write-with-empty-ancillary = []
34+
35+
[dev-dependencies]
36+
compio = { version = "0.18", features = ["net", "rustls", "macros"] }
37+
rustls-native-certs = "0.8"
38+
rustls-pemfile = "2.2"
39+
40+
[patch.crates-io.compio]
41+
git = "https://github.com/fantix/compio.git"
42+
rev = "a40dbc4685898f61f8a6702da9aa38872e5f7629"
43+
44+
[patch.crates-io.compio-io]
45+
git = "https://github.com/fantix/compio.git"
46+
rev = "a40dbc4685898f61f8a6702da9aa38872e5f7629"
47+
48+
[patch.crates-io.compio-buf]
49+
git = "https://github.com/fantix/compio.git"
50+
rev = "a40dbc4685898f61f8a6702da9aa38872e5f7629"

README.md

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,88 @@
22

33
Kernel TLS (kTLS) support for [Compio](https://github.com/compio-rs/compio).
44

5+
[![中文](https://img.shields.io/badge/Zh-中文-informational?logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAQCAYAAAAWGF8bAAAAAXNSR0IArs4c6QAAAERlWElmTU0AKgAAAAgAAYdpAAQAAAABAAAAGgAAAAAAA6ABAAMAAAABAAEAAKACAAQAAAABAAAAFKADAAQAAAABAAAAEAAAAABHXVY9AAABc0lEQVQ4EaWSOy8EURTHd+wDEY94JVtgg9BI1B6dQqHiE1CrRasT30DpC2hVQimiFkJWVsSj2U1EsmQH4/ff3CO3WDuDk/zmf8/jnntm7qRSMRZFUQ4WYSimNFmaRlsgq8F83K6WuALyva4mixbc+kfJcGqa7CqU4AjaocNpG5oHsx7qB3EqQRC8K4g/gazAMbFTBdbgL1Zh0w2EbnMVHdMrd4LZNotZmIZJKMAemC2z0MS6oDlYhzOQ6c3yGR5Fec4OGPvEHCmn3np+kfyT51+QH8afcbFLTfjgFVS9tZrpwC4v1k9M39w3NTQrBxSM4127SAmNoBt0Ma3QyHRwGUIYdQUh0+c0wZsLPKKH8AwvoHgNlmABZLtwBdqnP0DD9IEG2If6N0oz5SbYSfW4PYhvgNmUxU1JZGEEAsUyjPmB7lhBA1Xe7NMWpuzXa39fnC7lN1b/mZttSNLQv9XXZs2US9LwzjU5R+/d+n/CBx9I2uELeXrRajeDqHwAAAAASUVORK5CYII=)](README.zh.md)
6+
[![CI](https://img.shields.io/github/actions/workflow/status/fantix/compio-ktls/ci.yml?label=CI&logo=github)](https://github.com/fantix/compio-ktls/actions/workflows/ci.yml)
7+
[![license](https://img.shields.io/badge/license-Apache--2.0-success?logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAgCAYAAAASYli2AAAACXBIWXMAAAsTAAALEwEAmpwYAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAGqSURBVHgBrVbtcYMwDFVy/V82KCMwAp2g2aDtJGSDZoPQDegE0AmSDUwngA1UKYggHBtwyLvTgcF66ONZCcAdQMSMrCEzZHGILzvHZJFaf+AYxxCyTDlmQm5k3cj1EEJ4Qjf085322UI4KrJrCTabTXFDKKmU8uUjWSLvfy2yWixWWbDf16g58tBGadWQsUc/GuZ6Es4YbpGK6ehewI9iLkIMiO7Up7z11AoctcOJyF6pObWOMMJBVy6wmI0rapv9EiGxt3T5nIiOETvePaN99LCTTCL3B0/tfALvYbCTWww4pFJ6HHe4HCXLppVgU0dKP2RvsBwJzESwQ3czfDj0dXRpzODydFkhe+a6nBTqMhPybabCrybSzaUcrVgtSrl2miPhbufqqyn6tWnQN6lxPIGbgHSNi4+FHal1tCDdHt+uh1zDs2ez/VtRy94/soJqVoEPOD4hjdTPrlkKIVANOaJ/VBVzPP2AZelwc2pJ7d2xl2WRw1JC5VQJKc/Ivkm8zkdaWwJ0zLdQbBVZBMOgWE8I3bSpYCU0YUI1OsNK3PPPYZ5QDnoND8A/4kV4DUnNfc8AAAAASUVORK5CYII=)](https://www.apache.org/licenses/LICENSE-2.0)
8+
[![license](https://img.shields.io/badge/license-MulanPSL--2.0-success?logo=opensourceinitiative&logoColor=white)](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+
```
587

688
## License
789

README.zh.md

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,88 @@
11
# compio-ktls
22

3-
[Compio](https://github.com/compio-rs/compio) 提供内核 TLS (kTLS) 支持
3+
[Compio](https://github.com/compio-rs/compio) 的内核 TLS (kTLS) 支持
44

5+
[![English](https://img.shields.io/badge/英文-English-informational?logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABsAAAAQCAYAAADnEwSWAAAABGdBTUEAALGPC/xhBQAAADhlWElmTU0AKgAAAAgAAYdpAAQAAAABAAAAGgAAAAAAAqACAAQAAAABAAAAG6ADAAQAAAABAAAAEAAAAACiF0fSAAABJUlEQVQ4EWP8//8/MwMDAysQEw0YGRl/EK0YWSHQsgogJgX8RNZPCpuJFMWUqqWrZSw4XBsCFL+FQ+4/DnEUYWC8gNKCEDB+X8MlgIKVWCJMD64ADwOorwmIHyNhdyDbFYgPAfFXIAaBV0CcCTIG5DNsLo0GKnDEYc8+oGsvQ+UEgLQMkrpYIDsSiJGjRxTInwY07wmuYCxDMgCdmQUUgFmGLheNLoDEzwG5gBFJgFQmNr3ZQEPsgfgImmEquHy2BajwHZpiGPcmjAGk0aPgCDCIp4HkgcHWA6RsQGwoEMEVZ9VATZdgqkig7yKp/YjEBjORIxJdjhw+3tIFVzCGAoPBEo9ta4E+f4NHHqsULstqsKpGCJ4BMkm2jNrBiHAOFhZdLQMA8pKhkQYZiokAAAAASUVORK5CYII=)](README.md)
6+
[![CI](https://img.shields.io/github/actions/workflow/status/fantix/compio-ktls/ci.yml?label=CI&logo=github)](https://github.com/fantix/compio-ktls/actions/workflows/test.yml)
7+
[![许可](https://img.shields.io/badge/许可-Apache--2.0-success?logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAgCAYAAAASYli2AAAACXBIWXMAAAsTAAALEwEAmpwYAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAGqSURBVHgBrVbtcYMwDFVy/V82KCMwAp2g2aDtJGSDZoPQDegE0AmSDUwngA1UKYggHBtwyLvTgcF66ONZCcAdQMSMrCEzZHGILzvHZJFaf+AYxxCyTDlmQm5k3cj1EEJ4Qjf085322UI4KrJrCTabTXFDKKmU8uUjWSLvfy2yWixWWbDf16g58tBGadWQsUc/GuZ6Es4YbpGK6ehewI9iLkIMiO7Up7z11AoctcOJyF6pObWOMMJBVy6wmI0rapv9EiGxt3T5nIiOETvePaN99LCTTCL3B0/tfALvYbCTWww4pFJ6HHe4HCXLppVgU0dKP2RvsBwJzESwQ3czfDj0dXRpzODydFkhe+a6nBTqMhPybabCrybSzaUcrVgtSrl2miPhbufqqyn6tWnQN6lxPIGbgHSNi4+FHal1tCDdHt+uh1zDs2ez/VtRy94/soJqVoEPOD4hjdTPrlkKIVANOaJ/VBVzPP2AZelwc2pJ7d2xl2WRw1JC5VQJKc/Ivkm8zkdaWwJ0zLdQbBVZBMOgWE8I3bSpYCU0YUI1OsNK3PPPYZ5QDnoND8A/4kV4DUnNfc8AAAAASUVORK5CYII=)](https://www.apache.org/licenses/LICENSE-2.0)
8+
[![许可](https://img.shields.io/badge/许可-MulanPSL--2.0-success?logo=opensourceinitiative&logoColor=white)](https://license.coscl.org.cn/MulanPSL2/)
9+
10+
## 概述
11+
12+
- 基于 [ktls-core](https://github.com/hanyu-dev/ktls) 实现
13+
- 不锁定特定的 Compio 运行时实现
14+
- 可插拔的 TLS 实现(目前支持 Rustls)
15+
- 目前仅支持 TLS 1.3
16+
- 支持 NewSessionTicket、KeyUpdate 和 Alert 消息处理
17+
18+
## 可选 features
19+
20+
- `rustls`(默认):启用 Rustls 集成
21+
- `ring`:使用 ring 作为加密后端
22+
- `app-write-with-empty-ancillary`:在写入应用数据时使用 `write_with_ancillary()` 而非
23+
`write()`。compio-rs/compio#756 引入了 io-uring 的零拷贝写入,改变了 `write()`
24+
的默认行为,而这会在启用了 kTLS 的 socket 上出错。因此,使用 io-uring 时,应启用该 feature
25+
来绕过 zero-copy 写入与 kTLS 的冲突。
26+
27+
## 使用方法
28+
29+
```rust
30+
use compio_ktls::{KtlsConnector, KtlsAcceptor};
31+
32+
// 客户端
33+
let connector = KtlsConnector::from(client_config);
34+
match connector.connect("example.com", tcp_stream).await? {
35+
Ok(stream) => {
36+
// 成功启用 kTLS
37+
}
38+
Err(stream) => {
39+
// kTLS 不可用,回退到原始 stream
40+
}
41+
}
42+
43+
// 服务端
44+
let acceptor = KtlsAcceptor::from(server_config);
45+
match acceptor.accept(tcp_stream).await? {
46+
Ok(stream) => {
47+
// 成功启用 kTLS
48+
}
49+
Err(stream) => {
50+
// kTLS 不可用,回退到原始 stream
51+
}
52+
}
53+
```
54+
55+
## 环境要求
56+
57+
需要 Linux 内核支持 kTLS,建议使用 6.6 或更新版本的 LTS 内核。
58+
59+
检查内核是否已加载 kTLS 模块:
60+
61+
```bash
62+
lsmod | grep tls
63+
```
64+
65+
如果没有加载,可以手动加载:
66+
67+
```bash
68+
sudo modprobe tls
69+
```
70+
71+
另需 Rustls 启用 `enable_secret_extraction`
72+
73+
```rust
74+
use std::sync::Arc;
75+
use rustls::ClientConfig;
76+
77+
let mut config = ClientConfig::builder()
78+
.dangerous()
79+
.with_custom_certificate_verifier(/* ... */)
80+
.with_no_client_auth();
81+
82+
config.enable_secret_extraction = true;
83+
84+
let config = Arc::new(config);
85+
```
586

687
## 许可证
788

rustfmt.toml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
unstable_features = true
2+
3+
style_edition = "2024"
4+
5+
group_imports = "StdExternalCrate"
6+
imports_granularity = "Crate"
7+
reorder_imports = true
8+
9+
wrap_comments = true
10+
normalize_comments = true
11+
12+
reorder_impl_items = true
13+
condense_wildcard_suffixes = true
14+
enum_discrim_align_threshold = 20
15+
use_field_init_shorthand = true
16+
17+
format_strings = true
18+
format_code_in_doc_comments = true
19+
format_macro_matchers = true

0 commit comments

Comments
 (0)