# TOSV Standalone SFTP Server: Integration Requirements - Version: 0.2.0 (round 2: SFTP IDs corrected, KEX plus chacha constructions pinned, strict-kex required) - Status: Draft for owner implementation - Date: 2026-10-31 - Owner: retoor (implements); this document is the contract ## 0. Control ### 0.1 Owner decisions (settled, not re-asked) | # | Decision | |---|----------| | OD-01 | Transport: standalone SSH daemon. The owner implements the SSH transport and the SFTP subsystem. No wolfSSH, no libssh, no OpenSSH code linkage. | | OD-02 | Home: this repo, same discipline. Second single-file C++17 program plus one shared include, static libsodium only, same CI gates. | | OD-03 | Auth: SSH layer authenticates a gateway identity; SFTP paths carry the grant; a second in-band credential check binds the two. | ### 0.2 Normative IDs - SF-NN: requirements on the new server (this document defines SF-01 onward, never reused once retired). - INT-NN: requirements on existing tosv.cpp and repo files, needed for integration. - Every SF and INT below traces to a tosv.cpp line or a SPEC.md ID in section 12. No open gaps: each would-be gap is a decided default with rationale inline. ### 0.3 Scope effect on the spec (process, not optional) Adopting this work retires SPEC NG-01, D-06, and PIN-10 (SFTP excluded), and amends NFR-01 and PIN-03 (single file) to "one file per binary plus one shared vault.inc". ADR-0008 records the retirement with rationale before any merge, then SPEC bumps to 0.4.0 in the same commit, per DOC-01 and the ADR-before-merge register rule. ### 0.4 Non-goals in v1 (decided, not deferred) - No SSH shell, exec, PTY, port forwarding, X11, agent, or other subsystem. SFTP only, always. - No SSH algorithms beyond section 1. No RSA, ECDSA, FFDH groups, GSSAPI, or certs. - No second crypto format. Byte-identical v2 blobs, section 5. - No separate credential store. The DB stays canonical, section 2. - No Windows or macOS target. Linux x86_64 only, same as tosv. ## 1. SSH transport subset The server implements exactly this profile, negotiates nothing else, and fails closed (DISCONNECT or CHANNEL_FAILURE, no fallback) on any peer proposal outside it. Rationale: libsodium provides every primitive below and nothing above; one algorithm per slot keeps the audit surface minimal; stock OpenSSH clients speak all of it. | Slot | REQUIRED value | libsodium primitive | |------|---------------|---------------------| | KEX | mlkem768x25519-sha256 (preferred, SF-61), curve25519-sha256, plus curve25519-sha256@libssh.org alias | mlkem-native (ML-KEM-768) plus crypto_scalarmult | | KEX strict | REQUIRED both directions, disconnect peers that omit it, see SF-58 | sequence reset plus disconnect | | Host key | ssh-ed25519 only | crypto_sign | | Cipher both directions | chacha20-poly1305@openssh.com only | crypto_stream_chacha20_ietf_xor_ic plus crypto_onetimeauth, see SF-60 (NOT the RFC 7539 AEAD: OpenSSH uses a custom two-key layout) | | MAC | negotiate minimal list, then ignore (AEAD integrates auth); see SF-52 | none computed, ever | | Compression | none only | n/a | | Userauth | publickey with ssh-ed25519 keys only | crypto_sign_verify_detached | | Channel | session, then one `subsystem` request with name `sftp` | n/a | | Service | ssh-userauth, then ssh-connection | n/a | - SF-01: version string `SSH-2.0-TOSV_` with VERSION from the repo VERSION file. No OS, library, or patchlevel text. - SF-02: host key file `sftp_host_ed25519` in BASE (same dir as .env.json). Generated once at first start if missing, 0600, Ed25519, never overwritten, never logged. The .pub companion may exist for operator convenience and carries no authority. - SF-03: maximum SSH packet 262144 bytes on receipt; beyond that DISCONNECT with reason, no reply. RFC 4253 section 6.1 only requires 35000; we accept up to 262144 for stock-client parity. All KEXINIT names, strings, and cookies are length-checked before any alloc. - SF-04: rekey after 2^30 bytes in either direction or 3600 seconds, whichever comes first. Failed rekey disconnects. Rekey state machine tolerates exactly one in-flight KEX. - SF-05: every rejected channel request (shell, exec, pty-req, env, tcpip-forward, direct-tcpip, x11, auth-agent-req, subsystems other than sftp, global requests) gets CHANNEL_FAILURE or REQUEST_FAILURE, plus audit line `sftp-ssh-reject `, plus no retry path. Shell and exec are never implemented, not merely disabled. - SF-06: DEBUG and DISCONNECT messages carry generic text only (parity with UX-03: denied, blocked, too early, expired, missing, failure). Never echo key material, paths, or grant ids in disconnect text. - SF-07: handshake timeouts: 30 seconds to complete KEX (parity with the 30s recv timeout in tosv.cpp main), 120 seconds to first authenticated channel request, then idle 300 seconds per connection. Timeout closes, no wipe (parity with D-04). - SF-08: loopback default bind, port argv[1] default 2222 (parity with OPS-01 shape). AF_INET 127.0.0.1 only, no v6 listen in v1 (parity with the tosv.cpp bind call). DTAP uses separate ports plus data dirs with zero shared state (parity with OPS-05). ### 1B. SSH channels, packets, and crypto construction - SF-49: exactly one session channel per connection; a second CHANNEL_OPEN gets OPEN_FAILURE administratively prohibited (decided: one channel keeps audit attribution trivial). Non-session channel types get OPEN_FAILURE unknown type. Initial window 2097152 bytes, maximum packet 262144, WINDOW_ADJUST top-up sent eagerly before the window halves. CHANNEL_DATA beyond the window is a protocol error: disconnect, no reply. - SF-50: strict RFC 4251 type codec on both directions: every string, mpint, and name-list length is bounds-checked before alloc; short reads give disconnect pre-auth and CHANNEL_FAILURE post-auth, never an over-alloc. Client version line max 255 bytes or drop. KEXINIT cookie is 16 libsodium random bytes; first-kext-packet-follows is always ignored (never act on a guess). Service requests accept `ssh-userauth` then `ssh-connection` only. CHANNEL_REQUEST honors want-reply (reply only when true). No USERAUTH_BANNER is ever sent. Partial success (value 1 in USERAUTH_FAILURE) is never sent. Rekey pauses channel data until NEWKEYS both directions per RFC 4253. - SF-51: the chacha20-poly1305@openssh.com construction follows the OpenSSH implementation (cipher-chachapoly.c plus packet.c, filed as T1 in Appendix C) exactly, as pinned in SF-60. Verification is the stock-client interop test (SF-39) plus a captured-session decrypt check in tests: no hand-waving, the client either talks or the gate is red. - SF-52: MAC negotiation offers `hmac-sha2-256` only and accepts the peer overlap normally, then never computes or checks any MAC while the AEAD cipher is active. If a future cipher ever needs a MAC, that cipher ships its own ADR; in v1 the agreed name is agreed-and-ignored, which matches OpenSSH behavior (packet.c sets mac to NULL when cipher_authlen is nonzero) and is proven by interop. - SF-58: strict key exchange is REQUIRED, not preferred. The server offers kex-strict-s-v00@openssh.com and disconnects (code 2 PROTOCOL_ERROR, audit `sftp-ssh-reject kex-strict`) any peer whose KEXINIT omits kex-strict-c-v00@openssh.com. Rationale: our only cipher is chacha20-poly1305, which the Terrapin researchers name as affected by prefix-truncation when strict kex is absent; a vault takes no downgrade-by-truncation risk. Compatibility floor: stock OpenSSH 9.6 and later, verified by SF-39 negotiation against the local client. - SF-59: curve25519-sha256 key exchange (RFC 8731 over RFC 5656 section 4): messages SSH_MSG_KEX_ECDH_INIT 30 (client ephemeral key Q_C as SSH string) and SSH_MSG_KEX_ECDH_REPLY 31 (K_S host key blob string, Q_S server ephemeral key string, signature over H). Exchange hash H is SHA-256 over string V_C, string V_S (CR LF excluded), string I_C, string I_S (raw KEXINIT payloads), string K_S, string Q_C, string Q_S, mpint K. K is encoded as mpint per RFC 4251: length-prefixed, with a leading zero byte when the top bit is set (32-byte secrets with top bit set encode as 33 bytes). Abort rules: peer key not exactly 32 bytes, or shared secret all-zero, or libsodium reports failure, gives immediate disconnect with code 3 KEY_EXCHANGE_FAILED and audit `sftp-kex-fail`. Host signature uses ssh-ed25519 per RFC 8709; the K_S blob and signature blob encodings follow that RFC and are proven by interop. - SF-61: mlkem768x25519-sha256 hybrid key exchange (draft-ietf-sshm-mlkem-hybrid-kex, FIPS 203): same messages 30 and 31 as SF-59. Q_C is one string of exactly 1216 bytes: ML-KEM-768 encapsulation key (1184) then X25519 public key (32). Q_S is one string of exactly 1120 bytes: ML-KEM ciphertext (1088) then server X25519 public key (32). Shared secret K is SHA-256 over ML-KEM shared secret (32) then X25519 shared secret (32), and K is encoded as a plain SSH string (not mpint) in H and in every KDF input. Negotiation: the first client KEX entry the server also offers wins; server offers mlkem768x25519-sha256, curve25519-sha256, curve25519-sha256@libssh.org. Abort rules as SF-59 plus: Q_C length wrong, ML-KEM encapsulation key rejected by the modulus check, or X25519 secret all-zero gives disconnect code 3 and audit `sftp-kex-fail`. Audit `sftp-kex mlkem768x25519` or `sftp-kex curve25519` on the first exchange, `sftp-rekey pq` on PQ rekey. Library: mlkem-native v2.0.0 pinned (ADR 0009). Proof: OpenSSH 10.2 negotiates it with host signature verified, Gate 2 pq-only and classic fallback asserts. - SF-63: channel EOF from the client ends the subsystem: the server replies EOF then CHANNEL_CLOSE (RFC 4254 section 5.3) and keeps serving until the peer CLOSE or disconnect (dropping the TCP connection at once resets unread data and truncates transfers, seen in gate2). Before 0.5.3 it replied EOF only, so OpenSSH 10.2 sftp -b hung forever after any failed batch command (found 2026-11-05, reproduced on 2bb51c0, deterministic). - SF-62: PQ-only by default: only mlkem768x25519-sha256 (plus the strict-kex marker) is offered and accepted; TOSV_SFTP_ALLOW_CLASSICAL=1 (exactly the string 1) re-adds curve25519-sha256 and its libssh alias. Clients without ML-KEM get "no matching algorithm". ADR 0011 - SF-60: packet protection construction (from cipher-chachapoly.c): per direction, 64 bytes of key material from the RFC 4253 section 7.2 KDF with SHA-256, letters C (client to server) or D (server to client), extended K1 plus K2 to 64 bytes; first 32 bytes are the main key, last 32 the header key. Letters A, B, E, F are never derived (no IVs, no MAC; documented, safe: KDF outputs are independent). Per packet with sequence number N as 8-byte big-endian: Poly1305 one-time key is ChaCha20 main-key block at counter 0; the 4 length bytes are encrypted with the header key at counter 0; the payload is encrypted with the main key at counter 1; the 16-byte tag is Poly1305 over ciphertext (length plus payload) and is verified BEFORE any decryption. libsodium mapping: crypto_stream_chacha20_ietf_xor_ic with 12-byte nonce 00000000 plus N and initial counters 0 or 1, plus crypto_onetimeauth; the 8-byte wire nonce maps to the low 8 bytes of the IETF nonce. Any deviation fails interop, so the Gate is the proof. ## 2. Authentication and authorization Two layers, both mandatory, in this order. Rationale: OD-03 splits transport identity (SSH key, long-lived, operator-managed) from grant rights (DB rows, short windows, per-fetcher), so key rotation never touches grants and grant expiry never touches keys. ### 2.1 SSH layer: gateway identity - SF-09: userauth `none` is always answered with failure listing `publickey` only (method discovery, never access). `password`, `keyboard-interactive`, `hostbased` are answered with failure and audited as `sftp-auth-reject `. Only `publickey` with `ssh-ed25519` blob type proceeds to signature check. - SF-10: gateway keys file `sftp_gateway_keys` in BASE, 0600, one entry per line: `ssh-ed25519