private async void Page_Loaded(object sender, RoutedEventArgs e)
        {
            #region initialization
            txtBlock.Text = "";
            DateTime before = DateTime.UtcNow;
            AppendLine("Initializing...");
            await Task.Run(new Action(() => {
                alice = new CryptoState();
                bob = new CryptoState();
            }));
            DateTime after = DateTime.UtcNow;

            aliceAddress = new AxolotlAddress(alicePhoneNumber, DEVICE_ID);
            bobAddress = new AxolotlAddress(bobPhoneNumber, DEVICE_ID);

            PreKeyBundle bobsBundle = bob.GetMySignedPreKey();
            alice.SetupSession(bobAddress, bobsBundle);

            PreKeyBundle alicesBundle = alice.GetMySignedPreKey();
            bob.SetupSession(aliceAddress, alicesBundle);
            #endregion

            AppendLine("Timing: " + after.Subtract(before).TotalMilliseconds);

            //first message automatic, just for demonstration purposes
            #region Alice initiates a message to bob
            AppendLine("Alice initiates a chat session with bob...");
            txtMessage.Text = "Bob, what's up?";
            btnAliceSays_Click(sender, e);
            #endregion

        }
        public bool ContainsSession(AxolotlAddress address)
        {
            var name = address.Name;
            var deviceId = address.DeviceId;
            var query = conn.Table<Session>().Where(v => v.Name == name && v.DeviceId == deviceId);

            return query.Count() != 0;
        }
Esempio 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);
		}
Esempio 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);
 }
