Home | History | Annotate | Download | only in ssl
      1 /*
      2  *  Licensed to the Apache Software Foundation (ASF) under one or more
      3  *  contributor license agreements.  See the NOTICE file distributed with
      4  *  this work for additional information regarding copyright ownership.
      5  *  The ASF licenses this file to You under the Apache License, Version 2.0
      6  *  (the "License"); you may not use this file except in compliance with
      7  *  the License.  You may obtain a copy of the License at
      8  *
      9  *     http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  *  Unless required by applicable law or agreed to in writing, software
     12  *  distributed under the License is distributed on an "AS IS" BASIS,
     13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  *  See the License for the specific language governing permissions and
     15  *  limitations under the License.
     16  */
     17 
     18 package javax.net.ssl;
     19 
     20 import java.io.IOException;
     21 import java.net.Socket;
     22 import java.security.AccessController;
     23 import java.security.PrivilegedAction;
     24 import java.security.Security;
     25 // BEGIN android-added
     26 import java.util.logging.Logger;
     27 // END android-added
     28 
     29 import javax.net.SocketFactory;
     30 
     31 /**
     32  * The abstract factory implementation to create {@code SSLSocket}s.
     33  */
     34 public abstract class SSLSocketFactory extends SocketFactory {
     35     // FIXME EXPORT CONTROL
     36 
     37     // The default SSL socket factory
     38     private static SocketFactory defaultSocketFactory;
     39 
     40     private static String defaultName;
     41 
     42     /**
     43      * Returns the default {@code SSLSocketFactory} instance. The default is
     44      * defined by the security property {@code 'ssl.SocketFactory.provider'}.
     45      *
     46      * @return the default ssl socket factory instance.
     47      */
     48     public static synchronized SocketFactory getDefault() {
     49         if (defaultSocketFactory != null) {
     50             // BEGIN android-added
     51             log("SSLSocketFactory", "Using factory " + defaultSocketFactory);
     52             // END android-added
     53             return defaultSocketFactory;
     54         }
     55         if (defaultName == null) {
     56             AccessController.doPrivileged(new PrivilegedAction<Void>() {
     57                 public Void run() {
     58                     defaultName = Security.getProperty("ssl.SocketFactory.provider");
     59                     if (defaultName != null) {
     60                         ClassLoader cl = Thread.currentThread().getContextClassLoader();
     61                         if (cl == null) {
     62                             cl = ClassLoader.getSystemClassLoader();
     63                         }
     64                         try {
     65                             final Class<?> sfc = Class.forName(defaultName, true, cl);
     66                             defaultSocketFactory = (SocketFactory) sfc.newInstance();
     67                         } catch (Exception e) {
     68                         }
     69                     }
     70                     return null;
     71                 }
     72             });
     73         }
     74 
     75         if (defaultSocketFactory == null) {
     76             // Try to find in providers
     77             SSLContext context = DefaultSSLContext.getContext();
     78             if (context != null) {
     79                 defaultSocketFactory = context.getSocketFactory();
     80             }
     81         }
     82         if (defaultSocketFactory == null) {
     83             // Use internal implementation
     84             defaultSocketFactory = new DefaultSSLSocketFactory("No SSLSocketFactory installed");
     85         }
     86             // BEGIN android-added
     87             log("SSLSocketFactory", "Using factory " + defaultSocketFactory);
     88             // END android-added
     89         return defaultSocketFactory;
     90     }
     91 
     92     // BEGIN android-added
     93     @SuppressWarnings("unchecked")
     94     private static void log(String tag, String msg) {
     95         Logger.getLogger(tag).info(msg);
     96     }
     97     // END android-added
     98 
     99     /**
    100      * Creates a new {@code SSLSocketFactory}.
    101      */
    102     public SSLSocketFactory() {
    103         super();
    104     }
    105 
    106     /**
    107      * Returns the names of the cipher suites that are enabled by default.
    108      *
    109      * @return the names of the cipher suites that are enabled by default.
    110      */
    111     public abstract String[] getDefaultCipherSuites();
    112 
    113     /**
    114      * Returns the names of the cipher suites that are supported and could be
    115      * enabled for an SSL connection.
    116      *
    117      * @return the names of the cipher suites that are supported.
    118      */
    119     public abstract String[] getSupportedCipherSuites();
    120 
    121     /**
    122      * Creates an {@code SSLSocket} over the specified socket that is connected
    123      * to the specified host at the specified port.
    124      *
    125      * @param s
    126      *            the socket.
    127      * @param host
    128      *            the host.
    129      * @param port
    130      *            the port number.
    131      * @param autoClose
    132      *            {@code true} if socket {@code s} should be closed when the
    133      *            created socket is closed, {@code false} if the socket
    134      *            {@code s} should be left open.
    135      * @return the creates ssl socket.
    136      * @throws IOException
    137      *             if creating the socket fails.
    138      * @throws java.net.UnknownHostException
    139      *             if the host is unknown.
    140      */
    141     public abstract Socket createSocket(Socket s, String host, int port, boolean autoClose)
    142             throws IOException;
    143 }
    144