sequoia-sq 0.23.0

Command-line frontends for Sequoia
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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
use anyhow::Context as _;
use clap::ArgMatches;
use itertools::Itertools;
use std::time::{SystemTime, Duration};

use crate::openpgp::KeyHandle;
use crate::openpgp::Packet;
use crate::openpgp::Result;
use crate::openpgp::armor::{Writer, Kind};
use crate::openpgp::cert::prelude::*;
use crate::openpgp::packet::prelude::*;
use crate::openpgp::packet::signature::subpacket::SubpacketTag;
use crate::openpgp::parse::Parse;
use crate::openpgp::policy::Policy;
use crate::openpgp::serialize::Serialize;
use crate::openpgp::types::KeyFlags;
use crate::openpgp::types::SignatureType;

use crate::{
    open_or_stdin,
};
use crate::Config;
use crate::create_or_stdout;
use crate::SECONDS_IN_YEAR;
use crate::parse_duration;
use crate::decrypt_key;

pub fn generate(m: &ArgMatches, force: bool) -> Result<()> {
    let mut builder = CertBuilder::new();

    // User ID
    match m.values_of("userid") {
        Some(uids) => for uid in uids {
            builder = builder.add_userid(uid);
        },
        None => {
            eprintln!("No user ID given, using direct key signature");
        }
    }

    // Expiration.
    match (m.value_of("expires"), m.value_of("expires-in")) {
        (None, None) => // Default expiration.
            builder = builder.set_validity_period(
                Some(Duration::new(3 * SECONDS_IN_YEAR, 0))),
        (Some(t), None) if t == "never" =>
            builder = builder.set_validity_period(None),
        (Some(t), None) => {
            let now = builder.creation_time()
                .unwrap_or_else(std::time::SystemTime::now);
            let expiration = SystemTime::from(
                crate::parse_iso8601(t, chrono::NaiveTime::from_hms(0, 0, 0))?);
            let validity = expiration.duration_since(now)?;
            builder = builder.set_creation_time(now)
                .set_validity_period(validity);
        },
        (None, Some(d)) if d == "never" =>
            builder = builder.set_validity_period(None),
        (None, Some(d)) => {
            let d = parse_duration(d)?;
            builder = builder.set_validity_period(Some(d));
        },
        (Some(_), Some(_)) => unreachable!("conflicting args"),
    }

    // Cipher Suite
    match m.value_of("cipher-suite") {
        Some("rsa3k") => {
            builder = builder.set_cipher_suite(CipherSuite::RSA3k);
        }
        Some("rsa4k") => {
            builder = builder.set_cipher_suite(CipherSuite::RSA4k);
        }
        Some("cv25519") => {
            builder = builder.set_cipher_suite(CipherSuite::Cv25519);
        }
        Some(ref cs) => {
            return Err(anyhow::anyhow!("Unknown cipher suite '{}'", cs));
        }
        None => panic!("argument has a default value"),
    }

    // Signing Capability
    match (m.is_present("can-sign"), m.is_present("cannot-sign")) {
        (false, false) | (true, false) => {
            builder = builder.add_signing_subkey();
        }
        (false, true) => { /* no signing subkey */ }
        (true, true) => {
            return Err(
                anyhow::anyhow!("Conflicting arguments --can-sign and --cannot-sign"));
        }
    }

    // Encryption Capability
    match (m.value_of("can-encrypt"), m.is_present("cannot-encrypt")) {
        (Some("universal"), false) | (None, false) => {
            builder = builder.add_subkey(KeyFlags::empty()
                                         .set_transport_encryption()
                                         .set_storage_encryption(),
                                         None,
                                         None);
        }
        (Some("storage"), false) => {
            builder = builder.add_storage_encryption_subkey();
        }
        (Some("transport"), false) => {
            builder = builder.add_transport_encryption_subkey();
        }
        (None, true) => { /* no encryption subkey */ }
        (Some(_), true) => {
            return Err(
                anyhow::anyhow!("Conflicting arguments --can-encrypt and \
                             --cannot-encrypt"));
        }
        (Some(ref cap), false) => {
            return Err(
                anyhow::anyhow!("Unknown encryption capability '{}'", cap));
        }
    }

    if m.is_present("with-password") {
        let p0 = rpassword::read_password_from_tty(Some(
            "Enter password to protect the key: "))?.into();
        let p1 = rpassword::read_password_from_tty(Some(
            "Repeat the password once more: "))?.into();

        if p0 == p1 {
            builder = builder.set_password(Some(p0));
        } else {
            return Err(anyhow::anyhow!("Passwords do not match."));
        }
    }

    // Generate the key
    let (cert, rev) = builder.generate()?;

    // Export
    if m.is_present("export") {
        let (key_path, rev_path) =
            match (m.value_of("export"), m.value_of("rev-cert")) {
                (Some("-"), Some("-")) =>
                    ("-".to_string(), "-".to_string()),
                (Some("-"), Some(ref rp)) =>
                    ("-".to_string(), rp.to_string()),
                (Some("-"), None) =>
                    return Err(
                        anyhow::anyhow!("Missing arguments: --rev-cert is mandatory \
                                     if --export is '-'.")),
                (Some(ref kp), None) =>
                    (kp.to_string(), format!("{}.rev", kp)),
                (Some(ref kp), Some("-")) =>
                    (kp.to_string(), "-".to_string()),
                (Some(ref kp), Some(ref rp)) =>
                    (kp.to_string(), rp.to_string()),
                _ =>
                    return Err(
                        anyhow::anyhow!("Conflicting arguments --rev-cert and \
                                     --export")),
            };

        let headers = cert.armor_headers();

        // write out key
        {
            let headers: Vec<_> = headers.iter()
                .map(|value| ("Comment", value.as_str()))
                .collect();

            let w = create_or_stdout(Some(&key_path), force)?;
            let mut w = Writer::with_headers(w, Kind::SecretKey, headers)?;
            cert.as_tsk().serialize(&mut w)?;
            w.finalize()?;
        }

        // write out rev cert
        {
            let mut headers: Vec<_> = headers.iter()
                .map(|value| ("Comment", value.as_str()))
                .collect();
            headers.insert(0, ("Comment", "Revocation certificate for"));

            let w = create_or_stdout(Some(&rev_path), force)?;
            let mut w = Writer::with_headers(w, Kind::Signature, headers)?;
            Packet::Signature(rev).serialize(&mut w)?;
            w.finalize()?;
        }
    } else {
        return Err(
            anyhow::anyhow!("Saving generated key to the store isn't implemented \
                         yet."));
    }

    Ok(())
}

