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
|
//! Tests gpg-agent interaction.
use std::io::{self, Write};
extern crate futures;
use futures::future::Future;
use futures::stream::Stream;
extern crate sequoia_openpgp as openpgp;
use crate::openpgp::constants::{
HashAlgorithm,
SymmetricAlgorithm,
};
use crate::openpgp::crypto::SessionKey;
use crate::openpgp::constants::KeyFlags;
use crate::openpgp::parse::stream::*;
use crate::openpgp::serialize::{Serialize, stream::*};
use crate::openpgp::tpk::{TPKBuilder, CipherSuite};
extern crate sequoia_ipc as ipc;
use crate::ipc::gnupg::{Context, Agent, KeyPair};
macro_rules! make_context {
() => {{
let ctx = match Context::ephemeral() {
Ok(c) => c,
Err(e) => {
eprintln!("SKIP: Failed to create GnuPG context: {}\n\
SKIP: Is GnuPG installed?", e);
return;
},
};
match ctx.start("gpg-agent") {
Ok(_) => (),
Err(e) => {
eprintln!("SKIP: Failed to create GnuPG context: {}\n\
SKIP: Is the GnuPG agent installed?", e);
return;
},
}
ctx
}};
}
#[test]
fn nop() {
let ctx = make_context!();
let mut agent = Agent::connect(&ctx).wait().unwrap();
agent.send("NOP").unwrap();
let response = agent.wait().collect::<Vec<_>>();
assert_eq!(response.len(), 1);
assert!(response[0].is_ok());
}
#[test]
fn help() {
let ctx = make_context!();
let mut agent = Agent::connect(&ctx).wait().unwrap();
agent.send("HELP").unwrap();
let response = agent.wait().collect::<Vec<_>>();
assert!(response.len() > 3);
assert!(response.iter().last().unwrap().is_ok());
}
const MESSAGE: &'static str = "дружба";
fn gpg_import(ctx: &Context, what: &[u8]) {
use std::process::{Command, Stdio};
let mut gpg = Command::new("gpg")
.stdin(Stdio::piped())
.arg("--homedir").arg(ctx.directory("homedir").unwrap())
.arg("--import")
.spawn()
.expect("failed to start gpg");
gpg.stdin.as_mut().unwrap().write_all(what).unwrap();
let status = gpg.wait().unwrap();
assert!(status.success());
}
#[test]
fn sign() {
use self::CipherSuite::*;
let ctx = make_context!();
for cs in &[RSA2k, Cv25519, P521] {
let (tpk, _) = TPKBuilder::new()
.set_cipher_suite(*cs)
.add_userid("someone@example.org")
.add_signing_subkey()
.generate().unwrap();
let mut buf = Vec::new();
tpk.as_tsk().serialize(&mut buf).unwrap();
gpg_import(&ctx, &buf);
let keypair = KeyPair::new(
&ctx, tpk.keys_valid().signing_capable().take(1).next().unwrap().2)
.unwrap();
let mut message = Vec::new();
{
// Start streaming an OpenPGP message.
let message = Message::new(&mut message);
// We want to sign a literal data packet.
let signer = Signer::new(message, keypair)
// XXX: Is this necessary? If so, it shouldn't.
.hash_algo(HashAlgorithm::SHA512).unwrap()
.build().unwrap();
// Emit a literal data packet.
let mut literal_writer = LiteralWriter::new(
signer).build().unwrap();
// Sign the data.
literal_writer.write_all(MESSAGE.as_bytes()).unwrap();
// Finalize the OpenPGP message to make sure that all data is
// written.
literal_writer.finalize().unwrap();
}
// Make a helper that that feeds the sender's public key to the
// verifier.
let helper = Helper { tpk: &tpk };
// Now, create a verifier with a helper using the given TPKs.
let mut verifier =
Verifier::from_bytes(&message, helper, None).unwrap();
// Verify the data.
let mut sink = Vec::new();
io::copy(&mut verifier, &mut sink).unwrap();
assert_eq!(MESSAGE.as_bytes(), &sink[..]);
}
struct Helper<'a> {
tpk: &'a openpgp::TPK,
}
impl<'a> VerificationHelper for Helper<'a> {
fn get_public_keys(&mut self, _ids: &[openpgp::KeyID])
-> openpgp::Result<Vec<openpgp::TPK>> {
// Return public keys for signature verification here.
Ok(vec![self.tpk.clone()])
}
fn check(&mut self, structure: &MessageStructure)
-> openpgp::Result<()> {
// In this function, we implement our signature verification
// policy.
let mut good = false;
for (i, layer) in structure.iter().enumerate() {
match (i, layer) {
// First, we are interested in signatures over the
// data, i.e. level 0 signatures.
(0, MessageLayer::SignatureGroup { ref results }) => {
// Finally, given a VerificationResult, which only says
// whether the signature checks out mathematically, we apply
// our policy.
match results.get(0) {
Some(VerificationResult::GoodChecksum(..)) =>
good = true,
Some(VerificationResult::NotAlive(_)) =>
return Err(failure::err_msg(
"Good, but not live signature")),
Some(VerificationResult::MissingKey(_)) =>
return Err(failure::err_msg(
"Missing key to verify signature")),
Some(VerificationResult::BadChecksum(_)) =>
return Err(failure::err_msg("Bad signature")),
None =>
return Err(failure::err_msg("No signature")),
}
},
_ => return Err(failure::err_msg(
"Unexpected message structure")),
}
}
if good {
Ok(()) // Good signature.
} else {
Err(failure::err_msg("Signature verification failed"))
|