Avoid NPE on certificates that cannot be read
Skip certificates in a DirectoryCertificateSource that cannot be read to
due IOExceptions or CertificateExceptions, this prevents a NPE but
connections will still fail due to the certificate being unusable and no
valid trust-anchor existing.
This also logs the error since this really shouldn't happen.
Bug: 29997695
Change-Id: I9f7327efc302a259fb951f1f61f7fc4d647821fa
diff --git a/core/java/android/security/net/config/DirectoryCertificateSource.java b/core/java/android/security/net/config/DirectoryCertificateSource.java
index e3c9d65..119f5d0 100644
--- a/core/java/android/security/net/config/DirectoryCertificateSource.java
+++ b/core/java/android/security/net/config/DirectoryCertificateSource.java
@@ -19,6 +19,7 @@
import android.os.Environment;
import android.os.UserHandle;
import android.util.ArraySet;
+import android.util.Log;
import android.util.Pair;
import java.io.BufferedInputStream;
import java.io.File;
@@ -44,6 +45,7 @@
* @hide
*/
abstract class DirectoryCertificateSource implements CertificateSource {
+ private static final String LOG_TAG = "DirectoryCertificateSrc";
private final File mDir;
private final Object mLock = new Object();
private final CertificateFactory mCertFactory;
@@ -149,6 +151,9 @@
continue;
}
X509Certificate cert = readCertificate(fileName);
+ if (cert == null) {
+ continue;
+ }
if (!subj.equals(cert.getSubjectX500Principal())) {
continue;
}
@@ -173,6 +178,9 @@
continue;
}
X509Certificate cert = readCertificate(fileName);
+ if (cert == null) {
+ continue;
+ }
if (!subj.equals(cert.getSubjectX500Principal())) {
continue;
}
@@ -194,6 +202,7 @@
is = new BufferedInputStream(new FileInputStream(new File(mDir, file)));
return (X509Certificate) mCertFactory.generateCertificate(is);
} catch (CertificateException | IOException e) {
+ Log.e(LOG_TAG, "Failed to read certificate from " + file, e);
return null;
} finally {
IoUtils.closeQuietly(is);