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 */
020 package org.apache.directory.shared.ldap.util;
021
022
023 import java.io.IOException;
024 import java.net.InetAddress;
025 import java.net.Socket;
026 import java.net.UnknownHostException;
027 import java.security.SecureRandom;
028 import java.security.cert.CertificateException;
029 import java.security.cert.X509Certificate;
030
031 import javax.net.SocketFactory;
032 import javax.net.ssl.SSLContext;
033 import javax.net.ssl.SSLSocketFactory;
034 import javax.net.ssl.TrustManager;
035 import javax.net.ssl.X509TrustManager;
036
037
038 /**
039 * A SSLSocketFactory that accepts every certificat without validation.
040 *
041 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
042 * @version $Rev$, $Date$
043 */
044 public class DummySSLSocketFactory extends SSLSocketFactory
045 {
046
047 /** The default instance. */
048 private static SocketFactory instance;
049
050
051 /**
052 * Gets the default instance.
053 *
054 * Note: This method is invoked from the JNDI framework when
055 * creating a ldaps:// connection.
056 *
057 * @return the default instance
058 */
059 public static SocketFactory getDefault()
060 {
061 if ( instance == null )
062 {
063 instance = new DummySSLSocketFactory();
064 }
065 return instance;
066 }
067
068 /** The delegate. */
069 private SSLSocketFactory delegate;
070
071
072 /**
073 * Creates a new instance of DummySSLSocketFactory.
074 */
075 public DummySSLSocketFactory()
076 {
077 try
078 {
079 TrustManager tm = new X509TrustManager()
080 {
081 public X509Certificate[] getAcceptedIssuers()
082 {
083 return new X509Certificate[0];
084 }
085
086
087 public void checkClientTrusted( X509Certificate[] arg0, String arg1 ) throws CertificateException
088 {
089 }
090
091
092 public void checkServerTrusted( X509Certificate[] arg0, String arg1 ) throws CertificateException
093 {
094 }
095 };
096 TrustManager[] tma =
097 { tm };
098 SSLContext sc = SSLContext.getInstance( "TLS" ); //$NON-NLS-1$
099 sc.init( null, tma, new SecureRandom() );
100 delegate = sc.getSocketFactory();
101 }
102 catch ( Exception e )
103 {
104 e.printStackTrace();
105 }
106 }
107
108
109 /**
110 * @see javax.net.ssl.SSLSocketFactory#getDefaultCipherSuites()
111 */
112 public String[] getDefaultCipherSuites()
113 {
114 return delegate.getDefaultCipherSuites();
115 }
116
117
118 /**
119 * @see javax.net.ssl.SSLSocketFactory#getSupportedCipherSuites()
120 */
121 public String[] getSupportedCipherSuites()
122 {
123 return delegate.getSupportedCipherSuites();
124 }
125
126
127 /**
128 * @see javax.net.ssl.SSLSocketFactory#createSocket(java.net.Socket, java.lang.String, int, boolean)
129 */
130 public Socket createSocket( Socket arg0, String arg1, int arg2, boolean arg3 ) throws IOException
131 {
132 try
133 {
134 return delegate.createSocket( arg0, arg1, arg2, arg3 );
135 }
136 catch ( IOException e )
137 {
138 e.printStackTrace();
139 throw e;
140 }
141 }
142
143
144 /**
145 * @see javax.net.SocketFactory#createSocket(java.lang.String, int)
146 */
147 public Socket createSocket( String arg0, int arg1 ) throws IOException, UnknownHostException
148 {
149 try
150 {
151 return delegate.createSocket( arg0, arg1 );
152 }
153 catch ( IOException e )
154 {
155 e.printStackTrace();
156 throw e;
157 }
158 }
159
160
161 /**
162 * @see javax.net.SocketFactory#createSocket(java.net.InetAddress, int)
163 */
164 public Socket createSocket( InetAddress arg0, int arg1 ) throws IOException
165 {
166 try
167 {
168 return delegate.createSocket( arg0, arg1 );
169 }
170 catch ( IOException e )
171 {
172 e.printStackTrace();
173 throw e;
174 }
175 }
176
177
178 /**
179 * @see javax.net.SocketFactory#createSocket(java.lang.String, int, java.net.InetAddress, int)
180 */
181 public Socket createSocket( String arg0, int arg1, InetAddress arg2, int arg3 ) throws IOException,
182 UnknownHostException
183 {
184 try
185 {
186 return delegate.createSocket( arg0, arg1, arg2, arg3 );
187 }
188 catch ( IOException e )
189 {
190 e.printStackTrace();
191 throw e;
192 }
193 }
194
195
196 /**
197 * @see javax.net.SocketFactory#createSocket(java.net.InetAddress, int, java.net.InetAddress, int)
198 */
199 public Socket createSocket( InetAddress arg0, int arg1, InetAddress arg2, int arg3 ) throws IOException
200 {
201 try
202 {
203 return delegate.createSocket( arg0, arg1, arg2, arg3 );
204 }
205 catch ( IOException e )
206 {
207 e.printStackTrace();
208 throw e;
209 }
210 }
211 }