| 1 |
use std::cmp::Ordering; |
| 2 |
use std::io::IsTerminal; |
| 3 |
use std::io:: Write; |
| 4 |
use std::time::SystemTime; |
| 5 |
use std::sync::Arc; |
| 6 |
|
| 7 |
use termcolor::{WriteColor, StandardStream, ColorChoice, ColorSpec, Color}; |
| 8 |
|
| 9 |
use anyhow::Context; |
| 10 |
|
| 11 |
use sequoia_openpgp as openpgp; |
| 12 |
use openpgp::Result; |
| 13 |
use openpgp::armor; |
| 14 |
use openpgp::cert::prelude::*; |
| 15 |
use openpgp::crypto::Signer; |
| 16 |
use openpgp::packet::prelude::*; |
| 17 |
use openpgp::policy::Policy; |
| 18 |
use openpgp::policy::StandardPolicy; |
| 19 |
use openpgp::serialize::Serialize; |
| 20 |
use openpgp::types::HashAlgorithm; |
| 21 |
use openpgp::types::KeyFlags; |
| 22 |
use openpgp::types::RevocationStatus; |
| 23 |
use openpgp::types::SignatureType; |
| 24 |
use openpgp::types::SymmetricAlgorithm; |
| 25 |
|
| 26 |
use sequoia_cert_store as cert_store; |
| 27 |
use cert_store::LazyCert; |
| 28 |
use cert_store::StoreUpdate; |
| 29 |
|
| 30 |
use crate::{ |
| 31 |
Sq, |
| 32 |
cli::cert::lint::Command, |
| 33 |
cli::types::cert_designator::CertDesignator, |
| 34 |
commands::FileOrStdout, |
| 35 |
common::ui, |
| 36 |
sq::TrustThreshold, |
| 37 |
}; |
| 38 |
|
| 39 |
fn update_cert_revocation(sq: &Sq, |
| 40 |
cert: &Cert, rev: &Signature, |
| 41 |
reference_time: &SystemTime) |
| 42 |
-> Result<Signature> |
| 43 |
{ |
| 44 |
assert_eq!(rev.typ(), SignatureType::KeyRevocation); |
| 45 |
|
| 46 |
let ka = cert.primary_key(); |
| 47 |
let pk = ka.key(); |
| 48 |
|
| 49 |
// Derive a signer. |
| 50 |
let mut signer = sq.get_signer(&ka)?; |
| 51 |
|
| 52 |
let sig = SignatureBuilder::from(rev.clone()) |
| 53 |
.set_signature_creation_time(reference_time.clone())? |
| 54 |
.set_hash_algo(HashAlgorithm::SHA256) |
| 55 |
.preserve_signature_creation_time()? |
| 56 |
.sign_direct_key(&mut signer, pk)?; |
| 57 |
|
| 58 |
Ok(sig) |
| 59 |
} |
| 60 |
|
| 61 |
const GOOD_HASHES: &[ HashAlgorithm ] = &[ |
| 62 |
HashAlgorithm::SHA256, |
| 63 |
HashAlgorithm::SHA512, |
| 64 |
]; |
| 65 |
|
| 66 |
// Update the binding signature for ua. |
| 67 |
// |
| 68 |
// ua is using a weak policy. |
| 69 |
fn update_user_id_binding(sq: &Sq, |
| 70 |
ua: &ValidUserIDAmalgamation, |
| 71 |
reference_time: &SystemTime) |
| 72 |
-> Result<Signature> |
| 73 |
{ |
| 74 |
let ka = ua.cert().primary_key(); |
| 75 |
let pk = ka.key(); |
| 76 |
|
| 77 |
// Derive a signer. |
| 78 |
let mut signer = sq.get_signer(&ka)?; |
| 79 |
|
| 80 |
let sym = &[ |
| 81 |
SymmetricAlgorithm::AES128, |
| 82 |
SymmetricAlgorithm::AES192, |
| 83 |
SymmetricAlgorithm::AES256, |
| 84 |
SymmetricAlgorithm::Twofish, |
| 85 |
SymmetricAlgorithm::Camellia128, |
| 86 |
SymmetricAlgorithm::Camellia192, |
| 87 |
SymmetricAlgorithm::Camellia256, |
| 88 |
]; |
| 89 |
|
| 90 |
// Update the signature. |
| 91 |
let sig = ua.binding_signature(); |
| 92 |
let sig = SignatureBuilder::from(sig.clone()) |
| 93 |
.set_signature_creation_time(reference_time.clone())? |
| 94 |
.set_hash_algo(GOOD_HASHES[0]) |
| 95 |
.set_preferred_hash_algorithms( |
| 96 |
sig.preferred_hash_algorithms() |
| 97 |
.unwrap_or(&[ HashAlgorithm::SHA512, HashAlgorithm::SHA256 ]) |
| 98 |
.iter() |
| 99 |
.map(|h| h.clone()) |
| 100 |
.filter(|a| GOOD_HASHES.contains(&a)) |
| 101 |
.collect())? |
| 102 |
.set_preferred_symmetric_algorithms( |
| 103 |
sig.preferred_symmetric_algorithms() |
| 104 |
.unwrap_or(&[ |
| 105 |
SymmetricAlgorithm::AES128, |
| 106 |
SymmetricAlgorithm::AES256, |
| 107 |
]) |
| 108 |
.iter() |
| 109 |
.map(|h| h.clone()) |
| 110 |
.filter(|a| sym.contains(&a)) |
| 111 |
.collect())? |
| 112 |
.sign_userid_binding(&mut signer, pk, ua.userid())?; |
| 113 |
|
| 114 |
// Verify it. |
| 115 |
assert!(sig.verify_userid_binding(signer.public(), pk, ua.userid()) |
| 116 |
.is_ok()); |
| 117 |
|
| 118 |
// Make sure the signature is integrated. |
| 119 |
assert!(ua.cert().clone() |
| 120 |
.insert_packets(Packet::from(sig.clone())).unwrap().0 |
| 121 |
.into_packets() |
| 122 |
.any(|p| { |
| 123 |
if let Packet::Signature(s) = p { |
| 124 |
s == sig |
| 125 |
} else { |
| 126 |
false |
| 127 |
} |
| 128 |
})); |
| 129 |
|
| 130 |
Ok(sig) |
| 131 |
} |
| 132 |
|
| 133 |
// Update the subkey binding signature for ka. |
| 134 |
// |
| 135 |
// ka is using a weak policy. |
| 136 |
fn update_subkey_binding<P>(sq: &Sq, |
| 137 |
ka: &ValidSubordinateKeyAmalgamation<P>, |
| 138 |
reference_time: &SystemTime) |
| 139 |
-> Result<Signature> |
| 140 |
where P: key::KeyParts + Clone |
| 141 |
{ |
| 142 |
let primary = ka.cert().primary_key(); |
| 143 |
let pk = primary.key(); |
| 144 |
|
| 145 |
// Derive a signer. |
| 146 |
let mut signer = sq.get_signer(&primary)?; |
| 147 |
|
| 148 |
// Update the signature. |
| 149 |
let sig = ka.binding_signature(); |
| 150 |
let mut builder = SignatureBuilder::from(sig.clone()) |
| 151 |
.set_signature_creation_time(reference_time.clone())? |
| 152 |
.set_hash_algo(HashAlgorithm::SHA256); |
| 153 |
|
| 154 |
// If there is a valid backsig, recreate it. |
| 155 |
if let Some(backsig) = sig.embedded_signatures() |
| 156 |
.filter(|backsig| { |
| 157 |
(*backsig).clone().verify_primary_key_binding( |
| 158 |
pk, |
| 159 |
ka.key()).is_ok() |
| 160 |
}) |
| 161 |
.nth(0) |
| 162 |
{ |
| 163 |
// Derive a signer. |
| 164 |
let mut subkey_signer = sq.get_signer(ka.amalgamation())?; |
| 165 |
|
| 166 |
let backsig = SignatureBuilder::from(backsig.clone()) |
| 167 |
.set_signature_creation_time(reference_time.clone())? |
| 168 |
.set_hash_algo(HashAlgorithm::SHA256) |
| 169 |
.sign_primary_key_binding(&mut subkey_signer, pk, ka.key())?; |
| 170 |
builder = builder.set_embedded_signature(backsig)?; |
| 171 |
} |
| 172 |
|
| 173 |
let sig = builder.sign_subkey_binding(&mut signer, pk, ka.key())?; |
| 174 |
|
| 175 |
// Verify it. |
| 176 |
assert!(sig.verify_subkey_binding(signer.public(), pk, ka.key()) |
| 177 |
.is_ok()); |
| 178 |
|
| 179 |
// Make sure the signature is integrated. |
| 180 |
assert!(ka.cert().clone() |
| 181 |
.insert_packets(Packet::from(sig.clone())).unwrap().0 |
| 182 |
.into_packets() |
| 183 |
.any(|p| { |
| 184 |
if let Packet::Signature(s) = p { |
| 185 |
s == sig |
| 186 |
} else { |
| 187 |
false |
| 188 |
} |
| 189 |
})); |
| 190 |
|
| 191 |
Ok(sig) |
| 192 |
} |
| 193 |
|
| 194 |
pub fn lint(sq: Sq, mut args: Command) -> Result<()> { |
| 195 |
// Number of certs that have issues. |
| 196 |
let mut certs_with_issues = 0; |
| 197 |
// Whether we were unable to fix at least one issue. |
| 198 |
let mut unfixed_issue = 0; |
| 199 |
|
| 200 |
// Standard policy that unconditionally rejects SHA-1: this is |
| 201 |
// where we want to be. |
| 202 |
let mut sp = StandardPolicy::at(sq.policy_as_of); |
| 203 |
sp.reject_hash(HashAlgorithm::SHA1); |
| 204 |
let sp = &sp; |
| 205 |
|
| 206 |
// A standard policy that also accepts SHA-1. |
| 207 |
let mut sp_sha1 = StandardPolicy::at(sq.policy_as_of); |
| 208 |
sp_sha1.accept_hash(HashAlgorithm::SHA1); |
| 209 |
let sp_sha1 = &sp_sha1; |
| 210 |
|
| 211 |
// The number of valid and invalid certificates (according to |
| 212 |
// SP+SHA-1). |
| 213 |
let mut certs_valid = 0; |
| 214 |
let mut certs_invalid = 0; |
| 215 |
|
| 216 |
// Certificates that are revoked. |
| 217 |
let mut certs_revoked = 0; |
| 218 |
let mut certs_with_inadequota_revocations = 0; |
| 219 |
let mut certs_expired = 0; |
| 220 |
|
| 221 |
// Certificates that are valid and have a valid User ID. |
| 222 |
let mut certs_sp_sha1_userids = 0; |
| 223 |
let mut certs_with_a_sha1_protected_userid = 0; |
| 224 |
let mut certs_with_only_sha1_protected_userids = 0; |
| 225 |
|
| 226 |
// Subkeys. |
| 227 |
let mut certs_with_subkeys = 0; |
| 228 |
let mut certs_with_a_sha1_protected_binding_sig = 0; |
| 229 |
let mut certs_with_signing_subkeys = 0; |
| 230 |
let mut certs_with_sha1_protected_backsig = 0; |
| 231 |
|
| 232 |
let reference_time = sq.time; |
| 233 |
|
| 234 |
// If no inputs are given, read from stdin. |
| 235 |
if args.certs.is_empty() { |
| 236 |
args.certs.designators.push(CertDesignator::Stdin); |
| 237 |
} |
| 238 |
|
| 239 |
let certs = sq.resolve_certs_or_fail( |
| 240 |
&args.certs, TrustThreshold::Full)?; |
| 241 |
|
| 242 |
let mut out = if args.output.is_some() |
| 243 |
|| args.certs.iter().any(|d| d.from_file() || d.from_stdin()) |
| 244 |
{ |
| 245 |
let output = if let Some(output) = args.output { |
| 246 |
output |
| 247 |
} else { |
| 248 |
FileOrStdout::new(None) |
| 249 |
}; |
| 250 |
|
| 251 |
Some(output.create_pgp_safe( |
| 252 |
&sq, false, |
| 253 |
if certs.iter().any(|c| c.is_tsk()) { |
| 254 |
armor::Kind::SecretKey |
| 255 |
} else { |
| 256 |
armor::Kind::PublicKey |
| 257 |
})?) |
| 258 |
} else { |
| 259 |
None |
| 260 |
}; |
| 261 |
|
| 262 |
{ |
| 263 |
'next_cert: for cert in certs { |
| 264 |
// Whether we found at least one issue. |
| 265 |
let mut found_issue = false; |
| 266 |
|
| 267 |
macro_rules! diag { |
| 268 |
($($arg:tt)*) => {{ |
| 269 |
// found_issue may appear to be unused if |
| 270 |
// diag is immediately followed by a |
| 271 |
// continue or break. |
| 272 |
#[allow(unused_assignments)] |
| 273 |
{ |
| 274 |
if ! found_issue { |
| 275 |
certs_with_issues += 1; |
| 276 |
found_issue = true; |
| 277 |
} |
| 278 |
if ! sq.quiet() { |
| 279 |
weprintln!($($arg)*); |
| 280 |
} |
| 281 |
} |
| 282 |
}}; |
| 283 |
} |
| 284 |
|
| 285 |
let mut updates: Vec<Signature> = Vec::new(); |
| 286 |
|
| 287 |
macro_rules! next_cert { |
| 288 |
() => {{ |
| 289 |
if updates.len() > 0 { |
| 290 |
let cert = cert.insert_packets(updates)?.0; |
| 291 |
if let Some(mut out) = out.as_mut() { |
| 292 |
cert.as_tsk().serialize(&mut out)?; |
| 293 |
} else { |
| 294 |
let fpr = cert.fingerprint(); |
| 295 |
let cert_store = sq.cert_store_or_else()?; |
| 296 |
cert_store.update(Arc::new(LazyCert::from(cert))) |
| 297 |
.with_context(|| { |
| 298 |
format!("Error importing the {} into cert store", |
| 299 |
fpr) |
| 300 |
})?; |
| 301 |
} |
| 302 |
} |
| 303 |
continue 'next_cert; |
| 304 |
}} |
| 305 |
} |
| 306 |
|
| 307 |
let sp_vc = cert.with_policy(sp, reference_time); |
| 308 |
|
| 309 |
let sp_sha1_vc = cert.with_policy(sp_sha1, reference_time); |
| 310 |
if let Err(ref err) = sp_sha1_vc { |
| 311 |
diag!("Certificate {} is not valid under \ |
| 312 |
the standard policy + SHA-1: {}", |
| 313 |
cert.keyid().to_hex(), err); |
| 314 |
certs_invalid += 1; |
| 315 |
unfixed_issue += 1; |
| 316 |
continue; |
| 317 |
} |
| 318 |
let sp_sha1_vc = sp_sha1_vc.unwrap(); |
| 319 |
|
| 320 |
certs_valid += 1; |
| 321 |
|
| 322 |
// Check if the certificate is revoked. |
| 323 |
// |
| 324 |
// There are four cases to consider: |
| 325 |
// |
| 326 |
// 1. SHA1 certificate, SHA1 revocation certificate |
| 327 |
// 2. SHA1 certificate, SHA256 revocation certificate |
| 328 |
// 3. SHA256 certificate, SHA1 revocation certificate |
| 329 |
// 4. SHA256 certificate, SHA256 revocation certificate |
| 330 |
// |
| 331 |
// When the revocation certificate uses SHA256, |
| 332 |
// there is nothing to do even if something else |
| 333 |
// relies on SHA1: the certificate should be |
| 334 |
// ignore, because it is revoked! |
| 335 |
// |
| 336 |
// In the case that we have a SHA1 certificate and |
| 337 |
// a SHA1 revocation certificate, we also don't |
| 338 |
// have to do anything: either the whole |
| 339 |
// certificate will be considered invalid or |
| 340 |
// implementation accepts SHA1 and it will be |
| 341 |
// considered revoked. |
| 342 |
// |
| 343 |
// So, the only case that we have to fix is when |
| 344 |
// the certificate uses SHA256, but the revocation |
| 345 |
// certificate uses SHA1. In this case, we need |
| 346 |
// to upgrade the revocation certificate. |
| 347 |
if let RevocationStatus::Revoked(mut revs) |
| 348 |
= sp_sha1_vc.revocation_status() |
| 349 |
{ |
| 350 |
certs_revoked += 1; |
| 351 |
|
| 352 |
if sp_vc.is_err() { |
| 353 |
// Cases #1 and #2. Nothing to do. |
| 354 |
next_cert!(); |
| 355 |
} |
| 356 |
|
| 357 |
// Dedup based on creation time and the reason |
| 358 |
// for revocation. Prefer revocations that do |
| 359 |
// not use SHA-1. |
| 360 |
let cmp = |a: &&Signature, b: &&Signature| -> Ordering |
| 361 |
{ |
| 362 |
a.signature_creation_time() |
| 363 |
.cmp(&b.signature_creation_time()) |
| 364 |
.then(a.reason_for_revocation() |
| 365 |
.cmp(&b.reason_for_revocation())) |
| 366 |
}; |
| 367 |
|
| 368 |
revs.sort_by(cmp); |
| 369 |
revs.dedup_by( |
| 370 |
|a: &mut &Signature, b: &mut &Signature| -> bool |
| 371 |
{ |
| 372 |
let x = cmp(a, b); |
| 373 |
if x != Ordering::Equal { |
| 374 |
return false; |
| 375 |
} |
| 376 |
|
| 377 |
// Prefer the non-SHA-1 variant. |
| 378 |
// Recall: if the elements are |
| 379 |
// considered equal, a is removed and |
| 380 |
// b is kept. |
| 381 |
if GOOD_HASHES.contains(&a.hash_algo()) |
| 382 |
&& b.hash_algo() == HashAlgorithm::SHA1 |
| 383 |
{ |
| 384 |
*b = *a; |
| 385 |
} |
| 386 |
true |
| 387 |
}); |
| 388 |
|
| 389 |
// See what revocation certificates need to be |
| 390 |
// fixed. |
| 391 |
let mut inadequate_revocation = false; |
| 392 |
for rev in revs { |
| 393 |
if rev.hash_algo() == HashAlgorithm::SHA1 { |
| 394 |
if ! inadequate_revocation { |
| 395 |
inadequate_revocation = true; |
| 396 |
certs_with_inadequota_revocations += 1; |
| 397 |
} |
| 398 |
|
| 399 |
diag!("Certificate {}: Revocation certificate \ |
| 400 |
{:02X}{:02X} uses SHA-1.", |
| 401 |
cert.keyid().to_hex(), |
| 402 |
rev.digest_prefix()[0], |
| 403 |
rev.digest_prefix()[1]); |
| 404 |
if args.fix { |
| 405 |
match update_cert_revocation( |
| 406 |
&sq, &cert, rev, &reference_time) |
| 407 |
{ |
| 408 |
Ok(sig) => { |
| 409 |
updates.push(sig); |
| 410 |
} |
| 411 |
Err(err) => { |
| 412 |
unfixed_issue += 1; |
| 413 |
weprintln!("Certificate {}: \ |
| 414 |
Failed to update \ |
| 415 |
revocation certificate \ |
| 416 |
{:02X}{:02X}: {}", |
| 417 |
cert.keyid().to_hex(), |
| 418 |
rev.digest_prefix()[0], |
| 419 |
rev.digest_prefix()[1], |
| 420 |
err); |
| 421 |
} |
| 422 |
} |
| 423 |
} |
| 424 |
} |
| 425 |
|
| 426 |
continue; |
| 427 |
} |
| 428 |
|
| 429 |
next_cert!(); |
| 430 |
} |
| 431 |
|
| 432 |
|
| 433 |
// Check if the certificate is alive. |
| 434 |
match (sp_sha1_vc.alive(), |
| 435 |
sp_vc.as_ref().map(|vc| vc.alive())) |
| 436 |
{ |
| 437 |
(Err(_), Err(_)) => { |
| 438 |
// SP+SHA1: Not alive, SP: Invalid |
| 439 |
// |
| 440 |
// It only uses SHA1, and under SP+SHA1, |
| 441 |
// it is expired. Invalid or expired, we |
| 442 |
// don't need to fix it. |
| 443 |
certs_expired += 1; |
| 444 |
next_cert!(); |
| 445 |
} |
| 446 |
(Err(_), Ok(Err(_))) => { |
| 447 |
// SP+SHA1: Not alive, SP: Not alive. |
| 448 |
// |
| 449 |
// However you look at it, it's expired. |
| 450 |
certs_expired += 1; |
| 451 |
next_cert!(); |
| 452 |
} |
| 453 |
(Err(_), Ok(Ok(_))) => { |
| 454 |
// SP+SHA1: Not alive, SP: Alive |
| 455 |
// |
| 456 |
// Impossible. |
| 457 |
panic!(); |
| 458 |
} |
| 459 |
(Ok(_), Err(_)) => { |
| 460 |
// SP+SHA1: Alive, SP: Invalid. |
| 461 |
// |
| 462 |
// The certificate only uses SHA-1. Lint |
| 463 |
// it as usual. |
| 464 |
() |
| 465 |
} |
| 466 |
(Ok(_), Ok(Err(_))) => { |
| 467 |
// SP+SHA1: Alive, SP: Not alive. |
| 468 |
// |
| 469 |
// We have a binding signature using SHA1 |
| 470 |
// that says the certificate does not |
| 471 |
// expire, and a newer binding signature |
| 472 |
// using SHA2+ that is expired. |
| 473 |
// |
| 474 |
// Linting should(tm) fix this. |
| 475 |
diag!("Certificate {} is live under SP+SHA1, \ |
| 476 |
but expire under SP.", |
| 477 |
cert.keyid().to_hex()); |
| 478 |
} |
| 479 |
(Ok(_), Ok(Ok(_))) => { |
| 480 |
// SP+SHA1: Alive, SP: Alive. Lint it as |
| 481 |
// usual. |
| 482 |
() |
| 483 |
} |
| 484 |
} |
| 485 |
|
| 486 |
|
| 487 |
if let Err(ref err) = sp_vc { |
| 488 |
diag!("Certificate {} is not valid under \ |
| 489 |
the standard policy: {}", |
| 490 |
cert.keyid().to_hex(), err); |
| 491 |
} |
| 492 |
|
| 493 |
|
| 494 |
// User IDs that are not revoked, and valid under |
| 495 |
// the standard policy + SHA-1. |
| 496 |
let mut a_userid = false; |
| 497 |
let mut sha1_protected_userid = false; |
| 498 |
let mut not_sha1_protected_userid = false; |
| 499 |
|
| 500 |
let not_revoked = |ua: &ValidUserIDAmalgamation| -> bool { |
| 501 |
if let RevocationStatus::Revoked(_) |
| 502 |
= ua.revocation_status() |
| 503 |
{ |
| 504 |
false |
| 505 |
} else { |
| 506 |
true |
| 507 |
} |
| 508 |
}; |
| 509 |
|
| 510 |
for ua in sp_sha1_vc.userids().filter(not_revoked) { |
| 511 |
if ! a_userid { |
| 512 |
a_userid = true; |
| 513 |
certs_sp_sha1_userids += 1; |
| 514 |
} |
| 515 |
|
| 516 |
let sig = ua.binding_signature(); |
| 517 |
if sig.hash_algo() == HashAlgorithm::SHA1 { |
| 518 |
diag!("Certificate {} contains a \ |
| 519 |
User ID ({}) protected by SHA-1", |
| 520 |
cert.keyid().to_hex(), |
| 521 |
ui::Safe(ua.userid())); |
| 522 |
|
| 523 |
if !sha1_protected_userid { |
| 524 |
sha1_protected_userid = true; |
| 525 |
certs_with_a_sha1_protected_userid += 1; |
| 526 |
} |
| 527 |
if args.fix { |
| 528 |
match update_user_id_binding( |
| 529 |
&sq, &ua, &reference_time) |
| 530 |
{ |
| 531 |
Ok(sig) => { |
| 532 |
updates.push(sig); |
| 533 |
} |
| 534 |
Err(err) => { |
| 535 |
unfixed_issue += 1; |
| 536 |
weprintln!("Certificate {}: User ID {}: \ |
| 537 |
Failed to update \ |
| 538 |
binding signature: {}", |
| 539 |
cert.keyid().to_hex(), |
| 540 |
ui::Safe(ua.userid()), |
| 541 |
err); |
| 542 |
} |
| 543 |
} |
| 544 |
} |
| 545 |
} else { |
| 546 |
if !not_sha1_protected_userid { |
| 547 |
not_sha1_protected_userid = true; |
| 548 |
} |
| 549 |
} |
| 550 |
} |
| 551 |
|
| 552 |
if sha1_protected_userid && ! not_sha1_protected_userid { |
| 553 |
certs_with_only_sha1_protected_userids += 1; |
| 554 |
} |
| 555 |
|
| 556 |
let sha1_subkeys: Vec<_> = sp_sha1_vc |
| 557 |
.keys().subkeys() |
| 558 |
.revoked(false).alive() |
| 559 |
.collect(); |
| 560 |
if sha1_subkeys.len() > 0 { |
| 561 |
certs_with_subkeys += 1; |
| 562 |
|
| 563 |
// Does this certificate have any subkeys whose |
| 564 |
// binding signatures use SHA-1? |
| 565 |
let mut uses_sha1_protected_binding_sig = false; |
| 566 |
let mut uses_certs_with_signing_subkeys = false; |
| 567 |
let mut uses_sha1_protected_backsig = false; |
| 568 |
for ka in sha1_subkeys.into_iter() { |
| 569 |
let sig = ka.binding_signature(); |
| 570 |
if sig.hash_algo() == HashAlgorithm::SHA1 { |
| 571 |
diag!("Certificate {}, key {} uses a \ |
| 572 |
SHA-1-protected binding signature.", |
| 573 |
cert.keyid().to_hex(), |
| 574 |
ka.key().keyid().to_hex()); |
| 575 |
if ! uses_sha1_protected_binding_sig { |
| 576 |
uses_sha1_protected_binding_sig = true; |
| 577 |
certs_with_a_sha1_protected_binding_sig += 1; |
| 578 |
} |
| 579 |
if args.fix { |
| 580 |
match update_subkey_binding( |
| 581 |
&sq, &ka, &reference_time) |
| 582 |
{ |
| 583 |
Ok(sig) => updates.push(sig), |
| 584 |
Err(err) => { |
| 585 |
unfixed_issue += 1; |
| 586 |
weprintln!("Certificate {}, key {}: \ |
| 587 |
Failed to update \ |
| 588 |
binding signature: {}", |
| 589 |
cert.keyid().to_hex(), |
| 590 |
ka.key().keyid().to_hex(), |
| 591 |
err); |
| 592 |
} |
| 593 |
} |
| 594 |
} |
| 595 |
|
| 596 |
continue; |
| 597 |
} |
| 598 |
|
| 599 |
// Check if the backsig uses SHA-1. |
| 600 |
|
| 601 |
if ! ka.has_any_key_flag( |
| 602 |
KeyFlags::empty() |
| 603 |
.set_signing() |
| 604 |
.set_certification()) |
| 605 |
{ |
| 606 |
// No backsig required. |
| 607 |
continue; |
| 608 |
} |
| 609 |
|
| 610 |
if ! uses_certs_with_signing_subkeys { |
| 611 |
uses_certs_with_signing_subkeys = true; |
| 612 |
certs_with_signing_subkeys += 1; |
| 613 |
} |
| 614 |
|
| 615 |
// Get the cryptographically valid backsigs. |
| 616 |
let mut backsigs: Vec<_> = sig.embedded_signatures() |
| 617 |
.filter(|backsig| { |
| 618 |
(*backsig).clone().verify_primary_key_binding( |
| 619 |
cert.primary_key().key(), |
| 620 |
ka.key()).is_ok() |
| 621 |
}) |
| 622 |
.collect(); |
| 623 |
if backsigs.len() == 0 { |
| 624 |
// We can't get here. If the key is |
| 625 |
// valid under sp+SHA-1, and requires |
| 626 |
// a backsig, then it must have a |
| 627 |
// valid backsig. |
| 628 |
panic!("Valid signing-capable subkey without \ |
| 629 |
a valid backsig?"); |
| 630 |
} |
| 631 |
backsigs.sort(); |
| 632 |
backsigs.dedup(); |
| 633 |
|
| 634 |
if backsigs.len() > 1 { |
| 635 |
weprintln!("Warning: multiple cryptographically \ |
| 636 |
valid backsigs."); |
| 637 |
} |
| 638 |
|
| 639 |
if backsigs |
| 640 |
.iter() |
| 641 |
.any(|s| { |
| 642 |
sp.signature(s, ka.key().hash_algo_security()) |
| 643 |
.is_ok() |
| 644 |
}) |
| 645 |
{ |
| 646 |
// It's valid under the standard |
| 647 |
// policy. We're fine. |
| 648 |
} else if backsigs |
| 649 |
.iter() |
| 650 |
.any(|s| { |
| 651 |
sp_sha1.signature(s, ka.key().hash_algo_security()) |
| 652 |
.is_ok() |
| 653 |
}) |
| 654 |
{ |
| 655 |
// It's valid under SP+SHA-1 policy. |
| 656 |
// Update it. |
| 657 |
diag!("Certificate {}, key {} uses a \ |
| 658 |
{}-protected binding signature, \ |
| 659 |
but a SHA-1-protected backsig", |
| 660 |
cert.keyid().to_hex(), |
| 661 |
ka.key().keyid().to_hex(), |
| 662 |
sig.hash_algo()); |
| 663 |
if ! uses_sha1_protected_backsig { |
| 664 |
uses_sha1_protected_backsig = true; |
| 665 |
certs_with_sha1_protected_backsig += 1; |
| 666 |
} |
| 667 |
if args.fix { |
| 668 |
match update_subkey_binding( |
| 669 |
&sq, &ka, &reference_time) |
| 670 |
{ |
| 671 |
Ok(sig) => updates.push(sig), |
| 672 |
Err(err) => { |
| 673 |
unfixed_issue += 1; |
| 674 |
weprintln!("Certificate {}, key: {}: \ |
| 675 |
Failed to update \ |
| 676 |
binding signature: {}", |
| 677 |
cert.keyid().to_hex(), |
| 678 |
ka.key().keyid().to_hex(), |
| 679 |
err); |
| 680 |
} |
| 681 |
} |
| 682 |
} |
| 683 |
} else { |
| 684 |
let sig = backsigs[0]; |
| 685 |
let err = sp_sha1.signature(sig, ka.key().hash_algo_security()).unwrap_err(); |
| 686 |
diag!("Cert {}: backsig {:02X}{:02X} for \ |
| 687 |
{} is not valid under SP+SHA-1: {}. \ |
| 688 |
Ignoring.", |
| 689 |
cert.keyid().to_hex(), |
| 690 |
sig.digest_prefix()[0], |
| 691 |
sig.digest_prefix()[1], |
| 692 |
ka.key().keyid().to_hex(), |
| 693 |
err); |
| 694 |
unfixed_issue += 1; |
| 695 |
} |
| 696 |
} |
| 697 |
} |
| 698 |
|
| 699 |
if !found_issue { |
| 700 |
if let Err(err) = sp_vc { |
| 701 |
diag!("Certificate {} is not valid under \ |
| 702 |
the standard policy: {}", |
| 703 |
cert.keyid().to_hex(), err); |
| 704 |
} |
| 705 |
} |
| 706 |
|
| 707 |
next_cert!(); |
| 708 |
} |
| 709 |
} |
| 710 |
|
| 711 |
if let Some(out) = out { |
| 712 |
out.finalize()?; |
| 713 |
} |
| 714 |
|
| 715 |
let pl = |n, singular, plural| { if n == 1 { singular } else { plural } }; |
| 716 |
macro_rules! err { |
| 717 |
($n:expr, $($arg:tt)*) => {{ |
| 718 |
eprint!($($arg)*); |
| 719 |
eprint!(" ("); |
| 720 |
if $n > 0 { |
| 721 |
if std::io::stderr().is_terminal() { |
| 722 |
let mut stderr = StandardStream::stderr(ColorChoice::Auto); |
| 723 |
stderr.set_color(ColorSpec::new().set_fg(Some(Color::Red)))?; |
| 724 |
write!(&mut stderr, "BAD")?; |
| 725 |
stderr.reset()?; |
| 726 |
} else { |
| 727 |
eprint!("BAD"); |
| 728 |
} |
| 729 |
} else { |
| 730 |
if std::io::stderr().is_terminal() { |
| 731 |
let mut stderr = StandardStream::stderr(ColorChoice::Auto); |
| 732 |
stderr.set_color(ColorSpec::new().set_fg(Some(Color::Green)))?; |
| 733 |
write!(&mut stderr, "GOOD")?; |
| 734 |
stderr.reset()?; |
| 735 |
} else { |
| 736 |
eprint!("GOOD"); |
| 737 |
} |
| 738 |
} |
| 739 |
weprintln!(")"); |
| 740 |
}}; |
| 741 |
} |
| 742 |
|
| 743 |
if ! sq.quiet() { |
| 744 |
weprintln!("Examined {} {}.", |
| 745 |
certs_valid + certs_invalid, |
| 746 |
pl(certs_valid + certs_invalid, |
| 747 |
"certificate", "certificates")); |
| 748 |
|
| 749 |
err!(certs_invalid, |
| 750 |
" {} {} invalid and {} not linted.", |
| 751 |
certs_invalid, |
| 752 |
pl(certs_invalid, "certificate is", "certificates are"), |
| 753 |
pl(certs_invalid, "was", "were")); |
| 754 |
if certs_valid > 0 { |
| 755 |
weprintln!(" {} {} linted.", |
| 756 |
certs_valid, |
| 757 |
pl(certs_valid, |
| 758 |
"certificate was", "certificates were")); |
| 759 |
err!(certs_with_issues, |
| 760 |
" {} of the {} certificates ({}%) \ |
| 761 |
{} at least one issue.", |
| 762 |
certs_with_issues, |
| 763 |
certs_valid + certs_invalid, |
| 764 |
certs_with_issues * 100 / (certs_valid + certs_invalid), |
| 765 |
pl(certs_with_issues, "has", "have")); |
| 766 |
weprintln!("{} of the linted certificates {} revoked.", |
| 767 |
certs_revoked, |
| 768 |
pl(certs_revoked, "was", "were")); |
| 769 |
err!(certs_with_inadequota_revocations, |
| 770 |
" {} of the {} certificates has revocation certificates \ |
| 771 |
that are weaker than the certificate and should be \ |
| 772 |
recreated.", |
| 773 |
certs_with_inadequota_revocations, |
| 774 |
certs_revoked); |
| 775 |
weprintln!("{} of the linted certificates {} expired.", |
| 776 |
certs_expired, |
| 777 |
pl(certs_expired, "was", "were")); |
| 778 |
weprintln!("{} of the non-revoked linted {} at least one non-revoked User ID:", |
| 779 |
certs_sp_sha1_userids, |
| 780 |
pl(certs_sp_sha1_userids, |
| 781 |
"certificate has", "certificates have")); |
| 782 |
err!(certs_with_a_sha1_protected_userid, |
| 783 |
" {} {} at least one User ID protected by SHA-1.", |
| 784 |
certs_with_a_sha1_protected_userid, |
| 785 |
pl(certs_with_a_sha1_protected_userid, "has", "have")); |
| 786 |
err!(certs_with_only_sha1_protected_userids, |
| 787 |
" {} {} all User IDs protected by SHA-1.", |
| 788 |
certs_with_only_sha1_protected_userids, |
| 789 |
pl(certs_with_only_sha1_protected_userids, |
| 790 |
"has", "have")); |
| 791 |
weprintln!("{} of the non-revoked linted certificates {} at least one \ |
| 792 |
non-revoked, live subkey:", |
| 793 |
certs_with_subkeys, |
| 794 |
pl(certs_with_subkeys, |
| 795 |
"has", "have")); |
| 796 |
err!(certs_with_a_sha1_protected_binding_sig, |
| 797 |
" {} {} at least one non-revoked, live subkey with \ |
| 798 |
a binding signature that uses SHA-1.", |
| 799 |
certs_with_a_sha1_protected_binding_sig, |
| 800 |
pl(certs_with_a_sha1_protected_binding_sig, |
| 801 |
"has", "have")); |
| 802 |
weprintln!("{} of the non-revoked linted certificates {} at least one non-revoked, live, \ |
| 803 |
signing-capable subkey:", |
| 804 |
certs_with_signing_subkeys, |
| 805 |
pl(certs_with_signing_subkeys, |
| 806 |
"has", "have")); |
| 807 |
err!(certs_with_sha1_protected_backsig, |
| 808 |
" {} {} at least one non-revoked, live, signing-capable subkey \ |
| 809 |
with a strong binding signature, but a backsig \ |
| 810 |
that uses SHA-1.", |
| 811 |
certs_with_sha1_protected_backsig, |
| 812 |
pl(certs_with_sha1_protected_backsig, |
| 813 |
"certificate has", "certificates have")); |
| 814 |
} |
| 815 |
} |
| 816 |
|
| 817 |
if args.fix { |
| 818 |
if unfixed_issue > 0 { |
| 819 |
if ! sq.quiet() { |
| 820 |
err!(unfixed_issue, |
| 821 |
"Failed to fix {} {}.", |
| 822 |
unfixed_issue, |
| 823 |
pl(unfixed_issue, "issue", "issues")); |
| 824 |
} |
| 825 |
return Err(anyhow::anyhow!( |
| 826 |
"Failed to fix {} {}", |
| 827 |
unfixed_issue, |
| 828 |
pl(unfixed_issue, "issue", "issues"))); |
| 829 |
} |
| 830 |
} else if certs_with_issues > 0 { |
| 831 |
return Err(anyhow::anyhow!("{} {} have at least one issue", |
| 832 |
certs_with_issues, |
| 833 |
pl(certs_with_issues, |
| 834 |
"certificate", "certificates"))); |
| 835 |
} |
| 836 |
|
| 837 |
Ok(()) |
| 838 |
} |