aria2-protocol 0.3.7

Multi-protocol networking stack for aria2-rust: HTTP/HTTPS client, FTP/SFTP, full BitTorrent (DHT/PEX/MSE), and Metalink V3/V4 parser
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
//! FTPS transport primitives and RFC 4217 negotiation.
//!
//! This module owns the protocol-level TLS implementation used by FTP
//! callers.  The download engine is responsible only for choosing when to
//! use it and mapping application configuration into [`FtpsConfig`].

use std::io;
use std::path::{Path, PathBuf};
use std::pin::Pin;
use std::sync::Arc;
use std::task::{Context, Poll};

use rustls::DigitallySignedStruct;
use rustls::client::danger::{HandshakeSignatureValid, ServerCertVerified, ServerCertVerifier};
use rustls::pki_types::{CertificateDer, ServerName, UnixTime};
use tokio::io::{AsyncBufRead, AsyncRead, AsyncWrite, ReadBuf};
use tokio::net::TcpStream;
use tokio_rustls::client::TlsStream;
use tracing::{debug, info, warn};

/// FTPS (FTP over TLS) configuration per RFC 4217.
///
/// The engine maps its user-facing options into this transport configuration.
/// The protocol module then applies it consistently to control and data
/// channels.
#[derive(Debug, Clone)]
pub struct FtpsConfig {
    /// Whether the caller requested an FTPS connection.
    pub enabled: bool,
    /// Verify the server certificate chain.
    ///
    /// Disabling verification is insecure and should only be used when the
    /// caller explicitly opts into it.
    pub check_certificate: bool,
    /// Optional PEM file containing trusted CA certificates.
    ///
    /// When omitted, the bundled Mozilla roots are used.
    pub ca_certificate: Option<PathBuf>,
    /// Minimum TLS protocol version to negotiate.
    pub min_tls_version: TlsVersion,
}

impl Default for FtpsConfig {
    fn default() -> Self {
        Self {
            enabled: false,
            check_certificate: true,
            ca_certificate: None,
            min_tls_version: TlsVersion::Tls12,
        }
    }
}

/// Minimum TLS protocol version for FTPS connections.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum TlsVersion {
    /// TLS 1.2 (RFC 5246).
    #[default]
    Tls12,
    /// TLS 1.3 (RFC 8446).
    Tls13,
}

/// FTP control stream which can be plain TCP or TLS-protected.
#[derive(Debug)]
pub enum FtpControlStream {
    /// Unencrypted FTP control connection.
    Plain(TcpStream),
    /// TLS-encrypted FTP control connection.
    Tls(Box<TlsStream<TcpStream>>),
}

impl FtpControlStream {
    /// Returns whether this stream is TLS-protected.
    pub fn is_tls(&self) -> bool {
        matches!(self, Self::Tls(_))
    }

    /// Returns the underlying TCP stream reference.
    pub fn get_ref(&self) -> Option<&TcpStream> {
        match self {
            Self::Plain(stream) => Some(stream),
            Self::Tls(stream) => Some(stream.get_ref().0),
        }
    }
}

/// FTP data stream which can be plain TCP or protected by `PROT P`.
#[derive(Debug)]
pub enum FtpDataStream {
    /// Unencrypted FTP data connection.
    Plain(TcpStream),
    /// TLS-encrypted FTP data connection.
    Tls(Box<TlsStream<TcpStream>>),
}

impl FtpDataStream {
    /// Set TCP_NODELAY on the underlying socket.
    pub fn set_nodelay(&self, enabled: bool) -> io::Result<()> {
        match self {
            Self::Plain(stream) => stream.set_nodelay(enabled),
            Self::Tls(stream) => stream.get_ref().0.set_nodelay(enabled),
        }
    }
}

fn parse_response_code(response: &str, command: &str) -> Result<u16, String> {
    let code_bytes = response
        .as_bytes()
        .get(..3)
        .ok_or_else(|| format!("{} response too short: {}", command, response))?;
    let code_text = std::str::from_utf8(code_bytes)
        .map_err(|_| format!("Invalid {} response code: {}", command, response))?;
    code_text
        .parse()
        .map_err(|_| format!("Invalid {} response code: {}", command, response))
}

