summaryrefslogtreecommitdiffhomepage
path: root/mullvad-update/src/client/verify.rs
blob: 1242f5b6b19bf43f901b412953259c146b195b54 (plain)
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
use anyhow::Context;
use tokio::{
    fs,
    io::{AsyncRead, BufReader},
};

use std::{future::Future, path::Path};

/// A verifier of digital file signatures or hashes
pub trait AppVerifier: 'static + Clone {
    type Parameters;

    /// Verify `bin_path` using `parameters`, and return an error if this fails for any reason.
    fn verify(
        bin_path: impl AsRef<Path>,
        parameters: Self::Parameters,
    ) -> impl Future<Output = anyhow::Result<()>>;
}

/// Checksum verifier that uses SHA256
#[derive(Clone)]
pub struct Sha256Verifier;

impl AppVerifier for Sha256Verifier {
    /// The checksum
    type Parameters = [u8; 32];

    fn verify(
        bin_path: impl AsRef<Path>,
        expected_hash: Self::Parameters,
    ) -> impl Future<Output = anyhow::Result<()>> {
        let bin_path = bin_path.as_ref().to_owned();

        async move {
            let file = fs::File::open(&bin_path)
                .await
                .context(format!("Failed to open file at {}", bin_path.display()))?;
            let file = BufReader::new(file);

            Self::verify_inner(file, expected_hash).await
        }
    }
}

impl Sha256Verifier {
    async fn verify_inner(
        reader: impl AsyncRead + Unpin,
        expected_hash: [u8; 32],
    ) -> anyhow::Result<()> {
        let actual_hash = crate::hash::checksum(reader).await?;

        // Verify that hash is correct
        if expected_hash != actual_hash {
            anyhow::bail!("Invalid checksum for bin file");
        }

        Ok(())
    }
}

#[cfg(test)]
mod test {
    use rand::RngCore;
    use sha2::Digest;
    use std::io::Cursor;

    use super::*;

    #[tokio::test]
    async fn test_sha256_checksum() {
        // Generate some random data
        let mut data = vec![0u8; 1024 * 1024];
        rand::rng().fill_bytes(&mut data);

        // Hash it
        let mut hasher = sha2::Sha256::new();
        hasher.update(&data);
        let expected_hash = hasher.finalize();
        let expected_hash: [u8; 32] = expected_hash[..].try_into().unwrap();

        // Same data should be accepted
        Sha256Verifier::verify_inner(Cursor::new(&data), expected_hash)
            .await
            .expect("expected checksum match");

        // Compare the hash against some random data, which should fail
        rand::rng().fill_bytes(&mut data);
        Sha256Verifier::verify_inner(Cursor::new(&data), expected_hash)
            .await
            .expect_err("expected checksum mismatch");
    }
}