From 31c39b05337590c3f64dac265a0e6dbba53f7e91 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sun, 28 Jul 2019 09:15:41 -0700 Subject: Expose more known_hosts related constants This brings us up to date with the current set of host key and known host key enum variants so that we can parse and edit current known_hosts files. This also adds a convenience `From` impl that allows converting from the host key type to a known hosts entry type. --- src/knownhosts.rs | 6 +----- src/lib.rs | 36 ++++++++++++++++++++++++++++++------ src/session.rs | 6 ++++++ 3 files changed, 37 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/knownhosts.rs b/src/knownhosts.rs index 4a6c78d..d5d93c5 100644 --- a/src/knownhosts.rs +++ b/src/knownhosts.rs @@ -39,11 +39,7 @@ use util::{self, Binding, SessionBinding}; /// /// println!("adding {} to the known hosts", host); /// -/// known_hosts.add(host, key, host, match key_type { -/// HostKeyType::Rsa => KnownHostKeyFormat::SshRsa, -/// HostKeyType::Dss => KnownHostKeyFormat::SshDss, -/// HostKeyType::Unknown => panic!("unknown type of key!"), -/// }).unwrap(); +/// known_hosts.add(host, key, host, key_type.into()).unwrap(); /// known_hosts.write_file(&file, KnownHostFileKind::OpenSSH).unwrap(); /// } /// ``` diff --git a/src/lib.rs b/src/lib.rs index 1401a7b..cd98d1a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -199,11 +199,15 @@ pub enum DisconnectCode { } #[allow(missing_docs)] -#[derive(Copy, Clone)] +#[derive(Copy, Clone, Debug)] pub enum HostKeyType { Unknown = raw::LIBSSH2_HOSTKEY_TYPE_UNKNOWN as isize, Rsa = raw::LIBSSH2_HOSTKEY_TYPE_RSA as isize, Dss = raw::LIBSSH2_HOSTKEY_TYPE_DSS as isize, + Ecdsa256 = raw::LIBSSH2_HOSTKEY_TYPE_ECDSA_256 as isize, + Ecdsa384 = raw::LIBSSH2_HOSTKEY_TYPE_ECDSA_384 as isize, + Ecdsa521 = raw::LIBSSH2_HOSTKEY_TYPE_ECDSA_521 as isize, + Ed255219 = raw::LIBSSH2_HOSTKEY_TYPE_ED25519 as isize, } #[allow(missing_docs)] @@ -230,20 +234,21 @@ pub static FLUSH_ALL: i32 = -2; pub static EXTENDED_DATA_STDERR: i32 = 1; #[allow(missing_docs)] -#[derive(Copy, Clone)] +#[derive(Copy, Clone, Debug)] pub enum HashType { Md5 = raw::LIBSSH2_HOSTKEY_HASH_MD5 as isize, - Sha1 = raw:: LIBSSH2_HOSTKEY_HASH_SHA1 as isize, + Sha1 = raw::LIBSSH2_HOSTKEY_HASH_SHA1 as isize, + Sha256 = raw::LIBSSH2_HOSTKEY_HASH_SHA256 as isize, } #[allow(missing_docs)] -#[derive(Copy, Clone)] +#[derive(Copy, Clone, Debug)] pub enum KnownHostFileKind { OpenSSH = raw::LIBSSH2_KNOWNHOST_FILE_OPENSSH as isize, } /// Possible results of a call to `KnownHosts::check` -#[derive(Copy, Clone)] +#[derive(Copy, Clone, Debug)] pub enum CheckResult { /// Hosts and keys match Match = raw::LIBSSH2_KNOWNHOST_CHECK_MATCH as isize, @@ -256,9 +261,28 @@ pub enum CheckResult { } #[allow(missing_docs)] -#[derive(Copy, Clone)] +#[derive(Copy, Clone, Debug)] pub enum KnownHostKeyFormat { + Unknown = raw::LIBSSH2_KNOWNHOST_KEY_UNKNOWN as isize, Rsa1 = raw::LIBSSH2_KNOWNHOST_KEY_RSA1 as isize, SshRsa = raw::LIBSSH2_KNOWNHOST_KEY_SSHRSA as isize, SshDss = raw::LIBSSH2_KNOWNHOST_KEY_SSHDSS as isize, + Ecdsa256 = raw::LIBSSH2_KNOWNHOST_KEY_ECDSA_256 as isize, + Ecdsa384 = raw::LIBSSH2_KNOWNHOST_KEY_ECDSA_384 as isize, + Ecdsa521 = raw::LIBSSH2_KNOWNHOST_KEY_ECDSA_521 as isize, + Ed255219 = raw::LIBSSH2_KNOWNHOST_KEY_ED25519 as isize, +} + +impl From for KnownHostKeyFormat { + fn from(host_type: HostKeyType) -> KnownHostKeyFormat { + match host_type { + HostKeyType::Unknown => KnownHostKeyFormat::Unknown, + HostKeyType::Rsa => KnownHostKeyFormat::SshRsa, + HostKeyType::Dss => KnownHostKeyFormat::SshDss, + HostKeyType::Ecdsa256 => KnownHostKeyFormat::Ecdsa256, + HostKeyType::Ecdsa384 => KnownHostKeyFormat::Ecdsa384, + HostKeyType::Ecdsa521 => KnownHostKeyFormat::Ecdsa521, + HostKeyType::Ed255219 => KnownHostKeyFormat::Ed255219, + } + } } diff --git a/src/session.rs b/src/session.rs index c9dd266..4465eb0 100644 --- a/src/session.rs +++ b/src/session.rs @@ -565,6 +565,11 @@ impl Session { let kind = match kind { raw::LIBSSH2_HOSTKEY_TYPE_RSA => HostKeyType::Rsa, raw::LIBSSH2_HOSTKEY_TYPE_DSS => HostKeyType::Dss, + raw::LIBSSH2_HOSTKEY_TYPE_ECDSA_256 => HostKeyType::Ecdsa256, + raw::LIBSSH2_HOSTKEY_TYPE_ECDSA_384 => HostKeyType::Ecdsa384, + raw::LIBSSH2_HOSTKEY_TYPE_ECDSA_521 => HostKeyType::Ecdsa521, + raw::LIBSSH2_HOSTKEY_TYPE_ED25519 => HostKeyType::Ed255219, + raw::LIBSSH2_HOSTKEY_TYPE_UNKNOWN => HostKeyType::Unknown, _ => HostKeyType::Unknown, }; Some((data, kind)) @@ -579,6 +584,7 @@ impl Session { let len = match hash { HashType::Md5 => 16, HashType::Sha1 => 20, + HashType::Sha256 => 32, }; unsafe { let ret = raw::libssh2_hostkey_hash(self.raw, hash as c_int); -- cgit v1.2.3