Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/ssl/SSLEngineImpl/EmptyExtensionData.java
38853 views
1
/*
2
* Copyright (c) 2008, 2020, 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
// SunJSSE does not support dynamic system properties, no way to re-use
26
// system properties in samevm/agentvm mode.
27
//
28
29
/*
30
* @test
31
* @bug 6728126
32
* @summary Parsing Extensions in Client Hello message is done in a wrong way
33
* @library /lib/security
34
* @run main/othervm EmptyExtensionData
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 EmptyExtensionData {
44
45
private static boolean debug = false;
46
47
private static String pathToStores = "../../../../javax/net/ssl/etc";
48
private static String keyStoreFile = "keystore";
49
private static String trustStoreFile = "truststore";
50
private static String passwd = "passphrase";
51
52
private static String keyFilename =
53
System.getProperty("test.src", "./") + "/" + pathToStores +
54
"/" + keyStoreFile;
55
private static String trustFilename =
56
System.getProperty("test.src", "./") + "/" + pathToStores +
57
"/" + trustStoreFile;
58
59
private static void checkDone(SSLEngine ssle) throws Exception {
60
if (!ssle.isInboundDone()) {
61
throw new Exception("isInboundDone isn't done");
62
}
63
if (!ssle.isOutboundDone()) {
64
throw new Exception("isOutboundDone isn't done");
65
}
66
}
67
68
private static void runTest(SSLEngine ssle) throws Exception {
69
// a client hello message with an empty extension data
70
byte[] msg_clihello = {
71
(byte)0x16, (byte)0x03, (byte)0x01, (byte)0x00,
72
(byte)0x6f, (byte)0x01, (byte)0x00, (byte)0x00,
73
(byte)0x6b, (byte)0x03, (byte)0x01, (byte)0x48,
74
(byte)0x90, (byte)0x71, (byte)0xfc, (byte)0xf9,
75
(byte)0xa2, (byte)0x3a, (byte)0xd7, (byte)0xa8,
76
(byte)0x0b, (byte)0x25, (byte)0xf1, (byte)0x2b,
77
(byte)0x88, (byte)0x80, (byte)0x66, (byte)0xca,
78
(byte)0x07, (byte)0x78, (byte)0x2a, (byte)0x08,
79
(byte)0x9d, (byte)0x62, (byte)0x1d, (byte)0x89,
80
(byte)0xc9, (byte)0x1e, (byte)0x1f, (byte)0xe5,
81
(byte)0x92, (byte)0xfe, (byte)0x8d, (byte)0x00,
82
(byte)0x00, (byte)0x24, (byte)0x00, (byte)0x88,
83
(byte)0x00, (byte)0x87, (byte)0x00, (byte)0x39,
84
(byte)0x00, (byte)0x38, (byte)0x00, (byte)0x84,
85
(byte)0x00, (byte)0x35, (byte)0x00, (byte)0x45,
86
(byte)0x00, (byte)0x44, (byte)0x00, (byte)0x33,
87
(byte)0x00, (byte)0x32, (byte)0x00, (byte)0x41,
88
(byte)0x00, (byte)0x04, (byte)0x00, (byte)0x05,
89
(byte)0x00, (byte)0x2f, (byte)0x00, (byte)0x16,
90
(byte)0x00, (byte)0x13, (byte)0xfe, (byte)0xff,
91
(byte)0x00, (byte)0x0a, (byte)0x01, (byte)0x00,
92
(byte)0x00, (byte)0x1e, (byte)0x00, (byte)0x00,
93
(byte)0x00, (byte)0x16, (byte)0x00, (byte)0x14,
94
(byte)0x00, (byte)0x00, (byte)0x11, (byte)0x6a,
95
(byte)0x75, (byte)0x73, (byte)0x74, (byte)0x69,
96
(byte)0x6e, (byte)0x2e, (byte)0x75, (byte)0x6b,
97
(byte)0x2e, (byte)0x73, (byte)0x75, (byte)0x6e,
98
(byte)0x2e, (byte)0x63, (byte)0x6f, (byte)0x6d,
99
(byte)0x00, (byte)0x23, (byte)0x00, (byte)0x00
100
};
101
ByteBuffer bf_clihello = ByteBuffer.wrap(msg_clihello);
102
103
SSLSession session = ssle.getSession();
104
int appBufferMax = session.getApplicationBufferSize();
105
int netBufferMax = session.getPacketBufferSize();
106
107
ByteBuffer serverIn = ByteBuffer.allocate(appBufferMax + 50);
108
ByteBuffer serverOut = ByteBuffer.wrap("I'm Server".getBytes());
109
ByteBuffer sTOc = ByteBuffer.allocate(netBufferMax);
110
111
ssle.beginHandshake();
112
113
// unwrap the clientHello message.
114
SSLEngineResult result = ssle.unwrap(bf_clihello, serverIn);
115
System.out.println("server unwrap " + result);
116
runDelegatedTasks(result, ssle);
117
118
// one more step, ensure the clientHello message is parsed.
119
SSLEngineResult.HandshakeStatus status = ssle.getHandshakeStatus();
120
if ( status == HandshakeStatus.NEED_UNWRAP) {
121
result = ssle.unwrap(bf_clihello, serverIn);
122
System.out.println("server unwrap " + result);
123
runDelegatedTasks(result, ssle);
124
} else if ( status == HandshakeStatus.NEED_WRAP) {
125
result = ssle.wrap(serverOut, sTOc);
126
System.out.println("server wrap " + result);
127
runDelegatedTasks(result, ssle);
128
} else {
129
throw new Exception("unexpected handshake status " + status);
130
}
131
132
// enough, stop
133
}
134
135
/*
136
* If the result indicates that we have outstanding tasks to do,
137
* go ahead and run them in this thread.
138
*/
139
private static void runDelegatedTasks(SSLEngineResult result,
140
SSLEngine engine) throws Exception {
141
142
if (result.getHandshakeStatus() == HandshakeStatus.NEED_TASK) {
143
Runnable runnable;
144
while ((runnable = engine.getDelegatedTask()) != null) {
145
log("\trunning delegated task...");
146
runnable.run();
147
}
148
HandshakeStatus hsStatus = engine.getHandshakeStatus();
149
if (hsStatus == HandshakeStatus.NEED_TASK) {
150
throw new Exception(
151
"handshake shouldn't need additional tasks");
152
}
153
log("\tnew HandshakeStatus: " + hsStatus);
154
}
155
}
156
157
public static void main(String args[]) throws Exception {
158
// Re-enable TLSv1 since test depends on it.
159
SecurityUtils.removeFromDisabledTlsAlgs("TLSv1");
160
161
SSLEngine ssle = createSSLEngine(keyFilename, trustFilename);
162
runTest(ssle);
163
164
System.out.println("Test Passed.");
165
}
166
167
/*
168
* Create an initialized SSLContext to use for this test.
169
*/
170
static private SSLEngine createSSLEngine(String keyFile, String trustFile)
171
throws Exception {
172
173
SSLEngine ssle;
174
175
KeyStore ks = KeyStore.getInstance("JKS");
176
KeyStore ts = KeyStore.getInstance("JKS");
177
178
char[] passphrase = "passphrase".toCharArray();
179
180
ks.load(new FileInputStream(keyFile), passphrase);
181
ts.load(new FileInputStream(trustFile), passphrase);
182
183
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
184
kmf.init(ks, passphrase);
185
186
TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
187
tmf.init(ts);
188
189
SSLContext sslCtx = SSLContext.getInstance("TLS");
190
191
sslCtx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
192
193
ssle = sslCtx.createSSLEngine();
194
ssle.setUseClientMode(false);
195
196
return ssle;
197
}
198
199
200
private static void log(String str) {
201
if (debug) {
202
System.out.println(str);
203
}
204
}
205
}
206
207