sett 0.4.0

Rust port of sett (data compression, encryption and transfer tool).
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
//! Decrypt workflow

use std::{
    collections::BTreeMap,
    io::{self, Write as _},
    path::{Path, PathBuf},
};

use flate2::read::GzDecoder;
use sequoia_openpgp::{
    parse::{Parse, stream::DecryptorBuilder},
    policy::StandardPolicy,
};
use tracing::{debug, info, instrument};
use walkdir::WalkDir;

use crate::{
    filesystem::{check_space, get_combined_file_size},
    package::{CHECKSUM_FILE, CompressionAlgorithm, DATA_FILE, Package},
    progress::{ProgressDisplay, ProgressReader},
    task::{Mode, Status},
};

const HEAP_BUFFER_SIZE: usize = 1 << 22;

/// Options required by the decrypt workflow
pub struct DecryptOpts<T, F> {
    /// Private OpenPGP key store (used for decrypting data).
    pub key_store: crate::openpgp::keystore::KeyStore,
    /// Public OpenPGP certificate store (used for verifying signatures).
    pub cert_store: crate::openpgp::certstore::CertStore<'static>,
    /// Password for decrypting recipients' keys.
    pub password: F,
    /// Output path for the decrypted data.
    pub output: Option<PathBuf>,
    /// Decrypt data without unpacking it.
    pub decrypt_only: bool,
    /// Run the workflow or only perform a check.
    pub mode: Mode,
    /// Report decryption progress using this callback.
    pub progress: Option<T>,
}

impl<T, F> std::fmt::Debug for DecryptOpts<T, F> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("DecryptOpts")
            .field("output", &self.output.as_ref().map(|p| p.display()))
            .field("decrypt_only", &self.decrypt_only)
            .field("mode", &self.mode)
            .finish()
    }
}

/// Verifies, decrypts, and (optionally) decompresses a data package.
///
/// While decrypting/decompressing signatures and checksums are verified.
#[instrument(err(Debug, level=tracing::Level::ERROR))]
pub async fn decrypt<S, T, F>(
    package: Package<S, crate::package::state::Unverified>,
    opts: DecryptOpts<T, F>,
) -> Result<Status, Error<S::Error>>
where
    S: crate::package::source::PackageStream + std::fmt::Debug + 'static,
    T: ProgressDisplay + Send + 'static,
    F: Fn(crate::openpgp::crypto::PasswordHint) -> super::secret::Secret + Send + 'static,
    <S as crate::package::source::PackageStream>::Error: Send + std::fmt::Debug + 'static,
{
    let package = package.verify(&opts.cert_store).await?;
    decrypt_verified(&package, opts).await
}

pub(crate) async fn decrypt_verified<S, T, F>(
    package: &Package<S, crate::package::state::Verified>,
    mut opts: DecryptOpts<T, F>,
) -> Result<Status, Error<S::Error>>
where
    S: crate::package::source::PackageStream + std::fmt::Debug + 'static,
    <S as crate::package::source::PackageStream>::Error: Send + std::fmt::Debug + 'static,
    T: ProgressDisplay + Send + 'static,
    F: Fn(crate::openpgp::crypto::PasswordHint) -> crate::secret::Secret + Send + 'static,
{
    let output = get_output_path(
        opts.output.map(|p| p.canonicalize()).transpose()?,
        &package.name,
    )?;
    let policy = StandardPolicy::new();
    let metadata = package.metadata().await?;
    let (data_stream, data_size) = package.data().await?;
    check_space(
        data_size,
        output.parent().ok_or(io::Error::new(
            io::ErrorKind::NotFound,
            "destination directory not found",
        ))?,
        opts.mode,
    )?;
    use crate::package::source::IntoAsyncRead as _;
    let data_reader = data_stream.into_async_reader();
    let mut data_reader = tokio_util::io::SyncIoBridge::new(data_reader);
    let status = tokio::task::spawn_blocking(move || -> Result<_, Error<S::Error>> {
        let mut decryptor = DecryptorBuilder::from_reader(&mut data_reader)
            .map_err(crate::openpgp::error::PgpError::from)?
            .with_policy(
                &policy,
                None,
                crate::openpgp::crypto::DecryptionHelper {
                    cert_store: &opts.cert_store,
                    key_store: &mut opts.key_store,
                    password: opts.password,
                },
            )
            .map_err(crate::openpgp::error::PgpError::from)?;
        let status = if let Mode::Check = opts.mode {
            Status::Checked {
                destination: output.to_string_lossy().to_string(),
                source_size: data_size,
            }
        } else {
            std::fs::create_dir_all(&output)?;
            if let Some(pg) = opts.progress {
                let mut progress_reader = ProgressReader::new(decryptor, pg.start(data_size));
                if opts.decrypt_only {
                    write_to_file(&mut progress_reader, &output)?;
                } else {
                    unpack(
                        &mut progress_reader,
                        &output,
                        metadata.compression_algorithm,
                    )?;
                }
            } else if opts.decrypt_only {
                write_to_file(&mut decryptor, &output)?;
            } else {
                unpack(&mut decryptor, &output, metadata.compression_algorithm)?;
            }

            let output_files = WalkDir::new(&output)
                .into_iter()
                .flatten()
                .filter(|entry| entry.file_type().is_file())
                .map(|entry| entry.into_path());

            Status::Completed {
                source_size: data_size,
                destination_size: get_combined_file_size(output_files)?,
                destination: output.to_string_lossy().to_string(),
                metadata,
            }
        };
        Ok(status)
    })
    .await??;
    match &status {
        Status::Checked {
            destination,
            source_size,
        } => {
            debug!(destination, source_size, "Checked decryption task input");
        }
        Status::Completed {
            destination,
            source_size,
            destination_size,
            metadata,
        } => {
            info!(
                destination,
                source_size,
                destination_size,
                metadata = metadata.to_json_or_debug(),
                "Successfully decrypted data package"
            )
        }
    }
    Ok(status)
}

