1 23 package com.sun.enterprise.security; 24 25 import java.security.Security ; 26 import java.security.Provider ; 27 28 import sun.security.tools.*; 29 import java.io.*; 30 import java.util.*; 31 import java.security.cert.*; 32 import java.security.KeyStore ; 33 import java.security.Key ; 34 import com.sun.enterprise.util.*; 35 import java.util.logging.*; 36 import com.sun.logging.*; 37 38 45 public final class KeyTool { 46 47 private static Logger _logger=null; 48 static { 49 _logger=LogDomains.getLogger(LogDomains.SECURITY_LOGGER); 50 } 51 52 private static final String JSSE_PROVIDER = 53 "com.sun.net.ssl.internal.ssl.Provider"; 54 55 private File inputFile=null; 57 private File outputFile=null; 59 private char[] jksKeyStorePass; 60 private char[] pkcsKeyStorePass = null; 61 private char[] jksKeyPass = null; 63 private char[] pkcsKeyPass = null; 64 65 private String provider = null; 66 private KeyStore pkcs12KeyStore = null; 68 private KeyStore jksKeyStore = null; 69 70 private static String PKCS12 = "-pkcs12"; 71 private static String INFILE = "-pkcsFile"; 72 private static String OUTFILE = "-jksFile"; 73 private static String PKCSKEYSTOREPASS = "-pkcsKeyStorePass"; 74 private static String PKCSKEYPASS = "-pkcsKeyPass"; 75 private static String JKSKEYSTOREPASS = "-jksKeyStorePass"; 77 private static String JKSKEYPASS = "-jksKeyPass"; 78 private static LocalStringManagerImpl localStrings = 79 new LocalStringManagerImpl(KeyTool.class); 80 81 94 public KeyTool (String infile, String outfile, String pkcsKeyStorePass, 95 String pkcsKeyPass, String jksKeyStorePass, 96 String jksKeyPass, 97 String provider) throws IOException { 98 inputFile = new File (infile); 99 outputFile = new File (outfile); 100 this.pkcsKeyStorePass = pkcsKeyStorePass.toCharArray (); 101 this.pkcsKeyPass = pkcsKeyPass.toCharArray (); 102 this.jksKeyStorePass = jksKeyStorePass.toCharArray (); 103 this.jksKeyPass = jksKeyPass.toCharArray (); 104 this.provider = provider; 105 try{ 107 if (outputFile.exists ()){ 108 throw new IOException ("Output file already exists!"); 109 } 110 pkcs12KeyStore = KeyStore.getInstance ("PKCS12", provider); 112 jksKeyStore = KeyStore.getInstance ("JKS"); 113 114 } catch (Exception e) { 115 throw new IOException (e.getMessage ()); 117 } 118 readKeyStores (); 119 } 120 125 public void readKeyStores() throws IOException { 126 FileInputStream pkcsFis = null; 127 FileInputStream jksFis = null; 128 try { 129 pkcsFis = new FileInputStream(inputFile); 130 jksFis = new FileInputStream (outputFile); 131 } catch(Exception e) { 132 133 } finally { 134 try { 135 pkcs12KeyStore.load(pkcsFis, pkcsKeyStorePass); 136 jksKeyStore.load (jksFis, null); 139 } catch(Exception ce) { 140 _logger.log(Level.SEVERE, 142 "java_security.KeyStore_load_exception",ce); 143 } 144 if(pkcsFis != null) 145 pkcsFis.close(); 146 if (jksFis != null) 147 jksFis.close (); 148 } 149 } 150 154 public void writeJksKeyStore() throws IOException { 155 FileOutputStream fos = null; 156 try { 157 fos = new FileOutputStream(outputFile); 158 } catch(Exception e) { 159 } finally { 162 try { 163 jksKeyStore.store (fos, jksKeyStorePass); 164 } catch(Exception ce) { 165 _logger.log(Level.SEVERE, 167 "java_security.KeyStore_store_exception",ce); 168 } 169 if(fos != null) 170 fos.close(); 171 } 172 } 173 179 public void replicatePkcs12ToJks () throws Exception { 180 Enumeration e = pkcs12KeyStore.aliases (); 181 for (; e.hasMoreElements (); ){ 182 String alias = (String )e.nextElement (); 183 if (pkcs12KeyStore.isKeyEntry (alias)){ 184 185 188 Key key = pkcs12KeyStore.getKey (alias, pkcsKeyPass); 189 Certificate[] certs = 190 pkcs12KeyStore.getCertificateChain (alias); 191 jksKeyStore.setKeyEntry (alias, key, jksKeyPass, certs); 192 } else if (pkcs12KeyStore.isCertificateEntry (alias)){ 193 194 jksKeyStore.setCertificateEntry 195 (alias, pkcs12KeyStore.getCertificate (alias)); 196 } 197 } 198 } 199 202 public void info () throws Exception { 203 _logger.log(Level.FINEST," Keystore Information"); 204 _logger.log(Level.FINEST," Type = " + pkcs12KeyStore.getType ()); 205 _logger.log(Level.FINEST," Provider = "+ pkcs12KeyStore.getProvider ()); 206 _logger.log(Level.FINEST," KeyStore size = "+pkcs12KeyStore.size ()); 207 Enumeration e = pkcs12KeyStore.aliases (); 208 _logger.log(Level.FINEST," Kstore Aliases "); 209 for (; e.hasMoreElements (); ){ 210 String alias = (String )e.nextElement (); 211 _logger.log(Level.FINEST," Alias = "+ alias); 212 if (pkcs12KeyStore.isKeyEntry (alias)){ 213 _logger.log(Level.FINEST,"Alias is a key entry "); 214 Key key = pkcs12KeyStore.getKey (alias, pkcsKeyPass); 215 _logger.log(Level.FINEST," Format = "+key.getFormat ()); 216 } else if (pkcs12KeyStore.isCertificateEntry (alias)){ 217 _logger.log(Level.FINEST," Alias is a certificate entry"); 218 } 219 } 220 _logger.log(Level.FINEST," End of Information"); 221 } 222 225 public static void initProvider() { 226 try { 227 Provider p = 228 (Provider ) Class.forName(JSSE_PROVIDER).newInstance(); 229 Security.addProvider(p); 230 231 } catch(Exception e) { 232 _logger.log(Level.SEVERE,"java_security.provider_exception",e); 233 } 234 } 235 238 public static String getProviderName (){ 239 try{ 240 Provider p = 241 (Provider ) Class.forName(JSSE_PROVIDER).newInstance(); 242 return p.getName (); 243 } catch (Exception e) { 244 _logger.log(Level.SEVERE,"java_security.getName_exception",e); 245 } 246 return null; 247 } 248 public static void help (boolean exit){ 249 250 System.out.println 251 (localStrings.getLocalString ("enterprise.security.keytool", 252 "keytool")); 253 System.out.println 254 (localStrings.getLocalString 255 ("enterprise.security.keytooloptions", "PKCS Options:")); 256 System.out.println (" "+ PKCS12 + 257 " "+ INFILE + " fileName" + 258 " "+ PKCSKEYSTOREPASS + " password" + 259 " "+PKCSKEYPASS +" password" + 260 " "+OUTFILE+ " outputFileName"+ 261 " "+JKSKEYSTOREPASS + " password"); 262 265 if (exit) 266 System.exit (-1); 267 } 268 public static void main(String [] args) { 269 boolean pkcs = false; 270 initProvider(); 271 String provider = null; 272 String inFile = null; 273 String outFile = null; 274 String jksKeyPass = null; 275 String jksKeyStorePass = null; 276 String pkcsKeyPass = null; 277 String pkcsKeyStorePass = null; 278 try{ 279 if (args.length == 0){ 280 help (false); 281 sun.security.tools.KeyTool.main (args); 282 } 283 if (args[0].equalsIgnoreCase (PKCS12)){ 284 pkcs = true; 285 if (args.length != 11) 286 help (true); 287 if (!args[1].equalsIgnoreCase (INFILE)) 288 help (true); 289 inFile = args[2]; 290 if (!args[3].equalsIgnoreCase (PKCSKEYSTOREPASS)) 291 help (true); 292 pkcsKeyStorePass = args[4]; 293 if (!args[5].equalsIgnoreCase (PKCSKEYPASS)) 294 help (true); 295 pkcsKeyPass = args[6]; 296 if (!args[7].equalsIgnoreCase (OUTFILE)) 297 help (true); 298 outFile = args[8]; 299 if (!args[9].equalsIgnoreCase (JKSKEYSTOREPASS)) 300 help (true); 301 302 jksKeyStorePass = args[10]; 303 jksKeyPass = jksKeyStorePass; 304 312 } 313 if (!pkcs){ 314 sun.security.tools.KeyTool.main(args); 315 } else{ 316 provider = getProviderName (); 317 KeyTool kt = new KeyTool (inFile, outFile, pkcsKeyStorePass, 318 pkcsKeyPass, jksKeyStorePass, 319 jksKeyPass, 320 provider); 321 kt.replicatePkcs12ToJks (); 322 kt.writeJksKeyStore (); 323 } 324 } catch (Exception e){ 325 _logger.log(Level.SEVERE,"java_security.main_exception",e); 326 } 327 } 328 } 329 330 331 332 | Popular Tags |