1 13 14 package org.ejbca.core.protocol.ws.client; 15 16 import java.io.File ; 17 import java.io.FileInputStream ; 18 import java.io.FileNotFoundException ; 19 import java.io.FileOutputStream ; 20 import java.io.IOException ; 21 import java.util.ArrayList ; 22 23 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 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 53 public PKCS10ReqCommand(String [] args) { 54 super(args); 55 } 56 57 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 username = args[ARG_USERNAME]; 73 String password = args[ARG_PASSWORD]; 74 String pkcs10 = getPKCS10(args[ARG_PKCS10]); 75 String encoding = getEncoding(args[ARG_ENCODING]); 76 String hardtokensn = getHardTokenSN(args[ARG_HARDTOKENSN]); 77 78 String 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 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 fos = new FileOutputStream (filepath); 102 fos.write(CertificateHelper.getCertificate(result.getCertificateData()).getEncoded()); 103 fos.close(); 104 }else{ 105 FileOutputStream fos = new FileOutputStream (filepath); 106 ArrayList <java.security.cert.Certificate > list = new ArrayList <java.security.cert.Certificate >(); 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 e) { 118 throw new ErrorAdminCommandException(e); 119 } 120 } 121 122 123 124 125 126 private String getHardTokenSN(String hardtokensn) { 127 if(hardtokensn.equalsIgnoreCase("NONE")){ 128 return null; 129 } 130 131 return hardtokensn; 132 } 133 134 private String getPKCS10(String pkcs10Path) { 135 String retval=null; 136 try { 137 FileInputStream fis = new FileInputStream (pkcs10Path); 138 byte[] contents = new byte[fis.available()]; 139 fis.read(contents); 140 fis.close(); 141 retval = new String (contents); 142 } catch (FileNotFoundException e) { 143 getPrintStream().println("Error : PKCS10 file couln't be found."); 144 System.exit(-1); 145 } catch (IOException 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 getOutputPath(String outputpath) { 155 File dir = new File (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 getEncoding(String 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 |