Skip to main content

justssh_wire/
messages.rs

1//! Transport-layer messages: RFC 4253 and the ECDH exchange of RFC 5656 §4.
2
3use crate::codec::{DecodeError, Reader, Writer};
4use crate::name::Name;
5use crate::number;
6
7/// `SSH_MSG_KEXINIT` (RFC 4253 §7.1).
8#[non_exhaustive]
9#[derive(Debug, Clone, Default, PartialEq, Eq)]
10pub struct KexInit {
11    /// 16 random bytes.
12    pub cookie: [u8; 16],
13    pub kex_algorithms: Vec<String>,
14    pub server_host_key_algorithms: Vec<String>,
15    pub encryption_algorithms_client_to_server: Vec<String>,
16    pub encryption_algorithms_server_to_client: Vec<String>,
17    pub mac_algorithms_client_to_server: Vec<String>,
18    pub mac_algorithms_server_to_client: Vec<String>,
19    pub compression_algorithms_client_to_server: Vec<String>,
20    pub compression_algorithms_server_to_client: Vec<String>,
21    pub languages_client_to_server: Vec<String>,
22    pub languages_server_to_client: Vec<String>,
23    pub first_kex_packet_follows: bool,
24    pub reserved: u32,
25}
26
27/// `SSH_MSG_KEX_ECDH_INIT`: the client's ephemeral public key `Q_C`.
28#[non_exhaustive]
29#[derive(Debug, Clone, Default, PartialEq, Eq)]
30pub struct KexEcdhInit {
31    pub q_c: Vec<u8>,
32}
33
34/// `SSH_MSG_KEX_ECDH_REPLY`: the server's host key `K_S`, ephemeral public key `Q_S` and
35/// the signature over the exchange hash.
36#[non_exhaustive]
37#[derive(Debug, Clone, Default, PartialEq, Eq)]
38pub struct KexEcdhReply {
39    pub host_key: Vec<u8>,
40    pub q_s: Vec<u8>,
41    pub signature: Vec<u8>,
42}
43
44/// `SSH_MSG_NEWKEYS`.
45#[non_exhaustive]
46#[derive(Debug, Clone, Default, PartialEq, Eq)]
47pub struct NewKeys;
48
49/// `SSH_MSG_SERVICE_REQUEST`.
50#[non_exhaustive]
51#[derive(Debug, Clone, Default, PartialEq, Eq)]
52pub struct ServiceRequest {
53    pub service: String,
54}
55
56/// `SSH_MSG_SERVICE_ACCEPT`.
57#[non_exhaustive]
58#[derive(Debug, Clone, Default, PartialEq, Eq)]
59pub struct ServiceAccept {
60    pub service: String,
61}
62
63/// `SSH_MSG_DISCONNECT`.
64#[non_exhaustive]
65#[derive(Debug, Clone, Default, PartialEq, Eq)]
66pub struct Disconnect {
67    pub reason: u32,
68    pub description: String,
69    pub language: String,
70}
71
72/// `SSH_MSG_IGNORE`.
73#[non_exhaustive]
74#[derive(Debug, Clone, Default, PartialEq, Eq)]
75pub struct Ignore {
76    pub data: Vec<u8>,
77}
78
79/// `SSH_MSG_DEBUG`.
80#[non_exhaustive]
81#[derive(Debug, Clone, Default, PartialEq, Eq)]
82pub struct Debug {
83    pub always_display: bool,
84    pub message: String,
85    pub language: String,
86}
87
88/// `SSH_MSG_UNIMPLEMENTED`: the sequence number of the packet that was not understood.
89#[non_exhaustive]
90#[derive(Debug, Clone, Default, PartialEq, Eq)]
91pub struct Unimplemented {
92    pub sequence_number: u32,
93}
94
95impl KexEcdhInit {
96    pub fn new(q_c: Vec<u8>) -> Self {
97        KexEcdhInit { q_c }
98    }
99}
100
101impl KexEcdhReply {
102    pub fn new(host_key: Vec<u8>, q_s: Vec<u8>, signature: Vec<u8>) -> Self {
103        KexEcdhReply {
104            host_key,
105            q_s,
106            signature,
107        }
108    }
109}
110
111impl ServiceRequest {
112    pub fn new(service: impl Into<String>) -> Self {
113        ServiceRequest {
114            service: service.into(),
115        }
116    }
117}
118
119impl ServiceAccept {
120    pub fn new(service: impl Into<String>) -> Self {
121        ServiceAccept {
122            service: service.into(),
123        }
124    }
125}
126
127impl Disconnect {
128    pub fn new(reason: u32, description: impl Into<String>, language: impl Into<String>) -> Self {
129        Disconnect {
130            reason,
131            description: description.into(),
132            language: language.into(),
133        }
134    }
135}
136
137impl Ignore {
138    pub fn new(data: Vec<u8>) -> Self {
139        Ignore { data }
140    }
141}
142
143impl Debug {
144    pub fn new(
145        always_display: bool,
146        message: impl Into<String>,
147        language: impl Into<String>,
148    ) -> Self {
149        Debug {
150            always_display,
151            message: message.into(),
152            language: language.into(),
153        }
154    }
155}
156
157impl Unimplemented {
158    pub fn new(sequence_number: u32) -> Self {
159        Unimplemented { sequence_number }
160    }
161}
162
163/// A transport message, or the number of one this crate does not decode.
164#[non_exhaustive]
165#[derive(Debug, Clone, PartialEq, Eq)]
166pub enum Message {
167    KexInit(KexInit),
168    KexEcdhInit(KexEcdhInit),
169    KexEcdhReply(KexEcdhReply),
170    NewKeys(NewKeys),
171    ServiceRequest(ServiceRequest),
172    ServiceAccept(ServiceAccept),
173    Disconnect(Disconnect),
174    Ignore(Ignore),
175    Debug(Debug),
176    Unimplemented(Unimplemented),
177    ExtInfo(ExtInfo),
178    UserauthRequest(UserauthRequest),
179    UserauthFailure(UserauthFailure),
180    UserauthSuccess(UserauthSuccess),
181    UserauthBanner(UserauthBanner),
182    Ping(Ping),
183    Pong(Pong),
184    GlobalRequest(GlobalRequest),
185    RequestSuccess(RequestSuccess),
186    RequestFailure(RequestFailure),
187    ChannelOpen(ChannelOpen),
188    ChannelOpenConfirmation(ChannelOpenConfirmation),
189    ChannelOpenFailure(ChannelOpenFailure),
190    ChannelWindowAdjust(ChannelWindowAdjust),
191    ChannelData(ChannelData),
192    ChannelExtendedData(ChannelExtendedData),
193    ChannelEof(ChannelEof),
194    ChannelClose(ChannelClose),
195    ChannelRequest(ChannelRequest),
196    ChannelSuccess(ChannelSuccess),
197    ChannelFailure(ChannelFailure),
198    /// A message number this crate has no decoder for; its body is not read.
199    Unknown(u8),
200}
201
202impl Message {
203    /// The payload: message number followed by the fields.
204    pub fn encode(&self) -> Vec<u8> {
205        let mut out = Vec::new();
206        self.encode_into(&mut out);
207        out
208    }
209
210    /// Appends the payload to `out`.
211    pub fn encode_into(&self, out: &mut Vec<u8>) {
212        let mut w = Writer::appending(core::mem::take(out));
213        match self {
214            Message::KexInit(m) => {
215                w.u8(number::KEXINIT)
216                    .bytes(&m.cookie)
217                    .name_list(&m.kex_algorithms)
218                    .name_list(&m.server_host_key_algorithms)
219                    .name_list(&m.encryption_algorithms_client_to_server)
220                    .name_list(&m.encryption_algorithms_server_to_client)
221                    .name_list(&m.mac_algorithms_client_to_server)
222                    .name_list(&m.mac_algorithms_server_to_client)
223                    .name_list(&m.compression_algorithms_client_to_server)
224                    .name_list(&m.compression_algorithms_server_to_client)
225                    .name_list(&m.languages_client_to_server)
226                    .name_list(&m.languages_server_to_client)
227                    .bool(m.first_kex_packet_follows)
228                    .u32(m.reserved);
229            }
230            Message::KexEcdhInit(m) => {
231                w.u8(number::KEX_ECDH_INIT).string(&m.q_c);
232            }
233            Message::KexEcdhReply(m) => {
234                w.u8(number::KEX_ECDH_REPLY)
235                    .string(&m.host_key)
236                    .string(&m.q_s)
237                    .string(&m.signature);
238            }
239            Message::NewKeys(_) => {
240                w.u8(number::NEWKEYS);
241            }
242            Message::ServiceRequest(m) => {
243                w.u8(number::SERVICE_REQUEST).string(m.service.as_bytes());
244            }
245            Message::ServiceAccept(m) => {
246                w.u8(number::SERVICE_ACCEPT).string(m.service.as_bytes());
247            }
248            Message::Disconnect(m) => {
249                w.u8(number::DISCONNECT)
250                    .u32(m.reason)
251                    .string(m.description.as_bytes())
252                    .string(m.language.as_bytes());
253            }
254            Message::Ignore(m) => {
255                w.u8(number::IGNORE).string(&m.data);
256            }
257            Message::Debug(m) => {
258                w.u8(number::DEBUG)
259                    .bool(m.always_display)
260                    .string(m.message.as_bytes())
261                    .string(m.language.as_bytes());
262            }
263            Message::Unimplemented(m) => {
264                w.u8(number::UNIMPLEMENTED).u32(m.sequence_number);
265            }
266            Message::ExtInfo(m) => {
267                let count =
268                    u32::try_from(m.extensions.len()).expect("more than u32::MAX extensions");
269                w.u8(number::EXT_INFO).u32(count);
270                for (name, value) in &m.extensions {
271                    w.string(name.as_bytes()).string(value);
272                }
273            }
274            Message::UserauthRequest(m) => {
275                w.u8(number::USERAUTH_REQUEST)
276                    .string(&m.user)
277                    .string(m.service.as_bytes())
278                    .string(m.method.as_bytes())
279                    .bytes(&m.method_data);
280            }
281            Message::UserauthFailure(m) => {
282                w.u8(number::USERAUTH_FAILURE)
283                    .name_list(&m.methods)
284                    .bool(m.partial_success);
285            }
286            Message::UserauthSuccess(_) => {
287                w.u8(number::USERAUTH_SUCCESS);
288            }
289            Message::UserauthBanner(m) => {
290                w.u8(number::USERAUTH_BANNER)
291                    .string(&m.message)
292                    .string(&m.language);
293            }
294            Message::Ping(m) => {
295                w.u8(number::PING).string(&m.data);
296            }
297            Message::Pong(m) => {
298                w.u8(number::PONG).string(&m.data);
299            }
300            Message::GlobalRequest(m) => {
301                w.u8(number::GLOBAL_REQUEST)
302                    .string(m.kind.as_bytes())
303                    .bool(m.want_reply)
304                    .bytes(&m.data);
305            }
306            Message::RequestSuccess(m) => {
307                w.u8(number::REQUEST_SUCCESS).bytes(&m.data);
308            }
309            Message::RequestFailure(m) => {
310                w.u8(number::REQUEST_FAILURE).bytes(&m.data);
311            }
312            Message::ChannelOpen(m) => {
313                w.u8(number::CHANNEL_OPEN)
314                    .string(m.kind.as_bytes())
315                    .u32(m.sender)
316                    .u32(m.window)
317                    .u32(m.max_packet)
318                    .bytes(&m.data);
319            }
320            Message::ChannelOpenConfirmation(m) => {
321                w.u8(number::CHANNEL_OPEN_CONFIRMATION)
322                    .u32(m.recipient)
323                    .u32(m.sender)
324                    .u32(m.window)
325                    .u32(m.max_packet)
326                    .bytes(&m.data);
327            }
328            Message::ChannelOpenFailure(m) => {
329                w.u8(number::CHANNEL_OPEN_FAILURE)
330                    .u32(m.recipient)
331                    .u32(m.reason)
332                    .string(&m.description)
333                    .string(&m.language);
334            }
335            Message::ChannelWindowAdjust(m) => {
336                w.u8(number::CHANNEL_WINDOW_ADJUST)
337                    .u32(m.recipient)
338                    .u32(m.bytes);
339            }
340            Message::ChannelData(m) => {
341                w.u8(number::CHANNEL_DATA).u32(m.recipient).string(&m.data);
342            }
343            Message::ChannelExtendedData(m) => {
344                w.u8(number::CHANNEL_EXTENDED_DATA)
345                    .u32(m.recipient)
346                    .u32(m.code)
347                    .string(&m.data);
348            }
349            Message::ChannelEof(m) => {
350                w.u8(number::CHANNEL_EOF).u32(m.recipient);
351            }
352            Message::ChannelClose(m) => {
353                w.u8(number::CHANNEL_CLOSE).u32(m.recipient);
354            }
355            Message::ChannelRequest(m) => {
356                w.u8(number::CHANNEL_REQUEST)
357                    .u32(m.recipient)
358                    .string(m.kind.as_bytes())
359                    .bool(m.want_reply)
360                    .bytes(&m.data);
361            }
362            Message::ChannelSuccess(m) => {
363                w.u8(number::CHANNEL_SUCCESS).u32(m.recipient);
364            }
365            Message::ChannelFailure(m) => {
366                w.u8(number::CHANNEL_FAILURE).u32(m.recipient);
367            }
368            Message::Unknown(n) => {
369                w.u8(*n);
370            }
371        }
372        *out = w.into_bytes();
373    }
374
375    /// Decodes a whole payload. Every field must be present and no byte may be left over,
376    /// except for [`Message::Unknown`], whose body is not read.
377    pub fn decode(payload: &[u8]) -> Result<Message, DecodeError> {
378        let (&n, body) = payload.split_first().ok_or(DecodeError::Empty)?;
379        let mut r = Reader::new(body);
380        let m = match n {
381            number::KEXINIT => {
382                let mut cookie = [0u8; 16];
383                cookie.copy_from_slice(r.bytes(16)?);
384                Message::KexInit(KexInit {
385                    cookie,
386                    kex_algorithms: r.name_list()?,
387                    server_host_key_algorithms: r.name_list()?,
388                    encryption_algorithms_client_to_server: r.name_list()?,
389                    encryption_algorithms_server_to_client: r.name_list()?,
390                    mac_algorithms_client_to_server: r.name_list()?,
391                    mac_algorithms_server_to_client: r.name_list()?,
392                    compression_algorithms_client_to_server: r.name_list()?,
393                    compression_algorithms_server_to_client: r.name_list()?,
394                    languages_client_to_server: r.name_list()?,
395                    languages_server_to_client: r.name_list()?,
396                    first_kex_packet_follows: r.bool()?,
397                    reserved: r.u32()?,
398                })
399            }
400            number::KEX_ECDH_INIT => Message::KexEcdhInit(KexEcdhInit::new(r.string()?.to_vec())),
401            number::KEX_ECDH_REPLY => Message::KexEcdhReply(KexEcdhReply::new(
402                r.string()?.to_vec(),
403                r.string()?.to_vec(),
404                r.string()?.to_vec(),
405            )),
406            number::NEWKEYS => Message::NewKeys(NewKeys),
407            number::SERVICE_REQUEST => Message::ServiceRequest(ServiceRequest::new(r.utf8()?)),
408            number::SERVICE_ACCEPT => Message::ServiceAccept(ServiceAccept::new(r.utf8()?)),
409            number::DISCONNECT => {
410                let reason = r.u32()?;
411                let description = String::from_utf8_lossy(r.string()?).into_owned();
412                let language = String::from_utf8_lossy(r.string()?).into_owned();
413                Message::Disconnect(Disconnect {
414                    reason,
415                    description,
416                    language,
417                })
418            }
419            number::IGNORE => Message::Ignore(Ignore::new(r.string()?.to_vec())),
420            number::DEBUG => {
421                let always_display = r.bool()?;
422                let message = String::from_utf8_lossy(r.string()?).into_owned();
423                let language = String::from_utf8_lossy(r.string()?).into_owned();
424                Message::Debug(Debug {
425                    always_display,
426                    message,
427                    language,
428                })
429            }
430            number::UNIMPLEMENTED => Message::Unimplemented(Unimplemented::new(r.u32()?)),
431            number::EXT_INFO => {
432                let count = r.u32()?;
433                let mut extensions = Vec::new();
434                for _ in 0..count {
435                    let name = r.utf8()?.to_owned();
436                    extensions.push((name, r.string()?.to_vec()));
437                }
438                Message::ExtInfo(ExtInfo { extensions })
439            }
440            number::USERAUTH_REQUEST => {
441                let user = r.string()?.to_vec();
442                let service = r.utf8()?.to_owned();
443                let method = r.utf8()?.to_owned();
444                let method_data = r.remaining().to_vec();
445                return Ok(Message::UserauthRequest(UserauthRequest {
446                    user,
447                    service,
448                    method,
449                    method_data,
450                }));
451            }
452            number::USERAUTH_FAILURE => Message::UserauthFailure(UserauthFailure {
453                methods: r.name_list()?,
454                partial_success: r.bool()?,
455            }),
456            number::USERAUTH_SUCCESS => Message::UserauthSuccess(UserauthSuccess),
457            number::USERAUTH_BANNER => Message::UserauthBanner(UserauthBanner {
458                message: r.string()?.to_vec(),
459                language: r.string()?.to_vec(),
460            }),
461            number::PING => Message::Ping(Ping::new(r.string()?.to_vec())),
462            number::PONG => Message::Pong(Pong::new(r.string()?.to_vec())),
463            number::GLOBAL_REQUEST => {
464                let kind = Name::from(r.string()?);
465                let want_reply = r.bool()?;
466                let data = r.remaining().to_vec();
467                return Ok(Message::GlobalRequest(GlobalRequest::new(
468                    kind, want_reply, data,
469                )));
470            }
471            number::REQUEST_SUCCESS => {
472                return Ok(Message::RequestSuccess(RequestSuccess::new(
473                    r.remaining().to_vec(),
474                )));
475            }
476            number::REQUEST_FAILURE => {
477                return Ok(Message::RequestFailure(RequestFailure::new(
478                    r.remaining().to_vec(),
479                )));
480            }
481            number::CHANNEL_OPEN => {
482                let kind = Name::from(r.string()?);
483                let (sender, window, max_packet) = (r.u32()?, r.u32()?, r.u32()?);
484                let data = r.remaining().to_vec();
485                return Ok(Message::ChannelOpen(ChannelOpen::new(
486                    kind, sender, window, max_packet, data,
487                )));
488            }
489            number::CHANNEL_OPEN_CONFIRMATION => {
490                let (recipient, sender, window, max_packet) =
491                    (r.u32()?, r.u32()?, r.u32()?, r.u32()?);
492                let data = r.remaining().to_vec();
493                return Ok(Message::ChannelOpenConfirmation(
494                    ChannelOpenConfirmation::new(recipient, sender, window, max_packet, data),
495                ));
496            }
497            number::CHANNEL_OPEN_FAILURE => {
498                let (recipient, reason) = (r.u32()?, r.u32()?);
499                let (description, language) = (r.string()?.to_vec(), r.string()?.to_vec());
500                Message::ChannelOpenFailure(ChannelOpenFailure::new(
501                    recipient,
502                    reason,
503                    description,
504                    language,
505                ))
506            }
507            number::CHANNEL_WINDOW_ADJUST => {
508                Message::ChannelWindowAdjust(ChannelWindowAdjust::new(r.u32()?, r.u32()?))
509            }
510            number::CHANNEL_DATA => {
511                Message::ChannelData(ChannelData::new(r.u32()?, r.string()?.to_vec()))
512            }
513            number::CHANNEL_EXTENDED_DATA => Message::ChannelExtendedData(
514                ChannelExtendedData::new(r.u32()?, r.u32()?, r.string()?.to_vec()),
515            ),
516            number::CHANNEL_EOF => Message::ChannelEof(ChannelEof::new(r.u32()?)),
517            number::CHANNEL_CLOSE => Message::ChannelClose(ChannelClose::new(r.u32()?)),
518            number::CHANNEL_REQUEST => {
519                let recipient = r.u32()?;
520                let kind = Name::from(r.string()?);
521                let want_reply = r.bool()?;
522                let data = r.remaining().to_vec();
523                return Ok(Message::ChannelRequest(ChannelRequest::new(
524                    recipient, kind, want_reply, data,
525                )));
526            }
527            number::CHANNEL_SUCCESS => Message::ChannelSuccess(ChannelSuccess::new(r.u32()?)),
528            number::CHANNEL_FAILURE => Message::ChannelFailure(ChannelFailure::new(r.u32()?)),
529            other => return Ok(Message::Unknown(other)),
530        };
531        r.finish()?;
532        Ok(m)
533    }
534}
535
536/// `SSH_MSG_EXT_INFO` (RFC 8308 §2.3): extension names and their values.
537#[non_exhaustive]
538#[derive(Debug, Clone, Default, PartialEq, Eq)]
539pub struct ExtInfo {
540    pub extensions: Vec<(String, Vec<u8>)>,
541}
542
543impl ExtInfo {
544    pub fn new(extensions: Vec<(String, Vec<u8>)>) -> Self {
545        ExtInfo { extensions }
546    }
547}
548
549/// `SSH_MSG_USERAUTH_REQUEST` (RFC 4252 §5). `method_data` is everything after the method
550/// name, left undecoded; it can hold a password, so `Debug` shows only its length.
551#[non_exhaustive]
552#[derive(Clone, Default, PartialEq, Eq)]
553pub struct UserauthRequest {
554    /// The user name as sent; RFC 4252 calls for UTF-8, but nothing on the wire enforces it.
555    pub user: Vec<u8>,
556    pub service: String,
557    pub method: String,
558    pub method_data: Vec<u8>,
559}
560
561impl core::fmt::Debug for UserauthRequest {
562    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
563        f.debug_struct("UserauthRequest")
564            .field("user", &String::from_utf8_lossy(&self.user))
565            .field("service", &self.service)
566            .field("method", &self.method)
567            .field("method_data_len", &self.method_data.len())
568            .finish()
569    }
570}
571
572impl UserauthRequest {
573    pub fn new(
574        user: Vec<u8>,
575        service: impl Into<String>,
576        method: impl Into<String>,
577        method_data: Vec<u8>,
578    ) -> Self {
579        UserauthRequest {
580            user,
581            service: service.into(),
582            method: method.into(),
583            method_data,
584        }
585    }
586}
587
588/// `SSH_MSG_USERAUTH_FAILURE`.
589#[non_exhaustive]
590#[derive(Debug, Clone, Default, PartialEq, Eq)]
591pub struct UserauthFailure {
592    /// Methods that may continue.
593    pub methods: Vec<String>,
594    pub partial_success: bool,
595}
596
597impl UserauthFailure {
598    pub fn new(methods: Vec<String>, partial_success: bool) -> Self {
599        UserauthFailure {
600            methods,
601            partial_success,
602        }
603    }
604}
605
606/// `SSH_MSG_USERAUTH_SUCCESS`.
607#[non_exhaustive]
608#[derive(Debug, Clone, Default, PartialEq, Eq)]
609pub struct UserauthSuccess;
610
611/// `SSH_MSG_USERAUTH_BANNER` (RFC 4252 §5.4), its strings kept as sent.
612#[non_exhaustive]
613#[derive(Debug, Clone, Default, PartialEq, Eq)]
614pub struct UserauthBanner {
615    pub message: Vec<u8>,
616    pub language: Vec<u8>,
617}
618
619impl UserauthBanner {
620    pub fn new(message: Vec<u8>, language: Vec<u8>) -> Self {
621        UserauthBanner { message, language }
622    }
623}
624
625/// `SSH2_MSG_PING` (OpenSSH PROTOCOL §1.8).
626#[non_exhaustive]
627#[derive(Debug, Clone, Default, PartialEq, Eq)]
628pub struct Ping {
629    pub data: Vec<u8>,
630}
631
632impl Ping {
633    pub fn new(data: Vec<u8>) -> Self {
634        Ping { data }
635    }
636}
637
638/// `SSH_MSG_GLOBAL_REQUEST` (RFC 4254 §4). `data` is the request-specific part, undecoded.
639#[non_exhaustive]
640#[derive(Debug, Clone, Default, PartialEq, Eq)]
641pub struct GlobalRequest {
642    pub kind: Name,
643    pub want_reply: bool,
644    pub data: Vec<u8>,
645}
646
647impl GlobalRequest {
648    pub fn new(kind: Name, want_reply: bool, data: Vec<u8>) -> Self {
649        GlobalRequest {
650            kind,
651            want_reply,
652            data,
653        }
654    }
655}
656
657/// `SSH_MSG_REQUEST_SUCCESS`: everything after the number, undecoded.
658#[non_exhaustive]
659#[derive(Debug, Clone, Default, PartialEq, Eq)]
660pub struct RequestSuccess {
661    pub data: Vec<u8>,
662}
663
664impl RequestSuccess {
665    pub fn new(data: Vec<u8>) -> Self {
666        RequestSuccess { data }
667    }
668}
669
670/// `SSH_MSG_REQUEST_FAILURE`: everything after the number, undecoded.
671#[non_exhaustive]
672#[derive(Debug, Clone, Default, PartialEq, Eq)]
673pub struct RequestFailure {
674    pub data: Vec<u8>,
675}
676
677impl RequestFailure {
678    pub fn new(data: Vec<u8>) -> Self {
679        RequestFailure { data }
680    }
681}
682
683/// `SSH2_MSG_PONG`: the data of the `SSH2_MSG_PING` it answers.
684#[non_exhaustive]
685#[derive(Debug, Clone, Default, PartialEq, Eq)]
686pub struct Pong {
687    pub data: Vec<u8>,
688}
689
690impl Pong {
691    pub fn new(data: Vec<u8>) -> Self {
692        Pong { data }
693    }
694}
695
696/// `SSH_MSG_CHANNEL_OPEN` (RFC 4254 §5.1). `data` is the channel-type-specific part, undecoded.
697#[non_exhaustive]
698#[derive(Debug, Clone, Default, PartialEq, Eq)]
699pub struct ChannelOpen {
700    pub kind: Name,
701    pub sender: u32,
702    pub window: u32,
703    pub max_packet: u32,
704    pub data: Vec<u8>,
705}
706
707impl ChannelOpen {
708    pub fn new(kind: Name, sender: u32, window: u32, max_packet: u32, data: Vec<u8>) -> Self {
709        ChannelOpen {
710            kind,
711            sender,
712            window,
713            max_packet,
714            data,
715        }
716    }
717}
718
719/// `SSH_MSG_CHANNEL_OPEN_CONFIRMATION`. `data` is the channel-type-specific part, undecoded.
720#[non_exhaustive]
721#[derive(Debug, Clone, Default, PartialEq, Eq)]
722pub struct ChannelOpenConfirmation {
723    pub recipient: u32,
724    pub sender: u32,
725    pub window: u32,
726    pub max_packet: u32,
727    pub data: Vec<u8>,
728}
729
730impl ChannelOpenConfirmation {
731    pub fn new(recipient: u32, sender: u32, window: u32, max_packet: u32, data: Vec<u8>) -> Self {
732        ChannelOpenConfirmation {
733            recipient,
734            sender,
735            window,
736            max_packet,
737            data,
738        }
739    }
740}
741
742/// `SSH_MSG_CHANNEL_OPEN_FAILURE`.
743#[non_exhaustive]
744#[derive(Debug, Clone, Default, PartialEq, Eq)]
745pub struct ChannelOpenFailure {
746    pub recipient: u32,
747    pub reason: u32,
748    pub description: Vec<u8>,
749    pub language: Vec<u8>,
750}
751
752impl ChannelOpenFailure {
753    pub fn new(
754        recipient: u32,
755        reason: u32,
756        description: impl Into<Vec<u8>>,
757        language: impl Into<Vec<u8>>,
758    ) -> Self {
759        ChannelOpenFailure {
760            recipient,
761            reason,
762            description: description.into(),
763            language: language.into(),
764        }
765    }
766}
767
768/// `SSH_MSG_CHANNEL_WINDOW_ADJUST`.
769#[non_exhaustive]
770#[derive(Debug, Clone, Default, PartialEq, Eq)]
771pub struct ChannelWindowAdjust {
772    pub recipient: u32,
773    pub bytes: u32,
774}
775
776impl ChannelWindowAdjust {
777    pub fn new(recipient: u32, bytes: u32) -> Self {
778        ChannelWindowAdjust { recipient, bytes }
779    }
780}
781
782/// `SSH_MSG_CHANNEL_DATA`.
783#[non_exhaustive]
784#[derive(Debug, Clone, Default, PartialEq, Eq)]
785pub struct ChannelData {
786    pub recipient: u32,
787    pub data: Vec<u8>,
788}
789
790impl ChannelData {
791    pub fn new(recipient: u32, data: Vec<u8>) -> Self {
792        ChannelData { recipient, data }
793    }
794
795    /// Appends to `out` the payload of a `ChannelData` to `recipient` carrying `data`, as
796    /// [`Message::encode`] writes it, without the message owning the data.
797    pub fn encode_from(recipient: u32, data: &[u8], out: &mut Vec<u8>) {
798        let mut w = Writer::appending(core::mem::take(out));
799        w.u8(number::CHANNEL_DATA).u32(recipient).string(data);
800        *out = w.into_bytes();
801    }
802}
803
804/// `SSH_MSG_CHANNEL_EXTENDED_DATA`; `code` 1 is stderr.
805#[non_exhaustive]
806#[derive(Debug, Clone, Default, PartialEq, Eq)]
807pub struct ChannelExtendedData {
808    pub recipient: u32,
809    pub code: u32,
810    pub data: Vec<u8>,
811}
812
813impl ChannelExtendedData {
814    pub fn new(recipient: u32, code: u32, data: Vec<u8>) -> Self {
815        ChannelExtendedData {
816            recipient,
817            code,
818            data,
819        }
820    }
821
822    /// Appends to `out` the payload of a `ChannelExtendedData` as [`ChannelData::encode_from`]
823    /// does.
824    pub fn encode_from(recipient: u32, code: u32, data: &[u8], out: &mut Vec<u8>) {
825        let mut w = Writer::appending(core::mem::take(out));
826        w.u8(number::CHANNEL_EXTENDED_DATA)
827            .u32(recipient)
828            .u32(code)
829            .string(data);
830        *out = w.into_bytes();
831    }
832}
833
834/// `SSH_MSG_CHANNEL_EOF`.
835#[non_exhaustive]
836#[derive(Debug, Clone, Default, PartialEq, Eq)]
837pub struct ChannelEof {
838    pub recipient: u32,
839}
840
841impl ChannelEof {
842    pub fn new(recipient: u32) -> Self {
843        ChannelEof { recipient }
844    }
845}
846
847/// `SSH_MSG_CHANNEL_CLOSE`.
848#[non_exhaustive]
849#[derive(Debug, Clone, Default, PartialEq, Eq)]
850pub struct ChannelClose {
851    pub recipient: u32,
852}
853
854impl ChannelClose {
855    pub fn new(recipient: u32) -> Self {
856        ChannelClose { recipient }
857    }
858}
859
860/// `SSH_MSG_CHANNEL_REQUEST` (RFC 4254 §5.4). `data` is the request-type-specific part,
861/// undecoded.
862#[non_exhaustive]
863#[derive(Debug, Clone, Default, PartialEq, Eq)]
864pub struct ChannelRequest {
865    pub recipient: u32,
866    pub kind: Name,
867    pub want_reply: bool,
868    pub data: Vec<u8>,
869}
870
871impl ChannelRequest {
872    pub fn new(recipient: u32, kind: Name, want_reply: bool, data: Vec<u8>) -> Self {
873        ChannelRequest {
874            recipient,
875            kind,
876            want_reply,
877            data,
878        }
879    }
880}
881
882/// `SSH_MSG_CHANNEL_SUCCESS`.
883#[non_exhaustive]
884#[derive(Debug, Clone, Default, PartialEq, Eq)]
885pub struct ChannelSuccess {
886    pub recipient: u32,
887}
888
889impl ChannelSuccess {
890    pub fn new(recipient: u32) -> Self {
891        ChannelSuccess { recipient }
892    }
893}
894
895/// `SSH_MSG_CHANNEL_FAILURE`.
896#[non_exhaustive]
897#[derive(Debug, Clone, Default, PartialEq, Eq)]
898pub struct ChannelFailure {
899    pub recipient: u32,
900}
901
902impl ChannelFailure {
903    pub fn new(recipient: u32) -> Self {
904        ChannelFailure { recipient }
905    }
906}