Merge from mainline.
[official-gcc.git] / libjava / classpath / tools / gnu / classpath / tools / keytool / Main.java
blobfb7aa4509da8004611f94fc148b5fb2ad20124d1
1 /* Main.java -- Implementation of the keytool security tool
2 Copyright (C) 2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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.
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.
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.
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.
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. */
39 package gnu.classpath.tools.keytool;
41 import gnu.classpath.tools.HelpPrinter;
42 import gnu.classpath.tools.common.ProviderUtil;
43 import gnu.java.security.Registry;
44 import gnu.javax.crypto.jce.GnuCrypto;
45 import gnu.javax.security.auth.callback.GnuCallbacks;
47 import java.util.logging.Logger;
49 /**
50 * The GNU Classpath implementation of the keytool security tool.
51 * <p>
52 * Except for the <code>-identitydb</code> command, available for importing
53 * JDK 1.1 <i>identities</i> into a key store, this implementation is intended
54 * to be compatible with the behaviour described in the public documentation of
55 * the same tool included in JDK 1.4.
57 public class Main
59 private static final Logger log = Logger.getLogger(Main.class.getName());
60 /** The relative file path to the command tool's help text. */
61 private static final String HELP_PATH = "keytool/keytool.txt"; //$NON-NLS-1$
62 /** The Preferences key name for the last issued certificate serial nbr. */
63 static final String LAST_SERIAL_NUMBER = "lastSerialNumber"; //$NON-NLS-1$
64 /** Constant denoting the X.509 certificate type. */
65 static final String X_509 = "X.509"; //$NON-NLS-1$
67 /** Whether we have already printed the help text or not. */
68 private boolean helpPrinted;
69 /** The new position of GnuCRYPTO provider if it is not already installed. */
70 private int gnuCryptoProviderNdx = -2;
71 /** The new position of GNU Callbacks provider if it is not already installed. */
72 private int gnuCallbacksNdx = -2;
74 private Main()
76 super();
79 public static final void main(String[] args)
81 log.entering(Main.class.getName(), "main", args); //$NON-NLS-1$
83 Main tool = new Main();
84 try
86 tool.setup();
87 tool.start(args);
89 catch (SecurityException x)
91 log.throwing(Main.class.getName(), "main", x); //$NON-NLS-1$
92 System.err.println(Messages.getString("Main.6") + x.getMessage()); //$NON-NLS-1$
94 catch (Exception x)
96 log.throwing(Main.class.getName(), "main", x); //$NON-NLS-1$
97 System.err.println(Messages.getString("Main.8") + x); //$NON-NLS-1$
99 finally
101 tool.teardown();
104 log.exiting(Main.class.getName(), "main"); //$NON-NLS-1$
105 // System.exit(0);
108 // helper methods -----------------------------------------------------------
110 private void start(String[] args) throws Exception
112 log.entering(this.getClass().getName(), "start", args); //$NON-NLS-1$
114 if (args == null)
115 args = new String[0];
117 int limit = args.length;
118 log.finest("args.length=" + limit); //$NON-NLS-1$
119 int i = 0;
120 String opt;
121 Command cmd;
122 while (i < limit)
124 opt = args[i];
125 log.finest("args[" + i + "]=" + opt); //$NON-NLS-1$ //$NON-NLS-2$
126 if (opt == null || opt.length() == 0)
127 continue;
129 cmd = null;
130 if ("-genkey".equals(opt)) //$NON-NLS-1$
131 cmd = new GenKeyCmd();
132 else if ("-import".equals(opt)) //$NON-NLS-1$
133 cmd = new ImportCmd();
134 else if ("-selfcert".equals(opt)) //$NON-NLS-1$
135 cmd = new SelfCertCmd();
136 else if ("-identitydb".equals(opt)) //$NON-NLS-1$
137 cmd = new IdentityDBCmd();
138 else if ("-certreq".equals(opt)) //$NON-NLS-1$
139 cmd = new CertReqCmd();
140 else if ("-export".equals(opt)) //$NON-NLS-1$
141 cmd = new ExportCmd();
142 else if ("-list".equals(opt)) //$NON-NLS-1$
143 cmd = new ListCmd();
144 else if ("-printcert".equals(opt)) //$NON-NLS-1$
145 cmd = new PrintCertCmd();
146 else if ("-keyclone".equals(opt)) //$NON-NLS-1$
147 cmd = new KeyCloneCmd();
148 else if ("-storepasswd".equals(opt)) //$NON-NLS-1$
149 cmd = new StorePasswdCmd();
150 else if ("-keypasswd".equals(opt)) //$NON-NLS-1$
151 cmd = new KeyPasswdCmd();
152 else if ("-delete".equals(opt)) //$NON-NLS-1$
153 cmd = new DeleteCmd();
154 else if ("-help".equals(opt)) //$NON-NLS-1$
156 printHelp();
157 i++;
159 else
161 log.fine("Unknown command [" + opt + "] at index #" + i //$NON-NLS-1$ //$NON-NLS-2$
162 + ". Arguments from that token onward will be ignored"); //$NON-NLS-1$
163 break;
166 if (cmd != null)
168 i = cmd.processArgs(args, i);
169 cmd.doCommand();
173 // the -help command is the default; i.e.
174 // keytool
175 // is equivalent to:
176 // keytool -help
177 if (i == 0)
178 printHelp();
180 if (i < limit) // more options than needed
181 log.fine("Last recognized argument is assumed at index #" + (i - 1) //$NON-NLS-1$
182 + ". Remaining arguments (" + args[i] + "...) will be ignored"); //$NON-NLS-1$ //$NON-NLS-2$
184 log.exiting(this.getClass().getName(), "start"); //$NON-NLS-1$
187 private void setup()
189 log.entering(this.getClass().getName(), "setup"); //$NON-NLS-1$
191 gnuCryptoProviderNdx = ProviderUtil.addProvider(new GnuCrypto());
192 gnuCallbacksNdx = ProviderUtil.addProvider(new GnuCallbacks());
194 log.exiting(this.getClass().getName(), "setup"); //$NON-NLS-1$
197 private void teardown()
199 log.entering(this.getClass().getName(), "teardown"); //$NON-NLS-1$
201 // if we added our own providers remove them
202 if (gnuCryptoProviderNdx > 0)
203 ProviderUtil.removeProvider(Registry.GNU_CRYPTO);
205 if (gnuCallbacksNdx > 0)
206 ProviderUtil.removeProvider("GNU-CALLBACKS"); //$NON-NLS-1$
208 log.exiting(this.getClass().getName(), "teardown"); //$NON-NLS-1$
211 private void printHelp()
213 if (helpPrinted)
214 return;
216 HelpPrinter.printHelp(HELP_PATH);
217 helpPrinted = true;