| 1 |
//! Implements `sq config inspect policy`. |
| 2 |
|
| 3 |
use std::{ |
| 4 |
collections::BTreeMap, |
| 5 |
io, |
| 6 |
path::PathBuf, |
| 7 |
time::{SystemTime, UNIX_EPOCH}, |
| 8 |
}; |
| 9 |
|
| 10 |
use anyhow::Result; |
| 11 |
|
| 12 |
use sequoia_openpgp::{ |
| 13 |
packet, |
| 14 |
policy::{ |
| 15 |
AsymmetricAlgorithm, |
| 16 |
StandardPolicy, |
| 17 |
}, |
| 18 |
types::{ |
| 19 |
AEADAlgorithm, |
| 20 |
HashAlgorithm, |
| 21 |
SymmetricAlgorithm, |
| 22 |
}, |
| 23 |
}; |
| 24 |
|
| 25 |
use sequoia_policy_config::ConfiguredStandardPolicy; |
| 26 |
|
| 27 |
use crate::{ |
| 28 |
Convert, |
| 29 |
Sq, |
| 30 |
cli::config::inspect, |
| 31 |
config::ConfigFile, |
| 32 |
toml_edit_tree::Node, |
| 33 |
}; |
| 34 |
|
| 35 |
/// Implements `sq config inspect policy`. |
| 36 |
pub fn dispatch(sq: Sq, _: inspect::policy::Command) -> Result<()> { |
| 37 |
let sink = &mut std::io::stdout(); |
| 38 |
|
| 39 |
explain(sink, "Asymmetric algorithms", |
| 40 |
AsymmetricAlgorithm::variants(), |
| 41 |
|a| sq.policy.asymmetric_algo_cutoff(a))?; |
| 42 |
|
| 43 |
explain(sink, "Symmetric algorithms", |
| 44 |
SymmetricAlgorithm::variants(), |
| 45 |
|a| sq.policy.symmetric_algo_cutoff(a))?; |
| 46 |
|
| 47 |
explain(sink, "AEAD algorithms", |
| 48 |
AEADAlgorithm::variants(), |
| 49 |
|a| sq.policy.aead_algo_cutoff(a))?; |
| 50 |
|
| 51 |
// Now the hashes, which are more complicated. |
| 52 |
explain_hashes(sink, sq.policy)?; |
| 53 |
|
| 54 |
// Then, the packets with their versions. |
| 55 |
explain_packets(sink, sq.policy)?; |
| 56 |
|
| 57 |
// Finally, explain where this policy was loaded from. |
| 58 |
explain_configuration(sink, &sq)?; |
| 59 |
|
| 60 |
Ok(()) |
| 61 |
} |
| 62 |
|
| 63 |
/// Explains asymmetric algorithms, symmetric algorithms, and AEAD |
| 64 |
/// modes. |
| 65 |
fn explain<C, K, T>(sink: &mut dyn io::Write, what: &str, known: K, cutoff: C) |
| 66 |
-> Result<()> |
| 67 |
where |
| 68 |
T: Clone, |
| 69 |
T: std::fmt::Display, |
| 70 |
K: Iterator<Item = T>, |
| 71 |
C: Fn(T) -> Option<SystemTime>, |
| 72 |
{ |
| 73 |
wwriteln!(stream = sink, initial_indent = " - ", "{}", what); |
| 74 |
|
| 75 |
let variants = known.collect::<Vec<_>>(); |
| 76 |
let unconstrained = variants.iter() |
| 77 |
.filter(|a| cutoff((*a).clone()).is_none()) |
| 78 |
.map(|a| a.to_string()) |
| 79 |
.collect::<Vec<_>>(); |
| 80 |
wwriteln!(stream = sink, initial_indent = " - Accepted: ", "{}", |
| 81 |
unconstrained.join(", ")); |
| 82 |
|
| 83 |
let mut constrained: BTreeMap<_, Vec<_>> = BTreeMap::default(); |
| 84 |
for (cutoff, algo) in variants.iter() |
| 85 |
.filter_map(|a| cutoff((*a).clone()).map(|t| (t, a.to_string()))) |
| 86 |
{ |
| 87 |
constrained.entry(cutoff).or_default().push(algo); |
| 88 |
} |
| 89 |
for (cutoff, algos) in constrained.iter().rev() { |
| 90 |
if is_rejection(cutoff) { |
| 91 |
wwriteln!(stream = sink, initial_indent = " - Rejected: ", "{}", |
| 92 |
algos.join(", ")); |
| 93 |
} else { |
| 94 |
wwriteln!(stream = sink, initial_indent = " - ", "Accepted until {:?}: {}", |
| 95 |
cutoff.convert(), algos.join(", ")); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
wwriteln!(stream = sink); |
| 100 |
|
| 101 |
Ok(()) |
| 102 |
} |
| 103 |
|
| 104 |
/// Explains hash algorithms. |
| 105 |
fn explain_hashes(sink: &mut dyn io::Write, p: &StandardPolicy) |
| 106 |
-> Result<()> |
| 107 |
{ |
| 108 |
use sequoia_openpgp::policy::HashAlgoSecurity::*; |
| 109 |
|
| 110 |
wwriteln!(stream = sink, initial_indent = " - ", "Hash algorithms"); |
| 111 |
|
| 112 |
let variants = HashAlgorithm::variants().collect::<Vec<_>>(); |
| 113 |
let mut unconstrained = Vec::new(); |
| 114 |
let mut constrained: BTreeMap<_, Vec<_>> = BTreeMap::default(); |
| 115 |
|
| 116 |
for (spr_cutoff, cr_cutoff, algo) in variants.iter() |
| 117 |
.map(|a| { |
| 118 |
let spr = p.hash_cutoff((*a).clone(), SecondPreImageResistance); |
| 119 |
let cr = p.hash_cutoff((*a).clone(), CollisionResistance); |
| 120 |
(spr, cr, a.to_string()) |
| 121 |
}) |
| 122 |
{ |
| 123 |
match (spr_cutoff, cr_cutoff) { |
| 124 |
(None, None) => unconstrained.push(algo), |
| 125 |
(Some(spr), None) => { |
| 126 |
constrained.entry(spr).or_default().push( |
| 127 |
format!("{} requiring second-preimage-resistance", algo)); |
| 128 |
unconstrained.push( |
| 129 |
format!("{} requiring collision-resistance", algo)); |
| 130 |
}, |
| 131 |
(None, Some(cr)) => { |
| 132 |
unconstrained.push( |
| 133 |
format!("{} requiring second-preimage-resistance", algo)); |
| 134 |
constrained.entry(cr).or_default().push( |
| 135 |
format!("{} requiring collision-resistance", algo)); |
| 136 |
}, |
| 137 |
(Some(spr), Some(cr)) => if spr == cr { |
| 138 |
constrained.entry(spr).or_default().push(algo); |
| 139 |
} else { |
| 140 |
constrained.entry(spr).or_default().push( |
| 141 |
format!("{} requiring second-preimage-resistance", algo)); |
| 142 |
constrained.entry(cr).or_default().push( |
| 143 |
format!("{} requiring collision-resistance", algo)); |
| 144 |
}, |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
wwriteln!(stream = sink, initial_indent = " - Accepted: ", "{}", |
| 149 |
unconstrained.join(", ")); |
| 150 |
|
| 151 |
for (cutoff, algos) in constrained.iter().rev() { |
| 152 |
if is_rejection(cutoff) { |
| 153 |
wwriteln!(stream = sink, initial_indent = " - Rejected: ", "{}", |
| 154 |
algos.join(", ")); |
| 155 |
} else { |
| 156 |
wwriteln!(stream = sink, initial_indent = " - ", "Accepted until {:?}: {}", |
| 157 |
cutoff.convert(), algos.join(", ")); |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
wwriteln!(stream = sink); |
| 162 |
|
| 163 |
Ok(()) |
| 164 |
} |
| 165 |
|
| 166 |
/// Explains versioned packets. |
| 167 |
fn explain_packets(sink: &mut dyn io::Write, p: &StandardPolicy) |
| 168 |
-> Result<()> |
| 169 |
{ |
| 170 |
wwriteln!(stream = sink, initial_indent = " - ", "OpenPGP packets"); |
| 171 |
|
| 172 |
let variants = packet::Tag::variants().collect::<Vec<_>>(); |
| 173 |
let mut constrains: BTreeMap<_, Vec<_>> = BTreeMap::default(); |
| 174 |
|
| 175 |
for tag in variants.iter().cloned() { |
| 176 |
if (0..=255).into_iter() |
| 177 |
.all(|v| p.packet_tag_version_cutoff(tag, v).is_none()) |
| 178 |
{ |
| 179 |
constrains.entry(None).or_default() |
| 180 |
.push(tag.to_string()); |
| 181 |
continue; |
| 182 |
} |
| 183 |
|
| 184 |
if (0..=255).into_iter() |
| 185 |
.all(|v| p.packet_tag_version_cutoff(tag, v) |
| 186 |
.map(|c| is_rejection(&c)) |
| 187 |
.unwrap_or(false)) |
| 188 |
{ |
| 189 |
constrains.entry(Some(REJECTION)).or_default() |
| 190 |
.push(tag.to_string()); |
| 191 |
continue; |
| 192 |
} |
| 193 |
|
| 194 |
let default_disposition = p.packet_tag_version_cutoff(tag, 0); |
| 195 |
constrains.entry(default_disposition).or_default() |
| 196 |
.push(tag.to_string()); |
| 197 |
|
| 198 |
for v in 0..=255 { |
| 199 |
let cutoff = p.packet_tag_version_cutoff(tag, v); |
| 200 |
if cutoff != default_disposition { |
| 201 |
constrains.entry(cutoff).or_default() |
| 202 |
.push(format!("v{} {}", v, tag)); |
| 203 |
} |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
for (_, packets) in constrains.iter() |
| 208 |
.filter(|(cutoff, _)| cutoff.is_none()) |
| 209 |
{ |
| 210 |
wwriteln!(stream = sink, initial_indent = " - Accepted: ", |
| 211 |
"{}", packets.join(", ")); |
| 212 |
} |
| 213 |
|
| 214 |
for (cutoff, packets) in constrains.iter().rev() |
| 215 |
.filter_map(|(cutoff, packet)| cutoff.map(|c| (c, packet))) |
| 216 |
{ |
| 217 |
if is_rejection(&cutoff) { |
| 218 |
wwriteln!(stream = sink, initial_indent = " - Rejected: ", |
| 219 |
"{}", packets.join(", ")); |
| 220 |
} else { |
| 221 |
wwriteln!(stream = sink, initial_indent = " - ", |
| 222 |
"Accepted until {:?}: {}", |
| 223 |
cutoff.convert(), packets.join(", ")); |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
wwriteln!(stream = sink); |
| 228 |
|
| 229 |
Ok(()) |
| 230 |
} |
| 231 |
|
| 232 |
/// Explains where the policy was loaded from. |
| 233 |
fn explain_configuration(sink: &mut dyn io::Write, sq: &Sq) |
| 234 |
-> Result<()> |
| 235 |
{ |
| 236 |
wwriteln!(stream = sink, initial_indent = " - ", "Policy configuration"); |
| 237 |
|
| 238 |
// Whether any policy configuration has been read. |
| 239 |
let mut read_any = false; |
| 240 |
|
| 241 |
// The global configuration. |
| 242 |
let global = PathBuf::from(ConfigFile::global_crypto_policy_file()); |
| 243 |
let global_var = ConfiguredStandardPolicy::ENV_VAR; |
| 244 |
if global.exists() { |
| 245 |
wwriteln!(stream = sink, initial_indent = " - ", |
| 246 |
"global cryptographic policy"); |
| 247 |
wwriteln!(stream = sink, initial_indent = " - ", |
| 248 |
"{}", global.display()); |
| 249 |
|
| 250 |
if std::env::var(global_var).is_ok() { |
| 251 |
wwriteln!(stream = sink, initial_indent = " - ", |
| 252 |
"overwritten via {}", global_var); |
| 253 |
} else { |
| 254 |
wwriteln!(stream = sink, initial_indent = " - ", |
| 255 |
"can be overwritten via {}", global_var); |
| 256 |
} |
| 257 |
|
| 258 |
read_any = true; |
| 259 |
} |
| 260 |
|
| 261 |
// The referenced policy, if any. |
| 262 |
if let Some(path) = sq.config.policy_path() { |
| 263 |
wwriteln!(stream = sink, initial_indent = " - ", |
| 264 |
"referenced cryptographic policy"); |
| 265 |
wwriteln!(stream = sink, initial_indent = " - ", |
| 266 |
"{}", path.display()); |
| 267 |
wwriteln!(stream = sink, initial_indent = " - ", |
| 268 |
"can be changed via the configuration setting \ |
| 269 |
`policy.path`"); |
| 270 |
|
| 271 |
read_any = true; |
| 272 |
} |
| 273 |
|
| 274 |
// The inline policy, if any. |
| 275 |
let config_file = sq.home.as_ref().map(ConfigFile::file_name); |
| 276 |
if let Some(config) = &config_file { |
| 277 |
if config.exists() && [ |
| 278 |
"policy.hash_algorithms", |
| 279 |
"policy.symmetric_algorithms", |
| 280 |
"policy.asymmetric_algorithms", |
| 281 |
"policy.aead_algorithms", |
| 282 |
"policy.packets", |
| 283 |
].iter().any(|p| { |
| 284 |
sq.config_file.as_item().traverse(&p.parse().unwrap()) |
| 285 |
.ok() |
| 286 |
.and_then(|n| n.iter().next()) |
| 287 |
.map(|_| true) |
| 288 |
.unwrap_or(false) |
| 289 |
}) { |
| 290 |
wwriteln!(stream = sink, initial_indent = " - ", |
| 291 |
"inline cryptographic policy"); |
| 292 |
wwriteln!(stream = sink, initial_indent = " - ", |
| 293 |
"can be changed in the configuration file \ |
| 294 |
{}", config.display()); |
| 295 |
|
| 296 |
read_any = true; |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
if ! read_any { |
| 301 |
wwriteln!(stream = sink, initial_indent = " - ", |
| 302 |
"default cryptographic policy"); |
| 303 |
wwriteln!(stream = sink, initial_indent = " - ", |
| 304 |
"This is the default cryptographic policy. \ |
| 305 |
It can be modified by:"); |
| 306 |
wwriteln!(stream = sink, initial_indent = " - ", |
| 307 |
"providing a global policy in {}", global.display()); |
| 308 |
wwriteln!(stream = sink, initial_indent = " - ", |
| 309 |
"referencing a policy via the configuration setting \ |
| 310 |
`policy.path`"); |
| 311 |
if let Some(config) = &config_file { |
| 312 |
wwriteln!(stream = sink, initial_indent = " - ", |
| 313 |
"providing an inline policy in the configuration \ |
| 314 |
file {}", config.display()); |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
Ok(()) |
| 319 |
} |
| 320 |
|
| 321 |
/// Whether the given cut-off time is an outright rejection. |
| 322 |
fn is_rejection(cutoff: &SystemTime) -> bool { |
| 323 |
*cutoff == REJECTION |
| 324 |
} |
| 325 |
|
| 326 |
/// Cutoff marker for outright rejection. |
| 327 |
const REJECTION: SystemTime = UNIX_EPOCH; |