author | lana |
Thu, 11 Sep 2014 14:28:19 -0700 | |
changeset 26520 | abaac15e3bd1 |
parent 23052 | 241885315119 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
7043 | 2 |
* Copyright (c) 2003, 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 4495742 |
|
27 |
* @summary Add non-blocking SSL/TLS functionality, usable with any |
|
28 |
* I/O abstraction |
|
7043 | 29 |
* @ignore JSSE supported cipher suites are changed with CR 6916074, |
30 |
* need to update this test case in JDK 7 soon |
|
2 | 31 |
* |
32 |
* This is intended to test many of the basic API calls to the SSLEngine |
|
33 |
* interface. This doesn't really exercise much of the SSL code. |
|
34 |
* |
|
35 |
* @author Brad Wetmore |
|
36 |
*/ |
|
37 |
||
38 |
import java.security.*; |
|
39 |
import java.io.*; |
|
40 |
import java.nio.*; |
|
41 |
import javax.net.ssl.*; |
|
42 |
import javax.net.ssl.SSLEngineResult.*; |
|
43 |
||
44 |
public class Basics { |
|
45 |
||
23052
241885315119
8032473: Restructure JSSE regression test hierarchy in jdk test
xuelei
parents:
7043
diff
changeset
|
46 |
private static String pathToStores = "../etc"; |
2 | 47 |
private static String keyStoreFile = "keystore"; |
48 |
private static String trustStoreFile = "truststore"; |
|
49 |
private static String passwd = "passphrase"; |
|
50 |
||
51 |
private static String keyFilename = |
|
52 |
System.getProperty("test.src", "./") + "/" + pathToStores + |
|
53 |
"/" + keyStoreFile; |
|
54 |
private static String trustFilename = |
|
55 |
System.getProperty("test.src", "./") + "/" + pathToStores + |
|
56 |
"/" + trustStoreFile; |
|
57 |
||
58 |
public static void main(String args[]) throws Exception { |
|
59 |
||
60 |
KeyStore ks = KeyStore.getInstance("JKS"); |
|
61 |
KeyStore ts = KeyStore.getInstance("JKS"); |
|
62 |
char[] passphrase = "passphrase".toCharArray(); |
|
63 |
||
64 |
ks.load(new FileInputStream(keyFilename), passphrase); |
|
65 |
ts.load(new FileInputStream(trustFilename), passphrase); |
|
66 |
||
67 |
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509"); |
|
68 |
kmf.init(ks, passphrase); |
|
69 |
||
70 |
TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509"); |
|
71 |
tmf.init(ks); |
|
72 |
||
73 |
SSLContext sslCtx = SSLContext.getInstance("TLS"); |
|
74 |
||
75 |
sslCtx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null); |
|
76 |
||
77 |
SSLEngine ssle = sslCtx.createSSLEngine(); |
|
78 |
||
79 |
System.out.println(ssle); |
|
80 |
||
81 |
String [] suites = ssle.getSupportedCipherSuites(); |
|
82 |
String secondSuite = suites[1]; |
|
83 |
String [] oneSuites = new String [] { secondSuite }; |
|
84 |
||
85 |
printStrings("Supported Ciphersuites", suites); |
|
86 |
printStrings("Enabled Ciphersuites", ssle.getEnabledCipherSuites()); |
|
87 |
ssle.setEnabledCipherSuites(oneSuites); |
|
88 |
printStrings("Set Ciphersuites", ssle.getEnabledCipherSuites()); |
|
89 |
||
90 |
suites = ssle.getEnabledCipherSuites(); |
|
91 |
if ((ssle.getEnabledCipherSuites().length != 1) || |
|
92 |
!(suites[0].equals(secondSuite))) { |
|
93 |
throw new Exception("set ciphers not what was expected"); |
|
94 |
} |
|
95 |
||
96 |
System.out.println(); |
|
97 |
||
98 |
String [] protocols = ssle.getSupportedProtocols(); |
|
99 |
String secondProtocol = protocols[1]; |
|
100 |
String [] oneProtocols = new String [] { protocols[1] }; |
|
101 |
||
102 |
printStrings("Supported Protocols", protocols); |
|
103 |
printStrings("Enabled Protocols", ssle.getEnabledProtocols()); |
|
104 |
ssle.setEnabledProtocols(oneProtocols); |
|
105 |
printStrings("Set Protocols", ssle.getEnabledProtocols()); |
|
106 |
||
107 |
protocols = ssle.getEnabledProtocols(); |
|
108 |
if ((ssle.getEnabledProtocols().length != 1) || |
|
109 |
!(protocols[0].equals(secondProtocol))) { |
|
110 |
throw new Exception("set protocols not what was expected"); |
|
111 |
} |
|
112 |
||
113 |
System.out.println("Checking get/setUseClientMode"); |
|
114 |
||
115 |
ssle.setUseClientMode(true); |
|
116 |
if (ssle.getUseClientMode() != true) { |
|
117 |
throw new Exception("set/getUseClientMode false"); |
|
118 |
} |
|
119 |
||
120 |
ssle.setUseClientMode(false); |
|
121 |
if (ssle.getUseClientMode() != false) { |
|
122 |
throw new Exception("set/getUseClientMode true"); |
|
123 |
} |
|
124 |
||
125 |
||
126 |
System.out.println("Checking get/setClientAuth"); |
|
127 |
||
128 |
ssle.setNeedClientAuth(false); |
|
129 |
if (ssle.getNeedClientAuth() != false) { |
|
130 |
throw new Exception("set/getNeedClientAuth true"); |
|
131 |
} |
|
132 |
||
133 |
ssle.setNeedClientAuth(true); |
|
134 |
if (ssle.getNeedClientAuth() != true) { |
|
135 |
throw new Exception("set/getNeedClientAuth false"); |
|
136 |
} |
|
137 |
||
138 |
ssle.setWantClientAuth(true); |
|
139 |
||
140 |
if (ssle.getNeedClientAuth() == true) { |
|
141 |
throw new Exception("set/getWantClientAuth need = true"); |
|
142 |
} |
|
143 |
||
144 |
if (ssle.getWantClientAuth() != true) { |
|
145 |
throw new Exception("set/getNeedClientAuth false"); |
|
146 |
} |
|
147 |
||
148 |
ssle.setWantClientAuth(false); |
|
149 |
if (ssle.getWantClientAuth() != false) { |
|
150 |
throw new Exception("set/getNeedClientAuth true"); |
|
151 |
} |
|
152 |
||
153 |
/* |
|
154 |
* Reset back to client mode |
|
155 |
*/ |
|
156 |
ssle.setUseClientMode(true); |
|
157 |
||
158 |
System.out.println("checking session creation"); |
|
159 |
||
160 |
ssle.setEnableSessionCreation(false); |
|
161 |
if (ssle.getEnableSessionCreation() != false) { |
|
162 |
throw new Exception("set/getSessionCreation true"); |
|
163 |
} |
|
164 |
||
165 |
ssle.setEnableSessionCreation(true); |
|
166 |
if (ssle.getEnableSessionCreation() != true) { |
|
167 |
throw new Exception("set/getSessionCreation false"); |
|
168 |
} |
|
169 |
||
170 |
/* Checking for overflow wrap/unwrap() */ |
|
171 |
ByteBuffer smallBB = ByteBuffer.allocate(10); |
|
172 |
||
173 |
if (ssle.wrap(smallBB, smallBB).getStatus() != |
|
174 |
Status.BUFFER_OVERFLOW) { |
|
175 |
throw new Exception("wrap should have overflowed"); |
|
176 |
} |
|
177 |
||
178 |
// For unwrap(), the BUFFER_OVERFLOW will not be generated |
|
179 |
// until received SSL/TLS application data. |
|
180 |
// Test test/javax/net/ssl/NewAPIs/SSLEngine/LargePacket will check |
|
181 |
// BUFFER_OVERFLOW/UNDERFLOW for both wrap() and unwrap(). |
|
182 |
// |
|
183 |
//if (ssle.unwrap(smallBB, smallBB).getStatus() != |
|
184 |
// Status.BUFFER_OVERFLOW) { |
|
185 |
// throw new Exception("unwrap should have overflowed"); |
|
186 |
//} |
|
187 |
||
188 |
SSLSession ssls = ssle.getSession(); |
|
189 |
||
190 |
ByteBuffer appBB = |
|
191 |
ByteBuffer.allocate(ssls.getApplicationBufferSize()); |
|
192 |
ByteBuffer netBB = |
|
193 |
ByteBuffer.allocate(ssls.getPacketBufferSize()); |
|
194 |
appBB.position(10); |
|
195 |
||
196 |
/* |
|
197 |
* start handshake, drain buffer |
|
198 |
*/ |
|
199 |
if (ssle.wrap(appBB, netBB).getHandshakeStatus() != |
|
200 |
HandshakeStatus.NEED_UNWRAP) { |
|
201 |
throw new Exception("initial client hello needs unwrap"); |
|
202 |
} |
|
203 |
||
204 |
/* Checking for overflow wrap/unwrap() */ |
|
205 |
||
206 |
if (ssle.wrap(appBB, netBB).getStatus() != |
|
207 |
Status.BUFFER_OVERFLOW) { |
|
208 |
throw new Exception("unwrap should have overflowed"); |
|
209 |
} |
|
210 |
||
211 |
ByteBuffer ro = appBB.asReadOnlyBuffer(); |
|
212 |
||
213 |
System.out.println("checking for wrap/unwrap on RO Buffers"); |
|
214 |
try { |
|
215 |
ssle.wrap(netBB, ro); |
|
216 |
throw new Exception("wrap wasn't ReadOnlyBufferException"); |
|
217 |
} catch (ReadOnlyBufferException e) { |
|
218 |
System.out.println("Caught the ReadOnlyBuffer: " + e); |
|
219 |
} |
|
220 |
||
221 |
try { |
|
222 |
ssle.unwrap(netBB, ro); |
|
223 |
throw new Exception("unwrap wasn't ReadOnlyBufferException"); |
|
224 |
} catch (ReadOnlyBufferException e) { |
|
225 |
System.out.println("Caught the ReadOnlyBuffer: " + e); |
|
226 |
} |
|
227 |
||
228 |
appBB.position(0); |
|
229 |
System.out.println("Check various UNDERFLOW conditions"); |
|
230 |
||
231 |
SSLEngineResult sslER; |
|
232 |
||
233 |
if ((sslER = |
|
234 |
ssle.unwrap(ByteBuffer.wrap(smallSSLHeader), |
|
235 |
appBB)).getStatus() != |
|
236 |
Status.BUFFER_UNDERFLOW) { |
|
237 |
System.out.println(sslER); |
|
238 |
throw new Exception("unwrap should underflow"); |
|
239 |
} |
|
240 |
||
241 |
if ((sslER = |
|
242 |
ssle.unwrap(ByteBuffer.wrap(incompleteSSLHeader), |
|
243 |
appBB)).getStatus() != |
|
244 |
Status.BUFFER_UNDERFLOW) { |
|
245 |
System.out.println(sslER); |
|
246 |
throw new Exception("unwrap should underflow"); |
|
247 |
} |
|
248 |
||
249 |
if ((sslER = |
|
250 |
ssle.unwrap(ByteBuffer.wrap(smallv2Header), |
|
251 |
appBB)).getStatus() != |
|
252 |
Status.BUFFER_UNDERFLOW) { |
|
253 |
System.out.println(sslER); |
|
254 |
throw new Exception("unwrap should underflow"); |
|
255 |
} |
|
256 |
||
257 |
// junk inbound message |
|
258 |
try { |
|
259 |
ssle.unwrap(ByteBuffer.wrap(gobblydegook), appBB); |
|
260 |
throw new Exception("Didn't catch the nasty SSLException"); |
|
261 |
} catch (SSLException e) { |
|
262 |
System.out.println("caught the nasty SSLException: " + e); |
|
263 |
} |
|
264 |
||
265 |
System.out.println("Test PASSED"); |
|
266 |
||
267 |
} |
|
268 |
||
269 |
static byte [] smallSSLHeader = new byte [] { |
|
270 |
(byte) 0x16, (byte) 0x03, (byte) 0x01, |
|
271 |
(byte) 0x05 }; |
|
272 |
||
273 |
static byte [] incompleteSSLHeader = new byte [] { |
|
274 |
(byte) 0x16, (byte) 0x03, (byte) 0x01, |
|
275 |
(byte) 0x00, (byte) 0x5, // 5 bytes |
|
276 |
(byte) 0x16, (byte) 0x03, (byte) 0x01, (byte) 0x00 }; |
|
277 |
||
278 |
static byte [] smallv2Header = new byte [] { |
|
279 |
(byte) 0x80, (byte) 0x03, (byte) 0x01, |
|
280 |
(byte) 0x00 }; |
|
281 |
||
282 |
static byte [] gobblydegook = new byte [] { |
|
283 |
// "HELLO HELLO" |
|
284 |
(byte) 0x48, (byte) 0x45, (byte) 0x4C, (byte) 0x4C, (byte) 0x20, |
|
285 |
(byte) 0x48, (byte) 0x45, (byte) 0x4C, (byte) 0x4C }; |
|
286 |
||
287 |
static void printStrings(String label, String [] strs) { |
|
288 |
System.out.println(label); |
|
289 |
||
290 |
for (int i = 0; i < strs.length; i++) { |
|
291 |
System.out.println(" " + strs[i]); |
|
292 |
} |
|
293 |
} |
|
294 |
} |