Use StackWalker and checking content type after decryption JDK-8145252-TLS13-branch
authorxuelei
Sun, 10 Jun 2018 13:36:40 -0700
branchJDK-8145252-TLS13-branch
changeset 56717 e4fe7c97b1de
parent 56716 38c2a4078033
child 56718 da9979451b7a
Use StackWalker and checking content type after decryption
src/java.base/share/classes/sun/security/ssl/SSLEngineInputRecord.java
src/java.base/share/classes/sun/security/ssl/SSLLogger.java
src/java.base/share/classes/sun/security/ssl/SSLSocketInputRecord.java
--- a/src/java.base/share/classes/sun/security/ssl/SSLEngineInputRecord.java	Sat Jun 09 21:52:54 2018 -0700
+++ b/src/java.base/share/classes/sun/security/ssl/SSLEngineInputRecord.java	Sun Jun 10 13:36:40 2018 -0700
@@ -228,15 +228,6 @@
         }
 
         //
-        // check for handshake fragment
-        //
-        if (contentType != ContentType.HANDSHAKE.id &&
-                handshakeBuffer != null && handshakeBuffer.hasRemaining()) {
-            throw new SSLProtocolException(
-                    "Expected to get a handshake fragment");
-        }
-
-        //
         // Decrypt the fragment
         //
         int recLim = srcPos + SSLRecord.headerSize + contentLen;
@@ -261,6 +252,16 @@
         }
 
         //
+        // check for handshake fragment
+        //
+        if (contentType != ContentType.HANDSHAKE.id &&
+                handshakeBuffer != null && handshakeBuffer.hasRemaining()) {
+            throw new SSLProtocolException(
+                    "Expecting a handshake fragment, but received " +
+                    ContentType.nameOf(contentType));
+        }
+
+        //
         // parse handshake messages
         //
         if (contentType == ContentType.HANDSHAKE.id) {
--- a/src/java.base/share/classes/sun/security/ssl/SSLLogger.java	Sat Jun 09 21:52:54 2018 -0700
+++ b/src/java.base/share/classes/sun/security/ssl/SSLLogger.java	Sun Jun 10 13:36:40 2018 -0700
@@ -388,25 +388,12 @@
         }
 
         private static String formatCaller() {
-            boolean isSSLLogger = false;
-            for (StackTraceElement ste: (new Throwable()).getStackTrace()) {
-                String cn = ste.getClassName();
-                if (cn.equals("sun.security.ssl.SSLLogger")) {
-                    // May be SSLLogger.log() or SSLLogger.fine(), etc.
-                    isSSLLogger = true;
-                } else if (isSSLLogger) {
-                    // Here is the caller to SSLLogger.fine(), etc.
-                    return ste.getFileName() + ":" + ste.getLineNumber();
-                }
-            }
-
-            return "unknown caller";
-        }
-
-        private static boolean isLoggerImpl(StackTraceElement ste) {
-            String cn = ste.getClassName();
-            System.out.println("class name: " + cn);
-            return cn.equals("sun.security.ssl.SSLLogger");
+            return StackWalker.getInstance().walk(s ->
+                s.dropWhile(f ->
+                    f.getClassName().startsWith("sun.security.ssl.SSLLogger") ||
+                    f.getClassName().startsWith("java.lang.System"))
+                .map(f -> f.getFileName() + ":" + f.getLineNumber())
+                .findFirst().orElse("unknown caller"));
         }
 
         private static String formatParameters(Object ... parameters) {
--- a/src/java.base/share/classes/sun/security/ssl/SSLSocketInputRecord.java	Sat Jun 09 21:52:54 2018 -0700
+++ b/src/java.base/share/classes/sun/security/ssl/SSLSocketInputRecord.java	Sun Jun 10 13:36:40 2018 -0700
@@ -268,10 +268,12 @@
             throw (SSLProtocolException)(new SSLProtocolException(
                     "Unexpected exception")).initCause(gse);
         }
+
         if (contentType != ContentType.HANDSHAKE.id &&
                 handshakeBuffer != null && handshakeBuffer.hasRemaining()) {
             throw new SSLProtocolException(
-                    "Expected to get a handshake fragment");
+                    "Expecting a handshake fragment, but received " +
+                    ContentType.nameOf(contentType));
         }
 
         //