store

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 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 store provides the certificate store: a private key and its certificate, kept in a Backend such as a file or the system keyring.

Index

Constants

This section is empty.

Variables

View Source
var ErrNotFound = errors.New("not found in store backend")

ErrNotFound is returned, possibly wrapped, by Backend.Get when nothing is stored in a slot

View Source
var ErrPrivateKeyExists = errors.New("private key already exists")

ErrPrivateKeyExists is returned by GeneratePrivateKey, when created with WithoutKeyReplacement, if a private key is already stored, as replacing an existing key (such as a SSH host key) would invalidate anything that trusts it

Functions

This section is empty.

Types

type Backend

type Backend interface {
	// Get returns the value in slot. An error wrapping [ErrNotFound] is
	// returned when the slot is empty.
	Get(slot Slot) ([]byte, error)
	// Put stores value in slot, replacing any existing value.
	Put(slot Slot, value []byte) error
	// Delete empties slot. Deleting an empty slot is not an error.
	Delete(slot Slot) error
}

Backend is where a Store keeps its private key and certificate. A Backend only stores bytes; validation, parsing, locking and policy are the job of the Store.

type FileBackend

type FileBackend struct {
	// contains filtered or unexported fields
}

FileBackend is a Backend that keeps the private key in a file and the certificate alongside it with a "-cert.pub" suffix, as OpenSSH expects. The private key file is only readable by its owner.

func NewFileBackend

func NewFileBackend(keypath string) *FileBackend

NewFileBackend returns a FileBackend for the private key at keypath

func (*FileBackend) Delete

func (b *FileBackend) Delete(slot Slot) error

func (*FileBackend) Get

func (b *FileBackend) Get(slot Slot) ([]byte, error)

func (*FileBackend) Put

func (b *FileBackend) Put(slot Slot, value []byte) error

type KeyringBackend

type KeyringBackend struct {
	// contains filtered or unexported fields
}

KeyringBackend is a Backend that keeps the private key and certificate in the system keyring (such as Windows Credential Manager, macOS Keychain or the Secret Service on Linux) under entries for a user. Values too large for a single entry are split into parts.

func NewKeyringBackend

func NewKeyringBackend(user string) *KeyringBackend

NewKeyringBackend returns a KeyringBackend for user

func (*KeyringBackend) Delete

func (b *KeyringBackend) Delete(slot Slot) error

func (*KeyringBackend) Get

func (b *KeyringBackend) Get(slot Slot) ([]byte, error)

func (*KeyringBackend) Put

func (b *KeyringBackend) Put(slot Slot, value []byte) error

type MemoryBackend

type MemoryBackend struct {
	// contains filtered or unexported fields
}

MemoryBackend is a Backend that keeps values in memory, for tests

func NewMemoryBackend

func NewMemoryBackend() *MemoryBackend

NewMemoryBackend returns an empty MemoryBackend

func (*MemoryBackend) Delete

func (b *MemoryBackend) Delete(slot Slot) error

func (*MemoryBackend) Get

func (b *MemoryBackend) Get(slot Slot) ([]byte, error)

func (*MemoryBackend) Put

func (b *MemoryBackend) Put(slot Slot, value []byte) error

type Option

type Option func(*Store)

func WithAgent

func WithAgent(a agent.ExtendedAgent) Option

WithAgent uses the provided agent instead of connecting to the local SSH agent when AddToAgent is called. The agent is not closed by the Store.

func WithCurve

func WithCurve(curve elliptic.Curve) Option

WithCurve sets the curve used when GeneratePrivateKey creates an ECDSA key. The default is P-256.

func WithKeyType

func WithKeyType(keyType sshkey.KeyType) Option

WithKeyType sets the type of key created by GeneratePrivateKey. The default is sshkey.KeyTypeECDSA. Existing keys of any supported type can be used regardless of this setting.

func WithoutKeyReplacement

func WithoutKeyReplacement() Option

WithoutKeyReplacement makes GeneratePrivateKey return ErrPrivateKeyExists rather than replace an existing private key, even one that cannot be parsed

type Slot

type Slot int

Slot identifies one of the two values a Store keeps in its Backend

const (
	// Key is the slot holding the PEM encoded private key
	Key Slot = iota
	// Cert is the slot holding the certificate in authorized_keys format
	Cert
)

