/** * Copyright 1999-2009 The Pegadi Team * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.pegadi.server; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLSocketFactory; import javax.net.ssl.TrustManager; import javax.net.ssl.TrustManagerFactory; import java.io.IOException; import java.io.InputStream; import java.io.Serializable; import java.net.Socket; import java.rmi.server.RMIClientSocketFactory; import java.security.KeyManagementException; import java.security.KeyStore; import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; import java.security.cert.CertificateException; public class RMISSLClientSocketFactory implements RMIClientSocketFactory, Serializable { private static final long serialVersionUID = 1934656691202185780L; public Socket createSocket(String host, int port) throws IOException { Logger log = LoggerFactory.getLogger(getClass()); try { TrustManager[] trustManagers = null; if (System.getProperty("javax.net.ssl.trustStore") == null) { log.info("javax.net.ssl.trustStore is not set, will look for trustStore in classpath (\"/cacerts\")"); KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); if(System.getProperty("developerMode") != null){ InputStream stream = getClass().getResourceAsStream("dummycacert"); log.info("(devmode) RMISSLClientSocketFactory.createSocket: truststore stream is " + stream); trustStore.load(stream, "changeme".toCharArray()); }else{ InputStream trustStoreStream = getClass().getResourceAsStream("cacerts"); log.info("RMISSLClientSocketFactory.createSocket: truststore stream is " + trustStoreStream); trustStore.load(trustStoreStream, "changeme".toCharArray()); } TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); tmf.init(trustStore); trustManagers = tmf.getTrustManagers(); } else { log.info("Using default trustStore: " + System.getProperty("javax.net.ssl.trustStore")); } SSLContext sslContext = SSLContext.getInstance("TLS"); sslContext.init(null, trustManagers, null); SSLSocketFactory factory = sslContext.getSocketFactory(); return factory.createSocket(host, port); } catch (NoSuchAlgorithmException e) { log.error("Exception during setup of client SSL",e); return null; } catch (KeyManagementException e) { log.error("Exception during setup of client SSL",e); return null; } catch (CertificateException e) { log.error("Exception during setup of client SSL",e); return null; } catch (KeyStoreException e) { log.error("Exception during setup of client SSL",e); return null; } } }