wire

package
v0.6.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 9, 2026 License: Apache-2.0 Imports: 20 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
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.

View Source
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).

View Source
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

View Source
var ErrProtocol = errors.New("ssh protocol error")

ErrProtocol marks input that breaks the protocol (as opposed to an I/O error).

Functions

func Clean

func Clean(s string, max int) string

Clean replaces control and non-printable characters and caps the length: what a server sends is never shown or stored raw.

func Fingerprint

func Fingerprint(blob []byte) string

Fingerprint is OpenSSH's SHA-256 fingerprint of a key blob.

func MPIntBytes

func MPIntBytes(v *big.Int) []byte

MPIntBytes is the body of a non-negative mpint.

func Packet

func Packet(payload []byte, rnd io.Reader) ([]byte, error)

Packet frames a payload as an unencrypted binary packet with random padding to a multiple of 8.

func ReadPacket

func ReadPacket(r io.Reader) ([]byte, error)

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) Bool

func (w *Builder) Bool(v bool)

Bool appends a boolean.

func (*Builder) Byte

func (w *Builder) Byte(v byte)

Byte appends a byte.

func (*Builder) MPInt

func (w *Builder) MPInt(v *big.Int)

MPInt appends a non-negative mpint (RFC 4251 section 5): a leading zero byte when the top bit is set.

func (*Builder) NameList

func (w *Builder) NameList(names []string)

NameList appends a name-list.

func (*Builder) String

func (w *Builder) String(v []byte)

String appends a string.

func (*Builder) Uint32

func (w *Builder) Uint32(v uint32)

Uint32 appends a uint32.

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

type Disconnect struct {
	Reason      uint32
	Description string
}

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

func ParseKexInit(p []byte) (*KexInit, error)

ParseKexInit reads a KEXINIT payload (message number included).

func (*KexInit) Marshal

func (k *KexInit) Marshal() []byte

Marshal encodes the KEXINIT payload.

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

func ParseKexReply(p []byte) (*KexReply, error)

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

func ParsePublicKey(blob []byte) (*PublicKey, error)

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.

func (*PublicKey) Blob

func (k *PublicKey) Blob() []byte

Blob is the key exactly as the server sent it.

func (*PublicKey) Verify

func (k *PublicKey) Verify(sigAlg string, h, sigBlob []byte) Verified

Verify checks the server's signature blob (string algorithm, string signature) over the exchange hash h. sigAlg is the host key algorithm negotiated (it decides the hash for RSA).

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 NewReader

func NewReader(b []byte) *Reader

NewReader reads from b.

func (*Reader) Bool

func (r *Reader) Bool() bool

Bool reads a boolean.

func (*Reader) Byte

func (r *Reader) Byte() byte

Byte reads one byte.

func (*Reader) Bytes

func (r *Reader) Bytes(n int) []byte

Bytes reads n raw bytes.

func (*Reader) Err

func (r *Reader) Err() error

Err is the first failure, or nil.

func (*Reader) MPInt

func (r *Reader) MPInt() *big.Int

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

func (r *Reader) NameList() []string

NameList reads a name-list: comma separated, each name cleaned and capped, at most MaxNames.

func (*Reader) Rest

func (r *Reader) Rest() []byte

Rest is what has not been read.

func (*Reader) String

func (r *Reader) String() []byte

String reads a string (uint32 length, then the bytes).

func (*Reader) Uint32

func (r *Reader) Uint32() uint32

Uint32 reads a uint32.

func (*Reader) Uint64

func (r *Reader) Uint64() uint64

Uint64 reads a uint64.

type Verified

type Verified struct {
	OK   bool
	Note string
}

Verified says whether a signature over the exchange hash checked out, and, when it did not or could not, why.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL