Path: blob/master/test/jdk/javax/net/ssl/TLSCommon/RehandshakeWithCipherChangeTest.java
41152 views
/*1* Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import javax.net.ssl.SSLContext;24import javax.net.ssl.SSLEngine;25import javax.net.ssl.SSLEngineResult;26import javax.net.ssl.SSLException;27import java.util.Random;28import jdk.test.lib.RandomFactory;2930/**31* Testing SSLEngines re-handshaking with cipher change. New cipher is taken32* randomly from the supported ciphers list.33*/34public class RehandshakeWithCipherChangeTest extends SSLEngineTestCase {3536public static void main(String[] s) {37RehandshakeWithCipherChangeTest test38= new RehandshakeWithCipherChangeTest();39test.runTests(Ciphers.ENABLED_NON_KRB_NOT_ANON_CIPHERS);40}4142@Override43protected void testOneCipher(String cipher) throws SSLException {44SSLContext context = getContext();45int maxPacketSize = getMaxPacketSize();46SSLEngine clientEngine = context.createSSLEngine();47clientEngine.setUseClientMode(true);48SSLEngine serverEngine = context.createSSLEngine();49serverEngine.setUseClientMode(false);50clientEngine.setEnabledCipherSuites(new String[]{cipher});51serverEngine.setEnabledCipherSuites(52Ciphers.ENABLED_NON_KRB_NOT_ANON_CIPHERS.ciphers);53String randomCipher;54serverEngine.setNeedClientAuth(true);55long initialEpoch = 0;56long secondEpoch = 0;57SSLEngineResult r;58doHandshake(clientEngine, serverEngine, maxPacketSize,59HandshakeMode.INITIAL_HANDSHAKE);60sendApplicationData(clientEngine, serverEngine);61r = sendApplicationData(serverEngine, clientEngine);62if (TESTED_SECURITY_PROTOCOL.contains("DTLS")) {63initialEpoch = r.sequenceNumber() >> 48;64}65final Random RNG = RandomFactory.getRandom();66randomCipher = Ciphers.ENABLED_NON_KRB_NOT_ANON_CIPHERS.ciphers[RNG67.nextInt(Ciphers.ENABLED_NON_KRB_NOT_ANON_CIPHERS.ciphers.length)];68clientEngine.setEnabledCipherSuites(new String[]{randomCipher});69doHandshake(clientEngine, serverEngine, maxPacketSize,70HandshakeMode.REHANDSHAKE_BEGIN_CLIENT);71sendApplicationData(clientEngine, serverEngine);72r = sendApplicationData(serverEngine, clientEngine);73if (TESTED_SECURITY_PROTOCOL.contains("DTLS")) {74secondEpoch = r.sequenceNumber() >> 48;75AssertionError epochError = new AssertionError("Epoch number"76+ " did not grow after re-handshake! "77+ " Was " + initialEpoch + ", now " + secondEpoch + ".");78if (Long.compareUnsigned(secondEpoch, initialEpoch) <= 0) {79throw epochError;80}81}82closeEngines(clientEngine, serverEngine);83}84}858687