Fixed crash when the device is turned while the warning dialog about server certifica...
[pub/Android/ownCloud.git] / src / com / owncloud / android / network / OwnCloudClientUtils.java
1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 *
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.
8 *
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.
13 *
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/>.
16 *
17 */
18 package com.owncloud.android.network;
19
20 import java.io.File;
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;
31
32 import javax.net.ssl.SSLContext;
33 import javax.net.ssl.TrustManager;
34
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;
39
40 import com.owncloud.android.AccountUtils;
41 import com.owncloud.android.authenticator.AccountAuthenticator;
42 import com.owncloud.android.utils.OwnCloudVersion;
43
44 import eu.alefzero.webdav.WebdavClient;
45
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;
51
52 public class OwnCloudClientUtils {
53
54 final private static String TAG = "OwnCloudClientFactory";
55
56 /** Default timeout for waiting data from the server */
57 public static final int DEFAULT_DATA_TIMEOUT = 60000;
58
59 /** Default timeout for establishing a connection */
60 public static final int DEFAULT_CONNECTION_TIMEOUT = 60000;
61
62 /** Connection manager for all the WebdavClients */
63 private static MultiThreadedHttpConnectionManager mConnManager = null;
64
65 private static Protocol mDefaultHttpsProtocol = null;
66
67 private static AdvancedSslSocketFactory mAdvancedSslSocketFactory = null;
68
69 private static X509HostnameVerifier mHostnameVerifier = null;
70
71
72 /**
73 * Creates a WebdavClient setup for an ownCloud account
74 *
75 * @param account The ownCloud account
76 * @param context The application context
77 * @return A WebdavClient object ready to be used
78 */
79 public static WebdavClient createOwnCloudClient (Account account, Context context) {
80 Log.d(TAG, "Creating WebdavClient associated to " + account.name);
81
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);
85
86 WebdavClient client = createOwnCloudClient(Uri.parse(baseUrl + webDavPath), context);
87
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);
91
92 client.setCredentials(username, password);
93
94 return client;
95 }
96
97
98 /**
99 * Creates a WebdavClient to try a new account before saving it
100 *
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
106 */
107 public static WebdavClient createOwnCloudClient(Uri uri, String username, String password, Context context) {
108 Log.d(TAG, "Creating WebdavClient for " + username + "@" + uri);
109
110 WebdavClient client = createOwnCloudClient(uri, context);
111
112 client.setCredentials(username, password);
113
114 return client;
115 }
116
117
118 /**
119 * Creates a WebdavClient to access a URL and sets the desired parameters for ownCloud client connections.
120 *
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
124 */
125 public static WebdavClient createOwnCloudClient(Uri uri, Context context) {
126 Log.d(TAG, "Creating WebdavClient for " + uri);
127
128 //allowSelfsignedCertificates(true);
129 try {
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);
133
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);
136 }
137
138 WebdavClient client = new WebdavClient(getMultiThreadedConnManager());
139
140 client.setDefaultTimeouts(DEFAULT_DATA_TIMEOUT, DEFAULT_CONNECTION_TIMEOUT);
141 client.setBaseUri(uri);
142
143 return client;
144 }
145
146
147 /**
148 * Allows or disallows self-signed certificates in ownCloud servers to reach
149 *
150 * @param allow 'True' to allow, 'false' to disallow
151 */
152 public static void allowSelfsignedCertificates(boolean allow) {
153 Protocol pr = null;
154 try {
155 pr = Protocol.getProtocol("https");
156 if (pr != null && mDefaultHttpsProtocol == null) {
157 mDefaultHttpsProtocol = pr;
158 }
159 } catch (IllegalStateException e) {
160 // nothing to do here; really
161 }
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);
168 }
169 }
170 }
171
172
173 /**
174 * Registers or unregisters the proper components for advanced SSL handling.
175 * @throws IOException
176 */
177 private static void registerAdvancedSslContext(boolean register, Context context) throws GeneralSecurityException, IOException {
178 Protocol pr = null;
179 try {
180 pr = Protocol.getProtocol("https");
181 if (pr != null && mDefaultHttpsProtocol == null) {
182 mDefaultHttpsProtocol = pr;
183 }
184 } catch (IllegalStateException e) {
185 // nothing to do here; really
186 }
187 boolean isRegistered = (pr != null && pr.getSocketFactory() instanceof AdvancedSslSocketFactory);
188 if (register && !isRegistered) {
189 Protocol.registerProtocol("https", new Protocol("https", getAdvancedSslSocketFactory(context), 443));
190
191 } else if (!register && isRegistered) {
192 if (mDefaultHttpsProtocol != null) {
193 Protocol.registerProtocol("https", mDefaultHttpsProtocol);
194 }
195 }
196 }
197
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 };
203
204 SSLContext sslContext = SSLContext.getInstance("TLS");
205 sslContext.init(null, tms, null);
206
207 mHostnameVerifier = new BrowserCompatHostnameVerifier();
208 mAdvancedSslSocketFactory = new AdvancedSslSocketFactory(sslContext, trustMgr, mHostnameVerifier);
209 }
210 return mAdvancedSslSocketFactory;
211 }
212
213
214 private static String LOCAL_TRUSTSTORE_FILENAME = "knownServers.bks";
215
216 private static String LOCAL_TRUSTSTORE_PASSWORD = "password";
217
218 private static KeyStore mKnownServersStore = null;
219
220 /**
221 * Returns the local store of reliable server certificates, explicitly accepted by the user.
222 *
223 * Returns a KeyStore instance with empty content if the local store was never created.
224 *
225 * Loads the store from the storage environment if needed.
226 *
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.
233 */
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);
242 try {
243 mKnownServersStore.load(in, LOCAL_TRUSTSTORE_PASSWORD.toCharArray());
244 } finally {
245 in.close();
246 }
247 } else {
248 mKnownServersStore.load(null, LOCAL_TRUSTSTORE_PASSWORD.toCharArray()); // necessary to initialize an empty KeyStore instance
249 }
250 }
251 return mKnownServersStore;
252 }
253
254
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;
260 try {
261 fos = context.openFileOutput(LOCAL_TRUSTSTORE_FILENAME, Context.MODE_PRIVATE);
262 knownServers.store(fos, LOCAL_TRUSTSTORE_PASSWORD.toCharArray());
263 } finally {
264 fos.close();
265 }
266 }
267
268
269 static private MultiThreadedHttpConnectionManager getMultiThreadedConnManager() {
270 if (mConnManager == null) {
271 mConnManager = new MultiThreadedHttpConnectionManager();
272 mConnManager.getParams().setDefaultMaxConnectionsPerHost(5);
273 mConnManager.getParams().setMaxTotalConnections(5);
274 }
275 return mConnManager;
276 }
277
278 }