46060
|
1 |
/*
|
|
2 |
* Copyright (c) 2017, 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 |
//
|
|
25 |
// This test case relies on updated static security property, no way to re-use
|
|
26 |
// security property in samevm/agentvm mode.
|
|
27 |
//
|
|
28 |
|
|
29 |
/*
|
|
30 |
* @test
|
|
31 |
* @bug 8180643
|
|
32 |
* @summary Illegal handshake message
|
56542
|
33 |
* @ignore the dependent implementation details are changed
|
46060
|
34 |
* @run main/othervm IllegalHandshakeMessage
|
|
35 |
*/
|
|
36 |
|
|
37 |
import javax.net.ssl.*;
|
|
38 |
import javax.net.ssl.SSLEngineResult.*;
|
|
39 |
import java.io.*;
|
|
40 |
import java.security.*;
|
|
41 |
import java.nio.*;
|
|
42 |
|
|
43 |
public class IllegalHandshakeMessage {
|
|
44 |
|
|
45 |
public static void main(String args[]) throws Exception {
|
|
46 |
SSLContext context = SSLContext.getDefault();
|
|
47 |
|
|
48 |
SSLEngine cliEngine = context.createSSLEngine();
|
|
49 |
cliEngine.setUseClientMode(true);
|
|
50 |
SSLEngine srvEngine = context.createSSLEngine();
|
|
51 |
srvEngine.setUseClientMode(false);
|
|
52 |
|
|
53 |
SSLSession session = cliEngine.getSession();
|
|
54 |
int netBufferMax = session.getPacketBufferSize();
|
|
55 |
int appBufferMax = session.getApplicationBufferSize();
|
|
56 |
|
|
57 |
ByteBuffer cliToSrv = ByteBuffer.allocateDirect(netBufferMax);
|
|
58 |
ByteBuffer srvToCli = ByteBuffer.allocateDirect(netBufferMax);
|
|
59 |
ByteBuffer srvIBuff = ByteBuffer.allocateDirect(appBufferMax + 50);
|
|
60 |
ByteBuffer cliOBuff = ByteBuffer.wrap("I'm client".getBytes());
|
|
61 |
ByteBuffer srvOBuff = ByteBuffer.wrap("I'm server".getBytes());
|
|
62 |
|
|
63 |
|
|
64 |
System.out.println("client hello (handshake type(0xAB))");
|
|
65 |
SSLEngineResult cliRes = cliEngine.wrap(cliOBuff, cliToSrv);
|
|
66 |
System.out.println("Client wrap result: " + cliRes);
|
|
67 |
cliToSrv.flip();
|
|
68 |
if (cliToSrv.limit() > 7) {
|
|
69 |
cliToSrv.put(5, (byte)0xAB); // use illegal handshake type
|
|
70 |
cliToSrv.put(7, (byte)0x80); // use illegal message length
|
|
71 |
} else {
|
|
72 |
// unlikely
|
|
73 |
throw new Exception("No handshage message generated.");
|
|
74 |
}
|
|
75 |
|
|
76 |
try {
|
|
77 |
SSLEngineResult srvRes = srvEngine.unwrap(cliToSrv, srvIBuff);
|
|
78 |
System.out.println("Server unwrap result: " + srvRes);
|
|
79 |
runDelegatedTasks(srvRes, srvEngine);
|
|
80 |
|
|
81 |
srvRes = srvEngine.wrap(srvOBuff, srvToCli);
|
|
82 |
System.out.println("Server wrap result: " + srvRes);
|
|
83 |
|
|
84 |
throw new Exception(
|
|
85 |
"Unsupported handshake message is not handled properly.");
|
|
86 |
} catch (SSLException e) {
|
|
87 |
// get the expected exception
|
|
88 |
System.out.println("Expected exception: " + e);
|
|
89 |
}
|
|
90 |
}
|
|
91 |
|
|
92 |
private static void runDelegatedTasks(SSLEngineResult result,
|
|
93 |
SSLEngine engine) throws Exception {
|
|
94 |
|
|
95 |
if (result.getHandshakeStatus() == HandshakeStatus.NEED_TASK) {
|
|
96 |
Runnable runnable;
|
|
97 |
while ((runnable = engine.getDelegatedTask()) != null) {
|
|
98 |
System.out.println("\trunning delegated task...");
|
|
99 |
runnable.run();
|
|
100 |
}
|
|
101 |
HandshakeStatus hsStatus = engine.getHandshakeStatus();
|
|
102 |
if (hsStatus == HandshakeStatus.NEED_TASK) {
|
|
103 |
throw new Exception(
|
|
104 |
"handshake shouldn't need additional tasks");
|
|
105 |
}
|
|
106 |
System.out.println("\tnew HandshakeStatus: " + hsStatus);
|
|
107 |
}
|
|
108 |
}
|
|
109 |
}
|
|
110 |
|