Search in sources :

Example 1 with SessionRecord

use of org.whispersystems.libsignal.state.SessionRecord in project Signal-Android by WhisperSystems.

the class TextSecureSessionStore method loadSession.

@Override
public SessionRecord loadSession(@NonNull SignalProtocolAddress address) {
    synchronized (FILE_LOCK) {
        try {
            FileInputStream in = new FileInputStream(getSessionFile(address));
            int versionMarker = readInteger(in);
            if (versionMarker > CURRENT_VERSION) {
                throw new AssertionError("Unknown version: " + versionMarker);
            }
            byte[] serialized = readBlob(in);
            in.close();
            if (versionMarker < PLAINTEXT_VERSION && masterSecret != null) {
                serialized = new MasterCipher(masterSecret).decryptBytes(serialized);
            } else if (versionMarker < PLAINTEXT_VERSION) {
                throw new AssertionError("Session didn't get migrated: (" + versionMarker + "," + address + ")");
            }
            if (versionMarker == SINGLE_STATE_VERSION) {
                SessionStructure sessionStructure = SessionStructure.parseFrom(serialized);
                SessionState sessionState = new SessionState(sessionStructure);
                return new SessionRecord(sessionState);
            } else if (versionMarker >= ARCHIVE_STATES_VERSION) {
                return new SessionRecord(serialized);
            } else {
                throw new AssertionError("Unknown version: " + versionMarker);
            }
        } catch (InvalidMessageException | IOException e) {
            Log.w(TAG, "No existing session information found.");
            return new SessionRecord();
        }
    }
}
Also used : SessionStructure(org.whispersystems.libsignal.state.StorageProtos.SessionStructure) SessionState(org.whispersystems.libsignal.state.SessionState) InvalidMessageException(org.whispersystems.libsignal.InvalidMessageException) MasterCipher(org.thoughtcrime.securesms.crypto.MasterCipher) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) SessionRecord(org.whispersystems.libsignal.state.SessionRecord)

Example 2 with SessionRecord

use of org.whispersystems.libsignal.state.SessionRecord in project Signal-Android by WhisperSystems.

the class TextSecureSessionStore method migrateSessions.

public void migrateSessions() {
    synchronized (FILE_LOCK) {
        File directory = getSessionDirectory();
        for (File session : directory.listFiles()) {
            if (session.isFile()) {
                SignalProtocolAddress address = getAddressName(session);
                if (address != null) {
                    SessionRecord sessionRecord = loadSession(address);
                    storeSession(address, sessionRecord);
                }
            }
        }
    }
}
Also used : RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) SignalProtocolAddress(org.whispersystems.libsignal.SignalProtocolAddress) SessionRecord(org.whispersystems.libsignal.state.SessionRecord)

Example 3 with SessionRecord

use of org.whispersystems.libsignal.state.SessionRecord in project Signal-Android by WhisperSystems.

the class IdentityUtil method getRemoteIdentityKey.

@UiThread
public static ListenableFuture<Optional<IdentityKey>> getRemoteIdentityKey(final Context context, final MasterSecret masterSecret, final Recipient recipient) {
    final SettableFuture<Optional<IdentityKey>> future = new SettableFuture<>();
    new AsyncTask<Recipient, Void, Optional<IdentityKey>>() {

        @Override
        protected Optional<IdentityKey> doInBackground(Recipient... recipient) {
            SessionStore sessionStore = new TextSecureSessionStore(context, masterSecret);
            SignalProtocolAddress axolotlAddress = new SignalProtocolAddress(recipient[0].getNumber(), SignalServiceAddress.DEFAULT_DEVICE_ID);
            SessionRecord record = sessionStore.loadSession(axolotlAddress);
            if (record == null) {
                return Optional.absent();
            }
            return Optional.fromNullable(record.getSessionState().getRemoteIdentityKey());
        }

        @Override
        protected void onPostExecute(Optional<IdentityKey> result) {
            future.set(result);
        }
    }.execute(recipient);
    return future;
}
Also used : SettableFuture(org.thoughtcrime.securesms.util.concurrent.SettableFuture) IdentityKey(org.whispersystems.libsignal.IdentityKey) Optional(org.whispersystems.libsignal.util.guava.Optional) Recipient(org.thoughtcrime.securesms.recipients.Recipient) TextSecureSessionStore(org.thoughtcrime.securesms.crypto.storage.TextSecureSessionStore) SessionStore(org.whispersystems.libsignal.state.SessionStore) TextSecureSessionStore(org.thoughtcrime.securesms.crypto.storage.TextSecureSessionStore) SignalProtocolAddress(org.whispersystems.libsignal.SignalProtocolAddress) SessionRecord(org.whispersystems.libsignal.state.SessionRecord) UiThread(android.support.annotation.UiThread)

Aggregations

SessionRecord (org.whispersystems.libsignal.state.SessionRecord)3 SignalProtocolAddress (org.whispersystems.libsignal.SignalProtocolAddress)2 UiThread (android.support.annotation.UiThread)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 RandomAccessFile (java.io.RandomAccessFile)1 MasterCipher (org.thoughtcrime.securesms.crypto.MasterCipher)1 TextSecureSessionStore (org.thoughtcrime.securesms.crypto.storage.TextSecureSessionStore)1 Recipient (org.thoughtcrime.securesms.recipients.Recipient)1 SettableFuture (org.thoughtcrime.securesms.util.concurrent.SettableFuture)1 IdentityKey (org.whispersystems.libsignal.IdentityKey)1 InvalidMessageException (org.whispersystems.libsignal.InvalidMessageException)1 SessionState (org.whispersystems.libsignal.state.SessionState)1 SessionStore (org.whispersystems.libsignal.state.SessionStore)1 SessionStructure (org.whispersystems.libsignal.state.StorageProtos.SessionStructure)1 Optional (org.whispersystems.libsignal.util.guava.Optional)1