sequoia-sqv 1.5.0

A simple OpenPGP signature verification program
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
//! A simple signature verification program.
//!
//! See <https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=872271> for
//! the motivation.

use std::borrow::Cow;
use std::io::Read;
use std::io;
use std::path::PathBuf;
use std::process::exit;
use std::sync::OnceLock;

use chrono::{DateTime, offset::Utc};
use anyhow::Context;

use clap::FromArgMatches;

use sequoia_openpgp as openpgp;

use crate::openpgp::{
    Cert,
    KeyHandle,
    Packet,
    Result,
    parse::PacketParser,
    parse::PacketParserResult,
    parse::Parse,
};
use crate::openpgp::parse::stream::{
    DetachedVerifierBuilder,
    MessageLayer,
    MessageStructure,
    VerificationHelper,
    GoodChecksum,
    VerificationError,
    VerifierBuilder,
};
use crate::openpgp::cert::prelude::*;

mod cli;

/// Returns stdin.
///
/// It is standard to `-` to mean read from `stdin` instead of a file.
/// Because it is almost always an error to use `-` more than once,
/// this errors out with a helpful error message in this case.
fn stdin(option: &'static str) -> Result<std::io::Stdin> {
    static OPTION: OnceLock<&'static str> = OnceLock::new();

    match OPTION.set(option) {
        Ok(()) => {
            Ok(std::io::stdin())
        }
        Err(_) => {
            let previous = OPTION.get().unwrap();
            Err(anyhow::anyhow!(
                "Can't use - to read from stdin multiple times \
                 (used with {} and {})",
                previous, option))
        }
    }
}

struct VHelper {
    not_before: Option<std::time::SystemTime>,
    not_after: std::time::SystemTime,

    good: usize,
    total: usize,
    threshold: usize,

    keyrings: Vec<PathBuf>,
}

impl std::fmt::Debug for VHelper {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        f.debug_struct("VHelper")
            .field("not_before", &self.not_before)
            .field("not_after", &self.not_after)
            .field("good", &self.good)
            .field("total", &self.total)
            .field("threshold", &self.threshold)
            .field("keyrings", &self.keyrings)
            .finish()
    }
}

impl VHelper {
    fn new(threshold: usize,
           not_before: Option<std::time::SystemTime>,
           not_after: std::time::SystemTime,
           keyrings: Vec<PathBuf>) -> Self {
        VHelper {
            not_before: not_before,
            not_after: not_after,
            good: 0,
            total: 0,
            threshold: threshold,
            keyrings: keyrings,
        }
    }
}

impl VerificationHelper for VHelper {
    fn get_certs(&mut self, ids: &[crate::KeyHandle]) -> Result<Vec<Cert>> {
        let mut certs = Vec::with_capacity(ids.len());

        // Load relevant keys from the keyring.
        for filename in self.keyrings.iter() {
            let parser = if filename.as_os_str().as_encoded_bytes() == b"-" {
                CertParser::from_reader(stdin("--keyring")?)
            } else {
                CertParser::from_file(filename)
            };
            let parser = parser
                .map_err(|err| {
                    let Ok(mut file) = std::fs::File::open(filename) else {
                        return err;
                    };
                    let mut buffer = [0; 0x0C];
                    let Ok(()) = file.read_exact(&mut buffer) else {
                        return err;
                    };
                    if buffer.ends_with(b"KBXf") {
                        anyhow::anyhow!("File appears to be in keybox format, \
                                         which is not supported")
                    } else {
                        err
                    }
                })
                .with_context(|| format!("Failed to parse keyring {:?}",
                                         filename))?;
            for cert in parser
                .unvalidated_cert_filter(|cert, _| {
                    // We don't skip keys that are valid (not revoked,
                    // alive, etc.) so that
                    cert.keys().key_handles(ids.iter()).next().is_some()
                })
            {
                certs.push(cert.with_context(|| {
                    format!("Malformed certificate in keyring {:?}", filename)
                })?);
            }
        }

        // Dedup.  To avoid cloning the certificates, we don't use
        // Vec::dedup.
        certs.sort_by(|a, b| a.fingerprint().cmp(&b.fingerprint()));
        let count = certs.len();
        let (certs, errs) = certs.into_iter().fold(
            (Vec::with_capacity(count), Vec::new()),
            |(mut certs, mut errs), a| {
                if certs.is_empty() {
                    certs.push(a);
                } else if certs[certs.len() - 1].fingerprint() == a.fingerprint() {
                    // Merge `a` into the last element.
                    match certs.pop().expect("non-empty vec").merge_public(a) {
                        Ok(cert) => certs.push(cert),
                        Err(err) => errs.push(err),
                    }
                } else {
                    certs.push(a);
                }

                (certs, errs)
            });

        if !errs.is_empty() {
            eprintln!("Error merging duplicate keys:");
            for err in errs.iter() {
                eprintln!("  {}", err);
            }
            Err(errs.into_iter().next().expect("non-empty vec"))
        } else {
            Ok(certs)
        }
    }

