1 /*
   2  * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 // This test case relies on updated static security property, no way to re-use
  25 // security property in samevm/agentvm mode.
  26 
  27 /*
  28  * @test
  29  * @bug 8042449
  30  * @summary Issue for negative byte major record version
  31  * @ignore the dependent implementation details are changed
  32  * @run main/othervm IllegalRecordVersion
  33  */
  34 
  35 import javax.net.ssl.*;
  36 import javax.net.ssl.SSLEngineResult.*;
  37 import java.io.*;
  38 import java.security.*;
  39 import java.nio.*;
  40 
  41 public class IllegalRecordVersion {
  42 
  43     public static void main(String args[]) throws Exception {
  44         SSLContext context = SSLContext.getDefault();
  45 
  46         SSLEngine cliEngine = context.createSSLEngine();
  47         cliEngine.setUseClientMode(true);
  48         SSLEngine srvEngine = context.createSSLEngine();
  49         srvEngine.setUseClientMode(false);
  50 
  51         SSLSession session = cliEngine.getSession();
  52         int netBufferMax = session.getPacketBufferSize();
  53         int appBufferMax = session.getApplicationBufferSize();
  54 
  55         ByteBuffer cliToSrv = ByteBuffer.allocateDirect(netBufferMax);
  56         ByteBuffer srvIBuff = ByteBuffer.allocateDirect(appBufferMax + 50);
  57         ByteBuffer cliOBuff = ByteBuffer.wrap("I'm client".getBytes());
  58 
  59 
  60         System.out.println("client hello (record version(0xa9, 0xa2))");
  61         SSLEngineResult cliRes = cliEngine.wrap(cliOBuff, cliToSrv);
  62         System.out.println("Client wrap result: " + cliRes);
  63         cliToSrv.flip();
  64         if (cliToSrv.limit() > 5) {
  65             cliToSrv.put(1, (byte)0xa9);
  66             cliToSrv.put(2, (byte)0xa2);
  67         }
  68 
  69         try {
  70             srvEngine.unwrap(cliToSrv, srvIBuff);
  71             throw new Exception(
  72                 "Cannot catch the unsupported record version issue");
  73         } catch (SSLException e) {
  74             // get the expected exception
  75         }
  76     }
  77 }