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 ¶
- Variables
- type Backend
- type FileBackend
- type KeyringBackend
- type MemoryBackend
- type Option
- type Slot
- type Store
- func (s *Store) AddToAgent() error
- func (s *Store) Certificate() (*ssh.Certificate, error)
- func (s *Store) CertificateBytes() ([]byte, error)
- func (s *Store) DeleteCertificate() error
- func (s *Store) GeneratePrivateKey() error
- func (s *Store) HasCertificate() bool
- func (s *Store) HasPrivateKey() bool
- func (s *Store) PrivateKey() (crypto.PrivateKey, error)
- func (s *Store) PrivateKeyBytes() ([]byte, error)
- func (s *Store) PublicKey() (ssh.PublicKey, error)
- func (s *Store) PublicKeyBytes() ([]byte, error)
- func (s *Store) RemoveFromAgent() error
- func (s *Store) RemoveStaleFromAgent() ([]*ssh.Certificate, error)
- func (s *Store) SaveCertificate(c *ssh.Certificate) error
- func (s *Store) Signer() (ssh.Signer, error)
Constants ¶
This section is empty.
Variables ¶
var ErrNotFound = errors.New("not found in store backend")
ErrNotFound is returned, possibly wrapped, by Backend.Get when nothing is stored in a slot
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
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
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
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 ¶
WithCurve sets the curve used when GeneratePrivateKey creates an ECDSA key. The default is P-256.
func WithKeyType ¶
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 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 ¶
New creates a Store that keeps its private key and certificate in backend and only accepts certificates signed by capubkey
func NewFile ¶
NewFile creates a Store using a FileBackend for the private key at keypath. An existing key file is never replaced (see WithoutKeyReplacement).
func NewKeyring ¶
NewKeyring creates a Store using a KeyringBackend for the current user
func (*Store) AddToAgent ¶
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 ¶
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 ¶
DeleteCertificate deletes the current certificate. It is not an error if there is no certificate.
func (*Store) GeneratePrivateKey ¶
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 ¶
Returns true or false depending if a certificate exists
func (*Store) HasPrivateKey ¶
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 ¶
Returns a byte slice containing the OpenSSH private key. This will return an error if no valid private key is found.
func (*Store) PublicKey ¶
Returns a ssh.PublicKey for the current private key. This will return an error if no valid private key is found.
func (*Store) PublicKeyBytes ¶
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 ¶
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