|
| 1 | +package io.split.android.client.network; |
| 2 | + |
| 3 | +import androidx.annotation.NonNull; |
| 4 | +import androidx.annotation.Nullable; |
| 5 | + |
| 6 | +import java.io.IOException; |
| 7 | +import java.net.HttpRetryException; |
| 8 | +import java.net.Socket; |
| 9 | +import java.net.URL; |
| 10 | +import java.security.cert.Certificate; |
| 11 | +import java.util.Map; |
| 12 | + |
| 13 | +import javax.net.ssl.SSLContext; |
| 14 | +import javax.net.ssl.SSLSocket; |
| 15 | +import javax.net.ssl.SSLSocketFactory; |
| 16 | + |
| 17 | +import io.split.android.client.utils.logger.Logger; |
| 18 | + |
| 19 | +/** |
| 20 | + * Handles PROXY_CACERT SSL proxy connections. |
| 21 | + * |
| 22 | + * This handler establishes SSL tunnels through SSL proxies using custom CA certificates |
| 23 | + * for proxy authentication, then executes HTTP requests through the SSL tunnel. |
| 24 | + * |
| 25 | + * CONNECT Specification Compliance: |
| 26 | + * - Establishes SSL connection to proxy for authentication |
| 27 | + * - Sends CONNECT request through SSL connection |
| 28 | + * - Maintains SSL socket connection after successful CONNECT |
| 29 | + * - Executes HTTP requests through SSL tunnel socket |
| 30 | + */ |
| 31 | +class ProxyCacertConnectionHandler implements SslProxyConnectionHandler { |
| 32 | + |
| 33 | + public static final String HTTPS = "https"; |
| 34 | + public static final String HTTP = "http"; |
| 35 | + public static final int PORT_HTTPS = 443; |
| 36 | + public static final int HTTP_PORT = 80; |
| 37 | + private final HttpOverTunnelExecutor mTunnelExecutor; |
| 38 | + |
| 39 | + public ProxyCacertConnectionHandler() { |
| 40 | + mTunnelExecutor = new HttpOverTunnelExecutor(); |
| 41 | + } |
| 42 | + |
| 43 | + // For testing - allow injection of dependencies |
| 44 | + ProxyCacertConnectionHandler(HttpOverTunnelExecutor tunnelExecutor) { |
| 45 | + mTunnelExecutor = tunnelExecutor; |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public boolean canHandle(@NonNull HttpProxy httpProxy) { |
| 50 | + return httpProxy.getAuthType() == HttpProxy.ProxyAuthType.PROXY_CACERT || httpProxy.getAuthType() == HttpProxy.ProxyAuthType.MTLS; |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + @NonNull |
| 55 | + public HttpResponse executeRequest(@NonNull HttpProxy httpProxy, |
| 56 | + @NonNull URL targetUrl, |
| 57 | + @NonNull HttpMethod method, |
| 58 | + @NonNull Map<String, String> headers, |
| 59 | + @Nullable String body, |
| 60 | + @NonNull SSLSocketFactory sslSocketFactory, |
| 61 | + @Nullable ProxyCredentialsProvider proxyCredentialsProvider) throws IOException { |
| 62 | + |
| 63 | + Logger.v("PROXY_CACERT: Executing request to: " + targetUrl); |
| 64 | + |
| 65 | + try { |
| 66 | + // PROXY_CACERT requires SSL authentication with proxy using CA certificate |
| 67 | + // Use the provided sslSocketFactory which contains the proxy CA certificate |
| 68 | + |
| 69 | + // Establish SSL tunnel through proxy with CA certificate authentication |
| 70 | + SslProxyTunnelEstablisher tunnelEstablisher = new SslProxyTunnelEstablisher(); |
| 71 | + Socket tunnelSocket = tunnelEstablisher.establishTunnel( |
| 72 | + httpProxy.getHost(), |
| 73 | + httpProxy.getPort(), |
| 74 | + targetUrl.getHost(), |
| 75 | + getTargetPort(targetUrl), |
| 76 | + sslSocketFactory, // Use the SSL socket factory with proxy CA certificate, |
| 77 | + proxyCredentialsProvider |
| 78 | + ); |
| 79 | + |
| 80 | + Logger.v("SSL tunnel established successfully"); |
| 81 | + |
| 82 | + Socket finalSocket = tunnelSocket; |
| 83 | + Certificate[] serverCertificates = null; |
| 84 | + |
| 85 | + // If the origin is HTTPS, wrap the tunnel socket with a new SSLSocket (system CA) |
| 86 | + if ("https".equalsIgnoreCase(targetUrl.getProtocol())) { |
| 87 | + Logger.v("Wrapping tunnel socket with new SSLSocket for origin server handshake"); |
| 88 | + try { |
| 89 | + // Get system default SSL context |
| 90 | + SSLContext systemSslContext = SSLContext.getInstance("TLS"); |
| 91 | + systemSslContext.init(null, null, null); // null = system default trust managers |
| 92 | + SSLSocketFactory systemSslSocketFactory = systemSslContext.getSocketFactory(); |
| 93 | + |
| 94 | + // Create SSLSocket layered over tunnel |
| 95 | + finalSocket = systemSslSocketFactory.createSocket( |
| 96 | + tunnelSocket, |
| 97 | + targetUrl.getHost(), |
| 98 | + getTargetPort(targetUrl), |
| 99 | + true // autoClose |
| 100 | + ); |
| 101 | + if (finalSocket instanceof SSLSocket) { |
| 102 | + SSLSocket originSslSocket = (SSLSocket) finalSocket; |
| 103 | + originSslSocket.setUseClientMode(true); |
| 104 | + originSslSocket.startHandshake(); |
| 105 | + |
| 106 | + // Capture server certificates after successful handshake |
| 107 | + try { |
| 108 | + serverCertificates = originSslSocket.getSession().getPeerCertificates(); |
| 109 | + Logger.v("Captured " + (serverCertificates != null ? serverCertificates.length : 0) + " certificates from origin server"); |
| 110 | + } catch (Exception certEx) { |
| 111 | + Logger.w("Could not capture origin server certificates: " + certEx.getMessage()); |
| 112 | + } |
| 113 | + } else { |
| 114 | + throw new IOException("Failed to create SSLSocket to origin"); |
| 115 | + } |
| 116 | + Logger.v("SSL handshake with origin server completed"); |
| 117 | + } catch (Exception sslEx) { |
| 118 | + Logger.e("Failed to establish SSL connection to origin: " + sslEx.getMessage()); |
| 119 | + throw new IOException("Failed to establish SSL connection to origin server", sslEx); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + // Execute request through the (possibly wrapped) socket, passing the certificates |
| 124 | + HttpResponse response = mTunnelExecutor.executeRequest( |
| 125 | + finalSocket, |
| 126 | + targetUrl, |
| 127 | + method, |
| 128 | + headers, |
| 129 | + body, |
| 130 | + serverCertificates |
| 131 | + ); |
| 132 | + |
| 133 | + Logger.v("PROXY_CACERT request completed successfully, status: " + response.getHttpStatus()); |
| 134 | + return response; |
| 135 | + |
| 136 | + } catch (Exception e) { |
| 137 | + if (e instanceof HttpRetryException) { |
| 138 | + throw (HttpRetryException) e; |
| 139 | + } |
| 140 | + throw new IOException("Failed to execute request through custom tunnel", e); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + private static int getTargetPort(@NonNull URL targetUrl) { |
| 145 | + int port = targetUrl.getPort(); |
| 146 | + if (port == -1) { |
| 147 | + if (HTTPS.equalsIgnoreCase(targetUrl.getProtocol())) { |
| 148 | + return PORT_HTTPS; |
| 149 | + } else if (HTTP.equalsIgnoreCase(targetUrl.getProtocol())) { |
| 150 | + return HTTP_PORT; |
| 151 | + } |
| 152 | + } |
| 153 | + return port; |
| 154 | + } |
| 155 | +} |
0 commit comments