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
#![doc = include_str!("../README.md")]

use std::io::{Result as IoResult, Error as IoError, ErrorKind, BufReader, BufWriter, BufRead, Read, Write};
use std::net::TcpStream;
use core::mem::size_of;

use rand_core::OsRng as Rng;
use aes::cipher::{KeyIvInit, StreamCipher};
use hmac::Hmac;
use ed25519_dalek::{Keypair, Verifier, Signer};

type Cipher = ctr::Ctr64BE<aes::Aes256>;

const VERSION_HEADER: &'static [u8] = b"SSH-2.0-tinyssh+1.0";
const U32: usize = size_of::<u32>();
const U8: usize = size_of::<u8>();

mod connection;
mod parsedump;
mod userauth;
mod channelrequest;
mod messages;
mod packets;
mod run;
mod hmac;
mod keygen;

#[doc(inline)]
pub use {
    connection::{Connection, Auth},
    run::{Run, RunResult, RunEvent, ExitStatus},
    messages::MessageType,
    keygen::{create_ed25519_keypair, dump_ed25519_pk_openssh},
};

fn sha256<'b, P: parsedump::ParseDump<'b>>(data: &P) -> Result<[u8; 32]> {
    use sha2::{Sha256, Digest};

    struct Wrapper(Sha256);
    impl Write for Wrapper {
        fn flush(&mut self) -> IoResult<()> { Ok(()) }
        fn write(&mut self, buf: &[u8]) -> IoResult<usize> {
            self.0.update(buf);
            Ok(buf.len())
        }
    }

    let mut hasher = Wrapper(Sha256::new());
    data.dump(&mut hasher)?;

    Ok(hasher.0.finalize().into())
}

pub(crate) const fn ed25519_blob_len(content_len: u32) -> u32 {
    4 + 11 + 4 + content_len
}

/// Fatal errors
#[derive(Copy, Clone, Debug)]
pub enum Error {
    /// No data to be read / send buffer is full.
    Timeout,
    /// Errors related to the TCP socket
    TcpError(ErrorKind),
    /// Invalid data type/encoding/size
    InvalidData,
    AuthenticationFailure,
    InvalidKeypair,
    ProcessHasExited,
    UnexpectedMessageType(MessageType),
    UnknownMessageType(u8),
    /// This can be raised instead of UnexpectedMessageType, if the peer sends random bytes
    Unimplemented,
}

pub type Result<T> = core::result::Result<T, Error>;

impl From<IoError> for Error {
    fn from(err: IoError) -> Self {
        Self::TcpError(err.kind())
    }
}