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 
29 /**
30  * Testing SSLEngines re-handshaking using each of the supported cipher suites
31  * with application data exchange before and after re-handshake and closing of
32  * the engines.
33  */
34 public class RehandshakeWithDataExTest extends SSLEngineTestCase {
35 
main(String[] args)36     public static void main(String[] args) {
37         RehandshakeWithDataExTest test = new RehandshakeWithDataExTest();
38         setUpAndStartKDCIfNeeded();
39         test.runTests();
40     }
41 
42     @Override
testOneCipher(String cipher)43     protected void testOneCipher(String cipher) throws SSLException {
44         SSLContext context = getContext();
45         int maxPacketSize = getMaxPacketSize();
46         boolean useSNI = !TEST_MODE.equals("norm");
47         SSLEngine clientEngine = getClientSSLEngine(context, useSNI);
48         SSLEngine serverEngine = getServerSSLEngine(context, useSNI);
49         clientEngine.setEnabledCipherSuites(new String[]{cipher});
50         serverEngine.setEnabledCipherSuites(new String[]{cipher});
51         serverEngine.setNeedClientAuth(!cipher.contains("anon"));
52         long initialEpoch = 0;
53         long secondEpoch = 0;
54         long thirdEpoch = 0;
55         SSLEngineResult r;
56         doHandshake(clientEngine, serverEngine, maxPacketSize,
57                 HandshakeMode.INITIAL_HANDSHAKE);
58         sendApplicationData(clientEngine, serverEngine);
59         r = sendApplicationData(serverEngine, clientEngine);
60         if (TESTED_SECURITY_PROTOCOL.contains("DTLS")) {
61             initialEpoch = r.sequenceNumber() >> 48;
62         }
63         doHandshake(clientEngine, serverEngine, maxPacketSize,
64                 HandshakeMode.REHANDSHAKE_BEGIN_CLIENT);
65         sendApplicationData(clientEngine, serverEngine);
66         r = sendApplicationData(serverEngine, clientEngine);
67         AssertionError epochError = new AssertionError("Epoch number"
68                 + " did not grow after re-handshake! "
69                 + " Was " + initialEpoch + ", now " + secondEpoch + ".");
70         if (TESTED_SECURITY_PROTOCOL.contains("DTLS")) {
71             secondEpoch = r.sequenceNumber() >> 48;
72             if (Long.compareUnsigned(secondEpoch, initialEpoch) <= 0) {
73                 throw epochError;
74             }
75         }
76         doHandshake(clientEngine, serverEngine, maxPacketSize,
77                 HandshakeMode.REHANDSHAKE_BEGIN_SERVER);
78         sendApplicationData(clientEngine, serverEngine);
79         r = sendApplicationData(serverEngine, clientEngine);
80         if (TESTED_SECURITY_PROTOCOL.contains("DTLS")) {
81         thirdEpoch = r.sequenceNumber() >> 48;
82             if (Long.compareUnsigned(thirdEpoch, secondEpoch) <= 0) {
83                 throw epochError;
84             }
85         }
86         closeEngines(clientEngine, serverEngine);
87     }
88 
89 }
90