From 54b1427471099456f9cec05b190239e4900cbea8 Mon Sep 17 00:00:00 2001 From: Andy Scherzinger Date: Fri, 16 Dec 2022 14:57:45 +0100 Subject: [PATCH] Implement closing for file input/output streams Signed-off-by: Andy Scherzinger --- .../talk/utils/ssl/MagicTrustManager.java | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 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 74484d0df..d33d1c2da 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 @@ -24,11 +24,12 @@ package com.nextcloud.talk.utils.ssl; import android.content.Context; import android.util.Log; + import com.nextcloud.talk.application.NextcloudTalkApplication; import com.nextcloud.talk.events.CertificateEvent; + import org.greenrobot.eventbus.EventBus; -import javax.net.ssl.*; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; @@ -37,6 +38,13 @@ import java.security.KeyStoreException; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; +import javax.net.ssl.HostnameVerifier; +import javax.net.ssl.SSLPeerUnverifiedException; +import javax.net.ssl.SSLSession; +import javax.net.ssl.TrustManager; +import javax.net.ssl.TrustManagerFactory; +import javax.net.ssl.X509TrustManager; + public class MagicTrustManager implements X509TrustManager { private static final String TAG = "MagicTrustManager"; @@ -49,9 +57,8 @@ public class MagicTrustManager implements X509TrustManager { keystoreFile = new File(NextcloudTalkApplication.Companion.getSharedApplication().getDir("CertsKeystore", Context.MODE_PRIVATE), "keystore.bks"); - try { + try (FileInputStream fileInputStream = new FileInputStream(keystoreFile)) { trustedKeyStore = KeyStore.getInstance(KeyStore.getDefaultType()); - FileInputStream fileInputStream = new FileInputStream(keystoreFile); trustedKeyStore.load(fileInputStream, null); } catch (Exception exception) { try { @@ -126,9 +133,8 @@ public class MagicTrustManager implements X509TrustManager { public void addCertInTrustStore(X509Certificate x509Certificate) { if (trustedKeyStore != null) { - try { + try (FileOutputStream fileOutputStream = new FileOutputStream(keystoreFile)) { trustedKeyStore.setCertificateEntry(x509Certificate.getSubjectDN().getName(), x509Certificate); - FileOutputStream fileOutputStream = new FileOutputStream(keystoreFile); trustedKeyStore.store(fileOutputStream, null); } catch (Exception exception) { Log.d(TAG, "Failed to set certificate entry " + exception.getLocalizedMessage());