1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
|
/*
* Copyright (c) 2004, 2007, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import java.io.IOException;
import java.io.File;
import java.io.Serializable;
import java.net.ServerSocket;
import java.net.Socket;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.server.RMIClientSocketFactory;
import java.rmi.server.RMIServerSocketFactory;
import java.rmi.server.UnicastRemoteObject;
import javax.net.ssl.SSLContext;
import javax.rmi.ssl.SslRMIClientSocketFactory;
import javax.rmi.ssl.SslRMIServerSocketFactory;
public class SSLSocketParametersTest implements Serializable {
public interface Hello extends Remote {
public String sayHello() throws RemoteException;
}
public class HelloImpl extends UnicastRemoteObject implements Hello {
public HelloImpl(int port,
RMIClientSocketFactory csf,
RMIServerSocketFactory ssf)
throws RemoteException {
super(port, csf, ssf);
}
public String sayHello() {
return "Hello World!";
}
public Remote runServer() throws IOException {
System.out.println("Inside HelloImpl::runServer");
// Get a remote stub for this RMI object
//
Remote stub = toStub(this);
System.out.println("Stub = " + stub);
return stub;
}
}
public class HelloClient {
public void runClient(Remote stub) throws IOException {
System.out.println("Inside HelloClient::runClient");
// "obj" is the identifier that we'll use to refer
// to the remote object that implements the "Hello"
// interface
Hello obj = (Hello) stub;
String message = obj.sayHello();
System.out.println(message);
}
}
public class ClientFactory extends SslRMIClientSocketFactory {
public ClientFactory() {
super();
}
public Socket createSocket(String host, int port) throws IOException {
System.out.println("ClientFactory::Calling createSocket(" +
host + "," + port + ")");
return super.createSocket(host, port);
}
}
public class ServerFactory extends SslRMIServerSocketFactory {
public ServerFactory() {
super();
}
public ServerFactory(String[] ciphers,
String[] protocols,
boolean need) {
super(ciphers, protocols, need);
}
public ServerFactory(SSLContext context,
String[] ciphers,
String[] protocols,
boolean need) {
super(context, ciphers, protocols, need);
}
public ServerSocket createServerSocket(int port) throws IOException {
System.out.println("ServerFactory::Calling createServerSocket(" +
port + ")");
return super.createServerSocket(port);
}
}
public void runTest(String[] args) {
int test = Integer.parseInt(args[0]);
String msg1 = "Running SSLSocketParametersTest [" + test + "]";
String msg2 = "SSLSocketParametersTest [" + test + "] PASSED!";
String msg3 = "SSLSocketParametersTest [" + test + "] FAILED!";
switch (test) {
case 1: /* default constructor - default config */
System.out.println(msg1);
try {
HelloImpl server = new HelloImpl(
0,
new ClientFactory(),
new ServerFactory());
Remote stub = server.runServer();
HelloClient client = new HelloClient();
client.runClient(stub);
System.out.println(msg2);
} catch (Exception e) {
System.out.println(msg3 + " Exception: " + e.toString());
e.printStackTrace(System.out);
System.exit(1);
}
break;
case 2: /* non-default constructor - default config */
System.out.println(msg1);
try {
HelloImpl server = new HelloImpl(
0,
new ClientFactory(),
new ServerFactory(null,
null,
false));
Remote stub = server.runServer();
HelloClient client = new HelloClient();
client.runClient(stub);
System.out.println(msg2);
} catch (Exception e) {
System.out.println(msg3 + " Exception: " + e.toString());
e.printStackTrace(System.out);
System.exit(1);
}
break;
case 3: /* needClientAuth=true */
System.out.println(msg1);
try {
HelloImpl server = new HelloImpl(
0,
new ClientFactory(),
new ServerFactory(null,
null,
null,
true));
Remote stub = server.runServer();
HelloClient client = new HelloClient();
client.runClient(stub);
System.out.println(msg2);
} catch (Exception e) {
System.out.println(msg3 + " Exception: " + e.toString());
e.printStackTrace(System.out);
System.exit(1);
}
break;
case 4: /* server side dummy_ciphersuite */
System.out.println(msg1);
try {
HelloImpl server = new HelloImpl(
0,
new ClientFactory(),
new ServerFactory(SSLContext.getDefault(),
new String[] {"dummy_ciphersuite"},
null,
false));
Remote stub = server.runServer();
HelloClient client = new HelloClient();
client.runClient(stub);
System.out.println(msg3);
System.exit(1);
} catch (Exception e) {
System.out.println(msg2 + " Exception: " + e.toString());
System.exit(0);
}
break;
case 5: /* server side dummy_protocol */
System.out.println(msg1);
try {
HelloImpl server = new HelloImpl(
0,
new ClientFactory(),
new ServerFactory(null,
new String[] {"dummy_protocol"},
false));
Remote stub = server.runServer();
HelloClient client = new HelloClient();
client.runClient(stub);
System.out.println(msg3);
System.exit(1);
} catch (Exception e) {
System.out.println(msg2 + " Exception: " + e.toString());
System.exit(0);
}
break;
case 6: /* client side dummy_ciphersuite */
System.out.println(msg1);
try {
System.setProperty("javax.rmi.ssl.client.enabledCipherSuites",
"dummy_ciphersuite");
HelloImpl server = new HelloImpl(
0,
new ClientFactory(),
new ServerFactory());
Remote stub = server.runServer();
HelloClient client = new HelloClient();
client.runClient(stub);
System.out.println(msg3);
System.exit(1);
} catch (Exception e) {
System.out.println(msg2 + " Exception: " + e.toString());
System.exit(0);
}
break;
case 7: /* client side dummy_protocol */
System.out.println(msg1);
try {
System.setProperty("javax.rmi.ssl.client.enabledProtocols",
"dummy_protocol");
HelloImpl server = new HelloImpl(
0,
new ClientFactory(),
new ServerFactory());
Remote stub = server.runServer();
HelloClient client = new HelloClient();
client.runClient(stub);
System.out.println(msg3);
System.exit(1);
} catch (Exception e) {
System.out.println(msg2 + " Exception: " + e.toString());
System.exit(0);
}
break;
default:
throw new IllegalArgumentException("invalid test number");
}
}
public static void main(String[] args) {
// Set keystore properties (server-side)
//
final String keystore = System.getProperty("test.src") +
File.separator + "keystore";
System.out.println("KeyStore = " + keystore);
System.setProperty("javax.net.ssl.keyStore", keystore);
System.setProperty("javax.net.ssl.keyStorePassword", "password");
// Set truststore properties (client-side)
//
final String truststore = System.getProperty("test.src") +
File.separator + "truststore";
System.out.println("TrustStore = " + truststore);
System.setProperty("javax.net.ssl.trustStore", truststore);
System.setProperty("javax.net.ssl.trustStorePassword", "trustword");
// Run test
//
SSLSocketParametersTest test = new SSLSocketParametersTest();
test.runTest(args);
System.exit(0);
}
}
|