56 static final String TSKS = "tsks"; |
56 static final String TSKS = "tsks"; |
57 static final String JAR = "old.jar"; |
57 static final String JAR = "old.jar"; |
58 |
58 |
59 static final String defaultPolicyId = "2.3.4.5"; |
59 static final String defaultPolicyId = "2.3.4.5"; |
60 |
60 |
61 static class Handler implements HttpHandler { |
61 static class Handler implements HttpHandler, AutoCloseable { |
|
62 |
|
63 private final HttpServer httpServer; |
|
64 private final String keystore; |
|
65 |
|
66 @Override |
62 public void handle(HttpExchange t) throws IOException { |
67 public void handle(HttpExchange t) throws IOException { |
63 int len = 0; |
68 int len = 0; |
64 for (String h: t.getRequestHeaders().keySet()) { |
69 for (String h: t.getRequestHeaders().keySet()) { |
65 if (h.equalsIgnoreCase("Content-length")) { |
70 if (h.equalsIgnoreCase("Content-length")) { |
66 len = Integer.valueOf(t.getRequestHeaders().get(h).get(0)); |
71 len = Integer.valueOf(t.getRequestHeaders().get(h).get(0)); |
238 DerOutputStream out = new DerOutputStream(); |
245 DerOutputStream out = new DerOutputStream(); |
239 out.write(DerValue.tag_Sequence, response); |
246 out.write(DerValue.tag_Sequence, response); |
240 |
247 |
241 return out.toByteArray(); |
248 return out.toByteArray(); |
242 } |
249 } |
|
250 |
|
251 private Handler(HttpServer httpServer, String keystore) { |
|
252 this.httpServer = httpServer; |
|
253 this.keystore = keystore; |
|
254 } |
|
255 |
|
256 /** |
|
257 * Initialize TSA instance. |
|
258 * |
|
259 * Extended Key Info extension of certificate that is used for |
|
260 * signing TSA responses should contain timeStamping value. |
|
261 */ |
|
262 static Handler init(int port, String keystore) throws IOException { |
|
263 HttpServer httpServer = HttpServer.create( |
|
264 new InetSocketAddress(port), 0); |
|
265 Handler tsa = new Handler(httpServer, keystore); |
|
266 httpServer.createContext("/", tsa); |
|
267 return tsa; |
|
268 } |
|
269 |
|
270 /** |
|
271 * Start TSA service. |
|
272 */ |
|
273 void start() { |
|
274 httpServer.start(); |
|
275 } |
|
276 |
|
277 /** |
|
278 * Stop TSA service. |
|
279 */ |
|
280 void stop() { |
|
281 httpServer.stop(0); |
|
282 } |
|
283 |
|
284 /** |
|
285 * Return server port number. |
|
286 */ |
|
287 int getPort() { |
|
288 return httpServer.getAddress().getPort(); |
|
289 } |
|
290 |
|
291 @Override |
|
292 public void close() throws Exception { |
|
293 stop(); |
|
294 } |
243 } |
295 } |
244 |
296 |
245 public static void main(String[] args) throws Exception { |
297 public static void main(String[] args) throws Exception { |
246 |
298 try (Handler tsa = Handler.init(0, TSKS);) { |
247 Handler h = new Handler(); |
299 tsa.start(); |
248 HttpServer server = HttpServer.create(new InetSocketAddress(0), 0); |
300 int port = tsa.getPort(); |
249 int port = server.getAddress().getPort(); |
301 |
250 HttpContext ctx = server.createContext("/", h); |
302 String cmd; |
251 server.start(); |
303 // Use -J-Djava.security.egd=file:/dev/./urandom to speed up |
252 |
304 // nonce generation in timestamping request. Not avaibale on |
253 String cmd = null; |
305 // Windows and defaults to thread seed generator, not too bad. |
254 // Use -J-Djava.security.egd=file:/dev/./urandom to speed up |
306 if (System.getProperty("java.home").endsWith("jre")) { |
255 // nonce generation in timestamping request. Not avaibale on |
307 cmd = System.getProperty("java.home") + "/../bin/jarsigner"; |
256 // Windows and defaults to thread seed generator, not too bad. |
308 } else { |
257 if (System.getProperty("java.home").endsWith("jre")) { |
309 cmd = System.getProperty("java.home") + "/bin/jarsigner"; |
258 cmd = System.getProperty("java.home") + "/../bin/jarsigner"; |
310 } |
259 } else { |
311 |
260 cmd = System.getProperty("java.home") + "/bin/jarsigner"; |
312 cmd += System.getProperty("test.tool.vm.opts") |
261 } |
313 + " -J-Djava.security.egd=file:/dev/./urandom" |
262 |
314 + " -debug -keystore " + TSKS + " -storepass changeit" |
263 cmd += " " + System.getProperty("test.tool.vm.opts") + |
315 + " -tsa http://localhost:" + port + "/%d" |
264 " -J-Djava.security.egd=file:/dev/./urandom" + |
316 + " -signedjar new_%d.jar " + JAR + " old"; |
265 " -debug -keystore " + TSKS + " -storepass changeit" + |
317 |
266 " -tsa http://localhost:" + port + "/%d" + |
|
267 " -signedjar new_%d.jar " + JAR + " old"; |
|
268 |
|
269 try { |
|
270 if (args.length == 0) { // Run this test |
318 if (args.length == 0) { // Run this test |
271 jarsigner(cmd, 0, true); // Success, normal call |
319 jarsigner(cmd, 0, true); // Success, normal call |
272 jarsigner(cmd, 1, false); // These 4 should fail |
320 jarsigner(cmd, 1, false); // These 4 should fail |
273 jarsigner(cmd, 2, false); |
321 jarsigner(cmd, 2, false); |
274 jarsigner(cmd, 3, false); |
322 jarsigner(cmd, 3, false); |