| 1 |
use std::collections::HashSet; |
| 2 |
|
| 3 |
use sequoia_openpgp as openpgp; |
| 4 |
use openpgp::Cert; |
| 5 |
use openpgp::Fingerprint; |
| 6 |
use openpgp::Result; |
| 7 |
use openpgp::cert::amalgamation::ValidAmalgamation; |
| 8 |
use openpgp::parse::Parse; |
| 9 |
use openpgp::types::RevocationStatus; |
| 10 |
|
| 11 |
use super::common::FileOrKeyHandle; |
| 12 |
use super::common::STANDARD_POLICY; |
| 13 |
use super::common::Sq; |
| 14 |
|
| 15 |
#[test] |
| 16 |
fn sq_key_password() -> Result<()> { |
| 17 |
let mut sq = Sq::new(); |
| 18 |
|
| 19 |
let (cert, cert_path, _rev_path) = sq.key_generate(&[], &["alice"]); |
| 20 |
|
| 21 |
let orig_password = sq.scratch_file("orig-password.txt"); |
| 22 |
std::fs::write(&orig_password, "t00 ez").unwrap(); |
| 23 |
|
| 24 |
let new_password = sq.scratch_file("new-password.txt"); |
| 25 |
std::fs::write(&new_password, "crazy passw0rd").unwrap(); |
| 26 |
|
| 27 |
let msg_txt = sq.scratch_file("msg.txt"); |
| 28 |
std::fs::write(&msg_txt, "hello world").unwrap(); |
| 29 |
|
| 30 |
|
| 31 |
for keystore in [false, true] { |
| 32 |
eprintln!("Keystore: {}", keystore); |
| 33 |
|
| 34 |
// Two days go by. |
| 35 |
sq.tick(2 * 24 * 60 * 60); |
| 36 |
|
| 37 |
if keystore { |
| 38 |
sq.key_import(&cert_path); |
| 39 |
} |
| 40 |
|
| 41 |
let cert_handle = if keystore { |
| 42 |
FileOrKeyHandle::from(cert.fingerprint()) |
| 43 |
} else { |
| 44 |
cert_path.as_path().into() |
| 45 |
}; |
| 46 |
|
| 47 |
// Sign a message. No password should be required. |
| 48 |
sq.sign(&cert_handle, None, msg_txt.as_path(), None); |
| 49 |
|
| 50 |
// Change the key's password. |
| 51 |
eprintln!("Change the key's password."); |
| 52 |
let cert_updated = sq.scratch_file("cert-updated"); |
| 53 |
let cert = sq.key_password( |
| 54 |
&cert_handle, |
| 55 |
None, Some(&new_password), |
| 56 |
if keystore { None } else { Some(cert_updated.as_path()) }); |
| 57 |
assert!(cert.keys().all(|ka| { |
| 58 |
ka.key().has_secret() |
| 59 |
&& ! ka.key().has_unencrypted_secret() |
| 60 |
})); |
| 61 |
|
| 62 |
let cert_handle = if keystore { |
| 63 |
FileOrKeyHandle::from(cert.fingerprint()) |
| 64 |
} else { |
| 65 |
cert_updated.as_path().into() |
| 66 |
}; |
| 67 |
|
| 68 |
// Sign a message. |
| 69 |
sq.sign(&cert_handle, |
| 70 |
Some(new_password.as_path()), |
| 71 |
msg_txt.as_path(), None); |
| 72 |
|
| 73 |
// Clear the key's password. |
| 74 |
eprintln!("Clear the key's password."); |
| 75 |
let cert_updated2 = sq.scratch_file("cert-updated2"); |
| 76 |
|
| 77 |
let cert = sq.key_password( |
| 78 |
&cert_handle, |
| 79 |
Some(&new_password), None, |
| 80 |
if keystore { None } else { Some(cert_updated2.as_path()) }); |
| 81 |
assert!(cert.keys().all(|ka| ka.key().has_unencrypted_secret())); |
| 82 |
|
| 83 |
let cert_handle = if keystore { |
| 84 |
FileOrKeyHandle::from(cert.fingerprint()) |
| 85 |
} else { |
| 86 |
cert_updated2.as_path().into() |
| 87 |
}; |
| 88 |
|
| 89 |
// Sign a message. |
| 90 |
sq.sign(&cert_handle, None, msg_txt.as_path(), None); |
| 91 |
} |
| 92 |
|
| 93 |
Ok(()) |
| 94 |
} |
| 95 |
|
| 96 |
#[test] |
| 97 |
fn unbound_subkey() { |
| 98 |
// Make sure we don't change the password for an unbound subkey. |
| 99 |
|
| 100 |
let sq = Sq::new(); |
| 101 |
|
| 102 |
let new_password = sq.scratch_file("new-password.txt"); |
| 103 |
std::fs::write(&new_password, "crazy passw0rd").unwrap(); |
| 104 |
|
| 105 |
let cert_path = sq.test_data() |
| 106 |
.join("keys") |
| 107 |
.join("unbound-subkey.pgp"); |
| 108 |
|
| 109 |
let cert = Cert::from_file(&cert_path).expect("can read"); |
| 110 |
let vc = cert.with_policy(STANDARD_POLICY, sq.now()) |
| 111 |
.expect("valid cert"); |
| 112 |
|
| 113 |
// One subkey should be considered invalid. |
| 114 |
let bound: HashSet<Fingerprint> |
| 115 |
= HashSet::from_iter(vc.keys().map(|ka| ka.key().fingerprint())); |
| 116 |
let all: HashSet<Fingerprint> |
| 117 |
= HashSet::from_iter(cert.keys().map(|ka| ka.key().fingerprint())); |
| 118 |
assert!(bound.len() < all.len()); |
| 119 |
|
| 120 |
|
| 121 |
let result = sq.key_password( |
| 122 |
&cert_path, None, Some(&new_password), None); |
| 123 |
|
| 124 |
// Make sure the password for the unbound key was not changed. |
| 125 |
for ka in result.keys() { |
| 126 |
if bound.contains(&ka.key().fingerprint()) { |
| 127 |
assert!(! ka.key().has_unencrypted_secret()); |
| 128 |
} else { |
| 129 |
assert!(ka.key().has_unencrypted_secret()); |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
#[test] |
| 135 |
fn soft_revoked_subkey() { |
| 136 |
// Make sure we change the password for a soft revoked subkey. |
| 137 |
|
| 138 |
let sq = Sq::new(); |
| 139 |
|
| 140 |
let new_password = sq.scratch_file("new-password.txt"); |
| 141 |
std::fs::write(&new_password, "crazy passw0rd").unwrap(); |
| 142 |
|
| 143 |
let cert_path = sq.test_data() |
| 144 |
.join("keys") |
| 145 |
.join("soft-revoked-subkey.pgp"); |
| 146 |
|
| 147 |
let cert = Cert::from_file(&cert_path).expect("can read"); |
| 148 |
let vc = cert.with_policy(STANDARD_POLICY, sq.now()) |
| 149 |
.expect("valid cert"); |
| 150 |
|
| 151 |
// Make sure the revoked key is there and is really revoked. |
| 152 |
let mut revoked = None; |
| 153 |
for k in vc.keys().subkeys() { |
| 154 |
if let RevocationStatus::Revoked(_) = k.revocation_status() { |
| 155 |
assert!(revoked.is_none(), |
| 156 |
"Only expected a single revoked subkey"); |
| 157 |
revoked = Some(k.key().key_handle()); |
| 158 |
} |
| 159 |
} |
| 160 |
if revoked.is_none() { |
| 161 |
panic!("Expected a revoked subkey, but didn't fine one"); |
| 162 |
} |
| 163 |
|
| 164 |
let updated = sq.key_password( |
| 165 |
cert_path, None, Some(new_password.as_path()), None); |
| 166 |
for ka in updated.keys() { |
| 167 |
assert!(! ka.key().has_unencrypted_secret()); |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
#[test] |
| 172 |
fn hard_revoked_subkey() { |
| 173 |
// Make sure we can delete a hard revoked subkey. |
| 174 |
|
| 175 |
let sq = Sq::new(); |
| 176 |
|
| 177 |
let new_password = sq.scratch_file("new-password.txt"); |
| 178 |
std::fs::write(&new_password, "crazy passw0rd").unwrap(); |
| 179 |
|
| 180 |
let cert_path = sq.test_data() |
| 181 |
.join("keys") |
| 182 |
.join("hard-revoked-subkey.pgp"); |
| 183 |
|
| 184 |
let cert = Cert::from_file(&cert_path).expect("can read"); |
| 185 |
let vc = cert.with_policy(STANDARD_POLICY, sq.now()) |
| 186 |
.expect("valid cert"); |
| 187 |
|
| 188 |
// Make sure the revoked key is there and is really revoked. |
| 189 |
let mut revoked = None; |
| 190 |
for k in vc.keys().subkeys() { |
| 191 |
if let RevocationStatus::Revoked(_) = k.revocation_status() { |
| 192 |
assert!(revoked.is_none(), |
| 193 |
"Only expected a single revoked subkey"); |
| 194 |
revoked = Some(k.key().key_handle()); |
| 195 |
} |
| 196 |
} |
| 197 |
if revoked.is_none() { |
| 198 |
panic!("Expected a revoked subkey, but didn't fine one"); |
| 199 |
} |
| 200 |
|
| 201 |
let updated = sq.key_password( |
| 202 |
cert_path, None, Some(new_password.as_path()), None); |
| 203 |
for ka in updated.keys() { |
| 204 |
assert!(! ka.key().has_unencrypted_secret()); |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
#[test] |
| 209 |
fn sha1_subkey() { |
| 210 |
// Make sure we can change the password of keys that are bound |
| 211 |
// using SHA-1. |
| 212 |
|
| 213 |
let sq = Sq::new(); |
| 214 |
|
| 215 |
let new_password = sq.scratch_file("new-password.txt"); |
| 216 |
std::fs::write(&new_password, "crazy passw0rd").unwrap(); |
| 217 |
|
| 218 |
let cert_path = sq.test_data() |
| 219 |
.join("keys") |
| 220 |
.join("sha1-subkey-priv.pgp"); |
| 221 |
|
| 222 |
let cert = Cert::from_file(&cert_path).expect("can read"); |
| 223 |
let vc = cert.with_policy(STANDARD_POLICY, sq.now()) |
| 224 |
.expect("valid cert"); |
| 225 |
|
| 226 |
// Make sure the subkey key is there and really uses SHA-1. |
| 227 |
let valid_subkeys: Vec<_> = vc.keys().subkeys() |
| 228 |
.map(|ka| ka.key().fingerprint()) |
| 229 |
.collect(); |
| 230 |
let all_subkeys: Vec<_> = cert.keys().subkeys() |
| 231 |
.map(|ka| ka.key().fingerprint()) |
| 232 |
.collect(); |
| 233 |
|
| 234 |
assert_eq!(valid_subkeys.len(), 0); |
| 235 |
assert_eq!(all_subkeys.len(), 1); |
| 236 |
|
| 237 |
let updated = sq.key_password( |
| 238 |
cert_path, None, Some(new_password.as_path()), None); |
| 239 |
for ka in updated.keys() { |
| 240 |
assert!(! ka.key().has_unencrypted_secret()); |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
#[test] |
| 245 |
fn subkey_without_secret_key_material() { |
| 246 |
// Make sure we can change the password of keys where some of the |
| 247 |
// subkeys are missing secret key material. |
| 248 |
|
| 249 |
let sq = Sq::new(); |
| 250 |
|
| 251 |
let new_password = sq.scratch_file("new-password.txt"); |
| 252 |
std::fs::write(&new_password, "crazy passw0rd").unwrap(); |
| 253 |
|
| 254 |
let (cert, cert_path, _rev_path) = sq.key_generate(&[], &["alice"]); |
| 255 |
|
| 256 |
// Delete some secret key material. |
| 257 |
let stripped = cert.keys().subkeys().next().unwrap().key(); |
| 258 |
|
| 259 |
let update = sq.scratch_file( |
| 260 |
Some(&format!("delete-{}", stripped.fingerprint())[..])); |
| 261 |
sq.key_subkey_delete( |
| 262 |
cert_path, &[stripped.key_handle()], update.as_path()); |
| 263 |
|
| 264 |
// Make sure it is stripped. |
| 265 |
let cert = Cert::from_file(&update).expect("can read"); |
| 266 |
for ka in cert.keys() { |
| 267 |
if ka.key().fingerprint() == stripped.fingerprint() { |
| 268 |
assert!(! ka.key().has_secret(), |
| 269 |
"{} still has secret key material", ka.key().fingerprint()); |
| 270 |
} else { |
| 271 |
assert!(ka.key().has_secret()); |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
let updated = sq.key_password( |
| 276 |
&update, None, Some(new_password.as_path()), None); |
| 277 |
for ka in updated.keys() { |
| 278 |
assert!(! ka.key().has_unencrypted_secret()); |
| 279 |
} |
| 280 |
} |