Skip to content

Commit 7f30c21

Browse files
committed
fix: tls trust known cert hostname
1 parent b69ef77 commit 7f30c21

3 files changed

Lines changed: 77 additions & 0 deletions

File tree

‎owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/HttpClient.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
import com.owncloud.android.lib.common.http.logging.LogInterceptor;
3030
import com.owncloud.android.lib.common.network.AdvancedX509TrustManager;
31+
import com.owncloud.android.lib.common.network.KnownServersHostnameVerifier;
3132
import com.owncloud.android.lib.common.network.NetworkUtils;
3233
import okhttp3.Cookie;
3334
import okhttp3.CookieJar;
@@ -125,6 +126,7 @@ private OkHttpClient buildNewOkHttpClient(SSLSocketFactory sslSocketFactory, X50
125126
.connectTimeout(HttpConstants.DEFAULT_CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS)
126127
.followRedirects(false)
127128
.sslSocketFactory(sslSocketFactory, trustManager)
129+
.hostnameVerifier(new KnownServersHostnameVerifier(mContext))
128130
.cookieJar(cookieJar)
129131
.build();
130132
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
* ownCloud Android client application
3+
*
4+
* Copyright (C) 2026 ownCloud GmbH.
5+
*
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License version 2,
8+
* as published by the Free Software Foundation.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
19+
package com.owncloud.android.lib.common.network;
20+
21+
import android.content.Context;
22+
23+
import okhttp3.internal.tls.OkHostnameVerifier;
24+
import timber.log.Timber;
25+
26+
import javax.net.ssl.HostnameVerifier;
27+
import javax.net.ssl.SSLPeerUnverifiedException;
28+
import javax.net.ssl.SSLSession;
29+
import java.security.cert.Certificate;
30+
import java.security.cert.X509Certificate;
31+
public class KnownServersHostnameVerifier implements HostnameVerifier {
32+
33+
private final Context mContext;
34+
private final HostnameVerifier mDelegate;
35+
36+
public KnownServersHostnameVerifier(Context context) {
37+
this(context, OkHostnameVerifier.INSTANCE);
38+
}
39+
40+
KnownServersHostnameVerifier(Context context, HostnameVerifier delegate) {
41+
if (context == null) {
42+
throw new IllegalArgumentException("Context may not be NULL!");
43+
}
44+
mContext = context.getApplicationContext() != null ? context.getApplicationContext() : context;
45+
mDelegate = delegate;
46+
}
47+
48+
@Override
49+
public boolean verify(String hostname, SSLSession session) {
50+
if (mDelegate.verify(hostname, session)) {
51+
return true;
52+
}
53+
try {
54+
Certificate[] peerCerts = session.getPeerCertificates();
55+
if (peerCerts.length > 0 && peerCerts[0] instanceof X509Certificate) {
56+
return NetworkUtils.isCertInKnownServersStore(peerCerts[0], mContext);
57+
}
58+
} catch (SSLPeerUnverifiedException e) {
59+
Timber.d(e, "No peer certificates during hostname verification for %s", hostname);
60+
}
61+
return false;
62+
}
63+
}

‎owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/network/NetworkUtils.java‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,16 @@ public static void addCertToKnownServersStore(Certificate cert, Context context)
9494
}
9595
}
9696

97+
public static boolean isCertInKnownServersStore(Certificate cert, Context context) {
98+
if (cert == null || context == null) {
99+
return false;
100+
}
101+
try {
102+
return getKnownServersStore(context).getCertificateAlias(cert) != null;
103+
} catch (KeyStoreException | IOException | NoSuchAlgorithmException | CertificateException e) {
104+
Timber.e(e, "Fail while checking certificate in the known-servers store");
105+
return false;
106+
}
107+
}
108+
97109
}

0 commit comments

Comments
 (0)