1 13 14 package org.ejbca.ui.cli; 15 16 import java.io.FileInputStream ; 17 import java.io.FileOutputStream ; 18 19 20 21 26 public class HSMKeyTool { 27 private static String CREATE_CA_SWITCH = "createca"; 28 private static String ENCRYPT_SWITCH = "encrypt"; 29 private static String DECRYPT_SWITCH = "decrypt"; 30 private static String GENERATE_SWITCH = "generate"; 31 private static String DELETE_SWITCH = "delete"; 32 private static String TEST_SWITCH = "test"; 33 private static String CREATE_KEYSTORE_SWITCH = "createkeystore"; 34 private static String CREATE_KEYSTORE_MODULE_SWITCH = "createkeystoremodule"; 35 private static String MOVE_SWITCH = "move"; 36 39 public static void main(String [] 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 e) { 45 System.out.println(e.getMessage()); 46 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 (args[6]), new FileOutputStream (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 (args[6]), new FileOutputStream (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 e) { 96 e.printStackTrace(System.err); 97 } 98 } 99 } 100 | Popular Tags |