jdk/test/sun/security/tools/jarsigner/TimestampCheck.java
author ascarpino
Wed, 08 Feb 2017 12:08:28 -0800
changeset 43701 fe8c324ba97c
parent 42180 11be74fc0be5
child 44046 762e807bfac1
permissions -rw-r--r--
8160655: Fix denyAfter and usage types for security properties Reviewed-by: mullan, xuelei
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
/*
39147
98f41520c884 8146393: sun/security/tools/jarsigner/ts.sh failed on OEL Linux 7 with ar_SA Locale
tidu
parents: 32933
diff changeset
     2
 * Copyright (c) 2003, 2016, 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
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
    46
import jdk.testlibrary.*;
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
    47
import jdk.testlibrary.JarUtils;
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    48
import sun.security.pkcs.ContentInfo;
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    49
import sun.security.pkcs.PKCS7;
24034
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
    50
import sun.security.pkcs.PKCS9Attribute;
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    51
import sun.security.pkcs.SignerInfo;
24034
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
    52
import sun.security.timestamp.TimestampToken;
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    53
import sun.security.util.DerOutputStream;
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    54
import sun.security.util.DerValue;
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    55
import sun.security.util.ObjectIdentifier;
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    56
import sun.security.x509.AlgorithmId;
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    57
import sun.security.x509.X500Name;
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    58
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
    59
/*
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
    60
 * @test
42180
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
    61
 * @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
    62
 * @summary checking response of timestamp
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
    63
 * @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
    64
 *          java.base/sun.security.timestamp
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
    65
 *          java.base/sun.security.x509
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
    66
 *          java.base/sun.security.util
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
    67
 *          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
    68
 * @library /lib/testlibrary
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
    69
 * @run main/timeout=600 TimestampCheck
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
    70
 */
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    71
public class TimestampCheck {
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    72
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
    73
    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
    74
    static String host = null;
24034
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
    75
28662
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
    76
    static class Handler implements HttpHandler, AutoCloseable {
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
    77
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
    78
        private final HttpServer httpServer;
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
    79
        private final String keystore;
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
    80
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
    81
        @Override
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    82
        public void handle(HttpExchange t) throws IOException {
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    83
            int len = 0;
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    84
            for (String h: t.getRequestHeaders().keySet()) {
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    85
                if (h.equalsIgnoreCase("Content-length")) {
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    86
                    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
    87
                }
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    88
            }
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    89
            byte[] input = new byte[len];
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    90
            t.getRequestBody().read(input);
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    91
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    92
            try {
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
    93
                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
    94
                byte[] output = sign(input, path);
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    95
                Headers out = t.getResponseHeaders();
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    96
                out.set("Content-Type", "application/timestamp-reply");
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
                t.sendResponseHeaders(200, output.length);
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    99
                OutputStream os = t.getResponseBody();
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   100
                os.write(output);
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   101
            } catch (Exception e) {
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   102
                e.printStackTrace();
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   103
                t.sendResponseHeaders(500, 0);
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   104
            }
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   105
            t.close();
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   106
        }
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
        /**
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   109
         * @param input The data to sign
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   110
         * @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
   111
         * @returns the signed
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   112
         */
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   113
        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
   114
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   115
            DerValue value = new DerValue(input);
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   116
            System.err.println("\nIncoming Request\n===================");
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   117
            System.err.println("Version: " + value.data.getInteger());
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   118
            DerValue messageImprint = value.data.getDerValue();
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   119
            AlgorithmId aid = AlgorithmId.parse(
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   120
                    messageImprint.data.getDerValue());
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   121
            System.err.println("AlgorithmId: " + aid);
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   122
24034
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   123
            ObjectIdentifier policyId = new ObjectIdentifier(defaultPolicyId);
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   124
            BigInteger nonce = null;
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   125
            while (value.data.available() > 0) {
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   126
                DerValue v = value.data.getDerValue();
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   127
                if (v.tag == DerValue.tag_Integer) {
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   128
                    nonce = v.getBigInteger();
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   129
                    System.err.println("nonce: " + nonce);
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   130
                } else if (v.tag == DerValue.tag_Boolean) {
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   131
                    System.err.println("certReq: " + v.getBoolean());
24034
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   132
                } else if (v.tag == DerValue.tag_ObjectId) {
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   133
                    policyId = v.getOID();
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   134
                    System.err.println("PolicyID: " + policyId);
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   135
                }
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   136
            }
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   137
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   138
            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
   139
            KeyStore ks = KeyStore.getInstance(
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   140
                    new File(keystore), "changeit".toCharArray());
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   141
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   142
            String alias = "ts";
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   143
            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
   144
                alias = "ts" + path;
28662
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   145
            }
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   146
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   147
            if (path.equals("diffpolicy")) {
24034
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   148
                policyId = new ObjectIdentifier(defaultPolicyId);
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   149
            }
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   150
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   151
            DerOutputStream statusInfo = new DerOutputStream();
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   152
            statusInfo.putInteger(0);
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   153
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   154
            AlgorithmId[] algorithms = {aid};
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   155
            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
   156
            X509Certificate[] signerCertificateChain;
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   157
            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
   158
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   159
            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
   160
                signerCertificateChain = new X509Certificate[chain.length];
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   161
                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
   162
                    signerCertificateChain[i] = (X509Certificate)chain[i];
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   163
                }
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   164
            } else if (path.equals("nocert")) {
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   165
                signerCertificateChain = new X509Certificate[0];
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   166
            } else {
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   167
                signerCertificateChain = new X509Certificate[1];
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   168
                signerCertificateChain[0] = (X509Certificate)chain[0];
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   169
            }
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   170
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   171
            DerOutputStream tst = new DerOutputStream();
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   172
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   173
            tst.putInteger(1);
