Skip to content

Commit

Permalink
Add backward compatibility for single bundle (#1355)
Browse files Browse the repository at this point in the history
We change the protocol to accomodate publishing multiple bundles, in
order to propagate bundles for group chats and have a way to extend it
further.
This commit re-introduces backward compatibility for direct messages,
to be removed once that is not an issue anymore.
  • Loading branch information
cammellos authored Jan 21, 2019
1 parent ad51c01 commit 2df5422
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
16 changes: 11 additions & 5 deletions services/shhext/chat/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,21 @@ func NewProtocolService(encryption *EncryptionService, addedBundlesHandler func(
}
}

func (p *ProtocolService) addBundleAndMarshal(myIdentityKey *ecdsa.PrivateKey, msg *ProtocolMessage) ([]byte, error) {
func (p *ProtocolService) addBundleAndMarshal(myIdentityKey *ecdsa.PrivateKey, msg *ProtocolMessage, sendSingle bool) ([]byte, error) {
// Get a bundle
bundle, err := p.encryption.CreateBundle(myIdentityKey)
if err != nil {
p.log.Error("encryption-service", "error creating bundle", err)
return nil, err
}

msg.Bundles = []*Bundle{bundle}
if sendSingle {
// DEPRECATED: This is only for backward compatibility, remove once not
// an issue anymore
msg.Bundle = bundle
} else {
msg.Bundles = []*Bundle{bundle}
}

// marshal for sending to wire
marshaledMessage, err := proto.Marshal(msg)
Expand All @@ -52,7 +58,7 @@ func (p *ProtocolService) BuildPublicMessage(myIdentityKey *ecdsa.PrivateKey, pa
PublicMessage: payload,
}

return p.addBundleAndMarshal(myIdentityKey, protocolMessage)
return p.addBundleAndMarshal(myIdentityKey, protocolMessage, false)
}

// BuildDirectMessage marshals a 1:1 chat message given the user identity private key, the recipient's public key, and a payload
Expand All @@ -72,7 +78,7 @@ func (p *ProtocolService) BuildDirectMessage(myIdentityKey *ecdsa.PrivateKey, pa
DirectMessage: encryptionResponse,
}

payload, err := p.addBundleAndMarshal(myIdentityKey, protocolMessage)
payload, err := p.addBundleAndMarshal(myIdentityKey, protocolMessage, true)
if err != nil {
return nil, err
}
Expand All @@ -99,7 +105,7 @@ func (p *ProtocolService) BuildPairingMessage(myIdentityKey *ecdsa.PrivateKey, p
DirectMessage: encryptionResponse,
}

return p.addBundleAndMarshal(myIdentityKey, protocolMessage)
return p.addBundleAndMarshal(myIdentityKey, protocolMessage, true)
}

// ProcessPublicBundle processes a received X3DH bundle.
Expand Down
29 changes: 23 additions & 6 deletions services/shhext/chat/protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,28 @@ func (s *ProtocolServiceTestSuite) SetupTest() {
s.bob = NewProtocolService(NewEncryptionService(bobPersistence, DefaultEncryptionServiceConfig("2")), addedBundlesHandler)
}

func (s *ProtocolServiceTestSuite) TestBuildPublicMessage() {
aliceKey, err := crypto.GenerateKey()
s.NoError(err)

payload, err := proto.Marshal(&ChatMessagePayload{
Content: "Test content",
ClockValue: 1,
ContentType: "a",
MessageType: "some type",
})
s.NoError(err)

marshaledMsg, err := s.alice.BuildPublicMessage(aliceKey, payload)
s.NoError(err)
s.NotNil(marshaledMsg, "It creates a message")

unmarshaledMsg := &ProtocolMessage{}
err = proto.Unmarshal(marshaledMsg, unmarshaledMsg)
s.NoError(err)
s.NotNilf(unmarshaledMsg.GetBundles(), "It adds a bundle to the message")
}

func (s *ProtocolServiceTestSuite) TestBuildDirectMessage() {
bobKey, err := crypto.GenerateKey()
s.NoError(err)
Expand All @@ -59,24 +81,19 @@ func (s *ProtocolServiceTestSuite) TestBuildDirectMessage() {
s.NoError(err)

marshaledMsg, err := s.alice.BuildDirectMessage(aliceKey, payload, &bobKey.PublicKey, &aliceKey.PublicKey)

s.NoError(err)
s.NotNil(marshaledMsg, "It creates a message")
s.NotNil(marshaledMsg[&aliceKey.PublicKey], "It creates a single message")

unmarshaledMsg := &ProtocolMessage{}
err = proto.Unmarshal(marshaledMsg[&bobKey.PublicKey], unmarshaledMsg)

s.NoError(err)

s.NotNilf(unmarshaledMsg.GetBundles(), "It adds a bundle to the message")
s.NotNilf(unmarshaledMsg.GetBundle(), "It adds a bundle to the message")

directMessage := unmarshaledMsg.GetDirectMessage()

s.NotNilf(directMessage, "It sets the direct message")

encryptedPayload := directMessage["none"].GetPayload()

s.NotNilf(encryptedPayload, "It sets the payload of the message")

s.NotEqualf(payload, encryptedPayload, "It encrypts the payload")
Expand Down

0 comments on commit 2df5422

Please sign in to comment.