1 package com.ca.commons.security; 2 3 6 7 import java.io.*; 8 import java.net.*; 9 import javax.net.SocketFactory; 10 import javax.net.ssl.SSLSocketFactory; 11 12 import java.security.*; 13 import java.awt.*; 14 15 import javax.net.ssl.*; 17 18 36 37 public class JXSSLSocketFactory extends SSLSocketFactory 38 { 39 40 44 45 private static SSLSocketFactory factory = null; 46 47 52 53 private static JXSSLSocketFactory default_factory = null; 54 55 56 private static KeyStore clientKeystore; 57 58 62 63 private static final String DEFAULT_KEYSTORE_TYPE = "JKS"; 64 65 69 70 private static ClassLoader myClassLoader = null; 71 72 73 77 78 79 80 public static void setClassLoader(ClassLoader newLoader) 81 { 82 myClassLoader = newLoader; 83 } 84 85 90 91 private static ClassLoader getClassLoader() 92 { 93 if (myClassLoader == null) 94 myClassLoader = ClassLoader.getSystemClassLoader(); 95 96 return myClassLoader; 97 } 98 99 100 101 105 106 public static void setDebug(boolean status) 107 { 108 126 127 if (status == true) 128 System.setProperty("javax.net.debug", "ssl,handshake,verbose"); 129 else 130 { 131 System.setProperty("javax.net.debug", " "); 132 } 133 } 134 135 167 168 169 170 public static void init(String caKeystoreFile, String clientKeystoreFile, 179 char[] caPassphrase, char[] clientPassphrase, 180 String caKeystoreType, String clientKeystoreType, Frame owner) 181 throws GeneralSecurityException, IOException 183 { 184 boolean usingSASL = false; 186 checkFileSanity(caKeystoreFile, clientKeystoreFile, clientPassphrase); 187 188 189 if ((clientPassphrase!=null) && (clientPassphrase.length>0) && (clientKeystoreFile != null)) 190 usingSASL = true; 191 192 if (caKeystoreFile == null && clientKeystoreFile != null) 194 caKeystoreFile = clientKeystoreFile; 195 196 if (caKeystoreType == null) 198 caKeystoreType = DEFAULT_KEYSTORE_TYPE; 199 200 SSLContext sslctx = setSSLContextProtocol(); 202 203 208 209 KeyManagerFactory clientKeyManagerFactory = null; 210 TrustManagerFactory caTrustManagerFactory = null; 211 KeyStore caKeystore = null; 212 KeyManager[] clientKeyManagers = null; 213 214 215 220 if (usingSASL) 221 { 222 225 226 if (clientKeystoreType == null) 227 clientKeystoreType = DEFAULT_KEYSTORE_TYPE; 228 229 clientKeystore = KeyStore.getInstance(clientKeystoreType); 231 235 236 if (clientKeystoreFile != null) 237 clientKeystore.load(new FileInputStream(clientKeystoreFile), clientPassphrase); 238 239 242 243 clientKeyManagerFactory = KeyManagerFactory.getInstance("SunX509"); 244 245 249 250 clientKeyManagerFactory.init(clientKeystore, clientPassphrase); 251 252 255 256 clientKeyManagers = clientKeyManagerFactory.getKeyManagers(); 257 } 258 else 259 { 260 clientKeystore = null; 261 } 262 263 266 267 268 caKeystore = KeyStore.getInstance(caKeystoreType); 269 270 273 274 if (caKeystoreFile != null) 275 { 276 caKeystore.load(new FileInputStream(caKeystoreFile), caPassphrase); 278 } 279 280 285 String defaultTrustAlgorithm = TrustManagerFactory.getDefaultAlgorithm(); 286 287 caTrustManagerFactory = TrustManagerFactory.getInstance(defaultTrustAlgorithm); 288 289 292 294 297 298 caTrustManagerFactory.init(caKeystore); 299 300 304 305 TrustManager[] caTrustManagers = caTrustManagerFactory.getTrustManagers(); 306 307 caTrustManagers = JXTrustManager.convert(caTrustManagers, caKeystore, caKeystoreFile, caPassphrase, caKeystoreType, owner); 308 309 TrustManager[] trustedServerAndCAManagers = caTrustManagers; 310 311 sslctx.init(clientKeyManagers, trustedServerAndCAManagers, null); 312 313 factory = sslctx.getSocketFactory(); 314 315 318 synchronized(JXSSLSocketFactory.class) 319 { 320 default_factory = new JXSSLSocketFactory(); 321 } 322 } 323 324 331 private static SSLContext setSSLContextProtocol() throws NoSuchAlgorithmException 332 { 333 SSLContext sslctx; 334 335 String protocol = System.getProperty("sslversion", "TLS"); if (!"TLS".equals(protocol)) 337 System.out.println("SECURITY WARNING: Using non-standard ssl version: '" + protocol + "'"); 338 sslctx = SSLContext.getInstance(protocol); 339 return sslctx; 340 } 341 342 350 351 private static void checkFileSanity(String caKeystoreFile, String clientKeystoreFile, char[] clientPassphrase) 352 throws SSLException 353 { 354 if (clientKeystoreFile == null && caKeystoreFile == null) 355 throw new SSLException("SSL Initialisation error: No valid keystore files available."); 356 357 if (caKeystoreFile != null) 358 if (new File(caKeystoreFile).exists() == false) 359 throw new SSLException("SSL Initialisation error: file '" + caKeystoreFile + "' does not exist."); 360 361 if (clientKeystoreFile != null && clientPassphrase != null) 362 if (new File(clientKeystoreFile).exists() == false) 363 throw new SSLException("SSL Initialisation error: file '" + clientKeystoreFile + "' does not exist."); 364 } 365 366 367 411 412 415 public JXSSLSocketFactory() 416 { 417 } 418 419 427 428 public static SocketFactory getDefault() 429 { 430 synchronized(JXSSLSocketFactory.class) 431 { 432 if (default_factory == null) 433 default_factory = new JXSSLSocketFactory(); 434 } 435 436 return default_factory; 437 } 438 439 440 public static KeyStore getClientKeyStore() { 441 return clientKeystore; 442 } 443 444 453 public Socket createSocket(String host, int port) 454 throws IOException, UnknownHostException 455 { 456 return factory.createSocket(host, port); 457 } 458 459 468 public Socket createSocket(InetAddress host, int port) 469 throws IOException, UnknownHostException 470 { 471 return factory.createSocket(host, port); 472 } 473 474 475 487 public Socket createSocket(InetAddress host, int port, 488 InetAddress client_host, int client_port) 489 throws IOException, UnknownHostException 490 { 491 return factory.createSocket(host, port, client_host, client_port); 492 } 493 494 495 507 public Socket createSocket(String host, int port, 508 InetAddress client_host, int client_port) 509 throws IOException, UnknownHostException 510 { 511 return factory.createSocket(host, port, client_host, client_port); 512 } 513 514 517 public Socket createSocket(Socket socket, String host, int port, boolean autoclose) 518 throws IOException, UnknownHostException 519 { 520 return factory.createSocket(socket, host, port, autoclose); 521 } 522 523 526 public String [] getDefaultCipherSuites() 527 { 528 return factory.getDefaultCipherSuites(); 529 } 530 531 534 public String [] getSupportedCipherSuites() 535 { 536 return factory.getSupportedCipherSuites(); 537 } 538 } | Popular Tags |