1 23 24 package com.sun.enterprise.tools.upgrade.certconversion; 25 26 import java.io.*; 27 import java.util.*; 28 import java.util.logging.*; 29 import java.security.*; 30 import java.security.cert.*; 31 import com.sun.enterprise.tools.upgrade.logging.*; 32 import com.sun.enterprise.util.i18n.StringManager; 33 import com.sun.enterprise.tools.upgrade.common.*; 34 35 40 41 public class JKStoJKSConversionModule implements BaseModule{ 42 43 private static Logger _logger = LogService.getLogger(LogService.UPGRADE_LOGGER); 44 private StringManager sm; 45 private String jksPath; 46 private String sourceJksPath; 47 private String trustJksPath; 48 private String sourceTrustJksPath; 49 private String jksKeyStorePassword; 50 private String jksCAkeyStorePassword; 51 private boolean certificateError = false; 52 private CommonInfoModel commonInfo; 53 54 public JKStoJKSConversionModule(){ 55 sm = StringManager.getManager(LogService.UPGRADE_CERTCONVERSION_LOGGER); 56 } 57 58 public boolean upgrade(CommonInfoModel commonInfo){ 59 try { 60 String currentDomain = commonInfo.getCurrentDomain(); 61 if(!(commonInfo.getDomainOptionList().contains(currentDomain))) 62 return true; 63 this.jksPath=commonInfo.getTargetJKSKeyStorePath(); 64 this.sourceJksPath=commonInfo.getSourceJKSKeyStorePath(); 65 this.trustJksPath = commonInfo.getTargetTrustedJKSKeyStorePath(); 66 this.sourceTrustJksPath = commonInfo.getSourceTrustedJKSKeyStorePath(); 67 this.jksKeyStorePassword=commonInfo.getJksKeystorePassword(); 68 this.jksCAkeyStorePassword=commonInfo.getJksCAKeystorePassword(); 69 this.commonInfo = commonInfo; 70 _logger.log(Level.INFO,sm.getString("enterprise.tools.upgrade.certconversion.start_certificate_migration",currentDomain)); 71 doBackup(commonInfo); 72 copyKeyPairs(sourceJksPath,jksPath); 73 copyCACertificates(sourceTrustJksPath,trustJksPath); 74 _logger.log(Level.INFO,sm.getString("enterprise.tools.upgrade.certconversion.finished_certificate_migration",currentDomain)); 75 }catch(CertificateException ce) { 76 _logger.log(Level.WARNING,sm.getString("enterprise.tools.upgrade.certconversion.could_not_migrate_certificates",ce)); 77 UpdateProgressManager.getProgressManager().setContinueUpgrade(false); 78 return false; 79 } 80 return true; 81 } 82 83 private void doBackup(CommonInfoModel commonInfo) throws CertificateException { 84 doCACertificateBackup(); 85 doKeyPairBackup(); 86 } 87 88 private void doCACertificateBackup() throws CertificateException{ 89 copyCACertificates(trustJksPath, trustJksPath+".back"); 90 127 } 128 129 private void copyCACertificates(String sourceTrustJksPath, String targetTrustJksPath) throws CertificateException { 130 FileInputStream in = null; 131 FileOutputStream out = null; 132 try { 133 KeyStore keystoreSource = KeyStore.getInstance(KeyStore.getDefaultType()); 134 KeyStore keystoreTarget = KeyStore.getInstance(KeyStore.getDefaultType()); 135 in = new FileInputStream(new File(sourceTrustJksPath)); 136 keystoreSource.load(in,jksCAkeyStorePassword.toCharArray()); 137 keystoreTarget.load(null, jksCAkeyStorePassword.toCharArray()); 138 java.util.Enumeration en = keystoreSource.aliases(); 139 for(; en.hasMoreElements(); ){ 140 String alias = (String ) en.nextElement(); 141 java.security.cert.Certificate cert = keystoreSource.getCertificate(alias); 142 keystoreTarget.setCertificateEntry(alias,cert); 143 _logger.log(Level.INFO,sm.getString("enterprise.tools.upgrade.certconversion.ca_transferred",alias)); 144 145 } 146 out = new FileOutputStream(targetTrustJksPath); 147 keystoreTarget.store(out, jksCAkeyStorePassword.toCharArray()); 148 } catch (java.security.cert.CertificateException e) { 149 _logger.log(Level.WARNING,sm.getString("enterprise.tools.upgrade.unknownError"),e); 150 throw new CertificateException(sm.getString("enterprise.tools.upgrade.unknownError")); 151 } catch (NoSuchAlgorithmException e) { 152 _logger.log(Level.WARNING,sm.getString("enterprise.tools.upgrade.unknownError"),e); 153 throw new CertificateException(sm.getString("enterprise.tools.upgrade.unknownError")); 154 } catch (FileNotFoundException e) { 155 _logger.log(Level.WARNING,sm.getString("enterprise.tools.upgrade.unknownError"),e); 156 throw new CertificateException(sm.getString("enterprise.tools.upgrade.unknownError")); 157 } catch (KeyStoreException e) { 159 _logger.log(Level.WARNING,sm.getString("enterprise.tools.upgrade.unknownError"),e); 160 throw new CertificateException(sm.getString("enterprise.tools.upgrade.unknownError")); 161 } catch (IOException e) { 162 _logger.log(Level.WARNING,sm.getString("enterprise.tools.upgrade.unknownError"),e); 163 throw new CertificateException(sm.getString("enterprise.tools.upgrade.unknownError")); 164 }catch(Exception e) { 165 _logger.log(Level.WARNING,sm.getString("enterprise.tools.upgrade.unknownError"),e); 166 throw new CertificateException(sm.getString("enterprise.tools.upgrade.unknownError")); 167 }finally { 168 try { 169 if(in!=null) 170 in.close(); 171 if(out!=null) 172 out.close(); 173 }catch(Exception ex){} 174 } 175 } 176 177 private void doKeyPairBackup() throws CertificateException{ 178 copyKeyPairs(jksPath, jksPath+".back") ; 179 } 180 181 private void copyKeyPairs(String sourceJksPath, String targetJksPath) throws CertificateException{ 182 FileInputStream in = null; 183 FileOutputStream out = null; 184 try { 185 KeyStore keystoreSource = KeyStore.getInstance(KeyStore.getDefaultType()); 186 KeyStore keystoreTarget = KeyStore.getInstance(KeyStore.getDefaultType()); 187 in = new FileInputStream(new File(sourceJksPath)); 188 keystoreSource.load(in,jksKeyStorePassword.toCharArray()); 189 keystoreTarget.load(null, jksKeyStorePassword.toCharArray()); 190 java.util.Enumeration en = keystoreSource.aliases(); 191 for(; en.hasMoreElements(); ){ 192 String alias = (String ) en.nextElement(); 193 _logger.log(Level.INFO,sm.getString("enterprise.tools.upgrade.certconversion.processing_keypair",alias)); 194 Key key = keystoreSource.getKey(alias, jksKeyStorePassword.toCharArray()); 195 java.security.cert.Certificate [] cert = keystoreSource.getCertificateChain(alias); 196 keystoreTarget.setKeyEntry(alias, key, jksKeyStorePassword.toCharArray(), cert); 197 _logger.log(Level.INFO,sm.getString("enterprise.tools.upgrade.certconversion.alias_transferred",alias)); 198 } 199 out = new FileOutputStream(targetJksPath); 200 keystoreTarget.store(out, jksKeyStorePassword.toCharArray()); 201 } catch (java.security.cert.CertificateException e) { 202 _logger.log(Level.WARNING,sm.getString("enterprise.tools.upgrade.unknownError"),e); 203 throw new CertificateException(sm.getString("enterprise.tools.upgrade.unknownError")); 204 } catch (NoSuchAlgorithmException e) { 205 _logger.log(Level.WARNING,sm.getString("enterprise.tools.upgrade.unknownError"),e); 206 throw new CertificateException(sm.getString("enterprise.tools.upgrade.unknownError")); 207 } catch (FileNotFoundException e) { 208 _logger.log(Level.WARNING,sm.getString("enterprise.tools.upgrade.unknownError"),e); 209 throw new CertificateException(sm.getString("enterprise.tools.upgrade.unknownError")); 210 } catch (KeyStoreException e) { 212 _logger.log(Level.WARNING,sm.getString("enterprise.tools.upgrade.unknownError"),e); 213 throw new CertificateException(sm.getString("enterprise.tools.upgrade.unknownError")); 214 } catch (IOException e) { 215 _logger.log(Level.WARNING,sm.getString("enterprise.tools.upgrade.unknownError"),e); 216 throw new CertificateException(sm.getString("enterprise.tools.upgrade.unknownError")); 217 }catch(Exception e) { 218 _logger.log(Level.WARNING,sm.getString("enterprise.tools.upgrade.unknownError"),e); 219 throw new CertificateException(sm.getString("enterprise.tools.upgrade.unknownError")); 220 }finally { 221 try { 222 if(in!=null) 223 in.close(); 224 if(out!=null) 225 out.close(); 226 }catch(Exception ex){} 227 } 228 } 229 230 public void recovery(CommonInfoModel commonInfo) { 231 File keypairKeyStoreOriginalFile = new File(jksPath); 232 File keypairKeyStoreBackupFile = new File(jksPath+".back"); 233 File trustedKeyStoreOriginalFile = new File(trustJksPath); 234 File trustedKeyStoreBackupFile = new File(trustJksPath+".back"); 235 new File(jksPath+".back1").delete(); 236 new File(trustJksPath+".back1").delete(); 237 boolean success = keypairKeyStoreOriginalFile.renameTo(new File(jksPath+".back1")); 238 if(!success) { 239 _logger.log(Level.WARNING,sm.getString("enterprise.tools.upgrade.certconversion.could_not_recover_certificates")); 240 return; 241 } 242 success = trustedKeyStoreOriginalFile.renameTo(new File(trustJksPath+".back1")); 243 if(!success) { 244 _logger.log(Level.WARNING,sm.getString("enterprise.tools.upgrade.certconversion.could_not_recover_certificates")); 245 return; 246 } 247 keypairKeyStoreOriginalFile.delete(); 248 trustedKeyStoreOriginalFile.delete(); 249 success = keypairKeyStoreBackupFile.renameTo(keypairKeyStoreOriginalFile); 250 if(!success) { 251 _logger.log(Level.WARNING,sm.getString("enterprise.tools.upgrade.certconversion.could_not_recover_certificates")); 252 File keypairKeyStoreanotherBackupFile = new File(jksPath+".back1"); 253 File trustedKeyStoreanotherBackupFile = new File(trustJksPath+".back1"); 254 keypairKeyStoreanotherBackupFile.renameTo(keypairKeyStoreOriginalFile); 255 trustedKeyStoreanotherBackupFile.renameTo(trustedKeyStoreOriginalFile); 256 return; 257 } 258 success = trustedKeyStoreBackupFile.renameTo(trustedKeyStoreOriginalFile); 259 if(!success) { 260 _logger.log(Level.WARNING,sm.getString("enterprise.tools.upgrade.certconversion.could_not_recover_certificates")); 261 File keypairKeyStoreanotherBackupFile = new File(jksPath+".back1"); 262 File trustedKeyStoreanotherBackupFile = new File(trustJksPath+".back1"); 263 keypairKeyStoreanotherBackupFile.renameTo(keypairKeyStoreOriginalFile); 264 trustedKeyStoreanotherBackupFile.renameTo(trustedKeyStoreOriginalFile); 265 return; 266 } 267 new File(jksPath+".back1").delete(); 268 new File(trustJksPath+".back1").delete(); 269 } 270 271 272 public static void main(String [] args){ 273 CommonInfoModel commonInfo = new CommonInfoModel(); 274 commonInfo.setSourceInstallDir(args[0]); 275 commonInfo.setTargetInstallDir(args[1]); 276 commonInfo.setCertDbPassword(args[2]); 277 commonInfo.setJksKeystorePassword(args[3]); 278 JKStoJKSConversionModule convModule = new JKStoJKSConversionModule(); 279 convModule.upgrade(commonInfo); 280 } 281 282 public String getName() { 283 return sm.getString("enterprise.tools.upgrade.certconversion.moduleName"); 284 } 285 286 } 287 288 | Popular Tags |