sealwax-cli 0.1.0

Command-line tool of sealwax: inspect documents, survey a corpus.
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
//! `sealwax sign`: one signature appended to a document, its value made by an outside command,
//! as an HSM makes it: the command reads the digest to sign in hexadecimal on its standard input
//! and writes the signature value in hexadecimal on its standard output. A timestamp, if asked
//! for, comes from another command: the request (DER) on its standard input, the response (DER)
//! on its standard output.

use std::io::Write as _;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};

use clap::ValueEnum;
use sealwax_sign::{
    Algorithm, Integrity, Kind, Options, Policy, RevocationFetcher, Signer, TimestampClient, Trust,
    Validation,
};

/// How the signer signs, by its JOSE name (RFC 7518), as cloud key vaults name it.
#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
pub(crate) enum KeyAlgorithm {
    /// RSA, PKCS #1 v1.5 with SHA-256.
    #[value(alias = "rsa")]
    Rs256,
    /// RSA, PKCS #1 v1.5 with SHA-384.
    Rs384,
    /// RSA, PKCS #1 v1.5 with SHA-512.
    Rs512,
    /// RSA-PSS with SHA-256.
    Ps256,
    /// RSA-PSS with SHA-384.
    Ps384,
    /// RSA-PSS with SHA-512.
    Ps512,
    /// ECDSA with SHA-256.
    #[value(alias = "ecdsa")]
    Es256,
    /// ECDSA with SHA-384.
    Es384,
    /// ECDSA with SHA-512.
    Es512,
}

impl KeyAlgorithm {
    fn algorithm(self) -> Algorithm {
        match self {
            Self::Rs256 => Algorithm::RsaPkcs1Sha256,
            Self::Rs384 => Algorithm::RsaPkcs1Sha384,
            Self::Rs512 => Algorithm::RsaPkcs1Sha512,
            Self::Ps256 => Algorithm::RsaPssSha256,
            Self::Ps384 => Algorithm::RsaPssSha384,
            Self::Ps512 => Algorithm::RsaPssSha512,
            Self::Es256 => Algorithm::EcdsaSha256,
            Self::Es384 => Algorithm::EcdsaSha384,
            Self::Es512 => Algorithm::EcdsaSha512,
        }
    }
}

/// What to sign and how.
#[derive(Debug, clap::Args)]
pub(crate) struct Arguments {
    /// The PDF to sign.
    pub(crate) input: PathBuf,
    /// The signed PDF.
    #[arg(long)]
    pub(crate) out: PathBuf,
    /// The signer's certificate, PEM or DER.
    #[arg(long)]
    pub(crate) cert: PathBuf,
    /// The rest of its chain, PEM or DER: intermediates, the root if wanted.
    #[arg(long)]
    pub(crate) chain: Vec<PathBuf>,
    /// How the signer signs; the digest it gets is that of the algorithm (32, 48 or 64 bytes).
    #[arg(long, value_enum, default_value = "rs256")]
    pub(crate) algorithm: KeyAlgorithm,
    /// The name of the new signature field.
    #[arg(long)]
    pub(crate) field: String,
    /// Certify the document (the first signature) rather than approve it.
    #[arg(long)]
    pub(crate) certify: bool,
    #[arg(long)]
    pub(crate) reason: Option<String>,
    #[arg(long)]
    pub(crate) location: Option<String>,
    #[arg(long)]
    pub(crate) contact: Option<String>,
    /// The signing time `/M` declares, in seconds since 1970 (UTC); now by default.
    #[arg(long)]
    pub(crate) time: Option<i64>,
    /// The timestamp authority's command.
    #[arg(long)]
    pub(crate) tsa: Option<String>,
    /// An argument of the TSA command (repeated).
    #[arg(long = "tsa-arg", requires = "tsa", allow_hyphen_values = true)]
    pub(crate) tsa_args: Vec<String>,
    /// A root the authority must chain to, PEM or DER (repeated).
    #[arg(long = "tsa-root", requires = "tsa")]
    pub(crate) tsa_roots: Vec<PathBuf>,
    /// An intermediate of the authority its tokens may leave out, PEM or DER (repeated).
    #[arg(long = "tsa-chain", requires = "tsa")]
    pub(crate) tsa_chain: Vec<PathBuf>,
    /// Sign without the timestamp when it fails, rather than not sign.
    #[arg(long = "tsa-best-effort", requires = "tsa")]
    pub(crate) tsa_best_effort: bool,
    /// Sign even when a signature already in the document is not intact.
    #[arg(long)]
    pub(crate) allow_broken_signatures: bool,
    /// The signer command and its arguments.
    #[arg(last = true, required = true)]
    pub(crate) signer: Vec<String>,
}

