Home | History | Annotate | Download | only in conscrypt
      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     public String[] getDefaultCipherSuites() {
     49         return NativeCrypto.getDefaultCipherSuites();
     50     }
     51 
     52     public String[] getSupportedCipherSuites() {
     53         return NativeCrypto.getSupportedCipherSuites();
     54     }
     55 
     56     public Socket createSocket() throws IOException {
     57         if (instantiationException != null) {
     58             throw instantiationException;
     59         }
     60         return new OpenSSLSocketImpl((SSLParametersImpl) sslParameters.clone());
     61     }
     62 
     63     public Socket createSocket(String host, int port) throws IOException, UnknownHostException {
     64         return new OpenSSLSocketImpl(host, port, (SSLParametersImpl) sslParameters.clone());
     65     }
     66 
     67     public Socket createSocket(String host, int port, InetAddress localHost, int localPort)
     68             throws IOException, UnknownHostException {
     69         return new OpenSSLSocketImpl(host,
     70                                      port,
     71                                      localHost,
     72                                      localPort,
     73                                      (SSLParametersImpl) sslParameters.clone());
     74     }
     75 
     76     public Socket createSocket(InetAddress host, int port) throws IOException {
     77         return new OpenSSLSocketImpl(host, port, (SSLParametersImpl) sslParameters.clone());
     78     }
     79 
     80     public Socket createSocket(InetAddress address,
     81                                int port,
     82                                InetAddress localAddress,
     83                                int localPort)
     84             throws IOException {
     85         return new OpenSSLSocketImpl(address,
     86                                      port,
     87                                      localAddress,
     88                                      localPort,
     89                                      (SSLParametersImpl) sslParameters.clone());
     90     }
     91 
     92     public Socket createSocket(Socket s, String host, int port, boolean autoClose)
     93             throws IOException {
     94         return new OpenSSLSocketImplWrapper(s,
     95                                             host,
     96                                             port,
     97                                             autoClose,
     98                                             (SSLParametersImpl) sslParameters.clone());
     99     }
    100 }
    101