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
- Variables
- func CheckProof(m *Message, want []byte) error
- func Code(channelID string, inviter, invitee ed25519.PublicKey, secret []byte) string
- func CodeMatches(typed, want string) bool
- func CurrentKey(c *card.Card) (*card.Key, error)
- func EncodeProof(proof []byte) string
- func NewPrekeys(ctx context.Context, k *store.Keys, n int) ([]relay.Prekey, error)
- func PeerCard(m *Message) (*card.Card, *card.Signed, ed25519.PublicKey, error)
- func PrekeyIdentity(e envelope.Envelope) ([32]byte, error)
- func Proof(secret []byte, channelID string, inviterRoot, inviteeRoot ed25519.PublicKey, ...) []byte
- func RoomKind(kind string) bool
- func ValidKind(kind string) bool
- type Message
- type PutError
- type Session
- func (s *Session) CheckIdentity(c *card.Card) error
- func (s *Session) Encrypt(ctx context.Context, m *Message) (envelope.Envelope, error)
- func (s *Session) Establish(ctx context.Context, k *card.Key, opk *relay.Prekey) error
- func (s *Session) Receive(ctx context.Context, rec relay.Record) (*Message, error)
- func (s *Session) Send(ctx context.Context, outbox *store.Journal, rc *relay.Client, token string, ...) (relay.Receipt, error)
Constants ¶
const ( KindText = "text" KindTask = "task" KindQuestion = "question" KindAnswer = "answer" KindDoc = "doc" KindFile = "file" KindCard = "card" KindAck = "ack" // 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>".
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.
const MessageVersion = 1
MessageVersion is the plaintext format version.
const ProofSize = sha256.Size
ProofSize is the accept proof's length in bytes.
Variables ¶
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.
var Addr = protocol.NewSignalAddress("peer", keys.DeviceID)
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.
var ErrKind = errors.New("session: unknown message kind")
ErrKind is a message kind outside the v1 set and not a vendor kind.
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 ¶
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 ¶
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 ¶
CodeMatches compares the typed code with the channel's in constant time.
func CurrentKey ¶
CurrentKey is the card's key in force: the first with no Until.
func EncodeProof ¶
EncodeProof is the proof as the message carries it: base64url, no padding.
func NewPrekeys ¶
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 ¶
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 ¶
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.
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 ¶
CardMessage packs a signed card as a kind-card message body.
func NewMessage ¶
NewMessage makes a message with a fresh id, stamped now.
func UnmarshalMessage ¶
UnmarshalMessage reads a decrypted plaintext.
type PutError ¶
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.
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 (*Session) CheckIdentity ¶
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 ¶
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 ¶
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 ¶
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.