2
|
1 |
/*
|
7668
|
2 |
* Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
|
2
|
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 |
*
|
5506
|
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.
|
2
|
22 |
*/
|
|
23 |
|
|
24 |
/*
|
|
25 |
* @test
|
|
26 |
* @bug 4390659
|
7043
|
27 |
* @run main/othervm -Djavax.net.debug=all ClientModeClientAuth
|
2
|
28 |
* @summary setNeedClientAuth() isn't working after a handshaker is established
|
|
29 |
* @author Brad Wetmore
|
|
30 |
*/
|
|
31 |
|
|
32 |
import java.io.*;
|
|
33 |
import java.net.*;
|
|
34 |
import javax.net.ssl.*;
|
|
35 |
import java.security.cert.*;
|
|
36 |
|
|
37 |
public class ClientModeClientAuth {
|
|
38 |
|
|
39 |
/*
|
|
40 |
* =============================================================
|
|
41 |
* Set the various variables needed for the tests, then
|
|
42 |
* specify what tests to run on each side.
|
|
43 |
*/
|
|
44 |
|
|
45 |
/*
|
|
46 |
* Should we run the client or server in a separate thread?
|
|
47 |
* Both sides can throw exceptions, but do you have a preference
|
|
48 |
* as to which side should be the main thread.
|
|
49 |
*/
|
|
50 |
static boolean separateServerThread = false;
|
|
51 |
|
|
52 |
/*
|
|
53 |
* Where do we find the keystores?
|
|
54 |
*/
|
|
55 |
static String pathToStores = "../../../../../../../etc";
|
|
56 |
static String keyStoreFile = "keystore";
|
|
57 |
static String trustStoreFile = "truststore";
|
|
58 |
static String passwd = "passphrase";
|
|
59 |
|
|
60 |
/*
|
|
61 |
* Is the server ready to serve?
|
|
62 |
*/
|
|
63 |
volatile static boolean serverReady = false;
|
|
64 |
|
|
65 |
/*
|
|
66 |
* Define the server side of the test.
|
|
67 |
*/
|
|
68 |
void doServerSide() throws Exception {
|
|
69 |
|
|
70 |
ServerSocket serverSocket = null;
|
|
71 |
serverSocket = new ServerSocket(serverPort);
|
|
72 |
serverPort = serverSocket.getLocalPort();
|
|
73 |
|
|
74 |
/*
|
|
75 |
* Signal Client, we're ready for his connect.
|
|
76 |
*/
|
|
77 |
serverReady = true;
|
|
78 |
|
|
79 |
Socket socket = serverSocket.accept();
|
|
80 |
OutputStream out = socket.getOutputStream();
|
|
81 |
InputStream in = socket.getInputStream();
|
|
82 |
|
|
83 |
/*
|
|
84 |
* send data to make sure we are ok.
|
|
85 |
*/
|
|
86 |
out.write(85);
|
|
87 |
out.flush();
|
|
88 |
in.read();
|
|
89 |
|
|
90 |
SSLSocketFactory sslsf =
|
|
91 |
(SSLSocketFactory) SSLSocketFactory.getDefault();
|
|
92 |
SSLSocket sslSocket =
|
|
93 |
(SSLSocket) sslsf.createSocket(
|
|
94 |
socket, socket.getInetAddress().getHostName(),
|
|
95 |
socket.getPort(), true);
|
|
96 |
|
|
97 |
sslSocket.setUseClientMode(false);
|
|
98 |
sslSocket.setNeedClientAuth(true);
|
|
99 |
|
|
100 |
InputStream sslIS = sslSocket.getInputStream();
|
|
101 |
OutputStream sslOS = sslSocket.getOutputStream();
|
|
102 |
|
|
103 |
sslOS.write(85);
|
|
104 |
sslOS.flush();
|
|
105 |
sslIS.read();
|
|
106 |
|
|
107 |
System.out.println("About to get PeerCertificates");
|
|
108 |
Certificate[] certs =
|
|
109 |
sslSocket.getSession().getPeerCertificates();
|
|
110 |
if (certs[0] instanceof X509Certificate) {
|
|
111 |
System.out.println("Peer: " +
|
|
112 |
((X509Certificate)certs[0]).getSubjectDN());
|
|
113 |
}
|
|
114 |
|
|
115 |
sslIS.close();
|
|
116 |
sslOS.close();
|
|
117 |
sslSocket.close();
|
|
118 |
}
|
|
119 |
|
|
120 |
/*
|
|
121 |
* Define the client side of the test.
|
|
122 |
*/
|
|
123 |
void doClientSide() throws Exception {
|
|
124 |
|
|
125 |
/*
|
|
126 |
* Wait for host to set up his port.
|
|
127 |
*/
|
|
128 |
while (!serverReady) {
|
|
129 |
Thread.sleep(50);
|
|
130 |
}
|
|
131 |
|
|
132 |
Socket socket = new Socket("localhost", serverPort);
|
|
133 |
InputStream in = socket.getInputStream();
|
|
134 |
OutputStream out = socket.getOutputStream();
|
|
135 |
|
|
136 |
in.read();
|
|
137 |
out.write(280);
|
|
138 |
out.flush();
|
|
139 |
|
|
140 |
SSLSocketFactory sslsf =
|
|
141 |
(SSLSocketFactory) SSLSocketFactory.getDefault();
|
|
142 |
|
|
143 |
SSLSocket sslSocket = (SSLSocket)
|
|
144 |
sslsf.createSocket(socket, socket.getInetAddress().getHostName(),
|
|
145 |
socket.getPort(), true);
|
|
146 |
|
|
147 |
sslSocket.setUseClientMode(true);
|
|
148 |
|
|
149 |
InputStream sslIS = sslSocket.getInputStream();
|
|
150 |
OutputStream sslOS = sslSocket.getOutputStream();
|
|
151 |
|
|
152 |
sslIS.read();
|
|
153 |
sslOS.write(280);
|
|
154 |
sslOS.flush();
|
|
155 |
|
|
156 |
sslIS.close();
|
|
157 |
sslOS.close();
|
|
158 |
sslSocket.close();
|
|
159 |
}
|
|
160 |
|
|
161 |
/*
|
|
162 |
* =============================================================
|
|
163 |
* The remainder is just support stuff
|
|
164 |
*/
|
|
165 |
|
|
166 |
// use any free port by default
|
|
167 |
volatile int serverPort = 0;
|
|
168 |
|
|
169 |
volatile Exception serverException = null;
|
|
170 |
volatile Exception clientException = null;
|
|
171 |
|
|
172 |
public static void main(String[] args) throws Exception {
|
|
173 |
String keyFilename =
|
|
174 |
System.getProperty("test.src", "./") + "/" + pathToStores +
|
|
175 |
"/" + keyStoreFile;
|
|
176 |
String trustFilename =
|
|
177 |
System.getProperty("test.src", "./") + "/" + pathToStores +
|
|
178 |
"/" + trustStoreFile;
|
|
179 |
|
|
180 |
System.setProperty("javax.net.ssl.keyStore", keyFilename);
|
|
181 |
System.setProperty("javax.net.ssl.keyStorePassword", passwd);
|
|
182 |
System.setProperty("javax.net.ssl.trustStore", trustFilename);
|
|
183 |
System.setProperty("javax.net.ssl.trustStorePassword", passwd);
|
|
184 |
|
|
185 |
/*
|
|
186 |
* Start the tests.
|
|
187 |
*/
|
|
188 |
new ClientModeClientAuth();
|
|
189 |
}
|
|
190 |
|
|
191 |
Thread clientThread = null;
|
|
192 |
Thread serverThread = null;
|
|
193 |
|
|
194 |
ClientModeClientAuth() throws Exception {
|
|
195 |
if (separateServerThread) {
|
|
196 |
startServer(true);
|
|
197 |
startClient(false);
|
|
198 |
} else {
|
|
199 |
startClient(true);
|
|
200 |
startServer(false);
|
|
201 |
}
|
|
202 |
|
|
203 |
/*
|
|
204 |
* Wait for other side to close down.
|
|
205 |
*/
|
|
206 |
if (separateServerThread) {
|
|
207 |
serverThread.join();
|
|
208 |
} else {
|
|
209 |
clientThread.join();
|
|
210 |
}
|
|
211 |
|
|
212 |
/*
|
|
213 |
* When we get here, the test is pretty much over.
|
|
214 |
*
|
|
215 |
* If the main thread excepted, that propagates back
|
|
216 |
* immediately. If the other thread threw an exception, we
|
|
217 |
* should report back.
|
|
218 |
*/
|
|
219 |
if (serverException != null)
|
|
220 |
throw serverException;
|
|
221 |
if (clientException != null)
|
|
222 |
throw clientException;
|
|
223 |
}
|
|
224 |
|
|
225 |
void startServer(boolean newThread) throws Exception {
|
|
226 |
if (newThread) {
|
|
227 |
serverThread = new Thread() {
|
|
228 |
public void run() {
|
|
229 |
try {
|
|
230 |
doServerSide();
|
|
231 |
} catch (Exception e) {
|
|
232 |
/*
|
|
233 |
* Our server thread just died.
|
|
234 |
*
|
|
235 |
* Release the client, if not active already...
|
|
236 |
*/
|
|
237 |
System.out.println("Server died...");
|
|
238 |
serverReady = true;
|
|
239 |
serverException = e;
|
|
240 |
}
|
|
241 |
}
|
|
242 |
};
|
|
243 |
serverThread.start();
|
|
244 |
} else {
|
|
245 |
doServerSide();
|
|
246 |
}
|
|
247 |
}
|
|
248 |
|
|
249 |
void startClient(boolean newThread) throws Exception {
|
|
250 |
if (newThread) {
|
|
251 |
clientThread = new Thread() {
|
|
252 |
public void run() {
|
|
253 |
try {
|
|
254 |
doClientSide();
|
|
255 |
} catch (Exception e) {
|
|
256 |
/*
|
|
257 |
* Our client thread just died.
|
|
258 |
*/
|
|
259 |
System.out.println("Client died...");
|
|
260 |
clientException = e;
|
|
261 |
}
|
|
262 |
}
|
|
263 |
};
|
|
264 |
clientThread.start();
|
|
265 |
} else {
|
|
266 |
doClientSide();
|
|
267 |
}
|
|
268 |
}
|
|
269 |
}
|