1 /* ownCloud Android client application
2 * Copyright (C) 2012-2013 ownCloud Inc.
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2,
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package com
.owncloud
.android
.network
;
20 import java
.io
.FileInputStream
;
21 import java
.io
.FileOutputStream
;
22 import java
.io
.IOException
;
23 import java
.io
.InputStream
;
24 import java
.security
.GeneralSecurityException
;
25 import java
.security
.KeyStore
;
26 import java
.security
.KeyStoreException
;
27 import java
.security
.NoSuchAlgorithmException
;
28 import java
.security
.cert
.Certificate
;
29 import java
.security
.cert
.CertificateException
;
31 import javax
.net
.ssl
.SSLContext
;
32 import javax
.net
.ssl
.TrustManager
;
34 import org
.apache
.commons
.httpclient
.MultiThreadedHttpConnectionManager
;
35 import org
.apache
.commons
.httpclient
.protocol
.Protocol
;
36 import org
.apache
.http
.conn
.ssl
.BrowserCompatHostnameVerifier
;
37 import org
.apache
.http
.conn
.ssl
.X509HostnameVerifier
;
39 import com
.owncloud
.android
.AccountUtils
;
40 import com
.owncloud
.android
.Log_OC
;
42 import eu
.alefzero
.webdav
.WebdavClient
;
44 import android
.accounts
.Account
;
45 import android
.accounts
.AccountManager
;
46 import android
.content
.Context
;
47 import android
.net
.Uri
;
48 import android
.util
.Log
;
50 public class OwnCloudClientUtils
{
52 final private static String TAG
= "OwnCloudClientFactory";
54 /** Default timeout for waiting data from the server */
55 public static final int DEFAULT_DATA_TIMEOUT
= 60000;
57 /** Default timeout for establishing a connection */
58 public static final int DEFAULT_CONNECTION_TIMEOUT
= 60000;
60 /** Connection manager for all the WebdavClients */
61 private static MultiThreadedHttpConnectionManager mConnManager
= null
;
63 private static Protocol mDefaultHttpsProtocol
= null
;
65 private static AdvancedSslSocketFactory mAdvancedSslSocketFactory
= null
;
67 private static X509HostnameVerifier mHostnameVerifier
= null
;
71 * Creates a WebdavClient setup for an ownCloud account
73 * @param account The ownCloud account
74 * @param context The application context
75 * @return A WebdavClient object ready to be used
77 public static WebdavClient
createOwnCloudClient (Account account
, Context context
) {
78 Log_OC
.d(TAG
, "Creating WebdavClient associated to " + account
.name
);
80 Uri uri
= Uri
.parse(AccountUtils
.constructFullURLForAccount(context
, account
));
81 WebdavClient client
= createOwnCloudClient(uri
, context
);
83 String username
= account
.name
.substring(0, account
.name
.lastIndexOf('@'));
84 String password
= AccountManager
.get(context
).getPassword(account
);
85 //String password = am.blockingGetAuthToken(mAccount, AccountAuthenticator.AUTH_TOKEN_TYPE, true);
87 client
.setCredentials(username
, password
);
94 * Creates a WebdavClient to try a new account before saving it
96 * @param uri URL to the ownCloud server
97 * @param username User name
98 * @param password User password
99 * @param context Android context where the WebdavClient is being created.
100 * @return A WebdavClient object ready to be used
102 public static WebdavClient
createOwnCloudClient(Uri uri
, String username
, String password
, Context context
) {
103 Log_OC
.d(TAG
, "Creating WebdavClient for " + username
+ "@" + uri
);
105 WebdavClient client
= createOwnCloudClient(uri
, context
);
107 client
.setCredentials(username
, password
);
114 * Creates a WebdavClient to access a URL and sets the desired parameters for ownCloud client connections.
116 * @param uri URL to the ownCloud server
117 * @param context Android context where the WebdavClient is being created.
118 * @return A WebdavClient object ready to be used
120 public static WebdavClient
createOwnCloudClient(Uri uri
, Context context
) {
121 Log_OC
.d(TAG
, "Creating WebdavClient for " + uri
);
123 //allowSelfsignedCertificates(true);
125 registerAdvancedSslContext(true
, context
);
126 } catch (GeneralSecurityException e
) {
127 Log_OC
.e(TAG
, "Advanced SSL Context could not be loaded. Default SSL management in the system will be used for HTTPS connections", e
);
129 } catch (IOException e
) {
130 Log_OC
.e(TAG
, "The local server truststore could not be read. Default SSL management in the system will be used for HTTPS connections", e
);
133 WebdavClient client
= new WebdavClient(getMultiThreadedConnManager());
135 client
.setDefaultTimeouts(DEFAULT_DATA_TIMEOUT
, DEFAULT_CONNECTION_TIMEOUT
);
136 client
.setBaseUri(uri
);
143 * Registers or unregisters the proper components for advanced SSL handling.
144 * @throws IOException
146 private static void registerAdvancedSslContext(boolean register
, Context context
) throws GeneralSecurityException
, IOException
{
149 pr
= Protocol
.getProtocol("https");
150 if (pr
!= null
&& mDefaultHttpsProtocol
== null
) {
151 mDefaultHttpsProtocol
= pr
;
153 } catch (IllegalStateException e
) {
154 // nothing to do here; really
156 boolean isRegistered
= (pr
!= null
&& pr
.getSocketFactory() instanceof AdvancedSslSocketFactory
);
157 if (register
&& !isRegistered
) {
158 Protocol
.registerProtocol("https", new Protocol("https", getAdvancedSslSocketFactory(context
), 443));
160 } else if (!register
&& isRegistered
) {
161 if (mDefaultHttpsProtocol
!= null
) {
162 Protocol
.registerProtocol("https", mDefaultHttpsProtocol
);
167 public static AdvancedSslSocketFactory
getAdvancedSslSocketFactory(Context context
) throws GeneralSecurityException
, IOException
{
168 if (mAdvancedSslSocketFactory
== null
) {
169 KeyStore trustStore
= getKnownServersStore(context
);
170 AdvancedX509TrustManager trustMgr
= new AdvancedX509TrustManager(trustStore
);
171 TrustManager
[] tms
= new TrustManager
[] { trustMgr
};
173 SSLContext sslContext
= SSLContext
.getInstance("TLS");
174 sslContext
.init(null
, tms
, null
);
176 mHostnameVerifier
= new BrowserCompatHostnameVerifier();
177 mAdvancedSslSocketFactory
= new AdvancedSslSocketFactory(sslContext
, trustMgr
, mHostnameVerifier
);
179 return mAdvancedSslSocketFactory
;
183 private static String LOCAL_TRUSTSTORE_FILENAME
= "knownServers.bks";
185 private static String LOCAL_TRUSTSTORE_PASSWORD
= "password";
187 private static KeyStore mKnownServersStore
= null
;
190 * Returns the local store of reliable server certificates, explicitly accepted by the user.
192 * Returns a KeyStore instance with empty content if the local store was never created.
194 * Loads the store from the storage environment if needed.
196 * @param context Android context where the operation is being performed.
197 * @return KeyStore instance with explicitly-accepted server certificates.
198 * @throws KeyStoreException When the KeyStore instance could not be created.
199 * @throws IOException When an existing local trust store could not be loaded.
200 * @throws NoSuchAlgorithmException When the existing local trust store was saved with an unsupported algorithm.
201 * @throws CertificateException When an exception occurred while loading the certificates from the local trust store.
203 private static KeyStore
getKnownServersStore(Context context
) throws KeyStoreException
, IOException
, NoSuchAlgorithmException
, CertificateException
{
204 if (mKnownServersStore
== null
) {
205 //mKnownServersStore = KeyStore.getInstance("BKS");
206 mKnownServersStore
= KeyStore
.getInstance(KeyStore
.getDefaultType());
207 File localTrustStoreFile
= new File(context
.getFilesDir(), LOCAL_TRUSTSTORE_FILENAME
);
208 Log_OC
.d(TAG
, "Searching known-servers store at " + localTrustStoreFile
.getAbsolutePath());
209 if (localTrustStoreFile
.exists()) {
210 InputStream
in = new FileInputStream(localTrustStoreFile
);
212 mKnownServersStore
.load(in, LOCAL_TRUSTSTORE_PASSWORD
.toCharArray());
217 mKnownServersStore
.load(null
, LOCAL_TRUSTSTORE_PASSWORD
.toCharArray()); // necessary to initialize an empty KeyStore instance
220 return mKnownServersStore
;
224 public static void addCertToKnownServersStore(Certificate cert
, Context context
) throws KeyStoreException
, NoSuchAlgorithmException
,
225 CertificateException
, IOException
{
226 KeyStore knownServers
= getKnownServersStore(context
);
227 knownServers
.setCertificateEntry(Integer
.toString(cert
.hashCode()), cert
);
228 FileOutputStream fos
= null
;
230 fos
= context
.openFileOutput(LOCAL_TRUSTSTORE_FILENAME
, Context
.MODE_PRIVATE
);
231 knownServers
.store(fos
, LOCAL_TRUSTSTORE_PASSWORD
.toCharArray());
238 static private MultiThreadedHttpConnectionManager
getMultiThreadedConnManager() {
239 if (mConnManager
== null
) {
240 mConnManager
= new MultiThreadedHttpConnectionManager();
241 mConnManager
.getParams().setDefaultMaxConnectionsPerHost(5);
242 mConnManager
.getParams().setMaxTotalConnections(5);