// =============================================================================
// TLS connector construction
// =============================================================================

/// Build a `tokio_rustls::TlsConnector` from an [`FtpsConfig`].
pub fn build_tls_connector(config: &FtpsConfig) -> Result<tokio_rustls::TlsConnector, String> {
    use rustls::ClientConfig;
    use rustls::crypto::ring::default_provider;

    let _ = default_provider().install_default();
    let mut root_store = rustls::RootCertStore::empty();

    if config.check_certificate {
        if let Some(ref ca_path) = config.ca_certificate {
            let certs = load_pem_certs(ca_path)?;
            let (added, rejected) = root_store.add_parsable_certificates(certs);
            if added == 0 {
                return Err(format!(
                    "No valid CA certificates found in: {} ({} rejected)",
                    ca_path.display(),
                    rejected
                ));
            }
            info!(
                "Loaded {} CA certificate(s) from {} ({} rejected)",
                added,
                ca_path.display(),
                rejected
            );
        } else {
            root_store.roots = webpki_roots::TLS_SERVER_ROOTS.to_vec();
            if root_store.is_empty() {
                return Err("No bundled root certificates available".to_string());
            }
            debug!(
                "Using {} bundled Mozilla root certificates",
                root_store.len()
            );
        }
    } else {
        warn!("Certificate verification DISABLED — FTPS connection is insecure");
    }

    let mut client_config = match config.min_tls_version {
        TlsVersion::Tls12 => ClientConfig::builder()
            .with_root_certificates(root_store)
            .with_no_client_auth(),
        TlsVersion::Tls13 => ClientConfig::builder_with_provider(Arc::new(default_provider()))
            .with_protocol_versions(&[&rustls::version::TLS13])
            .map_err(|e| format!("Failed to set TLS 1.3 only: {}", e))?
            .with_root_certificates(root_store)
            .with_no_client_auth(),
    };

    if !config.check_certificate {
        client_config
            .dangerous()
            .set_certificate_verifier(Arc::new(NoCertificateVerification));
    }

    Ok(tokio_rustls::TlsConnector::from(Arc::new(client_config)))
}

fn load_pem_certs(path: &Path) -> Result<Vec<CertificateDer<'static>>, String> {
    use std::io::BufReader as SyncBufReader;

    let file = std::fs::File::open(path).map_err(|e| {
        format!(
            "Failed to open CA certificate file {}: {}",
            path.display(),
            e
        )
    })?;

    let mut reader = SyncBufReader::new(file);
    let certs: Vec<CertificateDer<'static>> = rustls_pemfile::certs(&mut reader)
        .collect::<Result<Vec<_>, _>>()
        .map_err(|e| {
            format!(
                "Failed to parse CA certificates from {}: {}",
                path.display(),
                e
            )
        })?;

    if certs.is_empty() {
        return Err(format!("No PEM certificates found in {}", path.display()));
    }

    Ok(certs)
}

/// Certificate verifier used when certificate checking is explicitly disabled.
#[derive(Debug)]
pub(crate) struct NoCertificateVerification;

