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
use minisign::{PublicKeyBox, SignatureBox};
use pacman_bintrans_common::errors::*;
use pacman_bintrans_common::http::{Client, Proxy};
use sha2::{Digest, Sha256};
use std::fs;
use std::io::Cursor;
use std::process::Stdio;
use tempfile::NamedTempFile;
use tokio::io::AsyncWriteExt;
use tokio::process::Command;
use url::Url;

const REKOR_BIN: &str = "rekor-cli";
const PROOF_SIZE_LIMIT: usize = 1024; // 1K

async fn rekor_verify(
    pubkey: &PublicKeyBox,
    artifact: &[u8],
    signature: &[u8],
    proxy: &Option<Proxy>,
) -> Result<()> {
    rekor_exec(
        pubkey,
        artifact,
        signature,
        proxy,
        "upload",
        &["--format", "json"],
    )
    .await
}

async fn rekor_upload(
    pubkey: &PublicKeyBox,
    artifact: &[u8],
    signature: &[u8],
    proxy: &Option<Proxy>,
) -> Result<()> {
    rekor_exec(pubkey, artifact, signature, proxy, "upload", &[]).await
}

async fn rekor_exec(
    pubkey: &PublicKeyBox,
    artifact: &[u8],
    signature: &[u8],
    proxy: &Option<Proxy>,
    action: &str,
    extra_args: &[&str],
) -> Result<()> {
    let pubkey_file = NamedTempFile::new()?;
    let sig_file = NamedTempFile::new()?;

    let pubkey = pubkey.to_string();
    debug!(
        "Writing to pubkey temp file {:?}: {:?}",
        pubkey_file.path(),
        pubkey
    );
    fs::write(pubkey_file.path(), pubkey)?;
    debug!(
        "Writing to signature temp file {:?}: {:?}",
        sig_file.path(),
        signature
    );
    fs::write(sig_file.path(), signature)?;

    let mut cmd = Command::new(REKOR_BIN);
    cmd.arg(action)
        .arg("--pki-format=minisign")
        .arg("--public-key")
        .arg(pubkey_file.path())
        .arg("--artifact")
        .arg("/dev/stdin")
        .arg("--signature")
        .arg(sig_file.path());

    for arg in extra_args {
        cmd.arg(arg);
    }

    cmd.stdin(Stdio::piped()).stdout(Stdio::piped());

    if let Some(proxy) = proxy {
        let proxy = proxy.as_text();
        debug!("Setting proxy for rekor-cli child process: {:?}", proxy);
        cmd.env("http_proxy", proxy);
        cmd.env("https_proxy", proxy);
    }

    debug!(
        "Executing {:?} {:?}",
        REKOR_BIN,
        cmd.as_std().get_args().collect::<Vec<_>>()
    );
    let mut child = cmd.spawn().context("failed to spawn")?;

    let mut stdin = child
        .stdin
        .take()
        .context("child did not have a handle to stdin")?;

    debug!("Sending to child stdin: {:?}", artifact);
    stdin.write_all(artifact).await?;
    stdin.flush().await?;
    drop(stdin);

    let exit = child.wait_with_output().await?;

    debug!(
        "Child wrote to stdout: {:?}",
        String::from_utf8_lossy(&exit.stdout)
    );
    debug!("Child exited with {:?}", exit.status);

    if exit.status.success() {
        Ok(())
    } else {
        bail!("Sigstore verify failed");
    }
}

pub async fn verify(
    pubkey: &PublicKeyBox,
    artifact: &[u8],
    sig: &[u8],
    proxy: &Option<Proxy>,
) -> Result<()> {
    info!("Calculating sha256sum for {} bytes", artifact.len());
    let mut hasher = Sha256::new();
    hasher.update(artifact);
    let sha256 = hex::encode(hasher.finalize());

    info!("Verifying transparency signature");
    let data_reader = Cursor::new(&sha256);
    let sig_box = SignatureBox::from_string(&String::from_utf8_lossy(sig))?;
    let pk = pubkey.clone().into_public_key()?;
    minisign::verify(&pk, &sig_box, data_reader, true, false, true)?;

    info!("Verifying signature is in transparency log");
    if let Err(err) = rekor_verify(pubkey, sha256.as_bytes(), sig, proxy).await {
        warn!(
            "Verification failed, uploading signature to log next: {:#}",
            err
        );
        rekor_upload(pubkey, sha256.as_bytes(), sig, proxy)
            .await
            .context("Failed to upload signature")?;
        rekor_verify(pubkey, sha256.as_bytes(), sig, proxy)
            .await
            .context("Repeated lookup in transparency log failed")?;
    }

    info!("Success: package verified");
    Ok(())
}

pub async fn fetch_and_verify(
    client: &Client,
    pubkey: &PublicKeyBox,
    url: &Url,
    pkg: &[u8],
    proxy: &Option<Proxy>,
) -> Result<()> {
    let url = format!("{}.t", url.as_str());
    info!("Trying to download transparency proof from {:?}", url);
    let url = url.parse::<Url>()?;

    let proof = client
        .download_to_mem(url.as_str(), Some(PROOF_SIZE_LIMIT))
        .await?;
    debug!("Downloaded {} bytes", proof.len());

    verify(pubkey, pkg, &proof, proxy).await
}