8298869: Update ConnectionTest.java for changes to TLS implementation

Reviewed-by: rhalade
This commit is contained in:
Matthew Donovan 2023-01-26 21:36:36 +00:00 committed by Rajan Halade
parent c4278144be
commit d6007a356f
2 changed files with 358 additions and 437 deletions

View File

@ -587,7 +587,6 @@ sun/security/tools/keytool/ListKeychainStore.sh 8156889 macosx-a
javax/net/ssl/SSLEngine/TestAllSuites.java 8298874 generic-all
javax/net/ssl/SSLEngine/EngineCloseOnAlert.java 8298868 generic-all
javax/net/ssl/SSLEngine/ConnectionTest.java 8298869 generic-all
javax/net/ssl/SSLEngine/CheckStatus.java 8298872 generic-all
sun/security/provider/KeyStore/DKSTest.sh 8180266 windows-all

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2023, 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
@ -28,7 +28,8 @@
* I/O abstraction
* @author Brad Wetmore
*
* @run main/othervm ConnectionTest
* @run main/othervm ConnectionTest TLSv1.2 TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384
* @run main/othervm ConnectionTest TLSv1.3 TLS_AES_256_GCM_SHA384
*/
/*
@ -45,44 +46,46 @@ import java.nio.*;
public class ConnectionTest {
private SSLContext sslc;
private SSLEngine ssle1;
private SSLEngine ssle2;
private final SSLEngine clientEngine;
private final SSLEngine serverEngine;
private static String pathToStores = "../etc";
private static String keyStoreFile = "keystore";
private static String trustStoreFile = "truststore";
private static String passwd = "passphrase";
private static final String PATH_TO_STORES = "../etc";
private static final String KEYSTORE_FILE = "keystore";
private static final String TRUSTSTORE_FILE = "truststore";
private static String keyFilename =
System.getProperty("test.src", "./") + "/" + pathToStores +
"/" + keyStoreFile;
private static String trustFilename =
System.getProperty("test.src", "./") + "/" + pathToStores +
"/" + trustStoreFile;
private static final String KEYSTORE_PATH =
System.getProperty("test.src", "./") + "/" + PATH_TO_STORES +
"/" + KEYSTORE_FILE;
private static final String TRUSTSTORE_PATH =
System.getProperty("test.src", "./") + "/" + PATH_TO_STORES +
"/" + TRUSTSTORE_FILE;
private ByteBuffer appIn1, appOut1;
private ByteBuffer appIn2, appOut2;
private ByteBuffer oneToTwo, twoToOne;
private ByteBuffer clientIn, clientOut;
private ByteBuffer serverIn, serverOut;
private ByteBuffer clientToServer, serverToClient;
private ByteBuffer emptyBuffer;
private ByteBuffer oneToTwoShifter, twoToOneShifter;
private ByteBuffer clientToServerShifter, serverToClientShifter;
private String hostname = "hostname";
private int portNumber = 77;
private final String HOSTNAME = "hostname";
public ConnectionTest()
private final int PORT_NUMBER = 77;
public ConnectionTest(String enabledProtocol, String enabledCipherSuite)
throws Exception {
sslc = getSSLContext();
ssle1 = sslc.createSSLEngine(hostname, portNumber);
ssle2 = sslc.createSSLEngine();
SSLContext sslContext = getSSLContext();
clientEngine = sslContext.createSSLEngine(HOSTNAME, PORT_NUMBER);
serverEngine = sslContext.createSSLEngine();
ssle1.setEnabledCipherSuites(new String [] {
"SSL_RSA_WITH_RC4_128_MD5"});
clientEngine.setEnabledCipherSuites(new String [] {
enabledCipherSuite});
clientEngine.setEnabledProtocols(new String[]{enabledProtocol});
serverEngine.setEnabledCipherSuites(new String [] {
enabledCipherSuite});
serverEngine.setEnabledProtocols(new String[]{enabledProtocol});
ssle2.setEnabledCipherSuites(new String [] {
"SSL_RSA_WITH_RC4_128_MD5"});
createBuffers();
}
@ -92,8 +95,8 @@ public class ConnectionTest {
KeyStore ts = KeyStore.getInstance("JKS");
char[] passphrase = "passphrase".toCharArray();
ks.load(new FileInputStream(keyFilename), passphrase);
ts.load(new FileInputStream(trustFilename), passphrase);
ks.load(new FileInputStream(KEYSTORE_PATH), passphrase);
ts.load(new FileInputStream(TRUSTSTORE_PATH), passphrase);
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
kmf.init(ks, passphrase);
@ -110,62 +113,62 @@ public class ConnectionTest {
private void createBuffers() {
// Size the buffers as appropriate.
SSLSession session = ssle1.getSession();
SSLSession session = clientEngine.getSession();
int appBufferMax = session.getApplicationBufferSize();
int netBufferMax = session.getPacketBufferSize();
appIn1 = ByteBuffer.allocateDirect(appBufferMax + 10);
appIn2 = ByteBuffer.allocateDirect(appBufferMax + 10);
clientIn = ByteBuffer.allocateDirect(appBufferMax + 10);
serverIn = ByteBuffer.allocateDirect(appBufferMax + 10);
appIn1.position(10);
appIn2.position(10);
clientIn.position(10);
serverIn.position(10);
oneToTwo = ByteBuffer.allocateDirect(netBufferMax + 10);
twoToOne = ByteBuffer.allocateDirect(netBufferMax + 10);
clientToServer = ByteBuffer.allocateDirect(netBufferMax + 10);
serverToClient = ByteBuffer.allocateDirect(netBufferMax + 10);
oneToTwo.position(10);
twoToOne.position(10);
oneToTwoShifter = oneToTwo.slice();
twoToOneShifter = twoToOne.slice();
clientToServer.position(10);
serverToClient.position(10);
clientToServerShifter = clientToServer.slice();
serverToClientShifter = serverToClient.slice();
appOut1 = ByteBuffer.wrap("Hi Engine2, I'm SSLEngine1".getBytes());
appOut2 = ByteBuffer.wrap("Hello Engine1, I'm SSLEngine2".getBytes());
clientOut = ByteBuffer.wrap("Hi Engine2, I'm SSLEngine1".getBytes());
serverOut = ByteBuffer.wrap("Hello Engine1, I'm SSLEngine2".getBytes());
emptyBuffer = ByteBuffer.allocate(10);
emptyBuffer.limit(5);
emptyBuffer.position(emptyBuffer.limit());
System.out.println("AppOut1 = " + appOut1);
System.out.println("AppOut2 = " + appOut2);
System.out.println();
log("clientOut = " + clientOut);
log("serverOut = " + serverOut);
log("");
}
private void checkResult(SSLEngineResult result, Status status,
HandshakeStatus hsStatus, int consumed, int produced,
boolean done) throws Exception {
boolean done) {
if ((status != null) && (result.getStatus() != status)) {
throw new Exception("Unexpected Status: need = " + status +
throw new RuntimeException("Unexpected Status: need = " + status +
" got = " + result.getStatus());
}
if ((hsStatus != null) && (result.getHandshakeStatus() != hsStatus)) {
throw new Exception("Unexpected hsStatus: need = " + hsStatus +
throw new RuntimeException("Unexpected hsStatus: need = " + hsStatus +
" got = " + result.getHandshakeStatus());
}
if ((consumed != -1) && (consumed != result.bytesConsumed())) {
throw new Exception("Unexpected consumed: need = " + consumed +
throw new RuntimeException("Unexpected consumed: need = " + consumed +
" got = " + result.bytesConsumed());
}
if ((produced != -1) && (produced != result.bytesProduced())) {
throw new Exception("Unexpected produced: need = " + produced +
throw new RuntimeException("Unexpected produced: need = " + produced +
" got = " + result.bytesProduced());
}
if (done && (hsStatus == HandshakeStatus.FINISHED)) {
throw new Exception(
throw new RuntimeException(
"Handshake already reported finished");
}
@ -176,13 +179,13 @@ public class ConnectionTest {
}
private void test() throws Exception {
ssle1.setUseClientMode(true);
ssle2.setUseClientMode(false);
ssle2.setNeedClientAuth(true);
clientEngine.setUseClientMode(true);
serverEngine.setUseClientMode(false);
serverEngine.setNeedClientAuth(true);
System.out.println("Testing for early unwrap/wrap");
SSLEngineResult result1 = ssle1.unwrap(twoToOne, appIn1);
SSLEngineResult result2 = ssle2.wrap(appOut2, oneToTwo);
log("Testing for early unwrap/wrap");
SSLEngineResult result1 = clientEngine.unwrap(serverToClient, clientIn);
SSLEngineResult result2 = serverEngine.wrap(serverOut, clientToServer);
/*
* These should not consume/produce data, because they
@ -194,195 +197,66 @@ public class ConnectionTest {
checkResult(result2, Status.OK, HandshakeStatus.NEED_UNWRAP,
0, 0, false);
System.out.println("Doing Initial Handshake");
boolean done1 = false;
boolean done2 = false;
log("Doing Initial Handshake");
/*
* Do initial handshaking
*/
while (isHandshaking(ssle1) ||
isHandshaking(ssle2)) {
handshake();
System.out.println("================");
checkEngineAndSession();
result1 = ssle1.wrap(emptyBuffer, oneToTwo);
checkResult(result1, null, null, 0, -1, done1);
result2 = ssle2.wrap(emptyBuffer, twoToOne);
checkResult(result2, null, null, 0, -1, done2);
if (result1.getHandshakeStatus() == HandshakeStatus.FINISHED) {
done1 = true;
}
if (result2.getHandshakeStatus() == HandshakeStatus.FINISHED) {
done2 = true;
}
System.out.println("wrap1 = " + result1);
System.out.println("wrap2 = " + result2);
if (result1.getHandshakeStatus() == HandshakeStatus.NEED_TASK) {
Runnable runnable;
while ((runnable = ssle1.getDelegatedTask()) != null) {
runnable.run();
}
}
if (result2.getHandshakeStatus() == HandshakeStatus.NEED_TASK) {
Runnable runnable;
while ((runnable = ssle2.getDelegatedTask()) != null) {
runnable.run();
}
}
oneToTwo.flip();
twoToOne.flip();
oneToTwo.position(10);
twoToOne.position(10);
System.out.println("----");
result1 = ssle1.unwrap(twoToOne, appIn1);
checkResult(result1, null, null, -1, 0, done1);
result2 = ssle2.unwrap(oneToTwo, appIn2);
checkResult(result2, null, null, -1, 0, done2);
if (result1.getHandshakeStatus() == HandshakeStatus.FINISHED) {
done1 = true;
}
if (result2.getHandshakeStatus() == HandshakeStatus.FINISHED) {
done2 = true;
}
if (result1.getHandshakeStatus() == HandshakeStatus.NEED_TASK) {
Runnable runnable;
while ((runnable = ssle1.getDelegatedTask()) != null) {
runnable.run();
}
}
if (result2.getHandshakeStatus() == HandshakeStatus.NEED_TASK) {
Runnable runnable;
while ((runnable = ssle2.getDelegatedTask()) != null) {
runnable.run();
}
}
System.out.println("unwrap1 = " + result1);
System.out.println("unwrap2 = " + result2);
oneToTwoShifter.position(oneToTwo.position() - 10);
oneToTwoShifter.limit(oneToTwo.limit() - 10);
twoToOneShifter.position(twoToOne.position() - 10);
twoToOneShifter.limit(twoToOne.limit() - 10);
oneToTwoShifter.compact();
twoToOneShifter.compact();
oneToTwo.position(oneToTwoShifter.position() + 10);
oneToTwo.limit(oneToTwoShifter.limit() + 10);
twoToOne.position(twoToOneShifter.position() + 10);
twoToOne.limit(twoToOneShifter.limit() + 10);
}
System.out.println("\nDONE HANDSHAKING");
System.out.println("================");
if (!done1 || !done2) {
throw new Exception("Both should be true:\n" +
" done1 = " + done1 + " done2 = " + done2);
}
String host = ssle1.getPeerHost();
int port = ssle1.getPeerPort();
if (!host.equals(hostname) || (port != portNumber)) {
throw new Exception("unexpected host/port " + host + ":" + port);
}
host = ssle2.getPeerHost();
port = ssle2.getPeerPort();
if ((host != null) || (port != -1)) {
throw new Exception("unexpected host/port " + host + ":" + port);
}
SSLSession ssls1 = ssle1.getSession();
host = ssls1.getPeerHost();
port = ssls1.getPeerPort();
if (!host.equals(hostname) || (port != portNumber)) {
throw new Exception("unexpected host/port " + host + ":" + port);
}
SSLSession ssls2 = ssle2.getSession();
host = ssls2.getPeerHost();
port = ssls2.getPeerPort();
if ((host != null) || (port != -1)) {
throw new Exception("unexpected host/port " + host + ":" + port);
}
SSLSession clientSession1 = clientEngine.getSession();
SSLSession serverSession1 = serverEngine.getSession();
/*
* Should be able to write/read a small buffer like this.
*/
int appOut1Len = appOut1.remaining();
int appOut2Len = appOut2.remaining();
int appOut1Len = clientOut.remaining();
int appOut2Len = serverOut.remaining();
int net1Len;
int net2Len;
result1 = ssle1.wrap(appOut1, oneToTwo);
result1 = clientEngine.wrap(clientOut, clientToServer);
checkResult(result1, Status.OK, HandshakeStatus.NOT_HANDSHAKING,
appOut1Len, -1, false);
result2 = ssle2.wrap(appOut2, twoToOne);
result2 = serverEngine.wrap(serverOut, serverToClient);
checkResult(result2, Status.OK, HandshakeStatus.NOT_HANDSHAKING,
appOut2Len, -1, false);
net1Len = result1.bytesProduced();
net2Len = result2.bytesProduced();
System.out.println("wrap1 = " + result1);
System.out.println("wrap2 = " + result2);
log("wrap1 = " + result1);
log("wrap2 = " + result2);
oneToTwo.flip();
twoToOne.flip();
clientToServer.flip();
serverToClient.flip();
oneToTwo.position(10);
twoToOne.position(10);
clientToServer.position(10);
serverToClient.position(10);
System.out.println("----");
log("----");
result1 = ssle1.unwrap(twoToOne, appIn1);
result1 = clientEngine.unwrap(serverToClient, clientIn);
checkResult(result1, Status.OK, HandshakeStatus.NOT_HANDSHAKING,
net2Len, appOut2Len, false);
result2 = ssle2.unwrap(oneToTwo, appIn2);
result2 = serverEngine.unwrap(clientToServer, serverIn);
checkResult(result2, Status.OK, HandshakeStatus.NOT_HANDSHAKING,
net1Len, appOut1Len, false);
System.out.println("unwrap1 = " + result1);
System.out.println("unwrap2 = " + result2);
log("unwrap1 = " + result1);
log("unwrap2 = " + result2);
oneToTwoShifter.position(oneToTwo.position() - 10);
oneToTwoShifter.limit(oneToTwo.limit() - 10);
twoToOneShifter.position(twoToOne.position() - 10);
twoToOneShifter.limit(twoToOne.limit() - 10);
oneToTwoShifter.compact();
twoToOneShifter.compact();
oneToTwo.position(oneToTwoShifter.position() + 10);
oneToTwo.limit(oneToTwoShifter.limit() + 10);
twoToOne.position(twoToOneShifter.position() + 10);
twoToOne.limit(twoToOneShifter.limit() + 10);
updateByteBuffers();
ssls2.invalidate();
ssle2.beginHandshake();
serverSession1.invalidate();
serverEngine.beginHandshake();
System.out.println("\nRENEGOTIATING");
System.out.println("=============");
log("\nRENEGOTIATING");
log("=============");
done1 = false;
done2 = false;
appIn1.clear();
appIn2.clear();
clientIn.clear();
serverIn.clear();
/*
* Do a quick test to see if this can do a switch
@ -390,295 +264,343 @@ public class ConnectionTest {
* to switch back.
*/
try {
System.out.println("Try to change client mode");
ssle2.setUseClientMode(true);
throw new Exception("Should have thrown IllegalArgumentException");
log("Try to change client mode");
serverEngine.setUseClientMode(true);
throw new RuntimeException("Should have thrown IllegalArgumentException");
} catch (IllegalArgumentException e) {
System.out.println("Caught correct IllegalArgumentException");
log("Caught correct IllegalArgumentException");
}
while (isHandshaking(ssle1) ||
isHandshaking(ssle2)) {
handshake();
System.out.println("================");
SSLSession clientSession2 = clientEngine.getSession();
SSLSession serverSession2 = serverEngine.getSession();
result1 = ssle1.wrap(emptyBuffer, oneToTwo);
checkResult(result1, null, null, 0, -1, done1);
result2 = ssle2.wrap(emptyBuffer, twoToOne);
checkResult(result2, null, null, 0, -1, done2);
if (result1.getHandshakeStatus() == HandshakeStatus.FINISHED) {
done1 = true;
}
log("\nDoing close");
log("===========");
if (result2.getHandshakeStatus() == HandshakeStatus.FINISHED) {
done2 = true;
}
clientEngine.closeOutbound();
serverEngine.closeOutbound();
System.out.println("wrap1 = " + result1);
System.out.println("wrap2 = " + result2);
clientToServer.flip();
serverToClient.flip();
clientToServer.position(10);
serverToClient.position(10);
if (result1.getHandshakeStatus() == HandshakeStatus.NEED_TASK) {
Runnable runnable;
while ((runnable = ssle1.getDelegatedTask()) != null) {
runnable.run();
}
}
clientIn.clear();
serverIn.clear();
if (result2.getHandshakeStatus() == HandshakeStatus.NEED_TASK) {
Runnable runnable;
while ((runnable = ssle2.getDelegatedTask()) != null) {
runnable.run();
}
}
oneToTwo.flip();
twoToOne.flip();
oneToTwo.position(10);
twoToOne.position(10);
System.out.println("----");
result1 = ssle1.unwrap(twoToOne, appIn1);
checkResult(result1, null, null, -1, 0, done1);
result2 = ssle2.unwrap(oneToTwo, appIn2);
checkResult(result2, null, null, -1, 0, done2);
if (result1.getHandshakeStatus() == HandshakeStatus.FINISHED) {
done1 = true;
}
if (result2.getHandshakeStatus() == HandshakeStatus.FINISHED) {
done2 = true;
}
System.out.println("unwrap1 = " + result1);
System.out.println("unwrap2 = " + result2);
if (result1.getHandshakeStatus() == HandshakeStatus.NEED_TASK) {
Runnable runnable;
while ((runnable = ssle1.getDelegatedTask()) != null) {
runnable.run();
}
}
if (result2.getHandshakeStatus() == HandshakeStatus.NEED_TASK) {
Runnable runnable;
while ((runnable = ssle2.getDelegatedTask()) != null) {
runnable.run();
}
}
oneToTwoShifter.position(oneToTwo.position() - 10);
oneToTwoShifter.limit(oneToTwo.limit() - 10);
twoToOneShifter.position(twoToOne.position() - 10);
twoToOneShifter.limit(twoToOne.limit() - 10);
oneToTwoShifter.compact();
twoToOneShifter.compact();
oneToTwo.position(oneToTwoShifter.position() + 10);
oneToTwo.limit(oneToTwoShifter.limit() + 10);
twoToOne.position(twoToOneShifter.position() + 10);
twoToOne.limit(twoToOneShifter.limit() + 10);
}
host = ssle1.getPeerHost();
port = ssle1.getPeerPort();
if (!host.equals(hostname) || (port != portNumber)) {
throw new Exception("unexpected host/port " + host + ":" + port);
}
host = ssle2.getPeerHost();
port = ssle2.getPeerPort();
if ((host != null) || (port != -1)) {
throw new Exception("unexpected host/port " + host + ":" + port);
}
SSLSession ssls3 = ssle2.getSession();
host = ssls1.getPeerHost();
port = ssls1.getPeerPort();
if (!host.equals(hostname) || (port != portNumber)) {
throw new Exception("unexpected host/port " + host + ":" + port);
}
SSLSession ssls4 = ssle2.getSession();
host = ssls2.getPeerHost();
port = ssls2.getPeerPort();
if ((host != null) || (port != -1)) {
throw new Exception("unexpected host/port " + host + ":" + port);
}
System.out.println("\nDoing close");
System.out.println("===========");
ssle1.closeOutbound();
ssle2.closeOutbound();
oneToTwo.flip();
twoToOne.flip();
oneToTwo.position(10);
twoToOne.position(10);
appIn1.clear();
appIn2.clear();
System.out.println("LAST UNWRAP");
result1 = ssle1.unwrap(twoToOne, appIn1);
log("LAST UNWRAP");
result1 = clientEngine.unwrap(serverToClient, clientIn);
checkResult(result1, Status.BUFFER_UNDERFLOW,
HandshakeStatus.NEED_WRAP, 0, 0, false);
result2 = ssle2.unwrap(oneToTwo, appIn2);
result2 = serverEngine.unwrap(clientToServer, serverIn);
checkResult(result2, Status.BUFFER_UNDERFLOW,
HandshakeStatus.NEED_WRAP, 0, 0, false);
System.out.println("unwrap1 = " + result1);
System.out.println("unwrap2 = " + result2);
log("unwrap1 = " + result1);
log("unwrap2 = " + result2);
oneToTwoShifter.position(oneToTwo.position() - 10);
oneToTwoShifter.limit(oneToTwo.limit() - 10);
twoToOneShifter.position(twoToOne.position() - 10);
twoToOneShifter.limit(twoToOne.limit() - 10);
oneToTwoShifter.compact();
twoToOneShifter.compact();
oneToTwo.position(oneToTwoShifter.position() + 10);
oneToTwo.limit(oneToTwoShifter.limit() + 10);
twoToOne.position(twoToOneShifter.position() + 10);
twoToOne.limit(twoToOneShifter.limit() + 10);
updateByteBuffers();
System.out.println("LAST WRAP");
result1 = ssle1.wrap(appOut1, oneToTwo);
checkResult(result1, Status.CLOSED, HandshakeStatus.NEED_UNWRAP,
log("LAST WRAP");
result1 = clientEngine.wrap(clientOut, clientToServer);
checkResult(result1, Status.CLOSED, HandshakeStatus.NOT_HANDSHAKING,
0, -1, false);
result2 = ssle2.wrap(appOut2, twoToOne);
checkResult(result2, Status.CLOSED, HandshakeStatus.NEED_UNWRAP,
result2 = serverEngine.wrap(serverOut, serverToClient);
checkResult(result2, Status.CLOSED, HandshakeStatus.NOT_HANDSHAKING,
0, -1, false);
System.out.println("wrap1 = " + result1);
System.out.println("wrap2 = " + result2);
log("wrap1 = " + result1);
log("wrap2 = " + result2);
net1Len = result1.bytesProduced();
net2Len = result2.bytesProduced();
oneToTwo.flip();
twoToOne.flip();
clientToServer.flip();
serverToClient.flip();
oneToTwo.position(10);
twoToOne.position(10);
clientToServer.position(10);
serverToClient.position(10);
result1 = ssle1.unwrap(twoToOne, appIn1);
result1 = clientEngine.unwrap(serverToClient, clientIn);
checkResult(result1, Status.CLOSED, HandshakeStatus.NOT_HANDSHAKING,
net1Len, 0, false);
result2 = ssle2.unwrap(oneToTwo, appIn2);
result2 = serverEngine.unwrap(clientToServer, serverIn);
checkResult(result2, Status.CLOSED, HandshakeStatus.NOT_HANDSHAKING,
net2Len, 0, false);
System.out.println("unwrap1 = " + result1);
System.out.println("unwrap2 = " + result2);
log("unwrap1 = " + result1);
log("unwrap2 = " + result2);
oneToTwoShifter.position(oneToTwo.position() - 10);
oneToTwoShifter.limit(oneToTwo.limit() - 10);
twoToOneShifter.position(twoToOne.position() - 10);
twoToOneShifter.limit(twoToOne.limit() - 10);
oneToTwoShifter.compact();
twoToOneShifter.compact();
oneToTwo.position(oneToTwoShifter.position() + 10);
oneToTwo.limit(oneToTwoShifter.limit() + 10);
twoToOne.position(twoToOneShifter.position() + 10);
twoToOne.limit(twoToOneShifter.limit() + 10);
updateByteBuffers();
System.out.println("EXTRA WRAP");
result1 = ssle1.wrap(appOut1, oneToTwo);
log("EXTRA WRAP");
result1 = clientEngine.wrap(clientOut, clientToServer);
checkResult(result1, Status.CLOSED, HandshakeStatus.NOT_HANDSHAKING,
0, 0, false);
result2 = ssle2.wrap(appOut2, twoToOne);
result2 = serverEngine.wrap(serverOut, serverToClient);
checkResult(result2, Status.CLOSED, HandshakeStatus.NOT_HANDSHAKING,
0, 0, false);
System.out.println("wrap1 = " + result1);
System.out.println("wrap2 = " + result2);
log("wrap1 = " + result1);
log("wrap2 = " + result2);
oneToTwo.flip();
twoToOne.flip();
oneToTwo.position(10);
twoToOne.position(10);
clientToServer.flip();
serverToClient.flip();
clientToServer.position(10);
serverToClient.position(10);
System.out.println("EXTRA UNWRAP");
result1 = ssle1.unwrap(twoToOne, appIn1);
log("EXTRA UNWRAP");
result1 = clientEngine.unwrap(serverToClient, clientIn);
checkResult(result1, Status.CLOSED, HandshakeStatus.NOT_HANDSHAKING,
0, 0, false);
result2 = ssle2.unwrap(oneToTwo, appIn2);
result2 = serverEngine.unwrap(clientToServer, serverIn);
checkResult(result2, Status.CLOSED, HandshakeStatus.NOT_HANDSHAKING,
0, 0, false);
System.out.println("unwrap1 = " + result1);
System.out.println("unwrap2 = " + result2);
log("unwrap1 = " + result1);
log("unwrap2 = " + result2);
checkSession(ssls1, ssls2, ssls3, ssls4);
System.out.println(ssle1);
System.out.println(ssle2);
checkSession(clientSession1, serverSession1, clientSession2, serverSession2);
log(clientEngine);
log(serverEngine);
}
private static void checkSession(SSLSession ssls1, SSLSession ssls2,
SSLSession ssls3, SSLSession ssls4) throws Exception {
System.out.println("\nSession Info for SSLEngine1");
System.out.println(ssls1);
System.out.println(ssls1.getCreationTime());
String peer1 = ssls1.getPeerHost();
System.out.println(peer1);
String protocol1 = ssls1.getProtocol();
System.out.println(protocol1);
java.security.cert.Certificate cert1 = ssls1.getPeerCertificates()[0];
System.out.println(cert1);
String ciphersuite1 = ssls1.getCipherSuite();
System.out.println(ciphersuite1);
System.out.println();
private void handshake() throws Exception {
boolean clientDone = false;
boolean serverDone = false;
SSLEngineResult result2;
SSLEngineResult result1;
while (isHandshaking(clientEngine) ||
isHandshaking(serverEngine)) {
System.out.println("\nSession Info for SSLEngine2");
System.out.println(ssls2);
System.out.println(ssls2.getCreationTime());
String peer2 = ssls2.getPeerHost();
System.out.println(peer2);
String protocol2 = ssls2.getProtocol();
System.out.println(protocol2);
java.security.cert.Certificate cert2 = ssls2.getPeerCertificates()[0];
System.out.println(cert2);
String ciphersuite2 = ssls2.getCipherSuite();
System.out.println(ciphersuite2);
System.out.println();
log("================");
result1 = clientEngine.wrap(emptyBuffer, clientToServer);
checkResult(result1, null, null, 0, -1, clientDone);
result2 = serverEngine.wrap(emptyBuffer, serverToClient);
checkResult(result2, null, null, 0, -1, serverDone);
if (result1.getHandshakeStatus() == HandshakeStatus.FINISHED) {
clientDone = true;
}
if (result2.getHandshakeStatus() == HandshakeStatus.FINISHED) {
serverDone = true;
}
log("wrap1 = " + result1);
log("wrap2 = " + result2);
if (result1.getHandshakeStatus() == HandshakeStatus.NEED_TASK) {
Runnable runnable;
while ((runnable = clientEngine.getDelegatedTask()) != null) {
runnable.run();
}
}
if (result2.getHandshakeStatus() == HandshakeStatus.NEED_TASK) {
Runnable runnable;
while ((runnable = serverEngine.getDelegatedTask()) != null) {
runnable.run();
}
}
clientToServer.flip();
serverToClient.flip();
clientToServer.position(10);
serverToClient.position(10);
log("----");
result1 = clientEngine.unwrap(serverToClient, clientIn);
checkResult(result1, null, null, -1, 0, clientDone);
result2 = serverEngine.unwrap(clientToServer, serverIn);
checkResult(result2, null, null, -1, 0, serverDone);
if (result1.getHandshakeStatus() == HandshakeStatus.FINISHED) {
clientDone = true;
}
if (result2.getHandshakeStatus() == HandshakeStatus.FINISHED) {
serverDone = true;
}
log("unwrap1 = " + result1);
log("unwrap2 = " + result2);
if (result1.getHandshakeStatus() == HandshakeStatus.NEED_TASK) {
Runnable runnable;
while ((runnable = clientEngine.getDelegatedTask()) != null) {
runnable.run();
}
}
if (result2.getHandshakeStatus() == HandshakeStatus.NEED_TASK) {
Runnable runnable;
while ((runnable = serverEngine.getDelegatedTask()) != null) {
runnable.run();
}
}
updateByteBuffers();
}
log("\nDONE HANDSHAKING");
log("================");
if (!clientDone || !serverDone) {
throw new RuntimeException("Both should be true:\n" +
" clientDone = " + clientDone + " serverDone = " + serverDone);
}
}
private void updateByteBuffers() {
clientToServerShifter.position(clientToServer.position() - 10);
clientToServerShifter.limit(clientToServer.limit() - 10);
serverToClientShifter.position(serverToClient.position() - 10);
serverToClientShifter.limit(serverToClient.limit() - 10);
clientToServerShifter.compact();
serverToClientShifter.compact();
clientToServer.position(clientToServerShifter.position() + 10);
clientToServer.limit(clientToServerShifter.limit() + 10);
serverToClient.position(serverToClientShifter.position() + 10);
serverToClient.limit(serverToClientShifter.limit() + 10);
}
private static void checkSession(SSLSession clientSession1, SSLSession serverSession1,
SSLSession clientSession2, SSLSession serverSession2) {
log("\nSession Info for client SSLEngine 1");
log(clientSession1);
log(clientSession1.getCreationTime());
String peer1 = clientSession1.getPeerHost();
log(peer1);
String protocol1 = clientSession1.getProtocol();
log(protocol1);
String ciphersuite1 = clientSession1.getCipherSuite();
log(ciphersuite1);
log("");
log("\nSession Info for server SSLEngine 1");
log(serverSession1);
log(serverSession1.getCreationTime());
String peer2 = serverSession1.getPeerHost();
log(peer2);
String protocol2 = serverSession1.getProtocol();
log(protocol2);
String ciphersuite2 = serverSession1.getCipherSuite();
log(ciphersuite2);
log("");
if (peer1.equals(peer2)) {
throw new Exception("peer hostnames not equal");
throw new RuntimeException("peer hostnames not equal");
}
if (!protocol1.equals(protocol2)) {
throw new Exception("protocols not equal");
throw new RuntimeException("protocols not equal");
}
if (!cert1.equals(cert2)) {
throw new Exception("certs not equal");
}
compareCertificates(clientSession1, serverSession1);
compareCertificates(clientSession2, serverSession2);
if (!ciphersuite1.equals(ciphersuite2)) {
throw new Exception("ciphersuites not equal");
throw new RuntimeException("ciphersuites not equal");
}
System.out.println("\nSession Info for SSLEngine3");
System.out.println(ssls3);
System.out.println("\nSession Info for SSLEngine4");
System.out.println(ssls4);
log("\nSession Info for client SSLEngine 2");
log(clientSession2);
log("\nSession Info for server SSLEngine 2");
log(serverSession2);
}
if (ssls3.equals(ssls1) || ssls4.equals(ssls2)) {
throw new Exception("sessions should not be equals");
private static void compareCertificates(SSLSession client, SSLSession server) {
try {
java.security.cert.Certificate clientLocal = client.getLocalCertificates()[0];
java.security.cert.Certificate clientPeer = client.getPeerCertificates()[0];
java.security.cert.Certificate serverLocal = server.getLocalCertificates()[0];
java.security.cert.Certificate serverPeer = server.getPeerCertificates()[0];
log(String.format("Client local cert: %s%nClient peer cert: %s%n"
+ "Server local cert: %s%nServer peer cert: %s%n",
clientLocal, clientPeer, serverLocal, serverPeer));
if (!clientLocal.equals(serverPeer)) {
throw new RuntimeException("Client's local certificate does "
+ "not match server's peer certificate");
}
if (!clientPeer.equals(serverLocal)) {
throw new RuntimeException("Client's peer certificate does "
+ "not match server's local certificate");
}
} catch (SSLPeerUnverifiedException e) {
throw new RuntimeException("Could not get peer certificate!", e);
}
}
private void checkEngineAndSession()
throws Exception {
String host = clientEngine.getPeerHost();
int port = clientEngine.getPeerPort();
if (!host.equals(HOSTNAME) || (port != PORT_NUMBER)) {
throw new Exception("Unexpected host/port from client engine."
+ " Expected " + HOSTNAME + ":" + PORT_NUMBER
+ " Received " +host + ":" + port);
}
host = serverEngine.getPeerHost();
port = serverEngine.getPeerPort();
if ((host != null) || (port != -1)) {
throw new Exception("Unexpected host/port from server engine."
+ " Expected null:-1"
+ " Received " + host + ":" + port);
}
SSLSession clientSession = clientEngine.getSession();
host = clientSession.getPeerHost();
port = clientSession.getPeerPort();
if (!host.equals(HOSTNAME) || (port != PORT_NUMBER)) {
throw new Exception("Unexpected host/port from client session."
+ " Expected " + HOSTNAME + ":" + PORT_NUMBER
+ " Received " + host + ":" + port);
}
SSLSession serverSession = serverEngine.getSession();
host = serverSession.getPeerHost();
port = serverSession.getPeerPort();
if ((host != null) || (port != -1)) {
throw new Exception("Unexpected host/port from server session."
+ " Expected null:-1"
+ " Received " + host + ":" + port);
}
}
private static void log(Object msg) {
System.out.println(msg);
}
public static void main(String args[]) throws Exception {
// reset the security property to make sure that the algorithms
// and keys used in this test are not disabled.
Security.setProperty("jdk.tls.disabledAlgorithms", "");
ConnectionTest ct = new ConnectionTest();
log(String.format("Running with %s and %s%n", args[0], args[1]));
ConnectionTest ct = new ConnectionTest(args[0], args[1]);
ct.test();
}
}
}