pub fn adopt(config: Config, m: &ArgMatches, p: &dyn Policy) -> Result<()> {
    let input = open_or_stdin(m.value_of("certificate"))?;
    let cert = Cert::from_reader(input)?;
    let mut wanted: Vec<(KeyHandle,
                         Option<(Key<key::PublicParts, key::SubordinateRole>,
                                 SignatureBuilder)>)>
        = vec![];

    // Gather the Key IDs / Fingerprints and make sure they are valid.
    for id in m.values_of("key").unwrap_or_default() {
        let h = keyhandle_from_str(&id)?;
        if keyhandle_is_invalid(&h) {
            return Err(anyhow::anyhow!(
                "Invalid Fingerprint or KeyID ('{:?}')", id));
        }
        wanted.push((h, None));
    }

    let null_policy = &crate::openpgp::policy::NullPolicy::new();
    let adoptee_policy = if m.values_of("allow-broken-crypto").is_some() {
        null_policy
    } else {
        p
    };

    // Find the corresponding keys.
    for keyring in m.values_of("keyring").unwrap_or_default() {
        for cert in CertParser::from_file(keyring)
            .context(format!("Parsing: {}", keyring))?
        {
            let cert = cert.context(format!("Parsing {}", keyring))?;

            let vc = match cert.with_policy(adoptee_policy, None) {
                Ok(vc) => vc,
                Err(err) => {
                    eprintln!("Ignoring {} from '{}': {}",
                              cert.keyid().to_hex(), keyring, err);
                    continue;
                }
            };

            for key in vc.keys() {
                for (id, ref mut keyo) in wanted.iter_mut() {
                    if id.aliases(key.key_handle()) {
                        match keyo {
                            Some((_, _)) =>
                                // We already saw this key.
                                (),
                            None => {
                                let sig = key.binding_signature();
                                let builder: SignatureBuilder = match sig.typ() {
                                    SignatureType::SubkeyBinding =>
                                        sig.clone().into(),
                                    SignatureType::DirectKey
                                        | SignatureType::PositiveCertification
                                        | SignatureType::CasualCertification
                                        | SignatureType::PersonaCertification
                                        | SignatureType::GenericCertification =>
                                    {
                                        // Convert to a binding
                                        // signature.
                                        let kf = sig.key_flags()
                                            .context("Missing required \
                                                      subpacket, KeyFlags")?;
                                        SignatureBuilder::new(
                                            SignatureType::SubkeyBinding)
                                            .set_key_flags(kf)?
                                    },
                                    _ => panic!("Unsupported binding \
                                                 signature: {:?}",
                                                sig),
                                };

                                *keyo = Some(
                                    (key.key().clone().role_into_subordinate(),
                                     builder));
                            }
                        }
                    }
                }
            }
        }
    }


    // If we are missing any keys, stop now.
    let missing: Vec<&KeyHandle> = wanted
        .iter()
        .filter_map(|(id, keyo)| {
            match keyo {
                Some(_) => None,
                None => Some(id),
            }
        })
        .collect();
    if missing.len() > 0 {
        return Err(anyhow::anyhow!(
            "Keys not found: {}",
            missing.iter().map(|&h| format!("{:X}", h)).join(", ")));
    }


    let passwords = &mut Vec::new();

    // Get a signer.
    let pk = cert.primary_key().key();
    let mut pk_signer =
        decrypt_key(
            pk.clone().parts_into_secret()?,
            passwords)?
        .into_keypair()?;


    // Add the keys and signatues to cert.
    let mut packets: Vec<Packet> = vec![];
    for (_, ka) in wanted.into_iter() {
        let (key, builder) = ka.expect("Checked for missing keys above.");
        let mut builder = builder;

        // If there is a valid backsig, recreate it.
        let need_backsig = builder.key_flags()
            .map(|kf| kf.for_signing() || kf.for_certification())
            .expect("Missing keyflags");

        if need_backsig {
            // Derive a signer.
            let mut subkey_signer
                = decrypt_key(
                    key.clone().parts_into_secret()?,
                    passwords)?
                .into_keypair()?;

            let backsig = builder.embedded_signatures()
                .filter(|backsig| {
                    (*backsig).clone().verify_primary_key_binding(
                        &cert.primary_key(),
                        &key).is_ok()
                })
                .nth(0)
                .map(|sig| SignatureBuilder::from(sig.clone()))
                .unwrap_or_else(|| {
                    SignatureBuilder::new(SignatureType::PrimaryKeyBinding)
                })
                .sign_primary_key_binding(&mut subkey_signer, pk, &key)?;

            builder = builder.set_embedded_signature(backsig)?;
        } else {
            builder = builder.modify_hashed_area(|mut a| {
                a.remove_all(SubpacketTag::EmbeddedSignature);
                Ok(a)
            })?;
        }

        let mut sig = builder.sign_subkey_binding(&mut pk_signer, pk, &key)?;

        // Verify it.
        assert!(sig.verify_subkey_binding(pk_signer.public(), pk, &key)
                .is_ok());

        packets.push(key.into());
        packets.push(sig.into());
    }

    let cert = cert.clone().insert_packets(packets.clone())?;

    let mut message = crate::create_or_stdout_pgp(
        m.value_of("output"), config.force,
        m.is_present("binary"), sequoia_openpgp::armor::Kind::SecretKey)?;
    cert.as_tsk().serialize(&mut message)?;
    message.finalize()?;

    let vc = cert.with_policy(p, None).expect("still valid");
    for pair in packets[..].chunks(2) {
        let newkey: &Key<key::PublicParts, key::UnspecifiedRole> = match pair[0] {
            Packet::PublicKey(ref k) => k.into(),
            Packet::PublicSubkey(ref k) => k.into(),
            Packet::SecretKey(ref k) => k.into(),
            Packet::SecretSubkey(ref k) => k.into(),
            ref p => panic!("Expected a key, got: {:?}", p),
        };
        let newsig: &Signature = match pair[1] {
            Packet::Signature(ref s) => s,
            ref p => panic!("Expected a sig, got: {:?}", p),
        };

        let mut found = false;
        for key in vc.keys() {
            if key.fingerprint() == newkey.fingerprint() {
                for sig in key.self_signatures() {
                    if sig == newsig {
                        found = true;
                        break;
                    }
                }
            }
        }
        assert!(found, "Subkey: {:?}\nSignature: {:?}", newkey, newsig);
    }

    Ok(())
}

