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

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 779 jeremybenn
/* CACertCmd.java -- GNU specific cacert handler
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.File;
49
import java.io.IOException;
50
import java.security.KeyStoreException;
51
import java.security.NoSuchAlgorithmException;
52
import java.security.cert.Certificate;
53
import java.security.cert.CertificateException;
54
import java.security.cert.CertificateFactory;
55
import java.util.logging.Logger;
56
 
57
/**
58
 * The <code>-cacert</code> keytol command handler is used to import a CA
59
 * trusted X.509 certificate into a key store.
60
 * <p>
61
 * Possible options for this command are:
62
 * <p>
63
 * <dl>
64
 *      <dt>-file FILE_NAME</dt>
65
 *      <dd>The fully qualified path of the file containing the trusted CA
66
 *      certificate to import. If omitted, the tool will process STDIN.
67
 *      <p></dd>
68
 *
69
 *      <dt>-storetype STORE_TYPE</dt>
70
 *      <dd>Use this option to specify the type of the key store to use. The
71
 *      default value, if this option is omitted, is that of the property
72
 *      <code>keystore.type</code> in the security properties file, which is
73
 *      obtained by invoking the {@link java.security.KeyStore#getDefaultType()}
74
 *      static method.
75
 *      <p></dd>
76
 *
77
 *      <dt>-keystore URL</dt>
78
 *      <dd>Use this option to specify the location of the key store to use.
79
 *      The default value is a file {@link java.net.URL} referencing the file
80
 *      named <code>.keystore</code> located in the path returned by the call to
81
 *      {@link java.lang.System#getProperty(String)} using <code>user.home</code>
82
 *      as argument.
83
 *      <p>
84
 *      If a URL was specified, but was found to be malformed --e.g. missing
85
 *      protocol element-- the tool will attempt to use the URL value as a file-
86
 *      name (with absolute or relative path-name) of a key store --as if the
87
 *      protocol was <code>file:</code>.
88
 *      <p></dd>
89
 *
90
 *      <dt>-storepass PASSWORD</dt>
91
 *      <dd>Use this option to specify the password protecting the key store. If
92
 *      this option is omitted from the command line, you will be prompted to
93
 *      provide a password.
94
 *      <p></dd>
95
 *
96
 *      <dt>-provider PROVIDER_CLASS_NAME</dt>
97
 *      <dd>A fully qualified class name of a Security Provider to add to the
98
 *      current list of Security Providers already installed in the JVM in-use.
99
 *      If a provider class is specified with this option, and was successfully
100
 *      added to the runtime --i.e. it was not already installed-- then the tool
101
 *      will attempt to removed this Security Provider before exiting.
102
 *      <p></dd>
103
 *
104
 *      <dt>-v</dt>
105
 *      <dd>Use this option to enable more verbose output.</dd>
106
 * </dl>
107
 */
108
public class CACertCmd
109
    extends Command
