store

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2026 License: GPL-3.0 Imports: 26 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 is the party's session-state store: the records every fleet host shares, loaded as a snapshot at version v, mutated in memory through the libsignal store interfaces, and written back by Commit under compare-and-swap. A Commit that finds another host's version refuses with ErrConflict and changes nothing; the caller reloads and redoes its work (plan decision 5).

Two record kinds: `keys`, the party keys record (package keys), and one `ss.<channel>` per channel — the Signal session, the remote identity, the seen message ids, the pull cursor and the pinned peer. A Backend holds them: file (JSON + flock) for one machine, pages (a ccc-pages /d/ data record) for a fleet, wrapped in Sealed so the page holds ciphertext only.

The orders decision 5 fixes: serve = decrypt → spool → Commit → inbox birth from the spool; send = encrypt → outbox → Commit → put → clear. The Journal is the spool and the outbox; ReplaySpool and ReplayOutbox run every serve round and finish what a crash or a failed step interrupted.

Index

Constants

View Source
const KeysKey = "keys"

KeysKey is the party keys record's key.

View Source
const MaxSeen = 1000

MaxSeen is how many message ids a channel record remembers; the oldest go first. The ratchet rejects a replayed ciphertext on its own; the seen set is the fleet's second check, by message id, and bounded so the record stays well under the backend's cap.

View Source
const OwnCardKey = "card"

OwnCardKey is the record key of the party's own card.

Variables

View Source
var (
	ErrConflict = errors.New("store: version conflict")
	ErrTooLarge = errors.New("store: record too large")
	ErrCorrupt  = errors.New("store: record does not open")
	ErrNoKeys   = errors.New("store: party keys record absent")
)

Errors a caller can match with errors.Is.

View Source
var ErrBusy = errors.New("store: another process holds the journal's lock")

ErrBusy is a lock another process on this machine holds.

View Source
var ErrPermanent = errors.New("store: entry cannot be put")

ErrPermanent is a put no retry can land: the channel is gone, the token is refused, the entry's own bytes are wrong. ReplayOutbox drops such an entry.

View Source
var ErrProbe = errors.New("store: a snapshot that admits any identity is not committed")

ErrProbe is a Commit of a snapshot that admits any identity.

Functions

func ChannelKey

func ChannelKey(channel string) string

ChannelKey is a channel record's key.

func ReplayOutbox

func ReplayOutbox(j *Journal, put func(*Entry) error) error

ReplayOutbox puts the outbox entries whose Commit landed and drops the uncommitted ones — a ciphertext whose session state was never committed must not reach the relay, since the next send reuses its counter. A put the relay already holds is refused by the receiver's ratchet as a replay; a committed entry never put leaves a counter gap the receiver's skipped-key store absorbs.

One entry never blocks the others: a put that returns ErrPermanent drops its entry, any other failure leaves it for the next replay, and either way the entries behind it — other channels, other relays — are tried. Every failure comes back joined, for the caller to report.

func SealKey

func SealKey(id *keys.Identity) [32]byte

SealKey derives the store's sealing key from the identity key — HKDF-SHA256 over the identity private key — so every fleet host, which holds the identity file, opens the same records and nothing else has to be shared.

func ValidKey

func ValidKey(key string) bool

ValidKey reports whether key is a record key both backends accept.

Types

type Backend

type Backend interface {
	// Load returns a record's bytes and version; an absent key is nil, 0, nil.
	Load(ctx context.Context, key string) ([]byte, int64, error)
	// Store writes data as version expect+1 when the stored version is expect
	// (0 for an absent key); another version refuses with a *ConflictError.
	Store(ctx context.Context, key string, data []byte, expect int64) (int64, error)
}

Backend holds versioned records by key.

func Sealed

func Sealed(b Backend, key [32]byte) Backend

Sealed wraps a backend so records leave the process sealed: AES-256-GCM, a random nonce before the ciphertext, the record key as associated data so a blob cannot be moved under another key. A blob that does not open refuses with ErrCorrupt before any libsignal deserialization sees it.

type Channel

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

