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
|
/*
* Copyright (c) 2018, 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
* 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 8164879 8300285
* @library ../../
* /test/lib
* /javax/net/ssl/templates
* @summary Verify AEAD TLS cipher suite limits set in the jdk.tls.keyLimits
* property
* start a new handshake sequence to renegotiate the symmetric key with an
* SSLSocket connection. This test verifies the handshake method was called
* via debugging info. It does not verify the renegotiation was successful
* as that is very hard.
*
* @run main SSLEngineKeyLimit 0 server TLS_AES_256_GCM_SHA384
* AES/GCM/NoPadding keyupdate 1050000
* @run main SSLEngineKeyLimit 1 client TLS_AES_256_GCM_SHA384
* AES/GCM/NoPadding keyupdate 2^22
* @run main SSLEngineKeyLimit 0 server TLS_CHACHA20_POLY1305_SHA256
* AES/GCM/NoPadding keyupdate 1050000, ChaCha20-Poly1305 KeyUpdate 1050000
* @run main SSLEngineKeyLimit 1 client TLS_CHACHA20_POLY1305_SHA256
* AES/GCM/NoPadding keyupdate 2^22, ChaCha20-Poly1305 KeyUpdate 2^22
*/
/*
* This test runs in another process so we can monitor the debug
* results. The OutputAnalyzer must see correct debug output to return a
* success.
*/
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLEngine;
import javax.net.ssl.SSLEngineResult;
import java.io.File;
import java.io.PrintWriter;
import java.nio.ByteBuffer;
import java.util.Arrays;
import jdk.test.lib.process.ProcessTools;
import jdk.test.lib.process.OutputAnalyzer;
import jdk.test.lib.Utils;
public class SSLEngineKeyLimit extends SSLContextTemplate {
SSLEngine eng;
static ByteBuffer cTos;
static ByteBuffer sToc;
static ByteBuffer outdata;
ByteBuffer buf;
static boolean ready = false;
static int dataLen = 10240;
static boolean serverwrite = true;
int totalDataLen = 0;
static boolean sc = true;
int delay = 1;
static boolean readdone = false;
// Turn on debugging
static boolean debug = false;
SSLEngineKeyLimit() {
buf = ByteBuffer.allocate(dataLen*4);
}
/**
* args should have two values: server|client, cipher suite, <limit size>
* Prepending 'p' is for internal use only.
*/
public static void main(String args[]) throws Exception {
for (int i = 0; i < args.length; i++) {
System.out.print(" " + args[i]);
}
System.out.println();
if (args[0].compareTo("p") != 0) {
boolean expectedFail = (Integer.parseInt(args[0]) == 1);
if (expectedFail) {
System.out.println("Test expected to not find updated msg");
}
// Write security property file to overwrite default
File f = new File("keyusage."+ System.nanoTime());
PrintWriter p = new PrintWriter(f);
p.write("jdk.tls.keyLimits=");
for (int i = 3; i < args.length; i++) {
p.write(" "+ args[i]);
}
p.close();
System.setProperty("test.java.opts",
"-Dtest.src=" + System.getProperty("test.src") +
" -Dtest.jdk=" + System.getProperty("test.jdk") +
" -Djavax.net.debug=ssl,handshake" +
" -Djava.security.properties=" + f.getName());
System.out.println("test.java.opts: " +
System.getProperty("test.java.opts"));
ProcessBuilder pb = ProcessTools.createTestJavaProcessBuilder(
Utils.addTestJavaOpts("SSLEngineKeyLimit", "p", args[1],
args[2]));
OutputAnalyzer output = ProcessTools.executeProcess(pb);
try {
output.shouldContain(String.format(
"\"cipher suite\" : \"%s", args[2]));
if (expectedFail) {
output.shouldNotContain("KeyUpdate: write key updated");
output.shouldNotContain("KeyUpdate: read key updated");
} else {
output.shouldContain("trigger key update");
output.shouldContain("KeyUpdate: write key updated");
output.shouldContain("KeyUpdate: read key updated");
}
} catch (Exception e) {
throw e;
} finally {
System.out.println("-- BEGIN Stdout:");
System.out.println(output.getStdout());
System.out.println("-- END Stdout");
System.out.println("-- BEGIN Stderr:");
System.out.println(output.getStderr());
System.out.println("-- END Stderr");
}
return;
}
if (args[0].compareTo("p") != 0) {
throw new Exception ("Tried to run outside of a spawned process");
}
if (args[1].compareTo("client") == 0) {
serverwrite = false;
}
cTos = ByteBuffer.allocateDirect(dataLen*4);
sToc = ByteBuffer.allocateDirect(dataLen*4);
outdata = ByteBuffer.allocateDirect(dataLen);
byte[] data = new byte[dataLen];
Arrays.fill(data, (byte)0x0A);
outdata.put(data);
outdata.flip();
cTos.clear();
sToc.clear();
Thread ts = new Thread(serverwrite ? new Client() :
new Server(args[2]));
ts.start();
(serverwrite ? new Server(args[2]) : new Client()).run();
ts.interrupt();
ts.join();
}
private static void doTask(SSLEngineResult result,
SSLEngine engine) throws Exception {
if (result.getHandshakeStatus() ==
SSLEngineResult.HandshakeStatus.NEED_TASK) {
Runnable runnable;
while ((runnable = engine.getDelegatedTask()) != null) {
print("\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");
}
print("\tnew HandshakeStatus: " + hsStatus);
}
}
static void print(String s) {
if (debug) {
System.out.println(s);
}
}
static void log(String s, SSLEngineResult r) {
if (!debug) {
return;
}
System.out.println(s + ": " +
r.getStatus() + "/" + r.getHandshakeStatus()+ " " +
r.bytesConsumed() + "/" + r.bytesProduced() + " ");
}
void write() throws Exception {
int i = 0;
SSLEngineResult r;
boolean again = true;
while (!ready) {
Thread.sleep(delay);
}
print("Write-side. ");
while (i++ < 150) {
while (sc) {
if (readdone) {
return;
}
Thread.sleep(delay);
}
outdata.rewind();
print("write wrap");
while (true) {
r = eng.wrap(outdata, getWriteBuf());
log("write wrap", r);
if (debug && r.getStatus() != SSLEngineResult.Status.OK) {
print("outdata pos: " + outdata.position() +
" rem: " + outdata.remaining() +
" lim: " + outdata.limit() +
" cap: " + outdata.capacity());
print("writebuf pos: " + getWriteBuf().position() +
" rem: " + getWriteBuf().remaining() +
" lim: " + getWriteBuf().limit() +
" cap: " + getWriteBuf().capacity());
}
if (again && r.getStatus() == SSLEngineResult.Status.OK &&
r.getHandshakeStatus() ==
SSLEngineResult.HandshakeStatus.NEED_WRAP) {
print("again");
again = false;
continue;
}
break;
}
doTask(r, eng);
getWriteBuf().flip();
sc = true;
while (sc) {
if (readdone) {
return;
}
Thread.sleep(delay);
}
while (true) {
buf.clear();
r = eng.unwrap(getReadBuf(), buf);
log("write unwrap", r);
if (debug && r.getStatus() != SSLEngineResult.Status.OK) {
print("buf pos: " + buf.position() +
" rem: " + buf.remaining() +
" lim: " + buf.limit() +
" cap: " + buf.capacity());
print("readbuf pos: " + getReadBuf().position() +
" rem: " + getReadBuf().remaining() +
" lim: " + getReadBuf().limit() +
" cap:" + getReadBuf().capacity());
}
break;
}
doTask(r, eng);
getReadBuf().compact();
print("compacted readbuf pos: " + getReadBuf().position() +
" rem: " + getReadBuf().remaining() +
" lim: " + getReadBuf().limit() +
" cap: " + getReadBuf().capacity());
sc = true;
}
}
void read() throws Exception {
byte b = 0x0B;
ByteBuffer buf2 = ByteBuffer.allocateDirect(dataLen);
SSLEngineResult r = null;
boolean exit, again = true;
while (eng == null) {
Thread.sleep(delay);
}
try {
System.out.println("connected");
print("entering read loop");
ready = true;
while (true) {
while (!sc) {
Thread.sleep(delay);
}
print("read wrap");
exit = false;
while (!exit) {
buf2.put(b);
buf2.flip();
r = eng.wrap(buf2, getWriteBuf());
log("read wrap", r);
if (debug) {
// && r.getStatus() != SSLEngineResult.Status.OK) {
print("buf2 pos: " + buf2.position() +
" rem: " + buf2.remaining() +
" cap: " + buf2.capacity());
print("writebuf pos: " + getWriteBuf().position() +
" rem: " + getWriteBuf().remaining() +
" cap: " + getWriteBuf().capacity());
}
if (again && r.getStatus() == SSLEngineResult.Status.OK &&
r.getHandshakeStatus() ==
SSLEngineResult.HandshakeStatus.NEED_WRAP) {
buf2.compact();
again = false;
continue;
}
exit = true;
}
doTask(r, eng);
buf2.clear();
getWriteBuf().flip();
sc = false;
while (!sc) {
Thread.sleep(delay);
}
while (true) {
buf.clear();
r = eng.unwrap(getReadBuf(), buf);
log("read unwrap", r);
if (debug &&
r.getStatus() != SSLEngineResult.Status.OK) {
print("buf pos " + buf.position() +
" rem: " + buf.remaining() +
" lim: " + buf.limit() +
" cap: " + buf.capacity());
print("readbuf pos: " + getReadBuf().position() +
" rem: " + getReadBuf().remaining() +
" lim: " + getReadBuf().limit() +
" cap: " + getReadBuf().capacity());
doTask(r, eng);
}
if (again && r.getStatus() == SSLEngineResult.Status.OK &&
r.getHandshakeStatus() ==
SSLEngineResult.HandshakeStatus.NEED_UNWRAP) {
buf.clear();
print("again");
again = false;
continue;
}
break;
}
buf.clear();
getReadBuf().compact();
totalDataLen += r.bytesProduced();
sc = false;
}
} catch (Exception e) {
sc = false;
readdone = true;
System.out.println(e.getMessage());
e.printStackTrace();
System.out.println("Total data read = " + totalDataLen);
}
}
ByteBuffer getReadBuf() {
return null;
}
ByteBuffer getWriteBuf() {
return null;
}
SSLContext initContext() throws Exception {
return createServerSSLContext();
}
@Override
protected ContextParameters getServerContextParameters() {
return new ContextParameters("TLSv1.3", "PKIX", "NewSunX509");
}
static class Server extends SSLEngineKeyLimit implements Runnable {
Server(String cipherSuite) throws Exception {
super();
eng = initContext().createSSLEngine();
eng.setUseClientMode(false);
eng.setNeedClientAuth(true);
if (cipherSuite != null && cipherSuite.length() > 0) {
eng.setEnabledCipherSuites(new String[] { cipherSuite });
}
}
public void run() {
try {
if (serverwrite) {
write();
} else {
read();
}
} catch (Exception e) {
System.out.println("server: " + e.getMessage());
e.printStackTrace();
}
System.out.println("Server closed");
}
@Override
ByteBuffer getWriteBuf() {
return sToc;
}
@Override
ByteBuffer getReadBuf() {
return cTos;
}
}
static class Client extends SSLEngineKeyLimit implements Runnable {
Client() throws Exception {
super();
eng = initContext().createSSLEngine();
eng.setUseClientMode(true);
}
public void run() {
try {
if (!serverwrite) {
write();
} else {
read();
}
} catch (Exception e) {
System.out.println("client: " + e.getMessage());
e.printStackTrace();
}
System.out.println("Client closed");
}
@Override
ByteBuffer getWriteBuf() {
return cTos;
}
@Override
ByteBuffer getReadBuf() {
return sToc;
}
}
}
|