Esempio n. 5
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;
 }
 /**
  * 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;
 }
Esempio n. 7
0
        public override bool Equals(Object other)
        {
            if (other == null)
            {
                return(false);
            }
            if (!(other is AxolotlAddress))
            {
                return(false);
            }

            AxolotlAddress that = (AxolotlAddress)other;

            return(this.name.Equals(that.name) && this.deviceId == that.deviceId);
        }
        public SessionRecord LoadSession(AxolotlAddress address)
        {
            var name = address.Name;
            var deviceId = address.DeviceId;
            var query = conn.Table<Session>().Where(t => t.Name == name && t.DeviceId == deviceId);

            if (query != null && query.Any())
            {
                return new SessionRecord(query.First().Record);
            }
            else
            {
                return new SessionRecord();
            }

        }
        public OutgoingPushMessage encrypt(AxolotlAddress destination, byte[] unpaddedMessage, bool legacy)
        {
            SessionCipher sessionCipher = new SessionCipher(axolotlStore, destination);
            PushTransportDetails transportDetails = new PushTransportDetails(sessionCipher.getSessionVersion());
            CiphertextMessage message = sessionCipher.encrypt(transportDetails.getPaddedMessageBody(unpaddedMessage));
            uint remoteRegistrationId = sessionCipher.getRemoteRegistrationId();
            String body = Base64.encodeBytes(message.serialize());

            uint type;

            switch (message.getType())
            {
                case CiphertextMessage.PREKEY_TYPE: type = (uint)Envelope.Types.Type.PREKEY_BUNDLE; break; // todo check
                case CiphertextMessage.WHISPER_TYPE: type = (uint)Envelope.Types.Type.CIPHERTEXT; break; // todo check
                default: throw new Exception("Bad type: " + message.getType());
            }

            return new OutgoingPushMessage(type, destination.DeviceId, remoteRegistrationId, legacy ? body : null, legacy ? null : body);
        }
		//[MethodImpl(MethodImplOptions.Synchronized)]
		public SessionRecord LoadSession(AxolotlAddress remoteAddress)
		{
			try
			{
				if (ContainsSession(remoteAddress))
				{
					byte[] session;
					sessions.TryGetValue(remoteAddress, out session); // get()

					return new SessionRecord(session);
				}
				else
				{
					return new SessionRecord();
				}
			}
			catch (Exception e)
			{
				throw new Exception(e.Message);
			}
		}
		public bool ContainsSession(AxolotlAddress address)
		{
			return sessions.ContainsKey(address);
		}
		public void StoreSession(AxolotlAddress address, SessionRecord record)
		{
			sessions[address] = record.serialize();
		}
Esempio n. 13
0
 public bool ContainsSession(AxolotlAddress address)
 {
     return sessionStore.ContainsSession(address);
 }
Esempio n. 14
0
 public void DeleteSession(AxolotlAddress address)
 {
     sessionStore.DeleteSession(address);
 }
Esempio n. 15
0
 public void StoreSession(AxolotlAddress address, SessionRecord record)
 {
     sessionStore.StoreSession(address, record);
 }
Esempio n. 16
0
 public void storeSession(AxolotlAddress address, SessionRecord record)
 {
     if (sessions.ContainsKey(address)) //mimic HashMap update behaviour
     {
         sessions.Remove(address);
     }
     sessions.Add(address, record.serialize()); // put
 }
Esempio n. 17
0
 public void DeleteSession(AxolotlAddress address)
 {
     var name = address.Name;
     var deviceId = address.DeviceId;
     var query = conn.Table<Session>().Delete(t => t.Name == name && t.DeviceId == deviceId);
 }
Esempio n. 18
0
        public void StoreSession(AxolotlAddress address, SessionRecord record)
        {
            DeleteSession(address); // TODO: sqlite-net combined private keys for insertOrReplace

            var session = new Session() { DeviceId = address.DeviceId, Name = address.Name, Record = record.serialize() };
            conn.InsertOrReplace(session);
            return;
        }
		public void DeleteSession(AxolotlAddress address)
		{
			sessions.Remove(address);
		}
        private OutgoingPushMessage getEncryptedMessage(PushServiceSocket socket, TextSecureAddress recipient, uint deviceId, byte[] plaintext, bool legacy)
        {
            AxolotlAddress axolotlAddress = new AxolotlAddress(recipient.getNumber(), deviceId);
            TextSecureCipher cipher = new TextSecureCipher(localAddress, store);

            if (!store.ContainsSession(axolotlAddress))
            {
                try
                {
                    List<PreKeyBundle> preKeys = socket.getPreKeys(recipient, deviceId).Result;

                    foreach (PreKeyBundle preKey in preKeys)
                    {
                        try
                        {
                            AxolotlAddress preKeyAddress = new AxolotlAddress(recipient.getNumber(), preKey.getDeviceId());
                            SessionBuilder sessionBuilder = new SessionBuilder(store, preKeyAddress);
                            sessionBuilder.process(preKey);
                        }
                        catch (libaxolotl.exceptions.UntrustedIdentityException e)
                        {
                            throw new UntrustedIdentityException("Untrusted identity key!", recipient.getNumber(), preKey.getIdentityKey());
                        }
                    }

                    if (eventListener.HasValue)
                    {
                        eventListener.ForceGetValue().onSecurityEvent(recipient);
                    }
                }
                catch (InvalidKeyException e)
                {
                    throw new Exception(e.Message);
                }
            }

            return cipher.encrypt(axolotlAddress, plaintext, legacy);
        }
        private byte[] decrypt(TextSecureEnvelope envelope, byte[] ciphertext)

        {
            AxolotlAddress sourceAddress = new AxolotlAddress(envelope.getSource(), envelope.getSourceDevice());
            SessionCipher sessionCipher = new SessionCipher(axolotlStore, sourceAddress);

            byte[] paddedMessage;

            if (envelope.isPreKeyWhisperMessage())
            {
                paddedMessage = sessionCipher.decrypt(new PreKeyWhisperMessage(ciphertext));
            }
            else if (envelope.isWhisperMessage())
            {
                paddedMessage = sessionCipher.decrypt(new WhisperMessage(ciphertext));
            }
            else
            {
                throw new InvalidMessageException("Unknown type: " + envelope.getType());
            }

            PushTransportDetails transportDetails = new PushTransportDetails(sessionCipher.getSessionVersion());
            return transportDetails.getStrippedPaddingMessageBody(paddedMessage);
        }
Esempio n. 22
0
 public SessionCipher(AxolotlStore store, AxolotlAddress remoteAddress)
     : this(store, store, store, store, remoteAddress)
 {
 }
Esempio n. 23
0
 public SessionRecord LoadSession(AxolotlAddress address)
 {
     return sessionStore.LoadSession(address);
 }
Esempio n. 24
0
		public SessionCipher(AxolotlStore store, AxolotlAddress remoteAddress)
			: this(store, store, store, store, remoteAddress)
		{

		}
 /// <summary>
 /// Start a session for communicating with a remote party.
 /// </summary>
 /// <param name="remoteAddress">Remote party</param>
 /// <param name="preKey">Pre Key for that remote party, pulled from the server (or pulled from memory, like in this demo).</param>
 public void SetupSession(AxolotlAddress remoteAddress, PreKeyBundle preKey)
 {
     sessionBuilder = new SessionBuilder(axolotlStore, remoteAddress);
     sessionBuilder.process(preKey);
     sessionCipher = new SessionCipher(axolotlStore, remoteAddress);
 }