OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libjava/] [classpath/] [tools/] [gnu/] [classpath/] [tools/] [keytool/] [KeyCloneCmd.java] - Blame information for rev 779

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.