/************************************************************************* * * * SignServer: The OpenSource Automated Signing Server * * * * This software is free software; you can redistribute it and/or * * modify it under the terms of the GNU Lesser General Public * * License as published by the Free Software Foundation; either * * version 2.1 of the License, or any later version. * * * * See terms of license at gnu.org. * * * *************************************************************************/ package org.signserver.testutils; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.security.KeyManagementException; import java.security.KeyStore; import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.security.UnrecoverableKeyException; import java.security.cert.CertificateException; import java.util.Properties; import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.KeyManager; import javax.net.ssl.KeyManagerFactory; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLSocketFactory; import javax.net.ssl.TrustManagerFactory; import javax.net.ssl.X509KeyManager; import org.apache.log4j.Logger; import org.signserver.client.cli.defaultimpl.AliasKeyManager; /** * Class containing utility methods used to simplify testing. * * @author Philip Vendil 21 okt 2007 * @version $Id: TestUtils.java 3470 2013-05-03 22:12:25Z netmackan $ */ public class TestUtils { /** Logger for this class. */ private static final Logger LOG = Logger.getLogger(TestUtils.class); private Properties buildConfig; public void setupSSLTruststore() { // This does not work on JDK 7 / GlassFish 3 // System.setProperty("javax.net.ssl.trustStore", trustStore); // System.setProperty("javax.net.ssl.trustStorePassword", // getTrustStorePassword()); //System.setProperty("javax.net.ssl.keyStore", "../../p12/testadmin.jks"); //System.setProperty("javax.net.ssl.keyStorePassword", "foo123"); // Instead set the socket factory try { KeyStore truststore = loadKeyStore(getTruststoreFile(), getTrustStorePassword()); setDefaultSocketFactory(truststore, null, null, null); } catch (Exception ex) { throw new RuntimeException("Setup SSL truststore failed", ex); } } private static KeyStore loadKeyStore(final File truststoreFile, final String truststorePassword) throws KeyStoreException, FileNotFoundException, IOException, NoSuchAlgorithmException, CertificateException { KeyStore keystore = KeyStore.getInstance("JKS"); keystore.load(new FileInputStream(truststoreFile), truststorePassword.toCharArray()); return keystore; } private static void setDefaultSocketFactory(final KeyStore truststore, final KeyStore keystore, String keyAlias, char[] keystorePassword) throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException, UnrecoverableKeyException { final TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509"); tmf.init(truststore); final KeyManager[] keyManagers; if (keystore == null) { keyManagers = null; } else { if (keyAlias == null) { keyAlias = keystore.aliases().nextElement(); } final KeyManagerFactory kKeyManagerFactory = KeyManagerFactory.getInstance("SunX509"); kKeyManagerFactory.init(keystore, keystorePassword); keyManagers = kKeyManagerFactory.getKeyManagers(); for (int i = 0; i < keyManagers.length; i++) { if (keyManagers[i] instanceof X509KeyManager) { keyManagers[i] = new AliasKeyManager( (X509KeyManager) keyManagers[i], keyAlias); } } } final SSLContext context = SSLContext.getInstance("TLS"); context.init(keyManagers, tmf.getTrustManagers(), new SecureRandom()); SSLSocketFactory factory = context.getSocketFactory(); HttpsURLConnection.setDefaultSSLSocketFactory(factory); } private Properties getBuildConfig() { if (buildConfig == null) { buildConfig = new Properties(); File confFile1 = new File("../../signserver_build.properties"); File confFile2 = new File("../../conf/signserver_build.properties"); try { if (confFile1.exists()) { buildConfig.load(new FileInputStream(confFile1)); } else { buildConfig.load(new FileInputStream(confFile2)); } } catch (FileNotFoundException ignored) { LOG.debug("No signserver_build.properties"); } catch (IOException ex) { LOG.error("Not using signserver_build.properties: " + ex.getMessage()); } } return buildConfig; } public File getTruststoreFile() { return new File(System.getenv("SIGNSERVER_HOME"), "p12/truststore.jks"); } public String getTrustStorePassword() { return getBuildConfig().getProperty("java.trustpassword", "changeit"); } }