SSLSocketFactory.java 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /* SSLSocketFactory.java -- factory for SSL client sockets.
  2. Copyright (C) 2004 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package javax.net.ssl;
  32. import java.io.IOException;
  33. import java.net.InetAddress;
  34. import java.net.Socket;
  35. import java.security.KeyStore;
  36. import java.security.Security;
  37. import javax.net.SocketFactory;
  38. /**
  39. * A socket factory for creating <i>Secure Socket Layer</i> (<b>SSL</b>)
  40. * sockets.
  41. */
  42. public abstract class SSLSocketFactory extends SocketFactory
  43. {
  44. // Constants.
  45. // -------------------------------------------------------------------------
  46. private static SSLContext context;
  47. // Constructor.
  48. // -------------------------------------------------------------------------
  49. public SSLSocketFactory()
  50. {
  51. super();
  52. }
  53. // Class methods.
  54. // -------------------------------------------------------------------------
  55. /**
  56. * Returns a default implementation of a SSL socket factory.
  57. *
  58. * <p>To control the class that gets returned by this method, set the
  59. * security property "ssl.SocketFactory.provider" to the class
  60. * name of a concrete implementation of this class. If not set, a
  61. * system-dependent implementation will be used.</p>
  62. *
  63. * <p>The implementation returned is created by the first implementation
  64. * of the {@link SSLContext} class found, which is initialized with
  65. * default parameters. To control the key and trust manager factory
  66. * algorithms used as defaults, set the security properties
  67. * "ssl.keyManagerFactory.algorithm" and "ssl.trustManagerFactory.algorithm"
  68. * to the appropriate names.</p>
  69. *
  70. * <p>Using this method is not recommended. Instead, use the methods of
  71. * {@link SSLContext}, which provide much better control over the
  72. * creation of socket factories.</p>
  73. *
  74. * @return The default socket factory.
  75. * @throws RuntimeException If no default can be created.
  76. */
  77. public static synchronized SocketFactory getDefault()
  78. {
  79. try
  80. {
  81. String s = Security.getProperty("ssl.SocketFactory.provider");
  82. ClassLoader cl = ClassLoader.getSystemClassLoader();
  83. if (s != null && cl != null)
  84. {
  85. return (SocketFactory) cl.loadClass(s).newInstance();
  86. }
  87. }
  88. catch (Exception e)
  89. {
  90. }
  91. if (context == null)
  92. {
  93. KeyManager[] km = null;
  94. TrustManager[] tm = null;
  95. // 1. Determine which algorithms to use for the key and trust
  96. // manager factories.
  97. String kmAlg = KeyManagerFactory.getDefaultAlgorithm();
  98. String tmAlg = TrustManagerFactory.getDefaultAlgorithm();
  99. // 2. Try to initialize the factories with default parameters.
  100. try
  101. {
  102. KeyManagerFactory kmf = KeyManagerFactory.getInstance(kmAlg);
  103. kmf.init(null, null);
  104. km = kmf.getKeyManagers();
  105. }
  106. catch (Exception ex)
  107. {
  108. }
  109. try
  110. {
  111. TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmAlg);
  112. tmf.init((KeyStore) null);
  113. tm = tmf.getTrustManagers();
  114. }
  115. catch (Exception ex)
  116. {
  117. }
  118. // 3. Create and initialize a context.
  119. try
  120. {
  121. context = SSLContext.getInstance("SSLv3");
  122. context.init(km, tm, null);
  123. }
  124. catch (Exception ex)
  125. {
  126. return new ErrorSocketFactory(new RuntimeException(
  127. "error instantiating default socket factory: " + ex.toString(),
  128. ex));
  129. }
  130. }
  131. try
  132. {
  133. return context.getSocketFactory();
  134. }
  135. catch (Exception e)
  136. {
  137. }
  138. return new ErrorSocketFactory(new RuntimeException(
  139. "no SSLSocketFactory implementation available"));
  140. }
  141. private static final class ErrorSocketFactory extends SSLSocketFactory
  142. {
  143. private RuntimeException x;
  144. ErrorSocketFactory(RuntimeException x)
  145. {
  146. this.x = x;
  147. }
  148. public Socket createSocket() throws IOException
  149. {
  150. throw (IOException) new IOException().initCause(x);
  151. }
  152. public Socket createSocket(String host, int port)
  153. throws IOException
  154. {
  155. throw (IOException) new IOException().initCause(x);
  156. }
  157. public Socket createSocket(String host, int port, InetAddress localHost,
  158. int localPort)
  159. throws IOException
  160. {
  161. throw (IOException) new IOException().initCause(x);
  162. }
  163. public Socket createSocket(InetAddress host, int port) throws IOException
  164. {
  165. throw (IOException) new IOException().initCause(x);
  166. }
  167. public Socket createSocket(InetAddress hast, int port, InetAddress localHost,
  168. int localPort)
  169. throws IOException
  170. {
  171. throw (IOException) new IOException().initCause(x);
  172. }
  173. public String[] getDefaultCipherSuites()
  174. {
  175. throw new RuntimeException(x);
  176. }
  177. public String[] getSupportedCipherSuites()
  178. {
  179. throw new RuntimeException(x);
  180. }
  181. public Socket createSocket(Socket s, String host, int port,
  182. boolean autoClose)
  183. throws IOException
  184. {
  185. throw new RuntimeException(x);
  186. }
  187. }
  188. // Abstract methods.
  189. // -------------------------------------------------------------------------
  190. /**
  191. * Creates a SSL socket wrapped around an existing socket.
  192. *
  193. * @param socket The socket to wrap.
  194. * @param host The host the socket is connected to.
  195. * @param port The port the socket is connected to.
  196. * @param autoClose Whether or not the wrapped socket should be closed
  197. * automatically.
  198. * @return The new SSL socket.
  199. * @throws IOException If the socket could not be created.
  200. */
  201. public abstract Socket createSocket(Socket socket, String host,
  202. int port, boolean autoClose)
  203. throws IOException;
  204. /**
  205. * Returns the list of cipher suites that will be enabled in sockets
  206. * created by this factory.
  207. *
  208. * @return The default cipher suites.
  209. */
  210. public abstract String[] getDefaultCipherSuites();
  211. /**
  212. * Returns the list of all cipher suites supported by this factory.
  213. *
  214. * @return The list of supported cipher suites.
  215. */
  216. public abstract String[] getSupportedCipherSuites();
  217. }