impl ServerCertVerifier for NoCertificateVerification {
    fn verify_server_cert(
        &self,
        _end_entity: &CertificateDer<'_>,
        _intermediates: &[CertificateDer<'_>],
        _server_name: &ServerName<'_>,
        _ocsp_response: &[u8],
        _now: UnixTime,
    ) -> Result<ServerCertVerified, rustls::Error> {
        Ok(ServerCertVerified::assertion())
    }

    fn verify_tls12_signature(
        &self,
        _message: &[u8],
        _cert: &CertificateDer<'_>,
        _dss: &DigitallySignedStruct,
    ) -> Result<HandshakeSignatureValid, rustls::Error> {
        Ok(HandshakeSignatureValid::assertion())
    }

    fn verify_tls13_signature(
        &self,
        _message: &[u8],
        _cert: &CertificateDer<'_>,
        _dss: &DigitallySignedStruct,
    ) -> Result<HandshakeSignatureValid, rustls::Error> {
        Ok(HandshakeSignatureValid::assertion())
    }

    fn supported_verify_schemes(&self) -> Vec<rustls::SignatureScheme> {
        rustls::crypto::ring::default_provider()
            .signature_verification_algorithms
            .supported_schemes()
    }
}

// =============================================================================
// RFC 4217 control-channel negotiation
// =============================================================================

/// Negotiate protected FTP data channels on an established control stream.
///
/// This sends `PBSZ 0` followed by `PROT P`. It is shared by explicit FTPS
/// after `AUTH TLS` and implicit FTPS after the initial TLS handshake.
/// Returning an error for either rejected response keeps callers from
/// accidentally treating a TLS control channel as a protected FTPS session
/// when its data channel would still be plaintext.
pub async fn negotiate_protected_data_channel<R>(reader: &mut R) -> Result<(), String>
where
    R: AsyncBufRead + AsyncWrite + Unpin,
{
    use tokio::io::{AsyncBufReadExt, AsyncWriteExt};

    debug!("Sending PBSZ 0");
    reader
        .write_all(b"PBSZ 0\r\n")
        .await
        .map_err(|e| format!("Failed to send PBSZ 0: {}", e))?;
    reader
        .flush()
        .await
        .map_err(|e| format!("Failed to flush PBSZ 0: {}", e))?;

    let mut line_buf = String::new();
    reader
        .read_line(&mut line_buf)
        .await
        .map_err(|e| format!("Failed to read PBSZ response: {}", e))?;
    let pbsz_resp = line_buf.trim();
    debug!("PBSZ response: {}", pbsz_resp);
    let pbsz_code = parse_response_code(pbsz_resp, "PBSZ")?;
    if pbsz_code != 200 {
        return Err(format!(
            "PBSZ 0 rejected by server: {} (expected 200)",
            pbsz_resp
        ));
    }

    debug!("Sending PROT P");
    reader
        .write_all(b"PROT P\r\n")
        .await
        .map_err(|e| format!("Failed to send PROT P: {}", e))?;
    reader
        .flush()
        .await
        .map_err(|e| format!("Failed to flush PROT P: {}", e))?;

    line_buf.clear();
    reader
        .read_line(&mut line_buf)
        .await
        .map_err(|e| format!("Failed to read PROT response: {}", e))?;
    let prot_resp = line_buf.trim();
    debug!("PROT response: {}", prot_resp);
    let prot_code = parse_response_code(prot_resp, "PROT")?;
    if prot_code != 200 {
        return Err(format!(
            "PROT P rejected by server: {} (data channel would be unencrypted)",
            prot_resp
        ));
    }

    info!("PBSZ 0 and PROT P accepted — data channel will be TLS-protected");
    Ok(())
}

/// Perform `AUTH TLS`, the TLS handshake, `PBSZ 0`, and `PROT P`.
pub async fn upgrade_control_stream(
    mut stream: TcpStream,
    host: &str,
    config: &FtpsConfig,
) -> Result<TlsStream<TcpStream>, String> {
    use tokio::io::{AsyncBufReadExt, AsyncWriteExt};

    debug!("Sending AUTH TLS to {}", host);
    stream
        .write_all(b"AUTH TLS\r\n")
        .await
        .map_err(|e| format!("Failed to send AUTH TLS: {}", e))?;
    stream
        .flush()
        .await
        .map_err(|e| format!("Failed to flush AUTH TLS: {}", e))?;

    let mut reader = tokio::io::BufReader::new(stream);
    let mut line_buf = String::new();
    reader
        .read_line(&mut line_buf)
        .await
        .map_err(|e| format!("Failed to read AUTH TLS response: {}", e))?;

    let response = line_buf.trim();
    debug!("AUTH TLS response: {}", response);
    let code = parse_response_code(response, "AUTH TLS")?;
    if code != 234 {
        return Err(format!(
            "AUTH TLS rejected by server: {} (expected 234)",
            response
        ));
    }
    info!("AUTH TLS accepted (234) — proceeding with TLS handshake");

    let stream = reader.into_inner();
    let mut tls_stream = perform_tls_handshake(stream, host, config).await?;
    let mut reader = tokio::io::BufReader::new(&mut tls_stream);
    negotiate_protected_data_channel(&mut reader).await?;
    Ok(tls_stream)
}

/// Perform a TLS handshake on an FTP control or implicit-FTPS connection.
pub async fn perform_tls_handshake(
    stream: TcpStream,
    host: &str,
    config: &FtpsConfig,
) -> Result<TlsStream<TcpStream>, String> {
    let connector = build_tls_connector(config)?;
    let server_name: ServerName<'static> = ServerName::try_from(host.to_string())
        .map_err(|e| format!("Invalid FTPS server name '{}': {:?}", host, e))?;

    debug!("Starting TLS handshake with {}", host);
    let tls_stream = connector
        .connect(server_name, stream)
        .await
        .map_err(|e| format!("FTPS TLS handshake failed for {}: {}", host, e))?;
    info!("TLS handshake completed successfully with {}", host);
    Ok(tls_stream)
}

/// Upgrade a `PROT P` FTP data connection to TLS.
pub async fn upgrade_data_stream(
    stream: TcpStream,
    host: &str,
    config: &FtpsConfig,
) -> Result<TlsStream<TcpStream>, String> {
    debug!("Upgrading FTP data connection to TLS for {}", host);
    let tls_stream = perform_tls_handshake(stream, host, config).await?;
    info!("FTPS data connection TLS handshake completed with {}", host);
    Ok(tls_stream)
}

impl AsyncRead for FtpControlStream {
    fn poll_read(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        buf: &mut ReadBuf<'_>,
    ) -> Poll<io::Result<()>> {
        match self.get_mut() {
            Self::Plain(stream) => Pin::new(stream).poll_read(cx, buf),
            Self::Tls(stream) => Pin::new(stream.as_mut()).poll_read(cx, buf),
        }
    }
}

impl AsyncWrite for FtpControlStream {
    fn poll_write(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        buf: &[u8],
    ) -> Poll<io::Result<usize>> {
        match self.get_mut() {
            Self::Plain(stream) => Pin::new(stream).poll_write(cx, buf),
            Self::Tls(stream) => Pin::new(stream.as_mut()).poll_write(cx, buf),
        }
    }

    fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
        match self.get_mut() {
            Self::Plain(stream) => Pin::new(stream).poll_flush(cx),
            Self::Tls(stream) => Pin::new(stream.as_mut()).poll_flush(cx),
        }
    }

    fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
        match self.get_mut() {
            Self::Plain(stream) => Pin::new(stream).poll_shutdown(cx),
            Self::Tls(stream) => Pin::new(stream.as_mut()).poll_shutdown(cx),
        }
    }
}

impl AsyncRead for FtpDataStream {
    fn poll_read(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        buf: &mut ReadBuf<'_>,
    ) -> Poll<io::Result<()>> {
        match self.get_mut() {
            Self::Plain(stream) => Pin::new(stream).poll_read(cx, buf),
            Self::Tls(stream) => Pin::new(stream.as_mut()).poll_read(cx, buf),
        }
    }
}

impl AsyncWrite for FtpDataStream {
    fn poll_write(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        buf: &[u8],
    ) -> Poll<io::Result<usize>> {
        match self.get_mut() {
            Self::Plain(stream) => Pin::new(stream).poll_write(cx, buf),
            Self::Tls(stream) => Pin::new(stream.as_mut()).poll_write(cx, buf),
        }
    }

    fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
        match self.get_mut() {
            Self::Plain(stream) => Pin::new(stream).poll_flush(cx),
            Self::Tls(stream) => Pin::new(stream.as_mut()).poll_flush(cx),
        }
    }

    fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
        match self.get_mut() {
            Self::Plain(stream) => Pin::new(stream).poll_shutdown(cx),
            Self::Tls(stream) => Pin::new(stream.as_mut()).poll_shutdown(cx),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn ftps_config_default() {
        let config = FtpsConfig::default();
        assert!(!config.enabled);
        assert!(config.check_certificate);
        assert!(config.ca_certificate.is_none());
        assert_eq!(config.min_tls_version, TlsVersion::Tls12);
    }

    #[test]
    fn tls_version_default() {
        assert_eq!(TlsVersion::default(), TlsVersion::Tls12);
    }

    #[test]
    fn response_code_requires_ascii_three_digit_prefix() {
        assert_eq!(parse_response_code("200 OK", "TEST").unwrap(), 200);
        assert!(parse_response_code("20", "TEST").is_err());
        assert!(parse_response_code("é00 OK", "TEST").is_err());
        assert!(parse_response_code("ABC OK", "TEST").is_err());
    }

    #[test]
    fn connector_builds_with_check_enabled() {
        let config = FtpsConfig {
            enabled: true,
            check_certificate: true,
            ca_certificate: None,
            min_tls_version: TlsVersion::Tls12,
        };
        assert!(build_tls_connector(&config).is_ok());
    }

    #[test]
    fn connector_builds_with_check_disabled() {
        let config = FtpsConfig {
            enabled: true,
            check_certificate: false,
            ca_certificate: None,
            min_tls_version: TlsVersion::Tls12,
        };
        assert!(build_tls_connector(&config).is_ok());
    }

    #[test]
    fn connector_rejects_missing_ca_file() {
        let config = FtpsConfig {
            enabled: true,
            check_certificate: true,
            ca_certificate: Some(PathBuf::from("/nonexistent/ca.pem")),
            min_tls_version: TlsVersion::Tls12,
        };
        assert!(build_tls_connector(&config).is_err());
    }

    #[test]
    fn connector_builds_with_tls13() {
        let config = FtpsConfig {
            enabled: true,
            check_certificate: true,
            ca_certificate: None,
            min_tls_version: TlsVersion::Tls13,
        };
        assert!(build_tls_connector(&config).is_ok());
    }

    #[tokio::test]
    async fn protected_data_channel_negotiation_requires_both_responses() {
        use tokio::io::{AsyncBufReadExt, AsyncWriteExt};

        let (client, server) = tokio::io::duplex(256);
        let server = tokio::spawn(async move {
            let mut reader = tokio::io::BufReader::new(server);
            let mut line = String::new();
            reader.read_line(&mut line).await.unwrap();
            assert_eq!(line, "PBSZ 0\r\n");
            reader
                .get_mut()
                .write_all(b"200 PBSZ ok\r\n")
                .await
                .unwrap();
            line.clear();
            reader.read_line(&mut line).await.unwrap();
            assert_eq!(line, "PROT P\r\n");
            reader
                .get_mut()
                .write_all(b"200 PROT ok\r\n")
                .await
                .unwrap();
        });

        let mut client = tokio::io::BufReader::new(client);
        negotiate_protected_data_channel(&mut client).await.unwrap();
        server.await.unwrap();
    }

    #[tokio::test]
    async fn protected_data_channel_negotiation_fails_closed_on_prot_rejection() {
        use tokio::io::{AsyncBufReadExt, AsyncWriteExt};

        let (client, server) = tokio::io::duplex(256);
        let server = tokio::spawn(async move {
            let mut reader = tokio::io::BufReader::new(server);
            let mut line = String::new();
            reader.read_line(&mut line).await.unwrap();
            reader
                .get_mut()
                .write_all(b"200 PBSZ ok\r\n")
                .await
                .unwrap();
            line.clear();
            reader.read_line(&mut line).await.unwrap();
            reader
                .get_mut()
                .write_all(b"534 PROT rejected\r\n")
                .await
                .unwrap();
        });

        let mut client = tokio::io::BufReader::new(client);
        let result = negotiate_protected_data_channel(&mut client).await;
        assert!(result.is_err());
        server.await.unwrap();
    }
}