Channel is one channel's record, loaded at a version: the libsignal session store and identity store for the channel, the seen ids, the pull cursor and the pinned peer. Mutations stay in memory until Commit.

func (*Channel) AdmitAnyIdentity

func (c *Channel) AdmitAnyIdentity()

AdmitAnyIdentity makes this snapshot admit every identity key, to read the record a key change arrives in (design §3.2) without applying anything: the snapshot refuses to commit from here on.

func (*Channel) Announced

func (c *Channel) Announced() uint32

Announced is the signed prekey id the card last sent on this channel carried, 0 when unknown.

func (*Channel) Card

func (c *Channel) Card() *card.Signed

Card is the peer's newest pinned card as it travelled, nil before PinCard.

func (*Channel) ClearHeld

func (c *Channel) ClearHeld()

ClearHeld drops the held card.

func (*Channel) Commit

func (c *Channel) Commit(ctx context.Context) error

Commit writes the record as version Version()+1. Another host's commit in between refuses with ErrConflict and changes nothing here: reload the channel and redo the work on it.

func (*Channel) ContainsSession

func (c *Channel) ContainsSession(ctx context.Context, addr *protocol.SignalAddress) (bool, error)

ContainsSession reports whether addr has a session record.

func (*Channel) Cursor

func (c *Channel) Cursor() int64

Cursor is the relay seq of the last envelope pulled, -1 before the first.

func (*Channel) DeleteAllSessions

func (c *Channel) DeleteAllSessions(ctx context.Context) error

DeleteAllSessions drops every session record of the channel.

func (*Channel) DeleteSession

func (c *Channel) DeleteSession(ctx context.Context, addr *protocol.SignalAddress) error

DeleteSession drops addr's session record.

func (*Channel) Deposited

func (c *Channel) Deposited() int

Deposited is how many one-time prekeys the relay is believed to hold for this party's side of the channel.

func (*Channel) Drop added in v0.3.0

func (c *Channel) Drop(ctx context.Context) error

Drop replaces the record with a tombstone at the next version: the party forgot the peer (peer forget), so the sessions, the pinned root and card, the invite secret and the seen ids go. Channel ids are never reused, so nothing loads the tombstone again. Another host's version refuses with ErrConflict.

func (*Channel) GetIdentityKeyPair

func (c *Channel) GetIdentityKeyPair() *identity.KeyPair

GetIdentityKeyPair serves the party's identity key pair.

func (*Channel) GetLocalRegistrationID

func (c *Channel) GetLocalRegistrationID() uint32

GetLocalRegistrationID serves the party's registrationId.

func (*Channel) GetSubDeviceSessions

func (c *Channel) GetSubDeviceSessions(ctx context.Context, name string) ([]uint32, error)

GetSubDeviceSessions lists the device ids other than keys.DeviceID that name has sessions for.

func (*Channel) Gone added in v0.3.0

func (c *Channel) Gone() bool

Gone reports whether the record is a tombstone (Drop).

func (*Channel) Held

func (c *Channel) Held() (*card.Signed, int64)

Held is the peer's root-signed card naming a new identity key that waits for the human, with the relay seq it came in under; nil, 0 when none.

func (*Channel) Hold

func (c *Channel) Hold(s *card.Signed, seq int64)

Hold keeps s, verified by the caller against the pinned root, for the human's yes; seq is the relay seq it came in under.

func (*Channel) IsTrustedIdentity

func (c *Channel) IsTrustedIdentity(ctx context.Context, addr *protocol.SignalAddress, key *identity.Key) (bool, error)

IsTrustedIdentity accepts any identity key until a card is pinned, and after that only the identity keys the pinned card lists and has not retired: a rotated card makes the old session's next Encrypt or Decrypt fail with signalerror.ErrUntrustedIdentity, the teardown design §3.2 wants, and whoever holds the voided key is refused with it. Before the pin the host checks the first contact itself, after decrypting the peer's card (§3.4: session identity == card keys[].identity).

func (*Channel) LoadSession

func (c *Channel) LoadSession(ctx context.Context, addr *protocol.SignalAddress) (*record.Session, error)

