netio/ssh.rs#
Source: lib/netio/ssh.rs
ssh -- SSH-2 for running commands on a machine (RFC 4253 / 4252 / 4254)
Client::connect(stream, HostKeyCheck) -> Client version, key exchange, host key check
client.host_key() type, blob, "SHA256:..." fingerprint
client.auth_password(user, pw) / auth_key(user, &Keypair)
client.exec(command, stdin, max_out) -> Exec { status, stdout, stderr }
client.set_time_limit(Some(ms)) auth_* and exec end with TimedOut after
ms in all (a server that keeps sending
held them forever); None: no limit
Transport::accept(stream, &host_key) the server side of the transport
(tests drive it with OpenSSH's client)
private_key(text) -> Keypair an unencrypted Ed25519 key ("OPENSSH PRIVATE KEY")
public_key_line(&Keypair, comment) -> "ssh-ed25519 AAAA... comment" (authorized_keys)
fingerprint(blob) -> "SHA256:..." the way ssh-keygen -l shows it
Algorithms: curve25519-sha256 (also @libssh.org) with OpenSSH's strict key exchange (against
the Terrapin prefix truncation), ssh-ed25519 host and user keys, chacha20-poly1305@openssh.com.
The host key is checked by the caller's rule: HostKeyCheck::Pinned(fingerprint) or Any (only
to learn the fingerprint). Not here: RSA / ECDSA keys, compression, port forwarding, PTYs,
rekeying (a peer that starts a new key exchange ends the session; commands here are short).
A peer that sends only messages to skip (ignore, debug, banners, global requests) while an
answer is due ends the session after MAX_SKIPPED of them in a row; data past the window the
client granted is a protocol error.
MSG_DISCONNECT: u8 = 1
MSG_IGNORE: u8 = 2
MSG_UNIMPLEMENTED: u8 = 3
MSG_DEBUG: u8 = 4
MSG_SERVICE_REQUEST: u8 = 5
MSG_SERVICE_ACCEPT: u8 = 6
MSG_EXT_INFO: u8 = 7
MSG_KEXINIT: u8 = 20
MSG_NEWKEYS: u8 = 21
MSG_KEX_ECDH_INIT: u8 = 30
MSG_KEX_ECDH_REPLY: u8 = 31
MSG_USERAUTH_REQUEST: u8 = 50
MSG_USERAUTH_FAILURE: u8 = 51
MSG_USERAUTH_SUCCESS: u8 = 52
MSG_USERAUTH_BANNER: u8 = 53
MSG_USERAUTH_PK_OK: u8 = 60
MSG_GLOBAL_REQUEST: u8 = 80
MSG_REQUEST_FAILURE: u8 = 82
MSG_CHANNEL_OPEN: u8 = 90
MSG_CHANNEL_OPEN_CONFIRMATION: u8 = 91
MSG_CHANNEL_OPEN_FAILURE: u8 = 92
MSG_CHANNEL_WINDOW_ADJUST: u8 = 93
MSG_CHANNEL_DATA: u8 = 94
MSG_CHANNEL_EXTENDED_DATA: u8 = 95
MSG_CHANNEL_EOF: u8 = 96
MSG_CHANNEL_CLOSE: u8 = 97
MSG_CHANNEL_REQUEST: u8 = 98
MSG_CHANNEL_SUCCESS: u8 = 99
MSG_CHANNEL_FAILURE: u8 = 100
enum SshErrorNet(NetError)Protocol(String)-- the peer broke the protocol or offers nothing we speakHostKey(String)-- the host key is not the expected one (the fingerprint it showed)Auth-- the user name / password / key was refusedDisconnected(String)-- the peer ended the session (its reason)
struct W(pub Vec<u8>)an SSH message under construction
impl W
fn msg(t: u8) -> Wfn u8(mut self, v: u8) -> Wfn u32(mut self, v: u32) -> Wfn bool(self, v: bool) -> Wfn bytes(mut self, v: &[u8]) -> Wfn str(self, v: &str) -> Wfn mpint(self, v: &[u8]) -> Wan unsigned big-endian integer as mpintfn raw(mut self, v: &[u8]) -> Wstruct R<'a>reading an SSH message
impl<'a> R<'a>
fn new(b: &'a [u8]) -> R<'a>fn u8(&mut self) -> Option<u8>fn u32(&mut self) -> Option<u32>fn bool(&mut self) -> Option<bool>fn bytes(&mut self) -> Option<&'a [u8]>fn str(&mut self) -> Option<&'a str>fn skip(&mut self, n: usize) -> Option<()>fn rest(&self) -> &'a [u8]fn fingerprint(blob: &[u8]) -> String"SHA256:<base64 without padding>" of a public key blobfn public_key_line(k: &Keypair, comment: &str) -> String"ssh-ed25519 AAAA... comment"fn private_key(text: &str) -> Option<Keypair>an unencrypted Ed25519 key in OpenSSH's format ("-----BEGIN OPENSSH PRIVATE KEY-----")struct HostKeythe host key a server showedkey_type: Stringblob: Vec<u8>fingerprint: String
enum HostKeyCheckPinned(String)-- exactly this key ("SHA256:...")Any-- any key (to read its fingerprint for a decision)
struct Transportthe encrypted packet stream of one connectionsession_id: Vec<u8>peer_version: String-- the peer's version line
impl Transport
fn send(&mut self, payload: &[u8]) -> Result<(), SshError>one packet outfn recv(&mut self) -> Result<Vec<u8>, SshError>the next message that matters: ignore / debug / extension info are skipped, a disconnect ends the session, a new key exchange is not supportedfn accept(s: Box<dyn Stream>, host: &Keypair) -> Result<Transport, SshError>the server side: version, key exchange with this host keyfn disconnect(&mut self, reason: &str)struct Execwhat a command didstatus: Option<u32>-- the exit code (None: it ended by a signal or the server did not say)stdout: Vec<u8>stderr: Vec<u8>
struct Client
impl Client
fn connect(s: Box<dyn Stream>, check: HostKeyCheck) -> Result<Client, SshError>fn set_time_limit(&mut self, ms: Option<u32>)Each followingauth_password,auth_keyandexecends withSshError::Net(TimedOut) when it takes longer thanmsin all;None(the default): no limit. The stream's read timeout only limits each wait: a server that keeps sending (banners, endless output) held these calls forever. After a TimedOut the session is in an unknown state: close it.fn host_key(&self) -> &HostKeyfn auth_password(&mut self, user: &str, password: &str) -> Result<(), SshError>fn auth_key(&mut self, user: &str, key: &Keypair) -> Result<(), SshError>fn exec(&mut self, command: &str, stdin: &[u8], max_out: usize) -> Result<Exec, SshError>run a command, feed itstdin, collect at mostmax_outbytes of each output (the rest is read and dropped until the command ends: set_time_limit bounds a command that does not)fn close(mut self)