| 1 |
use std::time::Duration; |
| 2 |
|
| 3 |
use openpgp::packet::UserID; |
| 4 |
use openpgp::parse::Parse; |
| 5 |
use openpgp::types::ReasonForRevocation; |
| 6 |
use openpgp::types::RevocationStatus; |
| 7 |
use openpgp::types::SignatureType; |
| 8 |
use openpgp::Cert; |
| 9 |
use openpgp::Result; |
| 10 |
use sequoia_openpgp as openpgp; |
| 11 |
|
| 12 |
use super::common::NO_USERIDS; |
| 13 |
use super::common::STANDARD_POLICY; |
| 14 |
use super::common::Sq; |
| 15 |
use super::common::UserIDArg; |
| 16 |
use super::common::compare_notations; |
| 17 |
use super::common::time_as_string; |
| 18 |
|
| 19 |
#[test] |
| 20 |
fn sq_key_userid_revoke() -> Result<()> { |
| 21 |
let sq = Sq::new(); |
| 22 |
let time = sq.now(); |
| 23 |
|
| 24 |
let userids = &["alice <[email protected]>", "alice <[email protected]>"]; |
| 25 |
// revoke the last userid |
| 26 |
let userid_revoke = userids.last().unwrap(); |
| 27 |
|
| 28 |
// revoke for various reasons, with or without notations added, or with |
| 29 |
// a revocation whose reference time is one hour after the creation of the |
| 30 |
// certificate |
| 31 |
for (reason, reason_str, notations, revocation_time) in [ |
| 32 |
(ReasonForRevocation::UIDRetired, "retired", &[][..], None), |
| 33 |
( |
| 34 |
ReasonForRevocation::UIDRetired, |
| 35 |
"retired", |
| 36 |
&[][..], |
| 37 |
Some(time + Duration::new(60 * 60, 0)), |
| 38 |
), |
| 39 |
( |
| 40 |
ReasonForRevocation::UIDRetired, |
| 41 |
"retired", |
| 42 |
&[("foo", "bar"), ("[email protected]", "VALUE")][..], |
| 43 |
None, |
| 44 |
), |
| 45 |
(ReasonForRevocation::Unspecified, "unspecified", &[][..], None), |
| 46 |
( |
| 47 |
ReasonForRevocation::Unspecified, |
| 48 |
"unspecified", |
| 49 |
&[][..], |
| 50 |
Some(time + Duration::new(60 * 60, 0)), |
| 51 |
), |
| 52 |
( |
| 53 |
ReasonForRevocation::Unspecified, |
| 54 |
"unspecified", |
| 55 |
&[("foo", "bar"), ("[email protected]", "VALUE")][..], |
| 56 |
None, |
| 57 |
), |
| 58 |
] { |
| 59 |
eprintln!("=========================="); |
| 60 |
eprintln!("reason: {}, message: {}, notations: {:?}, time: {:?}", |
| 61 |
reason, reason_str, notations, revocation_time); |
| 62 |
|
| 63 |
for keystore in [false, true].into_iter() { |
| 64 |
eprintln!("--------------------------"); |
| 65 |
eprintln!("keystore: {}", keystore); |
| 66 |
|
| 67 |
let (cert, cert_path, _rev_path) = sq.key_generate(&[], userids); |
| 68 |
|
| 69 |
let valid_cert = cert.with_policy(STANDARD_POLICY, Some(time.into()))?; |
| 70 |
let fingerprint = valid_cert.fingerprint(); |
| 71 |
|
| 72 |
let message = "message"; |
| 73 |
|
| 74 |
let revocation = sq.scratch_file(Some(&*format!( |
| 75 |
"revocation_{}_{}_{}.rev", |
| 76 |
reason_str, |
| 77 |
if notations.is_empty() { |
| 78 |
"no_notations" |
| 79 |
} else { |
| 80 |
"notations" |
| 81 |
}, |
| 82 |
if revocation_time.is_some() { |
| 83 |
"time" |
| 84 |
} else { |
| 85 |
"no_time" |
| 86 |
} |
| 87 |
))); |
| 88 |
|
| 89 |
if keystore { |
| 90 |
// When using the keystore, we need to import the key. |
| 91 |
sq.key_import(&cert_path); |
| 92 |
} |
| 93 |
|
| 94 |
let mut cmd = sq.command(); |
| 95 |
cmd.args([ |
| 96 |
"key", |
| 97 |
"userid", |
| 98 |
"revoke", |
| 99 |
"--userid", userid_revoke, |
| 100 |
"--reason", reason_str, |
| 101 |
"--message", message, |
| 102 |
]); |
| 103 |
|
| 104 |
if keystore { |
| 105 |
cmd.args([ |
| 106 |
"--cert", &cert.fingerprint().to_string(), |
| 107 |
]); |
| 108 |
} else { |
| 109 |
cmd.arg("--cert-file").arg(&cert_path) |
| 110 |
.arg("--output").arg(&revocation); |
| 111 |
} |
| 112 |
|
| 113 |
for (k, v) in notations { |
| 114 |
cmd.args(["--signature-notation", k, v]); |
| 115 |
} |
| 116 |
if let Some(time) = revocation_time { |
| 117 |
cmd.args([ |
| 118 |
"--time", |
| 119 |
&time_as_string(time.into()), |
| 120 |
]); |
| 121 |
} |
| 122 |
let output = cmd.output()?; |
| 123 |
if !output.status.success() { |
| 124 |
panic!( |
| 125 |
"sq exited with non-zero status code: {}", |
| 126 |
String::from_utf8(output.stderr)? |
| 127 |
); |
| 128 |
} |
| 129 |
|
| 130 |
// read revocation cert |
| 131 |
let rev = if keystore { |
| 132 |
sq.cert_export(cert.key_handle()) |
| 133 |
} else { |
| 134 |
Cert::from_file(&revocation)? |
| 135 |
}; |
| 136 |
assert!(! rev.is_tsk()); |
| 137 |
let cert = cert.clone().merge_public(rev)?; |
| 138 |
let valid_cert = |
| 139 |
cert.with_policy(STANDARD_POLICY, revocation_time.map(Into::into))?; |
| 140 |
|
| 141 |
// whether we found a revocation signature |
| 142 |
let mut found_revoked = false; |
| 143 |
|
| 144 |
valid_cert.userids().for_each(|x| { |
| 145 |
if x.userid().value() == userid_revoke.as_bytes() { |
| 146 |
if let RevocationStatus::Revoked(sigs) = x.bundle() |
| 147 |
.revocation_status( |
| 148 |
STANDARD_POLICY, |
| 149 |
revocation_time.map(Into::into), |
| 150 |
) |
| 151 |
{ |
| 152 |
// there is only one signature packet |
| 153 |
assert_eq!(sigs.len(), 1); |
| 154 |
let sig = sigs.into_iter().next().unwrap(); |
| 155 |
|
| 156 |
// it is a certification revocation |
| 157 |
assert_eq!( |
| 158 |
sig.typ(), |
| 159 |
SignatureType::CertificationRevocation |
| 160 |
); |
| 161 |
|
| 162 |
// the issuer is the certificate owner |
| 163 |
assert_eq!( |
| 164 |
sig.get_issuers().into_iter().next().as_ref(), |
| 165 |
Some(&fingerprint.clone().into()) |
| 166 |
); |
| 167 |
|
| 168 |
// our reason for revocation and message matches |
| 169 |
assert_eq!( |
| 170 |
sig.reason_for_revocation(), |
| 171 |
Some((reason, message.as_bytes())) |
| 172 |
); |
| 173 |
|
| 174 |
// the notations of the revocation match the ones |
| 175 |
// we passed in |
| 176 |
assert!(compare_notations(sig, notations).is_ok()); |
| 177 |
|
| 178 |
found_revoked = true; |
| 179 |
} |
| 180 |
} |
| 181 |
}); |
| 182 |
|
| 183 |
if !found_revoked { |
| 184 |
panic!("the revoked userid is not found in the revocation cert"); |
| 185 |
} |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
Ok(()) |
| 190 |
} |
| 191 |
|
| 192 |
#[test] |
| 193 |
fn sq_key_userid_revoke_thirdparty() -> Result<()> { |
| 194 |
let sq = Sq::new(); |
| 195 |
let time = sq.now(); |
| 196 |
|
| 197 |
let userids = &["alice <[email protected]>", "alice <[email protected]>"]; |
| 198 |
// revoke the last userid |
| 199 |
let userid_revoke = userids.last().unwrap(); |
| 200 |
|
| 201 |
// revoke for various reasons, with or without notations added, or with |
| 202 |
// a revocation whose reference time is one hour after the creation of the |
| 203 |
// certificate |
| 204 |
for (reason, reason_str, notations, revocation_time) in [ |
| 205 |
(ReasonForRevocation::UIDRetired, "retired", &[][..], None), |
| 206 |
( |
| 207 |
ReasonForRevocation::UIDRetired, |
| 208 |
"retired", |
| 209 |
&[][..], |
| 210 |
Some(time + Duration::new(60 * 60, 0)), |
| 211 |
), |
| 212 |
( |
| 213 |
ReasonForRevocation::UIDRetired, |
| 214 |
"retired", |
| 215 |
&[("foo", "bar"), ("[email protected]", "VALUE")][..], |
| 216 |
None, |
| 217 |
), |
| 218 |
(ReasonForRevocation::Unspecified, "unspecified", &[][..], None), |
| 219 |
( |
| 220 |
ReasonForRevocation::Unspecified, |
| 221 |
"unspecified", |
| 222 |
&[][..], |
| 223 |
Some(time + Duration::new(60 * 60, 0)), |
| 224 |
), |
| 225 |
( |
| 226 |
ReasonForRevocation::Unspecified, |
| 227 |
"unspecified", |
| 228 |
&[("foo", "bar"), ("[email protected]", "VALUE")][..], |
| 229 |
None, |
| 230 |
), |
| 231 |
] { |
| 232 |
for keystore in [false, true].into_iter() { |
| 233 |
let (cert, cert_path, _rev_path) = sq.key_generate(&[], userids); |
| 234 |
let (thirdparty_cert, thirdparty_path, _rev_path) |
| 235 |
= sq.key_generate(&[], &["bob <[email protected]>"]); |
| 236 |
let thirdparty_valid_cert = thirdparty_cert |
| 237 |
.with_policy(STANDARD_POLICY, Some(time.into()))?; |
| 238 |
let thirdparty_fingerprint = thirdparty_valid_cert.fingerprint(); |
| 239 |
|
| 240 |
let message = "message"; |
| 241 |
|
| 242 |
let revocation = sq.scratch_file(Some(&*format!( |
| 243 |
"revocation_{}_{}_{}.rev", |
| 244 |
reason_str, |
| 245 |
if notations.is_empty() { |
| 246 |
"no_notations" |
| 247 |
} else { |
| 248 |
"notations" |
| 249 |
}, |
| 250 |
if revocation_time.is_some() { |
| 251 |
"time" |
| 252 |
} else { |
| 253 |
"no_time" |
| 254 |
} |
| 255 |
))); |
| 256 |
|
| 257 |
if keystore { |
| 258 |
// When using the keystore, we need to import the key. |
| 259 |
|
| 260 |
for path in &[ &cert_path, &thirdparty_path ] { |
| 261 |
sq.key_import(path); |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
let mut cmd = sq.command(); |
| 266 |
cmd.args([ |
| 267 |
"key", |
| 268 |
"userid", |
| 269 |
"revoke", |
| 270 |
"--userid", userid_revoke, |
| 271 |
"--reason", reason_str, |
| 272 |
"--message", message, |
| 273 |
]); |
| 274 |
|
| 275 |
if keystore { |
| 276 |
cmd.args([ |
| 277 |
"--cert", &cert.fingerprint().to_string(), |
| 278 |
"--revoker", &thirdparty_cert.fingerprint().to_string(), |
| 279 |
]); |
| 280 |
} else { |
| 281 |
cmd.arg("--output").arg(&revocation) |
| 282 |
.arg("--cert-file").arg(&cert_path) |
| 283 |
.arg("--revoker-file").arg(&thirdparty_path); |
| 284 |
} |
| 285 |
|
| 286 |
for (k, v) in notations { |
| 287 |
cmd.args(["--signature-notation", k, v]); |
| 288 |
} |
| 289 |
if let Some(time) = revocation_time { |
| 290 |
cmd.args([ |
| 291 |
"--time", |
| 292 |
&time_as_string(time.into()), |
| 293 |
]); |
| 294 |
} |
| 295 |
let output = cmd.output()?; |
| 296 |
if !output.status.success() { |
| 297 |
panic!( |
| 298 |
"sq exited with non-zero status code: {}", |
| 299 |
String::from_utf8(output.stderr)? |
| 300 |
); |
| 301 |
} |
| 302 |
|
| 303 |
// read revocation cert |
| 304 |
let rev = if keystore { |
| 305 |
sq.cert_export(cert.key_handle()) |
| 306 |
} else { |
| 307 |
Cert::from_file(&revocation)? |
| 308 |
}; |
| 309 |
assert!(! rev.is_tsk()); |
| 310 |
let revocation_cert = cert.clone().merge_public(rev)?; |
| 311 |
let revocation_valid_cert = revocation_cert |
| 312 |
.with_policy(STANDARD_POLICY, revocation_time.map(Into::into))?; |
| 313 |
|
| 314 |
// whether we found a revocation signature |
| 315 |
let mut found_revoked = false; |
| 316 |
|
| 317 |
revocation_valid_cert.userids().for_each(|x| { |
| 318 |
if x.userid().value() == userid_revoke.as_bytes() { |
| 319 |
if let RevocationStatus::CouldBe(sigs) = x.bundle() |
| 320 |
.revocation_status( |
| 321 |
STANDARD_POLICY, |
| 322 |
revocation_time.map(Into::into), |
| 323 |
) |
| 324 |
{ |
| 325 |
// there is only one signature packet |
| 326 |
assert_eq!(sigs.len(), 1); |
| 327 |
let sig = sigs.into_iter().next().unwrap(); |
| 328 |
|
| 329 |
// it is a certification revocation |
| 330 |
assert_eq!( |
| 331 |
sig.typ(), |
| 332 |
SignatureType::CertificationRevocation |
| 333 |
); |
| 334 |
|
| 335 |
// the issuer is a thirdparty revoker |
| 336 |
assert_eq!( |
| 337 |
sig.get_issuers().into_iter().next().as_ref(), |
| 338 |
Some(&thirdparty_fingerprint.clone().into()) |
| 339 |
); |
| 340 |
|
| 341 |
// the revocation can be verified |
| 342 |
if sig |
| 343 |
.clone() |
| 344 |
.verify_userid_revocation( |
| 345 |
thirdparty_cert.primary_key().key(), |
| 346 |
revocation_cert.primary_key().key(), |
| 347 |
&UserID::from(*userid_revoke), |
| 348 |
) |
| 349 |
.is_err() |
| 350 |
{ |
| 351 |
panic!("revocation is not valid") |
| 352 |
} |
| 353 |
|
| 354 |
// our reason for revocation and message matches |
| 355 |
assert_eq!( |
| 356 |
sig.reason_for_revocation(), |
| 357 |
Some((reason, message.as_bytes())) |
| 358 |
); |
| 359 |
|
| 360 |
// the notations of the revocation match the ones |
| 361 |
// we passed in |
| 362 |
assert!(compare_notations(sig, notations).is_ok()); |
| 363 |
|
| 364 |
found_revoked = true; |
| 365 |
} |
| 366 |
} |
| 367 |
}); |
| 368 |
|
| 369 |
if !found_revoked { |
| 370 |
panic!("the revoked userid is not found in the revocation cert"); |
| 371 |
} |
| 372 |
} |
| 373 |
} |
| 374 |
|
| 375 |
Ok(()) |
| 376 |
} |
| 377 |
|
| 378 |
#[test] |
| 379 |
fn sq_key_userid_add() -> Result<()> { |
| 380 |
let sq = Sq::new(); |
| 381 |
let (key, _, _) = sq.key_generate(&[], NO_USERIDS); |
| 382 |
assert_eq!(key.userids().count(), 0); |
| 383 |
|
| 384 |
let key = sq.key_userid_add( |
| 385 |
&[], |
| 386 |
key, |
| 387 |
&[ |
| 388 |
UserIDArg::Name("Joan Clarke"), |
| 389 |
UserIDArg::Name("Joan Clarke Murray"), |
| 390 |
UserIDArg::Email("[email protected]"), |
| 391 |
])?; |
| 392 |
|
| 393 |
assert_eq!(key.userids().count(), 3); |
| 394 |
assert!(key.userids().any(|u| u.userid().value() == b"Joan Clarke")); |
| 395 |
assert!(key.userids().any(|u| u.userid().value() == b"Joan Clarke Murray")); |
| 396 |
assert!( |
| 397 |
key.userids().any(|u| u.userid().value() == b"<[email protected]>")); |
| 398 |
|
| 399 |
Ok(()) |
| 400 |
} |