puressh 0.0.7

A pure-Rust SSH (Secure Shell) protocol library, in the spirit of libssh, built on purecrypto.
Documentation
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
//! Pure, I/O-free codec for the mux control-socket protocol.
//!
//! See the [module-level docs](super) for the frame layout. Every [`Frame`]
//! variant has a single-byte type tag and a self-describing payload; the codec
//! is symmetric ([`Frame::encode`] / [`Frame::decode`]) and exhaustively
//! round-trip tested.

use std::fmt;

/// Protocol version carried in [`Frame::Hello`]. Bumped on any incompatible
/// wire change; a master refuses a client whose `HELLO` version differs.
pub const PROTOCOL_VERSION: u32 = 1;

/// Upper bound on a single frame's body length (type byte + payload). Guards
/// the reader against a hostile/garbled length prefix demanding an unbounded
/// allocation. 16 MiB is far above any legitimate stdin/stdout chunk (the
/// pumps use 32 KiB buffers).
pub const MAX_FRAME_LEN: usize = 16 * 1024 * 1024;

// Frame type tags.
const T_HELLO: u8 = 1;
const T_OPEN_SESSION: u8 = 2;
const T_STDIN_DATA: u8 = 3;
const T_STDOUT_DATA: u8 = 4;
const T_STDERR_DATA: u8 = 5;
const T_EOF: u8 = 6;
const T_WINDOW_CHANGE: u8 = 7;
const T_EXIT_STATUS: u8 = 8;
const T_EXIT_SIGNAL: u8 = 9;
const T_ALIVE_CHECK: u8 = 10;
const T_ALIVE_OK: u8 = 11;
const T_EXIT_REQUEST: u8 = 12;
const T_OPEN_DIRECT_TCPIP: u8 = 13;
const T_OPEN_OK: u8 = 14;
const T_OPEN_FAIL: u8 = 15;

/// A decoded mux control-socket message.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Frame {
    /// First frame in either direction: protocol-version handshake.
    Hello {
        /// Sender's [`PROTOCOL_VERSION`].
        version: u32,
    },
    /// Client → master: open a new session channel.
    OpenSession {
        /// Allocate a PTY for the session.
        want_pty: bool,
        /// `$TERM` value (empty when `want_pty` is false).
        term: String,
        /// Terminal width in columns.
        cols: u32,
        /// Terminal height in rows.
        rows: u32,
        /// Environment variables to set on the remote session.
        env: Vec<(String, String)>,
        /// Remote command to exec; `None` requests an interactive shell.
        command: Option<String>,
    },
    /// Client → master: bytes for the session's stdin.
    StdinData(Vec<u8>),
    /// Master → client: bytes from the session's stdout.
    StdoutData(Vec<u8>),
    /// Master → client: bytes from the session's stderr.
    StderrData(Vec<u8>),
    /// Either direction: the sender's write side is finished (half-close).
    Eof,
    /// Client → master: terminal resize.
    WindowChange {
        /// New width in columns.
        cols: u32,
        /// New height in rows.
        rows: u32,
    },
    /// Master → client: the remote session exited with this status code.
    ExitStatus {
        /// Remote exit code (0–255).
        code: u32,
    },
    /// Master → client: the remote session was killed by a signal.
    ExitSignal {
        /// Signal name without the `SIG` prefix (e.g. `TERM`, `KILL`).
        name: String,
    },
    /// Client → master: liveness probe (used by `probe_master`).
    AliveCheck,
    /// Master → client: liveness reply to [`Frame::AliveCheck`].
    AliveOk,
    /// Client → master: ask the master to shut down (`ssh -O exit`).
    ExitRequest,
    /// Client → master: open a `direct-tcpip` channel on the master's SSH
    /// connection (the mux carrier for `ssh -L` / `ssh -D`). The master dials
    /// `dest_host:dest_port` *through the server* and, on success, splices the
    /// resulting channel against this control connection using the same
    /// `StdinData`/`StdoutData`/`Eof` frames a session uses.
    OpenDirectTcpip {
        /// Destination host the *server* should connect to.
        dest_host: String,
        /// Destination port the server should connect to.
        dest_port: u32,
        /// Informational originator address echoed in the channel open.
        orig_host: String,
        /// Informational originator port echoed in the channel open.
        orig_port: u32,
    },
    /// Master → client: the requested channel open succeeded; byte splicing
    /// follows. Sent in reply to [`Frame::OpenDirectTcpip`].
    OpenOk,
    /// Master → client: the requested channel open failed. `reason` is a
    /// human-readable diagnostic. Sent in reply to [`Frame::OpenDirectTcpip`].
    OpenFail {
        /// Human-readable failure reason.
        reason: String,
    },
}

