KickJava   Java API By Example, From Geeks To Geeks.

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


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.BufferedReader JavaDoc;
17 import java.io.FileInputStream JavaDoc;
18 import java.io.InputStreamReader JavaDoc;
19 import java.security.KeyStore JavaDoc;
20 import java.util.Enumeration JavaDoc;
21
22 import org.ejbca.util.FileTools;
23
24 /**
25  * Imports a PKCS12 file and created a new CA from it.
26  *
27  * @version $Id: CaImportCACommand.java,v 1.2.10.1 2007/04/02 08:22:52 jeklund Exp $
28  */

29 public class CaImportCACommand extends BaseCaAdminCommand {
30     /**
31      * Creates a new instance of CaInfoCommand
32      *
33      * @param args command line arguments
34      */

35     public CaImportCACommand(String JavaDoc[] args) {
36         super(args);
37     }
38
39     /**
40      * Runs the command
41      *
42      * @throws IllegalAdminCommandException Error in command args
43      * @throws ErrorAdminCommandException Error running command
44      */

45     public void execute() throws IllegalAdminCommandException, ErrorAdminCommandException {
46         if (args.length < 3) {
47            String JavaDoc 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 JavaDoc caName = args[1];
55             String JavaDoc p12file = args[2];
56             String JavaDoc alias = null;
57             String JavaDoc 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 JavaDoc kspwd = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(System.in)).readLine();
66             // Read old keystore file in the beginning so we know it's good
67
byte[] keystorebytes = null;
68             keystorebytes = FileTools.readFiletoBuffer(p12file);
69             // Import CA from PKCS12 file
70
if (alias == null) {
71                 // First we must find what aliases there is in the pkcs12-file
72
KeyStore JavaDoc ks = KeyStore.getInstance("PKCS12","BC");
73                 FileInputStream JavaDoc fis = new FileInputStream JavaDoc(p12file);
74                 ks.load(fis, kspwd.toCharArray());
75                 fis.close();
76                 Enumeration JavaDoc aliases = ks.aliases();
77                 int length = 0;
78                 while (aliases.hasMoreElements()) {
79                     alias = (String JavaDoc)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                 // else alias already contains the only alias, so we can use that
89
}
90             getCAAdminSessionRemote().importCAFromKeyStore(administrator, caName, keystorebytes, kspwd.toCharArray(), kspwd.toCharArray(), alias, encryptionAlias);
91           
92         } catch (ErrorAdminCommandException e) {
93             throw e;
94         } catch (Exception JavaDoc e) {
95             throw new ErrorAdminCommandException(e);
96         }
97     } // execute
98
} // CaImportCACommand
99
Popular Tags