Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/net/ssl/templates/SSLEngineTemplate.java
41152 views
1
/*
2
* Copyright (c) 2003, 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
// SunJSSE does not support dynamic system properties, no way to re-use
25
// system properties in samevm/agentvm mode.
26
27
/*
28
* @test
29
* @bug 8250839
30
* @summary Improve test template SSLEngineTemplate with SSLContextTemplate
31
* @build SSLContextTemplate
32
* @run main/othervm SSLEngineTemplate
33
*/
34
import javax.net.ssl.*;
35
import javax.net.ssl.SSLEngineResult.HandshakeStatus;
36
import java.nio.ByteBuffer;
37
38
/**
39
* A SSLEngine usage example which simplifies the presentation
40
* by removing the I/O and multi-threading concerns.
41
*
42
* The test creates two SSLEngines, simulating a client and server.
43
* The "transport" layer consists two byte buffers: think of them
44
* as directly connected pipes.
45
*
46
* Note, this is a *very* simple example: real code will be much more
47
* involved. For example, different threading and I/O models could be
48
* used, transport mechanisms could close unexpectedly, and so on.
49
*
50
* When this application runs, notice that several messages
51
* (wrap/unwrap) pass before any application data is consumed or
52
* produced.
53
*/
54
public class SSLEngineTemplate implements SSLContextTemplate {
55
protected final SSLEngine clientEngine; // client Engine
56
protected final ByteBuffer clientOut; // write side of clientEngine
57
protected final ByteBuffer clientIn; // read side of clientEngine
58
59
protected final SSLEngine serverEngine; // server Engine
60
protected final ByteBuffer serverOut; // write side of serverEngine
61
protected final ByteBuffer serverIn; // read side of serverEngine
62
63
// For data transport, this example uses local ByteBuffers. This
64
// isn't really useful, but the purpose of this example is to show
65
// SSLEngine concepts, not how to do network transport.
66
protected final ByteBuffer cTOs; // "reliable" transport client->server
67
protected final ByteBuffer sTOc; // "reliable" transport server->client
68
69
protected SSLEngineTemplate() throws Exception {
70
serverEngine = configureServerEngine(
71
createServerSSLContext().createSSLEngine());
72
73
clientEngine = configureClientEngine(
74
createClientSSLContext().createSSLEngine());
75
76
// We'll assume the buffer sizes are the same
77
// between client and server.
78
SSLSession session = clientEngine.getSession();
79
int appBufferMax = session.getApplicationBufferSize();
80
int netBufferMax = session.getPacketBufferSize();
81
82
// We'll make the input buffers a bit bigger than the max needed
83
// size, so that unwrap()s following a successful data transfer
84
// won't generate BUFFER_OVERFLOWS.
85
//
86
// We'll use a mix of direct and indirect ByteBuffers for
87
// tutorial purposes only. In reality, only use direct
88
// ByteBuffers when they give a clear performance enhancement.
89
clientIn = ByteBuffer.allocate(appBufferMax + 50);
90
serverIn = ByteBuffer.allocate(appBufferMax + 50);
91
92
cTOs = ByteBuffer.allocateDirect(netBufferMax);
93
sTOc = ByteBuffer.allocateDirect(netBufferMax);
94
95
clientOut = ByteBuffer.wrap("Hi Server, I'm Client".getBytes());
96
serverOut = ByteBuffer.wrap("Hello Client, I'm Server".getBytes());
97
}
98
99
//
100
// Protected methods could be used to customize the test case.
101
//
102
103
/*
104
* Configure the client side engine.
105
*/
106
protected SSLEngine configureClientEngine(SSLEngine clientEngine) {
107
clientEngine.setUseClientMode(true);
108
109
// Get/set parameters if needed
110
// SSLParameters paramsClient = clientEngine.getSSLParameters();
111
// clientEngine.setSSLParameters(paramsClient);
112
113
return clientEngine;
114
}
115
116
/*
117
* Configure the server side engine.
118
*/
119
protected SSLEngine configureServerEngine(SSLEngine serverEngine) {
120
serverEngine.setUseClientMode(false);
121
serverEngine.setNeedClientAuth(true);
122
123
// Get/set parameters if needed
124
//
125
// SSLParameters paramsServer = serverEngine.getSSLParameters();
126
// serverEngine.setSSLParameters(paramsServer);
127
128
return serverEngine;
129
}
130
131
public static void main(String[] args) throws Exception {
132
new SSLEngineTemplate().runTest();
133
}
134
135
//
136
// Private methods that used to build the common part of the test.
137
//
138
139
private void runTest() throws Exception {
140
SSLEngineResult clientResult;
141
SSLEngineResult serverResult;
142
143
boolean dataDone = false;
144
while (isOpen(clientEngine) || isOpen(serverEngine)) {
145
log("=================");
146
147
// client wrap
148
log("---Client Wrap---");
149
clientResult = clientEngine.wrap(clientOut, cTOs);
150
logEngineStatus(clientEngine, clientResult);
151
runDelegatedTasks(clientEngine);
152
153
// server wrap
154
log("---Server Wrap---");
155
serverResult = serverEngine.wrap(serverOut, sTOc);
156
logEngineStatus(serverEngine, serverResult);
157
runDelegatedTasks(serverEngine);
158
159
cTOs.flip();
160
sTOc.flip();
161
162
// client unwrap
163
log("---Client Unwrap---");
164
clientResult = clientEngine.unwrap(sTOc, clientIn);
165
logEngineStatus(clientEngine, clientResult);
166
runDelegatedTasks(clientEngine);
167
168
// server unwrap
169
log("---Server Unwrap---");
170
serverResult = serverEngine.unwrap(cTOs, serverIn);
171
logEngineStatus(serverEngine, serverResult);
172
runDelegatedTasks(serverEngine);
173
174
cTOs.compact();
175
sTOc.compact();
176
177
// After we've transferred all application data between the client
178
// and server, we close the clientEngine's outbound stream.
179
// This generates a close_notify handshake message, which the
180
// server engine receives and responds by closing itself.
181
if (!dataDone && (clientOut.limit() == serverIn.position()) &&
182
(serverOut.limit() == clientIn.position())) {
183
184
// A sanity check to ensure we got what was sent.
185
checkTransfer(serverOut, clientIn);
186
checkTransfer(clientOut, serverIn);
187
188
log("\tClosing clientEngine's *OUTBOUND*...");
189
clientEngine.closeOutbound();
190
logEngineStatus(clientEngine);
191
192
dataDone = true;
193
log("\tClosing serverEngine's *OUTBOUND*...");
194
serverEngine.closeOutbound();
195
logEngineStatus(serverEngine);
196
}
197
}
198
}
199
200
static boolean isOpen(SSLEngine engine) {
201
return (!engine.isOutboundDone() || !engine.isInboundDone());
202
}
203
204
private static void logEngineStatus(SSLEngine engine) {
205
log("\tCurrent HS State: " + engine.getHandshakeStatus());
206
log("\tisInboundDone() : " + engine.isInboundDone());
207
log("\tisOutboundDone(): " + engine.isOutboundDone());
208
}
209
210
private static void logEngineStatus(
211
SSLEngine engine, SSLEngineResult result) {
212
log("\tResult Status : " + result.getStatus());
213
log("\tResult HS Status : " + result.getHandshakeStatus());
214
log("\tEngine HS Status : " + engine.getHandshakeStatus());
215
log("\tisInboundDone() : " + engine.isInboundDone());
216
log("\tisOutboundDone() : " + engine.isOutboundDone());
217
log("\tMore Result : " + result);
218
}
219
220
private static void log(String message) {
221
System.err.println(message);
222
}
223
224
// If the result indicates that we have outstanding tasks to do,
225
// go ahead and run them in this thread.
226
protected static void runDelegatedTasks(SSLEngine engine) throws Exception {
227
if (engine.getHandshakeStatus() == HandshakeStatus.NEED_TASK) {
228
Runnable runnable;
229
while ((runnable = engine.getDelegatedTask()) != null) {
230
log(" running delegated task...");
231
runnable.run();
232
}
233
HandshakeStatus hsStatus = engine.getHandshakeStatus();
234
if (hsStatus == HandshakeStatus.NEED_TASK) {
235
throw new Exception(
236
"handshake shouldn't need additional tasks");
237
}
238
logEngineStatus(engine);
239
}
240
}
241
242
// Simple check to make sure everything came across as expected.
243
static void checkTransfer(ByteBuffer a, ByteBuffer b)
244
throws Exception {
245
a.flip();
246
b.flip();
247
248
if (!a.equals(b)) {
249
throw new Exception("Data didn't transfer cleanly");
250
} else {
251
log("\tData transferred cleanly");
252
}
253
254
a.position(a.limit());
255
b.position(b.limit());
256
a.limit(a.capacity());
257
b.limit(b.capacity());
258
}
259
}
260
261