impl Frame {
    /// Serialise the frame into a `length(u32) | type(u8) | payload` byte
    /// vector ready to write to the socket.
    pub fn encode(&self) -> Vec<u8> {
        let mut body = Vec::new();
        self.encode_body(&mut body);
        let mut out = Vec::with_capacity(4 + body.len());
        out.extend_from_slice(&(body.len() as u32).to_be_bytes());
        out.extend_from_slice(&body);
        out
    }

    /// Encode just the body (`type | payload`), without the length prefix.
    fn encode_body(&self, out: &mut Vec<u8>) {
        match self {
            Frame::Hello { version } => {
                out.push(T_HELLO);
                out.extend_from_slice(&version.to_be_bytes());
            }
            Frame::OpenSession {
                want_pty,
                term,
                cols,
                rows,
                env,
                command,
            } => {
                out.push(T_OPEN_SESSION);
                out.push(*want_pty as u8);
                put_str(out, term);
                out.extend_from_slice(&cols.to_be_bytes());
                out.extend_from_slice(&rows.to_be_bytes());
                out.extend_from_slice(&(env.len() as u32).to_be_bytes());
                for (k, v) in env {
                    put_str(out, k);
                    put_str(out, v);
                }
                // command: 1-byte present flag, then the string when present.
                match command {
                    Some(c) => {
                        out.push(1);
                        put_str(out, c);
                    }
                    None => out.push(0),
                }
            }
            Frame::StdinData(d) => {
                out.push(T_STDIN_DATA);
                out.extend_from_slice(d);
            }
            Frame::StdoutData(d) => {
                out.push(T_STDOUT_DATA);
                out.extend_from_slice(d);
            }
            Frame::StderrData(d) => {
                out.push(T_STDERR_DATA);
                out.extend_from_slice(d);
            }
            Frame::Eof => out.push(T_EOF),
            Frame::WindowChange { cols, rows } => {
                out.push(T_WINDOW_CHANGE);
                out.extend_from_slice(&cols.to_be_bytes());
                out.extend_from_slice(&rows.to_be_bytes());
            }
            Frame::ExitStatus { code } => {
                out.push(T_EXIT_STATUS);
                out.extend_from_slice(&code.to_be_bytes());
            }
            Frame::ExitSignal { name } => {
                out.push(T_EXIT_SIGNAL);
                put_str(out, name);
            }
            Frame::AliveCheck => out.push(T_ALIVE_CHECK),
            Frame::AliveOk => out.push(T_ALIVE_OK),
            Frame::ExitRequest => out.push(T_EXIT_REQUEST),
            Frame::OpenDirectTcpip {
                dest_host,
                dest_port,
                orig_host,
                orig_port,
            } => {
                out.push(T_OPEN_DIRECT_TCPIP);
                put_str(out, dest_host);
                out.extend_from_slice(&dest_port.to_be_bytes());
                put_str(out, orig_host);
                out.extend_from_slice(&orig_port.to_be_bytes());
            }
            Frame::OpenOk => out.push(T_OPEN_OK),
            Frame::OpenFail { reason } => {
                out.push(T_OPEN_FAIL);
                put_str(out, reason);
            }
        }
    }

