1 |
779 |
jeremybenn |
/* DeleteCmd.java -- The delete command handler of the keytool
|
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.getopt.Option;
|
44 |
|
|
import gnu.classpath.tools.getopt.OptionException;
|
45 |
|
|
import gnu.classpath.tools.getopt.OptionGroup;
|
46 |
|
|
import gnu.classpath.tools.getopt.Parser;
|
47 |
|
|
|
48 |
|
|
import java.io.IOException;
|
49 |
|
|
import java.security.KeyStoreException;
|
50 |
|
|
import java.security.NoSuchAlgorithmException;
|
51 |
|
|
import java.security.cert.CertificateException;
|
52 |
|
|
import java.util.logging.Logger;
|
53 |
|
|
|
54 |
|
|
import javax.security.auth.callback.Callback;
|
55 |
|
|
import javax.security.auth.callback.NameCallback;
|
56 |
|
|
import javax.security.auth.callback.UnsupportedCallbackException;
|
57 |
|
|
|
58 |
|
|
/**
|
59 |
|
|
* The <b>-delete</b> keytool command handler is used to delete from the key
|
60 |
|
|
* store the entry associated with a designated alias.
|
61 |
|
|
* <p>
|
62 |
|
|
* Possible options for this command are:
|
63 |
|
|
* <p>
|
64 |
|
|
* <dl>
|
65 |
|
|
* <dt>-alias ALIAS</dt>
|
66 |
|
|
* <dd>Every entry, be it a <i>Key Entry</i> or a <i>Trusted
|
67 |
|
|
* Certificate</i>, in a key store is uniquely identified by a user-defined
|
68 |
|
|
* <i>Alias</i> string. Use this option to specify the <i>Alias</i> to use
|
69 |
|
|
* when referring to an entry in the key store. Unless specified otherwise,
|
70 |
|
|
* a default value of <code>mykey</code> shall be used when this option is
|
71 |
|
|
* omitted from the command line.
|
72 |
|
|
* <p></dd>
|
73 |
|
|
*
|
74 |
|
|
* <dt>-storetype STORE_TYPE</dt>
|
75 |
|
|
* <dd>Use this option to specify the type of the key store to use. The
|
76 |
|
|
* default value, if this option is omitted, is that of the property
|
77 |
|
|
* <code>keystore.type</code> in the security properties file, which is
|
78 |
|
|
* obtained by invoking the {@link java.security.KeyStore#getDefaultType()}
|
79 |
|
|
* static method.
|
80 |
|
|
* <p></dd>
|
81 |
|
|
*
|
82 |
|
|
* <dt>-keystore URL</dt>
|
83 |
|
|
* <dd>Use this option to specify the location of the key store to use.
|
84 |
|
|
* The default value is a file {@link java.net.URL} referencing the file
|
85 |
|
|
* named <code>.keystore</code> located in the path returned by the call to
|
86 |
|
|
* {@link java.lang.System#getProperty(String)} using <code>user.home</code>
|
87 |
|
|
* as argument.
|
88 |
|
|
* <p>
|
89 |
|
|
* If a URL was specified, but was found to be malformed --e.g. missing
|
90 |
|
|
* protocol element-- the tool will attempt to use the URL value as a file-
|
91 |
|
|
* name (with absolute or relative path-name) of a key store --as if the
|
92 |
|
|
* protocol was <code>file:</code>.
|
93 |
|
|
* <p></dd>
|
94 |
|
|
*
|
95 |
|
|
* <dt>-storepass PASSWORD</dt>
|
96 |
|
|
* <dd>Use this option to specify the password protecting the key store. If
|
97 |
|
|
* this option is omitted from the command line, you will be prompted to
|
98 |
|
|
* provide a password.
|
99 |
|
|
* <p></dd>
|
100 |
|
|
*
|
101 |
|
|
* <dt>-provider PROVIDER_CLASS_NAME</dt>
|
102 |
|
|
* <dd>A fully qualified class name of a Security Provider to add to the
|
103 |
|
|
* current list of Security Providers already installed in the JVM in-use.
|
104 |
|
|
* If a provider class is specified with this option, and was successfully
|
105 |
|
|
* added to the runtime --i.e. it was not already installed-- then the tool
|
106 |
|
|
* will attempt to removed this Security Provider before exiting.
|
107 |
|
|
* <p></dd>
|
108 |
|
|
*
|
109 |
|
|
* <dt>-v</dt>
|
110 |
|
|
* <dd>Use this option to enable more verbose output.</dd>
|
111 |
|
|
* </dl>
|
112 |
|
|
*/
|
113 |
|
|
class DeleteCmd extends Command
|
114 |
|
|
{
|
115 |
|
|
private static final Logger log = Logger.getLogger(DeleteCmd.class.getName());
|
116 |
|
|
protected String _alias;
|
117 |
|
|
protected String _ksType;
|
118 |
|
|
protected String _ksURL;
|
119 |
|
|
protected String _ksPassword;
|
120 |
|
|
protected String _providerClassName;
|
121 |
|
|
|
122 |
|
|
// default 0-arguments constructor
|
123 |
|
|
|
124 |
|
|
// public setters -----------------------------------------------------------
|
125 |
|
|
|
126 |
|
|
/** @param alias the alias to use. */
|
127 |
|
|
public void setAlias(String alias)
|
128 |
|
|
{
|
129 |
|
|
this._alias = alias;
|
130 |
|
|
}
|
131 |
|
|
|
132 |
|
|
/** @param type the key-store type to use. */
|
133 |
|
|
public void setStoretype(String type)
|
134 |
|
|
{
|
135 |
|
|
this._ksType = type;
|
136 |
|
|
}
|
137 |
|
|
|
138 |
|
|
/** @param url the key-store URL to use. */
|
139 |
|
|
public void setKeystore(String url)
|
140 |
|
|
{
|
141 |
|
|
this._ksURL = url;
|
142 |
|
|
}
|
143 |
|
|
|
144 |
|
|
/** @param password the key-store password to use. */
|
145 |
|
|
public void setStorepass(String password)
|
146 |
|
|
{
|
147 |
|
|
this._ksPassword = password;
|
148 |
|
|
}
|
149 |
|
|
|
150 |
|
|
/** @param className a security provider fully qualified class name to use. */
|
151 |
|
|
public void setProvider(String className)
|
152 |
|
|
{
|
153 |
|
|
this._providerClassName = className;
|
154 |
|
|
}
|
155 |
|
|
|
156 |
|
|
// life-cycle methods -------------------------------------------------------
|
157 |
|
|
|
158 |
|
|
void setup() throws Exception
|
159 |
|
|
{
|
160 |
|
|
setKeyStoreParams(_providerClassName, _ksType, _ksPassword, _ksURL);
|
161 |
|
|
setTheAlias(_alias);
|
162 |
|
|
if (Configuration.DEBUG)
|
163 |
|
|
{
|
164 |
|
|
log.fine("-delete handler will use the following options:"); //$NON-NLS-1$
|
165 |
|
|
log.fine(" -alias=" + alias); //$NON-NLS-1$
|
166 |
|
|
log.fine(" -storetype=" + storeType); //$NON-NLS-1$
|
167 |
|
|
log.fine(" -keystore=" + storeURL); //$NON-NLS-1$
|
168 |
|
|
log.fine(" -provider=" + provider); //$NON-NLS-1$
|
169 |
|
|
log.fine(" -v=" + verbose); //$NON-NLS-1$
|
170 |
|
|
}
|
171 |
|
|
}
|
172 |
|
|
|
173 |
|
|
void start() throws KeyStoreException, NoSuchAlgorithmException,
|
174 |
|
|
CertificateException, IOException
|
175 |
|
|
{
|
176 |
|
|
if (Configuration.DEBUG)
|
177 |
|
|
log.entering(this.getClass().getName(), "start"); //$NON-NLS-1$
|
178 |
|
|
ensureStoreContainsAlias();
|
179 |
|
|
store.deleteEntry(alias);
|
180 |
|
|
saveKeyStore();
|
181 |
|
|
if (Configuration.DEBUG)
|
182 |
|
|
log.exiting(this.getClass().getName(), "start"); //$NON-NLS-1$
|
183 |
|
|
}
|
184 |
|
|
|
185 |
|
|
// own methods --------------------------------------------------------------
|
186 |
|
|
|
187 |
|
|
Parser getParser()
|
188 |
|
|
{
|
189 |
|
|
if (Configuration.DEBUG)
|
190 |
|
|
log.entering(this.getClass().getName(), "getParser"); //$NON-NLS-1$
|
191 |
|
|
Parser result = new ClasspathToolParser(Main.DELETE_CMD, true);
|
192 |
|
|
result.setHeader(Messages.getString("DeleteCmd.18")); //$NON-NLS-1$
|
193 |
|
|
result.setFooter(Messages.getString("DeleteCmd.17")); //$NON-NLS-1$
|
194 |
|
|
OptionGroup options = new OptionGroup(Messages.getString("DeleteCmd.16")); //$NON-NLS-1$
|
195 |
|
|
options.add(new Option(Main.ALIAS_OPT,
|
196 |
|
|
Messages.getString("DeleteCmd.15"), //$NON-NLS-1$
|
197 |
|
|
Messages.getString("DeleteCmd.14")) //$NON-NLS-1$
|
198 |
|
|
{
|
199 |
|
|
public void parsed(String argument) throws OptionException
|
200 |
|
|
{
|
201 |
|
|
_alias = argument;
|
202 |
|
|
}
|
203 |
|
|
});
|
204 |
|
|
options.add(new Option(Main.STORETYPE_OPT,
|
205 |
|
|
Messages.getString("DeleteCmd.13"), //$NON-NLS-1$
|
206 |
|
|
Messages.getString("DeleteCmd.12")) //$NON-NLS-1$
|
207 |
|
|
{
|
208 |
|
|
public void parsed(String argument) throws OptionException
|
209 |
|
|
{
|
210 |
|
|
_ksType = argument;
|
211 |
|
|
}
|
212 |
|
|
});
|
213 |
|
|
options.add(new Option(Main.KEYSTORE_OPT,
|
214 |
|
|
Messages.getString("DeleteCmd.11"), //$NON-NLS-1$
|
215 |
|
|
Messages.getString("DeleteCmd.10")) //$NON-NLS-1$
|
216 |
|
|
{
|
217 |
|
|
public void parsed(String argument) throws OptionException
|
218 |
|
|
{
|
219 |
|
|
_ksURL = argument;
|
220 |
|
|
}
|
221 |
|
|
});
|
222 |
|
|
options.add(new Option(Main.STOREPASS_OPT,
|
223 |
|
|
Messages.getString("DeleteCmd.9"), //$NON-NLS-1$
|
224 |
|
|
Messages.getString("DeleteCmd.8")) //$NON-NLS-1$
|
225 |
|
|
{
|
226 |
|
|
public void parsed(String argument) throws OptionException
|
227 |
|
|
{
|
228 |
|
|
_ksPassword = argument;
|
229 |
|
|
}
|
230 |
|
|
});
|
231 |
|
|
options.add(new Option(Main.PROVIDER_OPT,
|
232 |
|
|
Messages.getString("DeleteCmd.7"), //$NON-NLS-1$
|
233 |
|
|
Messages.getString("DeleteCmd.6")) //$NON-NLS-1$
|
234 |
|
|
{
|
235 |
|
|
public void parsed(String argument) throws OptionException
|
236 |
|
|
{
|
237 |
|
|
_providerClassName = argument;
|
238 |
|
|
}
|
239 |
|
|
});
|
240 |
|
|
options.add(new Option(Main.VERBOSE_OPT,
|
241 |
|
|
Messages.getString("DeleteCmd.5")) //$NON-NLS-1$
|
242 |
|
|
{
|
243 |
|
|
public void parsed(String argument) throws OptionException
|
244 |
|
|
{
|
245 |
|
|
verbose = true;
|
246 |
|
|
}
|
247 |
|
|
});
|
248 |
|
|
result.add(options);
|
249 |
|
|
if (Configuration.DEBUG)
|
250 |
|
|
log.exiting(this.getClass().getName(), "getParser", result); //$NON-NLS-1$
|
251 |
|
|
return result;
|
252 |
|
|
}
|
253 |
|
|
|
254 |
|
|
/**
|
255 |
|
|
* Set the alias to delete from the key store.
|
256 |
|
|
* <p>
|
257 |
|
|
* Unlike in other keytool handlers, the default value (<i>mykey</i>) for the
|
258 |
|
|
* Alias is not used. Instead, if an alias was not found on the command line,
|
259 |
|
|
* the user is prompted to enter one.
|
260 |
|
|
*
|
261 |
|
|
* @param anAlias a possibly null Alias gleaned from the command line.
|
262 |
|
|
* @throws IOException if an I/O related exception occurs during the process.
|
263 |
|
|
* @throws UnsupportedCallbackException if no implementation of a password
|
264 |
|
|
* callback handler was found.
|
265 |
|
|
*/
|
266 |
|
|
private void setTheAlias(String anAlias) throws IOException,
|
267 |
|
|
UnsupportedCallbackException
|
268 |
|
|
{
|
269 |
|
|
if (anAlias == null || anAlias.trim().length() == 0)
|
270 |
|
|
{
|
271 |
|
|
String prompt = Messages.getString("DeleteCmd.19"); //$NON-NLS-1$
|
272 |
|
|
NameCallback ncb = new NameCallback(prompt);
|
273 |
|
|
getCallbackHandler().handle(new Callback[] { ncb });
|
274 |
|
|
anAlias = ncb.getName();
|
275 |
|
|
if (anAlias == null || anAlias.trim().length() == 0)
|
276 |
|
|
throw new SecurityException(Messages.getString("DeleteCmd.20")); //$NON-NLS-1$
|
277 |
|
|
}
|
278 |
|
|
alias = anAlias.trim();
|
279 |
|
|
}
|
280 |
|
|
}
|