Skip to content

Commit f5ee356

Browse files
author
Jamil Nimeh
committed
8179503: Java should support GET OCSP calls
Reviewed-by: xuelei
1 parent 8435f0d commit f5ee356

File tree

3 files changed

+334
-55
lines changed

3 files changed

+334
-55
lines changed

src/java.base/share/classes/sun/security/provider/certpath/OCSP.java

Lines changed: 41 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,20 @@
2424
*/
2525
package sun.security.provider.certpath;
2626

27-
import java.io.InputStream;
2827
import java.io.IOException;
2928
import java.io.OutputStream;
3029
import java.net.URI;
3130
import java.net.URL;
3231
import java.net.HttpURLConnection;
32+
import java.net.URLEncoder;
3333
import java.security.cert.CertificateException;
3434
import java.security.cert.CertPathValidatorException;
3535
import java.security.cert.CertPathValidatorException.BasicReason;
3636
import java.security.cert.CRLReason;
3737
import java.security.cert.Extension;
3838
import java.security.cert.TrustAnchor;
3939
import java.security.cert.X509Certificate;
40-
import java.util.Arrays;
40+
import java.util.Base64;
4141
import java.util.Collections;
4242
import java.util.Date;
4343
import java.util.List;
@@ -46,6 +46,7 @@
4646
import sun.security.action.GetIntegerAction;
4747
import sun.security.util.Debug;
4848
import sun.security.util.Event;
49+
import sun.security.util.IOUtils;
4950
import sun.security.validator.Validator;
5051
import sun.security.x509.AccessDescription;
5152
import sun.security.x509.AuthorityInfoAccessExtension;
@@ -224,71 +225,61 @@ public static byte[] getOCSPBytes(List<CertId> certIds, URI responderURI,
224225
OCSPRequest request = new OCSPRequest(certIds, extensions);
225226
byte[] bytes = request.encodeBytes();
226227

227-
InputStream in = null;
228-
OutputStream out = null;
229-
byte[] response = null;
228+
if (debug != null) {
229+
debug.println("connecting to OCSP service at: " + responderURI);
230+
}
231+
Event.report(Event.ReporterCategory.CRLCHECK, "event.ocsp.check",
232+
responderURI.toString());
230233

234+
URL url;
235+
HttpURLConnection con = null;
231236
try {
232-
URL url = responderURI.toURL();
233-
if (debug != null) {
234-
debug.println("connecting to OCSP service at: " + url);
237+
String encodedGetReq = responderURI.toString() + "/" +
238+
URLEncoder.encode(Base64.getEncoder().encodeToString(bytes),
239+
"UTF-8");
240+
241+
if (encodedGetReq.length() <= 255) {
242+
url = new URL(encodedGetReq);
243+
con = (HttpURLConnection)url.openConnection();
244+
con.setDoOutput(true);
245+
con.setDoInput(true);
246+
con.setRequestMethod("GET");
247+
} else {
248+
url = responderURI.toURL();
249+
con = (HttpURLConnection)url.openConnection();
250+
con.setConnectTimeout(CONNECT_TIMEOUT);
251+
con.setReadTimeout(CONNECT_TIMEOUT);
252+
con.setDoOutput(true);
253+
con.setDoInput(true);
254+
con.setRequestMethod("POST");
255+
con.setRequestProperty
256+
("Content-type", "application/ocsp-request");
257+
con.setRequestProperty
258+
("Content-length", String.valueOf(bytes.length));
259+
OutputStream out = con.getOutputStream();
260+
out.write(bytes);
261+
out.flush();
235262
}
236263

237-
Event.report(Event.ReporterCategory.CRLCHECK, "event.ocsp.check", url.toString());
238-
HttpURLConnection con = (HttpURLConnection)url.openConnection();
239-
con.setConnectTimeout(CONNECT_TIMEOUT);
240-
con.setReadTimeout(CONNECT_TIMEOUT);
241-
con.setDoOutput(true);
242-
con.setDoInput(true);
243-
con.setRequestMethod("POST");
244-
con.setRequestProperty
245-
("Content-type", "application/ocsp-request");
246-
con.setRequestProperty
247-
("Content-length", String.valueOf(bytes.length));
248-
out = con.getOutputStream();
249-
out.write(bytes);
250-
out.flush();
251264
// Check the response
252265
if (debug != null &&
253266
con.getResponseCode() != HttpURLConnection.HTTP_OK) {
254267
debug.println("Received HTTP error: " + con.getResponseCode()
255268
+ " - " + con.getResponseMessage());
256269
}
257-
in = con.getInputStream();
270+
258271
int contentLength = con.getContentLength();
259272
if (contentLength == -1) {
260273
contentLength = Integer.MAX_VALUE;
261274
}
262-
response = new byte[contentLength > 2048 ? 2048 : contentLength];
263-
int total = 0;
264-
while (total < contentLength) {
265-
int count = in.read(response, total, response.length - total);
266-
if (count < 0)
267-
break;
268-
269-
total += count;
270-
if (total >= response.length && total < contentLength) {
271-
response = Arrays.copyOf(response, total * 2);
272-
}
273-
}
274-
response = Arrays.copyOf(response, total);
275+
276+
return IOUtils.readExactlyNBytes(con.getInputStream(),
277+
contentLength);
275278
} finally {
276-
if (in != null) {
277-
try {
278-
in.close();
279-
} catch (IOException ioe) {
280-
throw ioe;
281-
}
282-
}
283-
if (out != null) {
284-
try {
285-
out.close();
286-
} catch (IOException ioe) {
287-
throw ioe;
288-
}
279+
if (con != null) {
280+
con.disconnect();
289281
}
290282
}
291-
return response;
292283
}
293284

294285
/**

0 commit comments

Comments
 (0)