接通 Windows 与 Web 设备间加密文字聊天

This commit is contained in:
xhgit committed 2026-11-13 18:43:04 +08:00
1 parent b04034ee89
commit d1efdedc57
27 files changed
+880 -22

No files matched your search

+56 -4
View File
@@ -10,9 +10,15 @@ import (
)
type DirectoryDevice struct {
ID string `json:"id"`
Name string `json:"name"`
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 {
@@ -23,6 +29,15 @@ type KeyPackage struct {
var ErrSignature = errors.New("invalid key package device signature")
var ErrPackageLimit = errors.New("device key package limit reached")
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)
@@ -31,7 +46,8 @@ const acceptedContact = `
func (s *Store) Directory(ctx context.Context, owner, contact string) ([]DirectoryDevice, error) {
rows, err := s.db.Query(ctx, `
SELECT id,name,public_key FROM devices
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 {
@@ -41,14 +57,50 @@ func (s *Store) Directory(ctx context.Context, owner, contact string) ([]Directo
result := make([]DirectoryDevice, 0)
for rows.Next() {
var device DirectoryDevice
if err := rows.Scan(&device.ID, &device.Name, &device.PublicKey); err != nil {
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 {