/// Returns output path based on the provided or default path and the package name.
fn get_output_path(
    output: Option<PathBuf>,
    pkg_file_name: &str,
) -> Result<PathBuf, std::io::Error> {
    let base = if let Some(p) = output {
        p
    } else {
        std::env::current_dir()?
    };
    let pkg_base_name = pkg_file_name
        .split('.')
        .next()
        .ok_or_else(|| std::io::Error::other("Package file has no extension"))?;
    let mut output = base.join(pkg_base_name);
    let mut i = 1;
    while output.exists() {
        output = base.join(format!("{pkg_base_name}_{i}"));
        i += 1;
    }
    Ok(output)
}

/// Decompresses source while writing to destination.
#[instrument(skip(source))]
fn unpack<R: io::Read + Send, E: Send + 'static + std::fmt::Debug>(
    source: &mut R,
    output: &Path,
    compression_algorithm: CompressionAlgorithm,
) -> Result<(), Error<E>> {
    match compression_algorithm {
        CompressionAlgorithm::Stored => unpack_tar(&mut tar::Archive::new(source), output),
        CompressionAlgorithm::Gzip(_) => {
            unpack_tar(&mut tar::Archive::new(GzDecoder::new(source)), output)
        }
        CompressionAlgorithm::Zstandard(_) => unpack_tar(
            &mut tar::Archive::new(zstd::stream::read::Decoder::new(source)?),
            output,
        ),
    }?;
    Ok(())
}

/// Returns the destination path for a file extracted from a tar archive.
///
/// It sanitizes the file path to prevent tar bombs and resolves symbolic links.
///
/// Note: the sanitization implementation is taken from the `tar` crate.
fn sanitize_path(dest: &Path, path: &Path) -> Result<PathBuf, std::io::Error> {
    use std::path::Component;
    let mut sanitized = PathBuf::new();

    for part in path.components() {
        match part {
            // Leading '/' characters, root paths, and '.'
            // components are just ignored and treated as "empty
            // components"
            Component::Prefix(_) | Component::RootDir | Component::CurDir => continue,

            // If any part of the filename is '..', then skip over
            // unpacking the file to prevent directory traversal
            // security issues.  See, e.g.: CVE-2001-1267,
            // CVE-2002-0399, CVE-2005-1918, CVE-2007-4131
            Component::ParentDir => {
                Err(std::io::Error::other("file path contains a relative part"))?;
            }

            Component::Normal(part) => sanitized.push(part),
        }
    }
    if sanitized.parent().is_none() {
        return Err(std::io::Error::other("empty file path"));
    }
    Ok(dest.join(&sanitized))
}

enum Message {
    Init(PathBuf),
    Payload(bytes::Bytes),
    Finalize,
}

