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