Path: blob/master/src/java.rmi/share/classes/javax/rmi/ssl/SslRMIClientSocketFactory.java
41171 views
/*1* Copyright (c) 2003, 2008, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.rmi.ssl;2627import java.io.IOException;28import java.io.Serializable;29import java.net.Socket;30import java.rmi.server.RMIClientSocketFactory;31import java.util.StringTokenizer;32import javax.net.SocketFactory;33import javax.net.ssl.SSLSocket;34import javax.net.ssl.SSLSocketFactory;3536/**37* <p>An <code>SslRMIClientSocketFactory</code> instance is used by the RMI38* runtime in order to obtain client sockets for RMI calls via SSL.</p>39*40* <p>This class implements <code>RMIClientSocketFactory</code> over41* the Secure Sockets Layer (SSL) or Transport Layer Security (TLS)42* protocols.</p>43*44* <p>This class creates SSL sockets using the default45* <code>SSLSocketFactory</code> (see {@link46* SSLSocketFactory#getDefault}). All instances of this class are47* functionally equivalent. In particular, they all share the same48* truststore, and the same keystore when client authentication is49* required by the server. This behavior can be modified in50* subclasses by overriding the {@link #createSocket(String,int)}51* method; in that case, {@link #equals(Object) equals} and {@link52* #hashCode() hashCode} may also need to be overridden.</p>53*54* <p>If the system property55* {@systemProperty javax.rmi.ssl.client.enabledCipherSuites} is specified,56* the {@link #createSocket(String,int)} method will call {@link57* SSLSocket#setEnabledCipherSuites(String[])} before returning the58* socket. The value of this system property is a string that is a59* comma-separated list of SSL/TLS cipher suites to enable.</p>60*61* <p>If the system property62* {@systemProperty javax.rmi.ssl.client.enabledProtocols} is specified,63* the {@link #createSocket(String,int)} method will call {@link64* SSLSocket#setEnabledProtocols(String[])} before returning the65* socket. The value of this system property is a string that is a66* comma-separated list of SSL/TLS protocol versions to enable.</p>67*68* @see javax.net.ssl.SSLSocketFactory69* @see javax.rmi.ssl.SslRMIServerSocketFactory70* @since 1.571*/72public class SslRMIClientSocketFactory73implements RMIClientSocketFactory, Serializable {7475/**76* <p>Creates a new <code>SslRMIClientSocketFactory</code>.</p>77*/78public SslRMIClientSocketFactory() {79// We don't force the initialization of the default SSLSocketFactory80// at construction time - because the RMI client socket factory is81// created on the server side, where that initialization is a priori82// meaningless, unless both server and client run in the same JVM.83// We could possibly override readObject() to force this initialization,84// but it might not be a good idea to actually mix this with possible85// deserialization problems.86// So contrarily to what we do for the server side, the initialization87// of the SSLSocketFactory will be delayed until the first time88// createSocket() is called - note that the default SSLSocketFactory89// might already have been initialized anyway if someone in the JVM90// already called SSLSocketFactory.getDefault().91//92}9394/**95* <p>Creates an SSL socket.</p>96*97* <p>If the system property98* {@systemProperty javax.rmi.ssl.client.enabledCipherSuites} is99* specified, this method will call {@link100* SSLSocket#setEnabledCipherSuites(String[])} before returning101* the socket. The value of this system property is a string that102* is a comma-separated list of SSL/TLS cipher suites to103* enable.</p>104*105* <p>If the system property106* {@systemProperty javax.rmi.ssl.client.enabledProtocols} is107* specified, this method will call {@link108* SSLSocket#setEnabledProtocols(String[])} before returning the109* socket. The value of this system property is a string that is a110* comma-separated list of SSL/TLS protocol versions to111* enable.</p>112*/113public Socket createSocket(String host, int port) throws IOException {114// Retrieve the SSLSocketFactory115//116final SocketFactory sslSocketFactory = getDefaultClientSocketFactory();117// Create the SSLSocket118//119final SSLSocket sslSocket = (SSLSocket)120sslSocketFactory.createSocket(host, port);121// Set the SSLSocket Enabled Cipher Suites122//123final String enabledCipherSuites =124System.getProperty("javax.rmi.ssl.client.enabledCipherSuites");125if (enabledCipherSuites != null) {126StringTokenizer st = new StringTokenizer(enabledCipherSuites, ",");127int tokens = st.countTokens();128String enabledCipherSuitesList[] = new String[tokens];129for (int i = 0 ; i < tokens; i++) {130enabledCipherSuitesList[i] = st.nextToken();131}132try {133sslSocket.setEnabledCipherSuites(enabledCipherSuitesList);134} catch (IllegalArgumentException e) {135throw (IOException)136new IOException(e.getMessage()).initCause(e);137}138}139// Set the SSLSocket Enabled Protocols140//141final String enabledProtocols =142System.getProperty("javax.rmi.ssl.client.enabledProtocols");143if (enabledProtocols != null) {144StringTokenizer st = new StringTokenizer(enabledProtocols, ",");145int tokens = st.countTokens();146String enabledProtocolsList[] = new String[tokens];147for (int i = 0 ; i < tokens; i++) {148enabledProtocolsList[i] = st.nextToken();149}150try {151sslSocket.setEnabledProtocols(enabledProtocolsList);152} catch (IllegalArgumentException e) {153throw (IOException)154new IOException(e.getMessage()).initCause(e);155}156}157// Return the preconfigured SSLSocket158//159return sslSocket;160}161162/**163* <p>Indicates whether some other object is "equal to" this one.</p>164*165* <p>Because all instances of this class are functionally equivalent166* (they all use the default167* <code>SSLSocketFactory</code>), this method simply returns168* <code>this.getClass().equals(obj.getClass())</code>.</p>169*170* <p>A subclass should override this method (as well171* as {@link #hashCode()}) if its instances are not all172* functionally equivalent.</p>173*/174public boolean equals(Object obj) {175if (obj == null) return false;176if (obj == this) return true;177return this.getClass().equals(obj.getClass());178}179180/**181* <p>Returns a hash code value for this182* <code>SslRMIClientSocketFactory</code>.</p>183*184* @return a hash code value for this185* <code>SslRMIClientSocketFactory</code>.186*/187public int hashCode() {188return this.getClass().hashCode();189}190191// We use a static field because:192//193// SSLSocketFactory.getDefault() always returns the same object194// (at least on Sun's implementation), and we want to make sure195// that the Javadoc & the implementation stay in sync.196//197// If someone needs to have different SslRMIClientSocketFactory factories198// with different underlying SSLSocketFactory objects using different key199// and trust stores, he can always do so by subclassing this class and200// overriding createSocket(String host, int port).201//202private static SocketFactory defaultSocketFactory = null;203204private static synchronized SocketFactory getDefaultClientSocketFactory() {205if (defaultSocketFactory == null)206defaultSocketFactory = SSLSocketFactory.getDefault();207return defaultSocketFactory;208}209210private static final long serialVersionUID = -8310631444933958385L;211}212213214