дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-04 17:21:13 +02:00
The built-in SSH client only offered russh's defaults and ignored the algorithm options of ~/.ssh/config, so a server that only speaks older algorithms could not be reached. The client now starts from the OpenSSH client's defaults (adding the ECDH key exchanges, aes128-gcm and the SHA-1 MACs) and applies KexAlgorithms, HostKeyAlgorithms, Ciphers and MACs with OpenSSH's +, - and ^ forms. They apply to SSH configurations that sign in with a password or key as well as to OpenSSH config hosts. When negotiation fails, the error names what the server offers and the line that allows it. Fixes #7757
177 рядки
6.6 KiB
Rust
177 рядки
6.6 KiB
Rust
//! The algorithms a connection offers: the OpenSSH client's defaults, changed by
|
|
//! `KexAlgorithms`, `HostKeyAlgorithms`, `Ciphers` and `MACs` of `~/.ssh/config`.
|
|
|
|
use std::borrow::Cow;
|
|
|
|
use russh::keys::{Algorithm, EcdsaCurve, HashAlg};
|
|
use russh::{Preferred, cipher, kex, mac};
|
|
|
|
use crate::target::glob_match;
|
|
|
|
/// The algorithm lists of one host in `~/.ssh/config`, as written.
|
|
#[derive(Debug, Clone, Default, PartialEq, Eq)]
|
|
pub(crate) struct AlgorithmConfig {
|
|
pub kex: Option<String>,
|
|
pub host_keys: Option<String>,
|
|
pub ciphers: Option<String>,
|
|
pub macs: Option<String>,
|
|
}
|
|
|
|
const DEFAULT_KEX: &[kex::Name] = &[
|
|
kex::MLKEM768X25519_SHA256,
|
|
kex::CURVE25519,
|
|
kex::CURVE25519_PRE_RFC_8731,
|
|
kex::ECDH_SHA2_NISTP256,
|
|
kex::ECDH_SHA2_NISTP384,
|
|
kex::ECDH_SHA2_NISTP521,
|
|
kex::DH_GEX_SHA256,
|
|
kex::DH_G16_SHA512,
|
|
kex::DH_G18_SHA512,
|
|
kex::DH_G14_SHA256,
|
|
];
|
|
|
|
/// Not algorithms but markers of the key exchange list, always offered.
|
|
const KEX_EXTENSIONS: &[kex::Name] = &[kex::EXTENSION_SUPPORT_AS_CLIENT, kex::EXTENSION_OPENSSH_STRICT_KEX_AS_CLIENT];
|
|
|
|
const DEFAULT_CIPHERS: &[cipher::Name] = &[
|
|
cipher::CHACHA20_POLY1305,
|
|
cipher::AES_128_CTR,
|
|
cipher::AES_192_CTR,
|
|
cipher::AES_256_CTR,
|
|
cipher::AES_128_GCM,
|
|
cipher::AES_256_GCM,
|
|
];
|
|
|
|
const DEFAULT_MACS: &[mac::Name] = &[
|
|
mac::HMAC_SHA256_ETM,
|
|
mac::HMAC_SHA512_ETM,
|
|
mac::HMAC_SHA1_ETM,
|
|
mac::HMAC_SHA256,
|
|
mac::HMAC_SHA512,
|
|
mac::HMAC_SHA1,
|
|
];
|
|
|
|
fn default_host_keys() -> Vec<Algorithm> {
|
|
vec![
|
|
Algorithm::Ed25519,
|
|
Algorithm::Ecdsa { curve: EcdsaCurve::NistP256 },
|
|
Algorithm::Ecdsa { curve: EcdsaCurve::NistP384 },
|
|
Algorithm::Ecdsa { curve: EcdsaCurve::NistP521 },
|
|
Algorithm::Rsa { hash: Some(HashAlg::Sha512) },
|
|
Algorithm::Rsa { hash: Some(HashAlg::Sha256) },
|
|
Algorithm::Rsa { hash: None },
|
|
]
|
|
}
|
|
|
|
impl AlgorithmConfig {
|
|
pub(crate) fn preferred(&self) -> Preferred {
|
|
let mut kex = apply(self.kex.as_deref(), DEFAULT_KEX, kex::ALL_KEX_ALGORITHMS.iter().map(|n| **n));
|
|
kex.retain(|n| *n != kex::NONE);
|
|
kex.extend_from_slice(KEX_EXTENSIONS);
|
|
// Every host key algorithm this client verifies is a default.
|
|
let host_keys = apply(self.host_keys.as_deref(), &default_host_keys(), default_host_keys());
|
|
let mut ciphers = apply(self.ciphers.as_deref(), DEFAULT_CIPHERS, cipher::ALL_CIPHERS.iter().map(|n| **n));
|
|
ciphers.retain(|n| *n != cipher::NONE && *n != cipher::CLEAR);
|
|
let mut macs = apply(self.macs.as_deref(), DEFAULT_MACS, mac::ALL_MAC_ALGORITHMS.iter().map(|n| **n));
|
|
macs.retain(|n| *n != mac::NONE);
|
|
Preferred {
|
|
kex: Cow::Owned(kex),
|
|
key: Cow::Owned(host_keys),
|
|
cipher: Cow::Owned(ciphers),
|
|
mac: Cow::Owned(macs),
|
|
..Preferred::default()
|
|
}
|
|
}
|
|
}
|
|
|
|
/// One list option as OpenSSH reads it: `+a,b` appends to the defaults, `-a,b` removes
|
|
/// from them, `^a,b` puts first, anything else replaces them. Names may hold `*` and `?`;
|
|
/// those this client doesn't implement are skipped, and a list left empty keeps the defaults.
|
|
fn apply<N: Clone + PartialEq + AsRef<str>>(
|
|
spec: Option<&str>,
|
|
defaults: &[N],
|
|
supported: impl IntoIterator<Item = N>,
|
|
) -> Vec<N> {
|
|
let Some(spec) = spec.map(str::trim).filter(|s| !s.is_empty()) else { return defaults.to_vec() };
|
|
let (op, list) = match spec.chars().next() {
|
|
Some(op @ ('+' | '-' | '^')) => (Some(op), &spec[1..]),
|
|
_ => (None, spec),
|
|
};
|
|
let patterns: Vec<&str> = list.split(',').map(str::trim).filter(|p| !p.is_empty()).collect();
|
|
let matches = |n: &N| patterns.iter().any(|p| glob_match(p, n.as_ref()));
|
|
let supported: Vec<N> = supported.into_iter().collect();
|
|
let mut named: Vec<N> = Vec::new();
|
|
for p in &patterns {
|
|
for n in supported.iter().filter(|n| glob_match(p, n.as_ref())) {
|
|
if !named.contains(n) {
|
|
named.push(n.clone());
|
|
}
|
|
}
|
|
}
|
|
let result: Vec<N> = match op {
|
|
Some('+') => {
|
|
let extra: Vec<N> = named.into_iter().filter(|n| !defaults.contains(n)).collect();
|
|
defaults.iter().cloned().chain(extra).collect()
|
|
}
|
|
Some('-') => defaults.iter().filter(|n| !matches(n)).cloned().collect(),
|
|
Some(_) => {
|
|
let rest: Vec<N> = defaults.iter().filter(|n| !named.contains(n)).cloned().collect();
|
|
named.into_iter().chain(rest).collect()
|
|
}
|
|
None => named,
|
|
};
|
|
if result.is_empty() {
|
|
tracing::warn!("ssh config: no supported algorithm in `{spec}`; using the defaults");
|
|
return defaults.to_vec();
|
|
}
|
|
result
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
fn names<N: AsRef<str>>(list: &[N]) -> Vec<&str> {
|
|
list.iter().map(AsRef::as_ref).collect()
|
|
}
|
|
|
|
#[test]
|
|
fn defaults_follow_the_openssh_client() {
|
|
let p = AlgorithmConfig::default().preferred();
|
|
assert!(names(&p.kex).contains(&"ecdh-sha2-nistp256"));
|
|
assert!(!names(&p.kex).contains(&"diffie-hellman-group14-sha1"));
|
|
assert!(names(&p.kex).ends_with(&["ext-info-c", "[email protected]"]));
|
|
assert!(names(&p.mac).contains(&"hmac-sha1"));
|
|
assert!(!names(&p.cipher).iter().any(|c| c.ends_with("-cbc")));
|
|
}
|
|
|
|
#[test]
|
|
fn legacy_algorithms_are_added_removed_or_put_first() {
|
|
let config = AlgorithmConfig {
|
|
kex: Some("+diffie-hellman-group1-sha1,diffie-hellman-group14-sha1,unknown-kex".into()),
|
|
host_keys: Some("^ssh-rsa".into()),
|
|
ciphers: Some("aes128-cbc,aes256-ctr".into()),
|
|
macs: Some("-*sha1*".into()),
|
|
};
|
|
let p = config.preferred();
|
|
let kex = names(&p.kex);
|
|
assert_eq!(kex[..3], ["mlkem768x25519-sha256", "curve25519-sha256", "[email protected]"]);
|
|
let legacy = kex.iter().position(|k| *k == "diffie-hellman-group1-sha1").unwrap();
|
|
assert_eq!(kex[legacy..legacy + 2], ["diffie-hellman-group1-sha1", "diffie-hellman-group14-sha1"]);
|
|
assert!(!kex.contains(&"unknown-kex"));
|
|
assert_eq!(p.key.first(), Some(&Algorithm::Rsa { hash: None }));
|
|
assert_eq!(p.key.len(), default_host_keys().len());
|
|
assert_eq!(names(&p.cipher), ["aes128-cbc", "aes256-ctr"]);
|
|
assert_eq!(
|
|
names(&p.mac),
|
|
["[email protected]", "[email protected]", "hmac-sha2-256", "hmac-sha2-512"]
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn a_list_of_nothing_supported_keeps_the_defaults() {
|
|
let p = AlgorithmConfig { ciphers: Some("blowfish-cbc".into()), ..Default::default() }.preferred();
|
|
assert_eq!(names(&p.cipher), names(DEFAULT_CIPHERS));
|
|
}
|
|
}
|