-
Notifications
You must be signed in to change notification settings - Fork 247
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: introduce CommunitiesKeyDistributor
This component decouples key distribution from the Messenger, enhancing code maintainability, extensibility and testability. It also alleviates the need to impact all methods potentially affecting encryption keys. Moreover, it allows key distribution inspection for integration tests. part of: status-im/status-desktop#10998
- Loading branch information
Showing
10 changed files
with
427 additions
and
208 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
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
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,120 @@ | ||
package protocol | ||
|
||
import ( | ||
"context" | ||
"crypto/ecdsa" | ||
|
||
"github.com/status-im/status-go/protocol/common" | ||
"github.com/status-im/status-go/protocol/communities" | ||
"github.com/status-im/status-go/protocol/encryption" | ||
"github.com/status-im/status-go/protocol/protobuf" | ||
) | ||
|
||
type CommunitiesKeyDistributor interface { | ||
Distribute(community *communities.Community, keyActions *communities.EncryptionKeyActions) error | ||
Rekey(community *communities.Community) error | ||
} | ||
|
||
type CommunitiesKeyDistributorImpl struct { | ||
sender *common.MessageSender | ||
encryptor *encryption.Protocol | ||
} | ||
|
||
func (ckd *CommunitiesKeyDistributorImpl) Distribute(community *communities.Community, keyActions *communities.EncryptionKeyActions) error { | ||
if !community.IsOwner() { | ||
return communities.ErrNotOwner | ||
} | ||
|
||
err := ckd.distributeKey(community.ID(), community.ID(), &keyActions.CommunityKeyAction) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for chatID := range keyActions.ChannelKeysActions { | ||
keyAction := keyActions.ChannelKeysActions[chatID] | ||
err := ckd.distributeKey(community.ID(), []byte(chatID), &keyAction) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (ckd *CommunitiesKeyDistributorImpl) Rekey(community *communities.Community) error { | ||
if !community.IsOwner() { | ||
return communities.ErrNotOwner | ||
} | ||
|
||
err := ckd.distributeKey(community.ID(), community.ID(), &communities.EncryptionKeyAction{ | ||
ActionType: communities.EncryptionKeyRekey, | ||
Members: community.Members(), | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for channelID, channel := range community.Chats() { | ||
err := ckd.distributeKey(community.ID(), []byte(community.IDString()+channelID), &communities.EncryptionKeyAction{ | ||
ActionType: communities.EncryptionKeyRekey, | ||
Members: channel.Members, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (ckd *CommunitiesKeyDistributorImpl) distributeKey(communityID, hashRatchetGroupID []byte, keyAction *communities.EncryptionKeyAction) error { | ||
pubkeys := make([]*ecdsa.PublicKey, len(keyAction.Members)) | ||
i := 0 | ||
for hex := range keyAction.Members { | ||
pubkeys[i], _ = common.HexToPubkey(hex) | ||
i++ | ||
} | ||
|
||
switch keyAction.ActionType { | ||
case communities.EncryptionKeyAdd: | ||
_, err := ckd.encryptor.GenerateHashRatchetKey(hashRatchetGroupID) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = ckd.sendKeyExchangeMessage(communityID, hashRatchetGroupID, pubkeys, common.KeyExMsgReuse) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
case communities.EncryptionKeyRekey: | ||
err := ckd.sendKeyExchangeMessage(communityID, hashRatchetGroupID, pubkeys, common.KeyExMsgRekey) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
case communities.EncryptionKeySendToMembers: | ||
err := ckd.sendKeyExchangeMessage(communityID, hashRatchetGroupID, pubkeys, common.KeyExMsgReuse) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (ckd *CommunitiesKeyDistributorImpl) sendKeyExchangeMessage(communityID, hashRatchetGroupID []byte, pubkeys []*ecdsa.PublicKey, msgType common.CommKeyExMsgType) error { | ||
rawMessage := common.RawMessage{ | ||
SkipProtocolLayer: false, | ||
CommunityID: communityID, | ||
CommunityKeyExMsgType: msgType, | ||
Recipients: pubkeys, | ||
MessageType: protobuf.ApplicationMetadataMessage_CHAT_MESSAGE, | ||
} | ||
_, err := ckd.sender.SendCommunityMessage(context.Background(), rawMessage) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
Oops, something went wrong.