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/] [SelfCertCmd.java] - Blame information for rev 779

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 779 jeremybenn
/* SelfCertCmd.java -- The selfcert 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.x509.X500DistinguishedName;
48
 
49
import java.io.ByteArrayInputStream;
50
import java.io.IOException;
51
import java.security.InvalidKeyException;
52
import java.security.Key;
53
import java.security.KeyStoreException;
54
import java.security.NoSuchAlgorithmException;
55
import java.security.PrivateKey;
56
import java.security.PublicKey;
57
import java.security.SignatureException;
58
import java.security.UnrecoverableKeyException;
59
import java.security.cert.Certificate;
60
import java.security.cert.CertificateException;
61
import java.security.cert.CertificateFactory;
62
import java.security.cert.X509Certificate;
63
import java.util.logging.Logger;
64
 
65
import javax.security.auth.callback.UnsupportedCallbackException;
66
import javax.security.auth.x500.X500Principal;
67
 
68
/**
69
 * The <b>-selfcert</b> keytool command handler is used to generate a self-
70
 * signed X.509 version 1 certificate using key store credentials stored under a
71
 * designated alias.
72
 * <p>
73
 * Possible options for this command are:
74
 * <p>
75
 * <dl>
76
 *      <dt>-alias ALIAS</dt>
77
 *      <dd>Every entry, be it a <i>Key Entry</i> or a <i>Trusted
78
 *      Certificate</i>, in a key store is uniquely identified by a user-defined
79
 *      <i>Alias</i> string. Use this option to specify the <i>Alias</i> to use
80
 *      when referring to an entry in the key store. Unless specified otherwise,
81
 *      a default value of <code>mykey</code> shall be used when this option is
82
 *      omitted from the command line.
83
 *      <p></dd>
84
 *
85
 *      <dt>-sigalg ALGORITHM</dt>
86
 *      <dd>The canonical name of the digital signature algorithm to use for
87
 *      signing the certificate. If this option is omitted, a default value will
88
 *      be chosen based on the type of the private key associated with the
89
 *      designated <i>Alias</i>. If the private key is a <code>DSA</code> one,
90
 *      the value for the signature algorithm will be <code>SHA1withDSA</code>.
91
 *      If on the other hand the private key is an <code>RSA</code> one, then
92
 *      the tool will use <code>MD5withRSA</code> as the signature algorithm.
93
 *      <p></dd>
94
 *
95
 *      <dt>-dname NAME</dt>
96
 *      <dd>Use this option to specify the <i>Distinguished Name</i> of the
97
 *      newly generated self-signed certificate. If this option is omitted, the
98
 *      existing <i>Distinguished Name</i> of the base certificate in the chain
99
 *      associated with the designated <i>Alias</i> will be used instead.
100
 *      <p>
101
 *      The syntax of a valid value for this option MUST follow RFC-2253
102
 *      specifications. Namely the following components (with their accepted
103
 *      meaning) will be recognized. Note that the component name is case-
104
 *      insensitive:
105
 *      <dl>
106
 *              <dt>CN</dt>
107
 *              <dd>The Common Name; e.g. "host.domain.com"</dd>
108
 *
109
 *              <dt>OU</dt>
110
 *              <dd>The Organizational Unit; e.g. "IT Department"</dd>
111
 *
112
 *              <dt>O</dt>
113
 *              <dd>The Organization Name; e.g. "The Sample Company"</dd>
114
 *
115
 *              <dt>L</dt>
116
 *              <dd>The Locality Name; e.g. "Sydney"</dd>
117
 *
118
 *              <dt>ST</dt>
119
 *              <dd>The State Name; e.g. "New South Wales"</dd>
120
 *
121
 *              <dt>C</dt>
122
 *              <dd>The 2-letter Country identifier; e.g. "AU"</dd>
123
 *      </dl>
124
 *      <p>
125
 *      When specified with a <code>-dname</code> option, each pair of component
126
 *      / value will be separated from the other with a comma. Each component
127
 *      and value pair MUST be separated by an equal sign. For example, the
128
 *      following is a valid DN value:
129
 *      <pre>
130
 *        CN=host.domain.com, O=The Sample Company, L=Sydney, ST=NSW, C=AU
131
 *      </pre>
132
 *      <p></dd>
133
 *
134
 *      <dt>-validity DAY_COUNT</dt>
135
 *
136
 *      <dt>-keypass PASSWORD</dt>
137
 *
138
 *      <dt>-storetype STORE_TYPE</dt>
139
 *      <dd>Use this option to specify the type of the key store to use. The
140
 *      default value, if this option is omitted, is that of the property
141
 *      <code>keystore.type</code> in the security properties file, which is
142
 *      obtained by invoking the {@link java.security.KeyStore#getDefaultType()}
143
 *      static method.
144
 *      <p></dd>
145
 *
146
 *      <dt>-keystore URL</dt>
147
 *      <dd>Use this option to specify the location of the key store to use.
148
 *      The default value is a file {@link java.net.URL} referencing the file
149
 *      named <code>.keystore</code> located in the path returned by the call to
150
 *      {@link java.lang.System#getProperty(String)} using <code>user.home</code>
151
 *      as argument.
152
 *      <p>
153
 *      If a URL was specified, but was found to be malformed --e.g. missing
154
 *      protocol element-- the tool will attempt to use the URL value as a file-
155
 *      name (with absolute or relative path-name) of a key store --as if the
156
 *      protocol was <code>file:</code>.
157
 *      <p></dd>
158
 *
159
 *      <dt>-storepass PASSWORD</dt>
160
 *      <dd>Use this option to specify the password protecting the key store. If
161
 *      this option is omitted from the command line, you will be prompted to
162
 *      provide a password.
163
 *      <p></dd>
164
 *
165
 *      <dt>-provider PROVIDER_CLASS_NAME</dt>
166
 *      <dd>A fully qualified class name of a Security Provider to add to the
167
 *      current list of Security Providers already installed in the JVM in-use.
168
 *      If a provider class is specified with this option, and was successfully
169
 *      added to the runtime --i.e. it was not already installed-- then the tool
170
 *      will attempt to removed this Security Provider before exiting.
171
 *      <p></dd>
172
 *
173
 *      <dt>-v</dt>
174
 *      <dd>Use this option to enable more verbose output.</dd>
175
 * </dl>
176
 */
