1 23 24 package com.sun.enterprise.tools.upgrade.common; 25 26 import com.sun.enterprise.tools.upgrade.certconversion.ProcessAdaptor; 27 28 import java.io.*; 29 import java.security.KeyStore ; 30 import java.util.logging.Level ; 31 32 37 38 public class PasswordVerifier { 39 private static final String NSS_DB_LOCATION_OPTION = "-d"; 40 private static final String LIST_KEY_ID = "-K"; 41 private static final String CERT_NSS_PWD_OPTION = "-f"; 42 private static final String CERT_UTIL_UNIX = "certutil.sh"; 43 private static final String CERT_UTIL_WIN = "certutil.bat"; 44 private static final String CONFIG = "config"; 45 private static final String BIN = "bin"; 46 private static final String LIB = "lib"; 47 private static final String UPGRADE = "upgrade"; 48 49 public static boolean verifySourceNSSPassword(CommonInfoModel commonInfo,String configDir) { 50 if(commonInfo.getSourceDomainRootFlag()&& (new File(commonInfo.getSourceDomainRoot()).equals(new File(commonInfo.getTargetDomainRoot())))) 51 return true; 52 if(!(new File(configDir).exists())) 53 return true; 54 String osName = commonInfo.getOSName(); 55 String libDir = ""; 56 String binDir = ""; 57 String certutilLocation = ""; 58 if(commonInfo.getSourceVersion().equals(UpgradeConstants.VERSION_7X)){ 59 libDir = commonInfo.getSourceInstallDir() +File.separator + LIB; 60 binDir = commonInfo.getSourceInstallDir() +File.separator + BIN; 61 certutilLocation = commonInfo.getTargetInstallDir() +File.separator + LIB + File.separator + UPGRADE; 63 } else { 64 libDir = commonInfo.getTargetInstallDir() +File.separator + LIB; 65 binDir = commonInfo.getTargetInstallDir() +File.separator + BIN; 66 certutilLocation = commonInfo.getTargetInstallDir() +File.separator + LIB; 67 } 68 String certUtilPath = ""; 69 if(osName.indexOf("Windows") == -1) 70 certUtilPath = commonInfo.getTargetInstallDir() + File.separator + LIB + File.separator + CERT_UTIL_UNIX; 71 else 72 certUtilPath = commonInfo.getTargetInstallDir() + File.separator + LIB + File.separator + CERT_UTIL_WIN; 73 74 StringWriter result = new StringWriter(); 75 String nssPwd = commonInfo.getCertDbPassword(); 76 String domainRoot = commonInfo.getTargetDomainRoot(); 77 String nssPwdFile = domainRoot +File.separator +"pwdfile"; 79 PrintWriter pw = null; 80 try{ 81 pw = new PrintWriter(new FileOutputStream(nssPwdFile )); 82 pw.println(nssPwd); 83 pw.flush(); 84 pw.close(); 85 }catch(FileNotFoundException fe) { 86 }finally { 87 try { 88 if(pw !=null) 89 pw.close(); 90 }catch(Exception e){} 91 } 92 String commandString = certUtilPath + " " + 93 LIST_KEY_ID + " " + NSS_DB_LOCATION_OPTION + 94 " " + configDir + " " + CERT_NSS_PWD_OPTION + 95 " " + nssPwdFile + 96 " " +libDir + 97 " " +binDir + 98 " " +certutilLocation; 99 int exitVal = ProcessAdaptor.executeProcess(commandString, result); 100 result.flush(); 101 String resultString = result.toString(); 102 File pwdfile = new File(nssPwdFile); 103 pwdfile.delete(); 104 if(exitVal == 0) { 105 return parseAndVerify(resultString); 106 } else if(exitVal == 255) { return true; 108 } else { 109 return false; 110 } 111 112 } 113 114 public static boolean verifyTargetNSSPassword(CommonInfoModel commonInfo,String configDir) { 115 if(commonInfo.getSourceDomainRootFlag()&& (new File(commonInfo.getSourceDomainRoot()).equals(new File(commonInfo.getTargetDomainRoot())))) 116 return true; 117 if(!(new File(configDir).exists())) 118 return true; 119 String osName = commonInfo.getOSName(); 120 String libDir = ""; 121 String binDir = ""; 122 libDir = commonInfo.getTargetInstallDir() +File.separator + LIB; 123 binDir = commonInfo.getTargetInstallDir() +File.separator + BIN; 124 String certutilLocation = libDir; 125 String certUtilPath = ""; 126 if(osName.indexOf("Windows") == -1) 127 certUtilPath = commonInfo.getTargetInstallDir() + File.separator + LIB + File.separator + CERT_UTIL_UNIX; 128 else 129 certUtilPath = commonInfo.getTargetInstallDir() + File.separator + LIB + File.separator + CERT_UTIL_WIN; 130 131 StringWriter result = new StringWriter(); 132 String nssPwd = commonInfo.getTargetCertDbPassword(); 133 String domainRoot = commonInfo.getTargetDomainRoot(); 134 String nssPwdFile = domainRoot +File.separator +"pwdfile"; 136 PrintWriter pw = null; 137 try{ 138 pw = new PrintWriter(new FileOutputStream(nssPwdFile )); 139 pw.println(nssPwd); 140 pw.flush(); 141 pw.close(); 142 }catch(FileNotFoundException fe) { 143 }finally { 144 try { 145 if(pw !=null) 146 pw.close(); 147 }catch(Exception e){} 148 } 149 String commandString = certUtilPath + " " + 150 LIST_KEY_ID + " " + NSS_DB_LOCATION_OPTION + 151 " " + configDir + " " + CERT_NSS_PWD_OPTION + 152 " " + nssPwdFile + 153 " " +libDir + 154 " " +binDir + 155 " " +certutilLocation; 156 int exitVal = ProcessAdaptor.executeProcess(commandString, result); 157 result.flush(); 158 String resultString = result.toString(); 159 File pwdfile = new File(nssPwdFile); 160 pwdfile.delete(); 161 if(exitVal == 0) { 162 return parseAndVerify(resultString); 163 }else { 164 return false; 165 } 166 167 } 168 169 public static boolean verifySourceNSSPassword(CommonInfoModel commonInfo) { 170 if(commonInfo.getSourceDomainRootFlag()&& (new File(commonInfo.getSourceDomainRoot()).equals(new File(commonInfo.getTargetDomainRoot())))) 171 return true; 172 186 String instanceName = commonInfo.getCurrentSourceInstance(); 187 String certificateDomainDir = ""; 188 if((commonInfo.getSourceVersionAndEdition().equals(UpgradeConstants.VERSION_AS80_PE) || 189 commonInfo.getSourceVersionAndEdition().equals(UpgradeConstants.VERSION_AS81_PE) || 190 commonInfo.getSourceVersionAndEdition().equals(UpgradeConstants.VERSION_AS90_PE))&& 191 (commonInfo.getTargetVersionAndEdition().equals(UpgradeConstants.VERSION_AS90_SE) || 192 commonInfo.getTargetVersionAndEdition().equals(UpgradeConstants.VERSION_AS90_EE))) 193 certificateDomainDir = commonInfo.getDestinationDomainPath(); 194 else 195 certificateDomainDir = commonInfo.getSourceInstancePath(); 196 String configDir = certificateDomainDir + File.separator + CONFIG; 197 return verifySourceNSSPassword(commonInfo,configDir); 198 } 199 200 private static boolean parseAndVerify(String input) { 201 try { 202 BufferedReader reader = new BufferedReader(new StringReader(input)); 203 String readString =reader.readLine(); 205 while(readString != null) { 206 String marker = readString.substring(0,1); 208 String anotherMarker = readString.substring(2,3); 209 if(!(marker.equals("<") && anotherMarker.equals(">"))) { 210 return false; 211 } 212 readString =reader.readLine(); 213 } 214 }catch (Exception e) { 215 return false; 216 } 217 return true; 218 } 219 220 public static boolean verifyKeystorePassword(String jksPath, String jksKeyStorePassword ){ 221 File jksfile = new File(jksPath); 224 if(!jksfile.exists()) { 225 if(jksKeyStorePassword.equals("changeit")) 226 return true; 227 else 228 return false; 229 } 230 InputStream inputStreamJks = null; 231 KeyStore jksKeyStore; 232 try{ 233 inputStreamJks = new FileInputStream(jksPath); 234 jksKeyStore = KeyStore.getInstance("JKS"); 235 jksKeyStore.load(inputStreamJks, jksKeyStorePassword.toCharArray()); 236 }catch(Exception e){ 237 return false; 238 }finally{ 239 if(inputStreamJks!=null) 240 try{inputStreamJks.close();}catch(Exception e){} 241 } 242 return true; 243 } 244 } 245 | Popular Tags |