Ejemplo n.º 1
0
        public TextSecureAxolotlStore()
        {
            connection = new SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), AXOLOTLDB_PATH);

            this.preKeyStore = new TextSecurePreKeyStore(connection);
            this.signedPreKeyStore = new TextSecurePreKeyStore(connection);
            this.identityKeyStore = new TextSecureIdentityKeyStore(connection);
            this.sessionStore = new TextSecureSessionStore(connection);
        }
Ejemplo n.º 2
0
        public TextSecureAxolotlStore()
        {
            connection = new SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), AXOLOTLDB_PATH);

            this.preKeyStore       = new TextSecurePreKeyStore(connection);
            this.signedPreKeyStore = new TextSecurePreKeyStore(connection);
            this.identityKeyStore  = new TextSecureIdentityKeyStore(connection);
            this.sessionStore      = new TextSecureSessionStore(connection);
        }
Ejemplo n.º 3
0
		/**
         * Construct a SessionCipher for encrypt/decrypt operations on a session.
         * In order to use SessionCipher, a session must have already been created
         * and stored using {@link SessionBuilder}.
         *
         * @param  sessionStore The {@link SessionStore} that contains a session for this recipient.
         * @param  remoteAddress  The remote address that messages will be encrypted to or decrypted from.
         */
		public SessionCipher(SessionStore sessionStore, PreKeyStore preKeyStore,
							 SignedPreKeyStore signedPreKeyStore, IdentityKeyStore identityKeyStore,
							 AxolotlAddress remoteAddress)
		{
			this.sessionStore = sessionStore;
			this.preKeyStore = preKeyStore;
			this.remoteAddress = remoteAddress;
			this.sessionBuilder = new SessionBuilder(sessionStore, preKeyStore, signedPreKeyStore,
													 identityKeyStore, remoteAddress);
		}
Ejemplo n.º 4
0
 /**
  * Construct a SessionCipher for encrypt/decrypt operations on a session.
  * In order to use SessionCipher, a session must have already been created
  * and stored using {@link SessionBuilder}.
  *
  * @param  sessionStore The {@link SessionStore} that contains a session for this recipient.
  * @param  remoteAddress  The remote address that messages will be encrypted to or decrypted from.
  */
 public SessionCipher(SessionStore sessionStore, PreKeyStore preKeyStore,
                      SignedPreKeyStore signedPreKeyStore, IdentityKeyStore identityKeyStore,
                      AxolotlAddress remoteAddress)
 {
     this.sessionStore   = sessionStore;
     this.preKeyStore    = preKeyStore;
     this.remoteAddress  = remoteAddress;
     this.sessionBuilder = new SessionBuilder(sessionStore, preKeyStore, signedPreKeyStore,
                                              identityKeyStore, remoteAddress);
 }
 /**
  * Constructs a SessionBuilder.
  *
  * @param sessionStore The {@link org.whispersystems.libsignal.state.SessionStore} to store the constructed session in.
  * @param preKeyStore The {@link  org.whispersystems.libsignal.state.PreKeyStore} where the client's local {@link org.whispersystems.libsignal.state.PreKeyRecord}s are stored.
  * @param identityKeyStore The {@link org.whispersystems.libsignal.state.IdentityKeyStore} containing the client's identity key information.
  * @param remoteAddress The address of the remote user to build a session with.
  */
 public SessionBuilder(SessionStore sessionStore,
                       PreKeyStore preKeyStore,
                       SignedPreKeyStore signedPreKeyStore,
                       IdentityKeyStore identityKeyStore,
                       SignalProtocolAddress remoteAddress)
 {
     this.sessionStore      = sessionStore;
     this.preKeyStore       = preKeyStore;
     this.signedPreKeyStore = signedPreKeyStore;
     this.identityKeyStore  = identityKeyStore;
     this.remoteAddress     = remoteAddress;
 }
