2018-03-09 20:44:13 +00:00
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
2018-05-16 20:20:46 +00:00
|
|
|
"errors"
|
2018-03-09 20:44:13 +00:00
|
|
|
"fmt"
|
2018-03-15 20:53:22 +00:00
|
|
|
"git.mascherari.press/cwtch/protocol"
|
2018-03-30 21:16:51 +00:00
|
|
|
"github.com/golang/protobuf/proto"
|
2018-03-15 20:53:22 +00:00
|
|
|
"github.com/s-rah/go-ricochet/utils"
|
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
|
|
|
)
|
|
|
|
|
2018-03-15 16:33:26 +00:00
|
|
|
//Group defines and encapsulates Cwtch's conception of group chat. Which are sessions
|
|
|
|
// 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 {
|
2018-03-15 20:53:22 +00:00
|
|
|
GroupID string
|
|
|
|
SignedGroupID []byte
|
|
|
|
GroupKey [32]byte
|
|
|
|
GroupServer string
|
2018-04-02 21:10:29 +00:00
|
|
|
Timeline Timeline
|
2018-03-15 20:53:22 +00:00
|
|
|
Accepted bool
|
|
|
|
Owner string
|
2018-05-20 18:38:56 +00:00
|
|
|
IsCompromised bool
|
2018-05-09 19:09:00 +00:00
|
|
|
lock sync.Mutex
|
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-03-09 20:44:13 +00:00
|
|
|
func NewGroup(server string) *Group {
|
|
|
|
group := new(Group)
|
|
|
|
group.GroupServer = server
|
|
|
|
|
|
|
|
var groupID [16]byte
|
|
|
|
if _, err := io.ReadFull(rand.Reader, groupID[:]); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
group.GroupID = fmt.Sprintf("%x", groupID)
|
|
|
|
|
|
|
|
var groupKey [32]byte
|
|
|
|
if _, err := io.ReadFull(rand.Reader, groupKey[:]); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
copy(group.GroupKey[:], groupKey[:])
|
2018-03-15 20:53:22 +00:00
|
|
|
group.Owner = "self"
|
2018-03-09 20:44:13 +00:00
|
|
|
return group
|
|
|
|
}
|
|
|
|
|
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-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-05-16 20:18:47 +00:00
|
|
|
// Invite generates a invitation that can be sent to a cwtch peer
|
|
|
|
func (g *Group) Invite() ([]byte, error) {
|
|
|
|
|
|
|
|
if g.SignedGroupID == nil {
|
|
|
|
return nil, errors.New("group isn't signed")
|
|
|
|
}
|
|
|
|
|
2018-03-15 20:53:22 +00:00
|
|
|
gci := &protocol.GroupChatInvite{
|
|
|
|
GroupName: g.GroupID,
|
|
|
|
GroupSharedKey: g.GroupKey[:],
|
|
|
|
ServerHost: g.GroupServer,
|
2018-03-30 21:16:51 +00:00
|
|
|
SignedGroupId: g.SignedGroupID[:],
|
2018-03-15 20:53:22 +00:00
|
|
|
}
|
2018-04-28 17:52:59 +00:00
|
|
|
|
2018-03-15 20:53:22 +00:00
|
|
|
cp := &protocol.CwtchPeerPacket{
|
|
|
|
GroupChatInvite: gci,
|
|
|
|
}
|
|
|
|
invite, err := proto.Marshal(cp)
|
2018-05-16 20:18:47 +00:00
|
|
|
return invite, err
|
2018-03-15 20:53:22 +00:00
|
|
|
}
|
|
|
|
|
2018-05-16 20:18:47 +00:00
|
|
|
// AddMessage takes a DecryptedGroupMessage and adds it to the Groups Timeline
|
2018-03-31 19:33:32 +00:00
|
|
|
func (g *Group) AddMessage(message *protocol.DecryptedGroupMessage, verified bool) *Message {
|
2018-05-06 04:18:00 +00:00
|
|
|
g.lock.Lock()
|
2018-03-31 19:33:32 +00:00
|
|
|
timelineMessage := &Message{
|
2018-04-02 21:10:29 +00:00
|
|
|
Message: message.GetText(),
|
|
|
|
Timestamp: time.Unix(int64(message.GetTimestamp()), 0),
|
2018-05-09 19:09:00 +00:00
|
|
|
Received: time.Now(),
|
2018-04-02 21:10:29 +00:00
|
|
|
Signature: message.GetSignature(),
|
|
|
|
Verified: verified,
|
|
|
|
PeerID: message.GetOnion(),
|
|
|
|
PreviousMessageSig: message.GetPreviousMessageSig(),
|
2018-03-15 20:53:22 +00:00
|
|
|
}
|
2018-04-02 21:10:29 +00:00
|
|
|
g.Timeline.Insert(timelineMessage)
|
2018-05-06 04:18:00 +00:00
|
|
|
g.lock.Unlock()
|
2018-03-31 19:33:32 +00:00
|
|
|
return timelineMessage
|
2018-03-15 20:53:22 +00:00
|
|
|
}
|
|
|
|
|
2018-05-16 20:18:47 +00:00
|
|
|
// GetTimeline provides a safe copy of the timeline
|
2018-05-20 18:29:46 +00:00
|
|
|
func (g *Group) GetTimeline() (t []Message) {
|
2018-05-06 04:18:00 +00:00
|
|
|
g.lock.Lock()
|
2018-05-20 18:29:46 +00:00
|
|
|
t = g.Timeline.GetMessages()
|
2018-05-06 04:18:00 +00:00
|
|
|
g.lock.Unlock()
|
|
|
|
return
|
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.
|
2018-03-15 20:53:22 +00:00
|
|
|
func (g *Group) EncryptMessage(message *protocol.DecryptedGroupMessage) []byte {
|
2018-03-09 20:44:13 +00:00
|
|
|
var nonce [24]byte
|
|
|
|
if _, err := io.ReadFull(rand.Reader, nonce[:]); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-03-30 21:16:51 +00:00
|
|
|
wire, err := proto.Marshal(message)
|
2018-03-15 20:53:22 +00:00
|
|
|
utils.CheckError(err)
|
|
|
|
encrypted := secretbox.Seal(nonce[:], []byte(wire), &nonce, &g.GroupKey)
|
2018-03-09 20:44:13 +00:00
|
|
|
return encrypted
|
|
|
|
}
|
|
|
|
|
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.
|
2018-03-15 20:53:22 +00:00
|
|
|
func (g *Group) DecryptMessage(ciphertext []byte) (bool, *protocol.DecryptedGroupMessage) {
|
2018-03-09 20:44:13 +00:00
|
|
|
var decryptNonce [24]byte
|
|
|
|
copy(decryptNonce[:], ciphertext[:24])
|
|
|
|
decrypted, ok := secretbox.Open(nil, ciphertext[24:], &decryptNonce, &g.GroupKey)
|
|
|
|
if ok {
|
2018-03-15 20:53:22 +00:00
|
|
|
dm := &protocol.DecryptedGroupMessage{}
|
|
|
|
err := proto.Unmarshal(decrypted, dm)
|
|
|
|
if err == nil {
|
|
|
|
return true, dm
|
|
|
|
}
|
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
|
|
|
}
|