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 NSStoNSSConversionModule implements BaseModule{ 42 43 private static Logger _logger = LogService.getLogger(LogService.UPGRADE_LOGGER); 44 private StringManager sm; 45 private static final String PKCS12_OUTPUTFILE_OPTION = "-o"; 46 private static final String PKCS12_INPUTFILE_OPTION = "-i"; 47 private static final String NSS_DB_LOCATION_OPTION = "-d"; 48 private static final String ALIAS_OPTION = "-n"; 49 private static final String NSS_PWD_OPTION = "-K"; 50 private static final String NSS_DB_PREFIX = "-P"; 51 private static final String KEYSTORE_PWD_OPTION = "-W"; 52 private static final String LIST_KEY_ID = "-K"; 53 private static final String CREATE_NSS_DB = "-N"; 54 private static final String CERT_NSS_PWD_OPTION = "-f"; 55 private static final String CERT_UTIL_UNIX = "certutil.sh"; 56 private static final String PK12_UTIL_UNIX = "pk12util.sh"; 57 private static final String CERT_UTIL_WIN = "certutil.bat"; 58 private static final String PK12_UTIL_WIN = "pk12util.bat"; 59 private static final String CONFIG = "config"; 60 private static final String BIN = "bin"; 61 private static final String LIB = "lib"; 62 private static final String UPGRADE = "upgrade"; 63 String pathOfNSSDbFiles; 64 private List pkcs12PathList; 65 private List keyList; 66 private CommonInfoModel commonInfo; 67 68 public NSStoNSSConversionModule(){ 69 sm = StringManager.getManager(LogService.UPGRADE_CERTCONVERSION_LOGGER); 70 } 71 72 public boolean upgrade(CommonInfoModel commonInfo){ 73 try { 74 String currentDomain = commonInfo.getCurrentDomain(); 75 String currentInstance = currentDomain + ":" + commonInfo.getCurrentSourceInstance(); 76 if(!(commonInfo.getDomainOptionList().contains(currentDomain)) || commonInfo.getCertDbPassword() == null) 77 return true; 78 79 this.pkcs12PathList=new ArrayList(); 80 this.keyList = new ArrayList(); 81 this.commonInfo = commonInfo; 82 _logger.log(Level.INFO,sm.getString("enterprise.tools.upgrade.certconversion.start_certificate_migration",currentInstance)); 83 doBackup(commonInfo); 84 listAllKeysFromSourceInstall(); 85 generatePKCS12Certificates(); 86 migratePkcs12ToNss(""); 88 deletePKCS12Files(); 89 _logger.log(Level.INFO,sm.getString("enterprise.tools.upgrade.certconversion.finished_certificate_migration",currentInstance)); 90 }catch(CertificateException ce) { 91 _logger.log(Level.WARNING,sm.getString("enterprise.tools.upgrade.certconversion.could_not_migrate_certificates",ce)); 92 UpdateProgressManager.getProgressManager().setContinueUpgrade(false); 93 return false; 94 } 95 return true; 96 } 97 98 private void doBackup(CommonInfoModel commonInfo) throws CertificateException{ 99 } 103 104 private void doCACertificateBackup() { 105 142 } 143 144 private void doKeyPairBackup() { 145 183 } 184 185 186 public void recovery(CommonInfoModel commonInfo) { 187 225 } 226 227 private void listAllKeysFromSourceInstall() throws CertificateException{ 228 String osName = commonInfo.getOSName(); 229 String sourceDomainDir = commonInfo.getSourceInstancePath(); 230 String configDir = sourceDomainDir + File.separator + CONFIG; 231 String source70Lib = commonInfo.getSourceInstallDir() +File.separator + LIB; 232 String source70Bin = commonInfo.getSourceInstallDir() +File.separator + BIN; 233 String certUtilPath = ""; 234 String certutilLocation = commonInfo.getTargetInstallDir() +File.separator + LIB +File.separator + UPGRADE; 235 if(osName.indexOf("Windows") == -1) 236 certUtilPath = commonInfo.getTargetInstallDir() + File.separator + LIB + File.separator + CERT_UTIL_UNIX; 237 else 238 certUtilPath = commonInfo.getTargetInstallDir() + File.separator + LIB + File.separator + CERT_UTIL_WIN; 239 240 StringWriter result = new StringWriter(); 241 String nssPwd = commonInfo.getCertDbPassword(); 242 String domainPath = commonInfo.getDestinationDomainPath(); 244 String nssPwdFile = domainPath +File.separator +"pwdfile"; 245 PrintWriter pw = null; 246 try{ 247 pw = new PrintWriter(new FileOutputStream(nssPwdFile )); 248 pw.println(nssPwd); 249 pw.flush(); 250 pw.close(); 251 }catch(FileNotFoundException fe) { 252 _logger.log(Level.INFO,sm.getString("enterprise.tools.upgrade.certconversion.unknownError "),fe); 253 }finally { 254 try { 255 if(pw !=null) 256 pw.close(); 257 }catch(Exception e){} 258 } 259 String commandString = certUtilPath + " " + 260 LIST_KEY_ID + " " + NSS_DB_LOCATION_OPTION + 261 " " + configDir + " " + CERT_NSS_PWD_OPTION + 262 " " + nssPwdFile + 263 " " +source70Lib + 264 " " +source70Bin + 265 " " +certutilLocation; 266 int exitVal = ProcessAdaptor.executeProcess(commandString, result); 267 result.flush(); 268 String resultString = result.toString(); 269 if(exitVal == 0) { 270 parseAndGetKeys(resultString); 271 }else { 272 _logger.log(Level.WARNING, sm.getString("enterprise.tools.upgrade.certconversion.error_reading_source_keys",resultString)); 273 throw new CertificateException(sm.getString("enterprise.tools.upgrade.certconversion.error_reading_source_keys",resultString)); 274 } 275 276 } 277 278 private void parseAndGetKeys(String input) { 279 try { 280 BufferedReader reader = new BufferedReader(new StringReader(input)); 281 String readString =reader.readLine(); 283 while(readString != null) { 284 String marker = readString.substring(0,1); 286 String anotherMarker = readString.substring(2,3); 287 if(!(marker.equals("<") && anotherMarker.equals(">"))) { 288 _logger.log(Level.WARNING, sm.getString("enterprise.tools.upgrade.certconversion.error_executing_certutil",input)); 289 return; 290 } 291 String alias = readString.substring(4); 292 keyList.add(alias); 293 _logger.log(Level.INFO,sm.getString("enterprise.tools.upgrade.certconversion.alias_info",commonInfo.getCurrentDomain(), alias)); readString =reader.readLine(); 294 } 295 }catch (Exception e) { 296 _logger.log(Level.WARNING, sm.getString("enterprise.tools.upgrade.certconversion.unknownError"),e); 297 } 298 } 299 300 private void generatePKCS12Certificates() throws CertificateException{ 301 String osName = commonInfo.getOSName(); 302 int size = keyList.size(); 303 for (int i =0;i<size;i++) { 304 String pkcsFile = removeWhiteSpace("" + keyList.get(i)); 305 String pkcsFilePath = commonInfo.getDestinationDomainPath() + File.separator +pkcsFile +".pkcs12"; 306 String sourceDomainDir = commonInfo.getSourceInstancePath(); 307 String configDir = sourceDomainDir + File.separator + CONFIG; 308 String source70Lib = commonInfo.getSourceInstallDir() +File.separator + LIB; 309 String source70Bin = commonInfo.getSourceInstallDir() +File.separator + BIN; 310 String pk12UtilPath = ""; 311 if(osName.indexOf("Windows") == -1) 312 pk12UtilPath = commonInfo.getTargetInstallDir() + File.separator + LIB + File.separator + PK12_UTIL_UNIX; 313 else 314 pk12UtilPath = commonInfo.getTargetInstallDir() + File.separator + LIB + File.separator + PK12_UTIL_WIN; 315 String pk12utilLocation = commonInfo.getTargetInstallDir() + File.separator + LIB + File.separator + UPGRADE; 316 322 String [] commandArray = {pk12UtilPath, 323 source70Lib, 324 source70Bin, 325 pk12utilLocation, 326 PKCS12_OUTPUTFILE_OPTION, pkcsFilePath, 327 NSS_DB_LOCATION_OPTION, configDir, 328 ALIAS_OPTION, ""+ keyList.get(i) + "", 329 NSS_PWD_OPTION, commonInfo.getCertDbPassword(), 330 KEYSTORE_PWD_OPTION, commonInfo.getCertDbPassword() 331 }; 332 333 StringWriter result = new StringWriter(); 334 int exitVal = ProcessAdaptor.executeProcess(commandArray, result); 336 result.flush(); 337 if(exitVal == 0) 339 pkcs12PathList.add(pkcsFilePath); 340 else { 341 _logger.log(Level.INFO,sm.getString("enterprise.tools.upgrade.certconversion.certificateError", keyList.get(i),commonInfo.getCurrentDomain(),result.toString())); 342 throw new CertificateException(sm.getString("enterprise.tools.upgrade.certconversion.certificateError", keyList.get(i),commonInfo.getCurrentDomain(),result.toString())); 343 } 344 } 345 } 346 347 private void migratePkcs12ToNss(String certPrefix) throws CertificateException{ 348 String osName = commonInfo.getOSName(); 349 int size = keyList.size(); 350 String configDir = commonInfo.getTargetConfig(); String source70Lib = commonInfo.getTargetInstallDir() +File.separator + LIB; 354 String source70Bin = commonInfo.getTargetInstallDir() +File.separator + BIN; 355 String pk12UtilPath = ""; 356 if(osName.indexOf("Windows") == -1) 357 pk12UtilPath = commonInfo.getTargetInstallDir() + File.separator + LIB + File.separator + PK12_UTIL_UNIX; 358 else 359 pk12UtilPath = commonInfo.getTargetInstallDir() + File.separator + LIB + File.separator + PK12_UTIL_WIN; 360 String pk12utilLocation = commonInfo.getTargetInstallDir() + File.separator + LIB; 362 Iterator itr = pkcs12PathList.iterator(); 363 while(itr.hasNext()) { 364 String alias = (String )itr.next(); 365 _logger.log(Level.INFO,sm.getString("enterprise.tools.upgrade.certconversion.processing_keypair",alias)); 366 String [] commandArray = {pk12UtilPath, 367 source70Lib, 368 source70Bin, 369 pk12utilLocation, 370 PKCS12_INPUTFILE_OPTION, alias, 371 NSS_DB_LOCATION_OPTION, configDir, 372 NSS_PWD_OPTION, commonInfo.getTargetCertDbPassword(), 373 KEYSTORE_PWD_OPTION, commonInfo.getCertDbPassword() 374 }; 375 376 StringWriter result = new StringWriter(); 377 int exitVal = ProcessAdaptor.executeProcess(commandArray, result); 378 result.flush(); 379 if(exitVal == 0) { 380 _logger.log(Level.INFO,sm.getString("enterprise.tools.upgrade.certconversion.alias_transferred",alias)); 381 } else { 382 _logger.log(Level.INFO,sm.getString("enterprise.tools.upgrade.certconversion.certificateError", alias,commonInfo.getCurrentDomain(),result.toString())); 383 throw new CertificateException(sm.getString("enterprise.tools.upgrade.certconversion.certificateError", alias,commonInfo.getCurrentDomain(),result.toString())); 384 } 385 } 386 387 } 389 390 private void initializeNSSDB(String certPrefix) throws CertificateException { 391 String osName = commonInfo.getOSName(); 392 String sourceDomainDir = commonInfo.getSourceInstancePath(); 393 String configDir = commonInfo.getTargetConfig(); 394 String source70Lib = commonInfo.getSourceInstallDir() +File.separator + LIB; 395 String source70Bin = commonInfo.getSourceInstallDir() +File.separator + BIN; 396 String certutilLocation = commonInfo.getTargetInstallDir() +File.separator + LIB; 397 String certUtilPath = ""; 398 if(osName.indexOf("Windows") == -1) 399 certUtilPath = commonInfo.getTargetInstallDir() + File.separator + LIB + File.separator + CERT_UTIL_UNIX; 400 else 401 certUtilPath = commonInfo.getTargetInstallDir() + File.separator + LIB + File.separator + CERT_UTIL_WIN; 402 File key3db = new File(configDir+File.separator+certPrefix+"key3.db"); 403 if(key3db.exists()) 405 return; 406 StringWriter result = new StringWriter(); 407 String nssPwd = commonInfo.getTargetCertDbPassword(); 408 String domainPath = commonInfo.getDestinationDomainPath(); 410 String nssPwdFile = domainPath +File.separator +"pwdfile"; 411 PrintWriter pw = null; 412 try{ 413 pw = new PrintWriter(new FileOutputStream(nssPwdFile )); 414 pw.println(nssPwd); 415 pw.flush(); 416 pw.close(); 417 }catch(FileNotFoundException fe) { 418 _logger.log(Level.INFO,sm.getString("enterprise.tools.upgrade.certconversion.unknownError "),fe); 419 }finally { 420 try { 421 if(pw !=null) 422 pw.close(); 423 }catch(Exception e){} 424 } 425 String commandString = certUtilPath + " " + 426 CREATE_NSS_DB + " " +NSS_DB_LOCATION_OPTION + 427 " " + configDir + " " +NSS_DB_PREFIX + 428 " "+ certPrefix + " " +CERT_NSS_PWD_OPTION + 429 " " + nssPwdFile + 430 " " +source70Lib + 431 " " +source70Bin + 432 " " +certutilLocation; 433 int exitVal = ProcessAdaptor.executeProcess(commandString, result); 434 result.flush(); 435 String resultString = result.toString(); 436 if(exitVal == 0) { 437 }else { 439 _logger.log(Level.WARNING, sm.getString("enterprise.tools.upgrade.certconversion.error_executing_certutil",resultString)); 440 throw new CertificateException(sm.getString("enterprise.tools.upgrade.certconversion.error_executing_certutil",resultString)); 441 } 442 } 443 444 private String removeWhiteSpace(String str) { 445 446 String concat=""; 447 StringTokenizer st = new StringTokenizer(str); 448 while(st.hasMoreTokens()) { 449 concat=concat+st.nextToken(); 450 } 451 return concat; 452 } 453 454 455 private void deletePKCS12Files() { 456 String pkcsFilesPath = commonInfo.getDestinationDomainPath(); 457 String [] fileList = new File(pkcsFilesPath).list(); 458 for(int i=0; i<fileList.length; i++){ 459 File pkcsFile = new File(pkcsFilesPath+File.separator+fileList[i]); 460 if(pkcsFile.isFile() && fileList[i].endsWith(".pkcs12") ){ 461 pkcsFile.delete(); 462 } 463 } 464 String domainPath = commonInfo.getDestinationDomainPath(); 465 String nssPwdFile = domainPath +File.separator +"pwdfile"; 466 File pwdfile = new File(nssPwdFile); 467 pwdfile.delete(); 468 } 469 470 public static void main(String [] args){ 471 CommonInfoModel commonInfo = new CommonInfoModel(); 472 commonInfo.setSourceInstallDir(args[0]); 473 commonInfo.setTargetInstallDir(args[1]); 474 commonInfo.setCertDbPassword(args[2]); 475 commonInfo.setJksKeystorePassword(args[3]); 476 NSStoJKSConversionModule convModule = new NSStoJKSConversionModule(); 477 convModule.upgrade(commonInfo); 478 } 479 480 public String getName() { 481 return sm.getString("enterprise.tools.upgrade.certconversion.moduleName"); 482 } 483 484 } 485 486 | Popular Tags |