View Javadoc

1   /* 
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.security.util;
18  
19  import java.io.IOException;
20  import java.net.InetAddress;
21  import java.net.Socket;
22  import java.net.UnknownHostException;
23  import java.security.KeyManagementException;
24  import java.security.NoSuchAlgorithmException;
25  import java.security.SecureRandom;
26  import java.security.cert.CertificateException;
27  import java.security.cert.X509Certificate;
28  import javax.net.ssl.X509TrustManager;
29  import javax.net.SocketFactory;
30  import javax.net.ssl.SSLContext;
31  import javax.net.ssl.SSLSocketFactory;
32  import javax.net.ssl.TrustManager;
33  
34  /***
35   * Socket Factory for SSL connections which do not provide an authentication
36   * This is used to connect to servers where we are just interested in
37   * an encypted tunnel, and not to verify that both parties trust each other.
38   *
39   * @author <a href="mailto:b.vanhalderen@hippo.nl">Berry van Halderen</a>
40   * @version $Id: GullibleSSLSocketFactory.java 516448 2007-03-09 16:25:47Z ate $
41   *
42   */
43  public class GullibleSSLSocketFactory extends SSLSocketFactory {
44  
45    class GullibleTrustManager implements X509TrustManager
46    {
47      GullibleTrustManager() { }
48      public void checkClientTrusted(final X509Certificate[] chain, final String authType) throws CertificateException {
49      }
50  
51      public void checkServerTrusted(final X509Certificate[] chain, final String authType) throws CertificateException {
52      }
53    
54      public X509Certificate[] getAcceptedIssuers() {
55        return new X509Certificate[0];
56      }
57    }
58  
59     private SSLSocketFactory factory;
60     protected GullibleSSLSocketFactory() {
61        try {
62           SSLContext context = SSLContext.getInstance("TLS");
63           context.init(null, new TrustManager[] {new GullibleTrustManager()},
64              new SecureRandom());
65           factory = context.getSocketFactory();
66        } catch (NoSuchAlgorithmException e) {
67           e.printStackTrace();
68        } catch (KeyManagementException e) {
69           e.printStackTrace();
70        }
71     }
72     public static SocketFactory getDefault() {
73        return new GullibleSSLSocketFactory();
74     }
75     public String[] getDefaultCipherSuites() {
76        return factory.getDefaultCipherSuites();
77     }
78     public String[] getSupportedCipherSuites() {
79        return factory.getSupportedCipherSuites();
80     }
81     public Socket createSocket(final Socket s, final String host, final int port, final boolean autoClose) throws IOException {
82        return factory.createSocket(s, host, port, autoClose);
83     }
84     public Socket createSocket(final String host, final int port) throws IOException, UnknownHostException {
85        return factory.createSocket(host, port);
86     }
87     public Socket createSocket(final String host, final int port, final InetAddress localAddress, final int localPort) throws IOException, UnknownHostException {
88        return factory.createSocket(host, port, localAddress, localPort);
89     }
90     public Socket createSocket(final InetAddress host, final int port) throws IOException {
91        return factory.createSocket(host, port);
92     }
93     public Socket createSocket(final InetAddress address, final int port, final InetAddress localAddress, final int localPort) throws IOException {
94        return factory.createSocket(address, port, localAddress, localPort);
95     }
96  }