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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
use crate::{message, strings, size, util};
use crate::channel_exec::ChannelExec;
use crate::channel_shell::ChannelShell;
use crate::error::{SshError, SshErrorKind, SshResult};
use crate::kex::{Kex, processing_server_algorithm};
use crate::packet::{Data, Packet};
pub struct Channel {
pub(crate) kex: Kex,
pub(crate) server_channel: u32,
pub(crate) client_channel: u32,
pub(crate) remote_close: bool,
pub(crate) local_close: bool,
}
impl Channel {
pub fn other(&mut self, message_code: u8, result: Vec<u8>) -> SshResult<()> {
match message_code {
message::SSH_MSG_GLOBAL_REQUEST => {
let mut data = Data::new();
data.put_u8(message::SSH_MSG_REQUEST_FAILURE);
let mut packet = Packet::from(data);
packet.build();
let mut client = util::client()?;
client.write(packet.as_slice())?;
util::unlock(client)
}
message::SSH_MSG_KEXINIT => {
let data = Packet::processing_data(result);
self.kex.h.set_i_s(data.as_slice());
processing_server_algorithm(data)?;
self.kex.send_algorithm()?;
let config = util::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());
util::unlock(config);
self.kex.send_qc()?;
}
message::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))
}
self.kex.new_keys()?;
}
message::SSH_MSG_CHANNEL_WINDOW_ADJUST => {}
message::SSH_MSG_CHANNEL_EOF => {}
message::SSH_MSG_CHANNEL_REQUEST => {}
message::SSH_MSG_CHANNEL_SUCCESS => {}
message::SSH_MSG_CHANNEL_FAILURE => return Err(SshError::from(SshErrorKind::ChannelFailureError)),
message::SSH_MSG_CHANNEL_CLOSE => {
let mut data = Packet::processing_data(result);
data.get_u8();
let cc = data.get_u32();
if cc == self.client_channel {
self.remote_close = true;
self.close()?;
}
}
_ => {}
}
Ok(())
}
pub fn window_adjust(&mut self) -> SshResult<()> {
let mut client = util::client()?;
if client.sender_window_size >= (size::LOCAL_WINDOW_SIZE / 2) {
let mut data = Data::new();
data.put_u8(message::SSH_MSG_CHANNEL_WINDOW_ADJUST)
.put_u32(self.server_channel)
.put_u32(size::LOCAL_WINDOW_SIZE - client.sender_window_size);
let mut packet = Packet::from(data);
packet.build();
client.write(packet.as_slice())?;
client.sender_window_size = 0;
}
Ok(())
}
pub fn open_shell(mut self) -> SshResult<ChannelShell> {
loop {
let mut client = util::client()?;
let results = client.read()?;
util::unlock(client);
for result in results {
if result.is_empty() { continue }
let message_code = result[5];
match message_code {
message::SSH_MSG_CHANNEL_OPEN_CONFIRMATION => {
let mut data = Packet::processing_data(result);
data.get_u8();
data.get_u32();
self.server_channel = data.get_u32();
self.request_pty()?;
self.get_shell()?;
}
message::SSH_MSG_CHANNEL_SUCCESS => {
return Ok(ChannelShell(self))
}
_ => self.other(message_code, result)?
}
}
}
}
pub fn open_exec(mut self) -> SshResult<ChannelExec> {
loop {
let mut client = util::client()?;
let results = client.read()?;
util::unlock(client);
for result in results {
if result.is_empty() { continue }
let message_code = result[5];
match message_code {
message::SSH_MSG_CHANNEL_OPEN_CONFIRMATION => {
let mut data = Packet::processing_data(result);
data.get_u8();
data.get_u32();
self.server_channel = data.get_u32();
return Ok(ChannelExec(self))
}
_ => self.other(message_code, result)?
}
}
}
}
pub fn close(&mut self) -> SshResult<()> {
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(message::SSH_MSG_CHANNEL_CLOSE)
.put_u32(self.server_channel);
let mut packet = Packet::from(data);
packet.build();
let mut client = util::client()?;
client.write(packet.as_slice())?;
self.local_close = true;
Ok(())
}
fn receive_close(&mut self) -> SshResult<()> {
if self.remote_close { return Ok(()); }
loop {
let mut client = util::client()?;
let results = client.read()?;
util::unlock(client);
for result in results {
if result.is_empty() { continue }
let message_code = result[5];
match message_code {
message::SSH_MSG_CHANNEL_CLOSE => {
let mut data = Packet::processing_data(result);
data.get_u8();
let cc = data.get_u32();
if cc == self.client_channel {
self.remote_close = true;
return Ok(())
}
}
_ => self.other(message_code, result)?
}
}
}
}
fn get_shell(&self) -> SshResult<()> {
let mut data = Data::new();
data.put_u8(message::SSH_MSG_CHANNEL_REQUEST)
.put_u32(self.server_channel)
.put_str(strings::SHELL)
.put_u8(true as u8);
let mut packet = Packet::from(data);
packet.build();
let mut client = util::client()?;
client.write(packet.as_slice())
}
fn request_pty(&self) -> SshResult<()> {
let mut data = Data::new();
data.put_u8(message::SSH_MSG_CHANNEL_REQUEST)
.put_u32(0)
.put_str(strings::PTY_REQ)
.put_u8(false as u8)
.put_str(strings::XTERM_VAR)
.put_u32(80)
.put_u32(24)
.put_u32(640)
.put_u32(480);
let model = [
128,
0, 1, 0xc2, 0,
129,
0, 1, 0xc2, 0,
0_u8,
];
data.put_bytes(&model);
let mut packet = Packet::from(data);
packet.build();
let mut client = util::client()?;
client.write(packet.as_slice())
}
}