jdk/src/share/classes/sun/security/tools/jarsigner/Main.java
author sflores
Sun, 14 Oct 2012 22:58:59 +0100
changeset 14182 3041082abb40
parent 12046 jdk/src/share/classes/sun/security/tools/JarSigner.java@378aa3362868
child 14421 a64b2cc9d429
permissions -rw-r--r--
7194449: String resources for Key Tool and Policy Tool should be in their respective packages Reviewed-by: alanb, weijun, mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14182
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 12046
diff changeset
     2
 * Copyright (c) 1997, 2012, 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.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.zip.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.jar.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.math.BigInteger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.net.URI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.net.URISyntaxException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.text.Collator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.text.MessageFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.cert.Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.security.cert.X509Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.security.cert.CertificateException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.lang.reflect.Constructor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import com.sun.jarsigner.ContentSigner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import com.sun.jarsigner.ContentSignerParameters;
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
    45
import java.net.SocketTimeoutException;
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
    46
import java.net.URL;
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
    47
import java.net.URLClassLoader;
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
    48
import java.security.cert.CertPath;
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
    49
import java.security.cert.CertPathValidator;
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
    50
import java.security.cert.CertificateExpiredException;
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
    51
import java.security.cert.CertificateFactory;
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
    52
import java.security.cert.CertificateNotYetValidException;
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
    53
import java.security.cert.PKIXParameters;
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
    54
import java.security.cert.TrustAnchor;
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
    55
import java.util.Map.Entry;
14182
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 12046
diff changeset
    56
