test/jdk/sun/security/ssl/ClientHandshaker/LengthCheckTest.java
branchJDK-8145252-TLS13-branch
changeset 56542 56aaa6cb3693
parent 47216 71c04702a3d5
child 56806 32a737f51e37
equal deleted inserted replaced
56541:92cbbfc996f3 56542:56aaa6cb3693
    72 import java.io.*;
    72 import java.io.*;
    73 import java.security.*;
    73 import java.security.*;
    74 import java.nio.*;
    74 import java.nio.*;
    75 import java.util.List;
    75 import java.util.List;
    76 import java.util.ArrayList;
    76 import java.util.ArrayList;
    77 import sun.security.ssl.ProtocolVersion;
       
    78 
    77 
    79 public class LengthCheckTest {
    78 public class LengthCheckTest {
    80 
    79 
    81     /*
    80     /*
    82      * Enables logging of the SSLEngine operations.
    81      * Enables logging of the SSLEngine operations.
   153     private static final int TLS_ALERT_LVL_FATAL = 0x02;
   152     private static final int TLS_ALERT_LVL_FATAL = 0x02;
   154 
   153 
   155     private static final int TLS_ALERT_UNEXPECTED_MSG = 0x0A;
   154     private static final int TLS_ALERT_UNEXPECTED_MSG = 0x0A;
   156     private static final int TLS_ALERT_HANDSHAKE_FAILURE = 0x28;
   155     private static final int TLS_ALERT_HANDSHAKE_FAILURE = 0x28;
   157     private static final int TLS_ALERT_INTERNAL_ERROR = 0x50;
   156     private static final int TLS_ALERT_INTERNAL_ERROR = 0x50;
       
   157     private static final int TLS_ALERT_ILLEGAL_PARAMETER = 0x2F;
   158 
   158 
   159     public interface HandshakeTest {
   159     public interface HandshakeTest {
   160         void execTest() throws Exception;
   160         void execTest() throws Exception;
   161     }
   161     }
   162 
   162 
   266                 serverResult = serverEngine.wrap(serverOut, sTOc);
   266                 serverResult = serverEngine.wrap(serverOut, sTOc);
   267                 log("server wrap: ", serverResult);
   267                 log("server wrap: ", serverResult);
   268                 runDelegatedTasks(serverResult, serverEngine);
   268                 runDelegatedTasks(serverResult, serverEngine);
   269                 sTOc.flip();
   269                 sTOc.flip();
   270                 dumpByteBuffer("SERVER-TO-CLIENT", sTOc);
   270                 dumpByteBuffer("SERVER-TO-CLIENT", sTOc);
       
   271     
       
   272                 // We expect to see the server generate an alert here
       
   273                 serverResult = serverEngine.wrap(serverOut, sTOc);
       
   274                 log("server wrap: ", serverResult);
       
   275                 runDelegatedTasks(serverResult, serverEngine);
       
   276                 sTOc.flip();
       
   277                 dumpByteBuffer("SERVER-TO-CLIENT", sTOc);
   271             } catch (SSLProtocolException ssle) {
   278             } catch (SSLProtocolException ssle) {
   272                 log("Received expected SSLProtocolException: " + ssle);
   279                 log("Received expected SSLProtocolException: " + ssle);
   273                 gotException = true;
   280                 gotException = true;
   274             }
   281             }
   275 
       
   276             // We expect to see the server generate an alert here
       
   277             serverResult = serverEngine.wrap(serverOut, sTOc);
       
   278             log("server wrap: ", serverResult);
       
   279             runDelegatedTasks(serverResult, serverEngine);
       
   280             sTOc.flip();
       
   281             dumpByteBuffer("SERVER-TO-CLIENT", sTOc);
       
   282 
   282 
   283             // At this point we can verify that both an exception
   283             // At this point we can verify that both an exception
   284             // was thrown and the proper action (a TLS alert) was
   284             // was thrown and the proper action (a TLS alert) was
   285             // sent back to the client.
   285             // sent back to the client.
   286             if (gotException == false ||
   286             if (gotException == false ||
   287                 !isTlsMessage(sTOc, TLS_RECTYPE_ALERT, TLS_ALERT_LVL_FATAL,
   287                 !isTlsMessage(sTOc, TLS_RECTYPE_ALERT, TLS_ALERT_LVL_FATAL,
   288                         TLS_ALERT_UNEXPECTED_MSG)) {
   288                         TLS_ALERT_ILLEGAL_PARAMETER)) {
   289                 throw new SSLException(
   289                 throw new SSLException(
   290                     "Server failed to throw Alert:fatal:internal_error");
   290                     "Server failed to throw Alert:fatal:internal_error");
   291             }
   291             }
   292         }
   292         }
   293     };
   293     };
   781             // beginning of a TLS record.  Get the type, version and length.
   781             // beginning of a TLS record.  Get the type, version and length.
   782             int type = Byte.toUnsignedInt(bBuf.get());
   782             int type = Byte.toUnsignedInt(bBuf.get());
   783             int ver_major = Byte.toUnsignedInt(bBuf.get());
   783             int ver_major = Byte.toUnsignedInt(bBuf.get());
   784             int ver_minor = Byte.toUnsignedInt(bBuf.get());
   784             int ver_minor = Byte.toUnsignedInt(bBuf.get());
   785             int recLen = Short.toUnsignedInt(bBuf.getShort());
   785             int recLen = Short.toUnsignedInt(bBuf.getShort());
   786             ProtocolVersion pv = ProtocolVersion.valueOf(ver_major, ver_minor);
       
   787 
   786 
   788             log("===== " + header + " (" + tlsRecType(type) + " / " +
   787             log("===== " + header + " (" + tlsRecType(type) + " / " +
   789                 pv + " / " + bufLen + " bytes) =====");
   788                 ver_major + "." + ver_minor + " / " + bufLen + " bytes) =====");
   790             bBuf.reset();
   789             bBuf.reset();
   791             for (int i = 0; i < bufLen; i++) {
   790             for (int i = 0; i < bufLen; i++) {
   792                 if (i != 0 && i % 16 == 0) {
   791                 if (i != 0 && i % 16 == 0) {
   793                     System.out.print("\n");
   792                     System.out.print("\n");
   794                 }
   793                 }