moonbit_openssh

    An OpenSSH-compatible SSH client, server and SFTP stack written from scratch in MoonBit.

    ssh
    openssh
    crypto
    sftp
    Download zip
    Author
    Version
    0.2.1
    License
    MIT
    Last updated
    21 hours ago
    Downloads
    36

    #riantr/moonbit_openssh

    A hand-written SSH client and server in MoonBit, with no cryptography from any third-party SSH or TLS library.

    Everything below the socket is implemented in this repository: the key exchanges, the ciphers and MACs, signature verification (both directions), host key pinning, known_hosts parsing, SFTP (client and server), userauth, the OpenSSH certificate format, and the connection protocol that runs a channel. The only dependency that carries weight is moonbitlang/async, for filesystem and socket syscalls. moonbit-community/proton* is used by cmd/proton_gui only -- no package under src/ may import it.

    • Branch of record: loop41-m2-fault-console (currently at Loop #104)
    • Loop history and every design decision: AGENTS.md -- read this one, it is the actual documentation
    • License: MIT -- see LICENSE. Third-party reference code kept under verify_c/ and triple_check/ is NOT relicensed: those files carry their own upstream notices (BSD / public domain), which are left in place.
    • Mirrors: GitHub (tagged states only) and Gitee (the working remote, pushed continuously)

    #Install from mooncakes.io

    moon add riantr/moonbit_openssh

    The version on mooncakes.io is the one moon.mod names, and the release gate refuses to create a git tag unless the two agree (ci/publish.py --check), so a published version and a git tag name the same tested state by construction.

    This file is the module README, and moon.mod points readme at README.mbt.md. The repository-root README.md is a symlink to it (mode 120000 in git), so the two names are one file and editing either edits both. That is deliberate: a README that can drift from the one moon.mod ships is a README that will eventually be wrong.

    #What the live client actually offers

    This is the proposal cmd/ssh_live and cmd/wt_ssh send, read out of src/sshclient/live.mbt rather than from memory:

    fieldoffered
    key exchangediffie-hellman-group14-sha256
    host keyssh-ed25519, rsa-sha2-512, rsa-sha2-256, ecdsa-sha2-nistp256, ecdsa-sha2-nistp384
    host key, when a @cert-authority anchor existsthe above plus ssh-ed25519-cert-v01@openssh.com
    cipheraes128-ctr
    MAChmac-sha2-256
    compressionnone

    Anything wider is implemented and unit-tested but not offered in the live proposal -- curve25519-sha256 (src/kexc25519), chacha20-poly1305@openssh.com (src/aead, with RFC 8439 vectors), aes-*-gcm@openssh.com, hmac-sha2-512. Those carry known-answer tests; what is proven against a peer is the column above. Widening the proposal is a one-line change and its own piece of work.

    Also working, and proven against a real sshd or a man in the middle:

    • publickey userauth
    • interactive sessions: pty-req (columns before rows on the wire, which is the order the real server reads them in), shell, and window-change with want_reply = FALSE. A refused pty-req falls back to a session with no tty instead of failing. A SECOND pty-req ends the whole CONNECTION (DISCONNECT reason 2, "you already have a pty."), measured against the real server.
    • SFTP against internal-sftp, with window and max-packet clamping
    • host key pinning: known_hosts, last-match-wins, @revoked, TOFU
    • OpenSSH host certificates as a real trust anchor, with a genuine clock
    • port forwarding, both directions: direct-tcpip (ssh -L) and forwarded-tcpip (ssh -R), each measured against the real sshd
    • the SSH agent: ssh_agent holds keys, ssh_add talks to it, and the real ssh-add talks to our agent byte for byte. The transport is a Windows named pipe, because that is what the real client speaks.
    • two transports, one set of rules

    #The four targets

    host/verify.mjs builds cmd/main for 4 targets x 2 modes (debug and release) and asserts one SHA-256 across all eight outputs: x25519, KEXINIT, wire framing and chacha20-poly1305 produce the same bytes on every backend.

    What that covers, and what it deliberately does not:

    layerpackageswhat is proven
    protocol rulesbpacket, kexflow, sshdbuilt on all four by CI; src/sshd's 23 unit tests pass on each
    the democmd/mainbyte-exact on 4 targets x 2 modes; host/main.wasm is its wasm-gc build, instantiated and asserted in a browser
    platform seamsrng, tcp, subproc, sftpserver, scp, agentpipe, keypermcompile on all four: one file per backend, and the backend that lacks the platform says so instead of pretending
    live native stackkhfile, cmd/sshd, cmd/ssh_live, cmd/sftp_servernative only -- files, sockets and child processes
    live browser stackwt, wtlive, khstore, cmd/wt_sshjs only: extern "js", and MoonBit's async does not cover the wasm backends
    wasm socketsws_socket, wt_socketextern "wasm", so a host shim supplies send / recv / close

    So "runs on four targets" is a claim about the rules, not about a finished client. The wasm backends have no entropy source at all -- src/rng reports that rather than inventing bytes -- so a handshake there needs a host-provided CSPRNG before it can start. What runs on wasm today is the protocol and crypto kernel above, compared byte for byte; the product-shaped client and server run on native and, in a browser, on js.

    #The server

    cmd/sshd is a real SSH server, not a test double: it completes the same transport handshake as the client, accepts publickey userauth, and then speaks RFC 4254 -- channel open/confirm, exec, pty-req, shell, window-change, stdout and stderr as separate streams, EOF, exit-status, close. A real ssh.exe (OpenSSH 9.5p2) runs commands through it, and the transcripts are compared byte for byte against the same commands run through the real Windows sshd (OpenSSH 8.0): 18/18 cases identical on exit code, stdout and stderr, including the two cases that are only visible to a real client (cmd.exe keeps the trailing quote of /c "...", and a server that closes the socket before draining the peer's close loses the exit status to a TCP reset).

    The server answers pty-req with CHANNEL_SUCCESS and records the terminal size; a SECOND pty-req ends the whole CONNECTION with a DISCONNECT (reason 2, "you already have a pty."). shell is answered with CHANNEL_SUCCESS and starts the login shell -- its stdin is the peer's CHANNEL_DATA, its stdout comes back as CHANNEL_DATA (a pty session has ONE stream: no CHANNEL_EXTENDED_DATA). window-change is answered exactly when the peer set want_reply. env is dropped without a reply, the way RFC 4254 prescribes. The session ends with our EOF, then the exit status (a CHANNEL_REQUEST with want_reply = FALSE), then the CLOSE.

    The server half of SFTP already exists as src/sftpserver -- the same requests and replies, measured against the real sftp-server.exe -- and cmd/sftp_server drives it over standard input, wired to the channel layer.

    The server also carries a remote port forward (ssh -R): the client asks the server to listen on a port, and every connection that arrives there is carried to the client over a forwarded-tcpip channel the server opens (RFC 4254 §7.1). The rules are in src/sshd/remotefwd.mbt. The reply to a forward for port 0 carries the port the server chose, and the reply to a forward for a fixed port carries nothing -- both measured against the real OpenSSH-Win64 server, not recalled from the RFC. The four fields of the channel are the listening address and port first and the originator's address and port second, and the window is 2097152 with a maximum packet of 32768: the real client's own log says so, and the gate asserts the same bytes against the real server.

    host/verify_loop94.mjs is the gate (49 checks), host/loop94_mutations.py proves it would catch regressions (10/10 CAUGHT), and host/loop94_oracle.py re-measures the real sshd whenever the claim needs re-grounding. host/verify_loop104.mjs is the terminal gate (23 checks: pty-req, shell, window-change, and the second-pty disconnect).

    #The tools

    The OpenSSH command-line tools are being rebuilt one at a time, and each one is measured against the real script before it is written:

    • cmd/ssh_copy_id is ssh-copy-id. Upstream's is a POSIX shell script, and on a Windows server it exits 0, prints Number of key(s) added: 1, and installs nothing -- the remote command it sends is exec sh -c '...', which cmd.exe cannot run. This build asks the far side what it is first (echo %OS%), sends a POSIX one-liner to a POSIX server and powershell -NoProfile -EncodedCommand to a Windows one, and then authenticates with the key it just installed: if the server refuses it, the tool says so and exits 1 instead of claiming success. The sentences are upstream's own bytes, captured from the real script in the same run and compared byte for byte; two of upstream's quirks (the -f sentence with an empty path, the "too many arguments" sentence with an empty list) are kept on purpose. host/verify_loop103.mjs is the gate (45 checks). The POSIX one-liner follows upstream's current source; the copy of the script installed on a machine is not always that one, so the gate compares the part upstream has not changed byte for byte and checks the two clauses it has changed separately. docs/sshcopyid/loop103-summary.md has the measurements.
    • cmd/ssh_keygen, cmd/ssh_keyscan, cmd/scp and cmd/sftp_server are already here; ssh-agent/ssh-add are the agent pair.

    #Build and test

    # per-package unit tests cmd /c "moon test src\knownhosts" cmd /c "moon test src\hostkey" cmd /c "moon test src\khfile --target native" # native-only package # live client, and the server cmd /c "moon build cmd\ssh_live --target native" cmd /c "moon build cmd\sshd --target native"

    moon check --target native at the repository root is red by design; see Known gaps.

    #The local CI, the tag gate and publication

    ci/ci.py runs everything as named steps and prints one summary line that cannot disagree with the exit code:

    python ci\ci.py # env,build,gates (the default) python ci\ci.py --tiers env,build,gates,qa # the release run python ci\ci.py --self-check # tests the engine itself python ci\ci.py --only loop94 --tiers gates # one gate

    Three rules are enforced by code rather than by intent:

    • No tag while CI is red. --tag calls ci/release.py, which refuses to create a tag unless every step that ran passed -- including when asked directly, which is why it lives in a program and not in a habit. A tag only moves its final component (v0.1.2 -> v0.1.3).
    • The published version and the tag are one fact. ci/release.py's assert_version_matches_tag refuses to create a tag whose name is not moon.mod's version, and the release gate calls it before the tag exists -- after it, a disagreement could only be recorded as a mistake. ci/publish.py --check --next-tag vX.Y.Z asks the same question from the packaging side.
    • Publishing happens on GitHub, from the tag, with a stored credential. A tag push triggers the publish job and nothing else -- the gates already ran on the branch commit the tag names -- and the workflow a tag push runs is the one in the tagged commit, so the job that publishes is part of what the tag names. The runner restores %USERPROFILE%\.moon\credentials.json from the MOONCAKES_RIANTR_TOKEN secret -- measured to be a JSON object with one key, token, 32 characters -- and accepts either that whole file or the bare token, wrapping the latter into the file's own shape because a credentials.json that is not JSON is not a login, then asks moon whoami and reads the TEXT, because a stale login exits 0 while printing "username is unavailable". It refuses to publish when the tag is not the version moon.mod names. A release that silently did not publish would be worse than a failed one.
    • The same gate runs locally, for the tag. --tag still asks ci/release.py; --publish there refuses without a login rather than skipping, so the two paths cannot disagree about what "published" means.

    python ci\publish.py --check # offline: manifest + metadata python ci\publish.py --check --next-tag v0.1.4 # ...and the version match python ci\ci.py --tiers env,build,gates,qa --tag # tags; the tag push publishes # one-time: give the runner the login state that `moon login` wrote Get-Content -Raw "$env:USERPROFILE\.moon\credentials.json" | gh secret set MOONCAKES_RIANTR_TOKEN

    #Gates

    The host/verify_loop*.mjs scripts are the real test suite. Each is a self-contained run that starts its own man in the middle, drives the client through it, and asserts on transcripts rather than on the client's own report.

    node host\verify_loop103.mjs # 45 checks -- ssh-copy-id, against the real # upstream script and a real Windows sshd node host\verify_loop104.mjs # 23 checks -- the server's terminal: pty-req, # shell, window-change, second-pty disconnect node host\verify_loop94.mjs # 49 checks -- the server, against a real ssh.exe node host\verify_loop102.mjs # 50 checks -- the server's half of `ssh -R`, # against the real sshd AND a real ssh.exe node host\verify_loop93.mjs # 28 checks -- the server: KEX + publickey auth node host\verify_loop54.mjs # 16 checks -- the TOFU write-back lands node host\verify_loop53.mjs # 26 checks -- host certificates, both transports node host\verify_loop52.mjs # 9 checks -- the pin survives the page going away node host\verify_loop47.mjs # 19 checks -- a peer that lies about its host key node host\verify_loop45.mjs # 51 checks -- fault vectors over a real socket node host\verify_loop41.mjs # 29 checks -- byte-identical to a Python oracle node host\verify_loop89.mjs # 81 checks -- the agent, against the REAL ssh-add

    Mutation checks are separate, and are how a gate proves it would have caught something:

    python host\loop54_mutations.py # 3 CAUGHT, 0 MISSED python host\loop53_mutations.py python host\loop89_mutations.py # 23 CAUGHT, 0 MISSED python host\loop102_mutations.py # 12 CAUGHT, 0 MISSED python host\loop103_mutations.py # 8 CAUGHT, 0 MISSED python host\loop104_mutations.py # 6 CAUGHT, 2 MISSED (M5: cmd.exe under # pipes cannot read stdin interactively)

    Never run two browser gates concurrently. Each spawns Chromium, a static server and a WS bridge; one gate's moon clean has deleted another gate's freshly built _build mid-run, and the result reads like a protocol bug.

    #Layout

    src/ sshwire/ kexflow/ kexdh/ kexc25519/ transport and key exchange aes/ chacha/ aead/ hmac/ poly1305/ ciphers and MACs, written here sha256/ sha512/ bigint/ primitives, written here ed25519/ ecdsa/ rsa/ x25519/ key types, written here blowfish/ bcrypt_pbkdf/ the legacy cipher and the key-file KDF hostkey/ host keys, certificates, signatures knownhosts/ known_hosts parsing and pinning rules khfile/ khstore/ where a TOFU write-back goes (native file / browser IndexedDB) sftp/ sftpserver/ sftpdrive/ scp/ SFTP both ends, and scp sshcopyid/ ssh-copy-id: the sentences, the order, the two remote commands bpacket/ channel/ the packet and channel layer sshclient/ wtlive/ the two transport adapters socket/ tcp/ ws_socket/ wt_socket/ byte-stream seams, one per target sshd/ the server: transport, connection protocol subproc/ child processes, three pipes, deadlines userauth/ sshconfig/ agent/ agentserver/ agentpipe/ the SSH agent: protocol, server, pipe keyfile/ keyperm/ the OpenSSH private key file, its perms rng/ the platform CSPRNG, or the refusal cmd/ ssh_live/ the native client sshd/ the server wt_ssh/ the browser client ssh_agent/ ssh_add/ the agent daemon and its client ssh_keygen/ ssh_keyscan/ make, read, rewrite, scan keys ssh_copy_id/ ssh-copy-id scp/ sftp_server/ sftp_mitm/ the scp and SFTP drivers the gates use main/ the 4-target byte-exact demo *_test/ *_kat/ *_probe/ bench_*/ known-answer, property and bench drivers host/ python man in the middle + the gates ci/ the step engine, the tag gate, publish

    #The one rule

    A protocol rule lives in exactly one package, and both transports call it. src/knownhosts decides whether a host key is trusted; src/hostkey decides whether a signature is valid; src/sftp owns framing. src/sshclient and src/wtlive translate error types and nothing else. Two copies of a rule is the duplication this repository has been paying for, repeatedly, and it is the defect every recent loop was written to close.

    #Known gaps

    • The WebTransport path cannot be tested locally: Chromium rejects a locally-added certificate on the QUIC path (QUIC_TLS_CERTIFICATE_UNKNOWN). What is proven is everything above the transport, on a WebSocket carrier.
    • The wasm backends have no entropy source and no async: src/rng reports the failure rather than inventing bytes, and MoonBit's async does not cover wasm. That is why the browser live path (cmd/wt_ssh) is pinned to js. What runs on wasm is the protocol and crypto kernel, byte for byte; the client and the server do not run there.
    • moon check --target native at the repository root is red, correctly: src/ws_socket, src/wt_socket and the cmd/wt_* executables are js/wasm-only live packages. Check per package.
    • The pkg.generated.mbti files in this repository are stale and are not an API surface. Read the .mbt sources in .mooncakes.
    • host/main.wasm is a checked-in build artefact of cmd/main, not a source file. Regenerate it rather than editing it:

      cmd /c "moon build --target wasm-gc --release cmd\main" # then copy _build\wasm-gc\release\build\cmd\main\main.wasm to host\main.wasm node host\browser_test.mjs # asserts its output byte for byte

      It carries the module name in the name custom section behind a length prefix, so a search-and-replace inside it corrupts the file in a way WebAssembly.validate only sometimes reports: the renamed module riantr/moonbit_openssh is one byte shorter than the old one, and the stale prefix then reads one byte past the end of the string.
    • Concurrent writers of one known_hosts can lose an entry; there is no lock.
    • ssh-copy-id refuses -s (install over sftp) with a sentence of its own: this build has no sftp install path. It also honours only three of the -o options (UserKnownHostsFile=, StrictHostKeyChecking=, Port=); the rest are echoed in the closing sentence, as upstream prints them, and ignored.
    • A certificate chain longer than one CA is not followed.
    • The server carries one remote port forward at a time, and one connection on it at a time. A second tcpip-forward on the same connection is refused (the real server allows several), and the address it listens on is chosen by the driver rather than by GatewayPorts.