1
0
mirror of https://github.com/moparisthebest/k-9 synced 2024-11-27 11:42:16 -05:00

Merge branch 'cert_validation' of git://github.com/k9mail/k-9.git into cert_validation

Conflicts:
	tests/src/com/fsck/k9/mail/store/TrustManagerFactoryTest.java
This commit is contained in:
Joe Steele 2013-12-02 16:30:11 -05:00
commit 9e6abf5fa7

View File

@ -51,19 +51,159 @@ public class TrustManagerFactoryTest extends AndroidTestCase {
+ "G6A2hvA1lpvP97qQ/NFaGQ38XqSykZamZwSx3PlZUM/i9S9n/3MfuuXWqtLC\n"
+ "-----END CERTIFICATE-----\n";
private static final String CA_CERT =
"-----BEGIN CERTIFICATE-----\n"
+ "MIIDbTCCAlWgAwIBAgIJANCdQ+Cwnyg+MA0GCSqGSIb3DQEBBQUAME0xCzAJBgNV\n"
+ "BAYTAkNIMRMwEQYDVQQIDApTb21lLVN0YXRlMQwwCgYDVQQKDANLLTkxGzAZBgNV\n"
+ "BAMMEnRlc3QtY2EuazltYWlsLm9yZzAeFw0xMzEyMDIxMjUwNThaFw0yMzExMzAx\n"
+ "MjUwNThaME0xCzAJBgNVBAYTAkNIMRMwEQYDVQQIDApTb21lLVN0YXRlMQwwCgYD\n"
+ "VQQKDANLLTkxGzAZBgNVBAMMEnRlc3QtY2EuazltYWlsLm9yZzCCASIwDQYJKoZI\n"
+ "hvcNAQEBBQADggEPADCCAQoCggEBAJ+YLg9enfFk5eba6B3LtQzUE7GiR2tIpQSi\n"
+ "zHMtHzn8KUnRDiGwC8VnSuWCOX7hXyQ0P6i2+DVRVBYOAeDCNMZHOq1hRqI66B33\n"
+ "QqLfkBnJAIDeLqfqlgigHs1+//7eagVA6Z38ZFre3PFuKnK9NCwS+gz7PKw/poIG\n"
+ "/FZP+ltMlkwvPww4S8SMlY6RXXH09+S/uM8aG6DUBT298eoAXTbSEIeaNhwBHZPe\n"
+ "rXqqzd8QDAIE9BFXSkh/BQiVEFDPSBMSdmUzUAsT2aM8osntnKWY5/G7B60wutvA\n"
+ "jYCULgtR6l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3/\n"
+ "+mx9u8cY//7uAxLWYS5gIZlCbxcf0lkiKSHJB319\n"
+ "-----END CERTIFICATE-----\n";
private X509Certificate mCert1;
private X509Certificate mCert2;
private File mKeyStoreFile;
private LocalKeyStore mKeyStore;
private X509Certificate mCert1;
private X509Certificate mCert2;
private X509Certificate mCaCert;
private X509Certificate mCert3;
private X509Certificate mDigiCert;
private X509Certificate mGithubCert;
public TrustManagerFactoryTest() throws CertificateException {
mCert1 = loadCert(K9_EXAMPLE_COM_CERT1);
mCert2 = loadCert(K9_EXAMPLE_COM_CERT2);
mCaCert = loadCert(CA_CERT);
mCert3 = loadCert(CERT3);
mDigiCert = loadCert(DIGI_CERT);
mGithubCert = loadCert(GITHUB_CERT);
}
private X509Certificate loadCert(String encodedCert) throws CertificateException {
CertificateFactory certFactory = CertificateFactory.getInstance("X509");
mCert1 = (X509Certificate) certFactory.generateCertificate(
new ByteArrayInputStream(K9_EXAMPLE_COM_CERT1.getBytes()));
mCert2 = (X509Certificate) certFactory.generateCertificate(
new ByteArrayInputStream(K9_EXAMPLE_COM_CERT2.getBytes()));
return (X509Certificate) certFactory.generateCertificate(
new ByteArrayInputStream(encodedCert.getBytes()));
}
@Override
@ -116,14 +256,7 @@ public class TrustManagerFactoryTest extends AndroidTestCase {
public void testWrongCertificate() throws Exception {
mKeyStore.addCertificate(MATCHING_HOST, PORT1, mCert1);
X509TrustManager trustManager = TrustManagerFactory.get(MATCHING_HOST, PORT1, true);
boolean certificateValid;
try {
trustManager.checkServerTrusted(new X509Certificate[] { mCert2 }, "authType");
certificateValid = true;
} catch (CertificateException e) {
certificateValid = false;
}
assertFalse("The certificate should have been rejected but wasn't", certificateValid);
assertCertificateRejection(trustManager, new X509Certificate[] { mCert2 });
}
public void testCertificateOfOtherHost() throws Exception {
@ -131,9 +264,52 @@ public class TrustManagerFactoryTest extends AndroidTestCase {
mKeyStore.addCertificate(MATCHING_HOST, PORT2, mCert2);
X509TrustManager trustManager = TrustManagerFactory.get(MATCHING_HOST, PORT1, true);
assertCertificateRejection(trustManager, new X509Certificate[] { mCert2 });
}
public void testUntrustedCertificateChain() throws Exception {
X509TrustManager trustManager = TrustManagerFactory.get(MATCHING_HOST, PORT1, true);
assertCertificateRejection(trustManager, new X509Certificate[] { mCert3, mCaCert });
}
public void testLocallyTrustedCertificateChain() throws Exception {
mKeyStore.addCertificate(MATCHING_HOST, PORT1, mCert3);
X509TrustManager trustManager = TrustManagerFactory.get(MATCHING_HOST, PORT1, true);
trustManager.checkServerTrusted(new X509Certificate[] { mCert3, mCaCert }, "authType");
}
public void testLocallyTrustedCertificateChainNotMatchingHost() throws Exception {
mKeyStore.addCertificate(NOT_MATCHING_HOST, PORT1, mCert3);
X509TrustManager trustManager = TrustManagerFactory.get(NOT_MATCHING_HOST, PORT1, true);
trustManager.checkServerTrusted(new X509Certificate[] { mCert3, mCaCert }, "authType");
}
public void testGloballyTrustedCertificateChain() throws Exception {
X509TrustManager trustManager = TrustManagerFactory.get("github.com", PORT1, true);
X509Certificate[] certificates = new X509Certificate[] { mGithubCert, mDigiCert };
trustManager.checkServerTrusted(certificates, "authType");
}
public void testGloballyTrustedCertificateNotMatchingHost() throws Exception {
X509TrustManager trustManager = TrustManagerFactory.get(NOT_MATCHING_HOST, PORT1, true);
assertCertificateRejection(trustManager, new X509Certificate[] { mGithubCert, mDigiCert});
}
public void testGloballyTrustedCertificateNotMatchingHostOverride() throws Exception {
mKeyStore.addCertificate(MATCHING_HOST, PORT1, mGithubCert);
X509TrustManager trustManager = TrustManagerFactory.get(MATCHING_HOST, PORT1, true);
X509Certificate[] certificates = new X509Certificate[] { mGithubCert, mDigiCert };
trustManager.checkServerTrusted(certificates, "authType");
}
private void assertCertificateRejection(X509TrustManager trustManager,
X509Certificate[] certificates) {
boolean certificateValid;
try {
trustManager.checkServerTrusted(new X509Certificate[] { mCert2 }, "authType");
trustManager.checkServerTrusted(certificates, "authType");
certificateValid = true;
} catch (CertificateException e) {
certificateValid = false;