hostkeys

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Sep 25, 2026 License: GPL-3.0, LGPL-3.0 Imports: 16 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 hostkeys reads, writes and collects SSH host keys.

The host key file is the site's trust anchor: it is kept under version control and every connection is checked against it. Editing it with ssh-keygen -R and appending with ssh-keyscan, as the shell tools did, is neither atomic nor locked, so two administrators refreshing at once could lose an entry. Everything here goes through one locked, atomic update.

Index

Constants

View Source
const (
	// MarkerRevoked says the key on the line must never be accepted.
	MarkerRevoked = "@revoked"
	// MarkerCertAuthority says the key on the line signs host certificates;
	// it is not the key of a host.
	MarkerCertAuthority = "@cert-authority"
)

Markers a known_hosts line may start with.

Variables

DefaultAlgorithms are the host key algorithms collected for a host, best first. Ed25519 is preferred; RSA is kept for service processors and older systems that offer nothing else.

ssh-rsa comes last. It names the same RSA key as rsa-sha2-256 and differs only in the SHA-1 signature over the handshake, which matters for a session but not for reading the key. A server older than OpenSSH 7.2 signs with nothing else, and those are the hosts legacyAlgorithms exists for.

Functions

func DialCommand added in v0.3.0

func DialCommand(ctx context.Context, argv []string) (net.Conn, error)

DialCommand starts a command whose standard input and output are the connection, the way ssh's ProxyCommand works. With "ssh -W host:port jump" it reaches a host through its jump host, over the site's own ssh configuration and host key checks for the jump.

func Modify

func Modify(ctx context.Context, path string, change func(*File) error) error

Modify reads, changes and writes a known_hosts file under a lock, so that two administrators refreshing at the same time cannot lose an entry.

func Save

func Save(path string, f *File) error

Save writes a known_hosts file atomically.

Types

type Entry

type Entry struct {
	// Marker is "@revoked", "@cert-authority" or empty for a plain host key.
	Marker string `json:"marker,omitempty" yaml:"marker,omitempty"`
	// Hosts are the patterns the line applies to: names, wildcards,
	// negations and hashed names.
	Hosts []string `json:"hosts" yaml:"hosts"`
	// Type is the key algorithm, "ssh-ed25519" and the like.
	Type string `json:"type" yaml:"type"`
	// Key is the base64 encoded public key.
	Key string `json:"key" yaml:"key"`
	// Comment is whatever followed the key on the line.
	Comment string `json:"comment,omitempty" yaml:"comment,omitempty"`
	// Notes are the comment lines written above the entry. They travel with
	// it when the file is sorted, so that a note stays with the key it is
	// about.
	Notes []string `json:"notes,omitempty" yaml:"notes,omitempty"`
}

Entry is one line of a known_hosts file.

func (Entry) IsHostKey added in v0.3.0

func (e Entry) IsHostKey() bool

IsHostKey reports whether the line is a plain host key, rather than a revocation or a certificate authority.

func (Entry) Matches

func (e Entry) Matches(host string) bool

Matches reports whether the entry covers a host name, the way ssh decides it: a hashed name is compared by its hash, * and ? are wildcards, and a negated pattern that matches rules the line out whatever else matches.

func (Entry) String

func (e Entry) String() string

String renders the entry as a known_hosts line.

type File

type File struct {
	// Header holds the comment lines at the top of the file, which sites use
	// to say what the file is and how it is maintained.
	Header []string
	// Entries are the host keys, revocations and certificate authorities.
	// A comment between entries is kept as a note of the entry under it.
	Entries []Entry
	// Trailer holds the comment lines after the last entry.
	Trailer []string
}

File is a parsed known_hosts file: the comment block at the top, the entries under it and any comment after the last entry.

func Load

func Load(path string) (*File, error)

Load reads a known_hosts file, treating a missing file as empty.

func Parse

func Parse(data []byte) (*File, error)

Parse reads a known_hosts file.

func (*File) Add

func (f *File) Add(e Entry)

Add puts a host key in, replacing one of the same host and type and keeping its notes.

func (*File) Find

func (f *File) Find(host string) []Entry

Find returns the host keys that cover a host. Revocations and certificate authorities are not host keys and are left out.

func (*File) Remove

func (f *File) Remove(host string) int

Remove drops the host keys naming a host and reports how many went.

Only a pattern that names the host itself, literally or hashed, goes. A wildcard also covers other hosts, and a revocation or a certificate authority is a decision someone made on purpose, so neither is touched.

func (*File) Render

func (f *File) Render() []byte

Render writes the file back, keeping the header on top and sorting the entries so that two refreshes produce the same file and a diff stays readable. Each entry is written under its notes.

func (*File) Replace added in v0.3.0

func (f *File) Replace(host string, entries []Entry)

Replace puts a host's current keys in place of the ones the file holds for it, and carries the notes written above the old keys over to the new ones.

func (*File) Revoked added in v0.3.0

func (f *File) Revoked(host string, key Entry) bool

Revoked reports whether the file revokes a key for a host.

type Scanner

type Scanner struct {
	// Algorithms are the key types to ask for, best first.
	Algorithms []string
	// Timeout bounds one host, from the dial to the key.
	Timeout time.Duration
	// Port is the SSH port; empty means 22.
	Port string
	// Dial connects. It defaults to a TCP connection; a host behind a jump
	// host is reached through DialCommand instead, and tests replace it.
	Dial func(ctx context.Context, network, address string) (net.Conn, error)
}

Scanner collects host keys by starting an SSH handshake and stopping as soon as the server has presented its key.

func (*Scanner) Scan

func (s *Scanner) Scan(ctx context.Context, host string) ([]Entry, error)

Scan collects the host key a host offers. The algorithms are offered in one handshake, best first, so the server picks the strongest it has and an unreachable host costs one timeout rather than one for each algorithm.

Jump to

Keyboard shortcuts

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