17329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom/* 27329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom * Licensed to the Apache Software Foundation (ASF) under one or more 37329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom * contributor license agreements. See the NOTICE file distributed with 47329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom * this work for additional information regarding copyright ownership. 57329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom * The ASF licenses this file to You under the Apache License, Version 2.0 67329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom * (the "License"); you may not use this file except in compliance with 77329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom * the License. You may obtain a copy of the License at 87329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom * 97329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom * http://www.apache.org/licenses/LICENSE-2.0 107329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom * 117329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom * Unless required by applicable law or agreed to in writing, software 127329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom * distributed under the License is distributed on an "AS IS" BASIS, 137329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 147329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom * See the License for the specific language governing permissions and 157329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom * limitations under the License. 167329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom */ 177329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom 1838375a4d0b3d34e2babbd2f6a013976c7c439696Kenny Rootpackage org.conscrypt; 197329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom 207329fa972d9c20777444e5e1b13169d700de6567Brian Carlstromimport java.io.IOException; 217329fa972d9c20777444e5e1b13169d700de6567Brian Carlstromimport java.net.InetAddress; 227329fa972d9c20777444e5e1b13169d700de6567Brian Carlstromimport java.net.Socket; 237329fa972d9c20777444e5e1b13169d700de6567Brian Carlstromimport java.net.UnknownHostException; 247329fa972d9c20777444e5e1b13169d700de6567Brian Carlstromimport java.security.KeyManagementException; 257329fa972d9c20777444e5e1b13169d700de6567Brian Carlstromimport javax.net.ssl.SSLSocketFactory; 26cc73183fa6f5f6f9935307aacfbfc5d93a867a23Kenny Rootimport org.conscrypt.util.EmptyArray; 277329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom 287329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom/** 297329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom * Implementation of SSLSocketFactory. 307329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom */ 317329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrompublic class SSLSocketFactoryImpl extends SSLSocketFactory { 327329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom 3341ea5dcbab2b53238434831d2365fa65d6e911ffBrian Carlstrom private final SSLParametersImpl sslParameters; 3441ea5dcbab2b53238434831d2365fa65d6e911ffBrian Carlstrom private final IOException instantiationException; 357329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom 367329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom /** 377329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom * Constructor. 387329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom */ 397329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom public SSLSocketFactoryImpl() { 4041ea5dcbab2b53238434831d2365fa65d6e911ffBrian Carlstrom SSLParametersImpl sslParametersLocal = null; 4141ea5dcbab2b53238434831d2365fa65d6e911ffBrian Carlstrom IOException instantiationExceptionLocal = null; 427329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom try { 4341ea5dcbab2b53238434831d2365fa65d6e911ffBrian Carlstrom sslParametersLocal = SSLParametersImpl.getDefault(); 447329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom } catch (KeyManagementException e) { 4541ea5dcbab2b53238434831d2365fa65d6e911ffBrian Carlstrom instantiationExceptionLocal = new IOException("Delayed instantiation exception:"); 4641ea5dcbab2b53238434831d2365fa65d6e911ffBrian Carlstrom instantiationExceptionLocal.initCause(e); 477329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom } 4841ea5dcbab2b53238434831d2365fa65d6e911ffBrian Carlstrom this.sslParameters = sslParametersLocal; 4941ea5dcbab2b53238434831d2365fa65d6e911ffBrian Carlstrom this.instantiationException = instantiationExceptionLocal; 507329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom } 517329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom 527329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom /** 537329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom * Constructor. 547329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom */ 556812a2e8bb43d9a875633a9ba255d9882c63e327Brian Carlstrom protected SSLSocketFactoryImpl(SSLParametersImpl sslParameters) { 567329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom this.sslParameters = sslParameters; 5741ea5dcbab2b53238434831d2365fa65d6e911ffBrian Carlstrom this.instantiationException = null; 587329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom } 597329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom 607329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom /** 617329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom * @see javax.net.ssl.SSLSocketFactory#getDefaultCipherSuites() 627329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom */ 637329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom @Override 647329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom public String[] getDefaultCipherSuites() { 657329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom if (instantiationException != null) { 66693eacca9fa67ad79d1b35dbaad61c5ac1ac457cElliott Hughes return EmptyArray.STRING; 677329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom } 687329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom return sslParameters.getEnabledCipherSuites(); 697329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom } 707329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom 717329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom /** 727329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom * @see javax.net.ssl.SSLSocketFactory#getSupportedCipherSuites() 737329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom */ 747329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom @Override 757329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom public String[] getSupportedCipherSuites() { 767329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom if (instantiationException != null) { 77693eacca9fa67ad79d1b35dbaad61c5ac1ac457cElliott Hughes return EmptyArray.STRING; 787329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom } 797329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom return CipherSuite.getSupportedCipherSuiteNames(); 807329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom } 817329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom 827329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom /** 837329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom * @see javax.net.ssl.SSLSocketFactory#createSocket(Socket,String,int,boolean) 847329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom */ 857329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom @Override 867329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom public Socket createSocket(Socket s, String host, int port, 877329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom boolean autoClose) throws IOException { 887329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom if (instantiationException != null) { 897329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom throw instantiationException; 907329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom } 916d2a17ab04ab0967e3bff7fe6280066ef66d1d76Geremy Condra return new SSLSocketWrapper(s, host, port, autoClose, (SSLParametersImpl) sslParameters 927329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom .clone()); 937329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom } 947329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom 957329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom // -------------- Methods inherided from SocketFactory -------------- 967329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom 977329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom /** 987329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom * @see javax.net.SocketFactory#createSocket() 997329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom */ 1007329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom @Override 1017329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom public Socket createSocket() throws IOException { 1027329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom if (instantiationException != null) { 1037329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom throw instantiationException; 1047329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom } 1056812a2e8bb43d9a875633a9ba255d9882c63e327Brian Carlstrom return new SSLSocketImpl((SSLParametersImpl) sslParameters.clone()); 1067329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom } 1077329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom 1087329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom /** 1097329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom * @see javax.net.SocketFactory#createSocket(String,int) 1107329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom */ 1117329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom @Override 1127329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom public Socket createSocket(String host, int port) 1137329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom throws IOException, UnknownHostException { 1147329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom if (instantiationException != null) { 1157329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom throw instantiationException; 1167329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom } 1177329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom return new SSLSocketImpl(host, port, 1186812a2e8bb43d9a875633a9ba255d9882c63e327Brian Carlstrom (SSLParametersImpl) sslParameters.clone()); 1197329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom } 1207329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom 1217329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom /** 1227329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom * @see javax.net.SocketFactory#createSocket(String,int,InetAddress,int) 1237329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom */ 1247329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom @Override 1257329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom public Socket createSocket(String host, int port, 1267329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom InetAddress localHost, int localPort) throws IOException, 1277329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom UnknownHostException { 1287329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom if (instantiationException != null) { 1297329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom throw instantiationException; 1307329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom } 1317329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom return new SSLSocketImpl(host, port, localHost, localPort, 1326812a2e8bb43d9a875633a9ba255d9882c63e327Brian Carlstrom (SSLParametersImpl) sslParameters.clone()); 1337329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom } 1347329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom 1357329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom /** 1367329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom * @see javax.net.SocketFactory#createSocket(InetAddress,int) 1377329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom */ 1387329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom @Override 1397329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom public Socket createSocket(InetAddress host, int port) 1407329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom throws IOException { 1417329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom if (instantiationException != null) { 1427329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom throw instantiationException; 1437329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom } 1447329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom return new SSLSocketImpl(host, port, 1456812a2e8bb43d9a875633a9ba255d9882c63e327Brian Carlstrom (SSLParametersImpl) sslParameters.clone()); 1467329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom } 1477329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom 1487329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom /** 1497329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom * @see javax.net.SocketFactory#createSocket(InetAddress,int,InetAddress,int) 1507329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom */ 1517329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom @Override 1527329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom public Socket createSocket(InetAddress address, int port, 1537329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom InetAddress localAddress, int localPort) throws IOException { 1547329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom if (instantiationException != null) { 1557329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom throw instantiationException; 1567329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom } 1577329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom return new SSLSocketImpl(address, port, localAddress, localPort, 1586812a2e8bb43d9a875633a9ba255d9882c63e327Brian Carlstrom (SSLParametersImpl) sslParameters.clone()); 1597329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom } 1607329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom 1617329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom // ------------------------------------------------------------------ 1627329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom} 163