1 |
779 |
jeremybenn |
/* KeyPasswdCmd.java -- The keypasswd 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.SystemProperties;
|
43 |
|
|
import gnu.classpath.tools.common.ClasspathToolParser;
|
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 |
|
|
|
49 |
|
|
import java.io.IOException;
|
50 |
|
|
import java.security.Key;
|
51 |
|
|
import java.security.KeyStoreException;
|
52 |
|
|
import java.security.NoSuchAlgorithmException;
|
53 |
|
|
import java.security.UnrecoverableKeyException;
|
54 |
|
|
import java.security.cert.Certificate;
|
55 |
|
|
import java.security.cert.CertificateException;
|
56 |
|
|
import java.util.Arrays;
|
57 |
|
|
import java.util.logging.Logger;
|
58 |
|
|
|
59 |
|
|
import javax.security.auth.callback.Callback;
|
60 |
|
|
import javax.security.auth.callback.CallbackHandler;
|
61 |
|
|
import javax.security.auth.callback.PasswordCallback;
|
62 |
|
|
import javax.security.auth.callback.TextOutputCallback;
|
63 |
|
|
import javax.security.auth.callback.UnsupportedCallbackException;
|
64 |
|
|
|
65 |
|
|
/**
|
66 |
|
|
* The <b>-keypasswd</b> keytool command handler is used to change the password
|
67 |
|
|
* protecting the private key associated to a designated alias.
|
68 |
|
|
* <p>
|
69 |
|
|
* Possible options for this command are:
|
70 |
|
|
* <p>
|
71 |
|
|
* <dl>
|
72 |
|
|
* <dt>-alias ALIAS</dt>
|
73 |
|
|
* <dd>Every entry, be it a <i>Key Entry</i> or a <i>Trusted
|
74 |
|
|
* Certificate</i>, in a key store is uniquely identified by a user-defined
|
75 |
|
|
* <i>Alias</i> string. Use this option to specify the <i>Alias</i> to use
|
76 |
|
|
* when referring to an entry in the key store. Unless specified otherwise,
|
77 |
|
|
* a default value of <code>mykey</code> shall be used when this option is
|
78 |
|
|
* omitted from the command line.
|
79 |
|
|
* <p></dd>
|
80 |
|
|
*
|
81 |
|
|
* <dt>-keypass PASSWORD</dt>
|
82 |
|
|
* <dd>Use this option to specify the password which the tool will use to
|
83 |
|
|
* unlock the <i>Key Entry</i> associated with the designated <i>Alias</i>.
|
84 |
|
|
* <p>
|
85 |
|
|
* If this option is omitted, the tool will first attempt to unlock the
|
86 |
|
|
* <i>Key Entry</i> using the same password protecting the key store. If
|
87 |
|
|
* this fails, you will then be prompted to provide a password.
|
88 |
|
|
* <p></dd>
|
89 |
|
|
*
|
90 |
|
|
* <dt>-new PASSWORD</dt>
|
91 |
|
|
* <dd>The new, and different, password which will be used to protect the
|
92 |
|
|
* private key material of the designated Key Entry.
|
93 |
|
|
* <p></dd>
|
94 |
|
|
*
|
95 |
|
|
* <dt>-storetype STORE_TYPE</dt>
|
96 |
|
|
* <dd>Use this option to specify the type of the key store to use. The
|
97 |
|
|
* default value, if this option is omitted, is that of the property
|
98 |
|
|
* <code>keystore.type</code> in the security properties file, which is
|
99 |
|
|
* obtained by invoking the {@link java.security.KeyStore#getDefaultType()}
|
100 |
|
|
* static method.
|
101 |
|
|
* <p></dd>
|
102 |
|
|
*
|
103 |
|
|
* <dt>-keystore URL</dt>
|
104 |
|
|
* <dd>Use this option to specify the location of the key store to use.
|
105 |
|
|
* The default value is a file {@link java.net.URL} referencing the file
|
106 |
|
|
* named <code>.keystore</code> located in the path returned by the call to
|
107 |
|
|
* {@link java.lang.System#getProperty(String)} using <code>user.home</code>
|
108 |
|
|
* as argument.
|
109 |
|
|
* <p>
|
110 |
|
|
* If a URL was specified, but was found to be malformed --e.g. missing
|
111 |
|
|
* protocol element-- the tool will attempt to use the URL value as a file-
|
112 |
|
|
* name (with absolute or relative path-name) of a key store --as if the
|
113 |
|
|
* protocol was <code>file:</code>.
|
114 |
|
|
* <p></dd>
|
115 |
|
|
*
|
116 |
|
|
* <dt>-storepass PASSWORD</dt>
|
117 |
|
|
* <dd>Use this option to specify the password protecting the key store. If
|
118 |
|
|
* this option is omitted from the command line, you will be prompted to
|
119 |
|
|
* provide a password.
|
120 |
|
|
* <p></dd>
|
121 |
|
|
*
|
122 |
|
|
* <dt>-provider PROVIDER_CLASS_NAME</dt>
|
123 |
|
|
* <dd>A fully qualified class name of a Security Provider to add to the
|
124 |
|
|
* current list of Security Providers already installed in the JVM in-use.
|
125 |
|
|
* If a provider class is specified with this option, and was successfully
|
126 |
|
|
* added to the runtime --i.e. it was not already installed-- then the tool
|
127 |
|
|
* will attempt to removed this Security Provider before exiting.
|
128 |
|
|
* <p></dd>
|
129 |
|
|
*
|
130 |
|
|
* <dt>-v</dt>
|
131 |
|
|
* <dd>Use this option to enable more verbose output.</dd>
|
132 |
|
|
* </dl>
|
133 |
|
|
*/
|
134 |
|
|
class KeyPasswdCmd extends Command
|
135 |
|
|
{
|
136 |
|
|
private static final Logger log = Logger.getLogger(KeyPasswdCmd.class.getName());
|
137 |
|
|
protected String _alias;
|
138 |
|
|
protected String _password;
|
139 |
|
|
protected String _newPassword;
|
140 |
|
|
protected String _ksType;
|
141 |
|
|
protected String _ksURL;
|
142 |
|
|
protected String _ksPassword;
|
143 |
|
|
protected String _providerClassName;
|
144 |
|
|
private char[] newPasswordChars;
|
145 |
|
|
|
146 |
|
|
// default 0-arguments constructor
|
147 |
|
|
|
148 |
|
|
// public setters -----------------------------------------------------------
|
149 |
|
|
|
150 |
|
|
/** @param alias the alias to use. */
|
151 |
|
|
public void setAlias(String alias)
|
152 |
|
|
{
|
153 |
|
|
this._alias = alias;
|
154 |
|
|
}
|
155 |
|
|
|
156 |
|
|
/** @param password the existing (private) key password to use. */
|
157 |
|
|
public void setKeypass(String password)
|
158 |
|
|
{
|
159 |
|
|
this._password = password;
|
160 |
|
|
}
|
161 |
|
|
|
162 |
|
|
/** @param password the new (private) key password to use. */
|
163 |
|
|
public void setNew(String password)
|
164 |
|
|
{
|
165 |
|
|
this._newPassword = password;
|
166 |
|
|
}
|
167 |
|
|
|
168 |
|
|
/** @param type the key-store type to use. */
|
169 |
|
|
public void setStoretype(String type)
|
170 |
|
|
{
|
171 |
|
|
this._ksType = type;
|
172 |
|
|
}
|
173 |
|
|
|
174 |
|
|
/** @param url the key-store URL to use. */
|
175 |
|
|
public void setKeystore(String url)
|
176 |
|
|
{
|
177 |
|
|
this._ksURL = url;
|
178 |
|
|
}
|
179 |
|
|
|
180 |
|
|
/** @param password the key-store password to use. */
|
181 |
|
|
public void setStorepass(String password)
|
182 |
|
|
{
|
183 |
|
|
this._ksPassword = password;
|
184 |
|
|
}
|
185 |
|
|
|
186 |
|
|
/** @param className a security provider fully qualified class name to use. */
|
187 |
|
|
public void setProvider(String className)
|
188 |
|
|
{
|
189 |
|
|
this._providerClassName = className;
|
190 |
|
|
}
|
191 |
|
|
|
192 |
|
|
// life-cycle methods -------------------------------------------------------
|
193 |
|
|
|
194 |
|
|
void setup() throws Exception
|
195 |
|
|
{
|
196 |
|
|
setKeyStoreParams(_providerClassName, _ksType, _ksPassword, _ksURL);
|
197 |
|
|
setAliasParam(_alias);
|
198 |
|
|
setKeyPasswordNoPrompt(_password);
|
199 |
|
|
if (Configuration.DEBUG)
|
200 |
|
|
{
|
201 |
|
|
log.fine("-keypasswd handler will use the following options:"); //$NON-NLS-1$
|
202 |
|
|
log.fine(" -alias=" + alias); //$NON-NLS-1$
|
203 |
|
|
log.fine(" -new=" + _newPassword); //$NON-NLS-1$
|
204 |
|
|
log.fine(" -storetype=" + storeType); //$NON-NLS-1$
|
205 |
|
|
log.fine(" -keystore=" + storeURL); //$NON-NLS-1$
|
206 |
|
|
log.fine(" -provider=" + provider); //$NON-NLS-1$
|
207 |
|
|
log.fine(" -v=" + verbose); //$NON-NLS-1$
|
208 |
|
|
}
|
209 |
|
|
}
|
210 |
|
|
|
211 |
|
|
void start() throws KeyStoreException, NoSuchAlgorithmException, IOException,
|
212 |
|
|
UnsupportedCallbackException, UnrecoverableKeyException,
|
213 |
|
|
CertificateException
|
214 |
|
|
{
|
215 |
|
|
if (Configuration.DEBUG)
|
216 |
|
|
log.entering(this.getClass().getName(), "start"); //$NON-NLS-1$
|
217 |
|
|
// 1. get the key entry and certificate chain associated to alias
|
218 |
|
|
Key privateKey = getAliasPrivateKey();
|
219 |
|
|
Certificate[] chain = store.getCertificateChain(alias);
|
220 |
|
|
|
221 |
|
|
// 2. replace the old entry
|
222 |
|
|
setNewKeyPassword(_newPassword);
|
223 |
|
|
store.setKeyEntry(alias, privateKey, newPasswordChars, chain);
|
224 |
|
|
|
225 |
|
|
// 3. persist the key store
|
226 |
|
|
saveKeyStore();
|
227 |
|
|
if (Configuration.DEBUG)
|
228 |
|
|
log.exiting(this.getClass().getName(), "start"); //$NON-NLS-1$
|
229 |
|
|
}
|
230 |
|
|
|
231 |
|
|
// own methods --------------------------------------------------------------
|
232 |
|
|
|
233 |
|
|
Parser getParser()
|
234 |
|
|
{
|
235 |
|
|
if (Configuration.DEBUG)
|
236 |
|
|
log.entering(this.getClass().getName(), "getParser"); //$NON-NLS-1$
|
237 |
|
|
Parser result = new ClasspathToolParser(Main.KEYPASSWD_CMD, true);
|
238 |
|
|
result.setHeader(Messages.getString("KeyPasswdCmd.23")); //$NON-NLS-1$
|
239 |
|
|
result.setFooter(Messages.getString("KeyPasswdCmd.22")); //$NON-NLS-1$
|
240 |
|
|
OptionGroup options = new OptionGroup(Messages.getString("KeyPasswdCmd.21")); //$NON-NLS-1$
|
241 |
|
|
options.add(new Option(Main.ALIAS_OPT,
|
242 |
|
|
Messages.getString("KeyPasswdCmd.20"), //$NON-NLS-1$
|
243 |
|
|
Messages.getString("KeyPasswdCmd.19")) //$NON-NLS-1$
|
244 |
|
|
{
|
245 |
|
|
public void parsed(String argument) throws OptionException
|
246 |
|
|
{
|
247 |
|
|
_alias = argument;
|
248 |
|
|
}
|
249 |
|
|
});
|
250 |
|
|
options.add(new Option(Main.KEYPASS_OPT,
|
251 |
|
|
Messages.getString("KeyPasswdCmd.18"), //$NON-NLS-1$
|
252 |
|
|
Messages.getString("KeyPasswdCmd.9")) //$NON-NLS-1$
|
253 |
|
|
{
|
254 |
|
|
public void parsed(String argument) throws OptionException
|
255 |
|
|
{
|
256 |
|
|
_password = argument;
|
257 |
|
|
}
|
258 |
|
|
});
|
259 |
|
|
options.add(new Option(Main.NEW_OPT,
|
260 |
|
|
Messages.getString("KeyPasswdCmd.16"), //$NON-NLS-1$
|
261 |
|
|
Messages.getString("KeyPasswdCmd.9")) //$NON-NLS-1$
|
262 |
|
|
{
|
263 |
|
|
public void parsed(String argument) throws OptionException
|
264 |
|
|
{
|
265 |
|
|
_newPassword = argument;
|
266 |
|
|
}
|
267 |
|
|
});
|
268 |
|
|
options.add(new Option(Main.STORETYPE_OPT,
|
269 |
|
|
Messages.getString("KeyPasswdCmd.14"), //$NON-NLS-1$
|
270 |
|
|
Messages.getString("KeyPasswdCmd.13")) //$NON-NLS-1$
|
271 |
|
|
{
|
272 |
|
|
public void parsed(String argument) throws OptionException
|
273 |
|
|
{
|
274 |
|
|
_ksType = argument;
|
275 |
|
|
}
|
276 |
|
|
});
|
277 |
|
|
options.add(new Option(Main.KEYSTORE_OPT,
|
278 |
|
|
Messages.getString("KeyPasswdCmd.12"), //$NON-NLS-1$
|
279 |
|
|
Messages.getString("KeyPasswdCmd.11")) //$NON-NLS-1$
|
280 |
|
|
{
|
281 |
|
|
public void parsed(String argument) throws OptionException
|
282 |
|
|
{
|
283 |
|
|
_ksURL = argument;
|
284 |
|
|
}
|
285 |
|
|
});
|
286 |
|
|
options.add(new Option(Main.STOREPASS_OPT,
|
287 |
|
|
Messages.getString("KeyPasswdCmd.10"), //$NON-NLS-1$
|
288 |
|
|
Messages.getString("KeyPasswdCmd.9")) //$NON-NLS-1$
|
289 |
|
|
{
|
290 |
|
|
public void parsed(String argument) throws OptionException
|
291 |
|
|
{
|
292 |
|
|
_ksPassword = argument;
|
293 |
|
|
}
|
294 |
|
|
});
|
295 |
|
|
options.add(new Option(Main.PROVIDER_OPT,
|
296 |
|
|
Messages.getString("KeyPasswdCmd.8"), //$NON-NLS-1$
|
297 |
|
|
Messages.getString("KeyPasswdCmd.7")) //$NON-NLS-1$
|
298 |
|
|
{
|
299 |
|
|
public void parsed(String argument) throws OptionException
|
300 |
|
|
{
|
301 |
|
|
_providerClassName = argument;
|
302 |
|
|
}
|
303 |
|
|
});
|
304 |
|
|
options.add(new Option(Main.VERBOSE_OPT,
|
305 |
|
|
Messages.getString("KeyPasswdCmd.6")) //$NON-NLS-1$
|
306 |
|
|
{
|
307 |
|
|
public void parsed(String argument) throws OptionException
|
308 |
|
|
{
|
309 |
|
|
verbose = true;
|
310 |
|
|
}
|
311 |
|
|
});
|
312 |
|
|
result.add(options);
|
313 |
|
|
if (Configuration.DEBUG)
|
314 |
|
|
log.exiting(this.getClass().getName(), "getParser", result); //$NON-NLS-1$
|
315 |
|
|
return result;
|
316 |
|
|
}
|
317 |
|
|
|
318 |
|
|
/**
|
319 |
|
|
* Set the new password to use for protecting Alias's private key.
|
320 |
|
|
*
|
321 |
|
|
* @param password the new key password. if <code>null</code> prompt the
|
322 |
|
|
* user to provide one. When prompting, the password is entered twice
|
323 |
|
|
* and compared for a match.
|
324 |
|
|
* @throws IOException if an I/O related exception occurs during the process.
|
325 |
|
|
* @throws UnsupportedCallbackException if no implementation of a password
|
326 |
|
|
* callback handler was found.
|
327 |
|
|
*/
|
328 |
|
|
private void setNewKeyPassword(String password) throws IOException,
|
329 |
|
|
UnsupportedCallbackException
|
330 |
|
|
{
|
331 |
|
|
if (password != null)
|
332 |
|
|
newPasswordChars = password.toCharArray();
|
333 |
|
|
else
|
334 |
|
|
{
|
335 |
|
|
boolean ok = false;
|
336 |
|
|
Callback[] prompts = new Callback[1];
|
337 |
|
|
Callback[] errors = new Callback[1];
|
338 |
|
|
for (int i = 0; i < 3; i++)
|
339 |
|
|
if (prompt4NewPassword(getCallbackHandler(), prompts, errors))
|
340 |
|
|
{
|
341 |
|
|
ok = true;
|
342 |
|
|
break;
|
343 |
|
|
}
|
344 |
|
|
if (! ok)
|
345 |
|
|
throw new SecurityException(Messages.getString("StorePasswdCmd.19")); //$NON-NLS-1$
|
346 |
|
|
}
|
347 |
|
|
}
|
348 |
|
|
|
349 |
|
|
private boolean prompt4NewPassword(CallbackHandler handler,
|
350 |
|
|
Callback[] prompts, Callback[] errors)
|
351 |
|
|
throws IOException, UnsupportedCallbackException
|
352 |
|
|
{
|
353 |
|
|
// prompt user (1st time) to provide one
|
354 |
|
|
String p = Messages.getFormattedString("KeyPasswdCmd.24", alias); //$NON-NLS-1$
|
355 |
|
|
PasswordCallback pcb = new PasswordCallback(p, false);
|
356 |
|
|
prompts[0] = pcb;
|
357 |
|
|
handler.handle(prompts);
|
358 |
|
|
char[] pwd1 = pcb.getPassword();
|
359 |
|
|
pcb.clearPassword();
|
360 |
|
|
String ls = SystemProperties.getProperty("line.separator"); //$NON-NLS-1$
|
361 |
|
|
if (pwd1 == null || pwd1.length < 6)
|
362 |
|
|
{
|
363 |
|
|
String m = Messages.getString("StorePasswdCmd.21") + ls; //$NON-NLS-1$
|
364 |
|
|
errors[0] = new TextOutputCallback(TextOutputCallback.ERROR, m);
|
365 |
|
|
handler.handle(errors);
|
366 |
|
|
return false;
|
367 |
|
|
}
|
368 |
|
|
|
369 |
|
|
if (Arrays.equals(keyPasswordChars, pwd1))
|
370 |
|
|
{
|
371 |
|
|
String m = Messages.getString("StorePasswdCmd.22") + ls; //$NON-NLS-1$
|
372 |
|
|
errors[0] = new TextOutputCallback(TextOutputCallback.ERROR, m);
|
373 |
|
|
handler.handle(errors);
|
374 |
|
|
return false;
|
375 |
|
|
}
|
376 |
|
|
|
377 |
|
|
// prompt user (2nd time) for confirmation
|
378 |
|
|
p = Messages.getFormattedString("KeyPasswdCmd.28", alias); //$NON-NLS-1$
|
379 |
|
|
pcb = new PasswordCallback(p, false);
|
380 |
|
|
prompts[0] = pcb;
|
381 |
|
|
handler.handle(prompts);
|
382 |
|
|
char[] pwd2 = pcb.getPassword();
|
383 |
|
|
pcb.clearPassword();
|
384 |
|
|
if (! Arrays.equals(pwd1, pwd2))
|
385 |
|
|
{
|
386 |
|
|
String m = Messages.getString("StorePasswdCmd.24") + ls; //$NON-NLS-1$
|
387 |
|
|
errors[0] = new TextOutputCallback(TextOutputCallback.ERROR, m);
|
388 |
|
|
handler.handle(errors);
|
389 |
|
|
return false;
|
390 |
|
|
}
|
391 |
|
|
|
392 |
|
|
newPasswordChars = pwd2;
|
393 |
|
|
return true;
|
394 |
|
|
}
|
395 |
|
|
}
|