Search in sources :

Example 1 with SessionState

use of org.whispersystems.libsignal.state.SessionState 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 SessionState

use of org.whispersystems.libsignal.state.SessionState in project toshi-android-client by toshiapp.

the class SignalSessionStore method loadSession.

@Override
public SessionRecord loadSession(@NonNull final 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) {
                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 (final IOException e) {
            LogUtil.exception("No existing session information found.", e);
            return new SessionRecord();
        }
    }
}
Also used : SessionStructure(org.whispersystems.libsignal.state.StorageProtos.SessionStructure) SessionState(org.whispersystems.libsignal.state.SessionState) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) SessionRecord(org.whispersystems.libsignal.state.SessionRecord)

Example 3 with SessionState

use of org.whispersystems.libsignal.state.SessionState in project Signal-Android by signalapp.

the class SessionStoreMigrationHelper method migrateSessions.

static void migrateSessions(Context context, SQLiteDatabase database) {
    File directory = new File(context.getFilesDir(), SESSIONS_DIRECTORY_V2);
    if (directory.exists()) {
        File[] sessionFiles = directory.listFiles();
        if (sessionFiles != null) {
            for (File sessionFile : sessionFiles) {
                try {
                    String[] parts = sessionFile.getName().split("[.]");
                    Address address = Address.fromSerialized(parts[0]);
                    int deviceId;
                    if (parts.length > 1)
                        deviceId = Integer.parseInt(parts[1]);
                    else
                        deviceId = SignalServiceAddress.DEFAULT_DEVICE_ID;
                    FileInputStream in = new FileInputStream(sessionFile);
                    int versionMarker = readInteger(in);
                    if (versionMarker > CURRENT_VERSION) {
                        throw new AssertionError("Unknown version: " + versionMarker + ", " + sessionFile.getAbsolutePath());
                    }
                    byte[] serialized = readBlob(in);
                    in.close();
                    if (versionMarker < PLAINTEXT_VERSION) {
                        throw new AssertionError("Not plaintext: " + versionMarker + ", " + sessionFile.getAbsolutePath());
                    }
                    SessionRecord sessionRecord;
                    if (versionMarker == SINGLE_STATE_VERSION) {
                        Log.w(TAG, "Migrating single state version: " + sessionFile.getAbsolutePath());
                        SessionStructure sessionStructure = SessionStructure.parseFrom(serialized);
                        SessionState sessionState = new SessionState(sessionStructure);
                        sessionRecord = new SessionRecord(sessionState);
                    } else if (versionMarker >= ARCHIVE_STATES_VERSION) {
                        Log.w(TAG, "Migrating session: " + sessionFile.getAbsolutePath());
                        sessionRecord = new SessionRecord(serialized);
                    } else {
                        throw new AssertionError("Unknown version: " + versionMarker + ", " + sessionFile.getAbsolutePath());
                    }
                    ContentValues contentValues = new ContentValues();
                    contentValues.put(SessionDatabase.ADDRESS, address.serialize());
                    contentValues.put(SessionDatabase.DEVICE, deviceId);
                    contentValues.put(SessionDatabase.RECORD, sessionRecord.serialize());
                    database.insert(SessionDatabase.TABLE_NAME, null, contentValues);
                } catch (NumberFormatException | IOException e) {
                    Log.w(TAG, e);
                }
            }
        }
    }
}
Also used : SessionStructure(org.whispersystems.libsignal.state.StorageProtos.SessionStructure) ContentValues(android.content.ContentValues) SessionState(org.whispersystems.libsignal.state.SessionState) Address(org.thoughtcrime.securesms.database.Address) SignalServiceAddress(org.whispersystems.signalservice.api.push.SignalServiceAddress) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) File(java.io.File) SessionRecord(org.whispersystems.libsignal.state.SessionRecord)

Aggregations

FileInputStream (java.io.FileInputStream)3 IOException (java.io.IOException)3 SessionRecord (org.whispersystems.libsignal.state.SessionRecord)3 SessionState (org.whispersystems.libsignal.state.SessionState)3 SessionStructure (org.whispersystems.libsignal.state.StorageProtos.SessionStructure)3 ContentValues (android.content.ContentValues)1 File (java.io.File)1 MasterCipher (org.thoughtcrime.securesms.crypto.MasterCipher)1 Address (org.thoughtcrime.securesms.database.Address)1 InvalidMessageException (org.whispersystems.libsignal.InvalidMessageException)1 SignalServiceAddress (org.whispersystems.signalservice.api.push.SignalServiceAddress)1