示例#1
0
        private May <uint> processV2(SessionRecord sessionRecord, PreKeyWhisperMessage message)
        {
            if (!message.getPreKeyId().HasValue)
            {
                throw new InvalidKeyIdException("V2 message requires one time prekey id!");
            }

            if (!preKeyStore.ContainsPreKey(message.getPreKeyId().ForceGetValue()) &&
                sessionStore.ContainsSession(remoteAddress))
            {
                //Log.w(TAG, "We've already processed the prekey part of this V2 session, letting bundled message fall through...");
                return(May <uint> .NoValue); //May.absent();
            }

            ECKeyPair ourPreKey = preKeyStore.LoadPreKey(message.getPreKeyId().ForceGetValue()).getKeyPair();

            BobAxolotlParameters.Builder parameters = BobAxolotlParameters.newBuilder();

            parameters.setOurIdentityKey(identityKeyStore.GetIdentityKeyPair())
            .setOurSignedPreKey(ourPreKey)
            .setOurRatchetKey(ourPreKey)
            .setOurOneTimePreKey(May <ECKeyPair> .NoValue)             //absent
            .setTheirIdentityKey(message.getIdentityKey())
            .setTheirBaseKey(message.getBaseKey());

            if (!sessionRecord.isFresh())
            {
                sessionRecord.archiveCurrentState();
            }

            RatchetingSession.initializeSession(sessionRecord.getSessionState(), message.getMessageVersion(), parameters.create());

            sessionRecord.getSessionState().setLocalRegistrationId(identityKeyStore.GetLocalRegistrationId());
            sessionRecord.getSessionState().setRemoteRegistrationId(message.getRegistrationId());
            sessionRecord.getSessionState().setAliceBaseKey(message.getBaseKey().serialize());

            if (message.getPreKeyId().ForceGetValue() != Medium.MAX_VALUE)
            {
                return(message.getPreKeyId());
            }
            else
            {
                return(May <uint> .NoValue); // May.absent();
            }
        }