pub fn attest_certifications(config: Config, m: &ArgMatches, _p: &dyn Policy)
                             -> Result<()> {
    // XXX: This function has to do some steps manually, because
    // Sequoia does not expose this functionality because it has not
    // been standardized yet.
    use sequoia_openpgp::{
        crypto::hash::{Hash, Digest},
        packet::signature::subpacket::*,
        types::HashAlgorithm,
    };
    #[allow(non_upper_case_globals)]
    const SignatureType__AttestedKey: SignatureType =
        SignatureType::Unknown(0x16);
    #[allow(non_upper_case_globals)]
    const SubpacketTag__AttestedCertifications: SubpacketTag =
        SubpacketTag::Unknown(37);

    // Attest to all certifications?
    let all = ! m.is_present("none"); // All is the default.

    // Some configuration.
    let hash_algo = HashAlgorithm::default();
    let digest_size = hash_algo.context()?.digest_size();
    let reserve_area_space = 256; // For the other subpackets.
    let digests_per_sig = ((1usize << 16) - reserve_area_space) / digest_size;

    let input = open_or_stdin(m.value_of("key"))?;
    let key = Cert::from_reader(input)?;

    // First, remove all attestations.
    let key = Cert::from_packets(
        key.into_packets().filter(|p| match p {
            Packet::Signature(s) if s.typ() == SignatureType__AttestedKey =>
                false,
            _ => true,
        }))?;


    // Get a signer.
    let mut passwords = Vec::new();
    let pk = key.primary_key().key();
    let mut pk_signer =
        decrypt_key(
            pk.clone().parts_into_secret()?,
            &mut passwords)?
        .into_keypair()?;

    // Now, create new attestation signatures.
    let mut attestation_signatures = Vec::new();
    for uid in key.userids() {
        let mut attestations = Vec::new();

        if all {
            for certification in uid.certifications() {
                let mut h = hash_algo.context()?;
                hash_for_confirmation(certification, &mut h);
                attestations.push(h.into_digest()?);
            }
        }

        // Hashes SHOULD be sorted.
        attestations.sort();

        // All attestation signatures we generate for this component
        // should have the same creation time.  Fix it now.
        let t = std::time::SystemTime::now();

        // Hash the components like in a binding signature.
        let mut hash = hash_algo.context()?;
        key.primary_key().hash(&mut hash);
        uid.hash(&mut hash);

        for digests in attestations.chunks(digests_per_sig) {
            let mut body = Vec::with_capacity(digest_size * digests.len());
            digests.iter().for_each(|d| body.extend(d));

            attestation_signatures.push(
                SignatureBuilder::new(SignatureType__AttestedKey)
                    .set_signature_creation_time(t)?
                    .modify_hashed_area(|mut a| {
                        a.add(Subpacket::new(
                            SubpacketValue::Unknown {
                                tag: SubpacketTag__AttestedCertifications,
                                body,
                            },
                            true)?)?;
                        Ok(a)
                    })?
                    .sign_hash(&mut pk_signer, hash.clone())?);
        }
    }

    for ua in key.user_attributes() {
        let mut attestations = Vec::new();

        if all {
            for certification in ua.certifications() {
                let mut h = hash_algo.context()?;
                hash_for_confirmation(certification, &mut h);
                attestations.push(h.into_digest()?);
            }
        }

        // Hashes SHOULD be sorted.
        attestations.sort();

        // All attestation signatures we generate for this component
        // should have the same creation time.  Fix it now.
        let t = std::time::SystemTime::now();

        // Hash the components like in a binding signature.
        let mut hash = hash_algo.context()?;
        key.primary_key().hash(&mut hash);
        ua.hash(&mut hash);

        for digests in attestations.chunks(digests_per_sig) {
            let mut body = Vec::with_capacity(digest_size * digests.len());
            digests.iter().for_each(|d| body.extend(d));

            attestation_signatures.push(
                SignatureBuilder::new(SignatureType__AttestedKey)
                    .set_signature_creation_time(t)?
                    .modify_hashed_area(|mut a| {
                        a.add(Subpacket::new(
                            SubpacketValue::Unknown {
                                tag: SubpacketTag__AttestedCertifications,
                                body,
                            },
                            true)?)?;
                        Ok(a)
                    })?
                    .sign_hash(&mut pk_signer, hash.clone())?);
        }
    }

    // Finally, add the new signatures.
    let key = key.insert_packets(attestation_signatures)?;

    let mut message = crate::create_or_stdout_pgp(
        m.value_of("output"), config.force, m.is_present("binary"),
        sequoia_openpgp::armor::Kind::SecretKey)?;
    key.as_tsk().serialize(&mut message)?;
    message.finalize()?;
    Ok(())
}

