KickJava   Java API By Example, From Geeks To Geeks.

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


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.FileOutputStream JavaDoc;
18 import java.io.InputStreamReader JavaDoc;
19
20 /**
21  * Imports a PKCS12 file and created a new CA from it.
22  *
23  * @version $Id: CaExportCACommand.java,v 1.1.2.2 2007/04/02 08:22:52 jeklund Exp $
24  */

25 public class CaExportCACommand extends BaseCaAdminCommand {
26     /**
27      * Creates a new instance of CaInfoCommand
28      *
29      * @param args command line arguments
30      */

31     public CaExportCACommand(String JavaDoc[] args) {
32         super(args);
33     }
34
35     /**
36      * Runs the command
37      *
38      * @throws IllegalAdminCommandException Error in command args
39      * @throws ErrorAdminCommandException Error running command
40      */

41     public void execute() throws IllegalAdminCommandException, ErrorAdminCommandException {
42         String JavaDoc signatureKeyAlias = "SignatureKeyAlias";
43         String JavaDoc encryptionKeyAlias = "EncryptionKeyAlias";
44         if (args.length < 3) {
45            String JavaDoc msg = "Usage: CA exportca <CA name> <pkcs12 file> [<signature_key_alias>] [<encryption_key_alias>]\n" +
46                         "Default values for signature_key_alias is \"" + signatureKeyAlias + "\" and encryption_key_alias" +
47                         " is \"" + encryptionKeyAlias + "\".";
48            throw new IllegalAdminCommandException(msg);
49         }
50         try {
51             String JavaDoc caName = args[1];
52             String JavaDoc p12file = args[2];
53             if ( args.length > 3 ) {
54                 signatureKeyAlias = args[3];
55             }
56             if ( args.length > 4 ) {
57                 encryptionKeyAlias = args[4];
58             }
59             System.out.print("Enter keystore password: ");
60             String JavaDoc kspwd = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(System.in)).readLine();
61             
62             if ( !getCAAdminSessionRemote().isKeyStorePassword(administrator, kspwd) ) {
63                 throw new IllegalArgumentException JavaDoc("Keystore password does not match user-supplied password.");
64             }
65             byte[] keyStoreBytes = getCAAdminSessionRemote().exportCAKeyStore(administrator, caName, kspwd.toCharArray(), kspwd.toCharArray(), signatureKeyAlias, encryptionKeyAlias);
66             FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(p12file);
67             fos.write(keyStoreBytes);
68             fos.close();
69         } catch (ErrorAdminCommandException e) {
70             throw e;
71         } catch (Exception JavaDoc e) {
72             throw new ErrorAdminCommandException(e);
73         }
74     } // execute
75
}
76
Popular Tags