0N/A/*
2362N/A * Copyright (c) 2003, 2008, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage javax.rmi.ssl;
0N/A
0N/Aimport java.io.IOException;
0N/Aimport java.io.Serializable;
0N/Aimport java.net.Socket;
0N/Aimport java.rmi.server.RMIClientSocketFactory;
0N/Aimport java.util.StringTokenizer;
0N/Aimport javax.net.SocketFactory;
0N/Aimport javax.net.ssl.SSLSocket;
0N/Aimport javax.net.ssl.SSLSocketFactory;
0N/A
0N/A/**
0N/A * <p>An <code>SslRMIClientSocketFactory</code> instance is used by the RMI
0N/A * runtime in order to obtain client sockets for RMI calls via SSL.</p>
0N/A *
0N/A * <p>This class implements <code>RMIClientSocketFactory</code> over
0N/A * the Secure Sockets Layer (SSL) or Transport Layer Security (TLS)
0N/A * protocols.</p>
0N/A *
0N/A * <p>This class creates SSL sockets using the default
0N/A * <code>SSLSocketFactory</code> (see {@link
0N/A * SSLSocketFactory#getDefault}). All instances of this class are
0N/A * functionally equivalent. In particular, they all share the same
0N/A * truststore, and the same keystore when client authentication is
0N/A * required by the server. This behavior can be modified in
0N/A * subclasses by overriding the {@link #createSocket(String,int)}
0N/A * method; in that case, {@link #equals(Object) equals} and {@link
0N/A * #hashCode() hashCode} may also need to be overridden.</p>
0N/A *
0N/A * <p>If the system property
0N/A * <code>javax.rmi.ssl.client.enabledCipherSuites</code> is specified,
0N/A * the {@link #createSocket(String,int)} method will call {@link
0N/A * SSLSocket#setEnabledCipherSuites(String[])} before returning the
0N/A * socket. The value of this system property is a string that is a
0N/A * comma-separated list of SSL/TLS cipher suites to enable.</p>
0N/A *
0N/A * <p>If the system property
0N/A * <code>javax.rmi.ssl.client.enabledProtocols</code> is specified,
0N/A * the {@link #createSocket(String,int)} method will call {@link
0N/A * SSLSocket#setEnabledProtocols(String[])} before returning the
0N/A * socket. The value of this system property is a string that is a
0N/A * comma-separated list of SSL/TLS protocol versions to enable.</p>
0N/A *
0N/A * @see javax.net.ssl.SSLSocketFactory
0N/A * @see javax.rmi.ssl.SslRMIServerSocketFactory
0N/A * @since 1.5
0N/A */
0N/Apublic class SslRMIClientSocketFactory
0N/A implements RMIClientSocketFactory, Serializable {
0N/A
0N/A /**
0N/A * <p>Creates a new <code>SslRMIClientSocketFactory</code>.</p>
0N/A */
0N/A public SslRMIClientSocketFactory() {
0N/A // We don't force the initialization of the default SSLSocketFactory
0N/A // at construction time - because the RMI client socket factory is
0N/A // created on the server side, where that initialization is a priori
0N/A // meaningless, unless both server and client run in the same JVM.
0N/A // We could possibly override readObject() to force this initialization,
0N/A // but it might not be a good idea to actually mix this with possible
0N/A // deserialization problems.
0N/A // So contrarily to what we do for the server side, the initialization
0N/A // of the SSLSocketFactory will be delayed until the first time
0N/A // createSocket() is called - note that the default SSLSocketFactory
0N/A // might already have been initialized anyway if someone in the JVM
0N/A // already called SSLSocketFactory.getDefault().
0N/A //
0N/A }
0N/A
0N/A /**
0N/A * <p>Creates an SSL socket.</p>
0N/A *
0N/A * <p>If the system property
0N/A * <code>javax.rmi.ssl.client.enabledCipherSuites</code> is
0N/A * specified, this method will call {@link
0N/A * SSLSocket#setEnabledCipherSuites(String[])} before returning
0N/A * the socket. The value of this system property is a string that
0N/A * is a comma-separated list of SSL/TLS cipher suites to
0N/A * enable.</p>
0N/A *
0N/A * <p>If the system property
0N/A * <code>javax.rmi.ssl.client.enabledProtocols</code> is
0N/A * specified, this method will call {@link
0N/A * SSLSocket#setEnabledProtocols(String[])} before returning the
0N/A * socket. The value of this system property is a string that is a
0N/A * comma-separated list of SSL/TLS protocol versions to
0N/A * enable.</p>
0N/A */
0N/A public Socket createSocket(String host, int port) throws IOException {
0N/A // Retrieve the SSLSocketFactory
0N/A //
0N/A final SocketFactory sslSocketFactory = getDefaultClientSocketFactory();
0N/A // Create the SSLSocket
0N/A //
0N/A final SSLSocket sslSocket = (SSLSocket)
0N/A sslSocketFactory.createSocket(host, port);
0N/A // Set the SSLSocket Enabled Cipher Suites
0N/A //
28N/A final String enabledCipherSuites =
0N/A System.getProperty("javax.rmi.ssl.client.enabledCipherSuites");
0N/A if (enabledCipherSuites != null) {
0N/A StringTokenizer st = new StringTokenizer(enabledCipherSuites, ",");
0N/A int tokens = st.countTokens();
0N/A String enabledCipherSuitesList[] = new String[tokens];
0N/A for (int i = 0 ; i < tokens; i++) {
0N/A enabledCipherSuitesList[i] = st.nextToken();
0N/A }
0N/A try {
0N/A sslSocket.setEnabledCipherSuites(enabledCipherSuitesList);
0N/A } catch (IllegalArgumentException e) {
0N/A throw (IOException)
0N/A new IOException(e.getMessage()).initCause(e);
0N/A }
0N/A }
0N/A // Set the SSLSocket Enabled Protocols
0N/A //
28N/A final String enabledProtocols =
0N/A System.getProperty("javax.rmi.ssl.client.enabledProtocols");
0N/A if (enabledProtocols != null) {
0N/A StringTokenizer st = new StringTokenizer(enabledProtocols, ",");
0N/A int tokens = st.countTokens();
0N/A String enabledProtocolsList[] = new String[tokens];
0N/A for (int i = 0 ; i < tokens; i++) {
0N/A enabledProtocolsList[i] = st.nextToken();
0N/A }
0N/A try {
0N/A sslSocket.setEnabledProtocols(enabledProtocolsList);
0N/A } catch (IllegalArgumentException e) {
0N/A throw (IOException)
0N/A new IOException(e.getMessage()).initCause(e);
0N/A }
0N/A }
0N/A // Return the preconfigured SSLSocket
0N/A //
0N/A return sslSocket;
0N/A }
0N/A
0N/A /**
0N/A * <p>Indicates whether some other object is "equal to" this one.</p>
0N/A *
0N/A * <p>Because all instances of this class are functionally equivalent
0N/A * (they all use the default
0N/A * <code>SSLSocketFactory</code>), this method simply returns
0N/A * <code>this.getClass().equals(obj.getClass())</code>.</p>
0N/A *
0N/A * <p>A subclass should override this method (as well
0N/A * as {@link #hashCode()}) if its instances are not all
0N/A * functionally equivalent.</p>
0N/A */
0N/A public boolean equals(Object obj) {
0N/A if (obj == null) return false;
0N/A if (obj == this) return true;
0N/A return this.getClass().equals(obj.getClass());
0N/A }
0N/A
0N/A /**
0N/A * <p>Returns a hash code value for this
0N/A * <code>SslRMIClientSocketFactory</code>.</p>
0N/A *
0N/A * @return a hash code value for this
0N/A * <code>SslRMIClientSocketFactory</code>.
0N/A */
0N/A public int hashCode() {
0N/A return this.getClass().hashCode();
0N/A }
0N/A
0N/A // We use a static field because:
0N/A //
0N/A // SSLSocketFactory.getDefault() always returns the same object
0N/A // (at least on Sun's implementation), and we want to make sure
0N/A // that the Javadoc & the implementation stay in sync.
0N/A //
0N/A // If someone needs to have different SslRMIClientSocketFactory factories
0N/A // with different underlying SSLSocketFactory objects using different key
0N/A // and trust stores, he can always do so by subclassing this class and
0N/A // overriding createSocket(String host, int port).
0N/A //
0N/A private static SocketFactory defaultSocketFactory = null;
0N/A
0N/A private static synchronized SocketFactory getDefaultClientSocketFactory() {
0N/A if (defaultSocketFactory == null)
0N/A defaultSocketFactory = SSLSocketFactory.getDefault();
0N/A return defaultSocketFactory;
0N/A }
0N/A
0N/A private static final long serialVersionUID = -8310631444933958385L;
0N/A}