1 /* 2 * Copyright 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package org.conscrypt; 18 19 import java.io.FileDescriptor; 20 import java.io.IOException; 21 import java.io.InputStream; 22 import java.io.OutputStream; 23 import java.net.InetAddress; 24 import java.net.Socket; 25 import java.net.SocketAddress; 26 import java.net.SocketException; 27 import java.nio.channels.SocketChannel; 28 import java.security.PrivateKey; 29 import java.security.cert.CertificateEncodingException; 30 import java.security.cert.CertificateException; 31 import javax.net.ssl.HandshakeCompletedEvent; 32 import javax.net.ssl.HandshakeCompletedListener; 33 import javax.net.ssl.SSLException; 34 import javax.net.ssl.SSLParameters; 35 import javax.net.ssl.SSLSession; 36 37 /** 38 * This class delegates all calls to an {@code org.conscrypt.OpenSSLSocketImpl}. 39 * This is to work around code that checks that the socket is an 40 * {@code org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl} before 41 * calling methods, such as setting SNI. This is only for Pre-Kitkat devices. 42 * 43 * It delegates all public methods in Socket, SSLSocket, and OpenSSLSocket from 44 * JB. 45 */ 46 public class PreKitKatPlatformOpenSSLSocketImplAdapter 47 extends org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl { 48 49 50 private final org.conscrypt.OpenSSLSocketImpl delegate; 51 PreKitKatPlatformOpenSSLSocketImplAdapter(org.conscrypt.OpenSSLSocketImpl delegate)52 public PreKitKatPlatformOpenSSLSocketImplAdapter(org.conscrypt.OpenSSLSocketImpl delegate) 53 throws IOException { 54 super(null); 55 this.delegate = delegate; 56 } 57 58 // Socket methods. 59 60 @Override close()61 public void close() throws IOException { 62 delegate.close(); 63 } 64 65 @Override getInputStream()66 public InputStream getInputStream() throws IOException { 67 return delegate.getInputStream(); 68 } 69 70 @Override getLocalPort()71 public int getLocalPort() { 72 return delegate.getLocalPort(); 73 } 74 75 @Override getOutputStream()76 public OutputStream getOutputStream() throws IOException { 77 return delegate.getOutputStream(); 78 } 79 80 @Override getPort()81 public int getPort() { 82 return delegate.getPort(); 83 } 84 85 @Override connect(SocketAddress sockaddr, int timeout)86 public void connect(SocketAddress sockaddr, int timeout) throws IOException { 87 delegate.connect(sockaddr, timeout); 88 } 89 90 @Override connect(SocketAddress sockaddr)91 public void connect(SocketAddress sockaddr) throws IOException { 92 delegate.connect(sockaddr); 93 } 94 95 @Override bind(SocketAddress sockaddr)96 public void bind(SocketAddress sockaddr) throws IOException { 97 delegate.bind(sockaddr); 98 } 99 100 @Override getRemoteSocketAddress()101 public SocketAddress getRemoteSocketAddress() { 102 return delegate.getRemoteSocketAddress(); 103 } 104 105 @Override getLocalSocketAddress()106 public SocketAddress getLocalSocketAddress() { 107 return delegate.getLocalSocketAddress(); 108 } 109 110 @Override getLocalAddress()111 public InetAddress getLocalAddress() { 112 return delegate.getLocalAddress(); 113 } 114 115 @Override getInetAddress()116 public InetAddress getInetAddress() { 117 return delegate.getInetAddress(); 118 } 119 120 @Override toString()121 public String toString() { 122 return delegate.toString(); 123 } 124 125 @Override setSoLinger(boolean on, int linger)126 public void setSoLinger(boolean on, int linger) throws SocketException { 127 delegate.setSoLinger(on, linger); 128 } 129 130 @Override setTcpNoDelay(boolean on)131 public void setTcpNoDelay(boolean on) throws SocketException { 132 delegate.setTcpNoDelay(on); 133 } 134 135 @Override setReuseAddress(boolean on)136 public void setReuseAddress(boolean on) throws SocketException { 137 delegate.setReuseAddress(on); 138 } 139 140 @Override setKeepAlive(boolean on)141 public void setKeepAlive(boolean on) throws SocketException { 142 delegate.setKeepAlive(on); 143 } 144 145 @Override setTrafficClass(int tos)146 public void setTrafficClass(int tos) throws SocketException { 147 delegate.setTrafficClass(tos); 148 } 149 150 @Override setSoTimeout(int to)151 public void setSoTimeout(int to) throws SocketException { 152 delegate.setSoTimeout(to); 153 } 154 155 @Override setSendBufferSize(int size)156 public void setSendBufferSize(int size) throws SocketException { 157 delegate.setSendBufferSize(size); 158 } 159 160 @Override setReceiveBufferSize(int size)161 public void setReceiveBufferSize(int size) throws SocketException { 162 delegate.setReceiveBufferSize(size); 163 } 164 165 @Override getTcpNoDelay()166 public boolean getTcpNoDelay() throws SocketException { 167 return delegate.getTcpNoDelay(); 168 } 169 170 @Override getReuseAddress()171 public boolean getReuseAddress() throws SocketException { 172 return delegate.getReuseAddress(); 173 } 174 175 @Override getKeepAlive()176 public boolean getKeepAlive() throws SocketException { 177 return delegate.getKeepAlive(); 178 } 179 180 @Override getSoTimeout()181 public int getSoTimeout() throws SocketException { 182 return delegate.getSoTimeout(); 183 } 184 185 @Override getSoLinger()186 public int getSoLinger() throws SocketException { 187 return delegate.getSoLinger(); 188 } 189 190 @Override getSendBufferSize()191 public int getSendBufferSize() throws SocketException { 192 return delegate.getSendBufferSize(); 193 } 194 195 @Override getReceiveBufferSize()196 public int getReceiveBufferSize() throws SocketException { 197 return delegate.getReceiveBufferSize(); 198 } 199 200 @Override isConnected()201 public boolean isConnected() { 202 return delegate.isConnected(); 203 } 204 205 @Override isClosed()206 public boolean isClosed() { 207 return delegate.isClosed(); 208 } 209 210 @Override isBound()211 public boolean isBound() { 212 return delegate.isBound(); 213 } 214 215 @Override isOutputShutdown()216 public boolean isOutputShutdown() { 217 return delegate.isOutputShutdown(); 218 } 219 220 @Override isInputShutdown()221 public boolean isInputShutdown() { 222 return delegate.isInputShutdown(); 223 } 224 225 @Override shutdownInput()226 public void shutdownInput() throws IOException { 227 delegate.shutdownInput(); 228 } 229 230 @Override shutdownOutput()231 public void shutdownOutput() throws IOException { 232 delegate.shutdownOutput(); 233 } 234 235 @Override setOOBInline(boolean oobinline)236 public void setOOBInline(boolean oobinline) throws SocketException { 237 delegate.setOOBInline(oobinline); 238 } 239 240 @Override getOOBInline()241 public boolean getOOBInline() throws SocketException { 242 return delegate.getOOBInline(); 243 } 244 245 @Override getTrafficClass()246 public int getTrafficClass() throws SocketException { 247 return delegate.getTrafficClass(); 248 } 249 250 @Override sendUrgentData(int value)251 public void sendUrgentData(int value) throws IOException { 252 delegate.sendUrgentData(value); 253 } 254 255 @Override getChannel()256 public SocketChannel getChannel() { 257 return delegate.getChannel(); 258 } 259 260 @Override getFileDescriptor$()261 public FileDescriptor getFileDescriptor$() { 262 return delegate.getFileDescriptor$(); 263 } 264 265 @Override setPerformancePreferences(int connectionTime, int latency, int bandwidth)266 public void setPerformancePreferences(int connectionTime, int latency, int bandwidth) { 267 delegate.setPerformancePreferences(connectionTime, latency, bandwidth); 268 } 269 270 // SSLSocket methods. 271 272 @Override getSupportedCipherSuites()273 public String[] getSupportedCipherSuites() { 274 return delegate.getSupportedCipherSuites(); 275 } 276 277 @Override getEnabledCipherSuites()278 public String[] getEnabledCipherSuites() { 279 return delegate.getEnabledCipherSuites(); 280 } 281 282 @Override setEnabledCipherSuites(String[] suites)283 public void setEnabledCipherSuites(String[] suites) { 284 delegate.setEnabledCipherSuites(suites); 285 } 286 287 @Override getSupportedProtocols()288 public String[] getSupportedProtocols() { 289 return delegate.getSupportedProtocols(); 290 } 291 @Override getEnabledProtocols()292 public String[] getEnabledProtocols() { 293 return delegate.getEnabledProtocols(); 294 } 295 296 @Override setEnabledProtocols(String[] protocols)297 public void setEnabledProtocols(String[] protocols) { 298 delegate.setEnabledProtocols(protocols); 299 } 300 301 @Override getSession()302 public SSLSession getSession() { 303 return delegate.getSession(); 304 } 305 306 @Override addHandshakeCompletedListener(HandshakeCompletedListener listener)307 public void addHandshakeCompletedListener(HandshakeCompletedListener listener) { 308 delegate.addHandshakeCompletedListener(listener); 309 } 310 311 @Override removeHandshakeCompletedListener(HandshakeCompletedListener listener)312 public void removeHandshakeCompletedListener(HandshakeCompletedListener listener) { 313 delegate.removeHandshakeCompletedListener(listener); 314 } 315 316 @Override startHandshake()317 public void startHandshake() throws IOException { 318 delegate.startHandshake(); 319 } 320 321 @Override setUseClientMode(boolean mode)322 public void setUseClientMode(boolean mode) { 323 delegate.setUseClientMode(mode); 324 } 325 326 @Override getUseClientMode()327 public boolean getUseClientMode() { 328 return delegate.getUseClientMode(); 329 } 330 331 @Override setNeedClientAuth(boolean need)332 public void setNeedClientAuth(boolean need) { 333 delegate.setNeedClientAuth(need); 334 } 335 336 @Override setWantClientAuth(boolean want)337 public void setWantClientAuth(boolean want) { 338 delegate.setWantClientAuth(want); 339 } 340 341 @Override getNeedClientAuth()342 public boolean getNeedClientAuth() { 343 return delegate.getNeedClientAuth(); 344 } 345 346 @Override getWantClientAuth()347 public boolean getWantClientAuth() { 348 return delegate.getWantClientAuth(); 349 } 350 351 @Override setEnableSessionCreation(boolean flag)352 public void setEnableSessionCreation(boolean flag) { 353 delegate.setEnableSessionCreation(flag); 354 } 355 356 @Override getEnableSessionCreation()357 public boolean getEnableSessionCreation() { 358 return delegate.getEnableSessionCreation(); 359 } 360 361 @Override getSSLParameters()362 public SSLParameters getSSLParameters() { 363 return delegate.getSSLParameters(); 364 } 365 366 @Override setSSLParameters(SSLParameters p)367 public void setSSLParameters(SSLParameters p) { 368 delegate.setSSLParameters(p); 369 } 370 371 // OpenSSLSocket methods. 372 @Override clientCertificateRequested(byte[] keyTypeBytes, byte[][] asn1DerEncodedPrincipals)373 public void clientCertificateRequested(byte[] keyTypeBytes, byte[][] asn1DerEncodedPrincipals) 374 throws CertificateEncodingException, SSLException { 375 throw new RuntimeException("Shouldn't be here!"); 376 } 377 378 @Override handshakeCompleted()379 public void handshakeCompleted() { 380 throw new RuntimeException("Shouldn't be here!"); 381 } 382 383 @Override verifyCertificateChain(byte[][] bytes, String authMethod)384 public void verifyCertificateChain(byte[][] bytes, String authMethod) 385 throws CertificateException { 386 throw new RuntimeException("Shouldn't be here!"); 387 } 388 389 @Override setUseSessionTickets(boolean useSessionTickets)390 public void setUseSessionTickets(boolean useSessionTickets) { 391 delegate.setUseSessionTickets(useSessionTickets); 392 } 393 394 @Override setHostname(String hostname)395 public void setHostname(String hostname) { 396 delegate.setHostname(hostname); 397 } 398 399 @Override setChannelIdEnabled(boolean enabled)400 public void setChannelIdEnabled(boolean enabled) { 401 delegate.setChannelIdEnabled(enabled); 402 } 403 404 @Override getChannelId()405 public byte[] getChannelId() throws SSLException { 406 return delegate.getChannelId(); 407 } 408 409 @Override setChannelIdPrivateKey(PrivateKey privateKey)410 public void setChannelIdPrivateKey(PrivateKey privateKey) { 411 delegate.setChannelIdPrivateKey(privateKey); 412 } 413 414 @Override setSoWriteTimeout(int writeTimeoutMilliseconds)415 public void setSoWriteTimeout(int writeTimeoutMilliseconds) throws SocketException { 416 delegate.setSoWriteTimeout(writeTimeoutMilliseconds); 417 } 418 419 @Override getSoWriteTimeout()420 public int getSoWriteTimeout() throws SocketException { 421 return delegate.getSoWriteTimeout(); 422 } 423 424 @Override setHandshakeTimeout(int handshakeTimeoutMilliseconds)425 public void setHandshakeTimeout(int handshakeTimeoutMilliseconds) throws SocketException { 426 delegate.setHandshakeTimeout(handshakeTimeoutMilliseconds); 427 } 428 429 @Override getNpnSelectedProtocol()430 public byte[] getNpnSelectedProtocol() { 431 return delegate.getNpnSelectedProtocol(); 432 } 433 434 @Override setNpnProtocols(byte[] npnProtocols)435 public void setNpnProtocols(byte[] npnProtocols) { 436 delegate.setNpnProtocols(npnProtocols); 437 } 438 439 // These aren't in the Platform's OpenSSLSocketImpl but we have them to support duck typing. 440 getAlpnSelectedProtocol()441 public byte[] getAlpnSelectedProtocol() { 442 return delegate.getAlpnSelectedProtocol(); 443 } 444 setAlpnProtocols(byte[] alpnProtocols)445 public void setAlpnProtocols(byte[] alpnProtocols) { 446 delegate.setAlpnProtocols(alpnProtocols); 447 } 448 } 449