1 /* SSLSocketFactory.java -- factory for SSL client sockets.
2 Copyright (C) 2004 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package javax
.net
.ssl
;
41 import java
.io
.IOException
;
42 import java
.net
.Socket
;
43 import java
.security
.KeyStore
;
44 import java
.security
.Security
;
46 import javax
.net
.SocketFactory
;
49 * A socket factory for creating <i>Secure Socket Layer</i> (<b>SSL</b>)
52 public abstract class SSLSocketFactory
extends SocketFactory
55 // -------------------------------------------------------------------------
57 private static SSLContext context
;
60 // -------------------------------------------------------------------------
62 public SSLSocketFactory()
68 // -------------------------------------------------------------------------
71 * Returns a default implementation of a SSL socket factory.
73 * <p>To control the class that gets returned by this method, set the
74 * security property "ssl.SocketFactory.provider" to the class
75 * name of a concrete implementation of this class. If not set, a
76 * system-dependent implementation will be used.</p>
78 * <p>The implementation returned is created by the first implementation
79 * of the {@link SSLContext} class found, which is initialized with
80 * default parameters. To control the key and trust manager factory
81 * algorithms used as defaults, set the security properties
82 * "ssl.keyManagerFactory.algorithm" and "ssl.trustManagerFactory.algorithm"
83 * to the appropriate names.</p>
85 * <p>Using this method is not recommended. Instead, use the methods of
86 * {@link SSLContext}, which provide much better control over the
87 * creation of socket factories.</p>
89 * @return The default socket factory.
90 * @throws RuntimeException If no default can be created.
92 public static synchronized SocketFactory
getDefault()
96 String s
= Security
.getProperty("ssl.SocketFactory.provider");
97 ClassLoader cl
= ClassLoader
.getSystemClassLoader();
98 if (s
!= null && cl
!= null)
100 return (SocketFactory
) cl
.loadClass(s
).newInstance();
108 KeyManager
[] km
= null;
109 TrustManager
[] tm
= null;
111 // 1. Determine which algorithms to use for the key and trust
112 // manager factories.
113 String kmAlg
= KeyManagerFactory
.getDefaultAlgorithm();
114 String tmAlg
= TrustManagerFactory
.getDefaultAlgorithm();
116 // 2. Try to initialize the factories with default parameters.
119 KeyManagerFactory kmf
= KeyManagerFactory
.getInstance(kmAlg
);
120 kmf
.init(null, null);
121 km
= kmf
.getKeyManagers();
128 TrustManagerFactory tmf
= TrustManagerFactory
.getInstance(tmAlg
);
129 tmf
.init((KeyStore
) null);
130 tm
= tmf
.getTrustManagers();
136 // 3. Create and initialize a context.
139 context
= SSLContext
.getInstance("SSLv3");
140 context
.init(km
, tm
, null);
144 throw new RuntimeException("error instantiating default socket factory: "
150 return context
.getSocketFactory();
155 throw new RuntimeException("no SSLSocketFactory implementation available");
159 // -------------------------------------------------------------------------
162 * Creates a SSL socket wrapped around an existing socket.
164 * @param socket The socket to wrap.
165 * @param host The host the socket is connected to.
166 * @param port The port the socket is connected to.
167 * @param autoClose Whether or not the wrapped socket should be closed
169 * @return The new SSL socket.
170 * @throws IOException If the socket could not be created.
172 public abstract Socket
createSocket(Socket socket
, String host
,
173 int port
, boolean autoClose
)
177 * Returns the list of cipher suites that will be enabled in sockets
178 * created by this factory.
180 * @return The default cipher suites.
182 public abstract String
[] getDefaultCipherSuites();
185 * Returns the list of all cipher suites supported by this factory.
187 * @return The list of supported cipher suites.
189 public abstract String
[] getSupportedCipherSuites();