Debian Codegraph @

src/commands/autocrypt.rs

raw
1 use anyhow::Result;
2
3 use buffered_reader::{BufferedReader, Dup};
4 use sequoia_openpgp as openpgp;
5 use openpgp::{
6 packet::UserID,
7 parse::{Cookie, Parse, stream::DecryptorBuilder},
8 };
9 use sequoia_autocrypt as autocrypt;
10
11 use crate::{
12 Sq,
13 commands::{
14 cert::import::import_and_report,
15 network::certify_downloads,
16 },
17 output::import::ImportStats,
18 };
19
20 /// Imports certs encoded as Autocrypt headers.
21 ///
22 /// We also try to decrypt the message, and collect the gossip headers.
23 pub fn import_certs(sq: &mut Sq, source: &mut Box<dyn BufferedReader<Cookie>>,
24 stats: &mut ImportStats)
25 -> Result<()>
26 {
27 let o = &mut std::io::stdout();
28 let mut acc = Vec::new();
29
30 // First, get the Autocrypt headers from the outside.
31 let mut dup = Dup::with_cookie(&mut *source, Cookie::default());
32 let ac = autocrypt::AutocryptHeaders::from_reader(&mut dup)?;
33 let from = UserID::from(
34 ac.from.as_ref().ok_or(anyhow::anyhow!("no From: header"))?
35 .as_str());
36 let from_addr = from.email()?.ok_or(
37 anyhow::anyhow!("no email address in From: header"))?;
38
39 use autocrypt::AutocryptHeaderType::*;
40 let mut sender_cert = None;
41 let mut provenance_recorded = false;
42 for h in ac.headers.into_iter().filter(|h| h.header_type == Sender) {
43 if let Some(addr) = h.attributes.iter()
44 .find_map(|a| (&a.key == "addr"
45 && a.value.to_lowercase() == from_addr.to_lowercase())
46 .then(|| a.value.clone()))
47 {
48 if let Some(cert) = h.key {
49 sender_cert = Some(cert.clone());
50
51 if let Ok((ca, _)) = sq.certd_or_else()
52 .and_then(|certd| certd.shadow_ca_autocrypt())
53 {
54 acc.append(&mut certify_downloads(
55 sq, false, ca,
56 vec![cert], Some(&addr[..])));
57 provenance_recorded = true;
58 } else {
59 acc.push(cert);
60 }
61 }
62 }
63 }
64
65 import_and_report(o, sq, acc, None, stats, |o, _| {
66 if provenance_recorded {
67 wwriteln!(stream = o, initial_indent = " - ",
68 "provenance information recorded");
69 }
70
71 Ok(())
72 })?;
73
74 // If there is no Autocrypt header, don't bother looking for
75 // gossip.
76 let sender_cert = match sender_cert {
77 Some(c) => c,
78 None => return Ok(()),
79 };
80
81 // Then, try to decrypt the message, and look for gossip headers.
82 use crate::commands::decrypt::Helper;
83 let mut helper = Helper::new(
84 &sq,
85 1, // Require one trusted signature...
86 vec![sender_cert.clone()], // ... from this cert.
87 vec![], vec![], false);
88 helper.quiet(true);
89
90 let policy = sq.policy.clone();
91 let dup = Dup::with_cookie(source, Cookie::default());
92 let mut decryptor = match DecryptorBuilder::from_buffered_reader(dup)?
93 .with_policy(&policy, None, helper)
94 {
95 Ok(d) => d,
96 Err(e) => {
97 // The decryption failed, but we should still import the
98 // Autocrypt header.
99 if sq.verbose() {
100 weprintln!("Note: Processing of message failed: {}", e);
101 }
102
103 return Ok(());
104 },
105 };
106
107 let ac = autocrypt::AutocryptHeaders::from_reader(&mut decryptor)?;
108 let helper = decryptor.into_helper();
109
110 // We know there has been one good signature from the sender. Now
111 // check that the message was encrypted. Note: it doesn't have to
112 // be encrypted for the purpose of the certification, but
113 // Autocrypt requires messages to be signed and encrypted.
114 if helper.sym_algo.is_none() {
115 if sq.verbose() {
116 weprintln!("Note: Message is not encrypted, ignoring message");
117 }
118
119 return Ok(());
120 }
121
122 let mut acc = Vec::new();
123 for h in ac.headers.into_iter().filter(|h| h.header_type == Gossip) {
124 if let Some(_addr) = h.attributes.iter()
125 .find_map(|a| (&a.key == "addr").then(|| a.value.clone()))
126 {
127 if let Some(cert) = h.key {
128 acc.push(cert);
129 }
130 }
131 }
132
133 import_and_report(o, sq, acc, None, stats, |_, _| Ok(()))?;
134
135 Ok(())
136 }