110
{
111
  private static final Logger log = Logger.getLogger(CACertCmd.class.getName());
112
  /** Pathname of the file containing the CA certificate to import. */
113
  protected String _certFileName;
114
  /** Type of the key store to use. */
115
  protected String _ksType;
116
  /** The URL to the keystore where the trusted certificates will be added. */
117
  protected String _ksURL;
118
  /** The password protecting the keystore. */
119
  protected String _ksPassword;
120
  /** Class name of a security provider to use. */
121
  protected String _providerClassName;
122
  /** Reference to the X.509 factory. */
123
  private CertificateFactory x509Factory;
124
 
125
  // default 0-arguments constructor
126
 
127
  // public setters -----------------------------------------------------------
128
 
129
  /** @param pathName the fully qualified path name of the file to process. */
130
  public void setFile(String pathName)
131
  {
132
    this._certFileName = pathName;
133
  }
134
 
135
  /** @param type the key-store type to use. */
136
  public void setStoretype(String type)
137
  {
138
    this._ksType = type;
139
  }
140
 
141
  /** @param url the key-store URL to use. */
142
  public void setKeystore(String url)
143
  {
144
    this._ksURL = url;
145
  }
146
 
147
  /** @param password the key-store password to use. */
148
  public void setStorepass(String password)
149
  {
150
    this._ksPassword = password;
151
  }
152
 
153
  /** @param className a security provider fully qualified class name to use. */
154
  public void setProvider(String className)
155
  {
156
    this._providerClassName = className;
157
  }
158
 
159
  // life-cycle methods -------------------------------------------------------
160
 
161
  /* (non-Javadoc)
162
   * @see gnu.classpath.tools.keytool.Command#setup()
163
   */
164
  void setup() throws Exception
165
  {
166
    setInputStreamParam(_certFileName);
167
    setKeyStoreParams(_providerClassName, _ksType, _ksPassword, _ksURL);
168
    if (Configuration.DEBUG)
169
      {
170
        log.fine("-cacert handler will use the following options:"); //$NON-NLS-1$
171
        log.fine("  -file=" + _certFileName); //$NON-NLS-1$
172
        log.fine("  -storetype=" + storeType); //$NON-NLS-1$
173
        log.fine("  -keystore=" + storeURL); //$NON-NLS-1$
174
        log.fine("  -provider=" + provider); //$NON-NLS-1$
175
        log.fine("  -v=" + verbose); //$NON-NLS-1$
176
      }
177
  }
178
 
179
  void start() throws CertificateException, KeyStoreException,
180
      NoSuchAlgorithmException, IOException
181
  {
182
    if (Configuration.DEBUG)
183
      log.entering(this.getClass().getName(), "start"); //$NON-NLS-1$
184
    alias = getAliasFromFileName(_certFileName);
185
    if (store.containsAlias(alias))
186
      throw new IllegalArgumentException(Messages.getFormattedString("CACertCmd.0", //$NON-NLS-1$
187
                                                                     alias));
188
    x509Factory = CertificateFactory.getInstance("X.509"); //$NON-NLS-1$
189
    Certificate certificate = x509Factory.generateCertificate(inStream);
190
    if (Configuration.DEBUG)
191
      log.fine("certificate = " + certificate); //$NON-NLS-1$
192
    store.setCertificateEntry(alias, certificate);
193
    saveKeyStore();
194
    if (verbose)
195
      System.out.println(Messages.getFormattedString("CACertCmd.1", //$NON-NLS-1$
196
                                                     new Object[] { _certFileName,
197
                                                                    alias }));
198
    if (Configuration.DEBUG)
199
      log.exiting(this.getClass().getName(), "start"); //$NON-NLS-1$
200
  }
201
 
202
  // own methods --------------------------------------------------------------
203
 
204
  /* (non-Javadoc)
205
   * @see gnu.classpath.tools.keytool.Command#getParser()
206
   */
207
  Parser getParser()
208
  {
209
    if (Configuration.DEBUG)
210
      log.entering(this.getClass().getName(), "getParser"); //$NON-NLS-1$
211
    Parser result = new ClasspathToolParser(Main.CACERT_CMD, true);
212
    result.setHeader(Messages.getString("CACertCmd.2")); //$NON-NLS-1$
213
    result.setFooter(Messages.getString("CACertCmd.3")); //$NON-NLS-1$
214
    OptionGroup options = new OptionGroup(Messages.getString("CACertCmd.4")); //$NON-NLS-1$
215
    options.add(new Option(Main.FILE_OPT,
216
                           Messages.getString("CACertCmd.5"), //$NON-NLS-1$
217
                           Messages.getString("CACertCmd.6")) //$NON-NLS-1$
218
    {
219
      public void parsed(String argument) throws OptionException
220
      {
221
        _certFileName = argument;
222
      }
223
    });
224
    options.add(new Option(Main.STORETYPE_OPT,
225
                           Messages.getString("CACertCmd.7"), //$NON-NLS-1$
226
                           Messages.getString("CACertCmd.8")) //$NON-NLS-1$
227
    {
228
      public void parsed(String argument) throws OptionException
229
      {
230
        _ksType = argument;
231
      }
232
    });
233
    options.add(new Option(Main.KEYSTORE_OPT,
234
                           Messages.getString("CACertCmd.9"), //$NON-NLS-1$
235
                           Messages.getString("CACertCmd.10")) //$NON-NLS-1$
236
    {
237
      public void parsed(String argument) throws OptionException
238
      {
239
        _ksURL = argument;
240
      }
241
    });
242
    options.add(new Option(Main.STOREPASS_OPT,
243
                           Messages.getString("CACertCmd.11"), //$NON-NLS-1$
244
                           Messages.getString("CACertCmd.12")) //$NON-NLS-1$
245
    {
246
      public void parsed(String argument) throws OptionException
247
      {
248
        _ksPassword = argument;
249
      }
250
    });
251
    options.add(new Option(Main.PROVIDER_OPT,
252
                           Messages.getString("CACertCmd.13"), //$NON-NLS-1$
253
                           Messages.getString("CACertCmd.14")) //$NON-NLS-1$
254
    {
255
      public void parsed(String argument) throws OptionException
256
      {
257
        _providerClassName = argument;
258
      }
259
    });
260
    options.add(new Option(Main.VERBOSE_OPT,
261
                           Messages.getString("CACertCmd.15")) //$NON-NLS-1$
262
    {
263
      public void parsed(String argument) throws OptionException
264
      {
265
        verbose = true;
266
      }
267
    });
268
    result.add(options);
269
    if (Configuration.DEBUG)
270
      log.exiting(this.getClass().getName(), "getParser", result); //$NON-NLS-1$
271
    return result;
272
  }
273
 
274
  /**
275
   * Construct an Alias string from the name of the file containing the
276
   * certificate to import. This method first removes the last dot (".")
277
   * character and any subsequent characters from the input name, and then
278
   * replaces any space and dot characters with underscores. For example the
279
   * input string <code>brasil.gov.br.cert</code> will result in
280
   * <code>brasil_gov_br</code> as its alias.
281
   *
282
   * @param fileName the name of the file containing the CA certificate
283
   * @return a string which can, and will, be used as the Alias of this CA
284
   *         certificate.
285
   */
286
  private String getAliasFromFileName(String fileName)
287
  {
288
    if (Configuration.DEBUG)
289
      log.entering(this.getClass().getName(), "getAliasFromFileName", fileName); //$NON-NLS-1$
290
    // get the basename
291
    fileName = new File(fileName).getName();
292
    // remove '.' if at start
293
    if (fileName.startsWith(".")) //$NON-NLS-1$
294
      fileName = fileName.substring(1);
295
 
296
    // remove last \..+
297
    int ndx = fileName.lastIndexOf('.');
298
    if (ndx > 0)
299
      fileName = fileName.substring(0, ndx);
300
    // replace spaces and dots with underscores
301
    char[] chars = fileName.toCharArray();
302
    for (int i = 0; i < chars.length; i++)
303
      {
304
        char c = chars[i];
305
        if (c == ' ' || c == '.')
306
          chars[i] = '_';
307
      }
308
    String result = new String(chars);
309
    if (Configuration.DEBUG)
310
      log.exiting(this.getClass().getName(), "getAliasFromFileName", result); //$NON-NLS-1$
311
    return result;
312
  }
313
}

powered by: WebSVN 2.1.0

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