1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package com
.owncloud
.android
.network
;
21 import java
.io
.FileInputStream
;
22 import java
.io
.FileOutputStream
;
23 import java
.io
.IOException
;
24 import java
.io
.InputStream
;
25 import java
.security
.GeneralSecurityException
;
26 import java
.security
.KeyStore
;
27 import java
.security
.KeyStoreException
;
28 import java
.security
.NoSuchAlgorithmException
;
29 import java
.security
.cert
.Certificate
;
30 import java
.security
.cert
.CertificateException
;
32 import javax
.net
.ssl
.SSLContext
;
33 import javax
.net
.ssl
.TrustManager
;
35 import org
.apache
.commons
.httpclient
.MultiThreadedHttpConnectionManager
;
36 import org
.apache
.commons
.httpclient
.protocol
.Protocol
;
37 import org
.apache
.http
.conn
.ssl
.BrowserCompatHostnameVerifier
;
38 import org
.apache
.http
.conn
.ssl
.X509HostnameVerifier
;
40 import com
.owncloud
.android
.AccountUtils
;
41 import com
.owncloud
.android
.authenticator
.AccountAuthenticator
;
42 import com
.owncloud
.android
.utils
.OwnCloudVersion
;
44 import eu
.alefzero
.webdav
.WebdavClient
;
46 import android
.accounts
.Account
;
47 import android
.accounts
.AccountManager
;
48 import android
.content
.Context
;
49 import android
.net
.Uri
;
50 import android
.util
.Log
;
52 public class OwnCloudClientUtils
{
54 final private static String TAG
= "OwnCloudClientFactory";
56 /** Default timeout for waiting data from the server */
57 public static final int DEFAULT_DATA_TIMEOUT
= 60000;
59 /** Default timeout for establishing a connection */
60 public static final int DEFAULT_CONNECTION_TIMEOUT
= 60000;
62 /** Connection manager for all the WebdavClients */
63 private static MultiThreadedHttpConnectionManager mConnManager
= null
;
65 private static Protocol mDefaultHttpsProtocol
= null
;
67 private static AdvancedSslSocketFactory mAdvancedSslSocketFactory
= null
;
69 private static X509HostnameVerifier mHostnameVerifier
= null
;
73 * Creates a WebdavClient setup for an ownCloud account
75 * @param account The ownCloud account
76 * @param context The application context
77 * @return A WebdavClient object ready to be used
79 public static WebdavClient
createOwnCloudClient (Account account
, Context context
) {
80 Log
.d(TAG
, "Creating WebdavClient associated to " + account
.name
);
82 String baseUrl
= AccountManager
.get(context
).getUserData(account
, AccountAuthenticator
.KEY_OC_BASE_URL
);
83 OwnCloudVersion ownCloudVersion
= new OwnCloudVersion(AccountManager
.get(context
).getUserData(account
, AccountAuthenticator
.KEY_OC_VERSION
));
84 String webDavPath
= AccountUtils
.getWebdavPath(ownCloudVersion
);
86 WebdavClient client
= createOwnCloudClient(Uri
.parse(baseUrl
+ webDavPath
), context
);
88 String username
= account
.name
.substring(0, account
.name
.lastIndexOf('@'));
89 String password
= AccountManager
.get(context
).getPassword(account
);
90 //String password = am.blockingGetAuthToken(mAccount, AccountAuthenticator.AUTH_TOKEN_TYPE, true);
92 client
.setCredentials(username
, password
);
99 * Creates a WebdavClient to try a new account before saving it
101 * @param uri URL to the ownCloud server
102 * @param username User name
103 * @param password User password
104 * @param context Android context where the WebdavClient is being created.
105 * @return A WebdavClient object ready to be used
107 public static WebdavClient
createOwnCloudClient(Uri uri
, String username
, String password
, Context context
) {
108 Log
.d(TAG
, "Creating WebdavClient for " + username
+ "@" + uri
);
110 WebdavClient client
= createOwnCloudClient(uri
, context
);
112 client
.setCredentials(username
, password
);
119 * Creates a WebdavClient to access a URL and sets the desired parameters for ownCloud client connections.
121 * @param uri URL to the ownCloud server
122 * @param context Android context where the WebdavClient is being created.
123 * @return A WebdavClient object ready to be used
125 public static WebdavClient
createOwnCloudClient(Uri uri
, Context context
) {
126 Log
.d(TAG
, "Creating WebdavClient for " + uri
);
128 //allowSelfsignedCertificates(true);
130 registerAdvancedSslContext(true
, context
);
131 } catch (GeneralSecurityException e
) {
132 Log
.e(TAG
, "Advanced SSL Context could not be loaded. Default SSL management in the system will be used for HTTPS connections", e
);
134 } catch (IOException e
) {
135 Log
.e(TAG
, "The local server truststore could not be read. Default SSL management in the system will be used for HTTPS connections", e
);
138 WebdavClient client
= new WebdavClient(getMultiThreadedConnManager());
140 client
.setDefaultTimeouts(DEFAULT_DATA_TIMEOUT
, DEFAULT_CONNECTION_TIMEOUT
);
141 client
.setBaseUri(uri
);
148 * Allows or disallows self-signed certificates in ownCloud servers to reach
150 * @param allow 'True' to allow, 'false' to disallow
152 public static void allowSelfsignedCertificates(boolean allow
) {
155 pr
= Protocol
.getProtocol("https");
156 if (pr
!= null
&& mDefaultHttpsProtocol
== null
) {
157 mDefaultHttpsProtocol
= pr
;
159 } catch (IllegalStateException e
) {
160 // nothing to do here; really
162 boolean isAllowed
= (pr
!= null
&& pr
.getSocketFactory() instanceof EasySSLSocketFactory
);
163 if (allow
&& !isAllowed
) {
164 Protocol
.registerProtocol("https", new Protocol("https", new EasySSLSocketFactory(), 443));
165 } else if (!allow
&& isAllowed
) {
166 if (mDefaultHttpsProtocol
!= null
) {
167 Protocol
.registerProtocol("https", mDefaultHttpsProtocol
);
174 * Registers or unregisters the proper components for advanced SSL handling.
175 * @throws IOException
177 private static void registerAdvancedSslContext(boolean register
, Context context
) throws GeneralSecurityException
, IOException
{
180 pr
= Protocol
.getProtocol("https");
181 if (pr
!= null
&& mDefaultHttpsProtocol
== null
) {
182 mDefaultHttpsProtocol
= pr
;
184 } catch (IllegalStateException e
) {
185 // nothing to do here; really
187 boolean isRegistered
= (pr
!= null
&& pr
.getSocketFactory() instanceof AdvancedSslSocketFactory
);
188 if (register
&& !isRegistered
) {
189 Protocol
.registerProtocol("https", new Protocol("https", getAdvancedSslSocketFactory(context
), 443));
191 } else if (!register
&& isRegistered
) {
192 if (mDefaultHttpsProtocol
!= null
) {
193 Protocol
.registerProtocol("https", mDefaultHttpsProtocol
);
198 public static AdvancedSslSocketFactory
getAdvancedSslSocketFactory(Context context
) throws GeneralSecurityException
, IOException
{
199 if (mAdvancedSslSocketFactory
== null
) {
200 KeyStore trustStore
= getKnownServersStore(context
);
201 AdvancedX509TrustManager trustMgr
= new AdvancedX509TrustManager(trustStore
);
202 TrustManager
[] tms
= new TrustManager
[] { trustMgr
};
204 SSLContext sslContext
= SSLContext
.getInstance("TLS");
205 sslContext
.init(null
, tms
, null
);
207 mHostnameVerifier
= new BrowserCompatHostnameVerifier();
208 mAdvancedSslSocketFactory
= new AdvancedSslSocketFactory(sslContext
, trustMgr
, mHostnameVerifier
);
210 return mAdvancedSslSocketFactory
;
214 private static String LOCAL_TRUSTSTORE_FILENAME
= "knownServers.bks";
216 private static String LOCAL_TRUSTSTORE_PASSWORD
= "password";
218 private static KeyStore mKnownServersStore
= null
;
221 * Returns the local store of reliable server certificates, explicitly accepted by the user.
223 * Returns a KeyStore instance with empty content if the local store was never created.
225 * Loads the store from the storage environment if needed.
227 * @param context Android context where the operation is being performed.
228 * @return KeyStore instance with explicitly-accepted server certificates.
229 * @throws KeyStoreException When the KeyStore instance could not be created.
230 * @throws IOException When an existing local trust store could not be loaded.
231 * @throws NoSuchAlgorithmException When the existing local trust store was saved with an unsupported algorithm.
232 * @throws CertificateException When an exception occurred while loading the certificates from the local trust store.
234 private static KeyStore
getKnownServersStore(Context context
) throws KeyStoreException
, IOException
, NoSuchAlgorithmException
, CertificateException
{
235 if (mKnownServersStore
== null
) {
236 //mKnownServersStore = KeyStore.getInstance("BKS");
237 mKnownServersStore
= KeyStore
.getInstance(KeyStore
.getDefaultType());
238 File localTrustStoreFile
= new File(context
.getFilesDir(), LOCAL_TRUSTSTORE_FILENAME
);
239 Log
.d(TAG
, "Searching known-servers store at " + localTrustStoreFile
.getAbsolutePath());
240 if (localTrustStoreFile
.exists()) {
241 InputStream
in = new FileInputStream(localTrustStoreFile
);
243 mKnownServersStore
.load(in, LOCAL_TRUSTSTORE_PASSWORD
.toCharArray());
248 mKnownServersStore
.load(null
, LOCAL_TRUSTSTORE_PASSWORD
.toCharArray()); // necessary to initialize an empty KeyStore instance
251 return mKnownServersStore
;
255 public static void addCertToKnownServersStore(Certificate cert
, Context context
) throws KeyStoreException
, NoSuchAlgorithmException
,
256 CertificateException
, IOException
{
257 KeyStore knownServers
= getKnownServersStore(context
);
258 knownServers
.setCertificateEntry(Integer
.toString(cert
.hashCode()), cert
);
259 FileOutputStream fos
= null
;
261 fos
= context
.openFileOutput(LOCAL_TRUSTSTORE_FILENAME
, Context
.MODE_PRIVATE
);
262 knownServers
.store(fos
, LOCAL_TRUSTSTORE_PASSWORD
.toCharArray());
269 static private MultiThreadedHttpConnectionManager
getMultiThreadedConnManager() {
270 if (mConnManager
== null
) {
271 mConnManager
= new MultiThreadedHttpConnectionManager();
272 mConnManager
.getParams().setDefaultMaxConnectionsPerHost(5);
273 mConnManager
.getParams().setMaxTotalConnections(5);