LoadSession is the libsignal session store: a fresh record for an address without one, never nil.

func (*Channel) MarkSeen

func (c *Channel) MarkSeen(id string, seq int64)

MarkSeen records a message id with the relay seq it arrived under.

func (*Channel) Name

func (c *Channel) Name() string

Name is the channel id.

func (*Channel) PinCard

func (c *Channel) PinCard(s *card.Signed)

PinCard records s as the peer's newest root-signed card. The caller has verified it (card.Verify); from here on IsTrustedIdentity accepts only the identity keys it lists.

func (*Channel) PinRoot

func (c *Channel) PinRoot(root ed25519.PublicKey)

PinRoot records the peer's root public key.

func (*Channel) Received

func (c *Channel) Received() bool

Received reports whether a message from the peer has opened on this channel: the seen set holds an id. The set drops its oldest ids past MaxSeen and is never emptied.

func (*Channel) RemoteIdentity

func (c *Channel) RemoteIdentity(addr *protocol.SignalAddress) ([32]byte, bool)

RemoteIdentity is the identity key SaveIdentity recorded for addr — the key the session was built with — for the host's check against the pinned card's keys[].identity (design §3.4).

func (*Channel) Root

func (c *Channel) Root() ed25519.PublicKey

Root is the peer's pinned root public key, nil before PinRoot.

func (*Channel) SaveIdentity

func (c *Channel) SaveIdentity(ctx context.Context, addr *protocol.SignalAddress, key *identity.Key) error

SaveIdentity records the remote identity key the library saw for addr.

func (*Channel) Secret

func (c *Channel) Secret() []byte

Secret is the invite link's secret, nil before SetSecret.

func (*Channel) Seen

func (c *Channel) Seen(id string) bool

Seen reports whether a message id was marked seen.

func (*Channel) SetAnnounced

func (c *Channel) SetAnnounced(id uint32)

SetAnnounced records the signed prekey id the card being sent carries.

func (*Channel) SetCursor

func (c *Channel) SetCursor(seq int64)

SetCursor records the relay seq of the last envelope pulled.

func (*Channel) SetDeposited

func (c *Channel) SetDeposited(n int)

SetDeposited records the relay's count after a deposit.

func (*Channel) SetSecret

func (c *Channel) SetSecret(secret []byte)

SetSecret records the invite link's secret.

func (*Channel) SpendDeposit

func (c *Channel) SpendDeposit()

SpendDeposit records that a PreKey message spent one deposited key.

func (*Channel) StoreSession

func (c *Channel) StoreSession(ctx context.Context, addr *protocol.SignalAddress, r *record.Session) error

StoreSession keeps the session record's bytes for addr.

func (*Channel) Version

func (c *Channel) Version() int64

Version is the loaded version; Commit raises it.

type ConflictError

type ConflictError struct {
	Key             string
	Expected, Found int64
}

ConflictError is a Store that found another version than expected.

func (*ConflictError) Error

func (e *ConflictError) Error() string

func (*ConflictError) Is

func (e *ConflictError) Is(target error) bool

Is makes errors.Is(err, ErrConflict) true.

type Entry

type Entry struct {
	// ID is the message id; Channel the channel it belongs to.
	ID      string `json:"id"`
	Channel string `json:"channel"`
	// Seq is the relay seq the envelope arrived under (spool); 0 in the outbox.
	Seq int64 `json:"seq"`
	// Type is the envelope type; Body the plaintext (spool) or ciphertext (outbox).
	Type string `json:"type"`
	Body []byte `json:"body"`
	// Committed is set by MarkCommitted once the Commit this entry waited on landed.
	Committed bool `json:"committed"`
	// Bump is a room outbox entry whose put bumps the membership version: a
	// removal record, which bumps however often it is retried.
	Bump bool `json:"bump,omitempty"`
	// contains filtered or unexported fields
}

Entry is one journaled message.

type Journal

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

