1 16 package org.apache.axis.components.net; 17 18 import java.io.FileInputStream ; 19 import java.io.FileNotFoundException ; 20 import java.io.IOException ; 21 import java.io.InputStream ; 22 import java.security.KeyStore ; 23 import java.security.Security ; 24 import java.util.Hashtable ; 25 26 import com.sun.net.ssl.SSLContext; 27 28 34 public class SunJSSESocketFactory extends JSSESocketFactory implements SecureSocketFactory { 35 36 37 private String keystoreType; 38 39 40 static String defaultKeystoreType = "JKS"; 41 42 43 static String defaultProtocol = "TLS"; 44 45 46 static String defaultAlgorithm = "SunX509"; 47 48 49 static boolean defaultClientAuth = false; 50 51 52 private boolean clientAuth = false; 53 54 55 static String defaultKeystoreFile = 56 System.getProperty("user.home") + "/.keystore"; 57 58 59 static String defaultKeyPass = "changeit"; 60 61 66 public SunJSSESocketFactory(Hashtable attributes) { 67 super(attributes); 68 } 69 70 75 protected void initFactory() throws IOException { 76 77 try { 78 Security.addProvider(new sun.security.provider.Sun()); 79 Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider()); 80 81 SSLContext context = getContext(); 83 sslFactory = context.getSocketFactory(); 84 } catch (Exception e) { 85 if (e instanceof IOException ) { 86 throw (IOException ) e; 87 } 88 throw new IOException (e.getMessage()); 89 } 90 } 91 92 98 protected SSLContext getContext() throws Exception { 99 100 if(attributes == null) { 101 SSLContext context = 102 com.sun.net.ssl.SSLContext.getInstance("SSL"); context.init(null, null, null); 105 return context; 106 } 107 108 String keystoreFile = (String ) attributes.get("keystore"); 111 if (keystoreFile == null) { 112 keystoreFile = defaultKeystoreFile; 113 } 114 115 keystoreType = (String ) attributes.get("keystoreType"); 116 if (keystoreType == null) { 117 keystoreType = defaultKeystoreType; 118 } 119 120 clientAuth = null != (String ) attributes.get("clientauth"); 123 String keyPass = (String ) attributes.get("keypass"); 124 if (keyPass == null) { 125 keyPass = defaultKeyPass; 126 } 127 128 String keystorePass = (String ) attributes.get("keystorePass"); 129 if (keystorePass == null) { 130 keystorePass = keyPass; 131 } 132 133 String protocol = (String ) attributes.get("protocol"); 135 if (protocol == null) { 136 protocol = defaultProtocol; 137 } 138 139 String algorithm = (String ) attributes.get("algorithm"); 141 if (algorithm == null) { 142 algorithm = defaultAlgorithm; 143 } 144 145 KeyStore kstore = initKeyStore(keystoreFile, keystorePass); 148 149 com.sun.net.ssl.KeyManagerFactory kmf = 151 com.sun.net.ssl.KeyManagerFactory.getInstance(algorithm); 152 153 kmf.init(kstore, keyPass.toCharArray()); 154 155 com.sun.net.ssl.TrustManager[] tm = null; 157 158 if (clientAuth) { 159 com.sun.net.ssl.TrustManagerFactory tmf = 160 com.sun.net.ssl.TrustManagerFactory.getInstance("SunX509"); 161 162 tmf.init(kstore); 163 tm = tmf.getTrustManagers(); 164 } 165 166 SSLContext context = 169 com.sun.net.ssl.SSLContext.getInstance(protocol); 171 context.init(kmf.getKeyManagers(), tm, 173 new java.security.SecureRandom ()); 174 return context; 175 } 176 177 186 private KeyStore initKeyStore(String keystoreFile, String keyPass) 187 throws IOException { 188 try { 189 KeyStore kstore = KeyStore.getInstance(keystoreType); 190 191 InputStream istream = new FileInputStream (keystoreFile); 192 kstore.load(istream, keyPass.toCharArray()); 193 return kstore; 194 } catch (FileNotFoundException fnfe) { 195 throw fnfe; 196 } catch (IOException ioe) { 197 throw ioe; 198 } catch (Exception ex) { 199 ex.printStackTrace(); 200 throw new IOException ("Exception trying to load keystore " 201 + keystoreFile + ": " + ex.getMessage()); 202 } 203 } 204 } 205 | Popular Tags |