1 |
779 |
jeremybenn |
/* StorePasswdCmd.java -- The storepasswd 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.KeyStoreException;
|
51 |
|
|
import java.security.NoSuchAlgorithmException;
|
52 |
|
|
import java.security.cert.CertificateException;
|
53 |
|
|
import java.util.Arrays;
|
54 |
|
|
import java.util.logging.Logger;
|
55 |
|
|
|
56 |
|
|
import javax.security.auth.callback.Callback;
|
57 |
|
|
import javax.security.auth.callback.CallbackHandler;
|
58 |
|
|
import javax.security.auth.callback.PasswordCallback;
|
59 |
|
|
import javax.security.auth.callback.TextOutputCallback;
|
60 |
|
|
import javax.security.auth.callback.UnsupportedCallbackException;
|
61 |
|
|
|
62 |
|
|
/**
|
63 |
|
|
* The <b>-storepasswd</b> keytool command handler is used to change the
|
64 |
|
|
* password which protects the integrity of the key store.
|
65 |
|
|
* <p>
|
66 |
|
|
* Possible options for this command are:
|
67 |
|
|
* <p>
|
68 |
|
|
* <dl>
|
69 |
|
|
* <dt>-new PASSWORD</dt>
|
70 |
|
|
* <dd>The new, and different, password which will be used to protect the
|
71 |
|
|
* designated key store.
|
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 StorePasswdCmd extends Command
|
114 |
|
|
{
|
115 |
|
|
private static final Logger log = Logger.getLogger(StorePasswdCmd.class.getName());
|
116 |
|
|
protected String _newPassword;
|
117 |
|
|
protected String _ksType;
|
118 |
|
|
protected String _ksURL;
|
119 |
|
|
protected String _ksPassword;
|
120 |
|
|
protected String _providerClassName;
|
121 |
|
|
private char[] newStorePasswordChars;
|
122 |
|
|
|
123 |
|
|
// default 0-arguments constructor
|
124 |
|
|
|
125 |
|
|
// public setters -----------------------------------------------------------
|
126 |
|
|
|
127 |
|
|
/** @param password the new key-store password to use. */
|
128 |
|
|
public void setNew(String password)
|
129 |
|
|
{
|
130 |
|
|
this._newPassword = password;
|
131 |
|
|
}
|
132 |
|
|
|
133 |
|
|
/** @param type the key-store type to use. */
|
134 |
|
|
public void setStoretype(String type)
|
135 |
|
|
{
|
136 |
|
|
this._ksType = type;
|
137 |
|
|
}
|
138 |
|
|
|
139 |
|
|
/** @param url the key-store URL to use. */
|
140 |
|
|
public void setKeystore(String url)
|
141 |
|
|
{
|
142 |
|
|
this._ksURL = url;
|
143 |
|
|
}
|
144 |
|
|
|
145 |
|
|
/** @param password the key-store password to use. */
|
146 |
|
|
public void setStorepass(String password)
|
147 |
|
|
{
|
148 |
|
|
this._ksPassword = password;
|
149 |
|
|
}
|
150 |
|
|
|
151 |
|
|
/** @param className a security provider fully qualified class name to use. */
|
152 |
|
|
public void setProvider(String className)
|
153 |
|
|
{
|
154 |
|
|
this._providerClassName = className;
|
155 |
|
|
}
|
156 |
|
|
|
157 |
|
|
// life-cycle methods -------------------------------------------------------
|
158 |
|
|
|
159 |
|
|
void setup() throws Exception
|
160 |
|
|
{
|
161 |
|
|
setKeyStoreParams(_providerClassName, _ksType, _ksPassword, _ksURL);
|
162 |
|
|
setNewKeystorePassword(_newPassword);
|
163 |
|
|
if (Configuration.DEBUG)
|
164 |
|
|
{
|
165 |
|
|
log.fine("-storepasswd handler will use the following options:"); //$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 |
|
|
saveKeyStore(newStorePasswordChars);
|
179 |
|
|
if (Configuration.DEBUG)
|
180 |
|
|
log.exiting(getClass().getName(), "start"); //$NON-NLS-1$
|
181 |
|
|
}
|
182 |
|
|
|
183 |
|
|
// own methods --------------------------------------------------------------
|
184 |
|
|
|
185 |
|
|
Parser getParser()
|
186 |
|
|
{
|
187 |
|
|
if (Configuration.DEBUG)
|
188 |
|
|
log.entering(this.getClass().getName(), "getParser"); //$NON-NLS-1$
|
189 |
|
|
Parser result = new ClasspathToolParser(Main.STOREPASSWD_CMD, true);
|
190 |
|
|
result.setHeader(Messages.getString("StorePasswdCmd.18")); //$NON-NLS-1$
|
191 |
|
|
result.setFooter(Messages.getString("StorePasswdCmd.17")); //$NON-NLS-1$
|
192 |
|
|
OptionGroup options = new OptionGroup(Messages.getString("StorePasswdCmd.16")); //$NON-NLS-1$
|
193 |
|
|
options.add(new Option(Main.NEW_OPT,
|
194 |
|
|
Messages.getString("StorePasswdCmd.15"), //$NON-NLS-1$
|
195 |
|
|
Messages.getString("StorePasswdCmd.8")) //$NON-NLS-1$
|
196 |
|
|
{
|
197 |
|
|
public void parsed(String argument) throws OptionException
|
198 |
|
|
{
|
199 |
|
|
_newPassword = argument;
|
200 |
|
|
}
|
201 |
|
|
});
|
202 |
|
|
options.add(new Option(Main.STORETYPE_OPT,
|
203 |
|
|
Messages.getString("StorePasswdCmd.13"), //$NON-NLS-1$
|
204 |
|
|
Messages.getString("StorePasswdCmd.12")) //$NON-NLS-1$
|
205 |
|
|
{
|
206 |
|
|
public void parsed(String argument) throws OptionException
|
207 |
|
|
{
|
208 |
|
|
_ksType = argument;
|
209 |
|
|
}
|
210 |
|
|
});
|
211 |
|
|
options.add(new Option(Main.KEYSTORE_OPT,
|
212 |
|
|
Messages.getString("StorePasswdCmd.11"), //$NON-NLS-1$
|
213 |
|
|
Messages.getString("StorePasswdCmd.10")) //$NON-NLS-1$
|
214 |
|
|
{
|
215 |
|
|
public void parsed(String argument) throws OptionException
|
216 |
|
|
{
|
217 |
|
|
_ksURL = argument;
|
218 |
|
|
}
|
219 |
|
|
});
|
220 |
|
|
options.add(new Option(Main.STOREPASS_OPT,
|
221 |
|
|
Messages.getString("StorePasswdCmd.9"), //$NON-NLS-1$
|
222 |
|
|
Messages.getString("StorePasswdCmd.8")) //$NON-NLS-1$
|
223 |
|
|
{
|
224 |
|
|
public void parsed(String argument) throws OptionException
|
225 |
|
|
{
|
226 |
|
|
_ksPassword = argument;
|
227 |
|
|
}
|
228 |
|
|
});
|
229 |
|
|
options.add(new Option(Main.PROVIDER_OPT,
|
230 |
|
|
Messages.getString("StorePasswdCmd.7"), //$NON-NLS-1$
|
231 |
|
|
Messages.getString("StorePasswdCmd.6")) //$NON-NLS-1$
|
232 |
|
|
{
|
233 |
|
|
public void parsed(String argument) throws OptionException
|
234 |
|
|
{
|
235 |
|
|
_providerClassName = argument;
|
236 |
|
|
}
|
237 |
|
|
});
|
238 |
|
|
options.add(new Option(Main.VERBOSE_OPT,
|
239 |
|
|
Messages.getString("StorePasswdCmd.5")) //$NON-NLS-1$
|
240 |
|
|
{
|
241 |
|
|
public void parsed(String argument) throws OptionException
|
242 |
|
|
{
|
243 |
|
|
verbose = true;
|
244 |
|
|
}
|
245 |
|
|
});
|
246 |
|
|
result.add(options);
|
247 |
|
|
if (Configuration.DEBUG)
|
248 |
|
|
log.exiting(this.getClass().getName(), "getParser", result); //$NON-NLS-1$
|
249 |
|
|
return result;
|
250 |
|
|
}
|
251 |
|
|
|
252 |
|
|
protected void setNewKeystorePassword(String password) throws IOException,
|
253 |
|
|
UnsupportedCallbackException
|
254 |
|
|
{
|
255 |
|
|
if (password != null)
|
256 |
|
|
newStorePasswordChars = password.toCharArray();
|
257 |
|
|
else
|
258 |
|
|
{
|
259 |
|
|
boolean ok = false;
|
260 |
|
|
Callback[] prompts = new Callback[1];
|
261 |
|
|
Callback[] errors = new Callback[1];
|
262 |
|
|
for (int i = 0; i < 3; i++)
|
263 |
|
|
if (prompt4NewPassword(getCallbackHandler(), prompts, errors))
|
264 |
|
|
{
|
265 |
|
|
ok = true;
|
266 |
|
|
break;
|
267 |
|
|
}
|
268 |
|
|
if (! ok)
|
269 |
|
|
throw new SecurityException(Messages.getString("StorePasswdCmd.19")); //$NON-NLS-1$
|
270 |
|
|
}
|
271 |
|
|
}
|
272 |
|
|
|
273 |
|
|
private boolean prompt4NewPassword(CallbackHandler handler,
|
274 |
|
|
Callback[] prompts, Callback[] errors)
|
275 |
|
|
throws IOException, UnsupportedCallbackException
|
276 |
|
|
{
|
277 |
|
|
// prompt user (1st time) to provide one
|
278 |
|
|
String p = Messages.getString("StorePasswdCmd.20"); //$NON-NLS-1$
|
279 |
|
|
PasswordCallback pcb = new PasswordCallback(p, false);
|
280 |
|
|
prompts[0] = pcb;
|
281 |
|
|
handler.handle(prompts);
|
282 |
|
|
char[] pwd1 = pcb.getPassword();
|
283 |
|
|
pcb.clearPassword();
|
284 |
|
|
String ls = SystemProperties.getProperty("line.separator"); //$NON-NLS-1$
|
285 |
|
|
if (pwd1 == null || pwd1.length < 6)
|
286 |
|
|
{
|
287 |
|
|
String m = Messages.getString("StorePasswdCmd.21") + ls; //$NON-NLS-1$
|
288 |
|
|
errors[0] = new TextOutputCallback(TextOutputCallback.ERROR, m);
|
289 |
|
|
handler.handle(errors);
|
290 |
|
|
return false;
|
291 |
|
|
}
|
292 |
|
|
|
293 |
|
|
if (Arrays.equals(storePasswordChars, pwd1))
|
294 |
|
|
{
|
295 |
|
|
String m = Messages.getString("StorePasswdCmd.22") + ls; //$NON-NLS-1$
|
296 |
|
|
errors[0] = new TextOutputCallback(TextOutputCallback.ERROR, m);
|
297 |
|
|
handler.handle(errors);
|
298 |
|
|
return false;
|
299 |
|
|
}
|
300 |
|
|
|
301 |
|
|
// prompt user (2nd time) for confirmation
|
302 |
|
|
pcb = new PasswordCallback(Messages.getString("StorePasswdCmd.23"), false); //$NON-NLS-1$
|
303 |
|
|
prompts[0] = pcb;
|
304 |
|
|
handler.handle(prompts);
|
305 |
|
|
char[] pwd2 = pcb.getPassword();
|
306 |
|
|
pcb.clearPassword();
|
307 |
|
|
if (! Arrays.equals(pwd1, pwd2))
|
308 |
|
|
{
|
309 |
|
|
String m = Messages.getString("StorePasswdCmd.24") + ls; //$NON-NLS-1$
|
310 |
|
|
errors[0] = new TextOutputCallback(TextOutputCallback.ERROR, m);
|
311 |
|
|
handler.handle(errors);
|
312 |
|
|
return false;
|
313 |
|
|
}
|
314 |
|
|
|
315 |
|
|
newStorePasswordChars = pwd2;
|
316 |
|
|
return true;
|
317 |
|
|
}
|
318 |
|
|
}
|