| 1 |
use std::time::SystemTime; |
| 2 |
|
| 3 |
use clap::ArgMatches; |
| 4 |
|
| 5 |
use sequoia_openpgp as openpgp; |
| 6 |
use openpgp::{Cert, Result}; |
| 7 |
use openpgp::packet::prelude::*; |
| 8 |
use openpgp::policy::HashAlgoSecurity; |
| 9 |
use openpgp::policy::Policy; |
| 10 |
|
| 11 |
use crate::Sq; |
| 12 |
|
| 13 |
use crate::cli::encrypt::CompressionMode; |
| 14 |
use crate::cli::types::FileOrStdout; |
| 15 |
use crate::cli::types::MyAsRef; |
| 16 |
use crate::cli::{SqCommand, SqSubcommands}; |
| 17 |
|
| 18 |
pub mod autocrypt; |
| 19 |
pub mod cert; |
| 20 |
pub mod config; |
| 21 |
pub mod decrypt; |
| 22 |
pub mod download; |
| 23 |
pub mod encrypt; |
| 24 |
pub mod keyring; |
| 25 |
pub mod sign; |
| 26 |
pub mod inspect; |
| 27 |
pub mod key; |
| 28 |
pub mod network; |
| 29 |
pub mod packet; |
| 30 |
pub mod pki; |
| 31 |
pub mod verify; |
| 32 |
pub mod version; |
| 33 |
|
| 34 |
/// Dispatches the top-level subcommand. |
| 35 |
pub fn dispatch(sq: Sq, command: SqCommand, matches: &ArgMatches) -> Result<()> |
| 36 |
{ |
| 37 |
let matches = matches.subcommand().unwrap().1; |
| 38 |
match command.subcommand { |
| 39 |
SqSubcommands::Encrypt(mut command) => { |
| 40 |
command.profile_source = matches.value_source("profile"); |
| 41 |
encrypt::dispatch(sq, command) |
| 42 |
}, |
| 43 |
SqSubcommands::Decrypt(command) => |
| 44 |
decrypt::dispatch(sq, command), |
| 45 |
SqSubcommands::Sign(command) => |
| 46 |
sign::dispatch(sq, command), |
| 47 |
SqSubcommands::Verify(command) => |
| 48 |
verify::dispatch(sq, command), |
| 49 |
SqSubcommands::Download(command) => |
| 50 |
download::dispatch(sq, command), |
| 51 |
|
| 52 |
SqSubcommands::Inspect(command) => |
| 53 |
inspect::dispatch(sq, command), |
| 54 |
|
| 55 |
SqSubcommands::Cert(command) => |
| 56 |
cert::dispatch(sq, command), |
| 57 |
SqSubcommands::Key(command) => |
| 58 |
key::dispatch(sq, command, matches), |
| 59 |
|
| 60 |
SqSubcommands::Pki(command) => |
| 61 |
pki::dispatch(sq, command, matches), |
| 62 |
|
| 63 |
SqSubcommands::Network(command) => |
| 64 |
network::dispatch(sq, command, matches), |
| 65 |
SqSubcommands::Keyring(command) => |
| 66 |
keyring::dispatch(sq, command), |
| 67 |
SqSubcommands::Packet(command) => |
| 68 |
packet::dispatch(sq, command), |
| 69 |
|
| 70 |
SqSubcommands::Config(command) => |
| 71 |
config::dispatch(sq, command), |
| 72 |
|
| 73 |
SqSubcommands::Version(command) => |
| 74 |
version::dispatch(sq, command), |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
/// Returns the active certification, if any, for the specified bindings. |
| 79 |
/// |
| 80 |
/// Note: if `n` User IDs are provided, then the returned vector has |
| 81 |
/// `n` elements. |
| 82 |
pub fn active_certification<U>( |
| 83 |
sq: &Sq, |
| 84 |
cert: &Cert, userids: impl Iterator<Item=U>, |
| 85 |
issuer: &Key<openpgp::packet::key::PublicParts, |
| 86 |
openpgp::packet::key::UnspecifiedRole>) |
| 87 |
-> Vec<(U, Option<Signature>)> |
| 88 |
where |
| 89 |
U: MyAsRef<UserID> |
| 90 |
{ |
| 91 |
let issuer_kh = issuer.key_handle(); |
| 92 |
|
| 93 |
userids.map(|userid_ref| { |
| 94 |
let userid = userid_ref.as_ref(); |
| 95 |
|
| 96 |
let ua = match cert.userids() |
| 97 |
.filter(|ua| ua.userid() == userid).next() |
| 98 |
{ |
| 99 |
Some(ua) => ua, |
| 100 |
None => return (userid_ref, None), |
| 101 |
}; |
| 102 |
|
| 103 |
// Get certifications that: |
| 104 |
// |
| 105 |
// - Have a creation time, |
| 106 |
// - Are not younger than the reference time, |
| 107 |
// - Are not expired, |
| 108 |
// - Alias the issuer, and |
| 109 |
// - Satisfy the policy. |
| 110 |
let mut certifications = ua.bundle().certifications() |
| 111 |
.filter(|sig| { |
| 112 |
if let Some(ct) = sig.signature_creation_time() { |
| 113 |
ct <= sq.time |
| 114 |
&& sig.signature_validity_period() |
| 115 |
.map(|vp| { |
| 116 |
sq.time < ct + vp |
| 117 |
}) |
| 118 |
.unwrap_or(true) |
| 119 |
&& sig.get_issuers().iter().any(|i| i.aliases(&issuer_kh)) |
| 120 |
&& sq.policy.signature( |
| 121 |
sig, HashAlgoSecurity::CollisionResistance).is_ok() |
| 122 |
} else { |
| 123 |
false |
| 124 |
} |
| 125 |
}) |
| 126 |
.collect::<Vec<&Signature>>(); |
| 127 |
|
| 128 |
// Sort so the newest signature is first. |
| 129 |
certifications.sort_unstable_by(|a, b| { |
| 130 |
a.signature_creation_time().unwrap() |
| 131 |
.cmp(&b.signature_creation_time().unwrap()) |
| 132 |
.reverse() |
| 133 |
.then(a.mpis().cmp(&b.mpis())) |
| 134 |
}); |
| 135 |
|
| 136 |
// Return the first valid signature, which is the most recent one |
| 137 |
// that is no younger than sq.time. |
| 138 |
let pk = ua.cert().primary_key().key(); |
| 139 |
let certification = certifications.into_iter() |
| 140 |
.filter_map(|sig| { |
| 141 |
let sig = sig.clone(); |
| 142 |
if sig.verify_userid_binding(issuer, pk, userid).is_ok() { |
| 143 |
Some(sig) |
| 144 |
} else { |
| 145 |
None |
| 146 |
} |
| 147 |
}) |
| 148 |
.next(); |
| 149 |
(userid_ref, certification) |
| 150 |
}).collect() |
| 151 |
} |
| 152 |
|
| 153 |
// Returns the smallest valid certificate. |
| 154 |
// |
| 155 |
// Given a certificate, returns the smallest valid certificate that is |
| 156 |
// still technically valid according to RFC 4880 and popular OpenPGP |
| 157 |
// implementations. |
| 158 |
// |
| 159 |
// In particular, this function extracts the primary key, and a User |
| 160 |
// ID with its active binding signature. If there is no valid User |
| 161 |
// ID, it returns the active direct key signature. If no User ID is |
| 162 |
// specified, or the specified User ID does not occur, then the |
| 163 |
// primary User ID is used and the specified User ID is added without |
| 164 |
// a binding signature. |
| 165 |
#[allow(dead_code)] |
| 166 |
pub fn cert_stub(cert: Cert, |
| 167 |
policy: &dyn Policy, |
| 168 |
timestamp: Option<SystemTime>, |
| 169 |
userid: Option<&UserID>) |
| 170 |
-> Result<Cert> |
| 171 |
{ |
| 172 |
let vc = cert.with_policy(policy, timestamp)?; |
| 173 |
|
| 174 |
let mut packets = Vec::with_capacity(4); |
| 175 |
packets.push(Packet::from(vc.primary_key().key().clone())); |
| 176 |
|
| 177 |
let mut found = false; |
| 178 |
if let Some(userid) = userid { |
| 179 |
for u in vc.userids() { |
| 180 |
if u.userid() == userid { |
| 181 |
found = true; |
| 182 |
packets.push(Packet::from(userid.clone())); |
| 183 |
packets.push(Packet::from(u.binding_signature().clone())); |
| 184 |
} |
| 185 |
} |
| 186 |
} |
| 187 |
if ! found { |
| 188 |
// We didn't find the required User ID or no User ID was |
| 189 |
// specified. Emit the primary User ID. If there is none, |
| 190 |
// emit the direct key signature. |
| 191 |
if let Ok(uid) = vc.primary_userid() { |
| 192 |
packets.push(Packet::from(uid.userid().clone())); |
| 193 |
packets.push(Packet::from(uid.binding_signature().clone())); |
| 194 |
} else { |
| 195 |
packets.push( |
| 196 |
Packet::from(vc.primary_key().binding_signature().clone())); |
| 197 |
} |
| 198 |
|
| 199 |
// And include the specified User ID as the very last packet. |
| 200 |
// This is convenient when we append a revocation certificate |
| 201 |
// as the revocation certificate is at the right place. |
| 202 |
if let Some(userid) = userid { |
| 203 |
packets.push(Packet::from(userid.clone())); |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
Ok(Cert::from_packets(packets.into_iter())?) |
| 208 |
} |