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