author | xuelei |
Sat, 30 Oct 2010 18:39:17 +0800 | |
changeset 7039 | 6464c8e62a18 |
parent 6856 | 533f4ad71f88 |
child 8991 | 7df5283fd3b8 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
6856 | 2 |
* Copyright (c) 2002, 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 |
import java.io.*; |
|
25 |
import java.net.*; |
|
26 |
import java.util.*; |
|
27 |
import java.util.concurrent.*; |
|
28 |
||
29 |
import java.security.*; |
|
30 |
import java.security.cert.*; |
|
31 |
import java.security.cert.Certificate; |
|
32 |
||
33 |
import javax.net.ssl.*; |
|
34 |
||
35 |
/** |
|
36 |
* Test that all ciphersuites work in all versions and all client |
|
37 |
* authentication types. The way this is setup the server is stateless and |
|
38 |
* all checking is done on the client side. |
|
39 |
* |
|
40 |
* The test is multithreaded to speed it up, especially on multiprocessor |
|
41 |
* machines. To simplify debugging, run with -DnumThreads=1. |
|
42 |
* |
|
43 |
* @author Andreas Sterbenz |
|
44 |
*/ |
|
45 |
public class CipherTest { |
|
46 |
||
47 |
// use any available port for the server socket |
|
48 |
static int serverPort = 0; |
|
49 |
||
50 |
final int THREADS; |
|
51 |
||
52 |
// assume that if we do not read anything for 20 seconds, something |
|
53 |
// has gone wrong |
|
54 |
final static int TIMEOUT = 20 * 1000; |
|
55 |
||
56 |
static KeyStore trustStore, keyStore; |
|
57 |
static X509ExtendedKeyManager keyManager; |
|
58 |
static X509TrustManager trustManager; |
|
59 |
static SecureRandom secureRandom; |
|
60 |
||
61 |
private static PeerFactory peerFactory; |
|
62 |
||
63 |
static abstract class Server implements Runnable { |
|
64 |
||
65 |
final CipherTest cipherTest; |
|
66 |
||
67 |
Server(CipherTest cipherTest) throws Exception { |
|
68 |
this.cipherTest = cipherTest; |
|
69 |
} |
|
70 |
||
71 |
public abstract void run(); |
|
72 |
||
73 |
void handleRequest(InputStream in, OutputStream out) throws IOException { |
|
74 |
boolean newline = false; |
|
75 |
StringBuilder sb = new StringBuilder(); |
|
76 |
while (true) { |
|
77 |
int ch = in.read(); |
|
78 |
if (ch < 0) { |
|
79 |
throw new EOFException(); |
|
80 |
} |
|
81 |
sb.append((char)ch); |
|
82 |
if (ch == '\r') { |
|
83 |
// empty |
|
84 |
} else if (ch == '\n') { |
|
85 |
if (newline) { |
|
86 |
// 2nd newline in a row, end of request |
|
87 |
break; |
|
88 |
} |
|
89 |
newline = true; |
|
90 |
} else { |
|
91 |
newline = false; |
|
92 |
} |
|
93 |
} |
|
94 |
String request = sb.toString(); |
|
95 |
if (request.startsWith("GET / HTTP/1.") == false) { |
|
96 |
throw new IOException("Invalid request: " + request); |
|
97 |
} |
|
98 |
out.write("HTTP/1.0 200 OK\r\n\r\n".getBytes()); |
|
99 |
} |
|
100 |
||
101 |
} |
|
102 |
||
103 |
public static class TestParameters { |
|
104 |
||
105 |
String cipherSuite; |
|
106 |
String protocol; |
|
107 |
String clientAuth; |
|
108 |
||
109 |
TestParameters(String cipherSuite, String protocol, |
|
110 |
String clientAuth) { |
|
111 |
this.cipherSuite = cipherSuite; |
|
112 |
this.protocol = protocol; |
|
113 |
this.clientAuth = clientAuth; |
|
114 |
} |
|
115 |
||
116 |
boolean isEnabled() { |
|
6856 | 117 |
// ignore SCSV |
118 |
if (cipherSuite.equals("TLS_EMPTY_RENEGOTIATION_INFO_SCSV")) { |
|
119 |
return false; |
|
120 |
} |
|
121 |
||
7039 | 122 |
// ignore exportable cipher suite for TLSv1.1 |
123 |
if (protocol.equals("TLSv1.1")) { |
|
124 |
if(cipherSuite.indexOf("_EXPORT_") != -1) { |
|
125 |
return false; |
|
126 |
} |
|
127 |
} |
|
128 |
||
2 | 129 |
return true; |
130 |
} |
|
131 |
||
132 |
public String toString() { |
|
133 |
String s = cipherSuite + " in " + protocol + " mode"; |
|
134 |
if (clientAuth != null) { |
|
135 |
s += " with " + clientAuth + " client authentication"; |
|
136 |
} |
|
137 |
return s; |
|
138 |
} |
|
139 |
||
140 |
} |
|
141 |
||
142 |
private List<TestParameters> tests; |
|
143 |
private Iterator<TestParameters> testIterator; |
|
144 |
private SSLSocketFactory factory; |
|
145 |
private boolean failed; |
|
146 |
||
147 |
private CipherTest(PeerFactory peerFactory) throws IOException { |
|
148 |
THREADS = Integer.parseInt(System.getProperty("numThreads", "4")); |
|
149 |
factory = (SSLSocketFactory)SSLSocketFactory.getDefault(); |
|
150 |
SSLSocket socket = (SSLSocket)factory.createSocket(); |
|
151 |
String[] cipherSuites = socket.getSupportedCipherSuites(); |
|
152 |
String[] protocols = socket.getSupportedProtocols(); |
|
153 |
String[] clientAuths = {null, "RSA", "DSA", "ECDSA"}; |
|
154 |
tests = new ArrayList<TestParameters>( |
|
155 |
cipherSuites.length * protocols.length * clientAuths.length); |
|
156 |
for (int i = 0; i < cipherSuites.length; i++) { |
|
157 |
String cipherSuite = cipherSuites[i]; |
|
7039 | 158 |
|
2 | 159 |
for (int j = 0; j < protocols.length; j++) { |
160 |
String protocol = protocols[j]; |
|
7039 | 161 |
|
162 |
if (!peerFactory.isSupported(cipherSuite, protocol)) { |
|
2 | 163 |
continue; |
164 |
} |
|
7039 | 165 |
|
2 | 166 |
for (int k = 0; k < clientAuths.length; k++) { |
167 |
String clientAuth = clientAuths[k]; |
|
168 |
if ((clientAuth != null) && |
|
169 |
(cipherSuite.indexOf("DH_anon") != -1)) { |
|
170 |
// no client with anonymous ciphersuites |
|
171 |
continue; |
|
172 |
} |
|
173 |
tests.add(new TestParameters(cipherSuite, protocol, |
|
174 |
clientAuth)); |
|
175 |
} |
|
176 |
} |
|
177 |
} |
|
178 |
testIterator = tests.iterator(); |
|
179 |
} |
|
180 |
||
181 |
synchronized void setFailed() { |
|
182 |
failed = true; |
|
183 |
} |
|
184 |
||
185 |
public void run() throws Exception { |
|
186 |
Thread[] threads = new Thread[THREADS]; |
|
187 |
for (int i = 0; i < THREADS; i++) { |
|
188 |
try { |
|
189 |
threads[i] = new Thread(peerFactory.newClient(this), |
|
190 |
"Client " + i); |
|
191 |
} catch (Exception e) { |
|
192 |
e.printStackTrace(); |
|
193 |
return; |
|
194 |
} |
|
195 |
threads[i].start(); |
|
196 |
} |
|
197 |
try { |
|
198 |
for (int i = 0; i < THREADS; i++) { |
|
199 |
threads[i].join(); |
|
200 |
} |
|
201 |
} catch (InterruptedException e) { |
|
202 |
setFailed(); |
|
203 |
e.printStackTrace(); |
|
204 |
} |
|
205 |
if (failed) { |
|
206 |
throw new Exception("*** Test '" + peerFactory.getName() + |
|
207 |
"' failed ***"); |
|
208 |
} else { |
|
209 |
System.out.println("Test '" + peerFactory.getName() + |
|
210 |
"' completed successfully"); |
|
211 |
} |
|
212 |
} |
|
213 |
||
214 |
synchronized TestParameters getTest() { |
|
215 |
if (failed) { |
|
216 |
return null; |
|
217 |
} |
|
218 |
if (testIterator.hasNext()) { |
|
219 |
return (TestParameters)testIterator.next(); |
|
220 |
} |
|
221 |
return null; |
|
222 |
} |
|
223 |
||
224 |
SSLSocketFactory getFactory() { |
|
225 |
return factory; |
|
226 |
} |
|
227 |
||
228 |
static abstract class Client implements Runnable { |
|
229 |
||
230 |
final CipherTest cipherTest; |
|
231 |
||
232 |
Client(CipherTest cipherTest) throws Exception { |
|
233 |
this.cipherTest = cipherTest; |
|
234 |
} |
|
235 |
||
236 |
public final void run() { |
|
237 |
while (true) { |
|
238 |
TestParameters params = cipherTest.getTest(); |
|
239 |
if (params == null) { |
|
240 |
// no more tests |
|
241 |
break; |
|
242 |
} |
|
243 |
if (params.isEnabled() == false) { |
|
244 |
System.out.println("Skipping disabled test " + params); |
|
245 |
continue; |
|
246 |
} |
|
247 |
try { |
|
248 |
runTest(params); |
|
249 |
System.out.println("Passed " + params); |
|
250 |
} catch (Exception e) { |
|
251 |
cipherTest.setFailed(); |
|
252 |
System.out.println("** Failed " + params + "**"); |
|
253 |
e.printStackTrace(); |
|
254 |
} |
|
255 |
} |
|
256 |
} |
|
257 |
||
258 |
abstract void runTest(TestParameters params) throws Exception; |
|
259 |
||
260 |
void sendRequest(InputStream in, OutputStream out) throws IOException { |
|
261 |
out.write("GET / HTTP/1.0\r\n\r\n".getBytes()); |
|
262 |
out.flush(); |
|
263 |
StringBuilder sb = new StringBuilder(); |
|
264 |
while (true) { |
|
265 |
int ch = in.read(); |
|
266 |
if (ch < 0) { |
|
267 |
break; |
|
268 |
} |
|
269 |
sb.append((char)ch); |
|
270 |
} |
|
271 |
String response = sb.toString(); |
|
272 |
if (response.startsWith("HTTP/1.0 200 ") == false) { |
|
273 |
throw new IOException("Invalid response: " + response); |
|
274 |
} |
|
275 |
} |
|
276 |
||
277 |
} |
|
278 |
||
279 |
// for some reason, ${test.src} has a different value when the |
|
280 |
// test is called from the script and when it is called directly... |
|
281 |
static String pathToStores = "."; |
|
282 |
static String pathToStoresSH = "."; |
|
283 |
static String keyStoreFile = "keystore"; |
|
284 |
static String trustStoreFile = "truststore"; |
|
285 |
static char[] passwd = "passphrase".toCharArray(); |
|
286 |
||
287 |
static File PATH; |
|
288 |
||
289 |
private static KeyStore readKeyStore(String name) throws Exception { |
|
290 |
File file = new File(PATH, name); |
|
291 |
InputStream in = new FileInputStream(file); |
|
292 |
KeyStore ks = KeyStore.getInstance("JKS"); |
|
293 |
ks.load(in, passwd); |
|
294 |
in.close(); |
|
295 |
return ks; |
|
296 |
} |
|
297 |
||
298 |
public static void main(PeerFactory peerFactory, String[] args) |
|
299 |
throws Exception { |
|
300 |
long time = System.currentTimeMillis(); |
|
301 |
String relPath; |
|
3863
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
2
diff
changeset
|
302 |
if ((args != null) && (args.length > 0) && args[0].equals("sh")) { |
2 | 303 |
relPath = pathToStoresSH; |
304 |
} else { |
|
305 |
relPath = pathToStores; |
|
306 |
} |
|
307 |
PATH = new File(System.getProperty("test.src", "."), relPath); |
|
308 |
CipherTest.peerFactory = peerFactory; |
|
309 |
System.out.print( |
|
310 |
"Initializing test '" + peerFactory.getName() + "'..."); |
|
311 |
secureRandom = new SecureRandom(); |
|
312 |
secureRandom.nextInt(); |
|
313 |
trustStore = readKeyStore(trustStoreFile); |
|
314 |
keyStore = readKeyStore(keyStoreFile); |
|
315 |
KeyManagerFactory keyFactory = |
|
316 |
KeyManagerFactory.getInstance( |
|
317 |
KeyManagerFactory.getDefaultAlgorithm()); |
|
318 |
keyFactory.init(keyStore, passwd); |
|
319 |
keyManager = (X509ExtendedKeyManager)keyFactory.getKeyManagers()[0]; |
|
320 |
trustManager = new AlwaysTrustManager(); |
|
321 |
||
322 |
CipherTest cipherTest = new CipherTest(peerFactory); |
|
323 |
Thread serverThread = new Thread(peerFactory.newServer(cipherTest), |
|
324 |
"Server"); |
|
325 |
serverThread.setDaemon(true); |
|
326 |
serverThread.start(); |
|
327 |
System.out.println("Done"); |
|
328 |
cipherTest.run(); |
|
329 |
time = System.currentTimeMillis() - time; |
|
330 |
System.out.println("Done. (" + time + " ms)"); |
|
331 |
} |
|
332 |
||
333 |
static abstract class PeerFactory { |
|
334 |
||
335 |
abstract String getName(); |
|
336 |
||
337 |
abstract Client newClient(CipherTest cipherTest) throws Exception; |
|
338 |
||
339 |
abstract Server newServer(CipherTest cipherTest) throws Exception; |
|
340 |
||
7039 | 341 |
boolean isSupported(String cipherSuite, String protocol) { |
342 |
// skip kerberos cipher suites |
|
343 |
if (cipherSuite.startsWith("TLS_KRB5")) { |
|
344 |
System.out.println("Skipping unsupported test for " + |
|
345 |
cipherSuite + " of " + protocol); |
|
346 |
return false; |
|
347 |
} |
|
348 |
||
349 |
// skip SSLv2Hello protocol |
|
350 |
if (protocol.equals("SSLv2Hello")) { |
|
351 |
System.out.println("Skipping unsupported test for " + |
|
352 |
cipherSuite + " of " + protocol); |
|
353 |
return false; |
|
354 |
} |
|
355 |
||
356 |
// ignore exportable cipher suite for TLSv1.1 |
|
357 |
if (protocol.equals("TLSv1.1")) { |
|
358 |
if (cipherSuite.indexOf("_EXPORT_WITH") != -1) { |
|
359 |
System.out.println("Skipping obsoleted test for " + |
|
360 |
cipherSuite + " of " + protocol); |
|
361 |
return false; |
|
362 |
} |
|
363 |
} |
|
364 |
||
2 | 365 |
return true; |
366 |
} |
|
367 |
} |
|
368 |
||
369 |
} |
|
370 |
||
371 |
// we currently don't do any chain verification. we assume that works ok |
|
372 |
// and we can speed up the test. we could also just add a plain certificate |
|
373 |
// chain comparision with our trusted certificates. |
|
374 |
class AlwaysTrustManager implements X509TrustManager { |
|
375 |
||
376 |
public AlwaysTrustManager() { |
|
377 |
||
378 |
} |
|
379 |
||
380 |
public void checkClientTrusted(X509Certificate[] chain, String authType) |
|
381 |
throws CertificateException { |
|
382 |
// empty |
|
383 |
} |
|
384 |
||
385 |
public void checkServerTrusted(X509Certificate[] chain, String authType) |
|
386 |
throws CertificateException { |
|
387 |
// empty |
|
388 |
} |
|
389 |
||
390 |
public X509Certificate[] getAcceptedIssuers() { |
|
391 |
return new X509Certificate[0]; |
|
392 |
} |
|
393 |
} |
|
394 |
||
395 |
class MyX509KeyManager extends X509ExtendedKeyManager { |
|
396 |
||
397 |
private final X509ExtendedKeyManager keyManager; |
|
398 |
private String authType; |
|
399 |
||
400 |
MyX509KeyManager(X509ExtendedKeyManager keyManager) { |
|
401 |
this.keyManager = keyManager; |
|
402 |
} |
|
403 |
||
404 |
void setAuthType(String authType) { |
|
405 |
this.authType = "ECDSA".equals(authType) ? "EC" : authType; |
|
406 |
} |
|
407 |
||
408 |
public String[] getClientAliases(String keyType, Principal[] issuers) { |
|
409 |
if (authType == null) { |
|
410 |
return null; |
|
411 |
} |
|
412 |
return keyManager.getClientAliases(authType, issuers); |
|
413 |
} |
|
414 |
||
415 |
public String chooseClientAlias(String[] keyType, Principal[] issuers, |
|
416 |
Socket socket) { |
|
417 |
if (authType == null) { |
|
418 |
return null; |
|
419 |
} |
|
420 |
return keyManager.chooseClientAlias(new String[] {authType}, |
|
421 |
issuers, socket); |
|
422 |
} |
|
423 |
||
424 |
public String chooseEngineClientAlias(String[] keyType, |
|
425 |
Principal[] issuers, SSLEngine engine) { |
|
426 |
if (authType == null) { |
|
427 |
return null; |
|
428 |
} |
|
429 |
return keyManager.chooseEngineClientAlias(new String[] {authType}, |
|
430 |
issuers, engine); |
|
431 |
} |
|
432 |
||
433 |
public String[] getServerAliases(String keyType, Principal[] issuers) { |
|
434 |
throw new UnsupportedOperationException("Servers not supported"); |
|
435 |
} |
|
436 |
||
437 |
public String chooseServerAlias(String keyType, Principal[] issuers, |
|
438 |
Socket socket) { |
|
439 |
throw new UnsupportedOperationException("Servers not supported"); |
|
440 |
} |
|
441 |
||
442 |
public String chooseEngineServerAlias(String keyType, Principal[] issuers, |
|
443 |
SSLEngine engine) { |
|
444 |
throw new UnsupportedOperationException("Servers not supported"); |
|
445 |
} |
|
446 |
||
447 |
public X509Certificate[] getCertificateChain(String alias) { |
|
448 |
return keyManager.getCertificateChain(alias); |
|
449 |
} |
|
450 |
||
451 |
public PrivateKey getPrivateKey(String alias) { |
|
452 |
return keyManager.getPrivateKey(alias); |
|
453 |
} |
|
454 |
||
455 |
} |
|
456 |
||
457 |
class DaemonThreadFactory implements ThreadFactory { |
|
458 |
||
459 |
final static ThreadFactory INSTANCE = new DaemonThreadFactory(); |
|
460 |
||
461 |
private final static ThreadFactory DEFAULT = Executors.defaultThreadFactory(); |
|
462 |
||
463 |
public Thread newThread(Runnable r) { |
|
464 |
Thread t = DEFAULT.newThread(r); |
|
465 |
t.setDaemon(true); |
|
466 |
return t; |
|
467 |
} |
|
468 |
||
469 |
} |