    /// Decode a complete `length | type | payload` frame from `bytes`.
    /// Convenience for tests; the streaming reader strips the length prefix
    /// itself and calls [`Frame::decode_body`].
    pub fn decode(bytes: &[u8]) -> Result<Frame, MuxError> {
        if bytes.len() < 4 {
            return Err(MuxError::Malformed("frame shorter than length prefix"));
        }
        let len = u32::from_be_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]) as usize;
        let body = &bytes[4..];
        if body.len() != len {
            return Err(MuxError::Malformed("length prefix mismatches body"));
        }
        Frame::decode_body(body)
    }

    /// Decode the body (`type | payload`) of a frame whose length prefix has
    /// already been consumed.
    pub fn decode_body(body: &[u8]) -> Result<Frame, MuxError> {
        let mut r = Cursor::new(body);
        let tag = r.u8()?;
        let frame = match tag {
            T_HELLO => Frame::Hello { version: r.u32()? },
            T_OPEN_SESSION => {
                let want_pty = r.u8()? != 0;
                let term = r.str()?;
                let cols = r.u32()?;
                let rows = r.u32()?;
                let n = r.u32()? as usize;
                let mut env = Vec::with_capacity(n.min(64));
                for _ in 0..n {
                    let k = r.str()?;
                    let v = r.str()?;
                    env.push((k, v));
                }
                let command = match r.u8()? {
                    0 => None,
                    1 => Some(r.str()?),
                    _ => return Err(MuxError::Malformed("OPEN_SESSION: bad command flag")),
                };
                Frame::OpenSession {
                    want_pty,
                    term,
                    cols,
                    rows,
                    env,
                    command,
                }
            }
            T_STDIN_DATA => Frame::StdinData(r.rest().to_vec()),
            T_STDOUT_DATA => Frame::StdoutData(r.rest().to_vec()),
            T_STDERR_DATA => Frame::StderrData(r.rest().to_vec()),
            T_EOF => Frame::Eof,
            T_WINDOW_CHANGE => Frame::WindowChange {
                cols: r.u32()?,
                rows: r.u32()?,
            },
            T_EXIT_STATUS => Frame::ExitStatus { code: r.u32()? },
            T_EXIT_SIGNAL => Frame::ExitSignal { name: r.str()? },
            T_ALIVE_CHECK => Frame::AliveCheck,
            T_ALIVE_OK => Frame::AliveOk,
            T_EXIT_REQUEST => Frame::ExitRequest,
            T_OPEN_DIRECT_TCPIP => {
                let dest_host = r.str()?;
                let dest_port = r.u32()?;
                let orig_host = r.str()?;
                let orig_port = r.u32()?;
                Frame::OpenDirectTcpip {
                    dest_host,
                    dest_port,
                    orig_host,
                    orig_port,
                }
            }
            T_OPEN_OK => Frame::OpenOk,
            T_OPEN_FAIL => Frame::OpenFail { reason: r.str()? },
            other => return Err(MuxError::UnknownType(other)),
        };
        // Variants with fixed-length payloads must consume the whole body;
        // the open-ended *Data variants legitimately swallow `rest()`.
        if !matches!(
            frame,
            Frame::StdinData(_) | Frame::StdoutData(_) | Frame::StderrData(_)
        ) && !r.is_empty()
        {
            return Err(MuxError::Malformed("trailing bytes after frame payload"));
        }
        Ok(frame)
    }
}

/// Stateless codec marker. The actual work lives on [`Frame`]; this type
/// exists so callers can name the codec in signatures / docs.
#[derive(Clone, Copy, Debug, Default)]
pub struct FrameCodec;

impl FrameCodec {
    /// Encode one frame (delegates to [`Frame::encode`]).
    pub fn encode(frame: &Frame) -> Vec<u8> {
        frame.encode()
    }

    /// Decode one complete frame (delegates to [`Frame::decode`]).
    pub fn decode(bytes: &[u8]) -> Result<Frame, MuxError> {
        Frame::decode(bytes)
    }
}

