From eabd0b2d2cf3801228059c5d576a689358e63531 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Kr=C3=BCger?= Date: Mon, 9 Jan 2023 13:04:59 +0100 Subject: [PATCH] Split different exception scopes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Before this change the initinalization of the trusted key store was combined in one try-catch-block. If anything went wrong an new in memory trusted key store was created. Programming against an exception is an bad pattern. So the initialization is now splitted into multiple try-catch-blocks with it's own scopes and the decision if the trusted key store is newly created in memory or loaded from a exisitng file is done by an if condition check. Signed-off-by: Tim Krüger --- .../talk/utils/ssl/MagicTrustManager.java | 27 ++++++++++++------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/com/nextcloud/talk/utils/ssl/MagicTrustManager.java b/app/src/main/java/com/nextcloud/talk/utils/ssl/MagicTrustManager.java index d33d1c2da..7d9e76c8f 100644 --- a/app/src/main/java/com/nextcloud/talk/utils/ssl/MagicTrustManager.java +++ b/app/src/main/java/com/nextcloud/talk/utils/ssl/MagicTrustManager.java @@ -54,13 +54,22 @@ public class MagicTrustManager implements X509TrustManager { private KeyStore trustedKeyStore = null; public MagicTrustManager() { - keystoreFile = new File(NextcloudTalkApplication.Companion.getSharedApplication().getDir("CertsKeystore", - Context.MODE_PRIVATE), "keystore.bks"); - - try (FileInputStream fileInputStream = new FileInputStream(keystoreFile)) { + keystoreFile = new File(NextcloudTalkApplication.Companion.getSharedApplication() + .getDir("CertsKeystore", Context.MODE_PRIVATE), + "keystore.bks"); + try { trustedKeyStore = KeyStore.getInstance(KeyStore.getDefaultType()); - trustedKeyStore.load(fileInputStream, null); - } catch (Exception exception) { + } catch (KeyStoreException e) { + Log.e(TAG, "Trusted key store can't be created.", e); + } + + if (keystoreFile.exists()) { + try (FileInputStream fileInputStream = new FileInputStream(keystoreFile)) { + trustedKeyStore.load(fileInputStream, null); + } catch (Exception exception) { + Log.e(TAG, "Error during opening the trusted key store.", exception); + } + } else { try { trustedKeyStore.load(null, null); } catch (Exception e) { @@ -71,7 +80,7 @@ public class MagicTrustManager implements X509TrustManager { TrustManagerFactory trustManagerFactory = null; try { trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory. - getDefaultAlgorithm()); + getDefaultAlgorithm()); trustManagerFactory.init((KeyStore) null); @@ -101,10 +110,10 @@ public class MagicTrustManager implements X509TrustManager { } catch (CertificateException e) { if (!isCertInMagicTrustStore(x509Certificate)) { EventBus.getDefault().post(new CertificateEvent(x509Certificate, this, - null)); + null)); long startTime = System.currentTimeMillis(); while (!isCertInMagicTrustStore(x509Certificate) && System.currentTimeMillis() <= - startTime + 15000) { + startTime + 15000) { //do nothing } return isCertInMagicTrustStore(x509Certificate);