Beispiel #1
0
        public void SetSenderChainKey(ChainKey nextChainKey)
        {
            StorageProtos.SessionStructure.Types.Chain.Types.ChainKey chainKey = StorageProtos.SessionStructure.Types.Chain.Types.ChainKey.CreateBuilder()
                                                                                 .SetKey(ByteString.CopyFrom(nextChainKey.GetKey()))
                                                                                 .SetIndex(nextChainKey.GetIndex())
                                                                                 .Build();

            StorageProtos.SessionStructure.Types.Chain chain = _sessionStructure.SenderChain.ToBuilder()
                                                               .SetChainKey(chainKey).Build();

            _sessionStructure = _sessionStructure.ToBuilder().SetSenderChain(chain).Build();
        }
Beispiel #2
0
        public void SetUnacknowledgedPreKeyMessage(May <uint> preKeyId, uint signedPreKeyId, IEcPublicKey baseKey)
        {
            StorageProtos.SessionStructure.Types.PendingPreKey.Builder pending = StorageProtos.SessionStructure.Types.PendingPreKey.CreateBuilder()
                                                                                 .SetSignedPreKeyId((int)signedPreKeyId)
                                                                                 .SetBaseKey(ByteString.CopyFrom(baseKey.Serialize()));

            if (preKeyId.HasValue)
            {
                pending.SetPreKeyId(preKeyId.ForceGetValue());
            }

            _sessionStructure = _sessionStructure.ToBuilder()
                                .SetPendingPreKey(pending.Build())
                                .Build();
        }
Beispiel #3
0
        public void SetSenderChain(EcKeyPair senderRatchetKeyPair, ChainKey chainKey)
        {
            StorageProtos.SessionStructure.Types.Chain.Types.ChainKey chainKeyStructure = StorageProtos.SessionStructure.Types.Chain.Types.ChainKey.CreateBuilder()
                                                                                          .SetKey(ByteString.CopyFrom(chainKey.GetKey()))
                                                                                          .SetIndex(chainKey.GetIndex())
                                                                                          .Build();

            StorageProtos.SessionStructure.Types.Chain senderChain = StorageProtos.SessionStructure.Types.Chain.CreateBuilder()
                                                                     .SetSenderRatchetKey(ByteString.CopyFrom(senderRatchetKeyPair.GetPublicKey().Serialize()))
                                                                     .SetSenderRatchetKeyPrivate(ByteString.CopyFrom(senderRatchetKeyPair.GetPrivateKey().Serialize()))
                                                                     .SetChainKey(chainKeyStructure)
                                                                     .Build();

            _sessionStructure = _sessionStructure.ToBuilder().SetSenderChain(senderChain).Build();
        }
Beispiel #4
0
        public void SetReceiverChainKey(IEcPublicKey senderEphemeral, ChainKey chainKey)
        {
            Pair <StorageProtos.SessionStructure.Types.Chain, uint> chainAndIndex = GetReceiverChain(senderEphemeral);

            StorageProtos.SessionStructure.Types.Chain chain = chainAndIndex.First();

            StorageProtos.SessionStructure.Types.Chain.Types.ChainKey chainKeyStructure = StorageProtos.SessionStructure.Types.Chain.Types.ChainKey.CreateBuilder()
                                                                                          .SetKey(ByteString.CopyFrom(chainKey.GetKey()))
                                                                                          .SetIndex(chainKey.GetIndex())
                                                                                          .Build();

            StorageProtos.SessionStructure.Types.Chain updatedChain = chain.ToBuilder().SetChainKey(chainKeyStructure).Build();

            _sessionStructure = _sessionStructure.ToBuilder()
                                .SetReceiverChains((int)chainAndIndex.Second(), updatedChain)                                                                                  // TODO: conv
                                .Build();
        }
