Exemplo n.º 1
0
        private Maybe<UInt32> ProcessV3(SessionRecord sessionRecord, PreKeyWhisperMessage message)
        {
            if (sessionRecord.HasSessionState(message.MessageVersion, message.BaseKey.Serialize()))
            {
                Logger.w("SessionBuilder : ", "We've already setup a session for this V3 message, letting bundled message fall through...");
                return Maybe<UInt32>.Nothing;
            }

            var ourSignedPreKey = _signedPreKeyStore.LoadSignedPreKey(message.SignedPreKeyId).keyPair;

            var bobParams = BobAxolotlParameters.NewBuilder ();
            bobParams.SetTheirBaseKey(message.BaseKey)
                        .SetTheirIdentityKey(message.IdentityKey)
                        .SetOurIdentityKey(_identityKeyStore.GetIdentityKeyPair())
                        .SetOurSignedPreKey(ourSignedPreKey)
                        .SetOurRatchetKey(ourSignedPreKey);

            if (message.PreKeyId.IsSomething()) {
                message.PreKeyId.Do (pKid => {
                    bobParams.SetOurOneTimePreKey(_preKeyStore.LoadPreKey(pKid).KeyPair.ToMaybe());
                });
            } else {
                bobParams.SetOurOneTimePreKey(Maybe<ECKeyPair>.Nothing);
            }

            if (!sessionRecord.IsFresh) sessionRecord.ArchiveCurrentState();

            RatchetingSession.InitializeSession(sessionRecord.SessionState, message.MessageVersion, bobParams.Create());

            sessionRecord.SessionState.LocalRegistrationId  = _identityKeyStore.GetLocalRegistrationId();
            sessionRecord.SessionState.RemoteRegistrationId = message.RegistrationId;
            sessionRecord.SessionState.AliceBaseKey 		= message.BaseKey.Serialize();

            if (message.PreKeyId.IsSomething() &&
                message.PreKeyId.Value != Medium.MAX_VALUE) {
                return message.PreKeyId;
            } else {
                return Maybe<UInt32>.Nothing;
            }
        }
Exemplo n.º 2
0
        private Maybe<UInt32> ProcessV2(SessionRecord sessionRecord, PreKeyWhisperMessage message)
        {
            if (message.PreKeyId.IsNothing()) {
                throw new InvalidKeyIdException("V2 message requires one time prekey id!");
            }

            if (!_preKeyStore.ContainsPreKey(message.PreKeyId.Value) &&
                _sessionStore.ContainsSession(_remoteAddress))
            {
                Console.WriteLine("TAG" + "We've already processed the prekey part of this V2 session, letting bundled message fall through...");
                return Maybe<UInt32>.Nothing;
            }

            var ourPreKey = _preKeyStore.LoadPreKey(message.PreKeyId.Value).KeyPair;

            var bobParams = BobAxolotlParameters.NewBuilder();

            bobParams.SetOurIdentityKey(_identityKeyStore.GetIdentityKeyPair())
                    .SetOurSignedPreKey(ourPreKey)
                    .SetOurRatchetKey(ourPreKey)
                    .SetOurOneTimePreKey(Maybe<ECKeyPair>.Nothing)
                    .SetTheirIdentityKey(message.IdentityKey)
                    .SetTheirBaseKey(message.BaseKey);

            if (!sessionRecord.IsFresh) sessionRecord.ArchiveCurrentState();

            RatchetingSession.InitializeSession(sessionRecord.SessionState, message.MessageVersion, bobParams.Create());

            sessionRecord.SessionState.LocalRegistrationId 	= _identityKeyStore.GetLocalRegistrationId();
            sessionRecord.SessionState.RemoteRegistrationId = message.RegistrationId;
            sessionRecord.SessionState.AliceBaseKey 		= message.BaseKey.Serialize();

            if (message.PreKeyId.Value != Medium.MAX_VALUE) {
                return message.PreKeyId;
            } else {
                return Maybe<UInt32>.Nothing;
            }
        }