/// Where to add a document timestamp, and whom to trust.
#[derive(Debug, clap::Args)]
pub(crate) struct TimestampArguments {
    /// The PDF to timestamp.
    pub(crate) input: PathBuf,
    /// The timestamped PDF.
    #[arg(long)]
    pub(crate) out: PathBuf,
    /// The name of the new field.
    #[arg(long, default_value = "DocTimeStamp1")]
    pub(crate) field: String,
    /// The timestamp authority's command.
    #[arg(long)]
    pub(crate) tsa: String,
    /// An argument of the TSA command (repeated).
    #[arg(long = "tsa-arg", allow_hyphen_values = true)]
    pub(crate) tsa_args: Vec<String>,
    /// A root the authority must chain to, PEM or DER (repeated).
    #[arg(long = "tsa-root", required = true)]
    pub(crate) tsa_roots: Vec<PathBuf>,
    /// An intermediate of the authority its tokens may leave out, PEM or DER (repeated).
    #[arg(long = "tsa-chain")]
    pub(crate) tsa_chain: Vec<PathBuf>,
}

/// Which signature to add validation data to, and whom to trust.
#[derive(Debug, clap::Args)]
pub(crate) struct LtvArguments {
    /// The signed PDF.
    pub(crate) input: PathBuf,
    /// The PDF with the validation data.
    #[arg(long)]
    pub(crate) out: PathBuf,
    /// The signature's field.
    #[arg(long)]
    pub(crate) field: String,
    /// An anchor, the signer's or its timestamp authority's, PEM or DER (repeated).
    #[arg(long = "root", required = true)]
    pub(crate) roots: Vec<PathBuf>,
    /// An intermediate the signatures may leave out, PEM or DER (repeated).
    #[arg(long)]
    pub(crate) chain: Vec<PathBuf>,
    /// The fetch command.
    #[arg(long)]
    pub(crate) fetch: String,
    /// An argument of the fetch command (repeated), before `ocsp URL` or `crl URL`.
    #[arg(long = "fetch-arg", allow_hyphen_values = true)]
    pub(crate) fetch_args: Vec<String>,
    /// The time the data must be current at, in seconds since 1970 (UTC); now by default.
    #[arg(long)]
    pub(crate) time: Option<i64>,
}

/// A certificate file, PEM or DER, as DER.
fn certificate(path: &Path) -> Result<Vec<u8>, String> {
    let data = std::fs::read(path).map_err(|e| format!("{}: {e}", path.display()))?;
    if !data.starts_with(b"-----BEGIN") {
        return Ok(data);
    }
    let text = String::from_utf8_lossy(&data);
    let body: String = text
        .lines()
        .filter(|line| !line.starts_with("-----"))
        .collect::<String>()
        .split_whitespace()
        .collect();
    decode_base64(&body).ok_or_else(|| format!("{}: malformed PEM", path.display()))
}

fn decode_base64(text: &str) -> Option<Vec<u8>> {
    const ALPHABET: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    let mut out = Vec::with_capacity(text.len() * 3 / 4);
    let mut buffer = 0u32;
    let mut bits = 0;
    for byte in text.bytes() {
        if byte == b'=' {
            break;
        }
        let value = ALPHABET.iter().position(|c| *c == byte)?;
        buffer = (buffer << 6) | u32::try_from(value).ok()?;
        bits += 6;
        if bits >= 8 {
            bits -= 8;
            out.push(u8::try_from((buffer >> bits) & 0xff).ok()?);
        }
    }
    Some(out)
}

fn hex(bytes: &[u8]) -> String {
    bytes.iter().map(|b| format!("{b:02x}")).collect()
}

fn unhex(text: &str) -> Option<Vec<u8>> {
    let text = text.trim();
    if !text.len().is_multiple_of(2) {
        return None;
    }
    (0..text.len())
        .step_by(2)
        .map(|i| u8::from_str_radix(text.get(i..i + 2)?, 16).ok())
        .collect()
}

/// `input` written to the standard input of `command`, its standard output read back.
fn run_command(command: &[String], what: &str, input: &[u8]) -> Result<Vec<u8>, String> {
    let (program, arguments) = command
        .split_first()
        .ok_or_else(|| format!("no {what} command"))?;
    let mut child = Command::new(program)
        .args(arguments)
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .spawn()
        .map_err(|e| format!("{what} command: {e}"))?;
    child
        .stdin
        .take()
        .ok_or_else(|| format!("{what} command: no input"))?
        .write_all(input)
        .map_err(|e| format!("{what} command: {e}"))?;
    let output = child
        .wait_with_output()
        .map_err(|e| format!("{what} command: {e}"))?;
    if !output.status.success() {
        return Err(format!("{what} command failed: {}", output.status));
    }
    Ok(output.stdout)
}

