keyring

package
v0.5.2 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2026 License: MIT, MIT Imports: 14 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 keyring is the Secret Service's storage core: a collection of items whose labels, attributes and secrets are encrypted at rest under a key derived from the collection's password.

On disk a collection is one file:

magic     8 bytes  "LYSKR\x00\x00\x02"
kdf       u32 time, u32 memory KiB, u8 threads (Argon2id)
salt      16 bytes
check     40 bytes, XChaCha20-Poly1305 nonce and tag over nothing,
          under the key: it opens exactly when the password is right
items     u32 count, then per item: u32 id, u32 n, n × 32-byte
          SHA-256 of u32 len(name) ‖ name ‖ value, one per attribute
body      XChaCha20-Poly1305 nonce, ciphertext and tag of the
          JSON body, with everything above as associated data

Each item's owners, the programs that stored or changed it or that the user allowed to read it, are in the body.

The attribute hashes let a locked collection answer a search with the matching items, so a client knows to ask for an unlock, as gnome-keyring's format does; everything else is in the body. While unlocked, each item's secret stays sealed in memory under the key and opens only for the call that asks for it.

The check tells a wrong password from a changed file: when it opens and the body does not, the file was changed. It offers nothing the body does not, since whoever holds the file can already test a password against the body at the same Argon2id cost. A changed KDF, salt or check derives or finds no key, so it still looks like a wrong password.

The first format, "LYSKR\x00\x00\x01", has no check, so there a body that fails to open is always ErrWrongPassword. It is read as it stands and written as the current format once unlocked.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrWrongPassword is a password that finds no key. A changed KDF,
	// salt or check, or any change to a file of the first format, is
	// indistinguishable from it.
	ErrWrongPassword = errors.New("keyring: wrong password")
	// ErrTampered is a file changed after it was written: the password is
	// right and the contents no longer authenticate under it.
	ErrTampered = errors.New("keyring: the password is right but the file was changed: its contents do not authenticate")
	ErrLocked   = errors.New("keyring: the collection is locked")
	ErrNoItem   = errors.New("keyring: no such item")
)

Errors a caller tells apart.

View Source
var DefaultKDF = KDF{Time: 3, MemKiB: 64 * 1024, Threads: 4}

DefaultKDF is RFC 9106's second recommended option: 3 passes over 64 MiB.

View Source
var ErrChanged = errors.New("keyring: the collection changed while it was unlocking")

ErrChanged is an unlock finishing on a collection that is no longer what it began on.

Functions

func Argon2id

func Argon2id(password, salt []byte, kdf KDF) []byte

Argon2id is the Stretch of every real key.

Types

type Collection

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

Collection is one keyring. It is not safe for concurrent use.

func New

func New(label string, password []byte, kdf KDF, stretch Stretch, now time.Time) (*Collection, error)

New creates an unlocked collection whose key derives from password.

func Parse

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

Parse reads a collection file. The result is locked: its items are known by id and attribute hashes only, until Unlock.

func (*Collection) AddOwner

func (c *Collection) AddOwner(id uint32, owner Owner) error

AddOwner lets owner read an item without asking; the item's secret and times stay as they are.

func (*Collection) BeginUnlock

func (c *Collection) BeginUnlock() (*Opening, error)

BeginUnlock starts unlocking a locked collection.

func (*Collection) ChangeKey

func (c *Collection) ChangeKey(r *Rekey) error

ChangeKey moves an unlocked collection to the new key; r is used up. The old key stays, since a Clone shares it: Commit destroys it.

func (*Collection) Clone

func (c *Collection) Clone() *Collection

Clone is a copy of an unlocked collection to change while c goes on serving reads. It shares c's key: Commit or Discard ends it.

func (*Collection) Commit

func (c *Collection) Commit(next *Collection)

Commit makes next, a Clone of c, the collection, destroying c's key if next moved to another one; c is not used again.

func (*Collection) CreateItem

func (c *Collection) CreateItem(label string, attrs map[string]string, secret []byte, contentType string, owner Owner, replace bool, now time.Time) (id uint32, created bool, err error)

CreateItem stores a secret, owned by owner unless that is "". With replace, an item with exactly the same attributes is updated in place instead, and owner joins its owners; created reports which.

func (*Collection) Created

func (c *Collection) Created() time.Time

Created is when the collection was made, zero while locked.

func (*Collection) DeleteItem

func (c *Collection) DeleteItem(id uint32, now time.Time) error

DeleteItem removes an item.

func (*Collection) Destroy

func (c *Collection) Destroy()

Destroy drops the key and the unlocked state without sealing anything, for a collection that is not used again. It cannot fail.

func (*Collection) Discard

func (c *Collection) Discard(next *Collection)

Discard drops next, a Clone of c that will not be used, destroying the key it moved to if it moved to one.

func (*Collection) FinishUnlock

func (c *Collection) FinishUnlock(o *Opened) error