import sun.security.tools.KeyStoreUtil;
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.PathList;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
import sun.security.x509.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
import sun.security.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
import sun.misc.BASE64Encoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
    62
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * <p>The jarsigner utility.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
    66
 * The exit codes for the main method are:
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
    67
 *
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
    68
 * 0: success
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
    69
 * 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
    70
 *      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
    71
 *      TSP communication error
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
    72
 *      jarsigner command line error...
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
    73
 * otherwise: error codes from -strict
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
    74
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * @author Roland Schemers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * @author Jan Luehe
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 */
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 META_INF = "META-INF/";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    // prefix for new signature-related files in META-INF directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    private static final String SIG_PREFIX = META_INF + "SIG-";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    private static final Class[] PARAM_STRING = { String.class };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    private static final String NONE = "NONE";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    private static final String P11KEYSTORE = "PKCS11";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    private static final long SIX_MONTHS = 180*24*60*60*1000L; //milliseconds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    // Attention:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    // This is the entry that get launched by the security tool jarsigner.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    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
   106
        Main js = new Main();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        js.run(args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    static final String VERSION = "1.0";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   112
    static final int IN_KEYSTORE = 0x01;        // signer is in keystore
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    static final int IN_SCOPE = 0x02;
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   114
    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
   115
                                                // signer is not in alias list
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   116
    static final int SIGNED_BY_ALIAS = 0x08;    // signer is in alias list
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   118
    X509Certificate[] certChain;    // signer's cert chain (when composing)
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   119
    PrivateKey privateKey;          // private key
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   120
    KeyStore store;                 // the keystore specified by -keystore
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   121
                                    // or the default keystore, never null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    String keystore; // key store file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    boolean nullStream = false; // null keystore input stream (NONE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    boolean token = false; // token-based keystore
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5461
diff changeset
   126
    String jarfile;  // jar files to sign or verify
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    String alias;    // alias to sign jar with
7977
f47f211cd627 7008713: diamond conversion of kerberos5 and security tools
smarks
parents: 7525
diff changeset
   128
    List<String> ckaliases = new ArrayList<>(); // aliases in -verify
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    char[] storepass; // keystore password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    boolean protectedPath; // protected authentication path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    String storetype; // keystore type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    String providerName; // provider name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    Vector<String> providers = null; // list of providers
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   134
    // arguments for provider constructors
7977
f47f211cd627 7008713: diamond conversion of kerberos5 and security tools
smarks
parents: 7525
diff changeset
   135
    HashMap<String,String> providerArgs = new HashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    char[] keypass; // private key password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    String sigfile; // name of .SF file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    String sigalg; // name of signature algorithm
3318
dade78e63c92 6561126: keytool should use larger default keysize for keypairs
weijun
parents: 3047
diff changeset
   139
    String digestalg = "SHA-256"; // name of digest algorithm
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    String signedjar; // output filename
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    String tsaUrl; // location of the Timestamping Authority
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    String tsaAlias; // alias for the Timestamping Authority's certificate
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   143
    String altCertChain; // file to read alternative cert chain from
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    boolean verify = false; // verify the jar
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   145
    String verbose = null; // verbose output when signing/verifying
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    boolean showcerts = false; // show certs when verifying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    boolean debug = false; // debug
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    boolean signManifest = true; // "sign" the whole manifest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    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
   150
    boolean strict = false;  // treat warnings as error
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    // read zip entry raw bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    private ByteArrayOutputStream baos = new ByteArrayOutputStream(2048);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    private byte[] buffer = new byte[8192];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    private ContentSigner signingMechanism = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    private String altSignerClass = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    private String altSignerClasspath = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    private ZipFile zipFile = null;
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   159
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    private boolean hasExpiredCert = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    private boolean hasExpiringCert = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    private boolean notYetValidCert = false;
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   163
    private boolean chainNotValidated = false;
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   164
    private boolean notSignedByAlias = false;
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   165
    private boolean aliasNotInStore = false;
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   166
    private boolean hasUnsignedEntry = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    private boolean badKeyUsage = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    private boolean badExtendedKeyUsage = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    private boolean badNetscapeCertType = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   171
    CertificateFactory certificateFactory;
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   172
    CertPathValidator validator;
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   173
    PKIXParameters pkixParameters;
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   174
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    public void run(String args[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            parseArgs(args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            // Try to load and install the specified providers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            if (providers != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                ClassLoader cl = ClassLoader.getSystemClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                Enumeration<String> e = providers.elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                while (e.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                    String provName = e.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                    Class<?> provClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                    if (cl != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                        provClass = cl.loadClass(provName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                        provClass = Class.forName(provName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                    String provArg = providerArgs.get(provName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                    Object obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                    if (provArg == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                        obj = provClass.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                        Constructor<?> c =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                                provClass.getConstructor(PARAM_STRING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                        obj = c.newInstance(provArg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                    if (!(obj instanceof Provider)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                        MessageFormat form = new MessageFormat(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   204
                            ("provName.not.a.provider"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                        Object[] source = {provName};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                        throw new Exception(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                    Security.addProvider((Provider)obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            if (verify) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                    loadKeyStore(keystore, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                    if ((keystore != null) || (storepass != null)) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   217
                        System.out.println(rb.getString("jarsigner.error.") +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                                        e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                        System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                /*              if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                    SignatureFileVerifier.setDebug(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                    ManifestEntryVerifier.setDebug(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                verifyJar(jarfile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                loadKeyStore(keystore, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                getAliasInfo(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                // load the alternative signing mechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                if (altSignerClass != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                    signingMechanism = loadSigningMechanism(altSignerClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                        altSignerClasspath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                signJar(jarfile, alias, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        } catch (Exception e) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   240
            System.out.println(rb.getString("jarsigner.error.") + e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            // zero-out private key password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            if (keypass != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                Arrays.fill(keypass, ' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                keypass = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            // zero-out keystore password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            if (storepass != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                Arrays.fill(storepass, ' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                storepass = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        }
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   257
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   258
        if (strict) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   259
            int exitCode = 0;
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   260
            if (hasExpiringCert) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   261
                exitCode |= 2;
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   262
            }
12046
378aa3362868 7149012: jarsigner needs not warn about cert expiration if the jar has a TSA timestamp
weijun
parents: 10788
diff changeset
   263
            if (chainNotValidated || hasExpiredCert || notYetValidCert) {
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   264
                exitCode |= 4;
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   265
            }
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   266
            if (badKeyUsage || badExtendedKeyUsage || badNetscapeCertType) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   267
                exitCode |= 8;
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   268
            }
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   269
            if (hasUnsignedEntry) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   270
                exitCode |= 16;
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   271
            }
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   272
            if (notSignedByAlias || aliasNotInStore) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   273
                exitCode |= 32;
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   274
            }
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   275
            if (exitCode != 0) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   276
                System.exit(exitCode);
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
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * Parse command line arguments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    void parseArgs(String args[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        /* parse flags */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   288
        if (args.length == 0) fullusage();
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   289
        for (n=0; n < args.length; n++) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            String flags = args[n];
3951
e821908c953e 6868579: RFE: jarsigner to support reading password from environment variable
weijun
parents: 3716
diff changeset
   292
            String modifier = null;
e821908c953e 6868579: RFE: jarsigner to support reading password from environment variable
weijun
parents: 3716
diff changeset
   293
            if (flags.charAt(0) == '-') {
e821908c953e 6868579: RFE: jarsigner to support reading password from environment variable
weijun
parents: 3716
diff changeset
   294
                int pos = flags.indexOf(':');
e821908c953e 6868579: RFE: jarsigner to support reading password from environment variable
weijun
parents: 3716
diff changeset
   295
                if (pos > 0) {
e821908c953e 6868579: RFE: jarsigner to support reading password from environment variable
weijun
parents: 3716
diff changeset
   296
                    modifier = flags.substring(pos+1);
e821908c953e 6868579: RFE: jarsigner to support reading password from environment variable
weijun
parents: 3716
diff changeset
   297
                    flags = flags.substring(0, pos);
e821908c953e 6868579: RFE: jarsigner to support reading password from environment variable
weijun
parents: 3716
diff changeset
   298
                }
e821908c953e 6868579: RFE: jarsigner to support reading password from environment variable
weijun
parents: 3716
diff changeset
   299
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            if (collator.compare(flags, "-keystore") == 0) {
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   302
                if (++n == args.length) usageNoArg();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                keystore = args[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            } else if (collator.compare(flags, "-storepass") ==0) {
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   305
                if (++n == args.length) usageNoArg();
3951
e821908c953e 6868579: RFE: jarsigner to support reading password from environment variable
weijun
parents: 3716
diff changeset
   306
                storepass = getPass(modifier, args[n]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            } else if (collator.compare(flags, "-storetype") ==0) {
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   308
                if (++n == args.length) usageNoArg();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                storetype = args[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            } else if (collator.compare(flags, "-providerName") ==0) {
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   311
                if (++n == args.length) usageNoArg();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                providerName = args[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            } else if ((collator.compare(flags, "-provider") == 0) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                        (collator.compare(flags, "-providerClass") == 0)) {
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   315
                if (++n == args.length) usageNoArg();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                if (providers == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                    providers = new Vector<String>(3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                providers.add(args[n]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                if (args.length > (n+1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                    flags = args[n+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                    if (collator.compare(flags, "-providerArg") == 0) {
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   324
                        if (args.length == (n+2)) usageNoArg();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                        providerArgs.put(args[n], args[n+2]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                        n += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            } else if (collator.compare(flags, "-protected") ==0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                protectedPath = true;
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   331
            } else if (collator.compare(flags, "-certchain") ==0) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   332
                if (++n == args.length) usageNoArg();
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   333
                altCertChain = args[n];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            } else if (collator.compare(flags, "-debug") ==0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                debug = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            } else if (collator.compare(flags, "-keypass") ==0) {
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   337
                if (++n == args.length) usageNoArg();
3951
e821908c953e 6868579: RFE: jarsigner to support reading password from environment variable
weijun
parents: 3716
diff changeset
   338
                keypass = getPass(modifier, args[n]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            } else if (collator.compare(flags, "-sigfile") ==0) {
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   340
                if (++n == args.length) usageNoArg();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                sigfile = args[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            } else if (collator.compare(flags, "-signedjar") ==0) {
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   343
                if (++n == args.length) usageNoArg();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                signedjar = args[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            } else if (collator.compare(flags, "-tsa") ==0) {
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   346
                if (++n == args.length) usageNoArg();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                tsaUrl = args[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            } else if (collator.compare(flags, "-tsacert") ==0) {
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   349
                if (++n == args.length) usageNoArg();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                tsaAlias = args[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            } else if (collator.compare(flags, "-altsigner") ==0) {
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   352
                if (++n == args.length) usageNoArg();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                altSignerClass = args[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            } else if (collator.compare(flags, "-altsignerpath") ==0) {
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   355
                if (++n == args.length) usageNoArg();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                altSignerClasspath = args[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            } else if (collator.compare(flags, "-sectionsonly") ==0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                signManifest = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            } else if (collator.compare(flags, "-internalsf") ==0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                externalSF = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            } else if (collator.compare(flags, "-verify") ==0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                verify = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            } else if (collator.compare(flags, "-verbose") ==0) {
3951
e821908c953e 6868579: RFE: jarsigner to support reading password from environment variable
weijun
parents: 3716
diff changeset
   364
                verbose = (modifier != null) ? modifier : "all";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            } else if (collator.compare(flags, "-sigalg") ==0) {
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   366
                if (++n == args.length) usageNoArg();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                sigalg = args[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            } else if (collator.compare(flags, "-digestalg") ==0) {
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   369
                if (++n == args.length) usageNoArg();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                digestalg = args[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            } else if (collator.compare(flags, "-certs") ==0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                showcerts = true;
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   373
            } else if (collator.compare(flags, "-strict") ==0) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   374
                strict = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            } else if (collator.compare(flags, "-h") == 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                        collator.compare(flags, "-help") == 0) {
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   377
                fullusage();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            } else {
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   379
                if (!flags.startsWith("-")) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   380
                    if (jarfile == null) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   381
                        jarfile = flags;
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   382
                    } else {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   383
                        alias = flags;
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   384
                        ckaliases.add(alias);
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   385
                    }
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   386
                } else {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   387
                    System.err.println(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   388
                            rb.getString("Illegal.option.") + flags);
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   389
                    usage();
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   390
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   394
        // -certs must always be specified with -verbose
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   395
        if (verbose == null) showcerts = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   397
        if (jarfile == null) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   398
            System.err.println(rb.getString("Please.specify.jarfile.name"));
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   399
            usage();
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   400
        }
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   401
        if (!verify && alias == null) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   402
            System.err.println(rb.getString("Please.specify.alias.name"));
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   403
            usage();
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   404
        }
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   405
        if (!verify && ckaliases.size() > 1) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   406
            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
   407
            usage();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        if (storetype == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            storetype = KeyStore.getDefaultType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        storetype = KeyStoreUtil.niceStoreTypeName(storetype);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
3481
6ae7a2a6c956 6866479: libzip.so caused JVM to crash when running jarsigner
weijun
parents: 3318
diff changeset
   415
        try {
6ae7a2a6c956 6866479: libzip.so caused JVM to crash when running jarsigner
weijun
parents: 3318
diff changeset
   416
            if (signedjar != null && new File(signedjar).getCanonicalPath().equals(
6ae7a2a6c956 6866479: libzip.so caused JVM to crash when running jarsigner
weijun
parents: 3318
diff changeset
   417
                    new File(jarfile).getCanonicalPath())) {
6ae7a2a6c956 6866479: libzip.so caused JVM to crash when running jarsigner
weijun
parents: 3318
diff changeset
   418
                signedjar = null;
6ae7a2a6c956 6866479: libzip.so caused JVM to crash when running jarsigner
weijun
parents: 3318
diff changeset
   419
            }
6ae7a2a6c956 6866479: libzip.so caused JVM to crash when running jarsigner
weijun
parents: 3318
diff changeset
   420
        } catch (IOException ioe) {
6ae7a2a6c956 6866479: libzip.so caused JVM to crash when running jarsigner
weijun
parents: 3318
diff changeset
   421
            // File system error?
6ae7a2a6c956 6866479: libzip.so caused JVM to crash when running jarsigner
weijun
parents: 3318
diff changeset
   422
            // Just ignore it.
6ae7a2a6c956 6866479: libzip.so caused JVM to crash when running jarsigner
weijun
parents: 3318
diff changeset
   423
        }
6ae7a2a6c956 6866479: libzip.so caused JVM to crash when running jarsigner
weijun
parents: 3318
diff changeset
   424
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        if (P11KEYSTORE.equalsIgnoreCase(storetype) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                KeyStoreUtil.isWindowsKeyStore(storetype)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            token = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            if (keystore == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                keystore = NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        if (NONE.equals(keystore)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            nullStream = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        if (token && !nullStream) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            System.err.println(MessageFormat.format(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   439
                (".keystore.must.be.NONE.if.storetype.is.{0}"), storetype));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            usage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        if (token && keypass != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            System.err.println(MessageFormat.format(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   445
                (".keypass.can.not.be.specified.if.storetype.is.{0}"), storetype));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            usage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        if (protectedPath) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            if (storepass != null || keypass != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                System.err.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   452
                        ("If.protected.is.specified.then.storepass.and.keypass.must.not.be.specified"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                usage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        if (KeyStoreUtil.isWindowsKeyStore(storetype)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            if (storepass != null || keypass != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                System.err.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   459
                        ("If.keystore.is.not.password.protected.then.storepass.and.keypass.must.not.be.specified"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                usage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
3951
e821908c953e 6868579: RFE: jarsigner to support reading password from environment variable
weijun
parents: 3716
diff changeset
   465
    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
   466
        char[] output = KeyStoreUtil.getPassWithModifier(modifier, arg, rb);
3951
e821908c953e 6868579: RFE: jarsigner to support reading password from environment variable
weijun
parents: 3716
diff changeset
   467
        if (output != null) return output;
e821908c953e 6868579: RFE: jarsigner to support reading password from environment variable
weijun
parents: 3716
diff changeset
   468
        usage();
e821908c953e 6868579: RFE: jarsigner to support reading password from environment variable
weijun
parents: 3716
diff changeset
   469
        return null;    // Useless, usage() already exit
e821908c953e 6868579: RFE: jarsigner to support reading password from environment variable
weijun
parents: 3716
diff changeset
   470
    }
e821908c953e 6868579: RFE: jarsigner to support reading password from environment variable
weijun
parents: 3716
diff changeset
   471
e821908c953e 6868579: RFE: jarsigner to support reading password from environment variable
weijun
parents: 3716
diff changeset
   472
    static void usageNoArg() {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   473
        System.out.println(rb.getString("Option.lacks.argument"));
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   474
        usage();
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   475
    }
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   476
3951
e821908c953e 6868579: RFE: jarsigner to support reading password from environment variable
weijun
parents: 3716
diff changeset
   477
    static void usage() {
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   478
        System.out.println();
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   479
        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
   480
        System.exit(1);
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   481
    }
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   482
3951
e821908c953e 6868579: RFE: jarsigner to support reading password from environment variable
weijun
parents: 3716
diff changeset
   483
    static void fullusage() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        System.out.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   485
                ("Usage.jarsigner.options.jar.file.alias"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        System.out.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   487
                (".jarsigner.verify.options.jar.file.alias."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        System.out.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   490
                (".keystore.url.keystore.location"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        System.out.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   493
                (".storepass.password.password.for.keystore.integrity"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        System.out.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   496
                (".storetype.type.keystore.type"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        System.out.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   499
                (".keypass.password.password.for.private.key.if.different."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        System.out.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   502
                (".certchain.file.name.of.alternative.certchain.file"));
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   503
        System.out.println();
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   504
        System.out.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   505
                (".sigfile.file.name.of.SF.DSA.file"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        System.out.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   508
                (".signedjar.file.name.of.signed.JAR.file"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        System.out.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   511
                (".digestalg.algorithm.name.of.digest.algorithm"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        System.out.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   514
                (".sigalg.algorithm.name.of.signature.algorithm"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        System.out.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   517
                (".verify.verify.a.signed.JAR.file"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        System.out.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   520
                (".verbose.suboptions.verbose.output.when.signing.verifying."));
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   521
        System.out.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   522
                (".suboptions.can.be.all.grouped.or.summary"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        System.out.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   525
                (".certs.display.certificates.when.verbose.and.verifying"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        System.out.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   528
                (".tsa.url.location.of.the.Timestamping.Authority"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        System.out.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   531
                (".tsacert.alias.public.key.certificate.for.Timestamping.Authority"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        System.out.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   534
                (".altsigner.class.class.name.of.an.alternative.signing.mechanism"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        System.out.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   537
                (".altsignerpath.pathlist.location.of.an.alternative.signing.mechanism"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        System.out.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   540
                (".internalsf.include.the.SF.file.inside.the.signature.block"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        System.out.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   543
                (".sectionsonly.don.t.compute.hash.of.entire.manifest"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        System.out.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   546
                (".protected.keystore.has.protected.authentication.path"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        System.out.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   549
                (".providerName.name.provider.name"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        System.out.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   552
                (".providerClass.class.name.of.cryptographic.service.provider.s"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        System.out.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   554
                (".providerArg.arg.master.class.file.and.constructor.argument"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        System.out.println();
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   556
        System.out.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   557
                (".strict.treat.warnings.as.errors"));
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   558
        System.out.println();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   560
        System.exit(0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
    void verifyJar(String jarName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
    {
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   566
        boolean anySigned = false;  // if there exists entry inside jar signed
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        JarFile jf = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            jf = new JarFile(jarName, true);
7977
f47f211cd627 7008713: diamond conversion of kerberos5 and security tools
smarks
parents: 7525
diff changeset
   571
            Vector<JarEntry> entriesVec = new Vector<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
            byte[] buffer = new byte[8192];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
            Enumeration<JarEntry> entries = jf.entries();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            while (entries.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                JarEntry je = entries.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                entriesVec.addElement(je);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                InputStream is = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                    is = jf.getInputStream(je);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                    int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                    while ((n = is.read(buffer, 0, buffer.length)) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                        // we just read. this will throw a SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                        // if  a signature/digest check fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                    if (is != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                        is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
            Manifest man = jf.getManifest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   595
            // The map to record display info, only used when -verbose provided
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   596
            //      key: signer info string
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   597
            //      value: the list of files with common key
7977
f47f211cd627 7008713: diamond conversion of kerberos5 and security tools
smarks
parents: 7525
diff changeset
   598
            Map<String,List<String>> output = new LinkedHashMap<>();
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   599
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
            if (man != null) {
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   601
                if (verbose != null) System.out.println();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                Enumeration<JarEntry> e = entriesVec.elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   604
                String tab = rb.getString("6SPACE");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                while (e.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                    JarEntry je = e.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                    String name = je.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                    CodeSigner[] signers = je.getCodeSigners();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                    boolean isSigned = (signers != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                    anySigned |= isSigned;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                    hasUnsignedEntry |= !je.isDirectory() && !isSigned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                                        && !signatureRelated(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   615
                    int inStoreOrScope = inKeyStore(signers);
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   616
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   617
                    boolean inStore = (inStoreOrScope & IN_KEYSTORE) != 0;
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   618
                    boolean inScope = (inStoreOrScope & IN_SCOPE) != 0;
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   619
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   620
                    notSignedByAlias |= (inStoreOrScope & 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
   621
                    if (keystore != null) {
16d2b5e6517a 7004168: jarsigner -verify checks for KeyUsage codesigning ext on all certs instead of just signing cert
weijun
parents: 7524
diff changeset
   622
                        aliasNotInStore |= isSigned && (!inStore && !inScope);
16d2b5e6517a 7004168: jarsigner -verify checks for KeyUsage codesigning ext on all certs instead of just signing cert
weijun
parents: 7524
diff changeset
   623
                    }
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   624
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   625
                    // Only used when -verbose provided
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   626
                    StringBuffer sb = null;
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   627
                    if (verbose != null) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   628
                        sb = new StringBuffer();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                        boolean inManifest =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                            ((man.getAttributes(name) != null) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                             (man.getAttributes("./"+name) != null) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                             (man.getAttributes("/"+name) != null));
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   633
                        sb.append(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   634
                          (isSigned ? rb.getString("s") : rb.getString("SPACE")) +
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   635
                          (inManifest ? rb.getString("m") : rb.getString("SPACE")) +
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   636
                          (inStore ? rb.getString("k") : rb.getString("SPACE")) +
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   637
                          (inScope ? rb.getString("i") : rb.getString("SPACE")) +
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   638
                          ((inStoreOrScope & NOT_ALIAS) != 0 ?"X":" ") +
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   639
                          rb.getString("SPACE"));
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   640
                        sb.append("|");
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   641
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   643
                    // When -certs provided, display info has extra empty
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   644
                    // lines at the beginning and end.
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   645
                    if (isSigned) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   646
                        if (showcerts) sb.append('\n');
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   647
                        for (CodeSigner signer: signers) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   648
                            // signerInfo() must be called even if -verbose
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   649
                            // not provided. The method updates various
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   650
                            // warning flags.
12046
378aa3362868 7149012: jarsigner needs not warn about cert expiration if the jar has a TSA timestamp
weijun
parents: 10788
diff changeset
   651
                            String si = signerInfo(signer, tab);
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   652
                            if (showcerts) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   653
                                sb.append(si);
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   654
                                sb.append('\n');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                        }
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   657
                    } else if (showcerts && !verbose.equals("all")) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   658
                        // Print no info for unsigned entries when -verbose:all,
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   659
                        // to be consistent with old behavior.
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   660
                        if (signatureRelated(name)) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   661
                            sb.append("\n" + tab + rb.getString(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   662
                                    ".Signature.related.entries.") + "\n\n");
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   663
                        } else {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   664
                            sb.append("\n" + tab + rb.getString(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   665
                                    ".Unsigned.entries.") + "\n\n");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   669
                    if (verbose != null) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   670
                        String label = sb.toString();
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   671
                        if (signatureRelated(name)) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   672
                            // Entries inside META-INF and other unsigned
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   673
                            // entries are grouped separately.
7524
ec12e1e6fa20 7004035: signed jar with only META-INF/* inside is not verifiable
weijun
parents: 7179
diff changeset
   674
                            label = "-" + label;
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   675
                        }
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   676
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   677
                        // The label finally contains 2 parts separated by '|':
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   678
                        // The legend displayed before the entry names, and
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   679
                        // the cert info (if -certs specfied).
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   680
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   681
                        if (!output.containsKey(label)) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   682
                            output.put(label, new ArrayList<String>());
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   683
                        }
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   684
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   685
                        StringBuffer fb = new StringBuffer();
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   686
                        String s = Long.toString(je.getSize());
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   687
                        for (int i = 6 - s.length(); i > 0; --i) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   688
                            fb.append(' ');
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   689
                        }
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   690
                        fb.append(s).append(' ').
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   691
                                append(new Date(je.getTime()).toString());
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   692
                        fb.append(' ').append(name);
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   693
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   694
                        output.get(label).add(fb.toString());
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   695
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
            }
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   698
            if (verbose != null) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   699
                for (Entry<String,List<String>> s: output.entrySet()) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   700
                    List<String> files = s.getValue();
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   701
                    String key = s.getKey();
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   702
                    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
   703
                        key = key.substring(1);
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   704
                    }
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   705
                    int pipe = key.indexOf('|');
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   706
                    if (verbose.equals("all")) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   707
                        for (String f: files) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   708
                            System.out.println(key.substring(0, pipe) + f);
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   709
                            System.out.printf(key.substring(pipe+1));
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   710
                        }
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   711
                    } else {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   712
                        if (verbose.equals("grouped")) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   713
                            for (String f: files) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   714
                                System.out.println(key.substring(0, pipe) + f);
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   715
                            }
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   716
                        } else if (verbose.equals("summary")) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   717
                            System.out.print(key.substring(0, pipe));
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   718
                            if (files.size() > 1) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   719
                                System.out.println(files.get(0) + " " +
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   720
                                        String.format(rb.getString(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   721
                                        ".and.d.more."), files.size()-1));
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   722
                            } else {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   723
                                System.out.println(files.get(0));
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   724
                            }
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   725
                        }
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   726
                        System.out.printf(key.substring(pipe+1));
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   727
                    }
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   728
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
                System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
                System.out.println(rb.getString(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   731
                    ".s.signature.was.verified."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
                System.out.println(rb.getString(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   733
                    ".m.entry.is.listed.in.manifest"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
                System.out.println(rb.getString(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   735
                    ".k.at.least.one.certificate.was.found.in.keystore"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
                System.out.println(rb.getString(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   737
                    ".i.at.least.one.certificate.was.found.in.identity.scope"));
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   738
                if (ckaliases.size() > 0) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   739
                    System.out.println(rb.getString(
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   740
                        ".X.not.signed.by.specified.alias.es."));
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   741
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
            if (man == null)
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   745
                System.out.println(rb.getString("no.manifest."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
            if (!anySigned) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
                System.out.println(rb.getString(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   749
                      "jar.is.unsigned.signatures.missing.or.not.parsable."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
            } else {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   751
                System.out.println(rb.getString("jar.verified."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
                if (hasUnsignedEntry || hasExpiredCert || hasExpiringCert ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
                    badKeyUsage || badExtendedKeyUsage || badNetscapeCertType ||
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   754
                    notYetValidCert || chainNotValidated ||
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   755
                    aliasNotInStore || notSignedByAlias) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
                    System.out.println();
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   758
                    System.out.println(rb.getString("Warning."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
                    if (badKeyUsage) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
                        System.out.println(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   761
                            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
   762
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
                    if (badExtendedKeyUsage) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
                        System.out.println(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   766
                            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
   767
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
                    if (badNetscapeCertType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
                        System.out.println(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   771
                            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
   772
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
                    if (hasUnsignedEntry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
                        System.out.println(rb.getString(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   776
                            "This.jar.contains.unsigned.entries.which.have.not.been.integrity.checked."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
                    if (hasExpiredCert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
                        System.out.println(rb.getString(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   780
                            "This.jar.contains.entries.whose.signer.certificate.has.expired."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
                    if (hasExpiringCert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
                        System.out.println(rb.getString(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   784
                            "This.jar.contains.entries.whose.signer.certificate.will.expire.within.six.months."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
                    if (notYetValidCert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
                        System.out.println(rb.getString(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   788
                            "This.jar.contains.entries.whose.signer.certificate.is.not.yet.valid."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   791
                    if (chainNotValidated) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   792
                        System.out.println(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   793
                                rb.getString("This.jar.contains.entries.whose.certificate.chain.is.not.validated."));
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   794
                    }
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   795
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   796
                    if (notSignedByAlias) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   797
                        System.out.println(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   798
                                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
   799
                    }
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   800
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   801
                    if (aliasNotInStore) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   802
                        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
   803
                    }
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   804
                    if (! (verbose != null && showcerts)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
                        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
                        System.out.println(rb.getString(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   807
                            "Re.run.with.the.verbose.and.certs.options.for.more.details."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
            }
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   811
            return;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
        } catch (Exception e) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   813
            System.out.println(rb.getString("jarsigner.") + e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
            if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
        } finally { // close the resource
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
            if (jf != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
                jf.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
        System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
    private static MessageFormat validityTimeForm = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
    private static MessageFormat notYetTimeForm = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
    private static MessageFormat expiredTimeForm = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
    private static MessageFormat expiringTimeForm = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
     * Display some details about a certificate:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
     * [<tab>] <cert-type> [", " <subject-DN>] [" (" <keystore-entry-alias> ")"]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
     * [<validity-period> | <expiry-warning>]
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   836
     *
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   837
     * Note: no newline character at the end
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
    String printCert(String tab, Certificate c, boolean checkValidityPeriod,
12046
378aa3362868 7149012: jarsigner needs not warn about cert expiration if the jar has a TSA timestamp
weijun
parents: 10788
diff changeset
   840
        Date timestamp, boolean checkUsage) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
        StringBuilder certStr = new StringBuilder();
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   843
        String space = rb.getString("SPACE");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
        X509Certificate x509Cert = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
        if (c instanceof X509Certificate) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
            x509Cert = (X509Certificate) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
            certStr.append(tab).append(x509Cert.getType())
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   849
                .append(rb.getString("COMMA"))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
                .append(x509Cert.getSubjectDN().getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
            certStr.append(tab).append(c.getType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
        String alias = storeHash.get(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
        if (alias != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
            certStr.append(space).append(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
        if (checkValidityPeriod && x509Cert != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
            certStr.append("\n").append(tab).append("[");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
            Date notAfter = x509Cert.getNotAfter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
            try {
12046
378aa3362868 7149012: jarsigner needs not warn about cert expiration if the jar has a TSA timestamp
weijun
parents: 10788
diff changeset
   865
                boolean printValidity = true;
378aa3362868 7149012: jarsigner needs not warn about cert expiration if the jar has a TSA timestamp
weijun
parents: 10788
diff changeset
   866
                if (timestamp == null) {
378aa3362868 7149012: jarsigner needs not warn about cert expiration if the jar has a TSA timestamp
weijun
parents: 10788
diff changeset
   867
                    x509Cert.checkValidity();
378aa3362868 7149012: jarsigner needs not warn about cert expiration if the jar has a TSA timestamp
weijun
parents: 10788
diff changeset
   868
                    // 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
   869
                    if (notAfter.getTime() < System.currentTimeMillis() + SIX_MONTHS) {
378aa3362868 7149012: jarsigner needs not warn about cert expiration if the jar has a TSA timestamp
weijun
parents: 10788
diff changeset
   870
                        hasExpiringCert = true;
378aa3362868 7149012: jarsigner needs not warn about cert expiration if the jar has a TSA timestamp
weijun
parents: 10788
diff changeset
   871
                        if (expiringTimeForm == null) {
378aa3362868 7149012: jarsigner needs not warn about cert expiration if the jar has a TSA timestamp
weijun
parents: 10788
diff changeset
   872
                            expiringTimeForm = new MessageFormat(
378aa3362868 7149012: jarsigner needs not warn about cert expiration if the jar has a TSA timestamp
weijun
parents: 10788
diff changeset
   873
                                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
   874
                        }
378aa3362868 7149012: jarsigner needs not warn about cert expiration if the jar has a TSA timestamp
weijun
parents: 10788
diff changeset
   875
                        Object[] source = { notAfter };
378aa3362868 7149012: jarsigner needs not warn about cert expiration if the jar has a TSA timestamp
weijun
parents: 10788
diff changeset
   876
                        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
   877
                        printValidity = false;
378aa3362868 7149012: jarsigner needs not warn about cert expiration if the jar has a TSA timestamp
weijun
parents: 10788
diff changeset
   878
                    }
378aa3362868 7149012: jarsigner needs not warn about cert expiration if the jar has a TSA timestamp
weijun
parents: 10788
diff changeset
   879
                } else {
378aa3362868 7149012: jarsigner needs not warn about cert expiration if the jar has a TSA timestamp
weijun
parents: 10788
diff changeset
   880
                    x509Cert.checkValidity(timestamp);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
                }
12046
378aa3362868 7149012: jarsigner needs not warn about cert expiration if the jar has a TSA timestamp
weijun
parents: 10788
diff changeset
   882
                if (printValidity) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
                    if (validityTimeForm == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
                        validityTimeForm = new MessageFormat(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   885
                            rb.getString("certificate.is.valid.from"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
                    Object[] source = { x509Cert.getNotBefore(), notAfter };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
                    certStr.append(validityTimeForm.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
            } catch (CertificateExpiredException cee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
                hasExpiredCert = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
                if (expiredTimeForm == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
                    expiredTimeForm = new MessageFormat(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   895
                        rb.getString("certificate.expired.on"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
                Object[] source = { notAfter };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
                certStr.append(expiredTimeForm.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
            } catch (CertificateNotYetValidException cnyve) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
                notYetValidCert = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
                if (notYetTimeForm == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
                    notYetTimeForm = new MessageFormat(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   905
                        rb.getString("certificate.is.not.valid.until"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
                Object[] source = { x509Cert.getNotBefore() };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
                certStr.append(notYetTimeForm.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
            certStr.append("]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
7525
16d2b5e6517a 7004168: jarsigner -verify checks for KeyUsage codesigning ext on all certs instead of just signing cert
weijun
parents: 7524
diff changeset
   912
            if (checkUsage) {
16d2b5e6517a 7004168: jarsigner -verify checks for KeyUsage codesigning ext on all certs instead of just signing cert
weijun
parents: 7524
diff changeset
   913
                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
   914
                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
   915
                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
   916
                    String x = "";
16d2b5e6517a 7004168: jarsigner -verify checks for KeyUsage codesigning ext on all certs instead of just signing cert
weijun
parents: 7524
diff changeset
   917
                    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
   918
                        x ="KeyUsage";
16d2b5e6517a 7004168: jarsigner -verify checks for KeyUsage codesigning ext on all certs instead of just signing cert
weijun
parents: 7524
diff changeset
   919
                    }
16d2b5e6517a 7004168: jarsigner -verify checks for KeyUsage codesigning ext on all certs instead of just signing cert
weijun
parents: 7524
diff changeset
   920
                    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
   921
                        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
   922
                        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
   923
                    }
16d2b5e6517a 7004168: jarsigner -verify checks for KeyUsage codesigning ext on all certs instead of just signing cert
weijun
parents: 7524
diff changeset
   924
                    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
   925
                        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
   926
                        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
   927
                    }
16d2b5e6517a 7004168: jarsigner -verify checks for KeyUsage codesigning ext on all certs instead of just signing cert
weijun
parents: 7524
diff changeset
   928
                    certStr.append("\n").append(tab)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
                        .append(MessageFormat.format(rb.getString(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   930
                        ".{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
   931
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
        return certStr.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
    private static MessageFormat signTimeForm = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
    private String printTimestamp(String tab, Timestamp timestamp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
        if (signTimeForm == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
            signTimeForm =
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
   943
                new MessageFormat(rb.getString("entry.was.signed.on"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
        Object[] source = { timestamp.getTimestamp() };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
        return new StringBuilder().append(tab).append("[")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
            .append(signTimeForm.format(source)).append("]").toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
7977
f47f211cd627 7008713: diamond conversion of kerberos5 and security tools
smarks
parents: 7525
diff changeset
   951
    private Map<CodeSigner,Integer> cacheForInKS = new IdentityHashMap<>();
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   952
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   953
    private int inKeyStoreForOneSigner(CodeSigner signer) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   954
        if (cacheForInKS.containsKey(signer)) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   955
            return cacheForInKS.get(signer);
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   956
        }
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   957
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   958
        boolean found = false;
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   959
        int result = 0;
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   960
        List<? extends Certificate> certs = signer.getSignerCertPath().getCertificates();
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   961
        for (Certificate c : certs) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   962
            String alias = storeHash.get(c);
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   963
            if (alias != null) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   964
                if (alias.startsWith("(")) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   965
                    result |= IN_KEYSTORE;
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   966
                } else if (alias.startsWith("[")) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   967
                    result |= IN_SCOPE;
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   968
                }
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   969
                if (ckaliases.contains(alias.substring(1, alias.length() - 1))) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   970
                    result |= SIGNED_BY_ALIAS;
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   971
                }
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   972
            } else {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   973
                if (store != null) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   974
                    try {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   975
                        alias = store.getCertificateAlias(c);
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   976
                    } catch (KeyStoreException kse) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   977
                        // never happens, because keystore has been loaded
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   978
                    }
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   979
                    if (alias != null) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   980
                        storeHash.put(c, "(" + alias + ")");
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   981
                        found = true;
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   982
                        result |= IN_KEYSTORE;
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   983
                    }
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   984
                }
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   985
                if (ckaliases.contains(alias)) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   986
                    result |= SIGNED_BY_ALIAS;
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   987
                }
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   988
            }
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   989
        }
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   990
        cacheForInKS.put(signer, result);
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   991
        return result;
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   992
    }
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
   993
7977
f47f211cd627 7008713: diamond conversion of kerberos5 and security tools
smarks
parents: 7525
diff changeset
   994
    Hashtable<Certificate, String> storeHash = new Hashtable<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
    int inKeyStore(CodeSigner[] signers) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
        if (signers == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1001
        int output = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1003
        for (CodeSigner signer: signers) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1004
            int result = inKeyStoreForOneSigner(signer);
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1005
            output |= result;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
        }
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1007
        if (ckaliases.size() > 0 && (output & SIGNED_BY_ALIAS) == 0) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1008
            output |= NOT_ALIAS;
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1009
        }
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1010
        return output;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
    void signJar(String jarName, String alias, String[] args)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
        throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
        boolean aliasUsed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
        X509Certificate tsaCert = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
        if (sigfile == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
            sigfile = alias;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
            aliasUsed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
        if (sigfile.length() > 8) {
4152
bc36a9f01ac6 6870812: enhance security tools to use ECC algorithms
weijun
parents: 3951
diff changeset
  1024
            sigfile = sigfile.substring(0, 8).toUpperCase(Locale.ENGLISH);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
        } else {
4152
bc36a9f01ac6 6870812: enhance security tools to use ECC algorithms
weijun
parents: 3951
diff changeset
  1026
            sigfile = sigfile.toUpperCase(Locale.ENGLISH);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
        StringBuilder tmpSigFile = new StringBuilder(sigfile.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
        for (int j = 0; j < sigfile.length(); j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
            char c = sigfile.charAt(j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
            if (!
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
                ((c>= 'A' && c<= 'Z') ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
                (c>= '0' && c<= '9') ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
                (c == '-') ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
                (c == '_'))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
                if (aliasUsed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
                    // convert illegal characters from the alias to be _'s
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
                    c = '_';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
                 throw new
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
                   RuntimeException(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
  1043
                        ("signature.filename.must.consist.of.the.following.characters.A.Z.0.9.or."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
            tmpSigFile.append(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
        sigfile = tmpSigFile.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
        String tmpJarName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
        if (signedjar == null) tmpJarName = jarName+".sig";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
        else tmpJarName = signedjar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
        File jarFile = new File(jarName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
        File signedJarFile = new File(tmpJarName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
        // Open the jar (zip) file
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
            zipFile = new ZipFile(jarName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
        } catch (IOException ioe) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
  1062
            error(rb.getString("unable.to.open.jar.file.")+jarName, ioe);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
        FileOutputStream fos = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
            fos = new FileOutputStream(signedJarFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
        } catch (IOException ioe) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
  1069
            error(rb.getString("unable.to.create.")+tmpJarName, ioe);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
        PrintStream ps = new PrintStream(fos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
        ZipOutputStream zos = new ZipOutputStream(ps);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
        /* First guess at what they might be - we don't xclude RSA ones. */
4152
bc36a9f01ac6 6870812: enhance security tools to use ECC algorithms
weijun
parents: 3951
diff changeset
  1076
        String sfFilename = (META_INF + sigfile + ".SF").toUpperCase(Locale.ENGLISH);
bc36a9f01ac6 6870812: enhance security tools to use ECC algorithms
weijun
parents: 3951
diff changeset
  1077
        String bkFilename = (META_INF + sigfile + ".DSA").toUpperCase(Locale.ENGLISH);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
        Manifest manifest = new Manifest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
        Map<String,Attributes> mfEntries = manifest.getEntries();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
        // The Attributes of manifest before updating
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
        Attributes oldAttr = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
        boolean mfModified = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
        boolean mfCreated = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
        byte[] mfRawBytes = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
            MessageDigest digests[] = { MessageDigest.getInstance(digestalg) };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
            // Check if manifest exists
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
            ZipEntry mfFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
            if ((mfFile = getManifestFile(zipFile)) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
                // Manifest exists. Read its raw bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
                mfRawBytes = getBytes(zipFile, mfFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
                manifest.read(new ByteArrayInputStream(mfRawBytes));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
                oldAttr = (Attributes)(manifest.getMainAttributes().clone());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
                // Create new manifest
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
                Attributes mattr = manifest.getMainAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
                mattr.putValue(Attributes.Name.MANIFEST_VERSION.toString(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
                               "1.0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
                String javaVendor = System.getProperty("java.vendor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
                String jdkVersion = System.getProperty("java.version");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
                mattr.putValue("Created-By", jdkVersion + " (" +javaVendor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
                               + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
                mfFile = new ZipEntry(JarFile.MANIFEST_NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
                mfCreated = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
             * For each entry in jar
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
             * (except for signature-related META-INF entries),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
             * do the following:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
             * - if entry is not contained in manifest, add it to manifest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
             * - if entry is contained in manifest, calculate its hash and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
             *   compare it with the one in the manifest; if they are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
             *   different, replace the hash in the manifest with the newly
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
             *   generated one. (This may invalidate existing signatures!)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
            BASE64Encoder encoder = new JarBASE64Encoder();
7977
f47f211cd627 7008713: diamond conversion of kerberos5 and security tools
smarks
parents: 7525
diff changeset
  1124
            Vector<ZipEntry> mfFiles = new Vector<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
5461
a0e3063bc133 6948909: Jarsigner removes MANIFEST.MF info for badly packages jar's
weijun
parents: 4350
diff changeset
  1126
            boolean wasSigned = false;
a0e3063bc133 6948909: Jarsigner removes MANIFEST.MF info for badly packages jar's
weijun
parents: 4350
diff changeset
  1127
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
            for (Enumeration<? extends ZipEntry> enum_=zipFile.entries();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
                        enum_.hasMoreElements();) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
                ZipEntry ze = enum_.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
                if (ze.getName().startsWith(META_INF)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
                    // Store META-INF files in vector, so they can be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
                    // out first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
                    mfFiles.addElement(ze);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
5461
a0e3063bc133 6948909: Jarsigner removes MANIFEST.MF info for badly packages jar's
weijun
parents: 4350
diff changeset
  1137
                    if (SignatureFileVerifier.isBlockOrSF(
a0e3063bc133 6948909: Jarsigner removes MANIFEST.MF info for badly packages jar's
weijun
parents: 4350
diff changeset
  1138
                            ze.getName().toUpperCase(Locale.ENGLISH))) {
a0e3063bc133 6948909: Jarsigner removes MANIFEST.MF info for badly packages jar's
weijun
parents: 4350
diff changeset
  1139
                        wasSigned = true;
a0e3063bc133 6948909: Jarsigner removes MANIFEST.MF info for badly packages jar's
weijun
parents: 4350
diff changeset
  1140
                    }
a0e3063bc133 6948909: Jarsigner removes MANIFEST.MF info for badly packages jar's
weijun
parents: 4350
diff changeset
  1141
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
                    if (signatureRelated(ze.getName())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
                        // ignore signature-related and manifest files
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
                if (manifest.getAttributes(ze.getName()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
                    // jar entry is contained in manifest, check and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
                    // possibly update its digest attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
                    if (updateDigests(ze, zipFile, digests, encoder,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
                                      manifest) == true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
                        mfModified = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
                } else if (!ze.isDirectory()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
                    // Add entry to manifest
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
                    Attributes attrs = getDigestAttributes(ze, zipFile,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
                                                           digests,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
                                                           encoder);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
                    mfEntries.put(ze.getName(), attrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
                    mfModified = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
            // Recalculate the manifest raw bytes if necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
            if (mfModified) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
                manifest.write(baos);
5461
a0e3063bc133 6948909: Jarsigner removes MANIFEST.MF info for badly packages jar's
weijun
parents: 4350
diff changeset
  1169
                if (wasSigned) {
a0e3063bc133 6948909: Jarsigner removes MANIFEST.MF info for badly packages jar's
weijun
parents: 4350
diff changeset
  1170
                    byte[] newBytes = baos.toByteArray();
a0e3063bc133 6948909: Jarsigner removes MANIFEST.MF info for badly packages jar's
weijun
parents: 4350
diff changeset
  1171
                    if (mfRawBytes != null
a0e3063bc133 6948909: Jarsigner removes MANIFEST.MF info for badly packages jar's
weijun
parents: 4350
diff changeset
  1172
                            && oldAttr.equals(manifest.getMainAttributes())) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
5461
a0e3063bc133 6948909: Jarsigner removes MANIFEST.MF info for badly packages jar's
weijun
parents: 4350
diff changeset
  1174
                        /*
a0e3063bc133 6948909: Jarsigner removes MANIFEST.MF info for badly packages jar's
weijun
parents: 4350
diff changeset
  1175
                         * Note:
a0e3063bc133 6948909: Jarsigner removes MANIFEST.MF info for badly packages jar's
weijun
parents: 4350
diff changeset
  1176
                         *
a0e3063bc133 6948909: Jarsigner removes MANIFEST.MF info for badly packages jar's
weijun
parents: 4350
diff changeset
  1177
                         * The Attributes object is based on HashMap and can handle
a0e3063bc133 6948909: Jarsigner removes MANIFEST.MF info for badly packages jar's
weijun
parents: 4350
diff changeset
  1178
                         * continuation columns. Therefore, even if the contents are
a0e3063bc133 6948909: Jarsigner removes MANIFEST.MF info for badly packages jar's
weijun
parents: 4350
diff changeset
  1179
                         * not changed (in a Map view), the bytes that it write()
a0e3063bc133 6948909: Jarsigner removes MANIFEST.MF info for badly packages jar's
weijun
parents: 4350
diff changeset
  1180
                         * may be different from the original bytes that it read()
a0e3063bc133 6948909: Jarsigner removes MANIFEST.MF info for badly packages jar's
weijun
parents: 4350
diff changeset
  1181
                         * from. Since the signature on the main attributes is based
a0e3063bc133 6948909: Jarsigner removes MANIFEST.MF info for badly packages jar's
weijun
parents: 4350
diff changeset
  1182
                         * on raw bytes, we must retain the exact bytes.
a0e3063bc133 6948909: Jarsigner removes MANIFEST.MF info for badly packages jar's
weijun
parents: 4350
diff changeset
  1183
                         */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
5461
a0e3063bc133 6948909: Jarsigner removes MANIFEST.MF info for badly packages jar's
weijun
parents: 4350
diff changeset
  1185
                        int newPos = findHeaderEnd(newBytes);
a0e3063bc133 6948909: Jarsigner removes MANIFEST.MF info for badly packages jar's
weijun
parents: 4350
diff changeset
  1186
                        int oldPos = findHeaderEnd(mfRawBytes);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
5461
a0e3063bc133 6948909: Jarsigner removes MANIFEST.MF info for badly packages jar's
weijun
parents: 4350
diff changeset
  1188
                        if (newPos == oldPos) {
a0e3063bc133 6948909: Jarsigner removes MANIFEST.MF info for badly packages jar's
weijun
parents: 4350
diff changeset
  1189
                            System.arraycopy(mfRawBytes, 0, newBytes, 0, oldPos);
a0e3063bc133 6948909: Jarsigner removes MANIFEST.MF info for badly packages jar's
weijun
parents: 4350
diff changeset
  1190
                        } else {
a0e3063bc133 6948909: Jarsigner removes MANIFEST.MF info for badly packages jar's
weijun
parents: 4350
diff changeset
  1191
                            // cat oldHead newTail > newBytes
a0e3063bc133 6948909: Jarsigner removes MANIFEST.MF info for badly packages jar's
weijun
parents: 4350
diff changeset
  1192
                            byte[] lastBytes = new byte[oldPos +
a0e3063bc133 6948909: Jarsigner removes MANIFEST.MF info for badly packages jar's
weijun
parents: 4350
diff changeset
  1193
                                    newBytes.length - newPos];
a0e3063bc133 6948909: Jarsigner removes MANIFEST.MF info for badly packages jar's
weijun
parents: 4350
diff changeset
  1194
                            System.arraycopy(mfRawBytes, 0, lastBytes, 0, oldPos);
a0e3063bc133 6948909: Jarsigner removes MANIFEST.MF info for badly packages jar's
weijun
parents: 4350
diff changeset
  1195
                            System.arraycopy(newBytes, newPos, lastBytes, oldPos,
a0e3063bc133 6948909: Jarsigner removes MANIFEST.MF info for badly packages jar's
weijun
parents: 4350
diff changeset
  1196
                                    newBytes.length - newPos);
a0e3063bc133 6948909: Jarsigner removes MANIFEST.MF info for badly packages jar's
weijun
parents: 4350
diff changeset
  1197
                            newBytes = lastBytes;
a0e3063bc133 6948909: Jarsigner removes MANIFEST.MF info for badly packages jar's
weijun
parents: 4350
diff changeset
  1198
                        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
                    }
5461
a0e3063bc133 6948909: Jarsigner removes MANIFEST.MF info for badly packages jar's
weijun
parents: 4350
diff changeset
  1200
                    mfRawBytes = newBytes;
a0e3063bc133 6948909: Jarsigner removes MANIFEST.MF info for badly packages jar's
weijun
parents: 4350
diff changeset
  1201
                } else {
a0e3063bc133 6948909: Jarsigner removes MANIFEST.MF info for badly packages jar's
weijun
parents: 4350
diff changeset
  1202
                    mfRawBytes = baos.toByteArray();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
            // Write out the manifest
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
            if (mfModified) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
                // manifest file has new length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
                mfFile = new ZipEntry(JarFile.MANIFEST_NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
            }
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1211
            if (verbose != null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
                if (mfCreated) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
  1213
                    System.out.println(rb.getString(".adding.") +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
                                        mfFile.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
                } else if (mfModified) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
  1216
                    System.out.println(rb.getString(".updating.") +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
                                        mfFile.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
            zos.putNextEntry(mfFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
            zos.write(mfRawBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
            // Calculate SignatureFile (".SF") and SignatureBlockFile
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
            ManifestDigester manDig = new ManifestDigester(mfRawBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
            SignatureFile sf = new SignatureFile(digests, manifest, manDig,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
                                                 sigfile, signManifest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
            if (tsaAlias != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
                tsaCert = getTsaCert(tsaAlias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
            SignatureFile.Block block = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
                block =
8556
d3d6e4643560 7021789: Remove jarsigner -crl option
weijun
parents: 7977
diff changeset
  1236
                    sf.generateBlock(privateKey, sigalg, certChain,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
                        externalSF, tsaUrl, tsaCert, signingMechanism, args,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
                        zipFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
            } catch (SocketTimeoutException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
                // Provide a helpful message when TSA is beyond a firewall
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
  1241
                error(rb.getString("unable.to.sign.jar.") +
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
  1242
                rb.getString("no.response.from.the.Timestamping.Authority.") +
8994
ef946d820d04 7028490: better suggestion for jarsigner when TSA is not accessible
weijun
parents: 8556
diff changeset
  1243
                "\n  -J-Dhttp.proxyHost=<hostname>" +
ef946d820d04 7028490: better suggestion for jarsigner when TSA is not accessible
weijun
parents: 8556
diff changeset
  1244
                "\n  -J-Dhttp.proxyPort=<portnumber>\n" +
ef946d820d04 7028490: better suggestion for jarsigner when TSA is not accessible
weijun
parents: 8556
diff changeset
  1245
                rb.getString("or") +
ef946d820d04 7028490: better suggestion for jarsigner when TSA is not accessible
weijun
parents: 8556
diff changeset
  1246
                "\n  -J-Dhttps.proxyHost=<hostname> " +
ef946d820d04 7028490: better suggestion for jarsigner when TSA is not accessible
weijun
parents: 8556
diff changeset
  1247
                "\n  -J-Dhttps.proxyPort=<portnumber> ", e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
            sfFilename = sf.getMetaName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
            bkFilename = block.getMetaName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
            ZipEntry sfFile = new ZipEntry(sfFilename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
            ZipEntry bkFile = new ZipEntry(bkFilename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
            long time = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
            sfFile.setTime(time);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
            bkFile.setTime(time);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
            // signature file
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
            zos.putNextEntry(sfFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
            sf.write(zos);
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1263
            if (verbose != null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
                if (zipFile.getEntry(sfFilename) != null) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
  1265
                    System.out.println(rb.getString(".updating.") +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
                                sfFilename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
                } else {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
  1268
                    System.out.println(rb.getString(".adding.") +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
                                sfFilename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1273
            if (verbose != null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
                if (tsaUrl != null || tsaCert != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
                    System.out.println(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
  1276
                        rb.getString("requesting.a.signature.timestamp"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
                if (tsaUrl != null) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
  1279
                    System.out.println(rb.getString("TSA.location.") + tsaUrl);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
                if (tsaCert != null) {
10788
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10427
diff changeset
  1282
                    URI tsaURI = TimestampedSigner.getTimestampingURI(tsaCert);
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10427
diff changeset
  1283
                    if (tsaURI != null) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
  1284
                        System.out.println(rb.getString("TSA.location.") +
10788
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10427
diff changeset
  1285
                            tsaURI);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
                    }
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
  1287
                    System.out.println(rb.getString("TSA.certificate.") +
12046
378aa3362868 7149012: jarsigner needs not warn about cert expiration if the jar has a TSA timestamp
weijun
parents: 10788
diff changeset
  1288
                        printCert("", tsaCert, false, null, false));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
                if (signingMechanism != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
                    System.out.println(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
  1292
                        rb.getString("using.an.alternative.signing.mechanism"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
            // signature block file
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
            zos.putNextEntry(bkFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
            block.write(zos);
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1299
            if (verbose != null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
                if (zipFile.getEntry(bkFilename) != null) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
  1301
                    System.out.println(rb.getString(".updating.") +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
                        bkFilename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
                } else {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
  1304
                    System.out.println(rb.getString(".adding.") +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
                        bkFilename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
            // Write out all other META-INF files that we stored in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
            // vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
            for (int i=0; i<mfFiles.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
                ZipEntry ze = mfFiles.elementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
                if (!ze.getName().equalsIgnoreCase(JarFile.MANIFEST_NAME)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
                    && !ze.getName().equalsIgnoreCase(sfFilename)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
                    && !ze.getName().equalsIgnoreCase(bkFilename)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
                    writeEntry(zipFile, zos, ze);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
            // Write out all other files
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
            for (Enumeration<? extends ZipEntry> enum_=zipFile.entries();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
                        enum_.hasMoreElements();) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
                ZipEntry ze = enum_.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
                if (!ze.getName().startsWith(META_INF)) {
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1326
                    if (verbose != null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
                        if (manifest.getAttributes(ze.getName()) != null)
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
  1328
                          System.out.println(rb.getString(".signing.") +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
                                ze.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
                        else
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
  1331
                          System.out.println(rb.getString(".adding.") +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
                                ze.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
                    writeEntry(zipFile, zos, ze);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
        } catch(IOException ioe) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
  1338
            error(rb.getString("unable.to.sign.jar.")+ioe, ioe);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
            // close the resouces
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
            if (zipFile != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
                zipFile.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
                zipFile = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
            if (zos != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
                zos.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
        // no IOException thrown in the follow try clause, so disable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
        // the try clause.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
        // try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
            if (signedjar == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
                // attempt an atomic rename. If that fails,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
                // rename the original jar file, then the signed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
                // one, then delete the original.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
                if (!signedJarFile.renameTo(jarFile)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
                    File origJar = new File(jarName+".orig");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
                    if (jarFile.renameTo(origJar)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
                        if (signedJarFile.renameTo(jarFile)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
                            origJar.delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
                            MessageFormat form = new MessageFormat(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
  1366
                        ("attempt.to.rename.signedJarFile.to.jarFile.failed"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
                            Object[] source = {signedJarFile, jarFile};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
                            error(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
                        MessageFormat form = new MessageFormat(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
  1372
                            ("attempt.to.rename.jarFile.to.origJar.failed"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
                        Object[] source = {jarFile, origJar};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
                        error(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
            if (hasExpiredCert || hasExpiringCert || notYetValidCert
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1380
                    || badKeyUsage || badExtendedKeyUsage
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1381
                    || badNetscapeCertType || chainNotValidated) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
                System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
  1384
                System.out.println(rb.getString("Warning."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
                if (badKeyUsage) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
                    System.out.println(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
  1387
                        rb.getString("The.signer.certificate.s.KeyUsage.extension.doesn.t.allow.code.signing."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
                if (badExtendedKeyUsage) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
                    System.out.println(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
  1392
                        rb.getString("The.signer.certificate.s.ExtendedKeyUsage.extension.doesn.t.allow.code.signing."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
                if (badNetscapeCertType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
                    System.out.println(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
  1397
                        rb.getString("The.signer.certificate.s.NetscapeCertType.extension.doesn.t.allow.code.signing."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
                if (hasExpiredCert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
                    System.out.println(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
  1402
                        rb.getString("The.signer.certificate.has.expired."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
                } else if (hasExpiringCert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
                    System.out.println(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
  1405
                        rb.getString("The.signer.certificate.will.expire.within.six.months."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
                } else if (notYetValidCert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
                    System.out.println(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
  1408
                        rb.getString("The.signer.certificate.is.not.yet.valid."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
                }
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1410
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1411
                if (chainNotValidated) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1412
                    System.out.println(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
  1413
                            rb.getString("The.signer.s.certificate.chain.is.not.validated."));
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1414
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
        // no IOException thrown in the above try clause, so disable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
        // the catch clause.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
        // } catch(IOException ioe) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
  1420
        //     error(rb.getString("unable.to.sign.jar.")+ioe, ioe);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
        // }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
    /**
5461
a0e3063bc133 6948909: Jarsigner removes MANIFEST.MF info for badly packages jar's
weijun
parents: 4350
diff changeset
  1425
     * Find the length of header inside bs. The header is a multiple (>=0)
a0e3063bc133 6948909: Jarsigner removes MANIFEST.MF info for badly packages jar's
weijun
parents: 4350
diff changeset
  1426
     * lines of attributes plus an empty line. The empty line is included
a0e3063bc133 6948909: Jarsigner removes MANIFEST.MF info for badly packages jar's
weijun
parents: 4350
diff changeset
  1427
     * in the header.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
     */
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9011
diff changeset
  1429
    @SuppressWarnings("fallthrough")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
    private int findHeaderEnd(byte[] bs) {
5461
a0e3063bc133 6948909: Jarsigner removes MANIFEST.MF info for badly packages jar's
weijun
parents: 4350
diff changeset
  1431
        // Initial state true to deal with empty header
a0e3063bc133 6948909: Jarsigner removes MANIFEST.MF info for badly packages jar's
weijun
parents: 4350
diff changeset
  1432
        boolean newline = true;     // just met a newline
a0e3063bc133 6948909: Jarsigner removes MANIFEST.MF info for badly packages jar's
weijun
parents: 4350
diff changeset
  1433
        int len = bs.length;
a0e3063bc133 6948909: Jarsigner removes MANIFEST.MF info for badly packages jar's
weijun
parents: 4350
diff changeset
  1434
        for (int i=0; i<len; i++) {
a0e3063bc133 6948909: Jarsigner removes MANIFEST.MF info for badly packages jar's
weijun
parents: 4350
diff changeset
  1435
            switch (bs[i]) {
a0e3063bc133 6948909: Jarsigner removes MANIFEST.MF info for badly packages jar's
weijun
parents: 4350
diff changeset
  1436
                case '\r':
5614
9b34aca7cd0c 6954621: small error in 6948909
weijun
parents: 5462
diff changeset
  1437
                    if (i < len - 1 && bs[i+1] == '\n') i++;
5461
a0e3063bc133 6948909: Jarsigner removes MANIFEST.MF info for badly packages jar's
weijun
parents: 4350
diff changeset
  1438
                    // fallthrough
a0e3063bc133 6948909: Jarsigner removes MANIFEST.MF info for badly packages jar's
weijun
parents: 4350
diff changeset
  1439
                case '\n':
a0e3063bc133 6948909: Jarsigner removes MANIFEST.MF info for badly packages jar's
weijun
parents: 4350
diff changeset
  1440
                    if (newline) return i+1;    //+1 to get length
a0e3063bc133 6948909: Jarsigner removes MANIFEST.MF info for badly packages jar's
weijun
parents: 4350
diff changeset
  1441
                    newline = true;
a0e3063bc133 6948909: Jarsigner removes MANIFEST.MF info for badly packages jar's
weijun
parents: 4350
diff changeset
  1442
                    break;
a0e3063bc133 6948909: Jarsigner removes MANIFEST.MF info for badly packages jar's
weijun
parents: 4350
diff changeset
  1443
                default:
a0e3063bc133 6948909: Jarsigner removes MANIFEST.MF info for badly packages jar's
weijun
parents: 4350
diff changeset
  1444
                    newline = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
        }
5461
a0e3063bc133 6948909: Jarsigner removes MANIFEST.MF info for badly packages jar's
weijun
parents: 4350
diff changeset
  1447
        // If header end is not found, it means the MANIFEST.MF has only
a0e3063bc133 6948909: Jarsigner removes MANIFEST.MF info for badly packages jar's
weijun
parents: 4350
diff changeset
  1448
        // the main attributes section and it does not end with 2 newlines.
a0e3063bc133 6948909: Jarsigner removes MANIFEST.MF info for badly packages jar's
weijun
parents: 4350
diff changeset
  1449
        // Returns the whole length so that it can be completely replaced.
a0e3063bc133 6948909: Jarsigner removes MANIFEST.MF info for badly packages jar's
weijun
parents: 4350
diff changeset
  1450
        return len;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
     * signature-related files include:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
     * . META-INF/MANIFEST.MF
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
     * . META-INF/SIG-*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
     * . META-INF/*.SF
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
     * . META-INF/*.DSA
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
     * . META-INF/*.RSA
4152
bc36a9f01ac6 6870812: enhance security tools to use ECC algorithms
weijun
parents: 3951
diff changeset
  1460
     * . META-INF/*.EC
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
    private boolean signatureRelated(String name) {
4152
bc36a9f01ac6 6870812: enhance security tools to use ECC algorithms
weijun
parents: 3951
diff changeset
  1463
        String ucName = name.toUpperCase(Locale.ENGLISH);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
        if (ucName.equals(JarFile.MANIFEST_NAME) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
            ucName.equals(META_INF) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
            (ucName.startsWith(SIG_PREFIX) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
                ucName.indexOf("/") == ucName.lastIndexOf("/"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
        if (ucName.startsWith(META_INF) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
            SignatureFileVerifier.isBlockOrSF(ucName)) {
4152
bc36a9f01ac6 6870812: enhance security tools to use ECC algorithms
weijun
parents: 3951
diff changeset
  1473
            // .SF/.DSA/.RSA/.EC files in META-INF subdirs
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
            // are not considered signature-related
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
            return (ucName.indexOf("/") == ucName.lastIndexOf("/"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
7977
f47f211cd627 7008713: diamond conversion of kerberos5 and security tools
smarks
parents: 7525
diff changeset
  1481
    Map<CodeSigner,String> cacheForSignerInfo = new IdentityHashMap<>();
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1482
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1483
    /**
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1484
     * 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
  1485
     */
12046
378aa3362868 7149012: jarsigner needs not warn about cert expiration if the jar has a TSA timestamp
weijun
parents: 10788
diff changeset
  1486
    private String signerInfo(CodeSigner signer, String tab) {
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1487
        if (cacheForSignerInfo.containsKey(signer)) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1488
            return cacheForSignerInfo.get(signer);
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1489
        }
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1490
        StringBuffer s = new StringBuffer();
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1491
        List<? extends Certificate> certs = signer.getSignerCertPath().getCertificates();
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1492
        // 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
  1493
        Date timestamp;
378aa3362868 7149012: jarsigner needs not warn about cert expiration if the jar has a TSA timestamp
weijun
parents: 10788
diff changeset
  1494
        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
  1495
        if (ts != null) {
378aa3362868 7149012: jarsigner needs not warn about cert expiration if the jar has a TSA timestamp
weijun
parents: 10788
diff changeset
  1496
            s.append(printTimestamp(tab, ts));
4169
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  1497
            s.append('\n');
12046
378aa3362868 7149012: jarsigner needs not warn about cert expiration if the jar has a TSA timestamp
weijun
parents: 10788
diff changeset
  1498
            timestamp = ts.getTimestamp();
378aa3362868 7149012: jarsigner needs not warn about cert expiration if the jar has a TSA timestamp
weijun
parents: 10788
diff changeset
  1499
        } else {
378aa3362868 7149012: jarsigner needs not warn about cert expiration if the jar has a TSA timestamp
weijun
parents: 10788
diff changeset
  1500
            timestamp = null;
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1501
        }
12046
378aa3362868 7149012: jarsigner needs not warn about cert expiration if the jar has a TSA timestamp
weijun
parents: 10788
diff changeset
  1502
        // display the certificate(s). 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
  1503
        // 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
  1504
        boolean first = true;
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1505
        for (Certificate c : certs) {
12046
378aa3362868 7149012: jarsigner needs not warn about cert expiration if the jar has a TSA timestamp
weijun
parents: 10788
diff changeset
  1506
            s.append(printCert(tab, c, true, timestamp, first));
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1507
            s.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
  1508
            first = false;
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1509
        }
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1510
        try {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1511
            CertPath cp = certificateFactory.generateCertPath(certs);
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1512
            validator.validate(cp, pkixParameters);
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1513
        } catch (Exception e) {
10427
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1514
            if (debug) {
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1515
                e.printStackTrace();
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1516
            }
12046
378aa3362868 7149012: jarsigner needs not warn about cert expiration if the jar has a TSA timestamp
weijun
parents: 10788
diff changeset
  1517
            if (e.getCause() != null &&
378aa3362868 7149012: jarsigner needs not warn about cert expiration if the jar has a TSA timestamp
weijun
parents: 10788
diff changeset
  1518
                    (e.getCause() instanceof CertificateExpiredException ||
378aa3362868 7149012: jarsigner needs not warn about cert expiration if the jar has a TSA timestamp
weijun
parents: 10788
diff changeset
  1519
                     e.getCause() instanceof CertificateNotYetValidException)) {
378aa3362868 7149012: jarsigner needs not warn about cert expiration if the jar has a TSA timestamp
weijun
parents: 10788
diff changeset
  1520
                // No more warning, we alreay have hasExpiredCert or notYetValidCert
378aa3362868 7149012: jarsigner needs not warn about cert expiration if the jar has a TSA timestamp
weijun
parents: 10788
diff changeset
  1521
            } else {
378aa3362868 7149012: jarsigner needs not warn about cert expiration if the jar has a TSA timestamp
weijun
parents: 10788
diff changeset
  1522
                chainNotValidated = true;
378aa3362868 7149012: jarsigner needs not warn about cert expiration if the jar has a TSA timestamp
weijun
parents: 10788
diff changeset
  1523
                s.append(tab + rb.getString(".CertPath.not.validated.") +
378aa3362868 7149012: jarsigner needs not warn about cert expiration if the jar has a TSA timestamp
weijun
parents: 10788
diff changeset
  1524
                        e.getLocalizedMessage() + "]\n");   // TODO
378aa3362868 7149012: jarsigner needs not warn about cert expiration if the jar has a TSA timestamp
weijun
parents: 10788
diff changeset
  1525
            }
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1526
        }
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1527
        String result = s.toString();
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1528
        cacheForSignerInfo.put(signer, result);
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1529
        return result;
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1530
    }
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1531
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
    private void writeEntry(ZipFile zf, ZipOutputStream os, ZipEntry ze)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
    throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
        ZipEntry ze2 = new ZipEntry(ze.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
        ze2.setMethod(ze.getMethod());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
        ze2.setTime(ze.getTime());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
        ze2.setComment(ze.getComment());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
        ze2.setExtra(ze.getExtra());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
        if (ze.getMethod() == ZipEntry.STORED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
            ze2.setSize(ze.getSize());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
            ze2.setCrc(ze.getCrc());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
        os.putNextEntry(ze2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
        writeBytes(zf, ze, os);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
     * Writes all the bytes for a given entry to the specified output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
    private synchronized void writeBytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
        (ZipFile zf, ZipEntry ze, ZipOutputStream os) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
        int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
        InputStream is = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
            is = zf.getInputStream(ze);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
            long left = ze.getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
            while((left > 0) && (n = is.read(buffer, 0, buffer.length)) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
                os.write(buffer, 0, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
                left -= n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
            if (is != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
                is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
    void loadKeyStore(String keyStoreName, boolean prompt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
        if (!nullStream && keyStoreName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
            keyStoreName = System.getProperty("user.home") + File.separator
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
                + ".keystore";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
        try {
10427
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1579
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1580
            certificateFactory = CertificateFactory.getInstance("X.509");
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1581
            validator = CertPathValidator.getInstance("PKIX");
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1582
            Set<TrustAnchor> tas = new HashSet<>();
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1583
            try {
14182
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 12046
diff changeset
  1584
                KeyStore caks = KeyStoreUtil.getCacertsKeyStore();
10427
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1585
                if (caks != null) {
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1586
                    Enumeration<String> aliases = caks.aliases();
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1587
                    while (aliases.hasMoreElements()) {
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1588
                        String a = aliases.nextElement();
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1589
                        try {
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1590
                            tas.add(new TrustAnchor((X509Certificate)caks.getCertificate(a), null));
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1591
                        } catch (Exception e2) {
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1592
                            // ignore, when a SecretkeyEntry does not include a cert
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1593
                        }
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1594
                    }
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1595
                }
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1596
            } catch (Exception e) {
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1597
                // Ignore, if cacerts cannot be loaded
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1598
            }
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1599
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
            if (providerName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
                store = KeyStore.getInstance(storetype);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
                store = KeyStore.getInstance(storetype, providerName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
            // Get pass phrase
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
            // XXX need to disable echo; on UNIX, call getpass(char *prompt)Z
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
            // and on NT call ??
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
            if (token && storepass == null && !protectedPath
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
                    && !KeyStoreUtil.isWindowsKeyStore(storetype)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
                storepass = getPass
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
  1612
                        (rb.getString("Enter.Passphrase.for.keystore."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
            } else if (!token && storepass == null && prompt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
                storepass = getPass
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
  1615
                        (rb.getString("Enter.Passphrase.for.keystore."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
10427
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1618
            try {
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1619
                if (nullStream) {
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1620
                    store.load(null, storepass);
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1621
                } else {
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1622
                    keyStoreName = keyStoreName.replace(File.separatorChar, '/');
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1623
                    URL url = null;
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1624
                    try {
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1625
                        url = new URL(keyStoreName);
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1626
                    } catch (java.net.MalformedURLException e) {
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1627
                        // try as file
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1628
                        url = new File(keyStoreName).toURI().toURL();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
                    }
10427
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1630
                    InputStream is = null;
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1631
                    try {
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1632
                        is = url.openStream();
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1633
                        store.load(is, storepass);
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1634
                    } finally {
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1635
                        if (is != null) {
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1636
                            is.close();
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1637
                        }
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1638
                    }
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1639
                }
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1640
                Enumeration<String> aliases = store.aliases();
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1641
                while (aliases.hasMoreElements()) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1642
                    String a = aliases.nextElement();
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1643
                    try {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1644
                        X509Certificate c = (X509Certificate)store.getCertificate(a);
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1645
                        // Only add TrustedCertificateEntry and self-signed
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1646
                        // PrivateKeyEntry
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1647
                        if (store.isCertificateEntry(a) ||
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1648
                                c.getSubjectDN().equals(c.getIssuerDN())) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1649
                            tas.add(new TrustAnchor(c, null));
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1650
                        }
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1651
                    } catch (Exception e2) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1652
                        // ignore, when a SecretkeyEntry does not include a cert
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1653
                    }
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1654
                }
10427
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1655
            } finally {
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1656
                try {
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1657
                    pkixParameters = new PKIXParameters(tas);
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1658
                    pkixParameters.setRevocationEnabled(false);
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1659
                } catch (InvalidAlgorithmParameterException ex) {
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1660
                    // Only if tas is empty
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1661
                }
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1662
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
        } catch (IOException ioe) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
  1664
            throw new RuntimeException(rb.getString("keystore.load.") +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
                                        ioe.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
        } catch (java.security.cert.CertificateException ce) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
  1667
            throw new RuntimeException(rb.getString("certificate.exception.") +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
                                        ce.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
        } catch (NoSuchProviderException pe) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
  1670
            throw new RuntimeException(rb.getString("keystore.load.") +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
                                        pe.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
        } catch (NoSuchAlgorithmException nsae) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
  1673
            throw new RuntimeException(rb.getString("keystore.load.") +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
                                        nsae.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
        } catch (KeyStoreException kse) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
            throw new RuntimeException
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
  1677
                (rb.getString("unable.to.instantiate.keystore.class.") +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
                kse.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
    X509Certificate getTsaCert(String alias) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
        java.security.cert.Certificate cs = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
            cs = store.getCertificate(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
        } catch (KeyStoreException kse) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
            // this never happens, because keystore has been loaded
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
        if (cs == null || (!(cs instanceof X509Certificate))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
            MessageFormat form = new MessageFormat(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
  1693
                ("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
  1694
            Object[] source = {alias, alias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
            error(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
        return (X509Certificate) cs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
     * Check if userCert is designed to be a code signer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
     * @param userCert the certificate to be examined
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
     * @param bad 3 booleans to show if the KeyUsage, ExtendedKeyUsage,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
     *            NetscapeCertType has codeSigning flag turned on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
     *            If null, the class field badKeyUsage, badExtendedKeyUsage,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
     *            badNetscapeCertType will be set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
    void checkCertUsage(X509Certificate userCert, boolean[] bad) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
        // Can act as a signer?
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1711
        // 1. if KeyUsage, then [0:digitalSignature] or
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1712
        //    [1:nonRepudiation] should be true
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
        // 2. if ExtendedKeyUsage, then should contains ANY or CODE_SIGNING
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
        // 3. if NetscapeCertType, then should contains OBJECT_SIGNING
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
        // 1,2,3 must be true
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
        if (bad != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
            bad[0] = bad[1] = bad[2] = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
        boolean[] keyUsage = userCert.getKeyUsage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
        if (keyUsage != null) {
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1723
            keyUsage = Arrays.copyOf(keyUsage, 9);
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1724
            if (!keyUsage[0] && !keyUsage[1]) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
                if (bad != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
                    bad[0] = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
                    badKeyUsage = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
            List<String> xKeyUsage = userCert.getExtendedKeyUsage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
            if (xKeyUsage != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
                if (!xKeyUsage.contains("2.5.29.37.0") // anyExtendedKeyUsage
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
                        && !xKeyUsage.contains("1.3.6.1.5.5.7.3.3")) {  // codeSigning
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
                    if (bad != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
                        bad[1] = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
                        badExtendedKeyUsage = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
        } catch (java.security.cert.CertificateParsingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
            // shouldn't happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
            // OID_NETSCAPE_CERT_TYPE
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
            byte[] netscapeEx = userCert.getExtensionValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
                    ("2.16.840.1.113730.1.1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
            if (netscapeEx != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
                DerInputStream in = new DerInputStream(netscapeEx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
                byte[] encoded = in.getOctetString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
                encoded = new DerValue(encoded).getUnalignedBitString()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
                        .toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
                NetscapeCertTypeExtension extn =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
                        new NetscapeCertTypeExtension(encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9011
diff changeset
  1760
                Boolean val = extn.get(NetscapeCertTypeExtension.OBJECT_SIGNING);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
                if (!val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
                    if (bad != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
                        bad[2] = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
                        badNetscapeCertType = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
    void getAliasInfo(String alias) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
        Key key = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
            java.security.cert.Certificate[] cs = null;
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1779
            if (altCertChain != null) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1780
                try {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1781
                    cs = CertificateFactory.getInstance("X.509").
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1782
                            generateCertificates(new FileInputStream(altCertChain)).
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1783
                            toArray(new Certificate[0]);
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1784
                } catch (CertificateException ex) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
  1785
                    error(rb.getString("Cannot.restore.certchain.from.file.specified"));
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1786
                } catch (FileNotFoundException ex) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
  1787
                    error(rb.getString("File.specified.by.certchain.does.not.exist"));
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1788
                }
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1789
            } else {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1790
                try {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1791
                    cs = store.getCertificateChain(alias);
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1792
                } catch (KeyStoreException kse) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1793
                    // this never happens, because keystore has been loaded
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1794
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
            }
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1796
            if (cs == null || cs.length == 0) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1797
                if (altCertChain != null) {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1798
                    error(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
  1799
                            ("Certificate.chain.not.found.in.the.file.specified."));
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1800
                } else {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1801
                    MessageFormat form = new MessageFormat(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
  1802
                        ("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
  1803
                    Object[] source = {alias, alias};
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1804
                    error(form.format(source));
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1805
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
            certChain = new X509Certificate[cs.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
            for (int i=0; i<cs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
                if (!(cs[i] instanceof X509Certificate)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
                    error(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
  1812
                        ("found.non.X.509.certificate.in.signer.s.chain"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
                certChain[i] = (X509Certificate)cs[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1817
            // We don't meant to print anything, the next call
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1818
            // checks validity and keyUsage etc
12046
378aa3362868 7149012: jarsigner needs not warn about cert expiration if the jar has a TSA timestamp
weijun
parents: 10788
diff changeset
  1819
            printCert("", certChain[0], true, null, true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
2432
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1821
            try {
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1822
                CertPath cp = certificateFactory.generateCertPath(Arrays.asList(certChain));
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1823
                validator.validate(cp, pkixParameters);
dc17f417ef85 6802846: jarsigner needs enhanced cert validation(options)
weijun
parents: 2
diff changeset
  1824
            } catch (Exception e) {
10427
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1825
                if (debug) {
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1826
                    e.printStackTrace();
c255e1803e4d 7081783: jarsigner error when no $HOME/.keystore
weijun
parents: 10336
diff changeset
  1827
                }
12046
378aa3362868 7149012: jarsigner needs not warn about cert expiration if the jar has a TSA timestamp
weijun
parents: 10788
diff changeset
  1828
                if (e.getCause() != null &&
378aa3362868 7149012: jarsigner needs not warn about cert expiration if the jar has a TSA timestamp
weijun
parents: 10788
diff changeset
  1829
                        (e.getCause() instanceof CertificateExpiredException ||
378aa3362868 7149012: jarsigner needs not warn about cert expiration if the jar has a TSA timestamp
weijun
parents: 10788
diff changeset
  1830
                        e.getCause() instanceof CertificateNotYetValidException)) {
378aa3362868 7149012: jarsigner needs not warn about cert expiration if the jar has a TSA timestamp
weijun
parents: 10788
diff changeset
  1831
                    // No more warning, we alreay have hasExpiredCert or notYetValidCert
378aa3362868 7149012: jarsigner needs not warn about cert expiration if the jar has a TSA timestamp
weijun
parents: 10788
diff changeset
  1832
                } else {
378aa3362868 7149012: jarsigner needs not warn about cert expiration if the jar has a TSA timestamp
weijun
parents: 10788
diff changeset
  1833
                    chainNotValidated = true;
378aa3362868 7149012: jarsigner needs not warn about cert expiration if the jar has a TSA timestamp
weijun
parents: 10788
diff changeset
  1834
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
                if (!token && keypass == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
                    key = store.getKey(alias, storepass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
                    key = store.getKey(alias, keypass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
            } catch (UnrecoverableKeyException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
                if (token) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
                    throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
                } else if (keypass == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
                    // Did not work out, so prompt user for key password
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
                    MessageFormat form = new MessageFormat(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
  1848
                        ("Enter.key.password.for.alias."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
                    Object[] source = {alias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
                    keypass = getPass(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
                    key = store.getKey(alias, keypass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
        } catch (NoSuchAlgorithmException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
            error(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
        } catch (UnrecoverableKeyException e) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
  1857
            error(rb.getString("unable.to.recover.key.from.keystore"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
        } catch (KeyStoreException kse) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
            // this never happens, because keystore has been loaded
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
        if (!(key instanceof PrivateKey)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
            MessageFormat form = new MessageFormat(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
  1864
                ("key.associated.with.alias.not.a.private.key"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
            Object[] source = {alias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
            error(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
            privateKey = (PrivateKey)key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
    void error(String message)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
    {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
  1874
        System.out.println(rb.getString("jarsigner.")+message);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
        System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
    void error(String message, Exception e)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
    {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
  1881
        System.out.println(rb.getString("jarsigner.")+message);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
        System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
    char[] getPass(String prompt)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
        System.err.print(prompt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
        System.err.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
            char[] pass = Password.readPassword(System.in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
            if (pass == null) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
  1896
                error(rb.getString("you.must.enter.key.password"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
                return pass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
        } catch (IOException ioe) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
  1901
            error(rb.getString("unable.to.read.password.")+ioe.getMessage());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
        // this shouldn't happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
     * Reads all the bytes for a given zip entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
    private synchronized byte[] getBytes(ZipFile zf,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
                                         ZipEntry ze) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
        int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
        InputStream is = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
            is = zf.getInputStream(ze);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
            baos.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
            long left = ze.getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
            while((left > 0) && (n = is.read(buffer, 0, buffer.length)) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
                baos.write(buffer, 0, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
                left -= n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
            if (is != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
                is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
        return baos.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
     * Returns manifest entry from given jar file, or null if given jar file
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
     * does not have a manifest entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
    private ZipEntry getManifestFile(ZipFile zf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
        ZipEntry ze = zf.getEntry(JarFile.MANIFEST_NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
        if (ze == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
            // Check all entries for matching name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
            Enumeration<? extends ZipEntry> enum_ = zf.entries();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
            while (enum_.hasMoreElements() && ze == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
                ze = enum_.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
                if (!JarFile.MANIFEST_NAME.equalsIgnoreCase
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
                    (ze.getName())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
                    ze = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
        return ze;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
     * Computes the digests of a zip entry, and returns them as an array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
     * of base64-encoded strings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
    private synchronized String[] getDigests(ZipEntry ze, ZipFile zf,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
                                             MessageDigest[] digests,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
                                             BASE64Encoder encoder)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
        int n, i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
        InputStream is = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
            is = zf.getInputStream(ze);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
            long left = ze.getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
            while((left > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
                && (n = is.read(buffer, 0, buffer.length)) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
                for (i=0; i<digests.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
                    digests[i].update(buffer, 0, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
                left -= n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
            if (is != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
                is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
        // complete the digests
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
        String[] base64Digests = new String[digests.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
        for (i=0; i<digests.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
            base64Digests[i] = encoder.encode(digests[i].digest());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
        return base64Digests;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
     * Computes the digests of a zip entry, and returns them as a list of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
     * attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
    private Attributes getDigestAttributes(ZipEntry ze, ZipFile zf,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
                                           MessageDigest[] digests,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
                                           BASE64Encoder encoder)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
        String[] base64Digests = getDigests(ze, zf, digests, encoder);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
        Attributes attrs = new Attributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
        for (int i=0; i<digests.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
            attrs.putValue(digests[i].getAlgorithm()+"-Digest",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
                           base64Digests[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
        return attrs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
     * Updates the digest attributes of a manifest entry, by adding or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
     * replacing digest values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
     * A digest value is added if the manifest entry does not contain a digest
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
     * for that particular algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
     * A digest value is replaced if it is obsolete.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
     * Returns true if the manifest entry has been changed, and false
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
     * otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
    private boolean updateDigests(ZipEntry ze, ZipFile zf,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
                                  MessageDigest[] digests,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
                                  BASE64Encoder encoder,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
                                  Manifest mf) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
        boolean update = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
        Attributes attrs = mf.getAttributes(ze.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
        String[] base64Digests = getDigests(ze, zf, digests, encoder);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
        for (int i=0; i<digests.length; i++) {
3716
aa66143f7ad1 6876328: different names for the same digest algorithms breaks jarsigner
weijun
parents: 3481
diff changeset
  2027
            // The entry name to be written into attrs
aa66143f7ad1 6876328: different names for the same digest algorithms breaks jarsigner
weijun
parents: 3481
diff changeset
  2028
            String name = null;
aa66143f7ad1 6876328: different names for the same digest algorithms breaks jarsigner
weijun
parents: 3481
diff changeset
  2029
            try {
aa66143f7ad1 6876328: different names for the same digest algorithms breaks jarsigner
weijun
parents: 3481
diff changeset
  2030
                // Find if the digest already exists
aa66143f7ad1 6876328: different names for the same digest algorithms breaks jarsigner
weijun
parents: 3481
diff changeset
  2031
                AlgorithmId aid = AlgorithmId.get(digests[i].getAlgorithm());
aa66143f7ad1 6876328: different names for the same digest algorithms breaks jarsigner
weijun
parents: 3481
diff changeset
  2032
                for (Object key: attrs.keySet()) {
aa66143f7ad1 6876328: different names for the same digest algorithms breaks jarsigner
weijun
parents: 3481
diff changeset
  2033
                    if (key instanceof Attributes.Name) {
aa66143f7ad1 6876328: different names for the same digest algorithms breaks jarsigner
weijun
parents: 3481
diff changeset
  2034
                        String n = ((Attributes.Name)key).toString();
aa66143f7ad1 6876328: different names for the same digest algorithms breaks jarsigner
weijun
parents: 3481
diff changeset
  2035
                        if (n.toUpperCase(Locale.ENGLISH).endsWith("-DIGEST")) {
aa66143f7ad1 6876328: different names for the same digest algorithms breaks jarsigner
weijun
parents: 3481
diff changeset
  2036
                            String tmp = n.substring(0, n.length() - 7);
aa66143f7ad1 6876328: different names for the same digest algorithms breaks jarsigner
weijun
parents: 3481
diff changeset
  2037
                            if (AlgorithmId.get(tmp).equals(aid)) {
aa66143f7ad1 6876328: different names for the same digest algorithms breaks jarsigner
weijun
parents: 3481
diff changeset
  2038
                                name = n;
aa66143f7ad1 6876328: different names for the same digest algorithms breaks jarsigner
weijun
parents: 3481
diff changeset
  2039
                                break;
aa66143f7ad1 6876328: different names for the same digest algorithms breaks jarsigner
weijun
parents: 3481
diff changeset
  2040
                            }
aa66143f7ad1 6876328: different names for the same digest algorithms breaks jarsigner
weijun
parents: 3481
diff changeset
  2041
                        }
aa66143f7ad1 6876328: different names for the same digest algorithms breaks jarsigner
weijun
parents: 3481
diff changeset
  2042
                    }
aa66143f7ad1 6876328: different names for the same digest algorithms breaks jarsigner
weijun
parents: 3481
diff changeset
  2043
                }
aa66143f7ad1 6876328: different names for the same digest algorithms breaks jarsigner
weijun
parents: 3481
diff changeset
  2044
            } catch (NoSuchAlgorithmException nsae) {
aa66143f7ad1 6876328: different names for the same digest algorithms breaks jarsigner
weijun
parents: 3481
diff changeset
  2045
                // Ignored. Writing new digest entry.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
            }
3716
aa66143f7ad1 6876328: different names for the same digest algorithms breaks jarsigner
weijun
parents: 3481
diff changeset
  2047
aa66143f7ad1 6876328: different names for the same digest algorithms breaks jarsigner
weijun
parents: 3481
diff changeset
  2048
            if (name == null) {
aa66143f7ad1 6876328: different names for the same digest algorithms breaks jarsigner
weijun
parents: 3481
diff changeset
  2049
                name = digests[i].getAlgorithm()+"-Digest";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
                attrs.putValue(name, base64Digests[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
                update=true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
                // compare digests, and replace the one in the manifest
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
                // if they are different
3716
aa66143f7ad1 6876328: different names for the same digest algorithms breaks jarsigner
weijun
parents: 3481
diff changeset
  2055
                String mfDigest = attrs.getValue(name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
                if (!mfDigest.equalsIgnoreCase(base64Digests[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
                    attrs.putValue(name, base64Digests[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
                    update=true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
        return update;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
     * Try to load the specified signing mechanism.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
     * The URL class loader is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
    private ContentSigner loadSigningMechanism(String signerClassName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
        String signerClassPath) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
        // construct class loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2073
        String cpString = null;   // make sure env.class.path defaults to dot
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
        // do prepends to get correct ordering
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
        cpString = PathList.appendPath(System.getProperty("env.class.path"), cpString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
        cpString = PathList.appendPath(System.getProperty("java.class.path"), cpString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
        cpString = PathList.appendPath(signerClassPath, cpString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
        URL[] urls = PathList.pathToURLs(cpString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
        ClassLoader appClassLoader = new URLClassLoader(urls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2081
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2082
        // attempt to find signer
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9011
diff changeset
  2083
        Class<?> signerClass = appClassLoader.loadClass(signerClassName);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
        // Check that it implements ContentSigner
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
        Object signer = signerClass.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
        if (!(signer instanceof ContentSigner)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
            MessageFormat form = new MessageFormat(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5627
diff changeset
  2089
                rb.getString("signerClass.is.not.a.signing.mechanism"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
            Object[] source = {signerClass.getName()};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
            throw new IllegalArgumentException(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
        return (ContentSigner)signer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
 * This is a BASE64Encoder that does not insert a default newline at the end of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
 * every output line. This is necessary because java.util.jar does its own
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
 * line management (see Manifest.make72Safe()). Inserting additional new lines
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
 * can cause line-wrapping problems (see CR 6219522).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
class JarBASE64Encoder extends BASE64Encoder {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
     * Encode the suffix that ends every output line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
    protected void encodeLineSuffix(OutputStream aStream) throws IOException { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
class SignatureFile {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
    /** SignatureFile */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
    Manifest sf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
    /** .SF base name */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
    String baseName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
    public SignatureFile(MessageDigest digests[],
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
                         Manifest mf,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
                         ManifestDigester md,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
                         String baseName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
                         boolean signManifest)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
        this.baseName = baseName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
        String version = System.getProperty("java.version");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
        String javaVendor = System.getProperty("java.vendor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
        sf = new Manifest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
        Attributes mattr = sf.getMainAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
        BASE64Encoder encoder = new JarBASE64Encoder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
        mattr.putValue(Attributes.Name.SIGNATURE_VERSION.toString(), "1.0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
        mattr.putValue("Created-By", version + " (" + javaVendor + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
        if (signManifest) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2138
            // sign the whole manifest
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
            for (int i=0; i < digests.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
                mattr.putValue(digests[i].getAlgorithm()+"-Digest-Manifest",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
                               encoder.encode(md.manifestDigest(digests[i])));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2142
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2144
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2145
        // create digest of the manifest main attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2146
        ManifestDigester.Entry mde =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2147
                md.get(ManifestDigester.MF_MAIN_ATTRS, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2148
        if (mde != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2149
            for (int i=0; i < digests.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2150
                mattr.putValue(digests[i].getAlgorithm() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
                        "-Digest-" + ManifestDigester.MF_MAIN_ATTRS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2152
                        encoder.encode(mde.digest(digests[i])));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2153
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2154
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2155
            throw new IllegalStateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2156
                ("ManifestDigester failed to create " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
                "Manifest-Main-Attribute entry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2159
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2160
        /* go through the manifest entries and create the digests */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
        Map<String,Attributes> entries = sf.getEntries();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2163
        Iterator<Map.Entry<String,Attributes>> mit =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2164
                                mf.getEntries().entrySet().iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2165
        while(mit.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
            Map.Entry<String,Attributes> e = mit.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2167
            String name = e.getKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2168
            mde = md.get(name, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2169
            if (mde != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2170
                Attributes attr = new Attributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2171
                for (int i=0; i < digests.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2172
                    attr.putValue(digests[i].getAlgorithm()+"-Digest",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2173
                                  encoder.encode(mde.digest(digests[i])));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2174
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2175
                entries.put(name, attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2176
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2179
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2180
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2181
     * Writes the SignatureFile to the specified OutputStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2182
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2183
     * @param out the output stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2184
     * @exception IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2185
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2186
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2187
    public void write(OutputStream out) throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2188
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2189
        sf.write(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2191
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2192
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2193
     * get .SF file name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2194
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2195
    public String getMetaName()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2196
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2197
        return "META-INF/"+ baseName + ".SF";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2199
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2200
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2201
     * get base file name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2202
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2203
    public String getBaseName()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2204
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2205
        return baseName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2206
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2207
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2208
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2209
     * Generate a signed data block.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2210
     * If a URL or a certificate (containing a URL) for a Timestamping
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2211
     * Authority is supplied then a signature timestamp is generated and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2212
     * inserted into the signed data block.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2213
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2214
     * @param sigalg signature algorithm to use, or null to use default
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2215
     * @param tsaUrl The location of the Timestamping Authority. If null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2216
     *               then no timestamp is requested.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2217
     * @param tsaCert The certificate for the Timestamping Authority. If null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2218
     *               then no timestamp is requested.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2219
     * @param signingMechanism The signing mechanism to use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2220
     * @param args The command-line arguments to jarsigner.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2221
     * @param zipFile The original source Zip file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2222
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2223
    public Block generateBlock(PrivateKey privateKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2224
                               String sigalg,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2225
                               X509Certificate[] certChain,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2226
                               boolean externalSF, String tsaUrl,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2227
                               X509Certificate tsaCert,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2228
                               ContentSigner signingMechanism,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2229
                               String[] args, ZipFile zipFile)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2230
        throws NoSuchAlgorithmException, InvalidKeyException, IOException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2231
            SignatureException, CertificateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2232
    {
8556
d3d6e4643560 7021789: Remove jarsigner -crl option
weijun
parents: 7977
diff changeset
  2233
        return new Block(this, privateKey, sigalg, certChain, externalSF,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2234
                tsaUrl, tsaCert, signingMechanism, args, zipFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2235
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2236
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2237
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2238
    public static class Block {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2239
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2240
        private byte[] block;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2241
        private String blockFileName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2242
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2243
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2244
         * Construct a new signature block.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2245
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2246
        Block(SignatureFile sfg, PrivateKey privateKey, String sigalg,
8556
d3d6e4643560 7021789: Remove jarsigner -crl option
weijun
parents: 7977
diff changeset
  2247
            X509Certificate[] certChain, boolean externalSF, String tsaUrl,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2248
            X509Certificate tsaCert, ContentSigner signingMechanism,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2249
            String[] args, ZipFile zipFile)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2250
            throws NoSuchAlgorithmException, InvalidKeyException, IOException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2251
            SignatureException, CertificateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2252
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2253
            Principal issuerName = certChain[0].getIssuerDN();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2254
            if (!(issuerName instanceof X500Name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2255
                // must extract the original encoded form of DN for subsequent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2256
                // name comparison checks (converting to a String and back to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2257
                // an encoded DN could cause the types of String attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2258
                // values to be changed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2259
                X509CertInfo tbsCert = new
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2260
                    X509CertInfo(certChain[0].getTBSCertificate());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2261
                issuerName = (Principal)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2262
                    tbsCert.get(CertificateIssuerName.NAME + "." +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2263
                                CertificateIssuerName.DN_NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2264
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2265
            BigInteger serial = certChain[0].getSerialNumber();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2266
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2267
            String signatureAlgorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2268
            String keyAlgorithm = privateKey.getAlgorithm();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2269
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2270
             * If no signature algorithm was specified, we choose a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2271
             * default that is compatible with the private key algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2272
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2273
            if (sigalg == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2274
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2275
                if (keyAlgorithm.equalsIgnoreCase("DSA"))
4152
bc36a9f01ac6 6870812: enhance security tools to use ECC algorithms
weijun
parents: 3951
diff changeset
  2276
                    signatureAlgorithm = "SHA1withDSA";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2277
                else if (keyAlgorithm.equalsIgnoreCase("RSA"))
4152
bc36a9f01ac6 6870812: enhance security tools to use ECC algorithms
weijun
parents: 3951
diff changeset
  2278
                    signatureAlgorithm = "SHA256withRSA";
bc36a9f01ac6 6870812: enhance security tools to use ECC algorithms
weijun
parents: 3951
diff changeset
  2279
                else if (keyAlgorithm.equalsIgnoreCase("EC"))
bc36a9f01ac6 6870812: enhance security tools to use ECC algorithms
weijun
parents: 3951
diff changeset
  2280
                    signatureAlgorithm = "SHA256withECDSA";
bc36a9f01ac6 6870812: enhance security tools to use ECC algorithms
weijun
parents: 3951
diff changeset
  2281
                else
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2282
                    throw new RuntimeException("private key is not a DSA or "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2283
                                               + "RSA key");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2284
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2285
                signatureAlgorithm = sigalg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2286
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2287
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2288
            // check common invalid key/signature algorithm combinations
4152
bc36a9f01ac6 6870812: enhance security tools to use ECC algorithms
weijun
parents: 3951
diff changeset
  2289
            String sigAlgUpperCase = signatureAlgorithm.toUpperCase(Locale.ENGLISH);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2290
            if ((sigAlgUpperCase.endsWith("WITHRSA") &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2291
                !keyAlgorithm.equalsIgnoreCase("RSA")) ||
4152
bc36a9f01ac6 6870812: enhance security tools to use ECC algorithms
weijun
parents: 3951
diff changeset
  2292
                (sigAlgUpperCase.endsWith("WITHECDSA") &&
bc36a9f01ac6 6870812: enhance security tools to use ECC algorithms
weijun
parents: 3951
diff changeset
  2293
                !keyAlgorithm.equalsIgnoreCase("EC")) ||
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2294
                (sigAlgUpperCase.endsWith("WITHDSA") &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2295
                !keyAlgorithm.equalsIgnoreCase("DSA"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2296
                throw new SignatureException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2297
                    ("private key algorithm is not compatible with signature algorithm");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2298
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2299
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2300
            blockFileName = "META-INF/"+sfg.getBaseName()+"."+keyAlgorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2301
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2302
            AlgorithmId sigAlg = AlgorithmId.get(signatureAlgorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2303
            AlgorithmId digEncrAlg = AlgorithmId.get(keyAlgorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2304
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2305
            Signature sig = Signature.getInstance(signatureAlgorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2306
            sig.initSign(privateKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2307
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2308
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2309
            sfg.write(baos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2310
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2311
            byte[] content = baos.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2312
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2313
            sig.update(content);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2314
            byte[] signature = sig.sign();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2315
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2316
            // Timestamp the signature and generate the signature block file
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2317
            if (signingMechanism == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2318
                signingMechanism = new TimestampedSigner();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2319
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2320
            URI tsaUri = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2321
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2322
                if (tsaUrl != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2323
                    tsaUri = new URI(tsaUrl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2324
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2325
            } catch (URISyntaxException e) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9011
diff changeset
  2326
                throw new IOException(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2327
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2328
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2329
            // Assemble parameters for the signing mechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2330
            ContentSignerParameters params =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2331
                new JarSignerParameters(args, tsaUri, tsaCert, signature,
8556
d3d6e4643560 7021789: Remove jarsigner -crl option
weijun
parents: 7977
diff changeset
  2332
                    signatureAlgorithm, certChain, content, zipFile);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2333
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2334
            // Generate the signature block
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2335
            block = signingMechanism.generateSignedData(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2336
                    params, externalSF, (tsaUrl != null || tsaCert != null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2337
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2338
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2339
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2340
         * get block file name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2341
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2342
        public String getMetaName()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2343
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2344
            return blockFileName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2345
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2346
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2347
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2348
         * Writes the block file to the specified OutputStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2349
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2350
         * @param out the output stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2351
         * @exception IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2352
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2353
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2354
        public void write(OutputStream out) throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2355
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2356
            out.write(block);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2357
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2358
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2359
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2360
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2361
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2362
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2363
 * This object encapsulates the parameters used to perform content signing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2364
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2365
class JarSignerParameters implements ContentSignerParameters {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2366
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2367
    private String[] args;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2368
    private URI tsa;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2369
    private X509Certificate tsaCertificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2370
    private byte[] signature;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2371
    private String signatureAlgorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2372
    private X509Certificate[] signerCertificateChain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2373
    private byte[] content;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2374
    private ZipFile source;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2375
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2376
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2377
     * Create a new object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2378
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2379
    JarSignerParameters(String[] args, URI tsa, X509Certificate tsaCertificate,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2380
        byte[] signature, String signatureAlgorithm,
8556
d3d6e4643560 7021789: Remove jarsigner -crl option
weijun
parents: 7977
diff changeset
  2381
        X509Certificate[] signerCertificateChain, byte[] content,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2382
        ZipFile source) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2383
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2384
        if (signature == null || signatureAlgorithm == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2385
            signerCertificateChain == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2386
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2387
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2388
        this.args = args;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2389
        this.tsa = tsa;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2390
        this.tsaCertificate = tsaCertificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2391
        this.signature = signature;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2392
        this.signatureAlgorithm = signatureAlgorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2393
        this.signerCertificateChain = signerCertificateChain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2394
        this.content = content;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2395
        this.source = source;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2396
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2397
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2398
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2399
     * Retrieves the command-line arguments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2400
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2401
     * @return The command-line arguments. May be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2402
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2403
    public String[] getCommandLine() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2404
        return args;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2405
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2406
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2407
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2408
     * Retrieves the identifier for a Timestamping Authority (TSA).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2409
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2410
     * @return The TSA identifier. May be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2411
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2412
    public URI getTimestampingAuthority() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2413
        return tsa;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2414
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2415
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2416
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2417
     * Retrieves the certificate for a Timestamping Authority (TSA).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2418
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2419
     * @return The TSA certificate. May be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2420
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2421
    public X509Certificate getTimestampingAuthorityCertificate() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2422
        return tsaCertificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2423
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2424
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2425
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2426
     * Retrieves the signature.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2427
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2428
     * @return The non-null signature bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2429
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2430
    public byte[] getSignature() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2431
        return signature;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2432
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2433
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2434
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2435
     * Retrieves the name of the signature algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2436
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2437
     * @return The non-null string name of the signature algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2438
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2439
    public String getSignatureAlgorithm() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2440
        return signatureAlgorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2441
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2442
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2443
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2444
     * Retrieves the signer's X.509 certificate chain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2445
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2446
     * @return The non-null array of X.509 public-key certificates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2447
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2448
    public X509Certificate[] getSignerCertificateChain() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2449
        return signerCertificateChain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2450
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2451
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2452
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2453
     * Retrieves the content that was signed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2454
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2455
     * @return The content bytes. May be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2456
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2457
    public byte[] getContent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2458
        return content;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2459
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2460
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2461
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2462
     * Retrieves the original source ZIP file before it was signed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2463
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2464
     * @return The original ZIP file. May be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2465
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2466
    public ZipFile getSource() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2467
        return source;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2468
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2469
}