| 1 |
779 |
jeremybenn |
/* Main.java -- Implementation of the keytool security tool
|
| 2 |
|
|
Copyright (C) 2006 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.keytool;
|
| 40 |
|
|
|
| 41 |
|
|
import gnu.classpath.Configuration;
|
| 42 |
|
|
import gnu.classpath.tools.common.ClasspathToolParser;
|
| 43 |
|
|
import gnu.classpath.tools.common.ProviderUtil;
|
| 44 |
|
|
import gnu.classpath.tools.getopt.Option;
|
| 45 |
|
|
import gnu.classpath.tools.getopt.OptionException;
|
| 46 |
|
|
import gnu.classpath.tools.getopt.OptionGroup;
|
| 47 |
|
|
import gnu.classpath.tools.getopt.Parser;
|
| 48 |
|
|
import gnu.java.security.Registry;
|
| 49 |
|
|
import gnu.javax.crypto.jce.GnuCrypto;
|
| 50 |
|
|
import gnu.javax.security.auth.callback.GnuCallbacks;
|
| 51 |
|
|
|
| 52 |
|
|
import java.util.logging.Logger;
|
| 53 |
|
|
|
| 54 |
|
|
/**
|
| 55 |
|
|
* The GNU Classpath implementation of the keytool security tool.
|
| 56 |
|
|
* <p>
|
| 57 |
|
|
* Except for the <code>-identitydb</code> command, available for importing
|
| 58 |
|
|
* JDK 1.1 <i>identities</i> into a key store, this implementation is intended
|
| 59 |
|
|
* to be compatible with the behaviour described in the public documentation of
|
| 60 |
|
|
* the same tool included in JDK 1.4.
|
| 61 |
|
|
*/
|
| 62 |
|
|
public class Main
|
| 63 |
|
|
{
|
| 64 |
|
|
private static final Logger log = Logger.getLogger(Main.class.getName());
|
| 65 |
|
|
static final String KEYTOOL_TOOL = "keytool"; //$NON-NLS-1$
|
| 66 |
|
|
static final String GENKEY_CMD = "genkey"; //$NON-NLS-1$
|
| 67 |
|
|
static final String IMPORT_CMD = "import"; //$NON-NLS-1$
|
| 68 |
|
|
static final String SELFCERT_CMD = "selfcert"; //$NON-NLS-1$
|
| 69 |
|
|
static final String IDENTITYDB_CMD = "identitydb"; //$NON-NLS-1$
|
| 70 |
|
|
static final String CERTREQ_CMD = "certreq"; //$NON-NLS-1$
|
| 71 |
|
|
static final String EXPORT_CMD = "export"; //$NON-NLS-1$
|
| 72 |
|
|
static final String LIST_CMD = "list"; //$NON-NLS-1$
|
| 73 |
|
|
static final String PRINTCERT_CMD = "printcert"; //$NON-NLS-1$
|
| 74 |
|
|
static final String KEYCLONE_CMD = "keyclone"; //$NON-NLS-1$
|
| 75 |
|
|
static final String STOREPASSWD_CMD = "storepasswd"; //$NON-NLS-1$
|
| 76 |
|
|
static final String KEYPASSWD_CMD = "keypasswd"; //$NON-NLS-1$
|
| 77 |
|
|
static final String DELETE_CMD = "delete"; //$NON-NLS-1$
|
| 78 |
|
|
static final String CACERT_CMD = "cacert"; //$NON-NLS-1$
|
| 79 |
|
|
|
| 80 |
|
|
static final String _GENKEY = "-" + GENKEY_CMD; //$NON-NLS-1$
|
| 81 |
|
|
static final String _IMPORT = "-" + IMPORT_CMD; //$NON-NLS-1$
|
| 82 |
|
|
static final String _SELFCERT = "-" + SELFCERT_CMD; //$NON-NLS-1$
|
| 83 |
|
|
static final String _IDENTITYDB = "-" + IDENTITYDB_CMD; //$NON-NLS-1$
|
| 84 |
|
|
static final String _CERTREQ = "-" + CERTREQ_CMD; //$NON-NLS-1$
|
| 85 |
|
|
static final String _EXPORT = "-" + EXPORT_CMD; //$NON-NLS-1$
|
| 86 |
|
|
static final String _LIST = "-" + LIST_CMD; //$NON-NLS-1$
|
| 87 |
|
|
static final String _PRINTCERT = "-" + PRINTCERT_CMD; //$NON-NLS-1$
|
| 88 |
|
|
static final String _KEYCLONE = "-" + KEYCLONE_CMD; //$NON-NLS-1$
|
| 89 |
|
|
static final String _STOREPASSWD = "-" + STOREPASSWD_CMD; //$NON-NLS-1$
|
| 90 |
|
|
static final String _KEYPASSWD = "-" + KEYPASSWD_CMD; //$NON-NLS-1$
|
| 91 |
|
|
static final String _DELETE = "-" + DELETE_CMD; //$NON-NLS-1$
|
| 92 |
|
|
static final String _HELP = "-help"; //$NON-NLS-1$
|
| 93 |
|
|
static final String _CACERT = "-" + CACERT_CMD; //$NON-NLS-1$
|
| 94 |
|
|
|
| 95 |
|
|
static final String ALIAS_OPT = "alias"; //$NON-NLS-1$
|
| 96 |
|
|
static final String SIGALG_OPT = "sigalg"; //$NON-NLS-1$
|
| 97 |
|
|
static final String KEYALG_OPT = "keyalg"; //$NON-NLS-1$
|
| 98 |
|
|
static final String KEYSIZE_OPT = "keysize"; //$NON-NLS-1$
|
| 99 |
|
|
static final String KEYPASS_OPT = "keypass"; //$NON-NLS-1$
|
| 100 |
|
|
static final String VALIDITY_OPT = "validity"; //$NON-NLS-1$
|
| 101 |
|
|
static final String STORETYPE_OPT = "storetype"; //$NON-NLS-1$
|
| 102 |
|
|
static final String STOREPASS_OPT = "storepass"; //$NON-NLS-1$
|
| 103 |
|
|
static final String KEYSTORE_OPT = "keystore"; //$NON-NLS-1$
|
| 104 |
|
|
static final String PROVIDER_OPT = "provider"; //$NON-NLS-1$
|
| 105 |
|
|
static final String FILE_OPT = "file"; //$NON-NLS-1$
|
| 106 |
|
|
static final String VERBOSE_OPT = "v"; //$NON-NLS-1$
|
| 107 |
|
|
static final String DEST_OPT = "dest"; //$NON-NLS-1$
|
| 108 |
|
|
static final String NEW_OPT = "new"; //$NON-NLS-1$
|
| 109 |
|
|
static final String RFC_OPT = "rfc"; //$NON-NLS-1$
|
| 110 |
|
|
static final String DNAME_OPT = "dname"; //$NON-NLS-1$
|
| 111 |
|
|
|
| 112 |
|
|
/** The Preferences key name for the last issued certificate serial nbr. */
|
| 113 |
|
|
static final String LAST_SERIAL_NUMBER = "lastSerialNumber"; //$NON-NLS-1$
|
| 114 |
|
|
/** Constant denoting the X.509 certificate type. */
|
| 115 |
|
|
static final String X_509 = "X.509"; //$NON-NLS-1$
|
| 116 |
|
|
|
| 117 |
|
|
/** Whether we have already printed the help text or not. */
|
| 118 |
|
|
private boolean helpPrinted;
|
| 119 |
|
|
/** The new position of GnuCRYPTO provider if it is not already installed. */
|
| 120 |
|
|
private int gnuCryptoProviderNdx = -2;
|
| 121 |
|
|
/** The new position of GNU Callbacks provider if it is not already installed. */
|
| 122 |
|
|
private int gnuCallbacksNdx = -2;
|
| 123 |
|
|
/** The command line parser. */
|
| 124 |
|
|
private Parser cmdLineParser;
|
| 125 |
|
|
/** The shutdown hook. */
|
| 126 |
|
|
private ShutdownHook shutdownThread;
|
| 127 |
|
|
|
| 128 |
|
|
private Main()
|
| 129 |
|
|
{
|
| 130 |
|
|
super();
|
| 131 |
|
|
shutdownThread = new ShutdownHook();
|
| 132 |
|
|
Runtime.getRuntime().addShutdownHook(shutdownThread);
|
| 133 |
|
|
}
|
| 134 |
|
|
|
| 135 |
|
|
public static final void main(String[] args)
|
| 136 |
|
|
{
|
| 137 |
|
|
if (Configuration.DEBUG)
|
| 138 |
|
|
log.entering(Main.class.getName(), "main", args); //$NON-NLS-1$
|
| 139 |
|
|
Main tool = new Main();
|
| 140 |
|
|
int result = 1;
|
| 141 |
|
|
try
|
| 142 |
|
|
{
|
| 143 |
|
|
tool.setup();
|
| 144 |
|
|
tool.start(args);
|
| 145 |
|
|
result = 0;
|
| 146 |
|
|
}
|
| 147 |
|
|
catch (OptionException x)
|
| 148 |
|
|
{
|
| 149 |
|
|
System.err.println(x.getMessage());
|
| 150 |
|
|
if (tool.cmdLineParser != null)
|
| 151 |
|
|
tool.cmdLineParser.printHelp();
|
| 152 |
|
|
}
|
| 153 |
|
|
catch (SecurityException x)
|
| 154 |
|
|
{
|
| 155 |
|
|
if (Configuration.DEBUG)
|
| 156 |
|
|
log.throwing(Main.class.getName(), "main", x); //$NON-NLS-1$
|
| 157 |
|
|
System.err.println(Messages.getFormattedString("Main.6", //$NON-NLS-1$
|
| 158 |
|
|
x.getMessage()));
|
| 159 |
|
|
}
|
| 160 |
|
|
catch (Exception x)
|
| 161 |
|
|
{
|
| 162 |
|
|
if (Configuration.DEBUG)
|
| 163 |
|
|
log.throwing(Main.class.getName(), "main", x); //$NON-NLS-1$
|
| 164 |
|
|
System.err.println(Messages.getFormattedString("Main.8", x)); //$NON-NLS-1$
|
| 165 |
|
|
}
|
| 166 |
|
|
finally
|
| 167 |
|
|
{
|
| 168 |
|
|
tool.teardown();
|
| 169 |
|
|
if (tool.shutdownThread != null)
|
| 170 |
|
|
Runtime.getRuntime().removeShutdownHook(tool.shutdownThread);
|
| 171 |
|
|
}
|
| 172 |
|
|
if (Configuration.DEBUG)
|
| 173 |
|
|
log.exiting(Main.class.getName(), "main", Integer.valueOf(result)); //$NON-NLS-1$
|
| 174 |
|
|
System.exit(result);
|
| 175 |
|
|
}
|
| 176 |
|
|
|
| 177 |
|
|
// helper methods -----------------------------------------------------------
|
| 178 |
|
|
|
| 179 |
|
|
private void setup()
|
| 180 |
|
|
{
|
| 181 |
|
|
if (Configuration.DEBUG)
|
| 182 |
|
|
log.entering(this.getClass().getName(), "setup"); //$NON-NLS-1$
|
| 183 |
|
|
cmdLineParser = getParser();
|
| 184 |
|
|
gnuCryptoProviderNdx = ProviderUtil.addProvider(new GnuCrypto());
|
| 185 |
|
|
gnuCallbacksNdx = ProviderUtil.addProvider(new GnuCallbacks());
|
| 186 |
|
|
if (Configuration.DEBUG)
|
| 187 |
|
|
log.exiting(this.getClass().getName(), "setup"); //$NON-NLS-1$
|
| 188 |
|
|
}
|
| 189 |
|
|
|
| 190 |
|
|
private void start(String[] args) throws Exception
|
| 191 |
|
|
{
|
| 192 |
|
|
if (Configuration.DEBUG)
|
| 193 |
|
|
log.entering(this.getClass().getName(), "start"); //$NON-NLS-1$
|
| 194 |
|
|
if (args == null || args.length == 0)
|
| 195 |
|
|
throw new OptionException(""); //$NON-NLS-1$
|
| 196 |
|
|
|
| 197 |
|
|
String opt;
|
| 198 |
|
|
Command cmd;
|
| 199 |
|
|
while (args.length > 0)
|
| 200 |
|
|
{
|
| 201 |
|
|
opt = args[0];
|
| 202 |
|
|
cmd = null;
|
| 203 |
|
|
if (_GENKEY.equals(opt))
|
| 204 |
|
|
cmd = new GenKeyCmd();
|
| 205 |
|
|
else if (_IMPORT.equals(opt))
|
| 206 |
|
|
cmd = new ImportCmd();
|
| 207 |
|
|
else if (_SELFCERT.equals(opt))
|
| 208 |
|
|
cmd = new SelfCertCmd();
|
| 209 |
|
|
else if (_IDENTITYDB.equals(opt))
|
| 210 |
|
|
cmd = new IdentityDBCmd();
|
| 211 |
|
|
else if (_CERTREQ.equals(opt))
|
| 212 |
|
|
cmd = new CertReqCmd();
|
| 213 |
|
|
else if (_EXPORT.equals(opt))
|
| 214 |
|
|
cmd = new ExportCmd();
|
| 215 |
|
|
else if (_LIST.equals(opt))
|
| 216 |
|
|
cmd = new ListCmd();
|
| 217 |
|
|
else if (_PRINTCERT.equals(opt))
|
| 218 |
|
|
cmd = new PrintCertCmd();
|
| 219 |
|
|
else if (_KEYCLONE.equals(opt))
|
| 220 |
|
|
cmd = new KeyCloneCmd();
|
| 221 |
|
|
else if (_STOREPASSWD.equals(opt))
|
| 222 |
|
|
cmd = new StorePasswdCmd();
|
| 223 |
|
|
else if (_KEYPASSWD.equals(opt))
|
| 224 |
|
|
cmd = new KeyPasswdCmd();
|
| 225 |
|
|
else if (_DELETE.equals(opt))
|
| 226 |
|
|
cmd = new DeleteCmd();
|
| 227 |
|
|
else if (_CACERT.equals(opt))
|
| 228 |
|
|
cmd = new CACertCmd();
|
| 229 |
|
|
else if (_HELP.equals(opt))
|
| 230 |
|
|
throw new OptionException(""); //$NON-NLS-1$
|
| 231 |
|
|
else
|
| 232 |
|
|
throw new OptionException(Messages.getFormattedString("Main.18", //$NON-NLS-1$
|
| 233 |
|
|
opt));
|
| 234 |
|
|
|
| 235 |
|
|
String[] cmdArgs = new String[args.length - 1];
|
| 236 |
|
|
System.arraycopy(args, 1, cmdArgs, 0, cmdArgs.length);
|
| 237 |
|
|
args = cmd.processArgs(cmdArgs);
|
| 238 |
|
|
cmd.doCommand();
|
| 239 |
|
|
}
|
| 240 |
|
|
if (Configuration.DEBUG)
|
| 241 |
|
|
log.exiting(this.getClass().getName(), "start"); //$NON-NLS-1$
|
| 242 |
|
|
}
|
| 243 |
|
|
|
| 244 |
|
|
private Parser getParser()
|
| 245 |
|
|
{
|
| 246 |
|
|
if (Configuration.DEBUG)
|
| 247 |
|
|
log.entering(this.getClass().getName(), "getParser"); //$NON-NLS-1$
|
| 248 |
|
|
Parser result = new ClasspathToolParser(KEYTOOL_TOOL, true);
|
| 249 |
|
|
result.setHeader(Messages.getString("Main.19")); //$NON-NLS-1$
|
| 250 |
|
|
result.setFooter(Messages.getString("Main.20")); //$NON-NLS-1$
|
| 251 |
|
|
OptionGroup cmdGroup = new OptionGroup(Messages.getString("Main.21")); //$NON-NLS-1$
|
| 252 |
|
|
cmdGroup.add(new NoParseOption(GENKEY_CMD,
|
| 253 |
|
|
Messages.getString("Main.22"))); //$NON-NLS-1$
|
| 254 |
|
|
cmdGroup.add(new NoParseOption(IMPORT_CMD,
|
| 255 |
|
|
Messages.getString("Main.23"))); //$NON-NLS-1$
|
| 256 |
|
|
cmdGroup.add(new NoParseOption(SELFCERT_CMD,
|
| 257 |
|
|
Messages.getString("Main.24"))); //$NON-NLS-1$
|
| 258 |
|
|
cmdGroup.add(new NoParseOption(IDENTITYDB_CMD,
|
| 259 |
|
|
Messages.getString("Main.25"))); //$NON-NLS-1$
|
| 260 |
|
|
cmdGroup.add(new NoParseOption(CERTREQ_CMD,
|
| 261 |
|
|
Messages.getString("Main.26"))); //$NON-NLS-1$
|
| 262 |
|
|
cmdGroup.add(new NoParseOption(EXPORT_CMD,
|
| 263 |
|
|
Messages.getString("Main.27"))); //$NON-NLS-1$
|
| 264 |
|
|
cmdGroup.add(new NoParseOption(LIST_CMD,
|
| 265 |
|
|
Messages.getString("Main.28"))); //$NON-NLS-1$
|
| 266 |
|
|
cmdGroup.add(new NoParseOption(PRINTCERT_CMD,
|
| 267 |
|
|
Messages.getString("Main.29"))); //$NON-NLS-1$
|
| 268 |
|
|
cmdGroup.add(new NoParseOption(KEYCLONE_CMD,
|
| 269 |
|
|
Messages.getString("Main.30"))); //$NON-NLS-1$
|
| 270 |
|
|
cmdGroup.add(new NoParseOption(STOREPASSWD_CMD,
|
| 271 |
|
|
Messages.getString("Main.31"))); //$NON-NLS-1$
|
| 272 |
|
|
cmdGroup.add(new NoParseOption(KEYPASSWD_CMD,
|
| 273 |
|
|
Messages.getString("Main.32"))); //$NON-NLS-1$
|
| 274 |
|
|
cmdGroup.add(new NoParseOption(DELETE_CMD,
|
| 275 |
|
|
Messages.getString("Main.33"))); //$NON-NLS-1$
|
| 276 |
|
|
cmdGroup.add(new NoParseOption(CACERT_CMD,
|
| 277 |
|
|
Messages.getString("Main.5"))); //$NON-NLS-1$
|
| 278 |
|
|
result.add(cmdGroup);
|
| 279 |
|
|
if (Configuration.DEBUG)
|
| 280 |
|
|
log.exiting(this.getClass().getName(), "getParser", result); //$NON-NLS-1$
|
| 281 |
|
|
return result;
|
| 282 |
|
|
}
|
| 283 |
|
|
|
| 284 |
|
|
void teardown()
|
| 285 |
|
|
{
|
| 286 |
|
|
if (Configuration.DEBUG)
|
| 287 |
|
|
log.entering(this.getClass().getName(), "teardown"); //$NON-NLS-1$
|
| 288 |
|
|
// if we added our own providers remove them
|
| 289 |
|
|
if (gnuCryptoProviderNdx > 0)
|
| 290 |
|
|
ProviderUtil.removeProvider(Registry.GNU_CRYPTO);
|
| 291 |
|
|
|
| 292 |
|
|
if (gnuCallbacksNdx > 0)
|
| 293 |
|
|
ProviderUtil.removeProvider("GNU-CALLBACKS"); //$NON-NLS-1$
|
| 294 |
|
|
|
| 295 |
|
|
if (Configuration.DEBUG)
|
| 296 |
|
|
log.exiting(this.getClass().getName(), "teardown"); //$NON-NLS-1$
|
| 297 |
|
|
}
|
| 298 |
|
|
|
| 299 |
|
|
// Inner class(es)
|
| 300 |
|
|
// ==========================================================================
|
| 301 |
|
|
|
| 302 |
|
|
private class NoParseOption
|
| 303 |
|
|
extends Option
|
| 304 |
|
|
{
|
| 305 |
|
|
public NoParseOption(String name, String description)
|
| 306 |
|
|
{
|
| 307 |
|
|
super(name, description);
|
| 308 |
|
|
}
|
| 309 |
|
|
|
| 310 |
|
|
public NoParseOption(String name, String description, String param)
|
| 311 |
|
|
{
|
| 312 |
|
|
super(name, description, param);
|
| 313 |
|
|
}
|
| 314 |
|
|
|
| 315 |
|
|
public void parsed(String argument) throws OptionException
|
| 316 |
|
|
{
|
| 317 |
|
|
// do nothing
|
| 318 |
|
|
}
|
| 319 |
|
|
}
|
| 320 |
|
|
|
| 321 |
|
|
private class ShutdownHook
|
| 322 |
|
|
extends Thread
|
| 323 |
|
|
{
|
| 324 |
|
|
public void run()
|
| 325 |
|
|
{
|
| 326 |
|
|
teardown();
|
| 327 |
|
|
}
|
| 328 |
|
|
}
|
| 329 |
|
|
}
|