1/*2 * Licensed to the Apache Software Foundation (ASF) under one or more3 * contributor license agreements. See the NOTICE file distributed with4 * this work for additional information regarding copyright ownership.5 * The ASF licenses this file to You under the Apache License, Version 2.06 * (the "License"); you may not use this file except in compliance with7 * the License. You may obtain a copy of the License at8 *9 * http://www.apache.org/licenses/LICENSE-2.010 *11 * Unless required by applicable law or agreed to in writing, software12 * 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 and15 * limitations under the License.16 */17packageorg.apache.jetspeed.security.util;
1819import java.io.IOException;
20import java.net.InetAddress;
21import java.net.Socket;
22import java.net.UnknownHostException;
23import java.security.KeyManagementException;
24import java.security.NoSuchAlgorithmException;
25import java.security.SecureRandom;
26import java.security.cert.CertificateException;
27import java.security.cert.X509Certificate;
28import javax.net.ssl.X509TrustManager;
29import javax.net.SocketFactory;
30import javax.net.ssl.SSLContext;
31import javax.net.ssl.SSLSocketFactory;
32import javax.net.ssl.TrustManager;
3334/***35 * Socket Factory for SSL connections which do not provide an authentication36 * This is used to connect to servers where we are just interested in37 * 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 */43publicclassGullibleSSLSocketFactoryextends SSLSocketFactory {
4445class GullibleTrustManager implements X509TrustManager
46 {
47 GullibleTrustManager() { }
48publicvoid checkClientTrusted(final X509Certificate[] chain, final String authType) throws CertificateException {
49 }
5051publicvoid checkServerTrusted(final X509Certificate[] chain, final String authType) throws CertificateException {
52 }
5354public X509Certificate[] getAcceptedIssuers() {
55returnnew X509Certificate[0];
56 }
57 }
5859private SSLSocketFactory factory;
60protectedGullibleSSLSocketFactory() {
61try {
62 SSLContext context = SSLContext.getInstance("TLS");
63 context.init(null, new TrustManager[] {new GullibleTrustManager()},
64new SecureRandom());
65 factory = context.getSocketFactory();
66 } catch (NoSuchAlgorithmException e) {
67 e.printStackTrace();
68 } catch (KeyManagementException e) {
69 e.printStackTrace();
70 }
71 }
72publicstatic SocketFactory getDefault() {
73returnnewGullibleSSLSocketFactory();
74 }
75public String[] getDefaultCipherSuites() {
76return factory.getDefaultCipherSuites();
77 }
78public String[] getSupportedCipherSuites() {
79return factory.getSupportedCipherSuites();
80 }
81public Socket createSocket(final Socket s, final String host, finalint port, finalboolean autoClose) throws IOException {
82return factory.createSocket(s, host, port, autoClose);
83 }
84public Socket createSocket(final String host, finalint port) throws IOException, UnknownHostException {
85return factory.createSocket(host, port);
86 }
87public Socket createSocket(final String host, finalint port, final InetAddress localAddress, finalint localPort) throws IOException, UnknownHostException {
88return factory.createSocket(host, port, localAddress, localPort);
89 }
90public Socket createSocket(final InetAddress host, finalint port) throws IOException {
91return factory.createSocket(host, port);
92 }
93public Socket createSocket(final InetAddress address, finalint port, final InetAddress localAddress, finalint localPort) throws IOException {
94return factory.createSocket(address, port, localAddress, localPort);
95 }
96 }