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/>.
18 package com
.owncloud
.android
.network
;
20 import java
.security
.KeyStore
;
21 import java
.security
.KeyStoreException
;
22 import java
.security
.NoSuchAlgorithmException
;
23 import java
.security
.cert
.CertPathValidatorException
;
24 import java
.security
.cert
.CertStoreException
;
25 import java
.security
.cert
.CertificateException
;
26 import java
.security
.cert
.CertificateExpiredException
;
27 import java
.security
.cert
.CertificateNotYetValidException
;
28 import java
.security
.cert
.X509Certificate
;
30 import javax
.net
.ssl
.TrustManager
;
31 import javax
.net
.ssl
.TrustManagerFactory
;
32 import javax
.net
.ssl
.X509TrustManager
;
34 import com
.owncloud
.android
.Log_OC
;
37 * @author David A. Velasco
39 public class AdvancedX509TrustManager
implements X509TrustManager
{
41 private static final String TAG
= AdvancedX509TrustManager
.class.getSimpleName();
43 private X509TrustManager mStandardTrustManager
= null
;
44 private KeyStore mKnownServersKeyStore
;
47 * Constructor for AdvancedX509TrustManager
49 * @param knownServersCertStore Local certificates store with server certificates explicitly trusted by the user.
50 * @throws CertStoreException When no default X509TrustManager instance was found in the system.
52 public AdvancedX509TrustManager(KeyStore knownServersKeyStore
)
53 throws NoSuchAlgorithmException
, KeyStoreException
, CertStoreException
{
55 TrustManagerFactory factory
= TrustManagerFactory
56 .getInstance(TrustManagerFactory
.getDefaultAlgorithm());
57 factory
.init((KeyStore
)null
);
58 mStandardTrustManager
= findX509TrustManager(factory
);
60 mKnownServersKeyStore
= knownServersKeyStore
;
65 * Locates the first X509TrustManager provided by a given TrustManagerFactory
66 * @param factory TrustManagerFactory to inspect in the search for a X509TrustManager
67 * @return The first X509TrustManager found in factory.
68 * @throws CertStoreException When no X509TrustManager instance was found in factory
70 private X509TrustManager
findX509TrustManager(TrustManagerFactory factory
) throws CertStoreException
{
71 TrustManager tms
[] = factory
.getTrustManagers();
72 for (int i
= 0; i
< tms
.length
; i
++) {
73 if (tms
[i
] instanceof X509TrustManager
) {
74 return (X509TrustManager
) tms
[i
];
82 * @see javax.net.ssl.X509TrustManager#checkClientTrusted(X509Certificate[],
85 public void checkClientTrusted(X509Certificate
[] certificates
, String authType
) throws CertificateException
{
86 mStandardTrustManager
.checkClientTrusted(certificates
, authType
);
91 * @see javax.net.ssl.X509TrustManager#checkServerTrusted(X509Certificate[],
94 public void checkServerTrusted(X509Certificate
[] certificates
, String authType
) throws CertificateException
{
95 if (!isKnownServer(certificates
[0])) {
96 CertificateCombinedException result
= new CertificateCombinedException(certificates
[0]);
98 certificates
[0].checkValidity();
99 } catch (CertificateExpiredException c
) {
100 result
.setCertificateExpiredException(c
);
102 } catch (CertificateNotYetValidException c
) {
103 result
.setCertificateNotYetException(c
);
107 mStandardTrustManager
.checkServerTrusted(certificates
, authType
);
108 } catch (CertificateException c
) {
109 Throwable cause
= c
.getCause();
110 Throwable previousCause
= null
;
111 while (cause
!= null
&& cause
!= previousCause
&& !(cause
instanceof CertPathValidatorException
)) { // getCause() is not funny
112 previousCause
= cause
;
113 cause
= cause
.getCause();
115 if (cause
!= null
&& cause
instanceof CertPathValidatorException
) {
116 result
.setCertPathValidatorException((CertPathValidatorException
)cause
);
118 result
.setOtherCertificateException(c
);
122 if (result
.isException())
130 * @see javax.net.ssl.X509TrustManager#getAcceptedIssuers()
132 public X509Certificate
[] getAcceptedIssuers() {
133 return mStandardTrustManager
.getAcceptedIssuers();
137 public boolean isKnownServer(X509Certificate cert
) {
139 return (mKnownServersKeyStore
.getCertificateAlias(cert
) != null
);
140 } catch (KeyStoreException e
) {
141 Log_OC
.d(TAG
, "Fail while checking certificate in the known-servers store");