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.NoSuchAlgorithmException;
     23 import java.security.Security;
     24 import javax.net.SocketFactory;
     25 
     26 /**
     27  * The abstract factory implementation to create {@code SSLSocket}s.
     28  */
     29 public abstract class SSLSocketFactory extends SocketFactory {
     30     // FIXME EXPORT CONTROL
     31 
     32     // The default SSL socket factory
     33     private static SocketFactory defaultSocketFactory;
     34 
     35     private static String defaultName;
     36 
     37     /**
     38      * Returns the default {@code SSLSocketFactory} instance. The default is
     39      * defined by the security property {@code 'ssl.SocketFactory.provider'}.
     40      *
     41      * @return the default ssl socket factory instance.
     42      */
     43     public static synchronized SocketFactory getDefault() {
     44         if (defaultSocketFactory != null) {
     45             return defaultSocketFactory;
     46         }
     47         if (defaultName == null) {
     48             defaultName = Security.getProperty("ssl.SocketFactory.provider");
     49             if (defaultName != null) {
     50                 ClassLoader cl = Thread.currentThread().getContextClassLoader();
     51                 if (cl == null) {
     52                     cl = ClassLoader.getSystemClassLoader();
     53                 }
     54                 try {
     55                     final Class<?> sfc = Class.forName(defaultName, true, cl);
     56                     defaultSocketFactory = (SocketFactory) sfc.newInstance();
     57                 } catch (Exception e) {
     58                     System.logE("Problem creating " + defaultName, e);
     59                 }
     60             }
     61         }
     62 
     63         if (defaultSocketFactory == null) {
     64             SSLContext context;
     65             try {
     66                 context = SSLContext.getDefault();
     67             } catch (NoSuchAlgorithmException e) {
     68                 context = null;
     69             }
     70             if (context != null) {
     71                 defaultSocketFactory = context.getSocketFactory();
     72             }
     73         }
     74         if (defaultSocketFactory == null) {
     75             // Use internal implementation
     76             defaultSocketFactory = new DefaultSSLSocketFactory("No SSLSocketFactory installed");
     77         }
     78         return defaultSocketFactory;
     79     }
     80 
     81     /**
     82      * Creates a new {@code SSLSocketFactory}.
     83      */
     84     public SSLSocketFactory() {
     85     }
     86 
     87     /**
     88      * Returns the names of the cipher suites that are enabled by default.
     89      *
     90      * @return the names of the cipher suites that are enabled by default.
     91      */
     92     public abstract String[] getDefaultCipherSuites();
     93 
     94     /**
     95      * Returns the names of the cipher suites that are supported and could be
     96      * enabled for an SSL connection.
     97      *
     98      * @return the names of the cipher suites that are supported.
     99      */
    100     public abstract String[] getSupportedCipherSuites();
    101 
    102     /**
    103      * Creates an {@code SSLSocket} over the specified socket that is connected
    104      * to the specified host at the specified port.
    105      *
    106      * @param s
    107      *            the socket.
    108      * @param host
    109      *            the host.
    110      * @param port
    111      *            the port number.
    112      * @param autoClose
    113      *            {@code true} if socket {@code s} should be closed when the
    114      *            created socket is closed, {@code false} if the socket
    115      *            {@code s} should be left open.
    116      * @return the creates ssl socket.
    117      * @throws IOException
    118      *             if creating the socket fails.
    119      * @throws java.net.UnknownHostException
    120      *             if the host is unknown.
    121      */
    122     public abstract Socket createSocket(Socket s, String host, int port, boolean autoClose)
    123             throws IOException;
    124 }
    125