| 1 |
use std::time::Duration; |
| 2 |
|
| 3 |
use sequoia_openpgp as openpgp; |
| 4 |
use openpgp::parse::Parse; |
| 5 |
use openpgp::Cert; |
| 6 |
use openpgp::cert::amalgamation::ValidAmalgamation; |
| 7 |
use openpgp::Result; |
| 8 |
use openpgp::types::RevocationStatus; |
| 9 |
|
| 10 |
use super::common::STANDARD_POLICY; |
| 11 |
use super::common::Sq; |
| 12 |
use super::common::time_as_string; |
| 13 |
use super::common::UserIDArg; |
| 14 |
|
| 15 |
#[test] |
| 16 |
fn sq_key_expire() -> Result<()> { |
| 17 |
for keystore in [false, true] { |
| 18 |
let mut sq = Sq::new(); |
| 19 |
|
| 20 |
let (cert, cert_path, _rev_path) |
| 21 |
= sq.key_generate(&[], &["alice <[email protected]>"]); |
| 22 |
let fpr = cert.fingerprint().to_string(); |
| 23 |
|
| 24 |
// Two days go by. |
| 25 |
sq.tick(2 * 24 * 60 * 60); |
| 26 |
|
| 27 |
let updated_path = sq.scratch_file("updated.pgp"); |
| 28 |
let updated2_path = sq.scratch_file("updated2.pgp"); |
| 29 |
|
| 30 |
if keystore { |
| 31 |
sq.key_import(&cert_path); |
| 32 |
} |
| 33 |
|
| 34 |
// Change the key to expire in one day. |
| 35 |
let mut cmd = sq.command(); |
| 36 |
cmd.args(["key", "expire", "--expiration", "1d"]); |
| 37 |
if keystore { |
| 38 |
cmd.args(["--cert", &fpr ]); |
| 39 |
} else { |
| 40 |
cmd |
| 41 |
.arg("--overwrite") |
| 42 |
.arg("--cert-file").arg(&cert_path) |
| 43 |
.arg("--output").arg(&updated_path); |
| 44 |
} |
| 45 |
sq.run(cmd, true); |
| 46 |
|
| 47 |
let updated = if keystore { |
| 48 |
eprintln!("Updated certificate to expire in one day:\n{}", |
| 49 |
sq.inspect(cert.key_handle())); |
| 50 |
|
| 51 |
sq.cert_export(cert.key_handle()) |
| 52 |
} else { |
| 53 |
eprintln!("Updated certificate to expire in one day:\n{}", |
| 54 |
sq.inspect(&updated_path)); |
| 55 |
|
| 56 |
Cert::from_file(&updated_path).expect("valid cert") |
| 57 |
}; |
| 58 |
|
| 59 |
// It should be alive now. |
| 60 |
let vc = updated.with_policy(STANDARD_POLICY, sq.now()).expect("valid"); |
| 61 |
assert!(matches!(vc.alive(), Ok(()))); |
| 62 |
|
| 63 |
// It should be alive in 1 day minus 1 second. |
| 64 |
let t = sq.now() + Duration::new(24 * 60 * 60 - 1, 0); |
| 65 |
eprintln!("Checking expiration status at {}", time_as_string(t.into())); |
| 66 |
let vc = updated.with_policy(STANDARD_POLICY, t).expect("valid"); |
| 67 |
assert!(matches!(vc.alive(), Ok(()))); |
| 68 |
|
| 69 |
// But in exactly one day, it should be expired. |
| 70 |
let t = sq.now() + Duration::new(24 * 60 * 60, 0); |
| 71 |
eprintln!("Checking expiration status at {}", time_as_string(t.into())); |
| 72 |
let vc = updated.with_policy(STANDARD_POLICY, t).expect("valid"); |
| 73 |
assert!(matches!(vc.alive(), Err(_))); |
| 74 |
|
| 75 |
// 12 hours go by. Clear the expiration time. |
| 76 |
sq.tick(12 * 60 * 60); |
| 77 |
|
| 78 |
let mut cmd = sq.command(); |
| 79 |
cmd.args([ "key", "expire", "--expiration", "never" ]); |
| 80 |
if keystore { |
| 81 |
cmd.args([ "--cert", &fpr ]); |
| 82 |
} else { |
| 83 |
cmd.args([ |
| 84 |
"--cert-file", &updated_path.to_string_lossy(), |
| 85 |
"--output", &updated2_path.to_string_lossy(), |
| 86 |
]); |
| 87 |
} |
| 88 |
sq.run(cmd, true); |
| 89 |
|
| 90 |
let updated = if keystore { |
| 91 |
eprintln!("Updated certificate to expire in one day:\n{}", |
| 92 |
sq.inspect(cert.key_handle())); |
| 93 |
|
| 94 |
sq.cert_export(cert.key_handle()) |
| 95 |
} else { |
| 96 |
eprintln!("Updated certificate to expire in one day:\n{}", |
| 97 |
sq.inspect(&updated2_path)); |
| 98 |
|
| 99 |
Cert::from_file(&updated2_path).expect("valid cert") |
| 100 |
}; |
| 101 |
|
| 102 |
// It should be alive now. |
| 103 |
let vc = updated.with_policy(STANDARD_POLICY, None) |
| 104 |
.expect("valid"); |
| 105 |
assert!(matches!(vc.alive(), Ok(()))); |
| 106 |
|
| 107 |
// It should be alive in 1 day minus 1 second. |
| 108 |
let vc = updated.with_policy( |
| 109 |
STANDARD_POLICY, |
| 110 |
sq.now() + Duration::new(24 * 60 * 60 - 1, 0)) |
| 111 |
.expect("valid"); |
| 112 |
assert!(matches!(vc.alive(), Ok(()))); |
| 113 |
|
| 114 |
// And in exactly one day... |
| 115 |
let vc = updated.with_policy( |
| 116 |
STANDARD_POLICY, |
| 117 |
sq.now() + Duration::new(24 * 60 * 60, 0)) |
| 118 |
.expect("valid"); |
| 119 |
assert!(matches!(vc.alive(), Ok(()))); |
| 120 |
} |
| 121 |
|
| 122 |
Ok(()) |
| 123 |
} |
| 124 |
|
| 125 |
/// Tests changing the expiration time of a key without direct key |
| 126 |
/// signature. |
| 127 |
/// |
| 128 |
/// These kind of keys are generated by GnuPG. |
| 129 |
#[test] |
| 130 |
fn sq_key_expire_no_direct_key_sig() -> Result<()> { |
| 131 |
use openpgp::{ |
| 132 |
packet::{Any, Signature}, |
| 133 |
serialize::Serialize, |
| 134 |
types::SignatureType, |
| 135 |
}; |
| 136 |
|
| 137 |
let mut sq = Sq::new(); |
| 138 |
let (cert, _cert_path, _rev_path) |
| 139 |
= sq.key_generate(&[], &[UserIDArg::Email("[email protected]")]); |
| 140 |
let fipr = cert.fingerprint().to_string(); |
| 141 |
|
| 142 |
|
| 143 |
// Strip the direct key signature. |
| 144 |
let mut p = cert.as_tsk().into_packets().collect::<Vec<_>>(); |
| 145 |
p.retain(|p| Any::<Signature>::downcast_ref(p) |
| 146 |
.map(|sig| sig.typ() != SignatureType::DirectKey) |
| 147 |
.unwrap_or(true)); |
| 148 |
let cert = Cert::from_packets(p.into_iter())?; |
| 149 |
|
| 150 |
// Two days go by. |
| 151 |
sq.tick(2 * 24 * 60 * 60); |
| 152 |
|
| 153 |
let cert_path = sq.scratch_file("updated.pgp"); |
| 154 |
cert.as_tsk().serialize(&mut std::fs::File::create(&cert_path)?)?; |
| 155 |
sq.key_import(&cert_path); |
| 156 |
|
| 157 |
// Change the key to expire in one day. |
| 158 |
let mut cmd = sq.command(); |
| 159 |
cmd.args(["key", "expire", "--expiration", "1d", "--cert", &fipr]); |
| 160 |
sq.run(cmd, true); |
| 161 |
|
| 162 |
eprintln!("Updated certificate to expire in one day:\n{}", |
| 163 |
sq.inspect(cert.key_handle())); |
| 164 |
let updated = sq.cert_export(cert.key_handle()); |
| 165 |
|
| 166 |
// It should be alive now. |
| 167 |
let vc = updated.with_policy(STANDARD_POLICY, sq.now()).expect("valid"); |
| 168 |
assert!(matches!(vc.alive(), Ok(()))); |
| 169 |
|
| 170 |
// It should be alive in 1 day minus 1 second. |
| 171 |
let t = sq.now() + Duration::new(24 * 60 * 60 - 1, 0); |
| 172 |
eprintln!("Checking expiration status at {}", time_as_string(t.into())); |
| 173 |
let vc = updated.with_policy(STANDARD_POLICY, t).expect("valid"); |
| 174 |
assert!(matches!(vc.alive(), Ok(()))); |
| 175 |
|
| 176 |
// But in exactly one day, it should be expired. |
| 177 |
let t = sq.now() + Duration::new(24 * 60 * 60, 0); |
| 178 |
eprintln!("Checking expiration status at {}", time_as_string(t.into())); |
| 179 |
let vc = updated.with_policy(STANDARD_POLICY, t).expect("valid"); |
| 180 |
assert!(matches!(vc.alive(), Err(_))); |
| 181 |
|
| 182 |
// 12 hours go by. Clear the expiration time. |
| 183 |
sq.tick(12 * 60 * 60); |
| 184 |
|
| 185 |
let mut cmd = sq.command(); |
| 186 |
cmd.args(["key", "expire", "--expiration", "never", "--cert", &fipr]); |
| 187 |
sq.run(cmd, true); |
| 188 |
|
| 189 |
eprintln!("Updated certificate to expire in one day:\n{}", |
| 190 |
sq.inspect(cert.key_handle())); |
| 191 |
let updated = sq.cert_export(cert.key_handle()); |
| 192 |
|
| 193 |
// It should be alive now. |
| 194 |
let vc = updated.with_policy(STANDARD_POLICY, None) |
| 195 |
.expect("valid"); |
| 196 |
assert!(matches!(vc.alive(), Ok(()))); |
| 197 |
|
| 198 |
// It should be alive in 1 day minus 1 second. |
| 199 |
let vc = updated.with_policy( |
| 200 |
STANDARD_POLICY, |
| 201 |
sq.now() + Duration::new(24 * 60 * 60 - 1, 0)) |
| 202 |
.expect("valid"); |
| 203 |
assert!(matches!(vc.alive(), Ok(()))); |
| 204 |
|
| 205 |
// And in exactly one day... |
| 206 |
let vc = updated.with_policy( |
| 207 |
STANDARD_POLICY, |
| 208 |
sq.now() + Duration::new(24 * 60 * 60, 0)) |
| 209 |
.expect("valid"); |
| 210 |
assert!(matches!(vc.alive(), Ok(()))); |
| 211 |
|
| 212 |
Ok(()) |
| 213 |
} |
| 214 |
|
| 215 |
#[test] |
| 216 |
fn unbound_userid() { |
| 217 |
// Make sure we can extend the expiration time of a certificate |
| 218 |
// that includes an unbound user ID (i.e., a user ID without a |
| 219 |
// self signature). |
| 220 |
|
| 221 |
let sq = Sq::new(); |
| 222 |
|
| 223 |
let cert_path = sq.test_data() |
| 224 |
.join("keys") |
| 225 |
.join("unbound-userid.pgp"); |
| 226 |
|
| 227 |
let cert = Cert::from_file(&cert_path).expect("can read"); |
| 228 |
let vc = cert.with_policy(STANDARD_POLICY, sq.now()) |
| 229 |
.expect("valid cert"); |
| 230 |
// It shouldn't be expired yet. |
| 231 |
assert!(vc.primary_key().key_expiration_time().is_none()); |
| 232 |
|
| 233 |
// Set it to expire in a day. |
| 234 |
let updated_path = sq.scratch_file("updated"); |
| 235 |
let updated = sq.key_expire(cert_path, |
| 236 |
"1d", |
| 237 |
None, |
| 238 |
updated_path.as_path(), |
| 239 |
true) |
| 240 |
.expect("sq key expire should succeed"); |
| 241 |
|
| 242 |
let vc = updated.with_policy(STANDARD_POLICY, sq.now()) |
| 243 |
.expect("valid cert"); |
| 244 |
let expiration = vc.primary_key().key_expiration_time(); |
| 245 |
assert_eq!(expiration, |
| 246 |
Some(sq.now() + std::time::Duration::new(24 * 60 * 60, 0))); |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
#[test] |
| 251 |
fn revoked_userid() { |
| 252 |
// Make sure we can extend the expiration time of a certificate |
| 253 |
// that includes a revoked user ID (i.e., a user ID without a self |
| 254 |
// signature), and make sure we DON'T make the user ID valid. |
| 255 |
|
| 256 |
let sq = Sq::new(); |
| 257 |
|
| 258 |
let cert_path = sq.test_data() |
| 259 |
.join("keys") |
| 260 |
.join("retired-userid.pgp"); |
| 261 |
|
| 262 |
let cert = Cert::from_file(&cert_path).expect("can read"); |
| 263 |
let vc = cert.with_policy(STANDARD_POLICY, sq.now()) |
| 264 |
.expect("valid cert"); |
| 265 |
// It shouldn't be expired yet. |
| 266 |
assert!(vc.primary_key().key_expiration_time().is_none()); |
| 267 |
|
| 268 |
let ua = vc.userids().next().expect("have a user ID"); |
| 269 |
if let RevocationStatus::Revoked(_) = ua.revocation_status() { |
| 270 |
} else { |
| 271 |
panic!("User ID should be revoked, but isn't."); |
| 272 |
}; |
| 273 |
|
| 274 |
// Set it to expire in a day. |
| 275 |
let updated_path = sq.scratch_file("updated"); |
| 276 |
let updated = sq.key_expire(cert_path, |
| 277 |
"1d", |
| 278 |
None, |
| 279 |
updated_path.as_path(), |
| 280 |
true) |
| 281 |
.expect("sq key expire should succeed"); |
| 282 |
|
| 283 |
let vc = updated.with_policy(STANDARD_POLICY, sq.now()) |
| 284 |
.expect("valid cert"); |
| 285 |
let expiration = vc.primary_key().key_expiration_time(); |
| 286 |
assert_eq!(expiration, |
| 287 |
Some(sq.now() + std::time::Duration::new(24 * 60 * 60, 0))); |
| 288 |
|
| 289 |
let ua = vc.userids().next().expect("have a user ID"); |
| 290 |
if let RevocationStatus::Revoked(_) = ua.revocation_status() { |
| 291 |
} else { |
| 292 |
panic!("User ID should be revoked, but isn't."); |
| 293 |
}; |
| 294 |
} |