1/*
2 * Copyright (c) 2004, 2007, 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
24import java.io.IOException;
25import java.io.File;
26import java.io.Serializable;
27import java.net.ServerSocket;
28import java.net.Socket;
29import java.rmi.Remote;
30import java.rmi.RemoteException;
31import java.rmi.server.RMIClientSocketFactory;
32import java.rmi.server.RMIServerSocketFactory;
33import java.rmi.server.UnicastRemoteObject;
34import javax.net.ssl.SSLContext;
35import javax.rmi.ssl.SslRMIClientSocketFactory;
36import javax.rmi.ssl.SslRMIServerSocketFactory;
37
38public class SSLSocketParametersTest implements Serializable {
39
40    public interface Hello extends Remote {
41        public String sayHello() throws RemoteException;
42    }
43
44    public class HelloImpl extends UnicastRemoteObject implements Hello {
45
46        public HelloImpl(int port,
47                         RMIClientSocketFactory csf,
48                         RMIServerSocketFactory ssf)
49            throws RemoteException {
50            super(port, csf, ssf);
51        }
52
53        public String sayHello() {
54            return "Hello World!";
55        }
56
57        public Remote runServer() throws IOException {
58            System.out.println("Inside HelloImpl::runServer");
59            // Get a remote stub for this RMI object
60            //
61            Remote stub = toStub(this);
62            System.out.println("Stub = " + stub);
63            return stub;
64        }
65    }
66
67    public class HelloClient {
68
69        public void runClient(Remote stub) throws IOException {
70            System.out.println("Inside HelloClient::runClient");
71            // "obj" is the identifier that we'll use to refer
72            // to the remote object that implements the "Hello"
73            // interface
74            Hello obj = (Hello) stub;
75            String message = obj.sayHello();
76            System.out.println(message);
77        }
78    }
79
80    public class ClientFactory extends SslRMIClientSocketFactory {
81
82        public ClientFactory() {
83            super();
84        }
85
86        public Socket createSocket(String host, int port) throws IOException {
87            System.out.println("ClientFactory::Calling createSocket(" +
88                               host + "," + port + ")");
89            return super.createSocket(host, port);
90        }
91    }
92
93    public class ServerFactory extends SslRMIServerSocketFactory {
94
95        public ServerFactory() {
96            super();
97        }
98
99        public ServerFactory(String[] ciphers,
100                             String[] protocols,
101                             boolean need) {
102            super(ciphers, protocols, need);
103        }
104
105        public ServerFactory(SSLContext context,
106                             String[] ciphers,
107                             String[] protocols,
108                             boolean need) {
109            super(context, ciphers, protocols, need);
110        }
111
112        public ServerSocket createServerSocket(int port) throws IOException {
113            System.out.println("ServerFactory::Calling createServerSocket(" +
114                               port + ")");
115            return super.createServerSocket(port);
116        }
117    }
118
119    public void runTest(String[] args) {
120
121        int test = Integer.parseInt(args[0]);
122
123        String msg1 = "Running SSLSocketParametersTest [" + test + "]";
124        String msg2 = "SSLSocketParametersTest [" + test + "] PASSED!";
125        String msg3 = "SSLSocketParametersTest [" + test + "] FAILED!";
126
127        switch (test) {
128        case 1: /* default constructor - default config */
129            System.out.println(msg1);
130            try {
131                HelloImpl server = new HelloImpl(
132                          0,
133                          new ClientFactory(),
134                          new ServerFactory());
135                Remote stub = server.runServer();
136                HelloClient client = new HelloClient();
137                client.runClient(stub);
138                System.out.println(msg2);
139            } catch (Exception e) {
140                System.out.println(msg3 + " Exception: " + e.toString());
141                e.printStackTrace(System.out);
142                System.exit(1);
143            }
144            break;
145        case 2: /* non-default constructor - default config */
146            System.out.println(msg1);
147            try {
148                HelloImpl server = new HelloImpl(
149                          0,
150                          new ClientFactory(),
151                          new ServerFactory(null,
152                                            null,
153                                            false));
154                Remote stub = server.runServer();
155                HelloClient client = new HelloClient();
156                client.runClient(stub);
157                System.out.println(msg2);
158            } catch (Exception e) {
159                System.out.println(msg3 + " Exception: " + e.toString());
160                e.printStackTrace(System.out);
161                System.exit(1);
162            }
163            break;
164        case 3: /* needClientAuth=true */
165            System.out.println(msg1);
166            try {
167                HelloImpl server = new HelloImpl(
168                          0,
169                          new ClientFactory(),
170                          new ServerFactory(null,
171                                            null,
172                                            null,
173                                            true));
174                Remote stub = server.runServer();
175                HelloClient client = new HelloClient();
176                client.runClient(stub);
177                System.out.println(msg2);
178            } catch (Exception e) {
179                System.out.println(msg3 + " Exception: " + e.toString());
180                e.printStackTrace(System.out);
181                System.exit(1);
182            }
183            break;
184        case 4: /* server side dummy_ciphersuite */
185            System.out.println(msg1);
186            try {
187                HelloImpl server = new HelloImpl(
188                          0,
189                          new ClientFactory(),
190                          new ServerFactory(SSLContext.getDefault(),
191                                            new String[] {"dummy_ciphersuite"},
192                                            null,
193                                            false));
194                Remote stub = server.runServer();
195                HelloClient client = new HelloClient();
196                client.runClient(stub);
197                System.out.println(msg3);
198                System.exit(1);
199            } catch (Exception e) {
200                System.out.println(msg2 + " Exception: " + e.toString());
201                System.exit(0);
202            }
203            break;
204        case 5: /* server side dummy_protocol */
205            System.out.println(msg1);
206            try {
207                HelloImpl server = new HelloImpl(
208                          0,
209                          new ClientFactory(),
210                          new ServerFactory(null,
211                                            new String[] {"dummy_protocol"},
212                                            false));
213                Remote stub = server.runServer();
214                HelloClient client = new HelloClient();
215                client.runClient(stub);
216                System.out.println(msg3);
217                System.exit(1);
218            } catch (Exception e) {
219                System.out.println(msg2 + " Exception: " + e.toString());
220                System.exit(0);
221            }
222            break;
223        case 6: /* client side dummy_ciphersuite */
224            System.out.println(msg1);
225            try {
226                System.setProperty("javax.rmi.ssl.client.enabledCipherSuites",
227                                   "dummy_ciphersuite");
228                HelloImpl server = new HelloImpl(
229                          0,
230                          new ClientFactory(),
231                          new ServerFactory());
232                Remote stub = server.runServer();
233                HelloClient client = new HelloClient();
234                client.runClient(stub);
235                System.out.println(msg3);
236                System.exit(1);
237            } catch (Exception e) {
238                System.out.println(msg2 + " Exception: " + e.toString());
239                System.exit(0);
240            }
241            break;
242        case 7: /* client side dummy_protocol */
243            System.out.println(msg1);
244            try {
245                System.setProperty("javax.rmi.ssl.client.enabledProtocols",
246                                   "dummy_protocol");
247                HelloImpl server = new HelloImpl(
248                          0,
249                          new ClientFactory(),
250                          new ServerFactory());
251                Remote stub = server.runServer();
252                HelloClient client = new HelloClient();
253                client.runClient(stub);
254                System.out.println(msg3);
255                System.exit(1);
256            } catch (Exception e) {
257                System.out.println(msg2 + " Exception: " + e.toString());
258                System.exit(0);
259            }
260            break;
261        default:
262            throw new IllegalArgumentException("invalid test number");
263        }
264    }
265
266    public static void main(String[] args) {
267        // Set keystore properties (server-side)
268        //
269        final String keystore = System.getProperty("test.src") +
270            File.separator + "keystore";
271        System.out.println("KeyStore = " + keystore);
272        System.setProperty("javax.net.ssl.keyStore", keystore);
273        System.setProperty("javax.net.ssl.keyStorePassword", "password");
274
275        // Set truststore properties (client-side)
276        //
277        final String truststore = System.getProperty("test.src") +
278            File.separator + "truststore";
279        System.out.println("TrustStore = " + truststore);
280        System.setProperty("javax.net.ssl.trustStore", truststore);
281        System.setProperty("javax.net.ssl.trustStorePassword", "trustword");
282
283        // Run test
284        //
285        SSLSocketParametersTest test = new SSLSocketParametersTest();
286        test.runTest(args);
287        System.exit(0);
288    }
289}
290