| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.net; |
| 18 |
|
| 19 |
import android.compat.annotation.UnsupportedAppUsage; |
| 20 |
import android.os.Build; |
| 21 |
import android.os.SystemProperties; |
| 22 |
import android.util.Log; |
| 23 |
|
| 24 |
import com.android.internal.annotations.VisibleForTesting; |
| 25 |
import com.android.internal.os.RoSystemProperties; |
| 26 |
import com.android.org.conscrypt.ClientSessionContext; |
| 27 |
import com.android.org.conscrypt.OpenSSLSocketImpl; |
| 28 |
import com.android.org.conscrypt.SSLClientSessionCache; |
| 29 |
|
| 30 |
import java.io.IOException; |
| 31 |
import java.net.InetAddress; |
| 32 |
import java.net.Socket; |
| 33 |
import java.net.SocketException; |
| 34 |
import java.security.KeyManagementException; |
| 35 |
import java.security.NoSuchAlgorithmException; |
| 36 |
import java.security.NoSuchProviderException; |
| 37 |
import java.security.PrivateKey; |
| 38 |
import java.security.cert.X509Certificate; |
| 39 |
|
| 40 |
import javax.net.SocketFactory; |
| 41 |
import javax.net.ssl.HostnameVerifier; |
| 42 |
import javax.net.ssl.HttpsURLConnection; |
| 43 |
import javax.net.ssl.KeyManager; |
| 44 |
import javax.net.ssl.SSLContext; |
| 45 |
import javax.net.ssl.SSLException; |
| 46 |
import javax.net.ssl.SSLPeerUnverifiedException; |
| 47 |
import javax.net.ssl.SSLSession; |
| 48 |
import javax.net.ssl.SSLSocket; |
| 49 |
import javax.net.ssl.SSLSocketFactory; |
| 50 |
import javax.net.ssl.TrustManager; |
| 51 |
import javax.net.ssl.X509TrustManager; |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
@Deprecated |
| 101 |
public class SSLCertificateSocketFactory extends SSLSocketFactory { |
| 102 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
| 103 |
private static final String TAG = "SSLCertificateSocketFactory"; |
| 104 |
|
| 105 |
@UnsupportedAppUsage |
| 106 |
private static final TrustManager[] INSECURE_TRUST_MANAGER = new TrustManager[] { |
| 107 |
new X509TrustManager() { |
| 108 |
public X509Certificate[] getAcceptedIssuers() { return null; } |
| 109 |
public void checkClientTrusted(X509Certificate[] certs, String authType) { } |
| 110 |
public void checkServerTrusted(X509Certificate[] certs, String authType) { } |
| 111 |
} |
| 112 |
}; |
| 113 |
|
| 114 |
@UnsupportedAppUsage |
| 115 |
private SSLSocketFactory mInsecureFactory = null; |
| 116 |
@UnsupportedAppUsage |
| 117 |
private SSLSocketFactory mSecureFactory = null; |
| 118 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 119 |
private TrustManager[] mTrustManagers = null; |
| 120 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 121 |
private KeyManager[] mKeyManagers = null; |
| 122 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 123 |
private byte[] mNpnProtocols = null; |
| 124 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 125 |
private byte[] mAlpnProtocols = null; |
| 126 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 127 |
private PrivateKey mChannelIdPrivateKey = null; |
| 128 |
|
| 129 |
@UnsupportedAppUsage |
| 130 |
private final int mHandshakeTimeoutMillis; |
| 131 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 132 |
private final SSLClientSessionCache mSessionCache; |
| 133 |
@UnsupportedAppUsage |
| 134 |
private final boolean mSecure; |
| 135 |
|
| 136 |
|
| 137 |
@Deprecated |
| 138 |
public SSLCertificateSocketFactory(int handshakeTimeoutMillis) { |
| 139 |
this(handshakeTimeoutMillis, null, true); |
| 140 |
} |
| 141 |
|
| 142 |
@UnsupportedAppUsage |
| 143 |
private SSLCertificateSocketFactory( |
| 144 |
int handshakeTimeoutMillis, SSLSessionCache cache, boolean secure) { |
| 145 |
mHandshakeTimeoutMillis = handshakeTimeoutMillis; |
| 146 |
mSessionCache = cache == null ? null : cache.mSessionCache; |
| 147 |
mSecure = secure; |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
public static SocketFactory getDefault(int handshakeTimeoutMillis) { |
| 158 |
return new SSLCertificateSocketFactory(handshakeTimeoutMillis, null, true); |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
public static SSLSocketFactory getDefault(int handshakeTimeoutMillis, SSLSessionCache cache) { |
| 171 |
return new SSLCertificateSocketFactory(handshakeTimeoutMillis, cache, true); |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
public static SSLSocketFactory getInsecure(int handshakeTimeoutMillis, SSLSessionCache cache) { |
| 187 |
return new SSLCertificateSocketFactory(handshakeTimeoutMillis, cache, false); |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
@Deprecated |
| 207 |
public static org.apache.http.conn.ssl.SSLSocketFactory getHttpSocketFactory( |
| 208 |
int handshakeTimeoutMillis, SSLSessionCache cache) { |
| 209 |
return new org.apache.http.conn.ssl.SSLSocketFactory( |
| 210 |
new SSLCertificateSocketFactory(handshakeTimeoutMillis, cache, true)); |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
@UnsupportedAppUsage |
| 231 |
public static void verifyHostname(Socket socket, String hostname) throws IOException { |
| 232 |
if (!(socket instanceof SSLSocket)) { |
| 233 |
throw new IllegalArgumentException("Attempt to verify non-SSL socket"); |
| 234 |
} |
| 235 |
|
| 236 |
if (!isSslCheckRelaxed()) { |
| 237 |
|
| 238 |
|
| 239 |
SSLSocket ssl = (SSLSocket) socket; |
| 240 |
ssl.startHandshake(); |
| 241 |
|
| 242 |
SSLSession session = ssl.getSession(); |
| 243 |
if (session == null) { |
| 244 |
throw new SSLException("Cannot verify SSL socket without session"); |
| 245 |
} |
| 246 |
if (!HttpsURLConnection.getDefaultHostnameVerifier().verify(hostname, session)) { |
| 247 |
throw new SSLPeerUnverifiedException("Cannot verify hostname: " + hostname); |
| 248 |
} |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 253 |
private SSLSocketFactory makeSocketFactory( |
| 254 |
KeyManager[] keyManagers, TrustManager[] trustManagers) { |
| 255 |
try { |
| 256 |
SSLContext sslContext = SSLContext.getInstance("TLS", "AndroidOpenSSL"); |
| 257 |
sslContext.init(keyManagers, trustManagers, null); |
| 258 |
((ClientSessionContext) sslContext.getClientSessionContext()) |
| 259 |
.setPersistentCache(mSessionCache); |
| 260 |
return sslContext.getSocketFactory(); |
| 261 |
} catch (KeyManagementException | NoSuchAlgorithmException | NoSuchProviderException e) { |
| 262 |
Log.wtf(TAG, e); |
| 263 |
return (SSLSocketFactory) SSLSocketFactory.getDefault(); |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
@UnsupportedAppUsage |
| 268 |
private static boolean isSslCheckRelaxed() { |
| 269 |
return RoSystemProperties.DEBUGGABLE && |
| 270 |
SystemProperties.getBoolean("socket.relaxsslcheck", false); |
| 271 |
} |
| 272 |
|
| 273 |
@UnsupportedAppUsage |
| 274 |
private synchronized SSLSocketFactory getDelegate() { |
| 275 |
|
| 276 |
if (!mSecure || isSslCheckRelaxed()) { |
| 277 |
if (mInsecureFactory == null) { |
| 278 |
if (mSecure) { |
| 279 |
Log.w(TAG, "*** BYPASSING SSL SECURITY CHECKS (socket.relaxsslcheck=yes) ***"); |
| 280 |
} else { |
| 281 |
Log.w(TAG, "Bypassing SSL security checks at caller's request"); |
| 282 |
} |
| 283 |
mInsecureFactory = makeSocketFactory(mKeyManagers, INSECURE_TRUST_MANAGER); |
| 284 |
} |
| 285 |
return mInsecureFactory; |
| 286 |
} else { |
| 287 |
if (mSecureFactory == null) { |
| 288 |
mSecureFactory = makeSocketFactory(mKeyManagers, mTrustManagers); |
| 289 |
} |
| 290 |
return mSecureFactory; |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
public void setTrustManagers(TrustManager[] trustManager) { |
| 298 |
mTrustManagers = trustManager; |
| 299 |
|
| 300 |
|
| 301 |
mSecureFactory = null; |
| 302 |
|
| 303 |
|
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
public void setNpnProtocols(byte[][] npnProtocols) { |
| 324 |
this.mNpnProtocols = toLengthPrefixedList(npnProtocols); |
| 325 |
} |
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 347 |
public void setAlpnProtocols(byte[][] protocols) { |
| 348 |
this.mAlpnProtocols = toLengthPrefixedList(protocols); |
| 349 |
} |
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
@VisibleForTesting |
| 357 |
public static byte[] toLengthPrefixedList(byte[]... items) { |
| 358 |
if (items.length == 0) { |
| 359 |
throw new IllegalArgumentException("items.length == 0"); |
| 360 |
} |
| 361 |
int totalLength = 0; |
| 362 |
for (byte[] s : items) { |
| 363 |
if (s.length == 0 || s.length > 255) { |
| 364 |
throw new IllegalArgumentException("s.length == 0 || s.length > 255: " + s.length); |
| 365 |
} |
| 366 |
totalLength += 1 + s.length; |
| 367 |
} |
| 368 |
byte[] result = new byte[totalLength]; |
| 369 |
int pos = 0; |
| 370 |
for (byte[] s : items) { |
| 371 |
result[pos++] = (byte) s.length; |
| 372 |
for (byte b : s) { |
| 373 |
result[pos++] = b; |
| 374 |
} |
| 375 |
} |
| 376 |
return result; |
| 377 |
} |
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
public byte[] getNpnSelectedProtocol(Socket socket) { |
| 388 |
return castToOpenSSLSocket(socket).getNpnSelectedProtocol(); |
| 389 |
} |
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
| 402 |
public byte[] getAlpnSelectedProtocol(Socket socket) { |
| 403 |
return castToOpenSSLSocket(socket).getAlpnSelectedProtocol(); |
| 404 |
} |
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
public void setKeyManagers(KeyManager[] keyManagers) { |
| 410 |
mKeyManagers = keyManagers; |
| 411 |
|
| 412 |
|
| 413 |
mSecureFactory = null; |
| 414 |
mInsecureFactory = null; |
| 415 |
} |
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
| 428 |
public void setChannelIdPrivateKey(PrivateKey privateKey) { |
| 429 |
mChannelIdPrivateKey = privateKey; |
| 430 |
} |
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
public void setUseSessionTickets(Socket socket, boolean useSessionTickets) { |
| 441 |
castToOpenSSLSocket(socket).setUseSessionTickets(useSessionTickets); |
| 442 |
} |
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
public void setHostname(Socket socket, String hostName) { |
| 453 |
castToOpenSSLSocket(socket).setHostname(hostName); |
| 454 |
} |
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 468 |
public void setSoWriteTimeout(Socket socket, int writeTimeoutMilliseconds) |
| 469 |
throws SocketException { |
| 470 |
castToOpenSSLSocket(socket).setSoWriteTimeout(writeTimeoutMilliseconds); |
| 471 |
} |
| 472 |
|
| 473 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 474 |
private static OpenSSLSocketImpl castToOpenSSLSocket(Socket socket) { |
| 475 |
if (!(socket instanceof OpenSSLSocketImpl)) { |
| 476 |
throw new IllegalArgumentException("Socket not created by this factory: " |
| 477 |
+ socket); |
| 478 |
} |
| 479 |
|
| 480 |
return (OpenSSLSocketImpl) socket; |
| 481 |
} |
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
@Override |
| 493 |
public Socket createSocket(Socket k, String host, int port, boolean close) throws IOException { |
| 494 |
OpenSSLSocketImpl s = (OpenSSLSocketImpl) getDelegate().createSocket(k, host, port, close); |
| 495 |
s.setNpnProtocols(mNpnProtocols); |
| 496 |
s.setAlpnProtocols(mAlpnProtocols); |
| 497 |
s.setHandshakeTimeout(mHandshakeTimeoutMillis); |
| 498 |
s.setChannelIdPrivateKey(mChannelIdPrivateKey); |
| 499 |
if (mSecure) { |
| 500 |
verifyHostname(s, host); |
| 501 |
} |
| 502 |
return s; |
| 503 |
} |
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
@Override |
| 514 |
public Socket createSocket() throws IOException { |
| 515 |
OpenSSLSocketImpl s = (OpenSSLSocketImpl) getDelegate().createSocket(); |
| 516 |
s.setNpnProtocols(mNpnProtocols); |
| 517 |
s.setAlpnProtocols(mAlpnProtocols); |
| 518 |
s.setHandshakeTimeout(mHandshakeTimeoutMillis); |
| 519 |
s.setChannelIdPrivateKey(mChannelIdPrivateKey); |
| 520 |
return s; |
| 521 |
} |
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
@Override |
| 533 |
public Socket createSocket(InetAddress addr, int port, InetAddress localAddr, int localPort) |
| 534 |
throws IOException { |
| 535 |
OpenSSLSocketImpl s = (OpenSSLSocketImpl) getDelegate().createSocket( |
| 536 |
addr, port, localAddr, localPort); |
| 537 |
s.setNpnProtocols(mNpnProtocols); |
| 538 |
s.setAlpnProtocols(mAlpnProtocols); |
| 539 |
s.setHandshakeTimeout(mHandshakeTimeoutMillis); |
| 540 |
s.setChannelIdPrivateKey(mChannelIdPrivateKey); |
| 541 |
return s; |
| 542 |
} |
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
@Override |
| 554 |
public Socket createSocket(InetAddress addr, int port) throws IOException { |
| 555 |
OpenSSLSocketImpl s = (OpenSSLSocketImpl) getDelegate().createSocket(addr, port); |
| 556 |
s.setNpnProtocols(mNpnProtocols); |
| 557 |
s.setAlpnProtocols(mAlpnProtocols); |
| 558 |
s.setHandshakeTimeout(mHandshakeTimeoutMillis); |
| 559 |
s.setChannelIdPrivateKey(mChannelIdPrivateKey); |
| 560 |
return s; |
| 561 |
} |
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
@Override |
| 573 |
public Socket createSocket(String host, int port, InetAddress localAddr, int localPort) |
| 574 |
throws IOException { |
| 575 |
OpenSSLSocketImpl s = (OpenSSLSocketImpl) getDelegate().createSocket( |
| 576 |
host, port, localAddr, localPort); |
| 577 |
s.setNpnProtocols(mNpnProtocols); |
| 578 |
s.setAlpnProtocols(mAlpnProtocols); |
| 579 |
s.setHandshakeTimeout(mHandshakeTimeoutMillis); |
| 580 |
s.setChannelIdPrivateKey(mChannelIdPrivateKey); |
| 581 |
if (mSecure) { |
| 582 |
verifyHostname(s, host); |
| 583 |
} |
| 584 |
return s; |
| 585 |
} |
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
@Override |
| 597 |
public Socket createSocket(String host, int port) throws IOException { |
| 598 |
OpenSSLSocketImpl s = (OpenSSLSocketImpl) getDelegate().createSocket(host, port); |
| 599 |
s.setNpnProtocols(mNpnProtocols); |
| 600 |
s.setAlpnProtocols(mAlpnProtocols); |
| 601 |
s.setHandshakeTimeout(mHandshakeTimeoutMillis); |
| 602 |
s.setChannelIdPrivateKey(mChannelIdPrivateKey); |
| 603 |
if (mSecure) { |
| 604 |
verifyHostname(s, host); |
| 605 |
} |
| 606 |
return s; |
| 607 |
} |
| 608 |
|
| 609 |
@Override |
| 610 |
public String[] getDefaultCipherSuites() { |
| 611 |
return getDelegate().getDefaultCipherSuites(); |
| 612 |
} |
| 613 |
|
| 614 |
@Override |
| 615 |
public String[] getSupportedCipherSuites() { |
| 616 |
return getDelegate().getSupportedCipherSuites(); |
| 617 |
} |
| 618 |
} |