KickJava   Java API By Example, From Geeks To Geeks.

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


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.FileInputStream JavaDoc;
18 import java.io.FileNotFoundException JavaDoc;
19 import java.io.FileOutputStream JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.util.ArrayList JavaDoc;
22
23 //import org.ejbca.core.model.authorization.wsclient.AuthorizationDeniedException;
24
//import org.ejbca.core.protocol.ws.wsclient.Certificate;
25
import org.ejbca.core.protocol.ws.client.gen.AuthorizationDeniedException_Exception;
26 import org.ejbca.core.protocol.ws.client.gen.Certificate;
27 import org.ejbca.core.protocol.ws.common.CertificateHelper;
28 import org.ejbca.ui.cli.ErrorAdminCommandException;
29 import org.ejbca.ui.cli.IAdminCommand;
30 import org.ejbca.ui.cli.IllegalAdminCommandException;
31 import org.ejbca.util.CertTools;
32
33 /**
34  * Request a certificate given a pkcs10
35  *
36  * @version $Id: PKCS10ReqCommand.java,v 1.2 2006/10/08 22:53:26 herrvendil Exp $
37  */

38 public class PKCS10ReqCommand extends EJBCAWSRABaseCommand implements IAdminCommand{
39
40     
41     private static final int ARG_USERNAME = 1;
42     private static final int ARG_PASSWORD = 2;
43     private static final int ARG_PKCS10 = 3;
44     private static final int ARG_ENCODING = 4;
45     private static final int ARG_HARDTOKENSN = 5;
46     private static final int ARG_OUTPUTPATH = 6;
47     
48     /**
49      * Creates a new instance of PKCS10ReqCommand
50      *
51      * @param args command line arguments
52      */

53     public PKCS10ReqCommand(String JavaDoc[] args) {
54         super(args);
55     }
56
57     /**
58      * Runs the command
59      *
60      * @throws IllegalAdminCommandException Error in command args
61      * @throws ErrorAdminCommandException Error running command
62      */

63     public void execute() throws IllegalAdminCommandException, ErrorAdminCommandException {
64  
65         try {
66            
67             if(args.length < 6 || args.length > 7){
68                 usage();
69                 System.exit(-1);
70             }
71             
72             String JavaDoc username = args[ARG_USERNAME];
73             String JavaDoc password = args[ARG_PASSWORD];
74             String JavaDoc pkcs10 = getPKCS10(args[ARG_PKCS10]);
75             String JavaDoc encoding = getEncoding(args[ARG_ENCODING]);
76             String JavaDoc hardtokensn = getHardTokenSN(args[ARG_HARDTOKENSN]);
77             
78             String JavaDoc outputPath = null;
79             if(args.length == 7){
80               outputPath = getOutputPath(args[ARG_OUTPUTPATH]);
81             }
82             
83             try{
84                 Certificate result = getEjbcaRAWS().pkcs10Req(username,password,pkcs10,hardtokensn);
85                 
86                 if(result==null){
87                     getPrintStream().println("No certificate could be generated for user, check server logs for error.");
88                 }else{
89                     String JavaDoc filepath = username;
90                     if(encoding.equals("DER")){
91                         filepath += ".cer";
92                     }else{
93                         filepath += ".pem";
94                     }
95                     if(outputPath != null){
96                         filepath = outputPath + "/" + filepath;
97                     }
98                     
99                     
100                     if(encoding.equals("DER")){
101                         FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(filepath);
102                         fos.write(CertificateHelper.getCertificate(result.getCertificateData()).getEncoded());
103                         fos.close();
104                     }else{
105                         FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(filepath);
106                         ArrayList JavaDoc<java.security.cert.Certificate JavaDoc> list = new ArrayList JavaDoc<java.security.cert.Certificate JavaDoc>();
107                         list.add(CertificateHelper.getCertificate(result.getCertificateData()));
108                         fos.write(CertTools.getPEMFromCerts(list));
109                         fos.close();
110                     }
111                     getPrintStream().println("Certificate generated, written to " + filepath);
112                 }
113                              
114             }catch(AuthorizationDeniedException_Exception e){
115                 getPrintStream().println("Error : " + e.getMessage());
116             }
117         } catch (Exception JavaDoc e) {
118             throw new ErrorAdminCommandException(e);
119         }
120     }
121
122
123
124     
125
126     private String JavaDoc getHardTokenSN(String JavaDoc hardtokensn) {
127         if(hardtokensn.equalsIgnoreCase("NONE")){
128           return null;
129         }
130         
131         return hardtokensn;
132     }
133
134     private String JavaDoc getPKCS10(String JavaDoc pkcs10Path) {
135         String JavaDoc retval=null;
136         try {
137             FileInputStream JavaDoc fis = new FileInputStream JavaDoc(pkcs10Path);
138             byte[] contents = new byte[fis.available()];
139             fis.read(contents);
140             fis.close();
141             retval = new String JavaDoc(contents);
142         } catch (FileNotFoundException JavaDoc e) {
143             getPrintStream().println("Error : PKCS10 file couln't be found.");
144             System.exit(-1);
145         } catch (IOException JavaDoc e) {
146             getPrintStream().println("Error reading content of PKCS10 file.");
147             System.exit(-1);
148         }
149         
150         
151         return retval;
152     }
153
154     private String JavaDoc getOutputPath(String JavaDoc outputpath) {
155         File JavaDoc dir = new File JavaDoc(outputpath);
156         if(!dir.exists()){
157             getPrintStream().println("Error : Output directory doesn't seem to exist.");
158             System.exit(-1);
159         }
160         if(!dir.isDirectory()){
161             getPrintStream().println("Error : Output directory doesn't seem to be a directory.");
162             System.exit(-1);
163         }
164         if(!dir.canWrite()){
165             getPrintStream().println("Error : Output directory isn't writeable.");
166             System.exit(-1);
167
168         }
169         return outputpath;
170     }
171
172     private String JavaDoc getEncoding(String JavaDoc encoding) {
173         if(!encoding.equalsIgnoreCase("PEM") && !encoding.equalsIgnoreCase("DER")){
174             usage();
175             System.exit(-1);
176         }
177         
178         return encoding.toUpperCase();
179     }
180
181
182
183     protected void usage() {
184         getPrintStream().println("Command used to generate a users certificate");
185         getPrintStream().println("Usage : pkcs10req <username> <password> <pkcs10path> <encoding (DER|PEM)> <hardtokensn (or NONE)> <outputpath (optional)> \n\n");
186         getPrintStream().println("outputpath : directory where certificate is written in form username+.cer|.pem ");
187    }
188
189
190 }
191
Popular Tags