a329807a97451c54bb7e8589498258e0add8208f
[pub/Android/ownCloud.git] / oc_framework / src / com / owncloud / android / oc_framework / network / AdvancedX509TrustManager.java
1 package com.owncloud.android.oc_framework.network;
2 /* ownCloud Android client application
3 * Copyright (C) 2012-2013 ownCloud Inc.
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2,
7 * as published by the Free Software Foundation.
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
19
20
21 import java.security.KeyStore;
22 import java.security.KeyStoreException;
23 import java.security.NoSuchAlgorithmException;
24 import java.security.cert.CertPathValidatorException;
25 import java.security.cert.CertStoreException;
26 import java.security.cert.CertificateException;
27 import java.security.cert.CertificateExpiredException;
28 import java.security.cert.CertificateNotYetValidException;
29 import java.security.cert.X509Certificate;
30
31 import javax.net.ssl.TrustManager;
32 import javax.net.ssl.TrustManagerFactory;
33 import javax.net.ssl.X509TrustManager;
34
35 import android.util.Log;
36
37
38 /**
39 * @author David A. Velasco
40 */
41 public class AdvancedX509TrustManager implements X509TrustManager {
42
43 private static final String TAG = AdvancedX509TrustManager.class.getSimpleName();
44
45 private X509TrustManager mStandardTrustManager = null;
46 private KeyStore mKnownServersKeyStore;
47
48 /**
49 * Constructor for AdvancedX509TrustManager
50 *
51 * @param knownServersCertStore Local certificates store with server certificates explicitly trusted by the user.
52 * @throws CertStoreException When no default X509TrustManager instance was found in the system.
53 */
54 public AdvancedX509TrustManager(KeyStore knownServersKeyStore)
55 throws NoSuchAlgorithmException, KeyStoreException, CertStoreException {
56 super();
57 TrustManagerFactory factory = TrustManagerFactory
58 .getInstance(TrustManagerFactory.getDefaultAlgorithm());
59 factory.init((KeyStore)null);
60 mStandardTrustManager = findX509TrustManager(factory);
61
62 mKnownServersKeyStore = knownServersKeyStore;
63 }
64
65
66 /**
67 * Locates the first X509TrustManager provided by a given TrustManagerFactory
68 * @param factory TrustManagerFactory to inspect in the search for a X509TrustManager
69 * @return The first X509TrustManager found in factory.
70 * @throws CertStoreException When no X509TrustManager instance was found in factory
71 */
72 private X509TrustManager findX509TrustManager(TrustManagerFactory factory) throws CertStoreException {
73 TrustManager tms[] = factory.getTrustManagers();
74 for (int i = 0; i < tms.length; i++) {
75 if (tms[i] instanceof X509TrustManager) {
76 return (X509TrustManager) tms[i];
77 }
78 }
79 return null;
80 }
81
82
83 /**
84 * @see javax.net.ssl.X509TrustManager#checkClientTrusted(X509Certificate[],
85 * String authType)
86 */
87 public void checkClientTrusted(X509Certificate[] certificates, String authType) throws CertificateException {
88 mStandardTrustManager.checkClientTrusted(certificates, authType);
89 }
90
91
92 /**
93 * @see javax.net.ssl.X509TrustManager#checkServerTrusted(X509Certificate[],
94 * String authType)
95 */
96 public void checkServerTrusted(X509Certificate[] certificates, String authType) throws CertificateException {
97 if (!isKnownServer(certificates[0])) {
98 CertificateCombinedException result = new CertificateCombinedException(certificates[0]);
99 try {
100 certificates[0].checkValidity();
101 } catch (CertificateExpiredException c) {
102 result.setCertificateExpiredException(c);
103
104 } catch (CertificateNotYetValidException c) {
105 result.setCertificateNotYetException(c);
106 }
107
108 try {
109 mStandardTrustManager.checkServerTrusted(certificates, authType);
110 } catch (CertificateException c) {
111 Throwable cause = c.getCause();
112 Throwable previousCause = null;
113 while (cause != null && cause != previousCause && !(cause instanceof CertPathValidatorException)) { // getCause() is not funny
114 previousCause = cause;
115 cause = cause.getCause();
116 }
117 if (cause != null && cause instanceof CertPathValidatorException) {
118 result.setCertPathValidatorException((CertPathValidatorException)cause);
119 } else {
120 result.setOtherCertificateException(c);
121 }
122 }
123
124 if (result.isException())
125 throw result;
126
127 }
128 }
129
130
131 /**
132 * @see javax.net.ssl.X509TrustManager#getAcceptedIssuers()
133 */
134 public X509Certificate[] getAcceptedIssuers() {
135 return mStandardTrustManager.getAcceptedIssuers();
136 }
137
138
139 public boolean isKnownServer(X509Certificate cert) {
140 try {
141 return (mKnownServersKeyStore.getCertificateAlias(cert) != null);
142 } catch (KeyStoreException e) {
143 Log.d(TAG, "Fail while checking certificate in the known-servers store");
144 return false;
145 }
146 }
147
148 }