1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 28 package com.sun.star.wiki; 29 30 import java.io.IOException; 31 import java.net.InetAddress; 32 import java.net.InetSocketAddress; 33 import java.net.Socket; 34 import java.net.UnknownHostException; 35 import java.security.KeyStore; 36 import javax.net.ssl.SSLContext; 37 import javax.net.ssl.TrustManager; 38 import javax.net.ssl.TrustManagerFactory; 39 import javax.net.ssl.X509TrustManager; 40 import java.security.cert.CertificateException; 41 import java.security.cert.X509Certificate; 42 import org.apache.commons.httpclient.ConnectTimeoutException; 43 import org.apache.commons.httpclient.HttpClientError; 44 import org.apache.commons.httpclient.params.HttpConnectionParams; 45 import org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory; 46 47 class WikiProtocolSocketFactory implements SecureProtocolSocketFactory 48 { 49 private SSLContext m_aSSLContext; 50 51 public WikiProtocolSocketFactory() 52 { 53 super(); 54 } 55 56 public synchronized SSLContext GetNotSoSecureSSLContext() 57 { 58 if ( m_aSSLContext == null ) 59 { 60 TrustManager[] pTrustUnknownCerts = new TrustManager[] 61 { 62 new X509TrustManager() { 63 private X509TrustManager m_aOrgTrustManager; 64 65 private X509TrustManager GetOrgTrustManager() 66 { 67 if ( m_aOrgTrustManager == null ) 68 { 69 try 70 { 71 TrustManagerFactory aFactory = TrustManagerFactory.getInstance( TrustManagerFactory.getDefaultAlgorithm() ); 72 aFactory.init( (KeyStore)null ); 73 TrustManager[] pTrustmanagers = aFactory.getTrustManagers(); 74 if ( pTrustmanagers.length != 0 && pTrustmanagers[0] != null ) 75 m_aOrgTrustManager = (X509TrustManager)pTrustmanagers[0]; 76 } 77 catch( Exception e ) 78 { 79 throw new RuntimeException( "No access to the default trust manager!" ); 80 } 81 } 82 83 return m_aOrgTrustManager; 84 } 85 86 public X509Certificate[] getAcceptedIssuers() 87 { 88 return GetOrgTrustManager().getAcceptedIssuers(); 89 } 90 91 public void checkClientTrusted(X509Certificate[] certs, String authType) throws CertificateException 92 { 93 GetOrgTrustManager().checkClientTrusted( certs, authType ); 94 } 95 96 public void checkServerTrusted(X509Certificate[] certs, String authType) throws CertificateException 97 { 98 if ( certs == null || certs.length == 0 ) 99 GetOrgTrustManager().checkServerTrusted( certs, authType ); 100 else 101 for ( int nInd = 0; nInd < certs.length; nInd++ ) 102 certs[nInd].checkValidity(); 103 } 104 } 105 }; 106 107 try 108 { 109 SSLContext aContext = SSLContext.getInstance("SSL"); 110 if ( aContext != null ) 111 { 112 aContext.init( null, pTrustUnknownCerts, null ); 113 m_aSSLContext = aContext; 114 } 115 } 116 catch ( Exception e ) 117 { 118 } 119 } 120 121 if ( m_aSSLContext == null ) 122 throw new HttpClientError(); 123 124 return m_aSSLContext; 125 } 126 127 public Socket createSocket( String sHost, int nPort, InetAddress clientHost, int clientPort ) 128 throws IOException, UnknownHostException 129 { 130 return GetNotSoSecureSSLContext().getSocketFactory().createSocket( sHost, nPort, clientHost, clientPort ); 131 } 132 133 public Socket createSocket( final String sHost, final int nPort, final InetAddress aLocalAddress, final int nLocalPort, final HttpConnectionParams params ) 134 throws IOException, UnknownHostException, ConnectTimeoutException 135 { 136 if ( params == null ) 137 return createSocket( sHost, nPort, aLocalAddress, nLocalPort ); 138 139 int nTimeout = params.getConnectionTimeout(); 140 Socket aSocket = GetNotSoSecureSSLContext().getSocketFactory().createSocket(); 141 aSocket.bind( new InetSocketAddress( aLocalAddress, nLocalPort ) ); 142 aSocket.connect( new InetSocketAddress( sHost, nPort ), nTimeout ); 143 return aSocket; 144 } 145 146 public Socket createSocket( String sHost, int nPort ) 147 throws IOException, UnknownHostException 148 { 149 return GetNotSoSecureSSLContext().getSocketFactory().createSocket( sHost, nPort ); 150 } 151 152 public Socket createSocket( Socket aSocket, String sHost, int nPort, boolean bAutoClose ) 153 throws IOException, UnknownHostException 154 { 155 return GetNotSoSecureSSLContext().getSocketFactory().createSocket( aSocket, sHost, nPort, bAutoClose ); 156 } 157 158 public boolean equals(Object obj) 159 { 160 return ((obj != null) && obj.getClass().equals(WikiProtocolSocketFactory.class)); 161 } 162 163 public int hashCode() 164 { 165 return WikiProtocolSocketFactory.class.hashCode(); 166 } 167 }; 168 169