jdk/test/javax/net/ssl/TLSCommon/RehandshakeWithCipherChangeTest.java
author kshefov
Fri, 05 Jun 2015 12:22:36 +0300
changeset 31057 babdeee3c007
child 45288 58be10a068c2
permissions -rw-r--r--
8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS) Reviewed-by: xuelei, asmotrak, rhalade
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
31057
babdeee3c007 8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS)
kshefov
parents:
diff changeset
     1
/*
babdeee3c007 8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS)
kshefov
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
babdeee3c007 8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS)
kshefov
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
babdeee3c007 8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS)
kshefov
parents:
diff changeset
     4
 *
babdeee3c007 8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS)
kshefov
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
babdeee3c007 8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS)
kshefov
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
babdeee3c007 8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS)
kshefov
parents:
diff changeset
     7
 * published by the Free Software Foundation.
babdeee3c007 8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS)
kshefov
parents:
diff changeset
     8
 *
babdeee3c007 8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS)
kshefov
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
babdeee3c007 8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS)
kshefov
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
babdeee3c007 8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS)
kshefov
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
babdeee3c007 8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS)
kshefov
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
babdeee3c007 8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS)
kshefov
parents:
diff changeset
    13
 * accompanied this code).
babdeee3c007 8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS)
kshefov
parents:
diff changeset
    14
 *
babdeee3c007 8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS)
kshefov
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
babdeee3c007 8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS)
kshefov
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
babdeee3c007 8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS)
kshefov
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
babdeee3c007 8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS)
kshefov
parents:
diff changeset
    18
 *
babdeee3c007 8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS)
kshefov
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
babdeee3c007 8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS)
kshefov
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
babdeee3c007 8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS)
kshefov
parents:
diff changeset
    21
 * questions.
babdeee3c007 8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS)
kshefov
parents:
diff changeset
    22
 */
babdeee3c007 8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS)
kshefov
parents:
diff changeset
    23
babdeee3c007 8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS)
kshefov
parents:
diff changeset
    24
import javax.net.ssl.SSLContext;
babdeee3c007 8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS)
kshefov
parents:
diff changeset
    25
import javax.net.ssl.SSLEngine;
babdeee3c007 8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS)
kshefov
parents:
diff changeset
    26
import javax.net.ssl.SSLEngineResult;
babdeee3c007 8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS)
kshefov
parents:
diff changeset
    27
import javax.net.ssl.SSLException;
babdeee3c007 8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS)
kshefov
parents:
diff changeset
    28
import java.util.Random;
babdeee3c007 8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS)
kshefov
parents:
diff changeset
    29
import jdk.testlibrary.RandomFactory;
babdeee3c007 8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS)
kshefov
parents:
diff changeset
    30
babdeee3c007 8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS)
kshefov
parents:
diff changeset
    31
/**
babdeee3c007 8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS)
kshefov
parents:
diff changeset
    32
 * Testing SSLEngines re-handshaking with cipher change. New cipher is taken
babdeee3c007 8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS)
kshefov
parents:
diff changeset
    33
 * randomly from the supported ciphers list.
babdeee3c007 8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS)
kshefov
parents:
diff changeset
    34
 */
babdeee3c007 8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS)
kshefov
parents:
diff changeset
    35
public class RehandshakeWithCipherChangeTest extends SSLEngineTestCase {
babdeee3c007 8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS)
kshefov
parents:
diff changeset
    36
babdeee3c007 8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS)
kshefov
parents:
diff changeset
    37
    public static void main(String[] s) {
babdeee3c007 8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS)
kshefov
parents:
diff changeset
    38
        RehandshakeWithCipherChangeTest test
babdeee3c007 8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS)
kshefov
parents:
diff changeset
    39
                = new RehandshakeWithCipherChangeTest();
babdeee3c007 8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS)
kshefov
parents:
diff changeset
    40
        test.runTests(Ciphers.ENABLED_NON_KRB_NOT_ANON_CIPHERS);
babdeee3c007 8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS)
kshefov
parents:
diff changeset
    41
    }
babdeee3c007 8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS)
kshefov
parents:
diff changeset
    42
babdeee3c007 8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS)
kshefov
parents:
diff changeset
    43
    @Override
babdeee3c007 8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS)
kshefov
parents:
diff changeset
    44
    protected void testOneCipher(String cipher) throws SSLException {
babdeee3c007 8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS)
kshefov
parents:
diff changeset
    45
        SSLContext context = getContext();
babdeee3c007 8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS)
kshefov
parents:
diff changeset
    46
        int maxPacketSize = getMaxPacketSize();
babdeee3c007 8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS)
kshefov
parents:
diff changeset
    47
        SSLEngine clientEngine = context.createSSLEngine();
babdeee3c007 8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS)
kshefov
parents:
diff changeset
    48
        clientEngine.setUseClientMode(true);
babdeee3c007 8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS)
kshefov
parents:
diff changeset
    49
        SSLEngine serverEngine = context.createSSLEngine();
