author | wetmore |
Fri, 11 May 2018 15:53:12 -0700 | |
branch | JDK-8145252-TLS13-branch |
changeset 56542 | 56aaa6cb3693 |
parent 47216 | 71c04702a3d5 |
child 51141 | 2dd2d73c52f6 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
23010
6dadb192ad81
8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents:
19830
diff
changeset
|
2 |
* Copyright (c) 2004, 2013, 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 |
||
19830
9c6ba80438f9
8024444: Change to use othervm mode of tests in SSLEngineImpl
xuelei
parents:
5506
diff
changeset
|
24 |
// |
9c6ba80438f9
8024444: Change to use othervm mode of tests in SSLEngineImpl
xuelei
parents:
5506
diff
changeset
|
25 |
// SunJSSE does not support dynamic system properties, no way to re-use |
9c6ba80438f9
8024444: Change to use othervm mode of tests in SSLEngineImpl
xuelei
parents:
5506
diff
changeset
|
26 |
// system properties in samevm/agentvm mode. |
9c6ba80438f9
8024444: Change to use othervm mode of tests in SSLEngineImpl
xuelei
parents:
5506
diff
changeset
|
27 |
// |
9c6ba80438f9
8024444: Change to use othervm mode of tests in SSLEngineImpl
xuelei
parents:
5506
diff
changeset
|
28 |
|
2 | 29 |
/* |
30 |
* @test |
|
31 |
* @bug 4980882 |
|
32 |
* @summary SSLEngine should enforce setUseClientMode |
|
19830
9c6ba80438f9
8024444: Change to use othervm mode of tests in SSLEngineImpl
xuelei
parents:
5506
diff
changeset
|
33 |
* @run main/othervm EngineEnforceUseClientMode |
2 | 34 |
* @author Brad R. Wetmore |
35 |
*/ |
|
36 |
||
37 |
import javax.net.ssl.*; |
|
38 |
import javax.net.ssl.SSLEngineResult.*; |
|
39 |
import java.io.*; |
|
40 |
import java.security.*; |
|
41 |
import java.nio.*; |
|
42 |
||
43 |
public class EngineEnforceUseClientMode { |
|
44 |
||
45 |
private static boolean debug = false; |
|
46 |
||
47 |
private SSLContext sslc; |
|
48 |
private SSLEngine ssle1; // client |
|
49 |
private SSLEngine ssle2; // server |
|
50 |
||
51 |
private SSLEngine ssle3; // server |
|
52 |
private SSLEngine ssle4; // server |
|
53 |
private SSLEngine ssle5; // server |
|
54 |
||
23052
241885315119
8032473: Restructure JSSE regression test hierarchy in jdk test
xuelei
parents:
23010
diff
changeset
|
55 |
private static String pathToStores = "../../../../javax/net/ssl/etc"; |
2 | 56 |
private static String keyStoreFile = "keystore"; |
57 |
private static String trustStoreFile = "truststore"; |
|
58 |
private static String passwd = "passphrase"; |
|
59 |
||
60 |
private static String keyFilename = |
|
61 |
System.getProperty("test.src", "./") + "/" + pathToStores + |
|
62 |
"/" + keyStoreFile; |
|
63 |
private static String trustFilename = |
|
64 |
System.getProperty("test.src", "./") + "/" + pathToStores + |
|
65 |
"/" + trustStoreFile; |
|
66 |
||
67 |
private ByteBuffer appOut1; // write side of ssle1 |
|
68 |
private ByteBuffer appIn1; // read side of ssle1 |
|
69 |
private ByteBuffer appOut2; // write side of ssle2 |
|
70 |
private ByteBuffer appIn2; // read side of ssle2 |
|
71 |
||
72 |
private ByteBuffer oneToTwo; // "reliable" transport ssle1->ssle2 |
|
73 |
private ByteBuffer twoToOne; // "reliable" transport ssle2->ssle1 |
|
74 |
||
75 |
/* |
|
76 |
* Majority of the test case is here, setup is done below. |
|
77 |
*/ |
|
78 |
private void createSSLEngines() throws Exception { |
|
79 |
ssle1 = sslc.createSSLEngine("client", 1); |
|
80 |
ssle1.setUseClientMode(true); |
|
81 |
||
82 |
ssle2 = sslc.createSSLEngine(); |
|
83 |
ssle2.setUseClientMode(false); |
|
84 |
ssle2.setNeedClientAuth(true); |
|
85 |
||
86 |
/* |
|
87 |
* Note, these are not initialized to client/server |
|
88 |
*/ |
|
89 |
ssle3 = sslc.createSSLEngine(); |
|
90 |
ssle4 = sslc.createSSLEngine(); |
|
91 |
ssle5 = sslc.createSSLEngine(); |
|
92 |
} |
|
93 |
||
94 |
private void runTest() throws Exception { |
|
95 |
||
96 |
createSSLEngines(); |
|
97 |
createBuffers(); |
|
98 |
||
99 |
/* |
|
100 |
* First try the engines with no client/server initialization |
|
101 |
* All should fail. |
|
102 |
*/ |
|
103 |
try { |
|
104 |
System.out.println("Testing wrap()"); |
|
105 |
ssle3.wrap(appOut1, oneToTwo); |
|
106 |
throw new RuntimeException( |
|
107 |
"wrap(): Didn't catch the exception properly"); |
|
108 |
} catch (IllegalStateException e) { |
|
109 |
System.out.println("Caught the correct exception."); |
|
110 |
oneToTwo.flip(); |
|
111 |
if (oneToTwo.hasRemaining()) { |
|
56542 | 112 |
throw new Exception("wrap generated data"); |
2 | 113 |
} |
114 |
oneToTwo.clear(); |
|
115 |
} |
|
116 |
||
117 |
try { |
|
118 |
System.out.println("Testing unwrap()"); |
|
119 |
ssle4.unwrap(oneToTwo, appIn1); |
|
120 |
throw new RuntimeException( |
|
121 |
"unwrap(): Didn't catch the exception properly"); |
|
122 |
} catch (IllegalStateException e) { |
|
123 |
System.out.println("Caught the correct exception."); |
|
56542 | 124 |
appIn1.flip(); |
125 |
if (appIn1.hasRemaining()) { |
|
126 |
throw new Exception("unwrap generated data"); |
|
2 | 127 |
} |
56542 | 128 |
appIn1.clear(); |
2 | 129 |
} |
130 |
||
131 |
try { |
|
132 |
System.out.println("Testing beginHandshake()"); |
|
133 |
ssle5.beginHandshake(); |
|
134 |
throw new RuntimeException( |
|
135 |
"unwrap(): Didn't catch the exception properly"); |
|
136 |
} catch (IllegalStateException e) { |
|
137 |
System.out.println("Caught the correct exception."); |
|
138 |
} |
|
139 |
||
140 |
boolean dataDone = false; |
|
141 |
||
142 |
SSLEngineResult result1; // ssle1's results from last operation |
|
143 |
SSLEngineResult result2; // ssle2's results from last operation |
|
144 |
||
145 |
while (!isEngineClosed(ssle1) || !isEngineClosed(ssle2)) { |
|
146 |
||
147 |
log("================"); |
|
148 |
||
149 |
result1 = ssle1.wrap(appOut1, oneToTwo); |
|
150 |
result2 = ssle2.wrap(appOut2, twoToOne); |
|
151 |
||
152 |
log("wrap1: " + result1); |
|
153 |
log("oneToTwo = " + oneToTwo); |
|
154 |
log(""); |
|
155 |
||
156 |
log("wrap2: " + result2); |
|
157 |
log("twoToOne = " + twoToOne); |
|
158 |
||
159 |
runDelegatedTasks(result1, ssle1); |
|
160 |
runDelegatedTasks(result2, ssle2); |
|
161 |
||
162 |
oneToTwo.flip(); |
|
163 |
twoToOne.flip(); |
|
164 |
||
165 |
log("----"); |
|
166 |
||
167 |
result1 = ssle1.unwrap(twoToOne, appIn1); |
|
168 |
result2 = ssle2.unwrap(oneToTwo, appIn2); |
|
169 |
||
170 |
log("unwrap1: " + result1); |
|
171 |
log("twoToOne = " + twoToOne); |
|
172 |
log(""); |
|
173 |
||
174 |
log("unwrap2: " + result2); |
|
175 |
log("oneToTwo = " + oneToTwo); |
|
176 |
||
177 |
runDelegatedTasks(result1, ssle1); |
|
178 |
runDelegatedTasks(result2, ssle2); |
|
179 |
||
180 |
oneToTwo.compact(); |
|
181 |
twoToOne.compact(); |
|
182 |
||
183 |
/* |
|
184 |
* If we've transfered all the data between app1 and app2, |
|
185 |
* we try to close and see what that gets us. |
|
186 |
*/ |
|
187 |
if (!dataDone && (appOut1.limit() == appIn2.position()) && |
|
188 |
(appOut2.limit() == appIn1.position())) { |
|
189 |
||
190 |
checkTransfer(appOut1, appIn2); |
|
191 |
checkTransfer(appOut2, appIn1); |
|
192 |
||
193 |
System.out.println("Try changing modes..."); |
|
194 |
try { |
|
56542 | 195 |
ssle2.setUseClientMode(true); |
2 | 196 |
throw new RuntimeException( |
197 |
"setUseClientMode(): " + |
|
198 |
"Didn't catch the exception properly"); |
|
199 |
} catch (IllegalArgumentException e) { |
|
200 |
System.out.println("Caught the correct exception."); |
|
201 |
} |
|
202 |
||
203 |
return; |
|
204 |
} |
|
205 |
} |
|
206 |
} |
|
207 |
||
208 |
public static void main(String args[]) throws Exception { |
|
209 |
||
210 |
EngineEnforceUseClientMode test; |
|
211 |
||
212 |
test = new EngineEnforceUseClientMode(); |
|
213 |
||
214 |
test.createSSLEngines(); |
|
215 |
||
216 |
test.runTest(); |
|
217 |
||
218 |
System.out.println("Test Passed."); |
|
219 |
} |
|
220 |
||
221 |
/* |
|
222 |
* ********************************************************** |
|
223 |
* Majority of the test case is above, below is just setup stuff |
|
224 |
* ********************************************************** |
|
225 |
*/ |
|
226 |
||
227 |
public EngineEnforceUseClientMode() throws Exception { |
|
228 |
sslc = getSSLContext(keyFilename, trustFilename); |
|
229 |
} |
|
230 |
||
231 |
/* |
|
232 |
* Create an initialized SSLContext to use for this test. |
|
233 |
*/ |
|
234 |
private SSLContext getSSLContext(String keyFile, String trustFile) |
|
235 |
throws Exception { |
|
236 |
||
237 |
KeyStore ks = KeyStore.getInstance("JKS"); |
|
238 |
KeyStore ts = KeyStore.getInstance("JKS"); |
|
239 |
||
240 |
char[] passphrase = "passphrase".toCharArray(); |
|
241 |
||
242 |
ks.load(new FileInputStream(keyFile), passphrase); |
|
243 |
ts.load(new FileInputStream(trustFile), passphrase); |
|
244 |
||
245 |
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509"); |
|
246 |
kmf.init(ks, passphrase); |
|
247 |
||
248 |
TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509"); |
|
249 |
tmf.init(ts); |
|
250 |
||
251 |
SSLContext sslCtx = SSLContext.getInstance("TLS"); |
|
252 |
||
253 |
sslCtx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null); |
|
254 |
||
255 |
return sslCtx; |
|
256 |
} |
|
257 |
||
258 |
private void createBuffers() { |
|
259 |
// Size the buffers as appropriate. |
|
260 |
||
261 |
SSLSession session = ssle1.getSession(); |
|
262 |
int appBufferMax = session.getApplicationBufferSize(); |
|
263 |
int netBufferMax = session.getPacketBufferSize(); |
|
264 |
||
265 |
appIn1 = ByteBuffer.allocateDirect(appBufferMax + 50); |
|
266 |
appIn2 = ByteBuffer.allocateDirect(appBufferMax + 50); |
|
267 |
||
268 |
oneToTwo = ByteBuffer.allocateDirect(netBufferMax); |
|
269 |
twoToOne = ByteBuffer.allocateDirect(netBufferMax); |
|
270 |
||
271 |
appOut1 = ByteBuffer.wrap("Hi Engine2, I'm SSLEngine1".getBytes()); |
|
272 |
appOut2 = ByteBuffer.wrap("Hello Engine1, I'm SSLEngine2".getBytes()); |
|
273 |
||
274 |
log("AppOut1 = " + appOut1); |
|
275 |
log("AppOut2 = " + appOut2); |
|
276 |
log(""); |
|
277 |
} |
|
278 |
||
279 |
private static void runDelegatedTasks(SSLEngineResult result, |
|
280 |
SSLEngine engine) throws Exception { |
|
281 |
||
282 |
if (result.getHandshakeStatus() == HandshakeStatus.NEED_TASK) { |
|
283 |
Runnable runnable; |
|
284 |
while ((runnable = engine.getDelegatedTask()) != null) { |
|
285 |
log("running delegated task..."); |
|
286 |
runnable.run(); |
|
287 |
} |
|
288 |
} |
|
289 |
} |
|
290 |
||
291 |
private static boolean isEngineClosed(SSLEngine engine) { |
|
292 |
return (engine.isOutboundDone() && engine.isInboundDone()); |
|
293 |
} |
|
294 |
||
295 |
private static void checkTransfer(ByteBuffer a, ByteBuffer b) |
|
296 |
throws Exception { |
|
297 |
a.flip(); |
|
298 |
b.flip(); |
|
299 |
||
300 |
if (!a.equals(b)) { |
|
301 |
throw new Exception("Data didn't transfer cleanly"); |
|
302 |
} else { |
|
303 |
log("Data transferred cleanly"); |
|
304 |
} |
|
305 |
||
56542 | 306 |
a.position(a.limit()); |
307 |
b.position(b.limit()); |
|
308 |
a.limit(a.capacity()); |
|
309 |
b.limit(b.capacity()); |
|
2 | 310 |
} |
311 |
||
312 |
private static void log(String str) { |
|
313 |
if (debug) { |
|
314 |
System.out.println(str); |
|
315 |
} |
|
316 |
} |
|
317 |
} |