View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.api.util;
21  
22  
23  import java.io.IOException;
24  import java.net.InetAddress;
25  import java.net.Socket;
26  import java.security.SecureRandom;
27  import java.security.cert.CertificateException;
28  import java.security.cert.X509Certificate;
29  
30  import javax.net.SocketFactory;
31  import javax.net.ssl.SSLContext;
32  import javax.net.ssl.SSLSocketFactory;
33  import javax.net.ssl.TrustManager;
34  import javax.net.ssl.X509TrustManager;
35  
36  
37  /**
38   * A SSLSocketFactory that accepts every certificat without validation.
39   *
40   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
41   */
42  public class DummySSLSocketFactory extends SSLSocketFactory
43  {
44  
45      /** The default instance. */
46      private static SocketFactory instance;
47  
48  
49      /**
50       * Gets the default instance.
51       * 
52       * Note: This method is invoked from the JNDI framework when 
53       * creating a ldaps:// connection.
54       * 
55       * @return the default instance
56       */
57      public static SocketFactory getDefault()
58      {
59          if ( instance == null )
60          {
61              instance = new DummySSLSocketFactory();
62          }
63          return instance;
64      }
65  
66      /** The delegate. */
67      private SSLSocketFactory delegate;
68  
69  
70      /**
71       * Creates a new instance of DummySSLSocketFactory.
72       */
73      public DummySSLSocketFactory()
74      {
75          try
76          {
77              TrustManager tm = new X509TrustManager()
78              {
79                  public X509Certificate[] getAcceptedIssuers()
80                  {
81                      return new X509Certificate[0];
82                  }
83  
84  
85                  public void checkClientTrusted( X509Certificate[] arg0, String arg1 ) throws CertificateException
86                  {
87                  }
88  
89  
90                  public void checkServerTrusted( X509Certificate[] arg0, String arg1 ) throws CertificateException
91                  {
92                  }
93              };
94              TrustManager[] tma =
95                  { tm };
96              SSLContext sc = SSLContext.getInstance( "TLS" );
97              sc.init( null, tma, new SecureRandom() );
98              delegate = sc.getSocketFactory();
99          }
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 }