jdk/test/sun/security/tools/jarsigner/TimestampCheck.java
changeset 28662 efd0203db371
parent 24116 9f9b4ba34aad
child 28664 2f79ecb05ada
equal deleted inserted replaced
28661:4fe905a2d72f 28662:efd0203db371
     1 /*
     1 /*
     2  * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    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));
   134             }
   139             }
   135 
   140 
   136             // Write TSResponse
   141             // Write TSResponse
   137             System.err.println("\nResponse\n===================");
   142             System.err.println("\nResponse\n===================");
   138             KeyStore ks = KeyStore.getInstance("JKS");
   143             KeyStore ks = KeyStore.getInstance("JKS");
   139             ks.load(new FileInputStream(TSKS), "changeit".toCharArray());
   144             try (FileInputStream fis = new FileInputStream(keystore)) {
       
   145                 ks.load(fis, "changeit".toCharArray());
       
   146             }
   140 
   147 
   141             String alias = "ts";
   148             String alias = "ts";
   142             if (path == 6) alias = "tsbad1";
   149             if (path == 6) alias = "tsbad1";
   143             if (path == 7) alias = "tsbad2";
   150             if (path == 7) alias = "tsbad2";
   144             if (path == 8) alias = "tsbad3";
   151             if (path == 8) alias = "tsbad3";
   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);
   285                 checkTimestamp("new_12.jar", defaultPolicyId, "SHA-1");
   333                 checkTimestamp("new_12.jar", defaultPolicyId, "SHA-1");
   286             } else {                        // Run as a standalone server
   334             } else {                        // Run as a standalone server
   287                 System.err.println("Press Enter to quit server");
   335                 System.err.println("Press Enter to quit server");
   288                 System.in.read();
   336                 System.in.read();
   289             }
   337             }
   290         } finally {
       
   291             server.stop(0);
       
   292         }
   338         }
   293     }
   339     }
   294 
   340 
   295     static void checkTimestamp(String file, String policyId, String digestAlg)
   341     static void checkTimestamp(String file, String policyId, String digestAlg)
   296             throws Exception {
   342             throws Exception {