Skip to content

Commit

Permalink
add support for uuids signal-golang#1
Browse files Browse the repository at this point in the history
  • Loading branch information
nanu-c committed Dec 28, 2020
1 parent 0576dd9 commit 0221e4e
Show file tree
Hide file tree
Showing 14 changed files with 371 additions and 144 deletions.
32 changes: 32 additions & 0 deletions attachments.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"net/http"
"strconv"

signalservice "github.com/signal-golang/textsecure/protobuf"
textsecure "github.com/signal-golang/textsecure/protobuf"
)

Expand Down Expand Up @@ -157,6 +158,37 @@ func handleSingleAttachment(a *textsecure.AttachmentPointer) (*Attachment, error

return &Attachment{bytes.NewReader(b), a.GetContentType(), a.GetFileName()}, nil
}
func handleProfileAvatar(profileAvatar *signalservice.ContactDetails_Avatar, key []byte) (*Attachment, error) {

loc, err := getProfileLocation(profileAvatar.String())
if err != nil {
return nil, err
}
r, err := getAttachment(loc)
if err != nil {
return nil, err
}
defer r.Close()

b, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}

l := len(b) - 16
if !verifyMAC(key[16:], b[:l], b[l:]) {
return nil, ErrInvalidMACForAttachment
}

b, err = aesDecrypt(key[:16], b[:l])
if err != nil {
return nil, err
}

// TODO: verify digest

return &Attachment{bytes.NewReader(b), profileAvatar.GetContentType(), ""}, nil
}

