Help us shape the future of Go! Take the 2026 Go Developer Survey

sshkeys

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package sshkeys parses, describes, generates and certifies SSH keys (OpenSSH public/private keys, authorized_keys, RFC 4716 and OpenSSH certificates). Encrypted private keys are described without decrypting them whenever the format exposes the public key.

Index

Constants

View Source
const (
	KindPublic      = "public"
	KindPrivate     = "private"
	KindCertificate = "certificate"
)

Item kinds.

Variables

Algorithms usable for SSH keys, in the order offered to users.

View Source
var DefaultUserExtensions = []string{"permit-X11-forwarding", "permit-agent-forwarding", "permit-port-forwarding", "permit-pty", "permit-user-rc"}

DefaultUserExtensions are what ssh-keygen grants by default.

View Source
var ErrNoSSHKey = errors.New("no SSH key found")

ErrNoSSHKey means no SSH key was found.

Functions

func AuthorizedKey

func AuthorizedKey(pub ssh.PublicKey, comment string) []byte

AuthorizedKey renders "type base64 comment\n".

func Generate

func Generate(a keys.Algorithm, comment string, passphrase []byte) (crypto.Signer, []byte, []byte, error)

Generate creates a key pair and returns the OpenSSH private key PEM (encrypted when passphrase is set) and the authorized_keys line.

func Marshal

func Marshal(k crypto.PrivateKey, comment string, passphrase []byte) ([]byte, []byte, error)

Marshal encodes a private key in OpenSSH format plus its public line.

func ParseAlgorithm

func ParseAlgorithm(s string) (keys.Algorithm, error)

ParseAlgorithm accepts ed25519, ecdsa[-p256|-p384|-p521], rsa[-3072|-4096].

func SignCert

func SignCert(req CertRequest, ca crypto.Signer) (*ssh.Certificate, error)

SignCert issues an OpenSSH certificate. RSA CAs sign with rsa-sha2-512, never SHA-1 ssh-rsa.

func VerifyCert

func VerifyCert(c *ssh.Certificate, principal string) error

VerifyCert checks the certificate signature, validity window and that principal is listed (the first principal when empty).

Types

type CertInfo

type CertInfo struct {
	CertType        string            `json:"cert_type"` // user or host
	KeyID           string            `json:"key_id"`
	Serial          uint64            `json:"serial"`
	Principals      []string          `json:"principals"`
	ValidAfter      *time.Time        `json:"valid_after,omitempty"`
	ValidBefore     *time.Time        `json:"valid_before,omitempty"` // nil = forever
	Status          string            `json:"status"`                 // valid, expired, not_yet_valid
	CriticalOptions map[string]string `json:"critical_options"`
	Extensions      []string          `json:"extensions"`
	CAType          string            `json:"ca_type"`
	CAFingerprint   string            `json:"ca_fingerprint_sha256"`
	SignatureAlgo   string            `json:"signature_algorithm"`
	KeyFingerprint  string            `json:"key_fingerprint_sha256"`
}

CertInfo describes an OpenSSH certificate.

func DescribeCert

func DescribeCert(c *ssh.Certificate) CertInfo

DescribeCert describes c at the current time.

type CertRequest

type CertRequest struct {
	Key        ssh.PublicKey
	Host       bool
	KeyID      string
	Serial     uint64 // 0 = random
	Principals []string
	ValidAfter time.Time
	Validity   time.Duration // 0 = forever
	// Extensions for user certificates; nil = OpenSSH defaults.
	Extensions      []string
	CriticalOptions map[string]string
}

CertRequest describes a certificate to sign.

type Info

type Info struct {
	Kind              string    `json:"kind"`
	Format            string    `json:"format"`
	Type              string    `json:"type"`
	Bits              int       `json:"bits"`
	FingerprintSHA256 string    `json:"fingerprint_sha256"`
	FingerprintMD5    string    `json:"fingerprint_md5"`
	Comment           string    `json:"comment,omitempty"`
	Options           []string  `json:"options,omitempty"`
	Encrypted         bool      `json:"encrypted"`
	Strength          string    `json:"strength"`
	Notes             []string  `json:"notes,omitempty"`
	Certificate       *CertInfo `json:"certificate,omitempty"`
	PublicKey         string    `json:"public_key"` // authorized_keys line
}

Info is the stable description of an SSH key.

func Describe

func Describe(it Item) Info

Describe returns the description of an item.

type Item

type Item struct {
	Kind      string
	Format    string // openssh, pem, authorized_keys, rfc4716
	Public    ssh.PublicKey
	Cert      *ssh.Certificate
	Comment   string
	Options   []string // authorized_keys options
	Encrypted bool
	// Private is set only when the key was decrypted (or not encrypted).
	Private crypto.PrivateKey
	// Raw holds the original private key PEM so it can be stored unchanged
	// (still encrypted with its passphrase).
	Raw []byte
}

Item is one SSH object found in input.

func Parse

func Parse(data []byte) ([]Item, error)

Parse reads public keys (authorized_keys lines, RFC 4716), OpenSSH and PEM private keys, and OpenSSH certificates. Private keys are not decrypted: Private is nil for encrypted keys unless decrypt is called.

func (*Item) Decrypt

func (it *Item) Decrypt(passphrase []byte) error

Decrypt decrypts an encrypted private key item in place.

Jump to

Keyboard shortcuts

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