Journal is a crash-safe local list of entries that wait on a Commit, in a directory outside the tree: the serve spool (a decrypted message awaiting its inbox birth) and the send outbox (a ciphertext awaiting its relay put). The sequence is Add → Commit → MarkCommitted → deliver or put → Remove; a CAS loser Removes its entry; ReplaySpool and ReplayOutbox finish what a crash or a failed step cut, every serve round.

func OpenJournal

func OpenJournal(dir string) (*Journal, error)

OpenJournal opens (creating) the journal directory.

func (*Journal) Add

func (j *Journal) Add(e *Entry) error

Add journals e, uncommitted. Entries replay in Add order.

func (*Journal) Entries

func (j *Journal) Entries() ([]*Entry, error)

Entries lists the journal in Add order.

func (*Journal) Lock added in v0.2.0

func (j *Journal) Lock(key string, wait bool) (func(), error)

Lock takes the machine's exclusive lock on key's entries — a flock on a sibling key.lock file, so two processes on one host (peer send and peer serve) never put one entry twice — and returns its release. wait blocks until the holder releases; without it a held lock is ErrBusy. A process that dies drops its lock. The lock is per open file, so a nested Lock of the same key from one process waits on itself.

func (*Journal) MarkCommitted

func (j *Journal) MarkCommitted(e *Entry) error

MarkCommitted records that e's Commit landed.

func (*Journal) Remove

func (j *Journal) Remove(e *Entry) error

Remove deletes e; an entry already gone is not an error.

type Keys

type Keys struct {

	// PartyKeys is the record's content.
	PartyKeys *keys.PartyKeys
	// contains filtered or unexported fields
}

Keys is the party keys record, loaded at a version: the libsignal prekey and signed-prekey stores. PartyKeys is nil while the record is absent — the host seeds it from the party keys file peer init wrote and commits. The record keeps raw 32-byte keys (package keys), never libsignal's record bytes, whose reload corrupts the public key.

func (*Keys) Commit

func (k *Keys) Commit(ctx context.Context) error

Commit writes the record as version Version()+1; ErrConflict as Channel.Commit.

func (*Keys) ContainsPreKey

func (k *Keys) ContainsPreKey(ctx context.Context, id uint32) (bool, error)

ContainsPreKey reports whether the one-time prekey id is held.

func (*Keys) ContainsSignedPreKey

func (k *Keys) ContainsSignedPreKey(ctx context.Context, id uint32) (bool, error)

ContainsSignedPreKey reports whether id is a signed prekey held.

func (*Keys) LoadPreKey

func (k *Keys) LoadPreKey(ctx context.Context, id uint32) (*record.PreKey, error)

LoadPreKey is the libsignal prekey store: nil, nil for an unknown id.

func (*Keys) LoadSignedPreKey

func (k *Keys) LoadSignedPreKey(ctx context.Context, id uint32) (*record.SignedPreKey, error)

LoadSignedPreKey is the libsignal signed-prekey store: the current signed prekey or the one the last rotation replaced, nil, nil for any other id.

func (*Keys) LoadSignedPreKeys

func (k *Keys) LoadSignedPreKeys(ctx context.Context) ([]*record.SignedPreKey, error)

LoadSignedPreKeys lists the current signed prekey and the previous one.

func (*Keys) RemovePreKey

func (k *Keys) RemovePreKey(ctx context.Context, id uint32) error

RemovePreKey drops the one-time prekey id; the library calls it after a PreKey message used it.

func (*Keys) RemoveSignedPreKey

func (k *Keys) RemoveSignedPreKey(ctx context.Context, id uint32) error

RemoveSignedPreKey drops the signed prekey id, current or previous; a record without a current one cannot be committed.

func (*Keys) StorePreKey

func (k *Keys) StorePreKey(ctx context.Context, id uint32, p *record.PreKey) error

StorePreKey adds or replaces the one-time prekey id.

func (*Keys) StoreSignedPreKey

func (k *Keys) StoreSignedPreKey(ctx context.Context, id uint32, r *record.SignedPreKey) error

StoreSignedPreKey makes r the current signed prekey; the one it replaces becomes the previous.

func (*Keys) Version

func (k *Keys) Version() int64

Version is the loaded version; Commit raises it.

