2018-03-09 20:44:13 +00:00
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
2020-07-14 00:46:05 +00:00
|
|
|
"cwtch.im/cwtch/protocol/groups"
|
|
|
|
"encoding/json"
|
2018-05-16 20:20:46 +00:00
|
|
|
"errors"
|
2018-03-09 20:44:13 +00:00
|
|
|
"fmt"
|
2020-02-10 22:09:24 +00:00
|
|
|
"git.openprivacy.ca/openprivacy/connectivity/tor"
|
|
|
|
"git.openprivacy.ca/openprivacy/log"
|
2018-03-30 21:16:51 +00:00
|
|
|
"golang.org/x/crypto/nacl/secretbox"
|
|
|
|
"io"
|
2018-05-06 04:18:00 +00:00
|
|
|
"sync"
|
2018-05-09 19:09:00 +00:00
|
|
|
"time"
|
2018-03-09 20:44:13 +00:00
|
|
|
)
|
|
|
|
|
2020-09-28 18:18:18 +00:00
|
|
|
// CurrentGroupVersion is used to set the version of newly created groups and make sure group structs stored are correct and up to date
|
|
|
|
const CurrentGroupVersion = 2
|
|
|
|
|
2018-10-05 03:18:34 +00:00
|
|
|
// Group defines and encapsulates Cwtch's conception of group chat. Which are sessions
|
2019-02-03 01:18:33 +00:00
|
|
|
// tied to a server under a given group key. Each group has a set of Messages.
|
2018-03-09 20:44:13 +00:00
|
|
|
type Group struct {
|
2019-02-20 20:03:04 +00:00
|
|
|
GroupID string
|
|
|
|
SignedGroupID []byte
|
|
|
|
GroupKey [32]byte
|
|
|
|
GroupServer string
|
2019-07-19 17:27:50 +00:00
|
|
|
Timeline Timeline `json:"-"`
|
2019-02-20 20:03:04 +00:00
|
|
|
Accepted bool
|
|
|
|
Owner string
|
|
|
|
IsCompromised bool
|
|
|
|
InitialMessage []byte
|
|
|
|
Attributes map[string]string
|
|
|
|
lock sync.Mutex
|
|
|
|
LocalID string
|
2019-05-15 20:12:11 +00:00
|
|
|
State string `json:"-"`
|
2020-10-07 20:53:22 +00:00
|
|
|
UnacknowledgedMessages []Message
|
2020-09-28 18:18:18 +00:00
|
|
|
Version int
|
2018-03-09 20:44:13 +00:00
|
|
|
}
|
|
|
|
|
2018-03-15 16:33:26 +00:00
|
|
|
// NewGroup initializes a new group associated with a given CwtchServer
|
2018-09-27 00:08:54 +00:00
|
|
|
func NewGroup(server string) (*Group, error) {
|
2018-03-09 20:44:13 +00:00
|
|
|
group := new(Group)
|
2020-09-28 18:18:18 +00:00
|
|
|
group.Version = CurrentGroupVersion
|
2019-12-12 20:21:14 +00:00
|
|
|
group.LocalID = GenerateRandomID()
|
2018-11-21 22:15:43 +00:00
|
|
|
|
2020-02-10 22:09:24 +00:00
|
|
|
if tor.IsValidHostname(server) == false {
|
2018-11-21 22:15:43 +00:00
|
|
|
return nil, errors.New("Server is not a valid v3 onion")
|
|
|
|
}
|
|
|
|
|
2018-03-09 20:44:13 +00:00
|
|
|
group.GroupServer = server
|
|
|
|
|
|
|
|
var groupID [16]byte
|
|
|
|
if _, err := io.ReadFull(rand.Reader, groupID[:]); err != nil {
|
2018-12-04 02:52:11 +00:00
|
|
|
log.Errorf("Cannot read from random: %v\n", err)
|
2018-09-27 00:08:54 +00:00
|
|
|
return nil, err
|
2018-03-09 20:44:13 +00:00
|
|
|
}
|
|
|
|
group.GroupID = fmt.Sprintf("%x", groupID)
|
|
|
|
|
|
|
|
var groupKey [32]byte
|
|
|
|
if _, err := io.ReadFull(rand.Reader, groupKey[:]); err != nil {
|
2018-12-04 02:52:11 +00:00
|
|
|
log.Errorf("Error: Cannot read from random: %v\n", err)
|
2018-09-27 00:08:54 +00:00
|
|
|
return nil, err
|
2018-03-09 20:44:13 +00:00
|
|
|
}
|
|
|
|
copy(group.GroupKey[:], groupKey[:])
|
2018-03-15 20:53:22 +00:00
|
|
|
group.Owner = "self"
|
2018-11-02 23:43:40 +00:00
|
|
|
group.Attributes = make(map[string]string)
|
2018-09-27 00:08:54 +00:00
|
|
|
return group, nil
|
2018-03-09 20:44:13 +00:00
|
|
|
}
|
|
|
|
|
2018-05-16 20:18:47 +00:00
|
|
|
// SignGroup adds a signature to the group.
|
2018-03-15 20:53:22 +00:00
|
|
|
func (g *Group) SignGroup(signature []byte) {
|
|
|
|
g.SignedGroupID = signature
|
2018-06-15 16:21:07 +00:00
|
|
|
copy(g.Timeline.SignedGroupID[:], g.SignedGroupID)
|
2018-03-15 20:53:22 +00:00
|
|
|
}
|
|
|
|
|
2018-05-20 18:38:56 +00:00
|
|
|
// Compromised should be called if we detect a a groupkey leak.
|
|
|
|
func (g *Group) Compromised() {
|
|
|
|
g.IsCompromised = true
|
|
|
|
}
|
|
|
|
|
2018-09-21 18:02:46 +00:00
|
|
|
// GetInitialMessage returns the first message of the group, if one was sent with the invite.
|
|
|
|
func (g *Group) GetInitialMessage() []byte {
|
|
|
|
g.lock.Lock()
|
|
|
|
defer g.lock.Unlock()
|
|
|
|
return g.InitialMessage
|
|
|
|
}
|
|
|
|
|
2018-05-16 20:18:47 +00:00
|
|
|
// Invite generates a invitation that can be sent to a cwtch peer
|
2018-09-21 18:02:46 +00:00
|
|
|
func (g *Group) Invite(initialMessage []byte) ([]byte, error) {
|
2018-05-16 20:18:47 +00:00
|
|
|
|
|
|
|
if g.SignedGroupID == nil {
|
|
|
|
return nil, errors.New("group isn't signed")
|
|
|
|
}
|
|
|
|
|
2018-09-21 18:02:46 +00:00
|
|
|
g.InitialMessage = initialMessage[:]
|
|
|
|
|
2020-07-14 00:46:05 +00:00
|
|
|
gci := &groups.GroupInvite{
|
2018-03-15 20:53:22 +00:00
|
|
|
GroupName: g.GroupID,
|
2020-07-14 00:46:05 +00:00
|
|
|
SharedKey: g.GroupKey[:],
|
2018-03-15 20:53:22 +00:00
|
|
|
ServerHost: g.GroupServer,
|
2020-07-14 00:46:05 +00:00
|
|
|
SignedGroupID: g.SignedGroupID[:],
|
2018-09-21 18:02:46 +00:00
|
|
|
InitialMessage: initialMessage[:],
|
2018-03-15 20:53:22 +00:00
|
|
|
}
|
2018-04-28 17:52:59 +00:00
|
|
|
|
2020-07-14 00:46:05 +00:00
|
|
|
invite, err := json.Marshal(gci)
|
2018-05-16 20:18:47 +00:00
|
|
|
return invite, err
|
2018-03-15 20:53:22 +00:00
|
|
|
}
|
|
|
|
|
2019-02-20 20:03:04 +00:00
|
|
|
// AddSentMessage takes a DecryptedGroupMessage and adds it to the Groups Timeline
|
2020-07-14 00:46:05 +00:00
|
|
|
func (g *Group) AddSentMessage(message *groups.DecryptedGroupMessage, sig []byte) Message {
|
2019-02-20 20:03:04 +00:00
|
|
|
g.lock.Lock()
|
|
|
|
defer g.lock.Unlock()
|
|
|
|
timelineMessage := Message{
|
2020-07-14 00:46:05 +00:00
|
|
|
Message: message.Text,
|
|
|
|
Timestamp: time.Unix(int64(message.Timestamp), 0),
|
2019-02-20 20:03:04 +00:00
|
|
|
Received: time.Unix(0, 0),
|
|
|
|
Signature: sig,
|
2020-07-14 00:46:05 +00:00
|
|
|
PeerID: message.Onion,
|
|
|
|
PreviousMessageSig: message.PreviousMessageSig,
|
2019-10-18 23:56:10 +00:00
|
|
|
ReceivedByServer: false,
|
2019-02-20 20:03:04 +00:00
|
|
|
}
|
2020-10-07 20:53:22 +00:00
|
|
|
g.UnacknowledgedMessages = append(g.UnacknowledgedMessages, timelineMessage)
|
2019-02-20 20:03:04 +00:00
|
|
|
return timelineMessage
|
|
|
|
}
|
|
|
|
|
2019-10-18 23:56:10 +00:00
|
|
|
// ErrorSentMessage removes a sent message from the unacknowledged list and sets its error flag if found, otherwise returns false
|
|
|
|
func (g *Group) ErrorSentMessage(sig []byte, error string) bool {
|
|
|
|
g.lock.Lock()
|
|
|
|
defer g.lock.Unlock()
|
|
|
|
var message *Message
|
|
|
|
|
|
|
|
// Delete the message from the unack'd buffer if it exists
|
2020-10-07 20:53:22 +00:00
|
|
|
for i, unAckedMessage := range g.UnacknowledgedMessages {
|
2019-10-18 23:56:10 +00:00
|
|
|
if compareSignatures(unAckedMessage.Signature, sig) {
|
|
|
|
message = &unAckedMessage
|
2020-10-07 20:53:22 +00:00
|
|
|
g.UnacknowledgedMessages = append(g.UnacknowledgedMessages[:i], g.UnacknowledgedMessages[i+1:]...)
|
2019-10-18 23:56:10 +00:00
|
|
|
|
|
|
|
message.Error = error
|
|
|
|
g.Timeline.Insert(message)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-05-16 20:18:47 +00:00
|
|
|
// AddMessage takes a DecryptedGroupMessage and adds it to the Groups Timeline
|
2020-07-14 00:46:05 +00:00
|
|
|
func (g *Group) AddMessage(message *groups.DecryptedGroupMessage, sig []byte) (*Message, bool) {
|
2019-02-20 20:03:04 +00:00
|
|
|
|
|
|
|
g.lock.Lock()
|
|
|
|
defer g.lock.Unlock()
|
|
|
|
|
|
|
|
// Delete the message from the unack'd buffer if it exists
|
2020-10-07 20:53:22 +00:00
|
|
|
for i, unAckedMessage := range g.UnacknowledgedMessages {
|
2019-02-20 20:03:04 +00:00
|
|
|
if compareSignatures(unAckedMessage.Signature, sig) {
|
2020-10-07 20:53:22 +00:00
|
|
|
g.UnacknowledgedMessages = append(g.UnacknowledgedMessages[:i], g.UnacknowledgedMessages[i+1:]...)
|
2019-02-20 20:03:04 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-31 19:33:32 +00:00
|
|
|
timelineMessage := &Message{
|
2020-07-14 00:46:05 +00:00
|
|
|
Message: message.Text,
|
|
|
|
Timestamp: time.Unix(int64(message.Timestamp), 0),
|
2018-05-09 19:09:00 +00:00
|
|
|
Received: time.Now(),
|
2018-06-22 18:11:23 +00:00
|
|
|
Signature: sig,
|
2020-07-14 00:46:05 +00:00
|
|
|
PeerID: message.Onion,
|
|
|
|
PreviousMessageSig: message.PreviousMessageSig,
|
2019-10-18 23:56:10 +00:00
|
|
|
ReceivedByServer: true,
|
|
|
|
Error: "",
|
2018-03-15 20:53:22 +00:00
|
|
|
}
|
2019-02-03 03:24:42 +00:00
|
|
|
seen := g.Timeline.Insert(timelineMessage)
|
2019-02-20 20:03:04 +00:00
|
|
|
|
2019-02-03 03:24:42 +00:00
|
|
|
return timelineMessage, seen
|
2018-03-15 20:53:22 +00:00
|
|
|
}
|
|
|
|
|
2018-12-03 19:12:34 +00:00
|
|
|
// GetTimeline provides a safe copy of the timeline
|
|
|
|
func (g *Group) GetTimeline() (timeline []Message) {
|
2018-05-06 04:18:00 +00:00
|
|
|
g.lock.Lock()
|
2018-12-03 19:12:34 +00:00
|
|
|
defer g.lock.Unlock()
|
2020-10-07 20:53:22 +00:00
|
|
|
return append(g.Timeline.GetMessages(), g.UnacknowledgedMessages...)
|
2018-03-09 20:44:13 +00:00
|
|
|
}
|
|
|
|
|
2018-03-15 16:33:26 +00:00
|
|
|
//EncryptMessage takes a message and encrypts the message under the group key.
|
2020-07-14 00:46:05 +00:00
|
|
|
func (g *Group) EncryptMessage(message *groups.DecryptedGroupMessage) ([]byte, error) {
|
2018-03-09 20:44:13 +00:00
|
|
|
var nonce [24]byte
|
|
|
|
if _, err := io.ReadFull(rand.Reader, nonce[:]); err != nil {
|
2018-12-04 02:52:11 +00:00
|
|
|
log.Errorf("Cannot read from random: %v\n", err)
|
2018-09-27 00:08:54 +00:00
|
|
|
return nil, err
|
2018-03-09 20:44:13 +00:00
|
|
|
}
|
2020-07-14 00:46:05 +00:00
|
|
|
wire, err := json.Marshal(message)
|
2019-11-08 00:39:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-03-15 20:53:22 +00:00
|
|
|
encrypted := secretbox.Seal(nonce[:], []byte(wire), &nonce, &g.GroupKey)
|
2018-09-27 00:08:54 +00:00
|
|
|
return encrypted, nil
|
2018-03-09 20:44:13 +00:00
|
|
|
}
|
|
|
|
|
2018-03-15 16:33:26 +00:00
|
|
|
// DecryptMessage takes a ciphertext and returns true and the decrypted message if the
|
|
|
|
// cipher text can be successfully decrypted,else false.
|
2020-07-14 00:46:05 +00:00
|
|
|
func (g *Group) DecryptMessage(ciphertext []byte) (bool, *groups.DecryptedGroupMessage) {
|
2019-11-08 00:39:27 +00:00
|
|
|
if len(ciphertext) > 24 {
|
|
|
|
var decryptNonce [24]byte
|
|
|
|
copy(decryptNonce[:], ciphertext[:24])
|
|
|
|
decrypted, ok := secretbox.Open(nil, ciphertext[24:], &decryptNonce, &g.GroupKey)
|
|
|
|
if ok {
|
2020-07-14 00:46:05 +00:00
|
|
|
dm := &groups.DecryptedGroupMessage{}
|
|
|
|
err := json.Unmarshal(decrypted, dm)
|
2019-11-08 00:39:27 +00:00
|
|
|
if err == nil {
|
|
|
|
return true, dm
|
|
|
|
}
|
2018-03-15 20:53:22 +00:00
|
|
|
}
|
2018-03-09 20:44:13 +00:00
|
|
|
}
|
2018-03-15 20:53:22 +00:00
|
|
|
return false, nil
|
2018-03-09 20:44:13 +00:00
|
|
|
}
|
2018-11-02 23:43:40 +00:00
|
|
|
|
|
|
|
// SetAttribute allows applications to store arbitrary configuration info at the group level.
|
|
|
|
func (g *Group) SetAttribute(name string, value string) {
|
|
|
|
g.lock.Lock()
|
|
|
|
defer g.lock.Unlock()
|
|
|
|
g.Attributes[name] = value
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAttribute returns the value of a value set with SetAttribute. If no such value has been set exists is set to false.
|
|
|
|
func (g *Group) GetAttribute(name string) (value string, exists bool) {
|
|
|
|
g.lock.Lock()
|
|
|
|
defer g.lock.Unlock()
|
|
|
|
value, exists = g.Attributes[name]
|
|
|
|
return
|
|
|
|
}
|