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
|
use failure::{self, ResultExt};
use std::cmp::Ordering;
use std::fs::File;
use std::io::{self, Write};
use rpassword;
extern crate openpgp;
use openpgp::Packet;
use openpgp::packet::Tag;
use openpgp::serialize::stream::{
wrap, LiteralWriter, Encryptor, EncryptionMode,
};
extern crate sequoia_store as store;
// Indent packets according to their recursion level.
const INDENT: &'static str
= " ";
pub fn decrypt(input: &mut io::Read, output: &mut io::Write,
dump: bool, map: bool)
-> Result<(), failure::Error> {
#[derive(PartialEq)]
enum State {
Start(Vec<()>, Vec<openpgp::SKESK>),
Deciphered,
Done,
}
let mut state = State::Start(vec![], vec![]);
let mut ppo
= openpgp::parse::PacketParserBuilder::from_reader(input)?
.map(map).finalize()?;
while let Some(mut pp) = ppo {
if dump || map {
eprintln!("{}{:?}",
&INDENT[0..pp.recursion_depth as usize], pp.packet);
}
if let Some(ref map) = pp.map {
eprintln!();
let mut hd = HexDumper::new();
for (field, bytes) in map.iter() {
hd.print(bytes, field);
}
println!();
}
state = match state {
// Look for an PKESK or SKESK packet.
State::Start(pkesks, mut skesks) =>
match pp.packet {
Packet::SEIP(_) => {
let mut state = None;
for _pkesk in pkesks.iter() {
// XXX try to decrypt those
}
if ! skesks.is_empty() {
let pass = rpassword::prompt_password_stderr(
"Enter passphrase to decrypt message: ")?
.into_bytes();
for skesk in skesks.iter() {
let (algo, key) =
skesk.decrypt(&pass)?;
let r = pp.decrypt(algo, &key[..]);
if r.is_ok() {
state = Some(State::Deciphered);
break;
}
}
}
state.unwrap_or(State::Start(pkesks, skesks))
},
_ => State::Start(pkesks, skesks),
},
// Look for the literal data packet.
State::Deciphered =>
if let Packet::Literal(_) = pp.packet {
io::copy(&mut pp, output)?;
State::Done
} else {
State::Deciphered
},
// We continue to parse, useful for dumping
// encrypted packets.
State::Done => State::Done,
};
let (packet, _, ppo_tmp, _) = pp.recurse()?;
ppo = ppo_tmp;
state = match state {
// Look for an PKESK or SKESK packet.
State::Start(pkesks, mut skesks) =>
match packet {
Packet::Unknown(u) => {
match u.tag {
Tag::PKESK =>
eprintln!("Decryption using PKESK not yet \
supported."),
_ => (),
}
State::Start(pkesks, skesks)
},
Packet::SKESK(skesk) => {
skesks.push(skesk);
State::Start(pkesks, skesks)
},
_ => State::Start(pkesks, skesks),
},
// Do nothing in all other states.
s => s,
};
}
if state != State::Done {
return Err(failure::err_msg("Decryption failed."));
}
Ok(())
}
pub fn encrypt(store: &mut store::Store,
input: &mut io::Read, output: &mut io::Write,
npasswords: usize, recipients: Vec<&str>)
-> Result<(), failure::Error> {
let mut tpks = Vec::with_capacity(recipients.len());
for r in recipients {
tpks.push(store.lookup(r).context("No such key found")?.tpk()?);
}
let mut passwords = Vec::with_capacity(npasswords);
for n in 0..npasswords {
let nprompt = format!("Enter passphrase {}: ", n + 1);
passwords.push(rpassword::prompt_password_stderr(
if npasswords > 1 {
&nprompt
} else {
"Enter passphrase: "
})?);
}
// Build a vector of references to hand to Encryptor.
let recipients: Vec<&openpgp::TPK> = tpks.iter().collect();
let passwords_: Vec<&[u8]> =
passwords.iter().map(|p| p.as_bytes()).collect();
// We want to encrypt a literal data packet.
let encryptor = Encryptor::new(wrap(output),
&passwords_,
&recipients,
EncryptionMode::AtRest)
.context("Failed to create encryptor")?;
let mut literal_writer = LiteralWriter::new(encryptor, 'b', None, 0)
.context("Failed to create literal writer")?;
// Finally, copy stdin to our writer stack to encrypt the data.
io::copy(input, &mut literal_writer)
.context("Failed to encrypt")?;
Ok(())
}
pub fn dump(input: &mut io::Read, output: &mut io::Write, map: bool)
-> Result<(), failure::Error> {
let mut ppo
= openpgp::parse::PacketParserBuilder::from_reader(input)?
.map(map).finalize()?;
while let Some(mut pp) = ppo {
if let Some(ref map) = pp.map {
let mut hd = HexDumper::new();
writeln!(
|