24034
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   174
            tst.putOID(policyId);
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   175
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   176
            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
   177
                tst.putDerValue(messageImprint);
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   178
            } else {
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   179
                byte[] data = messageImprint.toByteArray();
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   180
                if (path.equals("diffalg")) {
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   181
                    data[6] = (byte)0x01;
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   182
                } else {
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   183
                    data[data.length-1] = (byte)0x01;
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   184
                    data[data.length-2] = (byte)0x02;
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   185
                    data[data.length-3] = (byte)0x03;
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   186
                }
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   187
                tst.write(data);
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   188
            }
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   189
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   190
            tst.putInteger(1);
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   191
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   192
            Calendar cal = Calendar.getInstance();
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   193
            tst.putGeneralizedTime(cal.getTime());
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   194
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   195
            if (path.equals("diffnonce")) {
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   196
                tst.putInteger(1234);
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   197
            } else if (path.equals("nononce")) {
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   198
                // no noce
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   199
            } else {
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   200
                tst.putInteger(nonce);
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
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   203
            DerOutputStream tstInfo = new DerOutputStream();
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   204
            tstInfo.write(DerValue.tag_Sequence, tst);
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   205
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   206
            DerOutputStream tstInfo2 = new DerOutputStream();
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   207
            tstInfo2.putOctetString(tstInfo.toByteArray());
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   208
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   209
            // 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
   210
            // so it is different from the hash algorithm.
21342
7bbb056a1c23 8027026: Change keytool -genkeypair to use -keyalg RSA
weijun
parents: 17161
diff changeset
   211
            Signature sig = Signature.getInstance("SHA1withRSA");
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   212
            sig.initSign((PrivateKey)(ks.getKey(
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   213
                    alias, "changeit".toCharArray())));
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   214
            sig.update(tstInfo.toByteArray());
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
            ContentInfo contentInfo = new ContentInfo(new ObjectIdentifier(
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   217
                    "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
   218
                    new DerValue(tstInfo2.toByteArray()));
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   219
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   220
            System.err.println("Signing...");
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   221
            System.err.println(new X500Name(signer
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   222
                    .getIssuerX500Principal().getName()));
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   223
            System.err.println(signer.getSerialNumber());
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   224
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   225
            SignerInfo signerInfo = new SignerInfo(
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   226
                    new X500Name(signer.getIssuerX500Principal().getName()),
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   227
                    signer.getSerialNumber(),
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   228
                    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
   229
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   230
            SignerInfo[] signerInfos = {signerInfo};
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   231
            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
   232
                    signerCertificateChain, signerInfos);
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   233
            ByteArrayOutputStream p7out = new ByteArrayOutputStream();
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   234
            p7.encodeSignedData(p7out);
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   235
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   236
            DerOutputStream response = new DerOutputStream();
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   237
            response.write(DerValue.tag_Sequence, statusInfo);
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   238
            response.putDerValue(new DerValue(p7out.toByteArray()));
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
            DerOutputStream out = new DerOutputStream();
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   241
            out.write(DerValue.tag_Sequence, response);
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   242
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   243
            return out.toByteArray();
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   244
        }
28662
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   245
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   246
        private Handler(HttpServer httpServer, String keystore) {
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   247
            this.httpServer = httpServer;
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   248
            this.keystore = keystore;
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   249
        }
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   250
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   251
        /**
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   252
         * Initialize TSA instance.
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   253
         *
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   254
         * Extended Key Info extension of certificate that is used for
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   255
         * signing TSA responses should contain timeStamping value.
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   256
         */
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   257
        static Handler init(int port, String keystore) throws IOException {
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   258
            HttpServer httpServer = HttpServer.create(
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   259
                    new InetSocketAddress(port), 0);
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   260
            Handler tsa = new Handler(httpServer, keystore);
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   261
            httpServer.createContext("/", tsa);
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   262
            return tsa;
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
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   265
        /**
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   266
         * Start TSA service.
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   267
         */
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   268
        void start() {
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   269
            httpServer.start();
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   270
        }
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   271
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   272
        /**
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   273
         * Stop TSA service.
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
        void stop() {
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   276
            httpServer.stop(0);
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
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   279
        /**
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   280
         * Return server port number.
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
        int getPort() {
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   283
            return httpServer.getAddress().getPort();
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
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   286
        @Override
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   287
        public void close() throws Exception {
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   288
            stop();
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   289
        }
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   290
    }
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   291
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   292
    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
   293
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   294
        prepare();
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   295
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   296
        try (Handler tsa = Handler.init(0, "tsks");) {
28662
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   297
            tsa.start();
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   298
            int port = tsa.getPort();
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   299
            host = "http://localhost:" + port + "/";
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
            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
   302
                sign("none")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   303
                        .shouldContain("is not timestamped")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   304
                        .shouldHaveExitValue(0);
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
                sign("badku")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   307
                        .shouldHaveExitValue(0);
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   308
                checkBadKU("badku.jar");
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   309
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   310
                sign("normal")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   311
                        .shouldNotContain("is not timestamped")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   312
                        .shouldHaveExitValue(0);
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   313
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   314
                sign("nononce")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   315
                        .shouldHaveExitValue(1);
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   316
                sign("diffnonce")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   317
                        .shouldHaveExitValue(1);
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   318
                sign("baddigest")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   319
                        .shouldHaveExitValue(1);
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   320
                sign("diffalg")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   321
                        .shouldHaveExitValue(1);
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   322
                sign("fullchain")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   323
                        .shouldHaveExitValue(0);   // Success, 6543440 solved.
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   324
                sign("bad1")
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("bad2")
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("bad3")
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("nocert")
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
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   333
                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
   334
                        .shouldHaveExitValue(0);
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   335
                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
   336
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   337
                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
   338
                        .shouldHaveExitValue(1);
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   339
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   340
                sign("tsaalg", "-tsadigestalg", "SHA")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   341
                        .shouldHaveExitValue(0);
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   342
                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
   343
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   344
                sign("weak", "-digestalg", "MD5",
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   345
                                "-sigalg", "MD5withRSA", "-tsadigestalg", "MD5")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   346
                        .shouldHaveExitValue(0)
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   347
                        .shouldMatch("MD5.*-digestalg.*risk")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   348
                        .shouldMatch("MD5.*-tsadigestalg.*risk")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   349
                        .shouldMatch("MD5withRSA.*-sigalg.*risk");
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   350
                checkWeak("weak.jar");
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   351
42180
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   352
                signWithAliasAndTsa("halfWeak", "old.jar", "old", "-digestalg", "MD5")
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   353
                        .shouldHaveExitValue(0);
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   354
                checkHalfWeak("halfWeak.jar");
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   355
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   356
                // sign with DSA key
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   357
                signWithAliasAndTsa("sign1", "old.jar", "dsakey")
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   358
                        .shouldHaveExitValue(0);
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   359
                // sign with RSAkeysize < 1024
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   360
                signWithAliasAndTsa("sign2", "sign1.jar", "weakkeysize")
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   361
                        .shouldHaveExitValue(0);
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   362
                checkMultiple("sign2.jar");
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   363
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   364
                // 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
   365
                checkMissingOrInvalidFiles("normal.jar");
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   366
            } else {                        // Run as a standalone server
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   367
                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
   368
                System.in.read();
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   369
            }
24034
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   370
        }
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   371
    }
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   372
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   373
    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
   374
            throws Throwable {
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   375
        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
   376
        verify("1.jar", "-verbose")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   377
                .shouldHaveExitValue(0)
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   378
                .shouldContain("treated as unsigned")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   379
                .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
   380
        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
   381
        verify("2.jar", "-verbose")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   382
                .shouldHaveExitValue(0)
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   383
                .shouldContain("treated as unsigned")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   384
                .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
   385
        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
   386
        verify("3.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("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
   390
        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
   391
        verify("4.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("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
   395
    }
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   396
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   397
    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
   398
            throws Throwable {
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   399
        JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("jarsigner")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   400
                .addVMArg("-Duser.language=en")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   401
                .addVMArg("-Duser.country=US")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   402
                .addToolArg("-keystore")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   403
                .addToolArg("tsks")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   404
                .addToolArg("-storepass")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   405
                .addToolArg("changeit");
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   406
        for (String s : extra) {
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   407
            if (s.startsWith("-J")) {
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   408
                launcher.addVMArg(s.substring(2));
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   409
            } else {
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   410
                launcher.addToolArg(s);
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   411
            }
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   412
        }
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   413
        return ProcessTools.executeCommand(launcher.getCommand());
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   414
    }
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   415
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   416
    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
   417
            throws Throwable {
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   418
        List<String> args = new ArrayList<>();
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   419
        args.add("-verify");
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   420
        args.add(file);
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   421
        args.addAll(Arrays.asList(extra));
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   422
        return jarsigner(args);
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   423
    }
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
    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
   426
        verify(file)
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   427
                .shouldHaveExitValue(0)
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   428
                .shouldContain("treated as unsigned")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   429
                .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
   430
        verify(file, "-verbose")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   431
                .shouldHaveExitValue(0)
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   432
                .shouldContain("Signed by")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   433
                .shouldContain("treated as unsigned")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   434
                .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
   435
        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
   436
                .shouldHaveExitValue(0)
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   437
                .shouldContain("SignatureException: Key usage restricted")
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
    }
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   441
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   442
    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
   443
        verify(file)
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   444
                .shouldHaveExitValue(0)
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   445
                .shouldContain("treated as unsigned")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   446
                .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
   447
                .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
   448
        verify(file, "-verbose")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   449
                .shouldHaveExitValue(0)
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   450
                .shouldContain("treated as unsigned")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   451
                .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
   452
                .shouldMatch("Digest algorithm: .*weak")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   453
                .shouldMatch("Signature algorithm: .*weak")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   454
                .shouldMatch("Timestamp digest algorithm: .*weak")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   455
                .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
   456
                .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
   457
        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
   458
                .shouldHaveExitValue(0)
43701
fe8c324ba97c 8160655: Fix denyAfter and usage types for security properties
ascarpino
parents: 42180
diff changeset
   459
                .shouldMatch("SignatureException:.*disabled");
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   460
    }
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   461
42180
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   462
    static void checkHalfWeak(String file) throws Throwable {
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   463
        verify(file)
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   464
                .shouldHaveExitValue(0)
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   465
                .shouldContain("treated as unsigned")
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   466
                .shouldMatch("weak algorithm that is now disabled.")
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   467
                .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
   468
        verify(file, "-verbose")
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   469
                .shouldHaveExitValue(0)
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   470
                .shouldContain("treated as unsigned")
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   471
                .shouldMatch("weak algorithm that is now disabled by")
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   472
                .shouldMatch("Digest algorithm: .*weak")
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   473
                .shouldNotMatch("Signature algorithm: .*weak")
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   474
                .shouldNotMatch("Timestamp digest algorithm: .*weak")
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   475
                .shouldNotMatch("Timestamp signature algorithm: .*weak.*weak")
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   476
                .shouldNotMatch("Timestamp signature algorithm: .*key.*weak");
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   477
     }
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   478
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   479
    static void checkMultiple(String file) throws Throwable {
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   480
        verify(file)
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   481
                .shouldHaveExitValue(0)
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   482
                .shouldContain("jar verified");
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   483
        verify(file, "-verbose", "-certs")
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   484
                .shouldHaveExitValue(0)
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   485
                .shouldContain("jar verified")
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   486
                .shouldMatch("X.509.*CN=dsakey")
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   487
                .shouldNotMatch("X.509.*CN=weakkeysize")
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   488
                .shouldMatch("Signed by .*CN=dsakey")
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   489
                .shouldMatch("Signed by .*CN=weakkeysize")
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   490
                .shouldMatch("Signature algorithm: .*key.*weak");
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   491
     }
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   492
24034
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   493
    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
   494
            throws Exception {
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   495
        try (JarFile jf = new JarFile(file)) {
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   496
            JarEntry je = jf.getJarEntry("META-INF/OLD.RSA");
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   497
            try (InputStream is = jf.getInputStream(je)) {
32933
83d50f1247bb 8138978: Examine usages of sun.misc.IOUtils
chegar
parents: 28664
diff changeset
   498
                byte[] content = is.readAllBytes();
24034
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   499
                PKCS7 p7 = new PKCS7(content);
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   500
                SignerInfo[] si = p7.getSignerInfos();
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   501
                if (si == null || si.length == 0) {
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   502
                    throw new Exception("Not signed");
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   503
                }
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   504
                PKCS9Attribute p9 = si[0].getUnauthenticatedAttributes()
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   505
                        .getAttribute(PKCS9Attribute.SIGNATURE_TIMESTAMP_TOKEN_OID);
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   506
                PKCS7 tsToken = new PKCS7((byte[]) p9.getValue());
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   507
                TimestampToken tt =
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   508
                        new TimestampToken(tsToken.getContentInfo().getData());
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   509
                if (!tt.getHashAlgorithm().toString().equals(digestAlg)) {
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   510
                    throw new Exception("Digest alg different");
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   511
                }
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   512
                if (!tt.getPolicyID().equals(policyId)) {
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   513
                    throw new Exception("policyId different");
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   514
                }
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   515
            }
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   516
        }
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   517
    }
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   518
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   519
    static int which = 0;
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   520
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   521
    /**
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   522
     * @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
   523
     */
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   524
    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
   525
            throws Throwable {
42180
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   526
        String alias = path.equals("badku") ? "badku" : "old";
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   527
        return signWithAliasAndTsa(path, "old.jar", alias, extra);
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   528
    }
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   529
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   530
    static OutputAnalyzer signWithAliasAndTsa (String path, String jar,
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   531
            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
   532
        which++;
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   533
        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
   534
        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
   535
                "-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
   536
        args = new ArrayList<>(args);
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   537
        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
   538
            args.add("-tsa");
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   539
            args.add(host + path);
5297
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
        args.addAll(Arrays.asList(extra));
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   542
        return jarsigner(args);
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   543
    }
22315
529f1cc322fc 8024302: Clarify jar verifications
weijun
parents: 17161
diff changeset
   544
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   545
    static void prepare() throws Exception {
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   546
        jdk.testlibrary.JarUtils.createJar("old.jar", "A");
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   547
        Files.deleteIfExists(Paths.get("tsks"));
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   548
        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
   549
        keytool("-alias old -genkeypair -dname CN=old");
42180
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   550
        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
   551
        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
   552
        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
   553
        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
   554
        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
   555
        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
   556
        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
   557
        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
   558
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   559
        gencert("old");
42180
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   560
        gencert("dsakey");
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   561
        gencert("weakkeysize");
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   562
        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
   563
        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
   564
        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
   565
        gencert("tsbad1");
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   566
        gencert("tsbad2", "-ext eku=ts");
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   567
        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
   568
    }
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   569
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   570
    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
   571
        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
   572
        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
   573
                alias + ".req -outfile " + alias + ".cert";
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   574
        for (String s : extra) {
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   575
            genCmd += " " + s;
22315
529f1cc322fc 8024302: Clarify jar verifications
weijun
parents: 17161
diff changeset
   576
        }
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   577
        keytool(genCmd);
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   578
        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
   579
    }
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
    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
   582
        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
   583
                "-keyalg rsa -validity 200 " + cmd;
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   584
        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
   585
    }
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   586
}