-
-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
175 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
use std::convert::TryFrom; | ||
|
||
#[derive(Debug, Clone)] | ||
pub enum Compression { | ||
None, | ||
|
@@ -19,10 +21,43 @@ pub enum Decompress { | |
Zlib(flate2::Decompress), | ||
} | ||
|
||
#[derive(Debug, PartialEq, Eq, Copy, Clone, Hash)] | ||
pub struct Name(&'static str); | ||
impl AsRef<str> for Name { | ||
fn as_ref(&self) -> &str { | ||
self.0 | ||
} | ||
} | ||
|
||
impl TryFrom<&str> for Name { | ||
type Error = (); | ||
fn try_from(s: &str) -> Result<Name, ()> { | ||
ALL_COMPRESSION_ALGORITHMS | ||
.iter() | ||
.find(|x| x.0 == s) | ||
.map(|x| **x) | ||
.ok_or(()) | ||
} | ||
} | ||
|
||
pub const NONE: Name = Name("none"); | ||
#[cfg(feature = "flate2")] | ||
pub const ZLIB: Name = Name("zlib"); | ||
#[cfg(feature = "flate2")] | ||
pub const ZLIB_LEGACY: Name = Name("[email protected]"); | ||
|
||
pub const ALL_COMPRESSION_ALGORITHMS: &[&Name] = &[ | ||
&NONE, | ||
#[cfg(feature = "flate2")] | ||
&ZLIB, | ||
#[cfg(feature = "flate2")] | ||
&ZLIB_LEGACY, | ||
]; | ||
|
||
#[cfg(feature = "flate2")] | ||
impl Compression { | ||
pub fn from_string(s: &str) -> Self { | ||
if s == "zlib" || s == "[email protected]" { | ||
pub fn new(name: &Name) -> Self { | ||
if name == &ZLIB || name == &ZLIB_LEGACY { | ||
Compression::Zlib | ||
} else { | ||
Compression::None | ||
|
@@ -56,7 +91,7 @@ impl Compression { | |
|
||
#[cfg(not(feature = "flate2"))] | ||
impl Compression { | ||
pub fn from_string(_: &str) -> Self { | ||
pub fn new(_name: &Name) -> Self { | ||
Compression::None | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ mod ecdh_nistp; | |
mod none; | ||
use std::cell::RefCell; | ||
use std::collections::HashMap; | ||
use std::convert::TryFrom; | ||
use std::fmt::Debug; | ||
|
||
use curve25519::Curve25519KexType; | ||
|
@@ -87,6 +88,13 @@ impl AsRef<str> for Name { | |
} | ||
} | ||
|
||
impl TryFrom<&str> for Name { | ||
type Error = (); | ||
fn try_from(s: &str) -> Result<Name, ()> { | ||
KEXES.keys().find(|x| x.0 == s).map(|x| **x).ok_or(()) | ||
} | ||
} | ||
|
||
/// `curve25519-sha256` | ||
pub const CURVE25519: Name = Name("curve25519-sha256"); | ||
/// `[email protected]` | ||
|
@@ -126,6 +134,19 @@ const _ECDH_SHA2_NISTP384: EcdhNistP384KexType = EcdhNistP384KexType {}; | |
const _ECDH_SHA2_NISTP521: EcdhNistP521KexType = EcdhNistP521KexType {}; | ||
const _NONE: none::NoneKexType = none::NoneKexType {}; | ||
|
||
pub const ALL_KEX_ALGORITHMS: &[&Name] = &[ | ||
&CURVE25519, | ||
&CURVE25519_PRE_RFC_8731, | ||
&DH_G1_SHA1, | ||
&DH_G14_SHA1, | ||
&DH_G14_SHA256, | ||
&DH_G16_SHA512, | ||
&ECDH_SHA2_NISTP256, | ||
&ECDH_SHA2_NISTP384, | ||
&ECDH_SHA2_NISTP521, | ||
&NONE, | ||
]; | ||
|
||
pub(crate) static KEXES: Lazy<HashMap<&'static Name, &(dyn KexType + Send + Sync)>> = | ||
Lazy::new(|| { | ||
let mut h: HashMap<&'static Name, &(dyn KexType + Send + Sync)> = HashMap::new(); | ||
|
@@ -139,6 +160,7 @@ pub(crate) static KEXES: Lazy<HashMap<&'static Name, &(dyn KexType + Send + Sync | |
h.insert(&ECDH_SHA2_NISTP384, &_ECDH_SHA2_NISTP384); | ||
h.insert(&ECDH_SHA2_NISTP521, &_ECDH_SHA2_NISTP521); | ||
h.insert(&NONE, &_NONE); | ||
assert_eq!(ALL_KEX_ALGORITHMS.len(), h.len()); | ||
h | ||
}); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.