test/jdk/sun/security/tools/jarsigner/TimestampCheck.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 45467 jdk/test/sun/security/tools/jarsigner/TimestampCheck.java@99c87a16a8e4
child 47469 6ae08c311cd3
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
     1
/*
45287
e0bb5f83e17a 8180888: move jdk.testlibrary.JarUtils to the top level testlibrary
iignatyev
parents: 44046
diff changeset
     2
 * Copyright (c) 2003, 2017, Oracle and/or its affiliates. All rights reserved.
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
     4
 *
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
     7
 * published by the Free Software Foundation.
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
     8
 *
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    13
 * accompanied this code).
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    14
 *
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5297
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5297
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5297
diff changeset
    21
 * questions.
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    22
 */
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    23
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    24
import com.sun.net.httpserver.*;
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    25
import java.io.ByteArrayOutputStream;
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
    26
import java.io.File;
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    27
import java.io.IOException;
24034
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
    28
import java.io.InputStream;
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    29
import java.io.OutputStream;
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    30
import java.math.BigInteger;
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    31
import java.net.InetSocketAddress;
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
    32
import java.nio.file.Files;
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
    33
import java.nio.file.Paths;
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    34
import java.security.KeyStore;
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    35
import java.security.PrivateKey;
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    36
import java.security.Signature;
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    37
import java.security.cert.Certificate;
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    38
import java.security.cert.X509Certificate;
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
    39
import java.util.ArrayList;
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
    40
import java.util.Arrays;
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    41
import java.util.Calendar;
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
    42
import java.util.List;
24034
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
    43
import java.util.jar.JarEntry;
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
    44
import java.util.jar.JarFile;
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
    45
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43701
diff changeset
    46
import jdk.test.lib.SecurityTools;
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
    47
import jdk.testlibrary.*;
45287
e0bb5f83e17a 8180888: move jdk.testlibrary.JarUtils to the top level testlibrary
iignatyev
parents: 44046
diff changeset
    48
import jdk.test.lib.util.JarUtils;
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    49
import sun.security.pkcs.ContentInfo;
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    50
import sun.security.pkcs.PKCS7;
24034
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
    51
import sun.security.pkcs.PKCS9Attribute;
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    52
import sun.security.pkcs.SignerInfo;
24034
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
    53
import sun.security.timestamp.TimestampToken;
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    54
import sun.security.util.DerOutputStream;
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    55
import sun.security.util.DerValue;
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    56
import sun.security.util.ObjectIdentifier;
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    57
import sun.security.x509.AlgorithmId;
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    58
import sun.security.x509.X500Name;
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    59
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
    60