Beispiel #5
0
        public void AddReceiverChain(IEcPublicKey senderRatchetKey, ChainKey chainKey)
        {
            StorageProtos.SessionStructure.Types.Chain.Types.ChainKey chainKeyStructure = StorageProtos.SessionStructure.Types.Chain.Types.ChainKey.CreateBuilder()
                                                                                          .SetKey(ByteString.CopyFrom(chainKey.GetKey()))
                                                                                          .SetIndex(chainKey.GetIndex())
                                                                                          .Build();

            StorageProtos.SessionStructure.Types.Chain chain = StorageProtos.SessionStructure.Types.Chain.CreateBuilder()
                                                               .SetChainKey(chainKeyStructure)
                                                               .SetSenderRatchetKey(ByteString.CopyFrom(senderRatchetKey.Serialize()))
                                                               .Build();

            _sessionStructure = _sessionStructure.ToBuilder().AddReceiverChains(chain).Build();

            if (_sessionStructure.ReceiverChainsList.Count > 5)
            {
                _sessionStructure = _sessionStructure.ToBuilder() /*.ClearReceiverChains()*/.Build();                //RemoveReceiverChains(0) TODO: why does it work without
            }
        }
Beispiel #6
0
        public void SetPendingKeyExchange(uint sequence,
                                          EcKeyPair ourBaseKey,
                                          EcKeyPair ourRatchetKey,
                                          IdentityKeyPair ourIdentityKey)
        {
            StorageProtos.SessionStructure.Types.PendingKeyExchange structure =
                StorageProtos.SessionStructure.Types.PendingKeyExchange.CreateBuilder()
                .SetSequence(sequence)
                .SetLocalBaseKey(ByteString.CopyFrom(ourBaseKey.GetPublicKey().Serialize()))
                .SetLocalBaseKeyPrivate(ByteString.CopyFrom(ourBaseKey.GetPrivateKey().Serialize()))
                .SetLocalRatchetKey(ByteString.CopyFrom(ourRatchetKey.GetPublicKey().Serialize()))
                .SetLocalRatchetKeyPrivate(ByteString.CopyFrom(ourRatchetKey.GetPrivateKey().Serialize()))
                .SetLocalIdentityKey(ByteString.CopyFrom(ourIdentityKey.GetPublicKey().Serialize()))
                .SetLocalIdentityKeyPrivate(ByteString.CopyFrom(ourIdentityKey.GetPrivateKey().Serialize()))
                .Build();

            _sessionStructure = _sessionStructure.ToBuilder()
                                .SetPendingKeyExchange(structure)
                                .Build();
        }
Beispiel #7
0
        public MessageKeys RemoveMessageKeys(IEcPublicKey senderEphemeral, uint counter)
        {
            Pair <StorageProtos.SessionStructure.Types.Chain, uint> chainAndIndex = GetReceiverChain(senderEphemeral);

            StorageProtos.SessionStructure.Types.Chain chain = chainAndIndex.First();

            if (chain == null)
            {
                return(null);
            }

            List <StorageProtos.SessionStructure.Types.Chain.Types.MessageKey>        messageKeyList     = new List <StorageProtos.SessionStructure.Types.Chain.Types.MessageKey>(chain.MessageKeysList);
            IEnumerator <StorageProtos.SessionStructure.Types.Chain.Types.MessageKey> messageKeyIterator = messageKeyList.GetEnumerator();
            MessageKeys result = null;

            while (messageKeyIterator.MoveNext())                                                                    //hasNext()
            {
                StorageProtos.SessionStructure.Types.Chain.Types.MessageKey messageKey = messageKeyIterator.Current; // next()

                if (messageKey.Index == counter)
                {
                    result = new MessageKeys(messageKey.CipherKey.ToByteArray(),
                                             messageKey.MacKey.ToByteArray(),
                                             messageKey.Iv.ToByteArray(),
                                             messageKey.Index);

                    messageKeyList.Remove(messageKey);                     //messageKeyIterator.remove();
                    break;
                }
            }

            StorageProtos.SessionStructure.Types.Chain updatedChain = chain.ToBuilder().ClearMessageKeys()
                                                                      .AddRangeMessageKeys(messageKeyList)     // AddAllMessageKeys
                                                                      .Build();

            _sessionStructure = _sessionStructure.ToBuilder()
                                .SetReceiverChains((int)chainAndIndex.Second(), updatedChain)                                                                                  // TODO: conv
                                .Build();

            return(result);
        }
