001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.security.util;
018:
019: import java.io.IOException;
020: import java.net.InetAddress;
021: import java.net.Socket;
022: import java.net.UnknownHostException;
023: import java.security.KeyManagementException;
024: import java.security.NoSuchAlgorithmException;
025: import java.security.SecureRandom;
026: import java.security.cert.CertificateException;
027: import java.security.cert.X509Certificate;
028: import javax.net.ssl.X509TrustManager;
029: import javax.net.SocketFactory;
030: import javax.net.ssl.SSLContext;
031: import javax.net.ssl.SSLSocketFactory;
032: import javax.net.ssl.TrustManager;
033:
034: /**
035: * Socket Factory for SSL connections which do not provide an authentication
036: * This is used to connect to servers where we are just interested in
037: * an encypted tunnel, and not to verify that both parties trust each other.
038: *
039: * @author <a href="mailto:b.vanhalderen@hippo.nl">Berry van Halderen</a>
040: * @version $Id: GullibleSSLSocketFactory.java 516448 2007-03-09 16:25:47Z ate $
041: *
042: */
043: public class GullibleSSLSocketFactory extends SSLSocketFactory {
044:
045: class GullibleTrustManager implements X509TrustManager {
046: GullibleTrustManager() {
047: }
048:
049: public void checkClientTrusted(final X509Certificate[] chain,
050: final String authType) throws CertificateException {
051: }
052:
053: public void checkServerTrusted(final X509Certificate[] chain,
054: final String authType) throws CertificateException {
055: }
056:
057: public X509Certificate[] getAcceptedIssuers() {
058: return new X509Certificate[0];
059: }
060: }
061:
062: private SSLSocketFactory factory;
063:
064: protected GullibleSSLSocketFactory() {
065: try {
066: SSLContext context = SSLContext.getInstance("TLS");
067: context.init(null,
068: new TrustManager[] { new GullibleTrustManager() },
069: new SecureRandom());
070: factory = context.getSocketFactory();
071: } catch (NoSuchAlgorithmException e) {
072: e.printStackTrace();
073: } catch (KeyManagementException e) {
074: e.printStackTrace();
075: }
076: }
077:
078: public static SocketFactory getDefault() {
079: return new GullibleSSLSocketFactory();
080: }
081:
082: public String[] getDefaultCipherSuites() {
083: return factory.getDefaultCipherSuites();
084: }
085:
086: public String[] getSupportedCipherSuites() {
087: return factory.getSupportedCipherSuites();
088: }
089:
090: public Socket createSocket(final Socket s, final String host,
091: final int port, final boolean autoClose) throws IOException {
092: return factory.createSocket(s, host, port, autoClose);
093: }
094:
095: public Socket createSocket(final String host, final int port)
096: throws IOException, UnknownHostException {
097: return factory.createSocket(host, port);
098: }
099:
100: public Socket createSocket(final String host, final int port,
101: final InetAddress localAddress, final int localPort)
102: throws IOException, UnknownHostException {
103: return factory
104: .createSocket(host, port, localAddress, localPort);
105: }
106:
107: public Socket createSocket(final InetAddress host, final int port)
108: throws IOException {
109: return factory.createSocket(host, port);
110: }
111:
112: public Socket createSocket(final InetAddress address,
113: final int port, final InetAddress localAddress,
114: final int localPort) throws IOException {
115: return factory.createSocket(address, port, localAddress,
116: localPort);
117: }
118: }
|