/*
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
    61
 * @test
42180
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
    62
 * @bug 6543842 6543440 6939248 8009636 8024302 8163304 8169911
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
    63
 * @summary checking response of timestamp
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
    64
 * @modules java.base/sun.security.pkcs
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
    65
 *          java.base/sun.security.timestamp
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
    66
 *          java.base/sun.security.x509
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
    67
 *          java.base/sun.security.util
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
    68
 *          java.base/sun.security.tools.keytool
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
    69
 * @library /lib/testlibrary
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43701
diff changeset
    70
 * @library /test/lib
45467
99c87a16a8e4 8181761: add explicit @build actions for jdk.test.lib classes in all :tier2 tests
iignatyev
parents: 45287
diff changeset
    71
 * @build jdk.test.lib.util.JarUtils
99c87a16a8e4 8181761: add explicit @build actions for jdk.test.lib classes in all :tier2 tests
iignatyev
parents: 45287
diff changeset
    72
 *        jdk.test.lib.SecurityTools
99c87a16a8e4 8181761: add explicit @build actions for jdk.test.lib classes in all :tier2 tests
iignatyev
parents: 45287
diff changeset
    73
 *        jdk.test.lib.Utils
99c87a16a8e4 8181761: add explicit @build actions for jdk.test.lib classes in all :tier2 tests
iignatyev
parents: 45287
diff changeset
    74
 *        jdk.test.lib.Asserts
99c87a16a8e4 8181761: add explicit @build actions for jdk.test.lib classes in all :tier2 tests
iignatyev
parents: 45287
diff changeset
    75
 *        jdk.test.lib.JDKToolFinder
99c87a16a8e4 8181761: add explicit @build actions for jdk.test.lib classes in all :tier2 tests
iignatyev
parents: 45287
diff changeset
    76
 *        jdk.test.lib.JDKToolLauncher
99c87a16a8e4 8181761: add explicit @build actions for jdk.test.lib classes in all :tier2 tests
iignatyev
parents: 45287
diff changeset
    77
 *        jdk.test.lib.Platform
99c87a16a8e4 8181761: add explicit @build actions for jdk.test.lib classes in all :tier2 tests
iignatyev
parents: 45287
diff changeset
    78
 *        jdk.test.lib.process.*
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
    79
 * @run main/timeout=600 TimestampCheck
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
    80
 */
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    81
public class TimestampCheck {
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    82
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
    83
    static final String defaultPolicyId = "2.3.4";
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
    84
    static String host = null;
24034
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
    85
28662
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
    86
    static class Handler implements HttpHandler, AutoCloseable {
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
    87
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
    88
        private final HttpServer httpServer;
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
    89
        private final String keystore;
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
    90
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
    91
        @Override
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    92
        public void handle(HttpExchange t) throws IOException {
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    93
            int len = 0;
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    94
            for (String h: t.getRequestHeaders().keySet()) {
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    95
                if (h.equalsIgnoreCase("Content-length")) {
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    96
                    len = Integer.valueOf(t.getRequestHeaders().get(h).get(0));
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    97
                }
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    98
            }
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    99
            byte[] input = new byte[len];
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   100
            t.getRequestBody().read(input);
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   101
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   102
            try {
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   103
                String path = t.getRequestURI().getPath().substring(1);
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   104
                byte[] output = sign(input, path);
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   105
                Headers out = t.getResponseHeaders();
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   106
                out.set("Content-Type", "application/timestamp-reply");
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   107
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   108
                t.sendResponseHeaders(200, output.length);
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   109
                OutputStream os = t.getResponseBody();
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   110
                os.write(output);
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   111
            } catch (Exception e) {
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   112
                e.printStackTrace();
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   113
                t.sendResponseHeaders(500, 0);
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   114
            }
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   115
            t.close();
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   116
        }
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   117
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   118
        /**
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   119
         * @param input The data to sign
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   120
         * @param path different cases to simulate, impl on URL path
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   121
         * @returns the signed
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   122
         */
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   123
        byte[] sign(byte[] input, String path) throws Exception {
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   124
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   125
            DerValue value = new DerValue(input);
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   126
            System.err.println("\nIncoming Request\n===================");
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   127
            System.err.println("Version: " + value.data.getInteger());
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   128
            DerValue messageImprint = value.data.getDerValue();
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   129
            AlgorithmId aid = AlgorithmId.parse(
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   130
                    messageImprint.data.getDerValue());
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   131
            System.err.println("AlgorithmId: " + aid);
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   132
24034
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   133
            ObjectIdentifier policyId = new ObjectIdentifier(defaultPolicyId);
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   134
            BigInteger nonce = null;
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   135
            while (value.data.available() > 0) {
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   136
                DerValue v = value.data.getDerValue();
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   137
                if (v.tag == DerValue.tag_Integer) {
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   138
                    nonce = v.getBigInteger();
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   139
                    System.err.println("nonce: " + nonce);
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   140
                } else if (v.tag == DerValue.tag_Boolean) {
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   141
                    System.err.println("certReq: " + v.getBoolean());
24034
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   142
                } else if (v.tag == DerValue.tag_ObjectId) {
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   143
                    policyId = v.getOID();
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   144
                    System.err.println("PolicyID: " + policyId);
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   145
                }
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   146
            }
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   147
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   148
            System.err.println("\nResponse\n===================");
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   149
            KeyStore ks = KeyStore.getInstance(
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   150
                    new File(keystore), "changeit".toCharArray());
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   151
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   152
            String alias = "ts";
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   153
            if (path.startsWith("bad") || path.equals("weak")) {
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   154
                alias = "ts" + path;
28662
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   155
            }
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   156
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   157
            if (path.equals("diffpolicy")) {
24034
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   158
                policyId = new ObjectIdentifier(defaultPolicyId);
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   159
            }
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   160
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   161
            DerOutputStream statusInfo = new DerOutputStream();
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   162
            statusInfo.putInteger(0);
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   163
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   164
            AlgorithmId[] algorithms = {aid};
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   165
            Certificate[] chain = ks.getCertificateChain(alias);
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   166
            X509Certificate[] signerCertificateChain;
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   167
            X509Certificate signer = (X509Certificate)chain[0];
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   168
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   169
            if (path.equals("fullchain")) {   // Only case 5 uses full chain
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   170
                signerCertificateChain = new X509Certificate[chain.length];
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   171
                for (int i=0; i<chain.length; i++) {
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   172
                    signerCertificateChain[i] = (X509Certificate)chain[i];
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   173
                }
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   174
            } else if (path.equals("nocert")) {
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   175
                signerCertificateChain = new X509Certificate[0];
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   176
            } else {
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   177
                signerCertificateChain = new X509Certificate[1];
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   178
                signerCertificateChain[0] = (X509Certificate)chain[0];
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   179
            }
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   180
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   181
            DerOutputStream tst = new DerOutputStream();
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   182
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   183
            tst.putInteger(1);
24034
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   184
            tst.putOID(policyId);
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   185
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   186
            if (!path.equals("baddigest") && !path.equals("diffalg")) {
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   187
                tst.putDerValue(messageImprint);
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   188
            } else {
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   189
                byte[] data = messageImprint.toByteArray();
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   190
                if (path.equals("diffalg")) {
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   191
                    data[6] = (byte)0x01;
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   192
                } else {
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   193
                    data[data.length-1] = (byte)0x01;
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   194
                    data[data.length-2] = (byte)0x02;
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   195
                    data[data.length-3] = (byte)0x03;
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   196
                }
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   197
                tst.write(data);
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   198
            }
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   199
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   200
            tst.putInteger(1);
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   201
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   202
            Calendar cal = Calendar.getInstance();
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   203
            tst.putGeneralizedTime(cal.getTime());
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   204
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   205
            if (path.equals("diffnonce")) {
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   206
                tst.putInteger(1234);
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   207
            } else if (path.equals("nononce")) {
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   208
                // no noce
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   209
            } else {
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   210
                tst.putInteger(nonce);
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   211
            }
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   212
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   213
            DerOutputStream tstInfo = new DerOutputStream();
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   214
            tstInfo.write(DerValue.tag_Sequence, tst);
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   215
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   216
            DerOutputStream tstInfo2 = new DerOutputStream();
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   217
            tstInfo2.putOctetString(tstInfo.toByteArray());
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   218
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   219
            // Always use the same algorithm at timestamp signing
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   220
            // so it is different from the hash algorithm.
21342
7bbb056a1c23 8027026: Change keytool -genkeypair to use -keyalg RSA
weijun
parents: 17161
diff changeset
   221
            Signature sig = Signature.getInstance("SHA1withRSA");
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   222
            sig.initSign((PrivateKey)(ks.getKey(
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   223
                    alias, "changeit".toCharArray())));
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   224
            sig.update(tstInfo.toByteArray());
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   225
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   226
            ContentInfo contentInfo = new ContentInfo(new ObjectIdentifier(
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   227
                    "1.2.840.113549.1.9.16.1.4"),
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   228
                    new DerValue(tstInfo2.toByteArray()));
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   229
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   230
            System.err.println("Signing...");
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   231
            System.err.println(new X500Name(signer
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   232
                    .getIssuerX500Principal().getName()));
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   233
            System.err.println(signer.getSerialNumber());
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   234
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   235
            SignerInfo signerInfo = new SignerInfo(
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   236
                    new X500Name(signer.getIssuerX500Principal().getName()),
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   237
                    signer.getSerialNumber(),
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   238
                    AlgorithmId.get("SHA-1"), AlgorithmId.get("RSA"), sig.sign());
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   239
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   240
            SignerInfo[] signerInfos = {signerInfo};
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   241
            PKCS7 p7 = new PKCS7(algorithms, contentInfo,
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   242
                    signerCertificateChain, signerInfos);
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   243
            ByteArrayOutputStream p7out = new ByteArrayOutputStream();
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   244
            p7.encodeSignedData(p7out);
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   245
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   246
            DerOutputStream response = new DerOutputStream();
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   247
            response.write(DerValue.tag_Sequence, statusInfo);
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   248
            response.putDerValue(new DerValue(p7out.toByteArray()));
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   249
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   250
            DerOutputStream out = new DerOutputStream();
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   251
            out.write(DerValue.tag_Sequence, response);
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   252
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   253
            return out.toByteArray();
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   254
        }
28662
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   255
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   256
        private Handler(HttpServer httpServer, String keystore) {
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   257
            this.httpServer = httpServer;
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   258
            this.keystore = keystore;
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   259
        }
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   260
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   261
        /**
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   262
         * Initialize TSA instance.
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   263
         *
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   264
         * Extended Key Info extension of certificate that is used for
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   265
         * signing TSA responses should contain timeStamping value.
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   266
         */
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   267
        static Handler init(int port, String keystore) throws IOException {
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   268
            HttpServer httpServer = HttpServer.create(
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   269
                    new InetSocketAddress(port), 0);
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   270
            Handler tsa = new Handler(httpServer, keystore);
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   271
            httpServer.createContext("/", tsa);
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   272
            return tsa;
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   273
        }
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   274
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   275
        /**
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   276
         * Start TSA service.
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   277
         */
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   278
        void start() {
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   279
            httpServer.start();
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   280
        }
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   281
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   282
        /**
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   283
         * Stop TSA service.
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   284
         */
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   285
        void stop() {
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   286
            httpServer.stop(0);
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   287
        }
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   288
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   289
        /**
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   290
         * Return server port number.
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   291
         */
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   292
        int getPort() {
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   293
            return httpServer.getAddress().getPort();
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   294
        }
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   295
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   296
        @Override
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   297
        public void close() throws Exception {
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   298
            stop();
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   299
        }
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   300
    }
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   301
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   302
    public static void main(String[] args) throws Throwable {
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   303
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   304
        prepare();
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   305
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   306
        try (Handler tsa = Handler.init(0, "tsks");) {
28662
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   307
            tsa.start();
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   308
            int port = tsa.getPort();
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   309
            host = "http://localhost:" + port + "/";
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   310
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   311
            if (args.length == 0) {         // Run this test
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   312
                sign("none")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   313
                        .shouldContain("is not timestamped")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   314
                        .shouldHaveExitValue(0);
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   315
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   316
                sign("badku")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   317
                        .shouldHaveExitValue(0);
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   318
                checkBadKU("badku.jar");
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   319
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   320
                sign("normal")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   321
                        .shouldNotContain("is not timestamped")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   322
                        .shouldHaveExitValue(0);
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   323
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   324
                sign("nononce")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   325
                        .shouldHaveExitValue(1);
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   326
                sign("diffnonce")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   327
                        .shouldHaveExitValue(1);
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   328
                sign("baddigest")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   329
                        .shouldHaveExitValue(1);
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   330
                sign("diffalg")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   331
                        .shouldHaveExitValue(1);
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   332
                sign("fullchain")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   333
                        .shouldHaveExitValue(0);   // Success, 6543440 solved.
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   334
                sign("bad1")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   335
                        .shouldHaveExitValue(1);
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   336
                sign("bad2")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   337
                        .shouldHaveExitValue(1);
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   338
                sign("bad3")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   339
                        .shouldHaveExitValue(1);
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   340
                sign("nocert")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   341
                        .shouldHaveExitValue(1);
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   342
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   343
                sign("policy", "-tsapolicyid",  "1.2.3")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   344
                        .shouldHaveExitValue(0);
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   345
                checkTimestamp("policy.jar", "1.2.3", "SHA-256");
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   346
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   347
                sign("diffpolicy", "-tsapolicyid", "1.2.3")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   348
                        .shouldHaveExitValue(1);
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   349
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   350
                sign("tsaalg", "-tsadigestalg", "SHA")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   351
                        .shouldHaveExitValue(0);
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   352
                checkTimestamp("tsaalg.jar", defaultPolicyId, "SHA-1");
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   353
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   354
                sign("weak", "-digestalg", "MD5",
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   355
                                "-sigalg", "MD5withRSA", "-tsadigestalg", "MD5")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   356
                        .shouldHaveExitValue(0)
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   357
                        .shouldMatch("MD5.*-digestalg.*risk")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   358
                        .shouldMatch("MD5.*-tsadigestalg.*risk")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   359
                        .shouldMatch("MD5withRSA.*-sigalg.*risk");
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   360
                checkWeak("weak.jar");
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   361
42180
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   362
                signWithAliasAndTsa("halfWeak", "old.jar", "old", "-digestalg", "MD5")
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   363
                        .shouldHaveExitValue(0);
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   364
                checkHalfWeak("halfWeak.jar");
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   365
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   366
                // sign with DSA key
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   367
                signWithAliasAndTsa("sign1", "old.jar", "dsakey")
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   368
                        .shouldHaveExitValue(0);
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   369
                // sign with RSAkeysize < 1024
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   370
                signWithAliasAndTsa("sign2", "sign1.jar", "weakkeysize")
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   371
                        .shouldHaveExitValue(0);
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   372
                checkMultiple("sign2.jar");
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   373
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   374
                // When .SF or .RSA is missing or invalid
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   375
                checkMissingOrInvalidFiles("normal.jar");
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   376
            } else {                        // Run as a standalone server
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   377
                System.err.println("Press Enter to quit server");
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   378
                System.in.read();
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   379
            }
24034
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   380
        }
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   381
    }
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   382
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   383
    private static void checkMissingOrInvalidFiles(String s)
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   384
            throws Throwable {
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   385
        JarUtils.updateJar(s, "1.jar", "-", "META-INF/OLD.SF");
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   386
        verify("1.jar", "-verbose")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   387
                .shouldHaveExitValue(0)
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   388
                .shouldContain("treated as unsigned")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   389
                .shouldContain("Missing signature-related file META-INF/OLD.SF");
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   390
        JarUtils.updateJar(s, "2.jar", "-", "META-INF/OLD.RSA");
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   391
        verify("2.jar", "-verbose")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   392
                .shouldHaveExitValue(0)
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   393
                .shouldContain("treated as unsigned")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   394
                .shouldContain("Missing block file for signature-related file META-INF/OLD.SF");
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   395
        JarUtils.updateJar(s, "3.jar", "META-INF/OLD.SF");
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   396
        verify("3.jar", "-verbose")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   397
                .shouldHaveExitValue(0)
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   398
                .shouldContain("treated as unsigned")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   399
                .shouldContain("Unparsable signature-related file META-INF/OLD.SF");
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   400
        JarUtils.updateJar(s, "4.jar", "META-INF/OLD.RSA");
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   401
        verify("4.jar", "-verbose")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   402
                .shouldHaveExitValue(0)
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   403
                .shouldContain("treated as unsigned")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   404
                .shouldContain("Unparsable signature-related file META-INF/OLD.RSA");
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   405
    }
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   406
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   407
    static OutputAnalyzer jarsigner(List<String> extra)
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   408
            throws Throwable {
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   409
        JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("jarsigner")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   410
                .addVMArg("-Duser.language=en")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   411
                .addVMArg("-Duser.country=US")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   412
                .addToolArg("-keystore")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   413
                .addToolArg("tsks")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   414
                .addToolArg("-storepass")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   415
                .addToolArg("changeit");
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   416
        for (String s : extra) {
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   417
            if (s.startsWith("-J")) {
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   418
                launcher.addVMArg(s.substring(2));
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   419
            } else {
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   420
                launcher.addToolArg(s);
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   421
            }
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   422
        }
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   423
        return ProcessTools.executeCommand(launcher.getCommand());
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   424
    }
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   425
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   426
    static OutputAnalyzer verify(String file, String... extra)
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   427
            throws Throwable {
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   428
        List<String> args = new ArrayList<>();
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   429
        args.add("-verify");
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   430
        args.add(file);
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   431
        args.addAll(Arrays.asList(extra));
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   432
        return jarsigner(args);
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   433
    }
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   434
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   435
    static void checkBadKU(String file) throws Throwable {
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   436
        verify(file)
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   437
                .shouldHaveExitValue(0)
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   438
                .shouldContain("treated as unsigned")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   439
                .shouldContain("re-run jarsigner with debug enabled");
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   440
        verify(file, "-verbose")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   441
                .shouldHaveExitValue(0)
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   442
                .shouldContain("Signed by")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   443
                .shouldContain("treated as unsigned")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   444
                .shouldContain("re-run jarsigner with debug enabled");
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   445
        verify(file, "-J-Djava.security.debug=jar")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   446
                .shouldHaveExitValue(0)
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   447
                .shouldContain("SignatureException: Key usage restricted")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   448
                .shouldContain("treated as unsigned")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   449
                .shouldContain("re-run jarsigner with debug enabled");
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   450
    }
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   451
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   452
    static void checkWeak(String file) throws Throwable {
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   453
        verify(file)
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   454
                .shouldHaveExitValue(0)
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   455
                .shouldContain("treated as unsigned")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   456
                .shouldMatch("weak algorithm that is now disabled.")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   457
                .shouldMatch("Re-run jarsigner with the -verbose option for more details");
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   458
        verify(file, "-verbose")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   459
                .shouldHaveExitValue(0)
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   460
                .shouldContain("treated as unsigned")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   461
                .shouldMatch("weak algorithm that is now disabled by")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   462
                .shouldMatch("Digest algorithm: .*weak")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   463
                .shouldMatch("Signature algorithm: .*weak")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   464
                .shouldMatch("Timestamp digest algorithm: .*weak")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   465
                .shouldNotMatch("Timestamp signature algorithm: .*weak.*weak")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   466
                .shouldMatch("Timestamp signature algorithm: .*key.*weak");
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   467
        verify(file, "-J-Djava.security.debug=jar")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   468
                .shouldHaveExitValue(0)
43701
fe8c324ba97c 8160655: Fix denyAfter and usage types for security properties
ascarpino
parents: 42180
diff changeset
   469
                .shouldMatch("SignatureException:.*disabled");
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43701
diff changeset
   470
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43701
diff changeset
   471
        // For 8171319: keytool should print out warnings when reading or
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43701
diff changeset
   472
        //              generating cert/cert req using weak algorithms.
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43701
diff changeset
   473
        // Must call keytool the command, otherwise doPrintCert() might not
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43701
diff changeset
   474
        // be able to reset "jdk.certpath.disabledAlgorithms".
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43701
diff changeset
   475
        String sout = SecurityTools.keytool("-printcert -jarfile weak.jar")
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43701
diff changeset
   476
                .stderrShouldContain("The TSA certificate uses a 512-bit RSA key" +
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43701
diff changeset
   477
                        " which is considered a security risk.")
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43701
diff changeset
   478
                .getStdout();
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43701
diff changeset
   479
        if (sout.indexOf("weak", sout.indexOf("Timestamp:")) < 0) {
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43701
diff changeset
   480
            throw new RuntimeException("timestamp not weak: " + sout);
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43701
diff changeset
   481
        }
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   482
    }
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   483
42180
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   484
    static void checkHalfWeak(String file) throws Throwable {
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   485
        verify(file)
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   486
                .shouldHaveExitValue(0)
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   487
                .shouldContain("treated as unsigned")
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   488
                .shouldMatch("weak algorithm that is now disabled.")
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   489
                .shouldMatch("Re-run jarsigner with the -verbose option for more details");
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   490
        verify(file, "-verbose")
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   491
                .shouldHaveExitValue(0)
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   492
                .shouldContain("treated as unsigned")
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   493
                .shouldMatch("weak algorithm that is now disabled by")
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   494
                .shouldMatch("Digest algorithm: .*weak")
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   495
                .shouldNotMatch("Signature algorithm: .*weak")
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   496
                .shouldNotMatch("Timestamp digest algorithm: .*weak")
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   497
                .shouldNotMatch("Timestamp signature algorithm: .*weak.*weak")
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   498
                .shouldNotMatch("Timestamp signature algorithm: .*key.*weak");
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   499
     }
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   500
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   501
    static void checkMultiple(String file) throws Throwable {
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   502
        verify(file)
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   503
                .shouldHaveExitValue(0)
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   504
                .shouldContain("jar verified");
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   505
        verify(file, "-verbose", "-certs")
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   506
                .shouldHaveExitValue(0)
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   507
                .shouldContain("jar verified")
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   508
                .shouldMatch("X.509.*CN=dsakey")
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   509
                .shouldNotMatch("X.509.*CN=weakkeysize")
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   510
                .shouldMatch("Signed by .*CN=dsakey")
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   511
                .shouldMatch("Signed by .*CN=weakkeysize")
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   512
                .shouldMatch("Signature algorithm: .*key.*weak");
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   513
     }
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   514
24034
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   515
    static void checkTimestamp(String file, String policyId, String digestAlg)
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   516
            throws Exception {
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   517
        try (JarFile jf = new JarFile(file)) {
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   518
            JarEntry je = jf.getJarEntry("META-INF/OLD.RSA");
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   519
            try (InputStream is = jf.getInputStream(je)) {
32933
83d50f1247bb 8138978: Examine usages of sun.misc.IOUtils
chegar
parents: 28664
diff changeset
   520
                byte[] content = is.readAllBytes();
24034
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   521
                PKCS7 p7 = new PKCS7(content);
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   522
                SignerInfo[] si = p7.getSignerInfos();
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   523
                if (si == null || si.length == 0) {
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   524
                    throw new Exception("Not signed");
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   525
                }
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   526
                PKCS9Attribute p9 = si[0].getUnauthenticatedAttributes()
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   527
                        .getAttribute(PKCS9Attribute.SIGNATURE_TIMESTAMP_TOKEN_OID);
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   528
                PKCS7 tsToken = new PKCS7((byte[]) p9.getValue());
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   529
                TimestampToken tt =
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   530
                        new TimestampToken(tsToken.getContentInfo().getData());
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   531
                if (!tt.getHashAlgorithm().toString().equals(digestAlg)) {
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   532
                    throw new Exception("Digest alg different");
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   533
                }
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   534
                if (!tt.getPolicyID().equals(policyId)) {
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   535
                    throw new Exception("policyId different");
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   536
                }
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   537
            }
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   538
        }
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   539
    }
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   540
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   541
    static int which = 0;
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   542
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   543
    /**
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   544
     * @param extra more args given to jarsigner
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   545
     */
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   546
    static OutputAnalyzer sign(String path, String... extra)
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   547
            throws Throwable {
42180
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   548
        String alias = path.equals("badku") ? "badku" : "old";
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   549
        return signWithAliasAndTsa(path, "old.jar", alias, extra);
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   550
    }
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   551
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   552
    static OutputAnalyzer signWithAliasAndTsa (String path, String jar,
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   553
            String alias, String...extra) throws Throwable {
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   554
        which++;
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   555
        System.err.println("\n>> Test #" + which + ": " + Arrays.toString(extra));
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   556
        List<String> args = List.of("-J-Djava.security.egd=file:/dev/./urandom",
42180
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   557
                "-debug", "-signedjar", path + ".jar", jar, alias);
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   558
        args = new ArrayList<>(args);
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   559
        if (!path.equals("none") && !path.equals("badku")) {
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   560
            args.add("-tsa");
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   561
            args.add(host + path);
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   562
        }
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   563
        args.addAll(Arrays.asList(extra));
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   564
        return jarsigner(args);
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   565
    }
22315
529f1cc322fc 8024302: Clarify jar verifications
weijun
parents: 17161
diff changeset
   566
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   567
    static void prepare() throws Exception {
45287
e0bb5f83e17a 8180888: move jdk.testlibrary.JarUtils to the top level testlibrary
iignatyev
parents: 44046
diff changeset
   568
        JarUtils.createJar("old.jar", "A");
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   569
        Files.deleteIfExists(Paths.get("tsks"));
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   570
        keytool("-alias ca -genkeypair -ext bc -dname CN=CA");
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   571
        keytool("-alias old -genkeypair -dname CN=old");
42180
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   572
        keytool("-alias dsakey -genkeypair -keyalg DSA -dname CN=dsakey");
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   573
        keytool("-alias weakkeysize -genkeypair -keysize 512 -dname CN=weakkeysize");
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   574
        keytool("-alias badku -genkeypair -dname CN=badku");
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   575
        keytool("-alias ts -genkeypair -dname CN=ts");
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   576
        keytool("-alias tsweak -genkeypair -keysize 512 -dname CN=tsbad1");
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   577
        keytool("-alias tsbad1 -genkeypair -dname CN=tsbad1");
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   578
        keytool("-alias tsbad2 -genkeypair -dname CN=tsbad2");
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   579
        keytool("-alias tsbad3 -genkeypair -dname CN=tsbad3");
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   580
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   581
        gencert("old");
42180
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   582
        gencert("dsakey");
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   583
        gencert("weakkeysize");
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   584
        gencert("badku", "-ext ku:critical=keyAgreement");
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   585
        gencert("ts", "-ext eku:critical=ts");
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   586
        gencert("tsweak", "-ext eku:critical=ts");
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   587
        gencert("tsbad1");
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   588
        gencert("tsbad2", "-ext eku=ts");
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   589
        gencert("tsbad3", "-ext eku:critical=cs");
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   590
    }
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   591
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   592
    static void gencert(String alias, String... extra) throws Exception {
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   593
        keytool("-alias " + alias + " -certreq -file " + alias + ".req");
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   594
        String genCmd = "-gencert -alias ca -infile " +
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   595
                alias + ".req -outfile " + alias + ".cert";
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   596
        for (String s : extra) {
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   597
            genCmd += " " + s;
22315
529f1cc322fc 8024302: Clarify jar verifications
weijun
parents: 17161
diff changeset
   598
        }
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   599
        keytool(genCmd);
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   600
        keytool("-alias " + alias + " -importcert -file " + alias + ".cert");
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   601
    }
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   602
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   603
    static void keytool(String cmd) throws Exception {
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   604
        cmd = "-keystore tsks -storepass changeit -keypass changeit " +
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   605
                "-keyalg rsa -validity 200 " + cmd;
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   606
        sun.security.tools.keytool.Main.main(cmd.split(" "));
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   607
    }
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   608
}