aboutsummaryrefslogtreecommitdiffstats
path: root/java/src/main/java/org/whispersystems/libaxolotl/groups/GroupSessionBuilder.java
blob: 8b73484b28ade646a825aa1c32736b6a60aacac1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package org.whispersystems.libaxolotl.groups;

import org.whispersystems.libaxolotl.ecc.ECKeyPair;
import org.whispersystems.libaxolotl.groups.state.SenderKeyRecord;
import org.whispersystems.libaxolotl.groups.state.SenderKeyStore;
import org.whispersystems.libaxolotl.protocol.SenderKeyDistributionMessage;

public class GroupSessionBuilder {

  private final SenderKeyStore senderKeyStore;

  public GroupSessionBuilder(SenderKeyStore senderKeyStore) {
    this.senderKeyStore = senderKeyStore;
  }

  public void process(String sender, SenderKeyDistributionMessage senderKeyDistributionMessage) {
    synchronized (GroupCipher.LOCK) {
      SenderKeyRecord senderKeyRecord = senderKeyStore.loadSenderKey(sender);
      senderKeyRecord.addSenderKeyState(senderKeyDistributionMessage.getId(),
                                        senderKeyDistributionMessage.getIteration(),
                                        senderKeyDistributionMessage.getChainKey(),
                                        senderKeyDistributionMessage.getSignatureKey());
      senderKeyStore.storeSenderKey(sender, senderKeyRecord);
    }
  }

  public SenderKeyDistributionMessage process(String groupId, int keyId, int iteration,
                                              byte[] chainKey, ECKeyPair signatureKey)
  {
    synchronized (GroupCipher.LOCK) {
      SenderKeyRecord senderKeyRecord = senderKeyStore.loadSenderKey(groupId);
      senderKeyRecord.setSenderKeyState(keyId, iteration, chainKey, signatureKey);
      senderKeyStore.storeSenderKey(groupId, senderKeyRecord);

      return new SenderKeyDistributionMessage(keyId, iteration, chainKey, signatureKey.getPublicKey());
    }
  }
}