test/jdk/sun/security/tools/jarsigner/TimestampCheck.java
author weijun
Wed, 14 Feb 2018 16:58:49 +0800
changeset 48893 454518b338b0
parent 48062 f14a1972f35d
permissions -rw-r--r--
8191438: jarsigner should print when a timestamp will expire Reviewed-by: mullan
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
/*
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
     2
 * Copyright (c) 2003, 2018, 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.*;
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
    25
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
    26
import java.io.ByteArrayInputStream;
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    27
import java.io.ByteArrayOutputStream;
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
    28
import java.io.File;
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    29
import java.io.IOException;
24034
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
    30
import java.io.InputStream;
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    31
import java.io.OutputStream;
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    32
import java.math.BigInteger;
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    33
import java.net.InetSocketAddress;
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
    34
import java.nio.file.Files;
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
    35
import java.nio.file.Paths;
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    36
import java.security.KeyStore;
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    37
import java.security.PrivateKey;
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    38
import java.security.Signature;
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    39
import java.security.cert.Certificate;
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
    40
import java.security.cert.CertificateException;
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
    41
import java.security.cert.CertificateFactory;
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    42
import java.security.cert.X509Certificate;
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
    43
import java.time.Instant;
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
    44
import java.time.temporal.ChronoUnit;
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
    45
import java.util.*;
24034
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
    46
import java.util.jar.JarEntry;
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
    47
import java.util.jar.JarFile;
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
    48
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43701
diff changeset
    49
import jdk.test.lib.SecurityTools;
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
    50
import jdk.test.lib.process.OutputAnalyzer;
45287
e0bb5f83e17a 8180888: move jdk.testlibrary.JarUtils to the top level testlibrary
iignatyev
parents: 44046
diff changeset
    51
import jdk.test.lib.util.JarUtils;
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    52
import sun.security.pkcs.ContentInfo;
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    53
import sun.security.pkcs.PKCS7;
24034
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
    54
import sun.security.pkcs.PKCS9Attribute;
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    55
import sun.security.pkcs.SignerInfo;
24034
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
    56
import sun.security.timestamp.TimestampToken;
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    57
import sun.security.util.DerOutputStream;
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    58
import sun.security.util.DerValue;
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    59
import sun.security.util.ObjectIdentifier;
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    60
import sun.security.x509.AlgorithmId;
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    61
import sun.security.x509.X500Name;
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    62
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
    63
/*
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
    64
 * @test
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
    65
 * @bug 6543842 6543440 6939248 8009636 8024302 8163304 8169911 8180289
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
    66
 * @summary checking response of timestamp
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
    67
 * @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
    68
 *          java.base/sun.security.timestamp
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
    69
 *          java.base/sun.security.x509
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
    70
 *          java.base/sun.security.util
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
    71
 *          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
    72
 * @library /lib/testlibrary
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43701
diff changeset
    73
 * @library /test/lib
45467
99c87a16a8e4 8181761: add explicit @build actions for jdk.test.lib classes in all :tier2 tests
iignatyev
parents: 45287
diff changeset
    74
 * @build jdk.test.lib.util.JarUtils
99c87a16a8e4 8181761: add explicit @build actions for jdk.test.lib classes in all :tier2 tests
iignatyev
parents: 45287
diff changeset
    75
 *        jdk.test.lib.SecurityTools
99c87a16a8e4 8181761: add explicit @build actions for jdk.test.lib classes in all :tier2 tests
iignatyev
parents: 45287
diff changeset
    76
 *        jdk.test.lib.Utils
99c87a16a8e4 8181761: add explicit @build actions for jdk.test.lib classes in all :tier2 tests
iignatyev
parents: 45287
diff changeset
    77
 *        jdk.test.lib.Asserts
99c87a16a8e4 8181761: add explicit @build actions for jdk.test.lib classes in all :tier2 tests
iignatyev
parents: 45287
diff changeset
    78
 *        jdk.test.lib.JDKToolFinder
99c87a16a8e4 8181761: add explicit @build actions for jdk.test.lib classes in all :tier2 tests
iignatyev
parents: 45287
diff changeset
    79
 *        jdk.test.lib.JDKToolLauncher
99c87a16a8e4 8181761: add explicit @build actions for jdk.test.lib classes in all :tier2 tests
iignatyev
parents: 45287
diff changeset
    80
 *        jdk.test.lib.Platform
99c87a16a8e4 8181761: add explicit @build actions for jdk.test.lib classes in all :tier2 tests
iignatyev
parents: 45287
diff changeset
    81
 *        jdk.test.lib.process.*
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
    82
 * @compile -XDignore.symbol.file TimestampCheck.java
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
    83
 * @run main/timeout=600 TimestampCheck
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
    84
 */
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    85
public class TimestampCheck {
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    86
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
    87
    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
    88
    static String host = null;
24034
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
    89
28662
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
    90
    static class Handler implements HttpHandler, AutoCloseable {
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
    91
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
    92
        private final HttpServer httpServer;
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
    93
        private final String keystore;
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
    94
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
    95
        @Override
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    96
        public void handle(HttpExchange t) throws IOException {
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    97
            int len = 0;
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    98
            for (String h: t.getRequestHeaders().keySet()) {
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
    99
                if (h.equalsIgnoreCase("Content-length")) {
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   100
                    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
   101
                }
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   102
            }
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   103
            byte[] input = new byte[len];
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   104
            t.getRequestBody().read(input);
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   105
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   106
            try {
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   107
                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
   108
                byte[] output = sign(input, path);
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   109
                Headers out = t.getResponseHeaders();
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   110
                out.set("Content-Type", "application/timestamp-reply");
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   111
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   112
                t.sendResponseHeaders(200, output.length);
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   113
                OutputStream os = t.getResponseBody();
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   114
                os.write(output);
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   115
            } catch (Exception e) {
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   116
                e.printStackTrace();
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   117
                t.sendResponseHeaders(500, 0);
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   118
            }
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   119
            t.close();
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   120
        }
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   121
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   122
        /**
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   123
         * @param input The data to sign
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   124
         * @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
   125
         * @returns the signed
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   126
         */
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   127
        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
   128
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   129
            DerValue value = new DerValue(input);
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   130
            System.out.println("#\n# Incoming Request\n===================");
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   131
            System.out.println("# Version: " + value.data.getInteger());
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   132
            DerValue messageImprint = value.data.getDerValue();
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   133
            AlgorithmId aid = AlgorithmId.parse(
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   134
                    messageImprint.data.getDerValue());
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   135
            System.out.println("# AlgorithmId: " + aid);
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   136
24034
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   137
            ObjectIdentifier policyId = new ObjectIdentifier(defaultPolicyId);
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   138
            BigInteger nonce = null;
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   139
            while (value.data.available() > 0) {
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   140
                DerValue v = value.data.getDerValue();
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   141
                if (v.tag == DerValue.tag_Integer) {
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   142
                    nonce = v.getBigInteger();
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   143
                    System.out.println("# nonce: " + nonce);
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   144
                } else if (v.tag == DerValue.tag_Boolean) {
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   145
                    System.out.println("# certReq: " + v.getBoolean());
24034
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   146
                } else if (v.tag == DerValue.tag_ObjectId) {
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   147
                    policyId = v.getOID();
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   148
                    System.out.println("# PolicyID: " + policyId);
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   149
                }
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   150
            }
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   151
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   152
            System.out.println("#\n# Response\n===================");
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   153
            KeyStore ks = KeyStore.getInstance(
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   154
                    new File(keystore), "changeit".toCharArray());
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   155
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   156
            // If path starts with "ts", use the TSA it points to.
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   157
            // Otherwise, always use "ts".
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   158
            String alias = path.startsWith("ts") ? path : "ts";
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   159
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   160
            if (path.equals("diffpolicy")) {
24034
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   161
                policyId = new ObjectIdentifier(defaultPolicyId);
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   162
            }
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   163
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   164
            DerOutputStream statusInfo = new DerOutputStream();
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   165
            statusInfo.putInteger(0);
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   166
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   167
            AlgorithmId[] algorithms = {aid};
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   168
            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
   169
            X509Certificate[] signerCertificateChain;
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   170
            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
   171
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   172
            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
   173
                signerCertificateChain = new X509Certificate[chain.length];
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   174
                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
   175
                    signerCertificateChain[i] = (X509Certificate)chain[i];
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   176
                }
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   177
            } else if (path.equals("nocert")) {
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   178
                signerCertificateChain = new X509Certificate[0];
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   179
            } else {
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   180
                signerCertificateChain = new X509Certificate[1];
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   181
                signerCertificateChain[0] = (X509Certificate)chain[0];
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   182
            }
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   183
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   184
            DerOutputStream tst = new DerOutputStream();
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   185
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   186
            tst.putInteger(1);
