From 0a49c7094fac8f5b7f9069f2fe2f0d38318b1360 Mon Sep 17 00:00:00 2001
From: Lorenzo Pichilli <pichillilorenzo@gmail.com>
Date: Thu, 13 Oct 2022 17:37:55 +0200
Subject: [PATCH] merged Android - Load client certificate from local storage

---
 CHANGELOG.md                                  |  1 +
 .../flutter_inappwebview/Util.java            | 22 ++++++++++++++++---
 2 files changed, 20 insertions(+), 3 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 60316b14..e45c05f7 100755
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -40,6 +40,7 @@
 - Merged "Fix RangeError: Maximum call stack size exceeded" [#1208](https://github.com/pichillilorenzo/flutter_inappwebview/pull/1208) (thanks to [liasica](https://github.com/liasica))
 - Merged "fix: try to open with Chrome if default browser app does not support custom tabs" [#1233](https://github.com/pichillilorenzo/flutter_inappwebview/pull/1233) (thanks to [addie9000](https://github.com/addie9000))
 - Merged "fix: Prevent Android java.lang.NullPointerException in InAppWebViewCl…" [#1237](https://github.com/pichillilorenzo/flutter_inappwebview/pull/1237) (thanks to [kamilpowalowski](https://github.com/kamilpowalowski))
+- Merged "Android - Load client certificate from local storage" [#1241](https://github.com/pichillilorenzo/flutter_inappwebview/pull/1241) (thanks to [akioyamamoto1977](https://github.com/akioyamamoto1977))
 
 ## 5.4.4+3
 
diff --git a/android/src/main/java/com/pichillilorenzo/flutter_inappwebview/Util.java b/android/src/main/java/com/pichillilorenzo/flutter_inappwebview/Util.java
index e102a72a..79acae67 100755
--- a/android/src/main/java/com/pichillilorenzo/flutter_inappwebview/Util.java
+++ b/android/src/main/java/com/pichillilorenzo/flutter_inappwebview/Util.java
@@ -21,6 +21,7 @@ import org.json.JSONObject;
 
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
+import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.lang.reflect.InvocationTargetException;
@@ -110,17 +111,23 @@ public class Util {
                                                                        @Nullable String certificatePassword,
                                                                        @NonNull String keyStoreType) {
     PrivateKeyAndCertificates privateKeyAndCertificates = null;
+    InputStream certificateFileStream = null;
 
     try {
-      InputStream certificateFileStream = getFileAsset(plugin, certificatePath);
+      certificateFileStream = getFileAsset(plugin, certificatePath);
+    } catch (IOException ignored) {}
 
+    try {
+      if (certificateFileStream == null) {
+        certificateFileStream = new FileInputStream(certificatePath);
+      }
       KeyStore keyStore = KeyStore.getInstance(keyStoreType);
-      keyStore.load(certificateFileStream, certificatePassword != null ? certificatePassword.toCharArray() : null);
+      keyStore.load(certificateFileStream, (certificatePassword != null ? certificatePassword : "").toCharArray());
 
       Enumeration<String> aliases = keyStore.aliases();
       String alias = aliases.nextElement();
 
-      Key key = keyStore.getKey(alias, certificatePassword != null ? certificatePassword.toCharArray() : null);
+      Key key = keyStore.getKey(alias, (certificatePassword != null ? certificatePassword : "").toCharArray());
       if (key instanceof PrivateKey) {
         PrivateKey privateKey = (PrivateKey)key;
         Certificate cert = keyStore.getCertificate(alias);
@@ -132,6 +139,15 @@ public class Util {
     } catch (Exception e) {
       e.printStackTrace();
       Log.e(LOG_TAG, e.getMessage());
+    } finally {
+      if (certificateFileStream != null) {
+        try {
+          certificateFileStream.close();
+        } catch (IOException ex) {
+          ex.printStackTrace();
+          Log.e(LOG_TAG, ex.getMessage());
+        }
+      }
     }
 
     return privateKeyAndCertificates;