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 ¶
- Variables
- func Argon2id(password, salt []byte, kdf KDF) []byte
- type Collection
- func (c *Collection) AddOwner(id uint32, owner Owner) error
- func (c *Collection) BeginUnlock() (*Opening, error)
- func (c *Collection) ChangeKey(r *Rekey) error
- func (c *Collection) Clone() *Collection
- func (c *Collection) Commit(next *Collection)
- func (c *Collection) CreateItem(label string, attrs map[string]string, secret []byte, contentType string, ...) (id uint32, created bool, err error)
- func (c *Collection) Created() time.Time
- func (c *Collection) DeleteItem(id uint32, now time.Time) error
- func (c *Collection) Destroy()
- func (c *Collection) Discard(next *Collection)
- func (c *Collection) FinishUnlock(o *Opened) error
- func (c *Collection) IDs() []uint32
- func (c *Collection) Item(id uint32) (Item, error)
- func (c *Collection) Label() string
- func (c *Collection) Legacy() bool
- func (c *Collection) Lock() error
- func (c *Collection) Locked() bool
- func (c *Collection) Marshal() ([]byte, error)
- func (c *Collection) Modified() time.Time
- func (c *Collection) Search(attrs map[string]string) []uint32
- func (c *Collection) Secret(id uint32) ([]byte, error)
- func (c *Collection) SetItemAttributes(id uint32, attrs map[string]string, now time.Time) error
- func (c *Collection) SetItemLabel(id uint32, label string, now time.Time) error
- func (c *Collection) SetLabel(label string, now time.Time) error
- func (c *Collection) SetSecret(id uint32, secret []byte, contentType string, owner Owner, now time.Time) error
- func (c *Collection) Verifier() (*Verifier, error)
- type Item
- type KDF
- type Opened
- type Opening
- type Owner
- type Rekey
- type Stretch
- type Verifier
Constants ¶
This section is empty.
Variables ¶
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.
var DefaultKDF = KDF{Time: 3, MemKiB: 64 * 1024, Threads: 4}
DefaultKDF is RFC 9106's second recommended option: 3 passes over 64 MiB.
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 ¶
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) 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 ¶
SetItemAttributes replaces an item's attributes.
func (*Collection) SetItemLabel ¶
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.
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.
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.
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.
type Stretch ¶
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.