1 13 14 package org.ejbca.ui.cli; 15 16 import java.io.BufferedReader ; 17 import java.io.FileInputStream ; 18 import java.io.InputStreamReader ; 19 import java.security.KeyStore ; 20 import java.util.Enumeration ; 21 22 import org.ejbca.util.FileTools; 23 24 29 public class CaImportCACommand extends BaseCaAdminCommand { 30 35 public CaImportCACommand(String [] args) { 36 super(args); 37 } 38 39 45 public void execute() throws IllegalAdminCommandException, ErrorAdminCommandException { 46 if (args.length < 3) { 47 String msg = "Usage: CA importca <CA name> <pkcs12 file> [<signature alias>] [<encryption alias>]\n" + 48 "Leave out both <alias> to use the only available alias or get a list of available aliases" + 49 "if there are more than one.\n" + 50 "If no encryption alias is given, the encryption keys will be generated."; 51 throw new IllegalAdminCommandException(msg); 52 } 53 try { 54 String caName = args[1]; 55 String p12file = args[2]; 56 String alias = null; 57 String encryptionAlias = null; 58 if (args.length > 3) { 59 alias = args[3]; 60 } 61 if (args.length > 4) { 62 encryptionAlias = args[4]; 63 } 64 System.out.print("Enter keystore password: "); 65 String kspwd = new BufferedReader (new InputStreamReader (System.in)).readLine(); 66 byte[] keystorebytes = null; 68 keystorebytes = FileTools.readFiletoBuffer(p12file); 69 if (alias == null) { 71 KeyStore ks = KeyStore.getInstance("PKCS12","BC"); 73 FileInputStream fis = new FileInputStream (p12file); 74 ks.load(fis, kspwd.toCharArray()); 75 fis.close(); 76 Enumeration aliases = ks.aliases(); 77 int length = 0; 78 while (aliases.hasMoreElements()) { 79 alias = (String )aliases.nextElement(); 80 getOutputStream().println("Keystore contains alias: "+alias); 81 length++; 82 } 83 if (length > 1) { 84 throw new ErrorAdminCommandException("Keystore contains more than one alias, alias must be provided as argument."); 85 } else if (length < 1) { 86 throw new ErrorAdminCommandException("Keystore does not contains any aliases. It can not be used for a CA."); 87 } 88 } 90 getCAAdminSessionRemote().importCAFromKeyStore(administrator, caName, keystorebytes, kspwd.toCharArray(), kspwd.toCharArray(), alias, encryptionAlias); 91 92 } catch (ErrorAdminCommandException e) { 93 throw e; 94 } catch (Exception e) { 95 throw new ErrorAdminCommandException(e); 96 } 97 } } | Popular Tags |