    fn check(&mut self, structure: MessageStructure) -> Result<()> {
        use self::VerificationError::*;

        let mut signers = Vec::with_capacity(2);
        let mut verification_err = None;

        for layer in structure.into_iter() {
            match layer {
                MessageLayer::SignatureGroup { results } =>
                    for result in results {
                        self.total += 1;
                        match result {
                            Ok(GoodChecksum { sig, ka, .. }) => {
                                match (sig.signature_creation_time(),
                                                self.not_before,
                                                self.not_after)
                                {
                                    (None, _, _) => {
                                        eprintln!("Malformed signature:");
                                        print_error_chain(&anyhow::anyhow!(
                                            "no signature creation time"));
                                    },
                                    (Some(t), Some(not_before), not_after) => {
                                        if t < not_before {
                                            eprintln!(
                                                "Signature by {:X} was created before \
                                                 the --not-before date.",
                                                ka.key().fingerprint());
                                        } else if t > not_after {
                                            eprintln!(
                                                "Signature by {:X} was created after \
                                                 the --not-after date.",
                                                ka.key().fingerprint());
                                        } else {
                                            signers.push(ka.cert().fingerprint());
                                        }
                                    }
                                    (Some(t), None, not_after) => {
                                        if t > not_after {
                                            eprintln!(
                                                "Signature by {:X} was created after \
                                                 the --not-after date.",
                                                ka.key().fingerprint());
                                        } else {
                                            signers.push(ka.cert().fingerprint());
                                        }
                                    }
                                };
                            }
                            Err(MalformedSignature { error, .. }) => {
                                eprintln!("Signature is malformed:");
                                print_error_chain(&error);
                            }
                            Err(MissingKey { sig, .. }) => {
                                let issuers = sig.get_issuers();
                                eprintln!("Missing key {}, which is needed to \
                                           verify signature.",
                                          issuers
                                              .first()
                                              .map(|issuer| {
                                                  Cow::Owned(issuer.to_string())
                                              })
                                              .unwrap_or_else(|| {
                                                  Cow::Borrowed("<unknown issuer>")
                                              }));
                            }
                            Err(UnboundKey { cert, error, .. }) => {
                                eprintln!("Signing key on {:X} is not bound:",
                                          cert.fingerprint());
                                print_error_chain(&error);
                            }
                            Err(BadKey { ka, error, .. }) => {
                                eprintln!("Signing key on {:X} is bad:",
                                          ka.cert().fingerprint());
                                print_error_chain(&error);
                            }
                            Err(BadSignature { error, .. }) => {
                                eprintln!("Verifying signature:");
                                print_error_chain(&error);
                                if verification_err.is_none() {
                                    verification_err = Some(error)
                                }
                            }

                            Err(UnknownSignature { sig, .. }) => {
                                eprintln!("Verifying signature:");
                                print_error_chain(sig.error());
                                if verification_err.is_none() {
                                    verification_err =
                                        Some(anyhow::anyhow!("{}", sig.error()));
                                }
                            }

                            Err(e) => {
                                eprintln!("Verifying signature: {}", e);
                                if verification_err.is_none() {
                                    verification_err =
                                        Some(anyhow::anyhow!("{}", e));
                                }
                            }
                        }
                    }
                MessageLayer::Compression { .. } => (),
                _ => unreachable!(),
            }
        }

        // Dedup the keys so that it is not possible to exceed the
        // threshold by duplicating signatures or by using the same
        // key.
        signers.sort();
        signers.dedup();

        self.good = signers.len();
        for signer in signers {
            println!("{:X}", signer);
        }

        Ok(())
    }
}

