Skip to content

Commit 21f5f1d

Browse files
committed
Add ThreadLocal for last server certificate in AdvancedX509TrustManager and update RemoteOperationResult to utilize it for SSL exceptions
1 parent cd0e178 commit 21f5f1d

2 files changed

Lines changed: 9 additions & 1 deletion

File tree

opencloudComLibrary/src/main/java/eu/opencloud/android/lib/common/network/AdvancedX509TrustManager.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,16 @@ public void checkClientTrusted(X509Certificate[] certificates, String authType)
8484
mStandardTrustManager.checkClientTrusted(certificates, authType);
8585
}
8686

87+
public static final ThreadLocal<X509Certificate> sLastCert = new ThreadLocal<>();
88+
8789
/**
8890
* @see javax.net.ssl.X509TrustManager#checkServerTrusted(X509Certificate[],
8991
* String authType)
9092
*/
9193
public void checkServerTrusted(X509Certificate[] certificates, String authType) {
94+
if (certificates != null && certificates.length > 0) {
95+
sLastCert.set(certificates[0]);
96+
}
9297
if (!isKnownServer(certificates[0])) {
9398
CertificateCombinedException result = new CertificateCombinedException(certificates[0]);
9499
try {

opencloudComLibrary/src/main/java/eu/opencloud/android/lib/common/operations/RemoteOperationResult.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import eu.opencloud.android.lib.common.accounts.AccountUtils;
3333
import eu.opencloud.android.lib.common.http.HttpConstants;
3434
import eu.opencloud.android.lib.common.http.methods.HttpBaseMethod;
35+
import eu.opencloud.android.lib.common.network.AdvancedX509TrustManager;
3536
import eu.opencloud.android.lib.common.network.CertificateCombinedException;
3637
import okhttp3.Headers;
3738
import org.apache.commons.lang3.exception.ExceptionUtils;
@@ -148,8 +149,10 @@ public RemoteOperationResult(Exception e) {
148149

149150
} else if (e instanceof SSLException || e instanceof RuntimeException) {
150151
if (e instanceof SSLPeerUnverifiedException) {
152+
java.security.cert.X509Certificate lastCert = AdvancedX509TrustManager.sLastCert.get();
153+
AdvancedX509TrustManager.sLastCert.remove();
151154
CertificateCombinedException sslPeerUnverifiedException =
152-
new CertificateCombinedException(null);
155+
new CertificateCombinedException(lastCert);
153156
sslPeerUnverifiedException.setSslPeerUnverifiedException((SSLPeerUnverifiedException) e);
154157
sslPeerUnverifiedException.initCause(e);
155158
mException = sslPeerUnverifiedException;

0 commit comments

Comments
 (0)