Debian Codegraph @

tests/integration/sq_pki_vouch_authorize.rs

raw
1 use super::common::NO_USERIDS;
2 use super::common::Sq;
3
4 #[test]
5 fn sq_pki_vouch_authorize_then_authenticate() {
6 let mut sq = Sq::new();
7
8 let otto_somewhere_com = "<[email protected]>";
9 let (otto, otto_pgp, _otto_rev)
10 = sq.key_generate(&[], &[otto_somewhere_com]);
11 sq.key_import(&otto_pgp);
12
13 let ca_example_org = "<[email protected]>";
14 let (ca, ca_pgp, _ca_rev)
15 = sq.key_generate(&[], &[ca_example_org]);
16 sq.key_import(&ca_pgp);
17
18 let alice_example_org = "<[email protected]>";
19 let (alice, alice_pgp, _alice_rev)
20 = sq.key_generate(&[], &[alice_example_org]);
21 sq.key_import(&alice_pgp);
22
23 let bob_example_org = "<[email protected]>";
24 let bob_other_org = "<[email protected]>";
25 let (bob, bob_pgp, _bob_rev)
26 = sq.key_generate(&[], &[bob_example_org, bob_other_org]);
27 sq.key_import(&bob_pgp);
28
29 sq.tick(1);
30
31 // The ca certifies alice's and bob's certificates for each of
32 // their user IDs.
33 let certification = sq.scratch_file(None);
34 sq.pki_vouch_add(
35 &[],
36 ca.key_handle(), alice.key_handle(),
37 &[ alice_example_org ],
38 certification.as_path());
39 sq.cert_import(&certification);
40
41 let certification = sq.scratch_file(None);
42 sq.pki_vouch_add(
43 &[],
44 ca.key_handle(), bob.key_handle(),
45 &[ bob_example_org, bob_other_org ],
46 certification.as_path());
47 sq.cert_import(certification);
48
49 // Check whether we can authenticate alice's and bob's
50 // certificates for their user ID using otto as the trust root.
51 let check = |sq: &Sq,
52 can_authenticate_alice,
53 can_authenticate_bob1,
54 can_authenticate_bob2|
55 {
56 for (cert, userid, can_authenticate) in &[
57 (&alice, alice_example_org, can_authenticate_alice),
58 (&bob, bob_example_org, can_authenticate_bob1),
59 (&bob, bob_other_org, can_authenticate_bob2),
60 ]
61 {
62 let r = sq.pki_authenticate(
63 &["--trust-root", &otto.fingerprint().to_string() ],
64 &cert.fingerprint().to_string(),
65 userid);
66
67 match (can_authenticate, r.is_ok()) {
68 (true, false) => {
69 panic!("Expected to authenticated {}, but didn't.",
70 userid);
71 }
72 (false, true) => {
73 panic!("Expected to NOT authenticated {}, but did.",
74 userid);
75 }
76 _ => (),
77 }
78 }
79 };
80
81 // No delegation yet.
82 println!("CA: not authorized");
83 check(
84 &sq,
85 false, // Alice.
86 false, // [email protected]
87 false);// [email protected]
88
89 // Otto authorizes completely to the CA.
90 let certification = sq.scratch_file(None);
91 sq.tick(1);
92 sq.pki_vouch_authorize(&["--unconstrained", "--all"],
93 otto.key_handle(), ca.key_handle(),
94 NO_USERIDS,
95 certification.as_path());
96 sq.cert_import(certification);
97
98 println!("CA: authorized, and unconstrained");
99 check(
100 &sq,
101 true, // [email protected]
102 true, // [email protected]
103 true);// [email protected]
104
105 // Otto authorizes to the CA with the regex contraint "example".
106 let certification = sq.scratch_file(None);
107 sq.tick(1);
108 sq.pki_vouch_authorize(&["--regex", "example", "--all"],
109 otto.key_handle(), ca.key_handle(),
110 NO_USERIDS,
111 certification.as_path());
112 sq.cert_import(certification);
113
114 println!("CA: authorized for \"example\"");
115 check(
116 &sq,
117 true, // [email protected]
118 true, // [email protected]
119 false);// [email protected]
120
121 // Otto authorizes to the CA with the domain contraint "example.org".
122 let certification = sq.scratch_file(None);
123 sq.tick(1);
124 sq.pki_vouch_authorize(&["--domain", "example.org", "--all"],
125 otto.key_handle(), ca.key_handle(),
126 NO_USERIDS,
127 certification.as_path());
128 sq.cert_import(certification);
129
130 println!("CA: authorized for \"example.org\"");
131 check(
132 &sq,
133 true, // [email protected]
134 true, // [email protected]
135 false);// [email protected]
136
137 // Otto authorizes to the CA with the regex contraint "other".
138 let certification = sq.scratch_file(None);
139 sq.tick(1);
140 sq.pki_vouch_authorize(&["--regex", "other", "--all"],
141 otto.key_handle(), ca.key_handle(),
142 NO_USERIDS,
143 certification.as_path());
144 sq.cert_import(certification);
145
146 println!("CA: authorized for \"other\"");
147 check(
148 &sq,
149 false, // [email protected]
150 false, // [email protected]
151 true); // [email protected]
152
153 // Otto authorizes to the CA with the regex contraint "bob".
154 let certification = sq.scratch_file(None);
155 sq.tick(1);
156 sq.pki_vouch_authorize(&["--regex", "bob", "--all"],
157 otto.key_handle(), ca.key_handle(),
158 NO_USERIDS,
159 certification.as_path());
160 sq.cert_import(certification);
161
162 println!("CA: authorized for \"bob\"");
163 check(
164 &sq,
165 false, // [email protected]
166 true, // [email protected]
167 true); // [email protected]
168
169 // Otto authorizes to the CA with the regex contraint "bob" or "alice".
170 let certification = sq.scratch_file(None);
171 sq.tick(1);
172 sq.pki_vouch_authorize(&["--regex", "bob", "--regex", "alice", "--all"],
173 otto.key_handle(), ca.key_handle(),
174 NO_USERIDS,
175 certification.as_path());
176 sq.cert_import(certification);
177
178 println!("CA: authorized for \"bob\" or \"alice\"");
179 check(
180 &sq,
181 true, // [email protected]
182 true, // [email protected]
183 true);// [email protected]
184
185
186 // Otto authorizes to the CA for the domains example.org and other.org.
187 let certification = sq.scratch_file(None);
188 sq.tick(1);
189 sq.pki_vouch_authorize(
190 &["--domain", "example.org", "--domain", "other.org", "--all"],
191 otto.key_handle(), ca.key_handle(),
192 NO_USERIDS,
193 certification.as_path());
194 sq.cert_import(certification);
195
196 println!("CA: authorized for the domains example.org and other.org");
197 check(
198 &sq,
199 true, // [email protected]
200 true, // [email protected]
201 true);// [email protected]
202
203
204 // Otto authorizes to the CA for the domain example.com and the regex alice.
205 let certification = sq.scratch_file(None);
206 sq.tick(1);
207 sq.pki_vouch_authorize(
208 &["--domain", "other.org", "--regex", "alice", "--all"],
209 otto.key_handle(), ca.key_handle(),
210 NO_USERIDS,
211 certification.as_path());
212 sq.cert_import(certification);
213
214 println!("CA: authorized for the domains example.org and other.org");
215 check(
216 &sq,
217 true, // [email protected]
218 false, // [email protected]
219 true); // [email protected]
220
221
222 // Otto authorizes to the CA with the regex contraint "zoo".
223 let certification = sq.scratch_file(None);
224 sq.tick(1);
225 sq.pki_vouch_authorize(&["--regex", "zoo", "--all"],
226 otto.key_handle(), ca.key_handle(),
227 NO_USERIDS,
228 certification.as_path());
229 sq.cert_import(certification);
230
231 println!("CA: authorized for \"zoo\"");
232 check(
233 &sq,
234 false, // [email protected]
235 false, // [email protected]
236 false);// [email protected]
237
238 // Otto authorizes to the CA with the domain contraint "example".
239 let certification = sq.scratch_file(None);
240 sq.tick(1);
241 sq.pki_vouch_authorize(&["--domain", "example", "--all"],
242 otto.key_handle(), ca.key_handle(),
243 NO_USERIDS,
244 certification.as_path());
245 sq.cert_import(certification);
246
247 println!("CA: authorized for \"zoo\"");
248 check(
249 &sq,
250 false, // [email protected]
251 false, // [email protected]
252 false);// [email protected]
253 }
254
255 #[test]
256 fn sq_pki_vouch_authorize_all_revoked() {
257 // When we don't provide any user IDs, `sq pki vouch authorize`
258 // certifies all of the self signed user IDs. Make sure this
259 // works in the presence of a revoked user ID, which should be
260 // ignored.
261
262 let mut sq = Sq::new();
263
264 let otto_somewhere_com = "<[email protected]>";
265 let (otto, otto_pgp, _otto_rev)
266 = sq.key_generate(&[], &[otto_somewhere_com]);
267 sq.key_import(&otto_pgp);
268
269 let ca_example_org = "<[email protected]>";
270 let ca_example_com = "<[email protected]>";
271 let (ca, ca_pgp, _ca_rev)
272 = sq.key_generate(&[], &[ca_example_org, ca_example_com]);
273 sq.key_import(&ca_pgp);
274
275 // Revoke [email protected].
276 sq.tick(1);
277 let revocation = sq.scratch_file("revocation");
278 sq.key_userid_revoke(&[], ca.fingerprint(), ca_example_com,
279 "retired", "bye", Some(revocation.as_path()));
280 sq.cert_import(&revocation);
281
282 let alice_example_org = "<[email protected]>";
283 let (alice, alice_pgp, _alice_rev)
284 = sq.key_generate(&[], &[alice_example_org]);
285 sq.key_import(&alice_pgp);
286
287 sq.tick(1);
288
289 // The ca certifies alice's certificate.
290 let certification = sq.scratch_file(None);
291 sq.pki_vouch_add(
292 &[],
293 ca.key_handle(), alice.key_handle(),
294 &[ alice_example_org ],
295 certification.as_path());
296 sq.cert_import(&certification);
297
298 // Check whether we can authenticate alice's and bob's
299 // certificates for their user ID using otto as the trust root.
300 let check = |sq: &Sq, can_authenticate|
301 {
302 let r = sq.pki_authenticate(
303 &["--trust-root", &otto.fingerprint().to_string() ],
304 &alice.fingerprint().to_string(),
305 alice_example_org);
306
307 match (can_authenticate, r.is_ok()) {
308 (true, false) => {
309 panic!("Expected to authenticated {}, but didn't.",
310 alice_example_org);
311 }
312 (false, true) => {
313 panic!("Expected to NOT authenticated {}, but did.",
314 alice_example_org);
315 }
316 _ => (),
317 }
318 };
319
320 // No delegation yet.
321 println!("CA: not authorized");
322 check(&sq, false);
323
324 // Otto completely authorizes the CA. Note: we don't specify any
325 // user IDs so only valid self-signed user IDs should be used.
326 // That means the revoked user ID should be skipped.
327 let certification = sq.scratch_file(None);
328 sq.tick(1);
329 sq.pki_vouch_authorize(&["--unconstrained", "--all"],
330 otto.key_handle(), ca.key_handle(),
331 NO_USERIDS,
332 certification.as_path());
333 sq.cert_import(certification);
334
335 println!("CA: authorized, and unconstrained");
336 check(&sq, true);
337 }