src/jdk.jartool/share/classes/sun/security/tools/jarsigner/Main.java
author chegar
Thu, 17 Oct 2019 20:54:25 +0100
branchdatagramsocketimpl-branch
changeset 58679 9c3209ff7550
parent 58678 9cf78a70fa4f
parent 57766 81ce766c9111
permissions -rw-r--r--
datagramsocketimpl-branch: merge with default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
55312
d8942f5d6c75 8221719: Jarsigner fails to verify signed by alias if alias given in wrong case
weijun
parents: 53398
diff changeset
     2
 * Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5462
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5462
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5462
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5462
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5462
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
14182
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 12046
diff changeset
    26
package sun.security.tools.jarsigner;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
48760
25725c11c296 8196823: jarsigner should not create a signed jar if the signing fails
weijun
parents: 48543
diff changeset
    29
import java.net.UnknownHostException;
43183
b50e0f90d284 8172529: Use PKIXValidator in jarsigner
weijun
parents: 41590
diff changeset
    30
import java.security.cert.CertPathValidatorException;
b50e0f90d284 8172529: Use PKIXValidator in jarsigner
weijun
parents: 41590
diff changeset
    31
import java.security.cert.PKIXBuilderParameters;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.*;
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
    33
import java.util.stream.Collectors;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.zip.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.jar.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.net.URI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.text.Collator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.text.MessageFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.security.cert.Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.security.cert.X509Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.security.cert.CertificateException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
    44
import java.net.SocketTimeoutException;
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
    45
import java.net.URL;
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
    46
import java.security.cert.CertPath;
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
    47
import java.security.cert.CertificateExpiredException;
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
    48
import java.security.cert.CertificateFactory;
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
    49
import java.security.cert.CertificateNotYetValidException;
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
    50
import java.security.cert.TrustAnchor;
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
    51
import java.util.Map.Entry;
33872
94e3836950ec 8056174: New APIs for jar signing
weijun
parents: 31270
diff changeset
    52
94e3836950ec 8056174: New APIs for jar signing
weijun
parents: 31270
diff changeset
    53
import jdk.security.jarsigner.JarSigner;
94e3836950ec 8056174: New APIs for jar signing
weijun
parents: 31270
diff changeset
    54
import jdk.security.jarsigner.JarSignerException;
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
    55
import sun.security.pkcs.PKCS7;
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
    56
import sun.security.pkcs.SignerInfo;
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
    57
import sun.security.timestamp.TimestampToken;
14182
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 12046
diff changeset
    58
import sun.security.tools.KeyStoreUtil;
43183
b50e0f90d284 8172529: Use PKIXValidator in jarsigner
weijun
parents: 41590
diff changeset
    59
import sun.security.validator.Validator;
b50e0f90d284 8172529: Use PKIXValidator in jarsigner
weijun
parents: 41590
diff changeset
    60
import sun.security.validator.ValidatorException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
import sun.security.x509.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
import sun.security.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
    64
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * <p>The jarsigner utility.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
    68
 * The exit codes for the main method are:
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
    69
 *
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
    70
 * 0: success
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
    71
 * 1: any error that the jar cannot be signed or verified, including:
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
    72
 *      keystore loading error
12046
378aa3362868 7149012: jarsigner needs not warn about cert expiration if the jar has a TSA timestamp
weijun
parents: 10788
diff changeset
    73
 *      TSP communication error
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
    74
 *      jarsigner command line error...
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
    75
 * otherwise: error codes from -strict
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
    76
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * @author Roland Schemers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * @author Jan Luehe
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 */
14182
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 12046
diff changeset
    80
