From 27952c00eded7cec1ef7ea0740c0a193e335e81b Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Thu, 19 Jan 2023 15:29:47 +0100 Subject: [PATCH] flush credential store file --- .../android/database/CredentialStore.java | 22 ++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/main/java/im/conversations/android/database/CredentialStore.java b/src/main/java/im/conversations/android/database/CredentialStore.java index c0678c4af..126d76f18 100644 --- a/src/main/java/im/conversations/android/database/CredentialStore.java +++ b/src/main/java/im/conversations/android/database/CredentialStore.java @@ -173,7 +173,7 @@ public class CredentialStore { final Map store; try { store = load(); - } catch (final GeneralSecurityException | IOException e) { + } catch (final Exception e) { return ImmutableMap.of(); } return store == null ? ImmutableMap.of() : store; @@ -188,19 +188,31 @@ public class CredentialStore { private void store(final Map store) throws GeneralSecurityException, IOException { - final EncryptedFile encryptedFile = getEncryptedFile(); - final FileOutputStream outputStream = encryptedFile.openFileOutput(); - GSON.toJson(store, new OutputStreamWriter(outputStream)); + final File file = getCredentialStoreFile(); + file.delete(); + final EncryptedFile encryptedFile = getEncryptedFile(file); + try (final FileOutputStream outputStream = encryptedFile.openFileOutput()) { + GSON.toJson(store, new OutputStreamWriter(outputStream)); + outputStream.flush(); + } } private EncryptedFile getEncryptedFile() throws GeneralSecurityException, IOException { + return getEncryptedFile(getCredentialStoreFile()); + } + + private EncryptedFile getEncryptedFile(final File file) throws GeneralSecurityException, IOException { final KeyGenParameterSpec keyGenParameterSpec = MasterKeys.AES256_GCM_SPEC; final String mainKeyAlias = MasterKeys.getOrCreate(keyGenParameterSpec); return new EncryptedFile.Builder( - new File(context.getFilesDir(), FILENAME), + file, context, mainKeyAlias, EncryptedFile.FileEncryptionScheme.AES256_GCM_HKDF_4KB) .build(); } + + private File getCredentialStoreFile() { + return new File(context.getFilesDir(), FILENAME); + } }