Ejemplo n.º 6
0
 /**
  * Constructs a SessionBuilder.
  *
  * @param sessionStore The {@link org.whispersystems.libaxolotl.state.SessionStore} to store the constructed session in.
  * @param preKeyStore The {@link  org.whispersystems.libaxolotl.state.PreKeyStore} where the client's local {@link org.whispersystems.libaxolotl.state.PreKeyRecord}s are stored.
  * @param identityKeyStore The {@link org.whispersystems.libaxolotl.state.IdentityKeyStore} containing the client's identity key information.
  * @param remoteAddress The address of the remote user to build a session with.
  */
 public SessionBuilder(SessionStore sessionStore,
                       PreKeyStore preKeyStore,
                       SignedPreKeyStore signedPreKeyStore,
                       IdentityKeyStore identityKeyStore,
                       AxolotlAddress remoteAddress)
 {
     this.sessionStore = sessionStore;
     this.preKeyStore = preKeyStore;
     this.signedPreKeyStore = signedPreKeyStore;
     this.identityKeyStore = identityKeyStore;
     this.remoteAddress = remoteAddress;
 }
Ejemplo n.º 7
0
        //--------------------------------------------------------Constructor:----------------------------------------------------------------\\
        #region --Constructors--
        /// <summary>
        /// Basic Constructor
        /// </summary>
        /// <history>
        /// 06/08/2018 Created [Fabian Sauter]
        /// </history>
        public OmemoHelper(XMPPConnection2 connection)
        {
            this.CONNECTION = connection;

            this.OMEMO_SESSIONS       = new Dictionary <string, OmemoSession>();
            this.MESSAGE_CACHE        = new Dictionary <string, Tuple <List <OmemoMessageMessage>, OmemoSessionBuildHelper> >();
            this.SESSION_STORE        = new OmemoSessionStore(connection.account);
            this.PRE_KEY_STORE        = new OmemoPreKeyStore(connection.account);
            this.SIGNED_PRE_KEY_STORE = new OmemoSignedPreKeyStore(connection.account);
            this.IDENTITY_STORE       = new OmemoIdentityKeyStore(connection.account);

            reset();
        }
        protected override void OnCreate(Bundle bundle)
        {
            // Getting saved data
            ISharedPreferences sharedPref = PreferenceManager.GetDefaultSharedPreferences(this);
            User SelectedFriend           = JsonConvert.DeserializeObject <User>(sharedPref.GetString("SelectedFriend", string.Empty));
            List <Models.Message> SelectedFriendMessageList = new List <Models.Message>();

            SelectedFriendMessageList = JsonConvert.DeserializeObject <List <Models.Message> >(sharedPref.GetString("SelectedFriendMessageList", string.Empty));
            foreach (Models.Message m in SelectedFriendMessageList)
            {
                SelectedFriend.SelectedUserMessages.Add(m);
            }
            InMemorySessionStore sessionStore = JsonConvert.DeserializeObject <InMemorySessionStore>(sharedPref.GetString("SessionStore", string.Empty));
            var allSessions = JsonConvert.DeserializeObject <List <Session> >(sharedPref.GetString("AllSessions", string.Empty));

            foreach (Session item in allSessions)
            {
                sessionStore.StoreSession(item.Name, item.DeviceID, item.array);
            }
            PreKeyStore              preKeyStore       = JsonConvert.DeserializeObject <InMemoryPreKeyStore>(sharedPref.GetString("PreKeyStore", string.Empty));
            SignedPreKeyStore        signedPreKeyStore = JsonConvert.DeserializeObject <InMemorySignedPreKeyStore>(sharedPref.GetString("SignedPreKeyStore", string.Empty));
            InMemoryIdentityKeyStore identityStore     = JsonConvert.DeserializeObject <InMemoryIdentityKeyStore>(sharedPref.GetString("IdentityStore", string.Empty));
            IdentityKeyPair          KeyPair           = new IdentityKeyPair(JsonConvert.DeserializeObject <byte[]>(sharedPref.GetString("IdentityKeyPair", string.Empty)));
            uint RegistrationID = Convert.ToUInt32(sharedPref.GetString("RegistrationId", string.Empty));

            identityStore.PutValues(KeyPair, RegistrationID);
            var allTrustedKeys = JsonConvert.DeserializeObject <List <TrustedKey> >(sharedPref.GetString("AllTrustedKeys", string.Empty));

            foreach (TrustedKey item in allTrustedKeys)
            {
                identityStore.SaveIdentity(item.Name, new IdentityKey(item.Identity, 0));
            }
            SignalProtocolAddress SelectedFriendAddress = new SignalProtocolAddress(SelectedFriend.RegisterationID.ToString(), 1);

            // Get the messages from the server
            base.OnCreate(bundle);
            TheirMessages = GetMessages(sharedPref, SelectedFriend, sessionStore, preKeyStore, signedPreKeyStore, identityStore, SelectedFriendAddress);
            // Set our view from the "ChatList" layout resource
            Title = SelectedFriend.Username;
            SetContentView(Resource.Layout.Message);
            listView = FindViewById <ListView>(Resource.Id.messageList);
            //***display FriendsListItem in ListView using Adapter
            listView.Adapter = adapter = new Adapter(this, TheirMessages, UserID);
            messageText      = FindViewById <EditText>(Resource.Id.messageText);
            sendButton       = FindViewById <Button>(Resource.Id.sendButton);

            // **implement messages sending and receiving here
            //listView.Adapter = adapter = new Adapter(this);

            sendButton.Click += (sender, e) =>
            {
                SessionCipher     sessionCipher = new SessionCipher(sessionStore, preKeyStore, signedPreKeyStore, identityStore, SelectedFriendAddress);
                CiphertextMessage cipherMessage = sessionCipher.encrypt(Encoding.UTF8.GetBytes(messageText.Text));
                SelectedFriend.SelectedUserMessages.Add(new Models.Message()
                {
                    MessageID = 1,
                    MessageReceiverRegisID = SelectedFriend.RegisterationID,
                    MessageSenderRegisID   = RegistrationID,
                    MessageText            = messageText.Text,
                    MessageTimestamp       = DateTime.Now
                });

                // Save stores in local Database
                ISharedPreferences       sharedprefs = PreferenceManager.GetDefaultSharedPreferences(this);
                ISharedPreferencesEditor editor      = sharedprefs.Edit();
                SelectedFriendMessageList.Clear();
                foreach (Models.Message m in SelectedFriend.SelectedUserMessages)
                {
                    SelectedFriendMessageList.Add(m);
                }
                editor.PutString("SelectedFriendMessageList", JsonConvert.SerializeObject(SelectedFriendMessageList));
                editor.PutString("SelectedFriend", JsonConvert.SerializeObject(SelectedFriend));
                editor.PutString("PreKeyStore", JsonConvert.SerializeObject(preKeyStore));
                editor.PutString("SignedPreKeyStore", JsonConvert.SerializeObject(signedPreKeyStore));
                List <Session> AllSessions = sessionStore.GetAllSessions();
                editor.PutString("AllSessions", JsonConvert.SerializeObject(AllSessions));
                editor.PutString("SessionStore", JsonConvert.SerializeObject(sessionStore));
                editor.PutString("IdentityStore", JsonConvert.SerializeObject(identityStore));
                List <TrustedKey> AllTrustedKeys = identityStore.GetAllTrustedKeys();
                editor.PutString("AllTrustedKeys", JsonConvert.SerializeObject(AllTrustedKeys));
                editor.Apply();

                Models.Message message = new Models.Message();
                message.MessageID = 4;
                message.MessageReceiverRegisID = SelectedFriend.RegisterationID;
                message.MessageSenderRegisID   = Convert.ToUInt32(sharedPref.GetString("RegistrationId", string.Empty));
                message.MessageText            = JsonConvert.SerializeObject(cipherMessage.serialize());
                message.MessageTimestamp       = DateTime.Now;

                //  call SendMessage() to send the
                SendMessage(message);
                //  *display the messages in user's own screen using adapter (always display in MyMessageListItem).
                adapter.NotifyDataSetInvalidated();
                listView.SetSelection(adapter.Count);
            };
        }
        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);
            }
        }