Beispiel #8
0
        public void SetMessageKeys(IEcPublicKey senderEphemeral, MessageKeys messageKeys)
        {
            Pair <StorageProtos.SessionStructure.Types.Chain, uint> chainAndIndex = GetReceiverChain(senderEphemeral);

            StorageProtos.SessionStructure.Types.Chain chain = chainAndIndex.First();
            StorageProtos.SessionStructure.Types.Chain.Types.MessageKey messageKeyStructure = StorageProtos.SessionStructure.Types.Chain.Types.MessageKey.CreateBuilder()
                                                                                              .SetCipherKey(ByteString.CopyFrom(messageKeys.GetCipherKey() /*.getEncoded()*/))
                                                                                              .SetMacKey(ByteString.CopyFrom(messageKeys.GetMacKey() /*.getEncoded()*/))
                                                                                              .SetIndex(messageKeys.GetCounter())
                                                                                              .SetIv(ByteString.CopyFrom(messageKeys.GetIv() /*.getIV()*/))
                                                                                              .Build();

            StorageProtos.SessionStructure.Types.Chain.Builder updatedChain = chain.ToBuilder().AddMessageKeys(messageKeyStructure);
            if (updatedChain.MessageKeysList.Count > MaxMessageKeys)
            {
                updatedChain.MessageKeysList.RemoveAt(0);
            }

            _sessionStructure = _sessionStructure.ToBuilder()
                                .SetReceiverChains((int)chainAndIndex.Second(), updatedChain.Build())                                                                                  // TODO: conv
                                .Build();
        }
Beispiel #9
0
 public void SetAliceBaseKey(byte[] aliceBaseKey)
 {
     _sessionStructure = _sessionStructure.ToBuilder()
                         .SetAliceBaseKey(ByteString.CopyFrom(aliceBaseKey))
                         .Build();
 }
Beispiel #10
0
 public void SetLocalRegistrationId(uint registrationId)
 {
     _sessionStructure = _sessionStructure.ToBuilder()
                         .SetLocalRegistrationId(registrationId)
                         .Build();
 }
Beispiel #11
0
 public void ClearUnacknowledgedPreKeyMessage()
 {
     _sessionStructure = _sessionStructure.ToBuilder()
                         .ClearPendingPreKey()
                         .Build();
 }
Beispiel #12
0
 public SessionState(SessionState copy)
 {
     _sessionStructure = copy._sessionStructure.ToBuilder().Build();
 }
Beispiel #13
0
 public SessionState(StorageProtos.SessionStructure sessionStructure)
 {
     _sessionStructure = sessionStructure;
 }
Beispiel #14
0
 public void SetRootKey(RootKey rootKey)
 {
     _sessionStructure = _sessionStructure.ToBuilder()
                         .SetRootKey(ByteString.CopyFrom(rootKey.GetKeyBytes()))
                         .Build();
 }
Beispiel #15
0
 public SessionState()
 {
     _sessionStructure = StorageProtos.SessionStructure.CreateBuilder().Build();
 }
Beispiel #16
0
 public void SetSessionVersion(uint version)
 {
     _sessionStructure = _sessionStructure.ToBuilder()
                         .SetSessionVersion(version)
                         .Build();
 }
Beispiel #17
0
 public void SetLocalIdentityKey(IdentityKey identityKey)
 {
     _sessionStructure = _sessionStructure.ToBuilder()
                         .SetLocalIdentityPublic(ByteString.CopyFrom(identityKey.Serialize()))
                         .Build();
 }
Beispiel #18
0
 public void SetPreviousCounter(uint previousCounter)
 {
     _sessionStructure = _sessionStructure.ToBuilder()
                         .SetPreviousCounter(previousCounter)
                         .Build();
 }