ddn.net.ssh.wire

SSH wire-format codec (RFC 4251 section 5).

Bounded reader and writer for the SSH data types: byte, boolean, uint32, uint64, string, name-list, and mpint. Every read is bounds checked against the remaining input and against explicit limits before any slicing happens, so malformed or hostile input can only produce SshWireException, never unchecked growth or crashes.

The reader returns slices into the source buffer (no copies), which lets callers preserve the exact received bytes for transcript hashing during key exchange; callers must not outlive the buffer.

The codec has no socket or session dependencies and is fully usable from plain unit tests.

Types 4

classSshWireException : Exception

Raised when wire data is malformed, truncated, non-canonical, or exceeds an explicit parser limit.

Constructors
this(string msg, string file = __FILE__, size_t line = __LINE__)Constructs a wire-format exception.

Parser limits enforced before any allocation or slicing.

Defaults follow the resource-limit inventory of the support matrix; specific readers may pass tighter or wider explicit limits.

stringMax = 32768Default maximum string length in bytes
nameListMax = 128Maximum items in a name-list
mpintMax = 2048Maximum mpint payload length in bytes
algorithmNameMax = 64Maximum characters per algorithm name (RFC 4251 section 6)

Sequential reader over an SSH wire-format buffer.

All read methods consume from the current position and throw SshWireException on truncation, non-canonical encodings, or limit violations. Returned strings are slices into the input.

Fields
private const(ubyte)[] buf_
private size_t pos_
Methods
size_t remaining() @property const @safe pure nothrow @nogcReturns: number of unread bytes.
bool atEnd() @property const @safe pure nothrow @nogcReturns: whether the buffer is fully consumed.
void requireFullyConsumed(string context = "message") @safeAsserts that the whole buffer was consumed.
ubyte readByte() @safeReads one byte.
bool readBoolean() @safeReads a boolean. Any non-zero byte reads as true (RFC 4251 section 5); the writer never emits values other than 0 and 1.
uint readUint32() @safeReads a uint32 in network byte order.
ulong readUint64() @safeReads a uint64 in network byte order.
const(ubyte)[] readString(size_t maxLength = SshWireLimits.stringMax) @safeReads a length-prefixed string as a slice into the source buffer; no copy is made so exact received bytes stay available for transcript hashing.
string readText(size_t maxLength = SshWireLimits.stringMax) @safeReads a length-prefixed string and converts it to text.
string[] readNameList(size_t maxEntries = SshWireLimits.nameListMax) @safeReads a name-list: a string of comma-separated, non-empty, printable ASCII names.
const(ubyte)[] readMpint(size_t maxBytes = SshWireLimits.mpintMax) @safeReads an mpint in its canonical two's-complement form and validates RFC 4251 section 5 encoding rules.
private void need(size_t n, string what) @safe
private size_t checkedLength(string what, size_t maxLength) @safe
private void validateAlgorithmName(string name) static @safe
Constructors
this(const(ubyte)[] data)Constructs a reader over the given buffer.

Growable writer for SSH wire-format values.

Writes enforce the same limits as the reader and emit only canonical encodings (booleans as 0/1, minimal mpints with sign padding).

Fields
private ubyte[] buf_
Methods
void putByte(ubyte b) @safe pure nothrowWrites one byte.
void putBoolean(bool b) @safe pure nothrowWrites a boolean as 0 or 1 (RFC 4251 section 5).
void putUint32(uint v) @safe pure nothrowWrites a uint32 in network byte order.
void putUint64(ulong v) @safe pure nothrowWrites a uint64 in network byte order.
void putString(const(ubyte)[] data, size_t maxLength = SshWireLimits.stringMax) @safeWrites a length-prefixed string.
void putRaw(scope const(ubyte)[] data) @safe pure nothrowAppends raw bytes without a length prefix.
void putString(string text, size_t maxLength = SshWireLimits.stringMax) @safeWrites a length-prefixed string from text.
void putNameList(scope const string[] names, size_t maxEntries = SshWireLimits.nameListMax) @safeWrites a name-list of non-empty, printable ASCII names.
void putMpint(const(ubyte)[] magnitude) @safeWrites an mpint in canonical form from a big-endian magnitude.
ubyte[] data() @safe pure nothrowReturns: the accumulated bytes, leaving the writer empty.
const(ubyte)[] peek() @property const @safe pure nothrowReturns: a copy of the accumulated bytes.
void clear() @safe pure nothrowResets the writer.

Functions 1

private fnstring wireToText(const(ubyte)[] data) pure @trustedCopies wire bytes as text without pulling the cast into safe code.