211 lines
6.3 KiB
Go
211 lines
6.3 KiB
Go
package delivery
|
|
|
|
import (
|
|
"context"
|
|
"crypto/ed25519"
|
|
"crypto/sha256"
|
|
"errors"
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
)
|
|
|
|
type DirectoryDevice struct {
|
|
ID string `json:"id"`
|
|
Name string `json:"name"`
|
|
PublicKey []byte `json:"publicKey"`
|
|
MLS *Credential `json:"mls,omitempty"`
|
|
}
|
|
|
|
type Credential struct {
|
|
PublicKey []byte `json:"publicKey"`
|
|
Signature []byte `json:"signature"`
|
|
}
|
|
|
|
type KeyPackage struct {
|
|
Package []byte `json:"package"`
|
|
Signature []byte `json:"signature"`
|
|
}
|
|
|
|
var ErrSignature = errors.New("invalid key package device signature")
|
|
var ErrPackageLimit = errors.New("device key package limit reached")
|
|
|
|
func (s *Store) AvailablePackages(ctx context.Context, device string) (int, error) {
|
|
var available int
|
|
err := s.db.QueryRow(ctx, `
|
|
SELECT count(*) FROM device_key_packages k JOIN devices d ON d.id=k.device_id
|
|
WHERE d.id=$1 AND d.revoked_at IS NULL AND k.claimed_at IS NULL
|
|
AND k.created_at > now() - interval '24 hours'
|
|
`, device).Scan(&available)
|
|
return available, err
|
|
}
|
|
|
|
func (s *Store) LocalIdentityKey(ctx context.Context, device string) ([]byte, error) {
|
|
var key []byte
|
|
err := s.db.QueryRow(ctx, "SELECT public_key FROM devices WHERE id=$1 AND revoked_at IS NULL", device).Scan(&key)
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return nil, ErrUnavailable
|
|
}
|
|
return key, err
|
|
}
|
|
|
|
const acceptedContact = `
|
|
EXISTS (SELECT 1 FROM contact_relationships
|
|
WHERE first_user=LEAST($1::uuid,$2::uuid) AND second_user=GREATEST($1::uuid,$2::uuid)
|
|
AND status='accepted')
|
|
`
|
|
|
|
func (s *Store) Directory(ctx context.Context, owner, contact string) ([]DirectoryDevice, error) {
|
|
rows, err := s.db.Query(ctx, `
|
|
SELECT id,name,devices.public_key, m.public_key, m.signature FROM devices
|
|
LEFT JOIN device_mls_credentials m ON m.device_id=devices.id
|
|
WHERE user_id=$2 AND revoked_at IS NULL AND `+acceptedContact+`
|
|
ORDER BY id LIMIT 128`, owner, contact)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
result := make([]DirectoryDevice, 0)
|
|
for rows.Next() {
|
|
var device DirectoryDevice
|
|
var credential Credential
|
|
if err := rows.Scan(&device.ID, &device.Name, &device.PublicKey, &credential.PublicKey, &credential.Signature); err != nil {
|
|
return nil, err
|
|
}
|
|
if credential.PublicKey != nil {
|
|
device.MLS = &credential
|
|
}
|
|
result = append(result, device)
|
|
}
|
|
return result, rows.Err()
|
|
}
|
|
|
|
func (s *Store) PublishCredential(ctx context.Context, owner, device string, credential Credential) error {
|
|
tx, err := s.db.Begin(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer tx.Rollback(ctx)
|
|
var public []byte
|
|
err = tx.QueryRow(ctx, "SELECT public_key FROM devices WHERE id=$1 AND user_id=$2 AND revoked_at IS NULL FOR UPDATE", device, owner).Scan(&public)
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return ErrUnavailable
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
binding := append([]byte("XHChat MLS credential v1\n"+owner+"\n"+device+"\n"), credential.PublicKey...)
|
|
if !ed25519.Verify(public, binding, credential.Signature) {
|
|
return ErrSignature
|
|
}
|
|
tag, err := tx.Exec(ctx, `
|
|
INSERT INTO device_mls_credentials VALUES($1,$2,$3)
|
|
ON CONFLICT(device_id) DO UPDATE SET signature=EXCLUDED.signature
|
|
WHERE device_mls_credentials.public_key=EXCLUDED.public_key
|
|
`, device, credential.PublicKey, credential.Signature)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if tag.RowsAffected() == 0 {
|
|
return ErrConflict
|
|
}
|
|
return tx.Commit(ctx)
|
|
}
|
|
|
|
func (s *Store) PublishPackage(ctx context.Context, owner, device string, item KeyPackage) error {
|
|
tx, err := s.db.Begin(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer tx.Rollback(ctx)
|
|
var public []byte
|
|
err = tx.QueryRow(ctx, "SELECT public_key FROM devices WHERE id=$1 AND user_id=$2 AND revoked_at IS NULL FOR UPDATE", device, owner).Scan(&public)
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return ErrUnavailable
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
binding := append([]byte("XHChat MLS KeyPackage v1\n"+owner+"\n"+device+"\n"), item.Package...)
|
|
if !ed25519.Verify(public, binding, item.Signature) {
|
|
return ErrSignature
|
|
}
|
|
hash := sha256.Sum256(item.Package)
|
|
var known bool
|
|
var available int
|
|
err = tx.QueryRow(ctx, `
|
|
SELECT count(*) FILTER (WHERE claimed_at IS NULL AND created_at > now() - interval '24 hours'),
|
|
COALESCE(bool_or(package_hash=$2),false)
|
|
FROM device_key_packages WHERE device_id=$1
|
|
`, device, hash[:]).Scan(&available, &known)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if known {
|
|
return tx.Commit(ctx)
|
|
}
|
|
if available >= 32 {
|
|
return ErrPackageLimit
|
|
}
|
|
if _, err := tx.Exec(ctx, "INSERT INTO device_key_packages(device_id,package_hash,package,signature) VALUES($1,$2,$3,$4)",
|
|
device, hash[:], item.Package, item.Signature); err != nil {
|
|
return err
|
|
}
|
|
return tx.Commit(ctx)
|
|
}
|
|
|
|
func (s *Store) ClaimPackage(ctx context.Context, owner, requester, contact, device string) (KeyPackage, error) {
|
|
var item KeyPackage
|
|
tx, err := s.db.Begin(ctx)
|
|
if err != nil {
|
|
return item, err
|
|
}
|
|
defer tx.Rollback(ctx)
|
|
if _, err := tx.Exec(ctx, "SELECT id FROM users WHERE id IN ($1,$2) ORDER BY id FOR UPDATE", owner, contact); err != nil {
|
|
return item, err
|
|
}
|
|
var friends bool
|
|
err = tx.QueryRow(ctx, "SELECT "+acceptedContact, owner, contact).Scan(&friends)
|
|
if err != nil {
|
|
return item, err
|
|
}
|
|
if !friends {
|
|
return item, ErrUnavailable
|
|
}
|
|
var active int
|
|
err = tx.QueryRow(ctx, `
|
|
SELECT count(*) FROM (
|
|
SELECT id FROM devices WHERE ((id=$1 AND user_id=$2) OR (id=$3 AND user_id=$4))
|
|
AND revoked_at IS NULL ORDER BY id FOR UPDATE
|
|
) AS active_devices
|
|
`, requester, owner, device, contact).Scan(&active)
|
|
if err != nil {
|
|
return item, err
|
|
}
|
|
if active != 2 {
|
|
return item, ErrUnavailable
|
|
}
|
|
item, err = TakePackage(ctx, tx, device)
|
|
if err != nil {
|
|
return item, err
|
|
}
|
|
return item, tx.Commit(ctx)
|
|
}
|
|
|
|
// TakePackage consumes a key package while the caller holds the device row lock.
|
|
func TakePackage(ctx context.Context, tx pgx.Tx, device string) (KeyPackage, error) {
|
|
var item KeyPackage
|
|
err := tx.QueryRow(ctx, `
|
|
UPDATE device_key_packages SET claimed_at=now()
|
|
WHERE (device_id, package_hash)=(
|
|
SELECT device_id, package_hash FROM device_key_packages
|
|
WHERE device_id=$1 AND claimed_at IS NULL AND created_at > now() - interval '24 hours'
|
|
ORDER BY created_at, package_hash LIMIT 1
|
|
)
|
|
RETURNING package, signature
|
|
`, device).Scan(&item.Package, &item.Signature)
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return item, ErrUnavailable
|
|
}
|
|
return item, err
|
|
}
|