1 23 24 29 30 package com.sun.enterprise.admin.servermgmt; 31 32 import com.sun.enterprise.security.store.PasswordAdapter; 33 import com.sun.enterprise.admin.jmx.remote.https.AsadminTruststore; 34 35 import com.sun.enterprise.admin.servermgmt.pe.PEFileLayout; 36 37 import com.sun.enterprise.util.io.FileUtils; 38 import com.sun.enterprise.util.i18n.StringManager; 39 import com.sun.enterprise.util.OS; 40 import com.sun.enterprise.util.SystemPropertyConstants; 41 import com.sun.enterprise.util.ProcessExecutor; 42 import com.sun.enterprise.util.ExecException; 43 import com.sun.enterprise.util.net.NetUtils; 44 45 import java.io.File ; 46 import java.io.IOException ; 47 48 51 public class KeystoreManager { 52 53 private static final String KEYTOOL_CMD = 54 System.getProperty(SystemPropertyConstants.JAVA_ROOT_PROPERTY) + "/bin/keytool"; 55 56 private static String CERTIFICATE_DN_PREFIX = "CN="; 57 58 private static String CERTIFICATE_DN_SUFFIX = 59 ",OU=Sun Java System Application Server,O=Sun Microsystems,L=Santa Clara,ST=California,C=US"; 60 61 private static String _certificateDN = null; 62 63 public static final String CERTIFICATE_ALIAS = "s1as"; 64 public static final String DEFAULT_MASTER_PASSWORD = "changeit"; 65 66 protected class KeytoolExecutor extends ProcessExecutor { 67 68 public KeytoolExecutor(String [] args, long timeoutInSeconds) 69 { 70 super(args, timeoutInSeconds); 71 setExecutionRetentionFlag(true); 72 addKeytoolCommand(); 73 } 74 75 public KeytoolExecutor(String [] args, long timeoutInSeconds, String [] inputLines) 76 { 77 super(args, timeoutInSeconds, inputLines); 78 setExecutionRetentionFlag(true); 79 addKeytoolCommand(); 80 } 81 82 protected String getExceptionMessage() 85 { 86 return getLatestOutput(mOutFile) + " " + getFileBuffer(mErrFile); 87 } 88 89 private void addKeytoolCommand() { 90 if (!mCmdStrings[0].equals(KEYTOOL_CMD)) { 91 String [] newArgs = new String [mCmdStrings.length + 1]; 92 newArgs[0] = KEYTOOL_CMD; 93 System.arraycopy(mCmdStrings, 0, newArgs, 1, mCmdStrings.length); 94 mCmdStrings = newArgs; 95 } 96 } 97 98 public void execute(String keystoreErrorMsg, File keystoreName) throws RepositoryException 99 { 100 try { 101 super.execute(); 102 if (getProcessExitValue() != 0) { 103 throw new RepositoryException(_strMgr.getString(keystoreErrorMsg, keystoreName) + 104 getLastExecutionError() + " " + getLastExecutionOutput()); 105 } 106 } catch (ExecException ex) { 107 throw new RepositoryException(_strMgr.getString(keystoreErrorMsg, 108 keystoreName) + getLastExecutionError() + " " + getLastExecutionOutput(), ex); 109 } 110 } 111 } 112 113 protected PEFileLayout _fileLayout = null; 114 115 private static final StringManager _strMgr = 116 StringManager.getManager(KeystoreManager.class); 117 118 119 public KeystoreManager() { 120 } 121 122 protected String getCertificateDN(String domainName) 123 { 124 if (_certificateDN == null) { 125 String hostName = null; 126 try { 127 hostName = NetUtils.getCanonicalHostName(); 128 } catch (Exception ex) { 129 hostName = "localhost"; 130 } 131 _certificateDN = CERTIFICATE_DN_PREFIX + hostName + CERTIFICATE_DN_SUFFIX; 132 } 133 return _certificateDN; 134 } 135 136 protected PEFileLayout getFileLayout(RepositoryConfig config) 137 { 138 if (_fileLayout == null) { 139 _fileLayout = new PEFileLayout(config); 140 } 141 return _fileLayout; 142 } 143 144 152 protected void createSSLCertificateDatabase(RepositoryConfig config, 153 String masterPassword) throws RepositoryException 154 { 155 createKeyStore(config, masterPassword); 156 createTrustStore(config, masterPassword); 157 } 158 159 165 protected void createKeyStore( 166 RepositoryConfig config, String masterPassword) throws RepositoryException 167 { 168 final PEFileLayout layout = getFileLayout(config); 170 final File keystore = layout.getKeyStore(); 171 final String [] keytoolCmd = { 173 "-genkey", 174 "-keyalg", "RSA", 175 "-keystore", keystore.getAbsolutePath(), 176 "-alias", CERTIFICATE_ALIAS, 177 "-dname", getCertificateDN(config.getDisplayName()), 178 "-validity", "3650", 179 "-keypass", masterPassword, 180 "-storepass", masterPassword 181 }; 182 183 KeytoolExecutor p = new KeytoolExecutor(keytoolCmd, 60); 184 p.execute("keystoreNotCreated", keystore); 185 } 186 187 188 protected void addToAsadminTrustStore( 189 RepositoryConfig config, File certFile) throws RepositoryException 190 { 191 boolean newTruststore = false; 192 final PEFileLayout layout = getFileLayout(config); 193 final File asadminTruststore = AsadminTruststore.getAsadminTruststore(); 195 196 if (!asadminTruststore.exists()) { 197 newTruststore = true; 198 } 199 200 String aliasName = layout.getRepositoryDir().getAbsolutePath(); 204 205 String [] keytoolCmd = new String [] { 208 "-delete", 209 "-keystore", asadminTruststore.getAbsolutePath(), 210 "-alias", aliasName, 211 }; 212 213 final String [] input = {AsadminTruststore.getAsadminTruststorePassword(), 214 AsadminTruststore.getAsadminTruststorePassword()}; KeytoolExecutor p = new KeytoolExecutor(keytoolCmd, 30, input); 216 try { 217 p.execute("trustStoreNotCreated", asadminTruststore); 218 } catch (RepositoryException ex) { 219 } 221 222 keytoolCmd = new String [] { 223 "-import", 224 "-noprompt", 225 "-keystore", asadminTruststore.getAbsolutePath(), 226 "-alias", aliasName, "-file", certFile.getAbsolutePath(), 228 }; 229 230 p = new KeytoolExecutor(keytoolCmd, 30, input); 231 p.execute("trustStoreNotCreated", asadminTruststore); 232 233 if (newTruststore) { 235 try { 236 chmod("600", asadminTruststore); 237 } catch (IOException ex) { 238 throw new RepositoryException(_strMgr.getString( 239 "trustStoreNotCreated", asadminTruststore), ex); 240 } 241 } 242 } 243 244 252 protected void createTrustStore( 253 RepositoryConfig config, String masterPassword) throws RepositoryException 254 { 255 final PEFileLayout layout = getFileLayout(config); 257 final File src = layout.getTrustStoreTemplate(); 258 final File truststore = layout.getTrustStore(); 259 File certFile = null; 260 261 try { 262 FileUtils.copy(src, truststore); 263 } catch (IOException ioe) { 264 throw new RepositoryException( 265 _strMgr.getString("trustStoreNotCreated", truststore), ioe); 266 } 267 268 try { 269 String [] input = {masterPassword}; 270 String [] keytoolCmd = null; 271 KeytoolExecutor p = null; 272 273 changeKeystorePassword(DEFAULT_MASTER_PASSWORD, masterPassword, truststore); 274 275 certFile = new File (layout.getConfigRoot(), CERTIFICATE_ALIAS + ".cer"); 277 keytoolCmd = new String [] { 278 "-export", 279 "-keystore", layout.getKeyStore().getAbsolutePath(), 280 "-alias", CERTIFICATE_ALIAS, 281 "-file", certFile.getAbsolutePath(), 282 }; 283 284 p = new KeytoolExecutor(keytoolCmd, 30, input); 285 p.execute("trustStoreNotCreated", truststore); 286 287 keytoolCmd = new String [] { 289 "-import", 290 "-noprompt", 291 "-keystore", truststore.getAbsolutePath(), 292 "-alias", CERTIFICATE_ALIAS, 293 "-file", certFile.getAbsolutePath(), 294 }; 295 296 p = new KeytoolExecutor(keytoolCmd, 30, input); 297 p.execute("trustStoreNotCreated", truststore); 298 299 addToAsadminTrustStore(config, certFile); 301 302 certFile.delete(); 304 certFile = null; 305 } finally { 306 if (certFile != null) { 307 certFile.delete(); 308 } 309 } 310 } 311 312 319 protected void changeKeystorePassword(String oldPassword, String newPassword, 320 File keystore) throws RepositoryException 321 { 322 if (!oldPassword.equals(newPassword)) { 323 String [] keytoolCmd = { 325 "-storepasswd", 326 "-keystore", keystore.getAbsolutePath(), 327 }; 328 329 KeytoolExecutor p = new KeytoolExecutor(keytoolCmd, 30, 330 new String [] {oldPassword, newPassword, newPassword}); 331 p.execute("keyStorePasswordNotChanged", keystore); 332 } 333 } 334 335 336 350 protected void changeS1ASAliasPassword(RepositoryConfig config, 351 String storePassword, String oldKeyPassword, String newKeyPassword) 352 throws RepositoryException 353 { 354 if (!storePassword.equals(oldKeyPassword) && !oldKeyPassword.equals(newKeyPassword)) { 355 final PEFileLayout layout = getFileLayout(config); 356 final File src = layout.getTrustStoreTemplate(); 357 final File keystore = layout.getKeyStore(); 358 String [] keytoolCmd = { 361 "-list", 362 "-keystore", keystore.getAbsolutePath(), 363 "-alias", CERTIFICATE_ALIAS, 364 }; 365 KeytoolExecutor p = new KeytoolExecutor(keytoolCmd, 30, 366 new String [] {storePassword}); 367 try { 368 p.execute("s1asKeyPasswordNotChanged", keystore); 369 } catch (RepositoryException ex) { 370 return; 371 } 372 373 keytoolCmd = new String [] { 375 "-keypasswd", 376 "-keystore", keystore.getAbsolutePath(), 377 "-alias", CERTIFICATE_ALIAS, 378 }; 379 p = new KeytoolExecutor(keytoolCmd, 30, 380 new String [] {storePassword, oldKeyPassword, newKeyPassword, newKeyPassword}); 381 p.execute("s1asKeyPasswordNotChanged", keystore); 382 } 383 } 384 385 395 protected void changeSSLCertificateDatabasePassword(RepositoryConfig config, 396 String oldPassword, String newPassword) throws RepositoryException 397 { 398 final PEFileLayout layout = getFileLayout(config); 399 File keystore = layout.getKeyStore(); 400 File truststore = layout.getTrustStore(); 401 402 if (keystore.exists()) { 403 changeKeystorePassword(oldPassword, newPassword, keystore); 405 try { 413 changeS1ASAliasPassword(config, newPassword, oldPassword, newPassword); 414 } catch (Exception ex) { 415 ex.printStackTrace(); 418 } 419 } 420 421 if (truststore.exists()) { 422 changeKeystorePassword(oldPassword, newPassword, truststore); 424 } 425 } 426 427 protected void chmod(String args, File file) throws IOException 428 { 429 if (OS.isUNIX()) { 430 Runtime.getRuntime().exec("/bin/chmod " + args + " " + 431 file.getAbsolutePath()); 432 } 433 } 434 } 435 | Popular Tags |