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
.InetSocketAddress
;
24 import java
.net
.Socket
;
25 import java
.net
.UnknownHostException
;
26 import java
.security
.KeyStore
;
27 import javax
.net
.ssl
.SSLContext
;
28 import javax
.net
.ssl
.TrustManager
;
29 import javax
.net
.ssl
.TrustManagerFactory
;
30 import javax
.net
.ssl
.X509TrustManager
;
31 import java
.security
.cert
.CertificateException
;
32 import java
.security
.cert
.X509Certificate
;
33 import org
.apache
.commons
.httpclient
.ConnectTimeoutException
;
34 import org
.apache
.commons
.httpclient
.HttpClientError
;
35 import org
.apache
.commons
.httpclient
.params
.HttpConnectionParams
;
36 import org
.apache
.commons
.httpclient
.protocol
.SecureProtocolSocketFactory
;
38 class WikiProtocolSocketFactory
implements SecureProtocolSocketFactory
40 private SSLContext m_aSSLContext
;
42 private synchronized SSLContext
GetNotSoSecureSSLContext()
44 if ( m_aSSLContext
!= null ) {
47 TrustManager
[] pTrustUnknownCerts
= new TrustManager
[]
49 new X509TrustManager() {
50 private X509TrustManager m_aOrgTrustManager
;
52 private X509TrustManager
GetOrgTrustManager()
54 if ( m_aOrgTrustManager
== null )
58 TrustManagerFactory aFactory
= TrustManagerFactory
.getInstance( TrustManagerFactory
.getDefaultAlgorithm() );
59 aFactory
.init( (KeyStore
)null );
60 TrustManager
[] pTrustmanagers
= aFactory
.getTrustManagers();
61 if ( pTrustmanagers
.length
!= 0 && pTrustmanagers
[0] != null )
62 m_aOrgTrustManager
= (X509TrustManager
)pTrustmanagers
[0];
66 throw new RuntimeException( "No access to the default trust manager!", e
);
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
);
88 for ( int nInd
= 0; nInd
< certs
.length
; nInd
++ )
89 certs
[nInd
].checkValidity();
96 SSLContext aContext
= SSLContext
.getInstance("SSL");
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 HttpClientError();
110 return m_aSSLContext
;
113 public Socket
createSocket( String sHost
, int nPort
, InetAddress clientHost
, int clientPort
)
114 throws IOException
, UnknownHostException
116 return GetNotSoSecureSSLContext().getSocketFactory().createSocket( sHost
, nPort
, clientHost
, clientPort
);
119 public Socket
createSocket( final String sHost
, final int nPort
, final InetAddress aLocalAddress
, final int nLocalPort
, final HttpConnectionParams params
)
120 throws IOException
, UnknownHostException
, ConnectTimeoutException
122 if ( params
== null )
123 return createSocket( sHost
, nPort
, aLocalAddress
, nLocalPort
);
125 int nTimeout
= params
.getConnectionTimeout();
126 Socket aSocket
= GetNotSoSecureSSLContext().getSocketFactory().createSocket();
127 aSocket
.bind( new InetSocketAddress( aLocalAddress
, nLocalPort
) );
128 aSocket
.connect( new InetSocketAddress( sHost
, nPort
), nTimeout
);
132 public Socket
createSocket( String sHost
, int nPort
)
133 throws IOException
, UnknownHostException
135 return GetNotSoSecureSSLContext().getSocketFactory().createSocket( sHost
, nPort
);
138 public Socket
createSocket( Socket aSocket
, String sHost
, int nPort
, boolean bAutoClose
)
139 throws IOException
, UnknownHostException
141 return GetNotSoSecureSSLContext().getSocketFactory().createSocket( aSocket
, sHost
, nPort
, bAutoClose
);
145 public boolean equals(Object obj
)
147 return ((obj
!= null) && obj
.getClass().equals(WikiProtocolSocketFactory
.class));
151 public int hashCode()
153 return WikiProtocolSocketFactory
.class.hashCode();