/// Errors from the mux codec / framed I/O layer.
#[derive(Debug)]
pub enum MuxError {
    /// Underlying socket I/O failed.
    Io(std::io::Error),
    /// A frame's bytes did not match the protocol grammar.
    Malformed(&'static str),
    /// The type tag is not one this version understands.
    UnknownType(u8),
    /// Peer reported an incompatible [`PROTOCOL_VERSION`] in its `HELLO`.
    VersionMismatch {
        /// Version this build speaks.
        ours: u32,
        /// Version the peer advertised.
        theirs: u32,
    },
    /// The peer sent an unexpected frame for the current protocol state
    /// (e.g. a non-`HELLO` first frame).
    Unexpected(&'static str),
    /// The master refused an `OpenDirectTcpip` (`ssh -L`/`-D` over mux); the
    /// payload is the master's human-readable reason.
    ForwardFailed(String),
}

impl fmt::Display for MuxError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            MuxError::Io(e) => write!(f, "mux io: {e}"),
            MuxError::Malformed(s) => write!(f, "mux malformed frame: {s}"),
            MuxError::UnknownType(t) => write!(f, "mux unknown frame type {t}"),
            MuxError::VersionMismatch { ours, theirs } => {
                write!(
                    f,
                    "mux protocol version mismatch: ours={ours} theirs={theirs}"
                )
            }
            MuxError::Unexpected(s) => write!(f, "mux unexpected frame: {s}"),
            MuxError::ForwardFailed(s) => write!(f, "mux forward refused: {s}"),
        }
    }
}

impl std::error::Error for MuxError {}

impl From<std::io::Error> for MuxError {
    fn from(e: std::io::Error) -> Self {
        MuxError::Io(e)
    }
}

/// Append a `u32`-length-prefixed string to `out`.
fn put_str(out: &mut Vec<u8>, s: &str) {
    out.extend_from_slice(&(s.len() as u32).to_be_bytes());
    out.extend_from_slice(s.as_bytes());
}

/// Minimal big-endian reader over a byte slice with bounds checks.
struct Cursor<'a> {
    buf: &'a [u8],
    pos: usize,
}

impl<'a> Cursor<'a> {
    fn new(buf: &'a [u8]) -> Self {
        Cursor { buf, pos: 0 }
    }

    fn is_empty(&self) -> bool {
        self.pos >= self.buf.len()
    }