/// A timestamp authority reached through a command.
struct ExternalAuthority<'a> {
    command: &'a [String],
}

impl TimestampClient for ExternalAuthority<'_> {
    fn timestamp(&self, request: &[u8]) -> Result<Vec<u8>, String> {
        run_command(self.command, "TSA", request)
    }
}

/// Revocation information fetched through a command.
struct ExternalFetcher<'a> {
    command: &'a [String],
}

impl ExternalFetcher<'_> {
    fn fetch(&self, kind: &str, url: &str, input: &[u8]) -> Result<Vec<u8>, String> {
        let command: Vec<String> = self
            .command
            .iter()
            .cloned()
            .chain([kind.to_owned(), url.to_owned()])
            .collect();
        run_command(&command, "fetch", input)
    }
}

impl RevocationFetcher for ExternalFetcher<'_> {
    fn ocsp(&self, url: &str, request: &[u8]) -> Result<Vec<u8>, String> {
        self.fetch("ocsp", url, request)
    }

    fn crl(&self, url: &str) -> Result<Vec<u8>, String> {
        self.fetch("crl", url, &[])
    }
}

/// A signer that runs a command for each digest.
struct External<'a> {
    certificate: Vec<u8>,
    chain: Vec<Vec<u8>>,
    algorithm: Algorithm,
    command: &'a [String],
}

impl Signer for External<'_> {
    fn certificate(&self) -> &[u8] {
        &self.certificate
    }

    fn chain(&self) -> &[Vec<u8>] {
        &self.chain
    }

    fn algorithm(&self) -> Algorithm {
        self.algorithm
    }

    fn sign(&self, _message: &[u8], digest: &[u8]) -> Result<Vec<u8>, String> {
        let output = run_command(self.command, "signer", hex(digest).as_bytes())?;
        unhex(&String::from_utf8_lossy(&output))
            .ok_or_else(|| "signer command wrote no hexadecimal signature".to_owned())
    }
}

pub(crate) fn run(arguments: &Arguments) -> Result<(), String> {
    let pdf = std::fs::read(&arguments.input)
        .map_err(|e| format!("{}: {e}", arguments.input.display()))?;
    let read_all = |paths: &[PathBuf]| -> Result<Vec<Vec<u8>>, String> {
        paths.iter().map(|path| certificate(path)).collect()
    };
    let signer = External {
        certificate: certificate(&arguments.cert)?,
        chain: read_all(&arguments.chain)?,
        algorithm: arguments.algorithm.algorithm(),
        command: &arguments.signer,
    };
    let mut options = Options::new(
        arguments.field.as_str(),
        if arguments.certify {
            Kind::certification()
        } else {
            Kind::Approval
        },
    );
    options.reason.clone_from(&arguments.reason);
    options.location.clone_from(&arguments.location);
    options.contact_info.clone_from(&arguments.contact);
    options.signing_time = arguments.time;
    options.allow_broken_signatures = arguments.allow_broken_signatures;
    let write = |pdf: &[u8]| {
        std::fs::write(&arguments.out, pdf).map_err(|e| format!("{}: {e}", arguments.out.display()))
    };
    let Some(program) = &arguments.tsa else {
        let signed =
            sealwax_sign::sign(&pdf, &signer, &options, None).map_err(|e| e.to_string())?;
        return write(&signed.pdf);
    };
    let trust = Trust::new(
        &read_all(&arguments.tsa_roots)?,
        &read_all(&arguments.tsa_chain)?,
    )
    .map_err(|e| e.to_string())?;
    let command: Vec<String> = std::iter::once(program.clone())
        .chain(arguments.tsa_args.iter().cloned())
        .collect();
    let authority = ExternalAuthority { command: &command };
    let timestamping = sealwax_sign::Timestamping {
        client: &authority,
        trust: &trust,
        policy: if arguments.tsa_best_effort {
            Policy::BestEffort
        } else {
            Policy::Required
        },
    };
    let signed = sealwax_sign::sign(&pdf, &signer, &options, Some(&timestamping))
        .map_err(|e| e.to_string())?;
    write(&signed.pdf)?;
    let report = match (&signed.timestamp, &signed.timestamp_error) {
        (Some(token), _) => serde_json::json!({
            "timestamp": {"time": token.time, "authority": token.authority},
        }),
        (None, Some(error)) => serde_json::json!({"timestamp_error": error.to_string()}),
        (None, None) => serde_json::json!({}),
    };
    println!("{report}");
    Ok(())
}

