| 1 |
779 |
jeremybenn |
/* Main.java -- JAR signing and verification tool not unlike jarsigner
|
| 2 |
|
|
Copyright (C) 2006, 2007 Free Software Foundation, Inc.
|
| 3 |
|
|
|
| 4 |
|
|
This file is part of GNU Classpath.
|
| 5 |
|
|
|
| 6 |
|
|
GNU Classpath is free software; you can redistribute it and/or modify
|
| 7 |
|
|
it under the terms of the GNU General Public License as published by
|
| 8 |
|
|
the Free Software Foundation; either version 2, or (at your option)
|
| 9 |
|
|
any later version.
|
| 10 |
|
|
|
| 11 |
|
|
GNU Classpath is distributed in the hope that it will be useful, but
|
| 12 |
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 13 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
| 14 |
|
|
General Public License for more details.
|
| 15 |
|
|
|
| 16 |
|
|
You should have received a copy of the GNU General Public License
|
| 17 |
|
|
along with GNU Classpath; see the file COPYING. If not, write to the
|
| 18 |
|
|
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
| 19 |
|
|
02110-1301 USA.
|
| 20 |
|
|
|
| 21 |
|
|
Linking this library statically or dynamically with other modules is
|
| 22 |
|
|
making a combined work based on this library. Thus, the terms and
|
| 23 |
|
|
conditions of the GNU General Public License cover the whole
|
| 24 |
|
|
combination.
|
| 25 |
|
|
|
| 26 |
|
|
As a special exception, the copyright holders of this library give you
|
| 27 |
|
|
permission to link this library with independent modules to produce an
|
| 28 |
|
|
executable, regardless of the license terms of these independent
|
| 29 |
|
|
modules, and to copy and distribute the resulting executable under
|
| 30 |
|
|
terms of your choice, provided that you also meet, for each linked
|
| 31 |
|
|
independent module, the terms and conditions of the license of that
|
| 32 |
|
|
module. An independent module is a module which is not derived from
|
| 33 |
|
|
or based on this library. If you modify this library, you may extend
|
| 34 |
|
|
this exception to your version of the library, but you are not
|
| 35 |
|
|
obligated to do so. If you do not wish to do so, delete this
|
| 36 |
|
|
exception statement from your version. */
|
| 37 |
|
|
|
| 38 |
|
|
|
| 39 |
|
|
package gnu.classpath.tools.jarsigner;
|
| 40 |
|
|
|
| 41 |
|
|
import gnu.classpath.Configuration;
|
| 42 |
|
|
import gnu.classpath.SystemProperties;
|
| 43 |
|
|
import gnu.classpath.tools.common.CallbackUtil;
|
| 44 |
|
|
import gnu.classpath.tools.common.ClasspathToolParser;
|
| 45 |
|
|
import gnu.classpath.tools.common.ProviderUtil;
|
| 46 |
|
|
import gnu.classpath.tools.getopt.FileArgumentCallback;
|
| 47 |
|
|
import gnu.classpath.tools.getopt.Option;
|
| 48 |
|
|
import gnu.classpath.tools.getopt.OptionException;
|
| 49 |
|
|
import gnu.classpath.tools.getopt.OptionGroup;
|
| 50 |
|
|
import gnu.java.security.OID;
|
| 51 |
|
|
import gnu.java.security.Registry;
|
| 52 |
|
|
import gnu.javax.security.auth.callback.ConsoleCallbackHandler;
|
| 53 |
|
|
|
| 54 |
|
|
import java.io.File;
|
| 55 |
|
|
import java.io.FileNotFoundException;
|
| 56 |
|
|
import java.io.IOException;
|
| 57 |
|
|
import java.io.InputStream;
|
| 58 |
|
|
import java.net.URL;
|
| 59 |
|
|
import java.security.Key;
|
| 60 |
|
|
import java.security.KeyStore;
|
| 61 |
|
|
import java.security.KeyStoreException;
|
| 62 |
|
|
import java.security.NoSuchAlgorithmException;
|
| 63 |
|
|
import java.security.PrivateKey;
|
| 64 |
|
|
import java.security.Provider;
|
| 65 |
|
|
import java.security.Security;
|
| 66 |
|
|
import java.security.UnrecoverableKeyException;
|
| 67 |
|
|
import java.security.cert.Certificate;
|
| 68 |
|
|
import java.security.cert.CertificateException;
|
| 69 |
|
|
import java.util.ArrayList;
|
| 70 |
|
|
import java.util.Locale;
|
| 71 |
|
|
import java.util.jar.Attributes.Name;
|
| 72 |
|
|
import java.util.logging.Logger;
|
| 73 |
|
|
|
| 74 |
|
|
import javax.security.auth.callback.Callback;
|
| 75 |
|
|
import javax.security.auth.callback.CallbackHandler;
|
| 76 |
|
|
import javax.security.auth.callback.PasswordCallback;
|
| 77 |
|
|
import javax.security.auth.callback.UnsupportedCallbackException;
|
| 78 |
|
|
|
| 79 |
|
|
/**
|
| 80 |
|
|
* The GNU Classpath implementation of the <i>jarsigner</i> tool.
|
| 81 |
|
|
* <p>
|
| 82 |
|
|
* The <i>jarsigner</i> tool is used to sign and verify JAR (Java ARchive)
|
| 83 |
|
|
* files.
|
| 84 |
|
|
* <p>
|
| 85 |
|
|
* This implementation is intended to be compatible with the behaviour
|
| 86 |
|
|
* described in the public documentation of the same tool included in JDK 1.4.
|
| 87 |
|
|
*/
|
| 88 |
|
|
public class Main
|
| 89 |
|
|
{
|
| 90 |
|
|
protected static final Logger log = Logger.getLogger(Main.class.getName());
|
| 91 |
|
|
static final String KEYTOOL_TOOL = "jarsigner"; //$NON-NLS-1$
|
| 92 |
|
|
private static final Locale EN_US_LOCALE = new Locale("en", "US"); //$NON-NLS-1$ //$NON-NLS-2$
|
| 93 |
|
|
static final String DIGEST = "SHA1-Digest"; //$NON-NLS-1$
|
| 94 |
|
|
static final String DIGEST_MANIFEST = "SHA1-Digest-Manifest"; //$NON-NLS-1$
|
| 95 |
|
|
static final Name DIGEST_ATTR = new Name(DIGEST);
|
| 96 |
|
|
static final Name DIGEST_MANIFEST_ATTR = new Name(DIGEST_MANIFEST);
|
| 97 |
|
|
static final OID DSA_SIGNATURE_OID = new OID(Registry.DSA_OID_STRING);
|
| 98 |
|
|
static final OID RSA_SIGNATURE_OID = new OID(Registry.RSA_OID_STRING);
|
| 99 |
|
|
|
| 100 |
|
|
protected boolean verify;
|
| 101 |
|
|
protected String ksURL;
|
| 102 |
|
|
protected String ksType;
|
| 103 |
|
|
protected String password;
|
| 104 |
|
|
protected String ksPassword;
|
| 105 |
|
|
protected String sigFileName;
|
| 106 |
|
|
protected String signedJarFileName;
|
| 107 |
|
|
protected boolean verbose;
|
| 108 |
|
|
protected boolean certs;
|
| 109 |
|
|
protected boolean internalSF;
|
| 110 |
|
|
protected boolean sectionsOnly;
|
| 111 |
|
|
protected String providerClassName;
|
| 112 |
|
|
protected String jarFileName;
|
| 113 |
|
|
protected String alias;
|
| 114 |
|
|
|
| 115 |
|
|
protected Provider provider;
|
| 116 |
|
|
private boolean providerInstalled;
|
| 117 |
|
|
private char[] ksPasswordChars;
|
| 118 |
|
|
private KeyStore store;
|
| 119 |
|
|
private char[] passwordChars;
|
| 120 |
|
|
private PrivateKey signerPrivateKey;
|
| 121 |
|
|
private Certificate[] signerCertificateChain;
|
| 122 |
|
|
/** The callback handler to use when needing to interact with user. */
|
| 123 |
|
|
private CallbackHandler handler;
|
| 124 |
|
|
/** The command line parser. */
|
| 125 |
|
|
private ToolParser cmdLineParser;
|
| 126 |
|
|
protected ArrayList<String> fileAndAlias = new ArrayList<String>();
|
| 127 |
|
|
|
| 128 |
|
|
private Main()
|
| 129 |
|
|
{
|
| 130 |
|
|
super();
|
| 131 |
|
|
}
|
| 132 |
|
|
|
| 133 |
|
|
public static final void main(String[] args)
|
| 134 |
|
|
{
|
| 135 |
|
|
if (Configuration.DEBUG)
|
| 136 |
|
|
log.entering(Main.class.getName(), "main", args); //$NON-NLS-1$
|
| 137 |
|
|
Main tool = new Main();
|
| 138 |
|
|
int result = 1;
|
| 139 |
|
|
try
|
| 140 |
|
|
{
|
| 141 |
|
|
tool.processArgs(args);
|
| 142 |
|
|
tool.start();
|
| 143 |
|
|
result = 0;
|
| 144 |
|
|
}
|
| 145 |
|
|
catch (SecurityException x)
|
| 146 |
|
|
{
|
| 147 |
|
|
if (Configuration.DEBUG)
|
| 148 |
|
|
log.throwing(Main.class.getName(), "main", x); //$NON-NLS-1$
|
| 149 |
|
|
System.err.println(Messages.getString("Main.7") + x.getMessage()); //$NON-NLS-1$
|
| 150 |
|
|
}
|
| 151 |
|
|
catch (Exception x)
|
| 152 |
|
|
{
|
| 153 |
|
|
if (Configuration.DEBUG)
|
| 154 |
|
|
log.throwing(Main.class.getName(), "main", x); //$NON-NLS-1$
|
| 155 |
|
|
System.err.println(Messages.getString("Main.9") + x); //$NON-NLS-1$
|
| 156 |
|
|
}
|
| 157 |
|
|
finally
|
| 158 |
|
|
{
|
| 159 |
|
|
tool.teardown();
|
| 160 |
|
|
}
|
| 161 |
|
|
if (Configuration.DEBUG)
|
| 162 |
|
|
log.exiting(Main.class.getName(), "main", Integer.valueOf(result)); //$NON-NLS-1$
|
| 163 |
|
|
System.exit(result);
|
| 164 |
|
|
}
|
| 165 |
|
|
|
| 166 |
|
|
/**
|
| 167 |
|
|
* Read the command line arguments setting the tool's parameters in
|
| 168 |
|
|
* preparation for the user desired action.
|
| 169 |
|
|
*
|
| 170 |
|
|
* @param args an array of options (strings).
|
| 171 |
|
|
* @throws Exception if an exception occurs during the process.
|
| 172 |
|
|
*/
|
| 173 |
|
|
private void processArgs(String[] args) throws Exception
|
| 174 |
|
|
{
|
| 175 |
|
|
if (Configuration.DEBUG)
|
| 176 |
|
|
log.entering(this.getClass().getName(), "processArgs", args); //$NON-NLS-1$
|
| 177 |
|
|
cmdLineParser = new ToolParser();
|
| 178 |
|
|
cmdLineParser.initializeParser();
|
| 179 |
|
|
cmdLineParser.parse(args, new ToolParserCallback());
|
| 180 |
|
|
|
| 181 |
|
|
setupCommonParams();
|
| 182 |
|
|
if (verify)
|
| 183 |
|
|
{
|
| 184 |
|
|
if (Configuration.DEBUG)
|
| 185 |
|
|
{
|
| 186 |
|
|
log.fine("Will verify with the following parameters:"); //$NON-NLS-1$
|
| 187 |
|
|
log.fine(" jar-file = '" + jarFileName + "'"); //$NON-NLS-1$ //$NON-NLS-2$
|
| 188 |
|
|
log.fine("Options:"); //$NON-NLS-1$
|
| 189 |
|
|
log.fine(" provider = '" + providerClassName + "'"); //$NON-NLS-1$ //$NON-NLS-2$
|
| 190 |
|
|
log.fine(" verbose ? " + verbose); //$NON-NLS-1$
|
| 191 |
|
|
log.fine(" certs ? " + certs); //$NON-NLS-1$
|
| 192 |
|
|
log.fine(" internalsf ? " + internalSF); //$NON-NLS-1$
|
| 193 |
|
|
log.fine(" sectionsonly ? " + sectionsOnly); //$NON-NLS-1$
|
| 194 |
|
|
}
|
| 195 |
|
|
}
|
| 196 |
|
|
else // sign
|
| 197 |
|
|
{
|
| 198 |
|
|
setupSigningParams();
|
| 199 |
|
|
if (Configuration.DEBUG)
|
| 200 |
|
|
{
|
| 201 |
|
|
log.fine("Will sign with the following parameters:"); //$NON-NLS-1$
|
| 202 |
|
|
log.fine(" jar-file = '" + jarFileName + "'"); //$NON-NLS-1$ //$NON-NLS-2$
|
| 203 |
|
|
log.fine(" alias = '" + alias + "'"); //$NON-NLS-1$ //$NON-NLS-2$
|
| 204 |
|
|
log.fine("Options:"); //$NON-NLS-1$
|
| 205 |
|
|
log.fine(" keystore = '" + ksURL + "'"); //$NON-NLS-1$ //$NON-NLS-2$
|
| 206 |
|
|
log.fine(" storetype = '" + ksType + "'"); //$NON-NLS-1$ //$NON-NLS-2$
|
| 207 |
|
|
log.fine(" storepass = '" + ksPassword + "'"); //$NON-NLS-1$ //$NON-NLS-2$
|
| 208 |
|
|
log.fine(" keypass = '" + password + "'"); //$NON-NLS-1$ //$NON-NLS-2$
|
| 209 |
|
|
log.fine(" sigfile = '" + sigFileName + "'"); //$NON-NLS-1$ //$NON-NLS-2$
|
| 210 |
|
|
log.fine(" signedjar = '" + signedJarFileName + "'"); //$NON-NLS-1$ //$NON-NLS-2$
|
| 211 |
|
|
log.fine(" provider = '" + providerClassName + "'"); //$NON-NLS-1$ //$NON-NLS-2$
|
| 212 |
|
|
log.fine(" verbose ? " + verbose); //$NON-NLS-1$
|
| 213 |
|
|
log.fine(" internalsf ? " + internalSF); //$NON-NLS-1$
|
| 214 |
|
|
log.fine(" sectionsonly ? " + sectionsOnly); //$NON-NLS-1$
|
| 215 |
|
|
}
|
| 216 |
|
|
}
|
| 217 |
|
|
if (Configuration.DEBUG)
|
| 218 |
|
|
log.exiting(this.getClass().getName(), "processArgs"); //$NON-NLS-1$
|
| 219 |
|
|
}
|
| 220 |
|
|
|
| 221 |
|
|
/**
|
| 222 |
|
|
* Invokes the <code>start()</code> method of the concrete handler.
|
| 223 |
|
|
* <p>
|
| 224 |
|
|
* Depending on the result of processing the command line arguments, this
|
| 225 |
|
|
* handler may be one for signing the jar, or verifying it.
|
| 226 |
|
|
*
|
| 227 |
|
|
* @throws Exception if an exception occurs during the process.
|
| 228 |
|
|
*/
|
| 229 |
|
|
private void start() throws Exception
|
| 230 |
|
|
{
|
| 231 |
|
|
if (Configuration.DEBUG)
|
| 232 |
|
|
log.entering(this.getClass().getName(), "start"); //$NON-NLS-1$
|
| 233 |
|
|
if (verify)
|
| 234 |
|
|
{
|
| 235 |
|
|
JarVerifier jv = new JarVerifier(this);
|
| 236 |
|
|
jv.start();
|
| 237 |
|
|
}
|
| 238 |
|
|
else
|
| 239 |
|
|
{
|
| 240 |
|
|
JarSigner js = new JarSigner(this);
|
| 241 |
|
|
js.start();
|
| 242 |
|
|
}
|
| 243 |
|
|
if (Configuration.DEBUG)
|
| 244 |
|
|
log.exiting(this.getClass().getName(), "start"); //$NON-NLS-1$
|
| 245 |
|
|
}
|
| 246 |
|
|
|
| 247 |
|
|
/**
|
| 248 |
|
|
* Ensures that the underlying JVM is left in the same state as we found it
|
| 249 |
|
|
* when we first launched the tool. Specifically, if we have installed a new
|
| 250 |
|
|
* security provider then now is the time to remove it.
|
| 251 |
|
|
* <p>
|
| 252 |
|
|
* Note (rsn): this may not be necessary if we terminate the JVM; i.e. call
|
| 253 |
|
|
* {@link System#exit(int)} at the end of the tool's invocation. Nevertheless
|
| 254 |
|
|
* it's good practive to return the JVM to its initial state.
|
| 255 |
|
|
*/
|
| 256 |
|
|
private void teardown()
|
| 257 |
|
|
{
|
| 258 |
|
|
if (Configuration.DEBUG)
|
| 259 |
|
|
log.entering(this.getClass().getName(), "teardown"); //$NON-NLS-1$
|
| 260 |
|
|
if (providerInstalled)
|
| 261 |
|
|
ProviderUtil.removeProvider(provider.getName());
|
| 262 |
|
|
|
| 263 |
|
|
if (Configuration.DEBUG)
|
| 264 |
|
|
log.exiting(this.getClass().getName(), "teardown"); //$NON-NLS-1$
|
| 265 |
|
|
}
|
| 266 |
|
|
|
| 267 |
|
|
/**
|
| 268 |
|
|
* After processing the command line arguments, this method is invoked to
|
| 269 |
|
|
* process the common parameters which may have been encountered among the
|
| 270 |
|
|
* actual arguments.
|
| 271 |
|
|
* <p>
|
| 272 |
|
|
* Common parameters are those which are allowed in both signing and
|
| 273 |
|
|
* verification modes.
|
| 274 |
|
|
*
|
| 275 |
|
|
* @throws InstantiationException if a security provider class name is
|
| 276 |
|
|
* specified but that class name is that of either an interface or
|
| 277 |
|
|
* an abstract class.
|
| 278 |
|
|
* @throws IllegalAccessException if a security provider class name is
|
| 279 |
|
|
* specified but no 0-arguments constructor is defined for that
|
| 280 |
|
|
* class.
|
| 281 |
|
|
* @throws ClassNotFoundException if a security provider class name is
|
| 282 |
|
|
* specified but no such class was found in the classpath.
|
| 283 |
|
|
* @throws IOException if the JAR file name for signing, or verifying, does
|
| 284 |
|
|
* not exist, exists but denotes a directory, or is not readable.
|
| 285 |
|
|
*/
|
| 286 |
|
|
private void setupCommonParams() throws InstantiationException,
|
| 287 |
|
|
IllegalAccessException, ClassNotFoundException, IOException
|
| 288 |
|
|
{
|
| 289 |
|
|
if (Configuration.DEBUG)
|
| 290 |
|
|
log.entering(this.getClass().getName(), "setupCommonParams"); //$NON-NLS-1$
|
| 291 |
|
|
File jar = new File(jarFileName);
|
| 292 |
|
|
if (! jar.exists())
|
| 293 |
|
|
throw new FileNotFoundException(jarFileName);
|
| 294 |
|
|
|
| 295 |
|
|
if (jar.isDirectory())
|
| 296 |
|
|
throw new IOException(Messages.getFormattedString("Main.70", jarFileName)); //$NON-NLS-1$
|
| 297 |
|
|
|
| 298 |
|
|
if (! jar.canRead())
|
| 299 |
|
|
throw new IOException(Messages.getFormattedString("Main.72", jarFileName)); //$NON-NLS-1$ //$NON-NLS-2$
|
| 300 |
|
|
|
| 301 |
|
|
if (providerClassName != null && providerClassName.length() > 0)
|
| 302 |
|
|
{
|
| 303 |
|
|
provider = (Provider) Class.forName(providerClassName).newInstance();
|
| 304 |
|
|
// is it already installed?
|
| 305 |
|
|
String providerName = provider.getName();
|
| 306 |
|
|
Provider installedProvider = Security.getProvider(providerName);
|
| 307 |
|
|
if (installedProvider != null)
|
| 308 |
|
|
{
|
| 309 |
|
|
if (Configuration.DEBUG)
|
| 310 |
|
|
log.finer("Provider " + providerName + " is already installed"); //$NON-NLS-1$ //$NON-NLS-2$
|
| 311 |
|
|
}
|
| 312 |
|
|
else // install it
|
| 313 |
|
|
installNewProvider();
|
| 314 |
|
|
}
|
| 315 |
|
|
|
| 316 |
|
|
if (! verbose && certs)
|
| 317 |
|
|
{
|
| 318 |
|
|
if (Configuration.DEBUG)
|
| 319 |
|
|
log.fine("Option <certs> is set but <verbose> is not. Ignored"); //$NON-NLS-1$
|
| 320 |
|
|
certs = false;
|
| 321 |
|
|
}
|
| 322 |
|
|
|
| 323 |
|
|
if (Configuration.DEBUG)
|
| 324 |
|
|
log.exiting(this.getClass().getName(), "setupCommonParams"); //$NON-NLS-1$
|
| 325 |
|
|
}
|
| 326 |
|
|
|
| 327 |
|
|
/**
|
| 328 |
|
|
* Install the user defined security provider in the underlying JVM.
|
| 329 |
|
|
* <p>
|
| 330 |
|
|
* Also record this fact so we can remove it when we exit the tool.
|
| 331 |
|
|
*/
|
| 332 |
|
|
private void installNewProvider()
|
| 333 |
|
|
{
|
| 334 |
|
|
if (Configuration.DEBUG)
|
| 335 |
|
|
log.entering(this.getClass().getName(), "installNewProvider"); //$NON-NLS-1$
|
| 336 |
|
|
providerInstalled = ProviderUtil.addProvider(provider) != -1;
|
| 337 |
|
|
if (Configuration.DEBUG)
|
| 338 |
|
|
log.exiting(this.getClass().getName(), "installNewProvider"); //$NON-NLS-1$
|
| 339 |
|
|
}
|
| 340 |
|
|
|
| 341 |
|
|
/**
|
| 342 |
|
|
* After processing the command line arguments, this method is invoked to
|
| 343 |
|
|
* process the parameters which may have been encountered among the actual
|
| 344 |
|
|
* arguments, and which are specific to the signing action of the tool.
|
| 345 |
|
|
*
|
| 346 |
|
|
* @throws KeyStoreException if no implementation of the designated (or
|
| 347 |
|
|
* default type) of a key store is availabe.
|
| 348 |
|
|
* @throws IOException if an I/O related exception occurs during the process.
|
| 349 |
|
|
* @throws NoSuchAlgorithmException if an implementation of an algorithm used
|
| 350 |
|
|
* by the key store is not available.
|
| 351 |
|
|
* @throws CertificateException if an exception occurs while reading a
|
| 352 |
|
|
* certificate from the key store.
|
| 353 |
|
|
* @throws UnsupportedCallbackException if no implementation of a password
|
| 354 |
|
|
* callback is available.
|
| 355 |
|
|
* @throws UnrecoverableKeyException if the wrong password was used to unlock
|
| 356 |
|
|
* the key store.
|
| 357 |
|
|
* @throws SecurityException if the designated alias is not known to the key
|
| 358 |
|
|
* store or is not an Alias of a Key Entry.
|
| 359 |
|
|
*/
|
| 360 |
|
|
private void setupSigningParams() throws KeyStoreException, IOException,
|
| 361 |
|
|
NoSuchAlgorithmException, CertificateException,
|
| 362 |
|
|
UnsupportedCallbackException, UnrecoverableKeyException
|
| 363 |
|
|
{
|
| 364 |
|
|
if (Configuration.DEBUG)
|
| 365 |
|
|
log.entering(this.getClass().getName(), "setupSigningParams"); //$NON-NLS-1$
|
| 366 |
|
|
if (ksURL == null || ksURL.trim().length() == 0)
|
| 367 |
|
|
{
|
| 368 |
|
|
String userHome = SystemProperties.getProperty("user.home"); //$NON-NLS-1$
|
| 369 |
|
|
if (userHome == null || userHome.trim().length() == 0)
|
| 370 |
|
|
throw new SecurityException(Messages.getString("Main.85")); //$NON-NLS-1$
|
| 371 |
|
|
|
| 372 |
|
|
ksURL = "file:" + userHome.trim() + "/.keystore"; //$NON-NLS-1$ //$NON-NLS-2$
|
| 373 |
|
|
}
|
| 374 |
|
|
else
|
| 375 |
|
|
{
|
| 376 |
|
|
ksURL = ksURL.trim();
|
| 377 |
|
|
if (ksURL.indexOf(":") == -1) //$NON-NLS-1$
|
| 378 |
|
|
ksURL = "file:" + ksURL; //$NON-NLS-1$
|
| 379 |
|
|
}
|
| 380 |
|
|
|
| 381 |
|
|
if (ksType == null || ksType.trim().length() == 0)
|
| 382 |
|
|
ksType = KeyStore.getDefaultType();
|
| 383 |
|
|
else
|
| 384 |
|
|
ksType = ksType.trim();
|
| 385 |
|
|
|
| 386 |
|
|
store = KeyStore.getInstance(ksType);
|
| 387 |
|
|
|
| 388 |
|
|
if (ksPassword == null)
|
| 389 |
|
|
{
|
| 390 |
|
|
// ask the user to provide one
|
| 391 |
|
|
PasswordCallback pcb = new PasswordCallback(Messages.getString("Main.92"), //$NON-NLS-1$
|
| 392 |
|
|
false);
|
| 393 |
|
|
getCallbackHandler().handle(new Callback[] { pcb });
|
| 394 |
|
|
ksPasswordChars = pcb.getPassword();
|
| 395 |
|
|
}
|
| 396 |
|
|
else
|
| 397 |
|
|
ksPasswordChars = ksPassword.toCharArray();
|
| 398 |
|
|
|
| 399 |
|
|
URL url = new URL(ksURL);
|
| 400 |
|
|
InputStream stream = url.openStream();
|
| 401 |
|
|
store.load(stream, ksPasswordChars);
|
| 402 |
|
|
|
| 403 |
|
|
if (! store.containsAlias(alias))
|
| 404 |
|
|
throw new SecurityException(Messages.getFormattedString("Main.6", alias)); //$NON-NLS-1$
|
| 405 |
|
|
|
| 406 |
|
|
if (! store.isKeyEntry(alias))
|
| 407 |
|
|
throw new SecurityException(Messages.getFormattedString("Main.95", alias)); //$NON-NLS-1$
|
| 408 |
|
|
|
| 409 |
|
|
Key key;
|
| 410 |
|
|
if (password == null)
|
| 411 |
|
|
{
|
| 412 |
|
|
passwordChars = ksPasswordChars;
|
| 413 |
|
|
try
|
| 414 |
|
|
{
|
| 415 |
|
|
key = store.getKey(alias, passwordChars);
|
| 416 |
|
|
}
|
| 417 |
|
|
catch (UnrecoverableKeyException x)
|
| 418 |
|
|
{
|
| 419 |
|
|
// ask the user to provide one
|
| 420 |
|
|
String prompt = Messages.getFormattedString("Main.97", alias); //$NON-NLS-1$
|
| 421 |
|
|
PasswordCallback pcb = new PasswordCallback(prompt, false);
|
| 422 |
|
|
getCallbackHandler().handle(new Callback[] { pcb });
|
| 423 |
|
|
passwordChars = pcb.getPassword();
|
| 424 |
|
|
// take 2
|
| 425 |
|
|
key = store.getKey(alias, passwordChars);
|
| 426 |
|
|
}
|
| 427 |
|
|
}
|
| 428 |
|
|
else
|
| 429 |
|
|
{
|
| 430 |
|
|
passwordChars = password.toCharArray();
|
| 431 |
|
|
key = store.getKey(alias, passwordChars);
|
| 432 |
|
|
}
|
| 433 |
|
|
|
| 434 |
|
|
if (! (key instanceof PrivateKey))
|
| 435 |
|
|
throw new SecurityException(Messages.getFormattedString("Main.99", alias)); //$NON-NLS-1$
|
| 436 |
|
|
|
| 437 |
|
|
signerPrivateKey = (PrivateKey) key;
|
| 438 |
|
|
signerCertificateChain = store.getCertificateChain(alias);
|
| 439 |
|
|
if (Configuration.DEBUG)
|
| 440 |
|
|
log.fine(String.valueOf(signerCertificateChain));
|
| 441 |
|
|
|
| 442 |
|
|
if (sigFileName == null)
|
| 443 |
|
|
sigFileName = alias;
|
| 444 |
|
|
|
| 445 |
|
|
sigFileName = sigFileName.toUpperCase(EN_US_LOCALE);
|
| 446 |
|
|
if (sigFileName.length() > 8)
|
| 447 |
|
|
sigFileName = sigFileName.substring(0, 8);
|
| 448 |
|
|
|
| 449 |
|
|
char[] chars = sigFileName.toCharArray();
|
| 450 |
|
|
for (int i = 0; i < chars.length; i++)
|
| 451 |
|
|
{
|
| 452 |
|
|
char c = chars[i];
|
| 453 |
|
|
if (! (Character.isLetter(c)
|
| 454 |
|
|
|| Character.isDigit(c)
|
| 455 |
|
|
|| c == '_'
|
| 456 |
|
|
|| c == '-'))
|
| 457 |
|
|
chars[i] = '_';
|
| 458 |
|
|
}
|
| 459 |
|
|
|
| 460 |
|
|
sigFileName = new String(chars);
|
| 461 |
|
|
|
| 462 |
|
|
if (signedJarFileName == null)
|
| 463 |
|
|
signedJarFileName = jarFileName;
|
| 464 |
|
|
|
| 465 |
|
|
if (Configuration.DEBUG)
|
| 466 |
|
|
log.exiting(this.getClass().getName(), "setupSigningParams"); //$NON-NLS-1$
|
| 467 |
|
|
}
|
| 468 |
|
|
|
| 469 |
|
|
boolean isVerbose()
|
| 470 |
|
|
{
|
| 471 |
|
|
return verbose;
|
| 472 |
|
|
}
|
| 473 |
|
|
|
| 474 |
|
|
boolean isCerts()
|
| 475 |
|
|
{
|
| 476 |
|
|
return certs;
|
| 477 |
|
|
}
|
| 478 |
|
|
|
| 479 |
|
|
String getSigFileName()
|
| 480 |
|
|
{
|
| 481 |
|
|
return this.sigFileName;
|
| 482 |
|
|
}
|
| 483 |
|
|
|
| 484 |
|
|
String getJarFileName()
|
| 485 |
|
|
{
|
| 486 |
|
|
return this.jarFileName;
|
| 487 |
|
|
}
|
| 488 |
|
|
|
| 489 |
|
|
boolean isSectionsOnly()
|
| 490 |
|
|
{
|
| 491 |
|
|
return this.sectionsOnly;
|
| 492 |
|
|
}
|
| 493 |
|
|
|
| 494 |
|
|
boolean isInternalSF()
|
| 495 |
|
|
{
|
| 496 |
|
|
return this.internalSF;
|
| 497 |
|
|
}
|
| 498 |
|
|
|
| 499 |
|
|
PrivateKey getSignerPrivateKey()
|
| 500 |
|
|
{
|
| 501 |
|
|
return this.signerPrivateKey;
|
| 502 |
|
|
}
|
| 503 |
|
|
|
| 504 |
|
|
Certificate[] getSignerCertificateChain()
|
| 505 |
|
|
{
|
| 506 |
|
|
return signerCertificateChain;
|
| 507 |
|
|
}
|
| 508 |
|
|
|
| 509 |
|
|
String getSignedJarFileName()
|
| 510 |
|
|
{
|
| 511 |
|
|
return this.signedJarFileName;
|
| 512 |
|
|
}
|
| 513 |
|
|
|
| 514 |
|
|
/**
|
| 515 |
|
|
* Return a CallbackHandler which uses the Console (System.in and System.out)
|
| 516 |
|
|
* for interacting with the user.
|
| 517 |
|
|
* <p>
|
| 518 |
|
|
* This method first finds all currently installed security providers capable
|
| 519 |
|
|
* of providing such service and then in turn attempts to instantiate the
|
| 520 |
|
|
* handler from those providers. As soon as one provider returns a non-null
|
| 521 |
|
|
* instance of the callback handler, the search stops and that instance is
|
| 522 |
|
|
* set to be used from now on.
|
| 523 |
|
|
* <p>
|
| 524 |
|
|
* If no installed providers were found, this method falls back on the GNU
|
| 525 |
|
|
* provider, by-passing the Security search mechanism. The default console
|
| 526 |
|
|
* callback handler implementation is {@link ConsoleCallbackHandler}.
|
| 527 |
|
|
*
|
| 528 |
|
|
* @return a console-based {@link CallbackHandler}.
|
| 529 |
|
|
*/
|
| 530 |
|
|
protected CallbackHandler getCallbackHandler()
|
| 531 |
|
|
{
|
| 532 |
|
|
if (handler == null)
|
| 533 |
|
|
handler = CallbackUtil.getConsoleHandler();
|
| 534 |
|
|
|
| 535 |
|
|
return handler;
|
| 536 |
|
|
}
|
| 537 |
|
|
|
| 538 |
|
|
private class ToolParserCallback
|
| 539 |
|
|
extends FileArgumentCallback
|
| 540 |
|
|
{
|
| 541 |
|
|
public void notifyFile(String fileArgument)
|
| 542 |
|
|
{
|
| 543 |
|
|
fileAndAlias.add(fileArgument);
|
| 544 |
|
|
}
|
| 545 |
|
|
}
|
| 546 |
|
|
|
| 547 |
|
|
private class ToolParser
|
| 548 |
|
|
extends ClasspathToolParser
|
| 549 |
|
|
{
|
| 550 |
|
|
public ToolParser()
|
| 551 |
|
|
{
|
| 552 |
|
|
super(KEYTOOL_TOOL, true);
|
| 553 |
|
|
}
|
| 554 |
|
|
|
| 555 |
|
|
protected void validate() throws OptionException
|
| 556 |
|
|
{
|
| 557 |
|
|
if (fileAndAlias.size() < 1)
|
| 558 |
|
|
throw new OptionException(Messages.getString("Main.133")); //$NON-NLS-1$
|
| 559 |
|
|
|
| 560 |
|
|
jarFileName = (String) fileAndAlias.get(0);
|
| 561 |
|
|
if (! verify) // must have an ALIAS. use "mykey" if undefined
|
| 562 |
|
|
if (fileAndAlias.size() < 2)
|
| 563 |
|
|
{
|
| 564 |
|
|
if (Configuration.DEBUG)
|
| 565 |
|
|
log.fine("Missing ALIAS argument. Will use [mykey] instead"); //$NON-NLS-1$
|
| 566 |
|
|
alias = "mykey"; //$NON-NLS-1$
|
| 567 |
|
|
}
|
| 568 |
|
|
else
|
| 569 |
|
|
alias = fileAndAlias.get(1);
|
| 570 |
|
|
}
|
| 571 |
|
|
|
| 572 |
|
|
public void initializeParser()
|
| 573 |
|
|
{
|
| 574 |
|
|
setHeader(Messages.getString("Main.2")); //$NON-NLS-1$
|
| 575 |
|
|
setFooter(Messages.getString("Main.1")); //$NON-NLS-1$
|
| 576 |
|
|
OptionGroup signGroup = new OptionGroup(Messages.getString("Main.0")); //$NON-NLS-1$
|
| 577 |
|
|
signGroup.add(new Option("keystore", //$NON-NLS-1$
|
| 578 |
|
|
Messages.getString("Main.101"), //$NON-NLS-1$
|
| 579 |
|
|
Messages.getString("Main.102")) //$NON-NLS-1$
|
| 580 |
|
|
{
|
| 581 |
|
|
public void parsed(String argument) throws OptionException
|
| 582 |
|
|
{
|
| 583 |
|
|
ksURL = argument;
|
| 584 |
|
|
}
|
| 585 |
|
|
});
|
| 586 |
|
|
signGroup.add(new Option("storetype", //$NON-NLS-1$
|
| 587 |
|
|
Messages.getString("Main.104"), //$NON-NLS-1$
|
| 588 |
|
|
Messages.getString("Main.105")) //$NON-NLS-1$
|
| 589 |
|
|
{
|
| 590 |
|
|
public void parsed(String argument) throws OptionException
|
| 591 |
|
|
{
|
| 592 |
|
|
ksType = argument;
|
| 593 |
|
|
}
|
| 594 |
|
|
});
|
| 595 |
|
|
signGroup.add(new Option("storepass", //$NON-NLS-1$
|
| 596 |
|
|
Messages.getString("Main.107"), //$NON-NLS-1$
|
| 597 |
|
|
Messages.getString("Main.108")) //$NON-NLS-1$
|
| 598 |
|
|
{
|
| 599 |
|
|
public void parsed(String argument) throws OptionException
|
| 600 |
|
|
{
|
| 601 |
|
|
ksPassword = argument;
|
| 602 |
|
|
}
|
| 603 |
|
|
});
|
| 604 |
|
|
signGroup.add(new Option("keypass", //$NON-NLS-1$
|
| 605 |
|
|
Messages.getString("Main.110"), //$NON-NLS-1$
|
| 606 |
|
|
Messages.getString("Main.111")) //$NON-NLS-1$
|
| 607 |
|
|
{
|
| 608 |
|
|
public void parsed(String argument) throws OptionException
|
| 609 |
|
|
{
|
| 610 |
|
|
password = argument;
|
| 611 |
|
|
}
|
| 612 |
|
|
});
|
| 613 |
|
|
signGroup.add(new Option("sigfile", //$NON-NLS-1$
|
| 614 |
|
|
Messages.getString("Main.113"), //$NON-NLS-1$
|
| 615 |
|
|
Messages.getString("Main.114")) //$NON-NLS-1$
|
| 616 |
|
|
{
|
| 617 |
|
|
public void parsed(String argument) throws OptionException
|
| 618 |
|
|
{
|
| 619 |
|
|
sigFileName = argument;
|
| 620 |
|
|
}
|
| 621 |
|
|
});
|
| 622 |
|
|
signGroup.add(new Option("signedjar", //$NON-NLS-1$
|
| 623 |
|
|
Messages.getString("Main.116"), //$NON-NLS-1$
|
| 624 |
|
|
Messages.getString("Main.117")) //$NON-NLS-1$
|
| 625 |
|
|
{
|
| 626 |
|
|
public void parsed(String argument) throws OptionException
|
| 627 |
|
|
{
|
| 628 |
|
|
signedJarFileName = argument;
|
| 629 |
|
|
}
|
| 630 |
|
|
});
|
| 631 |
|
|
add(signGroup);
|
| 632 |
|
|
|
| 633 |
|
|
OptionGroup verifyGroup = new OptionGroup(Messages.getString("Main.118")); //$NON-NLS-1$
|
| 634 |
|
|
verifyGroup.add(new Option("verify", //$NON-NLS-1$
|
| 635 |
|
|
Messages.getString("Main.120")) //$NON-NLS-1$
|
| 636 |
|
|
{
|
| 637 |
|
|
public void parsed(String argument) throws OptionException
|
| 638 |
|
|
{
|
| 639 |
|
|
verify = true;
|
| 640 |
|
|
}
|
| 641 |
|
|
});
|
| 642 |
|
|
verifyGroup.add(new Option("certs", //$NON-NLS-1$
|
| 643 |
|
|
Messages.getString("Main.122")) //$NON-NLS-1$
|
| 644 |
|
|
{
|
| 645 |
|
|
public void parsed(String argument) throws OptionException
|
| 646 |
|
|
{
|
| 647 |
|
|
certs = true;
|
| 648 |
|
|
}
|
| 649 |
|
|
});
|
| 650 |
|
|
add(verifyGroup);
|
| 651 |
|
|
|
| 652 |
|
|
OptionGroup commonGroup = new OptionGroup(Messages.getString("Main.123")); //$NON-NLS-1$
|
| 653 |
|
|
commonGroup.add(new Option("verbose", //$NON-NLS-1$
|
| 654 |
|
|
Messages.getString("Main.125")) //$NON-NLS-1$
|
| 655 |
|
|
{
|
| 656 |
|
|
public void parsed(String argument) throws OptionException
|
| 657 |
|
|
{
|
| 658 |
|
|
verbose = true;
|
| 659 |
|
|
}
|
| 660 |
|
|
});
|
| 661 |
|
|
commonGroup.add(new Option("internalsf", //$NON-NLS-1$
|
| 662 |
|
|
Messages.getString("Main.127")) //$NON-NLS-1$
|
| 663 |
|
|
{
|
| 664 |
|
|
public void parsed(String argument) throws OptionException
|
| 665 |
|
|
{
|
| 666 |
|
|
internalSF = true;
|
| 667 |
|
|
}
|
| 668 |
|
|
});
|
| 669 |
|
|
commonGroup.add(new Option("sectionsonly", //$NON-NLS-1$
|
| 670 |
|
|
Messages.getString("Main.129")) //$NON-NLS-1$
|
| 671 |
|
|
{
|
| 672 |
|
|
public void parsed(String argument) throws OptionException
|
| 673 |
|
|
{
|
| 674 |
|
|
sectionsOnly = true;
|
| 675 |
|
|
}
|
| 676 |
|
|
});
|
| 677 |
|
|
commonGroup.add(new Option("provider", //$NON-NLS-1$
|
| 678 |
|
|
Messages.getString("Main.131"), //$NON-NLS-1$
|
| 679 |
|
|
Messages.getString("Main.132")) //$NON-NLS-1$
|
| 680 |
|
|
{
|
| 681 |
|
|
public void parsed(String argument) throws OptionException
|
| 682 |
|
|
{
|
| 683 |
|
|
providerClassName = argument;
|
| 684 |
|
|
}
|
| 685 |
|
|
});
|
| 686 |
|
|
add(commonGroup);
|
| 687 |
|
|
}
|
| 688 |
|
|
}
|
| 689 |
|
|
}
|