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