session

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2026 License: GPL-3.0 Imports: 25 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 session runs the Signal session of one channel over the store's snapshots: X3DH from the peer's card and a one-time prekey, encrypt and decrypt with the libsignal Double Ratchet, the plaintext message format, the send order of plan decision 5, the accept proof and the 8-digit verification code.

Identity trust is the pinned card's: the channel's IsTrustedIdentity (package store) admits only the identity keys the peer's root-signed card lists, so Establish wants the card pinned first, and the host's first contact check (design §3.4) is CheckIdentity after the peer's card arrived in-session.

Index

Constants

View Source
const (
	KindText     = "text"
	KindTask     = "task"
	KindQuestion = "question"
	KindAnswer   = "answer"
	KindDoc      = "doc"
	KindFile     = "file"
	KindCard     = "card"
	KindAck      = "ack"
	// KindBye tells the peer this party forgot the channel (peer forget);
	// the session's own business, never delivered to an agent.
	KindBye = "bye"
	// KindRoster is a room's membership notice, born from a roster or leave
	// record the fold accepted (deliver.KindRoster); it never travels on a
	// channel.
	KindRoster = deliver.KindRoster
)

The v1 message kinds (design §3.5). A vendor kind is "vnd.<party>.<name>".

View Source
const (
	KindRoomInvite = "room-invite"
	KindRoomAccept = "room-accept"
	KindRoomSeat   = "room-seat"
	KindRoomJoin   = "room-join"
	KindRoomToken  = "room-token"
	KindRoomAdmin  = "room-admin"
)

The v2 channel kinds of a room's join and handover (wire contract v2 § Kinds): the session's own business, like card, never delivered to an agent.

View Source
const MessageVersion = 1

MessageVersion is the plaintext format version.

View Source
const ProofSize = sha256.Size

ProofSize is the accept proof's length in bytes.

Variables

View Source
var (
	ErrIdentity   = errors.New("session: the session's identity key is not the card's")
	ErrDuplicate  = errors.New("session: message already received")
	ErrNotMessage = errors.New("session: envelope is not a message")
	ErrNoCard     = errors.New("session: peer card not pinned")
	ErrNoKey      = errors.New("session: card has no current key")
	// ErrUntrusted is a record built under an identity key the pinned card
	// does not list — what a peer's key rotation looks like from here
	// (design §3.2): its new card comes in a new session, which the old
	// pinned card cannot admit. The record is readable by the version that
	// applies a key change, so a caller parks it rather than consuming it.
	ErrUntrusted = errors.New("session: the record's identity key is not the pinned card's")
)

Errors a caller can match.

Addr is the peer's libsignal address in every channel: v1 channels are pairwise (design §10 #1), so the one remote party is "peer" at keys.DeviceID and a channel record holds one session.

View Source
var ErrKind = errors.New("session: unknown message kind")

ErrKind is a message kind outside the v1 set and not a vendor kind.

View Source
var ErrProof = errors.New("session: the accept proof does not verify")

ErrProof is a first card message whose accept proof is absent or wrong: whoever joined does not hold the link's secret.

Functions

func CheckProof

func CheckProof(m *Message, want []byte) error

CheckProof verifies the proof m carries against want. A proof that is absent, does not decode strictly, has another length or differs is ErrProof; the comparison is constant-time.

func Code

func Code(channelID string, inviter, invitee ed25519.PublicKey, secret []byte) string

Code is the channel's 8-digit verification code (design §4): the channel id, both root keys (inviter first) and the invite link's secret, hashed under a fixed label — a new invite is a new code, and nothing public derives it. The invitee prints it after accept and reads it back, with its party id, over the chat the link came on; the inviter types it at confirm. Every input is raw bytes: channelID as its 32 hex characters, the roots as 32-byte Ed25519 keys, secret as its 16 decoded bytes.

func CodeMatches

func CodeMatches(typed, want string) bool

CodeMatches compares the typed code with the channel's in constant time.

func CurrentKey

func CurrentKey(c *card.Card) (*card.Key, error)

CurrentKey is the card's key in force: the first with no Until.

func EncodeProof

func EncodeProof(proof []byte) string

EncodeProof is the proof as the message carries it: base64url, no padding.

func NewPrekeys

func NewPrekeys(ctx context.Context, k *store.Keys, n int) ([]relay.Prekey, error)

NewPrekeys adds n fresh one-time prekeys to the party keys snapshot and returns their public halves for the relay. Each channel gets its own keys: a key deposited on two channels could be taken twice, and the second PreKey message naming it would find it gone. Commit the keys before depositing them.

func PeerCard

func PeerCard(m *Message) (*card.Card, *card.Signed, ed25519.PublicKey, error)

PeerCard reads a card the peer sent in-session (kind card): the detached signature and card bytes, verified against the root the card itself names. Whether that root is the right party is the caller's question — the pinned root, or the verification code at first contact.

func PrekeyIdentity

