Skip to content

Commit 9e0e0bc

Browse files
committed
Updated SSLSessionImpl constructor with Record interface methods
1 parent 46d4a6e commit 9e0e0bc

File tree

3 files changed

+55
-113
lines changed

3 files changed

+55
-113
lines changed

src/java.base/share/classes/sun/security/ssl/SSLSessionImpl.java

+35-65
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.math.BigInteger;
3131
import java.net.InetAddress;
3232
import java.nio.ByteBuffer;
33+
import java.nio.charset.StandardCharsets;
3334
import java.security.Principal;
3435
import java.security.PrivateKey;
3536
import java.security.cert.X509Certificate;
@@ -311,113 +312,90 @@ final class SSLSessionImpl extends ExtendedSSLSession {
311312
SSLSessionImpl(HandshakeContext hc, ByteBuffer buf) throws IOException {
312313
boundValues = new ConcurrentHashMap<>();
313314
this.protocolVersion =
314-
ProtocolVersion.valueOf(Short.toUnsignedInt(buf.getShort()));
315+
ProtocolVersion.valueOf(Record.getInt16(buf));
315316

316317
// The CH session id may reset this if it's provided
317318
this.sessionId = new SessionId(true,
318319
hc.sslContext.getSecureRandom());
319320

320321
this.cipherSuite =
321-
CipherSuite.valueOf(Short.toUnsignedInt(buf.getShort()));
322+
CipherSuite.valueOf(Record.getInt16(buf));
322323

323324
// Local Supported signature algorithms
324325
ArrayList<SignatureScheme> list = new ArrayList<>();
325-
int i = Byte.toUnsignedInt(buf.get());
326+
int i = Record.getInt8(buf);
326327
while (i-- > 0) {
327328
list.add(SignatureScheme.valueOf(
328-
Short.toUnsignedInt(buf.getShort())));
329+
Record.getInt16(buf)));
329330
}
330331
this.localSupportedSignAlgs = Collections.unmodifiableCollection(list);
331332

332333
// Peer Supported signature algorithms
333-
i = Byte.toUnsignedInt(buf.get());
334+
i = Record.getInt8(buf);
334335
list.clear();
335336
while (i-- > 0) {
336337
list.add(SignatureScheme.valueOf(
337-
Short.toUnsignedInt(buf.getShort())));
338+
Record.getInt16(buf)));
338339
}
339340
this.peerSupportedSignAlgs = Collections.unmodifiableCollection(list);
340341

