KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejbca > ui > cli > HSMKeyTool


1 /*************************************************************************
2  * *
3  * EJBCA: The OpenSource Certificate Authority *
4  * *
5  * This software is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU Lesser General Public *
7  * License as published by the Free Software Foundation; either *
8  * version 2.1 of the License, or any later version. *
9  * *
10  * See terms of license at gnu.org. *
11  * *
12  *************************************************************************/

13
14 package org.ejbca.ui.cli;
15
16 import java.io.FileInputStream JavaDoc;
17 import java.io.FileOutputStream JavaDoc;
18
19
20
21 /**
22  * @author lars
23  * @version $Id: HSMKeyTool.java,v 1.12.2.7 2007/04/10 20:47:50 primelars Exp $
24  *
25  */

26 public class HSMKeyTool {
27     private static String JavaDoc CREATE_CA_SWITCH = "createca";
28     private static String JavaDoc ENCRYPT_SWITCH = "encrypt";
29     private static String JavaDoc DECRYPT_SWITCH = "decrypt";
30     private static String JavaDoc GENERATE_SWITCH = "generate";
31     private static String JavaDoc DELETE_SWITCH = "delete";
32     private static String JavaDoc TEST_SWITCH = "test";
33     private static String JavaDoc CREATE_KEYSTORE_SWITCH = "createkeystore";
34     private static String JavaDoc CREATE_KEYSTORE_MODULE_SWITCH = "createkeystoremodule";
35     private static String JavaDoc MOVE_SWITCH = "move";
36     /**
37      * @param args
38      */

39     public static void main(String JavaDoc[] args) {
40         try {
41             if ( args.length > 1 && args[1].toLowerCase().trim().equals(CREATE_CA_SWITCH)) {
42                 try {
43                     new HwCaInitCommand(args).execute();
44                 } catch (Exception JavaDoc e) {
45                     System.out.println(e.getMessage());
46                     //e.printStackTrace();
47
System.exit(-1);
48                 }
49             } else if ( args.length > 1 && args[1].toLowerCase().trim().equals(GENERATE_SWITCH)) {
50                 if ( args.length < 6 )
51                     System.err.println(args[0] + " " + args[1] + " <key size> [<key entry name>] [<keystore ID>]");
52                 else
53                     new KeyStoreContainer(args[4], args[2], args[3], args.length>7 ? args[7] : null).generate(Integer.parseInt(args[5].trim()), args.length>6 ? args[6] :"myKey");
54             } else if ( args.length > 1 && args[1].toLowerCase().trim().equals(DELETE_SWITCH)) {
55                 if ( args.length < 6 )
56                     System.err.println(args[0] + " " + args[1] + " <keystore ID> [<key entry name>]");
57                 else
58                     new KeyStoreContainer(args[4], args[2], args[3], args[5]).delete(args.length>6 ? args[6] : null);
59             } else if ( args.length > 1 && args[1].toLowerCase().trim().equals(ENCRYPT_SWITCH)) {
60                 if ( args.length < 9 )
61                     System.err.println(args[0] + " " + args[1] + " <keystore ID> <input file> <output file> <key alias>");
62                 else
63                     new KeyStoreContainer(args[4], args[2], args[3], args[5]).encrypt(new FileInputStream JavaDoc(args[6]), new FileOutputStream JavaDoc(args[7]), args[8]);
64             } else if ( args.length > 1 && args[1].toLowerCase().trim().equals(DECRYPT_SWITCH)) {
65                 if ( args.length < 9 )
66                     System.err.println(args[0] + " " + args[1] + " <keystore ID> <input file> <output file> <key alias>");
67                 else
68                     new KeyStoreContainer(args[4], args[2], args[3], args[5]).decrypt(new FileInputStream JavaDoc(args[6]), new FileOutputStream JavaDoc(args[7]), args[8]);
69             } else if( args.length > 1 && args[1].toLowerCase().trim().equals(TEST_SWITCH)) {
70                 if ( args.length < 6 )
71                     System.err.println(args[0] + " " + args[1] + " <keystore ID> [<# of tests>]");
72                 else
73                     KeyStoreContainerTest.test(args[2], args[3], args[4], args[5], args.length>6 ? Integer.parseInt(args[6].trim()) : 1);
74             } else if( args.length > 1 && args[1].toLowerCase().trim().equals(CREATE_KEYSTORE_SWITCH)) {
75                 new KeyStoreContainer(args[4], args[2], args[3], null).storeKeyStore();
76             } else if( args.length > 1 && args[1].toLowerCase().trim().equals(CREATE_KEYSTORE_MODULE_SWITCH)) {
77                 System.setProperty("protect", "module");
78                 new KeyStoreContainer(args[4], args[2], args[3], null).storeKeyStore();
79             } else if( args.length > 1 && args[1].toLowerCase().trim().equals(MOVE_SWITCH)) {
80                 if ( args.length < 7 )
81                     System.err.println(args[0] + " " + args[1] + " <from keystore ID> <to keystore ID>");
82                 else
83                     KeyStoreContainer.move(args[2], args[3], args[4], args[5], args[6]);
84             } else
85                 System.err.println("Use \"" +
86                                    args[0]+" "+CREATE_CA_SWITCH+"\" or \"" +
87                                    args[0]+" "+GENERATE_SWITCH+"\" or \"" +
88                                    args[0]+" "+DELETE_SWITCH+"\" or \"" +
89                                    args[0]+" "+TEST_SWITCH+"\" or \"" +
90                                    args[0]+" "+CREATE_KEYSTORE_SWITCH+"\" or \"" +
91                                    args[0]+" "+CREATE_KEYSTORE_MODULE_SWITCH+"\" or \"" +
92                                    args[0]+" "+ENCRYPT_SWITCH+"\" or \"" +
93                                    args[0]+" "+DECRYPT_SWITCH+"\" or \"" +
94                                    args[0]+" "+MOVE_SWITCH+"\".");
95         } catch (Throwable JavaDoc e) {
96             e.printStackTrace(System.err);
97         }
98     }
99 }
100
Popular Tags