Split different exception scopes

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 <t@timkrueger.me>
This commit is contained in:
Tim Krüger 2023-01-09 13:04:59 +01:00 committed by Marcel Hibbe (Rebase PR Action)
parent 1bbbe20834
commit eabd0b2d2c

View File

@ -54,13 +54,22 @@ public class MagicTrustManager implements X509TrustManager {
private KeyStore trustedKeyStore = null; private KeyStore trustedKeyStore = null;
public MagicTrustManager() { public MagicTrustManager() {
keystoreFile = new File(NextcloudTalkApplication.Companion.getSharedApplication().getDir("CertsKeystore", keystoreFile = new File(NextcloudTalkApplication.Companion.getSharedApplication()
Context.MODE_PRIVATE), "keystore.bks"); .getDir("CertsKeystore", Context.MODE_PRIVATE),
"keystore.bks");
try (FileInputStream fileInputStream = new FileInputStream(keystoreFile)) { try {
trustedKeyStore = KeyStore.getInstance(KeyStore.getDefaultType()); trustedKeyStore = KeyStore.getInstance(KeyStore.getDefaultType());
trustedKeyStore.load(fileInputStream, null); } catch (KeyStoreException e) {
} catch (Exception exception) { 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 { try {
trustedKeyStore.load(null, null); trustedKeyStore.load(null, null);
} catch (Exception e) { } catch (Exception e) {
@ -71,7 +80,7 @@ public class MagicTrustManager implements X509TrustManager {
TrustManagerFactory trustManagerFactory = null; TrustManagerFactory trustManagerFactory = null;
try { try {
trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory. trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.
getDefaultAlgorithm()); getDefaultAlgorithm());
trustManagerFactory.init((KeyStore) null); trustManagerFactory.init((KeyStore) null);
@ -101,10 +110,10 @@ public class MagicTrustManager implements X509TrustManager {
} catch (CertificateException e) { } catch (CertificateException e) {
if (!isCertInMagicTrustStore(x509Certificate)) { if (!isCertInMagicTrustStore(x509Certificate)) {
EventBus.getDefault().post(new CertificateEvent(x509Certificate, this, EventBus.getDefault().post(new CertificateEvent(x509Certificate, this,
null)); null));
long startTime = System.currentTimeMillis(); long startTime = System.currentTimeMillis();
while (!isCertInMagicTrustStore(x509Certificate) && System.currentTimeMillis() <= while (!isCertInMagicTrustStore(x509Certificate) && System.currentTimeMillis() <=
startTime + 15000) { startTime + 15000) {
//do nothing //do nothing
} }
return isCertInMagicTrustStore(x509Certificate); return isCertInMagicTrustStore(x509Certificate);