J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 1997-2007 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun in the LICENSE file that accompanied this code. |
| 10 | * |
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | * accompanied this code). |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License version |
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | |
| 27 | package javax.net.ssl; |
| 28 | |
| 29 | import java.net.*; |
| 30 | import javax.net.SocketFactory; |
| 31 | import java.io.IOException; |
| 32 | import java.security.*; |
| 33 | |
| 34 | import sun.security.action.GetPropertyAction; |
| 35 | |
| 36 | /** |
| 37 | * <code>SSLSocketFactory</code>s create <code>SSLSocket</code>s. |
| 38 | * |
| 39 | * @since 1.4 |
| 40 | * @see SSLSocket |
| 41 | * @author David Brownell |
| 42 | */ |
| 43 | public abstract class SSLSocketFactory extends SocketFactory |
| 44 | { |
| 45 | private static SSLSocketFactory theFactory; |
| 46 | |
| 47 | private static boolean propertyChecked; |
| 48 | |
| 49 | static final boolean DEBUG; |
| 50 | |
| 51 | static { |
| 52 | String s = java.security.AccessController.doPrivileged( |
| 53 | new GetPropertyAction("javax.net.debug", "")).toLowerCase(); |
| 54 | DEBUG = s.contains("all") || s.contains("ssl"); |
| 55 | } |
| 56 | |
| 57 | private static void log(String msg) { |
| 58 | if (DEBUG) { |
| 59 | System.out.println(msg); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Constructor is used only by subclasses. |
| 65 | */ |
| 66 | public SSLSocketFactory() { |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Returns the default SSL socket factory. |
| 71 | * |
| 72 | * <p>The first time this method is called, the security property |
| 73 | * "ssl.SocketFactory.provider" is examined. If it is non-null, a class by |
| 74 | * that name is loaded and instantiated. If that is successful and the |
| 75 | * object is an instance of SSLSocketFactory, it is made the default SSL |
| 76 | * socket factory. |
| 77 | * |
| 78 | * <p>Otherwise, this method returns |
| 79 | * <code>SSLContext.getDefault().getSocketFactory()</code>. If that |
| 80 | * call fails, an inoperative factory is returned. |
| 81 | * |
| 82 | * @return the default <code>SocketFactory</code> |
| 83 | * @see SSLContext#getDefault |
| 84 | */ |
| 85 | public static synchronized SocketFactory getDefault() { |
| 86 | if (theFactory != null) { |
| 87 | return theFactory; |
| 88 | } |
| 89 | |
| 90 | if (propertyChecked == false) { |
| 91 | propertyChecked = true; |
| 92 | String clsName = getSecurityProperty("ssl.SocketFactory.provider"); |
| 93 | if (clsName != null) { |
| 94 | log("setting up default SSLSocketFactory"); |
| 95 | try { |
| 96 | Class cls = null; |
| 97 | try { |
| 98 | cls = Class.forName(clsName); |
| 99 | } catch (ClassNotFoundException e) { |
| 100 | ClassLoader cl = ClassLoader.getSystemClassLoader(); |
| 101 | if (cl != null) { |
| 102 | cls = cl.loadClass(clsName); |
| 103 | } |
| 104 | } |
| 105 | log("class " + clsName + " is loaded"); |
| 106 | SSLSocketFactory fac = (SSLSocketFactory)cls.newInstance(); |
| 107 | log("instantiated an instance of class " + clsName); |
| 108 | theFactory = fac; |
| 109 | return fac; |
| 110 | } catch (Exception e) { |
| 111 | log("SSLSocketFactory instantiation failed: " + e.toString()); |
| 112 | theFactory = new DefaultSSLSocketFactory(e); |
| 113 | return theFactory; |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | try { |
| 119 | return SSLContext.getDefault().getSocketFactory(); |
| 120 | } catch (NoSuchAlgorithmException e) { |
| 121 | return new DefaultSSLSocketFactory(e); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | static String getSecurityProperty(final String name) { |
| 126 | return AccessController.doPrivileged(new PrivilegedAction<String>() { |
| 127 | public String run() { |
| 128 | String s = java.security.Security.getProperty(name); |
| 129 | if (s != null) { |
| 130 | s = s.trim(); |
| 131 | if (s.length() == 0) { |
| 132 | s = null; |
| 133 | } |
| 134 | } |
| 135 | return s; |
| 136 | } |
| 137 | }); |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Returns the list of cipher suites which are enabled by default. |
| 142 | * Unless a different list is enabled, handshaking on an SSL connection |
| 143 | * will use one of these cipher suites. The minimum quality of service |
| 144 | * for these defaults requires confidentiality protection and server |
| 145 | * authentication (that is, no anonymous cipher suites). |
| 146 | * |
| 147 | * @see #getSupportedCipherSuites() |
| 148 | * @return array of the cipher suites enabled by default |
| 149 | */ |
| 150 | public abstract String [] getDefaultCipherSuites(); |
| 151 | |
| 152 | /** |
| 153 | * Returns the names of the cipher suites which could be enabled for use |
| 154 | * on an SSL connection. Normally, only a subset of these will actually |
| 155 | * be enabled by default, since this list may include cipher suites which |
| 156 | * do not meet quality of service requirements for those defaults. Such |
| 157 | * cipher suites are useful in specialized applications. |
| 158 | * |
| 159 | * @see #getDefaultCipherSuites() |
| 160 | * @return an array of cipher suite names |
| 161 | */ |
| 162 | public abstract String [] getSupportedCipherSuites(); |
| 163 | |
| 164 | /** |
| 165 | * Returns a socket layered over an existing socket connected to the named |
| 166 | * host, at the given port. This constructor can be used when tunneling SSL |
| 167 | * through a proxy or when negotiating the use of SSL over an existing |
| 168 | * socket. The host and port refer to the logical peer destination. |
| 169 | * This socket is configured using the socket options established for |
| 170 | * this factory. |
| 171 | * |
| 172 | * @param s the existing socket |
| 173 | * @param host the server host |
| 174 | * @param port the server port |
| 175 | * @param autoClose close the underlying socket when this socket is closed |
| 176 | * @return a socket connected to the specified host and port |
| 177 | * @throws IOException if an I/O error occurs when creating the socket |
| 178 | * @throws NullPointerException if the parameter s is null |
| 179 | */ |
| 180 | public abstract Socket createSocket(Socket s, String host, |
| 181 | int port, boolean autoClose) |
| 182 | throws IOException; |
| 183 | } |
| 184 | |
| 185 | |
| 186 | // file private |
| 187 | class DefaultSSLSocketFactory extends SSLSocketFactory |
| 188 | { |
| 189 | private Exception reason; |
| 190 | |
| 191 | DefaultSSLSocketFactory(Exception reason) { |
| 192 | this.reason = reason; |
| 193 | } |
| 194 | |
| 195 | private Socket throwException() throws SocketException { |
| 196 | throw (SocketException) |
| 197 | new SocketException(reason.toString()).initCause(reason); |
| 198 | } |
| 199 | |
| 200 | public Socket createSocket() |
| 201 | throws IOException |
| 202 | { |
| 203 | return throwException(); |
| 204 | } |
| 205 | |
| 206 | public Socket createSocket(String host, int port) |
| 207 | throws IOException |
| 208 | { |
| 209 | return throwException(); |
| 210 | } |
| 211 | |
| 212 | public Socket createSocket(Socket s, String host, |
| 213 | int port, boolean autoClose) |
| 214 | throws IOException |
| 215 | { |
| 216 | return throwException(); |
| 217 | } |
| 218 | |
| 219 | public Socket createSocket(InetAddress address, int port) |
| 220 | throws IOException |
| 221 | { |
| 222 | return throwException(); |
| 223 | } |
| 224 | |
| 225 | public Socket createSocket(String host, int port, |
| 226 | InetAddress clientAddress, int clientPort) |
| 227 | throws IOException |
| 228 | { |
| 229 | return throwException(); |
| 230 | } |
| 231 | |
| 232 | public Socket createSocket(InetAddress address, int port, |
| 233 | InetAddress clientAddress, int clientPort) |
| 234 | throws IOException |
| 235 | { |
| 236 | return throwException(); |
| 237 | } |
| 238 | |
| 239 | public String [] getDefaultCipherSuites() { |
| 240 | return new String[0]; |
| 241 | } |
| 242 | |
| 243 | public String [] getSupportedCipherSuites() { |
| 244 | return new String[0]; |
| 245 | } |
| 246 | } |