341342
// PSK
342-
byte[] b;
343-
i = Short.toUnsignedInt(buf.getShort());
344-
if (i > 0) {
345-
b = new byte[i];
346-
// Get algorithm string
347-
buf.get(b, 0, i);
348-
// Encoded length
349-
i = Short.toUnsignedInt(buf.getShort());
350-
// Encoded SecretKey
351-
b = new byte[i];
352-
buf.get(b);
343+
byte[] b = Record.getBytes16(buf);
344+
if (b.length > 0) {
345+
b = Record.getBytes16(buf);
353346
this.preSharedKey = new SecretKeySpec(b, "TlsMasterSecret");
354347
} else {
355348
this.preSharedKey = null;
356349
}
357350

358351
// PSK identity
359-
i = Byte.toUnsignedInt(buf.get());
360-
if (i > 0) {
361-
b = new byte[i];
362-
buf.get(b);
352+
b = Record.getBytes8(buf);
353+
if (b.length > 0) {
363354
this.pskIdentity = b;
364355
} else {
365356
this.pskIdentity = null;
366357
}
367358

368359
// Master secret length of secret key algorithm (one byte)
369-
i = buf.get();
370-
if (i > 0) {
371-
b = new byte[i];
372-
// Get algorithm string
373-
buf.get(b, 0, i);
374-
// Encoded length
375-
i = Short.toUnsignedInt(buf.getShort());
376-
// Encoded SecretKey
377-
b = new byte[i];
378-
buf.get(b);
360+
b = Record.getBytes8(buf);
361+
if (b.length > 0) {
362+
b = Record.getBytes16(buf);
379363
this.masterSecret = new SecretKeySpec(b, "TlsMasterSecret");
380364
} else {
381365
this.masterSecret = null;
382366
}
367+
383368
// Use extended master secret
384-
this.useExtendedMasterSecret = (buf.get() != 0);
369+
this.useExtendedMasterSecret = (Record.getInt8(buf) != 0);
385370

386371
// Identification Protocol
387-
i = buf.get();
388-
if (i == 0) {
372+
b = Record.getBytes8(buf);
373+
if (b.length == 0) {
389374
identificationProtocol = null;
390375
} else {
391-
b = new byte[i];
392-
buf.get(b);
393376
identificationProtocol = new String(b);
394377
}
395378

396379
// SNI
397-
i = Byte.toUnsignedInt(buf.get()); // length
398-
if (i == 0) {
380+
b = Record.getBytes8(buf);
381+
if (b.length == 0) {
399382
serverNameIndication = null;
400383
} else {
401-
b = new byte[i];
402-
buf.get(b, 0, b.length);
403384
serverNameIndication = new SNIHostName(b);
404385
}
405386

406387
// List of SNIServerName
407-
int len = Short.toUnsignedInt(buf.getShort());
388+
int len = Record.getInt16(buf);
408389
if (len == 0) {
409390
this.requestedServerNames = Collections.emptyList();
410391
} else {
411392
requestedServerNames = new ArrayList<>();
412393
while (len > 0) {
413-
int l = Byte.toUnsignedInt(buf.get());
414-
b = new byte[l];
415-
buf.get(b, 0, l);
394+
b = Record.getBytes8(buf);
416395
requestedServerNames.add(new SNIHostName(new String(b)));
417396
len--;
418397
}
419398
}
420-
421399
maximumPacketSize = buf.getInt();
422400
negotiatedMaxFragLen = buf.getInt();
423401

@@ -427,31 +405,28 @@ final class SSLSessionImpl extends ExtendedSSLSession {
427405
// Get Buffer sizes
428406

429407
// Status Response
430-
len = Short.toUnsignedInt(buf.getShort());
408+
len = Record.getInt16(buf);
431409
if (len == 0) {
432410
statusResponses = Collections.emptyList();
433411
} else {
434412
statusResponses = new ArrayList<>();
435413
}
436414
while (len-- > 0) {
437-
b = new byte[Short.toUnsignedInt(buf.getShort())];
438-
buf.get(b);
415+
b = Record.getBytes16(buf);
439416
statusResponses.add(b);
440417
}
441418

442419
// Get Peer host & port
443-
i = Byte.toUnsignedInt(buf.get());
444-
if (i == 0) {
420+
b = Record.getBytes8(buf);
421+
if (b.length == 0) {
445422
this.host = "";
446423
} else {
447-
b = new byte[i];
448-
buf.get(b, 0, i);
449424
this.host = new String(b);
450425
}
451-
this.port = Short.toUnsignedInt(buf.getShort());
426+
this.port = Record.getInt16(buf);
452427

453428
// Peer certs
454-
i = buf.get();
429+
i = Record.getInt8(buf);
455430
if (i == 0) {
456431
this.peerCerts = null;
457432
} else {
@@ -470,7 +445,7 @@ final class SSLSessionImpl extends ExtendedSSLSession {
470445
}
471446

472447
// Get local certs of PSK
473-
switch (buf.get()) {
448+
switch (Record.getInt8(buf)) {
474449
case 0:
475450
break;
476451
case 1:
@@ -492,21 +467,15 @@ final class SSLSessionImpl extends ExtendedSSLSession {
492467
case 2:
493468
// pre-shared key
494469
// Length of pre-shared key algorithm (one byte)
495-
i = buf.get();
496-
b = new byte[i];
497-
buf.get(b, 0, i);
470+
b = Record.getBytes8(buf);
498471
String alg = new String(b);
499-
// Get length of encoding
500-
i = Short.toUnsignedInt(buf.getShort());
501472
// Get encoding
502-
b = new byte[i];
503-
buf.get(b);
473+
b = Record.getBytes16(buf);
504474
this.preSharedKey = new SecretKeySpec(b, alg);
505475
// Get identity len
506-
i = buf.get();
476+
i = Record.getInt8(buf);
507477
if (i > 0) {
508-
this.pskIdentity = new byte[buf.get()];
509-
buf.get(pskIdentity);
478+
this.pskIdentity = Record.getBytes8(buf);
510479
} else {
511480
this.pskIdentity = null;
512481
}
@@ -520,6 +489,7 @@ final class SSLSessionImpl extends ExtendedSSLSession {
520489
this.lastUsedTime = System.currentTimeMillis();
521490
}
522491

492+
523493
// Some situations we cannot provide a stateless ticket, but after it
524494
// has been negotiated
525495
boolean isStatelessable() {

test/jdk/sun/security/ssl/SSLSessionImpl/ResumeClientTLS12withSNI.java

+20-48
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,16 @@
2525
* @test
2626
* @bug 8350830
2727
* @summary TLS 1.2 Client session resumption having ServerNameIndication
28-
* @run main/othervm ResumeClientTLS12withSNI
28+
* @modules java.base/sun.security.tools.keytool
29+
* @run main/othervm -Djavax.net.debug=all ResumeClientTLS12withSNI
2930
*/
3031

3132
import javax.net.ssl.*;
3233
import javax.net.ssl.SSLEngineResult.HandshakeStatus;
3334
import java.io.*;
3435
import java.nio.ByteBuffer;
36+
import java.nio.file.Files;
37+
import java.nio.file.Path;
3538
import java.security.GeneralSecurityException;
3639
import java.security.KeyStore;
3740
import java.util.*;
@@ -41,18 +44,7 @@ public class ResumeClientTLS12withSNI {
4144
/*
4245
* Enables logging of the SSLEngine operations.
4346
*/
44-
private static final boolean logging = false;
45-
46-
/*
47-
* Enables the JSSE system debugging system property:
48-
*
49-
* -Djavax.net.debug=ssl:handshake
50-
*
51-
* This gives a lot of low-level information about operations underway,
52-
* including specific handshake messages, and might be best examined
53-
* after gaining some familiarity with this application.
54-
*/
55-
private static final boolean debug = true;
47+
private static final boolean logging = true;
5648

5749
private static SSLContext sslc;
5850

@@ -70,15 +62,12 @@ public class ResumeClientTLS12withSNI {
7062
private ByteBuffer cTOs; // "reliable" transport client->server
7163
private ByteBuffer sTOc; // "reliable" transport server->client
7264

73-
74-
private byte[] previousSessionId;
75-
7665
/*
7766
* The following is to set up the keystores.
7867
*/
7968
private static final String pathToStores = System.getProperty("test.src", ".");
80-
private static final String keyStoreFile = "keystore_san.p12";
81-
private static final String trustStoreFile = "keystore_san.p12";
69+
private static final String keyStoreFile = "ks_san.p12";
70+
private static final String trustStoreFile = "ks_san.p12";
8271
private static final char[] passphrase = "123456".toCharArray();
8372

8473
private static final String keyFilename =
@@ -88,21 +77,28 @@ public class ResumeClientTLS12withSNI {
8877

8978
private static final String HOST_NAME = "arf.yak.foo.localhost123456.localhost123456.localhost123456.localhost123456.localhost123456.localhost123456."
9079
+ "localhost123456.localhost123456.localhost123456.localhost123456.localhost123456.localhost123456";
91-
private static final SNIHostName SNI_NAME = new SNIHostName(HOST_NAME);
9280
private static final SNIMatcher SNI_MATCHER = SNIHostName.createSNIMatcher("arf\\.yak\\.foo.*");
9381

9482
/*
9583
* Main entry point for this test.
9684
*/
9785
public static void main(String args[]) throws Exception {
98-
if (debug) {
99-
System.setProperty("javax.net.debug", "ssl");
100-
}
101-
86+
Files.deleteIfExists(Path.of(keyFilename));
87+
88+
sun.security.tools.keytool.Main.main(
89+
("-keystore " + keyFilename + " -storepass 123456 -keypass 123456 -dname"
90+
+ " CN=test" + " -alias ks_san -genkeypair -keyalg rsa -ext "
91+
+ "san=dns:localhost123.localhost123.localhost123.localhost123."
92+
+ "localhost123.localhost123.localhost123.localhost123.localhost123."
93+
+ "localhost123.localhost123.localhost123.localhost123.localhost123."
94+
+ "localhost123.localhost123.localhost123.localhost123.localhost123.com,"
95+
+ "dns:localhost456").split(" "));
10296
final ResumeClientTLS12withSNI clientSession = new ResumeClientTLS12withSNI("TLSv1.2");
10397
for (int i = 0; i < 2; i++) {
10498
clientSession.runTest();
10599
}
100+
101+
Files.deleteIfExists(Path.of(keyFilename));
106102
}
107103

108104
public ResumeClientTLS12withSNI(final String sslProtocol) throws Exception {
@@ -183,10 +179,6 @@ private void runTest() throws Exception {
183179
log("================");
184180

185181
clientResult = clientEngine.wrap(clientOut, cTOs);
186-
if (clientResult.getHandshakeStatus() == HandshakeStatus.FINISHED) {
187-
this.verifySessionResumption(this.previousSessionId, clientEngine);
188-
this.previousSessionId = clientEngine.getSession().getId();
189-
}
190182
log("client wrap: ", clientResult);
191183
runDelegatedTasks(clientResult, clientEngine);
192184

@@ -200,10 +192,6 @@ private void runTest() throws Exception {
200192
log("-------");
201193

202194
clientResult = clientEngine.unwrap(sTOc, clientIn);
203-
if (clientResult.getHandshakeStatus() == HandshakeStatus.FINISHED) {
204-
this.verifySessionResumption(this.previousSessionId, clientEngine);
205-
this.previousSessionId = clientEngine.getSession().getId();
206-
}
207195
log("client unwrap: ", clientResult);
208196
runDelegatedTasks(clientResult, clientEngine);
209197

@@ -262,10 +250,9 @@ private void createSSLEngines() throws Exception {
262250
/*
263251
* Similar to above, but using client mode instead.
264252
*/
265-
clientEngine = sslc.createSSLEngine("client", 80);
253+
clientEngine = sslc.createSSLEngine(HOST_NAME, 80);
266254
clientEngine.setUseClientMode(true);
267255
SSLParameters cliSSLParams = clientEngine.getSSLParameters();
268-
cliSSLParams.setServerNames(List.of(SNI_NAME));
269256
clientEngine.setSSLParameters(cliSSLParams);
270257
}
271258

@@ -377,19 +364,4 @@ private static void log(String str) {
377364
System.out.println(str);
378365
}
379366
}
380-
381-
private void verifySessionResumption(final byte[] expected, final SSLEngine engine) {
382-
if (expected == null) {
383-
// we haven't yet created a session previously, so there isn't any
384-
// session to be expected to resume
385-
return;
386-
}
387-
final byte[] sessionId = engine.getSession().getId();
388-
// compare and verify if they are same
389-
if (Arrays.equals(expected, sessionId)) {
390-
System.out.println(this.sslc.getProvider().getName() + " " + this.sslc.getProtocol() + " - Session resumption SUCCEEDED");
391-
} else {
392-
System.out.println(this.sslc.getProvider().getName() + " " + this.sslc.getProtocol() + " - Session resumption FAILED");
393-
}
394-
}
395367
}
Binary file not shown.

0 commit comments

Comments
 (0)