| 1 |
use std::sync::Arc; |
| 2 |
use std::time::SystemTime; |
| 3 |
|
| 4 |
use anyhow::Context; |
| 5 |
|
| 6 |
use sequoia_openpgp as openpgp; |
| 7 |
use openpgp::cert::UserIDRevocationBuilder; |
| 8 |
use openpgp::packet::signature::subpacket::NotationData; |
| 9 |
use openpgp::packet::signature::subpacket::SubpacketTag; |
| 10 |
use openpgp::packet::signature::SignatureBuilder; |
| 11 |
use openpgp::packet::UserID; |
| 12 |
use openpgp::policy::HashAlgoSecurity; |
| 13 |
use openpgp::policy::Policy; |
| 14 |
use openpgp::serialize::Serialize; |
| 15 |
use openpgp::types::ReasonForRevocation; |
| 16 |
use openpgp::types::SignatureType; |
| 17 |
use openpgp::Cert; |
| 18 |
use openpgp::Packet; |
| 19 |
use openpgp::Result; |
| 20 |
|
| 21 |
use sequoia_cert_store as cert_store; |
| 22 |
use cert_store::StoreUpdate; |
| 23 |
|
| 24 |
use crate::Sq; |
| 25 |
use crate::cli::key::userid::RevokeCommand; |
| 26 |
use crate::cli::types::userid_designator::ResolvedUserID; |
| 27 |
use crate::cli; |
| 28 |
use crate::common::RevocationOutput; |
| 29 |
use crate::common::get_secret_signer; |
| 30 |
use crate::common::ui; |
| 31 |
use crate::common::userid::{ |
| 32 |
lint_emails, |
| 33 |
lint_names, |
| 34 |
lint_userids, |
| 35 |
}; |
| 36 |
use crate::sq::TrustThreshold; |
| 37 |
|
| 38 |
/// Handle the revocation of a User ID |
| 39 |
struct UserIDRevocation { |
| 40 |
cert: Cert, |
| 41 |
revoker: Cert, |
| 42 |
revocation_packet: Packet, |
| 43 |
uid: UserID, |
| 44 |
} |
| 45 |
|
| 46 |
impl UserIDRevocation { |
| 47 |
/// Create a new UserIDRevocation |
| 48 |
pub fn new( |
| 49 |
sq: &Sq, |
| 50 |
uid: ResolvedUserID, |
| 51 |
cert: Cert, |
| 52 |
revoker: Option<Cert>, |
| 53 |
reason: ReasonForRevocation, |
| 54 |
message: &str, |
| 55 |
notations: &[(bool, NotationData)], |
| 56 |
) -> Result<Self> { |
| 57 |
let (revoker, mut signer) |
| 58 |
= get_secret_signer(sq, &cert, revoker.as_ref())?; |
| 59 |
|
| 60 |
let revocation_packet = { |
| 61 |
// Create a revocation for a User ID. |
| 62 |
let mut rev = UserIDRevocationBuilder::new() |
| 63 |
.set_reason_for_revocation(reason, message.as_bytes())?; |
| 64 |
rev = rev.set_signature_creation_time(sq.time)?; |
| 65 |
for (critical, notation) in notations { |
| 66 |
rev = rev.add_notation( |
| 67 |
notation.name(), |
| 68 |
notation.value(), |
| 69 |
Some(notation.flags().clone()), |
| 70 |
*critical, |
| 71 |
)?; |
| 72 |
} |
| 73 |
let rev = rev.build( |
| 74 |
&mut signer, |
| 75 |
&cert, |
| 76 |
uid.userid(), |
| 77 |
None, |
| 78 |
)?; |
| 79 |
Packet::Signature(rev) |
| 80 |
}; |
| 81 |
|
| 82 |
Ok(UserIDRevocation { |
| 83 |
cert, |
| 84 |
revoker, |
| 85 |
revocation_packet, |
| 86 |
uid: uid.userid().clone(), |
| 87 |
}) |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
impl RevocationOutput for UserIDRevocation |
| 92 |
{ |
| 93 |
fn cert(&self) -> Result<Cert> { |
| 94 |
let cert = Cert::from_packets(vec![ |
| 95 |
self.cert.primary_key().key().clone().into(), |
| 96 |
self.uid.clone().into(), |
| 97 |
self.revocation_packet.clone(), |
| 98 |
].into_iter())?; |
| 99 |
|
| 100 |
Ok(cert) |
| 101 |
} |
| 102 |
|
| 103 |
fn comment(&self) -> String { |
| 104 |
format!("This is a revocation certificate for \ |
| 105 |
the User ID {} of cert {}.", |
| 106 |
ui::Safe(&self.uid), |
| 107 |
self.cert.fingerprint()) |
| 108 |
} |
| 109 |
|
| 110 |
fn revoker(&self) -> &Cert { |
| 111 |
&self.revoker |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
pub fn dispatch( |
| 116 |
sq: Sq, |
| 117 |
command: cli::key::userid::Command, |
| 118 |
) -> Result<()> { |
| 119 |
match command { |
| 120 |
cli::key::userid::Command::Add(c) => userid_add(sq, c)?, |
| 121 |
cli::key::userid::Command::Revoke(c) => userid_revoke(sq, c)?, |
| 122 |
} |
| 123 |
|
| 124 |
Ok(()) |
| 125 |
} |
| 126 |
|
| 127 |
fn userid_add( |
| 128 |
sq: Sq, |
| 129 |
mut command: cli::key::userid::AddCommand, |
| 130 |
) -> Result<()> { |
| 131 |
let cert = sq.resolve_cert(&command.cert, TrustThreshold::Full)?.0; |
| 132 |
|
| 133 |
let mut signer = sq.get_primary_key(&cert, None)?; |
| 134 |
|
| 135 |
// Names, email addresses, and user IDs. |
| 136 |
lint_names(&command.names)?; |
| 137 |
for n in &command.names { |
| 138 |
command.userid.push(UserID::from(n.as_str())); |
| 139 |
} |
| 140 |
|
| 141 |
lint_emails(&command.emails)?; |
| 142 |
for n in &command.emails { |
| 143 |
command.userid.push(UserID::from_address(None, None, n)?); |
| 144 |
} |
| 145 |
|
| 146 |
// Make sure the user IDs are in canonical form. If not, and |
| 147 |
// `--allow-non-canonical-userids` is not set, error out. |
| 148 |
if ! command.allow_non_canonical_userids { |
| 149 |
lint_userids(&command.userid)?; |
| 150 |
} |
| 151 |
|
| 152 |
// Fail if any of the User IDs to add already exist in the ValidCert |
| 153 |
let cert_userids: Vec<_> = |
| 154 |
cert.userids().map(|u| u.userid().value()).collect(); |
| 155 |
let exists: Vec<_> = command |
| 156 |
.userid |
| 157 |
.iter() |
| 158 |
.filter(|s| cert_userids.contains(&s.value())) |
| 159 |
.collect(); |
| 160 |
if !exists.is_empty() { |
| 161 |
return Err(anyhow::anyhow!( |
| 162 |
"The certificate already contains the User ID(s) {}.", |
| 163 |
exists.iter().map(|s| ui::Safe(*s).to_string()) |
| 164 |
.collect::<Vec<_>>() |
| 165 |
.join(", "), |
| 166 |
)); |
| 167 |
} |
| 168 |
|
| 169 |
let vcert = cert |
| 170 |
.with_policy(sq.policy, sq.time) |
| 171 |
.with_context(|| { |
| 172 |
format!("Certificate {} is not valid", cert.fingerprint()) |
| 173 |
})?; |
| 174 |
|
| 175 |
// Use the primary User ID or direct key signature as template for the |
| 176 |
// SignatureBuilder. |
| 177 |
// |
| 178 |
// XXX: Long term, this functionality belongs next to |
| 179 |
// openpgp/src/cert/builder/key.rs. |
| 180 |
let mut sb = if let Ok(primary_user_id) = vcert.primary_userid() { |
| 181 |
SignatureBuilder::from(primary_user_id.binding_signature().clone()) |
| 182 |
} else if let Ok(direct_key_sig) = vcert.direct_key_signature() { |
| 183 |
SignatureBuilder::from(direct_key_sig.clone()) |
| 184 |
.set_type(SignatureType::PositiveCertification) |
| 185 |
} else { |
| 186 |
// If there is neither a valid uid binding signature, nor a |
| 187 |
// valid direct key signature, we shouldn't have gotten a |
| 188 |
// ValidCert above. |
| 189 |
unreachable!("ValidCert has to have one of the above.") |
| 190 |
}; |
| 191 |
|
| 192 |
// Remove bad algorithms from preferred algorithm subpackets, |
| 193 |
// and make sure preference lists contain at least one good algorithm. |
| 194 |
|
| 195 |
// - symmetric_algorithms |
| 196 |
let mut symmetric_algorithms: Vec<_> = |
| 197 |
sb.preferred_symmetric_algorithms().unwrap_or(&[]).to_vec(); |
| 198 |
symmetric_algorithms |
| 199 |
.retain(|algo| sq.policy.symmetric_algorithm(*algo).is_ok()); |
| 200 |
if symmetric_algorithms.is_empty() { |
| 201 |
symmetric_algorithms.push(Default::default()); |
| 202 |
} |
| 203 |
sb = sb.set_preferred_symmetric_algorithms(symmetric_algorithms)?; |
| 204 |
|
| 205 |
// - hash_algorithms |
| 206 |
let mut hash_algorithms: Vec<_> = |
| 207 |
sb.preferred_hash_algorithms().unwrap_or(&[]).to_vec(); |
| 208 |
hash_algorithms.retain(|algo| { |
| 209 |
sq |
| 210 |
.policy |
| 211 |
.hash_cutoff(*algo, HashAlgoSecurity::CollisionResistance) |
| 212 |
.map(|cutoff| cutoff.lt(&SystemTime::now())) |
| 213 |
.unwrap_or(true) |
| 214 |
}); |
| 215 |
if hash_algorithms.is_empty() { |
| 216 |
hash_algorithms.push(Default::default()); |
| 217 |
} |
| 218 |
sb = sb.set_preferred_hash_algorithms(hash_algorithms)?; |
| 219 |
|
| 220 |
// Remove the following types of SubPacket, if they exist |
| 221 |
#[allow(deprecated)] |
| 222 |
const REMOVE_SUBPACKETS: &[SubpacketTag] = &[ |
| 223 |
// The Signature should be exportable. |
| 224 |
// https://openpgp-wg.gitlab.io/rfc4880bis/#name-exportable-certification |
| 225 |
// "If this packet is not present, the certification is exportable; |
| 226 |
// it is equivalent to a flag containing a 1." |
| 227 |
SubpacketTag::ExportableCertification, |
| 228 |
// PreferredAEADAlgorithms has been removed by WG. |
| 229 |
// It was replaced by `39 Preferred AEAD Ciphersuites`, |
| 230 |
// see https://openpgp-wg.gitlab.io/rfc4880bis/#section-5.2.3.5-7) |
| 231 |
SubpacketTag::PreferredAEADAlgorithms, |
| 232 |
// Strip the primary userid SubPacket |
| 233 |
// (don't implicitly make a User ID primary) |
| 234 |
SubpacketTag::PrimaryUserID, |
| 235 |
// Other SubPacket types that shouldn't be in use in this context |
| 236 |
SubpacketTag::TrustSignature, |
| 237 |
SubpacketTag::RegularExpression, |
| 238 |
SubpacketTag::SignersUserID, |
| 239 |
SubpacketTag::ReasonForRevocation, |
| 240 |
SubpacketTag::SignatureTarget, |
| 241 |
SubpacketTag::EmbeddedSignature, |
| 242 |
SubpacketTag::ApprovedCertifications, |
| 243 |
]; |
| 244 |
|
| 245 |
sb = sb.modify_hashed_area(|mut subpacket_area| { |
| 246 |
REMOVE_SUBPACKETS |
| 247 |
.iter() |
| 248 |
.for_each(|sp| subpacket_area.remove_all(*sp)); |
| 249 |
|
| 250 |
Ok(subpacket_area) |
| 251 |
})?; |
| 252 |
|
| 253 |
// New User ID should only be made primary if explicitly specified by user. |
| 254 |
// xxx: add a parameter to set a new user id as primary? |
| 255 |
|
| 256 |
// Collect packets to add to the key (new User IDs and binding signatures) |
| 257 |
let mut add: Vec<Packet> = vec![]; |
| 258 |
|
| 259 |
// Make new User IDs and binding signatures |
| 260 |
for uid in command.userid { |
| 261 |
let uid: UserID = uid.into(); |
| 262 |
add.push(uid.clone().into()); |
| 263 |
|
| 264 |
// Creation time. |
| 265 |
sb = sb.set_signature_creation_time(sq.time)?; |
| 266 |
|
| 267 |
let binding = uid.bind(&mut signer, &cert, sb.clone())?; |
| 268 |
add.push(binding.into()); |
| 269 |
} |
| 270 |
|
| 271 |
// Merge the new User IDs into cert. |
| 272 |
let cert = cert.insert_packets(add)?.0; |
| 273 |
|
| 274 |
if let Some(output) = command.output { |
| 275 |
let mut sink = output.for_secrets().create_safe(&sq)?; |
| 276 |
if false { |
| 277 |
cert.as_tsk().serialize(&mut sink)?; |
| 278 |
} else { |
| 279 |
cert.as_tsk().armored().serialize(&mut sink)?; |
| 280 |
} |
| 281 |
} else { |
| 282 |
let cert_store = sq.cert_store_or_else()?; |
| 283 |
cert_store.update(Arc::new(cert.into()))?; |
| 284 |
} |
| 285 |
|
| 286 |
Ok(()) |
| 287 |
} |
| 288 |
|
| 289 |
/// Revoke a UserID of an existing primary key |
| 290 |
/// |
| 291 |
/// ## Errors |
| 292 |
/// |
| 293 |
/// Returns an error if reading of the [`Cert`] fails, if retrieval of |
| 294 |
/// [`NotationData`] fails or if the eventual revocation fails. |
| 295 |
pub fn userid_revoke( |
| 296 |
sq: Sq, |
| 297 |
command: RevokeCommand, |
| 298 |
) -> Result<()> { |
| 299 |
let cert = sq.resolve_cert(&command.cert, TrustThreshold::Full)?.0; |
| 300 |
// To revoke a user ID, we require the certificate be valid under |
| 301 |
// the current policy. Users can still revoke user IDs whose |
| 302 |
// binding signature relies on weak cryptography using |
| 303 |
// `--add-user`. |
| 304 |
let vcert = cert.with_policy(sq.policy, sq.time) |
| 305 |
.with_context(|| { |
| 306 |
format!("The certificate is not valid under the current \ |
| 307 |
policy. Consider revoking the whole certificate \ |
| 308 |
using `sq key revoke`, or fixing it using \ |
| 309 |
`sq cert lint` after verifying the certificate's \ |
| 310 |
integrity.") |
| 311 |
})?; |
| 312 |
let userids = command.userids.resolve(&vcert)?; |
| 313 |
assert_eq!(userids.len(), 1, "exactly one user ID enforced by clap"); |
| 314 |
let userid = userids.into_iter().next().unwrap(); |
| 315 |
|
| 316 |
let revoker = if command.revoker.is_empty() { |
| 317 |
None |
| 318 |
} else { |
| 319 |
Some(sq.resolve_cert(&command.revoker, TrustThreshold::Full)?.0) |
| 320 |
}; |
| 321 |
|
| 322 |
let notations = command.signature_notations.parse()?; |
| 323 |
|
| 324 |
let revocation = UserIDRevocation::new( |
| 325 |
&sq, |
| 326 |
userid, |
| 327 |
cert, |
| 328 |
revoker, |
| 329 |
command.reason.into(), |
| 330 |
&command.message, |
| 331 |
¬ations, |
| 332 |
)?; |
| 333 |
|
| 334 |
revocation.write(&sq, command.output, false)?; |
| 335 |
|
| 336 |
Ok(()) |
| 337 |
} |