1 |
779 |
jeremybenn |
/* GenKeyCmd.java -- The genkey 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 |
|
|
import gnu.java.security.util.Util;
|
48 |
|
|
import gnu.java.security.x509.X500DistinguishedName;
|
49 |
|
|
|
50 |
|
|
import java.io.ByteArrayInputStream;
|
51 |
|
|
import java.io.IOException;
|
52 |
|
|
import java.security.InvalidKeyException;
|
53 |
|
|
import java.security.KeyPair;
|
54 |
|
|
import java.security.KeyStoreException;
|
55 |
|
|
import java.security.NoSuchAlgorithmException;
|
56 |
|
|
import java.security.PrivateKey;
|
57 |
|
|
import java.security.PublicKey;
|
58 |
|
|
import java.security.SignatureException;
|
59 |
|
|
import java.security.cert.Certificate;
|
60 |
|
|
import java.security.cert.CertificateException;
|
61 |
|
|
import java.security.cert.CertificateFactory;
|
62 |
|
|
import java.util.logging.Logger;
|
63 |
|
|
|
64 |
|
|
import javax.security.auth.callback.Callback;
|
65 |
|
|
import javax.security.auth.callback.TextInputCallback;
|
66 |
|
|
import javax.security.auth.callback.TextOutputCallback;
|
67 |
|
|
import javax.security.auth.callback.UnsupportedCallbackException;
|
68 |
|
|
|
69 |
|
|
/**
|
70 |
|
|
* The <b>-genkey</b> keytool command handler is used to generate a key pair (a
|
71 |
|
|
* public, and associated private keys). It then generates a self-signed X509 v1
|
72 |
|
|
* certificate (authenticating the public key) and stores this certificate and
|
73 |
|
|
* the private key in the key store associating both to a designated alias.
|
74 |
|
|
* <p>
|
75 |
|
|
* Possible options for this command are:
|
76 |
|
|
* <p>
|
77 |
|
|
* <dl>
|
78 |
|
|
* <dt>-alias ALIAS</dt>
|
79 |
|
|
* <dd>Every entry, be it a <i>Key Entry</i> or a <i>Trusted
|
80 |
|
|
* Certificate</i>, in a key store is uniquely identified by a user-defined
|
81 |
|
|
* <i>Alias</i> string. Use this option to specify the <i>Alias</i> to use
|
82 |
|
|
* when referring to an entry in the key store. Unless specified otherwise,
|
83 |
|
|
* a default value of <code>mykey</code> shall be used when this option is
|
84 |
|
|
* omitted from the command line.
|
85 |
|
|
* <p></dd>
|
86 |
|
|
*
|
87 |
|
|
* <dt>-keyalg ALGORITHM</dt>
|
88 |
|
|
* <dd>Use this option to specify the canonical name of the key-pair
|
89 |
|
|
* generation algorithm. The default value for this option is
|
90 |
|
|
* <code>DSS</code> (a synonym for the Digital Signature Algorithm also
|
91 |
|
|
* known as <code>DSA</code>).
|
92 |
|
|
* <p></dd>
|
93 |
|
|
*
|
94 |
|
|
* <dt>-keysize KEY_SIZE</dt>
|
95 |
|
|
* <dd>Use this option to specify the number of bits of the shared modulus
|
96 |
|
|
* (for both the public and private keys) to use when generating new keys.
|
97 |
|
|
* A default value of <code>1024</code> will be used if this option is
|
98 |
|
|
* omitted from the command line.
|
99 |
|
|
* <p></dd>
|
100 |
|
|
*
|
101 |
|
|
* <dt>-sigalg ALGORITHM</dt>
|
102 |
|
|
* <dd>The canonical name of the digital signature algorithm to use for
|
103 |
|
|
* signing certificates. If this option is omitted, a default value will be
|
104 |
|
|
* chosen based on the type of the key-pair; i.e. the algorithm that ends
|
105 |
|
|
* up being used by the <code>-keyalg</code> option. If the key-pair
|
106 |
|
|
* generation algorithm is <code>DSA</code>, the value for the signature
|
107 |
|
|
* algorithm will be <code>SHA1withDSA</code>. If on the other hand the
|
108 |
|
|
* key-pair generation algorithm is <code>RSA</code>, then the tool will
|
109 |
|
|
* use <code>MD5withRSA</code> as the signature algorithm.
|
110 |
|
|
* <p></dd>
|
111 |
|
|
*
|
112 |
|
|
* <dt>-dname NAME</dt>
|
113 |
|
|
* <dd>This a mandatory value for this command. If this option is omitted
|
114 |
|
|
* the tool will prompt you to enter a <i>Distinguished Name</i> to use as
|
115 |
|
|
* both the <i>Owner</i> and <i>Issuer</i> of the generated self-signed
|
116 |
|
|
* certificate.
|
117 |
|
|
* <p>
|
118 |
|
|
* The syntax of a valid value for this option MUST follow RFC-2253
|
119 |
|
|
* specifications. Namely the following components (with their accepted
|
120 |
|
|
* meaning) will be recognized. Note that the component name is case-
|
121 |
|
|
* insensitive:
|
122 |
|
|
* <dl>
|
123 |
|
|
* <dt>CN</dt>
|
124 |
|
|
* <dd>The Common Name; e.g. "host.domain.com"</dd>
|
125 |
|
|
*
|
126 |
|
|
* <dt>OU</dt>
|
127 |
|
|
* <dd>The Organizational Unit; e.g. "IT Department"</dd>
|
128 |
|
|
*
|
129 |
|
|
* <dt>O</dt>
|
130 |
|
|
* <dd>The Organization Name; e.g. "The Sample Company"</dd>
|
131 |
|
|
*
|
132 |
|
|
* <dt>L</dt>
|
133 |
|
|
* <dd>The Locality Name; e.g. "Sydney"</dd>
|
134 |
|
|
*
|
135 |
|
|
* <dt>ST</dt>
|
136 |
|
|
* <dd>The State Name; e.g. "New South Wales"</dd>
|
137 |
|
|
*
|
138 |
|
|
* <dt>C</dt>
|
139 |
|
|
* <dd>The 2-letter Country identifier; e.g. "AU"</dd>
|
140 |
|
|
* </dl>
|
141 |
|
|
* <p>
|
142 |
|
|
* When specified with a <code>-dname</code> option, each pair of component
|
143 |
|
|
* / value will be separated from the other with a comma. Each component
|
144 |
|
|
* and value pair MUST be separated by an equal sign. For example, the
|
145 |
|
|
* following is a valid DN value:
|
146 |
|
|
* <pre>
|
147 |
|
|
* CN=host.domain.com, O=The Sample Company, L=Sydney, ST=NSW, C=AU
|
148 |
|
|
* </pre>
|
149 |
|
|
* If this option is omitted, the tool will prompt you to enter the
|
150 |
|
|
* information through the console.
|
151 |
|
|
* <p></dd>
|
152 |
|
|
*
|
153 |
|
|
* <dt>-keypass PASSWORD</dt>
|
154 |
|
|
* <dd>Use this option to specify the password which the tool will use to
|
155 |
|
|
* protect the newly created Key Entry.
|
156 |
|
|
* <p>
|
157 |
|
|
* If this option is omitted, you will be prompted to provide a password.
|
158 |
|
|
* <p></dd>
|
159 |
|
|
*
|
160 |
|
|
* <dt>-validity DAY_COUNT</dt>
|
161 |
|
|
*
|
162 |
|
|
* <dt>-storetype STORE_TYPE</dt>
|
163 |
|
|
* <dd>Use this option to specify the type of the key store to use. The
|
164 |
|
|
* default value, if this option is omitted, is that of the property
|
165 |
|
|
* <code>keystore.type</code> in the security properties file, which is
|
166 |
|
|
* obtained by invoking the {@link java.security.KeyStore#getDefaultType()}
|
167 |
|
|
* static method.
|
168 |
|
|
* <p></dd>
|
169 |
|
|
*
|
170 |
|
|
* <dt>-keystore URL</dt>
|
171 |
|
|
* <dd>Use this option to specify the location of the key store to use.
|
172 |
|
|
* The default value is a file {@link java.net.URL} referencing the file
|
173 |
|
|
* named <code>.keystore</code> located in the path returned by the call to
|
174 |
|
|
* {@link java.lang.System#getProperty(String)} using <code>user.home</code>
|
175 |
|
|
* as argument.
|
176 |
|
|
* <p>
|
177 |
|
|
* If a URL was specified, but was found to be malformed --e.g. missing
|
178 |
|
|
* protocol element-- the tool will attempt to use the URL value as a file-
|
179 |
|
|
* name (with absolute or relative path-name) of a key store --as if the
|
180 |
|
|
* protocol was <code>file:</code>.
|
181 |
|
|
* <p></dd>
|
182 |
|
|
*
|
183 |
|
|
* <dt>-storepass PASSWORD</dt>
|
184 |
|
|
* <dd>Use this option to specify the password protecting the key store. If
|
185 |
|
|
* this option is omitted from the command line, you will be prompted to
|
186 |
|
|
* provide a password.
|
187 |
|
|
* <p></dd>
|
188 |
|
|
*
|
189 |
|
|
* <dt>-provider PROVIDER_CLASS_NAME</dt>
|
190 |
|
|
* <dd>A fully qualified class name of a Security Provider to add to the
|
191 |
|
|
* current list of Security Providers already installed in the JVM in-use.
|
192 |
|
|
* If a provider class is specified with this option, and was successfully
|
193 |
|
|
* added to the runtime --i.e. it was not already installed-- then the tool
|
194 |
|
|
* will attempt to removed this Security Provider before exiting.
|
195 |
|
|
* <p></dd>
|
196 |
|
|
*
|
197 |
|
|
* <dt>-v</dt>
|
198 |
|
|
* <dd>Use this option to enable more verbose output.</dd>
|
199 |
|
|
* </dl>
|
200 |
|
|
*/
|
201 |
|
|
class GenKeyCmd extends Command
|
202 |
|
|
{
|
203 |
|
|
private static final Logger log = Logger.getLogger(GenKeyCmd.class.getName());
|
204 |
|
|
/** Default key size in bits. */
|
205 |
|
|
private static final int DEFAULT_KEY_SIZE = 1024;
|
206 |
|
|
|
207 |
|
|
protected String _alias;
|
208 |
|
|
protected String _keyAlgorithm;
|
209 |
|
|
protected String _keySizeStr;
|
210 |
|
|
protected String _sigAlgorithm;
|
211 |
|
|
protected String _dName;
|
212 |
|
|
protected String _password;
|
213 |
|
|
protected String _validityStr;
|
214 |
|
|
protected String _ksType;
|
215 |
|
|
protected String _ksURL;
|
216 |
|
|
protected String _ksPassword;
|
217 |
|
|
protected String _providerClassName;
|
218 |
|
|
private int keySize;
|
219 |
|
|
private X500DistinguishedName distinguishedName;
|
220 |
|
|
|
221 |
|
|
// default 0-arguments constructor
|
222 |
|
|
|
223 |
|
|
// public setters -----------------------------------------------------------
|
224 |
|
|
|
225 |
|
|
/** @param alias the alias to use. */
|
226 |
|
|
public void setAlias(String alias)
|
227 |
|
|
{
|
228 |
|
|
this._alias = alias;
|
229 |
|
|
}
|
230 |
|
|
|
231 |
|
|
/** @param algorithm the canonical name of the key-pair algorithm to use. */
|
232 |
|
|
public void setKeyalg(String algorithm)
|
233 |
|
|
{
|
234 |
|
|
this._keyAlgorithm = algorithm;
|
235 |
|
|
}
|
236 |
|
|
|
237 |
|
|
/**
|
238 |
|
|
* @param bits the string representation of the number of bits (a decimal
|
239 |
|
|
* positive integer) the modulus of the generated keys (private and
|
240 |
|
|
* public) should have.
|
241 |
|
|
*/
|
242 |
|
|
public void setKeysize(String bits)
|
243 |
|
|
{
|
244 |
|
|
this._validityStr = bits;
|
245 |
|
|
}
|
246 |
|
|
|
247 |
|
|
/**
|
248 |
|
|
* @param algorithm the canonical name of the digital signature algorithm to
|
249 |
|
|
* use.
|
250 |
|
|
*/
|
251 |
|
|
public void setSigalg(String algorithm)
|
252 |
|
|
{
|
253 |
|
|
this._sigAlgorithm = algorithm;
|
254 |
|
|
}
|
255 |
|
|
|
256 |
|
|
/** @param name the distiniguished name to use. */
|
257 |
|
|
public void setDname(String name)
|
258 |
|
|
{
|
259 |
|
|
this._dName = name;
|
260 |
|
|
}
|
261 |
|
|
|
262 |
|
|
/** @param password the (private) key password to use. */
|
263 |
|
|
public void setKeypass(String password)
|
264 |
|
|
{
|
265 |
|
|
this._password = password;
|
266 |
|
|
}
|
267 |
|
|
|
268 |
|
|
/**
|
269 |
|
|
* @param days the string representation of the number of days (a decimal,
|
270 |
|
|
* positive integer) to assign to the generated certificate.
|
271 |
|
|
*/
|
272 |
|
|
public void setValidity(String days)
|
273 |
|
|
{
|
274 |
|
|
this._validityStr = days;
|
275 |
|
|
}
|
276 |
|
|
|
277 |
|
|
/** @param type the key-store type to use. */
|
278 |
|
|
public void setStoretype(String type)
|
279 |
|
|
{
|
280 |
|
|
this._ksType = type;
|
281 |
|
|
}
|
282 |
|
|
|
283 |
|
|
/** @param url the key-store URL to use. */
|
284 |
|
|
public void setKeystore(String url)
|
285 |
|
|
{
|
286 |
|
|
this._ksURL = url;
|
287 |
|
|
}
|
288 |
|
|
|
289 |
|
|
/** @param password the key-store password to use. */
|
290 |
|
|
public void setStorepass(String password)
|
291 |
|
|
{
|
292 |
|
|
this._ksPassword = password;
|
293 |
|
|
}
|
294 |
|
|
|
295 |
|
|
/** @param className a security provider fully qualified class name to use. */
|
296 |
|
|
public void setProvider(String className)
|
297 |
|
|
{
|
298 |
|
|
this._providerClassName = className;
|
299 |
|
|
}
|
300 |
|
|
|
301 |
|
|
// life-cycle methods -------------------------------------------------------
|
302 |
|
|
|
303 |
|
|
void setup() throws Exception
|
304 |
|
|
{
|
305 |
|
|
setKeyStoreParams(true, _providerClassName, _ksType, _ksPassword, _ksURL);
|
306 |
|
|
setAliasParam(_alias);
|
307 |
|
|
setKeyPasswordParam(_password);
|
308 |
|
|
setAlgorithmParams(_keyAlgorithm, _sigAlgorithm);
|
309 |
|
|
setKeySize(_keySizeStr);
|
310 |
|
|
setDName(_dName);
|
311 |
|
|
setValidityParam(_validityStr);
|
312 |
|
|
if (Configuration.DEBUG)
|
313 |
|
|
{
|
314 |
|
|
log.fine("-genkey handler will use the following options:"); //$NON-NLS-1$
|
315 |
|
|
log.fine(" -alias=" + alias); //$NON-NLS-1$
|
316 |
|
|
log.fine(" -keyalg=" + keyPairGenerator.getAlgorithm()); //$NON-NLS-1$
|
317 |
|
|
log.fine(" -keysize=" + keySize); //$NON-NLS-1$
|
318 |
|
|
log.fine(" -sigalg=" + signatureAlgorithm.getAlgorithm()); //$NON-NLS-1$
|
319 |
|
|
log.fine(" -dname=" + distinguishedName); //$NON-NLS-1$
|
320 |
|
|
log.fine(" -validity=" + validityInDays); //$NON-NLS-1$
|
321 |
|
|
log.fine(" -storetype=" + storeType); //$NON-NLS-1$
|
322 |
|
|
log.fine(" -keystore=" + storeURL); //$NON-NLS-1$
|
323 |
|
|
log.fine(" -provider=" + provider); //$NON-NLS-1$
|
324 |
|
|
log.fine(" -v=" + verbose); //$NON-NLS-1$
|
325 |
|
|
}
|
326 |
|
|
}
|
327 |
|
|
|
328 |
|
|
void start() throws CertificateException, KeyStoreException,
|
329 |
|
|
InvalidKeyException, SignatureException, IOException,
|
330 |
|
|
NoSuchAlgorithmException
|
331 |
|
|
{
|
332 |
|
|
if (Configuration.DEBUG)
|
333 |
|
|
{
|
334 |
|
|
log.entering(this.getClass().getName(), "start"); //$NON-NLS-1$
|
335 |
|
|
log.fine("About to generate key-pair..."); //$NON-NLS-1$
|
336 |
|
|
}
|
337 |
|
|
// 1. generate a new key-pair
|
338 |
|
|
keyPairGenerator.initialize(keySize);
|
339 |
|
|
KeyPair kp = keyPairGenerator.generateKeyPair();
|
340 |
|
|
PublicKey publicKey = kp.getPublic();
|
341 |
|
|
PrivateKey privateKey = kp.getPrivate();
|
342 |
|
|
|
343 |
|
|
// 2. generate a self-signed certificate
|
344 |
|
|
if (Configuration.DEBUG)
|
345 |
|
|
log.fine("About to generate a self-signed certificate..."); //$NON-NLS-1$
|
346 |
|
|
byte[] derBytes = getSelfSignedCertificate(distinguishedName,
|
347 |
|
|
publicKey,
|
348 |
|
|
privateKey);
|
349 |
|
|
if (Configuration.DEBUG)
|
350 |
|
|
log.fine(Util.dumpString(derBytes, "derBytes ")); //$NON-NLS-1$
|
351 |
|
|
CertificateFactory x509Factory = CertificateFactory.getInstance(Main.X_509);
|
352 |
|
|
ByteArrayInputStream bais = new ByteArrayInputStream(derBytes);
|
353 |
|
|
Certificate certificate = x509Factory.generateCertificate(bais);
|
354 |
|
|
if (Configuration.DEBUG)
|
355 |
|
|
log.fine("certificate = " + certificate); //$NON-NLS-1$
|
356 |
|
|
|
357 |
|
|
// 3. store it, w/ its private key, associating them to alias
|
358 |
|
|
Certificate[] chain = new Certificate[] { certificate };
|
359 |
|
|
if (Configuration.DEBUG)
|
360 |
|
|
log.fine("About to store newly generated material in key store..."); //$NON-NLS-1$
|
361 |
|
|
store.setKeyEntry(alias, privateKey, keyPasswordChars, chain);
|
362 |
|
|
|
363 |
|
|
// 4. persist the key store
|
364 |
|
|
saveKeyStore();
|
365 |
|
|
if (Configuration.DEBUG)
|
366 |
|
|
log.exiting(this.getClass().getName(), "start"); //$NON-NLS-1$
|
367 |
|
|
}
|
368 |
|
|
|
369 |
|
|
// own methods --------------------------------------------------------------
|
370 |
|
|
|
371 |
|
|
Parser getParser()
|
372 |
|
|
{
|
373 |
|
|
if (Configuration.DEBUG)
|
374 |
|
|
log.entering(this.getClass().getName(), "getParser"); //$NON-NLS-1$
|
375 |
|
|
Parser result = new ClasspathToolParser(Main.GENKEY_CMD, true);
|
376 |
|
|
result.setHeader(Messages.getString("GenKeyCmd.57")); //$NON-NLS-1$
|
377 |
|
|
result.setFooter(Messages.getString("GenKeyCmd.58")); //$NON-NLS-1$
|
378 |
|
|
OptionGroup options = new OptionGroup(Messages.getString("GenKeyCmd.59")); //$NON-NLS-1$
|
379 |
|
|
options.add(new Option(Main.ALIAS_OPT,
|
380 |
|
|
Messages.getString("GenKeyCmd.60"), //$NON-NLS-1$
|
381 |
|
|
Messages.getString("GenKeyCmd.61")) //$NON-NLS-1$
|
382 |
|
|
{
|
383 |
|
|
public void parsed(String argument) throws OptionException
|
384 |
|
|
{
|
385 |
|
|
_alias = argument;
|
386 |
|
|
}
|
387 |
|
|
});
|
388 |
|
|
options.add(new Option(Main.KEYALG_OPT,
|
389 |
|
|
Messages.getString("GenKeyCmd.62"), //$NON-NLS-1$
|
390 |
|
|
Messages.getString("GenKeyCmd.63")) //$NON-NLS-1$
|
391 |
|
|
{
|
392 |
|
|
public void parsed(String argument) throws OptionException
|
393 |
|
|
{
|
394 |
|
|
_keyAlgorithm = argument;
|
395 |
|
|
}
|
396 |
|
|
});
|
397 |
|
|
options.add(new Option(Main.KEYSIZE_OPT,
|
398 |
|
|
Messages.getString("GenKeyCmd.64"), //$NON-NLS-1$
|
399 |
|
|
Messages.getString("GenKeyCmd.65")) //$NON-NLS-1$
|
400 |
|
|
{
|
401 |
|
|
public void parsed(String argument) throws OptionException
|
402 |
|
|
{
|
403 |
|
|
_keySizeStr = argument;
|
404 |
|
|
}
|
405 |
|
|
});
|
406 |
|
|
options.add(new Option(Main.SIGALG_OPT,
|
407 |
|
|
Messages.getString("GenKeyCmd.66"), //$NON-NLS-1$
|
408 |
|
|
Messages.getString("GenKeyCmd.63")) //$NON-NLS-1$
|
409 |
|
|
{
|
410 |
|
|
public void parsed(String argument) throws OptionException
|
411 |
|
|
{
|
412 |
|
|
_sigAlgorithm = argument;
|
413 |
|
|
}
|
414 |
|
|
});
|
415 |
|
|
options.add(new Option(Main.DNAME_OPT,
|
416 |
|
|
Messages.getString("GenKeyCmd.68"), //$NON-NLS-1$
|
417 |
|
|
Messages.getString("GenKeyCmd.69")) //$NON-NLS-1$
|
418 |
|
|
{
|
419 |
|
|
public void parsed(String argument) throws OptionException
|
420 |
|
|
{
|
421 |
|
|
_dName = argument;
|
422 |
|
|
}
|
423 |
|
|
});
|
424 |
|
|
options.add(new Option(Main.KEYPASS_OPT,
|
425 |
|
|
Messages.getString("GenKeyCmd.70"), //$NON-NLS-1$
|
426 |
|
|
Messages.getString("GenKeyCmd.71")) //$NON-NLS-1$
|
427 |
|
|
{
|
428 |
|
|
public void parsed(String argument) throws OptionException
|
429 |
|
|
{
|
430 |
|
|
_password = argument;
|
431 |
|
|
}
|
432 |
|
|
});
|
433 |
|
|
options.add(new Option(Main.VALIDITY_OPT,
|
434 |
|
|
Messages.getString("GenKeyCmd.72"), //$NON-NLS-1$
|
435 |
|
|
Messages.getString("GenKeyCmd.73")) //$NON-NLS-1$
|
436 |
|
|
{
|
437 |
|
|
public void parsed(String argument) throws OptionException
|
438 |
|
|
{
|
439 |
|
|
_validityStr = argument;
|
440 |
|
|
}
|
441 |
|
|
});
|
442 |
|
|
options.add(new Option(Main.STORETYPE_OPT,
|
443 |
|
|
Messages.getString("GenKeyCmd.74"), //$NON-NLS-1$
|
444 |
|
|
Messages.getString("GenKeyCmd.75")) //$NON-NLS-1$
|
445 |
|
|
{
|
446 |
|
|
public void parsed(String argument) throws OptionException
|
447 |
|
|
{
|
448 |
|
|
_ksType = argument;
|
449 |
|
|
}
|
450 |
|
|
});
|
451 |
|
|
options.add(new Option(Main.KEYSTORE_OPT,
|
452 |
|
|
Messages.getString("GenKeyCmd.76"), //$NON-NLS-1$
|
453 |
|
|
Messages.getString("GenKeyCmd.77")) //$NON-NLS-1$
|
454 |
|
|
{
|
455 |
|
|
public void parsed(String argument) throws OptionException
|
456 |
|
|
{
|
457 |
|
|
_ksURL = argument;
|
458 |
|
|
}
|
459 |
|
|
});
|
460 |
|
|
options.add(new Option(Main.STOREPASS_OPT,
|
461 |
|
|
Messages.getString("GenKeyCmd.78"), //$NON-NLS-1$
|
462 |
|
|
Messages.getString("GenKeyCmd.71")) //$NON-NLS-1$
|
463 |
|
|
{
|
464 |
|
|
public void parsed(String argument) throws OptionException
|
465 |
|
|
{
|
466 |
|
|
_ksPassword = argument;
|
467 |
|
|
}
|
468 |
|
|
});
|
469 |
|
|
options.add(new Option(Main.PROVIDER_OPT,
|
470 |
|
|
Messages.getString("GenKeyCmd.80"), //$NON-NLS-1$
|
471 |
|
|
Messages.getString("GenKeyCmd.81")) //$NON-NLS-1$
|
472 |
|
|
{
|
473 |
|
|
public void parsed(String argument) throws OptionException
|
474 |
|
|
{
|
475 |
|
|
_providerClassName = argument;
|
476 |
|
|
}
|
477 |
|
|
});
|
478 |
|
|
options.add(new Option(Main.VERBOSE_OPT,
|
479 |
|
|
Messages.getString("GenKeyCmd.82")) //$NON-NLS-1$
|
480 |
|
|
{
|
481 |
|
|
public void parsed(String argument) throws OptionException
|
482 |
|
|
{
|
483 |
|
|
verbose = true;
|
484 |
|
|
}
|
485 |
|
|
});
|
486 |
|
|
result.add(options);
|
487 |
|
|
if (Configuration.DEBUG)
|
488 |
|
|
log.exiting(this.getClass().getName(), "getParser", result); //$NON-NLS-1$
|
489 |
|
|
return result;
|
490 |
|
|
}
|
491 |
|
|
|
492 |
|
|
/**
|
493 |
|
|
* @param size the desired key size as a string.
|
494 |
|
|
* @throws NumberFormatException if the string does not represent a valid
|
495 |
|
|
* decimal integer value.
|
496 |
|
|
*/
|
497 |
|
|
private void setKeySize(String size)
|
498 |
|
|
{
|
499 |
|
|
if (size == null || size.trim().length() == 0)
|
500 |
|
|
this.keySize = DEFAULT_KEY_SIZE;
|
501 |
|
|
else
|
502 |
|
|
{
|
503 |
|
|
size = size.trim();
|
504 |
|
|
keySize = Integer.parseInt(size);
|
505 |
|
|
// When generating a DSA key pair, the key size must be in the range
|
506 |
|
|
// from 512 to 1024 bits, and must be a multiple of 64. The default
|
507 |
|
|
// key size for any algorithm is 1024 bits
|
508 |
|
|
if (keySize < 1)
|
509 |
|
|
throw new IllegalArgumentException(Messages.getString("GenKeyCmd.54")); //$NON-NLS-1$
|
510 |
|
|
}
|
511 |
|
|
}
|
512 |
|
|
|
513 |
|
|
/**
|
514 |
|
|
* @param name the X.500 distinguished name of the principal for whom the
|
515 |
|
|
* key/certificate are being generated.
|
516 |
|
|
* @throws UnsupportedCallbackException if no implementation of a name
|
517 |
|
|
* callback is available.
|
518 |
|
|
* @throws IOException if an I/O related exception occurs during the process.
|
519 |
|
|
* @throws IllegalArgumentException if the designated, or captured, value is
|
520 |
|
|
* not a valid X.500 distinguished name.
|
521 |
|
|
*/
|
522 |
|
|
private void setDName(String name) throws IOException,
|
523 |
|
|
UnsupportedCallbackException
|
524 |
|
|
{
|
525 |
|
|
if (name != null && name.trim().length() > 0)
|
526 |
|
|
name = name.trim();
|
527 |
|
|
else
|
528 |
|
|
{
|
529 |
|
|
// prompt user to provide one
|
530 |
|
|
String dnTxt = Messages.getString("GenKeyCmd.0"); //$NON-NLS-1$
|
531 |
|
|
String oDefault = Messages.getString("GenKeyCmd.6"); //$NON-NLS-1$
|
532 |
|
|
String lDefault = Messages.getString("GenKeyCmd.7"); //$NON-NLS-1$
|
533 |
|
|
String stDefault = Messages.getString("GenKeyCmd.8"); //$NON-NLS-1$
|
534 |
|
|
String cDefault = Messages.getString("GenKeyCmd.9"); //$NON-NLS-1$
|
535 |
|
|
String cnPrompt = Messages.getString("GenKeyCmd.10"); //$NON-NLS-1$
|
536 |
|
|
String oPrompt = Messages.getFormattedString("GenKeyCmd.11", oDefault); //$NON-NLS-1$
|
537 |
|
|
String ouPrompt = Messages.getString("GenKeyCmd.13"); //$NON-NLS-1$
|
538 |
|
|
String lPrompt = Messages.getFormattedString("GenKeyCmd.14", lDefault); //$NON-NLS-1$
|
539 |
|
|
String stPrompt = Messages.getFormattedString("GenKeyCmd.16", stDefault); //$NON-NLS-1$
|
540 |
|
|
String cPrompt = Messages.getFormattedString("GenKeyCmd.18", cDefault); //$NON-NLS-1$
|
541 |
|
|
|
542 |
|
|
TextOutputCallback dnCB = new TextOutputCallback(TextOutputCallback.INFORMATION,
|
543 |
|
|
dnTxt);
|
544 |
|
|
TextInputCallback cnCB = new TextInputCallback(cnPrompt);
|
545 |
|
|
TextInputCallback oCB = new TextInputCallback(oPrompt, oDefault);
|
546 |
|
|
TextInputCallback ouCB = new TextInputCallback(ouPrompt);
|
547 |
|
|
TextInputCallback lCB = new TextInputCallback(lPrompt, lDefault);
|
548 |
|
|
TextInputCallback sCB = new TextInputCallback(stPrompt, stDefault);
|
549 |
|
|
TextInputCallback cCB = new TextInputCallback(cPrompt, cDefault);
|
550 |
|
|
getCallbackHandler().handle(new Callback[] { dnCB, cnCB, oCB, ouCB, lCB, sCB, cCB });
|
551 |
|
|
StringBuilder sb = new StringBuilder();
|
552 |
|
|
|
553 |
|
|
// handle CN
|
554 |
|
|
name = parseUserPrompt(cnCB);
|
555 |
|
|
if (name != null && name.length() > 0)
|
556 |
|
|
sb.append("CN=").append(name); //$NON-NLS-1$
|
557 |
|
|
|
558 |
|
|
// handle O
|
559 |
|
|
name = parseUserPrompt(oCB);
|
560 |
|
|
if (name != null && name.length() > 0)
|
561 |
|
|
sb.append(",O=").append(name); //$NON-NLS-1$
|
562 |
|
|
|
563 |
|
|
// handle OU
|
564 |
|
|
name = parseUserPrompt(ouCB);
|
565 |
|
|
if (name != null && name.length() > 0)
|
566 |
|
|
sb.append(",OU=").append(name.trim()); //$NON-NLS-1$
|
567 |
|
|
|
568 |
|
|
// handle L
|
569 |
|
|
name = parseUserPrompt(lCB);
|
570 |
|
|
if (name != null && name.length() > 0)
|
571 |
|
|
sb.append(",L=").append(name.trim()); //$NON-NLS-1$
|
572 |
|
|
|
573 |
|
|
// handle ST
|
574 |
|
|
name = parseUserPrompt(sCB);
|
575 |
|
|
if (name != null && name.length() > 0)
|
576 |
|
|
sb.append(",ST=").append(name.trim()); //$NON-NLS-1$
|
577 |
|
|
|
578 |
|
|
// handle C
|
579 |
|
|
name = parseUserPrompt(cCB);
|
580 |
|
|
if (name != null && name.length() > 0)
|
581 |
|
|
sb.append(",C=").append(name.trim()); //$NON-NLS-1$
|
582 |
|
|
|
583 |
|
|
name = sb.toString().trim();
|
584 |
|
|
}
|
585 |
|
|
if (Configuration.DEBUG)
|
586 |
|
|
log.fine("dName=[" + name + "]"); //$NON-NLS-1$ //$NON-NLS-2$
|
587 |
|
|
distinguishedName = new X500DistinguishedName(name);
|
588 |
|
|
}
|
589 |
|
|
|
590 |
|
|
private String parseUserPrompt(TextInputCallback ticb)
|
591 |
|
|
{
|
592 |
|
|
String result = ticb.getText();
|
593 |
|
|
if (result == null || result.trim().length() == 0)
|
594 |
|
|
result = ticb.getDefaultText();
|
595 |
|
|
else if (result.trim().equals(".")) //$NON-NLS-1$
|
596 |
|
|
result = null;
|
597 |
|
|
else
|
598 |
|
|
result = result.trim();
|
599 |
|
|
|
600 |
|
|
return result;
|
601 |
|
|
}
|
602 |
|
|
}
|