Documentation
¶
Overview ¶
Package wire reads and writes the parts of the SSH transport protocol (RFC 4253) a scanner needs before any key is derived: the identification line, the unencrypted binary packets, KEXINIT, the key-exchange init and reply messages, public-key and signature blobs and OpenSSH certificates. Everything the server sends is bounded and parsed defensively: a hostile server is the normal case, not the exception. It is pure (no network, no clock) and fuzzed.
Index ¶
- Constants
- Variables
- func Clean(s string, max int) string
- func Fingerprint(blob []byte) string
- func MPIntBytes(v *big.Int) []byte
- func Packet(payload []byte, rnd io.Reader) ([]byte, error)
- func ReadPacket(r io.Reader) ([]byte, error)
- type Builder
- type Certificate
- type Disconnect
- type Identification
- type KexInit
- type KexReply
- type PublicKey
- type Reader
- func (r *Reader) Bool() bool
- func (r *Reader) Byte() byte
- func (r *Reader) Bytes(n int) []byte
- func (r *Reader) Err() error
- func (r *Reader) MPInt() *big.Int
- func (r *Reader) NameList() []string
- func (r *Reader) Rest() []byte
- func (r *Reader) String() []byte
- func (r *Reader) Uint32() uint32
- func (r *Reader) Uint64() uint64
- type Verified
Constants ¶
const ( // MaxIdentLine is RFC 4253 section 4.2's limit, CR LF included. MaxIdentLine = 255 // MaxPreambleLines and MaxPreambleBytes bound the lines a server may send before its // identification (RFC 4253 section 4.2 allows such lines; nothing bounds them). MaxPreambleLines = 20 MaxPreambleBytes = 8 << 10 // MaxPacket is the packet_length every implementation must accept (RFC 4253 section 6.1); // nothing longer is read before encryption starts. MaxPacket = 35000 // MaxNames caps a name-list; MaxName caps one name (RFC 4251 section 6 allows 64 characters). MaxNames = 128 MaxName = 64 )
Limits on what is read from a server.
const ( MsgDisconnect = 1 MsgIgnore = 2 MsgUnimpl = 3 MsgDebug = 4 MsgKexInit = 20 MsgNewKeys = 21 MsgKexInit30 = 30 // KEXDH_INIT, KEX_ECDH_INIT, KEX_HYBRID_INIT MsgKexReply31 = 31 // KEXDH_REPLY, KEX_ECDH_REPLY, KEX_HYBRID_REPLY )
Message numbers (RFC 4250 section 4.1.2; 30/31 are shared by every key-exchange method).
const ( MaxHostKeyBlob = 16 << 10 MaxSignatureBlob = 16 << 10 )
MaxHostKeyBlob caps a host key or certificate blob, and MaxSignatureBlob a signature: generous for ML-DSA-87 composites (about 2.6 KB of key, 4.9 KB of signature) and certificates.
Variables ¶
var ErrProtocol = errors.New("ssh protocol error")
ErrProtocol marks input that breaks the protocol (as opposed to an I/O error).
Functions ¶
func Clean ¶
Clean replaces control and non-printable characters and caps the length: what a server sends is never shown or stored raw.
func Fingerprint ¶
Fingerprint is OpenSSH's SHA-256 fingerprint of a key blob.
func MPIntBytes ¶
MPIntBytes is the body of a non-negative mpint.
func Packet ¶
Packet frames a payload as an unencrypted binary packet with random padding to a multiple of 8.
func ReadPacket ¶
ReadPacket reads one unencrypted binary packet (RFC 4253 section 6) and returns its payload.
Types ¶
type Builder ¶
type Builder struct{ B []byte }
Builder appends SSH data types.
func (*Builder) MPInt ¶
MPInt appends a non-negative mpint (RFC 4251 section 5): a leading zero byte when the top bit is set.
type Certificate ¶
type Certificate struct {
KeyType string `json:"keyType"`
Serial uint64 `json:"serial"`
CertType uint32 `json:"certType"` // 1 user, 2 host
KeyID string `json:"keyId,omitempty"`
Principals []string `json:"principals,omitempty"`
ValidAfter time.Time `json:"validAfter"`
ValidBefore time.Time `json:"validBefore"`
Forever bool `json:"forever,omitempty"`
CAType string `json:"caType"`
CAFingerprint string `json:"caFingerprint"`
}
Certificate is the part of an OpenSSH certificate (PROTOCOL.certkeys) a report shows.
type Disconnect ¶
Disconnect is an SSH_MSG_DISCONNECT.
func ParseDisconnect ¶
func ParseDisconnect(p []byte) Disconnect
ParseDisconnect reads message 1, best effort.
type Identification ¶
type Identification struct {
Raw string // the line without CR LF, cleaned
ProtoVersion string // "2.0", "1.99" (SSH-1 and SSH-2), "1.5" ...
Software string
Comment string
Preamble int // lines the server sent before it
}
Identification is the server's identification line (RFC 4253 section 4.2): "SSH-protoversion-softwareversion SP comments".
func ParseIdentification ¶
func ParseIdentification(line string) (Identification, error)
ParseIdentification splits an identification line.
func ReadIdentification ¶
func ReadIdentification(r *bufio.Reader) (Identification, []string, error)
ReadIdentification reads lines until one starts with "SSH-". It refuses more than MaxPreambleLines lines or MaxPreambleBytes bytes before it, and an identification line longer than MaxIdentLine.
func (Identification) SSH2 ¶
func (id Identification) SSH2() bool
SSH2 reports whether the server speaks SSH-2 ("2.0", or "1.99" which means both).
type KexInit ¶
type KexInit struct {
Cookie [16]byte
Kex, HostKey []string
CipherC2S, CipherS2C []string
MACC2S, MACS2C []string
CompressionC2S, CompS2C []string
LanguageC2S, LanguageS2C []string
FirstKexFollows bool
}
KexInit is an SSH_MSG_KEXINIT.
func ParseKexInit ¶
ParseKexInit reads a KEXINIT payload (message number included).
type KexReply ¶
type KexReply struct {
HostKey []byte
Value []byte // Q_S, or the body of the mpint f
Signature []byte
}
KexReply is the server's SSH_MSG_KEX_*_REPLY: its host key, its key-exchange value (Q_S for ECDH and the hybrids, f for finite-field Diffie-Hellman, kept raw) and its signature.
func ParseKexReply ¶
ParseKexReply reads message 31 (message number included).
type PublicKey ¶
type PublicKey struct {
Type string // the blob's own type name: "ssh-ed25519", "ecdsa-sha2-nistp256", "ssh-mldsa44-ed25519", "ssh-ed25519-cert-v01@openssh.com"
Algorithm string // the key's family in the catalogue's words: "Ed25519", "ECDSA", "RSA", "DSA", "ML-DSA-65", "ML-DSA-44+Ed25519"
Bits int
Curve string // "P-256" ...
PQ bool
// Fingerprint is OpenSSH's: "SHA256:" and the unpadded base64 of SHA-256 over the blob.
Fingerprint string
Cert *Certificate
// contains filtered or unexported fields
}
PublicKey is a parsed SSH public key or certificate blob.
func ParsePublicKey ¶
ParsePublicKey reads a public key or OpenSSH certificate blob. An unknown type is not an error: it is returned with its name and fingerprint, unverifiable.
type Reader ¶
type Reader struct {
// contains filtered or unexported fields
}
Reader consumes SSH data types from a buffer. Every read is bounded by what is left; the first failure sticks, so a parser checks Err once at the end.
func (*Reader) MPInt ¶
MPInt reads an mpint as a non-negative integer (a negative one is a protocol error here: no value this package reads may be negative).
func (*Reader) NameList ¶
NameList reads a name-list: comma separated, each name cleaned and capped, at most MaxNames.