pub(crate) fn run_ltv(arguments: &LtvArguments) -> Result<(), String> {
    let pdf = std::fs::read(&arguments.input)
        .map_err(|e| format!("{}: {e}", arguments.input.display()))?;
    let read_all = |paths: &[PathBuf]| -> Result<Vec<Vec<u8>>, String> {
        paths.iter().map(|path| certificate(path)).collect()
    };
    let trust = Trust::new(&read_all(&arguments.roots)?, &read_all(&arguments.chain)?)
        .map_err(|e| e.to_string())?;
    let command: Vec<String> = std::iter::once(arguments.fetch.clone())
        .chain(arguments.fetch_args.iter().cloned())
        .collect();
    let fetcher = ExternalFetcher { command: &command };
    let validation = Validation {
        fetcher: &fetcher,
        trust: &trust,
        time: arguments.time,
    };
    let (data, coverage) = sealwax_sign::add_validation_data(&pdf, &arguments.field, &validation)
        .map_err(|e| e.to_string())?;
    std::fs::write(&arguments.out, data)
        .map_err(|e| format!("{}: {e}", arguments.out.display()))?;
    println!(
        "{}",
        serde_json::json!({
            "certificates": coverage.certificates,
            "ocsp_responses": coverage.ocsp_responses,
            "crls": coverage.crls,
        })
    );
    Ok(())
}

pub(crate) fn run_timestamp(arguments: &TimestampArguments) -> Result<(), String> {
    let pdf = std::fs::read(&arguments.input)
        .map_err(|e| format!("{}: {e}", arguments.input.display()))?;
    let read_all = |paths: &[PathBuf]| -> Result<Vec<Vec<u8>>, String> {
        paths.iter().map(|path| certificate(path)).collect()
    };
    let trust = Trust::new(
        &read_all(&arguments.tsa_roots)?,
        &read_all(&arguments.tsa_chain)?,
    )
    .map_err(|e| e.to_string())?;
    let command: Vec<String> = std::iter::once(arguments.tsa.clone())
        .chain(arguments.tsa_args.iter().cloned())
        .collect();
    let authority = ExternalAuthority { command: &command };
    let (data, timestamp) =
        sealwax_sign::add_document_timestamp(&pdf, &arguments.field, &authority, &trust)
            .map_err(|e| e.to_string())?;
    std::fs::write(&arguments.out, data)
        .map_err(|e| format!("{}: {e}", arguments.out.display()))?;
    println!(
        "{}",
        serde_json::json!({"time": timestamp.time, "authority": timestamp.authority})
    );
    Ok(())
}

/// Print the check of each signature of `files`, one JSON line each; whether all are intact.
pub(crate) fn run_verify(files: &[PathBuf]) -> Result<bool, String> {
    let mut all_intact = true;
    for file in files {
        // A file that cannot be read or opened is reported, and the others checked.
        let read = std::fs::read(file)
            .map_err(|e| e.to_string())
            .and_then(|pdf| sealwax_sign::verify_signatures(&pdf).map_err(|e| e.to_string()));
        let checks = match read {
            Ok(checks) => checks,
            Err(error) => {
                all_intact = false;
                println!(
                    "{}",
                    serde_json::json!({"file": file.display().to_string(), "error": error})
                );
                continue;
            }
        };
        for check in checks {
            let (integrity, detail) = match &check.integrity {
                Integrity::Intact => ("intact", None),
                Integrity::Broken(problem) => ("broken", Some(problem.to_string())),
                Integrity::Unchecked(why) => ("unchecked", Some((*why).to_owned())),
                _ => ("unknown", None),
            };
            all_intact &= check.is_intact();
            println!(
                "{}",
                serde_json::json!({
                    "file": file.display().to_string(),
                    "field": check.field,
                    "kind": format!("{:?}", check.kind),
                    "sub_filter": check.sub_filter,
                    "signer": check.signer,
                    "timestamp_time": check.timestamp_time,
                    "later_revisions": check.later_revisions,
                    "integrity": integrity,
                    "detail": detail,
                })
            );
        }
    }
    Ok(all_intact)
}

#[cfg(test)]
mod tests {
    #![allow(clippy::unwrap_used)]

    use super::{decode_base64, unhex};

    #[test]
    fn base64_and_hexadecimal() {
        assert_eq!(decode_base64("TWFu").unwrap(), b"Man");
        assert_eq!(decode_base64("TWE=").unwrap(), b"Ma");
        assert_eq!(unhex("0aFF\n").unwrap(), [10, 255]);
        assert_eq!(unhex("abc"), None);
    }
}