cid#1607171 Data race condition
[LibreOffice.git] / swext / mediawiki / src / com / sun / star / wiki / WikiProtocolSocketFactory.java
blob7ca94390dce40a863baf1f0a4f84a41ad4ad1b01
1 /*
2 * This file is part of the LibreOffice project.
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/.
8 * This file incorporates work covered by the following license notice:
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 .
19 package com.sun.star.wiki;
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;
35 class WikiProtocolSocketFactory extends SSLSocketFactory
37 private SSLContext m_aSSLContext;
39 private synchronized SSLContext GetNotSoSecureSSLContext()
41 if ( m_aSSLContext != null ) {
42 return m_aSSLContext;
44 TrustManager[] pTrustUnknownCerts = new TrustManager[]
46 new X509TrustManager() {
47 private X509TrustManager m_aOrgTrustManager;
49 private X509TrustManager GetOrgTrustManager()
51 if ( m_aOrgTrustManager == null )
53 try
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];
61 catch( Exception e )
63 throw new RuntimeException( "No access to the default trust manager!", e );
67 if (m_aOrgTrustManager == null)
68 throw new RuntimeException("No access to the default trust manager!");
70 return m_aOrgTrustManager;
73 public X509Certificate[] getAcceptedIssuers()
75 return GetOrgTrustManager().getAcceptedIssuers();
78 public void checkClientTrusted(X509Certificate[] certs, String authType) throws CertificateException
80 GetOrgTrustManager().checkClientTrusted( certs, authType );
83 public void checkServerTrusted(X509Certificate[] certs, String authType) throws CertificateException
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();
94 try
96 SSLContext aContext = SSLContext.getInstance("TLSv1.2");
97 if ( aContext != null )
99 aContext.init( null, pTrustUnknownCerts, null );
100 m_aSSLContext = aContext;
103 catch ( Exception e )
107 if ( m_aSSLContext == null )
108 throw new RuntimeException("failed to create SSLContext");
110 return m_aSSLContext;
113 @Override
114 public Socket createSocket(InetAddress address, int port)
115 throws IOException
117 return GetNotSoSecureSSLContext().getSocketFactory().createSocket(address, port);
120 @Override
121 public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort)
122 throws IOException
124 return GetNotSoSecureSSLContext().getSocketFactory().createSocket(address, port, localAddress, localPort);
127 @Override
128 public Socket createSocket( String sHost, int nPort, InetAddress clientHost, int clientPort )
129 throws IOException, UnknownHostException
131 return GetNotSoSecureSSLContext().getSocketFactory().createSocket( sHost, nPort, clientHost, clientPort );
134 @Override
135 public Socket createSocket( String sHost, int nPort )
136 throws IOException, UnknownHostException
138 return GetNotSoSecureSSLContext().getSocketFactory().createSocket( sHost, nPort );
141 @Override
142 public Socket createSocket( Socket aSocket, String sHost, int nPort, boolean bAutoClose )
143 throws IOException
145 return GetNotSoSecureSSLContext().getSocketFactory().createSocket( aSocket, sHost, nPort, bAutoClose );
148 @Override
149 public String[] getDefaultCipherSuites()
151 return GetNotSoSecureSSLContext().getSocketFactory().getDefaultCipherSuites();
154 @Override
155 public String[] getSupportedCipherSuites()
157 return GetNotSoSecureSSLContext().getSocketFactory().getSupportedCipherSuites();
160 @Override
161 public boolean equals(Object obj)
163 return (obj != null) && (obj instanceof WikiProtocolSocketFactory);
166 @Override
167 public int hashCode()
169 return WikiProtocolSocketFactory.class.hashCode();
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
177 private static class LogSocket extends SSLSocket
179 private final SSLSocket m_Socket;
181 public LogSocket(SSLSocket socket)
183 m_Socket = socket;
186 private static class LogStream extends java.io.FilterOutputStream
188 public LogStream(java.io.OutputStream stream)
190 super(stream);
193 @Override
194 public void write(byte[] buf, int offset, int len)
195 throws IOException
197 System.err.println("LogStream.write: \"" + new String(buf, offset, len, "UTF-8") + "\"");
198 out.write(buf, offset, len);
202 @Override
203 public java.io.OutputStream getOutputStream() throws IOException
205 return new LogStream(m_Socket.getOutputStream());
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(); }
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(); }
272 @Override
273 public Socket createSocket(InetAddress address, int port)
274 throws IOException
276 return new LogSocket((SSLSocket) SSLSocketFactory.getDefault().createSocket(address, port));
279 @Override
280 public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort)
281 throws IOException
283 return new LogSocket((SSLSocket) SSLSocketFactory.getDefault().createSocket(address, port, localAddress, localPort));
286 @Override
287 public Socket createSocket( String sHost, int nPort, InetAddress clientHost, int clientPort )
288 throws IOException, UnknownHostException
290 return new LogSocket((SSLSocket) SSLSocketFactory.getDefault().createSocket(sHost, nPort, clientHost, clientPort));
293 @Override
294 public Socket createSocket( String sHost, int nPort )
295 throws IOException, UnknownHostException
297 return new LogSocket((SSLSocket) SSLSocketFactory.getDefault().createSocket(sHost, nPort));
300 @Override
301 public Socket createSocket( Socket aSocket, String sHost, int nPort, boolean bAutoClose )
302 throws IOException
304 return new LogSocket((SSLSocket) ((SSLSocketFactory) SSLSocketFactory.getDefault()).createSocket(aSocket, sHost, nPort, bAutoClose));
307 @Override
308 public String[] getDefaultCipherSuites()
310 // have to implement abstract method, just use the default
311 return ((SSLSocketFactory) SSLSocketFactory.getDefault()).getDefaultCipherSuites();
314 @Override
315 public String[] getSupportedCipherSuites()
317 // have to implement abstract method, just use the default
318 return ((SSLSocketFactory) SSLSocketFactory.getDefault()).getSupportedCipherSuites();
321 @Override
322 public boolean equals(Object obj)
324 return (obj != null) && (obj instanceof LoggingProtocolSocketFactory);
327 @Override
328 public int hashCode()
330 return LoggingProtocolSocketFactory.class.hashCode();