1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
use std::ops::{Deref, DerefMut};
use std::sync::atomic::AtomicU32;
use std::sync::atomic::Ordering::Relaxed;
use crate::constant::{ssh_msg_code};
use crate::error::{SshError, SshErrorKind, SshResult};
use crate::data::Data;
use crate::slog::log;
use crate::channel_exec::ChannelExec;
use crate::channel_scp::ChannelScp;
use crate::channel_shell::ChannelShell;
use crate::kex::{Kex, processing_server_algorithm};
use crate::{client, config};
use crate::window_size::WindowSize;
pub(crate) static CLIENT_CHANNEL_NO: AtomicU32 = AtomicU32::new(0);
pub fn current_client_channel_no() -> u32 {
let client_channel_no = CLIENT_CHANNEL_NO.load(Relaxed);
CLIENT_CHANNEL_NO.fetch_add(1, Relaxed);
client_channel_no
}
pub struct Channel {
pub(crate) kex: Kex,
pub(crate) remote_close: bool,
pub(crate) local_close: bool,
pub(crate) window_size: WindowSize
}
impl Deref for Channel {
type Target = WindowSize;
fn deref(&self) -> &Self::Target {
&self.window_size
}
}
impl DerefMut for Channel {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.window_size
}
}
impl Channel {
pub(crate) fn other(&mut self, message_code: u8, mut result: Data) -> SshResult<()> {
match message_code {
ssh_msg_code::SSH_MSG_GLOBAL_REQUEST => {
let mut data = Data::new();
data.put_u8(ssh_msg_code::SSH_MSG_REQUEST_FAILURE);
let client = client::default()?;
client.write(data)?;
}
ssh_msg_code::SSH_MSG_KEXINIT => {
let vec = result.to_vec();
let mut data = Data::from(vec![message_code]);
data.extend(vec);
self.kex.h.set_i_s(data.as_slice());
processing_server_algorithm(data)?;
self.kex.send_algorithm()?;
let config = config::config();
let (dh, sign) = config.algorithm.matching_algorithm()?;
self.kex.dh = dh;
self.kex.signature = sign;
self.kex.h.set_v_c(config.version.client_version.as_str());
self.kex.h.set_v_s(config.version.server_version.as_str());
self.kex.send_qc()?;
}
ssh_msg_code::SSH_MSG_KEX_ECDH_REPLY => {
let sig = self
.kex
.generate_session_id_and_get_signature(result)?;
let r = self
.kex
.signature
.verify_signature(&self.kex.h.k_s, &self.kex.session_id, &sig)?;
log::info!("signature Verification Result => {}", r);
if !r {
return Err(SshError::from(SshErrorKind::SignatureError))
}
}
ssh_msg_code::SSH_MSG_NEWKEYS => self.kex.new_keys()?,
ssh_msg_code::SSH_MSG_CHANNEL_WINDOW_ADJUST => {
result.get_u32();
let rws = result.get_u32();
self.window_size.add_remote_window_size(rws);
self.window_size.add_remote_max_window_size(rws);
},
ssh_msg_code::SSH_MSG_CHANNEL_EOF => {}
ssh_msg_code::SSH_MSG_CHANNEL_REQUEST => {}
ssh_msg_code::SSH_MSG_CHANNEL_SUCCESS => {}
ssh_msg_code::SSH_MSG_CHANNEL_FAILURE => return Err(SshError::from(SshErrorKind::ChannelFailureError)),
ssh_msg_code::SSH_MSG_CHANNEL_CLOSE => {
let cc = result.get_u32();
if cc == self.client_channel {
self.remote_close = true;
self.close()?;
}
}
_ => {}
}
Ok(())
}
pub fn open_shell(self) -> SshResult<ChannelShell> {
log::info!("shell opened.");
return ChannelShell::open(self)
}
pub fn open_exec(self) -> SshResult<ChannelExec> {
log::info!("exec opened.");
return Ok(ChannelExec::open(self))
}
pub fn open_scp(self) -> SshResult<ChannelScp> {
log::info!("scp opened.");
return Ok(ChannelScp::open(self))
}
pub fn close(&mut self) -> SshResult<()> {
log::info!("channel close.");
self.send_close()?;
self.receive_close()
}
fn send_close(&mut self) -> SshResult<()> {
if self.local_close { return Ok(()); }
let mut data = Data::new();
data.put_u8(ssh_msg_code::SSH_MSG_CHANNEL_CLOSE)
.put_u32(self.server_channel);
let client = client::default()?;
client.write(data)?;
self.local_close = true;
Ok(())
}
fn receive_close(&mut self) -> SshResult<()> {
if self.remote_close { return Ok(()); }
loop {
let client = client::default()?;
let results = client.read()?;
for mut result in results {
if result.is_empty() { continue }
let message_code = result.get_u8();
match message_code {
ssh_msg_code::SSH_MSG_CHANNEL_CLOSE => {
let cc = result.get_u32();
if cc == self.client_channel {
self.remote_close = true;
return Ok(())
}
}
_ => self.other(message_code, result)?
}
}
}
}
}