|
| 1 | +package com.datatheorem.android.trustkit.pinning; |
| 2 | + |
| 3 | +import android.net.http.X509TrustManagerExtensions; |
| 4 | + |
| 5 | +import androidx.annotation.NonNull; |
| 6 | +import androidx.annotation.RequiresApi; |
| 7 | + |
| 8 | +import com.datatheorem.android.trustkit.TrustKit; |
| 9 | +import com.datatheorem.android.trustkit.config.DomainPinningPolicy; |
| 10 | + |
| 11 | +import java.security.cert.CertificateException; |
| 12 | +import java.security.cert.X509Certificate; |
| 13 | + |
| 14 | +import javax.net.ssl.X509TrustManager; |
| 15 | + |
| 16 | +/** |
| 17 | + * {@link X509TrustManager} used for Certificate Pinning. |
| 18 | + * |
| 19 | + * <p>This trust manager delegates to the appropriate {@link PinningTrustManager} decided by the |
| 20 | + * hostname set by the {@link PinningInterceptor}.</p> |
| 21 | + */ |
| 22 | +@RequiresApi(api = 17) |
| 23 | +class RootTrustManager implements X509TrustManager { |
| 24 | + private final ThreadLocal<String> mServerHostname = new ThreadLocal<>(); |
| 25 | + |
| 26 | + @Override |
| 27 | + public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { |
| 28 | + TrustKit.getInstance().getTrustManager(mServerHostname.get()).checkClientTrusted(chain, authType); |
| 29 | + } |
| 30 | + |
| 31 | + @Override |
| 32 | + public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { |
| 33 | + String host = mServerHostname.get(); |
| 34 | + DomainPinningPolicy serverConfig = |
| 35 | + TrustKit.getInstance().getConfiguration().getPolicyForHostname(host); |
| 36 | + //This check is needed for compatibility with the Platform default's implementation of |
| 37 | + //the Trust Manager. For APIs 24 and greater, the Platform's default TrustManager states |
| 38 | + //that it requires usage of the hostname-aware version of checkServerTrusted for app's that |
| 39 | + //implement Android's network_security_config file. |
| 40 | + if (serverConfig == null) { |
| 41 | + new X509TrustManagerExtensions(TrustKit.getInstance().getTrustManager(host)).checkServerTrusted(chain, authType, host); |
| 42 | + } else { |
| 43 | + TrustKit.getInstance().getTrustManager(host).checkServerTrusted(chain, authType); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public X509Certificate[] getAcceptedIssuers() { |
| 49 | + return new X509Certificate[0]; |
| 50 | + } |
| 51 | + |
| 52 | + void setServerHostname(@NonNull String serverHostname) { |
| 53 | + mServerHostname.set(serverHostname); |
| 54 | + } |
| 55 | +} |
0 commit comments