Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/net/ssl/SSLEngine/ExtendedKeyEngine.java
41152 views
1
/*
2
* Copyright (c) 2004, 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
* @test
26
* @bug 4981697
27
* @summary Rework the X509KeyManager to avoid incompatibility issues
28
* @author Brad R. Wetmore
29
*
30
* @run main/othervm -Djdk.tls.acknowledgeCloseNotify=true ExtendedKeyEngine
31
*/
32
33
import javax.net.ssl.*;
34
import javax.net.ssl.SSLEngineResult.*;
35
import java.io.*;
36
import java.security.*;
37
import java.nio.*;
38
39
public class ExtendedKeyEngine {
40
41
private static boolean debug = false;
42
43
private SSLContext sslc;
44
private SSLEngine ssle1; // client
45
private SSLEngine ssle2; // server
46
47
private static String pathToStores = "../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 ByteBuffer appOut1; // write side of ssle1
60
private ByteBuffer appIn1; // read side of ssle1
61
private ByteBuffer appOut2; // write side of ssle2
62
private ByteBuffer appIn2; // read side of ssle2
63
64
private ByteBuffer oneToTwo; // "reliable" transport ssle1->ssle2
65
private ByteBuffer twoToOne; // "reliable" transport ssle2->ssle1
66
67
/*
68
* Majority of the test case is here, setup is done below.
69
*/
70
private void createSSLEngines() throws Exception {
71
ssle1 = sslc.createSSLEngine("client", 1);
72
ssle1.setUseClientMode(true);
73
74
ssle2 = sslc.createSSLEngine();
75
ssle2.setUseClientMode(false);
76
ssle2.setNeedClientAuth(true);
77
}
78
79
private void runTest() throws Exception {
80
boolean dataDone = false;
81
82
createSSLEngines();
83
createBuffers();
84
85
SSLEngineResult result1; // ssle1's results from last operation
86
SSLEngineResult result2; // ssle2's results from last operation
87
88
while (!isEngineClosed(ssle1) || !isEngineClosed(ssle2)) {
89
90
log("================");
91
92
result1 = ssle1.wrap(appOut1, oneToTwo);
93
result2 = ssle2.wrap(appOut2, twoToOne);
94
95
log("wrap1: " + result1);
96
log("oneToTwo = " + oneToTwo);
97
log("");
98
99
log("wrap2: " + result2);
100
log("twoToOne = " + twoToOne);
101
102
runDelegatedTasks(result1, ssle1);
103
runDelegatedTasks(result2, ssle2);
104
105
oneToTwo.flip();
106
twoToOne.flip();
107
108
log("----");
109
110
result1 = ssle1.unwrap(twoToOne, appIn1);
111
result2 = ssle2.unwrap(oneToTwo, appIn2);
112
113
log("unwrap1: " + result1);
114
log("twoToOne = " + twoToOne);
115
log("");
116
117
log("unwrap2: " + result2);
118
log("oneToTwo = " + oneToTwo);
119
120
runDelegatedTasks(result1, ssle1);
121
runDelegatedTasks(result2, ssle2);
122
123
oneToTwo.compact();
124
twoToOne.compact();
125
126
/*
127
* If we've transfered all the data between app1 and app2,
128
* we try to close and see what that gets us.
129
*/
130
if (!dataDone && (appOut1.limit() == appIn2.position()) &&
131
(appOut2.limit() == appIn1.position())) {
132
133
checkTransfer(appOut1, appIn2);
134
checkTransfer(appOut2, appIn1);
135
136
log("Closing ssle1's *OUTBOUND*...");
137
ssle1.closeOutbound();
138
dataDone = true;
139
}
140
}
141
}
142
143
public static void main(String args[]) throws Exception {
144
145
ExtendedKeyEngine test;
146
147
System.out.println("This test should run to completion");
148
test = new ExtendedKeyEngine(true);
149
test.createSSLEngines();
150
test.runTest();
151
System.out.println("Done!");
152
153
System.out.println("This test should fail with a Handshake Error");
154
test = new ExtendedKeyEngine(false);
155
test.createSSLEngines();
156
157
try {
158
test.runTest();
159
} catch (SSLHandshakeException e) {
160
System.out.println(
161
"Caught proper exception, should be 'no suites in common'");
162
e.printStackTrace();
163
}
164
165
System.out.println("Test Passed.");
166
}
167
168
/*
169
* **********************************************************
170
* Majority of the test case is above, below is just setup stuff
171
* **********************************************************
172
*/
173
174
public ExtendedKeyEngine(boolean abs) throws Exception {
175
sslc = getSSLContext(keyFilename, trustFilename, abs);
176
}
177
178
/*
179
* Create an initialized SSLContext to use for this test.
180
*/
181
private SSLContext getSSLContext(String keyFile, String trustFile,
182
boolean abs) throws Exception {
183
184
KeyStore ks = KeyStore.getInstance("JKS");
185
KeyStore ts = KeyStore.getInstance("JKS");
186
187
char[] passphrase = "passphrase".toCharArray();
188
189
ks.load(new FileInputStream(keyFile), passphrase);
190
ts.load(new FileInputStream(trustFile), passphrase);
191
192
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
193
kmf.init(ks, passphrase);
194
195
KeyManager [] kms = kmf.getKeyManagers();
196
if (abs) {
197
kms = new KeyManager [] {
198
new MyX509ExtendedKeyManager((X509ExtendedKeyManager)kms[0])
199
};
200
} else {
201
kms = new KeyManager [] {
202
new MyX509KeyManager((X509KeyManager)kms[0])
203
};
204
}
205
206
TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
207
tmf.init(ts);
208
TrustManager [] tms = tmf.getTrustManagers();
209
210
SSLContext sslCtx = SSLContext.getInstance("TLS");
211
212
sslCtx.init(kms, tms, null);
213
214
return sslCtx;
215
}
216
217
private void createBuffers() {
218
// Size the buffers as appropriate.
219
220
SSLSession session = ssle1.getSession();
221
int appBufferMax = session.getApplicationBufferSize();
222
int netBufferMax = session.getPacketBufferSize();
223
224
appIn1 = ByteBuffer.allocateDirect(appBufferMax + 50);
225
appIn2 = ByteBuffer.allocateDirect(appBufferMax + 50);
226
227
oneToTwo = ByteBuffer.allocateDirect(netBufferMax);
228
twoToOne = ByteBuffer.allocateDirect(netBufferMax);
229
230
appOut1 = ByteBuffer.wrap("Hi Engine2, I'm SSLEngine1".getBytes());
231
appOut2 = ByteBuffer.wrap("Hello Engine1, I'm SSLEngine2".getBytes());
232
233
log("AppOut1 = " + appOut1);
234
log("AppOut2 = " + appOut2);
235
log("");
236
}
237
238
private static void runDelegatedTasks(SSLEngineResult result,
239
SSLEngine engine) throws Exception {
240
241
if (result.getHandshakeStatus() == HandshakeStatus.NEED_TASK) {
242
Runnable runnable;
243
while ((runnable = engine.getDelegatedTask()) != null) {
244
log("running delegated task...");
245
runnable.run();
246
}
247
}
248
}
249
250
private static boolean isEngineClosed(SSLEngine engine) {
251
return (engine.isOutboundDone() && engine.isInboundDone());
252
}
253
254
private static void checkTransfer(ByteBuffer a, ByteBuffer b)
255
throws Exception {
256
a.flip();
257
b.flip();
258
259
if (!a.equals(b)) {
260
throw new Exception("Data didn't transfer cleanly");
261
} else {
262
log("Data transferred cleanly");
263
}
264
265
a.position(a.limit());
266
b.position(b.limit());
267
a.limit(a.capacity());
268
b.limit(b.capacity());
269
}
270
271
private static void log(String str) {
272
if (debug) {
273
System.out.println(str);
274
}
275
}
276
}
277
278