package com.dwave.rmi.ssl; /* * RMISSLServerSocketFactory * * Copyright (C) 2000 D-Wave Systems Inc. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ import java.io.Serializable; import java.io.IOException; import java.io.FileInputStream; import java.net.ServerSocket; import java.net.InetAddress; import java.rmi.server.RMIServerSocketFactory; import java.security.KeyStore; import javax.net.ssl.*; import javax.net.*; import com.sun.net.ssl.*; /** * RMISSLServerSocketFactory * * @author Michael Coury * @version 1.0, 10/06/2000 */ public class RMISSLServerSocketFactory implements RMIServerSocketFactory, Serializable { public ServerSocket createServerSocket(int port) throws IOException { System.setProperty("java.protocol.handler.pkgs","com.sun.net.ssl.internal.www.protocol"); java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider()); SSLServerSocketFactory ssf = null; SSLServerSocket server = null; try { SSLContext context; KeyManagerFactory keyManagerFactory; KeyStore keyStore; // this class needs to know the password to your keystore. // Replace 'yourPassword' with your password. An alternative to this // would be to rewrite some of these classes and send your password as // an argument or property. This also holds true for storePath, // certificate, and host address. Property would probably be the best char[] passphrase = "yourPassword".toCharArray(); String storePath= "/dir/subdir/your.keystore"; String sslContextType = "TLS"; String keyStoreType = "JKS"' String managerFactoryType = "SunX509"; String certificate = "someCert"; String serverURL = "someurl.com"; context = SSLContext.getInstance(sslContextType); keyStore = KeyStore.getInstance(keyStoreType); keyManagerFactory = KeyManagerFactory.getInstance(managerFactoryType); TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(managerFactoryType); keyStore.load(new FileInputStream(storePath), passphrase); System.out.println("\nSSLServer: KeyStore loaded."); java.security.cert.Certificate serverCert = keyStore.getCertificate(certificate); System.out.println("SSLServer: Loaded certificate: "+certificate); trustManagerFactory.init(keyStore); System.out.println("SSLServer: TrustManagerFactory initialized.\n\tDefault Algorithm: "+trustManagerFactory.getDefaultAlgorithm()); TrustManager[] trustManager = trustManagerFactory.getTrustManagers(); keyManagerFactory.init(keyStore, passphrase); System.out.println("SSLServer: KeyManagerFactory initialized.\n\tDefault Algorithm: "+keyManagerFactory.getDefaultAlgorithm()); KeyManager[] keyManager = keyManagerFactory.getKeyManagers(); context.init(keyManager, trustManager, null); System.out.println("SSLServer: SSLContext initialized.\n\tProtocol: "+context.getProtocol()); System.out.println("SSLServer: Retrieving SSLServerSocketFactory for SSLContext....."); ssf = (SSLServerSocketFactory) context.getServerSocketFactory(); System.out.println("SSLServer: SSLServerSocketFactory created"); System.out.println("SSLServer: Creating server socket at port "+port); server = (SSLServerSocket) ssf.createServerSocket(port, 100, InetAddress.getByName(serverURL)); String[] cipherSuites = server.getSupportedCipherSuites(); server.setEnabledCipherSuites(cipherSuites); server.setNeedClientAuth(false); server.setEnableSessionCreation(true); System.out.println("SSLServer: Server socket created: "+server.toString()); } catch (Exception e) { System.out.println("SSLServer: RMISSLServerSocketFactory ERROR"); e.printStackTrace(); } return (ServerSocket) server; } }