func handleAttachments(dm *textsecure.DataMessage) ([]*Attachment, error) {
atts := dm.GetAttachments()
Expand Down
7 changes: 4 additions & 3 deletions axolotl/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import (
"errors"
"fmt"

"github.com/golang/protobuf/proto"
protobuf "github.com/signal-golang/textsecure/axolotl/protobuf"
"github.com/signal-golang/textsecure/curve25519sign"
"github.com/signal-golang/textsecure/protobuf"
"github.com/golang/protobuf/proto"
signalservice "github.com/signal-golang/textsecure/protobuf"
log "github.com/sirupsen/logrus"
)

type sessionState struct {
Expand Down Expand Up @@ -658,7 +659,7 @@ func (ss *sessionState) decrypt(ciphertext *WhisperMessage) ([]byte, error) {
func (sc *SessionCipher) SessionDecryptWhisperMessage(ciphertext *WhisperMessage) ([]byte, error) {
sc.SessionStore.Lock()
defer sc.SessionStore.Unlock()

log.Debugln("a", sc.RecipientID)
sr, err := sc.SessionStore.LoadSession(sc.RecipientID, sc.DeviceID)
if err != nil {
return nil, err
Expand Down
3 changes: 2 additions & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import (

// Config holds application configuration settings
type Config struct {
Tel string `yaml:"tel"` // Our telephone number
Tel string `yaml:"tel"` // Our telephone number
UUID string `yaml:"uuid"`
Server string `yaml:"server"` // The TextSecure server URL
RootCA string `yaml:"rootCA"` // The TLS signing certificate of the server we connect to
ProxyServer string `yaml:"proxy"` // HTTP Proxy URL if one is being used
Expand Down
72 changes: 46 additions & 26 deletions contacts.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,31 @@
package textsecure

import (
"bytes"
"io"
"io/ioutil"

"github.com/signal-golang/textsecure/protobuf"
signalservice "github.com/signal-golang/textsecure/protobuf"
log "github.com/sirupsen/logrus"

"gopkg.in/yaml.v2"
)

// Contact contains information about a contact.
type Contact struct {
Tel string
Uuid string
Name string
Color string
Avatar []byte
Blocked bool
ExpireTimer uint32
UUID string
Tel string
ProfileKey []byte
IdentityKey []byte
Name string
Username string
Avatar []byte
Color string
Blocked bool
Verified *signalservice.Verified
ExpireTimer uint32
InboxPosition uint32
Archived bool
}

type yamlContacts struct {
Expand All @@ -41,6 +49,7 @@ func loadContacts(contactsYaml *yamlContacts) {

var filePath string

// ReadContacts loads the contacts yaml file and pareses it
func ReadContacts(fileName string) ([]Contact, error) {
b, err := ioutil.ReadFile(fileName)
filePath = fileName
Expand All @@ -66,6 +75,8 @@ func WriteContacts(filename string, contacts2 []Contact) error {
}
return ioutil.WriteFile(filename, b, 0600)
}

// WriteContactsToPath saves a list of contacts to a file at the standard location
func WriteContactsToPath() error {
c := contactsToYaml()
b, err := yaml.Marshal(c)
Expand All @@ -89,29 +100,36 @@ func contactsToYaml() *yamlContacts {
func updateContact(c *signalservice.ContactDetails) error {
log.Debugln("[textsecure] updateContact ", c.GetName())

// var r io.Reader
// av := c.GetAvatar()
// buf := new(bytes.Buffer)
// if av != nil {
// att, err := handleSingleAttachment(av)
// if err != nil {
// return err
// }
// r = att.R
// buf.ReadFrom(r)
// }
var r io.Reader
av := c.GetAvatar()
buf := new(bytes.Buffer)
if av != nil {
att, err := handleProfileAvatar(av, c.GetProfileKey())
if err != nil {
return err
}
r = att.R
buf.ReadFrom(r)
}
avatar, _ := ioutil.ReadAll(buf)

contacts[c.GetNumber()] = Contact{
Tel: c.GetNumber(),
Uuid: c.GetUuid(),
Name: c.GetName(),
Color: c.GetColor(),
Avatar: []byte(""),
Blocked: c.GetBlocked(),
ExpireTimer: c.GetExpireTimer(),
Tel: c.GetNumber(),
UUID: c.GetUuid(),
Name: c.GetName(),
Avatar: avatar,
Color: c.GetColor(),
Verified: c.GetVerified(),
ProfileKey: c.GetProfileKey(),
Blocked: c.GetBlocked(),
ExpireTimer: c.GetExpireTimer(),
InboxPosition: c.GetInboxPosition(),
Archived: c.GetArchived(),
}
log.Debugln(c.GetAvatar(), buf)
return WriteContactsToPath()
}

func handleContacts(src string, dm *signalservice.DataMessage) ([]*signalservice.DataMessage_Contact, error) {
cs := dm.GetContact()
if cs == nil {
Expand Down Expand Up @@ -145,6 +163,8 @@ func handleContacts(src string, dm *signalservice.DataMessage) ([]*signalservice

return nil, nil
}

// RequestContactInfo sends
func RequestContactInfo() error {
var t signalservice.SyncMessage_Request_Type
t = 1
Expand Down
30 changes: 28 additions & 2 deletions crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ import (
"fmt"
"io"

"github.com/signal-golang/textsecure/axolotl"
"github.com/signal-golang/textsecure/protobuf"
"github.com/golang/protobuf/proto"
"github.com/signal-golang/textsecure/axolotl"
signalservice "github.com/signal-golang/textsecure/protobuf"
log "github.com/sirupsen/logrus"
"golang.org/x/crypto/curve25519"
)

Expand Down Expand Up @@ -87,6 +88,8 @@ func aesDecrypt(key, ciphertext []byte) ([]byte, error) {
}

if len(ciphertext)%aes.BlockSize != 0 {
length := len(ciphertext) % aes.BlockSize
log.Debugln("[textsecure] aesDecrypt ciphertext not multiple of AES blocksize", length)
return nil, errors.New("ciphertext not multiple of AES blocksize")
}

Expand All @@ -100,6 +103,29 @@ func aesDecrypt(key, ciphertext []byte) ([]byte, error) {
return ciphertext[aes.BlockSize : len(ciphertext)-int(pad)], nil
}

func aesCtrNoPaddingDecrypt(key, ciphertext []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}

if len(ciphertext)%aes.BlockSize != 0 {
length := len(ciphertext) % aes.BlockSize
log.Debugln("[textsecure] aesDecrypt ciphertext not multiple of AES blocksize", length)
return nil, errors.New("ciphertext not multiple of AES blocksize")
}

iv := ciphertext[:aes.BlockSize]
mode := cipher.NewCBCDecrypter(block, iv)
// CryptBlocks can work in-place if the two arguments are the same.
mode.CryptBlocks(ciphertext, ciphertext)
// s := string(ciphertext[:])

return ciphertext, nil

// return ciphertext[aes.BlockSize : len(ciphertext)-int(pad)], nil
}

// ProvisioningCipher
func provisioningCipher(pm *signalservice.ProvisionMessage, theirPublicKey *axolotl.ECPublicKey) ([]byte, error) {
ourKeyPair := axolotl.GenerateIdentityKeyPair()
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ require (
golang.org/x/net v0.0.0-20200625001655-4c5254603344 // indirect
golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae // indirect
golang.org/x/text v0.3.2
google.golang.org/protobuf v1.25.0 // indirect
google.golang.org/protobuf v1.25.0
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
gopkg.in/yaml.v2 v2.3.0
)
33 changes: 20 additions & 13 deletions groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (

log "github.com/sirupsen/logrus"

"github.com/signal-golang/textsecure/protobuf"
signalservice "github.com/signal-golang/textsecure/protobuf"
"gopkg.in/yaml.v2"
)

Expand Down Expand Up @@ -74,6 +74,8 @@ func loadGroup(path string) error {
groups[hexid] = group
return nil
}

// RemoveGroupKey removes the group key
func RemoveGroupKey(hexid string) error {
err := os.Remove(config.StorageDir + "/groups/" + hexid)
if err != nil {
Expand Down Expand Up @@ -163,7 +165,7 @@ func quitGroup(src string, hexid string) error {
// GroupUpdateFlag signals that this message updates the group membership or name.
var GroupUpdateFlag uint32 = 1

// GroupLeavelag signals that this message is a group leave message
// GroupLeaveFlag signals that this message is a group leave message
var GroupLeaveFlag uint32 = 2

// handleGroups is the main entry point for handling the group metadata on messages.
Expand Down Expand Up @@ -232,7 +234,7 @@ func sendGroupHelper(hexid string, msg string, a *att, timer uint32) (uint64, er
for _, m := range g.Members {
if m != config.Tel {
omsg := &outgoingMessage{
tel: m,
destination: m,
msg: msg,
attachment: a,
expireTimer: timer,
Expand All @@ -246,10 +248,9 @@ func sendGroupHelper(hexid string, msg string, a *att, timer uint32) (uint64, er
if err != nil {
log.Errorln("[textsecure] sendGroupHelper", err, m)
return 0, err
} else {
log.Debugln("[textsecure] sendGroupHelper message to group sent", m)

}
log.Debugln("[textsecure] sendGroupHelper message to group sent", m)

}
}
return ts, nil
Expand All @@ -269,6 +270,8 @@ func SendGroupAttachment(hexid string, msg string, r io.Reader, timer uint32) (u
}
return sendGroupHelper(hexid, msg, a, timer)
}

// SendGroupVoiceNote sends an voice note to a group
func SendGroupVoiceNote(hexid string, msg string, r io.Reader, timer uint32) (uint64, error) {
ct, r := MIMETypeFromReader(r)
a, err := uploadVoiceNote(r, ct)
Expand Down Expand Up @@ -316,7 +319,7 @@ func sendUpdate(g *Group) error {
for _, m := range g.Members {
if m != config.Tel {
omsg := &outgoingMessage{
tel: m,
destination: m,
group: &groupMessage{
id: g.ID,
name: g.Name,
Expand Down Expand Up @@ -347,13 +350,15 @@ func newGroup(name string, members []string) (*Group, error) {
}
return groups[hexid], nil
}

// RequestGroupInfo updates the info for the group like members or the avatat
func RequestGroupInfo(g *Group) error {
log.Debugln("[textsecure] request group update", g.Hexid)
for _, m := range g.Members {
if m != config.Tel {
log.Debugln(m)
omsg := &outgoingMessage{
tel: m,
destination: m,
group: &groupMessage{
id: g.ID,
typ: signalservice.GroupContext_REQUEST_INFO,
Expand All @@ -367,7 +372,7 @@ func RequestGroupInfo(g *Group) error {
}
if len(g.Members) == 0 {
omsg := &outgoingMessage{
tel: config.Tel,
destination: config.Tel,
group: &groupMessage{
id: g.ID,
typ: signalservice.GroupContext_REQUEST_INFO,
Expand Down Expand Up @@ -409,10 +414,12 @@ func removeGroup(id []byte) error {
}
return nil
}
func GetGroupById(hexId string) (*Group, error) {
g, ok := groups[hexId]

// GetGroupById returns a group by it's id
func GetGroupById(hexID string) (*Group, error) {
g, ok := groups[hexID]
if !ok {
return nil, UnknownGroupIDError{hexId}
return nil, UnknownGroupIDError{hexID}
}
return g, nil
}
Expand All @@ -427,7 +434,7 @@ func LeaveGroup(hexid string) error {
for _, m := range g.Members {
if m != config.Tel {
omsg := &outgoingMessage{
tel: m,
destination: m,
group: &groupMessage{
id: g.ID,
typ: signalservice.GroupContext_QUIT,
Expand Down
4 changes: 1 addition & 3 deletions link.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

var code2 string

// AddNewLinkedDevice adds a new signal desktop client
func AddNewLinkedDevice(uuid string, publicKey string) error {
log.Printf("AddNewLinkedDevice")
if code2 == "" {
Expand All @@ -17,9 +18,6 @@ func AddNewLinkedDevice(uuid string, publicKey string) error {
}
code2 = code
}
// log.Printf("code: " + code2)
// log.Printf("uuid: " + uuid)
// log.Printf("publicKey: " + publicKey)

err := addNewDevice(uuid, publicKey, code2)
if err != nil {
Expand Down
Loading

0 comments on commit 0221e4e

Please sign in to comment.