Debian Codegraph @

tests/integration/sq_key_approvals_update.rs

raw
1 use std::ffi::OsStr;
2 use std::path::Path;
3
4 use super::common::{artifact, NO_USERIDS, Sq, STANDARD_POLICY, UserIDArg};
5
6 use sequoia_openpgp as openpgp;
7 use openpgp::{
8 Cert,
9 Result,
10 cert::amalgamation::ValidateAmalgamation,
11 parse::Parse,
12 };
13
14 #[test]
15 fn update_files() -> Result<()> {
16 // See https://gitlab.com/sequoia-pgp/sequoia/-/issues/1111
17 let now = std::time::SystemTime::now()
18 - std::time::Duration::new(60 * 60, 0);
19
20 let sq = Sq::at(now);
21
22 let alice_userid = "<[email protected]>";
23 let (alice, alice_pgp, _alice_rev)
24 = sq.key_generate(&[], &["<[email protected]>"]);
25 let (_bob, bob_pgp, _bob_rev)
26 = sq.key_generate(&[], &["<[email protected]>"]);
27
28 // Attest the certifications.
29 //
30 // public is first merged into private.
31 let attest = |sq: &Sq, public: &Path| {
32 let priv_file = sq.scratch_file(
33 &*format!("{}-priv",
34 public.file_name()
35 .unwrap_or(OsStr::new(""))
36 .to_str().unwrap_or("")));
37
38 sq.keyring_merge(
39 &[ public, &alice_pgp ][..], None,
40 &*priv_file);
41
42 let approval_file = sq.scratch_file(
43 &*format!("{}-approval", public.display()));
44
45 let approval = sq.key_approvals_update(
46 &["--add-all"], &priv_file, NO_USERIDS, &*approval_file);
47
48 eprintln!("{}", sq.inspect(&approval_file));
49
50 assert_eq!(approval.bad_signatures().count(), 0);
51
52 let approval_ua = approval.userids().next().unwrap();
53 assert_eq!(approval_ua.approvals().count(), 1);
54
55 };
56
57 // Attest nothing.
58 attest(&sq, &alice_pgp);
59
60 // Have Bob certify Alice.
61 let alice2_pub_pgp = sq.scratch_file("alice2_pub");
62 let alice2 = sq.pki_vouch_add(&[],
63 &bob_pgp,
64 &alice_pgp,
65 &[alice_userid],
66 &*alice2_pub_pgp);
67 assert_eq!(alice2.fingerprint(), alice.fingerprint());
68
69 // Attest Bob's certification.
70 attest(&sq, &alice2_pub_pgp);
71
72 Ok(())
73 }
74
75 const ALICE_USERID: &str = "<[email protected]>";
76 const BOB_USERID: &str = "<[email protected]>";
77
78 fn make_keys(sq: &mut Sq) -> Result<(Cert, Cert)> {
79 let (alice, alice_pgp, _alice_rev)
80 = sq.key_generate(&[], &[ALICE_USERID]);
81 let (bob, bob_pgp, _bob_rev)
82 = sq.key_generate(&[], &[BOB_USERID]);
83
84 sq.key_import(alice_pgp);
85 sq.key_import(bob_pgp);
86
87 // Give Sequoia a little space to backdate the signatures. Note:
88 // It will not backdate them past the key creation time.
89 sq.tick(10);
90
91 Ok((alice, bob))
92 }
93
94
95 #[test]
96 fn update_all() -> Result<()> {
97 // See https://gitlab.com/sequoia-pgp/sequoia/-/issues/1111
98 let now = std::time::SystemTime::now()
99 - std::time::Duration::new(60 * 60, 0);
100
101 let mut sq = Sq::at(now);
102 let (alice, bob) = make_keys(&mut sq)?;
103
104 // Attest the zero certifications.
105 let approval = sq.key_approvals_update(
106 &["--add-all"], alice.key_handle(), NO_USERIDS, None);
107
108 assert_eq!(approval.bad_signatures().count(), 0);
109 let approval_ua = approval.userids().next().unwrap();
110 assert_eq!(approval_ua.approvals().count(), 1);
111 assert_eq!(approval_ua.with_policy(STANDARD_POLICY, None).unwrap()
112 .approved_certifications().count(), 0);
113
114 // Have Bob certify Alice.
115 let alice2 = sq.pki_vouch_add(&[],
116 bob.key_handle(),
117 alice.key_handle(),
118 &[ALICE_USERID],
119 None);
120 assert_eq!(alice2.fingerprint(), alice.fingerprint());
121
122 // Attest Bob's certification.
123 let approval = sq.key_approvals_update(
124 &["--add-all"], &alice.key_handle(), NO_USERIDS, None);
125
126 assert_eq!(approval.bad_signatures().count(), 0);
127 let approval_ua = approval.userids().next().unwrap();
128 assert_eq!(approval_ua.approvals().count(), 2);
129 assert_eq!(approval_ua.with_policy(STANDARD_POLICY, None).unwrap()
130 .approved_certifications().count(), 1);
131
132 // Drop the approval of Bob's certification.
133 let approval = sq.key_approvals_update(
134 &["--remove-all"], &alice.key_handle(), NO_USERIDS, None);
135
136 assert_eq!(approval.bad_signatures().count(), 0);
137 let approval_ua = approval.userids().next().unwrap();
138 assert_eq!(approval_ua.approvals().count(), 3);
139 assert_eq!(approval_ua.with_policy(STANDARD_POLICY, None).unwrap()
140 .approved_certifications().count(), 0);
141
142 Ok(())
143 }
144
145 #[test]
146 fn update_by() -> Result<()> {
147 // See https://gitlab.com/sequoia-pgp/sequoia/-/issues/1111
148 let now = std::time::SystemTime::now()
149 - std::time::Duration::new(60 * 60, 0);
150
151 let mut sq = Sq::at(now);
152 let (alice, bob) = make_keys(&mut sq)?;
153 let bob_fp = bob.fingerprint().to_string();
154
155 // Attest the zero certifications.
156 let approval = sq.key_approvals_update(
157 &["--add-by", &bob_fp], alice.key_handle(), NO_USERIDS, None);
158
159 assert_eq!(approval.bad_signatures().count(), 0);
160 let approval_ua = approval.userids().next().unwrap();
161 assert_eq!(approval_ua.approvals().count(), 1);
162 assert_eq!(approval_ua.with_policy(STANDARD_POLICY, None).unwrap()
163 .approved_certifications().count(), 0);
164
165 // Have Bob certify Alice.
166 let alice2 = sq.pki_vouch_add(&[],
167 bob.key_handle(),
168 alice.key_handle(),
169 &[ALICE_USERID],
170 None);
171 assert_eq!(alice2.fingerprint(), alice.fingerprint());
172
173 // Attest Bob's certification.
174 let approval = sq.key_approvals_update(
175 &["--add-by", &bob_fp], &alice.key_handle(), NO_USERIDS, None);
176
177 assert_eq!(approval.bad_signatures().count(), 0);
178 let approval_ua = approval.userids().next().unwrap();
179 assert_eq!(approval_ua.approvals().count(), 2);
180 assert_eq!(approval_ua.with_policy(STANDARD_POLICY, None).unwrap()
181 .approved_certifications().count(), 1);
182
183 // Drop the approval of Bob's certification.
184 let approval = sq.key_approvals_update(
185 &["--remove-by", &bob_fp], &alice.key_handle(), NO_USERIDS, None);
186
187 assert_eq!(approval.bad_signatures().count(), 0);
188 let approval_ua = approval.userids().next().unwrap();
189 assert_eq!(approval_ua.approvals().count(), 3);
190 assert_eq!(approval_ua.with_policy(STANDARD_POLICY, None).unwrap()
191 .approved_certifications().count(), 0);
192
193 Ok(())
194 }
195
196
197 #[test]
198 fn update_authenticated() -> Result<()> {
199 // See https://gitlab.com/sequoia-pgp/sequoia/-/issues/1111
200 let now = std::time::SystemTime::now()
201 - std::time::Duration::new(60 * 60, 0);
202
203 let mut sq = Sq::at(now);
204 let (alice, bob) = make_keys(&mut sq)?;
205 let bob_fp = bob.fingerprint().to_string();
206
207 // Have Bob certify Alice.
208 let alice2 = sq.pki_vouch_add(&[],
209 bob.key_handle(),
210 alice.key_handle(),
211 &[ALICE_USERID],
212 None);
213 assert_eq!(alice2.fingerprint(), alice.fingerprint());
214
215 // Attest the zero certifications.
216 let approval = sq.key_approvals_update(
217 &["--add-authenticated"], alice.key_handle(), NO_USERIDS, None);
218
219 assert_eq!(approval.bad_signatures().count(), 0);
220 let approval_ua = approval.userids().next().unwrap();
221 assert_eq!(approval_ua.approvals().count(), 1);
222 assert_eq!(approval_ua.with_policy(STANDARD_POLICY, None).unwrap()
223 .approved_certifications().count(), 0);
224
225 // Link Bob's cert to his user ID.
226 let mut link = sq.command();
227 link.args(&["pki", "link", "add",
228 "--cert", &bob_fp,
229 "--userid", BOB_USERID]);
230 sq.run(link, true);
231
232 // Attest Bob's certification.
233 let approval = sq.key_approvals_update(
234 &["--add-authenticated"], &alice.key_handle(), NO_USERIDS, None);
235
236 assert_eq!(approval.bad_signatures().count(), 0);
237 let approval_ua = approval.userids().next().unwrap();
238 assert_eq!(approval_ua.approvals().count(), 2);
239 assert_eq!(approval_ua.with_policy(STANDARD_POLICY, None).unwrap()
240 .approved_certifications().count(), 1);
241
242 Ok(())
243 }
244
245 #[test]
246 fn ignore_shadow_ca() {
247 // Check that update ignores certificates made by shadow CAs.
248 let now = std::time::SystemTime::now()
249 - std::time::Duration::new(60 * 60, 0);
250
251 let mut sq = Sq::at(now);
252 let (alice, bob) = make_keys(&mut sq).unwrap();
253
254 // Have Bob certify Alice.
255 let alice2 = sq.pki_vouch_add(&[],
256 bob.key_handle(),
257 alice.key_handle(),
258 &[ALICE_USERID],
259 None);
260 assert_eq!(alice2.fingerprint(), alice.fingerprint());
261
262 let shadow_ca = artifact("keys/_sequoia_ca_keys.openpgp.org.pgp");
263 sq.key_import(&shadow_ca);
264 let shadow_ca = Cert::from_file(&shadow_ca).unwrap();
265
266 // Have the shadow CA certify Alice.
267 let alice2 = sq.pki_vouch_add(&[],
268 &shadow_ca.key_handle(),
269 alice.key_handle(),
270 &[ALICE_USERID],
271 None);
272 assert_eq!(alice2.fingerprint(), alice.fingerprint());
273
274 // Attest to all certifications. This should ignore the shadow
275 // CA's certification.
276 let approval = sq.key_approvals_update(
277 &["--add-all"], &alice.key_handle(), NO_USERIDS, None);
278
279 assert_eq!(approval.bad_signatures().count(), 0);
280 let approval_ua = approval.userids().next().unwrap();
281 // We have an approval key signature.
282 assert_eq!(approval_ua.approvals().count(), 1);
283 // With one approval (not two!).
284 assert_eq!(approval_ua.with_policy(STANDARD_POLICY, None).unwrap()
285 .approved_certifications().count(), 1);
286 }
287
288 #[test]
289 fn ignore_unexportable_certifications() {
290 // Check that update ignores certificates that are not exportable.
291 let now = std::time::SystemTime::now()
292 - std::time::Duration::new(60 * 60, 0);
293
294 let mut sq = Sq::at(now);
295 let (alice, bob) = make_keys(&mut sq).unwrap();
296
297 // Have Bob create a non-exportable certification for Alice.
298 let alice2 = sq.pki_vouch_add(&["--local"],
299 bob.key_handle(),
300 alice.key_handle(),
301 &[ALICE_USERID],
302 None);
303 assert_eq!(alice2.fingerprint(), alice.fingerprint());
304
305 // Attest to all certifications. This should ignore
306 // non-exportable certifications.
307 let approval = sq.key_approvals_update(
308 &["--add-all"], &alice.key_handle(), NO_USERIDS, None);
309
310 assert_eq!(approval.bad_signatures().count(), 0);
311 let approval_ua = approval.userids().next().unwrap();
312 for approval in approval_ua.approvals() {
313 eprintln!(" - {:?}", approval);
314 }
315 // We have an approval key signature.
316 assert_eq!(approval_ua.approvals().count(), 1);
317 // With zero approvals.
318 assert_eq!(approval_ua.with_policy(STANDARD_POLICY, None).unwrap()
319 .approved_certifications().count(), 0);
320 }
321
322 #[test]
323 fn userid_designators() {
324 let self_signed_email = "[email protected]";
325 let self_signed_userid
326 = &format!("Alice <{}>", self_signed_email);
327
328 let other_email = "[email protected]";
329 let other_userid = &format!("Alice <{}>", other_email);
330
331 let mut sq = Sq::new();
332
333 let (cert, cert_path, _rev_path)
334 = sq.key_generate(&[], &[ self_signed_userid ]);
335 sq.key_import(cert_path);
336
337 // Give Sequoia a little space to backdate the signatures. Note:
338 // It will not backdate them past the key creation time.
339 sq.tick(10);
340
341 // 1. --userid: use the specified self-signed user ID.
342 sq.key_approvals_update(
343 &["--add-all"], cert.key_handle(),
344 &[ UserIDArg::UserID(self_signed_userid) ], None);
345 assert!(sq.try_key_approvals_update(
346 &["--add-all"], cert.key_handle(),
347 &[ UserIDArg::UserID(other_userid) ], None).is_err());
348
349 // 2. --email: use the self-signed user ID with the specified
350 // email address.
351 sq.key_approvals_update(
352 &["--add-all"], cert.key_handle(),
353 &[ UserIDArg::Email(self_signed_email) ], None);
354 assert!(sq.try_key_approvals_update(
355 &["--add-all"], cert.key_handle(),
356 &[ UserIDArg::Email(other_email) ], None).is_err());
357 }