-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #74 from HsuJv/v0_4_1
add zlib, [email protected] support
- Loading branch information
Showing
17 changed files
with
233 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "ssh-rs" | ||
version = "0.4.0" | ||
version = "0.4.1" | ||
edition = "2021" | ||
authors = [ | ||
"Gao Xiang Kang <[email protected]>", | ||
|
@@ -24,6 +24,7 @@ deprecated-rsa-sha1 = ["dep:sha1"] | |
deprecated-dh-group1-sha1 = ["dep:sha1"] | ||
deprecated-aes-cbc = ["dep:cbc", "dep:cipher"] | ||
deprecated-des-cbc = ["dep:cbc", "dep:cipher", "dep:des"] | ||
deprecated-zlib = [] | ||
scp = ["dep:filetime"] | ||
|
||
[lib] | ||
|
@@ -57,6 +58,9 @@ ssh-key = { version = "0.6", features = ["rsa", "ed25519", "alloc"]} | |
signature = "2.1" | ||
ring = "0.16" | ||
|
||
## compression | ||
flate2 = "^1.0" | ||
|
||
## utils | ||
filetime = { version = "0.2", optional = true } | ||
|
||
|
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 |
---|---|---|
|
@@ -198,6 +198,8 @@ match ssh::create_session() | |
### 5. Compression algorithms | ||
|
||
* `none` | ||
* `[email protected]` | ||
* `zlib` (behind feature "zlib") | ||
|
||
--- | ||
|
||
|
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,6 @@ | ||
v0.4.1 (2023-09-20) | ||
1. Add zlib, [email protected] support | ||
|
||
v0.4.0 (2023-09-16) | ||
1. remove chinese comments | ||
2. add RFC links | ||
|
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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
use super::Compress; | ||
use crate::SshResult; | ||
|
||
mod zlib; | ||
/// <https://www.rfc-editor.org/rfc/rfc4253#section-6.2> | ||
pub(crate) trait Compression: Send + Sync { | ||
fn new() -> Self | ||
where | ||
Self: Sized; | ||
// The "[email protected]" method operates identically to the "zlib" | ||
// method described in [RFC4252] except that packet compression does not | ||
// start until the server sends a SSH_MSG_USERAUTH_SUCCESS packet | ||
// so | ||
// fn start(); | ||
fn compress(&mut self, buf: &[u8]) -> SshResult<Vec<u8>>; | ||
fn decompress(&mut self, buf: &[u8]) -> SshResult<Vec<u8>>; | ||
} | ||
|
||
pub(crate) fn from(comp: &Compress) -> Box<dyn Compression> { | ||
match comp { | ||
Compress::None => Box::new(CompressNone::new()), | ||
#[cfg(feature = "deprecated-zlib")] | ||
Compress::Zlib => Box::new(zlib::CompressZlib::new()), | ||
Compress::ZlibOpenSsh => Box::new(zlib::CompressZlib::new()), | ||
} | ||
} | ||
|
||
#[derive(Default)] | ||
pub(crate) struct CompressNone {} | ||
|
||
impl Compression for CompressNone { | ||
fn new() -> Self { | ||
Self {} | ||
} | ||
|
||
fn compress(&mut self, buf: &[u8]) -> SshResult<Vec<u8>> { | ||
Ok(buf.to_vec()) | ||
} | ||
|
||
fn decompress(&mut self, buf: &[u8]) -> SshResult<Vec<u8>> { | ||
Ok(buf.to_vec()) | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,118 @@ | ||
use flate2; | ||
|
||
use crate::SshError; | ||
|
||
use super::Compression; | ||
|
||
/// The "zlib" compression is described in [RFC1950] and in [RFC1951]. | ||
/// The compression context is initialized after each key exchange, and | ||
/// is passed from one packet to the next, with only a partial flush | ||
/// being performed at the end of each packet. A partial flush means | ||
/// that the current compressed block is ended and all data will be | ||
/// output. If the current block is not a stored block, one or more | ||
/// empty blocks are added after the current block to ensure that there | ||
/// are at least 8 bits, counting from the start of the end-of-block code | ||
/// of the current block to the end of the packet payload. | ||
/// | ||
/// <https://www.openssh.com/txt/draft-miller-secsh-compression-delayed-00.txt> | ||
/// The "[email protected]" method operates identically to the "zlib" | ||
/// method described in [RFC4252] except that packet compression does not | ||
/// start until the server sends a SSH_MSG_USERAUTH_SUCCESS packet, | ||
/// replacing the "zlib" method's start of compression when the server | ||
/// sends SSH_MSG_NEWKEYS. | ||
pub(super) struct CompressZlib { | ||
decompressor: flate2::Decompress, | ||
compressor: flate2::Compress, | ||
} | ||
|
||
impl Compression for CompressZlib { | ||
fn new() -> Self | ||
where | ||
Self: Sized, | ||
{ | ||
Self { | ||
decompressor: flate2::Decompress::new(true), | ||
compressor: flate2::Compress::new(flate2::Compression::fast(), true), | ||
} | ||
} | ||
|
||
fn decompress(&mut self, buf: &[u8]) -> crate::SshResult<Vec<u8>> { | ||
let mut buf_in = buf; | ||
let mut buf_once = [0; 4096]; | ||
let mut buf_out = vec![]; | ||
loop { | ||
let in_before = self.decompressor.total_in(); | ||
let out_before = self.decompressor.total_out(); | ||
|
||
let result = | ||
self.decompressor | ||
.decompress(buf_in, &mut buf_once, flate2::FlushDecompress::Sync); | ||
|
||
let consumed = (self.decompressor.total_in() - in_before) as usize; | ||
let produced = (self.decompressor.total_out() - out_before) as usize; | ||
|
||
match result { | ||
Ok(flate2::Status::Ok) => { | ||
buf_in = &buf_in[consumed..]; | ||
buf_out.extend(&buf_once[..produced]); | ||
} | ||
Ok(flate2::Status::StreamEnd) => { | ||
return Err(SshError::CompressionError( | ||
"Stream ends during the decompress".to_owned(), | ||
)); | ||
} | ||
Ok(flate2::Status::BufError) => { | ||
break; | ||
} | ||
Err(e) => return Err(SshError::CompressionError(e.to_string())), | ||
} | ||
} | ||
|
||
Ok(buf_out) | ||
} | ||
|
||
fn compress(&mut self, buf: &[u8]) -> crate::SshResult<Vec<u8>> { | ||
let mut buf_in = buf; | ||
let mut buf_once = [0; 4096]; | ||
let mut buf_out = vec![]; | ||
loop { | ||
let in_before = self.compressor.total_in(); | ||
let out_before = self.compressor.total_out(); | ||
|
||
let result = | ||
self.compressor | ||
.compress(buf_in, &mut buf_once, flate2::FlushCompress::Partial); | ||
|
||
let consumed = (self.compressor.total_in() - in_before) as usize; | ||
let produced = (self.compressor.total_out() - out_before) as usize; | ||
|
||
// tracing::info!(consumed); | ||
// tracing::info!(produced); | ||
|
||
// means an empty compress | ||
// 2 bytes ZLIB header at the start of the stream | ||
// 4 bytes CRC checksum at the end of the stream | ||
if produced == 6 { | ||
break; | ||
} | ||
|
||
match result { | ||
Ok(flate2::Status::Ok) => { | ||
buf_in = &buf_in[consumed..]; | ||
buf_out.extend(&buf_once[..produced]); | ||
} | ||
Ok(flate2::Status::StreamEnd) => { | ||
return Err(SshError::CompressionError( | ||
"Stream ends during the compress".to_owned(), | ||
)); | ||
} | ||
Ok(flate2::Status::BufError) => { | ||
break; | ||
} | ||
Err(e) => return Err(SshError::CompressionError(e.to_string())), | ||
} | ||
} | ||
|
||
Ok(buf_out) | ||
} | ||
} |
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,4 @@ | ||
pub(crate) mod compression; | ||
pub(crate) mod encryption; | ||
pub(crate) mod hash; | ||
pub(crate) mod key_exchange; | ||
|
@@ -79,6 +80,11 @@ pub enum Mac { | |
pub enum Compress { | ||
#[strum(serialize = "none")] | ||
None, | ||
#[cfg(feature = "deprecated-zlib")] | ||
#[strum(serialize = "zlib")] | ||
Zlib, | ||
#[strum(serialize = "[email protected]")] | ||
ZlibOpenSsh, | ||
} | ||
|
||
#[derive(Default)] | ||
|
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
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
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
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 +1 @@ | ||
0.4.0 | ||
0.4.1 |