1 11 12 package org.jivesoftware.messenger.net; 13 14 import org.jivesoftware.util.JiveGlobals; 15 import org.jivesoftware.util.Log; 16 17 import java.io.File ; 18 import java.io.FileInputStream ; 19 import java.io.FileOutputStream ; 20 import java.io.IOException ; 21 import java.net.InetAddress ; 22 import java.net.ServerSocket ; 23 import java.security.KeyStore ; 24 25 30 public class SSLConfig { 31 32 private static SSLJiveServerSocketFactory sslFactory; 33 private static KeyStore keyStore; 34 private static String keypass; 35 private static KeyStore trustStore; 36 private static String trustpass; 37 private static String keyStoreLocation; 38 private static String trustStoreLocation; 39 40 private SSLConfig() { 41 } 42 43 static { 44 String algorithm = JiveGlobals.getProperty("xmpp.socket.ssl.algorithm", "TLS"); 45 String storeType = JiveGlobals.getProperty("xmpp.socket.ssl.storeType", "jks"); 46 47 keyStoreLocation = JiveGlobals.getProperty("xmpp.socket.ssl.keystore", 49 "resources" + File.separator + "security" + File.separator + "keystore"); 50 keyStoreLocation = JiveGlobals.getHomeDirectory() + File.separator + keyStoreLocation; 51 52 keypass = JiveGlobals.getProperty("xmpp.socket.ssl.keypass", "changeit"); 54 keypass = keypass.trim(); 55 56 trustStoreLocation = JiveGlobals.getProperty("xmpp.socket.ssl.truststore", 58 "resources" + File.separator + "security" + File.separator + "truststore"); 59 trustStoreLocation = JiveGlobals.getHomeDirectory() + File.separator + trustStoreLocation; 60 61 trustpass = JiveGlobals.getProperty("xmpp.socket.ssl.trustpass", "changeit"); 63 trustpass = trustpass.trim(); 64 65 try { 66 keyStore = KeyStore.getInstance(storeType); 67 keyStore.load(new FileInputStream (keyStoreLocation), keypass.toCharArray()); 68 69 trustStore = KeyStore.getInstance(storeType); 70 trustStore.load(new FileInputStream (trustStoreLocation), trustpass.toCharArray()); 71 72 sslFactory = (SSLJiveServerSocketFactory)SSLJiveServerSocketFactory.getInstance( 73 algorithm, keyStore, trustStore); 74 } 75 catch (Exception e) { 76 Log.error("SSLConfig startup problem.\n" + 77 " storeType: [" + storeType + "]\n" + 78 " keyStoreLocation: [" + keyStoreLocation + "]\n" + 79 " keypass: [" + keypass + "]\n" + 80 " trustStoreLocation: [" + trustStoreLocation+ "]\n" + 81 " trustpass: [" + trustpass + "]", e); 82 keyStore = null; 83 trustStore = null; 84 sslFactory = null; 85 } 86 } 87 88 public static String getKeyPassword() { 89 return keypass; 90 } 91 92 public static String getTrustPassword() { 93 return trustpass; 94 } 95 96 public static String [] getDefaultCipherSuites() { 97 String [] suites; 98 if (sslFactory == null) { 99 suites = new String []{}; 100 } 101 else { 102 suites = sslFactory.getDefaultCipherSuites(); 103 } 104 return suites; 105 } 106 107 public static String [] getSpportedCipherSuites() { 108 String [] suites; 109 if (sslFactory == null) { 110 suites = new String []{}; 111 } 112 else { 113 suites = sslFactory.getSupportedCipherSuites(); 114 } 115 return suites; 116 } 117 118 public static KeyStore getKeyStore() throws IOException { 119 if (keyStore == null) { 120 throw new IOException (); 121 } 122 return keyStore; 123 } 124 125 public static KeyStore getTrustStore() throws IOException { 126 if (trustStore == null) { 127 throw new IOException (); 128 } 129 return trustStore; 130 } 131 132 public static void saveStores() throws IOException { 133 try { 134 keyStore.store(new FileOutputStream (keyStoreLocation), keypass.toCharArray()); 135 trustStore.store(new FileOutputStream (trustStoreLocation), trustpass.toCharArray()); 136 } 137 catch (IOException e) { 138 throw e; 139 } 140 catch (Exception e) { 141 throw new IOException (e.getMessage()); 142 } 143 } 144 145 public static ServerSocket createServerSocket(int port, InetAddress ifAddress) throws 146 IOException { 147 if (sslFactory == null) { 148 throw new IOException (); 149 } 150 else { 151 return sslFactory.createServerSocket(port, -1, ifAddress); 152 } 153 } 154 } | Popular Tags |