| 1 |
use std::sync::Arc; |
| 2 |
use std::time::Duration; |
| 3 |
use std::time::SystemTime; |
| 4 |
|
| 5 |
use anyhow::Context; |
| 6 |
|
| 7 |
use chrono::DateTime; |
| 8 |
use chrono::Utc; |
| 9 |
|
| 10 |
use sequoia_openpgp as openpgp; |
| 11 |
use openpgp::Cert; |
| 12 |
use openpgp::Result; |
| 13 |
use openpgp::packet::prelude::*; |
| 14 |
use openpgp::packet::signature::subpacket::NotationData; |
| 15 |
use openpgp::packet::signature::subpacket::NotationDataFlags; |
| 16 |
use openpgp::serialize::Serialize; |
| 17 |
use openpgp::types::RevocationStatus; |
| 18 |
use openpgp::types::SignatureType; |
| 19 |
|
| 20 |
use sequoia_cert_store as cert_store; |
| 21 |
use cert_store::StoreUpdate; |
| 22 |
use cert_store::store::UserIDQueryParams; |
| 23 |
|
| 24 |
use crate::Convert; |
| 25 |
use crate::Sq; |
| 26 |
use crate::cli::types::Expiration; |
| 27 |
use crate::cli::types::FileOrStdout; |
| 28 |
use crate::cli::types::TrustAmount; |
| 29 |
use crate::cli::types::userid_designator::ResolvedUserID; |
| 30 |
use crate::commands::active_certification; |
| 31 |
use crate::common::ui; |
| 32 |
|
| 33 |
// Returns whether two certifications have different parameters. |
| 34 |
// |
| 35 |
// Returns true if the parameters are different. Returns false if |
| 36 |
// they are the same. |
| 37 |
// |
| 38 |
// This does some normalization and only considers things that are |
| 39 |
// relevant to certifications: |
| 40 |
// |
| 41 |
// - Expiration time |
| 42 |
// - Trust depth |
| 43 |
// - Trust amount |
| 44 |
// - Regular expressions |
| 45 |
// - Notations |
| 46 |
// - Exportable |
| 47 |
pub fn diff_certification(unless_quiet: &mut dyn std::io::Write, |
| 48 |
old: &Signature, new: &SignatureBuilder, |
| 49 |
new_ct: SystemTime) |
| 50 |
-> bool |
| 51 |
{ |
| 52 |
let mut changed = false; |
| 53 |
|
| 54 |
let a_expiration = old.signature_expiration_time(); |
| 55 |
let b_expiration = if let Some(vp) = new.signature_validity_period() { |
| 56 |
Some(new_ct + vp) |
| 57 |
} else { |
| 58 |
None |
| 59 |
}; |
| 60 |
if a_expiration != b_expiration { |
| 61 |
changed = true; |
| 62 |
wwriteln!(stream = unless_quiet, initial_indent = " - ", |
| 63 |
"updating expiration time: {} -> {}", |
| 64 |
if let Some(a_expiration) = a_expiration { |
| 65 |
chrono::DateTime::<chrono::offset::Utc>::from( |
| 66 |
a_expiration).to_string() |
| 67 |
} else { |
| 68 |
"no expiration".to_string() |
| 69 |
}, |
| 70 |
if let Some(b_expiration) = b_expiration { |
| 71 |
chrono::DateTime::<chrono::offset::Utc>::from( |
| 72 |
b_expiration).to_string() |
| 73 |
} else { |
| 74 |
"no expiration".to_string() |
| 75 |
}); |
| 76 |
} |
| 77 |
|
| 78 |
let (a_depth, a_amount) = old.trust_signature().unwrap_or((0, 120)); |
| 79 |
let (b_depth, b_amount) = new.trust_signature().unwrap_or((0, 120)); |
| 80 |
|
| 81 |
if a_amount != b_amount { |
| 82 |
changed = true; |
| 83 |
wwriteln!(stream = unless_quiet, initial_indent = " - ", |
| 84 |
"updating trust amount: {} -> {}", |
| 85 |
a_amount, b_amount); |
| 86 |
} |
| 87 |
if a_depth != b_depth { |
| 88 |
changed = true; |
| 89 |
wwriteln!(stream = unless_quiet, initial_indent = " - ", |
| 90 |
"updating trust depth: {} -> {}", |
| 91 |
a_depth, b_depth); |
| 92 |
} |
| 93 |
|
| 94 |
let mut a_regex: Vec<_> = old.regular_expressions().collect(); |
| 95 |
a_regex.sort(); |
| 96 |
a_regex.dedup(); |
| 97 |
let mut b_regex: Vec<_> = new.regular_expressions().collect(); |
| 98 |
b_regex.sort(); |
| 99 |
b_regex.dedup(); |
| 100 |
|
| 101 |
if a_regex != b_regex { |
| 102 |
changed = true; |
| 103 |
wwriteln!(stream = unless_quiet, initial_indent = " - ", |
| 104 |
"updating regular expressions"); |
| 105 |
|
| 106 |
wwriteln!(stream = unless_quiet, initial_indent = " - ", |
| 107 |
"current certification"); |
| 108 |
for (i, r) in a_regex.into_iter().enumerate() { |
| 109 |
wwriteln!(stream = unless_quiet, initial_indent = " - ", |
| 110 |
"{}. {}", i + 1, ui::Safe(r)); |
| 111 |
} |
| 112 |
|
| 113 |
wwriteln!(stream = unless_quiet, initial_indent = " - ", |
| 114 |
"new certification"); |
| 115 |
for (i, r) in b_regex.into_iter().enumerate() { |
| 116 |
wwriteln!(stream = unless_quiet, initial_indent = " - ", |
| 117 |
"{}. {}", i + 1, ui::Safe(r)); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
let a_notations: Vec<_> = old.notation_data() |
| 122 |
.filter(|n| n.name() != "[email protected]") |
| 123 |
.collect(); |
| 124 |
let b_notations: Vec<_> = new.notation_data() |
| 125 |
.filter(|n| n.name() != "[email protected]") |
| 126 |
.collect(); |
| 127 |
if a_notations != b_notations { |
| 128 |
changed = true; |
| 129 |
wwriteln!(stream = unless_quiet, initial_indent = " - ", |
| 130 |
"updating notations"); |
| 131 |
|
| 132 |
wwriteln!(stream = unless_quiet, initial_indent = " - ", |
| 133 |
"current certification"); |
| 134 |
for (i, n) in a_notations.into_iter().enumerate() { |
| 135 |
wwriteln!(stream = unless_quiet, initial_indent = " - ", |
| 136 |
"{}. {}", i + 1, ui::Safe(n)); |
| 137 |
} |
| 138 |
|
| 139 |
wwriteln!(stream = unless_quiet, initial_indent = " - ", |
| 140 |
"updated certification"); |
| 141 |
for (i, n) in b_notations.into_iter().enumerate() { |
| 142 |
wwriteln!(stream = unless_quiet, initial_indent = " - ", |
| 143 |
"{}. {}", i + 1, ui::Safe(n)); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
let a_exportable = old.exportable_certification().unwrap_or(true); |
| 148 |
let b_exportable = new.exportable_certification().unwrap_or(true); |
| 149 |
if a_exportable != b_exportable { |
| 150 |
changed = true; |
| 151 |
wwriteln!(stream = unless_quiet, initial_indent = " - ", |
| 152 |
"updating exportable flag: {} -> {}", |
| 153 |
a_exportable, b_exportable); |
| 154 |
} |
| 155 |
|
| 156 |
changed |
| 157 |
} |
| 158 |
|
| 159 |
/// This function is used for certifications and retractions. |
| 160 |
/// |
| 161 |
/// If the trust amount is 0, the operation is interpreted as a |
| 162 |
/// retraction and the wording is changed accordingly. |
| 163 |
pub fn certify(o: &mut dyn std::io::Write, |
| 164 |
sq: &Sq, |
| 165 |
recreate: bool, |
| 166 |
certifier: &Cert, |
| 167 |
cert: &Cert, |
| 168 |
userids: &[ResolvedUserID], |
| 169 |
user_supplied_userids: bool, |
| 170 |
templates: &[(TrustAmount<u8>, Expiration)], |
| 171 |
trust_depth: u8, |
| 172 |
domain: &[String], |
| 173 |
regex: &[String], |
| 174 |
local: bool, |
| 175 |
non_revocable: bool, |
| 176 |
notations: &[(bool, NotationData)], |
| 177 |
output: Option<FileOrStdout>, |
| 178 |
binary: bool) |
| 179 |
-> Result<()> |
| 180 |
{ |
| 181 |
assert!(templates.len() > 0); |
| 182 |
assert!(userids.len() > 0); |
| 183 |
|
| 184 |
let unless_quiet = if sq.quiet() { |
| 185 |
&mut std::io::sink() |
| 186 |
} else { |
| 187 |
o |
| 188 |
}; |
| 189 |
|
| 190 |
|
| 191 |
if certifier.fingerprint() == cert.fingerprint() { |
| 192 |
sq.hint( |
| 193 |
format_args!("\ |
| 194 |
The certificate to certify is the same as the certificate being certified. \ |
| 195 |
If you are trying to add a user ID, try:")) |
| 196 |
.sq().arg("key").arg("userid").arg("add") |
| 197 |
.arg_value("--cert", cert.fingerprint()) |
| 198 |
.arg_value("--userid", userids[0].userid()) |
| 199 |
.done(); |
| 200 |
|
| 201 |
return Err( |
| 202 |
anyhow::format_err!("\ |
| 203 |
The certifier is the same as the certificate to certify.")); |
| 204 |
} |
| 205 |
|
| 206 |
if trust_depth == 0 && !regex.is_empty() { |
| 207 |
return Err( |
| 208 |
anyhow::format_err!("A regex constraint only makes sense \ |
| 209 |
if the trust depth is greater than 0")); |
| 210 |
} |
| 211 |
|
| 212 |
// Get the signer to certify with. |
| 213 |
let mut signer = sq.get_certification_key(certifier, None)?; |
| 214 |
|
| 215 |
let mut base |
| 216 |
= SignatureBuilder::new(SignatureType::GenericCertification) |
| 217 |
.set_signature_creation_time(sq.time)?; |
| 218 |
|
| 219 |
for domain in domain { |
| 220 |
if let Err(err) = UserIDQueryParams::is_domain(domain) { |
| 221 |
return Err(err).context(format!( |
| 222 |
"{:?} is not a valid domain", domain)); |
| 223 |
} |
| 224 |
|
| 225 |
// Escape any control characters. |
| 226 |
const CONTROL: &[(&str, &str)] = &[ |
| 227 |
(".", "\\."), |
| 228 |
("|", "\\|"), |
| 229 |
("(", "\\("), |
| 230 |
(")", "\\)"), |
| 231 |
("*", "\\*"), |
| 232 |
("+", "\\+"), |
| 233 |
("?", "\\?"), |
| 234 |
("^", "\\^"), |
| 235 |
("$", "\\$"), |
| 236 |
("[", "\\["), |
| 237 |
("]", "\\]"), |
| 238 |
]; |
| 239 |
|
| 240 |
let mut domain = domain.to_string(); |
| 241 |
for (c, e) in CONTROL.iter() { |
| 242 |
domain = domain.replace(c, e); |
| 243 |
} |
| 244 |
|
| 245 |
base = base.add_regular_expression(format!("<[^>]+[@.]{}>$", domain))?; |
| 246 |
} |
| 247 |
for regex in regex { |
| 248 |
base = base.add_regular_expression(regex)?; |
| 249 |
} |
| 250 |
|
| 251 |
if local { |
| 252 |
base = base.set_exportable_certification(false)?; |
| 253 |
} |
| 254 |
|
| 255 |
if non_revocable { |
| 256 |
base = base.set_revocable(false)?; |
| 257 |
} |
| 258 |
|
| 259 |
for (critical, n) in notations { |
| 260 |
base = base.add_notation( |
| 261 |
n.name(), |
| 262 |
n.value(), |
| 263 |
NotationDataFlags::empty().set_human_readable(), |
| 264 |
*critical)?; |
| 265 |
}; |
| 266 |
|
| 267 |
let mut retract = false; |
| 268 |
|
| 269 |
let mut builders = Vec::with_capacity(templates.len()); |
| 270 |
for (i, (trust_amount, expiration)) in templates.into_iter().enumerate() { |
| 271 |
let mut builder = base.clone(); |
| 272 |
|
| 273 |
let trust_amount: u8 = trust_amount.amount(); |
| 274 |
if trust_amount == 0 { |
| 275 |
retract = true; |
| 276 |
} |
| 277 |
if trust_depth != 0 || trust_amount != 120 { |
| 278 |
builder = builder.set_trust_signature( |
| 279 |
trust_depth, trust_amount)?; |
| 280 |
} |
| 281 |
|
| 282 |
// Creation time. |
| 283 |
// |
| 284 |
// If we should make two certifications, then the first one |
| 285 |
// should be at `sq.time - 1`, and the second one at |
| 286 |
// `sq.time`. That is, the first one is a second earlier. |
| 287 |
let backdate = Duration::new((templates.len() - 1 - i) as u64, 0); |
| 288 |
let ct = sq.time - backdate; |
| 289 |
builder = builder.set_signature_creation_time(ct)?; |
| 290 |
|
| 291 |
// Expiration. |
| 292 |
if let Some(validity) = expiration |
| 293 |
.as_duration(DateTime::<Utc>::from(sq.time))? |
| 294 |
{ |
| 295 |
builder = builder.set_signature_validity_period(validity)?; |
| 296 |
} |
| 297 |
|
| 298 |
builders.push(builder); |
| 299 |
} |
| 300 |
|
| 301 |
// Get the active certification as of the reference time. |
| 302 |
let certifications = active_certification( |
| 303 |
&sq, &cert, userids.iter(), |
| 304 |
certifier.primary_key().key().role_as_unspecified()) |
| 305 |
.into_iter() |
| 306 |
.map(|(userid, active_certification)| { |
| 307 |
if let Some(ua) = cert.userids().find(|ua| ua.userid() == userid.userid()) { |
| 308 |
if retract { |
| 309 |
// Check if we certified it. |
| 310 |
if ! ua.certifications().any(|c| { |
| 311 |
c.get_issuers().into_iter() |
| 312 |
.any(|issuer| issuer.aliases(&certifier.key_handle())) |
| 313 |
}) |
| 314 |
{ |
| 315 |
ui::emit_cert_userid(unless_quiet, cert, userid.userid())?; |
| 316 |
wwriteln!(stream = unless_quiet, initial_indent = " - ", |
| 317 |
"this binding was never certified; \ |
| 318 |
there is nothing to retract"); |
| 319 |
|
| 320 |
if user_supplied_userids { |
| 321 |
return Err(anyhow::anyhow!( |
| 322 |
"You never certified {} for {}, \ |
| 323 |
there is nothing to retract.", |
| 324 |
ui::Safe(userid.userid()), cert.fingerprint())); |
| 325 |
} else { |
| 326 |
return Ok(vec![ Packet::from(userid.userid().clone()) ]); |
| 327 |
} |
| 328 |
} |
| 329 |
} else { |
| 330 |
if let RevocationStatus::Revoked(_) |
| 331 |
= ua.revocation_status(sq.policy, sq.time) |
| 332 |
{ |
| 333 |
// It's revoked. |
| 334 |
if user_supplied_userids { |
| 335 |
// It was explicitly mentioned. Return an |
| 336 |
// error. |
| 337 |
return Err(anyhow::anyhow!( |
| 338 |
"Can't certify {} for {}, it's revoked", |
| 339 |
ui::Safe(userid.userid()), cert.fingerprint())); |
| 340 |
} else { |
| 341 |
// We're just considering valid, self-signed |
| 342 |
// user IDs. Silently, skip it. |
| 343 |
return Ok(vec![]); |
| 344 |
} |
| 345 |
} |
| 346 |
} |
| 347 |
} else if retract { |
| 348 |
ui::emit_cert_userid(unless_quiet, cert, userid.userid())?; |
| 349 |
wwriteln!(stream = unless_quiet, initial_indent = " - ", |
| 350 |
"this binding was never certified; \ |
| 351 |
there is nothing to retract"); |
| 352 |
|
| 353 |
if user_supplied_userids { |
| 354 |
return Err(anyhow::anyhow!( |
| 355 |
"You never certified {} for {}, \ |
| 356 |
there is nothing to retract.", |
| 357 |
ui::Safe(userid.userid()), cert.fingerprint())); |
| 358 |
} else { |
| 359 |
// The user passed --all. Don't error out if some |
| 360 |
// user IDs were not linked. Instead, return a |
| 361 |
// signature packet to indicate that we processed |
| 362 |
// something; just don't return a signature. |
| 363 |
return Ok(vec![ Packet::from(userid.userid().clone()) ]); |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
if let Some(active_certification) = active_certification { |
| 368 |
let active_certification_ct |
| 369 |
= active_certification.signature_creation_time() |
| 370 |
.expect("valid signature"); |
| 371 |
|
| 372 |
let retracted = matches!(active_certification.trust_signature(), |
| 373 |
Some((_depth, 0))); |
| 374 |
if retracted { |
| 375 |
ui::emit_cert_userid(unless_quiet, cert, userid.userid())?; |
| 376 |
wwriteln!(stream = unless_quiet, initial_indent = " - ", |
| 377 |
"a prior certification was retracted at {}", |
| 378 |
active_certification_ct.convert()); |
| 379 |
} else { |
| 380 |
ui::emit_cert_userid(unless_quiet, cert, userid.userid())?; |
| 381 |
wwriteln!(stream = unless_quiet, initial_indent = " - ", |
| 382 |
"was previously certified at {}", |
| 383 |
active_certification_ct.convert()); |
| 384 |
} |
| 385 |
|
| 386 |
let changed = diff_certification( |
| 387 |
unless_quiet, |
| 388 |
&active_certification, |
| 389 |
&builders[0], sq.time); |
| 390 |
|
| 391 |
if ! changed { |
| 392 |
wwriteln!(stream = unless_quiet, initial_indent = " - ", |
| 393 |
"certification parameters are unchanged; \ |
| 394 |
there is nothing to do"); |
| 395 |
|
| 396 |
if ! recreate { |
| 397 |
// Return a signature packet to indicate that we |
| 398 |
// processed something. But don't return a |
| 399 |
// signature. |
| 400 |
return Ok(vec![ Packet::from(userid.userid().clone()) ]); |
| 401 |
} |
| 402 |
} else { |
| 403 |
wwriteln!(stream = unless_quiet, initial_indent = " - ", |
| 404 |
"certification parameters changed, \ |
| 405 |
creating a new certification"); |
| 406 |
} |
| 407 |
} else { |
| 408 |
ui::emit_cert_userid(unless_quiet, cert, userid.userid())?; |
| 409 |
} |
| 410 |
|
| 411 |
if retract { |
| 412 |
wwriteln!(stream = unless_quiet, initial_indent = " - ", |
| 413 |
"certification retracted"); |
| 414 |
} else { |
| 415 |
wwriteln!(stream = unless_quiet, initial_indent = " - ", |
| 416 |
"certification created"); |
| 417 |
} |
| 418 |
|
| 419 |
let mut sigs = builders.iter() |
| 420 |
.map(|builder| { |
| 421 |
builder.clone().sign_userid_binding( |
| 422 |
&mut signer, |
| 423 |
cert.primary_key().key(), |
| 424 |
userid.userid()) |
| 425 |
.with_context(|| { |
| 426 |
format!("Creating certification for {}", |
| 427 |
ui::Safe(userid.userid())) |
| 428 |
}) |
| 429 |
.map(Into::into) |
| 430 |
}) |
| 431 |
.collect::<Result<Vec<Packet>>>()?; |
| 432 |
|
| 433 |
wwriteln!(stream = unless_quiet); |
| 434 |
|
| 435 |
let mut packets = vec![ Packet::from(userid.userid().clone()) ]; |
| 436 |
packets.append(&mut sigs); |
| 437 |
Ok(packets) |
| 438 |
}) |
| 439 |
.collect::<Result<Vec<Vec<Packet>>>>()? |
| 440 |
.into_iter() |
| 441 |
.flatten() |
| 442 |
.collect::<Vec<Packet>>(); |
| 443 |
|
| 444 |
if certifications.is_empty() { |
| 445 |
if retract { |
| 446 |
return Err(anyhow::anyhow!( |
| 447 |
"Can't retract links for {}. There is nothing to retract.", |
| 448 |
cert.fingerprint())); |
| 449 |
} else { |
| 450 |
return Err(anyhow::anyhow!( |
| 451 |
"Can't certify {}. The certificate has no self-signed \ |
| 452 |
user IDs and you didn't specify any user IDs to certify.", |
| 453 |
cert.fingerprint())); |
| 454 |
} |
| 455 |
} |
| 456 |
|
| 457 |
if certifications.iter().all(|p| matches!(p, Packet::UserID(_))) { |
| 458 |
// There are no signatures to insert. We're done. |
| 459 |
return Ok(()); |
| 460 |
} |
| 461 |
|
| 462 |
let cert = cert.clone().insert_packets(certifications)?.0; |
| 463 |
|
| 464 |
if let Some(output) = output { |
| 465 |
// And export it. |
| 466 |
let path = output.path().map(Clone::clone); |
| 467 |
let mut message = output.create_pgp_safe( |
| 468 |
&sq, |
| 469 |
binary, |
| 470 |
sequoia_openpgp::armor::Kind::PublicKey, |
| 471 |
)?; |
| 472 |
cert.serialize(&mut message)?; |
| 473 |
message.finalize()?; |
| 474 |
|
| 475 |
if ! local { |
| 476 |
if let Some(path) = path { |
| 477 |
sq.hint(format_args!( |
| 478 |
"Updated certificate written to {}. \ |
| 479 |
To make the update effective, it has to be published \ |
| 480 |
so that others can find it, for example using:", |
| 481 |
path.display())) |
| 482 |
.sq().arg("network").arg("keyserver").arg("publish") |
| 483 |
.arg_value("--cert-file", path.display()) |
| 484 |
.done(); |
| 485 |
} else { |
| 486 |
sq.hint(format_args!( |
| 487 |
"To make the update effective, it has to be published \ |
| 488 |
so that others can find it.")); |
| 489 |
} |
| 490 |
} |
| 491 |
} else { |
| 492 |
// Import it. |
| 493 |
let cert_store = sq.cert_store_or_else()?; |
| 494 |
|
| 495 |
let fipr = cert.fingerprint(); |
| 496 |
if let Err(err) = cert_store.update(Arc::new(cert.into())) { |
| 497 |
weprintln!("Error importing updated cert: {}", err); |
| 498 |
return Err(err); |
| 499 |
} else if ! local { |
| 500 |
sq.hint(format_args!( |
| 501 |
"Imported updated cert into the cert store. \ |
| 502 |
To make the update effective, it has to be published \ |
| 503 |
so that others can find it, for example using:")) |
| 504 |
.sq().arg("network").arg("keyserver").arg("publish") |
| 505 |
.arg_value("--cert", fipr) |
| 506 |
.done(); |
| 507 |
} |
| 508 |
} |
| 509 |
|
| 510 |
Ok(()) |
| 511 |
} |