| 1 |
/* |
| 2 |
* This file is part of the LibreOffice project. |
| 3 |
* |
| 4 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 5 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 6 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 7 |
* |
| 8 |
* This file incorporates work covered by the following license notice: |
| 9 |
* |
| 10 |
* Licensed to the Apache Software Foundation (ASF) under one or more |
| 11 |
* contributor license agreements. See the NOTICE file distributed |
| 12 |
* with this work for additional information regarding copyright |
| 13 |
* ownership. The ASF licenses this file to you under the Apache |
| 14 |
* License, Version 2.0 (the "License"); you may not use this file |
| 15 |
* except in compliance with the License. You may obtain a copy of |
| 16 |
* the License at http://www.apache.org/licenses/LICENSE-2.0 . |
| 17 |
*/ |
| 18 |
|
| 19 |
package com.sun.star.wiki; |
| 20 |
|
| 21 |
import java.io.IOException; |
| 22 |
import java.net.InetAddress; |
| 23 |
import java.net.Socket; |
| 24 |
import java.net.UnknownHostException; |
| 25 |
import java.security.KeyStore; |
| 26 |
import javax.net.ssl.SSLContext; |
| 27 |
import javax.net.ssl.SSLSocket; |
| 28 |
import javax.net.ssl.SSLSocketFactory; |
| 29 |
import javax.net.ssl.TrustManager; |
| 30 |
import javax.net.ssl.TrustManagerFactory; |
| 31 |
import javax.net.ssl.X509TrustManager; |
| 32 |
import java.security.cert.CertificateException; |
| 33 |
import java.security.cert.X509Certificate; |
| 34 |
|
| 35 |
class WikiProtocolSocketFactory extends SSLSocketFactory |
| 36 |
{ |
| 37 |
private SSLContext m_aSSLContext; |
| 38 |
|
| 39 |
private synchronized SSLContext GetNotSoSecureSSLContext() |
| 40 |
{ |
| 41 |
if ( m_aSSLContext != null ) { |
| 42 |
return m_aSSLContext; |
| 43 |
} |
| 44 |
TrustManager[] pTrustUnknownCerts = new TrustManager[] |
| 45 |
{ |
| 46 |
new X509TrustManager() { |
| 47 |
private X509TrustManager m_aOrgTrustManager; |
| 48 |
|
| 49 |
private X509TrustManager GetOrgTrustManager() |
| 50 |
{ |
| 51 |
if ( m_aOrgTrustManager == null ) |
| 52 |
{ |
| 53 |
try |
| 54 |
{ |
| 55 |
TrustManagerFactory aFactory = TrustManagerFactory.getInstance( TrustManagerFactory.getDefaultAlgorithm() ); |
| 56 |
aFactory.init( (KeyStore)null ); |
| 57 |
TrustManager[] pTrustmanagers = aFactory.getTrustManagers(); |
| 58 |
if ( pTrustmanagers.length != 0 && pTrustmanagers[0] != null ) |
| 59 |
m_aOrgTrustManager = (X509TrustManager)pTrustmanagers[0]; |
| 60 |
} |
| 61 |
catch( Exception e ) |
| 62 |
{ |
| 63 |
throw new RuntimeException( "No access to the default trust manager!", e ); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
if (m_aOrgTrustManager == null) |
| 68 |
throw new RuntimeException("No access to the default trust manager!"); |
| 69 |
|
| 70 |
return m_aOrgTrustManager; |
| 71 |
} |
| 72 |
|
| 73 |
public X509Certificate[] getAcceptedIssuers() |
| 74 |
{ |
| 75 |
return GetOrgTrustManager().getAcceptedIssuers(); |
| 76 |
} |
| 77 |
|
| 78 |
public void checkClientTrusted(X509Certificate[] certs, String authType) throws CertificateException |
| 79 |
{ |
| 80 |
GetOrgTrustManager().checkClientTrusted( certs, authType ); |
| 81 |
} |
| 82 |
|
| 83 |
public void checkServerTrusted(X509Certificate[] certs, String authType) throws CertificateException |
| 84 |
{ |
| 85 |
if ( certs == null || certs.length == 0 ) |
| 86 |
GetOrgTrustManager().checkServerTrusted( certs, authType ); |
| 87 |
else |
| 88 |
for ( int nInd = 0; nInd < certs.length; nInd++ ) |
| 89 |
certs[nInd].checkValidity(); |
| 90 |
} |
| 91 |
} |
| 92 |
}; |
| 93 |
|
| 94 |
try |
| 95 |
{ |
| 96 |
SSLContext aContext = SSLContext.getInstance("TLSv1.2"); |
| 97 |
if ( aContext != null ) |
| 98 |
{ |
| 99 |
aContext.init( null, pTrustUnknownCerts, null ); |
| 100 |
m_aSSLContext = aContext; |
| 101 |
} |
| 102 |
} |
| 103 |
catch ( Exception e ) |
| 104 |
{ |
| 105 |
} |
| 106 |
|
| 107 |
if ( m_aSSLContext == null ) |
| 108 |
throw new RuntimeException("failed to create SSLContext"); |
| 109 |
|
| 110 |
return m_aSSLContext; |
| 111 |
} |
| 112 |
|
| 113 |
@Override |
| 114 |
public Socket createSocket(InetAddress address, int port) |
| 115 |
throws IOException |
| 116 |
{ |
| 117 |
return GetNotSoSecureSSLContext().getSocketFactory().createSocket(address, port); |
| 118 |
} |
| 119 |
|
| 120 |
@Override |
| 121 |
public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) |
| 122 |
throws IOException |
| 123 |
{ |
| 124 |
return GetNotSoSecureSSLContext().getSocketFactory().createSocket(address, port, localAddress, localPort); |
| 125 |
} |
| 126 |
|
| 127 |
@Override |
| 128 |
public Socket createSocket( String sHost, int nPort, InetAddress clientHost, int clientPort ) |
| 129 |
throws IOException, UnknownHostException |
| 130 |
{ |
| 131 |
return GetNotSoSecureSSLContext().getSocketFactory().createSocket( sHost, nPort, clientHost, clientPort ); |
| 132 |
} |
| 133 |
|
| 134 |
@Override |
| 135 |
public Socket createSocket( String sHost, int nPort ) |
| 136 |
throws IOException, UnknownHostException |
| 137 |
{ |
| 138 |
return GetNotSoSecureSSLContext().getSocketFactory().createSocket( sHost, nPort ); |
| 139 |
} |
| 140 |
|
| 141 |
@Override |
| 142 |
public Socket createSocket( Socket aSocket, String sHost, int nPort, boolean bAutoClose ) |
| 143 |
throws IOException |
| 144 |
{ |
| 145 |
return GetNotSoSecureSSLContext().getSocketFactory().createSocket( aSocket, sHost, nPort, bAutoClose ); |
| 146 |
} |
| 147 |
|
| 148 |
@Override |
| 149 |
public String[] getDefaultCipherSuites() |
| 150 |
{ |
| 151 |
return GetNotSoSecureSSLContext().getSocketFactory().getDefaultCipherSuites(); |
| 152 |
} |
| 153 |
|
| 154 |
@Override |
| 155 |
public String[] getSupportedCipherSuites() |
| 156 |
{ |
| 157 |
return GetNotSoSecureSSLContext().getSocketFactory().getSupportedCipherSuites(); |
| 158 |
} |
| 159 |
|
| 160 |
@Override |
| 161 |
public boolean equals(Object obj) |
| 162 |
{ |
| 163 |
return (obj != null) && (obj instanceof WikiProtocolSocketFactory); |
| 164 |
} |
| 165 |
|
| 166 |
@Override |
| 167 |
public int hashCode() |
| 168 |
{ |
| 169 |
return WikiProtocolSocketFactory.class.hashCode(); |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
// A factory that creates streams that log everything that's written |
| 174 |
// to stderr - useful for debugging encrypted TLS connections |
| 175 |
class LoggingProtocolSocketFactory extends SSLSocketFactory |
| 176 |
{ |
| 177 |
private static class LogSocket extends SSLSocket |
| 178 |
{ |
| 179 |
private final SSLSocket m_Socket; |
| 180 |
|
| 181 |
public LogSocket(SSLSocket socket) |
| 182 |
{ |
| 183 |
m_Socket = socket; |
| 184 |
} |
| 185 |
|
| 186 |
private static class LogStream extends java.io.FilterOutputStream |
| 187 |
{ |
| 188 |
public LogStream(java.io.OutputStream stream) |
| 189 |
{ |
| 190 |
super(stream); |
| 191 |
} |
| 192 |
|
| 193 |
@Override |
| 194 |
public void write(byte[] buf, int offset, int len) |
| 195 |
throws IOException |
| 196 |
{ |
| 197 |
System.err.println("LogStream.write: \"" + new String(buf, offset, len, "UTF-8") + "\""); |
| 198 |
out.write(buf, offset, len); |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
@Override |
| 203 |
public java.io.OutputStream getOutputStream() throws IOException |
| 204 |
{ |
| 205 |
return new LogStream(m_Socket.getOutputStream()); |
| 206 |
} |
| 207 |
|
| 208 |
@Override public void addHandshakeCompletedListener(javax.net.ssl.HandshakeCompletedListener listener) { m_Socket.addHandshakeCompletedListener(listener); } |
| 209 |
@Override public String[] getEnabledCipherSuites() { return m_Socket.getEnabledCipherSuites(); } |
| 210 |
@Override public String[] getEnabledProtocols() { return m_Socket.getEnabledProtocols(); } |
| 211 |
@Override public boolean getEnableSessionCreation() { return m_Socket.getEnableSessionCreation(); } |
| 212 |
@Override public boolean getNeedClientAuth() { return m_Socket.getNeedClientAuth(); } |
| 213 |
@Override public javax.net.ssl.SSLSession getSession() { return m_Socket.getSession(); } |
| 214 |
@Override public javax.net.ssl.SSLParameters getSSLParameters() { return m_Socket.getSSLParameters(); } |
| 215 |
@Override public String[] getSupportedCipherSuites() { return m_Socket.getSupportedCipherSuites(); } |
| 216 |
@Override public String[] getSupportedProtocols() { return m_Socket.getSupportedProtocols(); } |
| 217 |
@Override public boolean getUseClientMode() { return m_Socket.getUseClientMode(); } |
| 218 |
@Override public boolean getWantClientAuth() { return m_Socket.getWantClientAuth(); } |
| 219 |
@Override public void removeHandshakeCompletedListener(javax.net.ssl.HandshakeCompletedListener listener) { m_Socket.removeHandshakeCompletedListener(listener); } |
| 220 |
@Override public void setEnabledCipherSuites(String[] suites) { m_Socket.setEnabledCipherSuites(suites); } |
| 221 |
@Override public void setEnabledProtocols(String[] protocols) { m_Socket.setEnabledProtocols(protocols); } |
| 222 |
@Override public void setEnableSessionCreation(boolean flag) { m_Socket.setEnableSessionCreation(flag); } |
| 223 |
@Override public void setNeedClientAuth(boolean need) { m_Socket.setNeedClientAuth(need); } |
| 224 |
@Override public void setSSLParameters(javax.net.ssl.SSLParameters params) { m_Socket.setSSLParameters(params); } |
| 225 |
@Override public void setUseClientMode(boolean mode) { m_Socket.setUseClientMode(mode); } |
| 226 |
@Override public void setWantClientAuth(boolean want) { m_Socket.setWantClientAuth(want); } |
| 227 |
@Override public void startHandshake() throws IOException { m_Socket.startHandshake(); } |
| 228 |
|
| 229 |
@Override public void bind(java.net.SocketAddress bindpoint) throws IOException { m_Socket.bind(bindpoint); } |
| 230 |
@Override public void close() throws IOException { m_Socket.close(); } |
| 231 |
@Override public void connect(java.net.SocketAddress endpoint) throws IOException { m_Socket.connect(endpoint); } |
| 232 |
@Override public void connect(java.net.SocketAddress endpoint, int timeout) throws IOException { m_Socket.connect(endpoint, timeout); } |
| 233 |
@Override public java.nio.channels.SocketChannel getChannel() { return m_Socket.getChannel(); } |
| 234 |
@Override public InetAddress getInetAddress() { return m_Socket.getInetAddress(); } |
| 235 |
@Override public java.io.InputStream getInputStream() throws IOException { return m_Socket.getInputStream(); } |
| 236 |
@Override public boolean getKeepAlive() throws java.net.SocketException { return m_Socket.getKeepAlive(); } |
| 237 |
@Override public InetAddress getLocalAddress() { return m_Socket.getLocalAddress(); } |
| 238 |
@Override public int getLocalPort() { return m_Socket.getLocalPort(); } |
| 239 |
@Override public java.net.SocketAddress getLocalSocketAddress() { return m_Socket.getLocalSocketAddress(); } |
| 240 |
@Override public boolean getOOBInline() throws java.net.SocketException { return m_Socket.getOOBInline(); } |
| 241 |
@Override public int getPort() { return m_Socket.getPort(); } |
| 242 |
@Override public int getReceiveBufferSize() throws java.net.SocketException { return m_Socket.getReceiveBufferSize(); } |
| 243 |
@Override public java.net.SocketAddress getRemoteSocketAddress() { return m_Socket.getRemoteSocketAddress(); } |
| 244 |
@Override public boolean getReuseAddress() throws java.net.SocketException { return m_Socket.getReuseAddress(); } |
| 245 |
@Override public int getSendBufferSize() throws java.net.SocketException { return m_Socket.getSendBufferSize(); } |
| 246 |
@Override public int getSoLinger() throws java.net.SocketException { return m_Socket.getSoLinger(); } |
| 247 |
@Override public int getSoTimeout() throws java.net.SocketException { return m_Socket.getSoTimeout(); } |
| 248 |
@Override public boolean getTcpNoDelay() throws java.net.SocketException { return m_Socket.getTcpNoDelay(); } |
| 249 |
@Override public int getTrafficClass() throws java.net.SocketException { return m_Socket.getTrafficClass(); } |
| 250 |
@Override public boolean isBound() { return m_Socket.isBound(); } |
| 251 |
@Override public boolean isClosed() { return m_Socket.isClosed(); } |
| 252 |
@Override public boolean isConnected() { return m_Socket.isConnected(); } |
| 253 |
@Override public boolean isInputShutdown() { return m_Socket.isInputShutdown(); } |
| 254 |
@Override public boolean isOutputShutdown() { return m_Socket.isOutputShutdown(); } |
| 255 |
@Override public void sendUrgentData(int data) throws IOException { m_Socket.sendUrgentData(data); } |
| 256 |
@Override public void setKeepAlive(boolean on) throws java.net.SocketException { m_Socket.setKeepAlive(on); } |
| 257 |
@Override public void setOOBInline(boolean on) throws java.net.SocketException { m_Socket.setOOBInline(on); } |
| 258 |
@Override public void setPerformancePreferences(int connectionTime, int latency, int bandwidth) { m_Socket.setPerformancePreferences(connectionTime, latency, bandwidth); } |
| 259 |
@Override public void setReceiveBufferSize(int size) throws java.net.SocketException { m_Socket.setReceiveBufferSize(size); } |
| 260 |
@Override public void setReuseAddress(boolean on) throws java.net.SocketException { m_Socket.setReuseAddress(on); } |
| 261 |
@Override public void setSendBufferSize(int size) throws java.net.SocketException { m_Socket.setSendBufferSize(size); } |
| 262 |
@Override public void setSoLinger(boolean on, int linger) throws java.net.SocketException { m_Socket.setSoLinger(on, linger); } |
| 263 |
@Override public void setSoTimeout(int timeout) throws java.net.SocketException{ m_Socket.setSoTimeout(timeout); } |
| 264 |
@Override public void setTcpNoDelay(boolean on) throws java.net.SocketException{ m_Socket.setTcpNoDelay(on); } |
| 265 |
@Override public void setTrafficClass(int tc) throws java.net.SocketException { m_Socket.setTrafficClass(tc); } |
| 266 |
@Override public void shutdownInput() throws IOException { m_Socket.shutdownInput(); } |
| 267 |
@Override public void shutdownOutput() throws IOException { m_Socket.shutdownOutput(); } |
| 268 |
@Override public String toString() { return m_Socket.toString(); } |
| 269 |
|
| 270 |
} |
| 271 |
|
| 272 |
@Override |
| 273 |
public Socket createSocket(InetAddress address, int port) |
| 274 |
throws IOException |
| 275 |
{ |
| 276 |
return new LogSocket((SSLSocket) SSLSocketFactory.getDefault().createSocket(address, port)); |
| 277 |
} |
| 278 |
|
| 279 |
@Override |
| 280 |
public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) |
| 281 |
throws IOException |
| 282 |
{ |
| 283 |
return new LogSocket((SSLSocket) SSLSocketFactory.getDefault().createSocket(address, port, localAddress, localPort)); |
| 284 |
} |
| 285 |
|
| 286 |
@Override |
| 287 |
public Socket createSocket( String sHost, int nPort, InetAddress clientHost, int clientPort ) |
| 288 |
throws IOException, UnknownHostException |
| 289 |
{ |
| 290 |
return new LogSocket((SSLSocket) SSLSocketFactory.getDefault().createSocket(sHost, nPort, clientHost, clientPort)); |
| 291 |
} |
| 292 |
|
| 293 |
@Override |
| 294 |
public Socket createSocket( String sHost, int nPort ) |
| 295 |
throws IOException, UnknownHostException |
| 296 |
{ |
| 297 |
return new LogSocket((SSLSocket) SSLSocketFactory.getDefault().createSocket(sHost, nPort)); |
| 298 |
} |
| 299 |
|
| 300 |
@Override |
| 301 |
public Socket createSocket( Socket aSocket, String sHost, int nPort, boolean bAutoClose ) |
| 302 |
throws IOException |
| 303 |
{ |
| 304 |
return new LogSocket((SSLSocket) ((SSLSocketFactory) SSLSocketFactory.getDefault()).createSocket(aSocket, sHost, nPort, bAutoClose)); |
| 305 |
} |
| 306 |
|
| 307 |
@Override |
| 308 |
public String[] getDefaultCipherSuites() |
| 309 |
{ |
| 310 |
// have to implement abstract method, just use the default |
| 311 |
return ((SSLSocketFactory) SSLSocketFactory.getDefault()).getDefaultCipherSuites(); |
| 312 |
} |
| 313 |
|
| 314 |
@Override |
| 315 |
public String[] getSupportedCipherSuites() |
| 316 |
{ |
| 317 |
// have to implement abstract method, just use the default |
| 318 |
return ((SSLSocketFactory) SSLSocketFactory.getDefault()).getSupportedCipherSuites(); |
| 319 |
} |
| 320 |
|
| 321 |
@Override |
| 322 |
public boolean equals(Object obj) |
| 323 |
{ |
| 324 |
return (obj != null) && (obj instanceof LoggingProtocolSocketFactory); |
| 325 |
} |
| 326 |
|
| 327 |
@Override |
| 328 |
public int hashCode() |
| 329 |
{ |
| 330 |
return LoggingProtocolSocketFactory.class.hashCode(); |
| 331 |
} |
| 332 |
} |
| 333 |
|