001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 * 019 */ 020package org.apache.directory.api.util; 021 022 023import java.io.IOException; 024import java.net.InetAddress; 025import java.net.Socket; 026import java.security.SecureRandom; 027import java.security.cert.CertificateException; 028import java.security.cert.X509Certificate; 029 030import javax.net.SocketFactory; 031import javax.net.ssl.SSLContext; 032import javax.net.ssl.SSLSocketFactory; 033import javax.net.ssl.TrustManager; 034import javax.net.ssl.X509TrustManager; 035 036 037/** 038 * A SSLSocketFactory that accepts every certificat without validation. 039 * 040 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 041 */ 042public class DummySSLSocketFactory extends SSLSocketFactory 043{ 044 045 /** The default instance. */ 046 private static SocketFactory instance; 047 048 049 /** 050 * Gets the default instance. 051 * 052 * Note: This method is invoked from the JNDI framework when 053 * creating a ldaps:// connection. 054 * 055 * @return the default instance 056 */ 057 public static SocketFactory getDefault() 058 { 059 if ( instance == null ) 060 { 061 instance = new DummySSLSocketFactory(); 062 } 063 return instance; 064 } 065 066 /** The delegate. */ 067 private SSLSocketFactory delegate; 068 069 070 /** 071 * Creates a new instance of DummySSLSocketFactory. 072 */ 073 public DummySSLSocketFactory() 074 { 075 try 076 { 077 TrustManager tm = new X509TrustManager() 078 { 079 public X509Certificate[] getAcceptedIssuers() 080 { 081 return new X509Certificate[0]; 082 } 083 084 085 public void checkClientTrusted( X509Certificate[] arg0, String arg1 ) throws CertificateException 086 { 087 } 088 089 090 public void checkServerTrusted( X509Certificate[] arg0, String arg1 ) throws CertificateException 091 { 092 } 093 }; 094 TrustManager[] tma = 095 { tm }; 096 SSLContext sc = SSLContext.getInstance( "TLS" ); 097 sc.init( null, tma, new SecureRandom() ); 098 delegate = sc.getSocketFactory(); 099 } 100 catch ( Exception e ) 101 { 102 e.printStackTrace(); 103 } 104 } 105 106 107 /** 108 * @see javax.net.ssl.SSLSocketFactory#getDefaultCipherSuites() 109 */ 110 public String[] getDefaultCipherSuites() 111 { 112 return delegate.getDefaultCipherSuites(); 113 } 114 115 116 /** 117 * @see javax.net.ssl.SSLSocketFactory#getSupportedCipherSuites() 118 */ 119 public String[] getSupportedCipherSuites() 120 { 121 return delegate.getSupportedCipherSuites(); 122 } 123 124 125 /** 126 * @see javax.net.ssl.SSLSocketFactory#createSocket(java.net.Socket, java.lang.String, int, boolean) 127 */ 128 public Socket createSocket( Socket arg0, String arg1, int arg2, boolean arg3 ) throws IOException 129 { 130 try 131 { 132 return delegate.createSocket( arg0, arg1, arg2, arg3 ); 133 } 134 catch ( IOException e ) 135 { 136 e.printStackTrace(); 137 throw e; 138 } 139 } 140 141 142 /** 143 * @see javax.net.SocketFactory#createSocket(java.lang.String, int) 144 */ 145 public Socket createSocket( String arg0, int arg1 ) throws IOException 146 { 147 try 148 { 149 return delegate.createSocket( arg0, arg1 ); 150 } 151 catch ( IOException e ) 152 { 153 e.printStackTrace(); 154 throw e; 155 } 156 } 157 158 159 /** 160 * @see javax.net.SocketFactory#createSocket(java.net.InetAddress, int) 161 */ 162 public Socket createSocket( InetAddress arg0, int arg1 ) throws IOException 163 { 164 try 165 { 166 return delegate.createSocket( arg0, arg1 ); 167 } 168 catch ( IOException e ) 169 { 170 e.printStackTrace(); 171 throw e; 172 } 173 } 174 175 176 /** 177 * @see javax.net.SocketFactory#createSocket(java.lang.String, int, java.net.InetAddress, int) 178 */ 179 public Socket createSocket( String arg0, int arg1, InetAddress arg2, int arg3 ) throws IOException 180 { 181 try 182 { 183 return delegate.createSocket( arg0, arg1, arg2, arg3 ); 184 } 185 catch ( IOException e ) 186 { 187 e.printStackTrace(); 188 throw e; 189 } 190 } 191 192 193 /** 194 * @see javax.net.SocketFactory#createSocket(java.net.InetAddress, int, java.net.InetAddress, int) 195 */ 196 public Socket createSocket( InetAddress arg0, int arg1, InetAddress arg2, int arg3 ) throws IOException 197 { 198 try 199 { 200 return delegate.createSocket( arg0, arg1, arg2, arg3 ); 201 } 202 catch ( IOException e ) 203 { 204 e.printStackTrace(); 205 throw e; 206 } 207 } 208}