Skip to content

Commit

Permalink
Update to latest signal protocol.
Browse files Browse the repository at this point in the history
- Remove legacy data messages
- Add digest for attachment uploads
- Update signal protocol buffers
  • Loading branch information
aebruno committed Nov 14, 2017
1 parent a288133 commit 2f2d099
Show file tree
Hide file tree
Showing 31 changed files with 2,322 additions and 1,056 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cmd/textsecure/textsecure
33 changes: 22 additions & 11 deletions attachments.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,21 @@ package textsecure

import (
"bytes"
"crypto/sha256"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"strconv"

"github.com/janimo/textsecure/protobuf"
textsecure "github.com/aebruno/textsecure/protobuf"
)

// getAttachment downloads an encrypted attachment blob from the given URL
func getAttachment(url string) (io.ReadCloser, error) {
req, err := http.NewRequest("GET", url, nil)
req.Header.Add("Content-type", "application/octet-stream")
req.Header.Add("Content-Type", "application/octet-stream")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
Expand All @@ -28,21 +29,25 @@ func getAttachment(url string) (io.ReadCloser, error) {
}

// putAttachment uploads an encrypted attachment to the given URL
func putAttachment(url string, body []byte) error {
func putAttachment(url string, body []byte) ([]byte, error) {
br := bytes.NewReader(body)
req, err := http.NewRequest("PUT", url, br)
if err != nil {
return err
return nil, err
}
req.Header.Add("Content-type", "application/octet-stream")
req.Header.Add("Content-length", strconv.Itoa(len(body)))
req.Header.Add("Content-Type", "application/octet-stream")
req.Header.Add("Content-Length", strconv.Itoa(len(body)))
resp, err := http.DefaultClient.Do(req)

if resp != nil && (resp.StatusCode < 200 || resp.StatusCode >= 300) {
return fmt.Errorf("HTTP status %d\n", resp.StatusCode)
return nil, fmt.Errorf("HTTP status %d\n", resp.StatusCode)
}

return err
defer resp.Body.Close()

hasher := sha256.New()
hasher.Write(body)

return hasher.Sum(nil), nil
}

// uploadAttachment encrypts, authenticates and uploads a given attachment to a location requested from the server
Expand All @@ -56,6 +61,8 @@ func uploadAttachment(r io.Reader, ct string) (*att, error) {
return nil, err
}

plaintextLength := len(b)

e, err := aesEncrypt(keys[:32], b)
if err != nil {
return nil, err
Expand All @@ -67,11 +74,12 @@ func uploadAttachment(r io.Reader, ct string) (*att, error) {
if err != nil {
return nil, err
}
err = putAttachment(location, m)
digest, err := putAttachment(location, m)
if err != nil {
return nil, err
}
return &att{id, ct, keys}, nil

return &att{id, ct, keys, digest, uint32(plaintextLength)}, nil
}

// ErrInvalidMACForAttachment signals that the downloaded attachment has an invalid MAC.
Expand Down Expand Up @@ -102,6 +110,9 @@ func handleSingleAttachment(a *textsecure.AttachmentPointer) (*Attachment, error
if err != nil {
return nil, err
}

// TODO: verify digest

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

Expand Down
10 changes: 5 additions & 5 deletions axolotl/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"crypto/hmac"
"errors"

protobuf "github.com/janimo/textsecure/axolotl/protobuf"
protobuf "github.com/aebruno/textsecure/axolotl/protobuf"

"github.com/golang/protobuf/proto"
)
Expand Down Expand Up @@ -45,7 +45,7 @@ func LoadWhisperMessage(serialized []byte) (*WhisperMessage, error) {
if version != currentVersion {
return nil, UnsupportedVersionError{version}
}
pwm := &protobuf.WhisperMessage{}
pwm := &protobuf.SignalMessage{}
err := proto.Unmarshal(message, pwm)
if err != nil {
return nil, err
Expand Down Expand Up @@ -73,7 +73,7 @@ func newWhisperMessage(messageVersion byte, macKey []byte, ratchetKey *ECPublicK

version := makeVersionByte(messageVersion, currentVersion)

pwm := &protobuf.WhisperMessage{
pwm := &protobuf.SignalMessage{
RatchetKey: ratchetKey.Serialize(),
Counter: &counter,
PreviousCounter: &previousCounter,
Expand Down Expand Up @@ -148,7 +148,7 @@ func LoadPreKeyWhisperMessage(serialized []byte) (*PreKeyWhisperMessage, error)
return nil, UnsupportedVersionError{version}
}

ppkwm := &protobuf.PreKeyWhisperMessage{}
ppkwm := &protobuf.PreKeySignalMessage{}
err := proto.Unmarshal(serialized[1:], ppkwm)
if err != nil {
return nil, err
Expand Down Expand Up @@ -181,7 +181,7 @@ func LoadPreKeyWhisperMessage(serialized []byte) (*PreKeyWhisperMessage, error)

func newPreKeyWhisperMessage(messageVersion byte, registrationID, preKeyID, signedPreKeyID uint32, baseKey *ECPublicKey, identityKey *IdentityKey, wm *WhisperMessage) (*PreKeyWhisperMessage, error) {

ppkwm := &protobuf.PreKeyWhisperMessage{
ppkwm := &protobuf.PreKeySignalMessage{
RegistrationId: &registrationID,
PreKeyId: &preKeyID,
SignedPreKeyId: &signedPreKeyID,
Expand Down
2 changes: 1 addition & 1 deletion axolotl/prekeys.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ package axolotl
// PreKey and SignedPreKey support

import (
protobuf "github.com/aebruno/textsecure/axolotl/protobuf"
"github.com/golang/protobuf/proto"
protobuf "github.com/janimo/textsecure/axolotl/protobuf"
)

var maxValue uint32 = 0xFFFFFF
Expand Down
88 changes: 88 additions & 0 deletions axolotl/protobuf/FingerprintProtocol.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions axolotl/protobuf/FingerprintProtocol.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package textsecure;

option java_package = "org.whispersystems.libsignal.fingerprint";
option java_outer_classname = "FingerprintProtos";

message LogicalFingerprint {
optional bytes content = 1;
// optional bytes identifier = 2;
}

message CombinedFingerprints {
optional uint32 version = 1;
optional LogicalFingerprint localFingerprint = 2;
optional LogicalFingerprint remoteFingerprint = 3;
}
Loading

0 comments on commit 2f2d099

Please sign in to comment.