1 25 26 package org.ofbiz.service.rmi.socket.ssl; 27 28 import java.io.FileInputStream ; 29 import java.io.IOException ; 30 import java.io.Serializable ; 31 import java.net.ServerSocket ; 32 import java.rmi.server.RMIServerSocketFactory ; 33 import java.security.GeneralSecurityException ; 34 import java.security.KeyStore ; 35 import java.security.KeyStoreException ; 36 import java.security.NoSuchAlgorithmException ; 37 import java.security.cert.CertificateException ; 38 import javax.net.ssl.SSLServerSocket; 39 40 import org.ofbiz.base.util.Debug; 41 import org.ofbiz.base.util.SSLUtil; 42 import org.ofbiz.base.util.UtilProperties; 43 44 51 public class SSLServerSocketFactory implements RMIServerSocketFactory , Serializable { 52 53 public static final String module = SSLServerSocketFactory.class.getName(); 54 protected boolean clientAuth = false; 55 56 public void setNeedClientAuth(boolean clientAuth) { 57 this.clientAuth = clientAuth; 58 } 59 60 public ServerSocket createServerSocket(int port) throws IOException { 61 String storeType = UtilProperties.getPropertyValue("jsse.properties", "ofbiz.rmi.keyStore.type", "jks"); 62 String storeFile = UtilProperties.getPropertyValue("jsse.properties", "ofbiz.rmi.keyStore", null); 63 String storeAlias = UtilProperties.getPropertyValue("jsse.properties", "ofbiz.rmi.keyStore.alias", null); 64 String storePass = UtilProperties.getPropertyValue("jsse.properties", "ofbiz.rmi.keyStore.password", null); 65 char[] passphrase = null; 66 if (storePass != null) { 67 passphrase = storePass.toCharArray(); 68 } 69 70 KeyStore ks = null; 71 try { 72 ks = KeyStore.getInstance(storeType); 73 ks.load(new FileInputStream (storeFile), passphrase); 74 } catch (NoSuchAlgorithmException e) { 75 Debug.logError(e, module); 76 throw new IOException (e.getMessage()); 77 } catch (CertificateException e) { 78 Debug.logError(e, module); 79 throw new IOException (e.getMessage()); 80 } catch (KeyStoreException e) { 81 Debug.logError(e, module); 82 throw new IOException (e.getMessage()); 83 } 84 85 if (ks == null) { 86 throw new IOException ("Unable to load KeyStore containing Service Engine RMI SSL certificate"); 87 } 88 89 90 javax.net.ssl.SSLServerSocketFactory factory = null; 91 try { 92 factory = SSLUtil.getSSLServerSocketFactory(ks, storePass, storeAlias); 93 } catch (GeneralSecurityException e) { 94 Debug.logError(e, "Error getting javax.net.ssl.SSLServerSocketFactory instance for Service Engine RMI calls: " + e.toString(), module); 95 throw new IOException (e.toString()); 96 } 97 98 if (factory == null) { 99 throw new IOException ("Unable to obtain SSLServerSocketFactory for provided KeyStore"); 100 } 101 102 SSLServerSocket socket = (SSLServerSocket) factory.createServerSocket(port); 103 socket.setNeedClientAuth(clientAuth); 104 return socket; 105 } 106 } 107 | Popular Tags |