func PrekeyIdentity(e envelope.Envelope) ([32]byte, error)

PrekeyIdentity is the identity key a PreKey record carries in clear — the key the sender built its session under, what a key change looks like from outside the ciphertext. An envelope of another type, or one the library does not read, is an error.

func Proof

func Proof(secret []byte, channelID string, inviterRoot, inviteeRoot ed25519.PublicKey, inviterIdentity, inviteeIdentity [32]byte) []byte

Proof is the guest's accept proof (design §4): HMAC-SHA256 under the link secret over the channel id, both roots and both session identity keys, so a party that joined with the token alone — the relay operator — cannot present it. The identity keys are the Curve25519 points the session was built with: on the host its own and Channel.RemoteIdentity; on the guest card #0's current key and its own. Inputs are raw bytes as in Code.

func RoomKind added in v0.2.0

func RoomKind(kind string) bool

RoomKind reports whether kind is one of a room's channel kinds.

func ValidKind

func ValidKind(kind string) bool

ValidKind reports whether kind is a v1 kind or a vendor kind. The vendor branch is the delivery record's grammar — vnd.<party id>.<name> — so a kind this admits is one the receiver can write on a frontmatter line.

Types

type Message

type Message struct {
	V    int       `json:"v"`
	ID   string    `json:"id"`
	From string    `json:"from"`
	To   string    `json:"to"`
	Kind string    `json:"kind"`
	At   time.Time `json:"at"`
	Body []byte    `json:"body"`
	// Proof is the accept proof (design §4), base64url without padding:
	// present on the guest's first card message alone, not part of the
	// signed card bytes, ignored on every other message. CheckProof reads it.
	Proof string `json:"proof,omitempty"`
}

Message is the plaintext inside a Signal ciphertext (plan decision 7): everything the relay must not see — who, to whom, what kind, when, the body. From and To are party ids, or "<party id>/<agent id>".

func CardMessage

func CardMessage(from, to string, s *card.Signed) (*Message, error)

CardMessage packs a signed card as a kind-card message body.

func NewMessage

func NewMessage(from, to, kind string, body []byte) (*Message, error)

NewMessage makes a message with a fresh id, stamped now.

func UnmarshalMessage

func UnmarshalMessage(b []byte) (*Message, error)

UnmarshalMessage reads a decrypted plaintext.

func (*Message) Marshal

func (m *Message) Marshal() ([]byte, error)

Marshal is the plaintext handed to the cipher; a message whose plaintext could not fit a relay record refuses here.

type PutError

type PutError struct {
	Entry *store.Entry
	Err   error
}

PutError is a Send whose relay put failed after the session state was committed: the ciphertext stays in the outbox, marked committed, for store.ReplayOutbox; a caller abandoning the channel removes Entry.

func (*PutError) Error

func (e *PutError) Error() string

func (*PutError) Unwrap

func (e *PutError) Unwrap() error

type Session

type Session struct {
	Channel *store.Channel
	Keys    *store.Keys
	// contains filtered or unexported fields
}

Session binds a channel's and the party keys' snapshots to libsignal for the channel's peer. Mutations stay in the snapshots until they are committed; Send commits the channel, every other caller commits itself.

func New

func New(ch *store.Channel, k *store.Keys) *Session

New opens the channel's session over its snapshots.

func (*Session) CheckIdentity

func (s *Session) CheckIdentity(c *card.Card) error

CheckIdentity is the first-contact check of design §3.4: the identity key the session was built with must be one the peer's card lists and has not retired — a key the card voids is no longer the party's (§3.2). Run it on the card that arrived in-session, before pinning it.

func (*Session) Encrypt

func (s *Session) Encrypt(ctx context.Context, m *Message) (envelope.Envelope, error)

Encrypt makes the relay record for m: a PreKey message until the peer's first reply is decrypted, a normal message after.

func (*Session) Establish

func (s *Session) Establish(ctx context.Context, k *card.Key, opk *relay.Prekey) error

Establish runs X3DH against the peer: k is the current key of its pinned card, opk a one-time prekey taken from the relay (nil when its side held none). The channel must have the card pinned so the bundle's identity key is admitted only when the card lists it.

func (*Session) Receive

func (s *Session) Receive(ctx context.Context, rec relay.Record) (*Message, error)

Receive decrypts one pulled record and advances the channel's cursor and seen set in the snapshot. A record at or before the cursor, or a message id already seen, is ErrDuplicate; a card record (envelope #0) is ErrNotMessage; malformed bytes never reach the library.

func (*Session) Send

func (s *Session) Send(ctx context.Context, outbox *store.Journal, rc *relay.Client, token string, m *Message) (relay.Receipt, error)

Send runs decision 5's send order: encrypt, outbox, commit the channel, put, clear. A commit that finds another host's version drops the ciphertext and returns store.ErrConflict — reload the channel and send again; a put that fails after the commit returns a *PutError.

Jump to

Keyboard shortcuts

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