Search in sources :

Example 1 with SessionStructure

use of org.whispersystems.libsignal.state.StorageProtos.SessionStructure 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)

Aggregations

FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 MasterCipher (org.thoughtcrime.securesms.crypto.MasterCipher)1 InvalidMessageException (org.whispersystems.libsignal.InvalidMessageException)1 SessionRecord (org.whispersystems.libsignal.state.SessionRecord)1 SessionState (org.whispersystems.libsignal.state.SessionState)1 SessionStructure (org.whispersystems.libsignal.state.StorageProtos.SessionStructure)1