babdeee3c007 8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS)
kshefov
parents:
diff changeset
    50
        serverEngine.setUseClientMode(false);
babdeee3c007 8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS)
kshefov
parents:
diff changeset
    51
        clientEngine.setEnabledCipherSuites(new String[]{cipher});
babdeee3c007 8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS)
kshefov
parents:
diff changeset
    52
        serverEngine.setEnabledCipherSuites(
babdeee3c007 8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS)
kshefov
parents:
diff changeset
    53
                Ciphers.ENABLED_NON_KRB_NOT_ANON_CIPHERS.ciphers);
babdeee3c007 8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS)
kshefov
parents:
diff changeset
    54
        String randomCipher;
babdeee3c007 8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS)
kshefov
parents:
diff changeset
    55
        serverEngine.setNeedClientAuth(true);
babdeee3c007 8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS)
kshefov
parents:
diff changeset
    56
        long initialEpoch = 0;
babdeee3c007 8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS)
kshefov
parents:
diff changeset
    57
        long secondEpoch = 0;
babdeee3c007 8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS)
kshefov
parents:
diff changeset
    58
        SSLEngineResult r;
babdeee3c007 8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS)
kshefov
parents:
diff changeset
    59
        doHandshake(clientEngine, serverEngine, maxPacketSize,
babdeee3c007 8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS)
kshefov
parents:
diff changeset
    60
                HandshakeMode.INITIAL_HANDSHAKE);
babdeee3c007 8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS)
kshefov
parents:
diff changeset
    61
        sendApplicationData(clientEngine, serverEngine);
babdeee3c007 8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS)
kshefov
parents:
diff changeset
    62
        r = sendApplicationData(serverEngine, clientEngine);
babdeee3c007 8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS)
kshefov
parents:
diff changeset
    63
        if (TESTED_SECURITY_PROTOCOL.contains("DTLS")) {
babdeee3c007 8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS)
kshefov
parents:
diff changeset
    64
            initialEpoch = r.sequenceNumber() >> 48;
babdeee3c007 8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS)
kshefov
parents:
diff changeset
    65
        }
babdeee3c007 8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS)
kshefov
parents:
diff changeset
    66
        final Random RNG = RandomFactory.getRandom();
babdeee3c007 8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS)
kshefov
parents:
diff changeset
    67
        randomCipher = Ciphers.ENABLED_NON_KRB_NOT_ANON_CIPHERS.ciphers[RNG
babdeee3c007 8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS)
kshefov
parents:
diff changeset
    68
                .nextInt(Ciphers.ENABLED_NON_KRB_NOT_ANON_CIPHERS.ciphers.length)];
babdeee3c007 8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS)
kshefov
parents:
diff changeset
    69
        clientEngine.setEnabledCipherSuites(new String[]{randomCipher});
babdeee3c007 8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS)
kshefov
parents:
diff changeset
    70
        doHandshake(clientEngine, serverEngine, maxPacketSize,
babdeee3c007 8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS)
kshefov
parents:
diff changeset
    71
                HandshakeMode.REHANDSHAKE_BEGIN_CLIENT);
babdeee3c007 8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS)
kshefov
parents:
diff changeset
    72
        sendApplicationData(clientEngine, serverEngine);
babdeee3c007 8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS)
kshefov
parents:
diff changeset
    73
        r = sendApplicationData(serverEngine, clientEngine);
babdeee3c007 8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS)
kshefov
parents:
diff changeset
    74
        if (TESTED_SECURITY_PROTOCOL.contains("DTLS")) {
babdeee3c007 8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS)
kshefov
parents:
diff changeset
    75
            secondEpoch = r.sequenceNumber() >> 48;
babdeee3c007 8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS)
kshefov
parents:
diff changeset
    76
            AssertionError epochError = new AssertionError("Epoch number"
babdeee3c007 8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS)
kshefov
parents:
diff changeset
    77
                    + " did not grow after re-handshake! "
babdeee3c007 8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS)
kshefov
parents:
diff changeset
    78
                    + " Was " + initialEpoch + ", now " + secondEpoch + ".");
babdeee3c007 8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS)
kshefov
parents:
diff changeset
    79
            if (Long.compareUnsigned(secondEpoch, initialEpoch) <= 0) {
babdeee3c007 8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS)
kshefov
parents:
diff changeset
    80
                throw epochError;
babdeee3c007 8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS)
kshefov
parents:
diff changeset
    81
            }
babdeee3c007 8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS)
kshefov
parents:
diff changeset
    82
        }
babdeee3c007 8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS)
kshefov
parents:
diff changeset
    83
        closeEngines(clientEngine, serverEngine);
babdeee3c007 8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS)
kshefov
parents:
diff changeset
    84
    }
babdeee3c007 8072515: Test Task: Develop new tests for JEP 219: Datagram Transport Layer Security (DTLS)
kshefov
parents:
diff changeset
    85
}