fn unpack_tar<E: Send + 'static + std::fmt::Debug>(
    archive: &mut tar::Archive<impl io::Read>,
    dest: &Path,
) -> Result<(), Error<E>> {
    let (tx_checksum, rx_checksum) = std::sync::mpsc::sync_channel(8);
    let (tx_write, rx_write) = std::sync::mpsc::sync_channel(8);

    let checksum_handle = std::thread::spawn(move || -> Result<_, Error<E>> {
        use sequoia_openpgp::types::HashAlgorithm::SHA256;
        let mut hasher = SHA256
            .context()
            .map_err(crate::openpgp::error::PgpError::from)?
            .for_digest();
        let mut path = None;
        let mut checksums = BTreeMap::new();
        while let Ok(message) = rx_checksum.recv() {
            match message {
                Message::Init(p) => {
                    path = Some(p);
                }
                Message::Payload(buf) => hasher.update(&buf),
                Message::Finalize => {
                    checksums.insert(
                        std::mem::take(&mut path).expect("path is initialized"),
                        crate::utils::to_hex_string(
                            &std::mem::replace(
                                &mut hasher,
                                SHA256
                                    .context()
                                    .map_err(crate::openpgp::error::PgpError::from)?
                                    .for_digest(),
                            )
                            .into_digest()
                            .map_err(crate::openpgp::error::PgpError::from)?,
                        ),
                    );
                }
            }
        }
        Ok(checksums)
    });

    let write_handle = std::thread::spawn(move || -> io::Result<()> {
        let mut writer = None;
        while let Ok(message) = rx_write.recv() {
            match message {
                Message::Init(p) => {
                    if let Some(parent) = p.parent()
                        && !parent.exists()
                    {
                        std::fs::create_dir_all(parent)?;
                    }
                    writer = Some(io::BufWriter::with_capacity(
                        HEAP_BUFFER_SIZE,
                        std::fs::File::create(&p)?,
                    ));
                }
                Message::Payload(buf) => writer
                    .as_mut()
                    .expect("writer is initialized")
                    .write_all(&buf)?,
                Message::Finalize => {
                    writer = None;
                }
            }
        }
        Ok(())
    });

    let read_result: Result<(), Error<E>> = (|| {
        for entry in archive.entries()? {
            let mut entry = entry?;
            let archive_path = entry.path()?.into_owned();
            let output_path = match sanitize_path(dest, &archive_path) {
                Ok(p) => p,
                Err(e) => {
                    tracing::warn!("{:?}: {}", archive_path, e);
                    continue;
                }
            };
            tx_checksum.send(Message::Init(archive_path))?;
            tx_write.send(Message::Init(output_path))?;
            copy_to_channels(&mut entry, [&tx_checksum, &tx_write])?;
            tx_checksum.send(Message::Finalize)?;
            tx_write.send(Message::Finalize)?;
        }
        Ok(())
    })();
    drop(tx_checksum);
    drop(tx_write);

    let write_result = write_handle.join().map_err(|_| Error::Thread("write"))?;
    let checksum_result = checksum_handle
        .join()
        .map_err(|_| Error::Thread("checksum"))?;
    if let Err(error) = &read_result {
        tracing::error!(?error, "unpacking loop failed");
    }

    // Prioritize writer error as return value over errors from reader and checksum
    write_result.inspect_err(|error| tracing::error!(?error, "writer thread failed"))?;
    let mut checksums =
        checksum_result.inspect_err(|error| tracing::error!(?error, "checksum thread failed"))?;
    read_result?;

    checksums.remove(Path::new(CHECKSUM_FILE));
    verify_checksums(&checksums, &read_checksum_file(dest.join(CHECKSUM_FILE))?)?;
    Ok(())
}

fn copy_to_channels<const N: usize, E>(
    reader: &mut impl io::Read,
    tx: [&std::sync::mpsc::SyncSender<Message>; N],
) -> Result<(), Error<E>> {
    let mut buf = [0; 8192];
    let mut bigbuf = bytes::BytesMut::with_capacity(HEAP_BUFFER_SIZE);

    fn exchange(
        buffer: bytes::BytesMut,
        bigbuf: &mut bytes::BytesMut,
        tx: &[&std::sync::mpsc::SyncSender<Message>],
    ) -> Result<(), std::sync::mpsc::SendError<Message>> {
        let b = std::mem::replace(bigbuf, buffer).freeze();
        for tx in tx {
            tx.send(Message::Payload(b.clone()))?;
        }
        Ok(())
    }
    loop {
        let n = reader.read(&mut buf)?;
        if n == 0 {
            if !bigbuf.is_empty() {
                exchange(bytes::BytesMut::new(), &mut bigbuf, &tx)?;
            }
            break;
        }
        if bigbuf.len() + n > bigbuf.capacity() {
            exchange(
                bytes::BytesMut::with_capacity(HEAP_BUFFER_SIZE),
                &mut bigbuf,
                &tx,
            )?;
        }
        bigbuf.extend_from_slice(&buf[..n]);
    }
    Ok(())
}

/// Writes source to a file.
fn write_to_file<R: io::Read, P: AsRef<Path>>(
    source: &mut R,
    output: P,
) -> Result<(), std::io::Error> {
    let mut f = std::fs::File::create(output.as_ref().join(DATA_FILE))?;
    io::copy(source, &mut f)?;
    Ok(())
}

