author | rhalade |
Tue, 12 Apr 2016 09:37:46 -0700 | |
changeset 37309 | 8f530b9d18f4 |
parent 32032 | 22badc53802f |
child 40390 | 64541737c7f7 |
permissions | -rw-r--r-- |
32032 | 1 |
/* |
37309
8f530b9d18f4
8153829: javax/net/ssl/Stapling/HttpsUrlConnClient.java fails intermittently with NullPointerException
rhalade
parents:
32032
diff
changeset
|
2 |
* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. |
32032 | 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.testlibrary; |
|
27 |
||
28 |
import java.io.*; |
|
29 |
import java.net.*; |
|
30 |
import java.security.*; |
|
31 |
import java.security.cert.CRLReason; |
|
32 |
import java.security.cert.X509Certificate; |
|
33 |
import java.security.cert.Extension; |
|
34 |
import java.security.cert.CertificateException; |
|
35 |
import java.security.cert.CertificateEncodingException; |
|
36 |
import java.security.Signature; |
|
37 |
import java.util.*; |
|
38 |
import java.util.concurrent.*; |
|
39 |
import java.text.SimpleDateFormat; |
|
40 |
import java.math.BigInteger; |
|
41 |
||
42 |
import sun.security.x509.*; |
|
43 |
import sun.security.x509.PKIXExtensions; |
|
44 |
import sun.security.provider.certpath.ResponderId; |
|
45 |
import sun.security.provider.certpath.CertId; |
|
46 |
import sun.security.provider.certpath.OCSPResponse; |
|
47 |
import sun.security.provider.certpath.OCSPResponse.ResponseStatus; |
|
48 |
import sun.security.util.Debug; |
|
49 |
import sun.security.util.DerInputStream; |
|
50 |
import sun.security.util.DerOutputStream; |
|
51 |
import sun.security.util.DerValue; |
|
52 |
import sun.security.util.ObjectIdentifier; |
|
53 |
||
54 |
||
55 |
/** |
|
56 |
* This is a simple OCSP server designed to listen and respond to incoming |
|
57 |
* requests. |
|
58 |
*/ |
|
59 |
public class SimpleOCSPServer { |
|
60 |
private final Debug debug = Debug.getInstance("oserv"); |
|
61 |
private static final ObjectIdentifier OCSP_BASIC_RESPONSE_OID = |
|
62 |
ObjectIdentifier.newInternal( |
|
63 |
new int[] { 1, 3, 6, 1, 5, 5, 7, 48, 1, 1}); |
|
64 |
private static final SimpleDateFormat utcDateFmt = |
|
65 |
new SimpleDateFormat("MMM dd yyyy, HH:mm:ss z"); |
|
66 |
||
67 |
// CertStatus values |
|
68 |
public static enum CertStatus { |
|
69 |
CERT_STATUS_GOOD, |
|
70 |
CERT_STATUS_REVOKED, |
|
71 |
CERT_STATUS_UNKNOWN, |
|
72 |
} |
|
73 |
||
74 |
// Fields used for the networking portion of the responder |
|
75 |
private ServerSocket servSocket; |
|
76 |
private InetAddress listenAddress; |
|
77 |
private int listenPort; |
|
78 |
||
79 |
// Keystore information (certs, keys, etc.) |
|
80 |
private KeyStore keystore; |
|
81 |
private X509Certificate issuerCert; |
|
82 |
private X509Certificate signerCert; |
|
83 |
private PrivateKey signerKey; |
|
84 |
||
85 |
// Fields used for the operational portions of the server |
|
86 |
private boolean logEnabled = false; |
|
87 |
private ExecutorService threadPool; |
|
88 |
private volatile boolean started = false; |
|
37309
8f530b9d18f4
8153829: javax/net/ssl/Stapling/HttpsUrlConnClient.java fails intermittently with NullPointerException
rhalade
parents:
32032
diff
changeset
|
89 |
private volatile boolean serverReady = false; |
32032 | 90 |
private volatile boolean receivedShutdown = false; |
91 |
private long delayMsec = 0; |
|
92 |
||
93 |
// Fields used in the generation of responses |
|
94 |
private long nextUpdateInterval = -1; |
|
95 |
private Date nextUpdate = null; |
|
96 |
private ResponderId respId; |
|
97 |
private AlgorithmId sigAlgId; |
|
98 |
private Map<CertId, CertStatusInfo> statusDb = |
|
99 |
Collections.synchronizedMap(new HashMap<>()); |
|
100 |
||
101 |
/** |
|
102 |
* Construct a SimpleOCSPServer using keystore, password, and alias |
|
103 |
* parameters. |
|
104 |
* |
|
105 |
* @param ks the keystore to be used |
|
106 |
* @param password the password to access key material in the keystore |
|
107 |
* @param issuerAlias the alias of the issuer certificate |
|
108 |
* @param signerAlias the alias of the signer certificate and key. A |
|
109 |
* value of {@code null} means that the {@code issuerAlias} will be used |
|
110 |
* to look up the signer key. |
|
111 |
* |
|
112 |
* @throws GeneralSecurityException if there are problems accessing the |
|
113 |
* keystore or finding objects within the keystore. |
|
114 |
* @throws IOException if a {@code ResponderId} cannot be generated from |
|
115 |
* the signer certificate. |
|
116 |
*/ |
|
117 |
public SimpleOCSPServer(KeyStore ks, String password, String issuerAlias, |
|
118 |
String signerAlias) throws GeneralSecurityException, IOException { |
|
119 |
this(null, 0, ks, password, issuerAlias, signerAlias); |
|
120 |
} |
|
121 |
||
122 |
/** |
|
123 |
* Construct a SimpleOCSPServer using specific network parameters, |
|
124 |
* keystore, password, and alias. |
|
125 |
* |
|
126 |
* @param addr the address to bind the server to. A value of {@code null} |
|
127 |
* means the server will bind to all interfaces. |
|
128 |
* @param port the port to listen on. A value of {@code 0} will mean that |
|
129 |
* the server will randomly pick an open ephemeral port to bind to. |
|
130 |
* @param ks the keystore to be used |
|
131 |
* @param password the password to access key material in the keystore |
|
132 |
* @param issuerAlias the alias of the issuer certificate |
|
133 |
* @param signerAlias the alias of the signer certificate and key. A |
|
134 |
* value of {@code null} means that the {@code issuerAlias} will be used |
|
135 |
* to look up the signer key. |
|
136 |
* |
|
137 |
* @throws GeneralSecurityException if there are problems accessing the |
|
138 |
* keystore or finding objects within the keystore. |
|
139 |
* @throws IOException if a {@code ResponderId} cannot be generated from |
|
140 |
* the signer certificate. |
|
141 |
*/ |
|
142 |
public SimpleOCSPServer(InetAddress addr, int port, KeyStore ks, |
|
143 |
String password, String issuerAlias, String signerAlias) |
|
144 |
throws GeneralSecurityException, IOException { |
|
145 |
Objects.requireNonNull(ks, "Null keystore provided"); |
|
146 |
Objects.requireNonNull(issuerAlias, "Null issuerName provided"); |
|
147 |
||
148 |
utcDateFmt.setTimeZone(TimeZone.getTimeZone("GMT")); |
|
149 |
||
150 |
keystore = ks; |
|
151 |
issuerCert = (X509Certificate)ks.getCertificate(issuerAlias); |
|
152 |
if (issuerCert == null) { |
|
153 |
throw new IllegalArgumentException("Certificate for alias " + |
|
154 |
issuerAlias + " not found"); |
|
155 |
} |
|
156 |
||
157 |
if (signerAlias != null) { |
|
158 |
signerCert = (X509Certificate)ks.getCertificate(signerAlias); |
|
159 |
if (signerCert == null) { |
|
160 |
throw new IllegalArgumentException("Certificate for alias " + |
|
161 |
signerAlias + " not found"); |
|
162 |
} |
|
163 |
signerKey = (PrivateKey)ks.getKey(signerAlias, |
|
164 |
password.toCharArray()); |
|
165 |
if (signerKey == null) { |
|
166 |
throw new IllegalArgumentException("PrivateKey for alias " + |
|
167 |
signerAlias + " not found"); |
|
168 |
} |
|
169 |
} else { |
|
170 |
signerCert = issuerCert; |
|
171 |
signerKey = (PrivateKey)ks.getKey(issuerAlias, |
|
172 |
password.toCharArray()); |
|
173 |
if (signerKey == null) { |
|
174 |
throw new IllegalArgumentException("PrivateKey for alias " + |
|
175 |
issuerAlias + " not found"); |
|
176 |
} |
|
177 |
} |
|
178 |
||
179 |
sigAlgId = AlgorithmId.get("Sha256withRSA"); |
|
180 |
respId = new ResponderId(signerCert.getSubjectX500Principal()); |
|
181 |
listenAddress = addr; |
|
182 |
listenPort = port; |
|
183 |
} |
|
184 |
||
185 |
/** |
|
186 |
* Start the server. The server will bind to the specified network |
|
187 |
* address and begin listening for incoming connections. |
|
188 |
* |
|
189 |
* @throws IOException if any number of things go wonky. |
|
190 |
*/ |
|
191 |
public synchronized void start() throws IOException { |
|
192 |
// You cannot start the server twice. |
|
193 |
if (started) { |
|
194 |
log("Server has already been started"); |
|
195 |
return; |
|
196 |
} else { |
|
197 |
started = true; |
|
198 |
} |
|
199 |
||
200 |
// Create and start the thread pool |
|
201 |
threadPool = Executors.newFixedThreadPool(32, new ThreadFactory() { |
|
202 |
@Override |
|
203 |
public Thread newThread(Runnable r) { |
|
204 |
Thread t = Executors.defaultThreadFactory().newThread(r); |
|
205 |
t.setDaemon(true); |
|
206 |
return t; |
|
207 |
} |
|
208 |
}); |
|
209 |
||
210 |
threadPool.submit(new Runnable() { |
|
211 |
@Override |
|
212 |
public void run() { |
|
213 |
try (ServerSocket sSock = new ServerSocket()) { |
|
214 |
servSocket = sSock; |
|
215 |
servSocket.setReuseAddress(true); |
|
216 |
servSocket.setSoTimeout(500); |
|
217 |
servSocket.bind(new InetSocketAddress(listenAddress, |
|
218 |
listenPort), 128); |
|
219 |
log("Listening on " + servSocket.getLocalSocketAddress()); |
|
220 |
||
37309
8f530b9d18f4
8153829: javax/net/ssl/Stapling/HttpsUrlConnClient.java fails intermittently with NullPointerException
rhalade
parents:
32032
diff
changeset
|
221 |
// Singal ready |
8f530b9d18f4
8153829: javax/net/ssl/Stapling/HttpsUrlConnClient.java fails intermittently with NullPointerException
rhalade
parents:
32032
diff
changeset
|
222 |
serverReady = true; |
8f530b9d18f4
8153829: javax/net/ssl/Stapling/HttpsUrlConnClient.java fails intermittently with NullPointerException
rhalade
parents:
32032
diff
changeset
|
223 |
|
32032 | 224 |
// Update the listenPort with the new port number. If |
225 |
// the server is restarted, it will bind to the same |
|
226 |
// port rather than picking a new one. |
|
227 |
listenPort = servSocket.getLocalPort(); |
|
228 |
||
229 |
// Main dispatch loop |
|
230 |
while (!receivedShutdown) { |
|
231 |
try { |
|
232 |
Socket newConnection = servSocket.accept(); |
|
233 |
threadPool.submit(new OcspHandler(newConnection)); |
|
234 |
} catch (SocketTimeoutException timeout) { |
|
235 |
// Nothing to do here. If receivedShutdown |
|
236 |
// has changed to true then the loop will |
|
237 |
// exit on its own. |
|
238 |
} catch (IOException ioe) { |
|
239 |
// Something bad happened, log and force a shutdown |
|
240 |
log("Unexpected Exception: " + ioe); |
|
241 |
stop(); |
|
242 |
} |
|
243 |
} |
|
244 |
||
245 |
log("Shutting down..."); |
|
246 |
threadPool.shutdown(); |
|
247 |
} catch (IOException ioe) { |
|
248 |
err(ioe); |
|
37309
8f530b9d18f4
8153829: javax/net/ssl/Stapling/HttpsUrlConnClient.java fails intermittently with NullPointerException
rhalade
parents:
32032
diff
changeset
|
249 |
} finally { |
8f530b9d18f4
8153829: javax/net/ssl/Stapling/HttpsUrlConnClient.java fails intermittently with NullPointerException
rhalade
parents:
32032
diff
changeset
|
250 |
// Reset state variables so the server can be restarted |
8f530b9d18f4
8153829: javax/net/ssl/Stapling/HttpsUrlConnClient.java fails intermittently with NullPointerException
rhalade
parents:
32032
diff
changeset
|
251 |
receivedShutdown = false; |
8f530b9d18f4
8153829: javax/net/ssl/Stapling/HttpsUrlConnClient.java fails intermittently with NullPointerException
rhalade
parents:
32032
diff
changeset
|
252 |
started = false; |
8f530b9d18f4
8153829: javax/net/ssl/Stapling/HttpsUrlConnClient.java fails intermittently with NullPointerException
rhalade
parents:
32032
diff
changeset
|
253 |
serverReady = false; |
32032 | 254 |
} |
255 |
} |
|
256 |
}); |
|
257 |
} |
|
258 |
||
259 |
/** |
|
260 |
* Stop the OCSP server. |
|
261 |
*/ |
|
262 |
public synchronized void stop() { |
|
263 |
if (started) { |
|
264 |
receivedShutdown = true; |
|
265 |
log("Received shutdown notification"); |
|
266 |
} |
|
267 |
} |
|
268 |
||
269 |
/** |
|
270 |
* Print {@code SimpleOCSPServer} operating parameters. |
|
271 |
* |
|
272 |
* @return the {@code SimpleOCSPServer} operating parameters in |
|
273 |
* {@code String} form. |
|
274 |
*/ |
|
275 |
@Override |
|
276 |
public String toString() { |
|
277 |
StringBuilder sb = new StringBuilder(); |
|
278 |
sb.append("OCSP Server:\n"); |
|
279 |
sb.append("----------------------------------------------\n"); |
|
280 |
sb.append("issuer: ").append(issuerCert.getSubjectX500Principal()). |
|
281 |
append("\n"); |
|
282 |
sb.append("signer: ").append(signerCert.getSubjectX500Principal()). |
|
283 |
append("\n"); |
|
284 |
sb.append("ResponderId: ").append(respId).append("\n"); |
|
285 |
sb.append("----------------------------------------------"); |
|
286 |
||
287 |
return sb.toString(); |
|
288 |
} |
|
289 |
||
290 |
/** |
|
291 |
* Helpful debug routine to hex dump byte arrays. |
|
292 |
* |
|
293 |
* @param data the array of bytes to dump to stdout. |
|
294 |
* |
|
295 |
* @return the hexdump of the byte array |
|
296 |
*/ |
|
297 |
private static String dumpHexBytes(byte[] data) { |
|
298 |
return dumpHexBytes(data, 16, "\n", " "); |
|
299 |
} |
|
300 |
||
301 |
/** |
|
302 |
* |
|
303 |
* @param data the array of bytes to dump to stdout. |
|
304 |
* @param itemsPerLine the number of bytes to display per line |
|
305 |
* if the {@code lineDelim} character is blank then all bytes will be |
|
306 |
* printed on a single line. |
|
307 |
* @param lineDelim the delimiter between lines |
|
308 |
* @param itemDelim the delimiter between bytes |
|
309 |
* |
|
310 |
* @return The hexdump of the byte array |
|
311 |
*/ |
|
312 |
private static String dumpHexBytes(byte[] data, int itemsPerLine, |
|
313 |
String lineDelim, String itemDelim) { |
|
314 |
StringBuilder sb = new StringBuilder(); |
|
315 |
if (data != null) { |
|
316 |
for (int i = 0; i < data.length; i++) { |
|
317 |
if (i % itemsPerLine == 0 && i != 0) { |
|
318 |
sb.append(lineDelim); |
|
319 |
} |
|
320 |
sb.append(String.format("%02X", data[i])).append(itemDelim); |
|
321 |
} |
|
322 |
} |
|
323 |
||
324 |
return sb.toString(); |
|
325 |
} |
|
326 |
||
327 |
/** |
|
328 |
* Enable or disable the logging feature. |
|
329 |
* |
|
330 |
* @param enable {@code true} to enable logging, {@code false} to |
|
331 |
* disable it. The setting must be activated before the server calls |
|
332 |
* its start method. Any calls after that have no effect. |
|
333 |
*/ |
|
334 |
public void enableLog(boolean enable) { |
|
335 |
if (!started) { |
|
336 |
logEnabled = enable; |
|
337 |
} |
|
338 |
} |
|
339 |
||
340 |
/** |
|
341 |
* Sets the nextUpdate interval. Intervals will be calculated relative |
|
342 |
* to the server startup time. When first set, the nextUpdate date is |
|
343 |
* calculated based on the current time plus the interval. After that, |
|
344 |
* calls to getNextUpdate() will return this date if it is still |
|
345 |
* later than current time. If not, the Date will be updated to the |
|
346 |
* next interval that is later than current time. This value must be set |
|
347 |
* before the server has had its start method called. Calls made after |
|
348 |
* the server has been started have no effect. |
|
349 |
* |
|
350 |
* @param interval the recurring time interval in seconds used to |
|
351 |
* calculate nextUpdate times. A value less than or equal to 0 will |
|
352 |
* disable the nextUpdate feature. |
|
353 |
*/ |
|
354 |
public synchronized void setNextUpdateInterval(long interval) { |
|
355 |
if (!started) { |
|
356 |
if (interval <= 0) { |
|
357 |
nextUpdateInterval = -1; |
|
358 |
nextUpdate = null; |
|
359 |
log("nexUpdate support has been disabled"); |
|
360 |
} else { |
|
361 |
nextUpdateInterval = interval * 1000; |
|
362 |
nextUpdate = new Date(System.currentTimeMillis() + |
|
363 |
nextUpdateInterval); |
|
364 |
log("nextUpdate set to " + nextUpdate); |
|
365 |
} |
|
366 |
} |
|
367 |
} |
|
368 |
||
369 |
/** |
|
370 |
* Return the nextUpdate {@code Date} object for this server. If the |
|
371 |
* nextUpdate date has already passed, set a new nextUpdate based on |
|
372 |
* the nextUpdate interval and return that date. |
|
373 |
* |
|
374 |
* @return a {@code Date} object set to the nextUpdate field for OCSP |
|
375 |
* responses. |
|
376 |
*/ |
|
377 |
private synchronized Date getNextUpdate() { |
|
378 |
if (nextUpdate != null && nextUpdate.before(new Date())) { |
|
379 |
long nuEpochTime = nextUpdate.getTime(); |
|
380 |
long currentTime = System.currentTimeMillis(); |
|
381 |
||
382 |
// Keep adding nextUpdate intervals until you reach a date |
|
383 |
// that is later than current time. |
|
384 |
while (currentTime >= nuEpochTime) { |
|
385 |
nuEpochTime += nextUpdateInterval; |
|
386 |
} |
|
387 |
||
388 |
// Set the nextUpdate for future threads |
|
389 |
nextUpdate = new Date(nuEpochTime); |
|
390 |
log("nextUpdate updated to new value: " + nextUpdate); |
|
391 |
} |
|
392 |
return nextUpdate; |
|
393 |
} |
|
394 |
||
395 |
/** |
|
396 |
* Add entries into the responder's status database. |
|
397 |
* |
|
398 |
* @param newEntries a map of {@code CertStatusInfo} objects, keyed on |
|
399 |
* their serial number (as a {@code BigInteger}). All serial numbers |
|
400 |
* are assumed to have come from this responder's issuer certificate. |
|
401 |
* |
|
402 |
* @throws IOException if a CertId cannot be generated. |
|
403 |
*/ |
|
404 |
public void updateStatusDb(Map<BigInteger, CertStatusInfo> newEntries) |
|
405 |
throws IOException { |
|
406 |
if (newEntries != null) { |
|
407 |
for (BigInteger serial : newEntries.keySet()) { |
|
408 |
CertStatusInfo info = newEntries.get(serial); |
|
409 |
if (info != null) { |
|
410 |
CertId cid = new CertId(issuerCert, |
|
411 |
new SerialNumber(serial)); |
|
412 |
statusDb.put(cid, info); |
|
413 |
log("Added entry for serial " + serial + "(" + |
|
414 |
info.getType() + ")"); |
|
415 |
} |
|
416 |
} |
|
417 |
} |
|
418 |
} |
|
419 |
||
420 |
/** |
|
421 |
* Check the status database for revocation information one one or more |
|
422 |
* certificates. |
|
423 |
* |
|
424 |
* @param reqList the list of {@code LocalSingleRequest} objects taken |
|
425 |
* from the incoming OCSP request. |
|
426 |
* |
|
427 |
* @return a {@code Map} of {@code CertStatusInfo} objects keyed by their |
|
428 |
* {@code CertId} values, for each single request passed in. Those |
|
429 |
* CertIds not found in the statusDb will have returned List members with |
|
430 |
* a status of UNKNOWN. |
|
431 |
*/ |
|
432 |
private Map<CertId, CertStatusInfo> checkStatusDb( |
|
433 |
List<LocalOcspRequest.LocalSingleRequest> reqList) { |
|
434 |
// TODO figure out what, if anything to do with request extensions |
|
435 |
Map<CertId, CertStatusInfo> returnMap = new HashMap<>(); |
|
436 |
||
437 |
for (LocalOcspRequest.LocalSingleRequest req : reqList) { |
|
438 |
CertId cid = req.getCertId(); |
|
439 |
CertStatusInfo info = statusDb.get(cid); |
|
440 |
if (info != null) { |
|
441 |
log("Status for SN " + cid.getSerialNumber() + ": " + |
|
442 |
info.getType()); |
|
443 |
returnMap.put(cid, info); |
|
444 |
} else { |
|
445 |
log("Status for SN " + cid.getSerialNumber() + |
|
446 |
" not found, using CERT_STATUS_UNKNOWN"); |
|
447 |
returnMap.put(cid, |
|
448 |
new CertStatusInfo(CertStatus.CERT_STATUS_UNKNOWN)); |
|
449 |
} |
|
450 |
} |
|
451 |
||
452 |
return Collections.unmodifiableMap(returnMap); |
|
453 |
} |
|
454 |
||
455 |
/** |
|
456 |
* Set the digital signature algorithm used to sign OCSP responses. |
|
457 |
* |
|
458 |
* @param algName The algorithm name |
|
459 |
* |
|
460 |
* @throws NoSuchAlgorithmException if the algorithm name is invalid. |
|
461 |
*/ |
|
462 |
public void setSignatureAlgorithm(String algName) |
|
463 |
throws NoSuchAlgorithmException { |
|
464 |
if (!started) { |
|
465 |
sigAlgId = AlgorithmId.get(algName); |
|
466 |
} |
|
467 |
} |
|
468 |
||
469 |
/** |
|
470 |
* Get the port the OCSP server is running on. |
|
471 |
* |
|
472 |
* @return the port that the OCSP server is running on, or -1 if the |
|
473 |
* server has not yet been bound to a port. |
|
474 |
*/ |
|
475 |
public int getPort() { |
|
37309
8f530b9d18f4
8153829: javax/net/ssl/Stapling/HttpsUrlConnClient.java fails intermittently with NullPointerException
rhalade
parents:
32032
diff
changeset
|
476 |
if (serverReady) { |
32032 | 477 |
InetSocketAddress inetSock = |
478 |
(InetSocketAddress)servSocket.getLocalSocketAddress(); |
|
479 |
return inetSock.getPort(); |
|
480 |
} else { |
|
481 |
return -1; |
|
482 |
} |
|
483 |
} |
|
484 |
||
485 |
/** |
|
37309
8f530b9d18f4
8153829: javax/net/ssl/Stapling/HttpsUrlConnClient.java fails intermittently with NullPointerException
rhalade
parents:
32032
diff
changeset
|
486 |
* Use to check if OCSP server is ready to accept connection. |
8f530b9d18f4
8153829: javax/net/ssl/Stapling/HttpsUrlConnClient.java fails intermittently with NullPointerException
rhalade
parents:
32032
diff
changeset
|
487 |
* |
8f530b9d18f4
8153829: javax/net/ssl/Stapling/HttpsUrlConnClient.java fails intermittently with NullPointerException
rhalade
parents:
32032
diff
changeset
|
488 |
* @return true if server ready, false otherwise |
8f530b9d18f4
8153829: javax/net/ssl/Stapling/HttpsUrlConnClient.java fails intermittently with NullPointerException
rhalade
parents:
32032
diff
changeset
|
489 |
*/ |
8f530b9d18f4
8153829: javax/net/ssl/Stapling/HttpsUrlConnClient.java fails intermittently with NullPointerException
rhalade
parents:
32032
diff
changeset
|
490 |
public boolean isServerReady() { |
8f530b9d18f4
8153829: javax/net/ssl/Stapling/HttpsUrlConnClient.java fails intermittently with NullPointerException
rhalade
parents:
32032
diff
changeset
|
491 |
return serverReady; |
8f530b9d18f4
8153829: javax/net/ssl/Stapling/HttpsUrlConnClient.java fails intermittently with NullPointerException
rhalade
parents:
32032
diff
changeset
|
492 |
} |
8f530b9d18f4
8153829: javax/net/ssl/Stapling/HttpsUrlConnClient.java fails intermittently with NullPointerException
rhalade
parents:
32032
diff
changeset
|
493 |
|
8f530b9d18f4
8153829: javax/net/ssl/Stapling/HttpsUrlConnClient.java fails intermittently with NullPointerException
rhalade
parents:
32032
diff
changeset
|
494 |
/** |
32032 | 495 |
* Set a delay between the reception of the request and production of |
496 |
* the response. |
|
497 |
* |
|
498 |
* @param delayMillis the number of milliseconds to wait before acting |
|
499 |
* on the incoming request. |
|
500 |
*/ |
|
501 |
public void setDelay(long delayMillis) { |
|
502 |
if (!started) { |
|
503 |
delayMsec = delayMillis > 0 ? delayMillis : 0; |
|
504 |
if (delayMsec > 0) { |
|
505 |
log("OCSP latency set to " + delayMsec + " milliseconds."); |
|
506 |
} else { |
|
507 |
log("OCSP latency disabled"); |
|
508 |
} |
|
509 |
} |
|
510 |
} |
|
511 |
||
512 |
/** |
|
513 |
* Log a message to stdout. |
|
514 |
* |
|
515 |
* @param message the message to log |
|
516 |
*/ |
|
517 |
private synchronized void log(String message) { |
|
518 |
if (logEnabled || debug != null) { |
|
519 |
System.out.println("[" + Thread.currentThread().getName() + "]: " + |
|
520 |
message); |
|
521 |
} |
|
522 |
} |
|
523 |
||
524 |
/** |
|
525 |
* Log an error message on the stderr stream. |
|
526 |
* |
|
527 |
* @param message the message to log |
|
528 |
*/ |
|
529 |
private static synchronized void err(String message) { |
|
530 |
System.out.println("[" + Thread.currentThread().getName() + "]: " + |
|
531 |
message); |
|
532 |
} |
|
533 |
||
534 |
/** |
|
535 |
* Log exception information on the stderr stream. |
|
536 |
* |
|
537 |
* @param exc the exception to dump information about |
|
538 |
*/ |
|
539 |
private static synchronized void err(Throwable exc) { |
|
540 |
System.out.print("[" + Thread.currentThread().getName() + |
|
541 |
"]: Exception: "); |
|
542 |
exc.printStackTrace(System.out); |
|
543 |
} |
|
544 |
||
545 |
/** |
|
546 |
* The {@code CertStatusInfo} class defines an object used to return |
|
547 |
* information from the internal status database. The data in this |
|
548 |
* object may be used to construct OCSP responses. |
|
549 |
*/ |
|
550 |
public static class CertStatusInfo { |
|
551 |
private CertStatus certStatusType; |
|
552 |
private CRLReason reason; |
|
553 |
private Date revocationTime; |
|
554 |
||
555 |
/** |
|
556 |
* Create a Certificate status object by providing the status only. |
|
557 |
* If the status is {@code REVOKED} then current time is assumed |
|
558 |
* for the revocation time. |
|
559 |
* |
|
560 |
* @param statType the status for this entry. |
|
561 |
*/ |
|
562 |
public CertStatusInfo(CertStatus statType) { |
|
563 |
this(statType, null, null); |
|
564 |
} |
|
565 |
||
566 |
/** |
|
567 |
* Create a CertStatusInfo providing both type and revocation date |
|
568 |
* (if applicable). |
|
569 |
* |
|
570 |
* @param statType the status for this entry. |
|
571 |
* @param revDate if applicable, the date that revocation took place. |
|
572 |
* A value of {@code null} indicates that current time should be used. |
|
573 |
* If the value of {@code statType} is not {@code CERT_STATUS_REVOKED}, |
|
574 |
* then the {@code revDate} parameter is ignored. |
|
575 |
*/ |
|
576 |
public CertStatusInfo(CertStatus statType, Date revDate) { |
|
577 |
this(statType, revDate, null); |
|
578 |
} |
|
579 |
||
580 |
/** |
|
581 |
* Create a CertStatusInfo providing type, revocation date |
|
582 |
* (if applicable) and revocation reason. |
|
583 |
* |
|
584 |
* @param statType the status for this entry. |
|
585 |
* @param revDate if applicable, the date that revocation took place. |
|
586 |
* A value of {@code null} indicates that current time should be used. |
|
587 |
* If the value of {@code statType} is not {@code CERT_STATUS_REVOKED}, |
|
588 |
* then the {@code revDate} parameter is ignored. |
|
589 |
* @param revReason the reason the certificate was revoked. A value of |
|
590 |
* {@code null} means that no reason was provided. |
|
591 |
*/ |
|
592 |
public CertStatusInfo(CertStatus statType, Date revDate, |
|
593 |
CRLReason revReason) { |
|
594 |
Objects.requireNonNull(statType, "Cert Status must be non-null"); |
|
595 |
certStatusType = statType; |
|
596 |
switch (statType) { |
|
597 |
case CERT_STATUS_GOOD: |
|
598 |
case CERT_STATUS_UNKNOWN: |
|
599 |
revocationTime = null; |
|
600 |
break; |
|
601 |
case CERT_STATUS_REVOKED: |
|
602 |
revocationTime = revDate != null ? (Date)revDate.clone() : |
|
603 |
new Date(); |
|
604 |
break; |
|
605 |
default: |
|
606 |
throw new IllegalArgumentException("Unknown status type: " + |
|
607 |
statType); |
|
608 |
} |
|
609 |
} |
|
610 |
||
611 |
/** |
|
612 |
* Get the cert status type |
|
613 |
* |
|
614 |
* @return the status applied to this object (e.g. |
|
615 |
* {@code CERT_STATUS_GOOD}, {@code CERT_STATUS_UNKNOWN}, etc.) |
|
616 |
*/ |
|
617 |
public CertStatus getType() { |
|
618 |
return certStatusType; |
|
619 |
} |
|
620 |
||
621 |
/** |
|
622 |
* Get the revocation time (if applicable). |
|
623 |
* |
|
624 |
* @return the revocation time as a {@code Date} object, or |
|
625 |
* {@code null} if not applicable (i.e. if the certificate hasn't been |
|
626 |
* revoked). |
|
627 |
*/ |
|
628 |
public Date getRevocationTime() { |
|
629 |
return (revocationTime != null ? (Date)revocationTime.clone() : |
|
630 |
null); |
|
631 |
} |
|
632 |
||
633 |
/** |
|
634 |
* Get the revocation reason. |
|
635 |
* |
|
636 |
* @return the revocation reason, or {@code null} if one was not |
|
637 |
* provided. |
|
638 |
*/ |
|
639 |
public CRLReason getRevocationReason() { |
|
640 |
return reason; |
|
641 |
} |
|
642 |
} |
|
643 |
||
644 |
/** |
|
645 |
* Runnable task that handles incoming OCSP Requests and returns |
|
646 |
* responses. |
|
647 |
*/ |
|
648 |
private class OcspHandler implements Runnable { |
|
649 |
private final Socket sock; |
|
650 |
InetSocketAddress peerSockAddr; |
|
651 |
||
652 |
/** |
|
653 |
* Construct an {@code OcspHandler}. |
|
654 |
* |
|
655 |
* @param incomingSocket the socket the server created on accept() |
|
656 |
*/ |
|
657 |
private OcspHandler(Socket incomingSocket) { |
|
658 |
sock = incomingSocket; |
|
659 |
} |
|
660 |
||
661 |
/** |
|
662 |
* Run the OCSP Request parser and construct a response to be sent |
|
663 |
* back to the client. |
|
664 |
*/ |
|
665 |
@Override |
|
666 |
public void run() { |
|
667 |
// If we have implemented a delay to simulate network latency |
|
668 |
// wait out the delay here before any other processing. |
|
669 |
try { |
|
670 |
if (delayMsec > 0) { |
|
671 |
Thread.sleep(delayMsec); |
|
672 |
} |
|
673 |
} catch (InterruptedException ie) { |
|
674 |
// Just log the interrupted sleep |
|
675 |
log("Delay of " + delayMsec + " milliseconds was interrupted"); |
|
676 |
} |
|
677 |
||
678 |
try (Socket ocspSocket = sock; |
|
679 |
InputStream in = ocspSocket.getInputStream(); |
|
680 |
OutputStream out = ocspSocket.getOutputStream()) { |
|
681 |
peerSockAddr = |
|
682 |
(InetSocketAddress)ocspSocket.getRemoteSocketAddress(); |
|
683 |
log("Received incoming connection from " + peerSockAddr); |
|
684 |
String[] headerTokens = readLine(in).split(" "); |
|
685 |
LocalOcspRequest ocspReq = null; |
|
686 |
LocalOcspResponse ocspResp = null; |
|
687 |
ResponseStatus respStat = ResponseStatus.INTERNAL_ERROR; |
|
688 |
try { |
|
689 |
if (headerTokens[0] != null) { |
|
690 |
switch (headerTokens[0]) { |
|
691 |
case "POST": |
|
692 |
ocspReq = parseHttpOcspPost(in); |
|
693 |
break; |
|
694 |
case "GET": |
|
695 |
// req = parseHttpOcspGet(in); |
|
696 |
// TODO implement the GET parsing |
|
697 |
throw new IOException("GET method unsupported"); |
|
698 |
default: |
|
699 |
respStat = ResponseStatus.MALFORMED_REQUEST; |
|
700 |
throw new IOException("Not a GET or POST"); |
|
701 |
} |
|
702 |
} else { |
|
703 |
respStat = ResponseStatus.MALFORMED_REQUEST; |
|
704 |
throw new IOException("Unable to get HTTP method"); |
|
705 |
} |
|
706 |
||
707 |
if (ocspReq != null) { |
|
708 |
log(ocspReq.toString()); |
|
709 |
// Get responses for all CertIds in the request |
|
710 |
Map<CertId, CertStatusInfo> statusMap = |
|
711 |
checkStatusDb(ocspReq.getRequests()); |
|
712 |
if (statusMap.isEmpty()) { |
|
713 |
respStat = ResponseStatus.UNAUTHORIZED; |
|
714 |
} else { |
|
715 |
ocspResp = new LocalOcspResponse( |
|
716 |
ResponseStatus.SUCCESSFUL, statusMap, |
|
717 |
ocspReq.getExtensions()); |
|
718 |
} |
|
719 |
} else { |
|
720 |
respStat = ResponseStatus.MALFORMED_REQUEST; |
|
721 |
throw new IOException("Found null request"); |
|
722 |
} |
|
723 |
} catch (IOException | RuntimeException exc) { |
|
724 |
err(exc); |
|
725 |
} |
|
726 |
if (ocspResp == null) { |
|
727 |
ocspResp = new LocalOcspResponse(respStat); |
|
728 |
} |
|
729 |
sendResponse(out, ocspResp); |
|
730 |
} catch (IOException | CertificateException exc) { |
|
731 |
err(exc); |
|
732 |
} |
|
733 |
} |
|
734 |
||
735 |
/** |
|
736 |
* Send an OCSP response on an {@code OutputStream}. |
|
737 |
* |
|
738 |
* @param out the {@code OutputStream} on which to send the response. |
|
739 |
* @param resp the OCSP response to send. |
|
740 |
* |
|
741 |
* @throws IOException if an encoding error occurs. |
|
742 |
*/ |
|
743 |
public void sendResponse(OutputStream out, LocalOcspResponse resp) |
|
744 |
throws IOException { |
|
745 |
StringBuilder sb = new StringBuilder(); |
|
746 |
||
747 |
byte[] respBytes; |
|
748 |
try { |
|
749 |
respBytes = resp.getBytes(); |
|
750 |
} catch (RuntimeException re) { |
|
751 |
err(re); |
|
752 |
return; |
|
753 |
} |
|
754 |
||
755 |
sb.append("HTTP/1.0 200 OK\r\n"); |
|
756 |
sb.append("Content-Type: application/ocsp-response\r\n"); |
|
757 |
sb.append("Content-Length: ").append(respBytes.length); |
|
758 |
sb.append("\r\n\r\n"); |
|
759 |
||
760 |
out.write(sb.toString().getBytes("UTF-8")); |
|
761 |
out.write(respBytes); |
|
762 |
log(resp.toString()); |
|
763 |
} |
|
764 |
||
765 |
/** |
|
766 |
* Parse the incoming HTTP POST of an OCSP Request. |
|
767 |
* |
|
768 |
* @param inStream the input stream from the socket bound to this |
|
769 |
* {@code OcspHandler}. |
|
770 |
* |
|
771 |
* @return the OCSP Request as a {@code LocalOcspRequest} |
|
772 |
* |
|
773 |
* @throws IOException if there are network related issues or problems |
|
774 |
* occur during parsing of the OCSP request. |
|
775 |
* @throws CertificateException if one or more of the certificates in |
|
776 |
* the OCSP request cannot be read/parsed. |
|
777 |
*/ |
|
778 |
private LocalOcspRequest parseHttpOcspPost(InputStream inStream) |
|
779 |
throws IOException, CertificateException { |
|
780 |
boolean endOfHeader = false; |
|
781 |
boolean properContentType = false; |
|
782 |
int length = -1; |
|
783 |
||
784 |
while (!endOfHeader) { |
|
785 |
String[] lineTokens = readLine(inStream).split(" "); |
|
786 |
if (lineTokens[0].isEmpty()) { |
|
787 |
endOfHeader = true; |
|
788 |
} else if (lineTokens[0].equalsIgnoreCase("Content-Type:")) { |
|
789 |
if (lineTokens[1] == null || |
|
790 |
!lineTokens[1].equals( |
|
791 |
"application/ocsp-request")) { |
|
792 |
log("Unknown Content-Type: " + |
|
793 |
(lineTokens[1] != null ? |
|
794 |
lineTokens[1] : "<NULL>")); |
|
795 |
return null; |
|
796 |
} else { |
|
797 |
properContentType = true; |
|
798 |
log("Content-Type = " + lineTokens[1]); |
|
799 |
} |
|
800 |
} else if (lineTokens[0].equalsIgnoreCase("Content-Length:")) { |
|
801 |
if (lineTokens[1] != null) { |
|
802 |
length = Integer.parseInt(lineTokens[1]); |
|
803 |
log("Content-Length = " + length); |
|
804 |
} |
|
805 |
} |
|
806 |
} |
|
807 |
||
808 |
// Okay, make sure we got what we needed from the header, then |
|
809 |
// read the remaining OCSP Request bytes |
|
810 |
if (properContentType && length >= 0) { |
|
811 |
byte[] ocspBytes = new byte[length]; |
|
812 |
inStream.read(ocspBytes); |
|
813 |
return new LocalOcspRequest(ocspBytes); |
|
814 |
} else { |
|
815 |
return null; |
|
816 |
} |
|
817 |
} |
|
818 |
||
819 |
/** |
|
820 |
* Read a line of text that is CRLF-delimited. |
|
821 |
* |
|
822 |
* @param is the {@code InputStream} tied to the socket |
|
823 |
* for this {@code OcspHandler} |
|
824 |
* |
|
825 |
* @return a {@code String} consisting of the line of text |
|
826 |
* read from the stream with the CRLF stripped. |
|
827 |
* |
|
828 |
* @throws IOException if any I/O error occurs. |
|
829 |
*/ |
|
830 |
private String readLine(InputStream is) throws IOException { |
|
831 |
PushbackInputStream pbis = new PushbackInputStream(is); |
|
832 |
ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
|
833 |
boolean done = false; |
|
834 |
while (!done) { |
|
835 |
byte b = (byte)pbis.read(); |
|
836 |
if (b == '\r') { |
|
837 |
byte bNext = (byte)pbis.read(); |
|
838 |
if (bNext == '\n' || bNext == -1) { |
|
839 |
done = true; |
|
840 |
} else { |
|
841 |
pbis.unread(bNext); |
|
842 |
bos.write(b); |
|
843 |
} |
|
844 |
} else if (b == -1) { |
|
845 |
done = true; |
|
846 |
} else { |
|
847 |
bos.write(b); |
|
848 |
} |
|
849 |
} |
|
850 |
||
851 |
return new String(bos.toByteArray(), "UTF-8"); |
|
852 |
} |
|
853 |
} |
|
854 |
||
855 |
||
856 |
/** |
|
857 |
* Simple nested class to handle OCSP requests without making |
|
858 |
* changes to sun.security.provider.certpath.OCSPRequest |
|
859 |
*/ |
|
860 |
public class LocalOcspRequest { |
|
861 |
||
862 |
private byte[] nonce; |
|
863 |
private byte[] signature = null; |
|
864 |
private AlgorithmId algId = null; |
|
865 |
private int version = 0; |
|
866 |
private GeneralName requestorName = null; |
|
867 |
private Map<String, Extension> extensions = Collections.emptyMap(); |
|
868 |
private final List<LocalSingleRequest> requestList = new ArrayList<>(); |
|
869 |
private final List<X509Certificate> certificates = new ArrayList<>(); |
|
870 |
||
871 |
/** |
|
872 |
* Construct a {@code LocalOcspRequest} from its DER encoding. |
|
873 |
* |
|
874 |
* @param requestBytes the DER-encoded bytes |
|
875 |
* |
|
876 |
* @throws IOException if decoding errors occur |
|
877 |
* @throws CertificateException if certificates are found in the |
|
878 |
* OCSP request and they do not parse correctly. |
|
879 |
*/ |
|
880 |
private LocalOcspRequest(byte[] requestBytes) throws IOException, |
|
881 |
CertificateException { |
|
882 |
Objects.requireNonNull(requestBytes, "Received null input"); |
|
883 |
||
884 |
DerInputStream dis = new DerInputStream(requestBytes); |
|
885 |
||
886 |
// Parse the top-level structure, it should have no more than |
|
887 |
// two elements. |
|
888 |
DerValue[] topStructs = dis.getSequence(2); |
|
889 |
for (DerValue dv : topStructs) { |
|
890 |
if (dv.tag == DerValue.tag_Sequence) { |
|
891 |
parseTbsRequest(dv); |
|
892 |
} else if (dv.isContextSpecific((byte)0)) { |
|
893 |
parseSignature(dv); |
|
894 |
} else { |
|
895 |
throw new IOException("Unknown tag at top level: " + |
|
896 |
dv.tag); |
|
897 |
} |
|
898 |
} |
|
899 |
} |
|
900 |
||
901 |
/** |
|
902 |
* Parse the signature block from an OCSP request |
|
903 |
* |
|
904 |
* @param sigSequence a {@code DerValue} containing the signature |
|
905 |
* block at the outer sequence datum. |
|
906 |
* |
|
907 |
* @throws IOException if any non-certificate-based parsing errors occur |
|
908 |
* @throws CertificateException if certificates are found in the |
|
909 |
* OCSP request and they do not parse correctly. |
|
910 |
*/ |
|
911 |
private void parseSignature(DerValue sigSequence) |
|
912 |
throws IOException, CertificateException { |
|
913 |
DerValue[] sigItems = sigSequence.data.getSequence(3); |
|
914 |
if (sigItems.length != 3) { |
|
915 |
throw new IOException("Invalid number of signature items: " + |
|
916 |
"expected 3, got " + sigItems.length); |
|
917 |
} |
|
918 |
||
919 |
algId = AlgorithmId.parse(sigItems[0]); |
|
920 |
signature = sigItems[1].getBitString(); |
|
921 |
||
922 |
if (sigItems[2].isContextSpecific((byte)0)) { |
|
923 |
DerValue[] certDerItems = sigItems[2].data.getSequence(4); |
|
924 |
int i = 0; |
|
925 |
for (DerValue dv : certDerItems) { |
|
926 |
X509Certificate xc = new X509CertImpl(dv); |
|
927 |
certificates.add(xc); |
|
928 |
} |
|
929 |
} else { |
|
930 |
throw new IOException("Invalid tag in signature block: " + |
|
931 |
sigItems[2].tag); |
|
932 |
} |
|
933 |
} |
|
934 |
||
935 |
/** |
|
936 |
* Parse the to-be-signed request data |
|
937 |
* |
|
938 |
* @param tbsReqSeq a {@code DerValue} object containing the to-be- |
|
939 |
* signed OCSP request at the outermost SEQUENCE tag. |
|
940 |
* @throws IOException if any parsing errors occur |
|
941 |
*/ |
|
942 |
private void parseTbsRequest(DerValue tbsReqSeq) throws IOException { |
|
943 |
while (tbsReqSeq.data.available() > 0) { |
|
944 |
DerValue dv = tbsReqSeq.data.getDerValue(); |
|
945 |
if (dv.isContextSpecific((byte)0)) { |
|
946 |
// The version was explicitly called out |
|
947 |
version = dv.data.getInteger(); |
|
948 |
} else if (dv.isContextSpecific((byte)1)) { |
|
949 |
// A GeneralName was provided |
|
950 |
requestorName = new GeneralName(dv.data.getDerValue()); |
|
951 |
} else if (dv.isContextSpecific((byte)2)) { |
|
952 |
// Parse the extensions |
|
953 |
DerValue[] extItems = dv.data.getSequence(2); |
|
954 |
extensions = parseExtensions(extItems); |
|
955 |
} else if (dv.tag == DerValue.tag_Sequence) { |
|
956 |
while (dv.data.available() > 0) { |
|
957 |
requestList.add(new LocalSingleRequest(dv.data)); |
|
958 |
} |
|
959 |
} |
|
960 |
} |
|
961 |
} |
|
962 |
||
963 |
/** |
|
964 |
* Parse a SEQUENCE of extensions. This routine is used both |
|
965 |
* at the overall request level and down at the singleRequest layer. |
|
966 |
* |
|
967 |
* @param extDerItems an array of {@code DerValue} items, each one |
|
968 |
* consisting of a DER-encoded extension. |
|
969 |
* |
|
970 |
* @return a {@code Map} of zero or more extensions, |
|
971 |
* keyed by its object identifier in {@code String} form. |
|
972 |
* |
|
973 |
* @throws IOException if any parsing errors occur. |
|
974 |
*/ |
|
975 |
private Map<String, Extension> parseExtensions(DerValue[] extDerItems) |
|
976 |
throws IOException { |
|
977 |
Map<String, Extension> extMap = new HashMap<>(); |
|
978 |
||
979 |
if (extDerItems != null && extDerItems.length != 0) { |
|
980 |
for (DerValue extDerVal : extDerItems) { |
|
981 |
sun.security.x509.Extension ext = |
|
982 |
new sun.security.x509.Extension(extDerVal); |
|
983 |
extMap.put(ext.getId(), ext); |
|
984 |
} |
|
985 |
} |
|
986 |
||
987 |
return extMap; |
|
988 |
} |
|
989 |
||
990 |
/** |
|
991 |
* Return the list of single request objects in this OCSP request. |
|
992 |
* |
|
993 |
* @return an unmodifiable {@code List} of zero or more requests. |
|
994 |
*/ |
|
995 |
private List<LocalSingleRequest> getRequests() { |
|
996 |
return Collections.unmodifiableList(requestList); |
|
997 |
} |
|
998 |
||
999 |
/** |
|
1000 |
* Return the list of X.509 Certificates in this OCSP request. |
|
1001 |
* |
|
1002 |
* @return an unmodifiable {@code List} of zero or more |
|
1003 |
* {@cpde X509Certificate} objects. |
|
1004 |
*/ |
|
1005 |
private List<X509Certificate> getCertificates() { |
|
1006 |
return Collections.unmodifiableList(certificates); |
|
1007 |
} |
|
1008 |
||
1009 |
/** |
|
1010 |
* Return the map of OCSP request extensions. |
|
1011 |
* |
|
1012 |
* @return an unmodifiable {@code Map} of zero or more |
|
1013 |
* {@code Extension} objects, keyed by their object identifiers |
|
1014 |
* in {@code String} form. |
|
1015 |
*/ |
|
1016 |
private Map<String, Extension> getExtensions() { |
|
1017 |
return Collections.unmodifiableMap(extensions); |
|
1018 |
} |
|
1019 |
||
1020 |
/** |
|
1021 |
* Display the {@code LocalOcspRequest} in human readable form. |
|
1022 |
* |
|
1023 |
* @return a {@code String} representation of the |
|
1024 |
* {@code LocalOcspRequest} |
|
1025 |
*/ |
|
1026 |
@Override |
|
1027 |
public String toString() { |
|
1028 |
StringBuilder sb = new StringBuilder(); |
|
1029 |
||
1030 |
sb.append(String.format("OCSP Request: Version %d (0x%X)", |
|
1031 |
version + 1, version)).append("\n"); |
|
1032 |
if (requestorName != null) { |
|
1033 |
sb.append("Requestor Name: ").append(requestorName). |
|
1034 |
append("\n"); |
|
1035 |
} |
|
1036 |
||
1037 |
int requestCtr = 0; |
|
1038 |
for (LocalSingleRequest lsr : requestList) { |
|
1039 |
sb.append("Request [").append(requestCtr++).append("]\n"); |
|
1040 |
sb.append(lsr).append("\n"); |
|
1041 |
} |
|
1042 |
if (!extensions.isEmpty()) { |
|
1043 |
sb.append("Extensions (").append(extensions.size()). |
|
1044 |
append(")\n"); |
|
1045 |
for (Extension ext : extensions.values()) { |
|
1046 |
sb.append("\t").append(ext).append("\n"); |
|
1047 |
} |
|
1048 |
} |
|
1049 |
if (signature != null) { |
|
1050 |
sb.append("Signature: ").append(algId).append("\n"); |
|
1051 |
sb.append(dumpHexBytes(signature)).append("\n"); |
|
1052 |
int certCtr = 0; |
|
1053 |
for (X509Certificate cert : certificates) { |
|
1054 |
sb.append("Certificate [").append(certCtr++).append("]"). |
|
1055 |
append("\n"); |
|
1056 |
sb.append("\tSubject: "); |
|
1057 |
sb.append(cert.getSubjectX500Principal()).append("\n"); |
|
1058 |
sb.append("\tIssuer: "); |
|
1059 |
sb.append(cert.getIssuerX500Principal()).append("\n"); |
|
1060 |
sb.append("\tSerial: ").append(cert.getSerialNumber()); |
|
1061 |
} |
|
1062 |
} |
|
1063 |
||
1064 |
return sb.toString(); |
|
1065 |
} |
|
1066 |
||
1067 |
/** |
|
1068 |
* Inner class designed to handle the decoding/representation of |
|
1069 |
* single requests within a {@code LocalOcspRequest} object. |
|
1070 |
*/ |
|
1071 |
public class LocalSingleRequest { |
|
1072 |
private final CertId cid; |
|
1073 |
private Map<String, Extension> extensions = Collections.emptyMap(); |
|
1074 |
||
1075 |
private LocalSingleRequest(DerInputStream dis) |
|
1076 |
throws IOException { |
|
1077 |
DerValue[] srItems = dis.getSequence(2); |
|
1078 |
||
1079 |
// There should be 1, possibly 2 DerValue items |
|
1080 |
if (srItems.length == 1 || srItems.length == 2) { |
|
1081 |
// The first parsable item should be the mandatory CertId |
|
1082 |
cid = new CertId(srItems[0].data); |
|
1083 |
if (srItems.length == 2) { |
|
1084 |
if (srItems[1].isContextSpecific((byte)0)) { |
|
1085 |
DerValue[] extDerItems = srItems[1].data.getSequence(2); |
|
1086 |
extensions = parseExtensions(extDerItems); |
|
1087 |
} else { |
|
1088 |
throw new IOException("Illegal tag in Request " + |
|
1089 |
"extensions: " + srItems[1].tag); |
|
1090 |
} |
|
1091 |
} |
|
1092 |
} else { |
|
1093 |
throw new IOException("Invalid number of items in " + |
|
1094 |
"Request (" + srItems.length + ")"); |
|
1095 |
} |
|
1096 |
} |
|
1097 |
||
1098 |
/** |
|
1099 |
* Get the {@code CertId} for this single request. |
|
1100 |
* |
|
1101 |
* @return the {@code CertId} for this single request. |
|
1102 |
*/ |
|
1103 |
private CertId getCertId() { |
|
1104 |
return cid; |
|
1105 |
} |
|
1106 |
||
1107 |
/** |
|
1108 |
* Return the map of single request extensions. |
|
1109 |
* |
|
1110 |
* @return an unmodifiable {@code Map} of zero or more |
|
1111 |
* {@code Extension} objects, keyed by their object identifiers |
|
1112 |
* in {@code String} form. |
|
1113 |
*/ |
|
1114 |
private Map<String, Extension> getExtensions() { |
|
1115 |
return Collections.unmodifiableMap(extensions); |
|
1116 |
} |
|
1117 |
||
1118 |
/** |
|
1119 |
* Display the {@code LocalSingleRequest} in human readable form. |
|
1120 |
* |
|
1121 |
* @return a {@code String} representation of the |
|
1122 |
* {@code LocalSingleRequest} |
|
1123 |
*/ |
|
1124 |
@Override |
|
1125 |
public String toString() { |
|
1126 |
StringBuilder sb = new StringBuilder(); |
|
1127 |
sb.append("CertId, Algorithm = "); |
|
1128 |
sb.append(cid.getHashAlgorithm()).append("\n"); |
|
1129 |
sb.append("\tIssuer Name Hash: "); |
|
1130 |
sb.append(dumpHexBytes(cid.getIssuerNameHash(), 256, "", "")); |
|
1131 |
sb.append("\n"); |
|
1132 |
sb.append("\tIssuer Key Hash: "); |
|
1133 |
sb.append(dumpHexBytes(cid.getIssuerKeyHash(), 256, "", "")); |
|
1134 |
sb.append("\n"); |
|
1135 |
sb.append("\tSerial Number: ").append(cid.getSerialNumber()); |
|
1136 |
if (!extensions.isEmpty()) { |
|
1137 |
sb.append("Extensions (").append(extensions.size()). |
|
1138 |
append(")\n"); |
|
1139 |
for (Extension ext : extensions.values()) { |
|
1140 |
sb.append("\t").append(ext).append("\n"); |
|
1141 |
} |
|
1142 |
} |
|
1143 |
||
1144 |
return sb.toString(); |
|
1145 |
} |
|
1146 |
} |
|
1147 |
} |
|
1148 |
||
1149 |
/** |
|
1150 |
* Simple nested class to handle OCSP requests without making |
|
1151 |
* changes to sun.security.provider.certpath.OCSPResponse |
|
1152 |
*/ |
|
1153 |
public class LocalOcspResponse { |
|
1154 |
private final int version = 0; |
|
1155 |
private final OCSPResponse.ResponseStatus responseStatus; |
|
1156 |
private final Map<CertId, CertStatusInfo> respItemMap; |
|
1157 |
private final Date producedAtDate; |
|
1158 |
private final List<LocalSingleResponse> singleResponseList = |
|
1159 |
new ArrayList<>(); |
|
1160 |
private final Map<String, Extension> responseExtensions; |
|
1161 |
private byte[] signature; |
|
1162 |
private final List<X509Certificate> certificates; |
|
1163 |
private final byte[] encodedResponse; |
|
1164 |
||
1165 |
/** |
|
1166 |
* Constructor for the generation of non-successful responses |
|
1167 |
* |
|
1168 |
* @param respStat the OCSP response status. |
|
1169 |
* |
|
1170 |
* @throws IOException if an error happens during encoding |
|
1171 |
* @throws NullPointerException if {@code respStat} is {@code null} |
|
1172 |
* or {@code respStat} is successful. |
|
1173 |
*/ |
|
1174 |
public LocalOcspResponse(OCSPResponse.ResponseStatus respStat) |
|
1175 |
throws IOException { |
|
1176 |
this(respStat, null, null); |
|
1177 |
} |
|
1178 |
||
1179 |
/** |
|
1180 |
* Construct a response from a list of certificate |
|
1181 |
* status objects and extensions. |
|
1182 |
* |
|
1183 |
* @param respStat the status of the entire response |
|
1184 |
* @param itemMap a {@code Map} of {@code CertId} objects and their |
|
1185 |
* respective revocation statuses from the server's response DB. |
|
1186 |
* @param reqExtensions a {@code Map} of request extensions |
|
1187 |
* |
|
1188 |
* @throws IOException if an error happens during encoding |
|
1189 |
* @throws NullPointerException if {@code respStat} is {@code null} |
|
1190 |
* or {@code respStat} is successful, and a {@code null} {@code itemMap} |
|
1191 |
* has been provided. |
|
1192 |
*/ |
|
1193 |
public LocalOcspResponse(OCSPResponse.ResponseStatus respStat, |
|
1194 |
Map<CertId, CertStatusInfo> itemMap, |
|
1195 |
Map<String, Extension> reqExtensions) throws IOException { |
|
1196 |
responseStatus = Objects.requireNonNull(respStat, |
|
1197 |
"Illegal null response status"); |
|
1198 |
if (responseStatus == ResponseStatus.SUCCESSFUL) { |
|
1199 |
respItemMap = Objects.requireNonNull(itemMap, |
|
1200 |
"SUCCESSFUL responses must have a response map"); |
|
1201 |
producedAtDate = new Date(); |
|
1202 |
||
1203 |
// Turn the answerd from the response DB query into a list |
|
1204 |
// of single responses. |
|
1205 |
for (CertId id : itemMap.keySet()) { |
|
1206 |
singleResponseList.add( |
|
1207 |
new LocalSingleResponse(id, itemMap.get(id))); |
|
1208 |
} |
|
1209 |
||
1210 |
responseExtensions = setResponseExtensions(reqExtensions); |
|
1211 |
certificates = new ArrayList<>(); |
|
1212 |
if (signerCert != issuerCert) { |
|
1213 |
certificates.add(signerCert); |
|
1214 |
} |
|
1215 |
certificates.add(issuerCert); |
|
1216 |
} else { |
|
1217 |
respItemMap = null; |
|
1218 |
producedAtDate = null; |
|
1219 |
responseExtensions = null; |
|
1220 |
certificates = null; |
|
1221 |
} |
|
1222 |
encodedResponse = this.getBytes(); |
|
1223 |
} |
|
1224 |
||
1225 |
/** |
|
1226 |
* Set the response extensions based on the request extensions |
|
1227 |
* that were received. Right now, this is limited to the |
|
1228 |
* OCSP nonce extension. |
|
1229 |
* |
|
1230 |
* @param reqExts a {@code Map} of zero or more request extensions |
|
1231 |
* |
|
1232 |
* @return a {@code Map} of zero or more response extensions, keyed |
|
1233 |
* by the extension object identifier in {@code String} form. |
|
1234 |
*/ |
|
1235 |
private Map<String, Extension> setResponseExtensions( |
|
1236 |
Map<String, Extension> reqExts) { |
|
1237 |
Map<String, Extension> respExts = new HashMap<>(); |
|
1238 |
String ocspNonceStr = PKIXExtensions.OCSPNonce_Id.toString(); |
|
1239 |
||
1240 |
if (reqExts != null) { |
|
1241 |
for (String id : reqExts.keySet()) { |
|
1242 |
if (id.equals(ocspNonceStr)) { |
|
1243 |
// We found a nonce, add it into the response extensions |
|
1244 |
Extension ext = reqExts.get(id); |
|
1245 |
if (ext != null) { |
|
1246 |
respExts.put(id, ext); |
|
1247 |
log("Added OCSP Nonce to response"); |
|
1248 |
} else { |
|
1249 |
log("Error: Found nonce entry, but found null " + |
|
1250 |
"value. Skipping"); |
|
1251 |
} |
|
1252 |
} |
|
1253 |
} |
|
1254 |
} |
|
1255 |
||
1256 |
return respExts; |
|
1257 |
} |
|
1258 |
||
1259 |
/** |
|
1260 |
* Get the DER-encoded response bytes for this response |
|
1261 |
* |
|
1262 |
* @return a byte array containing the DER-encoded bytes for |
|
1263 |
* the response |
|
1264 |
* |
|
1265 |
* @throws IOException if any encoding errors occur |
|
1266 |
*/ |
|
1267 |
private byte[] getBytes() throws IOException { |
|
1268 |
DerOutputStream outerSeq = new DerOutputStream(); |
|
1269 |
DerOutputStream responseStream = new DerOutputStream(); |
|
1270 |
responseStream.putEnumerated(responseStatus.ordinal()); |
|
1271 |
if (responseStatus == ResponseStatus.SUCCESSFUL && |
|
1272 |
respItemMap != null) { |
|
1273 |
encodeResponseBytes(responseStream); |
|
1274 |
} |
|
1275 |
||
1276 |
// Commit the outermost sequence bytes |
|
1277 |
outerSeq.write(DerValue.tag_Sequence, responseStream); |
|
1278 |
return outerSeq.toByteArray(); |
|
1279 |
} |
|
1280 |
||
1281 |
private void encodeResponseBytes(DerOutputStream responseStream) |
|
1282 |
throws IOException { |
|
1283 |
DerOutputStream explicitZero = new DerOutputStream(); |
|
1284 |
DerOutputStream respItemStream = new DerOutputStream(); |
|
1285 |
||
1286 |
respItemStream.putOID(OCSP_BASIC_RESPONSE_OID); |
|
1287 |
||
1288 |
byte[] basicOcspBytes = encodeBasicOcspResponse(); |
|
1289 |
respItemStream.putOctetString(basicOcspBytes); |
|
1290 |
explicitZero.write(DerValue.tag_Sequence, respItemStream); |
|
1291 |
responseStream.write(DerValue.createTag(DerValue.TAG_CONTEXT, |
|
1292 |
true, (byte)0), explicitZero); |
|
1293 |
} |
|
1294 |
||
1295 |
private byte[] encodeBasicOcspResponse() throws IOException { |
|
1296 |
DerOutputStream outerSeq = new DerOutputStream(); |
|
1297 |
DerOutputStream basicORItemStream = new DerOutputStream(); |
|
1298 |
||
1299 |
// Encode the tbsResponse |
|
1300 |
byte[] tbsResponseBytes = encodeTbsResponse(); |
|
1301 |
basicORItemStream.write(tbsResponseBytes); |
|
1302 |
||
1303 |
try { |
|
1304 |
sigAlgId.derEncode(basicORItemStream); |
|
1305 |
||
1306 |
// Create the signature |
|
1307 |
Signature sig = Signature.getInstance(sigAlgId.getName()); |
|
1308 |
sig.initSign(signerKey); |
|
1309 |
sig.update(tbsResponseBytes); |
|
1310 |
signature = sig.sign(); |
|
1311 |
basicORItemStream.putBitString(signature); |
|
1312 |
} catch (GeneralSecurityException exc) { |
|
1313 |
err(exc); |
|
1314 |
throw new IOException(exc); |
|
1315 |
} |
|
1316 |
||
1317 |
// Add certificates |
|
1318 |
try { |
|
1319 |
DerOutputStream certStream = new DerOutputStream(); |
|
1320 |
ArrayList<DerValue> certList = new ArrayList<>(); |
|
1321 |
if (signerCert != issuerCert) { |
|
1322 |
certList.add(new DerValue(signerCert.getEncoded())); |
|
1323 |
} |
|
1324 |
certList.add(new DerValue(issuerCert.getEncoded())); |
|
1325 |
DerValue[] dvals = new DerValue[certList.size()]; |
|
1326 |
certStream.putSequence(certList.toArray(dvals)); |
|
1327 |
basicORItemStream.write(DerValue.createTag(DerValue.TAG_CONTEXT, |
|
1328 |
true, (byte)0), certStream); |
|
1329 |
} catch (CertificateEncodingException cex) { |
|
1330 |
err(cex); |
|
1331 |
throw new IOException(cex); |
|
1332 |
} |
|
1333 |
||
1334 |
// Commit the outermost sequence bytes |
|
1335 |
outerSeq.write(DerValue.tag_Sequence, basicORItemStream); |
|
1336 |
return outerSeq.toByteArray(); |
|
1337 |
} |
|
1338 |
||
1339 |
private byte[] encodeTbsResponse() throws IOException { |
|
1340 |
DerOutputStream outerSeq = new DerOutputStream(); |
|
1341 |
DerOutputStream tbsStream = new DerOutputStream(); |
|
1342 |
||
1343 |
// Note: We're not going explicitly assert the version |
|
1344 |
tbsStream.write(respId.getEncoded()); |
|
1345 |
tbsStream.putGeneralizedTime(producedAtDate); |
|
1346 |
||
1347 |
// Sequence of responses |
|
1348 |
encodeSingleResponses(tbsStream); |
|
1349 |
||
1350 |
// TODO: add response extension support |
|
1351 |
encodeExtensions(tbsStream); |
|
1352 |
||
1353 |
outerSeq.write(DerValue.tag_Sequence, tbsStream); |
|
1354 |
return outerSeq.toByteArray(); |
|
1355 |
} |
|
1356 |
||
1357 |
private void encodeSingleResponses(DerOutputStream tbsStream) |
|
1358 |
throws IOException { |
|
1359 |
DerValue[] srDerVals = new DerValue[singleResponseList.size()]; |
|
1360 |
int srDvCtr = 0; |
|
1361 |
||
1362 |
for (LocalSingleResponse lsr : singleResponseList) { |
|
1363 |
srDerVals[srDvCtr++] = new DerValue(lsr.getBytes()); |
|
1364 |
} |
|
1365 |
||
1366 |
tbsStream.putSequence(srDerVals); |
|
1367 |
} |
|
1368 |
||
1369 |
private void encodeExtensions(DerOutputStream tbsStream) |
|
1370 |
throws IOException { |
|
1371 |
DerOutputStream extSequence = new DerOutputStream(); |
|
1372 |
DerOutputStream extItems = new DerOutputStream(); |
|
1373 |
||
1374 |
for (Extension ext : responseExtensions.values()) { |
|
1375 |
ext.encode(extItems); |
|
1376 |
} |
|
1377 |
extSequence.write(DerValue.tag_Sequence, extItems); |
|
1378 |
tbsStream.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, |
|
1379 |
(byte)1), extSequence); |
|
1380 |
} |
|
1381 |
||
1382 |
@Override |
|
1383 |
public String toString() { |
|
1384 |
StringBuilder sb = new StringBuilder(); |
|
1385 |
||
1386 |
sb.append("OCSP Response: ").append(responseStatus).append("\n"); |
|
1387 |
if (responseStatus == ResponseStatus.SUCCESSFUL) { |
|
1388 |
sb.append("Response Type: "). |
|
1389 |
append(OCSP_BASIC_RESPONSE_OID.toString()).append("\n"); |
|
1390 |
sb.append(String.format("Version: %d (0x%X)", version + 1, |
|
1391 |
version)).append("\n"); |
|
1392 |
sb.append("Responder Id: ").append(respId.toString()). |
|
1393 |
append("\n"); |
|
1394 |
sb.append("Produced At: "). |
|
1395 |
append(utcDateFmt.format(producedAtDate)).append("\n"); |
|
1396 |
||
1397 |
int srCtr = 0; |
|
1398 |
for (LocalSingleResponse lsr : singleResponseList) { |
|
1399 |
sb.append("SingleResponse [").append(srCtr++).append("]\n"); |
|
1400 |
sb.append(lsr); |
|
1401 |
} |
|
1402 |
||
1403 |
if (!responseExtensions.isEmpty()) { |
|
1404 |
sb.append("Extensions (").append(responseExtensions.size()). |
|
1405 |
append(")\n"); |
|
1406 |
for (Extension ext : responseExtensions.values()) { |
|
1407 |
sb.append("\t").append(ext).append("\n"); |
|
1408 |
} |
|
1409 |
} else { |
|
1410 |
sb.append("\n"); |
|
1411 |
} |
|
1412 |
||
1413 |
if (signature != null) { |
|
1414 |
sb.append("Signature: ").append(sigAlgId).append("\n"); |
|
1415 |
sb.append(dumpHexBytes(signature)).append("\n"); |
|
1416 |
int certCtr = 0; |
|
1417 |
for (X509Certificate cert : certificates) { |
|
1418 |
sb.append("Certificate [").append(certCtr++).append("]"). |
|
1419 |
append("\n"); |
|
1420 |
sb.append("\tSubject: "); |
|
1421 |
sb.append(cert.getSubjectX500Principal()).append("\n"); |
|
1422 |
sb.append("\tIssuer: "); |
|
1423 |
sb.append(cert.getIssuerX500Principal()).append("\n"); |
|
1424 |
sb.append("\tSerial: ").append(cert.getSerialNumber()); |
|
1425 |
sb.append("\n"); |
|
1426 |
} |
|
1427 |
} |
|
1428 |
} |
|
1429 |
||
1430 |
return sb.toString(); |
|
1431 |
} |
|
1432 |
||
1433 |
private class LocalSingleResponse { |
|
1434 |
private final CertId certId; |
|
1435 |
private final CertStatusInfo csInfo; |
|
1436 |
private final Date thisUpdate; |
|
1437 |
private final Date lsrNextUpdate; |
|
1438 |
private final Map<String, Extension> singleExtensions; |
|
1439 |
||
1440 |
public LocalSingleResponse(CertId cid, CertStatusInfo info) { |
|
1441 |
certId = Objects.requireNonNull(cid, "CertId must be non-null"); |
|
1442 |
csInfo = Objects.requireNonNull(info, |
|
1443 |
"CertStatusInfo must be non-null"); |
|
1444 |
||
1445 |
// For now, we'll keep things simple and make the thisUpdate |
|
1446 |
// field the same as the producedAt date. |
|
1447 |
thisUpdate = producedAtDate; |
|
1448 |
lsrNextUpdate = getNextUpdate(); |
|
1449 |
||
1450 |
// TODO Add extensions support |
|
1451 |
singleExtensions = Collections.emptyMap(); |
|
1452 |
} |
|
1453 |
||
1454 |
@Override |
|
1455 |
public String toString() { |
|
1456 |
StringBuilder sb = new StringBuilder(); |
|
1457 |
sb.append("Certificate Status: ").append(csInfo.getType()); |
|
1458 |
sb.append("\n"); |
|
1459 |
if (csInfo.getType() == CertStatus.CERT_STATUS_REVOKED) { |
|
1460 |
sb.append("Revocation Time: "); |
|
1461 |
sb.append(utcDateFmt.format(csInfo.getRevocationTime())); |
|
1462 |
sb.append("\n"); |
|
1463 |
if (csInfo.getRevocationReason() != null) { |
|
1464 |
sb.append("Revocation Reason: "); |
|
1465 |
sb.append(csInfo.getRevocationReason()).append("\n"); |
|
1466 |
} |
|
1467 |
} |
|
1468 |
||
1469 |
sb.append("CertId, Algorithm = "); |
|
1470 |
sb.append(certId.getHashAlgorithm()).append("\n"); |
|
1471 |
sb.append("\tIssuer Name Hash: "); |
|
1472 |
sb.append(dumpHexBytes(certId.getIssuerNameHash(), 256, "", "")); |
|
1473 |
sb.append("\n"); |
|
1474 |
sb.append("\tIssuer Key Hash: "); |
|
1475 |
sb.append(dumpHexBytes(certId.getIssuerKeyHash(), 256, "", "")); |
|
1476 |
sb.append("\n"); |
|
1477 |
sb.append("\tSerial Number: ").append(certId.getSerialNumber()); |
|
1478 |
sb.append("\n"); |
|
1479 |
sb.append("This Update: "); |
|
1480 |
sb.append(utcDateFmt.format(thisUpdate)).append("\n"); |
|
1481 |
if (lsrNextUpdate != null) { |
|
1482 |
sb.append("Next Update: "); |
|
1483 |
sb.append(utcDateFmt.format(lsrNextUpdate)).append("\n"); |
|
1484 |
} |
|
1485 |
||
1486 |
if (!singleExtensions.isEmpty()) { |
|
1487 |
sb.append("Extensions (").append(singleExtensions.size()). |
|
1488 |
append(")\n"); |
|
1489 |
for (Extension ext : singleExtensions.values()) { |
|
1490 |
sb.append("\t").append(ext).append("\n"); |
|
1491 |
} |
|
1492 |
} |
|
1493 |
||
1494 |
return sb.toString(); |
|
1495 |
} |
|
1496 |
||
1497 |
public byte[] getBytes() throws IOException { |
|
1498 |
byte[] nullData = { }; |
|
1499 |
DerOutputStream responseSeq = new DerOutputStream(); |
|
1500 |
DerOutputStream srStream = new DerOutputStream(); |
|
1501 |
||
1502 |
// Encode the CertId |
|
1503 |
certId.encode(srStream); |
|
1504 |
||
1505 |
// Next, encode the CertStatus field |
|
1506 |
CertStatus csiType = csInfo.getType(); |
|
1507 |
switch (csiType) { |
|
1508 |
case CERT_STATUS_GOOD: |
|
1509 |
srStream.write(DerValue.createTag(DerValue.TAG_CONTEXT, |
|
1510 |
false, (byte)0), nullData); |
|
1511 |
break; |
|
1512 |
case CERT_STATUS_REVOKED: |
|
1513 |
DerOutputStream revInfo = new DerOutputStream(); |
|
1514 |
revInfo.putGeneralizedTime(csInfo.getRevocationTime()); |
|
1515 |
CRLReason revReason = csInfo.getRevocationReason(); |
|
1516 |
if (revReason != null) { |
|
1517 |
byte[] revDer = new byte[3]; |
|
1518 |
revDer[0] = DerValue.tag_Enumerated; |
|
1519 |
revDer[1] = 1; |
|
1520 |
revDer[2] = (byte)revReason.ordinal(); |
|
1521 |
revInfo.write(DerValue.createTag( |
|
1522 |
DerValue.TAG_CONTEXT, true, (byte)0), |
|
1523 |
revDer); |
|
1524 |
} |
|
1525 |
srStream.write(DerValue.createTag( |
|
1526 |
DerValue.TAG_CONTEXT, true, (byte)1), |
|
1527 |
revInfo); |
|
1528 |
break; |
|
1529 |
case CERT_STATUS_UNKNOWN: |
|
1530 |
srStream.write(DerValue.createTag(DerValue.TAG_CONTEXT, |
|
1531 |
false, (byte)2), nullData); |
|
1532 |
break; |
|
1533 |
default: |
|
1534 |
throw new IOException("Unknown CertStatus: " + csiType); |
|
1535 |
} |
|
1536 |
||
1537 |
// Add the necessary dates |
|
1538 |
srStream.putGeneralizedTime(thisUpdate); |
|
1539 |
if (lsrNextUpdate != null) { |
|
1540 |
DerOutputStream nuStream = new DerOutputStream(); |
|
1541 |
nuStream.putGeneralizedTime(lsrNextUpdate); |
|
1542 |
srStream.write(DerValue.createTag(DerValue.TAG_CONTEXT, |
|
1543 |
true, (byte)0), nuStream); |
|
1544 |
} |
|
1545 |
||
1546 |
// TODO add singleResponse Extension support |
|
1547 |
||
1548 |
// Add the single response to the response output stream |
|
1549 |
responseSeq.write(DerValue.tag_Sequence, srStream); |
|
1550 |
return responseSeq.toByteArray(); |
|
1551 |
} |
|
1552 |
} |
|
1553 |
} |
|
1554 |
} |