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
|
use failure::{self, ResultExt};
use std::collections::HashMap;
use std::io;
use rpassword;
extern crate termsize;
extern crate sequoia_openpgp as openpgp;
use sequoia_core::Context;
use openpgp::constants::SymmetricAlgorithm;
use openpgp::conversions::hex;
use openpgp::crypto::SessionKey;
use openpgp::{Fingerprint, TPK, KeyID, Result};
use openpgp::packet::{Key, key::SecretKey, Signature, PKESK, SKESK};
use openpgp::parse::PacketParser;
use openpgp::parse::stream::{
VerificationHelper, DecryptionHelper, Decryptor, MessageStructure,
};
extern crate sequoia_store as store;
use super::{dump::PacketDumper, VHelper};
struct Helper<'a> {
vhelper: VHelper<'a>,
secret_keys: HashMap<KeyID, Key>,
key_identities: HashMap<KeyID, Fingerprint>,
key_hints: HashMap<KeyID, String>,
dump_session_key: bool,
dumper: Option<PacketDumper>,
hex: bool,
}
impl<'a> Helper<'a> {
fn new(ctx: &'a Context, store: &'a mut store::Store,
signatures: usize, tpks: Vec<TPK>, secrets: Vec<TPK>,
dump_session_key: bool, dump: bool, hex: bool)
-> Self {
let mut keys: HashMap<KeyID, Key> = HashMap::new();
let mut identities: HashMap<KeyID, Fingerprint> = HashMap::new();
let mut hints: HashMap<KeyID, String> = HashMap::new();
for tsk in secrets {
let can_encrypt = |_: &Key, sig: Option<&Signature>| -> bool {
if let Some(sig) = sig {
sig.key_flags().can_encrypt_at_rest()
|| sig.key_flags().can_encrypt_for_transport()
} else {
false
}
};
let hint = match tsk.userids().nth(0) {
Some(uid) => format!("{} ({})", uid.userid(),
tsk.fingerprint().to_keyid()),
None => format!("{}", tsk.fingerprint().to_keyid()),
};
if can_encrypt(tsk.primary(), tsk.primary_key_signature()) {
let id = tsk.fingerprint().to_keyid();
keys.insert(id.clone(), tsk.primary().clone());
identities.insert(id.clone(), tsk.fingerprint());
hints.insert(id, hint.clone());
}
for skb in tsk.subkeys() {
let key = skb.subkey();
if can_encrypt(key, skb.binding_signature()) {
let id = key.fingerprint().to_keyid();
keys.insert(id.clone(), key.clone());
identities.insert(id.clone(), tsk.fingerprint());
hints.insert(id, hint.clone());
}
}
}
Helper {
vhelper: VHelper::new(ctx, store, signatures, tpks),
secret_keys: keys,
key_identities: identities,
key_hints: hints,
dump_session_key: dump_session_key,
dumper: if dump || hex {
let width =
termsize::get().map(|s| s.cols as usize).unwrap_or(80);
Some(PacketDumper::new(width, false))
} else {
None
},
hex: hex,
}
}
}
impl<'a> VerificationHelper for Helper<'a> {
fn get_public_keys(&mut self, ids: &[KeyID]) -> Result<Vec<TPK>> {
self.vhelper.get_public_keys(ids)
}
fn check(&mut self, structure: &MessageStructure) -> Result<()> {
self.vhelper.check(structure)
}
}
impl<'a> DecryptionHelper for Helper<'a> {
fn mapping(&self) -> bool {
self.hex
}
fn inspect(&mut self, pp: &PacketParser) -> Result<()> {
if let Some(dumper) = self.dumper.as_mut() {
dumper.packet(&mut io::stderr(),
pp.recursion_depth() as usize,
pp.header().clone(), pp.packet.clone(),
pp.map().map(|m| m.clone()), None)?;
}
Ok(())
}
fn decrypt<D>(&mut self, pkesks: &[PKESK], skesks: &[SKESK],
mut decrypt: D) -> openpgp::Result<Option<Fingerprint>>
where D: FnMut(SymmetricAlgorithm, &SessionKey) -> openpgp::Result<()>
{
// First, we try those keys that we can use without prompting
// for a password.
for pkesk in pkesks {
let keyid = pkesk.recipient();
if let Some(key) = self.secret_keys.get(&keyid) {
if let Some(SecretKey::Unencrypted { mpis }) = key.secret() {
if let Ok(sk) = pkesks[0].decrypt(key, mpis)
.and_then(|(algo, sk)| { decrypt(algo, &sk)?; Ok(sk) })
{
if self.dump_session_key {
eprintln!("Session key: {}", hex::encode(&sk));
}
return Ok(self.key_identities.get(keyid)
.map(|fp| fp.clone()));
}
}
}
}
// Second, we try those keys that are encrypted.
'pkesk_loop: for pkesk in pkesks {
// Don't ask the user to decrypt a key if we don't support
// the algorithm.
if ! pkesk.pk_algo().is_supported() {
continue;
}
let keyid = pkesk.recipient();
if let Some(key) = self.secret_keys.get(&keyid) {
if key.secret().map(|s| ! s.is_encrypted())
.unwrap_or(true)
{
continue;
}
loop
|