001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
013: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
014: * License for the specific language governing permissions and limitations under
015: * the License.
016: */
017:
018: package org.apache.harmony.tools.keytool;
019:
020: /**
021: * The main class that bundles command line parsing, interaction with the user
022: * and work with keys and certificates.
023: *
024: * Class that implements the functionality of the key and certificate management
025: * tool.
026: */
027: public class Main {
028:
029: /**
030: * Does the actual work with keys and certificates, based on the parameter
031: * param. If something goes wrong an exception is thrown.
032: */
033: static void doWork(KeytoolParameters param) throws Exception {
034: switch (param.getCommand()) {
035: case EXPORT:
036: CertExporter.exportCert(param);
037: break;
038: case LIST:
039: KeyStoreCertPrinter.list(param);
040: break;
041: case PRINTCERT:
042: KeyStoreCertPrinter.printCert(param);
043: break;
044: case KEYCLONE:
045: EntryManager.keyClone(param);
046: break;
047: case DELETE:
048: EntryManager.delete(param);
049: break;
050: case STOREPASSWD:
051: KeytoolKSLoaderSaver.storePasswd(param);
052: break;
053: case KEYPASSWD:
054: EntryManager.keyPasswd(param);
055: break;
056: case IMPORT:
057: CertImporter.importCert(param);
058: break;
059: case CHECK:
060: CRLManager.checkRevoked(param);
061: break;
062: case VERIFY:
063: CertChainVerifier.verifyChain(param);
064: break;
065: case CERTREQ:
066: CSRGenerator.certReq(param);
067: break;
068: case HELP:
069: if (param.getHelpTopic() != null) {
070: HelpPrinter.topicHelp(param.getHelpTopic());
071: } else {
072: HelpPrinter.printHelp();
073: }
074: break;
075: case GENKEY:
076: KeyCertGenerator.genKey(param);
077: break;
078: case SELFCERT:
079: KeyCertGenerator.selfCert(param);
080: break;
081: case CONVERT:
082: KeyStoreConverter.convertKeyStore(param);
083: break;
084: }
085: }
086:
087: /**
088: * The main method to run from another program.
089: *
090: * @param args -
091: * command line with options.
092: */
093: public static void run(String[] args) throws Exception {
094: KeytoolParameters param = ArgumentsParser.parseArgs(args);
095:
096: if (param == null) {
097: HelpPrinter.printHelp();
098: System.exit(-1);
099: }
100:
101: Command command = param.getCommand();
102:
103: // all commands except printcert and help work with a store
104: if (command != Command.PRINTCERT && command != Command.HELP) {
105: // all commands that work with store except list and export
106: // need store password to with keystore.
107: if (param.getStorePass() == null && command != Command.LIST
108: && command != Command.EXPORT) {
109: throw new KeytoolException(
110: "Must specify store password to work with this command.");
111: }
112: // prompt for additional parameters if some of the expected
113: // ones have not been specified.
114: ArgumentsParser.getAdditionalParameters(param);
115:
116: // print the warning if store password is not set
117: if (param.getStorePass() == null) {
118: System.out
119: .println("\nWARNING!!!\nThe integrity "
120: + "of the keystore data has NOT been checked!\n"
121: + "To check it you must provide"
122: + " your keystore password!\n");
123: }
124: }
125:
126: // the work is being done here
127: doWork(param);
128:
129: if (param.isNeedSaveKS()) {
130: // save the store
131: KeytoolKSLoaderSaver.saveStore(param);
132: }
133: }
134:
135: /**
136: * The main method to run from command line.
137: *
138: * @param args -
139: * command line with options.
140: */
141: public static void main(String[] args) {
142: try {
143: run(args);
144: } catch (Exception e) {
145: // System.out.println("Keytool error: " + e);
146: e.printStackTrace();
147: }
148: }
149: }
|