FinishUnlock makes the collection unlocked with what Open found, under the lock that guards it. A collection already unlocked stays as it is, and one changed since BeginUnlock is ErrChanged; either way o is used up. A body that opens but does not match the header is corruption.

func (*Collection) IDs

func (c *Collection) IDs() []uint32

IDs is every item's id, ascending.

func (*Collection) Item

func (c *Collection) Item(id uint32) (Item, error)

Item is an item's metadata; locked, only its ID is known.

func (*Collection) Label

func (c *Collection) Label() string

Label is the collection's label, "" while locked.

func (*Collection) Legacy

func (c *Collection) Legacy() bool

Legacy reports a parsed file of the first format, not unlocked yet.

func (*Collection) Lock

func (c *Collection) Lock() error

Lock seals the collection's current state into its body and destroys the key: what remains in memory is the header a locked file shows. The key is destroyed whatever happens: state that fails to seal is an error, and the collection stays locked with no body, so it opens again only from its file, by a new Parse.

func (*Collection) Locked

func (c *Collection) Locked() bool

Locked reports whether the key is gone.

func (*Collection) Marshal

func (c *Collection) Marshal() ([]byte, error)

Marshal is the collection file. A locked collection writes the body it was parsed or locked with; one left without a body has no file to give.

func (*Collection) Modified

func (c *Collection) Modified() time.Time

Modified is when it last changed, zero while locked.

func (*Collection) Search

func (c *Collection) Search(attrs map[string]string) []uint32

Search is the items holding every attribute in attrs, ascending; it answers while locked too, from the hashes.

func (*Collection) Secret

func (c *Collection) Secret(id uint32) ([]byte, error)

Secret opens an item's secret. The caller clears it after use.

func (*Collection) SetItemAttributes

func (c *Collection) SetItemAttributes(id uint32, attrs map[string]string, now time.Time) error

SetItemAttributes replaces an item's attributes.

func (*Collection) SetItemLabel

func (c *Collection) SetItemLabel(id uint32, label string, now time.Time) error

SetItemLabel renames an item.

func (*Collection) SetLabel

func (c *Collection) SetLabel(label string, now time.Time) error

SetLabel renames an unlocked collection.

func (*Collection) SetSecret

func (c *Collection) SetSecret(id uint32, secret []byte, contentType string, owner Owner, now time.Time) error

SetSecret replaces an item's secret; owner, unless "", joins its owners.

func (*Collection) Verifier

func (c *Collection) Verifier() (*Verifier, error)

Verifier is what testing a password against the collection needs. A locked collection is tested by unlocking it.

type Item

type Item struct {
	ID          uint32
	Label       string
	Attributes  map[string]string
	ContentType string
	// Owners are the programs that may read the item without asking, in
	// the order they became owners; none for an item from before owners
	// were kept.
	Owners   []Owner
	Created  time.Time
	Modified time.Time
}

Item is one stored secret's metadata; the secret itself is read with Collection.Secret.

func (Item) Owned

func (it Item) Owned(o Owner) bool

Owned reports whether o is one of the item's owners.

type KDF

type KDF struct {
	Time    uint32
	MemKiB  uint32
	Threads uint8
}

KDF is the Argon2id cost a collection's key is derived with.

type Opened

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

Opened is a collection's unlocked state, its secrets sealed again under the key, waiting for FinishUnlock. Whoever gives up on one calls Discard.

func (*Opened) Discard

func (o *Opened) Discard()

Discard destroys the key of an Opened nothing will use.

type Opening

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

Opening is what unlocking a locked collection needs of it, taken under the lock that guards the collection and used outside it, since deriving the key takes as long as the KDF makes it.

func (*Opening) Open

func (o *Opening) Open(password []byte, stretch Stretch) (*Opened, error)

Open derives the key from password and opens the body. A wrong password is ErrWrongPassword and a changed file ErrTampered. No plain secret outlives it, whatever happens.

type Owner

type Owner string

Owner is a program an item belongs to: its executable's path, or "flatpak:" and the app id of a Flatpak app.

type Rekey

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

Rekey is a new key for a collection, derived outside the lock that guards it. Whoever gives up on one calls Discard.

func NewRekey

func NewRekey(password []byte, kdf KDF, stretch Stretch) (*Rekey, error)

NewRekey derives a new key from password with a fresh salt.

func (*Rekey) Discard

func (r *Rekey) Discard()

Discard destroys a new key nothing will use.

type Stretch

type Stretch func(password, salt []byte, kdf KDF) []byte

Stretch turns a password into key material. It is slow on purpose, so it runs outside whatever lock a caller of this package holds; Argon2id is what every real key uses.

type Verifier

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

Verifier tests a password against an unlocked collection's key check, outside the lock that guards the collection, since deriving the key takes as long as the KDF makes it.

func (*Verifier) Verify

func (v *Verifier) Verify(password []byte, stretch Stretch) error

Verify derives the key password gives and tests it against the key check: a password that finds another key is ErrWrongPassword.

Jump to

Keyboard shortcuts

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