1 13 14 package org.ejbca.core.protocol.ws.client; 15 16 import java.io.File ; 17 import java.io.FileOutputStream ; 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 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 46 public PKCS12ReqCommand(String [] args) { 47 super(args); 48 } 49 50 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 username = args[ARG_USERNAME]; 66 String password = args[ARG_PASSWORD]; 67 String keyspec = args[ARG_KEYSPEC]; 68 String keyalg = args[ARG_KEYALG]; 69 String hardtokensn = getHardTokenSN(args[ARG_HARDTOKENSN]); 70 71 String 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 filepath = username + ".p12"; 83 84 if(outputPath != null){ 85 filepath = outputPath + "/" + filepath; 86 } 87 88 FileOutputStream fos = new FileOutputStream (filepath); 89 java.security.KeyStore 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 e) { 99 throw new ErrorAdminCommandException(e); 100 } 101 } 102 103 104 private String getHardTokenSN(String hardtokensn) { 105 if(hardtokensn.equalsIgnoreCase("NONE")){ 106 return null; 107 } 108 109 return hardtokensn; 110 } 111 112 113 private String getOutputPath(String outputpath) { 114 File dir = new File (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 |