~ubuntu-branches/ubuntu/precise/classpath/precise

« back to all changes in this revision

Viewing changes to tools/gnu/classpath/tools/keytool/Main.java

  • Committer: Bazaar Package Importer
  • Author(s): Michael Koch
  • Date: 2006-05-27 16:11:15 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20060527161115-h6e39eposdt5snb6
Tags: 2:0.91-3
* Install header files to /usr/include/classpath.
* debian/control: classpath: Conflict with jamvm < 1.4.3 and
  cacao < 0.96 (Closes: #368172).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Main.java -- Implementation of the keytool security tool
 
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.tools.HelpPrinter;
 
42
import gnu.classpath.tools.common.ProviderUtil;
 
43
import gnu.java.security.Registry;
 
44
import gnu.javax.crypto.jce.GnuCrypto;
 
45
import gnu.javax.security.auth.callback.GnuCallbacks;
 
46
 
 
47
import java.util.logging.Logger;
 
48
 
 
49
/**
 
50
 * The GNU Classpath implementation of the keytool security tool.
 
51
 * <p>
 
52
 * Except for the <code>-identitydb</code> command, available for importing
 
53
 * JDK 1.1 <i>identities</i> into a key store, this implementation is intended
 
54
 * to be compatible with the behaviour described in the public documentation of
 
55
 * the same tool included in JDK 1.4.
 
56
 */
 
57
public class Main
 
58
{
 
59
  private static final Logger log = Logger.getLogger(Main.class.getName());
 
60
  /** The relative file path to the command tool's help text. */
 
61
  private static final String HELP_PATH = "keytool/keytool.txt"; //$NON-NLS-1$
 
62
  /** The Preferences key name for the last issued certificate serial nbr. */
 
63
  static final String LAST_SERIAL_NUMBER = "lastSerialNumber"; //$NON-NLS-1$
 
64
  /** Constant denoting the X.509 certificate type. */
 
65
  static final String X_509 = "X.509"; //$NON-NLS-1$
 
66
 
 
67
  /** Whether we have already printed the help text or not. */
 
68
  private boolean helpPrinted;
 
69
  /** The new position of GnuCRYPTO provider if it is not already installed. */
 
70
  private int gnuCryptoProviderNdx = -2;
 
71
  /** The new position of GNU Callbacks provider if it is not already installed. */
 
72
  private int gnuCallbacksNdx = -2;
 
73
 
 
74
  private Main()
 
75
  {
 
76
    super();
 
77
  }
 
78
 
 
79
  public static final void main(String[] args)
 
80
  {
 
81
    log.entering(Main.class.getName(), "main", args); //$NON-NLS-1$
 
82
 
 
83
    Main tool = new Main();
 
84
    try
 
85
      {
 
86
        tool.setup();
 
87
        tool.start(args);
 
88
      }
 
89
    catch (SecurityException x)
 
90
      {
 
91
        log.throwing(Main.class.getName(), "main", x); //$NON-NLS-1$
 
92
        System.err.println(Messages.getString("Main.6") + x.getMessage()); //$NON-NLS-1$
 
93
      }
 
94
    catch (Exception x)
 
95
      {
 
96
        log.throwing(Main.class.getName(), "main", x); //$NON-NLS-1$
 
97
        System.err.println(Messages.getString("Main.8") + x); //$NON-NLS-1$
 
98
      }
 
99
    finally
 
100
    {
 
101
      tool.teardown();
 
102
    }
 
103
 
 
104
    log.exiting(Main.class.getName(), "main"); //$NON-NLS-1$
 
105
    // System.exit(0);
 
106
  }
 
107
 
 
108
  // helper methods -----------------------------------------------------------
 
109
 
 
110
  private void start(String[] args) throws Exception
 
111
  {
 
112
    log.entering(this.getClass().getName(), "start", args); //$NON-NLS-1$
 
113
 
 
114
    if (args == null)
 
115
      args = new String[0];
 
116
 
 
117
    int limit = args.length;
 
118
    log.finest("args.length=" + limit); //$NON-NLS-1$
 
119
    int i = 0;
 
120
    String opt;
 
121
    Command cmd;
 
122
    while (i < limit)
 
123
      {
 
124
        opt = args[i];
 
125
        log.finest("args[" + i + "]=" + opt); //$NON-NLS-1$ //$NON-NLS-2$
 
126
        if (opt == null || opt.length() == 0)
 
127
          continue;
 
128
 
 
129
        cmd = null;
 
130
        if ("-genkey".equals(opt)) //$NON-NLS-1$
 
131
          cmd = new GenKeyCmd();
 
132
        else if ("-import".equals(opt)) //$NON-NLS-1$
 
133
          cmd = new ImportCmd();
 
134
        else if ("-selfcert".equals(opt)) //$NON-NLS-1$
 
135
          cmd = new SelfCertCmd();
 
136
        else if ("-identitydb".equals(opt)) //$NON-NLS-1$
 
137
          cmd = new IdentityDBCmd();
 
138
        else if ("-certreq".equals(opt)) //$NON-NLS-1$
 
139
          cmd = new CertReqCmd();
 
140
        else if ("-export".equals(opt)) //$NON-NLS-1$
 
141
          cmd = new ExportCmd();
 
142
        else if ("-list".equals(opt)) //$NON-NLS-1$
 
143
          cmd = new ListCmd();
 
144
        else if ("-printcert".equals(opt)) //$NON-NLS-1$
 
145
          cmd = new PrintCertCmd();
 
146
        else if ("-keyclone".equals(opt)) //$NON-NLS-1$
 
147
          cmd = new KeyCloneCmd();
 
148
        else if ("-storepasswd".equals(opt)) //$NON-NLS-1$
 
149
          cmd = new StorePasswdCmd();
 
150
        else if ("-keypasswd".equals(opt)) //$NON-NLS-1$
 
151
          cmd = new KeyPasswdCmd();
 
152
        else if ("-delete".equals(opt)) //$NON-NLS-1$
 
153
          cmd = new DeleteCmd();
 
154
        else if ("-help".equals(opt)) //$NON-NLS-1$
 
155
          {
 
156
            printHelp();
 
157
            i++;
 
158
          }
 
159
        else
 
160
          {
 
161
            log.fine("Unknown command [" + opt + "] at index #" + i //$NON-NLS-1$ //$NON-NLS-2$
 
162
                     + ". Arguments from that token onward will be ignored"); //$NON-NLS-1$
 
163
            break;
 
164
          }
 
165
 
 
166
        if (cmd != null)
 
167
          {
 
168
            i = cmd.processArgs(args, i);
 
169
            cmd.doCommand();
 
170
          }
 
171
      }
 
172
 
 
173
    // the -help command is the default; i.e.
 
174
    //     keytool
 
175
    // is equivalent to:
 
176
    //     keytool -help
 
177
    if (i == 0)
 
178
      printHelp();
 
179
 
 
180
    if (i < limit) // more options than needed
 
181
      log.fine("Last recognized argument is assumed at index #" + (i - 1) //$NON-NLS-1$
 
182
               + ". Remaining arguments (" + args[i] + "...) will be ignored"); //$NON-NLS-1$ //$NON-NLS-2$
 
183
 
 
184
    log.exiting(this.getClass().getName(), "start"); //$NON-NLS-1$
 
185
  }
 
186
 
 
187
  private void setup()
 
188
  {
 
189
    log.entering(this.getClass().getName(), "setup"); //$NON-NLS-1$
 
190
 
 
191
    gnuCryptoProviderNdx = ProviderUtil.addProvider(new GnuCrypto());
 
192
    gnuCallbacksNdx = ProviderUtil.addProvider(new GnuCallbacks());
 
193
 
 
194
    log.exiting(this.getClass().getName(), "setup"); //$NON-NLS-1$
 
195
  }
 
196
 
 
197
  private void teardown()
 
198
  {
 
199
    log.entering(this.getClass().getName(), "teardown"); //$NON-NLS-1$
 
200
 
 
201
    // if we added our own providers remove them
 
202
    if (gnuCryptoProviderNdx > 0)
 
203
      ProviderUtil.removeProvider(Registry.GNU_CRYPTO);
 
204
 
 
205
    if (gnuCallbacksNdx > 0)
 
206
      ProviderUtil.removeProvider("GNU-CALLBACKS"); //$NON-NLS-1$
 
207
 
 
208
    log.exiting(this.getClass().getName(), "teardown"); //$NON-NLS-1$
 
209
  }
 
210
 
 
211
  private void printHelp()
 
212
  {
 
213
    if (helpPrinted)
 
214
      return;
 
215
 
 
216
    HelpPrinter.printHelp(HELP_PATH);
 
217
    helpPrinted = true;
 
218
  }
 
219
}