2008-11-01 17:32:06 -04:00
|
|
|
|
2013-12-02 14:07:57 -05:00
|
|
|
package com.fsck.k9.net.ssl;
|
2008-11-01 17:32:06 -04:00
|
|
|
|
2009-12-09 22:16:42 -05:00
|
|
|
import android.util.Log;
|
2013-12-03 07:28:48 -05:00
|
|
|
|
2010-05-19 15:16:36 -04:00
|
|
|
import com.fsck.k9.helper.DomainNameChecker;
|
2013-08-25 15:43:36 -04:00
|
|
|
import com.fsck.k9.mail.CertificateChainException;
|
2013-12-02 14:07:57 -05:00
|
|
|
import com.fsck.k9.security.LocalKeyStore;
|
2013-08-25 15:43:36 -04:00
|
|
|
|
2009-12-09 22:16:42 -05:00
|
|
|
import javax.net.ssl.TrustManager;
|
|
|
|
import javax.net.ssl.X509TrustManager;
|
2008-11-01 17:32:06 -04:00
|
|
|
import java.security.KeyStore;
|
|
|
|
import java.security.KeyStoreException;
|
2009-12-09 22:16:42 -05:00
|
|
|
import java.security.NoSuchAlgorithmException;
|
2008-11-01 17:32:06 -04:00
|
|
|
import java.security.cert.CertificateException;
|
2009-12-09 22:16:42 -05:00
|
|
|
import java.security.cert.X509Certificate;
|
2010-07-27 14:59:41 -04:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
2008-12-02 19:04:24 -05:00
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
public final class TrustManagerFactory {
|
2008-11-01 17:32:06 -04:00
|
|
|
private static final String LOG_TAG = "TrustManagerFactory";
|
|
|
|
|
2008-12-02 19:04:24 -05:00
|
|
|
private static X509TrustManager defaultTrustManager;
|
|
|
|
private static X509TrustManager unsecureTrustManager;
|
2009-11-24 19:40:29 -05:00
|
|
|
|
2013-12-02 14:04:40 -05:00
|
|
|
private static LocalKeyStore keyStore;
|
2008-11-01 17:32:06 -04:00
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
private static class SimpleX509TrustManager implements X509TrustManager {
|
2008-11-01 17:32:06 -04:00
|
|
|
public void checkClientTrusted(X509Certificate[] chain, String authType)
|
2011-02-06 17:09:48 -05:00
|
|
|
throws CertificateException {
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public void checkServerTrusted(X509Certificate[] chain, String authType)
|
2011-02-06 17:09:48 -05:00
|
|
|
throws CertificateException {
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
public X509Certificate[] getAcceptedIssuers() {
|
2008-11-01 17:32:06 -04:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
private static class SecureX509TrustManager implements X509TrustManager {
|
2010-07-27 14:59:41 -04:00
|
|
|
private static final Map<String, SecureX509TrustManager> mTrustManager =
|
|
|
|
new HashMap<String, SecureX509TrustManager>();
|
2008-11-01 17:32:06 -04:00
|
|
|
|
2010-07-27 14:59:41 -04:00
|
|
|
private final String mHost;
|
2013-11-23 13:26:57 -05:00
|
|
|
private final int mPort;
|
2010-07-27 14:59:41 -04:00
|
|
|
|
2013-11-23 13:26:57 -05:00
|
|
|
private SecureX509TrustManager(String host, int port) {
|
2010-07-27 14:59:41 -04:00
|
|
|
mHost = host;
|
2013-11-23 13:26:57 -05:00
|
|
|
mPort = port;
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
|
|
|
|
2013-11-23 13:26:57 -05:00
|
|
|
public synchronized static X509TrustManager getInstance(String host, int port) {
|
2013-12-02 14:04:40 -05:00
|
|
|
String key = host + ":" + port;
|
2010-07-27 14:59:41 -04:00
|
|
|
SecureX509TrustManager trustManager;
|
2013-11-23 13:26:57 -05:00
|
|
|
if (mTrustManager.containsKey(key)) {
|
|
|
|
trustManager = mTrustManager.get(key);
|
2011-02-06 17:09:48 -05:00
|
|
|
} else {
|
2013-11-23 13:26:57 -05:00
|
|
|
trustManager = new SecureX509TrustManager(host, port);
|
|
|
|
mTrustManager.put(key, trustManager);
|
2009-11-24 19:40:29 -05:00
|
|
|
}
|
2010-07-27 14:59:41 -04:00
|
|
|
|
|
|
|
return trustManager;
|
2009-11-24 19:40:29 -05:00
|
|
|
}
|
2008-12-02 19:04:24 -05:00
|
|
|
|
2008-11-01 17:32:06 -04:00
|
|
|
public void checkClientTrusted(X509Certificate[] chain, String authType)
|
2011-02-06 17:09:48 -05:00
|
|
|
throws CertificateException {
|
2008-12-02 19:04:24 -05:00
|
|
|
defaultTrustManager.checkClientTrusted(chain, authType);
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public void checkServerTrusted(X509Certificate[] chain, String authType)
|
2013-11-29 07:56:09 -05:00
|
|
|
throws CertificateException {
|
|
|
|
boolean foundInGlobalKeyStore = false;
|
2011-02-06 17:09:48 -05:00
|
|
|
try {
|
2009-11-24 19:40:29 -05:00
|
|
|
defaultTrustManager.checkServerTrusted(chain, authType);
|
2013-11-29 07:56:09 -05:00
|
|
|
foundInGlobalKeyStore = true;
|
|
|
|
} catch (CertificateException e) { /* ignore */ }
|
|
|
|
|
|
|
|
X509Certificate certificate = chain[0];
|
|
|
|
|
|
|
|
// Check the local key store if we couldn't verify the certificate using the global
|
|
|
|
// key store or if the host name doesn't match the certificate name
|
2013-12-02 14:04:40 -05:00
|
|
|
if (foundInGlobalKeyStore
|
|
|
|
&& DomainNameChecker.match(certificate, mHost)
|
|
|
|
|| keyStore.isValidCertificate(certificate, mHost, mPort)) {
|
|
|
|
return;
|
2009-11-24 19:40:29 -05:00
|
|
|
}
|
2013-12-02 14:04:40 -05:00
|
|
|
String message = (foundInGlobalKeyStore) ?
|
|
|
|
"Certificate domain name does not match " + mHost :
|
|
|
|
"Couldn't find certificate in local key store";
|
|
|
|
|
|
|
|
throw new CertificateChainException(message, chain);
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
public X509Certificate[] getAcceptedIssuers() {
|
2008-12-02 19:04:24 -05:00
|
|
|
return defaultTrustManager.getAcceptedIssuers();
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
2008-12-02 19:04:24 -05:00
|
|
|
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
static {
|
|
|
|
try {
|
2013-12-03 19:20:20 -05:00
|
|
|
keyStore = LocalKeyStore.getInstance();
|
2013-11-29 07:56:09 -05:00
|
|
|
|
|
|
|
javax.net.ssl.TrustManagerFactory tmf = javax.net.ssl.TrustManagerFactory.getInstance("X509");
|
|
|
|
tmf.init((KeyStore) null);
|
|
|
|
|
2008-11-01 17:32:06 -04:00
|
|
|
TrustManager[] tms = tmf.getTrustManagers();
|
2011-02-06 17:09:48 -05:00
|
|
|
if (tms != null) {
|
|
|
|
for (TrustManager tm : tms) {
|
|
|
|
if (tm instanceof X509TrustManager) {
|
2008-12-02 19:04:24 -05:00
|
|
|
defaultTrustManager = (X509TrustManager) tm;
|
2008-11-01 17:32:06 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-02-06 17:09:48 -05:00
|
|
|
} catch (NoSuchAlgorithmException e) {
|
2008-11-01 17:32:06 -04:00
|
|
|
Log.e(LOG_TAG, "Unable to get X509 Trust Manager ", e);
|
2011-02-06 17:09:48 -05:00
|
|
|
} catch (KeyStoreException e) {
|
2008-11-01 17:32:06 -04:00
|
|
|
Log.e(LOG_TAG, "Key Store exception while initializing TrustManagerFactory ", e);
|
2013-11-29 04:49:52 -05:00
|
|
|
}
|
|
|
|
unsecureTrustManager = new SimpleX509TrustManager();
|
|
|
|
}
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
private TrustManagerFactory() {
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
|
|
|
|
2013-11-23 13:26:57 -05:00
|
|
|
public static X509TrustManager get(String host, int port, boolean secure) {
|
|
|
|
return secure ? SecureX509TrustManager.getInstance(host, port) :
|
2009-11-24 19:40:29 -05:00
|
|
|
unsecureTrustManager;
|
2008-12-02 19:04:24 -05:00
|
|
|
}
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|