jdk/test/javax/net/ssl/TLSCommon/RehandshakeWithCipherChangeTest.java
changeset 31057 babdeee3c007
child 45288 58be10a068c2
equal deleted inserted replaced
31008:5b500c93ce48 31057:babdeee3c007
       
     1 /*
       
     2  * Copyright (c) 2015, 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 import javax.net.ssl.SSLContext;
       
    25 import javax.net.ssl.SSLEngine;
       
    26 import javax.net.ssl.SSLEngineResult;
       
    27 import javax.net.ssl.SSLException;
       
    28 import java.util.Random;
       
    29 import jdk.testlibrary.RandomFactory;
       
    30 
       
    31 /**
       
    32  * Testing SSLEngines re-handshaking with cipher change. New cipher is taken
       
    33  * randomly from the supported ciphers list.
       
    34  */
       
    35 public class RehandshakeWithCipherChangeTest extends SSLEngineTestCase {
       
    36 
       
    37     public static void main(String[] s) {
       
    38         RehandshakeWithCipherChangeTest test
       
    39                 = new RehandshakeWithCipherChangeTest();
       
    40         test.runTests(Ciphers.ENABLED_NON_KRB_NOT_ANON_CIPHERS);
       
    41     }
       
    42 
       
    43     @Override
       
    44     protected void testOneCipher(String cipher) throws SSLException {
       
    45         SSLContext context = getContext();
       
    46         int maxPacketSize = getMaxPacketSize();
       
    47         SSLEngine clientEngine = context.createSSLEngine();
       
    48         clientEngine.setUseClientMode(true);
       
    49         SSLEngine serverEngine = context.createSSLEngine();
       
    50         serverEngine.setUseClientMode(false);
       
    51         clientEngine.setEnabledCipherSuites(new String[]{cipher});
       
    52         serverEngine.setEnabledCipherSuites(
       
    53                 Ciphers.ENABLED_NON_KRB_NOT_ANON_CIPHERS.ciphers);
       
    54         String randomCipher;
       
    55         serverEngine.setNeedClientAuth(true);
       
    56         long initialEpoch = 0;
       
    57         long secondEpoch = 0;
       
    58         SSLEngineResult r;
       
    59         doHandshake(clientEngine, serverEngine, maxPacketSize,
       
    60                 HandshakeMode.INITIAL_HANDSHAKE);
       
    61         sendApplicationData(clientEngine, serverEngine);
       
    62         r = sendApplicationData(serverEngine, clientEngine);
       
    63         if (TESTED_SECURITY_PROTOCOL.contains("DTLS")) {
       
    64             initialEpoch = r.sequenceNumber() >> 48;
       
    65         }
       
    66         final Random RNG = RandomFactory.getRandom();
       
    67         randomCipher = Ciphers.ENABLED_NON_KRB_NOT_ANON_CIPHERS.ciphers[RNG
       
    68                 .nextInt(Ciphers.ENABLED_NON_KRB_NOT_ANON_CIPHERS.ciphers.length)];
       
    69         clientEngine.setEnabledCipherSuites(new String[]{randomCipher});
       
    70         doHandshake(clientEngine, serverEngine, maxPacketSize,
       
    71                 HandshakeMode.REHANDSHAKE_BEGIN_CLIENT);
       
    72         sendApplicationData(clientEngine, serverEngine);
       
    73         r = sendApplicationData(serverEngine, clientEngine);
       
    74         if (TESTED_SECURITY_PROTOCOL.contains("DTLS")) {
       
    75             secondEpoch = r.sequenceNumber() >> 48;
       
    76             AssertionError epochError = new AssertionError("Epoch number"
       
    77                     + " did not grow after re-handshake! "
       
    78                     + " Was " + initialEpoch + ", now " + secondEpoch + ".");
       
    79             if (Long.compareUnsigned(secondEpoch, initialEpoch) <= 0) {
       
    80                 throw epochError;
       
    81             }
       
    82         }
       
    83         closeEngines(clientEngine, serverEngine);
       
    84     }
       
    85 }