Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/test/micro/org/openjdk/bench/java/security/SSLHandshake.java
66646 views
1
/*
2
* Copyright (c) 2022, 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
package org.openjdk.bench.java.security;
25
26
import org.openjdk.jmh.annotations.Benchmark;
27
import org.openjdk.jmh.annotations.BenchmarkMode;
28
import org.openjdk.jmh.annotations.Fork;
29
import org.openjdk.jmh.annotations.Level;
30
import org.openjdk.jmh.annotations.Measurement;
31
import org.openjdk.jmh.annotations.Mode;
32
import org.openjdk.jmh.annotations.OutputTimeUnit;
33
import org.openjdk.jmh.annotations.Param;
34
import org.openjdk.jmh.annotations.Scope;
35
import org.openjdk.jmh.annotations.Setup;
36
import org.openjdk.jmh.annotations.State;
37
import org.openjdk.jmh.annotations.TearDown;
38
import org.openjdk.jmh.annotations.Warmup;
39
40
import java.nio.ByteBuffer;
41
import java.security.KeyStore;
42
import java.util.concurrent.TimeUnit;
43
import javax.net.ssl.KeyManagerFactory;
44
import javax.net.ssl.SSLContext;
45
import javax.net.ssl.SSLEngine;
46
import javax.net.ssl.SSLEngineResult;
47
import javax.net.ssl.SSLEngineResult.HandshakeStatus;
48
import javax.net.ssl.SSLSession;
49
import javax.net.ssl.TrustManagerFactory;
50
51
@BenchmarkMode(Mode.Throughput)
52
@OutputTimeUnit(TimeUnit.SECONDS)
53
@State(Scope.Benchmark)
54
public class SSLHandshake {
55
56
private SSLContext sslc;
57
58
private SSLEngine clientEngine;
59
private ByteBuffer clientOut = ByteBuffer.allocate(5);
60
private ByteBuffer clientIn = ByteBuffer.allocate(1 << 15);
61
62
private SSLEngine serverEngine;
63
private ByteBuffer serverOut = ByteBuffer.allocate(5);
64
private ByteBuffer serverIn = ByteBuffer.allocate(1 << 15);
65
66
private ByteBuffer cTOs = ByteBuffer.allocateDirect(1 << 16);
67
private ByteBuffer sTOc = ByteBuffer.allocateDirect(1 << 16);
68
69
@Param({"true", "false"})
70
boolean resume;
71
72
@Param({"TLSv1.2", "TLS"})
73
String tlsVersion;
74
75
@Setup(Level.Trial)
76
public void init() throws Exception {
77
KeyStore ks = TestCertificates.getKeyStore();
78
KeyStore ts = TestCertificates.getTrustStore();
79
80
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
81
kmf.init(ks, new char[0]);
82
83
TrustManagerFactory tmf =
84
TrustManagerFactory.getInstance("SunX509");
85
tmf.init(ts);
86
87
SSLContext sslCtx = SSLContext.getInstance(tlsVersion);
88
sslCtx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
89
sslc = sslCtx;
90
}
91
92
private HandshakeStatus checkResult(SSLEngine engine, SSLEngineResult result) {
93
94
HandshakeStatus hsStatus = result.getHandshakeStatus();
95
96
if (hsStatus == HandshakeStatus.NEED_TASK) {
97
Runnable runnable;
98
while ((runnable = engine.getDelegatedTask()) != null) {
99
runnable.run();
100
}
101
hsStatus = engine.getHandshakeStatus();
102
}
103
return hsStatus;
104
}
105
106
/**
107
* This benchmark measures the time needed to perform a TLS handshake.
108
* Data is exchanged using a pair of ByteBuffers.
109
* The client and the server both operate on the same thread.
110
*/
111
@Benchmark
112
@Warmup(iterations = 5, time = 5, timeUnit = TimeUnit.SECONDS)
113
@Measurement(iterations = 5, time = 5, timeUnit = TimeUnit.SECONDS)
114
@Fork(3)
115
public SSLSession doHandshake() throws Exception {
116
117
createSSLEngines();
118
boolean isCtoS = true;
119
for (;;) {
120
HandshakeStatus result;
121
if (isCtoS) {
122
result = checkResult(clientEngine,
123
clientEngine.wrap(clientOut, cTOs)
124
);
125
cTOs.flip();
126
checkResult(serverEngine,
127
serverEngine.unwrap(cTOs, serverIn)
128
);
129
cTOs.compact();
130
if (result == HandshakeStatus.NEED_UNWRAP) {
131
isCtoS = false;
132
} else if (result == HandshakeStatus.FINISHED) {
133
break;
134
} else if (result != HandshakeStatus.NEED_WRAP) {
135
throw new Exception("Unexpected result "+result);
136
}
137
} else {
138
result = checkResult(serverEngine,
139
serverEngine.wrap(serverOut, sTOc)
140
);
141
sTOc.flip();
142
checkResult(clientEngine,
143
clientEngine.unwrap(sTOc, clientIn)
144
);
145
sTOc.compact();
146
if (result == HandshakeStatus.NEED_UNWRAP) {
147
isCtoS = true;
148
} else if (result == HandshakeStatus.FINISHED) {
149
break;
150
} else if (result != HandshakeStatus.NEED_WRAP) {
151
throw new Exception("Unexpected result "+result);
152
}
153
}
154
}
155
156
SSLSession session = clientEngine.getSession();
157
if (resume) {
158
// TLS 1.3 needs another wrap/unwrap to deliver a session ticket
159
serverEngine.wrap(serverOut, sTOc);
160
sTOc.flip();
161
clientEngine.unwrap(sTOc, clientIn);
162
sTOc.compact();
163
} else {
164
// invalidate TLS1.2 session. TLS 1.3 doesn't care
165
session.invalidate();
166
}
167
return session;
168
}
169
170
private void createSSLEngines() {
171
/*
172
* Configure the serverEngine to act as a server in the SSL/TLS
173
* handshake.
174
*/
175
serverEngine = sslc.createSSLEngine();
176
serverEngine.setUseClientMode(false);
177
178
/*
179
* Similar to above, but using client mode instead.
180
*/
181
clientEngine = sslc.createSSLEngine("client", 80);
182
clientEngine.setUseClientMode(true);
183
}
184
}
185
186