fn print_error_chain(err: &anyhow::Error) {
    eprintln!("           {}", err);
    err.chain().skip(1).for_each(|cause| eprintln!("  because: {}", cause));
}


fn main() -> Result<()> {
    let matches = cli::build().get_matches();
    let cli = cli::SqvCommand::from_arg_matches(&matches)?;

    if cli.data.is_none() && cli.detached.is_none()
        && ! cli.message && ! cli.cleartext
    {
        return Err(anyhow::anyhow!(
            "Either `--signature-file`, `--message`, or `--cleartext` \
             must be given."));
    }

    let verbose = cli.verbose;

    let good_threshold = cli.signatures;
    if good_threshold < 1 {
        eprintln!("Value passed to --signatures must be >= 1 (got: {:?}).",
                  good_threshold);
        exit(2);
    }

    // Parse the --time argument to get the session reference time.
    // If not given, defaults to the current time.
    let ref_time = cli.time.as_ref()
        .map(|at| at.to_system_time(std::time::SystemTime::now()))
        .transpose()
        .with_context(|| format!("Error parsing --time: {}",
                                 cli.time.as_ref().unwrap()))?
        .unwrap_or_else(std::time::SystemTime::now);

    let not_before: Option<std::time::SystemTime> =
        if let Some(t) = cli.not_before {
            Some(parse_iso8601(&t, chrono::NaiveTime::from_hms_opt(0, 0, 0).unwrap())
                 .context(format!("Bad value passed to --not-before: {:?}", t))?
                 .into())
        } else {
            None
        };
    let not_after: std::time::SystemTime =
        if let Some(t) = cli.not_after {
            Some(parse_iso8601(&t, chrono::NaiveTime::from_hms_opt(23, 59, 59).unwrap())
                 .context(format!("Bad value passed to --not-after: {:?}", t))?
                 .into())
        } else {
            None
        }.unwrap_or(ref_time);

    let h = VHelper::new(good_threshold, not_before, not_after, cli.keyring);

    let t = cli.policy_as_of.as_ref()
        .map(|at| at.to_system_time(ref_time))
        .transpose()
        .with_context(|| format!("Error parsing --policy-as-of: {}",
                                 cli.policy_as_of.unwrap()))?
        .unwrap_or(ref_time);

    let mut p = sequoia_policy_config::ConfiguredStandardPolicy::at(t);
    p.parse_default_config()?;
    let p = p.build();

    // Dispatch.
    let h = if cli.data.is_some() || cli.detached.is_some() {
        let sig = cli.detached.as_ref().unwrap_or(&cli.file);
        let data = cli.data.as_ref().unwrap_or(&cli.file);

        let v = if sig.as_os_str().as_encoded_bytes() == b"-" {
            DetachedVerifierBuilder::from_reader(stdin("--signature-file")?)?
        } else {
            DetachedVerifierBuilder::from_file(sig)?
        };
        let mut v = v.with_policy(&p, Some(ref_time), h)
            .map_err(|err| {
                // Check if the user reversed the arguments.
                if data.as_os_str().as_encoded_bytes() != b"-" {
                    if let Ok(ppr) = PacketParser::from_file(data) {
                        if let PacketParserResult::Some(pp) = ppr {
                            if let Packet::Signature(_) = pp.packet {
                                eprintln!(
                                    "Hint: Data file ({}) appears to contain \
                                     an OpenPGP signature, perhaps you \
                                     reversed the arguments.",
                                    data.display());
                            }
                        }
                    }
                }
                err
            })?;
        if data.as_os_str().as_encoded_bytes() == b"-" {
            v.verify_reader(stdin("FILE")?)?;
        } else {
            v.verify_file(data)?;
        }
        v.into_helper()
    } else {
        assert!(cli.message || cli.cleartext);
        let mut sink: Box<dyn io::Write> = if let Some(n) = &cli.output {
            let mut overwrite = cli.overwrite;

            // Imply --overwrite if writing to a special file.
            #[cfg(unix)]
            if ! overwrite {
                use std::os::unix::fs::FileTypeExt;

                if let Ok(metadata) = std::fs::metadata(n) {
                    let ft = metadata.file_type();
                    if ft.is_char_device()
                        || ft.is_block_device()
                        || ft.is_fifo()
                        || ft.is_socket()
                    {
                        overwrite = true;
                    }
                }
            }

            Box::new(std::fs::File::options()
                .write(true)
                .create(overwrite) // choose create() when allowing overwrite
                .create_new(!overwrite) // else insist on new file
                .open(n)?)
        } else {
            Box::new(io::stdout())
        };

        let v = if cli.file.as_os_str().as_encoded_bytes() == b"-" {
            VerifierBuilder::from_reader(stdin("FILE")?)?
        } else {
            VerifierBuilder::from_file(&cli.file)?
        };
        let mut v = v.with_policy(&p, Some(ref_time), h)?;
        io::copy(&mut v, &mut sink)?;
        v.into_helper()
    };

    if verbose {
        eprintln!("{} of {} signatures are valid (threshold is: {}).",
                  h.good, h.total, good_threshold);
    }

    exit(if h.good >= good_threshold { 0 } else { 1 });
}