示例#2
0
        private May <uint> ProcessV2(SessionRecord sessionRecord, PreKeyWhisperMessage message)
        {
            if (!message.GetPreKeyId().HasValue)
            {
                throw new InvalidKeyIdException("V2 message requires one time prekey id!");
            }

            if (!preKeyStore.ContainsPreKey(message.GetPreKeyId().ForceGetValue()) &&
                sessionStore.ContainsSession(remoteAddress))
            {
                return(May <uint> .NoValue);
            }

            ECKeyPair ourPreKey = preKeyStore.LoadPreKey(message.GetPreKeyId().ForceGetValue()).GetKeyPair();

            BobAxolotlParameters.Builder parameters = BobAxolotlParameters.NewBuilder();

            parameters.SetOurIdentityKey(identityKeyStore.GetIdentityKeyPair())
            .SetOurSignedPreKey(ourPreKey)
            .SetOurRatchetKey(ourPreKey)
            .SetOurOneTimePreKey(May <ECKeyPair> .NoValue)             //absent
            .SetTheirIdentityKey(message.GetIdentityKey())
            .SetTheirBaseKey(message.GetBaseKey());

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

            RatchetingSession.InitializeSession(sessionRecord.GetSessionState(), message.GetMessageVersion(), parameters.Create());

            sessionRecord.GetSessionState().SetLocalRegistrationId(identityKeyStore.GetLocalRegistrationId());
            sessionRecord.GetSessionState().SetRemoteRegistrationId(message.GetRegistrationId());
            sessionRecord.GetSessionState().SetAliceBaseKey(message.GetBaseKey().Serialize());

            if (message.GetPreKeyId().ForceGetValue() != Medium.MAX_VALUE)
            {
                return(message.GetPreKeyId());
            }
            else
            {
                return(May <uint> .NoValue); // May.absent();
            }
        }
        public Models.Message[] GetMessages(ISharedPreferences sharedPref, User SelectedFriend, SessionStore sessionStore,
                                            PreKeyStore preKeyStore, SignedPreKeyStore signedPreKeyStore, IdentityKeyStore identityStore, SignalProtocolAddress SelectedFriendAddress)
        {
            // send the server the user name
            // server side does the selection and return the messages and store it in a message array.
            //Send the login username and password to the server and get response
            string apiUrl    = "https://ycandgap.me/api_server2.php";
            string apiMethod = "getMessage";

            //Login_Request has two properties:username and password
            Login_Request myLogin_Request = new Login_Request();

            Models.Message recieveMessage = new Models.Message();

            //get the login username from previow login page.
            recieveMessage.MessageReceiverRegisID = Convert.ToUInt32(sharedPref.GetString("RegistrationId", string.Empty));
            recieveMessage.MessageSenderRegisID   = SelectedFriend.RegisterationID;
            myLogin_Request.message = recieveMessage;
            UserID = myLogin_Request.RegistrationID;


            // make http post request
            string response = Http.Post(apiUrl, new NameValueCollection()
            {
                { "api_method", apiMethod },
                { "api_data", JsonConvert.SerializeObject(myLogin_Request) }
            });

            // decode json string to dto object
            API_Response2 r = JsonConvert.DeserializeObject <API_Response2>(response);

            // check response
            if (r != null)
            {
                if (!r.IsError && !string.IsNullOrEmpty(r.MessageText))
                {
                    SessionCipher sessionCipher = new SessionCipher(sessionStore, preKeyStore, signedPreKeyStore, identityStore, SelectedFriendAddress);
                    byte[]        decipherMessage;
                    if (!sessionStore.ContainsSession(SelectedFriendAddress))
                    {
                        decipherMessage = sessionCipher.decrypt(new PreKeySignalMessage((JsonConvert.DeserializeObject <byte[]>(r.MessageText))));
                    }
                    else
                    {
                        decipherMessage = sessionCipher.decrypt(new SignalMessage((JsonConvert.DeserializeObject <byte[]>(r.MessageText))));
                    }
                    string checkMessage = Encoding.UTF8.GetString(decipherMessage);

                    SelectedFriend.SelectedUserMessages.Add(new Models.Message
                    {
                        MessageID              = r.MessageID,
                        MessageSenderRegisID   = r.MessageSenderRegisID,
                        MessageReceiverRegisID = r.MessageReceiverRegisID,
                        MessageText            = checkMessage,
                        MessageTimestamp       = r.MessageTimestamp
                    });

                    return(SelectedFriend.SelectedUserMessages.ToArray());
                }
                else
                {
                    //if login fails, pop up an alert message. Wrong username or password or a new user
                    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
                    if (!string.IsNullOrEmpty(r.ErrorMessage))
                    {
                        dialogBuilder.SetMessage(r.ErrorMessage);
                    }
                    else
                    {
                        dialogBuilder.SetMessage("No new messages");
                    }
                    //dialogBuilder.SetPositiveButton("Ok", null);
                    dialogBuilder.Show();
                    return(null);
                }
            }
            else
            {
                if (!sessionStore.ContainsSession(SelectedFriendAddress))
                {
                    // Instantiate a SessionBuilder for a remote recipientId + deviceId tuple.
                    SessionBuilder sessionBuilder = new SessionBuilder(sessionStore, preKeyStore, signedPreKeyStore,
                                                                       identityStore, SelectedFriendAddress);
                    RetrievedPreKey preKeyPublic = RetrieveSelectedFriendPublicPreKey(SelectedFriend);
                    IdentityKey     SelectedFriendSignedPreKey = new IdentityKey(JsonConvert.DeserializeObject <byte[]>(SelectedFriend.SignedPreKey), 0);
                    PreKeyBundle    retrievedPreKey            = new PreKeyBundle(SelectedFriend.RegisterationID, 1, preKeyPublic.PrekeyID, preKeyPublic.PublicPreKey.getPublicKey()
                                                                                  , SelectedFriend.SignedPreKeyID, SelectedFriendSignedPreKey.getPublicKey(), JsonConvert.DeserializeObject <byte[]>(SelectedFriend.SignedPreKeySignature)
                                                                                  , new IdentityKey(JsonConvert.DeserializeObject <byte[]>(SelectedFriend.IdentityKey), 0));
                    // Build a session with a PreKey retrieved from the server.
                    sessionBuilder.process(retrievedPreKey);
                }
                return(null);
            }
        }
示例#4
0
 public bool ContainsSession(AxolotlAddress address)
 {
     return(sessionStore.ContainsSession(address));
 }
示例#5
0
 public bool containsSession(SignalProtocolAddress address)
 {
     return(SESSION_STORE.ContainsSession(address));
 }
 public bool ContainsSession(SignalProtocolAddress address)
 {
     return(sessionStore.ContainsSession(address));
 }