Skip to content

Commit 2c44edd

Browse files
committed
refactor(setup): use libc instead of ktls-sys, and others
chore(ktls): update libc and nix version
1 parent bace1ca commit 2c44edd

File tree

7 files changed

+318
-55
lines changed

7 files changed

+318
-55
lines changed

Cargo.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ktls/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ repository.workspace = true
1414
[dependencies]
1515
futures-util = "0.3.30"
1616
ktls-sys = "1.0.1"
17-
libc = { version = "0.2.155", features = ["const-extern-fn"] }
17+
libc = { version = "0.2.175", features = ["const-extern-fn"] }
1818
memoffset = "0.9.1"
19-
nix = { version = "0.29.0", features = ["socket", "uio", "net"] }
19+
nix = { version = "0.30.1", features = ["socket", "uio", "net"] }
2020
num_enum = "0.7.3"
2121
pin-project-lite = "0.2.14"
2222
rustls = { version = "0.23.12", default-features = false }

ktls/src/ffi.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ pub fn send_close_notify(fd: RawFd) -> std::io::Result<()> {
270270
.payload
271271
.encode(&mut data);
272272

273-
let mut cmsg = Cmsg::new(SOL_TLS, TLS_SET_RECORD_TYPE, [ALERT]);
273+
let mut cmsg = Cmsg::new(libc::SOL_TLS, TLS_SET_RECORD_TYPE, [ALERT]);
274274

275275
let msg = libc::msghdr {
276276
msg_name: std::ptr::null_mut(),

ktls/src/lib.rs

Lines changed: 47 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@ mod async_read_ready;
77
mod cork_stream;
88
mod ffi;
99
mod ktls_stream;
10+
pub mod setup;
1011

1112
use std::future::Future;
1213
use std::io;
1314
use std::net::SocketAddr;
14-
use std::os::unix::prelude::{AsRawFd, RawFd};
15+
use std::os::fd::AsFd;
16+
use std::os::unix::prelude::AsRawFd;
1517

1618
use futures_util::future::try_join_all;
17-
use ktls_sys::bindings as sys;
1819
#[cfg(feature = "aws_lc_rs")]
1920
use rustls::crypto::aws_lc_rs::cipher_suite;
2021
#[cfg(feature = "ring")]
@@ -26,8 +27,6 @@ use tokio::net::{TcpListener, TcpStream};
2627

2728
pub use crate::async_read_ready::AsyncReadReady;
2829
pub use crate::cork_stream::CorkStream;
29-
pub use crate::ffi::CryptoInfo;
30-
use crate::ffi::{KtlsCompatibilityError, setup_tls_info, setup_ulp};
3130
pub use crate::ktls_stream::KtlsStream;
3231

3332
#[derive(Debug, Default)]
@@ -159,7 +158,10 @@ impl CompatibleCiphers {
159158
}
160159
}
161160

162-
fn sample_cipher_setup(sock: &TcpStream, cipher_suite: SupportedCipherSuite) -> Result<(), Error> {
161+
fn sample_cipher_setup(
162+
socket: &TcpStream,
163+
cipher_suite: SupportedCipherSuite,
164+
) -> Result<(), Error> {
163165
let kcs = match KtlsCipherSuite::try_from(cipher_suite) {
164166
Ok(kcs) => kcs,
165167
Err(_) => panic!("unsupported cipher suite"),
@@ -171,31 +173,35 @@ fn sample_cipher_setup(sock: &TcpStream, cipher_suite: SupportedCipherSuite) ->
171173
};
172174

173175
let crypto_info = match kcs.typ {
174-
KtlsCipherType::AesGcm128 => CryptoInfo::AesGcm128(sys::tls12_crypto_info_aes_gcm_128 {
175-
info: sys::tls_crypto_info {
176-
version: ffi_version,
177-
cipher_type: sys::TLS_CIPHER_AES_GCM_128 as _,
178-
},
179-
iv: Default::default(),
180-
key: Default::default(),
181-
salt: Default::default(),
182-
rec_seq: Default::default(),
183-
}),
184-
KtlsCipherType::AesGcm256 => CryptoInfo::AesGcm256(sys::tls12_crypto_info_aes_gcm_256 {
185-
info: sys::tls_crypto_info {
186-
version: ffi_version,
187-
cipher_type: sys::TLS_CIPHER_AES_GCM_256 as _,
188-
},
189-
iv: Default::default(),
190-
key: Default::default(),
191-
salt: Default::default(),
192-
rec_seq: Default::default(),
193-
}),
176+
KtlsCipherType::AesGcm128 => {
177+
setup::TlsCryptoInfo::AesGcm128(libc::tls12_crypto_info_aes_gcm_128 {
178+
info: libc::tls_crypto_info {
179+
version: ffi_version,
180+
cipher_type: libc::TLS_CIPHER_AES_GCM_128 as _,
181+
},
182+
iv: Default::default(),
183+
key: Default::default(),
184+
salt: Default::default(),
185+
rec_seq: Default::default(),
186+
})
187+
}
188+
KtlsCipherType::AesGcm256 => {
189+
setup::TlsCryptoInfo::AesGcm256(libc::tls12_crypto_info_aes_gcm_256 {
190+
info: libc::tls_crypto_info {
191+
version: ffi_version,
192+
cipher_type: libc::TLS_CIPHER_AES_GCM_256 as _,
193+
},
194+
iv: Default::default(),
195+
key: Default::default(),
196+
salt: Default::default(),
197+
rec_seq: Default::default(),
198+
})
199+
}
194200
KtlsCipherType::Chacha20Poly1305 => {
195-
CryptoInfo::Chacha20Poly1305(sys::tls12_crypto_info_chacha20_poly1305 {
196-
info: sys::tls_crypto_info {
201+
setup::TlsCryptoInfo::Chacha20Poly1305(libc::tls12_crypto_info_chacha20_poly1305 {
202+
info: libc::tls_crypto_info {
197203
version: ffi_version,
198-
cipher_type: sys::TLS_CIPHER_CHACHA20_POLY1305 as _,
204+
cipher_type: libc::TLS_CIPHER_CHACHA20_POLY1305 as _,
199205
},
200206
iv: Default::default(),
201207
key: Default::default(),
@@ -204,22 +210,20 @@ fn sample_cipher_setup(sock: &TcpStream, cipher_suite: SupportedCipherSuite) ->
204210
})
205211
}
206212
};
207-
let fd = sock.as_raw_fd();
208213

209-
setup_ulp(fd).map_err(Error::UlpError)?;
214+
setup::setup_ulp(socket).map_err(Error::UlpError)?;
210215

211-
setup_tls_info(fd, ffi::Direction::Tx, crypto_info)?;
216+
crypto_info
217+
.set_tx(socket)
218+
.map_err(Error::TlsCryptoInfoError)?;
212219

213220
Ok(())
214221
}
215222

216223
#[derive(thiserror::Error, Debug)]
217224
pub enum Error {
218-
#[error("failed to enable TLS ULP (upper level protocol): {0}")]
219-
UlpError(#[source] std::io::Error),
220-
221-
#[error("kTLS compatibility error: {0}")]
222-
KtlsCompatibility(#[from] KtlsCompatibilityError),
225+
#[error(transparent)]
226+
UlpError(#[from] setup::SetupUlpError),
223227

224228
#[error("failed to export secrets")]
225229
ExportSecrets(#[source] rustls::Error),
@@ -245,7 +249,7 @@ pub async fn config_ktls_server<IO>(
245249
mut stream: tokio_rustls::server::TlsStream<CorkStream<IO>>,
246250
) -> Result<KtlsStream<IO>, Error>
247251
where
248-
IO: AsRawFd + AsyncRead + AsyncReadReady + AsyncWrite + Unpin,
252+
IO: AsFd + AsRawFd + AsyncRead + AsyncReadReady + AsyncWrite + Unpin,
249253
{
250254
stream.get_mut().0.corked = true;
251255
let drained = drain(&mut stream)
@@ -254,7 +258,7 @@ where
254258
let (io, conn) = stream.into_inner();
255259
let io = io.io;
256260

257-
setup_inner(io.as_raw_fd(), Connection::Server(conn))?;
261+
setup_inner(&io, Connection::Server(conn))?;
258262
Ok(KtlsStream::new(io, drained))
259263
}
260264

@@ -268,7 +272,7 @@ pub async fn config_ktls_client<IO>(
268272
mut stream: tokio_rustls::client::TlsStream<CorkStream<IO>>,
269273
) -> Result<KtlsStream<IO>, Error>
270274
where
271-
IO: AsRawFd + AsyncRead + AsyncWrite + Unpin,
275+
IO: AsFd + AsRawFd + AsyncRead + AsyncWrite + Unpin,
272276
{
273277
stream.get_mut().0.corked = true;
274278
let drained = drain(&mut stream)
@@ -277,7 +281,7 @@ where
277281
let (io, conn) = stream.into_inner();
278282
let io = io.io;
279283

280-
setup_inner(io.as_raw_fd(), Connection::Client(conn))?;
284+
setup_inner(&io, Connection::Client(conn))?;
281285
Ok(KtlsStream::new(io, drained))
282286
}
283287

@@ -323,7 +327,7 @@ async fn drain(stream: &mut (impl AsyncRead + Unpin)) -> std::io::Result<Option<
323327
Ok(maybe_drained)
324328
}
325329

326-
fn setup_inner(fd: RawFd, conn: Connection) -> Result<(), Error> {
330+
fn setup_inner<S: AsFd>(socket: &S, conn: Connection) -> Result<(), Error> {
327331
let cipher_suite = match conn.negotiated_cipher_suite() {
328332
Some(cipher_suite) => cipher_suite,
329333
None => {
@@ -336,13 +340,8 @@ fn setup_inner(fd: RawFd, conn: Connection) -> Result<(), Error> {
336340
Err(err) => return Err(Error::ExportSecrets(err)),
337341
};
338342

339-
ffi::setup_ulp(fd).map_err(Error::UlpError)?;
340-
341-
let tx = CryptoInfo::from_rustls(cipher_suite, secrets.tx)?;
342-
setup_tls_info(fd, ffi::Direction::Tx, tx)?;
343-
344-
let rx = CryptoInfo::from_rustls(cipher_suite, secrets.rx)?;
345-
setup_tls_info(fd, ffi::Direction::Rx, rx)?;
343+
setup::setup_ulp(socket).map_err(Error::UlpError)?;
344+
setup::setup_tls_params(socket, cipher_suite, secrets).map_err(Error::TlsCryptoInfoError)?;
346345

347346
Ok(())
348347
}

ktls/src/setup.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//! Transport Layer Security (TLS) is a Upper Layer Protocol (ULP) that runs
2+
//! over TCP. TLS provides end-to-end data integrity and confidentiality.
3+
//!
4+
//! Once the TCP connection is established, sets the TLS ULP, which allows us to
5+
//! set/get TLS socket options.
6+
//!
7+
//! This module provides the [`setup_ulp`] function, which sets the ULP (Upper
8+
//! Layer Protocol) to TLS for a TCP socket. The user can also determine whether
9+
//! the kernel supports kTLS with [`setup_ulp`].
10+
//!
11+
//! After the TLS handshake is completed, we have all the parameters required to
12+
//! move the data-path to the kernel. There is a separate socket option for
13+
//! moving the transmit and the receive into the kernel.
14+
//!
15+
//! This module provides the low-level [`setup_tls_params`] function (when
16+
//! feature `raw-api` is enabled), which sets the Kernel TLS parameters on the
17+
//! TCP socket, allowing the kernel to handle encryption and decryption of the
18+
//! TLS data.
19+
20+
mod tls;
21+
mod ulp;
22+
23+
pub(crate) use tls::{setup_tls_params, TlsCryptoInfo};
24+
pub use ulp::{setup_ulp, SetupUlpError};

0 commit comments

Comments
 (0)