@@ -7,14 +7,15 @@ mod async_read_ready;
77mod cork_stream;
88mod ffi;
99mod ktls_stream;
10+ pub mod setup;
1011
1112use std:: future:: Future ;
1213use std:: io;
1314use 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
1618use futures_util:: future:: try_join_all;
17- use ktls_sys:: bindings as sys;
1819#[ cfg( feature = "aws_lc_rs" ) ]
1920use rustls:: crypto:: aws_lc_rs:: cipher_suite;
2021#[ cfg( feature = "ring" ) ]
@@ -26,9 +27,8 @@ use tokio::net::{TcpListener, TcpStream};
2627
2728pub use crate :: async_read_ready:: AsyncReadReady ;
2829pub use crate :: cork_stream:: CorkStream ;
29- pub use crate :: ffi:: CryptoInfo ;
30- use crate :: ffi:: { KtlsCompatibilityError , setup_tls_info, setup_ulp} ;
3130pub use crate :: ktls_stream:: KtlsStream ;
31+ pub use crate :: setup:: { setup_ulp, SetupUlpError } ;
3232
3333#[ derive( Debug , Default ) ]
3434pub struct CompatibleCiphers {
@@ -159,7 +159,10 @@ impl CompatibleCiphers {
159159 }
160160}
161161
162- fn sample_cipher_setup ( sock : & TcpStream , cipher_suite : SupportedCipherSuite ) -> Result < ( ) , Error > {
162+ fn sample_cipher_setup (
163+ socket : & TcpStream ,
164+ cipher_suite : SupportedCipherSuite ,
165+ ) -> Result < ( ) , Error > {
163166 let kcs = match KtlsCipherSuite :: try_from ( cipher_suite) {
164167 Ok ( kcs) => kcs,
165168 Err ( _) => panic ! ( "unsupported cipher suite" ) ,
@@ -171,31 +174,35 @@ fn sample_cipher_setup(sock: &TcpStream, cipher_suite: SupportedCipherSuite) ->
171174 } ;
172175
173176 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- } ) ,
177+ KtlsCipherType :: AesGcm128 => {
178+ setup:: TlsCryptoInfo :: AesGcm128 ( libc:: tls12_crypto_info_aes_gcm_128 {
179+ info : libc:: tls_crypto_info {
180+ version : ffi_version,
181+ cipher_type : libc:: TLS_CIPHER_AES_GCM_128 as _ ,
182+ } ,
183+ iv : Default :: default ( ) ,
184+ key : Default :: default ( ) ,
185+ salt : Default :: default ( ) ,
186+ rec_seq : Default :: default ( ) ,
187+ } )
188+ }
189+ KtlsCipherType :: AesGcm256 => {
190+ setup:: TlsCryptoInfo :: AesGcm256 ( libc:: tls12_crypto_info_aes_gcm_256 {
191+ info : libc:: tls_crypto_info {
192+ version : ffi_version,
193+ cipher_type : libc:: TLS_CIPHER_AES_GCM_256 as _ ,
194+ } ,
195+ iv : Default :: default ( ) ,
196+ key : Default :: default ( ) ,
197+ salt : Default :: default ( ) ,
198+ rec_seq : Default :: default ( ) ,
199+ } )
200+ }
194201 KtlsCipherType :: Chacha20Poly1305 => {
195- CryptoInfo :: Chacha20Poly1305 ( sys :: tls12_crypto_info_chacha20_poly1305 {
196- info : sys :: tls_crypto_info {
202+ setup :: TlsCryptoInfo :: Chacha20Poly1305 ( libc :: tls12_crypto_info_chacha20_poly1305 {
203+ info : libc :: tls_crypto_info {
197204 version : ffi_version,
198- cipher_type : sys :: TLS_CIPHER_CHACHA20_POLY1305 as _ ,
205+ cipher_type : libc :: TLS_CIPHER_CHACHA20_POLY1305 as _ ,
199206 } ,
200207 iv : Default :: default ( ) ,
201208 key : Default :: default ( ) ,
@@ -204,22 +211,20 @@ fn sample_cipher_setup(sock: &TcpStream, cipher_suite: SupportedCipherSuite) ->
204211 } )
205212 }
206213 } ;
207- let fd = sock. as_raw_fd ( ) ;
208214
209- setup_ulp ( fd ) . map_err ( Error :: UlpError ) ?;
215+ setup :: setup_ulp ( socket ) . map_err ( Error :: UlpError ) ?;
210216
211- setup_tls_info ( fd, ffi:: Direction :: Tx , crypto_info) ?;
217+ crypto_info
218+ . set_tx ( socket)
219+ . map_err ( Error :: TlsCryptoInfoError ) ?;
212220
213221 Ok ( ( ) )
214222}
215223
216224#[ derive( thiserror:: Error , Debug ) ]
217225pub 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 ) ,
226+ #[ error( transparent) ]
227+ UlpError ( #[ from] setup:: SetupUlpError ) ,
223228
224229 #[ error( "failed to export secrets" ) ]
225230 ExportSecrets ( #[ source] rustls:: Error ) ,
@@ -245,7 +250,7 @@ pub async fn config_ktls_server<IO>(
245250 mut stream : tokio_rustls:: server:: TlsStream < CorkStream < IO > > ,
246251) -> Result < KtlsStream < IO > , Error >
247252where
248- IO : AsRawFd + AsyncRead + AsyncReadReady + AsyncWrite + Unpin ,
253+ IO : AsFd + AsRawFd + AsyncRead + AsyncReadReady + AsyncWrite + Unpin ,
249254{
250255 stream. get_mut ( ) . 0 . corked = true ;
251256 let drained = drain ( & mut stream)
@@ -254,7 +259,7 @@ where
254259 let ( io, conn) = stream. into_inner ( ) ;
255260 let io = io. io ;
256261
257- setup_inner ( io . as_raw_fd ( ) , Connection :: Server ( conn) ) ?;
262+ setup_inner ( & io , Connection :: Server ( conn) ) ?;
258263 Ok ( KtlsStream :: new ( io, drained) )
259264}
260265
@@ -268,7 +273,7 @@ pub async fn config_ktls_client<IO>(
268273 mut stream : tokio_rustls:: client:: TlsStream < CorkStream < IO > > ,
269274) -> Result < KtlsStream < IO > , Error >
270275where
271- IO : AsRawFd + AsyncRead + AsyncWrite + Unpin ,
276+ IO : AsFd + AsRawFd + AsyncRead + AsyncWrite + Unpin ,
272277{
273278 stream. get_mut ( ) . 0 . corked = true ;
274279 let drained = drain ( & mut stream)
@@ -277,7 +282,7 @@ where
277282 let ( io, conn) = stream. into_inner ( ) ;
278283 let io = io. io ;
279284
280- setup_inner ( io . as_raw_fd ( ) , Connection :: Client ( conn) ) ?;
285+ setup_inner ( & io , Connection :: Client ( conn) ) ?;
281286 Ok ( KtlsStream :: new ( io, drained) )
282287}
283288
@@ -323,7 +328,7 @@ async fn drain(stream: &mut (impl AsyncRead + Unpin)) -> std::io::Result<Option<
323328 Ok ( maybe_drained)
324329}
325330
326- fn setup_inner ( fd : RawFd , conn : Connection ) -> Result < ( ) , Error > {
331+ fn setup_inner < S : AsFd > ( socket : & S , conn : Connection ) -> Result < ( ) , Error > {
327332 let cipher_suite = match conn. negotiated_cipher_suite ( ) {
328333 Some ( cipher_suite) => cipher_suite,
329334 None => {
@@ -336,13 +341,8 @@ fn setup_inner(fd: RawFd, conn: Connection) -> Result<(), Error> {
336341 Err ( err) => return Err ( Error :: ExportSecrets ( err) ) ,
337342 } ;
338343
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) ?;
344+ setup:: setup_ulp ( socket) . map_err ( Error :: UlpError ) ?;
345+ setup:: setup_tls_params ( socket, cipher_suite, secrets) . map_err ( Error :: TlsCryptoInfoError ) ?;
346346
347347 Ok ( ( ) )
348348}
0 commit comments