KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejbca > core > protocol > ws > client > PKCS12ReqCommand


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.core.protocol.ws.client;
15
16 import java.io.File JavaDoc;
17 import java.io.FileOutputStream JavaDoc;
18
19 import org.ejbca.core.protocol.ws.client.gen.AuthorizationDeniedException_Exception;
20 import org.ejbca.core.protocol.ws.client.gen.KeyStore;
21 import org.ejbca.core.protocol.ws.common.KeyStoreHelper;
22 import org.ejbca.ui.cli.ErrorAdminCommandException;
23 import org.ejbca.ui.cli.IAdminCommand;
24 import org.ejbca.ui.cli.IllegalAdminCommandException;
25
26 /**
27  * Request a keystore given a pkcs12
28  *
29  * @version $Id: PKCS12ReqCommand.java,v 1.4 2006/11/02 08:03:22 anatom Exp $
30  */

31 public class PKCS12ReqCommand extends EJBCAWSRABaseCommand implements IAdminCommand{
32
33     
34     private static final int ARG_USERNAME = 1;
35     private static final int ARG_PASSWORD = 2;
36     private static final int ARG_KEYSPEC = 3;
37     private static final int ARG_KEYALG = 4;
38     private static final int ARG_HARDTOKENSN = 5;
39     private static final int ARG_OUTPUTPATH = 6;
40     
41     /**
42      * Creates a new instance of PKCS12ReqCommand
43      *
44      * @param args command line arguments
45      */

46     public PKCS12ReqCommand(String JavaDoc[] args) {
47         super(args);
48     }
49
50     /**
51      * Runs the command
52      *
53      * @throws IllegalAdminCommandException Error in command args
54      * @throws ErrorAdminCommandException Error running command
55      */

56     public void execute() throws IllegalAdminCommandException, ErrorAdminCommandException {
57        
58         try {
59            
60             if(args.length < 5 || args.length > 6){
61                 usage();
62                 System.exit(-1);
63             }
64             
65             String JavaDoc username = args[ARG_USERNAME];
66             String JavaDoc password = args[ARG_PASSWORD];
67             String JavaDoc keyspec = args[ARG_KEYSPEC];
68             String JavaDoc keyalg = args[ARG_KEYALG];
69             String JavaDoc hardtokensn = getHardTokenSN(args[ARG_HARDTOKENSN]);
70             
71             String JavaDoc outputPath = null;
72             if(args.length == 6){
73               outputPath = getOutputPath(args[ARG_OUTPUTPATH]);
74             }
75             
76             try{
77                 KeyStore result = getEjbcaRAWS().pkcs12Req(username,password,hardtokensn,keyspec,keyalg);
78                 
79                 if(result==null){
80                     getPrintStream().println("No keystore could be generated for user, check server logs for error.");
81                 }else{
82                     String JavaDoc filepath = username + ".p12";
83                     
84                     if(outputPath != null){
85                         filepath = outputPath + "/" + filepath;
86                     }
87                                         
88                     FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(filepath);
89                     java.security.KeyStore JavaDoc ks = KeyStoreHelper.getKeyStore(result.getKeystoreData(),"PKCS12",password);
90                     ks.store(fos, password.toCharArray());
91                     fos.close();
92                     getPrintStream().println("Keystore generated, written to " + filepath);
93                 }
94                              
95             }catch(AuthorizationDeniedException_Exception e){
96                 getPrintStream().println("Error : " + e.getMessage());
97             }
98         } catch (Exception JavaDoc e) {
99             throw new ErrorAdminCommandException(e);
100         }
101     }
102
103
104     private String JavaDoc getHardTokenSN(String JavaDoc hardtokensn) {
105         if(hardtokensn.equalsIgnoreCase("NONE")){
106           return null;
107         }
108         
109         return hardtokensn;
110     }
111
112
113     private String JavaDoc getOutputPath(String JavaDoc outputpath) {
114         File JavaDoc dir = new File JavaDoc(outputpath);
115         if(!dir.exists()){
116             getPrintStream().println("Error : Output directory doesn't seem to exist.");
117             System.exit(-1);
118         }
119         if(!dir.isDirectory()){
120             getPrintStream().println("Error : Output directory doesn't seem to be a directory.");
121             System.exit(-1);
122         }
123         if(!dir.canWrite()){
124             getPrintStream().println("Error : Output directory isn't writeable.");
125             System.exit(-1);
126
127         }
128         return outputpath;
129     }
130
131
132
133
134     protected void usage() {
135         getPrintStream().println("Command used to generate a users keystore");
136         getPrintStream().println("Usage : pkcs12req <username> <password> <keyspec (512|1024|2048|4096|prime192v1 etc)> <keyalg (RSA|ECDSA)> <hardtokensn (or NONE)> <outputpath (optional)> \n\n");
137    }
138
139
140 }
141
Popular Tags