Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/sun/security/ssl/CookieExtension.java
41159 views
1
/*
2
* Copyright (c) 2018, 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. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
package sun.security.ssl;
27
28
import java.io.IOException;
29
import java.nio.ByteBuffer;
30
import java.text.MessageFormat;
31
import java.util.Locale;
32
import javax.net.ssl.SSLProtocolException;
33
34
import sun.security.ssl.ClientHello.ClientHelloMessage;
35
import sun.security.ssl.SSLExtension.ExtensionConsumer;
36
import sun.security.ssl.SSLHandshake.HandshakeMessage;
37
import sun.security.ssl.SSLExtension.SSLExtensionSpec;
38
import sun.security.ssl.ServerHello.ServerHelloMessage;
39
import sun.security.util.HexDumpEncoder;
40
41
public class CookieExtension {
42
static final HandshakeProducer chNetworkProducer =
43
new CHCookieProducer();
44
static final ExtensionConsumer chOnLoadConsumer =
45
new CHCookieConsumer();
46
static final HandshakeConsumer chOnTradeConsumer =
47
new CHCookieUpdate();
48
49
static final HandshakeProducer hrrNetworkProducer =
50
new HRRCookieProducer();
51
static final ExtensionConsumer hrrOnLoadConsumer =
52
new HRRCookieConsumer();
53
54
static final HandshakeProducer hrrNetworkReproducer =
55
new HRRCookieReproducer();
56
57
static final CookieStringizer cookieStringizer =
58
new CookieStringizer();
59
60
/**
61
* The "cookie" extension.
62
*/
63
static class CookieSpec implements SSLExtensionSpec {
64
final byte[] cookie;
65
66
private CookieSpec(HandshakeContext hc,
67
ByteBuffer m) throws IOException {
68
// opaque cookie<1..2^16-1>;
69
if (m.remaining() < 3) {
70
throw hc.conContext.fatal(Alert.DECODE_ERROR,
71
new SSLProtocolException(
72
"Invalid cookie extension: insufficient data"));
73
}
74
75
this.cookie = Record.getBytes16(m);
76
}
77
78
@Override
79
public String toString() {
80
MessageFormat messageFormat = new MessageFormat(
81
"\"cookie\": '{'\n" +
82
"{0}\n" +
83
"'}',", Locale.ENGLISH);
84
HexDumpEncoder hexEncoder = new HexDumpEncoder();
85
Object[] messageFields = {
86
Utilities.indent(hexEncoder.encode(cookie))
87
};
88
89
return messageFormat.format(messageFields);
90
}
91
}
92
93
private static final class CookieStringizer implements SSLStringizer {
94
@Override
95
public String toString(HandshakeContext hc, ByteBuffer buffer) {
96
try {
97
return (new CookieSpec(hc, buffer)).toString();
98
} catch (IOException ioe) {
99
// For debug logging only, so please swallow exceptions.
100
return ioe.getMessage();
101
}
102
}
103
}
104
105
private static final
106
class CHCookieProducer implements HandshakeProducer {
107
// Prevent instantiation of this class.
108
private CHCookieProducer() {
109
// blank
110
}
111
112
@Override
113
public byte[] produce(ConnectionContext context,
114
HandshakeMessage message) throws IOException {
115
ClientHandshakeContext chc = (ClientHandshakeContext) context;
116
117
// Is it a supported and enabled extension?
118
if (!chc.sslConfig.isAvailable(SSLExtension.CH_COOKIE)) {
119
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
120
SSLLogger.fine(
121
"Ignore unavailable cookie extension");
122
}
123
return null;
124
}
125
126
// response to an HelloRetryRequest cookie
127
CookieSpec spec = (CookieSpec)chc.handshakeExtensions.get(
128
SSLExtension.HRR_COOKIE);
129
130
if (spec != null &&
131
spec.cookie != null && spec.cookie.length != 0) {
132
byte[] extData = new byte[spec.cookie.length + 2];
133
ByteBuffer m = ByteBuffer.wrap(extData);
134
Record.putBytes16(m, spec.cookie);
135
return extData;
136
}
137
138
return null;
139
}
140
}
141
142
private static final
143
class CHCookieConsumer implements ExtensionConsumer {
144
// Prevent instantiation of this class.
145
private CHCookieConsumer() {
146
// blank
147
}
148
149
@Override
150
public void consume(ConnectionContext context,
151
HandshakeMessage message, ByteBuffer buffer) throws IOException {
152
// The consuming happens in server side only.
153
ServerHandshakeContext shc = (ServerHandshakeContext)context;
154
155
// Is it a supported and enabled extension?
156
if (!shc.sslConfig.isAvailable(SSLExtension.CH_COOKIE)) {
157
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
158
SSLLogger.fine(
159
"Ignore unavailable cookie extension");
160
}
161
return; // ignore the extension
162
}
163
164
CookieSpec spec = new CookieSpec(shc, buffer);
165
shc.handshakeExtensions.put(SSLExtension.CH_COOKIE, spec);
166
167
// No impact on session resumption.
168
//
169
// Note that the protocol version negotiation happens before the
170
// session resumption negotiation. And the session resumption
171
// negotiation depends on the negotiated protocol version.
172
}
173
}
174
175
private static final
176
class CHCookieUpdate implements HandshakeConsumer {
177
// Prevent instantiation of this class.
178
private CHCookieUpdate() {
179
// blank
180
}
181
182
@Override
183
public void consume(ConnectionContext context,
184
HandshakeMessage message) throws IOException {
185
// The consuming happens in server side only.
186
ServerHandshakeContext shc = (ServerHandshakeContext)context;
187
ClientHelloMessage clientHello = (ClientHelloMessage)message;
188
189
CookieSpec spec = (CookieSpec)
190
shc.handshakeExtensions.get(SSLExtension.CH_COOKIE);
191
if (spec == null) {
192
// Ignore, no "cookie" extension requested.
193
return;
194
}
195
196
HelloCookieManager hcm =
197
shc.sslContext.getHelloCookieManager(shc.negotiatedProtocol);
198
if (!hcm.isCookieValid(shc, clientHello, spec.cookie)) {
199
throw shc.conContext.fatal(Alert.UNEXPECTED_MESSAGE,
200
"unrecognized cookie");
201
}
202
}
203
}
204
205
private static final
206
class HRRCookieProducer implements HandshakeProducer {
207
// Prevent instantiation of this class.
208
private HRRCookieProducer() {
209
// blank
210
}
211
212
@Override
213
public byte[] produce(ConnectionContext context,
214
HandshakeMessage message) throws IOException {
215
// The producing happens in server side only.
216
ServerHandshakeContext shc = (ServerHandshakeContext)context;
217
ServerHelloMessage hrrm = (ServerHelloMessage)message;
218
219
// Is it a supported and enabled extension?
220
if (!shc.sslConfig.isAvailable(SSLExtension.HRR_COOKIE)) {
221
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
222
SSLLogger.fine(
223
"Ignore unavailable cookie extension");
224
}
225
return null;
226
}
227
228
HelloCookieManager hcm =
229
shc.sslContext.getHelloCookieManager(shc.negotiatedProtocol);
230
231
byte[] cookie = hcm.createCookie(shc, hrrm.clientHello);
232
233
byte[] extData = new byte[cookie.length + 2];
234
ByteBuffer m = ByteBuffer.wrap(extData);
235
Record.putBytes16(m, cookie);
236
237
return extData;
238
}
239
}
240
241
private static final
242
class HRRCookieConsumer implements ExtensionConsumer {
243
// Prevent instantiation of this class.
244
private HRRCookieConsumer() {
245
// blank
246
}
247
248
@Override
249
public void consume(ConnectionContext context,
250
HandshakeMessage message, ByteBuffer buffer) throws IOException {
251
// The consuming happens in client side only.
252
ClientHandshakeContext chc = (ClientHandshakeContext)context;
253
254
// Is it a supported and enabled extension?
255
if (!chc.sslConfig.isAvailable(SSLExtension.HRR_COOKIE)) {
256
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
257
SSLLogger.fine(
258
"Ignore unavailable cookie extension");
259
}
260
return; // ignore the extension
261
}
262
263
CookieSpec spec = new CookieSpec(chc, buffer);
264
chc.handshakeExtensions.put(SSLExtension.HRR_COOKIE, spec);
265
}
266
}
267
268
private static final
269
class HRRCookieReproducer implements HandshakeProducer {
270
// Prevent instantiation of this class.
271
private HRRCookieReproducer() {
272
// blank
273
}
274
275
@Override
276
public byte[] produce(ConnectionContext context,
277
HandshakeMessage message) throws IOException {
278
// The producing happens in server side only.
279
ServerHandshakeContext shc = (ServerHandshakeContext) context;
280
281
// Is it a supported and enabled extension?
282
if (!shc.sslConfig.isAvailable(SSLExtension.HRR_COOKIE)) {
283
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
284
SSLLogger.fine(
285
"Ignore unavailable cookie extension");
286
}
287
return null;
288
}
289
290
// copy of the ClientHello cookie
291
CookieSpec spec = (CookieSpec)shc.handshakeExtensions.get(
292
SSLExtension.CH_COOKIE);
293
294
if (spec != null &&
295
spec.cookie != null && spec.cookie.length != 0) {
296
byte[] extData = new byte[spec.cookie.length + 2];
297
ByteBuffer m = ByteBuffer.wrap(extData);
298
Record.putBytes16(m, spec.cookie);
299
return extData;
300
}
301
302
return null;
303
}
304
}
305
}
306
307