1 /*
  2  * Copyright (c) 2004, 2023, Oracle and/or its affiliates. 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.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 /*
 25  * @test
 26  * @bug 5016500
 27  * @library /test/lib/
 28  * @summary Test SslRmi[Client|Server]SocketFactory SSL socket parameters.
 29  * @run main/othervm SSLSocketParametersTest 1
 30  * @run main/othervm SSLSocketParametersTest 2
 31  * @run main/othervm SSLSocketParametersTest 3
 32  * @run main/othervm SSLSocketParametersTest 4
 33  * @run main/othervm SSLSocketParametersTest 5
 34  * @run main/othervm SSLSocketParametersTest 6
 35  * @run main/othervm SSLSocketParametersTest 7
 36  */
 37 import jdk.test.lib.Asserts;
 38 
 39 import java.io.IOException;
 40 import java.io.File;
 41 import java.io.Serializable;
 42 import java.lang.ref.Reference;
 43 import java.rmi.ConnectIOException;
 44 import java.rmi.Remote;
 45 import java.rmi.RemoteException;
 46 import java.rmi.server.RMIServerSocketFactory;
 47 import java.rmi.server.UnicastRemoteObject;
 48 import javax.net.ssl.SSLContext;
 49 import javax.rmi.ssl.SslRMIClientSocketFactory;
 50 import javax.rmi.ssl.SslRMIServerSocketFactory;
 51 
 52 public class SSLSocketParametersTest implements Serializable {
 53 
 54     public interface Hello extends Remote {
 55         String sayHello() throws RemoteException;
 56     }
 57 
 58     public class HelloImpl implements Hello {
 59         public String sayHello() {
 60             return "Hello World!";
 61         }
 62     }
 63 
 64     public void testRmiCommunication(RMIServerSocketFactory serverSocketFactory) throws Exception {
 65         HelloImpl server = new HelloImpl();
 66         Hello stub = (Hello)UnicastRemoteObject.exportObject(server,
 67                 0, new SslRMIClientSocketFactory(), serverSocketFactory);
 68         try {
 69             String msg = stub.sayHello();
 70             Asserts.assertEquals("Hello World!", msg);
 71         } finally {
 72             Reference.reachabilityFence(server);
 73         }
 74     }
 75 
 76     private static void testSslServerSocketFactory(String[] cipherSuites, String[] protocol) throws Exception {
 77         new SslRMIServerSocketFactory(SSLContext.getDefault(),
 78                     cipherSuites, protocol, false);
 79     }
 80 
 81     public void runTest(int testNumber) throws Exception {
 82         System.out.println("Running test " + testNumber);
 83 
 84         switch (testNumber) {
 85             /* default constructor - default config */
 86             case 1 ->
 87                 testRmiCommunication(new SslRMIServerSocketFactory());
 88 
 89             /* non-default constructor - default config */
 90             case 2 ->
 91                 testRmiCommunication(new SslRMIServerSocketFactory(null, null, false));
 92 
 93             /* needClientAuth=true */
 94             case 3 ->
 95                 testRmiCommunication(new SslRMIServerSocketFactory(null, null, null, true));
 96 
 97             /* server side dummy_ciphersuite */
 98             case 4 -> {
 99                 Exception exc = Asserts.assertThrows(IllegalArgumentException.class,
100                         () -> testSslServerSocketFactory(new String[]{"dummy_ciphersuite"}, null));
101                 if (!exc.getMessage().toLowerCase().contains("unsupported ciphersuite")) {
102                     throw exc;
103                 }
104             }
105 
106             /* server side dummy_protocol */
107             case 5 -> {
108                 Exception thrown = Asserts.assertThrows(IllegalArgumentException.class,
109                         () -> testSslServerSocketFactory(null, new String[]{"dummy_protocol"}));
110                 if (!thrown.getMessage().toLowerCase().contains("unsupported protocol")) {
111                     throw thrown;
112                 }
113             }
114 
115             /* client side dummy_ciphersuite */
116             case 6 -> {
117                 System.setProperty("javax.rmi.ssl.client.enabledCipherSuites",
118                         "dummy_ciphersuite");
119                 Asserts.assertThrows(ConnectIOException.class,
120                         () -> testRmiCommunication(new SslRMIServerSocketFactory()));
121             }
122 
123             /* client side dummy_protocol */
124             case 7 -> {
125                 System.setProperty("javax.rmi.ssl.client.enabledProtocols",
126                         "dummy_protocol");
127                 Asserts.assertThrows(ConnectIOException.class,
128                         () -> testRmiCommunication(new SslRMIServerSocketFactory()));
129             }
130 
131             default ->
132                     throw new RuntimeException("Unknown test number: " + testNumber);
133         }
134     }
135 
136     public static void main(String[] args) throws Exception {
137         // Set keystore properties (server-side)
138         //
139         final String keystore = System.getProperty("test.src") +
140                 File.separator + "keystore";
141         System.out.println("KeyStore = " + keystore);
142         System.setProperty("javax.net.ssl.keyStore", keystore);
143         System.setProperty("javax.net.ssl.keyStorePassword", "password");
144 
145         // Set truststore properties (client-side)
146         //
147         final String truststore = System.getProperty("test.src") +
148                 File.separator + "truststore";
149         System.out.println("TrustStore = " + truststore);
150         System.setProperty("javax.net.ssl.trustStore", truststore);
151         System.setProperty("javax.net.ssl.trustStorePassword", "trustword");
152 
153         SSLSocketParametersTest test = new SSLSocketParametersTest();
154         test.runTest(Integer.parseInt(args[0]));
155     }
156 }