-
Notifications
You must be signed in to change notification settings - Fork 248
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: encrypt
CommunityDescription
fields
Extended `CommunityDescription` with a `privateData` map. This map associates each hash ratchet `key_id` and `seq_no` with an encrypted `CommunityDescription`. Each encrypted instance includes only data requiring encryption. closes: status-im/status-desktop#12851 closes: status-im/status-desktop#12852 closes: status-im/status-desktop#12853
- Loading branch information
Showing
22 changed files
with
833 additions
and
288 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package communities | ||
|
||
import ( | ||
"github.com/golang/protobuf/proto" | ||
|
||
"go.uber.org/zap" | ||
|
||
"github.com/status-im/status-go/protocol/protobuf" | ||
) | ||
|
||
type DescriptionEncryptor interface { | ||
encryptCommunityDescription(community *Community, d *protobuf.CommunityDescription) (string, []byte, error) | ||
encryptCommunityDescriptionChannel(community *Community, channelID string, d *protobuf.CommunityDescription) (string, []byte, error) | ||
decryptCommunityDescription(keyIDSeqNo string, d []byte) (*protobuf.CommunityDescription, error) | ||
} | ||
|
||
// Encrypts members and chats | ||
func encryptDescription(encryptor DescriptionEncryptor, community *Community, description *protobuf.CommunityDescription) error { | ||
description.PrivateData = make(map[string][]byte) | ||
|
||
for channelID, channel := range description.Chats { | ||
if !community.channelEncrypted(channelID) { | ||
continue | ||
} | ||
|
||
descriptionToEncrypt := &protobuf.CommunityDescription{ | ||
Chats: map[string]*protobuf.CommunityChat{ | ||
channelID: proto.Clone(channel).(*protobuf.CommunityChat), | ||
}, | ||
} | ||
|
||
keyIDSeqNo, encryptedDescription, err := encryptor.encryptCommunityDescriptionChannel(community, channelID, descriptionToEncrypt) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Set private data and cleanup unencrypted channel's members | ||
description.PrivateData[keyIDSeqNo] = encryptedDescription | ||
channel.Members = make(map[string]*protobuf.CommunityMember) | ||
} | ||
|
||
if community.Encrypted() { | ||
descriptionToEncrypt := &protobuf.CommunityDescription{ | ||
Members: description.Members, | ||
Chats: description.Chats, | ||
} | ||
|
||
keyIDSeqNo, encryptedDescription, err := encryptor.encryptCommunityDescription(community, descriptionToEncrypt) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Set private data and cleanup unencrypted members and chats | ||
description.PrivateData[keyIDSeqNo] = encryptedDescription | ||
description.Members = make(map[string]*protobuf.CommunityMember) | ||
description.Chats = make(map[string]*protobuf.CommunityChat) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// Decrypts members and chats | ||
func decryptDescription(encryptor DescriptionEncryptor, description *protobuf.CommunityDescription, logger *zap.Logger) error { | ||
for keyIDSeqNo, encryptedDescription := range description.PrivateData { | ||
decryptedDescription, err := encryptor.decryptCommunityDescription(keyIDSeqNo, encryptedDescription) | ||
if err != nil { | ||
// ignore error, try to decrypt next data | ||
logger.Debug("failed to decrypt community private data", zap.String("keyIDSeqNo", keyIDSeqNo), zap.Error(err)) | ||
continue | ||
} | ||
|
||
for pk, member := range decryptedDescription.Members { | ||
if description.Members == nil { | ||
description.Members = make(map[string]*protobuf.CommunityMember) | ||
} | ||
description.Members[pk] = member | ||
} | ||
|
||
for id, decryptedChannel := range decryptedDescription.Chats { | ||
if description.Chats == nil { | ||
description.Chats = make(map[string]*protobuf.CommunityChat) | ||
} | ||
|
||
if channel := description.Chats[id]; channel != nil { | ||
if len(channel.Members) == 0 { | ||
channel.Members = decryptedChannel.Members | ||
} | ||
} else { | ||
description.Chats[id] = decryptedChannel | ||
} | ||
} | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.