1 25 package org.ofbiz.base.util; 26 27 import java.io.IOException ; 28 import java.security.GeneralSecurityException ; 29 import java.security.KeyStore ; 30 31 import javax.net.ssl.*; 32 33 40 public class SSLUtil { 41 42 public static final String module = SSLUtil.class.getName(); 43 private static boolean loadedProps = false; 44 45 static { 46 SSLUtil. loadJsseProperties(); 47 } 48 49 public static KeyManager[] getKeyManagers(KeyStore ks, String password, String alias) throws GeneralSecurityException { 50 KeyManagerFactory factory = KeyManagerFactory.getInstance("SunX509"); 51 factory.init(ks, password.toCharArray()); 52 KeyManager[] keyManagers = factory.getKeyManagers(); 53 if (alias != null) { 54 for (int i = 0; i < keyManagers.length; i++) { 55 if (keyManagers[i] instanceof X509KeyManager) { 56 keyManagers[i] = new AliasKeyManager((X509KeyManager)keyManagers[i], alias); 57 } 58 } 59 } 60 return keyManagers; 61 } 62 63 public static TrustManager[] getTrustManagers(KeyStore ks) throws GeneralSecurityException { 64 TrustManagerFactory factory = TrustManagerFactory.getInstance("SunX509"); 65 factory.init(ks); 66 return factory.getTrustManagers(); 67 } 68 69 public static SSLSocketFactory getSSLSocketFactory(KeyStore ks, String password, String alias) throws IOException , GeneralSecurityException { 70 KeyStore trustStore = KeyStoreUtil.getTrustStore(); 71 TrustManager[] tm = getTrustManagers(trustStore); 72 KeyManager[] km = getKeyManagers(ks, password, alias); 73 74 SSLContext context = SSLContext.getInstance("SSL"); 75 context.init(km, tm, null); 76 return context.getSocketFactory(); 77 } 78 79 public static SSLSocketFactory getSSLSocketFactory(String alias) throws IOException , GeneralSecurityException { 80 return getSSLSocketFactory(KeyStoreUtil.getKeyStore(), KeyStoreUtil.getKeyStorePassword(), alias); 81 } 82 83 public static SSLSocketFactory getSSLSocketFactory() throws IOException , GeneralSecurityException { 84 return getSSLSocketFactory(null); 85 } 86 87 public static SSLServerSocketFactory getSSLServerSocketFactory(KeyStore ks, String password, String alias) throws IOException , GeneralSecurityException { 88 KeyStore trustStore = KeyStoreUtil.getTrustStore(); 89 TrustManager[] tm = getTrustManagers(trustStore); 90 KeyManager[] km = getKeyManagers(ks, password, alias); 91 92 SSLContext context = SSLContext.getInstance("SSL"); 93 context.init(km, tm, null); 94 return context.getServerSocketFactory(); 95 } 96 97 public static void loadJsseProperties() { 98 loadJsseProperties(false); 99 } 100 101 public static synchronized void loadJsseProperties(boolean debug) { 102 if (!loadedProps) { 103 String protocol = UtilProperties.getPropertyValue("jsse.properties", "java.protocol.handler.pkgs", "NONE"); 104 String proxyHost = UtilProperties.getPropertyValue("jsse.properties", "https.proxyHost", "NONE"); 105 String proxyPort = UtilProperties.getPropertyValue("jsse.properties", "https.proxyPort", "NONE"); 106 String cypher = UtilProperties.getPropertyValue("jsse.properties", "https.cipherSuites", "NONE"); 107 if (protocol != null && !protocol.equals("NONE")) { 108 System.setProperty("java.protocol.handler.pkgs", protocol); 109 } 110 if (proxyHost != null && !proxyHost.equals("NONE")) { 111 System.setProperty("https.proxyHost", proxyHost); 112 } 113 if (proxyPort != null && !proxyPort.equals("NONE")) { 114 System.setProperty("https.proxyPort", proxyPort); 115 } 116 if (cypher != null && !cypher.equals("NONE")) { 117 System.setProperty("https.cipherSuites", cypher); 118 } 119 120 System.setProperty("javax.net.ssl.keyStore", KeyStoreUtil.getKeyStoreFileName()); 122 System.setProperty("javax.net.ssl.keyStorePassword", KeyStoreUtil.getKeyStorePassword()); 123 System.setProperty("javax.net.ssl.trustStore", KeyStoreUtil.getTrustStoreFileName()); 124 System.setProperty("javax.net.ssl.trustStorePassword", KeyStoreUtil.getTrustStorePassword()); 125 if (debug) { 126 System.setProperty("javax.net.debug","ssl:handshake"); 127 } 128 loadedProps = true; 129 } 130 } 131 } 132 | Popular Tags |