177
class SelfCertCmd extends Command
178
{
179
  private static final Logger log = Logger.getLogger(SelfCertCmd.class.getName());
180
  protected String _alias;
181
  protected String _sigAlgorithm;
182
  protected String _dName;
183
  protected String _password;
184
  protected String _validityStr;
185
  protected String _ksType;
186
  protected String _ksURL;
187
  protected String _ksPassword;
188
  protected String _providerClassName;
189
  private X500DistinguishedName distinguishedName;
190
  private int validityInDays;
191
 
192
  // default 0-arguments constructor
193
 
194
  // public setters -----------------------------------------------------------
195
 
196
  /** @param alias the alias to use. */
197
  public void setAlias(String alias)
198
  {
199
    this._alias = alias;
200
  }
201
 
202
  /**
203
   * @param algorithm the canonical name of the digital signature algorithm to
204
   *          use.
205
   */
206
  public void setSigalg(String algorithm)
207
  {
208
    this._sigAlgorithm = algorithm;
209
  }
210
 
211
  /**
212
   * @param name the distiniguished name of both the issuer and subject (since
213
   *          we are dealing with a self-signed certificate) to use.
214
   */
215
  public void setDname(String name)
216
  {
217
    this._dName = name;
218
  }
219
 
220
  /**
221
   * @param days the string representation of the number of days (a decimal,
222
   *          positive integer) to assign to the generated (self-signed)
223
   *          certificate.
224
   */
225
  public void setValidity(String days)
226
  {
227
    this._validityStr = days;
228
  }
229
 
230
  /** @param password the (private) key password to use. */
231
  public void setKeypass(String password)
232
  {
233
    this._password = password;
234
  }
235
 
236
  /** @param type the key-store type to use. */
237
  public void setStoretype(String type)
238
  {
239
    this._ksType = type;
240
  }
241
 
242
  /** @param url the key-store URL to use. */
243
  public void setKeystore(String url)
244
  {
245
    this._ksURL = url;
246
  }
247
 
248
  /** @param password the key-store password to use. */
249
  public void setStorepass(String password)
250
  {
251
    this._ksPassword = password;
252
  }
253
 
254
  /** @param className a security provider fully qualified class name to use. */
255
  public void setProvider(String className)
256
  {
257
    this._providerClassName = className;
258
  }
259
 
260
  // life-cycle methods -------------------------------------------------------
261
 
262
  void setup() throws Exception
263
  {
264
    setKeyStoreParams(_providerClassName, _ksType, _ksPassword, _ksURL);
265
    setAliasParam(_alias);
266
    setKeyPasswordNoPrompt(_password);
267
    setValidityParam(_validityStr);
268
    if (Configuration.DEBUG)
269
      {
270
        log.fine("-selfcert handler will use the following options:"); //$NON-NLS-1$
271
        log.fine("  -alias=" + alias); //$NON-NLS-1$
272
        log.fine("  -sigalg=" + _sigAlgorithm); //$NON-NLS-1$
273
        log.fine("  -dname=" + _dName); //$NON-NLS-1$
274
        log.fine("  -validity=" + validityInDays); //$NON-NLS-1$
275
        log.fine("  -storetype=" + storeType); //$NON-NLS-1$
276
        log.fine("  -keystore=" + storeURL); //$NON-NLS-1$
277
        log.fine("  -provider=" + provider); //$NON-NLS-1$
278
        log.fine("  -v=" + verbose); //$NON-NLS-1$
279
      }
280
  }
281
 
282
  void start() throws KeyStoreException, NoSuchAlgorithmException,
283
      UnrecoverableKeyException, IOException, UnsupportedCallbackException,
284
      InvalidKeyException, SignatureException, CertificateException
285
  {
286
    if (Configuration.DEBUG)
287
      log.entering(getClass().getName(), "start"); //$NON-NLS-1$
288
    // 1. get the key entry and certificate chain associated to alias
289
    Key privateKey = getAliasPrivateKey();
290
    Certificate[] chain = store.getCertificateChain(alias);
291
 
292
    // 2. if the user has not supplied a DN use one from the certificate chain
293
    X509Certificate bottomCertificate = (X509Certificate) chain[0];
294
    X500Principal defaultPrincipal = bottomCertificate.getIssuerX500Principal();
295
    setDName(_dName, defaultPrincipal);
296
 
297
    // 4. get alias's public key from certificate's SubjectPublicKeyInfo
298
    PublicKey publicKey = bottomCertificate.getPublicKey();
299
 
300
    // 5. issue the self-signed certificate
301
    setSignatureAlgorithmParam(_sigAlgorithm, privateKey);
302
 
303
    byte[] derBytes = getSelfSignedCertificate(distinguishedName,
304
                                               publicKey,
305
                                               (PrivateKey) privateKey);
306
    CertificateFactory x509Factory = CertificateFactory.getInstance("X.509"); //$NON-NLS-1$
307
    ByteArrayInputStream bais = new ByteArrayInputStream(derBytes);
308
    Certificate certificate = x509Factory.generateCertificate(bais);
309
 
310
    // 6. store it, w/ its private key, associating them to alias
311
    chain = new Certificate[] { certificate };
312
    store.setKeyEntry(alias, privateKey, keyPasswordChars, chain);
313
 
314
    // 7. persist the key store
315
    saveKeyStore();
316
    if (Configuration.DEBUG)
317
      log.exiting(getClass().getName(), "start"); //$NON-NLS-1$
318
  }
319
 
320
  // own methods --------------------------------------------------------------
321
 
322
  Parser getParser()
323
  {
324
    if (Configuration.DEBUG)
325
      log.entering(this.getClass().getName(), "getParser"); //$NON-NLS-1$
326
    Parser result = new ClasspathToolParser(Main.SELFCERT_CMD, true);
327
    result.setHeader(Messages.getString("SelfCertCmd.14")); //$NON-NLS-1$
328
    result.setFooter(Messages.getString("SelfCertCmd.15")); //$NON-NLS-1$
329
    OptionGroup options = new OptionGroup(Messages.getString("SelfCertCmd.16")); //$NON-NLS-1$
330
    options.add(new Option(Main.ALIAS_OPT,
331
                           Messages.getString("SelfCertCmd.17"), //$NON-NLS-1$
332
                           Messages.getString("SelfCertCmd.18")) //$NON-NLS-1$
333
    {
334
      public void parsed(String argument) throws OptionException
335
      {
336
        _alias = argument;
337
      }
338
    });
339
    options.add(new Option(Main.SIGALG_OPT,
340
                           Messages.getString("SelfCertCmd.19"), //$NON-NLS-1$
341
                           Messages.getString("SelfCertCmd.20")) //$NON-NLS-1$
342
    {
343
      public void parsed(String argument) throws OptionException
344
      {
345
        _sigAlgorithm = argument;
346
      }
347
    });
348
    options.add(new Option(Main.DNAME_OPT,
349
                           Messages.getString("SelfCertCmd.21"), //$NON-NLS-1$
350
                           Messages.getString("SelfCertCmd.22")) //$NON-NLS-1$
351
    {
352
      public void parsed(String argument) throws OptionException
353
      {
354
        _dName = argument;
355
      }
356
    });
357
    options.add(new Option(Main.KEYPASS_OPT,
358
                           Messages.getString("SelfCertCmd.23"), //$NON-NLS-1$
359
                           Messages.getString("SelfCertCmd.24")) //$NON-NLS-1$
360
    {
361
      public void parsed(String argument) throws OptionException
362
      {
363
        _password = argument;
364
      }
365
    });
366
    options.add(new Option(Main.VALIDITY_OPT,
367
                           Messages.getString("SelfCertCmd.25"), //$NON-NLS-1$
368
                           Messages.getString("SelfCertCmd.26")) //$NON-NLS-1$
369
    {
370
      public void parsed(String argument) throws OptionException
371
      {
372
        _validityStr = argument;
373
      }
374
    });
375
    options.add(new Option(Main.STORETYPE_OPT,
376
                           Messages.getString("SelfCertCmd.27"), //$NON-NLS-1$
377
                           Messages.getString("SelfCertCmd.28")) //$NON-NLS-1$
378
    {
379
      public void parsed(String argument) throws OptionException
380
      {
381
        _ksType = argument;
382
      }
383
    });
384
    options.add(new Option(Main.KEYSTORE_OPT,
385
                           Messages.getString("SelfCertCmd.29"), //$NON-NLS-1$
386
                           Messages.getString("SelfCertCmd.30")) //$NON-NLS-1$
387
    {
388
      public void parsed(String argument) throws OptionException
389
      {
390
        _ksURL = argument;
391
      }
392
    });
393
    options.add(new Option(Main.STOREPASS_OPT,
394
                           Messages.getString("SelfCertCmd.31"), //$NON-NLS-1$
395
                           Messages.getString("SelfCertCmd.32")) //$NON-NLS-1$
396
    {
397
      public void parsed(String argument) throws OptionException
398
      {
399
        _ksPassword = argument;
400
      }
401
    });
402
    options.add(new Option(Main.PROVIDER_OPT,
403
                           Messages.getString("SelfCertCmd.33"), //$NON-NLS-1$
404
                           Messages.getString("SelfCertCmd.34")) //$NON-NLS-1$
405
    {
406
      public void parsed(String argument) throws OptionException
407
      {
408
        _providerClassName = argument;
409
      }
410
    });
411
    options.add(new Option(Main.VERBOSE_OPT,
412
                           Messages.getString("SelfCertCmd.35")) //$NON-NLS-1$
413
    {
414
      public void parsed(String argument) throws OptionException
415
      {
416
        verbose = true;
417
      }
418
    });
419
    result.add(options);
420
    if (Configuration.DEBUG)
421
      log.exiting(this.getClass().getName(), "getParser", result); //$NON-NLS-1$
422
    return result;
423
  }
424
 
425
  private void setDName(String name, X500Principal defaultName)
426
  {
427
    if (name != null && name.trim().length() > 0)
428
      name = name.trim();
429
    else
430
      {
431
        // If dname is supplied at the command line, it is used as the X.500
432
        // Distinguished Name for both the issuer and subject of the certificate.
433
        // Otherwise, the X.500 Distinguished Name associated with alias (at the
434
        // bottom of its existing certificate chain) is used.
435
        name = defaultName.toString().trim();
436
      }
437
 
438
    distinguishedName = new X500DistinguishedName(name);
439
  }
440
}

powered by: WebSVN 2.1.0

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