/// Parses the given string depicting a ISO 8601 timestamp.
fn parse_iso8601(s: &str, pad_date_with: chrono::NaiveTime)
                 -> Result<DateTime<Utc>>
{
    // If you modify this function this function, synchronize the
    // changes with the copy in sqv.rs!
    for f in &[
        "%Y-%m-%dT%H:%M:%S%#z",
        "%Y-%m-%dT%H:%M:%S",
        "%Y-%m-%dT%H:%M%#z",
        "%Y-%m-%dT%H:%M",
        "%Y-%m-%dT%H%#z",
        "%Y-%m-%dT%H",
        "%Y%m%dT%H%M%S%#z",
        "%Y%m%dT%H%M%S",
        "%Y%m%dT%H%M%#z",
        "%Y%m%dT%H%M",
        "%Y%m%dT%H%#z",
        "%Y%m%dT%H",
    ] {
        if f.ends_with("%#z") {
            if let Ok(d) = DateTime::parse_from_str(s, *f) {
                return Ok(d.into());
            }
        } else {
            if let Ok(d) = chrono::NaiveDateTime::parse_from_str(s, *f) {
                return Ok(DateTime::from_naive_utc_and_offset(d, Utc));
            }
        }
    }
    for f in &[
        "%Y-%m-%d",
        "%Y-%m",
        "%Y-%j",
        "%Y%m%d",
        "%Y%m",
        "%Y%j",
        "%Y",
    ] {
        if let Ok(d) = chrono::NaiveDate::parse_from_str(s, *f) {
            return Ok(DateTime::from_naive_utc_and_offset(d.and_time(pad_date_with), Utc));
        }
    }
    Err(anyhow::anyhow!("Malformed ISO8601 timestamp: {}", s))
}

#[test]
fn test_parse_iso8601() {
    let z = chrono::NaiveTime::from_hms_opt(0, 0, 0).unwrap();
    parse_iso8601("2017-03-04T13:25:35Z", z).unwrap();
    parse_iso8601("2017-03-04T13:25:35+08:30", z).unwrap();
    parse_iso8601("2017-03-04T13:25:35", z).unwrap();
    parse_iso8601("2017-03-04T13:25Z", z).unwrap();
    parse_iso8601("2017-03-04T13:25", z).unwrap();
    // parse_iso8601("2017-03-04T13Z", z).unwrap(); // XXX: chrono doesn't like
    // parse_iso8601("2017-03-04T13", z).unwrap(); // ditto
    parse_iso8601("2017-03-04", z).unwrap();
    // parse_iso8601("2017-03", z).unwrap(); // ditto
    parse_iso8601("2017-031", z).unwrap();
    parse_iso8601("20170304T132535Z", z).unwrap();
    parse_iso8601("20170304T132535+0830", z).unwrap();
    parse_iso8601("20170304T132535", z).unwrap();
    parse_iso8601("20170304T1325Z", z).unwrap();
    parse_iso8601("20170304T1325", z).unwrap();
    // parse_iso8601("20170304T13Z", z).unwrap(); // ditto
    // parse_iso8601("20170304T13", z).unwrap(); // ditto
    parse_iso8601("20170304", z).unwrap();
    // parse_iso8601("201703", z).unwrap(); // ditto
    parse_iso8601("2017031", z).unwrap();
    // parse_iso8601("2017", z).unwrap(); // ditto
}