Implement closing for file input/output streams

Signed-off-by: Andy Scherzinger <info@andy-scherzinger.de>
This commit is contained in:
Andy Scherzinger 2022-12-16 14:57:45 +01:00
parent ae6b7156c0
commit 54b1427471
No known key found for this signature in database
GPG key ID: 6CADC7E3523C308B

View file

@ -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());