| 1 |
use std::collections::BTreeSet; |
| 2 |
use std::path::{Path, PathBuf}; |
| 3 |
use std::process::ExitStatus; |
| 4 |
use std::sync::{Mutex, OnceLock}; |
| 5 |
|
| 6 |
use tempfile::TempDir; |
| 7 |
|
| 8 |
use sequoia_openpgp as openpgp; |
| 9 |
use openpgp::KeyHandle; |
| 10 |
use openpgp::Result; |
| 11 |
use openpgp::Cert; |
| 12 |
use openpgp::parse::Parse; |
| 13 |
|
| 14 |
use super::common::FileOrKeyHandle; |
| 15 |
use super::common::NO_USERIDS; |
| 16 |
use super::common::Sq; |
| 17 |
use super::common::STANDARD_POLICY; |
| 18 |
use super::common::UserIDArg; |
| 19 |
use super::common::artifact; |
| 20 |
|
| 21 |
// We are going to replace certifications, and we want to make sure |
| 22 |
// that the newest one is the active one. This means ensuring that |
| 23 |
// the newer one has a newer timestamp. To avoid sleeping for a |
| 24 |
// second, the resolution of the time stamp, we pass an explicit time |
| 25 |
// to each operation. |
| 26 |
// |
| 27 |
// This function drives the clock forward, and ensures that every |
| 28 |
// operation "happens" at a different point in time. |
| 29 |
static TIME: OnceLock<Mutex<chrono::DateTime<chrono::Utc>>> = OnceLock::new(); |
| 30 |
|
| 31 |
fn tick() -> String { |
| 32 |
let t = TIME.get_or_init(|| Mutex::new(chrono::Utc::now())); |
| 33 |
let mut t = t.lock().unwrap(); |
| 34 |
*t = *t + chrono::Duration::seconds(10); |
| 35 |
t.format("%Y-%m-%dT%H:%M:%SZ").to_string() |
| 36 |
} |
| 37 |
|
| 38 |
// Returns the "current" time. |
| 39 |
fn now() -> chrono::DateTime<chrono::Utc> { |
| 40 |
*TIME.get_or_init(|| Mutex::new(chrono::Utc::now())).lock().unwrap() |
| 41 |
} |
| 42 |
|
| 43 |
// Verifies a signed message. |
| 44 |
fn sq_verify(sq: &Sq, |
| 45 |
time: Option<chrono::DateTime<chrono::Utc>>, |
| 46 |
trust_roots: &[&str], |
| 47 |
signer_files: &[&str], |
| 48 |
msg_pgp: &str, |
| 49 |
authenticated_sigs: usize, unauthenticated_sigs: usize) |
| 50 |
{ |
| 51 |
let mut cmd = sq.command(); |
| 52 |
for trust_root in trust_roots { |
| 53 |
cmd.args(&["--trust-root", trust_root]); |
| 54 |
} |
| 55 |
let time = if let Some(time) = time { |
| 56 |
time.format("%Y-%m-%dT%H:%M:%SZ").to_string() |
| 57 |
} else { |
| 58 |
tick() |
| 59 |
}; |
| 60 |
cmd.args(["verify", "--message", "--time", &time]); |
| 61 |
for signer_file in signer_files { |
| 62 |
cmd.args(&["--signer-file", signer_file]); |
| 63 |
} |
| 64 |
cmd.arg(msg_pgp); |
| 65 |
eprintln!("{:?}", cmd); |
| 66 |
let output = sq.run(cmd, None); |
| 67 |
|
| 68 |
let status = output.status; |
| 69 |
let stdout = String::from_utf8_lossy(&output.stdout).to_string(); |
| 70 |
let stderr = String::from_utf8_lossy(&output.stderr).to_string(); |
| 71 |
|
| 72 |
if authenticated_sigs > 0 { |
| 73 |
assert!(status.success(), |
| 74 |
"\nstdout:\n{}\nstderr:\n{}", |
| 75 |
stdout, stderr); |
| 76 |
assert!(stderr.contains(&format!("{} authenticated signature", |
| 77 |
authenticated_sigs)), |
| 78 |
"stdout:\n{}\nstderr:\n{}", |
| 79 |
stdout, stderr); |
| 80 |
} else { |
| 81 |
assert!(! status.success(), |
| 82 |
"\nstdout:\n{}\nstderr:\n{}", |
| 83 |
stdout, stderr); |
| 84 |
} |
| 85 |
|
| 86 |
if unauthenticated_sigs > 0 { |
| 87 |
assert!(stderr.contains(&format!("{} unauthenticated signature", |
| 88 |
unauthenticated_sigs)), |
| 89 |
"stdout:\n{}\nstderr:\n{}", stdout, stderr); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
// Links a User ID and a certificate. |
| 94 |
fn sq_link(sq: &Sq, |
| 95 |
cert: &str, userids: &[&str], emails: &[&str], more_args: &[&str], |
| 96 |
success: bool) |
| 97 |
-> (ExitStatus, String, String) |
| 98 |
{ |
| 99 |
let mut cmd = sq.command(); |
| 100 |
cmd.args(&["pki", "link", "add", "--time", &tick()]); |
| 101 |
cmd.arg("--cert").arg(cert); |
| 102 |
for userid in userids { |
| 103 |
cmd.arg("--userid").arg(userid); |
| 104 |
} |
| 105 |
for email in emails { |
| 106 |
cmd.arg("--email").arg(email); |
| 107 |
} |
| 108 |
cmd.args(more_args); |
| 109 |
let output = sq.run(cmd, None); |
| 110 |
|
| 111 |
let stdout = String::from_utf8_lossy(&output.stdout).to_string(); |
| 112 |
let stderr = String::from_utf8_lossy(&output.stderr).to_string(); |
| 113 |
|
| 114 |
if success { |
| 115 |
assert!(output.status.success(), |
| 116 |
"'sq pki link add' failed unexpectedly\ |
| 117 |
\nstdout:\n{}\nstderr:\n{}", |
| 118 |
stdout, stderr); |
| 119 |
} else { |
| 120 |
assert!(! output.status.success(), |
| 121 |
"'sq pki link add' succeeded unexpectedly\ |
| 122 |
\nstdout:\n{}\nstderr:\n{}", |
| 123 |
stdout, stderr); |
| 124 |
} |
| 125 |
|
| 126 |
(output.status, stdout, stderr) |
| 127 |
} |
| 128 |
|
| 129 |
fn sq_retract(sq: &Sq, cert: &str, userids: &[&str], emails: &[&str]) |
| 130 |
-> (ExitStatus, String, String) |
| 131 |
{ |
| 132 |
let mut cmd = sq.command(); |
| 133 |
cmd.args(&["pki", "link", "retract", "--time", &tick(), "--cert", cert]); |
| 134 |
for userid in userids { |
| 135 |
cmd.arg("--userid").arg(userid); |
| 136 |
} |
| 137 |
for email in emails { |
| 138 |
cmd.arg("--email").arg(email); |
| 139 |
} |
| 140 |
if userids.is_empty() && emails.is_empty() { |
| 141 |
cmd.arg("--all"); |
| 142 |
} |
| 143 |
eprintln!("{:?}", cmd); |
| 144 |
let output = sq.run(cmd, true); |
| 145 |
|
| 146 |
let stdout = String::from_utf8_lossy(&output.stdout).to_string(); |
| 147 |
let stderr = String::from_utf8_lossy(&output.stderr).to_string(); |
| 148 |
|
| 149 |
(output.status, stdout, stderr) |
| 150 |
} |
| 151 |
|
| 152 |
// Certifies a binding. |
| 153 |
// |
| 154 |
// The certification is imported into the cert store. |
| 155 |
fn sq_certify(sq: &Sq, |
| 156 |
certifier: &Path, cert: &str, userid: &str, |
| 157 |
trust_amount: Option<usize>) |
| 158 |
{ |
| 159 |
let mut extra_args = Vec::new(); |
| 160 |
let trust_amount_; |
| 161 |
if let Some(trust_amount) = trust_amount { |
| 162 |
extra_args.push("--amount"); |
| 163 |
trust_amount_ = format!("{}", trust_amount); |
| 164 |
extra_args.push(&trust_amount_); |
| 165 |
} |
| 166 |
|
| 167 |
let certification = sq.scratch_file(Some(&format!( |
| 168 |
"certification {} {} by {}", cert, userid, |
| 169 |
PathBuf::from(certifier.file_name().unwrap()).display())[..])); |
| 170 |
|
| 171 |
let cert = if let Ok(kh) = cert.parse::<KeyHandle>() { |
| 172 |
kh.into() |
| 173 |
} else { |
| 174 |
FileOrKeyHandle::FileOrStdin(cert.into()) |
| 175 |
}; |
| 176 |
|
| 177 |
sq.pki_vouch_add(&extra_args, certifier, cert, &[userid], |
| 178 |
Some(certification.as_path())); |
| 179 |
sq.cert_import(&certification); |
| 180 |
} |
| 181 |
|
| 182 |
fn sq_authorize(sq: &Sq, |
| 183 |
certifier: &Path, cert: &str, userid: &str, |
| 184 |
trust_amount: Option<usize>, depth: Option<usize>) |
| 185 |
{ |
| 186 |
let mut extra_args = vec![ "--unconstrained" ]; |
| 187 |
let trust_amount_; |
| 188 |
if let Some(trust_amount) = trust_amount { |
| 189 |
extra_args.push("--amount"); |
| 190 |
trust_amount_ = format!("{}", trust_amount); |
| 191 |
extra_args.push(&trust_amount_); |
| 192 |
} |
| 193 |
let depth_; |
| 194 |
if let Some(depth) = depth { |
| 195 |
extra_args.push("--depth"); |
| 196 |
depth_ = format!("{}", depth); |
| 197 |
extra_args.push(&depth_); |
| 198 |
} |
| 199 |
|
| 200 |
let certification = sq.scratch_file(Some(&format!( |
| 201 |
"certification {} {} by {}", cert, userid, |
| 202 |
PathBuf::from(certifier.file_name().unwrap()).display())[..])); |
| 203 |
|
| 204 |
let cert = if let Ok(kh) = cert.parse::<KeyHandle>() { |
| 205 |
kh.into() |
| 206 |
} else { |
| 207 |
FileOrKeyHandle::FileOrStdin(cert.into()) |
| 208 |
}; |
| 209 |
|
| 210 |
sq.pki_vouch_authorize(&extra_args, certifier, cert, &[userid], |
| 211 |
Some(certification.as_path())); |
| 212 |
sq.cert_import(&certification); |
| 213 |
} |
| 214 |
|
| 215 |
// Verify signatures using the acceptance machinery. |
| 216 |
#[test] |
| 217 |
fn sq_pki_link_add_retract() -> Result<()> { |
| 218 |
let sq = Sq::new(); |
| 219 |
let dir = TempDir::new()?; |
| 220 |
|
| 221 |
let certd = dir.path().join("cert.d").display().to_string(); |
| 222 |
std::fs::create_dir(&certd).expect("mkdir works"); |
| 223 |
|
| 224 |
struct Data { |
| 225 |
key_file: PathBuf, |
| 226 |
cert: Cert, // unused |
| 227 |
sig_file: String, |
| 228 |
} |
| 229 |
|
| 230 |
// Four certificates. |
| 231 |
let alice_userid = "<[email protected]>"; |
| 232 |
let (alice, alice_pgp, _) = sq.key_generate(&[], &[alice_userid]); |
| 233 |
sq.cert_import(&alice_pgp); |
| 234 |
let alice = Data { |
| 235 |
key_file: alice_pgp, |
| 236 |
cert: alice, |
| 237 |
sig_file: dir.path().join("alice.sig").display().to_string(), |
| 238 |
}; |
| 239 |
let alice_fpr = alice.cert.fingerprint().to_string(); |
| 240 |
|
| 241 |
let bob_userid = "<[email protected]>"; |
| 242 |
let (bob, bob_pgp, _) = sq.key_generate(&[], &[bob_userid]); |
| 243 |
sq.cert_import(&bob_pgp); |
| 244 |
let bob = Data { |
| 245 |
key_file: bob_pgp, |
| 246 |
cert: bob, |
| 247 |
sig_file: dir.path().join("bob.sig").display().to_string(), |
| 248 |
}; |
| 249 |
let bob_fpr = bob.cert.fingerprint().to_string(); |
| 250 |
|
| 251 |
let carol_userid = "<[email protected]>"; |
| 252 |
let (carol, carol_pgp, _) = sq.key_generate(&[], &[carol_userid]); |
| 253 |
sq.cert_import(&carol_pgp); |
| 254 |
let carol = Data { |
| 255 |
key_file: carol_pgp, |
| 256 |
cert: carol, |
| 257 |
sig_file: dir.path().join("carol.sig").display().to_string(), |
| 258 |
}; |
| 259 |
let carol_fpr = carol.cert.fingerprint().to_string(); |
| 260 |
|
| 261 |
let dave_userid = "<[email protected]>"; |
| 262 |
let (dave, dave_pgp, _) = sq.key_generate(&[], &[dave_userid]); |
| 263 |
sq.cert_import(&dave_pgp); |
| 264 |
let dave = Data { |
| 265 |
key_file: dave_pgp, |
| 266 |
cert: dave, |
| 267 |
sig_file: dir.path().join("dave.sig").display().to_string(), |
| 268 |
}; |
| 269 |
let dave_fpr = dave.cert.fingerprint().to_string(); |
| 270 |
|
| 271 |
let data: &[&Data] = &[ &alice, &bob, &carol, &dave ][..]; |
| 272 |
|
| 273 |
// Have each certificate sign a message. |
| 274 |
for data in data.iter() { |
| 275 |
sq.sign_args( |
| 276 |
&["--time", &tick()], |
| 277 |
&data.key_file, None, |
| 278 |
&artifact("messages/a-cypherpunks-manifesto.txt"), |
| 279 |
PathBuf::from(data.sig_file.clone()).as_path()); |
| 280 |
} |
| 281 |
|
| 282 |
// None of the certificates can be authenticated so verifying the |
| 283 |
// messages should fail. |
| 284 |
for data in data.iter() { |
| 285 |
sq_verify(&sq, None, &[], &[], &data.sig_file, 0, 1); |
| 286 |
} |
| 287 |
|
| 288 |
// Have Alice certify Bob as a trusted introducer and have Bob |
| 289 |
// certify Carol. |
| 290 |
sq_authorize(&sq, &alice.key_file, |
| 291 |
&bob.cert.fingerprint().to_string(), bob_userid, |
| 292 |
None, Some(1)); |
| 293 |
sq_certify(&sq, &bob.key_file, |
| 294 |
&carol.cert.fingerprint().to_string(), carol_userid, |
| 295 |
None); |
| 296 |
|
| 297 |
// We should be able to verify Alice's, Bob's and Carol's |
| 298 |
// signatures Alice as the trust root. And Bob's and Carols' with |
| 299 |
// Bob as the trust root. |
| 300 |
|
| 301 |
sq_verify(&sq, None, &[&alice_fpr], &[], &alice.sig_file, 1, 0); |
| 302 |
sq_verify(&sq, None, &[&alice_fpr], &[], &bob.sig_file, 1, 0); |
| 303 |
sq_verify(&sq, None, &[&alice_fpr], &[], &carol.sig_file, 1, 0); |
| 304 |
sq_verify(&sq, None, &[&alice_fpr], &[], &dave.sig_file, 0, 1); |
| 305 |
|
| 306 |
sq_verify(&sq, None, &[&bob_fpr], &[], &alice.sig_file, 0, 1); |
| 307 |
sq_verify(&sq, None, &[&bob_fpr], &[], &bob.sig_file, 1, 0); |
| 308 |
sq_verify(&sq, None, &[&bob_fpr], &[], &carol.sig_file, 1, 0); |
| 309 |
sq_verify(&sq, None, &[&bob_fpr], &[], &dave.sig_file, 0, 1); |
| 310 |
|
| 311 |
sq_verify(&sq, None, &[&carol_fpr], &[], &alice.sig_file, 0, 1); |
| 312 |
sq_verify(&sq, None, &[&carol_fpr], &[], &bob.sig_file, 0, 1); |
| 313 |
sq_verify(&sq, None, &[&carol_fpr], &[], &carol.sig_file, 1, 0); |
| 314 |
sq_verify(&sq, None, &[&carol_fpr], &[], &dave.sig_file, 0, 1); |
| 315 |
|
| 316 |
sq_verify(&sq, None, &[&dave_fpr], &[], &alice.sig_file, 0, 1); |
| 317 |
sq_verify(&sq, None, &[&dave_fpr], &[], &bob.sig_file, 0, 1); |
| 318 |
sq_verify(&sq, None, &[&dave_fpr], &[], &carol.sig_file, 0, 1); |
| 319 |
sq_verify(&sq, None, &[&dave_fpr], &[], &dave.sig_file, 1, 0); |
| 320 |
|
| 321 |
// Let's accept Alice, but not (yet) as a trusted introducer. We |
| 322 |
// should now be able to verify Alice's signature, but not Bob's. |
| 323 |
sq_link(&sq, &alice_fpr, &[ &alice_userid ], &[], &[], true); |
| 324 |
|
| 325 |
sq_verify(&sq, None, &[], &[], &alice.sig_file, 1, 0); |
| 326 |
sq_verify(&sq, None, &[], &[], &bob.sig_file, 0, 1); |
| 327 |
|
| 328 |
// Accept Alice as a trusted introducer. We should be able to |
| 329 |
// verify Alice, Bob, and Carol's signatures. |
| 330 |
sq.pki_link_authorize(&["--time", &tick(), "--unconstrained"], |
| 331 |
alice.cert.key_handle(), |
| 332 |
&[ &alice_userid ]); |
| 333 |
|
| 334 |
sq_verify(&sq, None, &[], &[], &alice.sig_file, 1, 0); |
| 335 |
sq_verify(&sq, None, &[], &[], &bob.sig_file, 1, 0); |
| 336 |
sq_verify(&sq, None, &[], &[], &carol.sig_file, 1, 0); |
| 337 |
sq_verify(&sq, None, &[], &[], &dave.sig_file, 0, 1); |
| 338 |
|
| 339 |
// Retract the acceptance for Alice. If we don't specify a trust |
| 340 |
// root, none of the signatures should verify. |
| 341 |
sq_retract(&sq, &alice_fpr, &[ &alice_userid ], &[]); |
| 342 |
|
| 343 |
for data in data.iter() { |
| 344 |
sq_verify(&sq, None, &[], &[], &data.sig_file, 0, 1); |
| 345 |
} |
| 346 |
|
| 347 |
// Accept Alice as a trusted introducer again. We should be able |
| 348 |
// to verify Alice, Bob, and Carol's signatures. |
| 349 |
sq.pki_link_authorize(&["--time", &tick(), "--unconstrained"], |
| 350 |
alice.cert.key_handle(), |
| 351 |
&[ &alice_userid ]); |
| 352 |
|
| 353 |
sq_verify(&sq, None, &[], &[], &alice.sig_file, 1, 0); |
| 354 |
sq_verify(&sq, None, &[], &[], &bob.sig_file, 1, 0); |
| 355 |
sq_verify(&sq, None, &[], &[], &carol.sig_file, 1, 0); |
| 356 |
sq_verify(&sq, None, &[], &[], &dave.sig_file, 0, 1); |
| 357 |
|
| 358 |
// Have Bob certify Dave. Now Dave's signature should also |
| 359 |
// verify. |
| 360 |
sq_certify(&sq, &bob.key_file, |
| 361 |
&dave.cert.fingerprint().to_string(), dave_userid, |
| 362 |
None); |
| 363 |
|
| 364 |
sq_verify(&sq, None, &[], &[], &alice.sig_file, 1, 0); |
| 365 |
sq_verify(&sq, None, &[], &[], &bob.sig_file, 1, 0); |
| 366 |
sq_verify(&sq, None, &[], &[], &carol.sig_file, 1, 0); |
| 367 |
sq_verify(&sq, None, &[], &[], &dave.sig_file, 1, 0); |
| 368 |
|
| 369 |
// Change Alice's acceptance to just be a normal certification. |
| 370 |
// We should only be able to verify her signature. |
| 371 |
sq_link(&sq, &alice_fpr, &[ &alice_userid ], &[], &[], true); |
| 372 |
|
| 373 |
sq_verify(&sq, None, &[], &[], &alice.sig_file, 1, 0); |
| 374 |
sq_verify(&sq, None, &[], &[], &bob.sig_file, 0, 1); |
| 375 |
sq_verify(&sq, None, &[], &[], &carol.sig_file, 0, 1); |
| 376 |
sq_verify(&sq, None, &[], &[], &dave.sig_file, 0, 1); |
| 377 |
|
| 378 |
// Change Alice's acceptance to be a ca, but only for example.org, |
| 379 |
// i.e., not for Dave. |
| 380 |
sq.pki_link_authorize(&["--time", &tick(), "--domain", "example.org"], |
| 381 |
alice.cert.key_handle(), |
| 382 |
&[ &alice_userid ]); |
| 383 |
|
| 384 |
sq_verify(&sq, None, &[], &[], &alice.sig_file, 1, 0); |
| 385 |
sq_verify(&sq, None, &[], &[], &bob.sig_file, 1, 0); |
| 386 |
sq_verify(&sq, None, &[], &[], &carol.sig_file, 1, 0); |
| 387 |
sq_verify(&sq, None, &[], &[], &dave.sig_file, 0, 1); |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
// A fifth certificate. |
| 392 |
let (ed, ed_pgp, _) = sq.key_generate( |
| 393 |
&["--allow-non-canonical-userids"], |
| 394 |
&[ |
| 395 |
"Ed <[email protected]>", |
| 396 |
"Eddie <[email protected]>", |
| 397 |
// This is not considered to be an email address as |
| 398 |
// it is not wrapped in angle brackets. |
| 399 |
"[email protected]", |
| 400 |
// But this is. |
| 401 |
"<[email protected]>", |
| 402 |
]); |
| 403 |
sq.cert_import(&ed_pgp); |
| 404 |
let ed_fpr = ed.fingerprint().to_string(); |
| 405 |
let ed_sig_file = dir.path().join("ed.sig"); |
| 406 |
|
| 407 |
sq.sign_args( |
| 408 |
&["--time", &tick()], |
| 409 |
&ed_pgp, None, |
| 410 |
&artifact("messages/a-cypherpunks-manifesto.txt"), |
| 411 |
ed_sig_file.as_path()); |
| 412 |
|
| 413 |
// If we don't use --petname, than a self-signed User ID must |
| 414 |
// exist. |
| 415 |
sq_link(&sq, &ed_fpr, &[ "[email protected]" ], &[], &[], false); |
| 416 |
let ed_sig_file = ed_sig_file.display().to_string(); |
| 417 |
sq_verify(&sq, None, &[], &[], &ed_sig_file, 0, 1); |
| 418 |
|
| 419 |
sq_link(&sq, &ed_fpr, &[], &[ "[email protected]" ], &[], false); |
| 420 |
sq_verify(&sq, None, &[], &[], &ed_sig_file, 0, 1); |
| 421 |
|
| 422 |
// We should only create links if all the supplied User IDs are |
| 423 |
// valid. |
| 424 |
sq_link(&sq, &ed_fpr, &["[email protected]", "[email protected]"], &[], &[], false); |
| 425 |
sq_verify(&sq, None, &[], &[], &ed_sig_file, 0, 1); |
| 426 |
|
| 427 |
sq_link(&sq, &ed_fpr, &["[email protected]"], &["[email protected]"], &[], false); |
| 428 |
sq_verify(&sq, None, &[], &[], &ed_sig_file, 0, 1); |
| 429 |
|
| 430 |
// Pass an email address to --userid. This shouldn't match |
| 431 |
// either. |
| 432 |
sq_link(&sq, &ed_fpr, &["[email protected]"], &[], &[], false); |
| 433 |
sq_verify(&sq, None, &[], &[], &ed_sig_file, 0, 1); |
| 434 |
|
| 435 |
// Link all User IDs individually. |
| 436 |
sq_link(&sq, &ed_fpr, |
| 437 |
&["[email protected]", "Ed <[email protected]>", "Eddie <[email protected]>"], |
| 438 |
&["[email protected]"], |
| 439 |
&[], true); |
| 440 |
sq_verify(&sq, None, &[], &[], &ed_sig_file, 1, 0); |
| 441 |
|
| 442 |
// Retract the links one at a time. |
| 443 |
sq_retract(&sq, &ed_fpr, &[], &[ "[email protected]" ]); |
| 444 |
sq_verify(&sq, None, &[], &[], &ed_sig_file, 1, 0); |
| 445 |
|
| 446 |
sq_retract(&sq, &ed_fpr, &[ "Ed <[email protected]>" ], &[]); |
| 447 |
sq_verify(&sq, None, &[], &[], &ed_sig_file, 1, 0); |
| 448 |
|
| 449 |
sq_retract(&sq, &ed_fpr, &[ "Eddie <[email protected]>" ], &[]); |
| 450 |
sq_verify(&sq, None, &[], &[], &ed_sig_file, 1, 0); |
| 451 |
|
| 452 |
sq_retract(&sq, &ed_fpr, &[ "[email protected]" ], &[]); |
| 453 |
// Now the certificate should no longer be authenticated. |
| 454 |
sq_verify(&sq, None, &[], &[], &ed_sig_file, 0, 1); |
| 455 |
|
| 456 |
Ok(()) |
| 457 |
} |
| 458 |
|
| 459 |
// Set the different parameters. When the parameters are the same, |
| 460 |
// make sure no certifications are written; when they are different |
| 461 |
// make sure the file changed. |
| 462 |
#[test] |
| 463 |
fn sq_pki_link_update_detection() -> Result<()> { |
| 464 |
let sq = Sq::new(); |
| 465 |
|
| 466 |
let alice_userid = "<[email protected]>"; |
| 467 |
let (alice, alice_pgp, _) = sq.key_generate(&[], &[alice_userid]); |
| 468 |
sq.cert_import(&alice_pgp); |
| 469 |
let alice_fpr = alice.fingerprint().to_string(); |
| 470 |
let alice_cert_pgp = sq.certd() |
| 471 |
.join(&alice_fpr[0..2].to_ascii_lowercase()) |
| 472 |
.join(&alice_fpr[2..].to_ascii_lowercase()); |
| 473 |
|
| 474 |
// Reads and returns file. Asserts that old and the new contexts |
| 475 |
// are the same (or not). |
| 476 |
let compare = |old: Vec<u8>, file: &Path, same: bool| -> Vec<u8> { |
| 477 |
let new = std::fs::read(file).unwrap(); |
| 478 |
if same { |
| 479 |
assert_eq!(old, new, "file unexpectedly changed"); |
| 480 |
} else { |
| 481 |
assert_ne!(old, new, "file unexpectedly stayed the same"); |
| 482 |
} |
| 483 |
new |
| 484 |
}; |
| 485 |
let bytes = std::fs::read(&alice_cert_pgp).unwrap(); |
| 486 |
|
| 487 |
// Retract it. There is nothing to retract (but this doesn't fail). |
| 488 |
let output = sq_retract(&sq, &alice_fpr, &[], &[]); |
| 489 |
assert!(output.1.contains("never certified"), |
| 490 |
"stdout:\n{}\nstderr:\n{}", output.1, output.2); |
| 491 |
let bytes = compare(bytes, &alice_cert_pgp, true); |
| 492 |
|
| 493 |
// Link it. |
| 494 |
sq_link(&sq, &alice_fpr, &[], &[], &["--all"], true); |
| 495 |
let bytes = compare(bytes, &alice_cert_pgp, false); |
| 496 |
|
| 497 |
// As no parameters changed, this should succeeded, but no |
| 498 |
// certification should be written. |
| 499 |
let output = sq_link(&sq, &alice_fpr, &[], &[], &["--all"], true); |
| 500 |
assert!(output.1.contains("certification parameters are unchanged"), |
| 501 |
"stdout:\n{}\nstderr:\n{}", output.1, output.2); |
| 502 |
let bytes = compare(bytes, &alice_cert_pgp, true); |
| 503 |
|
| 504 |
// Make Alice a CA. |
| 505 |
sq.pki_link_authorize(&["--time", &tick(), "--unconstrained", "--all"], |
| 506 |
alice.key_handle(), |
| 507 |
NO_USERIDS); |
| 508 |
let bytes = compare(bytes, &alice_cert_pgp, false); |
| 509 |
|
| 510 |
sq.pki_link_authorize(&["--time", &tick(), "--unconstrained", "--all"], |
| 511 |
alice.key_handle(), |
| 512 |
NO_USERIDS); |
| 513 |
let bytes = compare(bytes, &alice_cert_pgp, true); |
| 514 |
|
| 515 |
// Make her a partially trusted CA. |
| 516 |
let output = sq_link(&sq, &alice_fpr, &[], &[], |
| 517 |
&["--amount", "30", "--all"], true); |
| 518 |
assert!(output.1.contains("was previously"), |
| 519 |
"stdout:\n{}\nstderr:\n{}", output.1, output.2); |
| 520 |
let bytes = compare(bytes, &alice_cert_pgp, false); |
| 521 |
|
| 522 |
let output = sq_link(&sq, &alice_fpr, &[], &[], |
| 523 |
&["--amount", "30", "--all"], true); |
| 524 |
assert!(output.1.contains("certification parameters are unchanged"), |
| 525 |
"stdout:\n{}\nstderr:\n{}", output.1, output.2); |
| 526 |
let bytes = compare(bytes, &alice_cert_pgp, true); |
| 527 |
|
| 528 |
// Retract the link. |
| 529 |
let output = sq_retract(&sq, &alice_fpr, &[], &[]); |
| 530 |
assert!(output.1.contains("was previously"), |
| 531 |
"stdout:\n{}\nstderr:\n{}", output.1, output.2); |
| 532 |
let bytes = compare(bytes, &alice_cert_pgp, false); |
| 533 |
|
| 534 |
let output = sq_retract(&sq, &alice_fpr, &[], &[]); |
| 535 |
assert!(output.1.contains("certification parameters are unchanged"), |
| 536 |
"stdout:\n{}\nstderr:\n{}", output.1, output.2); |
| 537 |
let bytes = compare(bytes, &alice_cert_pgp, true); |
| 538 |
|
| 539 |
|
| 540 |
// Link it again. |
| 541 |
let output = sq_link(&sq, &alice_fpr, &[], &[], |
| 542 |
&["--amount", "10", "--all"], true); |
| 543 |
assert!(output.1.contains("was retracted"), |
| 544 |
"stdout:\n{}\nstderr:\n{}", output.1, output.2); |
| 545 |
let bytes = compare(bytes, &alice_cert_pgp, false); |
| 546 |
|
| 547 |
let output = sq_link(&sq, &alice_fpr, &[], &[], |
| 548 |
&["--amount", "10", "--all"], true); |
| 549 |
assert!(output.1.contains("certification parameters are unchanged"), |
| 550 |
"stdout:\n{}\nstderr:\n{}", output.1, output.2); |
| 551 |
let bytes = compare(bytes, &alice_cert_pgp, true); |
| 552 |
|
| 553 |
// Use a notation. |
| 554 |
let output = sq_link(&sq, &alice_fpr, &[], &[], |
| 555 |
&["--signature-notation", "foo", "10", "--all"], true); |
| 556 |
assert!(output.1.contains("was previously"), |
| 557 |
"stdout:\n{}\nstderr:\n{}", output.1, output.2); |
| 558 |
let bytes = compare(bytes, &alice_cert_pgp, false); |
| 559 |
|
| 560 |
let output = sq_link(&sq, &alice_fpr, &[], &[], |
| 561 |
&["--signature-notation", "foo", "10", "--all"], true); |
| 562 |
assert!(output.1.contains("certification parameters are unchanged"), |
| 563 |
"stdout:\n{}\nstderr:\n{}", output.1, output.2); |
| 564 |
let bytes = compare(bytes, &alice_cert_pgp, true); |
| 565 |
|
| 566 |
// The default link again. |
| 567 |
let output = sq_link(&sq, &alice_fpr, &[], &[], &["--all"], true); |
| 568 |
assert!(output.1.contains("was previously"), |
| 569 |
"stdout:\n{}\nstderr:\n{}", output.1, output.2); |
| 570 |
let bytes = compare(bytes, &alice_cert_pgp, false); |
| 571 |
|
| 572 |
let output = sq_link(&sq, &alice_fpr, &[], &[], &["--all"], true); |
| 573 |
assert!(output.1.contains("certification parameters are unchanged"), |
| 574 |
"stdout:\n{}\nstderr:\n{}", output.1, output.2); |
| 575 |
let bytes = compare(bytes, &alice_cert_pgp, true); |
| 576 |
|
| 577 |
let _ = bytes; |
| 578 |
Ok(()) |
| 579 |
} |
| 580 |
|
| 581 |
// Check that sq pki link add --temporary works. |
| 582 |
#[test] |
| 583 |
fn sq_pki_link_add_temporary() -> Result<()> { |
| 584 |
let sq = Sq::new(); |
| 585 |
|
| 586 |
let alice_userid = "<[email protected]>"; |
| 587 |
let (alice, alice_pgp, _) = sq.key_generate(&[], &[alice_userid]); |
| 588 |
sq.cert_import(&alice_pgp); |
| 589 |
let alice_fpr = alice.fingerprint().to_string(); |
| 590 |
let alice_cert_pgp = sq.certd() |
| 591 |
.join(&alice_fpr[0..2].to_ascii_lowercase()) |
| 592 |
.join(&alice_fpr[2..].to_ascii_lowercase()); |
| 593 |
|
| 594 |
let alice_sig_file = sq.base().join("alice.sig"); |
| 595 |
sq.sign_args( |
| 596 |
&["--time", &tick()], |
| 597 |
&alice_pgp, None, |
| 598 |
&artifact("messages/a-cypherpunks-manifesto.txt"), |
| 599 |
alice_sig_file.as_path()); |
| 600 |
|
| 601 |
// Reads and returns file. Asserts that old and the new contexts |
| 602 |
// are the same (or not). |
| 603 |
let compare = |old: Vec<u8>, file: &Path, same: bool| -> Vec<u8> { |
| 604 |
let new = std::fs::read(file).unwrap(); |
| 605 |
if same { |
| 606 |
assert_eq!(old, new, "file unexpectedly changed"); |
| 607 |
} else { |
| 608 |
assert_ne!(old, new, "file unexpectedly stayed the same"); |
| 609 |
} |
| 610 |
new |
| 611 |
}; |
| 612 |
let bytes = std::fs::read(&alice_cert_pgp).unwrap(); |
| 613 |
|
| 614 |
let alice_sig_file = alice_sig_file.display().to_string(); |
| 615 |
sq_verify(&sq, None, &[], &[], &alice_sig_file, 0, 1); |
| 616 |
|
| 617 |
let output = sq_link(&sq, &alice_fpr, &[], &[], &["--temporary", "--all"], true); |
| 618 |
assert!(output.1.contains("certification created"), |
| 619 |
"stdout:\n{}\nstderr:\n{}", output.1, output.2); |
| 620 |
let bytes = compare(bytes, &alice_cert_pgp, false); |
| 621 |
|
| 622 |
// Now it is fully trusted. |
| 623 |
sq_verify(&sq, None, &[], &[], &alice_sig_file, 1, 0); |
| 624 |
|
| 625 |
// In 6 days, too. |
| 626 |
sq_verify(&sq, |
| 627 |
Some(now() + chrono::Duration::seconds(6 * 24 * 60 * 60)), |
| 628 |
&[], &[], &alice_sig_file, 1, 0); |
| 629 |
|
| 630 |
// But in 8 days it will only be partially trusted. |
| 631 |
sq_verify(&sq, |
| 632 |
Some(now() + chrono::Duration::seconds(8 * 24 * 60 * 60)), |
| 633 |
&[], &[], &alice_sig_file, 0, 1); |
| 634 |
|
| 635 |
|
| 636 |
// Now mark it as fully trusted. It should be trusted now, in 6 |
| 637 |
// days and in 8 days. |
| 638 |
let output = sq_link(&sq, &alice_fpr, &[], &[], &["--all"], true); |
| 639 |
assert!(output.1.contains("was previously"), |
| 640 |
"stdout:\n{}\nstderr:\n{}", output.1, output.2); |
| 641 |
eprintln!("{:?}", output); |
| 642 |
let bytes = compare(bytes, &alice_cert_pgp, false); |
| 643 |
|
| 644 |
sq_verify(&sq, None, &[], &[], &alice_sig_file, 1, 0); |
| 645 |
|
| 646 |
sq_verify(&sq, |
| 647 |
Some(now() + chrono::Duration::seconds(6 * 24 * 60 * 60)), |
| 648 |
&[], &[], &alice_sig_file, 1, 0); |
| 649 |
|
| 650 |
sq_verify(&sq, |
| 651 |
Some(now() + chrono::Duration::seconds(8 * 24 * 60 * 60)), |
| 652 |
&[], &[], &alice_sig_file, 1, 0); |
| 653 |
|
| 654 |
let _bytes = bytes; |
| 655 |
|
| 656 |
Ok(()) |
| 657 |
} |
| 658 |
|
| 659 |
#[test] |
| 660 |
fn retract_non_self_signed() { |
| 661 |
// Make sure we can retract non-self signed user IDs. |
| 662 |
let mut sq = Sq::new(); |
| 663 |
|
| 664 |
let alice_userid = "Alice <[email protected]>"; |
| 665 |
let (alice, alice_pgp, _) = sq.key_generate(&[], &[alice_userid]); |
| 666 |
sq.key_import(&alice_pgp); |
| 667 |
|
| 668 |
let petname = "Mon chouchou"; |
| 669 |
|
| 670 |
let msg = artifact("messages/a-cypherpunks-manifesto.txt"); |
| 671 |
let sig_msg = sq.scratch_file(None); |
| 672 |
let sig_msg = sig_msg.as_path(); |
| 673 |
let sig_msg_str = sig_msg.display().to_string(); |
| 674 |
sq.sign(alice.key_handle(), None, &msg, sig_msg); |
| 675 |
|
| 676 |
// Verifying should fail: alice's certificate is not linked at all. |
| 677 |
sq_verify(&sq, None, &[], &[], &sig_msg_str, 0, 1); |
| 678 |
|
| 679 |
// Link a non-self-signed user ID. |
| 680 |
sq.tick(1); |
| 681 |
sq.pki_link_add(&[], alice.key_handle(), |
| 682 |
&[UserIDArg::AddUserID(petname)]); |
| 683 |
|
| 684 |
// Now it should work. |
| 685 |
sq_verify(&sq, None, &[], &[], &sig_msg_str, 1, 0); |
| 686 |
|
| 687 |
// Retract the link. |
| 688 |
sq.tick(1); |
| 689 |
sq_retract(&sq, &alice.fingerprint().to_string(), &[petname], &[]); |
| 690 |
|
| 691 |
// Now it should fail. |
| 692 |
sq_verify(&sq, None, &[], &[], &sig_msg_str, 0, 1); |
| 693 |
} |
| 694 |
|
| 695 |
#[test] |
| 696 |
fn retract_weak() { |
| 697 |
// Make sure we can retract signed user IDs whose binding |
| 698 |
// signatures rely on weak cryptography from a valid certificate. |
| 699 |
let sq = Sq::new(); |
| 700 |
|
| 701 |
let cert_path = sq.test_data() |
| 702 |
.join("keys") |
| 703 |
.join("sha1-userid-priv.pgp"); |
| 704 |
sq.key_import(&cert_path); |
| 705 |
|
| 706 |
let cert = Cert::from_file(&cert_path).expect("can read"); |
| 707 |
|
| 708 |
// Make sure the user ID is there and really uses SHA-1. |
| 709 |
let vc = cert.with_policy(STANDARD_POLICY, sq.now()) |
| 710 |
.expect("valid cert"); |
| 711 |
let valid_userids: BTreeSet<_> = vc.userids() |
| 712 |
.map(|ua| ua.userid()) |
| 713 |
.collect(); |
| 714 |
let all_userids: BTreeSet<_> = cert.userids() |
| 715 |
.map(|ua| ua.userid()) |
| 716 |
.collect(); |
| 717 |
|
| 718 |
assert!(valid_userids.len() < all_userids.len()); |
| 719 |
|
| 720 |
let weak_userids: Vec<_> |
| 721 |
= all_userids.difference(&valid_userids) |
| 722 |
.map(|u| { |
| 723 |
String::from_utf8_lossy(u.value()).to_string() |
| 724 |
}) |
| 725 |
.collect(); |
| 726 |
let weak_userids: Vec<&String> = weak_userids.iter().collect(); |
| 727 |
|
| 728 |
// The current policy doesn't allow SHA-1. |
| 729 |
assert!( |
| 730 |
sq.pki_link_add_maybe(&[], cert.key_handle(), &weak_userids) |
| 731 |
.is_err()); |
| 732 |
|
| 733 |
// But the policy as of 2003 did. |
| 734 |
sq.pki_link_add(&["--policy-as-of", "2003-01-01"], |
| 735 |
cert.key_handle(), &weak_userids); |
| 736 |
|
| 737 |
// Retract. |
| 738 |
sq.pki_link_retract(&[], cert.key_handle(), &weak_userids[..]); |
| 739 |
} |
| 740 |
|
| 741 |
#[test] |
| 742 |
fn retract_all() { |
| 743 |
// Link all self-signed user IDs and a non-self-signed user ID. |
| 744 |
// When we retract all, make sure they are all retracted. |
| 745 |
let mut sq = Sq::new(); |
| 746 |
|
| 747 |
let alice_userid = "Alice <[email protected]>"; |
| 748 |
let (alice, alice_pgp, _) = sq.key_generate(&[], &[alice_userid]); |
| 749 |
sq.key_import(&alice_pgp); |
| 750 |
|
| 751 |
let petname = "Mon chouchou"; |
| 752 |
|
| 753 |
let msg = artifact("messages/a-cypherpunks-manifesto.txt"); |
| 754 |
let sig_msg = sq.scratch_file(None); |
| 755 |
let sig_msg = sig_msg.as_path(); |
| 756 |
let sig_msg_str = sig_msg.display().to_string(); |
| 757 |
sq.sign(alice.key_handle(), None, &msg, sig_msg); |
| 758 |
|
| 759 |
// Verifying should fail: alice's certificate is not linked at all. |
| 760 |
sq_verify(&sq, None, &[], &[], &sig_msg_str, 0, 1); |
| 761 |
|
| 762 |
// Link a non-self-signed user ID. |
| 763 |
sq.tick(1); |
| 764 |
sq.pki_link_add(&[], alice.key_handle(), &[UserIDArg::AddUserID(petname)]); |
| 765 |
|
| 766 |
// Now it should work. |
| 767 |
sq_verify(&sq, None, &[], &[], &sig_msg_str, 1, 0); |
| 768 |
|
| 769 |
// Retract *all* links. |
| 770 |
sq.tick(1); |
| 771 |
sq_retract(&sq, &alice.fingerprint().to_string(), &[], &[]); |
| 772 |
|
| 773 |
// Now it should fail. |
| 774 |
sq_verify(&sq, None, &[], &[], &sig_msg_str, 0, 1); |
| 775 |
} |
| 776 |
|
| 777 |
#[test] |
| 778 |
fn no_ambiguous_email() { |
| 779 |
// Check that we can't address self-signed user IDs by an |
| 780 |
// ambiguous email address. |
| 781 |
let mut sq = Sq::new(); |
| 782 |
|
| 783 |
let alice_userids = &["Alice <[email protected]>", |
| 784 |
"Alice Lovelace <[email protected]>"][..]; |
| 785 |
let (alice, alice_pgp, _) = sq.key_generate(&[], alice_userids); |
| 786 |
sq.key_import(&alice_pgp); |
| 787 |
|
| 788 |
sq.tick(1); |
| 789 |
|
| 790 |
// --email links a user ID with the specified email address, if it |
| 791 |
// occurs in a self-signed user ID: Ambiguous is allowed. |
| 792 |
assert!( |
| 793 |
sq.pki_link_add_maybe( |
| 794 |
&[], alice.key_handle(), &[UserIDArg::Email("[email protected]")]) |
| 795 |
.is_ok()); |
| 796 |
// --userid-by-email links the matching self-signed user ID: |
| 797 |
// Ambiguous is not allowed. |
| 798 |
assert!( |
| 799 |
sq.pki_link_add_maybe( |
| 800 |
&[], alice.key_handle(), &[UserIDArg::ByEmail("[email protected]")]) |
| 801 |
.is_err()); |
| 802 |
// --add-email links a user ID with the email address: |
| 803 |
// Ambiguous is allowed. |
| 804 |
assert!( |
| 805 |
sq.pki_link_add_maybe( |
| 806 |
&[], alice.key_handle(), &[UserIDArg::AddEmail("[email protected]")]) |
| 807 |
.is_ok()); |
| 808 |
|
| 809 |
// Not a self-signed user ID. |
| 810 |
assert!( |
| 811 |
sq.pki_link_add_maybe( |
| 812 |
&[], alice.key_handle(), &[UserIDArg::UserID("[email protected]")]) |
| 813 |
.is_err()); |
| 814 |
|
| 815 |
// Fully qualified is okay. |
| 816 |
sq.pki_link_add( |
| 817 |
&[], alice.key_handle(), |
| 818 |
&[UserIDArg::UserID("Alice <[email protected]>")]); |
| 819 |
|
| 820 |
// As well as adding a user ID. |
| 821 |
sq.pki_link_add( |
| 822 |
&[], alice.key_handle(), |
| 823 |
&[UserIDArg::AddUserID("<[email protected]>")]); |
| 824 |
} |
| 825 |
|
| 826 |
#[test] |
| 827 |
fn special_names() { |
| 828 |
// Check that --cert-special works. |
| 829 |
let sq = Sq::new(); |
| 830 |
|
| 831 |
let check = |cmd: &str, args: &[&str], name: &str, success: bool| { |
| 832 |
let mut c = sq.command(); |
| 833 |
c.args([ "pki", "link", cmd, "--cert-special", name ]); |
| 834 |
c.args(args); |
| 835 |
sq.run(c, Some(success)); |
| 836 |
}; |
| 837 |
|
| 838 |
const SPECIAL_STRINGS: &'static [&'static str] = &[ |
| 839 |
"public-directories", |
| 840 |
"keys.openpgp.org", |
| 841 |
"keys.mailvelope.com", |
| 842 |
"proton.me", |
| 843 |
"wkd", |
| 844 |
"dane", |
| 845 |
"autocrypt", |
| 846 |
"web", |
| 847 |
]; |
| 848 |
|
| 849 |
for name in SPECIAL_STRINGS.iter() { |
| 850 |
check("add", &["--all"], name, true); |
| 851 |
} |
| 852 |
check("add", &["--all"], "xxx", false); |
| 853 |
|
| 854 |
for name in SPECIAL_STRINGS.iter() { |
| 855 |
check("retract", &["--all"], name, true); |
| 856 |
} |
| 857 |
check("retract", &["--all"], "xxx", false); |
| 858 |
|
| 859 |
for name in SPECIAL_STRINGS.iter() { |
| 860 |
check("authorize", &["--all", "--unconstrained"], name, true); |
| 861 |
} |
| 862 |
check("authorize", &["--all", "--unconstrained"], "xxx", false); |
| 863 |
|
| 864 |
for name in SPECIAL_STRINGS.iter() { |
| 865 |
check("retract", &["--all"], name, true); |
| 866 |
} |
| 867 |
check("retract", &["--all"], "xxx", false); |
| 868 |
} |
| 869 |
|
| 870 |
#[test] |
| 871 |
fn link_userid_designators() { |
| 872 |
for authorize in [true, false] { |
| 873 |
let link_maybe = |sq: &mut Sq, |
| 874 |
kh: KeyHandle, userid_arg: UserIDArg| |
| 875 |
-> Result<()> |
| 876 |
{ |
| 877 |
sq.tick(1); |
| 878 |
if authorize { |
| 879 |
sq.pki_link_authorize_maybe( |
| 880 |
&["--unconstrained"], kh, &[ userid_arg ]) |
| 881 |
} else { |
| 882 |
sq.pki_link_add_maybe(&[], kh, &[ userid_arg ]) |
| 883 |
} |
| 884 |
}; |
| 885 |
|
| 886 |
let link = |sq: &mut Sq, |
| 887 |
kh: KeyHandle, userid_arg: UserIDArg| |
| 888 |
{ |
| 889 |
link_maybe(sq, kh, userid_arg) |
| 890 |
.expect("success") |
| 891 |
}; |
| 892 |
|
| 893 |
let retract = |sq: &mut Sq, kh: KeyHandle| { |
| 894 |
sq.tick(1); |
| 895 |
sq.pki_link_retract(&[ "--all" ], kh.clone(), NO_USERIDS); |
| 896 |
}; |
| 897 |
|
| 898 |
// Check that the different user ID designators work. |
| 899 |
let mut sq = Sq::new(); |
| 900 |
|
| 901 |
let (cert, cert_path, _rev_path) = sq.key_generate( |
| 902 |
&[], |
| 903 |
&[ |
| 904 |
"Alice <[email protected]>", |
| 905 |
"Alice <[email protected]>", |
| 906 |
"Alice <[email protected]>", |
| 907 |
]); |
| 908 |
let fpr = cert.fingerprint().to_string(); |
| 909 |
sq.key_import(cert_path); |
| 910 |
|
| 911 |
|
| 912 |
// 1. Use --userid to link "Alice <[email protected]>", which is a |
| 913 |
// self-signed user ID. |
| 914 |
link(&mut sq, cert.key_handle(), |
| 915 |
UserIDArg::UserID("Alice <[email protected]>")); |
| 916 |
assert!(sq.pki_authenticate( |
| 917 |
&[], &fpr, UserIDArg::UserID("Alice <[email protected]>")).is_ok()); |
| 918 |
|
| 919 |
retract(&mut sq, cert.key_handle()); |
| 920 |
|
| 921 |
// 2. Use --add-userid to link "Alice <[email protected]>", which |
| 922 |
// is not a self-signed user ID. |
| 923 |
|
| 924 |
// This fails with --userid, because it expects a self-signed user ID. |
| 925 |
assert!(link_maybe( |
| 926 |
&mut sq, cert.key_handle(), |
| 927 |
UserIDArg::UserID("Alice <[email protected]>")).is_err()); |
| 928 |
|
| 929 |
// But it works with --add-userid. |
| 930 |
link(&mut sq, cert.key_handle(), |
| 931 |
UserIDArg::AddUserID("Alice <[email protected]>")); |
| 932 |
assert!(sq.pki_authenticate( |
| 933 |
&[], &fpr, UserIDArg::UserID("Alice <[email protected]>")).is_ok()); |
| 934 |
|
| 935 |
|
| 936 |
retract(&mut sq, cert.key_handle()); |
| 937 |
|
| 938 |
// 3. Use --email to link "<[email protected]>", which is part |
| 939 |
// of a self-signed user ID. |
| 940 |
// |
| 941 |
// --email => the email address must be part of a self-signed |
| 942 |
// user ID, but uses a user ID with just email address. |
| 943 |
link(&mut sq, cert.key_handle(), |
| 944 |
UserIDArg::Email("[email protected]")); |
| 945 |
|
| 946 |
assert!(sq.pki_authenticate( |
| 947 |
&[], &fpr, UserIDArg::UserID("<[email protected]>")).is_ok()); |
| 948 |
if ! authorize { |
| 949 |
// If '<[email protected]>' is a trusted introducer, then |
| 950 |
// it is used to authenticate the self-signed user ID. |
| 951 |
assert!(sq.pki_authenticate( |
| 952 |
&[], &fpr, |
| 953 |
UserIDArg::UserID("Alice <[email protected]>")).is_err()); |
| 954 |
} |
| 955 |
|
| 956 |
|
| 957 |
retract(&mut sq, cert.key_handle()); |
| 958 |
|
| 959 |
// 4. Use --userid-by-email to link "Alice |
| 960 |
// <[email protected]>", which is a self signed user ID. |
| 961 |
// |
| 962 |
// --userid-by-email => use the matching self signed user ID. |
| 963 |
link(&mut sq, cert.key_handle(), |
| 964 |
UserIDArg::ByEmail("[email protected]")); |
| 965 |
|
| 966 |
assert!(sq.pki_authenticate( |
| 967 |
&[], &fpr, UserIDArg::UserID("<[email protected]>")).is_err()); |
| 968 |
assert!(sq.pki_authenticate( |
| 969 |
&[], &fpr, UserIDArg::UserID("Alice <[email protected]>")).is_ok()); |
| 970 |
|
| 971 |
retract(&mut sq, cert.key_handle()); |
| 972 |
|
| 973 |
// 5. Use --add-email to link "<[email protected]>", which is |
| 974 |
// not part of a self signed user ID. |
| 975 |
|
| 976 |
// This fails with --email, because it expects a self-signed |
| 977 |
// user ID. |
| 978 |
assert!(link_maybe( |
| 979 |
&mut sq, cert.key_handle(), |
| 980 |
UserIDArg::Email("[email protected]")).is_err()); |
| 981 |
|
| 982 |
// This fails with --userid-by-email, because there is no |
| 983 |
// self-signed user ID with the email address. |
| 984 |
assert!(link_maybe( |
| 985 |
&mut sq, cert.key_handle(), |
| 986 |
UserIDArg::ByEmail("[email protected]")).is_err()); |
| 987 |
|
| 988 |
// But it works with --email-or-add. |
| 989 |
link(&mut sq, |
| 990 |
cert.key_handle(), UserIDArg::AddEmail("[email protected]")); |
| 991 |
assert!(sq.pki_authenticate( |
| 992 |
&[], &fpr, UserIDArg::UserID("<[email protected]>")).is_ok()); |
| 993 |
|
| 994 |
// Use --add-email to link "<[email protected]>", which is |
| 995 |
// part of the self signed user ID "Alice <[email protected]>". |
| 996 |
// This should link "<[email protected]>", not the self-signed |
| 997 |
// user ID. |
| 998 |
link(&mut sq, |
| 999 |
cert.key_handle(), UserIDArg::AddEmail("[email protected]")); |
| 1000 |
assert!(sq.pki_authenticate( |
| 1001 |
&[], &fpr, UserIDArg::UserID("<[email protected]>")).is_ok()); |
| 1002 |
if ! authorize { |
| 1003 |
// If '<[email protected]>' is a trusted introducer, then |
| 1004 |
// it is used to authenticate the self-signed user ID. |
| 1005 |
assert!(sq.pki_authenticate( |
| 1006 |
&[], &fpr, UserIDArg::UserID("Alice <[email protected]>")).is_err()); |
| 1007 |
} |
| 1008 |
} |
| 1009 |
} |
| 1010 |
|
| 1011 |
#[test] |
| 1012 |
fn link_retract_userid_designators() { |
| 1013 |
// Check that the different user ID designators work. |
| 1014 |
let mut sq = Sq::new(); |
| 1015 |
|
| 1016 |
let (cert, cert_path, _rev_path) = sq.key_generate( |
| 1017 |
&[], |
| 1018 |
&[ |
| 1019 |
"Alice <[email protected]>", |
| 1020 |
"<[email protected]>", |
| 1021 |
]); |
| 1022 |
let fpr = cert.fingerprint().to_string(); |
| 1023 |
sq.key_import(cert_path); |
| 1024 |
|
| 1025 |
// 1. Retract using --userid, which doesn't have to be |
| 1026 |
// self-signed. |
| 1027 |
|
| 1028 |
// Link "Alice <[email protected]>", which is self signed. |
| 1029 |
sq.tick(1); |
| 1030 |
sq.pki_link_add( |
| 1031 |
&[], cert.key_handle(), |
| 1032 |
&[ UserIDArg::UserID("Alice <[email protected]>") ]); |
| 1033 |
assert!(sq.pki_authenticate( |
| 1034 |
&[], &fpr, UserIDArg::UserID("Alice <[email protected]>")).is_ok()); |
| 1035 |
|
| 1036 |
// We can't retract using --email: it retracts "<[email protected]>". |
| 1037 |
sq.tick(1); |
| 1038 |
assert!(sq.pki_link_retract_maybe( |
| 1039 |
&[], cert.key_handle(), |
| 1040 |
&[ UserIDArg::Email("[email protected]") ]).is_err()); |
| 1041 |
|
| 1042 |
sq.tick(1); |
| 1043 |
sq.pki_link_retract(&[], cert.key_handle(), |
| 1044 |
&[ UserIDArg::UserID("Alice <[email protected]>") ]); |
| 1045 |
assert!(sq.pki_authenticate( |
| 1046 |
&[], &fpr, UserIDArg::UserID("Alice <[email protected]>")).is_err()); |
| 1047 |
|
| 1048 |
// Link "Alice <[email protected]>", which is not self signed. |
| 1049 |
sq.tick(1); |
| 1050 |
sq.pki_link_add( |
| 1051 |
&[], cert.key_handle(), |
| 1052 |
&[ UserIDArg::AddUserID("Alice <[email protected]>") ]); |
| 1053 |
assert!(sq.pki_authenticate( |
| 1054 |
&[], &fpr, UserIDArg::UserID("Alice <[email protected]>")).is_ok()); |
| 1055 |
|
| 1056 |
// We can't retract using --email: it retracts "<[email protected]>". |
| 1057 |
sq.tick(1); |
| 1058 |
assert!(sq.pki_link_retract_maybe( |
| 1059 |
&[], cert.key_handle(), |
| 1060 |
&[ UserIDArg::Email("[email protected]") ]).is_err()); |
| 1061 |
|
| 1062 |
// But we can retract using --user "Alice <[email protected]>". |
| 1063 |
sq.pki_link_retract(&[], cert.key_handle(), |
| 1064 |
&[ UserIDArg::UserID("Alice <[email protected]>") ]); |
| 1065 |
assert!(sq.pki_authenticate( |
| 1066 |
&[], &fpr, UserIDArg::UserID("Alice <[email protected]>")).is_err()); |
| 1067 |
|
| 1068 |
// 2. Retract using "--userid-by-email". |
| 1069 |
|
| 1070 |
// Link "Alice <[email protected]>", which is a self signed user |
| 1071 |
// ID. |
| 1072 |
sq.tick(1); |
| 1073 |
sq.pki_link_add( |
| 1074 |
&[], cert.key_handle(), |
| 1075 |
&[ UserIDArg::UserID("Alice <[email protected]>") ]); |
| 1076 |
assert!(sq.pki_authenticate( |
| 1077 |
&[], &fpr, UserIDArg::UserID("Alice <[email protected]>")).is_ok()); |
| 1078 |
|
| 1079 |
sq.tick(1); |
| 1080 |
sq.pki_link_retract(&[], cert.key_handle(), |
| 1081 |
&[ UserIDArg::ByEmail("[email protected]") ]); |
| 1082 |
assert!(sq.pki_authenticate( |
| 1083 |
&[], &fpr, UserIDArg::UserID("Alice <[email protected]>")).is_err()); |
| 1084 |
|
| 1085 |
|
| 1086 |
// 3. Retract using "--email". It uses a user ID with just the |
| 1087 |
// email address. |
| 1088 |
|
| 1089 |
// Link "<[email protected]>", which is NOT a self signed user |
| 1090 |
// ID. |
| 1091 |
sq.tick(1); |
| 1092 |
sq.pki_link_add( |
| 1093 |
&[], cert.key_handle(), |
| 1094 |
&[ UserIDArg::AddUserID("<[email protected]>") ]); |
| 1095 |
assert!(sq.pki_authenticate( |
| 1096 |
&[], &fpr, UserIDArg::UserID("Alice <[email protected]>")).is_err()); |
| 1097 |
assert!(sq.pki_authenticate( |
| 1098 |
&[], &fpr, UserIDArg::UserID("<[email protected]>")).is_ok()); |
| 1099 |
|
| 1100 |
// We can unlink it using --email, because that doesn't hvae to |
| 1101 |
// match on self-signed user IDs. |
| 1102 |
sq.tick(1); |
| 1103 |
assert!(sq.pki_link_retract_maybe( |
| 1104 |
&[], cert.key_handle(), |
| 1105 |
&[ UserIDArg::Email("[email protected]") ]).is_ok()); |
| 1106 |
assert!(sq.pki_authenticate( |
| 1107 |
&[], &fpr, UserIDArg::UserID("Alice <[email protected]>")).is_err()); |
| 1108 |
|
| 1109 |
// Link "<[email protected]>", which is a self signed user ID. |
| 1110 |
sq.tick(1); |
| 1111 |
sq.pki_link_add( |
| 1112 |
&[], cert.key_handle(), |
| 1113 |
&[ UserIDArg::UserID("<[email protected]>") ]); |
| 1114 |
assert!(sq.pki_authenticate( |
| 1115 |
&[], &fpr, UserIDArg::UserID("<[email protected]>")).is_ok()); |
| 1116 |
|
| 1117 |
// We can unlink it using --email: that matchs on self-signed user |
| 1118 |
// IDs. |
| 1119 |
sq.tick(1); |
| 1120 |
assert!(sq.pki_link_retract_maybe( |
| 1121 |
&[], cert.key_handle(), |
| 1122 |
&[ UserIDArg::Email("[email protected]") ]).is_ok()); |
| 1123 |
assert!(sq.pki_authenticate( |
| 1124 |
&[], &fpr, UserIDArg::UserID("<[email protected]>")).is_err()); |
| 1125 |
|
| 1126 |
// Link "Alice <[email protected]>", which is a self signed user |
| 1127 |
// ID. |
| 1128 |
sq.tick(1); |
| 1129 |
sq.pki_link_add( |
| 1130 |
&[], cert.key_handle(), |
| 1131 |
&[ UserIDArg::AddUserID("Alice <[email protected]>") ]); |
| 1132 |
assert!(sq.pki_authenticate( |
| 1133 |
&[], &fpr, UserIDArg::UserID("Alice <[email protected]>")).is_ok()); |
| 1134 |
|
| 1135 |
// We can't use --email to retract it: that retracts , as that |
| 1136 |
// "<[email protected]>". |
| 1137 |
sq.tick(1); |
| 1138 |
assert!(sq.pki_link_retract_maybe( |
| 1139 |
&[], cert.key_handle(), |
| 1140 |
&[ UserIDArg::Email("[email protected]") ]).is_err()); |
| 1141 |
assert!(sq.pki_authenticate( |
| 1142 |
&[], &fpr, UserIDArg::UserID("Alice <[email protected]>")).is_ok()); |
| 1143 |
// But we can use --userid-by-email |
| 1144 |
assert!(sq.pki_link_retract_maybe( |
| 1145 |
&[], cert.key_handle(), |
| 1146 |
&[ UserIDArg::ByEmail("[email protected]") ]).is_ok()); |
| 1147 |
assert!(sq.pki_authenticate( |
| 1148 |
&[], &fpr, UserIDArg::UserID("Alice <[email protected]>")).is_err()); |
| 1149 |
} |
| 1150 |
|
| 1151 |
#[test] |
| 1152 |
fn retract() { |
| 1153 |
let mut sq = Sq::new(); |
| 1154 |
|
| 1155 |
let (cert, cert_path, _rev_path) = sq.key_generate( |
| 1156 |
&[], &["Alice <[email protected]>" ]); |
| 1157 |
sq.key_import(cert_path); |
| 1158 |
|
| 1159 |
// If a user ID was never linked, retract fails. |
| 1160 |
|
| 1161 |
// Check for a self-signed user ID. |
| 1162 |
sq.tick(1); |
| 1163 |
assert!(sq.pki_link_retract_maybe( |
| 1164 |
&[], cert.key_handle(), |
| 1165 |
&[ UserIDArg::UserID("Alice <[email protected]>") ]).is_err()); |
| 1166 |
|
| 1167 |
assert!(sq.pki_link_retract_maybe( |
| 1168 |
&[], cert.key_handle(), |
| 1169 |
&[ UserIDArg::Email("[email protected]") ]).is_err()); |
| 1170 |
|
| 1171 |
// Check for a user ID that is not self signed. |
| 1172 |
assert!(sq.pki_link_retract_maybe( |
| 1173 |
&[], cert.key_handle(), |
| 1174 |
&[ UserIDArg::UserID("Alice <[email protected]>") ]).is_err()); |
| 1175 |
|
| 1176 |
assert!(sq.pki_link_retract_maybe( |
| 1177 |
&[], cert.key_handle(), |
| 1178 |
&[ UserIDArg::Email("[email protected]") ]).is_err()); |
| 1179 |
|
| 1180 |
// --all doesn't care. |
| 1181 |
assert!(sq.pki_link_retract_maybe( |
| 1182 |
&["--all"], cert.key_handle(), NO_USERIDS).is_ok()); |
| 1183 |
|
| 1184 |
// Now create a link. If we retract the same link multiple times, |
| 1185 |
// we don't get an error, but the subsequent calls won't do |
| 1186 |
// anything except emit the message "Certification parameters are |
| 1187 |
// unchanged.") |
| 1188 |
sq.tick(1); |
| 1189 |
sq.pki_link_add( |
| 1190 |
&[], cert.key_handle(), |
| 1191 |
&[ UserIDArg::UserID("Alice <[email protected]>") ]); |
| 1192 |
|
| 1193 |
for _ in 1..4 { |
| 1194 |
sq.tick(1); |
| 1195 |
sq.pki_link_retract( |
| 1196 |
&[], cert.key_handle(), |
| 1197 |
&[ UserIDArg::UserID("Alice <[email protected]>") ]); |
| 1198 |
|
| 1199 |
sq.tick(1); |
| 1200 |
// --email => "<[email protected]>", which was not linked. |
| 1201 |
assert!(sq.pki_link_retract_maybe( |
| 1202 |
&[], cert.key_handle(), |
| 1203 |
&[ UserIDArg::Email("[email protected]") ]).is_err()); |
| 1204 |
} |
| 1205 |
|
| 1206 |
sq.pki_link_add( |
| 1207 |
&[], cert.key_handle(), |
| 1208 |
&[ UserIDArg::AddUserID("Alice <[email protected]>") ]); |
| 1209 |
|
| 1210 |
for _ in 1..4 { |
| 1211 |
sq.tick(1); |
| 1212 |
sq.pki_link_retract( |
| 1213 |
&[], cert.key_handle(), |
| 1214 |
&[ UserIDArg::UserID("Alice <[email protected]>") ]); |
| 1215 |
|
| 1216 |
// --email => "<[email protected]>", which was not linked. |
| 1217 |
sq.tick(1); |
| 1218 |
assert!(sq.pki_link_retract_maybe( |
| 1219 |
&[], cert.key_handle(), |
| 1220 |
&[ UserIDArg::Email("[email protected]") ]).is_err()); |
| 1221 |
} |
| 1222 |
|
| 1223 |
// --all doesn't care. |
| 1224 |
assert!(sq.pki_link_retract_maybe( |
| 1225 |
&["--all"], cert.key_handle(), NO_USERIDS).is_ok()); |
| 1226 |
} |