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/CertificateStatus.java
41159 views
1
/*
2
* Copyright (c) 2015, 2021, 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.List;
32
import java.util.ArrayList;
33
import java.util.Locale;
34
import javax.net.ssl.SSLHandshakeException;
35
import java.security.cert.X509Certificate;
36
import sun.security.provider.certpath.OCSPResponse;
37
import sun.security.ssl.SSLHandshake.HandshakeMessage;
38
import static sun.security.ssl.CertStatusExtension.*;
39
import static sun.security.ssl.CertificateMessage.*;
40
41
/**
42
* Consumers and producers for the CertificateStatus handshake message.
43
* This message takes one of two related but slightly different forms,
44
* depending on the type of stapling selected by the server. The message
45
* data will be of the form(s):
46
*
47
* [status_request, RFC 6066]
48
*
49
* struct {
50
* CertificateStatusType status_type;
51
* select (status_type) {
52
* case ocsp: OCSPResponse;
53
* } response;
54
* } CertificateStatus;
55
*
56
* opaque OCSPResponse<1..2^24-1>;
57
*
58
* [status_request_v2, RFC 6961]
59
*
60
* struct {
61
* CertificateStatusType status_type;
62
* select (status_type) {
63
* case ocsp: OCSPResponse;
64
* case ocsp_multi: OCSPResponseList;
65
* } response;
66
* } CertificateStatus;
67
*
68
* opaque OCSPResponse<0..2^24-1>;
69
*
70
* struct {
71
* OCSPResponse ocsp_response_list<1..2^24-1>;
72
* } OCSPResponseList;
73
*/
74
final class CertificateStatus {
75
static final SSLConsumer handshakeConsumer =
76
new CertificateStatusConsumer();
77
static final HandshakeProducer handshakeProducer =
78
new CertificateStatusProducer();
79
static final HandshakeAbsence handshakeAbsence =
80
new CertificateStatusAbsence();
81
82
/**
83
* The CertificateStatus handshake message.
84
*/
85
static final class CertificateStatusMessage extends HandshakeMessage {
86
87
final CertStatusRequestType statusType;
88
final int encodedResponsesLen;
89
final int messageLength;
90
final List<byte[]> encodedResponses = new ArrayList<>();
91
92
CertificateStatusMessage(HandshakeContext handshakeContext) {
93
super(handshakeContext);
94
95
ServerHandshakeContext shc =
96
(ServerHandshakeContext)handshakeContext;
97
98
// Get the Certificates from the SSLContextImpl amd the Stapling
99
// parameters
100
StatusResponseManager.StaplingParameters stapleParams =
101
shc.stapleParams;
102
if (stapleParams == null) {
103
throw new IllegalArgumentException(
104
"Unexpected null stapling parameters");
105
}
106
107
X509Certificate[] certChain =
108
(X509Certificate[])shc.handshakeSession.getLocalCertificates();
109
if (certChain == null) {
110
throw new IllegalArgumentException(
111
"Unexpected null certificate chain");
112
}
113
114
// Walk the certificate list and add the correct encoded responses
115
// to the encoded responses list
116
statusType = stapleParams.statReqType;
117
int encodedLen = 0;
118
if (statusType == CertStatusRequestType.OCSP) {
119
// Just worry about the first cert in the chain
120
byte[] resp = stapleParams.responseMap.get(certChain[0]);
121
if (resp == null) {
122
// A not-found return status means we should include
123
// a zero-length response in CertificateStatus.
124
// This is highly unlikely to happen in practice.
125
resp = new byte[0];
126
}
127
encodedResponses.add(resp);
128
encodedLen += resp.length + 3;
129
} else if (statusType == CertStatusRequestType.OCSP_MULTI) {
130
for (X509Certificate cert : certChain) {
131
byte[] resp = stapleParams.responseMap.get(cert);
132
if (resp == null) {
133
resp = new byte[0];
134
}
135
encodedResponses.add(resp);
136
encodedLen += resp.length + 3;
137
}
138
} else {
139
throw new IllegalArgumentException(
140
"Unsupported StatusResponseType: " + statusType);
141
}
142
143
encodedResponsesLen = encodedLen;
144
messageLength = messageLength(statusType, encodedResponsesLen);
145
}
146
147
CertificateStatusMessage(HandshakeContext handshakeContext,
148
ByteBuffer m) throws IOException {
149
super(handshakeContext);
150
151
statusType = CertStatusRequestType.valueOf((byte)Record.getInt8(m));
152
if (statusType == CertStatusRequestType.OCSP) {
153
byte[] respDER = Record.getBytes24(m);
154
// Convert the incoming bytes to a OCSPResponse strucutre
155
if (respDER.length > 0) {
156
encodedResponses.add(respDER);
157
encodedResponsesLen = 3 + respDER.length;
158
} else {
159
throw handshakeContext.conContext.fatal(
160
Alert.HANDSHAKE_FAILURE,
161
"Zero-length OCSP Response");
162
}
163
} else if (statusType == CertStatusRequestType.OCSP_MULTI) {
164
int respListLen = Record.getInt24(m);
165
encodedResponsesLen = respListLen;
166
167
// Add each OCSP reponse into the array list in the order
168
// we receive them off the wire. A zero-length array is
169
// allowed for ocsp_multi, and means that a response for
170
// a given certificate is not available.
171
while (respListLen > 0) {
172
byte[] respDER = Record.getBytes24(m);
173
encodedResponses.add(respDER);
174
respListLen -= (respDER.length + 3);
175
}
176
177
if (respListLen != 0) {
178
throw handshakeContext.conContext.fatal(
179
Alert.INTERNAL_ERROR,
180
"Bad OCSP response list length");
181
}
182
} else {
183
throw handshakeContext.conContext.fatal(
184
Alert.HANDSHAKE_FAILURE,
185
"Unsupported StatusResponseType: " + statusType);
186
}
187
messageLength = messageLength(statusType, encodedResponsesLen);
188
}
189
190
private static int messageLength(
191
CertStatusRequestType statusType, int encodedResponsesLen) {
192
if (statusType == CertStatusRequestType.OCSP) {
193
return 1 + encodedResponsesLen;
194
} else if (statusType == CertStatusRequestType.OCSP_MULTI) {
195
return 4 + encodedResponsesLen;
196
}
197
198
return -1;
199
}
200
201
@Override
202
public SSLHandshake handshakeType() {
203
return SSLHandshake.CERTIFICATE_STATUS;
204
}
205
206
@Override
207
public int messageLength() {
208
return messageLength;
209
}
210
211
@Override
212
public void send(HandshakeOutStream s) throws IOException {
213
s.putInt8(statusType.id);
214
if (statusType == CertStatusRequestType.OCSP) {
215
s.putBytes24(encodedResponses.get(0));
216
} else if (statusType == CertStatusRequestType.OCSP_MULTI) {
217
s.putInt24(encodedResponsesLen);
218
for (byte[] respBytes : encodedResponses) {
219
s.putBytes24(respBytes);
220
}
221
} else {
222
// It is highly unlikely that we will fall into this section
223
// of the code.
224
throw new SSLHandshakeException("Unsupported status_type: " +
225
statusType.id);
226
}
227
}
228
229
@Override
230
public String toString() {
231
StringBuilder sb = new StringBuilder();
232
233
// Stringify the encoded OCSP response list
234
for (byte[] respDER : encodedResponses) {
235
if (respDER.length > 0) {
236
try {
237
OCSPResponse oResp = new OCSPResponse(respDER);
238
sb.append(oResp.toString()).append("\n");
239
} catch (IOException ioe) {
240
sb.append("OCSP Response Exception: ").append(ioe)
241
.append("\n");
242
}
243
} else {
244
sb.append("<Zero-length entry>\n");
245
}
246
}
247
248
MessageFormat messageFormat = new MessageFormat(
249
"\"CertificateStatus\": '{'\n" +
250
" \"type\" : \"{0}\",\n" +
251
" \"responses \" : [\n" + "{1}\n" + " ]\n" +
252
"'}'",
253
Locale.ENGLISH);
254
Object[] messageFields = {
255
statusType.name,
256
Utilities.indent(Utilities.indent(sb.toString()))
257
};
258
259
return messageFormat.format(messageFields);
260
}
261
}
262
263
/**
264
* The CertificateStatus handshake message consumer.
265
*/
266
private static final class CertificateStatusConsumer
267
implements SSLConsumer {
268
// Prevent instantiation of this class.
269
private CertificateStatusConsumer() {
270
// blank
271
}
272
273
@Override
274
public void consume(ConnectionContext context,
275
ByteBuffer message) throws IOException {
276
ClientHandshakeContext chc = (ClientHandshakeContext)context;
277
CertificateStatusMessage cst =
278
new CertificateStatusMessage(chc, message);
279
280
// Log the message
281
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
282
SSLLogger.fine(
283
"Consuming server CertificateStatus handshake message",
284
cst);
285
}
286
287
// Pin the received responses to the SSLSessionImpl. It will
288
// be retrieved by the X509TrustManagerImpl during the certificate
289
// checking phase.
290
chc.handshakeSession.setStatusResponses(cst.encodedResponses);
291
292
// Now perform the check
293
T12CertificateConsumer.checkServerCerts(chc, chc.deferredCerts);
294
295
// Update the handshake consumers to remove this message, indicating
296
// that it has been processed.
297
chc.handshakeConsumers.remove(SSLHandshake.CERTIFICATE_STATUS.id);
298
}
299
}
300
301
/**
302
* The CertificateStatus handshake message consumer.
303
*/
304
private static final class CertificateStatusProducer
305
implements HandshakeProducer {
306
// Prevent instantiation of this class.
307
private CertificateStatusProducer() {
308
// blank
309
}
310
311
@Override
312
public byte[] produce(ConnectionContext context,
313
HandshakeMessage message) throws IOException {
314
// Only the server-side should be a producer of this message
315
ServerHandshakeContext shc = (ServerHandshakeContext)context;
316
317
// If stapling is not active, immediately return without producing
318
// a message or any further processing.
319
if (!shc.staplingActive) {
320
return null;
321
}
322
323
// Create the CertificateStatus message from info in the
324
CertificateStatusMessage csm = new CertificateStatusMessage(shc);
325
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
326
SSLLogger.fine(
327
"Produced server CertificateStatus handshake message", csm);
328
}
329
330
// Output the handshake message.
331
csm.write(shc.handshakeOutput);
332
shc.handshakeOutput.flush();
333
334
// The handshake message has been delivered.
335
return null;
336
}
337
}
338
339
private static final class CertificateStatusAbsence
340
implements HandshakeAbsence {
341
// Prevent instantiation of this class
342
private CertificateStatusAbsence() {
343
// blank
344
}
345
346
@Override
347
public void absent(ConnectionContext context,
348
HandshakeMessage message) throws IOException {
349
ClientHandshakeContext chc = (ClientHandshakeContext)context;
350
351
// Processing should only continue if stapling is active
352
if (chc.staplingActive) {
353
// Because OCSP stapling is active, it means two things
354
// if we're here: 1) The server hello asserted the
355
// status_request[_v2] extension. 2) The CertificateStatus
356
// message was not sent. This means that cert path checking
357
// was deferred, but must happen immediately.
358
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
359
SSLLogger.fine("Server did not send CertificateStatus, " +
360
"checking cert chain without status info.");
361
}
362
T12CertificateConsumer.checkServerCerts(chc, chc.deferredCerts);
363
}
364
}
365
}
366
}
367
368
369