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.net.ServerSocket;
0N/Aimport java.net.Socket;
0N/Aimport java.rmi.server.RMIServerSocketFactory;
0N/Aimport java.util.Arrays;
0N/Aimport java.util.List;
0N/Aimport javax.net.ssl.SSLContext;
0N/Aimport javax.net.ssl.SSLServerSocketFactory;
0N/Aimport javax.net.ssl.SSLSocket;
0N/Aimport javax.net.ssl.SSLSocketFactory;
0N/A
0N/A/**
0N/A * <p>An <code>SslRMIServerSocketFactory</code> instance is used by the RMI
0N/A * runtime in order to obtain server sockets for RMI calls via SSL.</p>
0N/A *
0N/A * <p>This class implements <code>RMIServerSocketFactory</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}) or the default
0N/A * <code>SSLServerSocketFactory</code> (see {@link
0N/A * SSLServerSocketFactory#getDefault}) unless the
0N/A * constructor taking an <code>SSLContext</code> is
0N/A * used in which case the SSL sockets are created using
0N/A * the <code>SSLSocketFactory</code> returned by
0N/A * {@link SSLContext#getSocketFactory} or the
0N/A * <code>SSLServerSocketFactory</code> returned by
0N/A * {@link SSLContext#getServerSocketFactory}.
0N/A *
0N/A * When an <code>SSLContext</code> is not supplied all the instances of this
0N/A * class share the same keystore, and the same truststore (when client
0N/A * authentication is required by the server). This behavior can be modified
0N/A * by supplying an already initialized <code>SSLContext</code> instance.
0N/A *
0N/A * @see javax.net.ssl.SSLSocketFactory
0N/A * @see javax.net.ssl.SSLServerSocketFactory
0N/A * @see javax.rmi.ssl.SslRMIClientSocketFactory
0N/A * @since 1.5
0N/A */
0N/Apublic class SslRMIServerSocketFactory implements RMIServerSocketFactory {
0N/A
0N/A /**
0N/A * <p>Creates a new <code>SslRMIServerSocketFactory</code> with
0N/A * the default SSL socket configuration.</p>
0N/A *
0N/A * <p>SSL connections accepted by server sockets created by this
0N/A * factory have the default cipher suites and protocol versions
0N/A * enabled and do not require client authentication.</p>
0N/A */
0N/A public SslRMIServerSocketFactory() {
0N/A this(null, null, false);
0N/A }
0N/A
0N/A /**
0N/A * <p>Creates a new <code>SslRMIServerSocketFactory</code> with
0N/A * the specified SSL socket configuration.</p>
0N/A *
0N/A * @param enabledCipherSuites names of all the cipher suites to
0N/A * enable on SSL connections accepted by server sockets created by
0N/A * this factory, or <code>null</code> to use the cipher suites
0N/A * that are enabled by default
0N/A *
0N/A * @param enabledProtocols names of all the protocol versions to
0N/A * enable on SSL connections accepted by server sockets created by
0N/A * this factory, or <code>null</code> to use the protocol versions
0N/A * that are enabled by default
0N/A *
0N/A * @param needClientAuth <code>true</code> to require client
0N/A * authentication on SSL connections accepted by server sockets
0N/A * created by this factory; <code>false</code> to not require
0N/A * client authentication
0N/A *
0N/A * @exception IllegalArgumentException when one or more of the cipher
0N/A * suites named by the <code>enabledCipherSuites</code> parameter is
0N/A * not supported, when one or more of the protocols named by the
0N/A * <code>enabledProtocols</code> parameter is not supported or when
0N/A * a problem is encountered while trying to check if the supplied
0N/A * cipher suites and protocols to be enabled are supported.
0N/A *
0N/A * @see SSLSocket#setEnabledCipherSuites
0N/A * @see SSLSocket#setEnabledProtocols
0N/A * @see SSLSocket#setNeedClientAuth
0N/A */
0N/A public SslRMIServerSocketFactory(
0N/A String[] enabledCipherSuites,
0N/A String[] enabledProtocols,
0N/A boolean needClientAuth)
0N/A throws IllegalArgumentException {
0N/A this(null, enabledCipherSuites, enabledProtocols, needClientAuth);
0N/A }
0N/A
0N/A /**
0N/A * <p>Creates a new <code>SslRMIServerSocketFactory</code> with the
0N/A * specified <code>SSLContext</code> and SSL socket configuration.</p>
0N/A *
0N/A * @param context the SSL context to be used for creating SSL sockets.
0N/A * If <code>context</code> is null the default <code>SSLSocketFactory</code>
0N/A * or the default <code>SSLServerSocketFactory</code> will be used to
0N/A * create SSL sockets. Otherwise, the socket factory returned by
0N/A * <code>SSLContext.getSocketFactory()</code> or
0N/A * <code>SSLContext.getServerSocketFactory()</code> will be used instead.
0N/A *
0N/A * @param enabledCipherSuites names of all the cipher suites to
0N/A * enable on SSL connections accepted by server sockets created by
0N/A * this factory, or <code>null</code> to use the cipher suites
0N/A * that are enabled by default
0N/A *
0N/A * @param enabledProtocols names of all the protocol versions to
0N/A * enable on SSL connections accepted by server sockets created by
0N/A * this factory, or <code>null</code> to use the protocol versions
0N/A * that are enabled by default
0N/A *
0N/A * @param needClientAuth <code>true</code> to require client
0N/A * authentication on SSL connections accepted by server sockets
0N/A * created by this factory; <code>false</code> to not require
0N/A * client authentication
0N/A *
0N/A * @exception IllegalArgumentException when one or more of the cipher
0N/A * suites named by the <code>enabledCipherSuites</code> parameter is
0N/A * not supported, when one or more of the protocols named by the
0N/A * <code>enabledProtocols</code> parameter is not supported or when
0N/A * a problem is encountered while trying to check if the supplied
0N/A * cipher suites and protocols to be enabled are supported.
0N/A *
0N/A * @see SSLSocket#setEnabledCipherSuites
0N/A * @see SSLSocket#setEnabledProtocols
0N/A * @see SSLSocket#setNeedClientAuth
0N/A * @since 1.7
0N/A */
0N/A public SslRMIServerSocketFactory(
0N/A SSLContext context,
0N/A String[] enabledCipherSuites,
0N/A String[] enabledProtocols,
0N/A boolean needClientAuth)
0N/A throws IllegalArgumentException {
0N/A // Initialize the configuration parameters.
0N/A //
0N/A this.enabledCipherSuites = enabledCipherSuites == null ?
28N/A null : enabledCipherSuites.clone();
0N/A this.enabledProtocols = enabledProtocols == null ?
28N/A null : enabledProtocols.clone();
0N/A this.needClientAuth = needClientAuth;
0N/A
0N/A // Force the initialization of the default at construction time,
0N/A // rather than delaying it to the first time createServerSocket()
0N/A // is called.
0N/A //
0N/A this.context = context;
0N/A final SSLSocketFactory sslSocketFactory =
0N/A context == null ?
0N/A getDefaultSSLSocketFactory() : context.getSocketFactory();
0N/A SSLSocket sslSocket = null;
0N/A if (this.enabledCipherSuites != null || this.enabledProtocols != null) {
0N/A try {
0N/A sslSocket = (SSLSocket) sslSocketFactory.createSocket();
0N/A } catch (Exception e) {
0N/A final String msg = "Unable to check if the cipher suites " +
0N/A "and protocols to enable are supported";
0N/A throw (IllegalArgumentException)
0N/A new IllegalArgumentException(msg).initCause(e);
0N/A }
0N/A }
0N/A
0N/A // Check if all the cipher suites and protocol versions to enable
0N/A // are supported by the underlying SSL/TLS implementation and if
0N/A // true create lists from arrays.
0N/A //
0N/A if (this.enabledCipherSuites != null) {
0N/A sslSocket.setEnabledCipherSuites(this.enabledCipherSuites);
28N/A enabledCipherSuitesList = Arrays.asList(this.enabledCipherSuites);
0N/A }
0N/A if (this.enabledProtocols != null) {
0N/A sslSocket.setEnabledProtocols(this.enabledProtocols);
28N/A enabledProtocolsList = Arrays.asList(this.enabledProtocols);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * <p>Returns the names of the cipher suites enabled on SSL
0N/A * connections accepted by server sockets created by this factory,
0N/A * or <code>null</code> if this factory uses the cipher suites
0N/A * that are enabled by default.</p>
0N/A *
0N/A * @return an array of cipher suites enabled, or <code>null</code>
0N/A *
0N/A * @see SSLSocket#setEnabledCipherSuites
0N/A */
0N/A public final String[] getEnabledCipherSuites() {
0N/A return enabledCipherSuites == null ?
28N/A null : enabledCipherSuites.clone();
0N/A }
0N/A
0N/A /**
0N/A * <p>Returns the names of the protocol versions enabled on SSL
0N/A * connections accepted by server sockets created by this factory,
0N/A * or <code>null</code> if this factory uses the protocol versions
0N/A * that are enabled by default.</p>
0N/A *
0N/A * @return an array of protocol versions enabled, or
0N/A * <code>null</code>
0N/A *
0N/A * @see SSLSocket#setEnabledProtocols
0N/A */
0N/A public final String[] getEnabledProtocols() {
0N/A return enabledProtocols == null ?
28N/A null : enabledProtocols.clone();
0N/A }
0N/A
0N/A /**
0N/A * <p>Returns <code>true</code> if client authentication is
0N/A * required on SSL connections accepted by server sockets created
0N/A * by this factory.</p>
0N/A *
0N/A * @return <code>true</code> if client authentication is required
0N/A *
0N/A * @see SSLSocket#setNeedClientAuth
0N/A */
0N/A public final boolean getNeedClientAuth() {
0N/A return needClientAuth;
0N/A }
0N/A
0N/A /**
0N/A * <p>Creates a server socket that accepts SSL connections
0N/A * configured according to this factory's SSL socket configuration
0N/A * parameters.</p>
0N/A */
0N/A public ServerSocket createServerSocket(int port) throws IOException {
0N/A final SSLSocketFactory sslSocketFactory =
0N/A context == null ?
0N/A getDefaultSSLSocketFactory() : context.getSocketFactory();
0N/A return new ServerSocket(port) {
0N/A public Socket accept() throws IOException {
0N/A Socket socket = super.accept();
0N/A SSLSocket sslSocket = (SSLSocket) sslSocketFactory.createSocket(
0N/A socket, socket.getInetAddress().getHostName(),
0N/A socket.getPort(), true);
0N/A sslSocket.setUseClientMode(false);
0N/A if (enabledCipherSuites != null) {
0N/A sslSocket.setEnabledCipherSuites(enabledCipherSuites);
0N/A }
0N/A if (enabledProtocols != null) {
0N/A sslSocket.setEnabledProtocols(enabledProtocols);
0N/A }
0N/A sslSocket.setNeedClientAuth(needClientAuth);
0N/A return sslSocket;
0N/A }
0N/A };
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>Two <code>SslRMIServerSocketFactory</code> objects are equal
0N/A * if they have been constructed with the same SSL context and
0N/A * SSL socket configuration parameters.</p>
0N/A *
0N/A * <p>A subclass should override this method (as well as
0N/A * {@link #hashCode()}) if it adds instance state that affects
0N/A * equality.</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 if (!(obj instanceof SslRMIServerSocketFactory))
0N/A return false;
0N/A SslRMIServerSocketFactory that = (SslRMIServerSocketFactory) obj;
0N/A return (getClass().equals(that.getClass()) && checkParameters(that));
0N/A }
0N/A
0N/A private boolean checkParameters(SslRMIServerSocketFactory that) {
0N/A // SSL context
0N/A //
0N/A if (context == null ? that.context != null : !context.equals(that.context))
0N/A return false;
0N/A
0N/A // needClientAuth flag
0N/A //
0N/A if (needClientAuth != that.needClientAuth)
0N/A return false;
0N/A
0N/A // enabledCipherSuites
0N/A //
0N/A if ((enabledCipherSuites == null && that.enabledCipherSuites != null) ||
0N/A (enabledCipherSuites != null && that.enabledCipherSuites == null))
0N/A return false;
0N/A if (enabledCipherSuites != null && that.enabledCipherSuites != null) {
28N/A List<String> thatEnabledCipherSuitesList =
28N/A Arrays.asList(that.enabledCipherSuites);
0N/A if (!enabledCipherSuitesList.equals(thatEnabledCipherSuitesList))
0N/A return false;
0N/A }
0N/A
0N/A // enabledProtocols
0N/A //
0N/A if ((enabledProtocols == null && that.enabledProtocols != null) ||
0N/A (enabledProtocols != null && that.enabledProtocols == null))
0N/A return false;
0N/A if (enabledProtocols != null && that.enabledProtocols != null) {
28N/A List<String> thatEnabledProtocolsList =
28N/A Arrays.asList(that.enabledProtocols);
0N/A if (!enabledProtocolsList.equals(thatEnabledProtocolsList))
0N/A return false;
0N/A }
0N/A
0N/A return true;
0N/A }
0N/A
0N/A /**
0N/A * <p>Returns a hash code value for this
0N/A * <code>SslRMIServerSocketFactory</code>.</p>
0N/A *
0N/A * @return a hash code value for this
0N/A * <code>SslRMIServerSocketFactory</code>.
0N/A */
0N/A public int hashCode() {
0N/A return getClass().hashCode() +
0N/A (context == null ? 0 : context.hashCode()) +
0N/A (needClientAuth ? Boolean.TRUE.hashCode() : Boolean.FALSE.hashCode()) +
0N/A (enabledCipherSuites == null ? 0 : enabledCipherSuitesList.hashCode()) +
0N/A (enabledProtocols == null ? 0 : enabledProtocolsList.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 SslRMIServerSocketFactory
0N/A // factories with different underlying SSLSocketFactory objects
0N/A // using different keystores and truststores, he/she can always
0N/A // use the constructor that takes an SSLContext as input.
0N/A //
0N/A private static SSLSocketFactory defaultSSLSocketFactory = null;
0N/A
0N/A private static synchronized SSLSocketFactory getDefaultSSLSocketFactory() {
0N/A if (defaultSSLSocketFactory == null)
0N/A defaultSSLSocketFactory =
0N/A (SSLSocketFactory) SSLSocketFactory.getDefault();
0N/A return defaultSSLSocketFactory;
0N/A }
0N/A
0N/A private final String[] enabledCipherSuites;
0N/A private final String[] enabledProtocols;
0N/A private final boolean needClientAuth;
28N/A private List<String> enabledCipherSuitesList;
28N/A private List<String> enabledProtocolsList;
0N/A private SSLContext context;
0N/A}