|
| 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 | +} |
0 commit comments