func (Slot) String

func (s Slot) String() string

type Store

type Store struct {
	// contains filtered or unexported fields
}

Store is a certificate store. It holds one private key and its certificate in a Backend, validates certificates against the trusted CA before saving them and adds them to or removes them from the SSH agent.

func New

func New(capubkey ssh.PublicKey, backend Backend, opts ...Option) (*Store, error)

New creates a Store that keeps its private key and certificate in backend and only accepts certificates signed by capubkey

func NewFile

func NewFile(capubkey ssh.PublicKey, keypath string, opts ...Option) (*Store, error)

NewFile creates a Store using a FileBackend for the private key at keypath. An existing key file is never replaced (see WithoutKeyReplacement).

func NewKeyring

func NewKeyring(capubkey ssh.PublicKey, opts ...Option) (*Store, error)

NewKeyring creates a Store using a KeyringBackend for the current user

func (*Store) AddToAgent

func (s *Store) AddToAgent() error

Adds the existing key and certificate to the SSH Agent. A new connection to the agent is made for each call, so the agent does not need to be running until this is called. Failures connecting to or adding to the agent are returned as cert.ErrAddingToAgent.

func (*Store) Certificate

func (s *Store) Certificate() (*ssh.Certificate, error)

Returns a *ssh.Certificate for the current certificate. This will return an error if no valid certificate is found.

func (*Store) CertificateBytes

func (s *Store) CertificateBytes() ([]byte, error)

Returns a byte slice containing the stored OpenSSH certificate exactly as saved. This will return an error if the certificate is missing or cannot be parsed.

func (*Store) DeleteCertificate

func (s *Store) DeleteCertificate() error

DeleteCertificate deletes the current certificate. It is not an error if there is no certificate.

func (*Store) GeneratePrivateKey

func (s *Store) GeneratePrivateKey() error

GeneratePrivateKey generates a new SSH private key of the configured type (see WithKeyType), replacing any existing key and deleting any existing certificate. With WithoutKeyReplacement this returns ErrPrivateKeyExists rather than replace an existing key.

func (*Store) HasCertificate

func (s *Store) HasCertificate() bool

Returns true or false depending if a certificate exists

func (*Store) HasPrivateKey

func (s *Store) HasPrivateKey() bool

Returns true of false depending if a private key exists

func (*Store) PrivateKey

func (s *Store) PrivateKey() (crypto.PrivateKey, error)

Returns a crypto.PrivateKey for the current private key. This will return an error if no valid private key is found.

func (*Store) PrivateKeyBytes

func (s *Store) PrivateKeyBytes() ([]byte, error)

Returns a byte slice containing the OpenSSH private key. This will return an error if no valid private key is found.

func (*Store) PublicKey

func (s *Store) PublicKey() (ssh.PublicKey, error)

Returns a ssh.PublicKey for the current private key. This will return an error if no valid private key is found.

func (*Store) PublicKeyBytes

func (s *Store) PublicKeyBytes() ([]byte, error)

Returns a byte slice containing the OpenSSH public key for the current private key. This will return an error if no valid private key is found.

func (*Store) RemoveFromAgent

func (s *Store) RemoveFromAgent() error

RemoveFromAgent removes the current certificate from the SSH Agent, leaving the private key and any other identities in place. Nothing is done if there is no certificate, if no agent can be connected to or if the agent does not hold the certificate. Other failures are returned as cert.ErrRemovingFromAgent.

func (*Store) RemoveStaleFromAgent added in v0.27.0

func (s *Store) RemoveStaleFromAgent() ([]*ssh.Certificate, error)

RemoveStaleFromAgent removes every stale certificate from the SSH Agent: user certificates signed by the trusted CA that have expired. This matters on Windows, where certificates are added with no lifetime and the agent keeps them across reboots, so the certificate for a replaced private key would otherwise stay in the agent after it expires. Nothing is done if no agent can be connected to. The removed certificates are returned, along with any failures as cert.ErrRemovingFromAgent.

func (*Store) SaveCertificate

func (s *Store) SaveCertificate(c *ssh.Certificate) error

Saves the provided ssh.Certificate

func (*Store) Signer

func (s *Store) Signer() (ssh.Signer, error)

Returns a ssh.Signer for the current private key. This will return an error if no private key is found.

Jump to

Keyboard shortcuts

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