| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.net.http; |
| 18 |
|
| 19 |
import android.security.net.config.UserCertificateSource; |
| 20 |
|
| 21 |
import com.android.org.conscrypt.TrustManagerImpl; |
| 22 |
|
| 23 |
import java.lang.reflect.InvocationTargetException; |
| 24 |
import java.lang.reflect.Method; |
| 25 |
import java.security.cert.CertificateException; |
| 26 |
import java.security.cert.X509Certificate; |
| 27 |
import java.util.List; |
| 28 |
|
| 29 |
import javax.net.ssl.X509TrustManager; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public class X509TrustManagerExtensions { |
| 40 |
|
| 41 |
private final TrustManagerImpl mDelegate; |
| 42 |
|
| 43 |
private final X509TrustManager mTrustManager; |
| 44 |
private final Method mCheckServerTrusted; |
| 45 |
private final Method mIsSameTrustConfiguration; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public X509TrustManagerExtensions(X509TrustManager tm) throws IllegalArgumentException { |
| 54 |
if (tm instanceof TrustManagerImpl) { |
| 55 |
mDelegate = (TrustManagerImpl) tm; |
| 56 |
mTrustManager = null; |
| 57 |
mCheckServerTrusted = null; |
| 58 |
mIsSameTrustConfiguration = null; |
| 59 |
return; |
| 60 |
} |
| 61 |
|
| 62 |
mDelegate = null; |
| 63 |
mTrustManager = tm; |
| 64 |
|
| 65 |
try { |
| 66 |
mCheckServerTrusted = tm.getClass().getMethod("checkServerTrusted", |
| 67 |
X509Certificate[].class, |
| 68 |
String.class, |
| 69 |
String.class); |
| 70 |
} catch (NoSuchMethodException e) { |
| 71 |
throw new IllegalArgumentException("Required method" |
| 72 |
+ " checkServerTrusted(X509Certificate[], String, String, String) missing"); |
| 73 |
} |
| 74 |
|
| 75 |
Method isSameTrustConfiguration = null; |
| 76 |
try { |
| 77 |
isSameTrustConfiguration = tm.getClass().getMethod("isSameTrustConfiguration", |
| 78 |
String.class, |
| 79 |
String.class); |
| 80 |
} catch (ReflectiveOperationException ignored) { |
| 81 |
} |
| 82 |
mIsSameTrustConfiguration = isSameTrustConfiguration; |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
public List<X509Certificate> checkServerTrusted(X509Certificate[] chain, String authType, |
| 96 |
String host) throws CertificateException { |
| 97 |
if (mDelegate != null) { |
| 98 |
return mDelegate.checkServerTrusted(chain, authType, host); |
| 99 |
} else { |
| 100 |
try { |
| 101 |
return (List<X509Certificate>) mCheckServerTrusted.invoke(mTrustManager, chain, |
| 102 |
authType, host); |
| 103 |
} catch (IllegalAccessException e) { |
| 104 |
throw new CertificateException("Failed to call checkServerTrusted", e); |
| 105 |
} catch (InvocationTargetException e) { |
| 106 |
if (e.getCause() instanceof CertificateException) { |
| 107 |
throw (CertificateException) e.getCause(); |
| 108 |
} |
| 109 |
if (e.getCause() instanceof RuntimeException) { |
| 110 |
throw (RuntimeException) e.getCause(); |
| 111 |
} |
| 112 |
throw new CertificateException("checkServerTrusted failed", e.getCause()); |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
public boolean isUserAddedCertificate(X509Certificate cert) { |
| 128 |
return UserCertificateSource.getInstance().findBySubjectAndPublicKey(cert) != null; |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
public boolean isSameTrustConfiguration(String hostname1, String hostname2) { |
| 136 |
if (mIsSameTrustConfiguration == null) { |
| 137 |
return true; |
| 138 |
} |
| 139 |
try { |
| 140 |
return (Boolean) mIsSameTrustConfiguration.invoke(mTrustManager, hostname1, hostname2); |
| 141 |
} catch (IllegalAccessException e) { |
| 142 |
throw new RuntimeException("Failed to call isSameTrustConfiguration", e); |
| 143 |
} catch (InvocationTargetException e) { |
| 144 |
if (e.getCause() instanceof RuntimeException) { |
| 145 |
throw (RuntimeException) e.getCause(); |
| 146 |
} else { |
| 147 |
throw new RuntimeException("isSameTrustConfiguration failed", e.getCause()); |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
} |