diff --git a/app/src/main/java/com/nextcloud/talk/activities/BaseActivity.java b/app/src/main/java/com/nextcloud/talk/activities/BaseActivity.java index 5fe288096..eaf6e645b 100644 --- a/app/src/main/java/com/nextcloud/talk/activities/BaseActivity.java +++ b/app/src/main/java/com/nextcloud/talk/activities/BaseActivity.java @@ -92,7 +92,7 @@ public class BaseActivity extends AppCompatActivity { private void checkIfWeAreSecure() { keyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE); if (keyguardManager != null && keyguardManager.isKeyguardSecure() && appPreferences.getIsScreenLocked()) { - if (!SecurityUtils.checkIfWeAreAuthenticated()) { + if (!SecurityUtils.checkIfWeAreAuthenticated(appPreferences.getScreenLockTimeout())) { showAuthenticationScreen(); } } @@ -110,7 +110,7 @@ public class BaseActivity extends AppCompatActivity { if (requestCode == REQUEST_CODE_CONFIRM_DEVICE_CREDENTIALS) { if (resultCode == RESULT_OK) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { - if (SecurityUtils.checkIfWeAreAuthenticated()) { + if (SecurityUtils.checkIfWeAreAuthenticated(appPreferences.getScreenLockTimeout())) { // all went well } } diff --git a/app/src/main/java/com/nextcloud/talk/utils/SecurityUtils.java b/app/src/main/java/com/nextcloud/talk/utils/SecurityUtils.java index cf8a08f77..90424b7ee 100644 --- a/app/src/main/java/com/nextcloud/talk/utils/SecurityUtils.java +++ b/app/src/main/java/com/nextcloud/talk/utils/SecurityUtils.java @@ -44,7 +44,7 @@ public class SecurityUtils { private static final byte[] SECRET_BYTE_ARRAY = new byte[]{1, 2, 3, 4, 5, 6}; @RequiresApi(api = Build.VERSION_CODES.M) - public static boolean checkIfWeAreAuthenticated() { + public static boolean checkIfWeAreAuthenticated(String screenLockTimeout) { try { KeyStore keyStore = KeyStore.getInstance("AndroidKeyStore"); keyStore.load(null); @@ -63,11 +63,17 @@ public class SecurityUtils { // User is not authenticated, let's authenticate with device credentials. return false; } catch (KeyPermanentlyInvalidatedException e) { + // This happens if the lock screen has been disabled or reset after the key was + // generated after the key was generated. + // Shouldnt really happen because we regenerate the key every time an activity + // is created, but oh well + // Create key, and attempt again + createKey(screenLockTimeout); return false; } catch (BadPaddingException | IllegalBlockSizeException | KeyStoreException | CertificateException | UnrecoverableKeyException | IOException | NoSuchPaddingException | NoSuchAlgorithmException | InvalidKeyException e) { - return false; + throw new RuntimeException(e); } }