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
|
/*
* Copyright (c) 2004, 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.
*/
/*
* @test
* @bug 8133632
* @summary javax.net.ssl.SSLEngine does not properly handle received
* SSL fatal alerts
* @ignore the dependent implementation details are changed
* @run main/othervm EngineCloseOnAlert
*/
import java.io.FileInputStream;
import java.io.IOException;
import javax.net.ssl.*;
import java.nio.ByteBuffer;
import java.util.*;
import java.security.*;
import static javax.net.ssl.SSLEngineResult.HandshakeStatus.*;
public class EngineCloseOnAlert {
private static final String pathToStores = "../etc";
private static final String keyStoreFile = "keystore";
private static final String trustStoreFile = "truststore";
private static final String passwd = "passphrase";
private static final String keyFilename =
System.getProperty("test.src", ".") + "/" + pathToStores +
"/" + keyStoreFile;
private static final String trustFilename =
System.getProperty("test.src", ".") + "/" + pathToStores +
"/" + trustStoreFile;
private static KeyManagerFactory KMF;
private static TrustManagerFactory TMF;
private static TrustManagerFactory EMPTY_TMF;
private static final String[] TLS10ONLY = { "TLSv1" };
private static final String[] TLS12ONLY = { "TLSv1.2" };
private static final String[] ONECIPHER =
{ "TLS_RSA_WITH_AES_128_CBC_SHA" };
public interface TestCase {
public void runTest() throws Exception;
}
public static void main(String[] args) throws Exception {
int failed = 0;
List<TestCase> testMatrix = new LinkedList<TestCase>() {{
add(clientReceivesAlert);
add(serverReceivesAlert);
}};
// Create the various key/trust manager factories we'll need
createManagerFactories();
for (TestCase test : testMatrix) {
try {
test.runTest();
} catch (Exception e) {
System.out.println("Exception in test:\n" + e);
e.printStackTrace(System.out);
failed++;
}
}
System.out.println("Total tests: " + testMatrix.size() + ", passed: " +
(testMatrix.size() - failed) + ", failed: " + failed);
if (failed > 0) {
throw new RuntimeException("One or more tests failed.");
}
}
private static final TestCase clientReceivesAlert = new TestCase() {
@Override
public void runTest() throws Exception {
System.out.println("");
System.out.println("=======================================");
System.out.println("Test: Client receives alert from server");
System.out.println("=======================================");
// For this test, we won't initialize any keystore so the
// server will throw an exception because it has no key/cert to
// match the requested ciphers offered by the client. This
// will generate an alert from the server to the client.
SSLContext context = SSLContext.getDefault();
SSLEngine client = context.createSSLEngine();
SSLEngine server = context.createSSLEngine();
client.setUseClientMode(true);
server.setUseClientMode(false);
SSLEngineResult clientResult;
SSLEngineResult serverResult;
ByteBuffer raw = ByteBuffer.allocate(32768);
ByteBuffer plain = ByteBuffer.allocate(32768);
// Generate the client hello and have the server unwrap it
client.wrap(plain, raw);
checkEngineState(client, NEED_UNWRAP, false, false);
raw.flip();
System.out.println("Client-to-Server:\n-----------------\n" +
dumpHexBytes(raw, 16, "\n", ":"));
// The server should need to run a delegated task while processing
// the client hello data.
serverResult = server.unwrap(raw, plain);
checkEngineState(server, NEED_TASK, false, false);
System.out.println("Server result: " + serverResult);
runDelegatedTasks(serverResult, server);
checkEngineState(server, NEED_WRAP, true, false);
try {
raw.clear();
serverResult = server.wrap(plain, raw);
System.out.println("Server result: " + serverResult);
runDelegatedTasks(serverResult, server);
} catch (SSLException e) {
// This is the expected code path
System.out.println("Server throws exception: " + e);
System.out.println("Server engine state: " +
"isInboundDone = "+ server.isInboundDone() +
", isOutboundDone = " + server.isOutboundDone() +
", handshake status = " + server.getHandshakeStatus());
checkEngineState(server, NEED_WRAP, true, false);
}
raw.clear();
// The above should show that isInboundDone returns true, and
// handshake status is NEED_WRAP. That is the correct behavior,
// wrap will put a fatal alert message in the buffer.
serverResult = server.wrap(plain, raw);
System.out.println("Server result (wrap after exception): " +
serverResult);
System.out.println("Server engine closure state: isInboundDone="
+ server.isInboundDone() + ", isOutboundDone="
+ server.isOutboundDone());
checkEngineState(server, NEED_UNWRAP, true, true);
raw.flip();
System.out.println("Server-to-Client:\n-----------------\n" +
dumpHexBytes(raw, 16, "\n", ":"));
// Client side will read the fatal alert and throw exception.
try {
clientResult = client.unwrap(raw, plain);
System.out.println("Client result (unwrap alert): " +
clientResult);
} catch (SSLException e) {
System.out.println("Client throws exception: " + e);
System.out.println("Engine closure status: isInboundDone="
+ client.isInboundDone() + ", isOutboundDone="
+ client.isOutboundDone() + ", handshake status="
+ client.getHandshakeStatus());
checkEngineState(client, NOT_HANDSHAKING, true, true);
}
raw.clear();
// Last test, we try to unwrap
clientResult = client.unwrap(raw, plain);
checkEngineState(client, NOT_HANDSHAKING, true, true);
System.out.println("Client result (wrap after exception): " +
clientResult);
}
};
private static final TestCase serverReceivesAlert = new TestCase() {
@Override
public void runTest() throws Exception {
SSLContext cliContext = SSLContext.getDefault();
SSLContext servContext = SSLContext.getInstance("TLS");
servContext.init(KMF.getKeyManagers(), TMF.getTrustManagers(),
null);
SSLEngine client = cliContext.createSSLEngine();
SSLEngine server = servContext.createSSLEngine();
client.setUseClientMode(true);
client.setEnabledProtocols(TLS12ONLY);
client.setEnabledCipherSuites(ONECIPHER);
server.setUseClientMode(false);
server.setEnabledProtocols(TLS10ONLY);
SSLEngineResult clientResult;
SSLEngineResult serverResult;
ByteBuffer raw = ByteBuffer.allocate(32768);
ByteBuffer plain = ByteBuffer.allocate(32768);
System.out.println("");
System.out.println("=======================================");
System.out.println("Test: Server receives alert from client");
System.out.println("=======================================");
// Generate the client hello and have the server unwrap it
checkEngineState(client, NOT_HANDSHAKING, false, false);
client.wrap(plain, raw);
checkEngineState(client, NEED_UNWRAP, false, false);
raw.flip();
System.out.println("Client-to-Server:\n-----------------\n" +
dumpHexBytes(raw, 16, "\n", ":"));
// The server should need to run a delegated task while processing
// the client hello data.
serverResult = server.unwrap(raw, plain);
checkEngineState(server, NEED_TASK, false, false);
runDelegatedTasks(serverResult, server);
checkEngineState(server, NEED_WRAP, false, false);
raw.compact();
// The server should now wrap the response back to the client
server.wrap(plain, raw);
checkEngineState(server, NEED_UNWRAP, false, false);
raw.flip();
System.out.println("Server-to-Client:\n-----------------\n" +
dumpHexBytes(raw, 16, "\n", ":"));
// The client should parse this and throw an exception because
// It is unwiling to do TLS 1.0
clientResult = client.unwrap(raw, plain);
checkEngineState(client, NEED_TASK, false, false);
runDelegatedTasks(clientResult, client);
checkEngineState(client, NEED_UNWRAP, false, false);
try {
client.unwrap(raw, plain);
} catch (SSLException e) {
System.out.println("Client throws exception: " + e);
System.out.println("Engine closure status: isInboundDone="
+ client.isInboundDone() + ", isOutboundDone="
+ client.isOutboundDone() + ", handshake status="
+ client.getHandshakeStatus());
checkEngineState(client, NEED_WRAP, true, false);
}
raw.clear();
// Now the client should wrap the exception
client.wrap(plain, raw);
checkEngineState(client, NEED_UNWRAP, true, true);
raw.flip();
System.out.println("Client-to-Server:\n-----------------\n" +
dumpHexBytes(raw, 16, "\n", ":"));
try {
server.unwrap(raw, plain);
checkEngineState(server, NEED_UNWRAP, false, false);
} catch (SSLException e) {
System.out.println("Server throws exception: " + e);
System.out.println("Engine closure status: isInboundDone="
+ server.isInboundDone() + ", isOutboundDone="
+ server.isOutboundDone() + ", handshake status="
+ server.getHandshakeStatus());
checkEngineState(server, NOT_HANDSHAKING, true, true);
}
raw.clear();
}
};
/*
* If the result indicates that we have outstanding tasks to do,
* go ahead and run them in this thread.
*/
private static void runDelegatedTasks(SSLEngineResult result,
SSLEngine engine) throws Exception {
if (result.getHandshakeStatus() ==
SSLEngineResult.HandshakeStatus.NEED_TASK) {
Runnable runnable;
while ((runnable = engine.getDelegatedTask()) != null) {
System.out.println("\trunning delegated task...");
runnable.run();
}
SSLEngineResult.HandshakeStatus hsStatus =
engine.getHandshakeStatus();
if (hsStatus == SSLEngineResult.HandshakeStatus.NEED_TASK) {
throw new Exception(
"handshake shouldn't need additional tasks");
}
System.out.println("\tnew HandshakeStatus: " + hsStatus);
}
}
/**
*
* @param data The array of bytes to dump to stdout.
* @param itemsPerLine The number of bytes to display per line
* if the {@code lineDelim} character is blank then all bytes will be
* printed on a single line.
* @param lineDelim The delimiter between lines
* @param itemDelim The delimiter between bytes
*
* @return The hexdump of the byte array
*/
private static String dumpHexBytes(ByteBuffer data, int itemsPerLine,
String lineDelim, String itemDelim) {
StringBuilder sb = new StringBuilder();
if (data != null) {
data.mark();
for (int i = 0; i < data.limit(); i++) {
if (i % itemsPerLine == 0 && i != 0) {
sb.append(lineDelim);
}
sb.append(String.format("%02X", data.get(i)));
if (i % itemsPerLine != (itemsPerLine - 1) &&
i != (data.limit() -1)) {
sb.append(itemDelim);
}
}
data.reset();
}
return sb.toString();
}
private static void createManagerFactories()
throws GeneralSecurityException, IOException {
KeyStore keystore = KeyStore.getInstance("PKCS12");
KeyStore truststore = KeyStore.getInstance("PKCS12");
KeyStore empty_ts = KeyStore.getInstance("PKCS12");
char[] passphrase = passwd.toCharArray();
keystore.load(new FileInputStream(keyFilename), passphrase);
truststore.load(new FileInputStream(trustFilename), passphrase);
empty_ts.load(null, "".toCharArray());
KMF = KeyManagerFactory.getInstance("PKIX");
KMF.init(keystore, passphrase);
TMF = TrustManagerFactory.getInstance("PKIX");
TMF.init(truststore);
EMPTY_TMF = TrustManagerFactory.getInstance("PKIX");
EMPTY_TMF.init(truststore);
}
private static void checkEngineState(SSLEngine engine,
SSLEngineResult.HandshakeStatus expectedHSStat,
boolean expectedInboundDone, boolean expectedOutboundDone) {
if (engine.getHandshakeStatus() != expectedHSStat ||
engine.isInboundDone() != expectedInboundDone ||
engine.isOutboundDone() != expectedOutboundDone) {
throw new RuntimeException("Error: engine not in expected state\n" +
"Expected: state = " + expectedHSStat +
", inDone = " + expectedInboundDone +
", outDone = " + expectedOutboundDone + "\n" +
"Actual: state = " + engine.getHandshakeStatus() +
", inDone = " + engine.isInboundDone() +
", outDone = " + engine.isOutboundDone());
} else {
System.out.println((engine.getUseClientMode() ?
"Client" : "Server") + " handshake status: " +
engine.getHandshakeStatus() + ", inDone = " +
engine.isInboundDone() + ", outDone = " +
engine.isOutboundDone());
}
}
}
|