2019-07-08 09:21:21 +00:00
|
|
|
package encryption
|
|
|
|
|
|
|
|
import (
|
2019-07-16 10:43:07 +00:00
|
|
|
"io/ioutil"
|
2019-07-08 09:21:21 +00:00
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
2019-07-27 12:27:31 +00:00
|
|
|
migrations "github.com/status-im/status-protocol-go/encryption/migrations"
|
|
|
|
"github.com/status-im/status-protocol-go/sqlite"
|
|
|
|
|
2019-07-08 09:21:21 +00:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
|
|
"github.com/stretchr/testify/suite"
|
2019-07-18 13:41:48 +00:00
|
|
|
"go.uber.org/zap"
|
2019-07-08 09:21:21 +00:00
|
|
|
|
|
|
|
"github.com/status-im/status-protocol-go/encryption/multidevice"
|
|
|
|
"github.com/status-im/status-protocol-go/encryption/sharedsecret"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestProtocolServiceTestSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(ProtocolServiceTestSuite))
|
|
|
|
}
|
|
|
|
|
|
|
|
type ProtocolServiceTestSuite struct {
|
|
|
|
suite.Suite
|
2019-07-23 08:33:57 +00:00
|
|
|
aliceDBPath *os.File
|
|
|
|
bobDBPath *os.File
|
|
|
|
alice *Protocol
|
|
|
|
bob *Protocol
|
|
|
|
logger *zap.Logger
|
2019-07-08 09:21:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ProtocolServiceTestSuite) SetupTest() {
|
2019-07-16 10:43:07 +00:00
|
|
|
var err error
|
2019-07-08 09:21:21 +00:00
|
|
|
|
2019-07-18 13:41:48 +00:00
|
|
|
logger, err := zap.NewDevelopment()
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.logger = logger
|
|
|
|
|
2019-07-23 08:33:57 +00:00
|
|
|
s.aliceDBPath, err = ioutil.TempFile("", "alice.db.sql")
|
2019-07-16 10:43:07 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
aliceDBKey := "alice"
|
2019-07-08 09:21:21 +00:00
|
|
|
|
2019-07-23 08:33:57 +00:00
|
|
|
s.bobDBPath, err = ioutil.TempFile("", "bob.db.sql")
|
2019-07-16 10:43:07 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
bobDBKey := "bob"
|
2019-07-08 09:21:21 +00:00
|
|
|
|
|
|
|
addedBundlesHandler := func(addedBundles []*multidevice.Installation) {}
|
|
|
|
onNewSharedSecretHandler := func(secret []*sharedsecret.Secret) {}
|
|
|
|
|
2019-07-27 12:27:31 +00:00
|
|
|
db, err := sqlite.Open(s.aliceDBPath.Name(), aliceDBKey, sqlite.MigrationConfig{
|
|
|
|
AssetNames: migrations.AssetNames(),
|
|
|
|
AssetGetter: migrations.Asset,
|
|
|
|
})
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.alice = New(
|
|
|
|
db,
|
2019-07-16 10:43:07 +00:00
|
|
|
"1",
|
2019-07-08 09:21:21 +00:00
|
|
|
addedBundlesHandler,
|
|
|
|
onNewSharedSecretHandler,
|
2019-07-16 10:43:07 +00:00
|
|
|
func(*ProtocolMessageSpec) {},
|
2019-07-18 13:41:48 +00:00
|
|
|
logger.With(zap.String("user", "alice")),
|
2019-07-08 09:21:21 +00:00
|
|
|
)
|
|
|
|
|
2019-07-27 12:27:31 +00:00
|
|
|
db, err = sqlite.Open(s.bobDBPath.Name(), bobDBKey, sqlite.MigrationConfig{
|
|
|
|
AssetNames: migrations.AssetNames(),
|
|
|
|
AssetGetter: migrations.Asset,
|
|
|
|
})
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.bob = New(
|
|
|
|
db,
|
2019-07-16 10:43:07 +00:00
|
|
|
"2",
|
2019-07-08 09:21:21 +00:00
|
|
|
addedBundlesHandler,
|
|
|
|
onNewSharedSecretHandler,
|
2019-07-16 10:43:07 +00:00
|
|
|
func(*ProtocolMessageSpec) {},
|
2019-07-18 13:41:48 +00:00
|
|
|
logger.With(zap.String("user", "bob")),
|
2019-07-08 09:21:21 +00:00
|
|
|
)
|
2019-07-16 10:43:07 +00:00
|
|
|
}
|
2019-07-08 09:21:21 +00:00
|
|
|
|
2019-07-16 10:43:07 +00:00
|
|
|
func (s *ProtocolServiceTestSuite) TearDownTest() {
|
2019-07-23 08:33:57 +00:00
|
|
|
os.Remove(s.aliceDBPath.Name())
|
|
|
|
os.Remove(s.bobDBPath.Name())
|
2019-07-21 05:40:12 +00:00
|
|
|
_ = s.logger.Sync()
|
2019-07-08 09:21:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ProtocolServiceTestSuite) TestBuildPublicMessage() {
|
|
|
|
aliceKey, err := crypto.GenerateKey()
|
|
|
|
s.NoError(err)
|
|
|
|
|
|
|
|
payload := []byte("test")
|
|
|
|
s.NoError(err)
|
|
|
|
|
|
|
|
msg, err := s.alice.BuildPublicMessage(aliceKey, payload)
|
|
|
|
s.NoError(err)
|
|
|
|
s.NotNil(msg, "It creates a message")
|
|
|
|
|
2019-07-16 10:43:07 +00:00
|
|
|
s.NotNilf(msg.Message.GetBundles(), "It adds a bundle to the message")
|
2019-07-08 09:21:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ProtocolServiceTestSuite) TestBuildDirectMessage() {
|
|
|
|
bobKey, err := crypto.GenerateKey()
|
|
|
|
s.NoError(err)
|
|
|
|
aliceKey, err := crypto.GenerateKey()
|
|
|
|
s.NoError(err)
|
|
|
|
|
|
|
|
payload := []byte("test")
|
|
|
|
|
|
|
|
msgSpec, err := s.alice.BuildDirectMessage(aliceKey, &bobKey.PublicKey, payload)
|
|
|
|
s.NoError(err)
|
|
|
|
s.NotNil(msgSpec, "It creates a message spec")
|
|
|
|
|
|
|
|
msg := msgSpec.Message
|
|
|
|
s.NotNil(msg, "It creates a messages")
|
|
|
|
|
|
|
|
s.NotNilf(msg.GetBundle(), "It adds a bundle to the message")
|
|
|
|
|
|
|
|
directMessage := msg.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")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ProtocolServiceTestSuite) TestBuildAndReadDirectMessage() {
|
|
|
|
bobKey, err := crypto.GenerateKey()
|
|
|
|
s.Require().NoError(err)
|
|
|
|
aliceKey, err := crypto.GenerateKey()
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
payload := []byte("test")
|
|
|
|
|
|
|
|
// Message is sent with DH
|
|
|
|
msgSpec, err := s.alice.BuildDirectMessage(aliceKey, &bobKey.PublicKey, payload)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().NotNil(msgSpec)
|
|
|
|
|
|
|
|
msg := msgSpec.Message
|
|
|
|
s.Require().NotNil(msg)
|
|
|
|
|
|
|
|
// Bob is able to decrypt the message
|
|
|
|
unmarshaledMsg, err := s.bob.HandleMessage(bobKey, &aliceKey.PublicKey, msg, []byte("message-id"))
|
|
|
|
s.NoError(err)
|
|
|
|
s.NotNil(unmarshaledMsg)
|
|
|
|
|
|
|
|
recoveredPayload := []byte("test")
|
|
|
|
s.Equalf(payload, recoveredPayload, "It successfully unmarshal the decrypted message")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ProtocolServiceTestSuite) TestSecretNegotiation() {
|
|
|
|
var secretResponse []*sharedsecret.Secret
|
|
|
|
bobKey, err := crypto.GenerateKey()
|
|
|
|
s.NoError(err)
|
|
|
|
aliceKey, err := crypto.GenerateKey()
|
|
|
|
s.NoError(err)
|
|
|
|
|
|
|
|
payload := []byte("test")
|
|
|
|
|
|
|
|
s.bob.onNewSharedSecretHandler = func(secret []*sharedsecret.Secret) {
|
|
|
|
secretResponse = secret
|
|
|
|
}
|
|
|
|
msgSpec, err := s.alice.BuildDirectMessage(aliceKey, &bobKey.PublicKey, payload)
|
|
|
|
s.NoError(err)
|
|
|
|
s.NotNil(msgSpec, "It creates a message spec")
|
|
|
|
|
|
|
|
bundle := msgSpec.Message.GetBundle()
|
|
|
|
s.Require().NotNil(bundle)
|
|
|
|
|
|
|
|
signedPreKeys := bundle.GetSignedPreKeys()
|
|
|
|
s.Require().NotNil(signedPreKeys)
|
|
|
|
|
|
|
|
signedPreKey := signedPreKeys["1"]
|
|
|
|
s.Require().NotNil(signedPreKey)
|
|
|
|
|
|
|
|
s.Require().Equal(uint32(1), signedPreKey.GetProtocolVersion())
|
|
|
|
|
|
|
|
_, err = s.bob.HandleMessage(bobKey, &aliceKey.PublicKey, msgSpec.Message, []byte("message-id"))
|
|
|
|
s.NoError(err)
|
|
|
|
|
|
|
|
s.Require().NotNil(secretResponse)
|
|
|
|
}
|
2019-07-17 13:14:16 +00:00
|
|
|
|
|
|
|
func (s *ProtocolServiceTestSuite) TestPropagatingSavedSharedSecretsOnStart() {
|
|
|
|
var secretResponse []*sharedsecret.Secret
|
|
|
|
|
|
|
|
aliceKey, err := crypto.GenerateKey()
|
|
|
|
s.NoError(err)
|
|
|
|
bobKey, err := crypto.GenerateKey()
|
|
|
|
s.NoError(err)
|
|
|
|
|
|
|
|
// Generate and save a shared secret.
|
|
|
|
generatedSecret, err := s.alice.secret.Generate(aliceKey, &bobKey.PublicKey, "installation-1")
|
|
|
|
s.NoError(err)
|
|
|
|
|
|
|
|
s.alice.onNewSharedSecretHandler = func(secret []*sharedsecret.Secret) {
|
|
|
|
secretResponse = secret
|
|
|
|
}
|
|
|
|
|
|
|
|
err = s.alice.Start(aliceKey)
|
|
|
|
s.NoError(err)
|
|
|
|
|
|
|
|
s.Require().NotNil(secretResponse)
|
|
|
|
s.Require().Len(secretResponse, 1)
|
|
|
|
s.Equal(crypto.FromECDSAPub(generatedSecret.Identity), crypto.FromECDSAPub(secretResponse[0].Identity))
|
|
|
|
s.Equal(generatedSecret.Key, secretResponse[0].Key)
|
|
|
|
}
|