| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.security.net.config; |
| 18 |
|
| 19 |
import android.compat.annotation.UnsupportedAppUsage; |
| 20 |
|
| 21 |
import java.net.Socket; |
| 22 |
import java.security.cert.CertificateException; |
| 23 |
import java.security.cert.X509Certificate; |
| 24 |
import java.util.List; |
| 25 |
|
| 26 |
import javax.net.ssl.SSLEngine; |
| 27 |
import javax.net.ssl.SSLSession; |
| 28 |
import javax.net.ssl.SSLSocket; |
| 29 |
import javax.net.ssl.X509ExtendedTrustManager; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
public class RootTrustManager extends X509ExtendedTrustManager { |
| 43 |
private final ApplicationConfig mConfig; |
| 44 |
|
| 45 |
public RootTrustManager(ApplicationConfig config) { |
| 46 |
if (config == null) { |
| 47 |
throw new NullPointerException("config must not be null"); |
| 48 |
} |
| 49 |
mConfig = config; |
| 50 |
} |
| 51 |
|
| 52 |
@Override |
| 53 |
public void checkClientTrusted(X509Certificate[] chain, String authType) |
| 54 |
throws CertificateException { |
| 55 |
|
| 56 |
|
| 57 |
NetworkSecurityConfig config = mConfig.getConfigForHostname(""); |
| 58 |
config.getTrustManager().checkClientTrusted(chain, authType); |
| 59 |
} |
| 60 |
|
| 61 |
@Override |
| 62 |
public void checkClientTrusted(X509Certificate[] certs, String authType, Socket socket) |
| 63 |
throws CertificateException { |
| 64 |
|
| 65 |
|
| 66 |
NetworkSecurityConfig config = mConfig.getConfigForHostname(""); |
| 67 |
config.getTrustManager().checkClientTrusted(certs, authType, socket); |
| 68 |
} |
| 69 |
|
| 70 |
@Override |
| 71 |
public void checkClientTrusted(X509Certificate[] certs, String authType, SSLEngine engine) |
| 72 |
throws CertificateException { |
| 73 |
|
| 74 |
|
| 75 |
NetworkSecurityConfig config = mConfig.getConfigForHostname(""); |
| 76 |
config.getTrustManager().checkClientTrusted(certs, authType, engine); |
| 77 |
} |
| 78 |
|
| 79 |
@Override |
| 80 |
public void checkServerTrusted(X509Certificate[] certs, String authType, Socket socket) |
| 81 |
throws CertificateException { |
| 82 |
if (socket instanceof SSLSocket) { |
| 83 |
SSLSocket sslSocket = (SSLSocket) socket; |
| 84 |
SSLSession session = sslSocket.getHandshakeSession(); |
| 85 |
if (session == null) { |
| 86 |
throw new CertificateException("Not in handshake; no session available"); |
| 87 |
} |
| 88 |
String host = session.getPeerHost(); |
| 89 |
NetworkSecurityConfig config = mConfig.getConfigForHostname(host); |
| 90 |
config.getTrustManager().checkServerTrusted(certs, authType, socket); |
| 91 |
} else { |
| 92 |
|
| 93 |
checkServerTrusted(certs, authType); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
@Override |
| 98 |
public void checkServerTrusted(X509Certificate[] certs, String authType, SSLEngine engine) |
| 99 |
throws CertificateException { |
| 100 |
SSLSession session = engine.getHandshakeSession(); |
| 101 |
if (session == null) { |
| 102 |
throw new CertificateException("Not in handshake; no session available"); |
| 103 |
} |
| 104 |
String host = session.getPeerHost(); |
| 105 |
NetworkSecurityConfig config = mConfig.getConfigForHostname(host); |
| 106 |
config.getTrustManager().checkServerTrusted(certs, authType, engine); |
| 107 |
} |
| 108 |
|
| 109 |
@Override |
| 110 |
public void checkServerTrusted(X509Certificate[] certs, String authType) |
| 111 |
throws CertificateException { |
| 112 |
if (mConfig.hasPerDomainConfigs()) { |
| 113 |
throw new CertificateException( |
| 114 |
"Domain specific configurations require that hostname aware" |
| 115 |
+ " checkServerTrusted(X509Certificate[], String, String) is used"); |
| 116 |
} |
| 117 |
NetworkSecurityConfig config = mConfig.getConfigForHostname(""); |
| 118 |
config.getTrustManager().checkServerTrusted(certs, authType); |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
@UnsupportedAppUsage |
| 127 |
public List<X509Certificate> checkServerTrusted(X509Certificate[] certs, String authType, |
| 128 |
String hostname) throws CertificateException { |
| 129 |
if (hostname == null && mConfig.hasPerDomainConfigs()) { |
| 130 |
throw new CertificateException( |
| 131 |
"Domain specific configurations require that the hostname be provided"); |
| 132 |
} |
| 133 |
NetworkSecurityConfig config = mConfig.getConfigForHostname(hostname); |
| 134 |
return config.getTrustManager().checkServerTrusted(certs, authType, hostname); |
| 135 |
} |
| 136 |
|
| 137 |
@Override |
| 138 |
public X509Certificate[] getAcceptedIssuers() { |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
NetworkSecurityConfig config = mConfig.getConfigForHostname(""); |
| 143 |
return config.getTrustManager().getAcceptedIssuers(); |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
public boolean isSameTrustConfiguration(String hostname1, String hostname2) { |
| 153 |
return mConfig.getConfigForHostname(hostname1) |
| 154 |
.equals(mConfig.getConfigForHostname(hostname2)); |
| 155 |
} |
| 156 |
} |