// XXX: The following functions are backports from sequoia-openpgp
// 1.1.  Remove them by reverting the commit that introduced them once
// sequoia-sq depends on a newer version of sequoia-openpgp.

fn keyhandle_from_str(s: &str) -> Result<KeyHandle> {
    use sequoia_openpgp::{Fingerprint, KeyID};
    let bytes = &sequoia_openpgp::fmt::hex::decode_pretty(s)?[..];
    match Fingerprint::from_bytes(bytes) {
        fpr @ Fingerprint::Invalid(_) => {
            match KeyID::from_bytes(bytes) {
                // If it can't be parsed as either a Fingerprint or a
                // KeyID, return Fingerprint::Invalid.
                KeyID::Invalid(_) => Ok(fpr.into()),
                kid => Ok(kid.into()),
            }
        }
        fpr => Ok(fpr.into()),
    }
}

fn keyhandle_is_invalid(h: &KeyHandle) -> bool {
    use sequoia_openpgp::{Fingerprint, KeyID};
    match h {
        KeyHandle::Fingerprint(Fingerprint::Invalid(_)) => true,
        KeyHandle::KeyID(KeyID::Invalid(_)) => true,
        _ => false,
    }
}

/// Hashes this signature for use in a Third-Party Confirmation
/// signature.
use sequoia_openpgp::{crypto::hash::Digest, packet::Signature};
pub fn hash_for_confirmation(sig: &Signature, hash: &mut dyn Digest) {
    use sequoia_openpgp::serialize::{Marshal, MarshalInto};
    // Section 5.2.4 of RFC4880:
    //
    // > When a signature is made over a Signature packet (type
    // > 0x50), the hash data starts with the octet 0x88, followed
    // > by the four-octet length of the signature, and then the
    // > body of the Signature packet.  (Note that this is an
    // > old-style packet header for a Signature packet with the
    // > length-of-length set to zero.)  The unhashed subpacket
    // > data of the Signature packet being hashed is not included
    // > in the hash, and the unhashed subpacket data length value
    // > is set to zero.

    // This code assumes that the signature has been verified
    // prior to being confirmed, so it is well-formed.
    let mut body = Vec::new();
    body.push(sig.version());
    body.push(sig.typ().into());
    body.push(sig.pk_algo().into());
    body.push(sig.hash_algo().into());

    // The hashed area.
    let l = sig.hashed_area().serialized_len()
    // Assumes well-formedness.
        .min(std::u16::MAX as usize);
    body.extend(&(l as u16).to_be_bytes());
    // Assumes well-formedness.
    let _ = sig.hashed_area().serialize(&mut body);

    // The unhashed area.
    body.extend(&[0, 0]); // Size replaced by zero.
    // Unhashed packets omitted.

    body.extend(sig.digest_prefix());
    let _ = sig.mpis().serialize(&mut body);

    hash.update(&[0x88]);
    hash.update(&(body.len() as u32).to_be_bytes());
    hash.update(&body);
}