1 16 package com.sun.slamd.example; 17 18 19 20 import java.io.*; 21 import java.net.*; 22 import java.security.*; 23 import java.security.cert.*; 24 import javax.net.ssl.*; 25 import netscape.ldap.*; 26 27 28 29 38 public class JSSEBlindTrustSocketFactory 39 extends SSLSocketFactory 40 implements LDAPSocketFactory, X509TrustManager 41 { 42 boolean debugMode; 45 46 boolean disableSessionReuse; 49 50 SSLContext sslContext; 52 53 SSLSocketFactory sslSocketFactory; 55 56 String [] cipherNames; 58 59 60 61 67 public JSSEBlindTrustSocketFactory() 68 throws LDAPException 69 { 70 this(false); 71 } 72 73 74 75 86 public JSSEBlindTrustSocketFactory(boolean debugMode) 87 throws LDAPException 88 { 89 this.debugMode = debugMode; 90 91 92 Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider()); 94 System.setProperty("java.protocol.handler.pkgs", 95 "com.sun.net.ssl.internal.www.protocol"); 96 97 98 try 100 { 101 sslContext = SSLContext.getInstance("SSLv3"); 102 } 103 catch (NoSuchAlgorithmException nsae) 104 { 105 throw new LDAPException("Unable to initialize the SSL context: " + nsae); 106 } 107 108 109 try 112 { 113 sslContext.init(null, new TrustManager[] { this }, null); 114 } 115 catch (KeyManagementException kme) 116 { 117 throw new LDAPException("Unable to register a new trust manager with " + 118 "the SSL context: " + kme); 119 } 120 121 122 sslSocketFactory = sslContext.getSocketFactory(); 124 125 126 disableSessionReuse = false; 128 cipherNames = sslSocketFactory.getDefaultCipherSuites(); 129 130 131 if (debugMode) 134 { 135 System.err.println("New JSSEBlindTrustSocketFactory created"); 136 } 137 } 138 139 140 141 148 public void checkClientTrusted(X509Certificate[] chain, String authType) 149 { 150 if (debugMode) 153 { 154 System.err.println("checkClientTrusted() invoked"); 155 } 156 } 157 158 159 160 167 public void checkServerTrusted(X509Certificate[] chain, String authType) 168 { 169 if (debugMode) 172 { 173 System.err.println("checkServerTrusted() invoked"); 174 } 175 } 176 177 178 179 185 public X509Certificate[] getAcceptedIssuers() 186 { 187 if (debugMode) 188 { 189 System.err.println("getAcceptedIssuers() invoked"); 190 } 191 192 return new X509Certificate[0]; 193 } 194 195 196 197 212 public Socket makeSocket(String host, int port) 213 throws LDAPException 214 { 215 if (debugMode) 216 { 217 System.err.println("makeSocket(" + host + "," + port + ") invoked"); 218 } 219 220 try 221 { 222 SSLSocket s = (SSLSocket) sslSocketFactory.createSocket(host, port); 223 s.setEnabledCipherSuites(cipherNames); 224 225 if (disableSessionReuse) 226 { 227 s.getSession().invalidate(); 228 } 229 230 return s; 231 } 232 catch (Exception e) 233 { 234 throw new LDAPException("Unable to establish the SSL connection: " + e); 235 } 236 } 237 238 239 240 250 public Socket createSocket(String host, int port) 251 throws IOException 252 { 253 SSLSocket s = (SSLSocket) sslSocketFactory.createSocket(host, port); 254 s.setEnabledCipherSuites(cipherNames); 255 256 if (disableSessionReuse) 257 { 258 s.getSession().invalidate(); 259 } 260 261 return s; 262 } 263 264 265 266 280 public Socket createSocket(String host, int port, InetAddress localHost, 281 int localPort) 282 throws IOException 283 { 284 SSLSocket s = (SSLSocket) sslSocketFactory.createSocket(host, port, 285 localHost, 286 localPort); 287 s.setEnabledCipherSuites(cipherNames); 288 289 if (disableSessionReuse) 290 { 291 s.getSession().invalidate(); 292 } 293 294 return s; 295 } 296 297 298 299 309 public Socket createSocket(InetAddress host, int port) 310 throws IOException 311 { 312 SSLSocket s = (SSLSocket) sslSocketFactory.createSocket(host, port); 313 s.setEnabledCipherSuites(cipherNames); 314 315 if (disableSessionReuse) 316 { 317 s.getSession().invalidate(); 318 } 319 320 return s; 321 } 322 323 324 325 339 public Socket createSocket(InetAddress host, int port, 340 InetAddress localAddress, int localPort) 341 throws IOException 342 { 343 SSLSocket s = (SSLSocket) sslSocketFactory.createSocket(host, port, 344 localAddress, 345 localPort); 346 s.setEnabledCipherSuites(cipherNames); 347 348 if (disableSessionReuse) 349 { 350 s.getSession().invalidate(); 351 } 352 353 return s; 354 } 355 356 357 358 369 public Socket createSocket(Socket socket, String host, int port, 370 boolean autoClose) 371 throws IOException 372 { 373 SSLSocket s = (SSLSocket) sslSocketFactory.createSocket(socket, host, 374 port, autoClose); 375 s.setEnabledCipherSuites(cipherNames); 376 377 if (disableSessionReuse) 378 { 379 s.getSession().invalidate(); 380 } 381 382 return s; 383 } 384 385 386 387 394 public String [] getCiphers() 395 { 396 return cipherNames; 397 } 398 399 400 401 408 public void setCipher(String cipherName) 409 { 410 if (cipherName == null) 411 { 412 cipherNames = sslSocketFactory.getDefaultCipherSuites(); 413 } 414 else 415 { 416 cipherNames = new String [] { cipherName }; 417 } 418 } 419 420 421 422 429 public void setCiphers(String [] cipherNames) 430 { 431 if (cipherNames == null) 432 { 433 this.cipherNames = sslSocketFactory.getDefaultCipherSuites(); 434 } 435 else 436 { 437 this.cipherNames = cipherNames; 438 } 439 } 440 441 442 443 448 public String [] getDefaultCipherSuites() 449 { 450 return cipherNames; 451 } 452 453 454 455 460 public String [] getSupportedCipherSuites() 461 { 462 return cipherNames; 463 } 464 465 466 467 473 public boolean getDisableSessionReuse() 474 { 475 return disableSessionReuse; 476 } 477 478 479 480 486 public void setDisableSessionReuse(boolean disableSessionReuse) 487 { 488 this.disableSessionReuse = disableSessionReuse; 489 } 490 } 491 492 | Popular Tags |