Imported GNU Classpath 0.90
[official-gcc.git] / libjava / classpath / tools / gnu / classpath / tools / keytool / DeleteCmd.java
blob968af50f8e998705685755b8d574819cbee9c693
1 /* DeleteCmd.java -- The delete command handler of the keytool
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 java.io.IOException;
42 import java.security.KeyStoreException;
43 import java.security.NoSuchAlgorithmException;
44 import java.security.cert.CertificateException;
45 import java.util.logging.Logger;
47 import javax.security.auth.callback.Callback;
48 import javax.security.auth.callback.NameCallback;
49 import javax.security.auth.callback.UnsupportedCallbackException;
51 /**
52 * The <b>-delete</b> keytool command handler is used to delete from the key
53 * store the entry associated with a designated alias.
54 * <p>
55 * Possible options for this command are:
56 * <p>
57 * <dl>
58 * <dt>-alias ALIAS</dt>
59 * <dd>Every entry, be it a <i>Key Entry</i> or a <i>Trusted
60 * Certificate</i>, in a key store is uniquely identified by a user-defined
61 * <i>Alias</i> string. Use this option to specify the <i>Alias</i> to use
62 * when referring to an entry in the key store. Unless specified otherwise,
63 * a default value of <code>mykey</code> shall be used when this option is
64 * omitted from the command line.
65 * <p></dd>
67 * <dt>-storetype STORE_TYP}</dt>
68 * <dd>Use this option to specify the type of the key store to use. The
69 * default value, if this option is omitted, is that of the property
70 * <code>keystore.type</code> in the security properties file, which is
71 * obtained by invoking the {@link java.security.KeyStore#getDefaultType()}
72 * static method.
73 * <p></dd>
75 * <dt>-keystore URL</dt>
76 * <dd>Use this option to specify the location of the key store to use.
77 * The default value is a file {@link java.net.URL} referencing the file
78 * named <code>.keystore</code> located in the path returned by the call to
79 * {@link java.lang.System#getProperty(String)} using <code>user.home</code>
80 * as argument.
81 * <p>
82 * If a URL was specified, but was found to be malformed --e.g. missing
83 * protocol element-- the tool will attempt to use the URL value as a file-
84 * name (with absolute or relative path-name) of a key store --as if the
85 * protocol was <code>file:</code>.
86 * <p></dd>
88 * <dt>-storepass PASSWORD</dt>
89 * <dd>Use this option to specify the password protecting the key store. If
90 * this option is omitted from the command line, you will be prompted to
91 * provide a password.
92 * <p></dd>
94 * <dt>-provider PROVIDER_CLASS_NAME</dt>
95 * <dd>A fully qualified class name of a Security Provider to add to the
96 * current list of Security Providers already installed in the JVM in-use.
97 * If a provider class is specified with this option, and was successfully
98 * added to the runtime --i.e. it was not already installed-- then the tool
99 * will attempt to removed this Security Provider before exiting.
100 * <p></dd>
102 * <dt>-v</dt>
103 * <dd>Use this option to enable more verbose output.</dd>
104 * </dl>
106 class DeleteCmd extends Command
108 private static final Logger log = Logger.getLogger(DeleteCmd.class.getName());
109 private String _alias;
110 private String _ksType;
111 private String _ksURL;
112 private String _ksPassword;
113 private String _providerClassName;
115 // default 0-arguments constructor
117 // public setters -----------------------------------------------------------
119 /** @param alias the alias to use. */
120 public void setAlias(String alias)
122 this._alias = alias;
125 /** @param type the key-store type to use. */
126 public void setStoretype(String type)
128 this._ksType = type;
131 /** @param url the key-store URL to use. */
132 public void setKeystore(String url)
134 this._ksURL = url;
137 /** @param password the key-store password to use. */
138 public void setStorepass(String password)
140 this._ksPassword = password;
143 /** @param className a security provider fully qualified class name to use. */
144 public void setProvider(String className)
146 this._providerClassName = className;
149 // life-cycle methods -------------------------------------------------------
151 int processArgs(String[] args, int i)
153 int limit = args.length;
154 String opt;
155 while (++i < limit)
157 opt = args[i];
158 log.finest("args[" + i + "]=" + opt); //$NON-NLS-1$ //$NON-NLS-2$
159 if (opt == null || opt.length() == 0)
160 continue;
162 if ("-alias".equals(opt)) // -alias ALIAS //$NON-NLS-1$
163 _alias = args[++i];
164 else if ("-storetype".equals(opt)) // -storetype STORE_TYPE //$NON-NLS-1$
165 _ksType = args[++i];
166 else if ("-keystore".equals(opt)) // -keystore URL //$NON-NLS-1$
167 _ksURL = args[++i];
168 else if ("-storepass".equals(opt)) // -storepass PASSWORD //$NON-NLS-1$
169 _ksPassword = args[++i];
170 else if ("-provider".equals(opt)) // -provider PROVIDER_CLASS_NAME //$NON-NLS-1$
171 _providerClassName = args[++i];
172 else if ("-v".equals(opt)) //$NON-NLS-1$
173 verbose = true;
174 else
175 break;
178 return i;
181 void setup() throws Exception
183 setKeyStoreParams(_providerClassName, _ksType, _ksPassword, _ksURL);
184 setTheAlias(_alias);
186 log.finer("-delete handler will use the following options:"); //$NON-NLS-1$
187 log.finer(" -alias=" + alias); //$NON-NLS-1$
188 log.finer(" -storetype=" + storeType); //$NON-NLS-1$
189 log.finer(" -keystore=" + storeURL); //$NON-NLS-1$
190 log.finer(" -storepass=" + String.valueOf(storePasswordChars)); //$NON-NLS-1$
191 log.finer(" -provider=" + provider); //$NON-NLS-1$
192 log.finer(" -v=" + verbose); //$NON-NLS-1$
195 void start() throws KeyStoreException, NoSuchAlgorithmException,
196 CertificateException, IOException
198 log.entering(this.getClass().getName(), "start"); //$NON-NLS-1$
200 ensureStoreContainsAlias();
201 store.deleteEntry(alias);
202 saveKeyStore();
204 log.exiting(this.getClass().getName(), "start"); //$NON-NLS-1$
207 // own methods --------------------------------------------------------------
210 * Set the alias to delete from the key store.
211 * <p>
212 * Unlike in other keytool handlers, the default value (<i>mykey</i>) for the
213 * Alias is not used. Instead, if an alias was not found on the command line,
214 * the user is prompted to enter one.
216 * @param anAlias a possibly null Alias gleaned from the command line.
217 * @throws IOException if an I/O related exception occurs during the process.
218 * @throws UnsupportedCallbackException if no implementation of a password
219 * callback handler was found.
221 private void setTheAlias(String anAlias) throws IOException,
222 UnsupportedCallbackException
224 if (anAlias == null || anAlias.trim().length() == 0)
226 String prompt = Messages.getString("DeleteCmd.19"); //$NON-NLS-1$
227 NameCallback ncb = new NameCallback(prompt);
228 getCallbackHandler().handle(new Callback[] { ncb });
229 anAlias = ncb.getName();
230 if (anAlias == null || anAlias.trim().length() == 0)
231 throw new SecurityException(Messages.getString("DeleteCmd.20")); //$NON-NLS-1$
233 alias = anAlias.trim();