type OwnCard

type OwnCard struct {

	// Issued is when the card was signed; Identity the identity key its
	// current key names, in text form; SignedPrekey the signed prekey id it
	// carries — what a reader compares against its own to see the card is stale.
	Issued       time.Time
	Identity     string
	SignedPrekey uint32
	// Public is the card without details, Details the one with them.
	Public, Details *card.Signed
	// contains filtered or unexported fields
}

OwnCard is the party's own root-signed card on the store, in both forms it travels: without details (envelope #0, in clear on the relay) and with details (in-session). The root key signs it where the root lives — peer init, peer card publish, peer identity rotate — and every host reads it from here, so no fleet host needs root.json to open a channel, answer a card or announce a rotation. The fields are nil while the record is absent.

func (*OwnCard) Commit

func (c *OwnCard) Commit(ctx context.Context) error

Commit writes the record as version Version()+1; ErrConflict as Channel.Commit.

func (*OwnCard) Present

func (c *OwnCard) Present() bool

Present reports whether the record holds a card.

func (*OwnCard) Set

func (c *OwnCard) Set(issued time.Time, identity string, signedPrekey uint32, public, details *card.Signed)

Set replaces the record's content in memory; Commit writes it.

func (*OwnCard) Version

func (c *OwnCard) Version() int64

Version is the loaded version; Commit raises it.

type Store

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

Store is a party's state store: a backend plus the identity the libsignal identity store serves.

func New

func New(b Backend, id *keys.Identity) *Store

New opens a store over b for the party whose identity is id.

func (*Store) Channel

func (s *Store) Channel(ctx context.Context, name string) (*Channel, error)

Channel loads the channel's record, fresh at version 0 when absent.

func (*Store) Keys

func (s *Store) Keys(ctx context.Context) (*Keys, error)

Keys loads the party keys record.

func (*Store) OwnCard

func (s *Store) OwnCard(ctx context.Context) (*OwnCard, error)

OwnCard loads the party's card record, empty at version 0 when absent.

func (*Store) ReplaySpool

func (s *Store) ReplaySpool(ctx context.Context, j *Journal, deliver func(*Entry) error) error

ReplaySpool runs on serve's start and before every round's pulls: it delivers the spool entries whose Commit landed — marked committed, or whose id the channel record has seen — and drops the rest, whose message the cursor will pull again. A delivered entry is removed, so a replay delivers each message once; an entry another host also committed may be delivered twice — a duplicate at most, never a loss (decision 5).

Directories

Path Synopsis
Package file is the store backend for one machine: one JSON file per record under a directory, `{"version": n, "data": …}`, written under an exclusive flock on a sibling lock file so two processes on the machine compare-and-swap correctly.
Package file is the store backend for one machine: one JSON file per record under a directory, `{"version": n, "data": …}`, written under an exclusive flock on a sibling lock file so two processes on the machine compare-and-swap correctly.
Package pages is the fleet's store backend: each record is one ccc-pages /d/ data record on the party's private state page (UCC design v2 § 5 commitment 5) — `GET /d/<slug>/<key>` and `PUT /d/<slug>/<key>?v=<n>` with the owner Bearer, the bytes base64 once in the envelope's data string under Schema, the 256 KiB cap on the envelope.
Package pages is the fleet's store backend: each record is one ccc-pages /d/ data record on the party's private state page (UCC design v2 § 5 commitment 5) — `GET /d/<slug>/<key>` and `PUT /d/<slug>/<key>?v=<n>` with the owner Bearer, the bytes base64 once in the envelope's data string under Schema, the 256 KiB cap on the envelope.
pagestest
Package pagestest serves a fake ccc-pages /d/ store for tests: the owner Bearer, CAS by ?v=, the envelope cap, and the status codes and bodies the real one answers (plan-research-ccc-pages § 5).
Package pagestest serves a fake ccc-pages /d/ store for tests: the owner Bearer, CAS by ?v=, the envelope cap, and the status codes and bodies the real one answers (plan-research-ccc-pages § 5).

Jump to

Keyboard shortcuts

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