/// Returns the content of the data package checksum file.
///
/// The checksum file has the following structure:
///
/// ```text
/// <checksum1> <file 1 path inside the data package>
/// <checksum2> <file 2 path inside the data package>
/// ...
/// ```
fn read_checksum_file(path: impl AsRef<Path>) -> Result<BTreeMap<PathBuf, String>, std::io::Error> {
    use std::io::BufRead as _;

    let mut reader = io::BufReader::new(std::fs::File::open(path)?);
    let mut parsed = BTreeMap::new();
    let mut buf = String::new();
    while reader.read_line(&mut buf)? > 0 {
        let (checksum, path) = buf
            .trim()
            .split_once(char::is_whitespace)
            .ok_or_else(|| std::io::Error::other("Unable to parse the checksum file"))?;
        parsed.insert(PathBuf::from(path), checksum.to_string());
        buf.clear();
    }
    Ok(parsed)
}

fn verify_checksums<E>(
    source: &BTreeMap<PathBuf, String>,
    reference: &BTreeMap<PathBuf, String>,
) -> Result<(), Error<E>> {
    for (path, checksum) in source {
        let expected = reference.get(path).ok_or_else(|| {
            std::io::Error::new(
                std::io::ErrorKind::NotFound,
                format!("unable to find checksum for file: {path:?}"),
            )
        })?;
        if !checksum.eq_ignore_ascii_case(expected) {
            Err(ChecksumMismatchError {
                path: path.clone(),
                expected: expected.clone(),
                actual: checksum.clone(),
            })?;
        }
    }
    Ok(())
}

/// Decryption error
#[derive(Debug)]
pub enum Error<E> {
    /// I/O related error
    IO(std::io::Error),
    /// Async send error
    Send(String),
    /// PGP error
    Pgp(crate::openpgp::error::PgpError),
    /// Thread join
    Thread(&'static str),
    /// Checksum mismatch
    ChecksumMismatch(ChecksumMismatchError),
    /// Trouble with the metadata
    Verification(crate::package::error::VerificationError<E>),
    /// Zip reader error
    Zip(crate::zip::error::ReadStreamError<E>),
    /// Async task join
    AsyncTask(tokio::task::JoinError),
}

impl<E> std::fmt::Display for Error<E> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "Decryption failed")?;
        if let Self::Thread(thread) = self {
            write!(f, ": {thread} thread: join error")?;
        }
        Ok(())
    }
}

impl<E: core::error::Error + 'static> core::error::Error for Error<E> {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::IO(source) => Some(source),
            Self::Pgp(source) => Some(source),
            Self::ChecksumMismatch(source) => Some(source),
            Self::Verification(source) => Some(source),
            Self::Zip(source) => Some(source),
            Self::AsyncTask(source) => Some(source),
            Self::Send(_) | Self::Thread(_) => None,
        }
    }
}

impl<E> From<std::io::Error> for Error<E> {
    fn from(value: std::io::Error) -> Self {
        Self::IO(value)
    }
}
impl<E, T> From<std::sync::mpsc::SendError<T>> for Error<E> {
    fn from(value: std::sync::mpsc::SendError<T>) -> Self {
        Self::Send(format!("{value}"))
    }
}
impl<E> From<crate::openpgp::error::PgpError> for Error<E> {
    fn from(value: crate::openpgp::error::PgpError) -> Self {
        Self::Pgp(value)
    }
}
impl<E> From<ChecksumMismatchError> for Error<E> {
    fn from(value: ChecksumMismatchError) -> Self {
        Self::ChecksumMismatch(value)
    }
}
impl<E> From<crate::package::error::MetadataError<E>> for Error<E> {
    fn from(value: crate::package::error::MetadataError<E>) -> Self {
        Self::Verification(value.into())
    }
}
impl<E> From<crate::package::error::VerificationError<E>> for Error<E> {
    fn from(value: crate::package::error::VerificationError<E>) -> Self {
        Self::Verification(value)
    }
}
impl<E> From<crate::zip::error::ReadStreamError<E>> for Error<E> {
    fn from(value: crate::zip::error::ReadStreamError<E>) -> Self {
        Self::Zip(value)
    }
}
impl<E> From<tokio::task::JoinError> for Error<E> {
    fn from(value: tokio::task::JoinError) -> Self {
        Self::AsyncTask(value)
    }
}

/// Checksum mismatch error
#[derive(Debug)]
pub struct ChecksumMismatchError {
    /// Path with a mismatch
    pub path: std::path::PathBuf,
    /// expected checksum
    pub expected: String,
    /// actual checksum
    pub actual: String,
}

impl std::fmt::Display for ChecksumMismatchError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "wrong checksum for {:?} (expected {}, computed {})",
            self.path, self.expected, self.actual
        )
    }
}

impl core::error::Error for ChecksumMismatchError {}