| 1 |
use std::borrow::Cow; |
| 2 |
use std::collections::BTreeSet; |
| 3 |
use std::time::Duration; |
| 4 |
use std::time::SystemTime; |
| 5 |
|
| 6 |
use anyhow::Result; |
| 7 |
|
| 8 |
use sequoia_openpgp as openpgp; |
| 9 |
use openpgp::Fingerprint; |
| 10 |
use openpgp::KeyHandle; |
| 11 |
use openpgp::KeyID; |
| 12 |
use openpgp::packet::UserID; |
| 13 |
use openpgp::policy::NullPolicy; |
| 14 |
|
| 15 |
use sequoia_wot as wot; |
| 16 |
|
| 17 |
use crate::output::sanitize::Safe; |
| 18 |
|
| 19 |
pub mod cert_designator; |
| 20 |
pub mod file; |
| 21 |
|
| 22 |
mod revoke; |
| 23 |
pub use revoke::get_secret_signer; |
| 24 |
pub use revoke::RevocationOutput; |
| 25 |
|
| 26 |
pub mod key; |
| 27 |
|
| 28 |
pub mod password; |
| 29 |
pub mod pki; |
| 30 |
pub mod userid; |
| 31 |
|
| 32 |
pub mod types; |
| 33 |
pub mod ui; |
| 34 |
|
| 35 |
pub const NULL_POLICY: &NullPolicy = unsafe { &NullPolicy::new() }; |
| 36 |
|
| 37 |
/// Something like a User ID. |
| 38 |
/// |
| 39 |
/// This is used to avoid unnecessary allocations. |
| 40 |
#[derive(Debug, Clone, PartialOrd, Ord, PartialEq, Eq)] |
| 41 |
enum UserIDLike { |
| 42 |
UserID(UserID), |
| 43 |
String(String), |
| 44 |
Unknown, |
| 45 |
} |
| 46 |
|
| 47 |
/// The preferred user ID for a certificate. |
| 48 |
/// |
| 49 |
/// This can be smartly truncated using the precision formatting |
| 50 |
/// parameter, e.g.: |
| 51 |
/// |
| 52 |
/// ```text |
| 53 |
/// format!("{:.70}", userid); |
| 54 |
/// ``` |
| 55 |
#[derive(Clone, PartialOrd, Ord, PartialEq, Eq)] |
| 56 |
pub struct PreferredUserID { |
| 57 |
userid: UserIDLike, |
| 58 |
trust_amount: usize, |
| 59 |
} |
| 60 |
|
| 61 |
struct PreferredUserIDDisplay<'p>(&'p PreferredUserID); |
| 62 |
|
| 63 |
impl<'p> std::fmt::Display for PreferredUserIDDisplay<'p> { |
| 64 |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) |
| 65 |
-> Result<(), std::fmt::Error> |
| 66 |
{ |
| 67 |
let userid_; |
| 68 |
let userid = match self.0.userid { |
| 69 |
UserIDLike::Unknown => { |
| 70 |
return write!(f, "<unknown>"); |
| 71 |
} |
| 72 |
UserIDLike::UserID(ref userid) => { |
| 73 |
userid_ = String::from_utf8_lossy(userid.value()); |
| 74 |
&userid_[..] |
| 75 |
} |
| 76 |
UserIDLike::String(ref userid) => { |
| 77 |
&userid[..] |
| 78 |
} |
| 79 |
}; |
| 80 |
|
| 81 |
let userid = Safe(userid).to_string(); |
| 82 |
|
| 83 |
let suffix_; |
| 84 |
let suffix = if self.0.trust_amount == 0 { |
| 85 |
"(UNAUTHENTICATED)" |
| 86 |
} else if self.0.trust_amount < wot::FULLY_TRUSTED { |
| 87 |
suffix_ = format!("(partially authenticated, {}/{})", |
| 88 |
self.0.trust_amount, wot::FULLY_TRUSTED); |
| 89 |
&suffix_[..] |
| 90 |
} else { |
| 91 |
"(authenticated)" |
| 92 |
}; |
| 93 |
|
| 94 |
// We always keep the suffix and at least 16 characters of the user ID. |
| 95 |
const MIN_USERID: usize = 16; |
| 96 |
|
| 97 |
if let Some(width) = f.precision() { |
| 98 |
let space_for_userid = width.saturating_sub(1 + suffix.len()).max(MIN_USERID); |
| 99 |
if userid.chars().count() > space_for_userid { |
| 100 |
return write!(f, "{}… {}", |
| 101 |
userid.chars().take(MIN_USERID).collect::<String>(), |
| 102 |
suffix); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
write!(f, "{} {}", userid, suffix) |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
impl PreferredUserID { |
| 111 |
/// Returns a new `PreferredUserID`. |
| 112 |
pub fn from_userid<U>(userid: U, trust_amount: usize) -> Self |
| 113 |
where U: Into<UserID> |
| 114 |
{ |
| 115 |
Self { |
| 116 |
userid: UserIDLike::UserID(userid.into()), |
| 117 |
trust_amount, |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
/// Returns a new `PreferredUserID`. |
| 122 |
pub fn from_string<S>(userid: S, trust_amount: usize) -> Self |
| 123 |
where S: Into<String> |
| 124 |
{ |
| 125 |
Self { |
| 126 |
userid: UserIDLike::String(userid.into()), |
| 127 |
trust_amount, |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
/// Returns a new "unknown" `PreferredUserID`. |
| 132 |
pub fn unknown() -> Self { |
| 133 |
Self { |
| 134 |
userid: UserIDLike::Unknown, |
| 135 |
trust_amount: 0, |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
/// Returns the user ID with authentication information, suitable |
| 140 |
/// for displaying to the user. |
| 141 |
/// |
| 142 |
/// Do not use for arguments in hints. |
| 143 |
pub fn display<'p>(&'p self) -> impl std::fmt::Display + 'p { |
| 144 |
PreferredUserIDDisplay(self) |
| 145 |
} |
| 146 |
|
| 147 |
/// Returns the user ID without authentication information, |
| 148 |
/// suitable for use in text for displaying to the user, where a |
| 149 |
/// best effort string conversion is better than failing, and |
| 150 |
/// byte-accuracy is not required. |
| 151 |
/// |
| 152 |
/// Do not use for general displaying, use |
| 153 |
/// [`PreferredUserID::display`] for that which includes |
| 154 |
/// authentication information. |
| 155 |
/// |
| 156 |
/// Do not use for arguments in hints. |
| 157 |
pub fn userid_lossy(&self) -> Cow<str> { |
| 158 |
match &self.userid { |
| 159 |
UserIDLike::UserID(u) => String::from_utf8_lossy(u.value()), |
| 160 |
UserIDLike::String(u) => Cow::Borrowed(&u), |
| 161 |
UserIDLike::Unknown => Cow::Borrowed("<unknown userid>"), |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
/// Returns the user ID without authentication information, |
| 166 |
/// suitable for use as argument in hints. |
| 167 |
/// |
| 168 |
/// Do not use for general displaying, use |
| 169 |
/// [`PreferredUserID::display`] for that which includes |
| 170 |
/// authentication information. |
| 171 |
pub fn userid(&self) -> Result<&str> { |
| 172 |
match &self.userid { |
| 173 |
UserIDLike::UserID(u) => Ok(std::str::from_utf8(u.value())?), |
| 174 |
UserIDLike::String(u) => Ok(&u), |
| 175 |
UserIDLike::Unknown => |
| 176 |
Err(anyhow::anyhow!("User ID is unknown")), |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
/// Returns the trust amount. |
| 181 |
pub fn trust_amount(&self) -> usize { |
| 182 |
self.trust_amount |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
/// The creation time for the trust root and intermediate CAs. |
| 187 |
/// |
| 188 |
/// We use a creation time in the past (Feb 2002) so that it is still |
| 189 |
/// possible to use the CA when the reference time is in the past. |
| 190 |
// XXX: This is copied from sequoia-cert-store. It would be nice to |
| 191 |
// import it, but it is private. |
| 192 |
pub fn ca_creation_time() -> SystemTime { |
| 193 |
SystemTime::UNIX_EPOCH + Duration::new(1014235320, 0) |
| 194 |
} |
| 195 |
|
| 196 |
/// Dealias and deduplicate a list of key handles. |
| 197 |
/// |
| 198 |
/// A signature often has a fingerprint issuer packet and a key ID |
| 199 |
/// issuer packet where the key ID is just the key ID of the |
| 200 |
/// fingerprint. Remove these aliases. |
| 201 |
pub fn key_handle_dealias(khs: &[KeyHandle]) -> impl Iterator<Item = KeyHandle> { |
| 202 |
let mut fprs: Vec<Fingerprint> = Vec::with_capacity(khs.len()); |
| 203 |
let mut keyids: Vec<KeyID> = Vec::with_capacity(khs.len()); |
| 204 |
|
| 205 |
khs.iter().fold((&mut fprs, &mut keyids), |(fprs, keyids), kh| { |
| 206 |
match kh { |
| 207 |
KeyHandle::Fingerprint(fpr) => fprs.push(fpr.clone()), |
| 208 |
KeyHandle::KeyID(keyid) => keyids.push(keyid.clone()), |
| 209 |
} |
| 210 |
|
| 211 |
(fprs, keyids) |
| 212 |
}); |
| 213 |
|
| 214 |
fprs.sort(); |
| 215 |
fprs.dedup(); |
| 216 |
|
| 217 |
keyids.sort(); |
| 218 |
keyids.dedup(); |
| 219 |
|
| 220 |
// Remove any key IDs that alias a fingerprint. |
| 221 |
let dedup: BTreeSet<KeyID> = fprs |
| 222 |
.iter() |
| 223 |
.map(|fpr| KeyID::from(fpr)) |
| 224 |
.collect(); |
| 225 |
|
| 226 |
keyids.retain(|keyid| ! dedup.contains(keyid)); |
| 227 |
|
| 228 |
fprs.into_iter().map(KeyHandle::from) |
| 229 |
.chain(keyids.into_iter().map(KeyHandle::from)) |
| 230 |
} |