diff --git a/CHANGELOG.md b/CHANGELOG.md index a8e5900d..647425a6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,9 @@ - Move libunftp and unFTP from bol.com to [CloudInvoke](https://github.com/Cloudinvoke). The GitHub repository is now [Cloudinvoke/libunftp](https://github.com/Cloudinvoke/libunftp). +- [#606](https://github.com/Cloudinvoke/libunftp/pull/606) Start TLS on the control channel right after the + `234` reply to `AUTH TLS`, so a client that sends its ClientHello at once is no longer answered in plain text + ([#605](https://github.com/Cloudinvoke/libunftp/issues/605)). - [#601](https://github.com/bolcom/libunftp/pull/601) Upgrade `base64` to 0.23.1 for the JSON-file authenticator, and the filesystem backend's test dependencies to `nix` 0.31.3 and `rstest` 0.28.0. These backend releases are pending. diff --git a/Cargo.toml b/Cargo.toml index 45e2c55a..b844656e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -104,6 +104,7 @@ dashmap = "6.2.1" [dev-dependencies] libunftp = { path = "." } pretty_assertions = "1.4.1" +rustls = { version = "0.23.46", default-features = false } tokio = { workspace = true, features = ["macros", "rt-multi-thread"] } unftp-sbe-fs = { path = "crates/unftp-sbe-fs" } diff --git a/src/server/controlchan/commands/auth.rs b/src/server/controlchan/commands/auth.rs index 8b4399c4..b9749e91 100644 --- a/src/server/controlchan/commands/auth.rs +++ b/src/server/controlchan/commands/auth.rs @@ -6,13 +6,10 @@ use crate::{ auth::UserDetail, - server::{ - chancomms::ControlChanMsg, - controlchan::{ - Reply, ReplyCode, - error::ControlChanError, - handler::{CommandContext, CommandHandler}, - }, + server::controlchan::{ + Reply, ReplyCode, + error::ControlChanError, + handler::{CommandContext, CommandHandler}, }, storage::{Metadata, StorageBackend}, }; @@ -45,17 +42,11 @@ where { #[tracing_attributes::instrument] async fn handle(&self, args: CommandContext) -> Result { - let tx = args.tx_control_chan.clone(); - let logger = args.logger; match (args.tls_configured, self.protocol.clone()) { - (true, AuthParam::Tls) => { - tokio::spawn(async move { - if let Err(err) = tx.send(ControlChanMsg::SecureControlChannel).await { - slog::warn!(logger, "AUTH: Could not send internal message to notify of TLS upgrade: {}", err); - } - }); - Ok(Reply::new(ReplyCode::AuthOkayNoDataNeeded, "Upgrading to TLS")) - } + // The control loop starts TLS as soon as this reply is sent: the client begins the + // handshake right after reading it (RFC 4217, section 4), so no further line may be + // read from the plain connection in between. + (true, AuthParam::Tls) => Ok(Reply::new(ReplyCode::AuthOkayNoDataNeeded, "Upgrading to TLS")), (true, AuthParam::Ssl) => Ok(Reply::new(ReplyCode::CommandNotImplementedForParameter, "Auth SSL not implemented")), (false, _) => Ok(Reply::new(ReplyCode::CommandNotImplemented, "TLS/SSL not configured")), } diff --git a/src/server/controlchan/control_loop.rs b/src/server/controlchan/control_loop.rs index 59b71457..996315b4 100644 --- a/src/server/controlchan/control_loop.rs +++ b/src/server/controlchan/control_loop.rs @@ -13,7 +13,7 @@ use crate::{ auth::AuthMiddleware, codecs::FtpCodec, command::Command, - commands, + commands::{self, AuthParam}, error::ControlChanError, error::ControlChanErrorKind, ftps::{FtpsControlChanEnforcerMiddleware, FtpsDataChanEnforcerMiddleware}, @@ -35,7 +35,7 @@ use futures_util::{SinkExt, StreamExt}; use rustls::ServerConnection; use std::{collections::HashMap, net::SocketAddr, ops::RangeInclusive, sync::Arc, time::Duration}; use tokio::{ - io::{AsyncRead, AsyncWrite}, + io::{AsyncRead, AsyncReadExt, AsyncWrite}, net::TcpStream, sync::{ Mutex, @@ -49,6 +49,42 @@ trait AsyncReadAsyncWriteSendUnpin: AsyncRead + AsyncWrite + Send + Unpin {} impl AsyncReadAsyncWriteSendUnpin for T {} +type ControlChannel = Framed, FtpCodec>; + +/// Runs the server side of the TLS handshake on the control connection after the 234 reply to +/// `AUTH TLS`. Returns the secured connection and the client's certificate chain, if it sent one. +/// +/// Bytes the client sent after the `AUTH TLS` line that the codec has already read (the start of +/// the handshake, sent together with the command) are handed to the TLS acceptor, not dropped. +async fn accept_tls( + channel: ControlChannel, + ftps_config: &FtpsConfig, +) -> std::io::Result<(Box, Option>)> { + let acceptor: tokio_rustls::TlsAcceptor = match ftps_config { + FtpsConfig::On { tls_config } => tls_config.clone().into(), + _ => panic!("Could not create TLS acceptor. Illegal program state"), + }; + let parts = channel.into_parts(); + if parts.read_buf.is_empty() { + Ok(secured(acceptor.accept(parts.io).await?)) + } else { + let (read, write) = tokio::io::split(parts.io); + let io = tokio::io::join(std::io::Cursor::new(parts.read_buf).chain(read), write); + Ok(secured(acceptor.accept(io).await?)) + } +} + +fn secured(stream: tokio_rustls::server::TlsStream) -> (Box, Option>) +where + IO: AsyncRead + AsyncWrite + Send + Unpin + 'static, +{ + let connection: &ServerConnection = stream.get_ref().1; + let cert_chain = connection + .peer_certificates() + .map(|certs| certs.iter().map(|c| unftp_core::auth::ClientCert(c.as_ref().to_vec())).collect()); + (Box::new(stream), cert_chain) +} + #[derive(Debug, Clone)] pub struct Config where @@ -236,57 +272,61 @@ where return; } Some(Ok(event)) => { - if let Event::InternalMsg(ControlChanMsg::SecureControlChannel) = event { - slog::info!(logger, "Upgrading control channel to TLS"); - - // Get back the original TCP Stream - let codec_io = reply_sink.reunite(command_source).unwrap(); - let io = codec_io.into_inner(); - - // Wrap in TLS Stream - let acceptor: tokio_rustls::TlsAcceptor = match ftps_config.clone() { - FtpsConfig::On { tls_config } => tls_config.into(), - _ => panic!("Could not create TLS acceptor. Illegal program state"), - }; - let accepted = acceptor.accept(io).await; - let io: Box = match accepted { - Ok(stream) => { - let s: &ServerConnection = stream.get_ref().1; - if let Some(certs) = s.peer_certificates() { - let mut session = shared_session.lock().await; - session.cert_chain = Some(certs.iter().map(|c| unftp_core::auth::ClientCert(c.as_ref().to_vec())).collect()); - } - Box::new(stream) - } - Err(err) => { - slog::warn!(logger, "Closing control channel. Could not upgrade to TLS: {}", err); - return; - } - }; - - // Wrap in codec again and get sink + source - let codec = FtpCodec::new(); - let cmd_and_reply_stream = codec.framed(io); - let (sink, src) = cmd_and_reply_stream.split(); - reply_sink = sink; - command_source = src; - } - if let Event::Command(Command::User { username }) = &event { let s: String = String::from_utf8_lossy(username).into(); logger = logger.new(slog::o!("username" => s)); } + let auth_tls = matches!(event, Event::Command(Command::Auth { protocol: AuthParam::Tls })); + // TODO: Handle Event::InternalMsg(InternalMsg::PlaintextControlChannel) let handle_result = match event_chain.handle(event).await { Err(e) => Err(e), - Ok(reply) => reply_sink.send(reply).await, + Ok(reply) => { + let start_tls = auth_tls + && matches!( + reply, + Reply::CodeAndMsg { + code: ReplyCode::AuthOkayNoDataNeeded, + .. + } + ); + reply_sink.send(reply).await.map(|()| start_tls) + } }; - if let Err(chan_err) = handle_result { - slog::warn!(logger, "Event handler chain error: {:?}. Closing control connection", chan_err); - return; + match handle_result { + Err(chan_err) => { + slog::warn!(logger, "Event handler chain error: {:?}. Closing control connection", chan_err); + return; + } + Ok(false) => {} + Ok(true) => { + // The 234 reply to AUTH TLS is out and the client starts the TLS + // handshake right after reading it, so TLS starts here, before + // anything else is read from the plain connection. + slog::info!(logger, "Upgrading control channel to TLS"); + let channel = reply_sink.reunite(command_source).expect("halves of the same control channel"); + let (io, cert_chain) = match accept_tls(channel, &ftps_config).await { + Ok(accepted) => accepted, + Err(err) => { + slog::warn!(logger, "Closing control channel. Could not upgrade to TLS: {}", err); + return; + } + }; + if cert_chain.is_some() { + shared_session.lock().await.cert_chain = cert_chain; + } + let (sink, src) = FtpCodec::new().framed(io).split(); + reply_sink = sink; + command_source = src; + + if let Err(chan_err) = event_chain.handle(Event::InternalMsg(ControlChanMsg::SecureControlChannel)).await { + slog::warn!(logger, "Event handler chain error: {:?}. Closing control connection", chan_err); + return; + } + } } } Some(Err(e)) => { diff --git a/tests/ftps.rs b/tests/ftps.rs new file mode 100644 index 00000000..412e1538 --- /dev/null +++ b/tests/ftps.rs @@ -0,0 +1,125 @@ +#![allow(missing_docs)] + +//! FTPS control channel upgrade via `AUTH TLS` (RFC 4217, section 4). + +use libunftp::ServerBuilder; +use rustls::pki_types::{CertificateDer, ServerName, pem::PemObject}; +use rustls::{ClientConfig, ClientConnection, RootCertStore}; +use std::io::{Read, Write}; +use std::net::TcpStream; +use std::sync::Arc; +use std::time::Duration; +use unftp_sbe_fs::Filesystem; + +const CERTS: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/tests/resources/ftps/server.pem"); +const KEY: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/tests/resources/ftps/server.key"); +const CA: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/tests/resources/ftps/ca.pem"); + +/// How many control connections each test upgrades. +const SESSIONS: usize = 50; + +async fn start_server(addr: &'static str) { + let root = std::env::temp_dir(); + let server = ServerBuilder::new(Box::new(move || Filesystem::new(root.clone()).unwrap())) + .greeting("Welcome test") + .ftps(CERTS, KEY) + .build() + .unwrap(); + tokio::spawn(server.listen(addr)); + for _ in 0..50 { + if tokio::net::TcpStream::connect(addr).await.is_ok() { + return; + } + tokio::time::sleep(Duration::from_millis(100)).await; + } + panic!("the FTPS test server did not start on {addr}"); +} + +fn client_config() -> Arc { + let mut roots = RootCertStore::empty(); + for cert in CertificateDer::pem_file_iter(CA).unwrap() { + roots.add(cert.unwrap()).unwrap(); + } + Arc::new(ClientConfig::builder().with_root_certificates(roots).with_no_client_auth()) +} + +/// Reads one reply line, one byte at a time so that nothing after the line is consumed. +fn read_line(stream: &mut impl Read) -> std::io::Result { + let mut line = Vec::new(); + let mut byte = [0_u8; 1]; + while !line.ends_with(b"\r\n") { + if stream.read(&mut byte)? == 0 { + return Err(std::io::ErrorKind::UnexpectedEof.into()); + } + line.push(byte[0]); + } + Ok(String::from_utf8_lossy(&line).into_owned()) +} + +/// Upgrades one control connection with `AUTH TLS` and sends a command over TLS. +/// +/// The client sends its TLS ClientHello as soon as it has read the 234 reply, or, when +/// `pipelined` is set, in the same write as the `AUTH TLS` command. +fn upgrade_session(addr: &str, config: &Arc, pipelined: bool) -> Result<(), String> { + let mut stream = TcpStream::connect(addr).map_err(|e| e.to_string())?; + stream.set_read_timeout(Some(Duration::from_secs(5))).map_err(|e| e.to_string())?; + let greeting = read_line(&mut stream).map_err(|e| format!("greeting: {e}"))?; + if !greeting.starts_with("220") { + return Err(format!("greeting: {greeting:?}")); + } + + let mut tls = ClientConnection::new(config.clone(), ServerName::try_from("localhost").unwrap()).map_err(|e| e.to_string())?; + let mut client_hello = Vec::new(); + tls.write_tls(&mut client_hello).map_err(|e| e.to_string())?; + + let reply = if pipelined { + stream + .write_all(&[b"AUTH TLS\r\n".as_slice(), &client_hello].concat()) + .map_err(|e| e.to_string())?; + read_line(&mut stream).map_err(|e| format!("AUTH reply: {e}"))? + } else { + stream.write_all(b"AUTH TLS\r\n").map_err(|e| e.to_string())?; + let reply = read_line(&mut stream).map_err(|e| format!("AUTH reply: {e}"))?; + stream.write_all(&client_hello).map_err(|e| e.to_string())?; + reply + }; + if !reply.starts_with("234") { + return Err(format!("AUTH reply: {reply:?}")); + } + + let mut channel = rustls::Stream::new(&mut tls, &mut stream); + channel.write_all(b"NOOP\r\n").map_err(|e| format!("TLS handshake: {e}"))?; + let reply = read_line(&mut channel).map_err(|e| format!("NOOP over TLS: {e}"))?; + if !reply.starts_with("200") { + return Err(format!("NOOP over TLS: {reply:?}")); + } + Ok(()) +} + +async fn upgrade_sessions(addr: &'static str, pipelined: bool) { + start_server(addr).await; + let failures = tokio::task::spawn_blocking(move || { + let config = client_config(); + (0..SESSIONS) + .filter_map(|_| upgrade_session(addr, &config, pipelined).err()) + .collect::>() + }) + .await + .unwrap(); + assert!( + failures.is_empty(), + "{} of {SESSIONS} upgrades failed, first: {}", + failures.len(), + failures.first().unwrap() + ); +} + +#[tokio::test(flavor = "multi_thread")] +async fn auth_tls_upgrades_when_the_client_hello_follows_the_reply() { + upgrade_sessions("127.0.0.1:2160", false).await; +} + +#[tokio::test(flavor = "multi_thread")] +async fn auth_tls_upgrades_when_the_client_hello_arrives_with_the_command() { + upgrade_sessions("127.0.0.1:2161", true).await; +} diff --git a/tests/resources/ftps/ca.pem b/tests/resources/ftps/ca.pem new file mode 100644 index 00000000..66e439be --- /dev/null +++ b/tests/resources/ftps/ca.pem @@ -0,0 +1,19 @@ +-----BEGIN CERTIFICATE----- +MIIDKTCCAhGgAwIBAgIUYkEsWpwVWxuN6oFKv8YB6oZu8iwwDQYJKoZIhvcNAQEL +BQAwGzEZMBcGA1UEAwwQbGlidW5mdHAgdGVzdCBDQTAgFw0yNjExMDYyMDAxMDJa +GA8yMTI2MTAxMzIwMDEwMlowGzEZMBcGA1UEAwwQbGlidW5mdHAgdGVzdCBDQTCC +ASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBALjfBr0/toYDZoDYSjuuSD20 +KGjUbs7UEewJCHQ9D/OdoLbIMaFUa2vmkxVAFe1oxNQkg+GfocC9+dfri8NXgzgD +4v8ewhLCu+3FrGsIhA101GEi+wPwtNSuAQFveVVb37rP+sBYgFCOjmMSAT7LRXSX +3PlK0Nj1BmHDLZFDi029goS5b2QrgiECay5iUiLRmkBHvi5InmrmI33Wu0Bms6mV +k0BQW89HuRv4FSAaKY+LI9CR6XIvUfqIYRLL4yC0+wiuJOVNmrkC5fOQV0OvUCVi +OkxspqyMJwX7ffAprHdsqt2Yto4HUO5VVe+5AVpdd3hZruUY2guHfh+VPBDWV08C +AwEAAaNjMGEwHQYDVR0OBBYEFFWVIhJqh50xq8i6cW122RwRel+CMB8GA1UdIwQY +MBaAFFWVIhJqh50xq8i6cW122RwRel+CMA8GA1UdEwEB/wQFMAMBAf8wDgYDVR0P +AQH/BAQDAgIEMA0GCSqGSIb3DQEBCwUAA4IBAQA7qADAQSwy/b5q7N8cIsackQTn +VV64dGV6seZEV4LKb2qvlpyi1392w4V9NxYmzsKCS2eOvw2/1r068nBw+hEJ9vEo +fk0zIViVYC8hudM6VZkD5iNEvUNj1GIXxm6YX3h8NDnV/ClmZ6rmd2gQ29+Qyttj +NSAoEiPeVPzDKy4sjbHbTLWh+bLk8D36TMQqjPs+2CH8+OquqB4fzWv70yh6oAUx +nSGcSFkC3mVrUYQmOlfTOXL+M50PYtDHqrvmjUXwhNUbrFxqlnF/iMOtdjHTGpSe +H8OYUb+O32gHLklfYmtK3fkqWd4nEeBg3Ir6aHJiLZb+iAcw3Q85nlsw8RfA +-----END CERTIFICATE----- diff --git a/tests/resources/ftps/server.key b/tests/resources/ftps/server.key new file mode 100644 index 00000000..93d8a7cb --- /dev/null +++ b/tests/resources/ftps/server.key @@ -0,0 +1,28 @@ +-----BEGIN PRIVATE KEY----- +MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDHx8aMsXhSSvz9 +SwxUdXr1Ms3sHnNs74PXVaW1QM8LWlqAoEzuk19xmTQGjdUjVs8PXGLj2Xt9Xwmc +QW0I8q261kL11hocqiDzCEk25Kdu/Dmr20mJ00mOsOjvlQIUTifUFvXxKCZXa8CS +LyBHlzMWrW6iT4xS+NH8Efr1b1xjmFy6JV4yfEMwWDtlVCbmGly+jlnzwv7a3dnI +4n4OQqsNOj2ZAASYRVYHzKp4fmkZKLmDyFE50u/YOkt24QNes5eJoQQNtvYNrr7U +iM2+ULBmx4lUHqTNWvpkIdhxC1eRtrKC1yYwPiYvXxLM713+zlm8V9DKKK1e4n6w +O75mpB6hAgMBAAECggEAA0fiWJakg3N0TqOQHwXz4Dl2nMFOQ3R2fY+MQlr/W3Af +KtwXD1bvuxGZQ19KaANpsJajkBrW9pLcOUrww7QaisYwgNGEicJ4eo1BCIv+POoa +91nJTiBgZZ29oYuYpmU3Lbo3zT9Ly+U62xu3g711qxJrjfJQ+17XMfTIooWy5nbH +Wk4iNLhP7xq34T80+M5fMeNUICUS6NMt6Dwwnqu5zRwFT7K7bYaj/uX9Qq1CsI1y +S/WZO3oXda1eGkZWM2MDnoG80pypEXJ/hvn6sH/+u2CnoYVpFHHXF7hVBrbaFnBE +0eXUPyJ5l+hGsAa3+SvtVFToB3QnmyVoZrP1PPiKQQKBgQD+dDJY17IOlGc3Sazf +k2dE42galwLTQv9l0Qis4PfNCwXk9NdUtU7mT+zAaDRfuz63QKRW26BKQfqkPcRf +22/yexbImCTc3QefrqBW7z9oJtncPCODuF58XLe08HWZdgvrLUuN1GyrL5nt3iEb +DCBIV3tIwSeRizMs/b/Nnl02kQKBgQDI/ojAxBlvzCHIHYkjC7wSgHVkp1Utsthk +SiDai9H644DHUHr1KYg9wfzG4T8vpdXTi2qT4/poRfOW5UeULUj+yR45PD/u8Zp2 +JwlPJnAxn6K/TsxssoexVWRfE29Qva+X5nzA+32UXAhRZpeNmoQVu6AWfSgaoB/v +nKJekrEPEQKBgDVB8su/kagVjaj3xbQh8QptInv2QYQgUL5pywwMJjGHbhtm168G +J9ZywhXfeZ7T3SOT5xG69Ei2mJRJzVFW5450Due7WHPV22OjtDGd78KH5y9+ctx9 ++WDEMzsu3R5ao9H8cATmgjwuQWSVCo7TseoSTV6D9xkybmqrTbrmu5HRAoGBAMSW +UGNaahay7gew+8TPYXAnPKrqD6HW31eYOXIId6SQiBBqYxkPOjZNWlObVBXiebWj +Eub7gJwkbxATyWLguBib1XfduhqXLKCLpctIqUuf+6pqA//8zkNgZf+oHqQ2I1zL +DxIh6L4HFgun8GuLyc0H6piUaXlBTHssFJOznoJBAoGBAIzEdo300ypcZjyULvMl +kx+mbWdwU9XSe4agh7nEP1e/M2RKbymEKgV4z/Rq1bQyxJRVOc2kl8HwszwyyMs6 +CT2An3eIbkrZuIAiI/JgObcuj6oFOzOjRx+eAEZhT3HV8WAIIQmjndTG1TApraFS +pYxrobY6hUq2VuIzZOYiV6VQ +-----END PRIVATE KEY----- diff --git a/tests/resources/ftps/server.pem b/tests/resources/ftps/server.pem new file mode 100644 index 00000000..1f2236bb --- /dev/null +++ b/tests/resources/ftps/server.pem @@ -0,0 +1,20 @@ +-----BEGIN CERTIFICATE----- +MIIDNzCCAh+gAwIBAgIUYqaLW5OPfGcO21ldOpcFzAsFyBQwDQYJKoZIhvcNAQEL +BQAwGzEZMBcGA1UEAwwQbGlidW5mdHAgdGVzdCBDQTAgFw0yNjExMDYyMDAxMDNa +GA8yMTI2MTAxMzIwMDEwM1owFDESMBAGA1UEAwwJbG9jYWxob3N0MIIBIjANBgkq +hkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAx8fGjLF4Ukr8/UsMVHV69TLN7B5zbO+D +11WltUDPC1pagKBM7pNfcZk0Bo3VI1bPD1xi49l7fV8JnEFtCPKtutZC9dYaHKog +8whJNuSnbvw5q9tJidNJjrDo75UCFE4n1Bb18SgmV2vAki8gR5czFq1uok+MUvjR +/BH69W9cY5hcuiVeMnxDMFg7ZVQm5hpcvo5Z88L+2t3ZyOJ+DkKrDTo9mQAEmEVW +B8yqeH5pGSi5g8hROdLv2DpLduEDXrOXiaEEDbb2Da6+1IjNvlCwZseJVB6kzVr6 +ZCHYcQtXkbaygtcmMD4mL18SzO9d/s5ZvFfQyiitXuJ+sDu+ZqQeoQIDAQABo3gw +djAUBgNVHREEDTALgglsb2NhbGhvc3QwCQYDVR0TBAIwADATBgNVHSUEDDAKBggr +BgEFBQcDATAdBgNVHQ4EFgQUgaPTVppUNMdrMDfReyWkSmgcAlswHwYDVR0jBBgw +FoAUVZUiEmqHnTGryLpxbXbZHBF6X4IwDQYJKoZIhvcNAQELBQADggEBALXONQXA +We70GAiQsEdEXkcoARPzbP14SyFOrUskGPTMgQqa/6SC169osQO4tp+FpxniSlbn +FgcEBA9rcMS7fO7cXuF83KE1twdfOnPy69+jPLVxHdaPcDBfRKw611aJ/GX3466D +72exbhdgIl6Q8MkCuaRssoe74AWFM6kVvB7lo8rScqDEYK8no3MhFEpIlStaO9Ju +LSBecByMkl81B4InZJaGUVB+vpXNDrFf/8InMOT4n8P/FQDh14TMgP/jfTwr3wYL +rrCc3fTpPVeU8L0LNeR6yP0iVeXpMXHsyYXKMXZm6g64FxHDG/0i8fpGh3l4gYOl +TViG2is+loyMDGk= +-----END CERTIFICATE-----