| 1 |
//! Changes key expiration. |
| 2 |
|
| 3 |
use std::sync::Arc; |
| 4 |
|
| 5 |
use typenum::Unsigned; |
| 6 |
|
| 7 |
use sequoia_openpgp as openpgp; |
| 8 |
use openpgp::Packet; |
| 9 |
use openpgp::Result; |
| 10 |
use openpgp::cert::amalgamation::ValidAmalgamation; |
| 11 |
use openpgp::cert::amalgamation::key::PrimaryKey; |
| 12 |
use openpgp::packet::signature::SignatureBuilder; |
| 13 |
use openpgp::serialize::Serialize; |
| 14 |
use openpgp::types::RevocationStatus; |
| 15 |
use openpgp::types::SignatureType; |
| 16 |
|
| 17 |
use sequoia_cert_store::StoreUpdate; |
| 18 |
|
| 19 |
use crate::Sq; |
| 20 |
use crate::cli::types::CertDesignators; |
| 21 |
use crate::cli::types::Expiration; |
| 22 |
use crate::cli::types::FileOrStdout; |
| 23 |
use crate::cli::types::KeyDesignators; |
| 24 |
use crate::cli::types::cert_designator; |
| 25 |
use crate::sq::GetKeysOptions; |
| 26 |
use crate::sq::TrustThreshold; |
| 27 |
|
| 28 |
/// cert must resolve to a single certificate. |
| 29 |
/// |
| 30 |
/// If `keys` is `None`, the primary key's expiration is changed. |
| 31 |
/// Otherwise the expiration of each of the specified keys is |
| 32 |
/// extended. |
| 33 |
pub fn expire<A, P, O, D>(sq: Sq, |
| 34 |
cert: CertDesignators<A, P, O, D>, |
| 35 |
keys: Option<KeyDesignators>, |
| 36 |
expiration: Expiration, |
| 37 |
mut output: Option<FileOrStdout>, |
| 38 |
binary: bool) |
| 39 |
-> Result<()> |
| 40 |
where P: cert_designator::ArgumentPrefix, |
| 41 |
O: typenum::Unsigned, |
| 42 |
{ |
| 43 |
let options = O::to_usize(); |
| 44 |
let one_value |
| 45 |
= (options & cert_designator::OneValue::to_usize()) > 0; |
| 46 |
assert!(one_value); |
| 47 |
let optional_value |
| 48 |
= (options & cert_designator::OptionalValue::to_usize()) > 0; |
| 49 |
assert!(! optional_value); |
| 50 |
|
| 51 |
let (cert, cert_handle) |
| 52 |
= sq.resolve_cert(&cert, TrustThreshold::Full)?; |
| 53 |
|
| 54 |
let vc = cert.with_policy(sq.policy, sq.time)?; |
| 55 |
|
| 56 |
let keys = if let Some(keys) = keys { |
| 57 |
sq.resolve_keys(&vc, &cert_handle, &keys, false)? |
| 58 |
} else { |
| 59 |
// The primary key. |
| 60 |
vec![ vc.keys().next().expect("have a primary key") ] |
| 61 |
}; |
| 62 |
|
| 63 |
if cert_handle.is_file() { |
| 64 |
if output.is_none() { |
| 65 |
// None means to write to the cert store. When reading |
| 66 |
// from a file, we want to write to stdout by default. |
| 67 |
output = Some(FileOrStdout::new(None)); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
let mut primary_signer |
| 72 |
= sq.get_primary_key(&cert, Some(&[GetKeysOptions::AllowNotAlive]))?; |
| 73 |
|
| 74 |
// Fix the new expiration time. |
| 75 |
let expiration_time = expiration.to_system_time(sq.time)?; |
| 76 |
|
| 77 |
let (primary, subkeys): (Vec<_>, Vec<_>) |
| 78 |
= keys.into_iter().partition(|ka| ka.primary()); |
| 79 |
assert!(primary.len() <= 1); |
| 80 |
|
| 81 |
// Collect new signatures here, then canonicalize once. |
| 82 |
let mut acc = Vec::<Packet>::new(); |
| 83 |
|
| 84 |
// To update subkey expiration times, create new binding |
| 85 |
// signatures. |
| 86 |
for ka in subkeys { |
| 87 |
// Use the binding signature under our policy as the template. |
| 88 |
let template = ka.binding_signature(); |
| 89 |
|
| 90 |
// Push a copy of the key to make reordering easier. |
| 91 |
acc.push(Packet::from(ka.key().clone().role_into_subordinate())); |
| 92 |
acc.push(ka.key().role_as_subordinate().bind( |
| 93 |
&mut primary_signer, |
| 94 |
&cert, |
| 95 |
SignatureBuilder::from(template.clone()) |
| 96 |
.set_signature_creation_time(sq.time)? |
| 97 |
.set_key_expiration_time(ka.key(), expiration_time)?)? |
| 98 |
.into()); |
| 99 |
} |
| 100 |
|
| 101 |
// To change the cert's expiration time, create a new direct key |
| 102 |
// signature and new binding signatures for the user IDs. |
| 103 |
if let Some(ka) = primary.get(0) { |
| 104 |
let template = ka.binding_signature().clone(); |
| 105 |
|
| 106 |
// Clean the template. |
| 107 |
let template = SignatureBuilder::from(template) |
| 108 |
.modify_unhashed_area(|mut a| { |
| 109 |
a.clear(); |
| 110 |
Ok(a) |
| 111 |
})? |
| 112 |
.modify_hashed_area(|mut a| { |
| 113 |
// XXX: Rework once |
| 114 |
// https://gitlab.com/sequoia-pgp/sequoia/-/issues/1127 |
| 115 |
// is available. |
| 116 |
let mut strip_tags: std::collections::BTreeSet<_> = |
| 117 |
a.iter().map(|s| s.tag()).collect(); |
| 118 |
|
| 119 |
// Symbolic names for the policy below. |
| 120 |
let strip = true; |
| 121 |
let keep = false; |
| 122 |
|
| 123 |
use openpgp::packet::signature::subpacket::SubpacketTag::*; |
| 124 |
#[allow(deprecated)] |
| 125 |
strip_tags.retain(|t| match t { |
| 126 |
// These we keep. |
| 127 |
PreferredSymmetricAlgorithms => keep, |
| 128 |
RevocationKey => keep, |
| 129 |
NotationData => keep, |
| 130 |
PreferredHashAlgorithms => keep, |
| 131 |
PreferredCompressionAlgorithms => keep, |
| 132 |
KeyServerPreferences => keep, |
| 133 |
PreferredKeyServer => keep, |
| 134 |
PolicyURI => keep, |
| 135 |
KeyFlags => keep, |
| 136 |
Features => keep, |
| 137 |
// XXXv6: PreferredAEADCiphersuites => keep, |
| 138 |
|
| 139 |
// Oddballs, keep them. |
| 140 |
Reserved(_) => keep, |
| 141 |
Private(_) => keep, |
| 142 |
Unknown(_) => keep, |
| 143 |
|
| 144 |
// These we want to strip. |
| 145 |
SignatureCreationTime => strip, |
| 146 |
SignatureExpirationTime => strip, |
| 147 |
ExportableCertification => strip, |
| 148 |
TrustSignature => strip, |
| 149 |
RegularExpression => strip, |
| 150 |
Revocable => strip, |
| 151 |
KeyExpirationTime => strip, |
| 152 |
PlaceholderForBackwardCompatibility => strip, |
| 153 |
Issuer => strip, |
| 154 |
PrimaryUserID => strip, |
| 155 |
SignersUserID => strip, |
| 156 |
ReasonForRevocation => strip, |
| 157 |
SignatureTarget => strip, |
| 158 |
EmbeddedSignature => strip, |
| 159 |
IssuerFingerprint => strip, |
| 160 |
PreferredAEADAlgorithms => strip, // Note, "v5". |
| 161 |
IntendedRecipient => strip, |
| 162 |
ApprovedCertifications => strip, |
| 163 |
|
| 164 |
// Enum is non-exhaustive, conservative choice is |
| 165 |
// to keep unknown subpackets. |
| 166 |
_ => keep, |
| 167 |
}); |
| 168 |
|
| 169 |
for t in strip_tags { |
| 170 |
a.remove_all(t); |
| 171 |
} |
| 172 |
|
| 173 |
Ok(a) |
| 174 |
})?; |
| 175 |
|
| 176 |
// We can't add a copy of the primary key, as that's not |
| 177 |
// allowed by `Cert::insert_packets`. But it's easy to |
| 178 |
// reorder direct key signatures as there is only a single |
| 179 |
// possible component, the primary key. |
| 180 |
acc.push(template |
| 181 |
.set_type(SignatureType::DirectKey) |
| 182 |
.set_signature_creation_time(sq.time)? |
| 183 |
.set_key_expiration_time(cert.primary_key().key(), |
| 184 |
expiration_time)? |
| 185 |
.sign_direct_key(&mut primary_signer, None)? |
| 186 |
.into()); |
| 187 |
|
| 188 |
for uidb in vc.userids() { |
| 189 |
if let RevocationStatus::Revoked(_) = uidb.revocation_status() { |
| 190 |
// The user ID is revoked. Skip it. (Adding a new |
| 191 |
// self signature would actually "unrevoke it"!) |
| 192 |
continue; |
| 193 |
} |
| 194 |
|
| 195 |
// Use the binding signature that is valid under our |
| 196 |
// policy as of the reference time. |
| 197 |
let template = uidb.binding_signature().clone(); |
| 198 |
|
| 199 |
// Push a copy of the user ID to make reordering easier. |
| 200 |
acc.push(Packet::from(uidb.userid().clone())); |
| 201 |
acc.push(uidb.userid().bind( |
| 202 |
&mut primary_signer, |
| 203 |
&cert, |
| 204 |
SignatureBuilder::from(template) |
| 205 |
.set_signature_creation_time(sq.time)? |
| 206 |
.set_key_expiration_time(cert.primary_key().key(), |
| 207 |
expiration_time)?)? |
| 208 |
.into()); |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
// Merge and canonicalize. |
| 213 |
let cert = cert.insert_packets(acc)?.0; |
| 214 |
|
| 215 |
if let Some(sink) = output { |
| 216 |
let path = sink.path().map(Clone::clone); |
| 217 |
let mut output = sink.for_secrets().create_safe(&sq)?; |
| 218 |
if binary { |
| 219 |
cert.as_tsk().serialize(&mut output)?; |
| 220 |
} else { |
| 221 |
cert.as_tsk().armored().serialize(&mut output)?; |
| 222 |
} |
| 223 |
|
| 224 |
if let Some(path) = path { |
| 225 |
sq.hint(format_args!( |
| 226 |
"Updated certificate written to {}. \ |
| 227 |
To make the update effective, it has to be published \ |
| 228 |
so that others can find it, for example using:", |
| 229 |
path.display())) |
| 230 |
.sq().arg("network").arg("keyserver").arg("publish") |
| 231 |
.arg_value("--cert-file", path.display()) |
| 232 |
.done(); |
| 233 |
} else { |
| 234 |
sq.hint(format_args!( |
| 235 |
"To make the update effective, it has to be published \ |
| 236 |
so that others can find it.")); |
| 237 |
} |
| 238 |
} else { |
| 239 |
let cert_store = sq.cert_store_or_else()?; |
| 240 |
|
| 241 |
let fipr = cert.fingerprint(); |
| 242 |
if let Err(err) = cert_store.update(Arc::new(cert.into())) { |
| 243 |
weprintln!("Error importing updated cert: {}", err); |
| 244 |
return Err(err); |
| 245 |
} else { |
| 246 |
sq.hint(format_args!( |
| 247 |
"Imported updated cert into the cert store. \ |
| 248 |
To make the update effective, it has to be published \ |
| 249 |
so that others can find it, for example using:")) |
| 250 |
.sq().arg("network").arg("keyserver").arg("publish") |
| 251 |
.arg_value("--cert", fipr) |
| 252 |
.done(); |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
Ok(()) |
| 257 |
} |