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 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682
|
/*
* Copyright (c) 2010, 2016, 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.ByteArrayInputStream;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.security.KeyFactory;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.Principal;
import java.security.PrivateKey;
import java.security.SecureRandom;
import java.security.UnrecoverableKeyException;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.security.interfaces.RSAPrivateKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Base64;
import java.util.Collections;
import java.util.List;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLEngine;
import javax.net.ssl.SSLServerSocket;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.X509ExtendedKeyManager;
import javax.net.ssl.X509TrustManager;
/**
* Test that all ciphersuites work in all versions and all client authentication
* types. The way this is setup the server is stateless and all checking is done
* on the client side.
*/
public class CipherTestUtils {
public static final int TIMEOUT = 20 * 1000;
public static final SecureRandom secureRandom = new SecureRandom();
public static char[] PASSWORD = "passphrase".toCharArray();
private static final List<TestParameters> TESTS = new ArrayList<>(3);
private static final List<Exception> EXCEPTIONS
= Collections.synchronizedList(new ArrayList<>(1));
private static final String CLIENT_PUBLIC_KEY
= "-----BEGIN CERTIFICATE-----\n"
+ "MIICtTCCAh4CCQDkYJ46DMcGRjANBgkqhkiG9w0BAQUFADCBnDELMAkGA1UEBhMC\n"
+ "VVMxCzAJBgNVBAgMAkNBMRYwFAYDVQQHDA1Nb3VudGFpbiBWaWV3MR8wHQYDVQQK\n"
+ "DBZTdW4gTWljcm9zeXN0ZW1zLCBJbmMuMSYwJAYDVQQLDB1TdW4gTWljcm9zeXN0\n"
+ "ZW1zIExhYm9yYXRvcmllczEfMB0GA1UEAwwWVGVzdCBDQSAoMTAyNCBiaXQgUlNB\n"
+ "KTAeFw0wOTA0MjcwNDA0MDhaFw0xMzA2MDUwNDA0MDhaMIGgMQswCQYDVQQGEwJV\n"
+ "UzELMAkGA1UECAwCQ0ExFjAUBgNVBAcMDU1vdW50YWluIFZpZXcxHzAdBgNVBAoM\n"
+ "FlN1biBNaWNyb3N5c3RlbXMsIEluYy4xJj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xKhagj0C4UHelNyYpAJ9MLjNiGQ7I31yTeaNrUCAi0XSfsKTSrwbLSnJ\n"
+ "qsUPKMBrnzcWrOyui2+cupHZXaTlNeYB97teLJYpa6Ql9CZLoTHoim1+//s7diBh\n"
+ "03Vls+M6Poi5PMvv59UCQQD+k/BiokmbBgWHfBY5cZSlx3Z4VTwSHJmHDTO3Tjso\n"
+ "EVErXUSVvqD/KHX6eM4VPM8lySV5djWV8lDsESCWMtiLAkEA4/xFNsiOLMQpxW/O\n"
+ "bt2tukxJkAxldD4lPoFZR+zbXtMtt8OjERtX2wD+nj6h7jfIeSyVuBEcBN8Uj8xe\n"
+ "kgfgIwJAPbKG4LCqHAsCjgpRrIxNVTwZByLJEy6hOqzFathn19cSj+rjs1Lm28/n\n"
+ "f9OFRnpdTbAJB/3REM0QNZYVCrG57wJBAN0KuTytZJNouaswhPCew5Kt5mDgc/kp\n"
+ "S8j3dk2zCto8W8Ygy1iJrzuqEjPxO+UQdrFtlde51vWuKGxnVIW3VwsCQEldqk7r\n"
+ "8y7PgquPP+k3L0OXno5wGBrPcW1+U0mhIZGnwSzE4SPX2ddqUSEUA/Av4RjAckL/\n"
+ "fpqmCkpTanyYW9U=\n"
+ "-----END PRIVATE KEY-----";
private final SSLSocketFactory factory;
private final X509ExtendedKeyManager clientKeyManager;
private final X509ExtendedKeyManager serverKeyManager;
private final X509TrustManager clientTrustManager;
private final X509TrustManager serverTrustManager;
static abstract class Server implements Runnable, AutoCloseable {
final CipherTestUtils cipherTest;
Server(CipherTestUtils cipherTest) throws Exception {
this.cipherTest = cipherTest;
}
@Override
public abstract void run();
abstract int getPort();
void handleRequest(InputStream in, OutputStream out)
throws IOException {
boolean newline = false;
StringBuilder sb = new StringBuilder();
while (true) {
int ch = in.read();
if (ch < 0) {
throw new EOFException();
}
sb.append((char) ch);
if (ch == '\r') {
// empty
} else if (ch == '\n') {
if (newline) {
// 2nd newline in a row, end of request
break;
}
newline = true;
} else {
newline = false;
}
}
String request = sb.toString();
if (request.startsWith("GET / HTTP/1.") == false) {
throw new IOException("Invalid request: " + request);
}
out.write("HTTP/1.0 200 OK\r\n\r\n".getBytes());
out.write("Tested Scenario: ".getBytes());
TestParameters tp = (TestParameters) CipherTestUtils.TESTS.get(0);
out.write(tp.toString().getBytes());
out.write(" Test PASSED.".getBytes());
}
}
public static class TestParameters {
final String cipherSuite;
final String protocol;
final String clientAuth;
TestParameters(String cipherSuite, String protocol, String clientAuth) {
this.cipherSuite = cipherSuite;
this.protocol = protocol;
this.clientAuth = clientAuth;
}
boolean isEnabled() {
return true;
}
@Override
public String toString() {
String s = cipherSuite + " in " + protocol + " mode";
if (clientAuth != null) {
s += " with " + clientAuth + " client authentication";
}
return s;
}
}
private static volatile CipherTestUtils instance = null;
public static CipherTestUtils getInstance() throws Exception {
if (instance == null) {
synchronized (CipherTestUtils.class) {
if (instance == null) {
instance = new CipherTestUtils();
}
}
}
return instance;
}
public static void setTestedArguments(String protocol, String ciphersuite) {
ciphersuite = ciphersuite.trim();
TestParameters params = new TestParameters(ciphersuite, protocol, null);
TESTS.add(params);
}
public X509ExtendedKeyManager getClientKeyManager() {
return clientKeyManager;
}
public X509TrustManager getClientTrustManager() {
return clientTrustManager;
}
public X509ExtendedKeyManager getServerKeyManager() {
return serverKeyManager;
}
public X509TrustManager getServerTrustManager() {
return serverTrustManager;
}
public static void addFailure(Exception e) {
EXCEPTIONS.add(e);
}
private CipherTestUtils() throws Exception {
factory = (SSLSocketFactory) SSLSocketFactory.getDefault();
KeyStore serverKeyStore = createServerKeyStore(SERVER_PUBLIC_KEY,
SERVER_PRIVATE_KEY);
KeyStore serverTrustStore = createServerKeyStore(CA_PUBLIC_KEY,
CA_PRIVATE_KEY);
if (serverKeyStore != null) {
KeyManagerFactory keyFactory = KeyManagerFactory.getInstance(
KeyManagerFactory.getDefaultAlgorithm());
keyFactory.init(serverKeyStore, PASSWORD);
serverKeyManager = (X509ExtendedKeyManager)
keyFactory.getKeyManagers()[0];
} else {
serverKeyManager = null;
}
serverTrustManager = serverTrustStore != null
? new AlwaysTrustManager(serverTrustStore) : null;
KeyStore clientKeyStore, clientTrustStore;
clientTrustStore = serverTrustStore;
clientKeyStore =
createServerKeyStore(CLIENT_PUBLIC_KEY,CLIENT_PRIVATE_KEY);
if (clientKeyStore != null) {
KeyManagerFactory keyFactory = KeyManagerFactory.getInstance(
KeyManagerFactory.getDefaultAlgorithm());
keyFactory.init(clientKeyStore, PASSWORD);
clientKeyManager = (X509ExtendedKeyManager)
keyFactory.getKeyManagers()[0];
} else {
clientKeyManager = null;
}
clientTrustManager = (clientTrustStore != null)
? new AlwaysTrustManager(clientTrustStore) : null;
}
void checkResult(String exception) throws Exception {
if (EXCEPTIONS.size() >= 1) {
Exception actualException = EXCEPTIONS.get(0);
if (exception == null) {
throw new RuntimeException("FAILED: got unexpected exception: "
+ actualException);
}
if (!exception.equals(actualException.getClass().getName())) {
throw new RuntimeException("FAILED: got unexpected exception: "
+ actualException);
}
System.out.println("PASSED: got expected exception: "
+ actualException);
} else {
if (exception != null) {
throw new RuntimeException("FAILED: " + exception
+ " was expected");
}
System.out.println("PASSED");
}
}
SSLSocketFactory getFactory() {
return factory;
}
static abstract class Client implements Runnable {
final CipherTestUtils cipherTest;
TestParameters testedParams;
Client(CipherTestUtils cipherTest) throws Exception {
this.cipherTest = cipherTest;
}
Client(CipherTestUtils cipherTest, String testedCipherSuite)
throws Exception {
this.cipherTest = cipherTest;
}
@Override
public final void run() {
TESTS.stream().map((params) -> {
if (!params.isEnabled()) {
System.out.println("Skipping disabled test " + params);
}
return params;
}).forEach((params) -> {
try {
System.out.println("Testing " + params);
runTest(params);
System.out.println("Passed " + params);
} catch (Exception e) {
CipherTestUtils.addFailure(e);
System.out.println("** Failed " + params
+ "**, got exception:");
e.printStackTrace(System.out);
}
});
}
abstract void runTest(TestParameters params) throws Exception;
void sendRequest(InputStream in, OutputStream out) throws IOException {
out.write("GET / HTTP/1.0\r\n\r\n".getBytes());
out.flush();
StringBuilder sb = new StringBuilder();
while (true) {
int ch = in.read();
if (ch < 0) {
break;
}
sb.append((char) ch);
}
String response = sb.toString();
if (response.startsWith("HTTP/1.0 200 ") == false) {
throw new IOException("Invalid response: " + response);
} else {
System.out.println();
System.out.println("--- Response --- ");
System.out.println(response);
System.out.println("---------------- ");
}
}
}
public static void printStringArray(String[] stringArray) {
System.out.println(Arrays.toString(stringArray));
System.out.println();
}
public static void printInfo(SSLServerSocket socket) {
System.out.println();
System.out.println("--- SSL ServerSocket Info ---");
System.out.print("SupportedProtocols : ");
printStringArray(socket.getSupportedProtocols());
System.out.print("SupportedCipherSuites : ");
printStringArray(socket.getSupportedCipherSuites());
System.out.print("EnabledProtocols : ");
printStringArray(socket.getEnabledProtocols());
System.out.print("EnabledCipherSuites : ");
String[] supportedCipherSuites = socket.getEnabledCipherSuites();
Arrays.sort(supportedCipherSuites);
printStringArray(supportedCipherSuites);
System.out.println("NeedClientAuth : "
+ socket.getNeedClientAuth());
System.out.println("WantClientAuth : "
+ socket.getWantClientAuth());
System.out.println("-----------------------");
}
public static void printInfo(SSLSocket socket) {
System.out.println();
System.out.println("--- SSL Socket Info ---");
System.out.print(" SupportedProtocols : ");
printStringArray(socket.getSupportedProtocols());
System.out.println(" EnabledProtocols : "
+ socket.getEnabledProtocols()[0]);
System.out.print(" SupportedCipherSuites : ");
String[] supportedCipherSuites = socket.getEnabledCipherSuites();
Arrays.sort(supportedCipherSuites);
printStringArray(supportedCipherSuites);
System.out.println(" EnabledCipherSuites : "
+ socket.getEnabledCipherSuites()[0]);
System.out.println(" NeedClientAuth : "
+ socket.getNeedClientAuth());
System.out.println(" WantClientAuth : "
+ socket.getWantClientAuth());
System.out.println("-----------------------");
}
private static KeyStore createServerKeyStore(String publicKey,
String keySpecStr) throws KeyStoreException, IOException,
NoSuchAlgorithmException, CertificateException,
InvalidKeySpecException {
KeyStore ks = KeyStore.getInstance("JKS");
ks.load(null, null);
if (publicKey == null || keySpecStr == null) {
throw new IllegalArgumentException("publicKey or "
+ "keySpecStr cannot be null");
}
String strippedPrivateKey = keySpecStr.substring(
keySpecStr.indexOf("\n"), keySpecStr.lastIndexOf("\n"));
// generate the private key.
PKCS8EncodedKeySpec priKeySpec = new PKCS8EncodedKeySpec(
Base64.getMimeDecoder().decode(strippedPrivateKey));
KeyFactory kf = KeyFactory.getInstance("RSA");
RSAPrivateKey priKey
= (RSAPrivateKey) kf.generatePrivate(priKeySpec);
// generate certificate chain
try (InputStream is = new ByteArrayInputStream(publicKey.getBytes())) {
// generate certificate from cert string
CertificateFactory cf = CertificateFactory.getInstance("X.509");
Certificate keyCert = cf.generateCertificate(is);
Certificate[] chain = {keyCert};
ks.setKeyEntry("TestEntry", priKey, PASSWORD, chain);
}
return ks;
}
public static Server mainServer(PeerFactory peerFactory,
String expectedException) throws Exception {
setTestedArguments(peerFactory.getTestedProtocol(),
peerFactory.getTestedCipher());
System.out.print(
" Initializing test '" + peerFactory.getName() + "'...");
secureRandom.nextInt();
CipherTestUtils cipherTest = CipherTestUtils.getInstance();
Server srv = peerFactory.newServer(cipherTest, PeerFactory.FREE_PORT);
Thread serverThread = new Thread(srv, "Server");
serverThread.start();
return srv;
}
public static void mainClient(PeerFactory peerFactory, int port,
String expectedException) throws Exception {
long time = System.currentTimeMillis();
setTestedArguments(peerFactory.getTestedProtocol(),
peerFactory.getTestedCipher());
System.out.print(
" Initializing test '" + peerFactory.getName() + "'...");
secureRandom.nextInt();
CipherTestUtils cipherTest = CipherTestUtils.getInstance();
peerFactory.newClient(cipherTest, port).run();
cipherTest.checkResult(expectedException);
time = System.currentTimeMillis() - time;
System.out.println("Elapsed time " + time);
}
public static abstract class PeerFactory {
public static final int FREE_PORT = 0;
abstract String getName();
abstract String getTestedProtocol();
abstract String getTestedCipher();
abstract Client newClient(CipherTestUtils cipherTest, int testPort)
throws Exception;
abstract Server newServer(CipherTestUtils cipherTest, int testPort)
throws Exception;
boolean isSupported(String cipherSuite) {
return true;
}
}
}
class AlwaysTrustManager implements X509TrustManager {
X509TrustManager trustManager;
public AlwaysTrustManager(KeyStore keyStore)
throws NoSuchAlgorithmException, KeyStoreException {
TrustManagerFactory tmf
= TrustManagerFactory.getInstance(TrustManagerFactory.
getDefaultAlgorithm());
tmf.init(keyStore);
TrustManager tms[] = tmf.getTrustManagers();
for (TrustManager tm : tms) {
trustManager = (X509TrustManager) tm;
return;
}
}
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
try {
trustManager.checkClientTrusted(chain, authType);
} catch (CertificateException excep) {
System.out.println("ERROR in client trust manager: " + excep);
}
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
try {
trustManager.checkServerTrusted(chain, authType);
} catch (CertificateException excep) {
System.out.println("ERROR in server trust manager: " + excep);
}
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return trustManager.getAcceptedIssuers();
}
}
class MyX509KeyManager extends X509ExtendedKeyManager {
private final X509ExtendedKeyManager keyManager;
private String authType;
MyX509KeyManager(X509ExtendedKeyManager keyManager) {
this.keyManager = keyManager;
}
void setAuthType(String authType) {
this.authType = "ECDSA".equals(authType) ? "EC" : authType;
}
@Override
public String[] getClientAliases(String keyType, Principal[] issuers) {
if (authType == null) {
return null;
}
return keyManager.getClientAliases(authType, issuers);
}
@Override
public String chooseClientAlias(String[] keyType, Principal[] issuers,
Socket socket) {
if (authType == null) {
return null;
}
return keyManager.chooseClientAlias(new String[]{authType},
issuers, socket);
}
@Override
public String chooseEngineClientAlias(String[] keyType,
Principal[] issuers, SSLEngine engine) {
if (authType == null) {
return null;
}
return keyManager.chooseEngineClientAlias(new String[]{authType},
issuers, engine);
}
@Override
public String[] getServerAliases(String keyType, Principal[] issuers) {
throw new UnsupportedOperationException("Servers not supported");
}
@Override
public String chooseServerAlias(String keyType, Principal[] issuers,
Socket socket) {
throw new UnsupportedOperationException("Servers not supported");
}
@Override
public String chooseEngineServerAlias(String keyType, Principal[] issuers,
SSLEngine engine) {
throw new UnsupportedOperationException("Servers not supported");
}
@Override
public X509Certificate[] getCertificateChain(String alias) {
return keyManager.getCertificateChain(alias);
}
@Override
public PrivateKey getPrivateKey(String alias) {
return keyManager.getPrivateKey(alias);
}
}
|