1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 package com.sun.star.wiki;
25 
26 import java.io.IOException;
27 import java.net.InetAddress;
28 import java.net.InetSocketAddress;
29 import java.net.Socket;
30 import java.net.UnknownHostException;
31 import java.security.KeyStore;
32 import javax.net.ssl.SSLContext;
33 import javax.net.ssl.TrustManager;
34 import javax.net.ssl.TrustManagerFactory;
35 import javax.net.ssl.X509TrustManager;
36 import java.security.cert.CertificateException;
37 import java.security.cert.X509Certificate;
38 import org.apache.commons.httpclient.ConnectTimeoutException;
39 import org.apache.commons.httpclient.HttpClientError;
40 import org.apache.commons.httpclient.params.HttpConnectionParams;
41 import org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory;
42 
43 class WikiProtocolSocketFactory implements SecureProtocolSocketFactory
44 {
45     private SSLContext m_aSSLContext;
46 
WikiProtocolSocketFactory()47     public WikiProtocolSocketFactory()
48     {
49         super();
50     }
51 
GetNotSoSecureSSLContext()52     public synchronized SSLContext GetNotSoSecureSSLContext()
53     {
54         if ( m_aSSLContext == null )
55         {
56             TrustManager[] pTrustUnknownCerts = new TrustManager[]
57             {
58                 new X509TrustManager() {
59                     private X509TrustManager m_aOrgTrustManager;
60 
61                     private X509TrustManager GetOrgTrustManager()
62                     {
63                         if ( m_aOrgTrustManager == null )
64                         {
65                             try
66                             {
67                                 TrustManagerFactory aFactory = TrustManagerFactory.getInstance( TrustManagerFactory.getDefaultAlgorithm() );
68                                 aFactory.init( (KeyStore)null );
69                                 TrustManager[] pTrustmanagers = aFactory.getTrustManagers();
70                                 if ( pTrustmanagers.length != 0 && pTrustmanagers[0] != null )
71                                     m_aOrgTrustManager = (X509TrustManager)pTrustmanagers[0];
72                             }
73                             catch( Exception e )
74                             {
75                                 throw new RuntimeException( "No access to the default trust manager!" );
76                             }
77                         }
78 
79                         return m_aOrgTrustManager;
80                     }
81 
82                     public X509Certificate[] getAcceptedIssuers()
83                     {
84                         return GetOrgTrustManager().getAcceptedIssuers();
85                     }
86 
87                     public void checkClientTrusted(X509Certificate[] certs, String authType) throws CertificateException
88                     {
89                         GetOrgTrustManager().checkClientTrusted( certs, authType );
90                     }
91 
92                     public void checkServerTrusted(X509Certificate[] certs, String authType) throws CertificateException
93                     {
94                         if ( certs == null || certs.length == 0 )
95                             GetOrgTrustManager().checkServerTrusted( certs, authType );
96                         else
97                             for ( int nInd = 0; nInd < certs.length; nInd++ )
98                                 certs[nInd].checkValidity();
99                     }
100                 }
101             };
102 
103             try
104             {
105                 SSLContext aContext = SSLContext.getInstance("SSL");
106                 if ( aContext != null )
107                 {
108                     aContext.init( null, pTrustUnknownCerts, null );
109                     m_aSSLContext = aContext;
110                 }
111             }
112             catch ( Exception e )
113             {
114             }
115         }
116 
117         if ( m_aSSLContext == null )
118             throw new HttpClientError();
119 
120         return m_aSSLContext;
121     }
122 
createSocket( String sHost, int nPort, InetAddress clientHost, int clientPort )123     public Socket createSocket( String sHost, int nPort, InetAddress clientHost, int clientPort )
124         throws IOException, UnknownHostException
125     {
126         return GetNotSoSecureSSLContext().getSocketFactory().createSocket( sHost, nPort, clientHost, clientPort );
127     }
128 
createSocket( final String sHost, final int nPort, final InetAddress aLocalAddress, final int nLocalPort, final HttpConnectionParams params )129     public Socket createSocket( final String sHost, final int nPort, final InetAddress aLocalAddress, final int nLocalPort, final HttpConnectionParams params )
130         throws IOException, UnknownHostException, ConnectTimeoutException
131     {
132         if ( params == null )
133             return createSocket( sHost, nPort, aLocalAddress, nLocalPort );
134 
135         int nTimeout = params.getConnectionTimeout();
136         Socket aSocket = GetNotSoSecureSSLContext().getSocketFactory().createSocket();
137         aSocket.bind( new InetSocketAddress( aLocalAddress, nLocalPort ) );
138         aSocket.connect( new InetSocketAddress( sHost, nPort ), nTimeout );
139         return aSocket;
140     }
141 
createSocket( String sHost, int nPort )142     public Socket createSocket( String sHost, int nPort )
143         throws IOException, UnknownHostException
144     {
145         return GetNotSoSecureSSLContext().getSocketFactory().createSocket( sHost, nPort );
146     }
147 
createSocket( Socket aSocket, String sHost, int nPort, boolean bAutoClose )148     public Socket createSocket( Socket aSocket, String sHost, int nPort, boolean bAutoClose )
149         throws IOException, UnknownHostException
150     {
151         return GetNotSoSecureSSLContext().getSocketFactory().createSocket( aSocket, sHost, nPort, bAutoClose );
152     }
153 
equals(Object obj)154     public boolean equals(Object obj)
155     {
156         return ((obj != null) && obj.getClass().equals(WikiProtocolSocketFactory.class));
157     }
158 
hashCode()159     public int hashCode()
160     {
161         return WikiProtocolSocketFactory.class.hashCode();
162     }
163 };
164 
165