| 1 |
use sequoia_openpgp as openpgp; |
| 2 |
use openpgp::Cert; |
| 3 |
use openpgp::cert::amalgamation::ValidAmalgamation; |
| 4 |
use openpgp::parse::Parse; |
| 5 |
use openpgp::types::RevocationStatus; |
| 6 |
use openpgp::types::RevocationType; |
| 7 |
|
| 8 |
use super::common::power_set; |
| 9 |
use super::common::STANDARD_POLICY; |
| 10 |
use super::common::Sq; |
| 11 |
|
| 12 |
/// Check that invalid syntax is caught. |
| 13 |
#[test] |
| 14 |
fn sq_key_subkey_export_syntax() { |
| 15 |
let sq = Sq::new(); |
| 16 |
|
| 17 |
let userid = "alice <[email protected]>"; |
| 18 |
let (cert, cert_path, _rev_path) |
| 19 |
= sq.key_generate(&[], &[userid]); |
| 20 |
sq.key_import(&cert_path); |
| 21 |
|
| 22 |
// A trivial test to make sure it works. |
| 23 |
sq.key_subkey_export(cert.key_handle(), vec![ cert.key_handle() ]); |
| 24 |
|
| 25 |
let fpr = cert.fingerprint().to_string(); |
| 26 |
let subkey = cert.keys().subkeys().next().unwrap().key().fingerprint().to_string(); |
| 27 |
|
| 28 |
// Make sure "--key" is required. |
| 29 |
let mut cmd = sq.command(); |
| 30 |
cmd.args([ |
| 31 |
"key", "subkey", "export", |
| 32 |
"--cert", &fpr, |
| 33 |
]); |
| 34 |
sq.run(cmd, false); |
| 35 |
|
| 36 |
// Make sure "--cert" is specified at most once. |
| 37 |
let mut cmd = sq.command(); |
| 38 |
cmd.args([ |
| 39 |
"key", "subkey", "export", |
| 40 |
"--cert", &fpr, |
| 41 |
"--cert", &fpr, |
| 42 |
"--key", &fpr, |
| 43 |
]); |
| 44 |
sq.run(cmd, false); |
| 45 |
|
| 46 |
// Make sure arguments from the "--cert" family are specified at |
| 47 |
// most once. |
| 48 |
let mut cmd = sq.command(); |
| 49 |
cmd.args([ |
| 50 |
"key", "subkey", "export", |
| 51 |
"--cert", &fpr, |
| 52 |
"--userid", userid, |
| 53 |
"--key", &fpr, |
| 54 |
]); |
| 55 |
sq.run(cmd, false); |
| 56 |
|
| 57 |
// Make sure "--cert" is a primary key. |
| 58 |
let mut cmd = sq.command(); |
| 59 |
cmd.args([ |
| 60 |
"key", "subkey", "export", |
| 61 |
"--cert", &subkey, |
| 62 |
"--key", &fpr, |
| 63 |
]); |
| 64 |
sq.run(cmd, false); |
| 65 |
} |
| 66 |
|
| 67 |
#[test] |
| 68 |
fn by_email() { |
| 69 |
let mut sq = Sq::new(); |
| 70 |
|
| 71 |
let userid = "alice <[email protected]>"; |
| 72 |
let (cert1, cert1_path, _rev_path) |
| 73 |
= sq.key_generate(&[], &[userid]); |
| 74 |
sq.key_import(&cert1_path); |
| 75 |
|
| 76 |
let (cert2, cert2_path, _rev_path) |
| 77 |
= sq.key_generate(&[], &[userid]); |
| 78 |
sq.key_import(&cert2_path); |
| 79 |
|
| 80 |
for i in [0, 1, 2] { |
| 81 |
let mut cmd = sq.command(); |
| 82 |
cmd.args([ |
| 83 |
"key", "subkey", "export", |
| 84 |
"--cert-email", "[email protected]", |
| 85 |
"--key", &cert1.fingerprint().to_string(), |
| 86 |
]); |
| 87 |
let output = sq.run(cmd, i == 1); |
| 88 |
match i { |
| 89 |
0 => { |
| 90 |
// Not linked. |
| 91 |
assert!(! output.status.success()); |
| 92 |
|
| 93 |
// Link cert1. |
| 94 |
sq.tick(1); |
| 95 |
sq.pki_link_add(&[], cert1.key_handle(), &[userid]); |
| 96 |
} |
| 97 |
1 => { |
| 98 |
assert!(output.status.success()); |
| 99 |
let cert = Cert::from_bytes(&output.stdout) |
| 100 |
.expect("can read cert"); |
| 101 |
assert!(cert.is_tsk()); |
| 102 |
|
| 103 |
// Link cert2. |
| 104 |
sq.tick(1); |
| 105 |
sq.pki_link_add(&[], cert2.key_handle(), &[userid]); |
| 106 |
} |
| 107 |
2 => { |
| 108 |
// --email is now ambiguous. |
| 109 |
assert!(! output.status.success()); |
| 110 |
} |
| 111 |
_ => unreachable!(), |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
#[test] |
| 117 |
fn revoked_userid() { |
| 118 |
// Make sure we can export keys from a certificate where all user |
| 119 |
// IDs are revoked. |
| 120 |
|
| 121 |
let sq = Sq::new(); |
| 122 |
|
| 123 |
let cert_path = sq.test_data() |
| 124 |
.join("keys") |
| 125 |
.join("retired-userid.pgp"); |
| 126 |
|
| 127 |
let cert = Cert::from_file(&cert_path).expect("can read"); |
| 128 |
let vc = cert.with_policy(STANDARD_POLICY, sq.now()) |
| 129 |
.expect("valid cert"); |
| 130 |
|
| 131 |
let ua = vc.userids().next().expect("have a user ID"); |
| 132 |
if let RevocationStatus::Revoked(_) = ua.revocation_status() { |
| 133 |
} else { |
| 134 |
panic!("User ID should be revoked, but isn't."); |
| 135 |
}; |
| 136 |
|
| 137 |
sq.key_import(&cert_path); |
| 138 |
|
| 139 |
let keys = cert.keys().map(|k| k.key().fingerprint()).collect::<Vec<_>>(); |
| 140 |
for selection in power_set(&keys) { |
| 141 |
let exported = sq.key_subkey_export( |
| 142 |
vc.key_handle(), selection.clone()); |
| 143 |
for k in exported.keys() { |
| 144 |
if selection.contains(&k.key().fingerprint()) { |
| 145 |
assert!(k.key().has_secret()); |
| 146 |
} else { |
| 147 |
assert!(! k.key().has_secret()); |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
#[test] |
| 154 |
fn hard_revoked_subkey() { |
| 155 |
// Make sure we can export subkeys that are hard revoked. |
| 156 |
|
| 157 |
let sq = Sq::new(); |
| 158 |
|
| 159 |
let cert_path = sq.test_data() |
| 160 |
.join("keys") |
| 161 |
.join("hard-revoked-subkey.pgp"); |
| 162 |
|
| 163 |
let cert = Cert::from_file(&cert_path).expect("can read"); |
| 164 |
let vc = cert.with_policy(STANDARD_POLICY, sq.now()) |
| 165 |
.expect("valid cert"); |
| 166 |
|
| 167 |
let mut hard_revoked_subkeys = 0; |
| 168 |
for ka in vc.keys() { |
| 169 |
if let RevocationStatus::Revoked(sigs) = ka.revocation_status() { |
| 170 |
for sig in sigs { |
| 171 |
let reason = sig.reason_for_revocation(); |
| 172 |
let bad = if let Some((reason, _)) = reason { |
| 173 |
reason.revocation_type() == RevocationType::Hard |
| 174 |
} else { |
| 175 |
true |
| 176 |
}; |
| 177 |
|
| 178 |
if bad { |
| 179 |
hard_revoked_subkeys += 1; |
| 180 |
break; |
| 181 |
} |
| 182 |
} |
| 183 |
} |
| 184 |
} |
| 185 |
assert_eq!(hard_revoked_subkeys, 1); |
| 186 |
|
| 187 |
sq.key_import(&cert_path); |
| 188 |
|
| 189 |
let keys = cert.keys().map(|k| k.key().fingerprint()).collect::<Vec<_>>(); |
| 190 |
for selection in power_set(&keys) { |
| 191 |
let exported = sq.key_subkey_export( |
| 192 |
vc.key_handle(), selection.clone()); |
| 193 |
for k in exported.keys() { |
| 194 |
if selection.contains(&k.key().fingerprint()) { |
| 195 |
assert!(k.key().has_secret()); |
| 196 |
} else { |
| 197 |
assert!(! k.key().has_secret()); |
| 198 |
} |
| 199 |
} |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
#[test] |
| 204 |
fn only_sha1() { |
| 205 |
// Make sure we can export subkeys that are not valid under the |
| 206 |
// standard policy. |
| 207 |
|
| 208 |
let sq = Sq::new(); |
| 209 |
|
| 210 |
let cert_path = sq.test_data() |
| 211 |
.join("keys") |
| 212 |
.join("only-sha1-priv.pgp"); |
| 213 |
|
| 214 |
let cert = Cert::from_file(&cert_path).expect("can read"); |
| 215 |
|
| 216 |
// It shouldn't be valid under the standard policy. |
| 217 |
assert!(cert.with_policy(STANDARD_POLICY, sq.now()).is_err()); |
| 218 |
|
| 219 |
sq.key_import(&cert_path); |
| 220 |
|
| 221 |
let keys = cert.keys().map(|k| k.key().fingerprint()).collect::<Vec<_>>(); |
| 222 |
for selection in power_set(&keys) { |
| 223 |
let exported = sq.key_subkey_export( |
| 224 |
cert.key_handle(), selection.clone()); |
| 225 |
for k in exported.keys() { |
| 226 |
if selection.contains(&k.key().fingerprint()) { |
| 227 |
assert!(k.key().has_secret()); |
| 228 |
} else { |
| 229 |
assert!(! k.key().has_secret()); |
| 230 |
} |
| 231 |
} |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
#[test] |
| 236 |
fn sha1_subkey() { |
| 237 |
// Make sure we can export subkeys that are not valid under the |
| 238 |
// standard policy. |
| 239 |
|
| 240 |
let sq = Sq::new(); |
| 241 |
|
| 242 |
let cert_path = sq.test_data() |
| 243 |
.join("keys") |
| 244 |
.join("sha1-subkey-priv.pgp"); |
| 245 |
|
| 246 |
let cert = Cert::from_file(&cert_path).expect("can read"); |
| 247 |
|
| 248 |
let vc = cert.with_policy(STANDARD_POLICY, sq.now()) |
| 249 |
.expect("valid under standard policy"); |
| 250 |
assert!(cert.keys().count() > vc.keys().count()); |
| 251 |
|
| 252 |
sq.key_import(&cert_path); |
| 253 |
|
| 254 |
let keys = cert.keys().map(|k| k.key().fingerprint()).collect::<Vec<_>>(); |
| 255 |
for selection in power_set(&keys) { |
| 256 |
let exported = sq.key_subkey_export( |
| 257 |
cert.key_handle(), selection.clone()); |
| 258 |
for k in exported.keys() { |
| 259 |
if selection.contains(&k.key().fingerprint()) { |
| 260 |
assert!(k.key().has_secret()); |
| 261 |
} else { |
| 262 |
assert!(! k.key().has_secret()); |
| 263 |
} |
| 264 |
} |
| 265 |
} |
| 266 |
} |