add shhext.InitProtocolWithEncryptionKey (#1362)

* add shhext.InitProtocolWithEncryptionKey

* update test to avoid lint error
This commit is contained in:
Andrea Franz 2019-01-25 11:31:51 +01:00 committed by GitHub
parent 6545c4a483
commit 0e0c3cd859
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 20 deletions

View File

@ -456,7 +456,7 @@ func (b *StatusBackend) SelectAccount(walletAddress, chatAddress, password strin
return err
}
if err := st.InitProtocol(chatAddress, password); err != nil {
if err := st.InitProtocolWithPassword(chatAddress, password); err != nil {
return err
}
}
@ -521,7 +521,7 @@ func (b *StatusBackend) InjectChatAccount(chatKeyHex, encryptionKeyHex string) e
return err
}
if err := st.InitProtocol(chatAccount.Address.Hex(), encryptionKeyHex); err != nil {
if err := st.InitProtocolWithEncyptionKey(chatAccount.Address.Hex(), encryptionKeyHex); err != nil {
return err
}
}

View File

@ -112,15 +112,23 @@ func (s *Service) Protocols() []p2p.Protocol {
return []p2p.Protocol{}
}
// InitProtocol create an instance of ProtocolService given an address and password
func (s *Service) InitProtocol(address string, password string) error {
// InitProtocolWithPassword creates an instance of ProtocolService given an address and password used to generate an encryption key.
func (s *Service) InitProtocolWithPassword(address string, password string) error {
digest := sha3.Sum256([]byte(password))
encKey := fmt.Sprintf("%x", digest)
return s.initProtocol(address, encKey, password)
}
// InitProtocolWithEncyptionKey creates an instance of ProtocolService given an address and encryption key.
func (s *Service) InitProtocolWithEncyptionKey(address string, encKey string) error {
return s.initProtocol(address, encKey, "")
}
func (s *Service) initProtocol(address, encKey, password string) error {
if !s.pfsEnabled {
return nil
}
digest := sha3.Sum256([]byte(password))
hashedPassword := fmt.Sprintf("%x", digest)
if err := os.MkdirAll(filepath.Clean(s.dataDir), os.ModePerm); err != nil {
return err
}
@ -130,29 +138,31 @@ func (s *Service) InitProtocol(address string, password string) error {
v3Path := filepath.Join(s.dataDir, fmt.Sprintf("%s.v3.db", s.installationID))
v4Path := filepath.Join(s.dataDir, fmt.Sprintf("%s.v4.db", s.installationID))
if err := chat.MigrateDBFile(v0Path, v1Path, "ON", password); err != nil {
return err
if password != "" {
if err := chat.MigrateDBFile(v0Path, v1Path, "ON", password); err != nil {
return err
}
if err := chat.MigrateDBFile(v1Path, v2Path, password, encKey); err != nil {
// Remove db file as created with a blank password and never used,
// and there's no need to rekey in this case
os.Remove(v1Path)
os.Remove(v2Path)
}
}
if err := chat.MigrateDBFile(v1Path, v2Path, password, hashedPassword); err != nil {
// Remove db file as created with a blank password and never used,
// and there's no need to rekey in this case
os.Remove(v1Path)
os.Remove(v2Path)
}
if err := chat.MigrateDBKeyKdfIterations(v2Path, v3Path, hashedPassword); err != nil {
if err := chat.MigrateDBKeyKdfIterations(v2Path, v3Path, encKey); err != nil {
os.Remove(v2Path)
os.Remove(v3Path)
}
// Fix IOS not encrypting database
if err := chat.EncryptDatabase(v3Path, v4Path, hashedPassword); err != nil {
if err := chat.EncryptDatabase(v3Path, v4Path, encKey); err != nil {
os.Remove(v3Path)
os.Remove(v4Path)
}
persistence, err := chat.NewSQLLitePersistence(v4Path, hashedPassword)
persistence, err := chat.NewSQLLitePersistence(v4Path, encKey)
if err != nil {
return err
}

View File

@ -22,6 +22,7 @@ import (
"github.com/stretchr/testify/suite"
"github.com/syndtr/goleveldb/leveldb"
"github.com/syndtr/goleveldb/leveldb/storage"
"golang.org/x/crypto/sha3"
)
const (
@ -126,7 +127,12 @@ func (s *ShhExtSuite) SetupTest() {
}
func (s *ShhExtSuite) TestInitProtocol() {
err := s.services[0].InitProtocol("example-address", "`090///\nhtaa\rhta9x8923)$$'23")
err := s.services[0].InitProtocolWithPassword("example-address", "`090///\nhtaa\rhta9x8923)$$'23")
s.NoError(err)
digest := sha3.Sum256([]byte("`090///\nhtaa\rhta9x8923)$$'23"))
encKey := fmt.Sprintf("%x", digest)
err = s.services[0].InitProtocolWithEncyptionKey("example-address", encKey)
s.NoError(err)
}