/* scorpiox-scorpioplayer-ssh-transport-client — SSH-2 client transport in pure C. * * Connects to an SSH-2 server (row 48 spsshs, OpenSSH, dropbear): KEX * curve25519-sha256 (+@libssh.org) / diffie-hellman-group14-sha256 / -group16-sha512, * host-key verify ssh-ed25519 + rsa-sha2-256/512 with **trust-on-first-use pinning** * (SHA256 fingerprint in, mismatch = hard error), ciphers chacha20-poly1305@openssh.com, * aes{128,256}-gcm@openssh.com, aes{128,256}-ctr + hmac-sha2-256(-etm); strict KEX * (Terrapin, required); auth none(guest) / password / publickey (OpenSSH private key: * unencrypted or bcrypt-pbkdf-encrypted) / keyboard-interactive answered with the password. * Session channels: several concurrent, "subsystem" (duplex handle for row 41) and "exec" * (stdout/stderr/exit-status/exit-signal, streaming + timeout + output cap). Keep-alive * and a reconnect helper. The client is READ-ONLY: it never issues a write/delete/rename * SFTP operation (row 41 is the file layer). * * One client per process (singleton API). Functions return 0 on success and a negative * value on error unless documented otherwise; spsshc_last_error() explains it. Returned * const char* point to static storage valid until the next call on this thread's API use; * copy them immediately. See SPEC.md. */ #ifndef SPSSHC_H #define SPSSHC_H #include #include #ifdef __cplusplus extern "C" { #endif #if defined(SPSSHC_STATIC) # define SPSSHC_API #elif defined(_WIN32) # ifdef SPSSHC_BUILD # define SPSSHC_API __declspec(dllexport) # else # define SPSSHC_API __declspec(dllimport) # endif #else # define SPSSHC_API __attribute__((visibility("default"))) #endif /* ---- error codes (the error map, SPEC §5) ---- */ #define SPSSHC_OK 0 #define SPSSHC_E_ARG -1 /* bad argument */ #define SPSSHC_E_STATE -2 /* wrong state (not connected / already connected) */ #define SPSSHC_E_IO -3 /* socket / timeout / closed by peer */ #define SPSSHC_E_HOSTKEY -4 /* host key refused: wrong key type or PIN MISMATCH */ #define SPSSHC_E_AUTH -5 /* authentication failed (all methods refused) */ #define SPSSHC_E_PROTOCOL -6 /* peer sent something off-protocol */ #define SPSSHC_E_LIMIT -7 /* output cap / command too long / table full */ #define SPSSHC_E_KEY -8 /* private key could not be parsed / passphrase wrong */ #define SPSSHC_E_CRYPTO -9 /* self-test failure */ #define SPSSHC_E_TIMEOUT -10 /* operation timed out (exec / read) */ /* exec result markers (the exit status is 0..255; these are outside) */ #define SPSSHC_EXEC_KILLED 124 /* the client timeout killed the wait */ #define SPSSHC_EXEC_CAP 125 /* output cap reached */ #define SPSSHC_EXEC_SIGNALED 127 /* peer signalled instead of exit-status (no status) */ /* spsshc_exec_argv quoting error (the argv element itself was refused) */ /* test/fuzz entry: parse an unarmoured openssh-key-v1 blob as either key type */ int key_test_parse(const uint8_t *d, size_t n); /* selftest reports its first failure through the shared last_error buffer */ void sp_selftest_set_err(const char *msg); /* selftest reports its first failure through the shared last_error buffer */ #define SPSSHC_EXEC_ARGV_NUL 128 /* an argv element was NULL or contained NUL */ #define SPSSHC_EXEC_ARGV_LONG 129 /* quoted command would exceed the 4096 B cap */ /* ---- lifecycle ---- */ SPSSHC_API int spsshc_version(int *major, int *minor, int *patch); /* 0 = filled */ SPSSHC_API const char *spsshc_version_string(void); /* "1.0.0" */ SPSSHC_API int spsshc_selftest(void); /* vendored + in-tree crypto KATs; 0 = pass */ /* ---- connect ---- * host: numeric IPv4/IPv6 or a name resolved by the platform. port 1..65535. * pinned_fp: "SHA256:base64" the caller stored from a previous connect, NULL = trust on * first use (the returned fingerprint is then stored by the caller). Empty string * behaves as NULL. A mismatch is a hard SPSSHC_E_HOSTKEY (HOSTKEY_CHANGED). * timeout_ms: connect + banner + KEX budget in milliseconds (> 0). * Returns 0 connected (KEX done), or <0 with spsshc_last_error() and * spsshc_host_fingerprint() still reporting the peer's key for TOFU storage. */ SPSSHC_API int spsshc_connect(const char *host, int port, const char *pinned_fp, int timeout_ms); /* The peer host key fingerprint, "SHA256:..." (available after connect, even on a * mismatch, and after a refused key type). "" before the first connect. */ SPSSHC_API const char *spsshc_host_fingerprint(void); /* The negotiated algorithms after connect: kex / hostkey / cipher-c2s / cipher-s2c / * mac-c2s / mac-s2c, comma-separated, "" before. Written for the interop report. */ SPSSHC_API const char *spsshc_negotiated(void); SPSSHC_API int spsshc_is_connected(void); /* Same host/port/pin again after a drop: connect + auth_password with the stored user. */ SPSSHC_API int spsshc_reconnect(int timeout_ms); SPSSHC_API void spsshc_disconnect(void); /* ---- auth (in this order; each returns 0 ok, SPSSHC_E_AUTH refused) ---- */ SPSSHC_API int spsshc_auth_password(const char *user, const char *password); /* privkey: OpenSSH "-----BEGIN OPENSSH PRIVATE KEY-----" text (PEM armour), unencrypted * or bcrypt-pbkdf-encrypted with passphrase. passphrase may be NULL/"" for none. */ SPSSHC_API int spsshc_auth_key(const char *user, const char *privkey_pem, const char *passphrase); /* keyboard-interactive answered with `answer` for every prompt (many NAS need this). */ SPSSHC_API int spsshc_auth_keyboard_interactive(const char *user, const char *answer); /* Test-only hooks (not part of the product surface; used by unit tests + fuzzers): * skip_kex pretends KEX + auth are complete in the "none" cipher state; set_keys * installs symmetric cipher keys for the decrypting-framer fuzz target. */ SPSSHC_API void spsshc_test_skip_kex(int authed, const char *user); SPSSHC_API void spsshc_test_set_keys(int cipher, int mac, const unsigned char seed[64]); /* ---- session channels ---- * Several concurrent channels on one connection (SPEC D6). A channel id > 0 is returned; * negative on error. Channel reads respect the peer window; writes our own. */ SPSSHC_API int spsshc_channel_open(void); /* a bare "session" channel */ SPSSHC_API int spsshc_subsystem_open(const char *name); /* opens + starts a subsystem */ /* write: returns bytes accepted, <0 error. read: returns bytes (0 = EOF), <0 error, * waits up to timeout_ms for data. */ SPSSHC_API int spsshc_channel_write(int ch, const void *buf, int len); SPSSHC_API int spsshc_channel_read(int ch, void *buf, int len, int timeout_ms); SPSSHC_API void spsshc_channel_close(int ch); /* ---- exec ---- * exec(cmd): the command string is sent AS-IS (never built by concatenation here). * exec_argv(argv, argc): POSIX single-quote quoting of every element, then one exec — * the safe path for caller-supplied arguments. Returns the exit status (0..255), or * SPSSHC_EXEC_* / SPSSHC_E_* (all <0 except the two sentinel values above, which are * positive by convention with 124/125 the server's own markers). */ SPSSHC_API int spsshc_exec(int ch, const char *cmd, int timeout_ms, int max_out); SPSSHC_API int spsshc_exec_argv(int ch, const char *const *argv, int argc, int timeout_ms, int max_out); /* Results of the last exec on any channel (static buffers, copy immediately). */ SPSSHC_API const char *spsshc_exec_stdout(void); SPSSHC_API const char *spsshc_exec_stderr(void); /* The quoting itself, exposed for the tests and for callers that want to inspect it: * quotes one argv element for a POSIX shell into out (cap bytes). 0 ok. */ SPSSHC_API int spsshc_exec_argv_quote(const char *arg, char *out, size_t cap); /* ---- keep-alive + tuning ---- */ SPSSHC_API int spsshc_keepalive(int interval_s); /* 0 = off; >0 = send keepalive@openssh.com */ SPSSHC_API int spsshc_set_timeouts(int connect_ms, int read_ms, int rekey_ms); /* Read-only assertion helper for the test suite: 1 = the engine has no code path that * issues an SFTP write/delete/rename (the op table is read-only by construction). */ SPSSHC_API int spsshc_read_only(void); /* ---- errors ---- */ SPSSHC_API const char *spsshc_last_error(void); #ifdef __cplusplus } #endif #endif