24034
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   187
            tst.putOID(policyId);
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   188
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   189
            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
   190
                tst.putDerValue(messageImprint);
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   191
            } else {
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   192
                byte[] data = messageImprint.toByteArray();
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   193
                if (path.equals("diffalg")) {
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   194
                    data[6] = (byte)0x01;
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   195
                } else {
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   196
                    data[data.length-1] = (byte)0x01;
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   197
                    data[data.length-2] = (byte)0x02;
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   198
                    data[data.length-3] = (byte)0x03;
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   199
                }
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   200
                tst.write(data);
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
            tst.putInteger(1);
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   204
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   205
            Instant instant = Instant.now();
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   206
            if (path.equals("tsold")) {
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   207
                instant = instant.minus(20, ChronoUnit.DAYS);
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   208
            }
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   209
            tst.putGeneralizedTime(Date.from(instant));
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   210
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   211
            if (path.equals("diffnonce")) {
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   212
                tst.putInteger(1234);
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   213
            } else if (path.equals("nononce")) {
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   214
                // no noce
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   215
            } else {
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   216
                tst.putInteger(nonce);
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   217
            }
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   218
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   219
            DerOutputStream tstInfo = new DerOutputStream();
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   220
            tstInfo.write(DerValue.tag_Sequence, tst);
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   221
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   222
            DerOutputStream tstInfo2 = new DerOutputStream();
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   223
            tstInfo2.putOctetString(tstInfo.toByteArray());
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   224
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   225
            // 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
   226
            // so it is different from the hash algorithm.
21342
7bbb056a1c23 8027026: Change keytool -genkeypair to use -keyalg RSA
weijun
parents: 17161
diff changeset
   227
            Signature sig = Signature.getInstance("SHA1withRSA");
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   228
            sig.initSign((PrivateKey)(ks.getKey(
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   229
                    alias, "changeit".toCharArray())));
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   230
            sig.update(tstInfo.toByteArray());
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   231
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   232
            ContentInfo contentInfo = new ContentInfo(new ObjectIdentifier(
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   233
                    "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
   234
                    new DerValue(tstInfo2.toByteArray()));
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   235
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   236
            System.out.println("# Signing...");
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   237
            System.out.println("# " + new X500Name(signer
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   238
                    .getIssuerX500Principal().getName()));
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   239
            System.out.println("# " + signer.getSerialNumber());
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   240
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   241
            SignerInfo signerInfo = new SignerInfo(
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   242
                    new X500Name(signer.getIssuerX500Principal().getName()),
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   243
                    signer.getSerialNumber(),
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   244
                    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
   245
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   246
            SignerInfo[] signerInfos = {signerInfo};
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   247
            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
   248
                    signerCertificateChain, signerInfos);
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   249
            ByteArrayOutputStream p7out = new ByteArrayOutputStream();
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   250
            p7.encodeSignedData(p7out);
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   251
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   252
            DerOutputStream response = new DerOutputStream();
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   253
            response.write(DerValue.tag_Sequence, statusInfo);
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   254
            response.putDerValue(new DerValue(p7out.toByteArray()));
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   255
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   256
            DerOutputStream out = new DerOutputStream();
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   257
            out.write(DerValue.tag_Sequence, response);
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   258
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   259
            return out.toByteArray();
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   260
        }
28662
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   261
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   262
        private Handler(HttpServer httpServer, String keystore) {
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   263
            this.httpServer = httpServer;
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   264
            this.keystore = keystore;
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
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
         * Initialize TSA instance.
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   269
         *
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   270
         * Extended Key Info extension of certificate that is used for
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   271
         * signing TSA responses should contain timeStamping value.
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
        static Handler init(int port, String keystore) throws IOException {
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   274
            HttpServer httpServer = HttpServer.create(
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   275
                    new InetSocketAddress(port), 0);
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   276
            Handler tsa = new Handler(httpServer, keystore);
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   277
            httpServer.createContext("/", tsa);
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   278
            return tsa;
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
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
         * Start TSA service.
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   283
         */
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   284
        void start() {
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   285
            httpServer.start();
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   286
        }
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   287
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   288
        /**
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   289
         * Stop TSA service.
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   290
         */
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   291
        void stop() {
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   292
            httpServer.stop(0);
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   293
        }
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   294
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   295
        /**
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   296
         * Return server port number.
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   297
         */
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   298
        int getPort() {
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   299
            return httpServer.getAddress().getPort();
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   300
        }
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   301
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   302
        @Override
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   303
        public void close() throws Exception {
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   304
            stop();
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   305
        }
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   306
    }
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   307
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   308
    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
   309
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   310
        try (Handler tsa = Handler.init(0, "ks");) {
28662
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   311
            tsa.start();
efd0203db371 8049171: Additional tests for jarsigner's warnings
asmotrak
parents: 24116
diff changeset
   312
            int port = tsa.getPort();
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   313
            host = "http://localhost:" + port + "/";
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   314
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   315
            if (args.length == 0) {         // Run this test
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   316
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   317
                prepare();
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   318
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   319
                sign("normal")
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   320
                        .shouldNotContain("Warning")
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   321
                        .shouldContain("The signer certificate will expire on")
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   322
                        .shouldContain("The timestamp will expire on")
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   323
                        .shouldHaveExitValue(0);
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   324
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   325
                verify("normal.jar")
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   326
                        .shouldNotContain("Warning")
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   327
                        .shouldHaveExitValue(0);
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   328
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   329
                verify("normal.jar", "-verbose")
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   330
                        .shouldNotContain("Warning")
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   331
                        .shouldContain("The signer certificate will expire on")
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   332
                        .shouldContain("The timestamp will expire on")
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   333
                        .shouldHaveExitValue(0);
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   334
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   335
                // Simulate signing at a previous date:
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   336
                // 1. tsold will create a timestamp of 20 days ago.
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   337
                // 2. oldsigner expired 10 days ago.
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   338
                signVerbose("tsold", "unsigned.jar", "tsold.jar", "oldsigner")
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   339
                        .shouldNotContain("Warning")
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   340
                        .shouldMatch("signer certificate expired on .*. "
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   341
                                + "However, the JAR will be valid")
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   342
                        .shouldHaveExitValue(0);
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   343
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   344
                // It verifies perfectly.
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   345
                verify("tsold.jar", "-verbose", "-certs")
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   346
                        .shouldNotContain("Warning")
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   347
                        .shouldMatch("signer certificate expired on .*. "
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   348
                                + "However, the JAR will be valid")
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   349
                        .shouldHaveExitValue(0);
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   350
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   351
                // No timestamp
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   352
                signVerbose(null, "unsigned.jar", "none.jar", "signer")
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   353
                        .shouldContain("is not timestamped")
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   354
                        .shouldContain("The signer certificate will expire on")
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   355
                        .shouldHaveExitValue(0);
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   356
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   357
                verify("none.jar", "-verbose")
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   358
                        .shouldContain("do not include a timestamp")
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   359
                        .shouldContain("The signer certificate will expire on")
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   360
                        .shouldHaveExitValue(0);
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   361
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   362
                // Error cases
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   363
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   364
                signVerbose(null, "unsigned.jar", "badku.jar", "badku")
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   365
                        .shouldContain("KeyUsage extension doesn't allow code signing")
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   366
                        .shouldHaveExitValue(8);
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   367
                checkBadKU("badku.jar");
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   368
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   369
                // 8180289: unvalidated TSA cert chain
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   370
                sign("tsnoca")
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   371
                        .shouldContain("The TSA certificate chain is invalid. "
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   372
                                + "Reason: Path does not chain with any of the trust anchors")
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   373
                        .shouldHaveExitValue(64);
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   374
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   375
                verify("tsnoca.jar", "-verbose", "-certs")
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   376
                        .shouldHaveExitValue(64)
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   377
                        .shouldContain("jar verified")
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   378
                        .shouldContain("Invalid TSA certificate chain: "
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   379
                                + "Path does not chain with any of the trust anchors")
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   380
                        .shouldContain("TSA certificate chain is invalid."
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   381
                                + " Reason: Path does not chain with any of the trust anchors");
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   382
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   383
                sign("nononce")
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   384
                        .shouldContain("Nonce missing in timestamp token")
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   385
                        .shouldHaveExitValue(1);
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   386
                sign("diffnonce")
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   387
                        .shouldContain("Nonce changed in timestamp token")
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   388
                        .shouldHaveExitValue(1);
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   389
                sign("baddigest")
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   390
                        .shouldContain("Digest octets changed in timestamp token")
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   391
                        .shouldHaveExitValue(1);
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   392
                sign("diffalg")
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   393
                        .shouldContain("Digest algorithm not")
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   394
                        .shouldHaveExitValue(1);
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   395
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   396
                sign("fullchain")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   397
                        .shouldHaveExitValue(0);   // Success, 6543440 solved.
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   398
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   399
                sign("tsbad1")
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   400
                        .shouldContain("Certificate is not valid for timestamping")
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   401
                        .shouldHaveExitValue(1);
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   402
                sign("tsbad2")
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   403
                        .shouldContain("Certificate is not valid for timestamping")
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   404
                        .shouldHaveExitValue(1);
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   405
                sign("tsbad3")
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   406
                        .shouldContain("Certificate is not valid for timestamping")
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   407
                        .shouldHaveExitValue(1);
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   408
                sign("nocert")
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   409
                        .shouldContain("Certificate not included in timestamp token")
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   410
                        .shouldHaveExitValue(1);
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
                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
   413
                        .shouldHaveExitValue(0);
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   414
                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
   415
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   416
                sign("diffpolicy", "-tsapolicyid", "1.2.3")
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   417
                        .shouldContain("TSAPolicyID changed in timestamp token")
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   418
                        .shouldHaveExitValue(1);
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   419
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   420
                sign("sha1alg", "-tsadigestalg", "SHA")
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   421
                        .shouldHaveExitValue(0);
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   422
                checkTimestamp("sha1alg.jar", defaultPolicyId, "SHA-1");
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   423
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   424
                sign("tsweak", "-digestalg", "MD5",
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   425
                                "-sigalg", "MD5withRSA", "-tsadigestalg", "MD5")
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   426
                        .shouldHaveExitValue(68)
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   427
                        .shouldContain("The timestamp is invalid. Without a valid timestamp")
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   428
                        .shouldMatch("MD5.*-digestalg.*risk")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   429
                        .shouldMatch("MD5.*-tsadigestalg.*risk")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   430
                        .shouldMatch("MD5withRSA.*-sigalg.*risk");
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   431
                checkWeak("tsweak.jar");
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   432
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   433
                signVerbose("tsweak", "unsigned.jar", "tsweak2.jar", "signer")
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   434
                        .shouldHaveExitValue(64)
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   435
                        .shouldContain("The timestamp is invalid. Without a valid timestamp")
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   436
                        .shouldContain("TSA certificate chain is invalid");
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   437
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   438
                // Weak timestamp is an error and jar treated unsigned
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   439
                verify("tsweak2.jar", "-verbose")
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   440
                        .shouldHaveExitValue(16)
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   441
                        .shouldContain("treated as unsigned")
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   442
                        .shouldMatch("Timestamp.*512.*weak");
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   443
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   444
                // Algorithm used in signing is weak
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   445
                signVerbose("normal", "unsigned.jar", "halfWeak.jar", "signer",
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   446
                        "-digestalg", "MD5")
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   447
                        .shouldContain("-digestalg option is considered a security risk")
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   448
                        .shouldHaveExitValue(4);
42180
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   449
                checkHalfWeak("halfWeak.jar");
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   450
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   451
                // sign with DSA key
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   452
                signVerbose("normal", "unsigned.jar", "sign1.jar", "dsakey")
42180
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   453
                        .shouldHaveExitValue(0);
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   454
42180
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   455
                // sign with RSAkeysize < 1024
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   456
                signVerbose("normal", "sign1.jar", "sign2.jar", "weakkeysize")
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   457
                        .shouldContain("Algorithm constraints check failed on keysize")
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   458
                        .shouldHaveExitValue(4);
42180
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   459
                checkMultiple("sign2.jar");
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   460
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   461
                // 8191438: jarsigner should print when a timestamp will expire
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   462
                checkExpiration();
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   463
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   464
                // 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
   465
                checkMissingOrInvalidFiles("normal.jar");
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   466
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   467
                if (Files.exists(Paths.get("ts2.cert"))) {
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   468
                    checkInvalidTsaCertKeyUsage();
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   469
                }
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   470
            } else {                        // Run as a standalone server
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   471
                System.out.println("TSA started at " + host
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   472
                        + ". Press Enter to quit server");
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   473
                System.in.read();
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   474
            }
24034
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   475
        }
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   476
    }
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   477
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   478
    private static void checkExpiration() throws Exception {
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   479
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   480
        // Warning when expired or expiring
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   481
        signVerbose(null, "unsigned.jar", "expired.jar", "expired")
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   482
                .shouldContain("signer certificate has expired")
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   483
                .shouldHaveExitValue(4);
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   484
        verify("expired.jar")
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   485
                .shouldContain("signer certificate has expired")
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   486
                .shouldHaveExitValue(4);
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   487
        signVerbose(null, "unsigned.jar", "expiring.jar", "expiring")
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   488
                .shouldContain("signer certificate will expire within")
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   489
                .shouldHaveExitValue(0);
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   490
        verify("expiring.jar")
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   491
                .shouldContain("signer certificate will expire within")
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   492
                .shouldHaveExitValue(0);
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   493
        // Info for long
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   494
        signVerbose(null, "unsigned.jar", "long.jar", "long")
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   495
                .shouldNotContain("signer certificate has expired")
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   496
                .shouldNotContain("signer certificate will expire within")
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   497
                .shouldContain("signer certificate will expire on")
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   498
                .shouldHaveExitValue(0);
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   499
        verify("long.jar")
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   500
                .shouldNotContain("signer certificate has expired")
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   501
                .shouldNotContain("signer certificate will expire within")
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   502
                .shouldNotContain("The signer certificate will expire")
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   503
                .shouldHaveExitValue(0);
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   504
        verify("long.jar", "-verbose")
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   505
                .shouldContain("The signer certificate will expire")
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   506
                .shouldHaveExitValue(0);
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   507
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   508
        // Both expired
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   509
        signVerbose("tsexpired", "unsigned.jar",
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   510
                "tsexpired-expired.jar", "expired")
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   511
                .shouldContain("The signer certificate has expired.")
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   512
                .shouldContain("The timestamp has expired.")
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   513
                .shouldHaveExitValue(4);
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   514
        verify("tsexpired-expired.jar")
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   515
                .shouldContain("signer certificate has expired")
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   516
                .shouldContain("timestamp has expired.")
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   517
                .shouldHaveExitValue(4);
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   518
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   519
        // TS expired but signer still good
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   520
        signVerbose("tsexpired", "unsigned.jar",
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   521
                "tsexpired-long.jar", "long")
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   522
                .shouldContain("The timestamp expired on")
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   523
                .shouldHaveExitValue(0);
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   524
        verify("tsexpired-long.jar")
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   525
                .shouldMatch("timestamp expired on.*However, the JAR will be valid")
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   526
                .shouldNotContain("Error")
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   527
                .shouldHaveExitValue(0);
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   528
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   529
        signVerbose("tsexpired", "unsigned.jar",
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   530
                "tsexpired-ca.jar", "ca")
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   531
                .shouldContain("The timestamp has expired.")
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   532
                .shouldHaveExitValue(4);
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   533
        verify("tsexpired-ca.jar")
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   534
                .shouldNotContain("timestamp has expired")
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   535
                .shouldNotContain("Error")
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   536
                .shouldHaveExitValue(0);
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   537
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   538
        // Warning when expiring
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   539
        sign("tsexpiring")
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   540
                .shouldContain("timestamp will expire within")
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   541
                .shouldHaveExitValue(0);
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   542
        verify("tsexpiring.jar")
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   543
                .shouldContain("timestamp will expire within")
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   544
                .shouldNotContain("still valid")
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   545
                .shouldHaveExitValue(0);
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   546
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   547
        signVerbose("tsexpiring", "unsigned.jar",
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   548
                "tsexpiring-ca.jar", "ca")
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   549
                .shouldContain("self-signed")
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   550
                .stderrShouldNotMatch("The.*expir")
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   551
                .shouldHaveExitValue(4); // self-signed
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   552
        verify("tsexpiring-ca.jar")
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   553
                .stderrShouldNotMatch("The.*expir")
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   554
                .shouldHaveExitValue(0);
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   555
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   556
        signVerbose("tsexpiringsoon", "unsigned.jar",
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   557
                "tsexpiringsoon-long.jar", "long")
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   558
                .shouldContain("The timestamp will expire")
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   559
                .shouldHaveExitValue(0);
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   560
        verify("tsexpiringsoon-long.jar")
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   561
                .shouldMatch("timestamp will expire.*However, the JAR will be valid until")
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   562
                .shouldHaveExitValue(0);
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   563
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   564
        // Info for long
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   565
        sign("tslong")
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   566
                .shouldNotContain("timestamp has expired")
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   567
                .shouldNotContain("timestamp will expire within")
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   568
                .shouldContain("timestamp will expire on")
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   569
                .shouldContain("signer certificate will expire on")
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   570
                .shouldHaveExitValue(0);
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   571
        verify("tslong.jar")
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   572
                .shouldNotContain("timestamp has expired")
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   573
                .shouldNotContain("timestamp will expire within")
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   574
                .shouldNotContain("timestamp will expire on")
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   575
                .shouldNotContain("signer certificate will expire on")
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   576
                .shouldHaveExitValue(0);
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   577
        verify("tslong.jar", "-verbose")
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   578
                .shouldContain("timestamp will expire on")
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   579
                .shouldContain("signer certificate will expire on")
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   580
                .shouldHaveExitValue(0);
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   581
    }
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   582
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   583
    private static void checkInvalidTsaCertKeyUsage() throws Exception {
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   584
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   585
        // Hack: Rewrite the TSA cert inside normal.jar into ts2.jar.
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   586
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   587
        // Both the cert and the serial number must be rewritten.
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   588
        byte[] tsCert = Files.readAllBytes(Paths.get("ts.cert"));
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   589
        byte[] ts2Cert = Files.readAllBytes(Paths.get("ts2.cert"));
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   590
        byte[] tsSerial = getCert(tsCert)
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   591
                .getSerialNumber().toByteArray();
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   592
        byte[] ts2Serial = getCert(ts2Cert)
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   593
                .getSerialNumber().toByteArray();
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   594
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   595
        byte[] oldBlock;
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   596
        try (JarFile normal = new JarFile("normal.jar")) {
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   597
            oldBlock = normal.getInputStream(
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   598
                    normal.getJarEntry("META-INF/SIGNER.RSA")).readAllBytes();
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   599
        }
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   600
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   601
        JarUtils.updateJar("normal.jar", "ts2.jar",
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   602
                Map.of("META-INF/SIGNER.RSA",
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   603
                        updateBytes(updateBytes(oldBlock, tsCert, ts2Cert),
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   604
                                tsSerial, ts2Serial)));
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   605
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   606
        verify("ts2.jar", "-verbose", "-certs")
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   607
                .shouldHaveExitValue(64)
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   608
                .shouldContain("jar verified")
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   609
                .shouldContain("Invalid TSA certificate chain: Extended key usage does not permit use for TSA server");
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   610
    }
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   611
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   612
    public static X509Certificate getCert(byte[] data)
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   613
            throws CertificateException, IOException {
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   614
        return (X509Certificate)
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   615
                CertificateFactory.getInstance("X.509")
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   616
                        .generateCertificate(new ByteArrayInputStream(data));
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   617
    }
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   618
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   619
    private static byte[] updateBytes(byte[] old, byte[] from, byte[] to) {
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   620
        int pos = 0;
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   621
        while (true) {
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   622
            if (pos + from.length > old.length) {
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   623
                return null;
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   624
            }
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   625
            if (Arrays.equals(Arrays.copyOfRange(old, pos, pos+from.length), from)) {
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   626
                byte[] result = old.clone();
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   627
                System.arraycopy(to, 0, result, pos, from.length);
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   628
                return result;
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   629
            }
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   630
            pos++;
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   631
        }
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   632
    }
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   633
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   634
    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
   635
            throws Throwable {
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   636
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   637
        JarUtils.updateJar(s, "1.jar", Map.of("META-INF/SIGNER.SF", Boolean.FALSE));
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   638
        verify("1.jar", "-verbose")
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   639
                .shouldHaveExitValue(16)
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   640
                .shouldContain("treated as unsigned")
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   641
                .shouldContain("Missing signature-related file META-INF/SIGNER.SF");
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   642
        JarUtils.updateJar(s, "2.jar", Map.of("META-INF/SIGNER.RSA", Boolean.FALSE));
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   643
        verify("2.jar", "-verbose")
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   644
                .shouldHaveExitValue(16)
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   645
                .shouldContain("treated as unsigned")
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   646
                .shouldContain("Missing block file for signature-related file META-INF/SIGNER.SF");
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   647
        JarUtils.updateJar(s, "3.jar", Map.of("META-INF/SIGNER.SF", "dummy"));
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   648
        verify("3.jar", "-verbose")
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   649
                .shouldHaveExitValue(16)
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   650
                .shouldContain("treated as unsigned")
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   651
                .shouldContain("Unparsable signature-related file META-INF/SIGNER.SF");
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   652
        JarUtils.updateJar(s, "4.jar", Map.of("META-INF/SIGNER.RSA", "dummy"));
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   653
        verify("4.jar", "-verbose")
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   654
                .shouldHaveExitValue(16)
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   655
                .shouldContain("treated as unsigned")
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   656
                .shouldContain("Unparsable signature-related file META-INF/SIGNER.RSA");
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   657
    }
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   658
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   659
    static OutputAnalyzer jarsigner(List<String> extra)
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   660
            throws Exception {
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   661
        List<String> args = new ArrayList<>(
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   662
                List.of("-keystore", "ks", "-storepass", "changeit"));
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   663
        args.addAll(extra);
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   664
        return SecurityTools.jarsigner(args);
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   665
    }
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   666
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   667
    static OutputAnalyzer verify(String file, String... extra)
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   668
            throws Exception {
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   669
        List<String> args = new ArrayList<>();
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   670
        args.add("-verify");
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   671
        args.add("-strict");
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   672
        args.add(file);
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   673
        args.addAll(Arrays.asList(extra));
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   674
        return jarsigner(args);
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   675
    }
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   676
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   677
    static void checkBadKU(String file) throws Exception {
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   678
        verify(file)
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   679
                .shouldHaveExitValue(16)
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   680
                .shouldContain("treated as unsigned")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   681
                .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
   682
        verify(file, "-verbose")
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   683
                .shouldHaveExitValue(16)
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   684
                .shouldContain("Signed by")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   685
                .shouldContain("treated as unsigned")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   686
                .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
   687
        verify(file, "-J-Djava.security.debug=jar")
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   688
                .shouldHaveExitValue(16)
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   689
                .shouldContain("SignatureException: Key usage restricted")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   690
                .shouldContain("treated as unsigned")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   691
                .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
   692
    }
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   693
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   694
    static void checkWeak(String file) throws Exception {
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   695
        verify(file)
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   696
                .shouldHaveExitValue(16)
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   697
                .shouldContain("treated as unsigned")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   698
                .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
   699
                .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
   700
        verify(file, "-verbose")
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   701
                .shouldHaveExitValue(16)
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   702
                .shouldContain("treated as unsigned")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   703
                .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
   704
                .shouldMatch("Digest algorithm: .*weak")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   705
                .shouldMatch("Signature algorithm: .*weak")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   706
                .shouldMatch("Timestamp digest algorithm: .*weak")
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   707
                .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
   708
                .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
   709
        verify(file, "-J-Djava.security.debug=jar")
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   710
                .shouldHaveExitValue(16)
43701
fe8c324ba97c 8160655: Fix denyAfter and usage types for security properties
ascarpino
parents: 42180
diff changeset
   711
                .shouldMatch("SignatureException:.*disabled");
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43701
diff changeset
   712
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43701
diff changeset
   713
        // For 8171319: keytool should print out warnings when reading or
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43701
diff changeset
   714
        //              generating cert/cert req using weak algorithms.
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43701
diff changeset
   715
        // Must call keytool the command, otherwise doPrintCert() might not
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43701
diff changeset
   716
        // be able to reset "jdk.certpath.disabledAlgorithms".
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   717
        String sout = SecurityTools.keytool("-printcert -jarfile " + file)
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43701
diff changeset
   718
                .stderrShouldContain("The TSA certificate uses a 512-bit RSA key" +
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43701
diff changeset
   719
                        " which is considered a security risk.")
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43701
diff changeset
   720
                .getStdout();
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43701
diff changeset
   721
        if (sout.indexOf("weak", sout.indexOf("Timestamp:")) < 0) {
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43701
diff changeset
   722
            throw new RuntimeException("timestamp not weak: " + sout);
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43701
diff changeset
   723
        }
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   724
    }
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   725
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   726
    static void checkHalfWeak(String file) throws Exception {
42180
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   727
        verify(file)
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   728
                .shouldHaveExitValue(16)
42180
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   729
                .shouldContain("treated as unsigned")
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   730
                .shouldMatch("weak algorithm that is now disabled.")
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   731
                .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
   732
        verify(file, "-verbose")
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   733
                .shouldHaveExitValue(16)
42180
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   734
                .shouldContain("treated as unsigned")
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   735
                .shouldMatch("weak algorithm that is now disabled by")
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   736
                .shouldMatch("Digest algorithm: .*weak")
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   737
                .shouldNotMatch("Signature algorithm: .*weak")
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   738
                .shouldNotMatch("Timestamp digest algorithm: .*weak")
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   739
                .shouldNotMatch("Timestamp signature algorithm: .*weak.*weak")
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   740
                .shouldNotMatch("Timestamp signature algorithm: .*key.*weak");
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   741
     }
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   742
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   743
    static void checkMultiple(String file) throws Exception {
42180
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   744
        verify(file)
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   745
                .shouldHaveExitValue(0)
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   746
                .shouldContain("jar verified");
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   747
        verify(file, "-verbose", "-certs")
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   748
                .shouldHaveExitValue(0)
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   749
                .shouldContain("jar verified")
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   750
                .shouldMatch("X.509.*CN=dsakey")
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   751
                .shouldNotMatch("X.509.*CN=weakkeysize")
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   752
                .shouldMatch("Signed by .*CN=dsakey")
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   753
                .shouldMatch("Signed by .*CN=weakkeysize")
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   754
                .shouldMatch("Signature algorithm: .*key.*weak");
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   755
     }
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   756
24034
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   757
    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
   758
            throws Exception {
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   759
        try (JarFile jf = new JarFile(file)) {
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   760
            JarEntry je = jf.getJarEntry("META-INF/SIGNER.RSA");
24034
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   761
            try (InputStream is = jf.getInputStream(je)) {
32933
83d50f1247bb 8138978: Examine usages of sun.misc.IOUtils
chegar
parents: 28664
diff changeset
   762
                byte[] content = is.readAllBytes();
24034
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   763
                PKCS7 p7 = new PKCS7(content);
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   764
                SignerInfo[] si = p7.getSignerInfos();
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   765
                if (si == null || si.length == 0) {
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   766
                    throw new Exception("Not signed");
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   767
                }
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   768
                PKCS9Attribute p9 = si[0].getUnauthenticatedAttributes()
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   769
                        .getAttribute(PKCS9Attribute.SIGNATURE_TIMESTAMP_TOKEN_OID);
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   770
                PKCS7 tsToken = new PKCS7((byte[]) p9.getValue());
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   771
                TimestampToken tt =
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   772
                        new TimestampToken(tsToken.getContentInfo().getData());
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   773
                if (!tt.getHashAlgorithm().toString().equals(digestAlg)) {
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   774
                    throw new Exception("Digest alg different");
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   775
                }
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   776
                if (!tt.getPolicyID().equals(policyId)) {
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   777
                    throw new Exception("policyId different");
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   778
                }
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   779
            }
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   780
        }
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   781
    }
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   782
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   783
    static int which = 0;
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   784
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   785
    /**
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   786
     * Sign with a TSA path. Always use alias "signer" to sign "unsigned.jar".
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   787
     * The signed jar name is always path.jar.
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   788
     *
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   789
     * @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
   790
     */
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   791
    static OutputAnalyzer sign(String path, String... extra)
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   792
            throws Exception {
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   793
        return signVerbose(
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   794
                path,
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   795
                "unsigned.jar",
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   796
                path + ".jar",
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   797
                "signer",
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   798
                extra);
42180
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   799
    }
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   800
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   801
    static OutputAnalyzer signVerbose(
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   802
            String path,    // TSA URL path
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   803
            String oldJar,
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   804
            String newJar,
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   805
            String alias,   // signer
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   806
            String...extra) throws Exception {
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   807
        which++;
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   808
        System.out.println("\n>> Test #" + which);
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   809
        List<String> args = new ArrayList<>(List.of(
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   810
                "-strict", "-verbose", "-debug", "-signedjar", newJar, oldJar, alias));
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   811
        if (path != null) {
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   812
            args.add("-tsa");
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   813
            args.add(host + path);
5297
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   814
        }
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   815
        args.addAll(Arrays.asList(extra));
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   816
        return jarsigner(args);
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   817
    }
22315
529f1cc322fc 8024302: Clarify jar verifications
weijun
parents: 17161
diff changeset
   818
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   819
    static void prepare() throws Exception {
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   820
        JarUtils.createJar("unsigned.jar", "A");
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   821
        Files.deleteIfExists(Paths.get("ks"));
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   822
        keytool("-alias signer -genkeypair -ext bc -dname CN=signer");
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   823
        keytool("-alias oldsigner -genkeypair -dname CN=oldsigner");
42180
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   824
        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
   825
        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
   826
        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
   827
        keytool("-alias ts -genkeypair -dname CN=ts");
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   828
        keytool("-alias tsold -genkeypair -dname CN=tsold");
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   829
        keytool("-alias tsweak -genkeypair -keysize 512 -dname CN=tsweak");
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   830
        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
   831
        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
   832
        keytool("-alias tsbad3 -genkeypair -dname CN=tsbad3");
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   833
        keytool("-alias tsnoca -genkeypair -dname CN=tsnoca");
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   834
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   835
        keytool("-alias expired -genkeypair -dname CN=expired");
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   836
        keytool("-alias expiring -genkeypair -dname CN=expiring");
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   837
        keytool("-alias long -genkeypair -dname CN=long");
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   838
        keytool("-alias tsexpired -genkeypair -dname CN=tsexpired");
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   839
        keytool("-alias tsexpiring -genkeypair -dname CN=tsexpiring");
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   840
        keytool("-alias tsexpiringsoon -genkeypair -dname CN=tsexpiringsoon");
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   841
        keytool("-alias tslong -genkeypair -dname CN=tslong");
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   842
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   843
        // tsnoca's issuer will be removed from keystore later
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   844
        keytool("-alias ca -genkeypair -ext bc -dname CN=CA");
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   845
        gencert("tsnoca", "-ext eku:critical=ts");
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   846
        keytool("-delete -alias ca");
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   847
        keytool("-alias ca -genkeypair -ext bc -dname CN=CA -startdate -40d");
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   848
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   849
        gencert("signer");
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   850
        gencert("oldsigner", "-startdate -30d -validity 20");
42180
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   851
        gencert("dsakey");
11be74fc0be5 8169911: Enhanced tests for jarsigner -verbose -verify after JDK-8163304
amjiang
parents: 41590
diff changeset
   852
        gencert("weakkeysize");
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   853
        gencert("badku", "-ext ku:critical=keyAgreement");
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   854
        gencert("ts", "-ext eku:critical=ts -validity 500");
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   855
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   856
        gencert("expired", "-validity 10 -startdate -12d");
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   857
        gencert("expiring", "-validity 178");
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   858
        gencert("long", "-validity 182");
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   859
        gencert("tsexpired", "-ext eku:critical=ts -validity 10 -startdate -12d");
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   860
        gencert("tsexpiring", "-ext eku:critical=ts -validity 364");
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   861
        gencert("tsexpiringsoon", "-ext eku:critical=ts -validity 170"); // earlier than expiring
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   862
        gencert("tslong", "-ext eku:critical=ts -validity 367");
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   863
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   864
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   865
        for (int i = 0; i < 5; i++) {
48062
f14a1972f35d 8190674: sun/security/tools/jarsigner/TimestampCheck.java failed with java.nio.file.NoSuchFileException: ts2.cert
weijun
parents: 47469
diff changeset
   866
            // Issue another cert for "ts" with a different EKU.
f14a1972f35d 8190674: sun/security/tools/jarsigner/TimestampCheck.java failed with java.nio.file.NoSuchFileException: ts2.cert
weijun
parents: 47469
diff changeset
   867
            // Length might be different because serial number is
f14a1972f35d 8190674: sun/security/tools/jarsigner/TimestampCheck.java failed with java.nio.file.NoSuchFileException: ts2.cert
weijun
parents: 47469
diff changeset
   868
            // random. Try several times until a cert with the same
f14a1972f35d 8190674: sun/security/tools/jarsigner/TimestampCheck.java failed with java.nio.file.NoSuchFileException: ts2.cert
weijun
parents: 47469
diff changeset
   869
            // length is generated so we can substitute ts.cert
f14a1972f35d 8190674: sun/security/tools/jarsigner/TimestampCheck.java failed with java.nio.file.NoSuchFileException: ts2.cert
weijun
parents: 47469
diff changeset
   870
            // embedded in the PKCS7 block with ts2.cert.
f14a1972f35d 8190674: sun/security/tools/jarsigner/TimestampCheck.java failed with java.nio.file.NoSuchFileException: ts2.cert
weijun
parents: 47469
diff changeset
   871
            // If cannot create one, related test will be ignored.
f14a1972f35d 8190674: sun/security/tools/jarsigner/TimestampCheck.java failed with java.nio.file.NoSuchFileException: ts2.cert
weijun
parents: 47469
diff changeset
   872
            keytool("-gencert -alias ca -infile ts.req -outfile ts2.cert " +
f14a1972f35d 8190674: sun/security/tools/jarsigner/TimestampCheck.java failed with java.nio.file.NoSuchFileException: ts2.cert
weijun
parents: 47469
diff changeset
   873
                    "-ext eku:critical=1.3.6.1.5.5.7.3.9");
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   874
            if (Files.size(Paths.get("ts.cert")) != Files.size(Paths.get("ts2.cert"))) {
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   875
                Files.delete(Paths.get("ts2.cert"));
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   876
                System.out.println("Warning: cannot create same length");
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   877
            } else {
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   878
                break;
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   879
            }
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   880
        }
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   881
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48062
diff changeset
   882
        gencert("tsold", "-ext eku:critical=ts -startdate -40d -validity 500");
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   883
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   884
        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
   885
        gencert("tsbad1");
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   886
        gencert("tsbad2", "-ext eku=ts");
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   887
        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
   888
    }
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   889
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   890
    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
   891
        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
   892
        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
   893
                alias + ".req -outfile " + alias + ".cert";
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   894
        for (String s : extra) {
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   895
            genCmd += " " + s;
22315
529f1cc322fc 8024302: Clarify jar verifications
weijun
parents: 17161
diff changeset
   896
        }
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   897
        keytool(genCmd);
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   898
        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
   899
    }
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   900
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   901
    static void keytool(String cmd) throws Exception {
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   902
        cmd = "-keystore ks -storepass changeit -keypass changeit " +
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   903
                "-keyalg rsa -validity 200 " + cmd;
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 40262
diff changeset
   904
        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
   905
    }
61fb331e1dad 6939248: Jarsigner can't extract Extended Key Usage from Timestamp Reply correctly
weijun
parents:
diff changeset
   906
}