1 23 24 29 30 package com.sun.enterprise.tools.upgrade.certconversion; 31 32 import com.sun.enterprise.tools.upgrade.common.BaseModule; 33 import com.sun.enterprise.tools.upgrade.common.CommonInfoModel; 34 import com.sun.enterprise.tools.upgrade.common.UpgradeConstants; 35 import com.sun.enterprise.tools.upgrade.common.UpgradeUtils; 36 import com.sun.enterprise.util.i18n.StringManager; 37 import java.io.File ; 38 import java.io.IOException ; 39 import java.util.Enumeration ; 40 import java.util.Vector ; 41 import java.util.logging.Level ; 42 import java.util.logging.Logger ; 43 44 52 public class CertificateTransfer implements BaseModule { 53 54 private StringManager stringManager = StringManager.getManager("com.sun.enterprise.tools.upgrade.certconversion"); 55 private Logger logger = CommonInfoModel.getDefaultLogger(); 56 private Vector recoveryList = new Vector (); 57 private UpgradeUtils utils; 58 private String JAVA_HOME; 59 private CommonInfoModel cim; 60 private String targetJksPath; 61 private String sourceJksPath; 62 private String targetCaJksPath; 63 private String sourceCaJksPath; 64 private String targetNssPath; 65 private String sourceNssPath; 66 private String targetCaNssPath; 67 private String sourceCaNssPath; 68 69 private static String JKS_CERTS = "keystore.jks"; 70 private static String CA_JKS_CERTS = "cacerts.jks"; 71 private static String NSS_CERTS = "key3.db"; 72 private static String CA_NSS_CERTS = "cert8.db"; 73 private static String CA_70_CERTS = "cert7.db"; 74 75 76 public CertificateTransfer() { 77 JAVA_HOME = System.getProperty("com.sun.aas.java.home"); 78 } 79 80 public String getName() { 81 return stringManager.getString("enterprise.tools.upgrade.certconversion.moduleName"); 82 } 83 84 public boolean upgrade(CommonInfoModel cmi) { 85 cim = cmi; 86 logger.log(Level.INFO, stringManager.getString("enterprise.tools.upgrade.certconversion.start_certificate_migration",cmi.getCurrentDomain())); 87 targetJksPath = cim.getTargetJKSKeyStorePath(); 88 sourceJksPath = cim.getSourceJKSKeyStorePath(); 89 targetCaJksPath = cim.getTargetTrustedJKSKeyStorePath(); 90 sourceCaJksPath = cim.getSourceTrustedJKSKeyStorePath(); 91 String sourceConfigPath = cim.getSourceDomainPath()+ File.separator + "config"; 92 String targetConfigPath = cim.getTargetConfig(); 93 targetNssPath = targetConfigPath + File.separator + NSS_CERTS; 94 sourceNssPath = sourceConfigPath + File.separator + NSS_CERTS; 95 targetCaNssPath = targetConfigPath + File.separator + CA_NSS_CERTS; 96 if (cim.getSourceVersionAndEdition().equals(UpgradeConstants.VERSION_AS7X_PE)) { 97 sourceCaNssPath = sourceConfigPath + File.separator + CA_70_CERTS; 98 } else { 99 sourceCaNssPath = sourceConfigPath + File.separator + CA_NSS_CERTS; 100 } 101 utils = UpgradeUtils.getUpgradeUtils(cim); 102 String tv = cim.getTargetVersionAndEdition(); 103 String sv = cim.getSourceVersionAndEdition(); 104 105 if(sv.equals(UpgradeConstants.VERSION_AS80_PE) || sv.equals(UpgradeConstants.VERSION_AS81_PE)) { 107 if(tv.equals(UpgradeConstants.VERSION_AS90_PE)) { 108 return jksToJks(); 109 } else if ( tv.equals(UpgradeConstants.VERSION_AS90_EE) ){ 110 return jksToNss(); 111 } 112 } 113 if(cim.getSourceVersion().equals(UpgradeConstants.VERSION_7X) || 115 sv.equals(UpgradeConstants.VERSION_AS81_EE)) { 116 if(tv.equals(UpgradeConstants.VERSION_AS90_PE)) { 117 return nssToJks(); 118 } else if ( tv.equals(UpgradeConstants.VERSION_AS90_EE) ){ 119 return nssToNss(); 120 } 121 } 122 return false; 123 } 124 125 139 private void configureJks() { 140 141 String securityFile = JAVA_HOME + File.separator + "lib" + File.separator + "security" + File.separator + "java.security"; 143 File security = getSecurityFile(); 144 if(!security.exists()){ 145 logger.warning(stringManager.getString("enterprise.tools.upgrade.certconversion.errorConfiguringJKS")); 146 return; 147 } 148 149 151 } 152 153 172 173 private void configureNss() { 174 File security = getSecurityFile(); 175 if(!security.exists()){ 176 logger.warning(stringManager.getString("enterprise.tools.upgrade.certconversion.errorConfiguringNSS")); 177 return; 178 } 179 181 } 182 183 185 private boolean jksToNss() { 186 configureJks(); 187 return copyJksCerts(); 188 } 189 190 192 private boolean nssToJks() { 193 configureNss(); 194 return copyNssCerts(); 195 196 } 197 198 private boolean jksToJks() { 199 backupJksCerts(); 200 return copyJksCerts(); 201 } 202 203 private boolean nssToNss() { 204 backupNssCerts(); 205 return copyNssCerts(); 206 } 207 208 private boolean copyJksCerts() { 209 try { 210 utils.copyFile(sourceJksPath, targetJksPath); 211 utils.copyFile(sourceCaJksPath, targetCaJksPath); 212 } catch (Exception e) { 213 logger.log(Level.SEVERE, stringManager.getString("enterprise.tools.upgrade.certconversion.could_not_migrate_certificates",e)); 214 return false; 215 } 216 return true; 217 } 218 219 private boolean backupJksCerts() { 220 try { 221 backup(targetJksPath); 222 backup(targetCaJksPath); 223 } catch (Exception e) { 224 logger.log(Level.SEVERE, stringManager.getString("enterprise.tools.upgrade.certconversion.could_not_migrate_certificates",e)); 225 return false; 226 } 227 return true; 228 } 229 230 private boolean copyNssCerts() { 231 232 try { 233 utils.copyFile(sourceNssPath, targetNssPath); 234 utils.copyFile(sourceCaNssPath, targetCaNssPath); 235 } catch (Exception e) { 236 logger.log(Level.SEVERE, stringManager.getString("enterprise.tools.upgrade.certconversion.could_not_migrate_certificates",e)); 237 return false; 238 } 239 return true; 240 } 241 242 private boolean backupNssCerts() { 243 try { 244 backup(targetNssPath); 245 backup(targetCaNssPath); 246 } catch (Exception e) { 247 logger.log(Level.SEVERE, stringManager.getString("enterprise.tools.upgrade.certconversion.could_not_migrate_certificates",e)); 248 return false; 249 } 250 return true; 251 } 252 253 private void backup(String filePath) throws IOException { 254 String backupFilePath = filePath + ".bak"; 255 utils.copyFile(filePath, backupFilePath); 256 recoveryList.add(filePath); 257 } 258 259 public void recovery(CommonInfoModel commonInfo) { 260 Enumeration e = recoveryList.elements(); 261 while(e.hasMoreElements()){ 262 String recoverPath = (String )e.nextElement(); 263 String backupPath = recoverPath + ".bak"; 264 try { 265 utils.copyFile(backupPath, recoverPath); 266 new File (backupPath).delete(); 267 } catch (IOException ioe) { 268 logger.log(Level.SEVERE, stringManager.getString("enterprise.tools.upgrade.certconversion.could_not_migrate_certificates",ioe.getMessage()),new Object []{recoverPath,ioe}); 269 } 270 } 271 } 272 273 274 private File getSecurityFile(){ 275 String securityFile = JAVA_HOME + File.separator + "lib" + File.separator + "security" + File.separator + "java.security"; 276 return new File (securityFile); 277 } 278 } 279 | Popular Tags |