2008-11-01 17:32:06 -04:00
|
|
|
|
2008-12-16 18:34:01 -05:00
|
|
|
package com.android.email.mail.store;
|
2008-11-01 17:32:06 -04:00
|
|
|
|
|
|
|
import android.util.Log;
|
2008-12-02 19:04:24 -05:00
|
|
|
import android.app.Application;
|
|
|
|
import android.content.Context;
|
2008-11-01 17:32:06 -04:00
|
|
|
import android.net.http.DomainNameChecker;
|
|
|
|
|
2008-12-02 19:04:24 -05:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
import java.io.IOException;
|
2008-11-01 17:32:06 -04:00
|
|
|
import java.security.KeyStore;
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
|
import java.security.KeyStoreException;
|
|
|
|
import java.security.cert.X509Certificate;
|
|
|
|
import java.security.cert.CertificateException;
|
|
|
|
|
|
|
|
import javax.net.ssl.X509TrustManager;
|
|
|
|
import javax.net.ssl.TrustManager;
|
|
|
|
|
2008-12-16 18:34:01 -05:00
|
|
|
import com.android.email.Email;
|
2008-12-02 19:04:24 -05:00
|
|
|
|
2008-11-01 17:32:06 -04:00
|
|
|
public final class TrustManagerFactory {
|
|
|
|
private static final String LOG_TAG = "TrustManagerFactory";
|
|
|
|
|
2008-12-02 19:04:24 -05:00
|
|
|
private static X509TrustManager defaultTrustManager;
|
|
|
|
private static X509TrustManager unsecureTrustManager;
|
|
|
|
private static X509TrustManager localTrustManager;
|
2008-12-06 19:29:11 -05:00
|
|
|
|
2008-12-02 19:04:24 -05:00
|
|
|
private static SecureX509TrustManager secureTrustManager;
|
|
|
|
|
|
|
|
private static X509Certificate[] lastCertChain = null;
|
2008-12-06 19:29:11 -05:00
|
|
|
|
2008-12-02 19:04:24 -05:00
|
|
|
private static File keyStoreFile;
|
|
|
|
private static KeyStore keyStore;
|
|
|
|
|
2008-11-01 17:32:06 -04:00
|
|
|
|
|
|
|
private static class SimpleX509TrustManager implements X509TrustManager {
|
|
|
|
public void checkClientTrusted(X509Certificate[] chain, String authType)
|
|
|
|
throws CertificateException {
|
|
|
|
}
|
|
|
|
|
|
|
|
public void checkServerTrusted(X509Certificate[] chain, String authType)
|
|
|
|
throws CertificateException {
|
|
|
|
}
|
|
|
|
|
|
|
|
public X509Certificate[] getAcceptedIssuers() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static class SecureX509TrustManager implements X509TrustManager {
|
2008-12-02 19:04:24 -05:00
|
|
|
private static String mHost;
|
|
|
|
private static SecureX509TrustManager me;
|
2008-11-01 17:32:06 -04:00
|
|
|
|
2008-12-02 19:04:24 -05:00
|
|
|
private SecureX509TrustManager() {
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
|
|
|
|
2008-12-02 19:04:24 -05:00
|
|
|
public static X509TrustManager getInstance(String host) {
|
|
|
|
mHost = host;
|
|
|
|
if (me == null) {
|
|
|
|
me = new SecureX509TrustManager();
|
|
|
|
}
|
|
|
|
return me;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void setHost(String host){
|
|
|
|
mHost = host;
|
|
|
|
}
|
2008-12-04 17:15:43 -05:00
|
|
|
|
2008-11-01 17:32:06 -04:00
|
|
|
public void checkClientTrusted(X509Certificate[] chain, String authType)
|
|
|
|
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)
|
2008-12-04 17:15:43 -05:00
|
|
|
throws CertificateException {
|
2008-12-02 19:04:24 -05:00
|
|
|
TrustManagerFactory.setLastCertChain(chain);
|
|
|
|
try {
|
|
|
|
defaultTrustManager.checkServerTrusted(chain, authType);
|
|
|
|
} catch (CertificateException e) {
|
2008-12-04 17:03:53 -05:00
|
|
|
localTrustManager.checkServerTrusted(new X509Certificate[] {chain[0]}, authType);
|
2008-12-02 19:04:24 -05:00
|
|
|
}
|
|
|
|
if (!DomainNameChecker.match(chain[0], mHost)) {
|
|
|
|
try {
|
|
|
|
String dn = chain[0].getSubjectDN().toString();
|
|
|
|
if ((dn != null) && (dn.equalsIgnoreCase(keyStore.getCertificateAlias(chain[0])))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} catch (KeyStoreException e) {
|
|
|
|
throw new CertificateException("Certificate cannot be verified; KeyStore Exception: " + e);
|
|
|
|
}
|
2008-12-04 17:15:43 -05:00
|
|
|
throw new CertificateException("Certificate domain name does not match "
|
2008-12-02 19:04:24 -05:00
|
|
|
+ mHost);
|
|
|
|
}
|
2008-11-01 17:32:06 -04: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
|
|
|
}
|
|
|
|
|
|
|
|
static {
|
|
|
|
try {
|
|
|
|
javax.net.ssl.TrustManagerFactory tmf = javax.net.ssl.TrustManagerFactory.getInstance("X509");
|
2008-12-16 18:34:01 -05:00
|
|
|
Application app = Email.app;
|
2008-12-02 19:04:24 -05:00
|
|
|
keyStoreFile = new File(app.getDir("KeyStore", Context.MODE_PRIVATE) + File.separator + "KeyStore.bks");
|
|
|
|
keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
|
|
|
|
java.io.FileInputStream fis;
|
|
|
|
try {
|
|
|
|
fis = new java.io.FileInputStream(keyStoreFile);
|
|
|
|
} catch (FileNotFoundException e1) {
|
|
|
|
fis = null;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
keyStore.load(fis, "".toCharArray());
|
2009-01-04 19:05:43 -05:00
|
|
|
//if (fis != null) {
|
|
|
|
// fis.close();
|
|
|
|
//}
|
2008-12-02 19:04:24 -05:00
|
|
|
} catch (IOException e) {
|
|
|
|
Log.e(LOG_TAG, "KeyStore IOException while initializing TrustManagerFactory ", e);
|
|
|
|
keyStore = null;
|
|
|
|
} catch (CertificateException e) {
|
|
|
|
Log.e(LOG_TAG, "KeyStore CertificateException while initializing TrustManagerFactory ", e);
|
|
|
|
keyStore = null;
|
|
|
|
}
|
|
|
|
tmf.init(keyStore);
|
2008-11-01 17:32:06 -04:00
|
|
|
TrustManager[] tms = tmf.getTrustManagers();
|
|
|
|
if (tms != null) {
|
|
|
|
for (TrustManager tm : tms) {
|
|
|
|
if (tm instanceof X509TrustManager) {
|
2008-12-02 19:04:24 -05:00
|
|
|
localTrustManager = (X509TrustManager)tm;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tmf = javax.net.ssl.TrustManagerFactory.getInstance("X509");
|
|
|
|
tmf.init((KeyStore)null);
|
|
|
|
tms = tmf.getTrustManagers();
|
|
|
|
if (tms != null) {
|
|
|
|
for (TrustManager tm : tms) {
|
|
|
|
if (tm instanceof X509TrustManager) {
|
|
|
|
defaultTrustManager = (X509TrustManager) tm;
|
2008-11-01 17:32:06 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-12-02 19:04:24 -05:00
|
|
|
|
2008-11-01 17:32:06 -04:00
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
Log.e(LOG_TAG, "Unable to get X509 Trust Manager ", e);
|
|
|
|
} catch (KeyStoreException e) {
|
|
|
|
Log.e(LOG_TAG, "Key Store exception while initializing TrustManagerFactory ", e);
|
|
|
|
}
|
2008-12-02 19:04:24 -05:00
|
|
|
unsecureTrustManager = new SimpleX509TrustManager();
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
private TrustManagerFactory() {
|
|
|
|
}
|
|
|
|
|
|
|
|
public static X509TrustManager get(String host, boolean secure) {
|
2008-12-02 19:04:24 -05:00
|
|
|
return secure ? SecureX509TrustManager.getInstance(host) :
|
|
|
|
unsecureTrustManager;
|
|
|
|
}
|
2008-12-04 17:15:43 -05:00
|
|
|
|
2008-12-02 19:04:24 -05:00
|
|
|
public static KeyStore getKeyStore() {
|
|
|
|
return keyStore;
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
2008-12-04 17:15:43 -05:00
|
|
|
|
2008-12-02 19:04:24 -05:00
|
|
|
public static void setLastCertChain(X509Certificate[] chain) {
|
|
|
|
lastCertChain = chain;
|
|
|
|
}
|
|
|
|
public static X509Certificate[] getLastCertChain() {
|
|
|
|
return lastCertChain;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void addCertificateChain(String alias, X509Certificate[] chain) throws CertificateException {
|
|
|
|
try {
|
|
|
|
javax.net.ssl.TrustManagerFactory tmf = javax.net.ssl.TrustManagerFactory.getInstance("X509");
|
|
|
|
for (int i = 0; i < chain.length; i++)
|
|
|
|
{
|
|
|
|
keyStore.setCertificateEntry
|
|
|
|
(chain[i].getSubjectDN().toString(), chain[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
tmf.init(keyStore);
|
|
|
|
TrustManager[] tms = tmf.getTrustManagers();
|
|
|
|
if (tms != null) {
|
|
|
|
for (TrustManager tm : tms) {
|
|
|
|
if (tm instanceof X509TrustManager) {
|
|
|
|
localTrustManager = (X509TrustManager) tm;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
java.io.FileOutputStream keyStoreStream;
|
|
|
|
try {
|
|
|
|
keyStoreStream = new java.io.FileOutputStream(keyStoreFile);
|
|
|
|
keyStore.store(keyStoreStream, "".toCharArray());
|
|
|
|
keyStoreStream.close();
|
|
|
|
} catch (FileNotFoundException e) {
|
|
|
|
throw new CertificateException("Unable to write KeyStore: " + e.getMessage());
|
|
|
|
} catch (CertificateException e) {
|
|
|
|
throw new CertificateException("Unable to write KeyStore: " + e.getMessage());
|
|
|
|
} catch (IOException e) {
|
|
|
|
throw new CertificateException("Unable to write KeyStore: " + e.getMessage());
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
Log.e(LOG_TAG, "Unable to get X509 Trust Manager ", e);
|
|
|
|
} catch (KeyStoreException e) {
|
|
|
|
Log.e(LOG_TAG, "Key Store exception while initializing TrustManagerFactory ", e);
|
|
|
|
}
|
|
|
|
}
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|