1 /* 2 * Copyright (C) 2017 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 static org.conscrypt.Preconditions.checkNotNull; 20 21 import java.nio.ByteBuffer; 22 import java.security.PrivateKey; 23 import java.util.List; 24 import java.util.function.BiFunction; 25 import javax.net.ssl.SSLEngine; 26 import javax.net.ssl.SSLEngineResult; 27 import javax.net.ssl.SSLEngineResult.HandshakeStatus; 28 import javax.net.ssl.SSLException; 29 import javax.net.ssl.SSLParameters; 30 import javax.net.ssl.SSLSession; 31 import javax.net.ssl.SSLSocket; 32 33 /** 34 * A wrapper around {@link ConscryptEngine} that adapts to the new Java 9 (and potentially later 35 * patches of 8) {@code setHandshakeApplicationProtocolSelector} API (which requires Java 8 for 36 * compilation, due to the use of {@link BiFunction}). 37 */ 38 final class Java8EngineWrapper extends AbstractConscryptEngine { 39 private final ConscryptEngine delegate; 40 private BiFunction<SSLEngine, List<String>, String> selector; 41 42 Java8EngineWrapper(ConscryptEngine delegate) { 43 this.delegate = checkNotNull(delegate, "delegate"); 44 } 45 46 static SSLEngine getDelegate(SSLEngine engine) { 47 if (engine instanceof Java8EngineWrapper) { 48 return ((Java8EngineWrapper) engine).delegate; 49 } 50 return engine; 51 } 52 53 @Override 54 public SSLEngineResult wrap(ByteBuffer[] byteBuffers, ByteBuffer byteBuffer) 55 throws SSLException { 56 return delegate.wrap(byteBuffers, byteBuffer); 57 } 58 59 @Override 60 public SSLParameters getSSLParameters() { 61 return delegate.getSSLParameters(); 62 } 63 64 @Override 65 public void setSSLParameters(SSLParameters sslParameters) { 66 delegate.setSSLParameters(sslParameters); 67 } 68 69 @Override 70 void setBufferAllocator(BufferAllocator bufferAllocator) { 71 delegate.setBufferAllocator(bufferAllocator); 72 } 73 74 @Override 75 int maxSealOverhead() { 76 return delegate.maxSealOverhead(); 77 } 78 79 @Override 80 void setChannelIdEnabled(boolean enabled) { 81 delegate.setChannelIdEnabled(enabled); 82 } 83 84 @Override 85 byte[] getChannelId() throws SSLException { 86 return delegate.getChannelId(); 87 } 88 89 @Override 90 void setChannelIdPrivateKey(PrivateKey privateKey) { 91 delegate.setChannelIdPrivateKey(privateKey); 92 } 93 94 @Override 95 void setHandshakeListener(HandshakeListener handshakeListener) { 96 delegate.setHandshakeListener(handshakeListener); 97 } 98 99 @Override 100 void setHostname(String hostname) { 101 delegate.setHostname(hostname); 102 } 103 104 @Override 105 String getHostname() { 106 return delegate.getHostname(); 107 } 108 109 @Override 110 public String getPeerHost() { 111 return delegate.getPeerHost(); 112 } 113 114 @Override 115 public int getPeerPort() { 116 return delegate.getPeerPort(); 117 } 118 119 @Override 120 public void beginHandshake() throws SSLException { 121 delegate.beginHandshake(); 122 } 123 124 @Override 125 public void closeInbound() throws SSLException { 126 delegate.closeInbound(); 127 } 128 129 @Override 130 public void closeOutbound() { 131 delegate.closeOutbound(); 132 } 133 134 @Override 135 public Runnable getDelegatedTask() { 136 return delegate.getDelegatedTask(); 137 } 138 139 @Override 140 public String[] getEnabledCipherSuites() { 141 return delegate.getEnabledCipherSuites(); 142 } 143 144 @Override 145 public String[] getEnabledProtocols() { 146 return delegate.getEnabledProtocols(); 147 } 148 149 @Override 150 public boolean getEnableSessionCreation() { 151 return delegate.getEnableSessionCreation(); 152 } 153 154 @Override 155 public HandshakeStatus getHandshakeStatus() { 156 return delegate.getHandshakeStatus(); 157 } 158 159 @Override 160 public boolean getNeedClientAuth() { 161 return delegate.getNeedClientAuth(); 162 } 163 164 @Override 165 SSLSession handshakeSession() { 166 return delegate.handshakeSession(); 167 } 168 169 @Override 170 public SSLSession getSession() { 171 return delegate.getSession(); 172 } 173 174 @Override 175 public String[] getSupportedCipherSuites() { 176 return delegate.getSupportedCipherSuites(); 177 } 178 179 @Override 180 public String[] getSupportedProtocols() { 181 return delegate.getSupportedProtocols(); 182 } 183 184 @Override 185 public boolean getUseClientMode() { 186 return delegate.getUseClientMode(); 187 } 188 189 @Override 190 public boolean getWantClientAuth() { 191 return delegate.getWantClientAuth(); 192 } 193 194 @Override 195 public boolean isInboundDone() { 196 return delegate.isInboundDone(); 197 } 198 199 @Override 200 public boolean isOutboundDone() { 201 return delegate.isOutboundDone(); 202 } 203 204 @Override 205 public void setEnabledCipherSuites(String[] suites) { 206 delegate.setEnabledCipherSuites(suites); 207 } 208 209 @Override 210 public void setEnabledProtocols(String[] protocols) { 211 delegate.setEnabledProtocols(protocols); 212 } 213 214 @Override 215 public void setEnableSessionCreation(boolean flag) { 216 delegate.setEnableSessionCreation(flag); 217 } 218 219 @Override 220 public void setNeedClientAuth(boolean need) { 221 delegate.setNeedClientAuth(need); 222 } 223 224 @Override 225 public void setUseClientMode(boolean mode) { 226 delegate.setUseClientMode(mode); 227 } 228 229 @Override 230 public void setWantClientAuth(boolean want) { 231 delegate.setWantClientAuth(want); 232 } 233 234 @Override 235 public SSLEngineResult unwrap(ByteBuffer src, ByteBuffer dst) throws SSLException { 236 return delegate.unwrap(src, dst); 237 } 238 239 @Override 240 public SSLEngineResult unwrap(ByteBuffer src, ByteBuffer[] dsts) throws SSLException { 241 return delegate.unwrap(src, dsts); 242 } 243 244 @Override 245 public SSLEngineResult unwrap(ByteBuffer src, ByteBuffer[] dsts, int offset, int length) 246 throws SSLException { 247 return delegate.unwrap(src, dsts, offset, length); 248 } 249 250 @Override 251 SSLEngineResult unwrap(ByteBuffer[] srcs, ByteBuffer[] dsts) throws SSLException { 252 return delegate.unwrap(srcs, dsts); 253 } 254 255 @Override 256 SSLEngineResult unwrap(ByteBuffer[] srcs, int srcsOffset, int srcsLength, ByteBuffer[] dsts, 257 int dstsOffset, int dstsLength) throws SSLException { 258 return delegate.unwrap(srcs, srcsOffset, srcsLength, dsts, dstsOffset, dstsLength); 259 } 260 261 @Override 262 public SSLEngineResult wrap(ByteBuffer src, ByteBuffer dst) throws SSLException { 263 return delegate.wrap(src, dst); 264 } 265 266 @Override 267 public SSLEngineResult wrap(ByteBuffer[] srcs, int srcsOffset, int srcsLength, ByteBuffer dst) 268 throws SSLException { 269 return delegate.wrap(srcs, srcsOffset, srcsLength, dst); 270 } 271 272 @Override 273 void setUseSessionTickets(boolean useSessionTickets) { 274 delegate.setUseSessionTickets(useSessionTickets); 275 } 276 277 @Override 278 void setApplicationProtocols(String[] protocols) { 279 delegate.setApplicationProtocols(protocols); 280 } 281 282 @Override 283 String[] getApplicationProtocols() { 284 return delegate.getApplicationProtocols(); 285 } 286 287 @Override 288 public String getApplicationProtocol() { 289 return delegate.getApplicationProtocol(); 290 } 291 292 @Override 293 void setApplicationProtocolSelector(ApplicationProtocolSelector selector) { 294 delegate.setApplicationProtocolSelector( 295 selector == null ? null : new ApplicationProtocolSelectorAdapter(this, selector)); 296 } 297 298 @Override 299 byte[] getTlsUnique() { 300 return delegate.getTlsUnique(); 301 } 302 303 @Override 304 public String getHandshakeApplicationProtocol() { 305 return delegate.getHandshakeApplicationProtocol(); 306 } 307 308 /* @Override */ 309 @SuppressWarnings("MissingOverride") // For compilation with Java < 9. 310 public void setHandshakeApplicationProtocolSelector( 311 final BiFunction<SSLEngine, List<String>, String> selector) { 312 this.selector = selector; 313 setApplicationProtocolSelector(toApplicationProtocolSelector(selector)); 314 } 315 316 /* @Override */ 317 @SuppressWarnings("MissingOverride") // For compilation with Java < 9. 318 public BiFunction<SSLEngine, List<String>, String> getHandshakeApplicationProtocolSelector() { 319 return selector; 320 } 321 322 private static ApplicationProtocolSelector toApplicationProtocolSelector( 323 final BiFunction<SSLEngine, List<String>, String> selector) { 324 return selector == null ? null : new ApplicationProtocolSelector() { 325 @Override 326 public String selectApplicationProtocol(SSLEngine engine, List<String> protocols) { 327 return selector.apply(engine, protocols); 328 } 329 330 @Override 331 public String selectApplicationProtocol(SSLSocket socket, List<String> protocols) { 332 throw new UnsupportedOperationException(); 333 } 334 }; 335 } 336 } 337