1 /* 2 * Copyright (C) 2007 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.IOException; 20 import java.net.InetAddress; 21 import java.net.Socket; 22 import java.net.UnknownHostException; 23 import java.security.KeyManagementException; 24 25 public class OpenSSLSocketFactoryImpl extends javax.net.ssl.SSLSocketFactory { 26 27 private final SSLParametersImpl sslParameters; 28 private final IOException instantiationException; 29 30 public OpenSSLSocketFactoryImpl() { 31 SSLParametersImpl sslParametersLocal = null; 32 IOException instantiationExceptionLocal = null; 33 try { 34 sslParametersLocal = SSLParametersImpl.getDefault(); 35 } catch (KeyManagementException e) { 36 instantiationExceptionLocal = new IOException("Delayed instantiation exception:"); 37 instantiationExceptionLocal.initCause(e); 38 } 39 this.sslParameters = sslParametersLocal; 40 this.instantiationException = instantiationExceptionLocal; 41 } 42 43 public OpenSSLSocketFactoryImpl(SSLParametersImpl sslParameters) { 44 this.sslParameters = sslParameters; 45 this.instantiationException = null; 46 } 47 48 @Override 49 public String[] getDefaultCipherSuites() { 50 return sslParameters.getEnabledCipherSuites(); 51 } 52 53 @Override 54 public String[] getSupportedCipherSuites() { 55 return NativeCrypto.getSupportedCipherSuites(); 56 } 57 58 @Override 59 public Socket createSocket() throws IOException { 60 if (instantiationException != null) { 61 throw instantiationException; 62 } 63 return new OpenSSLSocketImpl((SSLParametersImpl) sslParameters.clone()); 64 } 65 66 @Override 67 public Socket createSocket(String hostname, int port) throws IOException, UnknownHostException { 68 return new OpenSSLSocketImpl(hostname, port, (SSLParametersImpl) sslParameters.clone()); 69 } 70 71 @Override 72 public Socket createSocket(String hostname, int port, InetAddress localHost, int localPort) 73 throws IOException, UnknownHostException { 74 return new OpenSSLSocketImpl(hostname, 75 port, 76 localHost, 77 localPort, 78 (SSLParametersImpl) sslParameters.clone()); 79 } 80 81 @Override 82 public Socket createSocket(InetAddress address, int port) throws IOException { 83 return new OpenSSLSocketImpl(address, port, (SSLParametersImpl) sslParameters.clone()); 84 } 85 86 @Override 87 public Socket createSocket(InetAddress address, 88 int port, 89 InetAddress localAddress, 90 int localPort) 91 throws IOException { 92 return new OpenSSLSocketImpl(address, 93 port, 94 localAddress, 95 localPort, 96 (SSLParametersImpl) sslParameters.clone()); 97 } 98 99 @Override 100 public Socket createSocket(Socket s, String hostname, int port, boolean autoClose) 101 throws IOException { 102 return new OpenSSLSocketImplWrapper(s, 103 hostname, 104 port, 105 autoClose, 106 (SSLParametersImpl) sslParameters.clone()); 107 } 108 } 109