    fn take(&mut self, n: usize) -> Result<&'a [u8], MuxError> {
        let end = self
            .pos
            .checked_add(n)
            .ok_or(MuxError::Malformed("length overflow"))?;
        if end > self.buf.len() {
            return Err(MuxError::Malformed("frame truncated"));
        }
        let s = &self.buf[self.pos..end];
        self.pos = end;
        Ok(s)
    }

    fn u8(&mut self) -> Result<u8, MuxError> {
        Ok(self.take(1)?[0])
    }

    fn u32(&mut self) -> Result<u32, MuxError> {
        let b = self.take(4)?;
        Ok(u32::from_be_bytes([b[0], b[1], b[2], b[3]]))
    }

    fn str(&mut self) -> Result<String, MuxError> {
        let n = self.u32()? as usize;
        let b = self.take(n)?;
        String::from_utf8(b.to_vec()).map_err(|_| MuxError::Malformed("string is not UTF-8"))
    }

    fn rest(&mut self) -> &'a [u8] {
        let s = &self.buf[self.pos..];
        self.pos = self.buf.len();
        s
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn round_trip(f: Frame) {
        let bytes = f.encode();
        let back = Frame::decode(&bytes).expect("decode");
        assert_eq!(f, back, "round-trip mismatch");
        // decode_body alone (no length prefix) must agree.
        let body = &bytes[4..];
        assert_eq!(Frame::decode_body(body).unwrap(), f);
    }

    #[test]
    fn round_trip_all_variants() {
        round_trip(Frame::Hello { version: 1 });
        round_trip(Frame::Hello {
            version: 0xDEAD_BEEF,
        });
        round_trip(Frame::OpenSession {
            want_pty: true,
            term: "xterm".into(),
            cols: 120,
            rows: 40,
            env: vec![
                ("A".into(), "1".into()),
                ("LONGKEY".into(), "a value with spaces".into()),
            ],
            command: Some("ls -la /tmp".into()),
        });
        round_trip(Frame::OpenSession {
            want_pty: false,
            term: String::new(),
            cols: 0,
            rows: 0,
            env: vec![],
            command: None,
        });
        round_trip(Frame::StdinData(vec![]));
        round_trip(Frame::StdinData(b"some bytes \x00\xff".to_vec()));
        round_trip(Frame::StdoutData(b"out".to_vec()));
        round_trip(Frame::StderrData(b"err".to_vec()));
        round_trip(Frame::Eof);
        round_trip(Frame::WindowChange { cols: 80, rows: 24 });
        round_trip(Frame::ExitStatus { code: 0 });
        round_trip(Frame::ExitStatus { code: 255 });
        round_trip(Frame::ExitSignal {
            name: "TERM".into(),
        });
        round_trip(Frame::AliveCheck);
        round_trip(Frame::AliveOk);
        round_trip(Frame::ExitRequest);
        round_trip(Frame::OpenDirectTcpip {
            dest_host: "example.com".into(),
            dest_port: 443,
            orig_host: "127.0.0.1".into(),
            orig_port: 51234,
        });
        round_trip(Frame::OpenDirectTcpip {
            dest_host: "2001:db8::1".into(),
            dest_port: 0,
            orig_host: String::new(),
            orig_port: 0,
        });
        round_trip(Frame::OpenOk);
        round_trip(Frame::OpenFail {
            reason: "connect refused".into(),
        });
    }

    #[test]
    fn open_session_unicode_and_empty_env() {
        round_trip(Frame::OpenSession {
            want_pty: true,
            term: "scréen-256".into(),
            cols: 1,
            rows: 1,
            env: vec![],
            command: Some("écho café".into()),
        });
    }

    #[test]
    fn length_prefix_is_body_length() {
        let f = Frame::ExitStatus { code: 7 };
        let bytes = f.encode();
        let len = u32::from_be_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]) as usize;
        assert_eq!(len, bytes.len() - 4);
        // body = T_EXIT_STATUS (1) + u32 code (4) = 5 bytes
        assert_eq!(len, 5);
    }

    #[test]
    fn decode_unknown_type_errors() {
        let body = [200u8]; // unknown tag, no payload
        assert!(matches!(
            Frame::decode_body(&body),
            Err(MuxError::UnknownType(200))
        ));
    }

    #[test]
    fn decode_truncated_errors() {
        // T_EXIT_STATUS expects 4 more bytes; give it 2.
        let body = [T_EXIT_STATUS, 0, 0];
        assert!(matches!(
            Frame::decode_body(&body),
            Err(MuxError::Malformed(_))
        ));
    }

    #[test]
    fn decode_trailing_bytes_errors() {
        // EOF takes no payload; appending a byte must be rejected.
        let body = [T_EOF, 0xAA];
        assert!(matches!(
            Frame::decode_body(&body),
            Err(MuxError::Malformed(_))
        ));
    }

    #[test]
    fn decode_empty_body_errors() {
        assert!(matches!(
            Frame::decode_body(&[]),
            Err(MuxError::Malformed(_))
        ));
    }

    #[test]
    fn data_frames_keep_trailing_bytes() {
        // The *Data variants intentionally absorb the entire remaining body,
        // including bytes that look like a tag.
        let f = Frame::StdoutData(vec![T_EOF, T_HELLO, 0, 0]);
        round_trip(f);
    }

    #[test]
    fn framecodec_delegates() {
        let f = Frame::AliveCheck;
        let b = FrameCodec::encode(&f);
        assert_eq!(FrameCodec::decode(&b).unwrap(), f);
    }
}