public class Main {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    // for i18n
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    private static final java.util.ResourceBundle rb =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        java.util.ResourceBundle.getBundle
14182
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 12046
diff changeset
    85
        ("sun.security.tools.jarsigner.Resources");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    private static final Collator collator = Collator.getInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        // this is for case insensitive string comparisions
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        collator.setStrength(Collator.PRIMARY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    private static final String NONE = "NONE";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    private static final String P11KEYSTORE = "PKCS11";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    private static final long SIX_MONTHS = 180*24*60*60*1000L; //milliseconds
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
    96
    private static final long ONE_YEAR = 366*24*60*60*1000L;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
    98
    private static final DisabledAlgorithmConstraints DISABLED_CHECK =
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
    99
            new DisabledAlgorithmConstraints(
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   100
                    DisabledAlgorithmConstraints.PROPERTY_JAR_DISABLED_ALGS);
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   101
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   102
    private static final Set<CryptoPrimitive> DIGEST_PRIMITIVE_SET = Collections
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   103
            .unmodifiableSet(EnumSet.of(CryptoPrimitive.MESSAGE_DIGEST));
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   104
    private static final Set<CryptoPrimitive> SIG_PRIMITIVE_SET = Collections
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   105
            .unmodifiableSet(EnumSet.of(CryptoPrimitive.SIGNATURE));
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   106
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
   107
    static final String VERSION = "1.0";
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
   108
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
   109
    static final int IN_KEYSTORE = 0x01;        // signer is in keystore
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
   110
    static final int NOT_ALIAS = 0x04;          // alias list is NOT empty and
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
   111
    // signer is not in alias list
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
   112
    static final int SIGNED_BY_ALIAS = 0x08;    // signer is in alias list
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
   113
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    // Attention:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    // This is the entry that get launched by the security tool jarsigner.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    public static void main(String args[]) throws Exception {
14182
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 12046
diff changeset
   117
        Main js = new Main();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        js.run(args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   121
    X509Certificate[] certChain;    // signer's cert chain (when composing)
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   122
    PrivateKey privateKey;          // private key
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   123
    KeyStore store;                 // the keystore specified by -keystore
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   124
                                    // or the default keystore, never null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    String keystore; // key store file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    boolean nullStream = false; // null keystore input stream (NONE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    boolean token = false; // token-based keystore
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5461
diff changeset
   129
    String jarfile;  // jar files to sign or verify
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    String alias;    // alias to sign jar with
7977
f47f211cd627 7008713: diamond conversion of kerberos5 and security tools
smarks
parents: 7525
diff changeset
   131
    List<String> ckaliases = new ArrayList<>(); // aliases in -verify
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    char[] storepass; // keystore password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    boolean protectedPath; // protected authentication path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    String storetype; // keystore type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    String providerName; // provider name
39633
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 34382
diff changeset
   136
    List<String> providers = null; // list of provider names
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 34382
diff changeset
   137
    List<String> providerClasses = null; // list of provider classes
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   138
    // arguments for provider constructors
7977
f47f211cd627 7008713: diamond conversion of kerberos5 and security tools
smarks
parents: 7525
diff changeset
   139
    HashMap<String,String> providerArgs = new HashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    char[] keypass; // private key password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    String sigfile; // name of .SF file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    String sigalg; // name of signature algorithm
33872
94e3836950ec 8056174: New APIs for jar signing
weijun
parents: 31270
diff changeset
   143
    String digestalg; // name of digest algorithm
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    String signedjar; // output filename
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    String tsaUrl; // location of the Timestamping Authority
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    String tsaAlias; // alias for the Timestamping Authority's certificate
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   147
    String altCertChain; // file to read alternative cert chain from
17161
df1ec0e2f0e7 8009636: JARSigner including TimeStamp PolicyID (TSAPolicyID) as defined in RFC3161
weijun
parents: 16020
diff changeset
   148
    String tSAPolicyID;
33872
94e3836950ec 8056174: New APIs for jar signing
weijun
parents: 31270
diff changeset
   149
    String tSADigestAlg;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    boolean verify = false; // verify the jar
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   151
    String verbose = null; // verbose output when signing/verifying
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    boolean showcerts = false; // show certs when verifying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    boolean debug = false; // debug
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    boolean signManifest = true; // "sign" the whole manifest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    boolean externalSF = true; // leave the .SF out of the PKCS7 block
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   156
    boolean strict = false;  // treat warnings as error
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    // read zip entry raw bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    private String altSignerClass = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    private String altSignerClasspath = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    private ZipFile zipFile = null;
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   162
22315
529f1cc322fc 8024302: Clarify jar verifications
weijun
parents: 19189
diff changeset
   163
    // Informational warnings
529f1cc322fc 8024302: Clarify jar verifications
weijun
parents: 19189
diff changeset
   164
    private boolean hasExpiringCert = false;
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
   165
    private boolean hasExpiringTsaCert = false;
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
   166
    private boolean noTimestamp = true;
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
   167
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
   168
    // Expiration date. The value could be null if signed by a trusted cert.
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
   169
    private Date expireDate = null;
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
   170
    private Date tsaExpireDate = null;
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
   171
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
   172
    // If there is a time stamp block inside the PKCS7 block file
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
   173
    boolean hasTimestampBlock = false;
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
   174
22315
529f1cc322fc 8024302: Clarify jar verifications
weijun
parents: 19189
diff changeset
   175
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   176
    // Severe warnings.
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   177
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   178
    // jarsigner used to check signer cert chain validity and key usages
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   179
    // itself and set various warnings. Later CertPath validation is
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   180
    // added but chainNotValidated is only flagged when no other existing
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   181
    // warnings are set. TSA cert chain check is added separately and
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   182
    // only tsaChainNotValidated is set, i.e. has no affect on hasExpiredCert,
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   183
    // notYetValidCert, or any badXyzUsage.
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   184
34382
5d11306d6969 8130132: jarsigner should emit warning if weak algorithms or keysizes are used
weijun
parents: 33872
diff changeset
   185
    private int weakAlg = 0; // 1. digestalg, 2. sigalg, 4. tsadigestalg
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    private boolean hasExpiredCert = false;
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
   187
    private boolean hasExpiredTsaCert = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    private boolean notYetValidCert = false;
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   189
    private boolean chainNotValidated = false;
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   190
    private boolean tsaChainNotValidated = false;
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   191
    private boolean notSignedByAlias = false;
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   192
    private boolean aliasNotInStore = false;
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   193
    private boolean hasUnsignedEntry = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    private boolean badKeyUsage = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    private boolean badExtendedKeyUsage = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    private boolean badNetscapeCertType = false;
34382
5d11306d6969 8130132: jarsigner should emit warning if weak algorithms or keysizes are used
weijun
parents: 33872
diff changeset
   197
    private boolean signerSelfSigned = false;
5d11306d6969 8130132: jarsigner should emit warning if weak algorithms or keysizes are used
weijun
parents: 33872
diff changeset
   198
5d11306d6969 8130132: jarsigner should emit warning if weak algorithms or keysizes are used
weijun
parents: 33872
diff changeset
   199
    private Throwable chainNotValidatedReason = null;
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   200
    private Throwable tsaChainNotValidatedReason = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   202
    private boolean seeWeak = false;
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   203
43183
b50e0f90d284 8172529: Use PKIXValidator in jarsigner
weijun
parents: 41590
diff changeset
   204
    PKIXBuilderParameters pkixParameters;
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
   205
    Set<X509Certificate> trustedCerts = new HashSet<>();
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   206
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    public void run(String args[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        try {
24868
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 24625
diff changeset
   209
            args = parseArgs(args);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            // Try to load and install the specified providers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            if (providers != null) {
39633
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 34382
diff changeset
   213
                for (String provName: providers) {
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 34382
diff changeset
   214
                    try {
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 34382
diff changeset
   215
                        KeyStoreUtil.loadProviderByName(provName,
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 34382
diff changeset
   216
                                providerArgs.get(provName));
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 34382
diff changeset
   217
                        if (debug) {
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 34382
diff changeset
   218
                            System.out.println("loadProviderByName: " + provName);
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 34382
diff changeset
   219
                        }
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 34382
diff changeset
   220
                    } catch (IllegalArgumentException e) {
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 34382
diff changeset
   221
                        throw new Exception(String.format(rb.getString(
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 34382
diff changeset
   222
                                "provider.name.not.found"), provName));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                    }
39633
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 34382
diff changeset
   224
                }
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 34382
diff changeset
   225
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
39633
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 34382
diff changeset
   227
            if (providerClasses != null) {
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 34382
diff changeset
   228
                ClassLoader cl = ClassLoader.getSystemClassLoader();
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 34382
diff changeset
   229
                for (String provClass: providerClasses) {
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 34382
diff changeset
   230
                    try {
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 34382
diff changeset
   231
                        KeyStoreUtil.loadProviderByClass(provClass,
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 34382
diff changeset
   232
                                providerArgs.get(provClass), cl);
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 34382
diff changeset
   233
                        if (debug) {
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 34382
diff changeset
   234
                            System.out.println("loadProviderByClass: " + provClass);
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 34382
diff changeset
   235
                        }
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 34382
diff changeset
   236
                    } catch (ClassCastException cce) {
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 34382
diff changeset
   237
                        throw new Exception(String.format(rb.getString(
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 34382
diff changeset
   238
                                "provclass.not.a.provider"), provClass));
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 34382
diff changeset
   239
                    } catch (IllegalArgumentException e) {
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 34382
diff changeset
   240
                        throw new Exception(String.format(rb.getString(
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 34382
diff changeset
   241
                                "provider.class.not.found"), provClass), e.getCause());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            if (verify) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                    loadKeyStore(keystore, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                    if ((keystore != null) || (storepass != null)) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   251
                        System.out.println(rb.getString("jarsigner.error.") +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                                        e.getMessage());
33872
94e3836950ec 8056174: New APIs for jar signing
weijun
parents: 31270
diff changeset
   253
                        if (debug) {
94e3836950ec 8056174: New APIs for jar signing
weijun
parents: 31270
diff changeset
   254
                            e.printStackTrace();
94e3836950ec 8056174: New APIs for jar signing
weijun
parents: 31270
diff changeset
   255
                        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                        System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                /*              if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                    SignatureFileVerifier.setDebug(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                    ManifestEntryVerifier.setDebug(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                verifyJar(jarfile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                loadKeyStore(keystore, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                getAliasInfo(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
33872
94e3836950ec 8056174: New APIs for jar signing
weijun
parents: 31270
diff changeset
   269
                signJar(jarfile, alias);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        } catch (Exception e) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   272
            System.out.println(rb.getString("jarsigner.error.") + e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            // zero-out private key password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            if (keypass != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                Arrays.fill(keypass, ' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                keypass = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            // zero-out keystore password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            if (storepass != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                Arrays.fill(storepass, ' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                storepass = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        }
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   289
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   290
        if (strict) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   291
            int exitCode = 0;
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
   292
            if (weakAlg != 0 || chainNotValidated || hasExpiredCert
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
   293
                    || hasExpiredTsaCert || notYetValidCert || signerSelfSigned) {
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   294
                exitCode |= 4;
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   295
            }
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   296
            if (badKeyUsage || badExtendedKeyUsage || badNetscapeCertType) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   297
                exitCode |= 8;
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   298
            }
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   299
            if (hasUnsignedEntry) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   300
                exitCode |= 16;
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   301
            }
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   302
            if (notSignedByAlias || aliasNotInStore) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   303
                exitCode |= 32;
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   304
            }
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   305
            if (tsaChainNotValidated) {
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   306
                exitCode |= 64;
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   307
            }
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   308
            if (exitCode != 0) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   309
                System.exit(exitCode);
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   310
            }
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   311
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * Parse command line arguments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     */
24868
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 24625
diff changeset
   317
    String[] parseArgs(String args[]) throws Exception {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        /* parse flags */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   321
        if (args.length == 0) fullusage();
24868
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 24625
diff changeset
   322
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 24625
diff changeset
   323
        String confFile = null;
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 24625
diff changeset
   324
        String command = "-sign";
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 24625
diff changeset
   325
        for (n=0; n < args.length; n++) {
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 24625
diff changeset
   326
            if (collator.compare(args[n], "-verify") == 0) {
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 24625
diff changeset
   327
                command = "-verify";
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 24625
diff changeset
   328
            } else if (collator.compare(args[n], "-conf") == 0) {
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 24625
diff changeset
   329
                if (n == args.length - 1) {
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 24625
diff changeset
   330
                    usageNoArg();
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 24625
diff changeset
   331
                }
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 24625
diff changeset
   332
                confFile = args[++n];
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 24625
diff changeset
   333
            }
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 24625
diff changeset
   334
        }
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 24625
diff changeset
   335
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 24625
diff changeset
   336
        if (confFile != null) {
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 24625
diff changeset
   337
            args = KeyStoreUtil.expandArgs(
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 24625
diff changeset
   338
                    "jarsigner", confFile, command, null, args);
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 24625
diff changeset
   339
        }
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 24625
diff changeset
   340
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 24625
diff changeset
   341
        debug = Arrays.stream(args).anyMatch(
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 24625
diff changeset
   342
                x -> collator.compare(x, "-debug") == 0);
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 24625
diff changeset
   343
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 24625
diff changeset
   344
        if (debug) {
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 24625
diff changeset
   345
            // No need to localize debug output
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 24625
diff changeset
   346
            System.out.println("Command line args: " +
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 24625
diff changeset
   347
                    Arrays.toString(args));
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 24625
diff changeset
   348
        }
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 24625
diff changeset
   349
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   350
        for (n=0; n < args.length; n++) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            String flags = args[n];
3951
e821908c953e 6868579: RFE: jarsigner to support reading password from environment variable
weijun
parents: 3716
diff changeset
   353
            String modifier = null;
19189
a4b8478a2bc5 8021789: jarsigner parses alias as command line option (depending on locale)
weijun
parents: 17161
diff changeset
   354
a4b8478a2bc5 8021789: jarsigner parses alias as command line option (depending on locale)
weijun
parents: 17161
diff changeset
   355
            if (flags.startsWith("-")) {
3951
e821908c953e 6868579: RFE: jarsigner to support reading password from environment variable
weijun
parents: 3716
diff changeset
   356
                int pos = flags.indexOf(':');
e821908c953e 6868579: RFE: jarsigner to support reading password from environment variable
weijun
parents: 3716
diff changeset
   357
                if (pos > 0) {
e821908c953e 6868579: RFE: jarsigner to support reading password from environment variable
weijun
parents: 3716
diff changeset
   358
                    modifier = flags.substring(pos+1);
e821908c953e 6868579: RFE: jarsigner to support reading password from environment variable
weijun
parents: 3716
diff changeset
   359
                    flags = flags.substring(0, pos);
e821908c953e 6868579: RFE: jarsigner to support reading password from environment variable
weijun
parents: 3716
diff changeset
   360
                }
e821908c953e 6868579: RFE: jarsigner to support reading password from environment variable
weijun
parents: 3716
diff changeset
   361
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
19189
a4b8478a2bc5 8021789: jarsigner parses alias as command line option (depending on locale)
weijun
parents: 17161
diff changeset
   363
            if (!flags.startsWith("-")) {
a4b8478a2bc5 8021789: jarsigner parses alias as command line option (depending on locale)
weijun
parents: 17161
diff changeset
   364
                if (jarfile == null) {
a4b8478a2bc5 8021789: jarsigner parses alias as command line option (depending on locale)
weijun
parents: 17161
diff changeset
   365
                    jarfile = flags;
a4b8478a2bc5 8021789: jarsigner parses alias as command line option (depending on locale)
weijun
parents: 17161
diff changeset
   366
                } else {
a4b8478a2bc5 8021789: jarsigner parses alias as command line option (depending on locale)
weijun
parents: 17161
diff changeset
   367
                    alias = flags;
a4b8478a2bc5 8021789: jarsigner parses alias as command line option (depending on locale)
weijun
parents: 17161
diff changeset
   368
                    ckaliases.add(alias);
a4b8478a2bc5 8021789: jarsigner parses alias as command line option (depending on locale)
weijun
parents: 17161
diff changeset
   369
                }
24868
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 24625
diff changeset
   370
            } else if (collator.compare(flags, "-conf") == 0) {
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 24625
diff changeset
   371
                if (++n == args.length) usageNoArg();
19189
a4b8478a2bc5 8021789: jarsigner parses alias as command line option (depending on locale)
weijun
parents: 17161
diff changeset
   372
            } else if (collator.compare(flags, "-keystore") == 0) {
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   373
                if (++n == args.length) usageNoArg();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                keystore = args[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            } else if (collator.compare(flags, "-storepass") ==0) {
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   376
                if (++n == args.length) usageNoArg();
3951
e821908c953e 6868579: RFE: jarsigner to support reading password from environment variable
weijun
parents: 3716
diff changeset
   377
                storepass = getPass(modifier, args[n]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            } else if (collator.compare(flags, "-storetype") ==0) {
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   379
                if (++n == args.length) usageNoArg();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                storetype = args[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            } else if (collator.compare(flags, "-providerName") ==0) {
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   382
                if (++n == args.length) usageNoArg();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                providerName = args[n];
39633
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 34382
diff changeset
   384
            } else if (collator.compare(flags, "-provider") == 0 ||
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 34382
diff changeset
   385
                        collator.compare(flags, "-providerClass") == 0) {
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 34382
diff changeset
   386
                if (++n == args.length) usageNoArg();
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 34382
diff changeset
   387
                if (providerClasses == null) {
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 34382
diff changeset
   388
                    providerClasses = new ArrayList<>(3);
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 34382
diff changeset
   389
                }
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 34382
diff changeset
   390
                providerClasses.add(args[n]);
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 34382
diff changeset
   391
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 34382
diff changeset
   392
                if (args.length > (n+1)) {
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 34382
diff changeset
   393
                    flags = args[n+1];
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 34382
diff changeset
   394
                    if (collator.compare(flags, "-providerArg") == 0) {
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 34382
diff changeset
   395
                        if (args.length == (n+2)) usageNoArg();
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 34382
diff changeset
   396
                        providerArgs.put(args[n], args[n+2]);
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 34382
diff changeset
   397
                        n += 2;
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 34382
diff changeset
   398
                    }
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 34382
diff changeset
   399
                }
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 34382
diff changeset
   400
            } else if (collator.compare(flags, "-addprovider") == 0) {
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   401
                if (++n == args.length) usageNoArg();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                if (providers == null) {
39633
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 34382
diff changeset
   403
                    providers = new ArrayList<>(3);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                providers.add(args[n]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                if (args.length > (n+1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                    flags = args[n+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                    if (collator.compare(flags, "-providerArg") == 0) {
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   410
                        if (args.length == (n+2)) usageNoArg();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                        providerArgs.put(args[n], args[n+2]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                        n += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            } else if (collator.compare(flags, "-protected") ==0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                protectedPath = true;
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   417
            } else if (collator.compare(flags, "-certchain") ==0) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   418
                if (++n == args.length) usageNoArg();
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   419
                altCertChain = args[n];
17161
df1ec0e2f0e7 8009636: JARSigner including TimeStamp PolicyID (TSAPolicyID) as defined in RFC3161
weijun
parents: 16020
diff changeset
   420
            } else if (collator.compare(flags, "-tsapolicyid") ==0) {
df1ec0e2f0e7 8009636: JARSigner including TimeStamp PolicyID (TSAPolicyID) as defined in RFC3161
weijun
parents: 16020
diff changeset
   421
                if (++n == args.length) usageNoArg();
df1ec0e2f0e7 8009636: JARSigner including TimeStamp PolicyID (TSAPolicyID) as defined in RFC3161
weijun
parents: 16020
diff changeset
   422
                tSAPolicyID = args[n];
24034
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23912
diff changeset
   423
            } else if (collator.compare(flags, "-tsadigestalg") ==0) {
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23912
diff changeset
   424
                if (++n == args.length) usageNoArg();
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23912
diff changeset
   425
                tSADigestAlg = args[n];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            } else if (collator.compare(flags, "-debug") ==0) {
24868
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 24625
diff changeset
   427
                // Already processed
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            } else if (collator.compare(flags, "-keypass") ==0) {
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   429
                if (++n == args.length) usageNoArg();
3951
e821908c953e 6868579: RFE: jarsigner to support reading password from environment variable
weijun
parents: 3716
diff changeset
   430
                keypass = getPass(modifier, args[n]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            } else if (collator.compare(flags, "-sigfile") ==0) {
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   432
                if (++n == args.length) usageNoArg();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                sigfile = args[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            } else if (collator.compare(flags, "-signedjar") ==0) {
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   435
                if (++n == args.length) usageNoArg();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                signedjar = args[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            } else if (collator.compare(flags, "-tsa") ==0) {
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   438
                if (++n == args.length) usageNoArg();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                tsaUrl = args[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            } else if (collator.compare(flags, "-tsacert") ==0) {
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   441
                if (++n == args.length) usageNoArg();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                tsaAlias = args[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            } else if (collator.compare(flags, "-altsigner") ==0) {
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   444
                if (++n == args.length) usageNoArg();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                altSignerClass = args[n];
31060
be4eb6360ee0 8076535: Deprecate the com.sun.jarsigner package
juh
parents: 29369
diff changeset
   446
                System.err.println(
be4eb6360ee0 8076535: Deprecate the com.sun.jarsigner package
juh
parents: 29369
diff changeset
   447
                        rb.getString("This.option.is.deprecated") +
be4eb6360ee0 8076535: Deprecate the com.sun.jarsigner package
juh
parents: 29369
diff changeset
   448
                                "-altsigner");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            } else if (collator.compare(flags, "-altsignerpath") ==0) {
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   450
                if (++n == args.length) usageNoArg();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                altSignerClasspath = args[n];
31060
be4eb6360ee0 8076535: Deprecate the com.sun.jarsigner package
juh
parents: 29369
diff changeset
   452
                System.err.println(
be4eb6360ee0 8076535: Deprecate the com.sun.jarsigner package
juh
parents: 29369
diff changeset
   453
                        rb.getString("This.option.is.deprecated") +
be4eb6360ee0 8076535: Deprecate the com.sun.jarsigner package
juh
parents: 29369
diff changeset
   454
                                "-altsignerpath");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            } else if (collator.compare(flags, "-sectionsonly") ==0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                signManifest = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            } else if (collator.compare(flags, "-internalsf") ==0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                externalSF = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            } else if (collator.compare(flags, "-verify") ==0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                verify = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            } else if (collator.compare(flags, "-verbose") ==0) {
3951
e821908c953e 6868579: RFE: jarsigner to support reading password from environment variable
weijun
parents: 3716
diff changeset
   462
                verbose = (modifier != null) ? modifier : "all";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            } else if (collator.compare(flags, "-sigalg") ==0) {
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   464
                if (++n == args.length) usageNoArg();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                sigalg = args[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            } else if (collator.compare(flags, "-digestalg") ==0) {
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   467
                if (++n == args.length) usageNoArg();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                digestalg = args[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            } else if (collator.compare(flags, "-certs") ==0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                showcerts = true;
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   471
            } else if (collator.compare(flags, "-strict") ==0) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   472
                strict = true;
48543
7067fe4e054e 8189102: All tools should support -?, -h and --help
goetz
parents: 47469
diff changeset
   473
            } else if (collator.compare(flags, "-?") == 0 ||
7067fe4e054e 8189102: All tools should support -?, -h and --help
goetz
parents: 47469
diff changeset
   474
                       collator.compare(flags, "-h") == 0 ||
7067fe4e054e 8189102: All tools should support -?, -h and --help
goetz
parents: 47469
diff changeset
   475
                       collator.compare(flags, "--help") == 0 ||
7067fe4e054e 8189102: All tools should support -?, -h and --help
goetz
parents: 47469
diff changeset
   476
                       // -help: legacy.
7067fe4e054e 8189102: All tools should support -?, -h and --help
goetz
parents: 47469
diff changeset
   477
                       collator.compare(flags, "-help") == 0) {
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   478
                fullusage();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            } else {
19189
a4b8478a2bc5 8021789: jarsigner parses alias as command line option (depending on locale)
weijun
parents: 17161
diff changeset
   480
                System.err.println(
a4b8478a2bc5 8021789: jarsigner parses alias as command line option (depending on locale)
weijun
parents: 17161
diff changeset
   481
                        rb.getString("Illegal.option.") + flags);
a4b8478a2bc5 8021789: jarsigner parses alias as command line option (depending on locale)
weijun
parents: 17161
diff changeset
   482
                usage();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   486
        // -certs must always be specified with -verbose
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   487
        if (verbose == null) showcerts = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   489
        if (jarfile == null) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   490
            System.err.println(rb.getString("Please.specify.jarfile.name"));
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   491
            usage();
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   492
        }
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   493
        if (!verify && alias == null) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   494
            System.err.println(rb.getString("Please.specify.alias.name"));
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   495
            usage();
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   496
        }
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   497
        if (!verify && ckaliases.size() > 1) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   498
            System.err.println(rb.getString("Only.one.alias.can.be.specified"));
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   499
            usage();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        if (storetype == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            storetype = KeyStore.getDefaultType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        storetype = KeyStoreUtil.niceStoreTypeName(storetype);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
3481
6ae7a2a6c956 6866479: libzip.so caused JVM to crash when running jarsigner
weijun
parents: 3318
diff changeset
   507
        try {
6ae7a2a6c956 6866479: libzip.so caused JVM to crash when running jarsigner
weijun
parents: 3318
diff changeset
   508
            if (signedjar != null && new File(signedjar).getCanonicalPath().equals(
6ae7a2a6c956 6866479: libzip.so caused JVM to crash when running jarsigner
weijun
parents: 3318
diff changeset
   509
                    new File(jarfile).getCanonicalPath())) {
6ae7a2a6c956 6866479: libzip.so caused JVM to crash when running jarsigner
weijun
parents: 3318
diff changeset
   510
                signedjar = null;
6ae7a2a6c956 6866479: libzip.so caused JVM to crash when running jarsigner
weijun
parents: 3318
diff changeset
   511
            }
6ae7a2a6c956 6866479: libzip.so caused JVM to crash when running jarsigner
weijun
parents: 3318
diff changeset
   512
        } catch (IOException ioe) {
6ae7a2a6c956 6866479: libzip.so caused JVM to crash when running jarsigner
weijun
parents: 3318
diff changeset
   513
            // File system error?
6ae7a2a6c956 6866479: libzip.so caused JVM to crash when running jarsigner
weijun
parents: 3318
diff changeset
   514
            // Just ignore it.
6ae7a2a6c956 6866479: libzip.so caused JVM to crash when running jarsigner
weijun
parents: 3318
diff changeset
   515
        }
6ae7a2a6c956 6866479: libzip.so caused JVM to crash when running jarsigner
weijun
parents: 3318
diff changeset
   516
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        if (P11KEYSTORE.equalsIgnoreCase(storetype) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                KeyStoreUtil.isWindowsKeyStore(storetype)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            token = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            if (keystore == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                keystore = NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        if (NONE.equals(keystore)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
            nullStream = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        if (token && !nullStream) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            System.err.println(MessageFormat.format(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   531
                (".keystore.must.be.NONE.if.storetype.is.{0}"), storetype));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            usage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        if (token && keypass != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            System.err.println(MessageFormat.format(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   537
                (".keypass.can.not.be.specified.if.storetype.is.{0}"), storetype));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            usage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        if (protectedPath) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            if (storepass != null || keypass != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                System.err.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   544
                        ("If.protected.is.specified.then.storepass.and.keypass.must.not.be.specified"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                usage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        if (KeyStoreUtil.isWindowsKeyStore(storetype)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            if (storepass != null || keypass != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                System.err.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   551
                        ("If.keystore.is.not.password.protected.then.storepass.and.keypass.must.not.be.specified"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                usage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        }
24868
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 24625
diff changeset
   555
        return args;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
3951
e821908c953e 6868579: RFE: jarsigner to support reading password from environment variable
weijun
parents: 3716
diff changeset
   558
    static char[] getPass(String modifier, String arg) {
14182
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 12046
diff changeset
   559
        char[] output = KeyStoreUtil.getPassWithModifier(modifier, arg, rb);
3951
e821908c953e 6868579: RFE: jarsigner to support reading password from environment variable
weijun
parents: 3716
diff changeset
   560
        if (output != null) return output;
e821908c953e 6868579: RFE: jarsigner to support reading password from environment variable
weijun
parents: 3716
diff changeset
   561
        usage();
e821908c953e 6868579: RFE: jarsigner to support reading password from environment variable
weijun
parents: 3716
diff changeset
   562
        return null;    // Useless, usage() already exit
e821908c953e 6868579: RFE: jarsigner to support reading password from environment variable
weijun
parents: 3716
diff changeset
   563
    }
e821908c953e 6868579: RFE: jarsigner to support reading password from environment variable
weijun
parents: 3716
diff changeset
   564
e821908c953e 6868579: RFE: jarsigner to support reading password from environment variable
weijun
parents: 3716
diff changeset
   565
    static void usageNoArg() {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   566
        System.out.println(rb.getString("Option.lacks.argument"));
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   567
        usage();
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   568
    }
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   569
3951
e821908c953e 6868579: RFE: jarsigner to support reading password from environment variable
weijun
parents: 3716
diff changeset
   570
    static void usage() {
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   571
        System.out.println();
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   572
        System.out.println(rb.getString("Please.type.jarsigner.help.for.usage"));
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   573
        System.exit(1);
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   574
    }
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   575
3951
e821908c953e 6868579: RFE: jarsigner to support reading password from environment variable
weijun
parents: 3716
diff changeset
   576
    static void fullusage() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        System.out.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   578
                ("Usage.jarsigner.options.jar.file.alias"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        System.out.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   580
                (".jarsigner.verify.options.jar.file.alias."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        System.out.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   583
                (".keystore.url.keystore.location"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        System.out.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   586
                (".storepass.password.password.for.keystore.integrity"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        System.out.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   589
                (".storetype.type.keystore.type"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        System.out.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   592
                (".keypass.password.password.for.private.key.if.different."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        System.out.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   595
                (".certchain.file.name.of.alternative.certchain.file"));
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   596
        System.out.println();
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   597
        System.out.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   598
                (".sigfile.file.name.of.SF.DSA.file"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        System.out.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   601
                (".signedjar.file.name.of.signed.JAR.file"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        System.out.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   604
                (".digestalg.algorithm.name.of.digest.algorithm"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        System.out.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   607
                (".sigalg.algorithm.name.of.signature.algorithm"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        System.out.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   610
                (".verify.verify.a.signed.JAR.file"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        System.out.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   613
                (".verbose.suboptions.verbose.output.when.signing.verifying."));
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   614
        System.out.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   615
                (".suboptions.can.be.all.grouped.or.summary"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        System.out.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   618
                (".certs.display.certificates.when.verbose.and.verifying"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        System.out.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   621
                (".tsa.url.location.of.the.Timestamping.Authority"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        System.out.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   624
                (".tsacert.alias.public.key.certificate.for.Timestamping.Authority"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        System.out.println(rb.getString
17161
df1ec0e2f0e7 8009636: JARSigner including TimeStamp PolicyID (TSAPolicyID) as defined in RFC3161
weijun
parents: 16020
diff changeset
   627
                (".tsapolicyid.tsapolicyid.for.Timestamping.Authority"));
df1ec0e2f0e7 8009636: JARSigner including TimeStamp PolicyID (TSAPolicyID) as defined in RFC3161
weijun
parents: 16020
diff changeset
   628
        System.out.println();
df1ec0e2f0e7 8009636: JARSigner including TimeStamp PolicyID (TSAPolicyID) as defined in RFC3161
weijun
parents: 16020
diff changeset
   629
        System.out.println(rb.getString
24034
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23912
diff changeset
   630
                (".tsadigestalg.algorithm.of.digest.data.in.timestamping.request"));
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23912
diff changeset
   631
        System.out.println();
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23912
diff changeset
   632
        System.out.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   633
                (".altsigner.class.class.name.of.an.alternative.signing.mechanism"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        System.out.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   636
                (".altsignerpath.pathlist.location.of.an.alternative.signing.mechanism"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        System.out.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   639
                (".internalsf.include.the.SF.file.inside.the.signature.block"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        System.out.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   642
                (".sectionsonly.don.t.compute.hash.of.entire.manifest"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        System.out.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   645
                (".protected.keystore.has.protected.authentication.path"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        System.out.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   648
                (".providerName.name.provider.name"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        System.out.println(rb.getString
39633
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 34382
diff changeset
   651
                (".add.provider.option"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        System.out.println(rb.getString
39633
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 34382
diff changeset
   653
                (".providerArg.option.1"));
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 34382
diff changeset
   654
        System.out.println();
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 34382
diff changeset
   655
        System.out.println(rb.getString
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 34382
diff changeset
   656
                (".providerClass.option"));
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 34382
diff changeset
   657
        System.out.println(rb.getString
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 34382
diff changeset
   658
                (".providerArg.option.2"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        System.out.println();
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   660
        System.out.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   661
                (".strict.treat.warnings.as.errors"));
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   662
        System.out.println();
24868
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 24625
diff changeset
   663
        System.out.println(rb.getString
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 24625
diff changeset
   664
                (".conf.url.specify.a.pre.configured.options.file"));
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 24625
diff changeset
   665
        System.out.println();
48543
7067fe4e054e 8189102: All tools should support -?, -h and --help
goetz
parents: 47469
diff changeset
   666
        System.out.println(rb.getString
7067fe4e054e 8189102: All tools should support -?, -h and --help
goetz
parents: 47469
diff changeset
   667
                (".print.this.help.message"));
7067fe4e054e 8189102: All tools should support -?, -h and --help
goetz
parents: 47469
diff changeset
   668
        System.out.println();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   670
        System.exit(0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
    void verifyJar(String jarName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
    {
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   676
        boolean anySigned = false;  // if there exists entry inside jar signed
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        JarFile jf = null;
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   678
        Map<String,String> digestMap = new HashMap<>();
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   679
        Map<String,PKCS7> sigMap = new HashMap<>();
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   680
        Map<String,String> sigNameMap = new HashMap<>();
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   681
        Map<String,String> unparsableSignatures = new HashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
            jf = new JarFile(jarName, true);
7977
f47f211cd627 7008713: diamond conversion of kerberos5 and security tools
smarks
parents: 7525
diff changeset
   685
            Vector<JarEntry> entriesVec = new Vector<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
            byte[] buffer = new byte[8192];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
57766
81ce766c9111 8229775: Incorrect warning when jar was signed with -sectionsonly
weijun
parents: 55312
diff changeset
   688
            String suffix1 = "-Digest-Manifest";
81ce766c9111 8229775: Incorrect warning when jar was signed with -sectionsonly
weijun
parents: 55312
diff changeset
   689
            String suffix2 = "-Digest-" + ManifestDigester.MF_MAIN_ATTRS;
81ce766c9111 8229775: Incorrect warning when jar was signed with -sectionsonly
weijun
parents: 55312
diff changeset
   690
81ce766c9111 8229775: Incorrect warning when jar was signed with -sectionsonly
weijun
parents: 55312
diff changeset
   691
            int suffixLength1 = suffix1.length();
81ce766c9111 8229775: Incorrect warning when jar was signed with -sectionsonly
weijun
parents: 55312
diff changeset
   692
            int suffixLength2 = suffix2.length();
81ce766c9111 8229775: Incorrect warning when jar was signed with -sectionsonly
weijun
parents: 55312
diff changeset
   693
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
            Enumeration<JarEntry> entries = jf.entries();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
            while (entries.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
                JarEntry je = entries.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
                entriesVec.addElement(je);
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   698
                try (InputStream is = jf.getInputStream(je)) {
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   699
                    String name = je.getName();
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   700
                    if (signatureRelated(name)
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   701
                            && SignatureFileVerifier.isBlockOrSF(name)) {
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   702
                        String alias = name.substring(name.lastIndexOf('/') + 1,
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   703
                                name.lastIndexOf('.'));
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   704
                        try {
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   705
                            if (name.endsWith(".SF")) {
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   706
                                Manifest sf = new Manifest(is);
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   707
                                boolean found = false;
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   708
                                for (Object obj : sf.getMainAttributes().keySet()) {
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   709
                                    String key = obj.toString();
57766
81ce766c9111 8229775: Incorrect warning when jar was signed with -sectionsonly
weijun
parents: 55312
diff changeset
   710
                                    if (key.endsWith(suffix1)) {
81ce766c9111 8229775: Incorrect warning when jar was signed with -sectionsonly
weijun
parents: 55312
diff changeset
   711
                                        digestMap.put(alias, key.substring(
81ce766c9111 8229775: Incorrect warning when jar was signed with -sectionsonly
weijun
parents: 55312
diff changeset
   712
                                                0, key.length() - suffixLength1));
81ce766c9111 8229775: Incorrect warning when jar was signed with -sectionsonly
weijun
parents: 55312
diff changeset
   713
                                        found = true;
81ce766c9111 8229775: Incorrect warning when jar was signed with -sectionsonly
weijun
parents: 55312
diff changeset
   714
                                        break;
81ce766c9111 8229775: Incorrect warning when jar was signed with -sectionsonly
weijun
parents: 55312
diff changeset
   715
                                    } else if (key.endsWith(suffix2)) {
81ce766c9111 8229775: Incorrect warning when jar was signed with -sectionsonly
weijun
parents: 55312
diff changeset
   716
                                        digestMap.put(alias, key.substring(
81ce766c9111 8229775: Incorrect warning when jar was signed with -sectionsonly
weijun
parents: 55312
diff changeset
   717
                                                0, key.length() - suffixLength2));
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   718
                                        found = true;
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   719
                                        break;
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   720
                                    }
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   721
                                }
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   722
                                if (!found) {
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   723
                                    unparsableSignatures.putIfAbsent(alias,
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   724
                                        String.format(
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   725
                                            rb.getString("history.unparsable"),
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   726
                                            name));
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   727
                                }
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   728
                            } else {
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   729
                                sigNameMap.put(alias, name);
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   730
                                sigMap.put(alias, new PKCS7(is));
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   731
                            }
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   732
                        } catch (IOException ioe) {
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   733
                            unparsableSignatures.putIfAbsent(alias, String.format(
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   734
                                    rb.getString("history.unparsable"), name));
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   735
                        }
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   736
                    } else {
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   737
                        while (is.read(buffer, 0, buffer.length) != -1) {
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   738
                            // we just read. this will throw a SecurityException
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   739
                            // if  a signature/digest check fails.
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   740
                        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
            Manifest man = jf.getManifest();
41582
246512d81eba 8165816: jarsigner -verify shows jar unsigned if it was signed with a weak algorithm
weijun
parents: 40177
diff changeset
   746
            boolean hasSignature = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   748
            // The map to record display info, only used when -verbose provided
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   749
            //      key: signer info string
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   750
            //      value: the list of files with common key
7977
f47f211cd627 7008713: diamond conversion of kerberos5 and security tools
smarks
parents: 7525
diff changeset
   751
            Map<String,List<String>> output = new LinkedHashMap<>();
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   752
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
            if (man != null) {
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   754
                if (verbose != null) System.out.println();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
                Enumeration<JarEntry> e = entriesVec.elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   757
                String tab = rb.getString("6SPACE");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
                while (e.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
                    JarEntry je = e.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
                    String name = je.getName();
41582
246512d81eba 8165816: jarsigner -verify shows jar unsigned if it was signed with a weak algorithm
weijun
parents: 40177
diff changeset
   762
246512d81eba 8165816: jarsigner -verify shows jar unsigned if it was signed with a weak algorithm
weijun
parents: 40177
diff changeset
   763
                    hasSignature = hasSignature
246512d81eba 8165816: jarsigner -verify shows jar unsigned if it was signed with a weak algorithm
weijun
parents: 40177
diff changeset
   764
                            || SignatureFileVerifier.isBlockOrSF(name);
246512d81eba 8165816: jarsigner -verify shows jar unsigned if it was signed with a weak algorithm
weijun
parents: 40177
diff changeset
   765
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
                    CodeSigner[] signers = je.getCodeSigners();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
                    boolean isSigned = (signers != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
                    anySigned |= isSigned;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
                    hasUnsignedEntry |= !je.isDirectory() && !isSigned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
                                        && !signatureRelated(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
40177
e2a7079bd50a 8163303: Remove identity scope information from jarsigner -verbose output
weijun
parents: 39633
diff changeset
   772
                    int inStoreWithAlias = inKeyStore(signers);
e2a7079bd50a 8163303: Remove identity scope information from jarsigner -verbose output
weijun
parents: 39633
diff changeset
   773
e2a7079bd50a 8163303: Remove identity scope information from jarsigner -verbose output
weijun
parents: 39633
diff changeset
   774
                    boolean inStore = (inStoreWithAlias & IN_KEYSTORE) != 0;
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   775
40177
e2a7079bd50a 8163303: Remove identity scope information from jarsigner -verbose output
weijun
parents: 39633
diff changeset
   776
                    notSignedByAlias |= (inStoreWithAlias & NOT_ALIAS) != 0;
7525
16d2b5e6517a 7004168: jarsigner -verify checks for KeyUsage codesigning ext on all certs instead of just signing cert
weijun
parents: 7524
diff changeset
   777
                    if (keystore != null) {
40177
e2a7079bd50a 8163303: Remove identity scope information from jarsigner -verbose output
weijun
parents: 39633
diff changeset
   778
                        aliasNotInStore |= isSigned && !inStore;
7525
16d2b5e6517a 7004168: jarsigner -verify checks for KeyUsage codesigning ext on all certs instead of just signing cert
weijun
parents: 7524
diff changeset
   779
                    }
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   780
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   781
                    // Only used when -verbose provided
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   782
                    StringBuffer sb = null;
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   783
                    if (verbose != null) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   784
                        sb = new StringBuffer();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
                        boolean inManifest =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
                            ((man.getAttributes(name) != null) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
                             (man.getAttributes("./"+name) != null) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
                             (man.getAttributes("/"+name) != null));
27957
24b4e6082f19 8055723: Replace concat String to append in StringBuilder parameters (dev)
weijun
parents: 27344
diff changeset
   789
                        sb.append(isSigned ? rb.getString("s") : rb.getString("SPACE"))
24b4e6082f19 8055723: Replace concat String to append in StringBuilder parameters (dev)
weijun
parents: 27344
diff changeset
   790
                                .append(inManifest ? rb.getString("m") : rb.getString("SPACE"))
24b4e6082f19 8055723: Replace concat String to append in StringBuilder parameters (dev)
weijun
parents: 27344
diff changeset
   791
                                .append(inStore ? rb.getString("k") : rb.getString("SPACE"))
40177
e2a7079bd50a 8163303: Remove identity scope information from jarsigner -verbose output
weijun
parents: 39633
diff changeset
   792
                                .append((inStoreWithAlias & NOT_ALIAS) != 0 ? 'X' : ' ')
27957
24b4e6082f19 8055723: Replace concat String to append in StringBuilder parameters (dev)
weijun
parents: 27344
diff changeset
   793
                                .append(rb.getString("SPACE"));
24b4e6082f19 8055723: Replace concat String to append in StringBuilder parameters (dev)
weijun
parents: 27344
diff changeset
   794
                        sb.append('|');
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   795
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   797
                    // When -certs provided, display info has extra empty
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   798
                    // lines at the beginning and end.
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   799
                    if (isSigned) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   800
                        if (showcerts) sb.append('\n');
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   801
                        for (CodeSigner signer: signers) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   802
                            // signerInfo() must be called even if -verbose
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   803
                            // not provided. The method updates various
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   804
                            // warning flags.
12046
378aa3362868 7149012: jarsigner needs not warn about cert expiration if the jar has a TSA timestamp
weijun
parents: 10788
diff changeset
   805
                            String si = signerInfo(signer, tab);
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   806
                            if (showcerts) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   807
                                sb.append(si);
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   808
                                sb.append('\n');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
                        }
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   811
                    } else if (showcerts && !verbose.equals("all")) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   812
                        // Print no info for unsigned entries when -verbose:all,
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   813
                        // to be consistent with old behavior.
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   814
                        if (signatureRelated(name)) {
27957
24b4e6082f19 8055723: Replace concat String to append in StringBuilder parameters (dev)
weijun
parents: 27344
diff changeset
   815
                            sb.append('\n')
24b4e6082f19 8055723: Replace concat String to append in StringBuilder parameters (dev)
weijun
parents: 27344
diff changeset
   816
                                    .append(tab)
24b4e6082f19 8055723: Replace concat String to append in StringBuilder parameters (dev)
weijun
parents: 27344
diff changeset
   817
                                    .append(rb
24b4e6082f19 8055723: Replace concat String to append in StringBuilder parameters (dev)
weijun
parents: 27344
diff changeset
   818
                                            .getString(".Signature.related.entries."))
24b4e6082f19 8055723: Replace concat String to append in StringBuilder parameters (dev)
weijun
parents: 27344
diff changeset
   819
                                    .append("\n\n");
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   820
                        } else {
27957
24b4e6082f19 8055723: Replace concat String to append in StringBuilder parameters (dev)
weijun
parents: 27344
diff changeset
   821
                            sb.append('\n').append(tab)
24b4e6082f19 8055723: Replace concat String to append in StringBuilder parameters (dev)
weijun
parents: 27344
diff changeset
   822
                                    .append(rb.getString(".Unsigned.entries."))
24b4e6082f19 8055723: Replace concat String to append in StringBuilder parameters (dev)
weijun
parents: 27344
diff changeset
   823
                                    .append("\n\n");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   827
                    if (verbose != null) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   828
                        String label = sb.toString();
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   829
                        if (signatureRelated(name)) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   830
                            // Entries inside META-INF and other unsigned
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   831
                            // entries are grouped separately.
7524
ec12e1e6fa20 7004035: signed jar with only META-INF/* inside is not verifiable
weijun
parents: 7179
diff changeset
   832
                            label = "-" + label;
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   833
                        }
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   834
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   835
                        // The label finally contains 2 parts separated by '|':
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   836
                        // The legend displayed before the entry names, and
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20754
diff changeset
   837
                        // the cert info (if -certs specified).
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   838
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   839
                        if (!output.containsKey(label)) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   840
                            output.put(label, new ArrayList<String>());
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   841
                        }
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   842
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 24868
diff changeset
   843
                        StringBuilder fb = new StringBuilder();
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   844
                        String s = Long.toString(je.getSize());
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   845
                        for (int i = 6 - s.length(); i > 0; --i) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   846
                            fb.append(' ');
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   847
                        }
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   848
                        fb.append(s).append(' ').
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   849
                                append(new Date(je.getTime()).toString());
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   850
                        fb.append(' ').append(name);
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   851
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   852
                        output.get(label).add(fb.toString());
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   853
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
            }
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   856
            if (verbose != null) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   857
                for (Entry<String,List<String>> s: output.entrySet()) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   858
                    List<String> files = s.getValue();
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   859
                    String key = s.getKey();
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   860
                    if (key.charAt(0) == '-') { // the signature-related group
7524
ec12e1e6fa20 7004035: signed jar with only META-INF/* inside is not verifiable
weijun
parents: 7179
diff changeset
   861
                        key = key.substring(1);
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   862
                    }
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   863
                    int pipe = key.indexOf('|');
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   864
                    if (verbose.equals("all")) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   865
                        for (String f: files) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   866
                            System.out.println(key.substring(0, pipe) + f);
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   867
                            System.out.printf(key.substring(pipe+1));
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   868
                        }
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   869
                    } else {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   870
                        if (verbose.equals("grouped")) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   871
                            for (String f: files) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   872
                                System.out.println(key.substring(0, pipe) + f);
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   873
                            }
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   874
                        } else if (verbose.equals("summary")) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   875
                            System.out.print(key.substring(0, pipe));
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   876
                            if (files.size() > 1) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   877
                                System.out.println(files.get(0) + " " +
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   878
                                        String.format(rb.getString(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   879
                                        ".and.d.more."), files.size()-1));
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   880
                            } else {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   881
                                System.out.println(files.get(0));
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   882
                            }
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   883
                        }
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   884
                        System.out.printf(key.substring(pipe+1));
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   885
                    }
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   886
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
                System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
                System.out.println(rb.getString(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   889
                    ".s.signature.was.verified."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
                System.out.println(rb.getString(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   891
                    ".m.entry.is.listed.in.manifest"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
                System.out.println(rb.getString(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   893
                    ".k.at.least.one.certificate.was.found.in.keystore"));
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   894
                if (ckaliases.size() > 0) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   895
                    System.out.println(rb.getString(
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   896
                        ".X.not.signed.by.specified.alias.es."));
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   897
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
            }
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   899
            if (man == null) {
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   900
                System.out.println();
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   901
                System.out.println(rb.getString("no.manifest."));
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   902
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
34382
5d11306d6969 8130132: jarsigner should emit warning if weak algorithms or keysizes are used
weijun
parents: 33872
diff changeset
   904
            // If signer is a trusted cert or private entry in user's own
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
   905
            // keystore, it can be self-signed. Please note aliasNotInStore
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
   906
            // is always false when ~/.keystore is used.
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
   907
            if (!aliasNotInStore && keystore != null) {
34382
5d11306d6969 8130132: jarsigner should emit warning if weak algorithms or keysizes are used
weijun
parents: 33872
diff changeset
   908
                signerSelfSigned = false;
5d11306d6969 8130132: jarsigner should emit warning if weak algorithms or keysizes are used
weijun
parents: 33872
diff changeset
   909
            }
5d11306d6969 8130132: jarsigner should emit warning if weak algorithms or keysizes are used
weijun
parents: 33872
diff changeset
   910
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   911
            // Even if the verbose option is not specified, all out strings
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   912
            // must be generated so seeWeak can be updated.
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   913
            if (!digestMap.isEmpty()
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   914
                    || !sigMap.isEmpty()
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   915
                    || !unparsableSignatures.isEmpty()) {
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   916
                if (verbose != null) {
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   917
                    System.out.println();
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   918
                }
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   919
                for (String s : sigMap.keySet()) {
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   920
                    if (!digestMap.containsKey(s)) {
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   921
                        unparsableSignatures.putIfAbsent(s, String.format(
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   922
                                rb.getString("history.nosf"), s));
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   923
                    }
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   924
                }
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   925
                for (String s : digestMap.keySet()) {
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   926
                    PKCS7 p7 = sigMap.get(s);
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   927
                    if (p7 != null) {
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   928
                        String history;
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   929
                        try {
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   930
                            SignerInfo si = p7.getSignerInfos()[0];
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   931
                            X509Certificate signer = si.getCertificate(p7);
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   932
                            String digestAlg = digestMap.get(s);
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   933
                            String sigAlg = AlgorithmId.makeSigAlg(
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   934
                                    si.getDigestAlgorithmId().getName(),
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   935
                                    si.getDigestEncryptionAlgorithmId().getName());
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   936
                            PublicKey key = signer.getPublicKey();
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   937
                            PKCS7 tsToken = si.getTsToken();
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   938
                            if (tsToken != null) {
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
   939
                                hasTimestampBlock = true;
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   940
                                SignerInfo tsSi = tsToken.getSignerInfos()[0];
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   941
                                X509Certificate tsSigner = tsSi.getCertificate(tsToken);
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   942
                                byte[] encTsTokenInfo = tsToken.getContentInfo().getData();
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   943
                                TimestampToken tsTokenInfo = new TimestampToken(encTsTokenInfo);
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   944
                                PublicKey tsKey = tsSigner.getPublicKey();
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   945
                                String tsDigestAlg = tsTokenInfo.getHashAlgorithm().getName();
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   946
                                String tsSigAlg = AlgorithmId.makeSigAlg(
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   947
                                        tsSi.getDigestAlgorithmId().getName(),
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   948
                                        tsSi.getDigestEncryptionAlgorithmId().getName());
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   949
                                Calendar c = Calendar.getInstance(
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   950
                                        TimeZone.getTimeZone("UTC"),
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   951
                                        Locale.getDefault(Locale.Category.FORMAT));
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   952
                                c.setTime(tsTokenInfo.getDate());
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   953
                                history = String.format(
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   954
                                        rb.getString("history.with.ts"),
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   955
                                        signer.getSubjectX500Principal(),
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   956
                                        withWeak(digestAlg, DIGEST_PRIMITIVE_SET),
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   957
                                        withWeak(sigAlg, SIG_PRIMITIVE_SET),
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   958
                                        withWeak(key),
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   959
                                        c,
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   960
                                        tsSigner.getSubjectX500Principal(),
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   961
                                        withWeak(tsDigestAlg, DIGEST_PRIMITIVE_SET),
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   962
                                        withWeak(tsSigAlg, SIG_PRIMITIVE_SET),
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   963
                                        withWeak(tsKey));
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   964
                            } else {
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   965
                                history = String.format(
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   966
                                        rb.getString("history.without.ts"),
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   967
                                        signer.getSubjectX500Principal(),
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   968
                                        withWeak(digestAlg, DIGEST_PRIMITIVE_SET),
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   969
                                        withWeak(sigAlg, SIG_PRIMITIVE_SET),
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   970
                                        withWeak(key));
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   971
                            }
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   972
                        } catch (Exception e) {
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   973
                            // The only usage of sigNameMap, remember the name
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   974
                            // of the block file if it's invalid.
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   975
                            history = String.format(
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   976
                                    rb.getString("history.unparsable"),
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   977
                                    sigNameMap.get(s));
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   978
                        }
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   979
                        if (verbose != null) {
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   980
                            System.out.println(history);
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   981
                        }
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   982
                    } else {
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   983
                        unparsableSignatures.putIfAbsent(s, String.format(
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   984
                                rb.getString("history.nobk"), s));
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   985
                    }
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   986
                }
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   987
                if (verbose != null) {
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   988
                    for (String s : unparsableSignatures.keySet()) {
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   989
                        System.out.println(unparsableSignatures.get(s));
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   990
                    }
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   991
                }
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   992
            }
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   993
            System.out.println();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
            if (!anySigned) {
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   995
                if (seeWeak) {
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   996
                    if (verbose != null) {
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   997
                        System.out.println(rb.getString("jar.treated.unsigned.see.weak.verbose"));
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   998
                        System.out.println("\n  " +
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
   999
                                DisabledAlgorithmConstraints.PROPERTY_JAR_DISABLED_ALGS +
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
  1000
                                "=" + Security.getProperty(DisabledAlgorithmConstraints.PROPERTY_JAR_DISABLED_ALGS));
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
  1001
                    } else {
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
  1002
                        System.out.println(rb.getString("jar.treated.unsigned.see.weak"));
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
  1003
                    }
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
  1004
                } else if (hasSignature) {
41582
246512d81eba 8165816: jarsigner -verify shows jar unsigned if it was signed with a weak algorithm
weijun
parents: 40177
diff changeset
  1005
                    System.out.println(rb.getString("jar.treated.unsigned"));
246512d81eba 8165816: jarsigner -verify shows jar unsigned if it was signed with a weak algorithm
weijun
parents: 40177
diff changeset
  1006
                } else {
246512d81eba 8165816: jarsigner -verify shows jar unsigned if it was signed with a weak algorithm
weijun
parents: 40177
diff changeset
  1007
                    System.out.println(rb.getString("jar.is.unsigned"));
246512d81eba 8165816: jarsigner -verify shows jar unsigned if it was signed with a weak algorithm
weijun
parents: 40177
diff changeset
  1008
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
            } else {
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1010
                displayMessagesAndResult(false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
            }
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1012
            return;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
        } catch (Exception e) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
  1014
            System.out.println(rb.getString("jarsigner.") + e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
            if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
        } finally { // close the resource
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
            if (jf != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
                jf.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
        System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1027
    private void displayMessagesAndResult(boolean isSigning) {
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1028
        String result;
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1029
        List<String> errors = new ArrayList<>();
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1030
        List<String> warnings = new ArrayList<>();
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1031
        List<String> info = new ArrayList<>();
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1032
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1033
        boolean signerNotExpired = expireDate == null
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1034
                || expireDate.after(new Date());
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1035
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1036
        if (badKeyUsage || badExtendedKeyUsage || badNetscapeCertType ||
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1037
                notYetValidCert || chainNotValidated || hasExpiredCert ||
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1038
                hasUnsignedEntry || signerSelfSigned || (weakAlg != 0) ||
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1039
                aliasNotInStore || notSignedByAlias ||
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1040
                tsaChainNotValidated ||
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1041
                (hasExpiredTsaCert && !signerNotExpired)) {
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1042
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1043
            if (strict) {
53398
dd1be616c95e 8215937: Check usages of security-related Resources files
weijun
parents: 48893
diff changeset
  1044
                result = isSigning
dd1be616c95e 8215937: Check usages of security-related Resources files
weijun
parents: 48893
diff changeset
  1045
                        ? rb.getString("jar.signed.with.signer.errors.")
dd1be616c95e 8215937: Check usages of security-related Resources files
weijun
parents: 48893
diff changeset
  1046
                        : rb.getString("jar.verified.with.signer.errors.");
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1047
            } else {
53398
dd1be616c95e 8215937: Check usages of security-related Resources files
weijun
parents: 48893
diff changeset
  1048
                result = isSigning
dd1be616c95e 8215937: Check usages of security-related Resources files
weijun
parents: 48893
diff changeset
  1049
                        ? rb.getString("jar.signed.")
dd1be616c95e 8215937: Check usages of security-related Resources files
weijun
parents: 48893
diff changeset
  1050
                        : rb.getString("jar.verified.");
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1051
            }
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1052
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1053
            if (badKeyUsage) {
53398
dd1be616c95e 8215937: Check usages of security-related Resources files
weijun
parents: 48893
diff changeset
  1054
                errors.add(isSigning
dd1be616c95e 8215937: Check usages of security-related Resources files
weijun
parents: 48893
diff changeset
  1055
                        ? rb.getString("The.signer.certificate.s.KeyUsage.extension.doesn.t.allow.code.signing.")
dd1be616c95e 8215937: Check usages of security-related Resources files
weijun
parents: 48893
diff changeset
  1056
                        : rb.getString("This.jar.contains.entries.whose.signer.certificate.s.KeyUsage.extension.doesn.t.allow.code.signing."));
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1057
            }
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1058
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1059
            if (badExtendedKeyUsage) {
53398
dd1be616c95e 8215937: Check usages of security-related Resources files
weijun
parents: 48893
diff changeset
  1060
                errors.add(isSigning
dd1be616c95e 8215937: Check usages of security-related Resources files
weijun
parents: 48893
diff changeset
  1061
                        ? rb.getString("The.signer.certificate.s.ExtendedKeyUsage.extension.doesn.t.allow.code.signing.")
dd1be616c95e 8215937: Check usages of security-related Resources files
weijun
parents: 48893
diff changeset
  1062
                        : rb.getString("This.jar.contains.entries.whose.signer.certificate.s.ExtendedKeyUsage.extension.doesn.t.allow.code.signing."));
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1063
            }
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1064
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1065
            if (badNetscapeCertType) {
53398
dd1be616c95e 8215937: Check usages of security-related Resources files
weijun
parents: 48893
diff changeset
  1066
                errors.add(isSigning
dd1be616c95e 8215937: Check usages of security-related Resources files
weijun
parents: 48893
diff changeset
  1067
                        ? rb.getString("The.signer.certificate.s.NetscapeCertType.extension.doesn.t.allow.code.signing.")
dd1be616c95e 8215937: Check usages of security-related Resources files
weijun
parents: 48893
diff changeset
  1068
                        : rb.getString("This.jar.contains.entries.whose.signer.certificate.s.NetscapeCertType.extension.doesn.t.allow.code.signing."));
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1069
            }
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1070
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1071
            // only in verifying
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1072
            if (hasUnsignedEntry) {
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1073
                errors.add(rb.getString(
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1074
                        "This.jar.contains.unsigned.entries.which.have.not.been.integrity.checked."));
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1075
            }
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1076
            if (hasExpiredCert) {
53398
dd1be616c95e 8215937: Check usages of security-related Resources files
weijun
parents: 48893
diff changeset
  1077
                errors.add(isSigning
dd1be616c95e 8215937: Check usages of security-related Resources files
weijun
parents: 48893
diff changeset
  1078
                        ? rb.getString("The.signer.certificate.has.expired.")
dd1be616c95e 8215937: Check usages of security-related Resources files
weijun
parents: 48893
diff changeset
  1079
                        : rb.getString("This.jar.contains.entries.whose.signer.certificate.has.expired."));
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1080
            }
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1081
            if (notYetValidCert) {
53398
dd1be616c95e 8215937: Check usages of security-related Resources files
weijun
parents: 48893
diff changeset
  1082
                errors.add(isSigning
dd1be616c95e 8215937: Check usages of security-related Resources files
weijun
parents: 48893
diff changeset
  1083
                        ? rb.getString("The.signer.certificate.is.not.yet.valid.")
dd1be616c95e 8215937: Check usages of security-related Resources files
weijun
parents: 48893
diff changeset
  1084
                        : rb.getString("This.jar.contains.entries.whose.signer.certificate.is.not.yet.valid."));
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1085
            }
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1086
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1087
            if (chainNotValidated) {
53398
dd1be616c95e 8215937: Check usages of security-related Resources files
weijun
parents: 48893
diff changeset
  1088
                errors.add(String.format(isSigning
dd1be616c95e 8215937: Check usages of security-related Resources files
weijun
parents: 48893
diff changeset
  1089
                                ? rb.getString("The.signer.s.certificate.chain.is.invalid.reason.1")
dd1be616c95e 8215937: Check usages of security-related Resources files
weijun
parents: 48893
diff changeset
  1090
                                : rb.getString("This.jar.contains.entries.whose.certificate.chain.is.invalid.reason.1"),
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1091
                        chainNotValidatedReason.getLocalizedMessage()));
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1092
            }
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1093
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1094
            if (hasExpiredTsaCert) {
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1095
                errors.add(rb.getString("The.timestamp.has.expired."));
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1096
            }
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1097
            if (tsaChainNotValidated) {
53398
dd1be616c95e 8215937: Check usages of security-related Resources files
weijun
parents: 48893
diff changeset
  1098
                errors.add(String.format(isSigning
dd1be616c95e 8215937: Check usages of security-related Resources files
weijun
parents: 48893
diff changeset
  1099
                                ? rb.getString("The.tsa.certificate.chain.is.invalid.reason.1")
dd1be616c95e 8215937: Check usages of security-related Resources files
weijun
parents: 48893
diff changeset
  1100
                                : rb.getString("This.jar.contains.entries.whose.tsa.certificate.chain.is.invalid.reason.1"),
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1101
                        tsaChainNotValidatedReason.getLocalizedMessage()));
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1102
            }
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1103
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1104
            // only in verifying
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1105
            if (notSignedByAlias) {
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1106
                errors.add(
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1107
                        rb.getString("This.jar.contains.signed.entries.which.is.not.signed.by.the.specified.alias.es."));
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1108
            }
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1109
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1110
            // only in verifying
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1111
            if (aliasNotInStore) {
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1112
                errors.add(rb.getString("This.jar.contains.signed.entries.that.s.not.signed.by.alias.in.this.keystore."));
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1113
            }
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1114
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1115
            if (signerSelfSigned) {
53398
dd1be616c95e 8215937: Check usages of security-related Resources files
weijun
parents: 48893
diff changeset
  1116
                errors.add(isSigning
dd1be616c95e 8215937: Check usages of security-related Resources files
weijun
parents: 48893
diff changeset
  1117
                        ? rb.getString("The.signer.s.certificate.is.self.signed.")
dd1be616c95e 8215937: Check usages of security-related Resources files
weijun
parents: 48893
diff changeset
  1118
                        : rb.getString("This.jar.contains.entries.whose.signer.certificate.is.self.signed."));
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1119
            }
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1120
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1121
            // weakAlg only detected in signing. The jar file is
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1122
            // now simply treated unsigned in verifying.
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1123
            if ((weakAlg & 1) == 1) {
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1124
                errors.add(String.format(
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1125
                        rb.getString("The.1.algorithm.specified.for.the.2.option.is.considered.a.security.risk."),
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1126
                        digestalg, "-digestalg"));
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1127
            }
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1128
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1129
            if ((weakAlg & 2) == 2) {
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1130
                errors.add(String.format(
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1131
                        rb.getString("The.1.algorithm.specified.for.the.2.option.is.considered.a.security.risk."),
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1132
                        sigalg, "-sigalg"));
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1133
            }
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1134
            if ((weakAlg & 4) == 4) {
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1135
                errors.add(String.format(
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1136
                        rb.getString("The.1.algorithm.specified.for.the.2.option.is.considered.a.security.risk."),
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1137
                        tSADigestAlg, "-tsadigestalg"));
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1138
            }
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1139
            if ((weakAlg & 8) == 8) {
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1140
                errors.add(String.format(
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1141
                        rb.getString("The.1.signing.key.has.a.keysize.of.2.which.is.considered.a.security.risk."),
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1142
                        privateKey.getAlgorithm(), KeyUtil.getKeySize(privateKey)));
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1143
            }
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1144
        } else {
53398
dd1be616c95e 8215937: Check usages of security-related Resources files
weijun
parents: 48893
diff changeset
  1145
            result = isSigning ? rb.getString("jar.signed.") : rb.getString("jar.verified.");
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1146
        }
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1147
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1148
        if (hasExpiredTsaCert) {
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1149
            // No need to warn about expiring if already expired
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1150
            hasExpiringTsaCert = false;
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1151
        }
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1152
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1153
        if (hasExpiringCert ||
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1154
                (hasExpiringTsaCert  && expireDate != null) ||
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1155
                (noTimestamp && expireDate != null) ||
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1156
                (hasExpiredTsaCert && signerNotExpired)) {
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1157
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1158
            if (hasExpiredTsaCert && signerNotExpired) {
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1159
                if (expireDate != null) {
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1160
                    warnings.add(String.format(
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1161
                            rb.getString("The.timestamp.expired.1.but.usable.2"),
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1162
                            tsaExpireDate,
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1163
                            expireDate));
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1164
                }
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1165
                // Reset the flag so exit code is 0
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1166
                hasExpiredTsaCert = false;
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1167
            }
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1168
            if (hasExpiringCert) {
53398
dd1be616c95e 8215937: Check usages of security-related Resources files
weijun
parents: 48893
diff changeset
  1169
                warnings.add(isSigning
dd1be616c95e 8215937: Check usages of security-related Resources files
weijun
parents: 48893
diff changeset
  1170
                        ? rb.getString("The.signer.certificate.will.expire.within.six.months.")
dd1be616c95e 8215937: Check usages of security-related Resources files
weijun
parents: 48893
diff changeset
  1171
                        : rb.getString("This.jar.contains.entries.whose.signer.certificate.will.expire.within.six.months."));
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1172
            }
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1173
            if (hasExpiringTsaCert && expireDate != null) {
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1174
                if (expireDate.after(tsaExpireDate)) {
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1175
                    warnings.add(String.format(rb.getString(
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1176
                            "The.timestamp.will.expire.within.one.year.on.1.but.2"), tsaExpireDate, expireDate));
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1177
                } else {
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1178
                    warnings.add(String.format(rb.getString(
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1179
                            "The.timestamp.will.expire.within.one.year.on.1"), tsaExpireDate));
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1180
                }
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1181
            }
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1182
            if (noTimestamp && expireDate != null) {
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1183
                if (hasTimestampBlock) {
53398
dd1be616c95e 8215937: Check usages of security-related Resources files
weijun
parents: 48893
diff changeset
  1184
                    warnings.add(String.format(isSigning
dd1be616c95e 8215937: Check usages of security-related Resources files
weijun
parents: 48893
diff changeset
  1185
                            ? rb.getString("invalid.timestamp.signing")
dd1be616c95e 8215937: Check usages of security-related Resources files
weijun
parents: 48893
diff changeset
  1186
                            : rb.getString("bad.timestamp.verifying"), expireDate));
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1187
                } else {
53398
dd1be616c95e 8215937: Check usages of security-related Resources files
weijun
parents: 48893
diff changeset
  1188
                    warnings.add(String.format(isSigning
dd1be616c95e 8215937: Check usages of security-related Resources files
weijun
parents: 48893
diff changeset
  1189
                            ? rb.getString("no.timestamp.signing")
dd1be616c95e 8215937: Check usages of security-related Resources files
weijun
parents: 48893
diff changeset
  1190
                            : rb.getString("no.timestamp.verifying"), expireDate));
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1191
                }
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1192
            }
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1193
        }
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1194
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1195
        System.out.println(result);
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1196
        if (strict) {
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1197
            if (!errors.isEmpty()) {
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1198
                System.out.println();
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1199
                System.out.println(rb.getString("Error."));
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1200
                errors.forEach(System.out::println);
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1201
            }
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1202
            if (!warnings.isEmpty()) {
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1203
                System.out.println();
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1204
                System.out.println(rb.getString("Warning."));
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1205
                warnings.forEach(System.out::println);
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1206
            }
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1207
        } else {
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1208
            if (!errors.isEmpty() || !warnings.isEmpty()) {
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1209
                System.out.println();
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1210
                System.out.println(rb.getString("Warning."));
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1211
                errors.forEach(System.out::println);
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1212
                warnings.forEach(System.out::println);
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1213
            }
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1214
        }
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1215
        if (!isSigning && (!errors.isEmpty() || !warnings.isEmpty())) {
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1216
            if (! (verbose != null && showcerts)) {
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1217
                System.out.println();
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1218
                System.out.println(rb.getString(
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1219
                        "Re.run.with.the.verbose.and.certs.options.for.more.details."));
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1220
            }
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1221
        }
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1222
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1223
        if (isSigning || verbose != null) {
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1224
            // Always print out expireDate, unless expired or expiring.
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1225
            if (!hasExpiringCert && !hasExpiredCert
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1226
                    && expireDate != null && signerNotExpired) {
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1227
                info.add(String.format(rb.getString(
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1228
                        "The.signer.certificate.will.expire.on.1."), expireDate));
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1229
            }
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1230
            if (!noTimestamp) {
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1231
                if (!hasExpiringTsaCert && !hasExpiredTsaCert && tsaExpireDate != null) {
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1232
                    if (signerNotExpired) {
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1233
                        info.add(String.format(rb.getString(
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1234
                                "The.timestamp.will.expire.on.1."), tsaExpireDate));
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1235
                    } else {
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1236
                        info.add(String.format(rb.getString(
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1237
                                "signer.cert.expired.1.but.timestamp.good.2."),
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1238
                                expireDate,
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1239
                                tsaExpireDate));
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1240
                    }
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1241
                }
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1242
            }
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1243
        }
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1244
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1245
        if (!info.isEmpty()) {
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1246
            System.out.println();
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1247
            info.forEach(System.out::println);
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1248
        }
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1249
    }
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1250
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
  1251
    private String withWeak(String alg, Set<CryptoPrimitive> primitiveSet) {
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
  1252
        if (DISABLED_CHECK.permits(primitiveSet, alg, null)) {
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
  1253
            return alg;
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
  1254
        } else {
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
  1255
            seeWeak = true;
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
  1256
            return String.format(rb.getString("with.weak"), alg);
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
  1257
        }
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
  1258
    }
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
  1259
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
  1260
    private String withWeak(PublicKey key) {
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
  1261
        if (DISABLED_CHECK.permits(SIG_PRIMITIVE_SET, key)) {
46139
5196af754957 8185934: keytool shows "Signature algorithm: SHA1withECDSA, -1-bit key"
weijun
parents: 44597
diff changeset
  1262
            int kLen = KeyUtil.getKeySize(key);
5196af754957 8185934: keytool shows "Signature algorithm: SHA1withECDSA, -1-bit key"
weijun
parents: 44597
diff changeset
  1263
            if (kLen >= 0) {
5196af754957 8185934: keytool shows "Signature algorithm: SHA1withECDSA, -1-bit key"
weijun
parents: 44597
diff changeset
  1264
                return String.format(rb.getString("key.bit"), kLen);
5196af754957 8185934: keytool shows "Signature algorithm: SHA1withECDSA, -1-bit key"
weijun
parents: 44597
diff changeset
  1265
            } else {
5196af754957 8185934: keytool shows "Signature algorithm: SHA1withECDSA, -1-bit key"
weijun
parents: 44597
diff changeset
  1266
                return rb.getString("unknown.size");
5196af754957 8185934: keytool shows "Signature algorithm: SHA1withECDSA, -1-bit key"
weijun
parents: 44597
diff changeset
  1267
            }
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
  1268
        } else {
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
  1269
            seeWeak = true;
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
  1270
            return String.format(
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
  1271
                    rb.getString("key.bit.weak"), KeyUtil.getKeySize(key));
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
  1272
        }
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
  1273
    }
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
  1274
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
    private static MessageFormat validityTimeForm = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
    private static MessageFormat notYetTimeForm = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
    private static MessageFormat expiredTimeForm = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
    private static MessageFormat expiringTimeForm = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
  1280
    /**
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
  1281
     * Returns a string about a certificate:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
     * [<tab>] <cert-type> [", " <subject-DN>] [" (" <keystore-entry-alias> ")"]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
     * [<validity-period> | <expiry-warning>]
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
  1285
     * [<key-usage-warning>]
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1286
     *
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
  1287
     * Note: no newline character at the end.
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
  1288
     *
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1289
     * This method sets global flags like hasExpiringCert, hasExpiredCert,
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1290
     * notYetValidCert, badKeyUsage, badExtendedKeyUsage, badNetscapeCertType,
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1291
     * hasExpiringTsaCert, hasExpiredTsaCert.
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
  1292
     *
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
  1293
     * @param isTsCert true if c is in the TSA cert chain, false otherwise.
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
  1294
     * @param checkUsage true to check code signer keyUsage
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
     */
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
  1296
    String printCert(boolean isTsCert, String tab, Certificate c,
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
  1297
        Date timestamp, boolean checkUsage) throws Exception {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
        StringBuilder certStr = new StringBuilder();
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
  1300
        String space = rb.getString("SPACE");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
        X509Certificate x509Cert = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
        if (c instanceof X509Certificate) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
            x509Cert = (X509Certificate) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
            certStr.append(tab).append(x509Cert.getType())
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
  1306
                .append(rb.getString("COMMA"))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
                .append(x509Cert.getSubjectDN().getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
            certStr.append(tab).append(c.getType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
        String alias = storeHash.get(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
        if (alias != null) {
55312
d8942f5d6c75 8221719: Jarsigner fails to verify signed by alias if alias given in wrong case
weijun
parents: 53398
diff changeset
  1314
            certStr.append(space).append("(").append(alias).append(")");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
  1317
        if (x509Cert != null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
            certStr.append("\n").append(tab).append("[");
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1320
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1321
            if (trustedCerts.contains(x509Cert)) {
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1322
                certStr.append(rb.getString("trusted.certificate"));
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1323
            } else {
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1324
                Date notAfter = x509Cert.getNotAfter();
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1325
                try {
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1326
                    boolean printValidity = true;
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1327
                    if (isTsCert) {
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1328
                        if (tsaExpireDate == null || tsaExpireDate.after(notAfter)) {
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1329
                            tsaExpireDate = notAfter;
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1330
                        }
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1331
                    } else {
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1332
                        if (expireDate == null || expireDate.after(notAfter)) {
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1333
                            expireDate = notAfter;
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1334
                        }
22315
529f1cc322fc 8024302: Clarify jar verifications
weijun
parents: 19189
diff changeset
  1335
                    }
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1336
                    if (timestamp == null) {
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1337
                        x509Cert.checkValidity();
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1338
                        // test if cert will expire within six months (or one year for tsa)
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1339
                        long age = isTsCert ? ONE_YEAR : SIX_MONTHS;
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1340
                        if (notAfter.getTime() < System.currentTimeMillis() + age) {
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1341
                            if (isTsCert) {
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1342
                                hasExpiringTsaCert = true;
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1343
                            } else {
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1344
                                hasExpiringCert = true;
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1345
                            }
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1346
                            if (expiringTimeForm == null) {
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1347
                                expiringTimeForm = new MessageFormat(
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1348
                                        rb.getString("certificate.will.expire.on"));
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1349
                            }
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1350
                            Object[] source = {notAfter};
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1351
                            certStr.append(expiringTimeForm.format(source));
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1352
                            printValidity = false;
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1353
                        }
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1354
                    } else {
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1355
                        x509Cert.checkValidity(timestamp);
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1356
                    }
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1357
                    if (printValidity) {
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1358
                        if (validityTimeForm == null) {
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1359
                            validityTimeForm = new MessageFormat(
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1360
                                    rb.getString("certificate.is.valid.from"));
12046
378aa3362868 7149012: jarsigner needs not warn about cert expiration if the jar has a TSA timestamp
weijun
parents: 10788
diff changeset
  1361
                        }
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1362
                        Object[] source = {x509Cert.getNotBefore(), notAfter};
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1363
                        certStr.append(validityTimeForm.format(source));
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1364
                    }
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1365
                } catch (CertificateExpiredException cee) {
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1366
                    if (isTsCert) {
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1367
                        hasExpiredTsaCert = true;
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1368
                    } else {
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1369
                        hasExpiredCert = true;
12046
378aa3362868 7149012: jarsigner needs not warn about cert expiration if the jar has a TSA timestamp
weijun
parents: 10788
diff changeset
  1370
                    }
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1371
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1372
                    if (expiredTimeForm == null) {
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1373
                        expiredTimeForm = new MessageFormat(
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1374
                                rb.getString("certificate.expired.on"));
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1375
                    }
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1376
                    Object[] source = {notAfter};
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1377
                    certStr.append(expiredTimeForm.format(source));
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1378
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1379
                } catch (CertificateNotYetValidException cnyve) {
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1380
                    if (!isTsCert) notYetValidCert = true;
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1381
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1382
                    if (notYetTimeForm == null) {
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1383
                        notYetTimeForm = new MessageFormat(
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1384
                                rb.getString("certificate.is.not.valid.until"));
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1385
                    }
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1386
                    Object[] source = {x509Cert.getNotBefore()};
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1387
                    certStr.append(notYetTimeForm.format(source));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
            certStr.append("]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
7525
16d2b5e6517a 7004168: jarsigner -verify checks for KeyUsage codesigning ext on all certs instead of just signing cert
weijun
parents: 7524
diff changeset
  1392
            if (checkUsage) {
16d2b5e6517a 7004168: jarsigner -verify checks for KeyUsage codesigning ext on all certs instead of just signing cert
weijun
parents: 7524
diff changeset
  1393
                boolean[] bad = new boolean[3];
16d2b5e6517a 7004168: jarsigner -verify checks for KeyUsage codesigning ext on all certs instead of just signing cert
weijun
parents: 7524
diff changeset
  1394
                checkCertUsage(x509Cert, bad);
16d2b5e6517a 7004168: jarsigner -verify checks for KeyUsage codesigning ext on all certs instead of just signing cert
weijun
parents: 7524
diff changeset
  1395
                if (bad[0] || bad[1] || bad[2]) {
16d2b5e6517a 7004168: jarsigner -verify checks for KeyUsage codesigning ext on all certs instead of just signing cert
weijun
parents: 7524
diff changeset
  1396
                    String x = "";
16d2b5e6517a 7004168: jarsigner -verify checks for KeyUsage codesigning ext on all certs instead of just signing cert
weijun
parents: 7524
diff changeset
  1397
                    if (bad[0]) {
16d2b5e6517a 7004168: jarsigner -verify checks for KeyUsage codesigning ext on all certs instead of just signing cert
weijun
parents: 7524
diff changeset
  1398
                        x ="KeyUsage";
16d2b5e6517a 7004168: jarsigner -verify checks for KeyUsage codesigning ext on all certs instead of just signing cert
weijun
parents: 7524
diff changeset
  1399
                    }
16d2b5e6517a 7004168: jarsigner -verify checks for KeyUsage codesigning ext on all certs instead of just signing cert
weijun
parents: 7524
diff changeset
  1400
                    if (bad[1]) {
16d2b5e6517a 7004168: jarsigner -verify checks for KeyUsage codesigning ext on all certs instead of just signing cert
weijun
parents: 7524
diff changeset
  1401
                        if (x.length() > 0) x = x + ", ";
16d2b5e6517a 7004168: jarsigner -verify checks for KeyUsage codesigning ext on all certs instead of just signing cert
weijun
parents: 7524
diff changeset
  1402
                        x = x + "ExtendedKeyUsage";
16d2b5e6517a 7004168: jarsigner -verify checks for KeyUsage codesigning ext on all certs instead of just signing cert
weijun
parents: 7524
diff changeset
  1403
                    }
16d2b5e6517a 7004168: jarsigner -verify checks for KeyUsage codesigning ext on all certs instead of just signing cert
weijun
parents: 7524
diff changeset
  1404
                    if (bad[2]) {
16d2b5e6517a 7004168: jarsigner -verify checks for KeyUsage codesigning ext on all certs instead of just signing cert
weijun
parents: 7524
diff changeset
  1405
                        if (x.length() > 0) x = x + ", ";
16d2b5e6517a 7004168: jarsigner -verify checks for KeyUsage codesigning ext on all certs instead of just signing cert
weijun
parents: 7524
diff changeset
  1406
                        x = x + "NetscapeCertType";
16d2b5e6517a 7004168: jarsigner -verify checks for KeyUsage codesigning ext on all certs instead of just signing cert
weijun
parents: 7524
diff changeset
  1407
                    }
16d2b5e6517a 7004168: jarsigner -verify checks for KeyUsage codesigning ext on all certs instead of just signing cert
weijun
parents: 7524
diff changeset
  1408
                    certStr.append("\n").append(tab)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
                        .append(MessageFormat.format(rb.getString(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
  1410
                        ".{0}.extension.does.not.support.code.signing."), x));
7525
16d2b5e6517a 7004168: jarsigner -verify checks for KeyUsage codesigning ext on all certs instead of just signing cert
weijun
parents: 7524
diff changeset
  1411
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
        return certStr.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
    private static MessageFormat signTimeForm = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
    private String printTimestamp(String tab, Timestamp timestamp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
        if (signTimeForm == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
            signTimeForm =
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
  1423
                new MessageFormat(rb.getString("entry.was.signed.on"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
        Object[] source = { timestamp.getTimestamp() };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
        return new StringBuilder().append(tab).append("[")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
            .append(signTimeForm.format(source)).append("]").toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
7977
f47f211cd627 7008713: diamond conversion of kerberos5 and security tools
smarks
parents: 7525
diff changeset
  1431
    private Map<CodeSigner,Integer> cacheForInKS = new IdentityHashMap<>();
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1433
    private int inKeyStoreForOneSigner(CodeSigner signer) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1434
        if (cacheForInKS.containsKey(signer)) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1435
            return cacheForInKS.get(signer);
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1436
        }
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1437
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1438
        int result = 0;
55312
d8942f5d6c75 8221719: Jarsigner fails to verify signed by alias if alias given in wrong case
weijun
parents: 53398
diff changeset
  1439
        if (store != null) {
d8942f5d6c75 8221719: Jarsigner fails to verify signed by alias if alias given in wrong case
weijun
parents: 53398
diff changeset
  1440
            try {
d8942f5d6c75 8221719: Jarsigner fails to verify signed by alias if alias given in wrong case
weijun
parents: 53398
diff changeset
  1441
                List<? extends Certificate> certs =
d8942f5d6c75 8221719: Jarsigner fails to verify signed by alias if alias given in wrong case
weijun
parents: 53398
diff changeset
  1442
                        signer.getSignerCertPath().getCertificates();
d8942f5d6c75 8221719: Jarsigner fails to verify signed by alias if alias given in wrong case
weijun
parents: 53398
diff changeset
  1443
                for (Certificate c : certs) {
d8942f5d6c75 8221719: Jarsigner fails to verify signed by alias if alias given in wrong case
weijun
parents: 53398
diff changeset
  1444
                    String alias = storeHash.get(c);
d8942f5d6c75 8221719: Jarsigner fails to verify signed by alias if alias given in wrong case
weijun
parents: 53398
diff changeset
  1445
                    if (alias == null) {
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1446
                        alias = store.getCertificateAlias(c);
55312
d8942f5d6c75 8221719: Jarsigner fails to verify signed by alias if alias given in wrong case
weijun
parents: 53398
diff changeset
  1447
                        if (alias != null) {
d8942f5d6c75 8221719: Jarsigner fails to verify signed by alias if alias given in wrong case
weijun
parents: 53398
diff changeset
  1448
                            storeHash.put(c, alias);
d8942f5d6c75 8221719: Jarsigner fails to verify signed by alias if alias given in wrong case
weijun
parents: 53398
diff changeset
  1449
                        }
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1450
                    }
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1451
                    if (alias != null) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1452
                        result |= IN_KEYSTORE;
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1453
                    }
55312
d8942f5d6c75 8221719: Jarsigner fails to verify signed by alias if alias given in wrong case
weijun
parents: 53398
diff changeset
  1454
                    for (String ckalias : ckaliases) {
d8942f5d6c75 8221719: Jarsigner fails to verify signed by alias if alias given in wrong case
weijun
parents: 53398
diff changeset
  1455
                        if (c.equals(store.getCertificate(ckalias))) {
d8942f5d6c75 8221719: Jarsigner fails to verify signed by alias if alias given in wrong case
weijun
parents: 53398
diff changeset
  1456
                            result |= SIGNED_BY_ALIAS;
d8942f5d6c75 8221719: Jarsigner fails to verify signed by alias if alias given in wrong case
weijun
parents: 53398
diff changeset
  1457
                            // must continue with next certificate c and cannot
d8942f5d6c75 8221719: Jarsigner fails to verify signed by alias if alias given in wrong case
weijun
parents: 53398
diff changeset
  1458
                            // return or break outer loop because has to fill
d8942f5d6c75 8221719: Jarsigner fails to verify signed by alias if alias given in wrong case
weijun
parents: 53398
diff changeset
  1459
                            // storeHash for printCert
d8942f5d6c75 8221719: Jarsigner fails to verify signed by alias if alias given in wrong case
weijun
parents: 53398
diff changeset
  1460
                            break;
d8942f5d6c75 8221719: Jarsigner fails to verify signed by alias if alias given in wrong case
weijun
parents: 53398
diff changeset
  1461
                        }
d8942f5d6c75 8221719: Jarsigner fails to verify signed by alias if alias given in wrong case
weijun
parents: 53398
diff changeset
  1462
                    }
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1463
                }
55312
d8942f5d6c75 8221719: Jarsigner fails to verify signed by alias if alias given in wrong case
weijun
parents: 53398
diff changeset
  1464
            } catch (KeyStoreException kse) {
d8942f5d6c75 8221719: Jarsigner fails to verify signed by alias if alias given in wrong case
weijun
parents: 53398
diff changeset
  1465
                // never happens, because keystore has been loaded
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1466
            }
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1467
        }
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1468
        cacheForInKS.put(signer, result);
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1469
        return result;
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1470
    }
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1471
55312
d8942f5d6c75 8221719: Jarsigner fails to verify signed by alias if alias given in wrong case
weijun
parents: 53398
diff changeset
  1472
    /**
d8942f5d6c75 8221719: Jarsigner fails to verify signed by alias if alias given in wrong case
weijun
parents: 53398
diff changeset
  1473
     * Maps certificates (as keys) to alias names associated in the keystore
d8942f5d6c75 8221719: Jarsigner fails to verify signed by alias if alias given in wrong case
weijun
parents: 53398
diff changeset
  1474
     * {@link #store} (as values).
d8942f5d6c75 8221719: Jarsigner fails to verify signed by alias if alias given in wrong case
weijun
parents: 53398
diff changeset
  1475
     */
7977
f47f211cd627 7008713: diamond conversion of kerberos5 and security tools
smarks
parents: 7525
diff changeset
  1476
    Hashtable<Certificate, String> storeHash = new Hashtable<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
    int inKeyStore(CodeSigner[] signers) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
        if (signers == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1483
        int output = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1485
        for (CodeSigner signer: signers) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1486
            int result = inKeyStoreForOneSigner(signer);
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1487
            output |= result;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
        }
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1489
        if (ckaliases.size() > 0 && (output & SIGNED_BY_ALIAS) == 0) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1490
            output |= NOT_ALIAS;
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1491
        }
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1492
        return output;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
33872
94e3836950ec 8056174: New APIs for jar signing
weijun
parents: 31270
diff changeset
  1495
    void signJar(String jarName, String alias)
34382
5d11306d6969 8130132: jarsigner should emit warning if weak algorithms or keysizes are used
weijun
parents: 33872
diff changeset
  1496
            throws Exception {
5d11306d6969 8130132: jarsigner should emit warning if weak algorithms or keysizes are used
weijun
parents: 33872
diff changeset
  1497
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
  1498
        if (digestalg != null && !DISABLED_CHECK.permits(
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
  1499
                DIGEST_PRIMITIVE_SET, digestalg, null)) {
34382
5d11306d6969 8130132: jarsigner should emit warning if weak algorithms or keysizes are used
weijun
parents: 33872
diff changeset
  1500
            weakAlg |= 1;
5d11306d6969 8130132: jarsigner should emit warning if weak algorithms or keysizes are used
weijun
parents: 33872
diff changeset
  1501
        }
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
  1502
        if (tSADigestAlg != null && !DISABLED_CHECK.permits(
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
  1503
                DIGEST_PRIMITIVE_SET, tSADigestAlg, null)) {
34382
5d11306d6969 8130132: jarsigner should emit warning if weak algorithms or keysizes are used
weijun
parents: 33872
diff changeset
  1504
            weakAlg |= 4;
5d11306d6969 8130132: jarsigner should emit warning if weak algorithms or keysizes are used
weijun
parents: 33872
diff changeset
  1505
        }
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
  1506
        if (sigalg != null && !DISABLED_CHECK.permits(
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
  1507
                SIG_PRIMITIVE_SET , sigalg, null)) {
34382
5d11306d6969 8130132: jarsigner should emit warning if weak algorithms or keysizes are used
weijun
parents: 33872
diff changeset
  1508
            weakAlg |= 2;
5d11306d6969 8130132: jarsigner should emit warning if weak algorithms or keysizes are used
weijun
parents: 33872
diff changeset
  1509
        }
41590
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
  1510
        if (!DISABLED_CHECK.permits(
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
  1511
                SIG_PRIMITIVE_SET, privateKey)) {
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
  1512
            weakAlg |= 8;
1c5b1891b8e0 8163304: jarsigner -verbose -verify should print the algorithms used to sign the jar
weijun
parents: 41582
diff changeset
  1513
        }
34382
5d11306d6969 8130132: jarsigner should emit warning if weak algorithms or keysizes are used
weijun
parents: 33872
diff changeset
  1514
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
        boolean aliasUsed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
        X509Certificate tsaCert = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
        if (sigfile == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
            sigfile = alias;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
            aliasUsed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
        if (sigfile.length() > 8) {
4152
bc36a9f01ac6 6870812: enhance security tools to use ECC algorithms
weijun
parents: 3951
diff changeset
  1524
            sigfile = sigfile.substring(0, 8).toUpperCase(Locale.ENGLISH);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
        } else {
4152
bc36a9f01ac6 6870812: enhance security tools to use ECC algorithms
weijun
parents: 3951
diff changeset
  1526
            sigfile = sigfile.toUpperCase(Locale.ENGLISH);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
        StringBuilder tmpSigFile = new StringBuilder(sigfile.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
        for (int j = 0; j < sigfile.length(); j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
            char c = sigfile.charAt(j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
            if (!
33872
94e3836950ec 8056174: New APIs for jar signing
weijun
parents: 31270
diff changeset
  1533
                    ((c>= 'A' && c<= 'Z') ||
94e3836950ec 8056174: New APIs for jar signing
weijun
parents: 31270
diff changeset
  1534
                            (c>= '0' && c<= '9') ||
94e3836950ec 8056174: New APIs for jar signing
weijun
parents: 31270
diff changeset
  1535
                            (c == '-') ||
94e3836950ec 8056174: New APIs for jar signing
weijun
parents: 31270
diff changeset
  1536
                            (c == '_'))) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
                if (aliasUsed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
                    // convert illegal characters from the alias to be _'s
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
                    c = '_';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
                } else {
33872
94e3836950ec 8056174: New APIs for jar signing
weijun
parents: 31270
diff changeset
  1541
                    throw new
94e3836950ec 8056174: New APIs for jar signing
weijun
parents: 31270
diff changeset
  1542
                            RuntimeException(rb.getString
94e3836950ec 8056174: New APIs for jar signing
weijun
parents: 31270
diff changeset
  1543
                            ("signature.filename.must.consist.of.the.following.characters.A.Z.0.9.or."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
            tmpSigFile.append(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
        sigfile = tmpSigFile.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
        String tmpJarName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
        if (signedjar == null) tmpJarName = jarName+".sig";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
        else tmpJarName = signedjar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
        File jarFile = new File(jarName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
        File signedJarFile = new File(tmpJarName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
        // Open the jar (zip) file
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
            zipFile = new ZipFile(jarName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
        } catch (IOException ioe) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
  1562
            error(rb.getString("unable.to.open.jar.file.")+jarName, ioe);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
33872
94e3836950ec 8056174: New APIs for jar signing
weijun
parents: 31270
diff changeset
  1565
        CertPath cp = CertificateFactory.getInstance("X.509")
94e3836950ec 8056174: New APIs for jar signing
weijun
parents: 31270
diff changeset
  1566
                .generateCertPath(Arrays.asList(certChain));
94e3836950ec 8056174: New APIs for jar signing
weijun
parents: 31270
diff changeset
  1567
        JarSigner.Builder builder = new JarSigner.Builder(privateKey, cp);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
33872
94e3836950ec 8056174: New APIs for jar signing
weijun
parents: 31270
diff changeset
  1569
        if (verbose != null) {
94e3836950ec 8056174: New APIs for jar signing
weijun
parents: 31270
diff changeset
  1570
            builder.eventHandler((action, file) -> {
53398
dd1be616c95e 8215937: Check usages of security-related Resources files
weijun
parents: 48893
diff changeset
  1571
                switch (action) {
dd1be616c95e 8215937: Check usages of security-related Resources files
weijun
parents: 48893
diff changeset
  1572
                    case "signing":
dd1be616c95e 8215937: Check usages of security-related Resources files
weijun
parents: 48893
diff changeset
  1573
                        System.out.println(rb.getString(".signing.") + file);
dd1be616c95e 8215937: Check usages of security-related Resources files
weijun
parents: 48893
diff changeset
  1574
                        break;
dd1be616c95e 8215937: Check usages of security-related Resources files
weijun
parents: 48893
diff changeset
  1575
                    case "adding":
dd1be616c95e 8215937: Check usages of security-related Resources files
weijun
parents: 48893
diff changeset
  1576
                        System.out.println(rb.getString(".adding.") + file);
dd1be616c95e 8215937: Check usages of security-related Resources files
weijun
parents: 48893
diff changeset
  1577
                        break;
dd1be616c95e 8215937: Check usages of security-related Resources files
weijun
parents: 48893
diff changeset
  1578
                    case "updating":
dd1be616c95e 8215937: Check usages of security-related Resources files
weijun
parents: 48893
diff changeset
  1579
                        System.out.println(rb.getString(".updating.") + file);
dd1be616c95e 8215937: Check usages of security-related Resources files
weijun
parents: 48893
diff changeset
  1580
                        break;
dd1be616c95e 8215937: Check usages of security-related Resources files
weijun
parents: 48893
diff changeset
  1581
                    default:
dd1be616c95e 8215937: Check usages of security-related Resources files
weijun
parents: 48893
diff changeset
  1582
                        throw new IllegalArgumentException("unknown action: "
dd1be616c95e 8215937: Check usages of security-related Resources files
weijun
parents: 48893
diff changeset
  1583
                                + action);
dd1be616c95e 8215937: Check usages of security-related Resources files
weijun
parents: 48893
diff changeset
  1584
                }
33872
94e3836950ec 8056174: New APIs for jar signing
weijun
parents: 31270
diff changeset
  1585
            });
94e3836950ec 8056174: New APIs for jar signing
weijun
parents: 31270
diff changeset
  1586
        }
94e3836950ec 8056174: New APIs for jar signing
weijun
parents: 31270
diff changeset
  1587
94e3836950ec 8056174: New APIs for jar signing
weijun
parents: 31270
diff changeset
  1588
        if (digestalg != null) {
94e3836950ec 8056174: New APIs for jar signing
weijun
parents: 31270
diff changeset
  1589
            builder.digestAlgorithm(digestalg);
94e3836950ec 8056174: New APIs for jar signing
weijun
parents: 31270
diff changeset
  1590
        }
94e3836950ec 8056174: New APIs for jar signing
weijun
parents: 31270
diff changeset
  1591
        if (sigalg != null) {
94e3836950ec 8056174: New APIs for jar signing
weijun
parents: 31270
diff changeset
  1592
            builder.signatureAlgorithm(sigalg);
94e3836950ec 8056174: New APIs for jar signing
weijun
parents: 31270
diff changeset
  1593
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
33872
94e3836950ec 8056174: New APIs for jar signing
weijun
parents: 31270
diff changeset
  1595
        URI tsaURI = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
33872
94e3836950ec 8056174: New APIs for jar signing
weijun
parents: 31270
diff changeset
  1597
        if (tsaUrl != null) {
94e3836950ec 8056174: New APIs for jar signing
weijun
parents: 31270
diff changeset
  1598
            tsaURI = new URI(tsaUrl);
94e3836950ec 8056174: New APIs for jar signing
weijun
parents: 31270
diff changeset
  1599
        } else if (tsaAlias != null) {
94e3836950ec 8056174: New APIs for jar signing
weijun
parents: 31270
diff changeset
  1600
            tsaCert = getTsaCert(tsaAlias);
94e3836950ec 8056174: New APIs for jar signing
weijun
parents: 31270
diff changeset
  1601
            tsaURI = TimestampedSigner.getTimestampingURI(tsaCert);
94e3836950ec 8056174: New APIs for jar signing
weijun
parents: 31270
diff changeset
  1602
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
33872
94e3836950ec 8056174: New APIs for jar signing
weijun
parents: 31270
diff changeset
  1604
        if (tsaURI != null) {
94e3836950ec 8056174: New APIs for jar signing
weijun
parents: 31270
diff changeset
  1605
            if (verbose != null) {
94e3836950ec 8056174: New APIs for jar signing
weijun
parents: 31270
diff changeset
  1606
                System.out.println(
94e3836950ec 8056174: New APIs for jar signing
weijun
parents: 31270
diff changeset
  1607
                        rb.getString("requesting.a.signature.timestamp"));
94e3836950ec 8056174: New APIs for jar signing
weijun
parents: 31270
diff changeset
  1608
                if (tsaUrl != null) {
94e3836950ec 8056174: New APIs for jar signing
weijun
parents: 31270
diff changeset
  1609
                    System.out.println(rb.getString("TSA.location.") + tsaUrl);
94e3836950ec 8056174: New APIs for jar signing
weijun
parents: 31270
diff changeset
  1610
                } else if (tsaCert != null) {
94e3836950ec 8056174: New APIs for jar signing
weijun
parents: 31270
diff changeset
  1611
                    System.out.println(rb.getString("TSA.certificate.") +
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
  1612
                            printCert(true, "", tsaCert, null, false));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
            }
33872
94e3836950ec 8056174: New APIs for jar signing
weijun
parents: 31270
diff changeset
  1615
            builder.tsa(tsaURI);
94e3836950ec 8056174: New APIs for jar signing
weijun
parents: 31270
diff changeset
  1616
            if (tSADigestAlg != null) {
94e3836950ec 8056174: New APIs for jar signing
weijun
parents: 31270
diff changeset
  1617
                builder.setProperty("tsaDigestAlg", tSADigestAlg);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
33872
94e3836950ec 8056174: New APIs for jar signing
weijun
parents: 31270
diff changeset
  1620
            if (tSAPolicyID != null) {
94e3836950ec 8056174: New APIs for jar signing
weijun
parents: 31270
diff changeset
  1621
                builder.setProperty("tsaPolicyId", tSAPolicyID);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
            }
33872
94e3836950ec 8056174: New APIs for jar signing
weijun
parents: 31270
diff changeset
  1623
        }
94e3836950ec 8056174: New APIs for jar signing
weijun
parents: 31270
diff changeset
  1624
94e3836950ec 8056174: New APIs for jar signing
weijun
parents: 31270
diff changeset
  1625
        if (altSignerClass != null) {
94e3836950ec 8056174: New APIs for jar signing
weijun
parents: 31270
diff changeset
  1626
            builder.setProperty("altSigner", altSignerClass);
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1627
            if (verbose != null) {
33872
94e3836950ec 8056174: New APIs for jar signing
weijun
parents: 31270
diff changeset
  1628
                System.out.println(
94e3836950ec 8056174: New APIs for jar signing
weijun
parents: 31270
diff changeset
  1629
                        rb.getString("using.an.alternative.signing.mechanism"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
            }
33872
94e3836950ec 8056174: New APIs for jar signing
weijun
parents: 31270
diff changeset
  1631
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
33872
94e3836950ec 8056174: New APIs for jar signing
weijun
parents: 31270
diff changeset
  1633
        if (altSignerClasspath != null) {
94e3836950ec 8056174: New APIs for jar signing
weijun
parents: 31270
diff changeset
  1634
            builder.setProperty("altSignerPath", altSignerClasspath);
94e3836950ec 8056174: New APIs for jar signing
weijun
parents: 31270
diff changeset
  1635
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
33872
94e3836950ec 8056174: New APIs for jar signing
weijun
parents: 31270
diff changeset
  1637
        builder.signerName(sigfile);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
33872
94e3836950ec 8056174: New APIs for jar signing
weijun
parents: 31270
diff changeset
  1639
        builder.setProperty("sectionsOnly", Boolean.toString(!signManifest));
94e3836950ec 8056174: New APIs for jar signing
weijun
parents: 31270
diff changeset
  1640
        builder.setProperty("internalSF", Boolean.toString(!externalSF));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
48760
25725c11c296 8196823: jarsigner should not create a signed jar if the signing fails
weijun
parents: 48543
diff changeset
  1642
        FileOutputStream fos = null;
25725c11c296 8196823: jarsigner should not create a signed jar if the signing fails
weijun
parents: 48543
diff changeset
  1643
        try {
25725c11c296 8196823: jarsigner should not create a signed jar if the signing fails
weijun
parents: 48543
diff changeset
  1644
            fos = new FileOutputStream(signedJarFile);
25725c11c296 8196823: jarsigner should not create a signed jar if the signing fails
weijun
parents: 48543
diff changeset
  1645
        } catch (IOException ioe) {
25725c11c296 8196823: jarsigner should not create a signed jar if the signing fails
weijun
parents: 48543
diff changeset
  1646
            error(rb.getString("unable.to.create.")+tmpJarName, ioe);
25725c11c296 8196823: jarsigner should not create a signed jar if the signing fails
weijun
parents: 48543
diff changeset
  1647
        }
25725c11c296 8196823: jarsigner should not create a signed jar if the signing fails
weijun
parents: 48543
diff changeset
  1648
25725c11c296 8196823: jarsigner should not create a signed jar if the signing fails
weijun
parents: 48543
diff changeset
  1649
        Throwable failedCause = null;
25725c11c296 8196823: jarsigner should not create a signed jar if the signing fails
weijun
parents: 48543
diff changeset
  1650
        String failedMessage = null;
25725c11c296 8196823: jarsigner should not create a signed jar if the signing fails
weijun
parents: 48543
diff changeset
  1651
33872
94e3836950ec 8056174: New APIs for jar signing
weijun
parents: 31270
diff changeset
  1652
        try {
94e3836950ec 8056174: New APIs for jar signing
weijun
parents: 31270
diff changeset
  1653
            builder.build().sign(zipFile, fos);
94e3836950ec 8056174: New APIs for jar signing
weijun
parents: 31270
diff changeset
  1654
        } catch (JarSignerException e) {
48760
25725c11c296 8196823: jarsigner should not create a signed jar if the signing fails
weijun
parents: 48543
diff changeset
  1655
            failedCause = e.getCause();
25725c11c296 8196823: jarsigner should not create a signed jar if the signing fails
weijun
parents: 48543
diff changeset
  1656
            if (failedCause instanceof SocketTimeoutException
25725c11c296 8196823: jarsigner should not create a signed jar if the signing fails
weijun
parents: 48543
diff changeset
  1657
                    || failedCause instanceof UnknownHostException) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
                // Provide a helpful message when TSA is beyond a firewall
48760
25725c11c296 8196823: jarsigner should not create a signed jar if the signing fails
weijun
parents: 48543
diff changeset
  1659
                failedMessage = rb.getString("unable.to.sign.jar.") +
33872
94e3836950ec 8056174: New APIs for jar signing
weijun
parents: 31270
diff changeset
  1660
                        rb.getString("no.response.from.the.Timestamping.Authority.") +
94e3836950ec 8056174: New APIs for jar signing
weijun
parents: 31270
diff changeset
  1661
                        "\n  -J-Dhttp.proxyHost=<hostname>" +
94e3836950ec 8056174: New APIs for jar signing
weijun
parents: 31270
diff changeset
  1662
                        "\n  -J-Dhttp.proxyPort=<portnumber>\n" +
94e3836950ec 8056174: New APIs for jar signing
weijun
parents: 31270
diff changeset
  1663
                        rb.getString("or") +
94e3836950ec 8056174: New APIs for jar signing
weijun
parents: 31270
diff changeset
  1664
                        "\n  -J-Dhttps.proxyHost=<hostname> " +
48760
25725c11c296 8196823: jarsigner should not create a signed jar if the signing fails
weijun
parents: 48543
diff changeset
  1665
                        "\n  -J-Dhttps.proxyPort=<portnumber> ";
33872
94e3836950ec 8056174: New APIs for jar signing
weijun
parents: 31270
diff changeset
  1666
            } else {
48760
25725c11c296 8196823: jarsigner should not create a signed jar if the signing fails
weijun
parents: 48543
diff changeset
  1667
                // JarSignerException might have a null cause
25725c11c296 8196823: jarsigner should not create a signed jar if the signing fails
weijun
parents: 48543
diff changeset
  1668
                if (failedCause == null) {
25725c11c296 8196823: jarsigner should not create a signed jar if the signing fails
weijun
parents: 48543
diff changeset
  1669
                    failedCause = e;
25725c11c296 8196823: jarsigner should not create a signed jar if the signing fails
weijun
parents: 48543
diff changeset
  1670
                }
25725c11c296 8196823: jarsigner should not create a signed jar if the signing fails
weijun
parents: 48543
diff changeset
  1671
                failedMessage = rb.getString("unable.to.sign.jar.") + failedCause;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
            }
48760
25725c11c296 8196823: jarsigner should not create a signed jar if the signing fails
weijun
parents: 48543
diff changeset
  1673
        } catch (Exception e) {
25725c11c296 8196823: jarsigner should not create a signed jar if the signing fails
weijun
parents: 48543
diff changeset
  1674
            failedCause = e;
25725c11c296 8196823: jarsigner should not create a signed jar if the signing fails
weijun
parents: 48543
diff changeset
  1675
            failedMessage = rb.getString("unable.to.sign.jar.") + failedCause;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
        } finally {
48760
25725c11c296 8196823: jarsigner should not create a signed jar if the signing fails
weijun
parents: 48543
diff changeset
  1677
            // close the resources
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
            if (zipFile != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
                zipFile.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
                zipFile = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
33872
94e3836950ec 8056174: New APIs for jar signing
weijun
parents: 31270
diff changeset
  1683
            if (fos != null) {
94e3836950ec 8056174: New APIs for jar signing
weijun
parents: 31270
diff changeset
  1684
                fos.close();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
            }
48760
25725c11c296 8196823: jarsigner should not create a signed jar if the signing fails
weijun
parents: 48543
diff changeset
  1686
25725c11c296 8196823: jarsigner should not create a signed jar if the signing fails
weijun
parents: 48543
diff changeset
  1687
        }
25725c11c296 8196823: jarsigner should not create a signed jar if the signing fails
weijun
parents: 48543
diff changeset
  1688
25725c11c296 8196823: jarsigner should not create a signed jar if the signing fails
weijun
parents: 48543
diff changeset
  1689
        if (failedCause != null) {
25725c11c296 8196823: jarsigner should not create a signed jar if the signing fails
weijun
parents: 48543
diff changeset
  1690
            signedJarFile.delete();
25725c11c296 8196823: jarsigner should not create a signed jar if the signing fails
weijun
parents: 48543
diff changeset
  1691
            error(failedMessage, failedCause);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1694
        if (verbose != null) {
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1695
            System.out.println();
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1696
        }
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1697
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
  1698
        // The JarSigner API always accepts the timestamp received.
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
  1699
        // We need to extract the certs from the signed jar to
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
  1700
        // validate it.
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1701
        try (JarFile check = new JarFile(signedJarFile)) {
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1702
            PKCS7 p7 = new PKCS7(check.getInputStream(check.getEntry(
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1703
                    "META-INF/" + sigfile + "." + privateKey.getAlgorithm())));
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1704
            Timestamp ts = null;
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1705
            try {
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
  1706
                SignerInfo si = p7.getSignerInfos()[0];
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1707
                if (si.getTsToken() != null) {
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1708
                    hasTimestampBlock = true;
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
  1709
                }
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1710
                ts = si.getTimestamp();
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
  1711
            } catch (Exception e) {
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1712
                tsaChainNotValidated = true;
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1713
                tsaChainNotValidatedReason = e;
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1714
            }
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1715
            // Spaces before the ">>> Signer" and other lines are different
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1716
            String result = certsAndTSInfo("", "    ", Arrays.asList(certChain), ts);
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1717
            if (verbose != null) {
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1718
                System.out.println(result);
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1719
            }
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1720
        } catch (Exception e) {
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1721
            if (debug) {
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1722
                e.printStackTrace();
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1723
            }
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1724
        }
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1725
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1726
        if (signedjar == null) {
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1727
            // attempt an atomic rename. If that fails,
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1728
            // rename the original jar file, then the signed
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1729
            // one, then delete the original.
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1730
            if (!signedJarFile.renameTo(jarFile)) {
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1731
                File origJar = new File(jarName+".orig");
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1732
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1733
                if (jarFile.renameTo(origJar)) {
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1734
                    if (signedJarFile.renameTo(jarFile)) {
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1735
                        origJar.delete();
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1736
                    } else {
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1737
                        MessageFormat form = new MessageFormat(rb.getString
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1738
                    ("attempt.to.rename.signedJarFile.to.jarFile.failed"));
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1739
                        Object[] source = {signedJarFile, jarFile};
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1740
                        error(form.format(source));
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1741
                    }
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1742
                } else {
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1743
                    MessageFormat form = new MessageFormat(rb.getString
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1744
                        ("attempt.to.rename.jarFile.to.origJar.failed"));
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1745
                    Object[] source = {jarFile, origJar};
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1746
                    error(form.format(source));
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
  1747
                }
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
  1748
            }
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
  1749
        }
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
  1750
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1751
        displayMessagesAndResult(true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
     * signature-related files include:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
     * . META-INF/MANIFEST.MF
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
     * . META-INF/SIG-*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
     * . META-INF/*.SF
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
     * . META-INF/*.DSA
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
     * . META-INF/*.RSA
4152
bc36a9f01ac6 6870812: enhance security tools to use ECC algorithms
weijun
parents: 3951
diff changeset
  1761
     * . META-INF/*.EC
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
    private boolean signatureRelated(String name) {
23912
9eab25093a89 8026067: Enhance signed jar verification
weijun
parents: 23010
diff changeset
  1764
        return SignatureFileVerifier.isSigningRelated(name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
7977
f47f211cd627 7008713: diamond conversion of kerberos5 and security tools
smarks
parents: 7525
diff changeset
  1767
    Map<CodeSigner,String> cacheForSignerInfo = new IdentityHashMap<>();
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1768
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1769
    /**
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1770
     * Returns a string of signer info, with a newline at the end.
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1771
     * Called by verifyJar().
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1772
     */
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
  1773
    private String signerInfo(CodeSigner signer, String tab) throws Exception {
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1774
        if (cacheForSignerInfo.containsKey(signer)) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1775
            return cacheForSignerInfo.get(signer);
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1776
        }
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1777
        List<? extends Certificate> certs = signer.getSignerCertPath().getCertificates();
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1778
        // signing time is only displayed on verification
12046
378aa3362868 7149012: jarsigner needs not warn about cert expiration if the jar has a TSA timestamp
weijun
parents: 10788
diff changeset
  1779
        Timestamp ts = signer.getTimestamp();
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1780
        String tsLine = "";
12046
378aa3362868 7149012: jarsigner needs not warn about cert expiration if the jar has a TSA timestamp
weijun
parents: 10788
diff changeset
  1781
        if (ts != null) {
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1782
            tsLine = printTimestamp(tab, ts) + "\n";
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1783
        }
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1784
        // Spaces before the ">>> Signer" and other lines are the same.
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1785
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1786
        String result = certsAndTSInfo(tab, tab, certs, ts);
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1787
        cacheForSignerInfo.put(signer, tsLine + result);
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1788
        return result;
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1789
    }
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1790
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1791
    /**
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1792
     * Fills info on certs and timestamp into a StringBuilder, sets
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1793
     * warning flags (through printCert) and validates cert chains.
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1794
     *
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1795
     * @param tab1 spaces before the ">>> Signer" line
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1796
     * @param tab2 spaces before the other lines
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1797
     * @param certs the signer cert
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1798
     * @param ts the timestamp, can be null
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1799
     * @return the info as a string
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1800
     */
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1801
    private String certsAndTSInfo(
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1802
            String tab1,
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1803
            String tab2,
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1804
            List<? extends Certificate> certs, Timestamp ts)
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1805
            throws Exception {
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1806
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1807
        Date timestamp;
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1808
        if (ts != null) {
12046
378aa3362868 7149012: jarsigner needs not warn about cert expiration if the jar has a TSA timestamp
weijun
parents: 10788
diff changeset
  1809
            timestamp = ts.getTimestamp();
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1810
            noTimestamp = false;
12046
378aa3362868 7149012: jarsigner needs not warn about cert expiration if the jar has a TSA timestamp
weijun
parents: 10788
diff changeset
  1811
        } else {
378aa3362868 7149012: jarsigner needs not warn about cert expiration if the jar has a TSA timestamp
weijun
parents: 10788
diff changeset
  1812
            timestamp = null;
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1813
        }
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 24868
diff changeset
  1814
        // display the certificate(sb). The first one is end-entity cert and
7525
16d2b5e6517a 7004168: jarsigner -verify checks for KeyUsage codesigning ext on all certs instead of just signing cert
weijun
parents: 7524
diff changeset
  1815
        // its KeyUsage should be checked.
16d2b5e6517a 7004168: jarsigner -verify checks for KeyUsage codesigning ext on all certs instead of just signing cert
weijun
parents: 7524
diff changeset
  1816
        boolean first = true;
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1817
        StringBuilder sb = new StringBuilder();
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1818
        sb.append(tab1).append(rb.getString("...Signer")).append('\n');
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1819
        for (Certificate c : certs) {
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1820
            sb.append(printCert(false, tab2, c, timestamp, first));
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 24868
diff changeset
  1821
            sb.append('\n');
7525
16d2b5e6517a 7004168: jarsigner -verify checks for KeyUsage codesigning ext on all certs instead of just signing cert
weijun
parents: 7524
diff changeset
  1822
            first = false;
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1823
        }
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1824
        try {
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
  1825
            validateCertChain(Validator.VAR_CODE_SIGNING, certs, ts);
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1826
        } catch (Exception e) {
43183
b50e0f90d284 8172529: Use PKIXValidator in jarsigner
weijun
parents: 41590
diff changeset
  1827
            chainNotValidated = true;
b50e0f90d284 8172529: Use PKIXValidator in jarsigner
weijun
parents: 41590
diff changeset
  1828
            chainNotValidatedReason = e;
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1829
            sb.append(tab2).append(rb.getString(".Invalid.certificate.chain."))
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
  1830
                    .append(e.getLocalizedMessage()).append("]\n");
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
  1831
        }
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
  1832
        if (ts != null) {
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1833
            sb.append(tab1).append(rb.getString("...TSA")).append('\n');
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
  1834
            for (Certificate c : ts.getSignerCertPath().getCertificates()) {
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1835
                sb.append(printCert(true, tab2, c, null, false));
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
  1836
                sb.append('\n');
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
  1837
            }
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
  1838
            try {
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
  1839
                validateCertChain(Validator.VAR_TSA_SERVER,
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
  1840
                        ts.getSignerCertPath().getCertificates(), null);
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
  1841
            } catch (Exception e) {
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
  1842
                tsaChainNotValidated = true;
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
  1843
                tsaChainNotValidatedReason = e;
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1844
                sb.append(tab2).append(rb.getString(".Invalid.TSA.certificate.chain."))
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
  1845
                        .append(e.getLocalizedMessage()).append("]\n");
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
  1846
            }
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1847
        }
34382
5d11306d6969 8130132: jarsigner should emit warning if weak algorithms or keysizes are used
weijun
parents: 33872
diff changeset
  1848
        if (certs.size() == 1
5d11306d6969 8130132: jarsigner should emit warning if weak algorithms or keysizes are used
weijun
parents: 33872
diff changeset
  1849
                && KeyStoreUtil.isSelfSigned((X509Certificate)certs.get(0))) {
5d11306d6969 8130132: jarsigner should emit warning if weak algorithms or keysizes are used
weijun
parents: 33872
diff changeset
  1850
            signerSelfSigned = true;
5d11306d6969 8130132: jarsigner should emit warning if weak algorithms or keysizes are used
weijun
parents: 33872
diff changeset
  1851
        }
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1852
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1853
        return sb.toString();
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1854
    }
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1855
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
    void loadKeyStore(String keyStoreName, boolean prompt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
        if (!nullStream && keyStoreName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
            keyStoreName = System.getProperty("user.home") + File.separator
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
                + ".keystore";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
        try {
10427
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1864
            try {
14182
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 12046
diff changeset
  1865
                KeyStore caks = KeyStoreUtil.getCacertsKeyStore();
10427
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1866
                if (caks != null) {
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1867
                    Enumeration<String> aliases = caks.aliases();
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1868
                    while (aliases.hasMoreElements()) {
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1869
                        String a = aliases.nextElement();
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1870
                        try {
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1871
                            trustedCerts.add((X509Certificate)caks.getCertificate(a));
10427
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1872
                        } catch (Exception e2) {
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1873
                            // ignore, when a SecretkeyEntry does not include a cert
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1874
                        }
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1875
                    }
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1876
                }
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1877
            } catch (Exception e) {
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1878
                // Ignore, if cacerts cannot be loaded
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1879
            }
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1880
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
            if (providerName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
                store = KeyStore.getInstance(storetype);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
                store = KeyStore.getInstance(storetype, providerName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
            // Get pass phrase
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
            // XXX need to disable echo; on UNIX, call getpass(char *prompt)Z
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
            // and on NT call ??
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
            if (token && storepass == null && !protectedPath
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
                    && !KeyStoreUtil.isWindowsKeyStore(storetype)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
                storepass = getPass
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
  1893
                        (rb.getString("Enter.Passphrase.for.keystore."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
            } else if (!token && storepass == null && prompt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
                storepass = getPass
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
  1896
                        (rb.getString("Enter.Passphrase.for.keystore."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
10427
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1899
            try {
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1900
                if (nullStream) {
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1901
                    store.load(null, storepass);
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1902
                } else {
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1903
                    keyStoreName = keyStoreName.replace(File.separatorChar, '/');
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1904
                    URL url = null;
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1905
                    try {
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1906
                        url = new URL(keyStoreName);
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1907
                    } catch (java.net.MalformedURLException e) {
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1908
                        // try as file
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1909
                        url = new File(keyStoreName).toURI().toURL();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
                    }
10427
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1911
                    InputStream is = null;
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1912
                    try {
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1913
                        is = url.openStream();
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1914
                        store.load(is, storepass);
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1915
                    } finally {
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1916
                        if (is != null) {
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1917
                            is.close();
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1918
                        }
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1919
                    }
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1920
                }
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1921
                Enumeration<String> aliases = store.aliases();
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1922
                while (aliases.hasMoreElements()) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1923
                    String a = aliases.nextElement();
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1924
                    try {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1925
                        X509Certificate c = (X509Certificate)store.getCertificate(a);
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1926
                        // Only add TrustedCertificateEntry and self-signed
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1927
                        // PrivateKeyEntry
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1928
                        if (store.isCertificateEntry(a) ||
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1929
                                c.getSubjectDN().equals(c.getIssuerDN())) {
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1930
                            trustedCerts.add(c);
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1931
                        }
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1932
                    } catch (Exception e2) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1933
                        // ignore, when a SecretkeyEntry does not include a cert
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1934
                    }
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1935
                }
10427
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1936
            } finally {
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1937
                try {
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1938
                    pkixParameters = new PKIXBuilderParameters(
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1939
                            trustedCerts.stream()
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1940
                                    .map(c -> new TrustAnchor(c, null))
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1941
                                    .collect(Collectors.toSet()),
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  1942
                            null);
10427
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1943
                    pkixParameters.setRevocationEnabled(false);
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1944
                } catch (InvalidAlgorithmParameterException ex) {
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1945
                    // Only if tas is empty
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1946
                }
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1947
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
        } catch (IOException ioe) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
  1949
            throw new RuntimeException(rb.getString("keystore.load.") +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
                                        ioe.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
        } catch (java.security.cert.CertificateException ce) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
  1952
            throw new RuntimeException(rb.getString("certificate.exception.") +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
                                        ce.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
        } catch (NoSuchProviderException pe) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
  1955
            throw new RuntimeException(rb.getString("keystore.load.") +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
                                        pe.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
        } catch (NoSuchAlgorithmException nsae) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
  1958
            throw new RuntimeException(rb.getString("keystore.load.") +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
                                        nsae.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
        } catch (KeyStoreException kse) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
            throw new RuntimeException
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
  1962
                (rb.getString("unable.to.instantiate.keystore.class.") +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
                kse.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
    X509Certificate getTsaCert(String alias) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
        java.security.cert.Certificate cs = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
            cs = store.getCertificate(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
        } catch (KeyStoreException kse) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
            // this never happens, because keystore has been loaded
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
        if (cs == null || (!(cs instanceof X509Certificate))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
            MessageFormat form = new MessageFormat(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
  1978
                ("Certificate.not.found.for.alias.alias.must.reference.a.valid.KeyStore.entry.containing.an.X.509.public.key.certificate.for.the"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
            Object[] source = {alias, alias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
            error(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
        return (X509Certificate) cs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
     * Check if userCert is designed to be a code signer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
     * @param userCert the certificate to be examined
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
     * @param bad 3 booleans to show if the KeyUsage, ExtendedKeyUsage,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
     *            NetscapeCertType has codeSigning flag turned on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
     *            If null, the class field badKeyUsage, badExtendedKeyUsage,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
     *            badNetscapeCertType will be set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
    void checkCertUsage(X509Certificate userCert, boolean[] bad) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
        // Can act as a signer?
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1996
        // 1. if KeyUsage, then [0:digitalSignature] or
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1997
        //    [1:nonRepudiation] should be true
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
        // 2. if ExtendedKeyUsage, then should contains ANY or CODE_SIGNING
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
        // 3. if NetscapeCertType, then should contains OBJECT_SIGNING
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
        // 1,2,3 must be true
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
        if (bad != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
            bad[0] = bad[1] = bad[2] = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
        boolean[] keyUsage = userCert.getKeyUsage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
        if (keyUsage != null) {
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  2008
            keyUsage = Arrays.copyOf(keyUsage, 9);
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  2009
            if (!keyUsage[0] && !keyUsage[1]) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
                if (bad != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
                    bad[0] = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
                    badKeyUsage = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
            List<String> xKeyUsage = userCert.getExtendedKeyUsage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
            if (xKeyUsage != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
                if (!xKeyUsage.contains("2.5.29.37.0") // anyExtendedKeyUsage
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
                        && !xKeyUsage.contains("1.3.6.1.5.5.7.3.3")) {  // codeSigning
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
                    if (bad != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
                        bad[1] = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
                        badExtendedKeyUsage = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
        } catch (java.security.cert.CertificateParsingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
            // shouldn't happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
            // OID_NETSCAPE_CERT_TYPE
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
            byte[] netscapeEx = userCert.getExtensionValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
                    ("2.16.840.1.113730.1.1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
            if (netscapeEx != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
                DerInputStream in = new DerInputStream(netscapeEx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
                byte[] encoded = in.getOctetString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
                encoded = new DerValue(encoded).getUnalignedBitString()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
                        .toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
                NetscapeCertTypeExtension extn =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
                        new NetscapeCertTypeExtension(encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9011
diff changeset
  2045
                Boolean val = extn.get(NetscapeCertTypeExtension.OBJECT_SIGNING);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
                if (!val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
                    if (bad != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
                        bad[2] = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2049
                        badNetscapeCertType = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  2058
    // Called by signJar().
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
  2059
    void getAliasInfo(String alias) throws Exception {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
        Key key = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
            java.security.cert.Certificate[] cs = null;
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  2065
            if (altCertChain != null) {
21642
5efc900f8ecd 8027991: InputStream should be closed in sun.security.tools.jarsigner.Main
weijun
parents: 21278
diff changeset
  2066
                try (FileInputStream fis = new FileInputStream(altCertChain)) {
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  2067
                    cs = CertificateFactory.getInstance("X.509").
21642
5efc900f8ecd 8027991: InputStream should be closed in sun.security.tools.jarsigner.Main
weijun
parents: 21278
diff changeset
  2068
                            generateCertificates(fis).
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  2069
                            toArray(new Certificate[0]);
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  2070
                } catch (FileNotFoundException ex) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
  2071
                    error(rb.getString("File.specified.by.certchain.does.not.exist"));
21642
5efc900f8ecd 8027991: InputStream should be closed in sun.security.tools.jarsigner.Main
weijun
parents: 21278
diff changeset
  2072
                } catch (CertificateException | IOException ex) {
5efc900f8ecd 8027991: InputStream should be closed in sun.security.tools.jarsigner.Main
weijun
parents: 21278
diff changeset
  2073
                    error(rb.getString("Cannot.restore.certchain.from.file.specified"));
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  2074
                }
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  2075
            } else {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  2076
                try {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  2077
                    cs = store.getCertificateChain(alias);
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  2078
                } catch (KeyStoreException kse) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  2079
                    // this never happens, because keystore has been loaded
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  2080
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2081
            }
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  2082
            if (cs == null || cs.length == 0) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  2083
                if (altCertChain != null) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  2084
                    error(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
  2085
                            ("Certificate.chain.not.found.in.the.file.specified."));
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  2086
                } else {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  2087
                    MessageFormat form = new MessageFormat(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
  2088
                        ("Certificate.chain.not.found.for.alias.alias.must.reference.a.valid.KeyStore.key.entry.containing.a.private.key.and"));
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  2089
                    Object[] source = {alias, alias};
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  2090
                    error(form.format(source));
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  2091
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
            certChain = new X509Certificate[cs.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
            for (int i=0; i<cs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
                if (!(cs[i] instanceof X509Certificate)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
                    error(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
  2098
                        ("found.non.X.509.certificate.in.signer.s.chain"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
                certChain[i] = (X509Certificate)cs[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
                if (!token && keypass == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
                    key = store.getKey(alias, storepass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
                    key = store.getKey(alias, keypass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
            } catch (UnrecoverableKeyException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
                if (token) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
                    throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
                } else if (keypass == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
                    // Did not work out, so prompt user for key password
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
                    MessageFormat form = new MessageFormat(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
  2114
                        ("Enter.key.password.for.alias."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
                    Object[] source = {alias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
                    keypass = getPass(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
                    key = store.getKey(alias, keypass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
        } catch (NoSuchAlgorithmException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
            error(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
        } catch (UnrecoverableKeyException e) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
  2123
            error(rb.getString("unable.to.recover.key.from.keystore"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
        } catch (KeyStoreException kse) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
            // this never happens, because keystore has been loaded
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
        if (!(key instanceof PrivateKey)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
            MessageFormat form = new MessageFormat(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
  2130
                ("key.associated.with.alias.not.a.private.key"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
            Object[] source = {alias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
            error(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
            privateKey = (PrivateKey)key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
33872
94e3836950ec 8056174: New APIs for jar signing
weijun
parents: 31270
diff changeset
  2138
    void error(String message) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
  2139
        System.out.println(rb.getString("jarsigner.")+message);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
        System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2142
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2143
33872
94e3836950ec 8056174: New APIs for jar signing
weijun
parents: 31270
diff changeset
  2144
    void error(String message, Throwable e) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
  2145
        System.out.println(rb.getString("jarsigner.")+message);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2146
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2147
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2149
        System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
  2152
    /**
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
  2153
     * Validates a cert chain.
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
  2154
     *
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
  2155
     * @param parameter this might be a timestamp
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
  2156
     */
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
  2157
    void validateCertChain(String variant, List<? extends Certificate> certs,
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  2158
                           Timestamp parameter)
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
  2159
            throws Exception {
43183
b50e0f90d284 8172529: Use PKIXValidator in jarsigner
weijun
parents: 41590
diff changeset
  2160
        try {
b50e0f90d284 8172529: Use PKIXValidator in jarsigner
weijun
parents: 41590
diff changeset
  2161
            Validator.getInstance(Validator.TYPE_PKIX,
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
  2162
                    variant,
43183
b50e0f90d284 8172529: Use PKIXValidator in jarsigner
weijun
parents: 41590
diff changeset
  2163
                    pkixParameters)
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
  2164
                    .validate(certs.toArray(new X509Certificate[certs.size()]),
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
  2165
                            null, parameter);
43183
b50e0f90d284 8172529: Use PKIXValidator in jarsigner
weijun
parents: 41590
diff changeset
  2166
        } catch (Exception e) {
b50e0f90d284 8172529: Use PKIXValidator in jarsigner
weijun
parents: 41590
diff changeset
  2167
            if (debug) {
b50e0f90d284 8172529: Use PKIXValidator in jarsigner
weijun
parents: 41590
diff changeset
  2168
                e.printStackTrace();
b50e0f90d284 8172529: Use PKIXValidator in jarsigner
weijun
parents: 41590
diff changeset
  2169
            }
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
  2170
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
  2171
            // Exception might be dismissed if another warning flag
48893
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  2172
            // is already set by printCert.
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  2173
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  2174
            if (variant.equals(Validator.VAR_TSA_SERVER) &&
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  2175
                    e instanceof ValidatorException) {
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  2176
                // Throw cause if it's CertPathValidatorException,
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  2177
                if (e.getCause() != null &&
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  2178
                        e.getCause() instanceof CertPathValidatorException) {
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  2179
                    e = (Exception) e.getCause();
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  2180
                    Throwable t = e.getCause();
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  2181
                    if ((t instanceof CertificateExpiredException &&
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  2182
                            hasExpiredTsaCert)) {
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  2183
                        // we already have hasExpiredTsaCert
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  2184
                        return;
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  2185
                    }
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  2186
                }
454518b338b0 8191438: jarsigner should print when a timestamp will expire
weijun
parents: 48760
diff changeset
  2187
            }
47469
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
  2188
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
  2189
            if (variant.equals(Validator.VAR_CODE_SIGNING) &&
6ae08c311cd3 8180289: jarsigner treats timestamped signed jar invalid after the signer cert expires
weijun
parents: 47216
diff changeset
  2190
                    e instanceof ValidatorException) {
43183
b50e0f90d284 8172529: Use PKIXValidator in jarsigner
weijun
parents: 41590
diff changeset
  2191
                // Throw cause if it's CertPathValidatorException,
b50e0f90d284 8172529: Use PKIXValidator in jarsigner
weijun
parents: 41590
diff changeset
  2192
                if (e.getCause() != null &&
b50e0f90d284 8172529: Use PKIXValidator in jarsigner
weijun
parents: 41590
diff changeset
  2193
                        e.getCause() instanceof CertPathValidatorException) {
b50e0f90d284 8172529: Use PKIXValidator in jarsigner
weijun
parents: 41590
diff changeset
  2194
                    e = (Exception) e.getCause();
b50e0f90d284 8172529: Use PKIXValidator in jarsigner
weijun
parents: 41590
diff changeset
  2195
                    Throwable t = e.getCause();
b50e0f90d284 8172529: Use PKIXValidator in jarsigner
weijun
parents: 41590
diff changeset
  2196
                    if ((t instanceof CertificateExpiredException &&
b50e0f90d284 8172529: Use PKIXValidator in jarsigner
weijun
parents: 41590
diff changeset
  2197
                                hasExpiredCert) ||
b50e0f90d284 8172529: Use PKIXValidator in jarsigner
weijun
parents: 41590
diff changeset
  2198
                            (t instanceof CertificateNotYetValidException &&
b50e0f90d284 8172529: Use PKIXValidator in jarsigner
weijun
parents: 41590
diff changeset
  2199
                                    notYetValidCert)) {
b50e0f90d284 8172529: Use PKIXValidator in jarsigner
weijun
parents: 41590
diff changeset
  2200
                        // we already have hasExpiredCert and notYetValidCert
b50e0f90d284 8172529: Use PKIXValidator in jarsigner
weijun
parents: 41590
diff changeset
  2201
                        return;
b50e0f90d284 8172529: Use PKIXValidator in jarsigner
weijun
parents: 41590
diff changeset
  2202
                    }
b50e0f90d284 8172529: Use PKIXValidator in jarsigner
weijun
parents: 41590
diff changeset
  2203
                }
b50e0f90d284 8172529: Use PKIXValidator in jarsigner
weijun
parents: 41590
diff changeset
  2204
                if (e instanceof ValidatorException) {
b50e0f90d284 8172529: Use PKIXValidator in jarsigner
weijun
parents: 41590
diff changeset
  2205
                    ValidatorException ve = (ValidatorException)e;
b50e0f90d284 8172529: Use PKIXValidator in jarsigner
weijun
parents: 41590
diff changeset
  2206
                    if (ve.getErrorType() == ValidatorException.T_EE_EXTENSIONS &&
b50e0f90d284 8172529: Use PKIXValidator in jarsigner
weijun
parents: 41590
diff changeset
  2207
                            (badKeyUsage || badExtendedKeyUsage || badNetscapeCertType)) {
b50e0f90d284 8172529: Use PKIXValidator in jarsigner
weijun
parents: 41590
diff changeset
  2208
                        // We already have badKeyUsage, badExtendedKeyUsage
b50e0f90d284 8172529: Use PKIXValidator in jarsigner
weijun
parents: 41590
diff changeset
  2209
                        // and badNetscapeCertType
b50e0f90d284 8172529: Use PKIXValidator in jarsigner
weijun
parents: 41590
diff changeset
  2210
                        return;
b50e0f90d284 8172529: Use PKIXValidator in jarsigner
weijun
parents: 41590
diff changeset
  2211
                    }
24625
22fb8a68756f 8036709: Java 7 jarsigner displays warning about cert policy tree
weijun
parents: 24034
diff changeset
  2212
                }
22fb8a68756f 8036709: Java 7 jarsigner displays warning about cert policy tree
weijun
parents: 24034
diff changeset
  2213
            }
43183
b50e0f90d284 8172529: Use PKIXValidator in jarsigner
weijun
parents: 41590
diff changeset
  2214
            throw e;
24625
22fb8a68756f 8036709: Java 7 jarsigner displays warning about cert policy tree
weijun
parents: 24034
diff changeset
  2215
        }
22fb8a68756f 8036709: Java 7 jarsigner displays warning about cert policy tree
weijun
parents: 24034
diff changeset
  2216
    }
22fb8a68756f 8036709: Java 7 jarsigner displays warning about cert policy tree
weijun
parents: 24034
diff changeset
  2217
33872
94e3836950ec 8056174: New APIs for jar signing
weijun
parents: 31270
diff changeset
  2218
    char[] getPass(String prompt) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2219
        System.err.print(prompt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2220
        System.err.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2221
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2222
            char[] pass = Password.readPassword(System.in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2223
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2224
            if (pass == null) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
  2225
                error(rb.getString("you.must.enter.key.password"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2226
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2227
                return pass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2228
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2229
        } catch (IOException ioe) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
  2230
            error(rb.getString("unable.to.read.password.")+ioe.getMessage());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2231
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2232
        // this shouldn't happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2233
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2234
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2235
}