diff options
-rw-r--r-- | common/java/com/android/common/DomainNameValidator.java | 3 | ||||
-rw-r--r-- | common/tests/res/raw/alt_ip_only.crt | 17 | ||||
-rw-r--r-- | common/tests/res/raw/subject_alt_only.crt | 17 | ||||
-rw-r--r-- | common/tests/res/raw/subject_only.crt | 18 | ||||
-rw-r--r-- | common/tests/res/raw/subject_with_alt_names.crt | 19 | ||||
-rw-r--r-- | common/tests/res/raw/subject_with_wild_alt_name.crt | 18 | ||||
-rw-r--r-- | common/tests/res/raw/wild_alt_name_only.crt | 17 | ||||
-rw-r--r-- | common/tests/src/com/android/common/DomainNameValidatorTest.java | 83 |
8 files changed, 182 insertions, 10 deletions
diff --git a/common/java/com/android/common/DomainNameValidator.java b/common/java/com/android/common/DomainNameValidator.java index ad44a7d..7d58ddf 100644 --- a/common/java/com/android/common/DomainNameValidator.java +++ b/common/java/com/android/common/DomainNameValidator.java @@ -166,6 +166,9 @@ public class DomainNameValidator { } } } catch (CertificateParsingException e) { + // TODO figure out if this comment is still true + // i.e. In what condition do we this exception with a valid certificate? + // one way we can get here is if an alternative name starts with // '*' character, which is contrary to one interpretation of the // spec (a valid DNS name must start with a letter); there is no diff --git a/common/tests/res/raw/alt_ip_only.crt b/common/tests/res/raw/alt_ip_only.crt new file mode 100644 index 0000000..3ac9f5a --- /dev/null +++ b/common/tests/res/raw/alt_ip_only.crt @@ -0,0 +1,17 @@ +-----BEGIN CERTIFICATE----- +MIICsjCCAZqgAwIBAgIJALrC37YAXFIeMA0GCSqGSIb3DQEBBQUAMA0xCzAJBgNV +BAYTAkpQMCAXDTEwMDExMjIxMzk0NloYDzIwNjQxMDE1MjEzOTQ2WjANMQswCQYD +VQQGEwJKUDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBALr8s/4Abpby +IYks5YCJE2nbWH7kj6XbwnRzsVP9RVC33bPoQ1M+2ZY24HqkigjQS/HEXR0s0bYh +dewNUnTj1uGyGs6cYzsbu7x114vmVYqjxUo3hKjwfYiPeF6f3IE1vpLI7I2G32gq +Zwm9c1/vXNHIdWQxCpFcuPA8P3YGfoApFX4pQPFplBUNAQqnjdmA68cbxxMC+1F3 +mX42D7iIEVwyVpah5HjyxjIZQlf3X7QBj0bCmkL+ibIHTALrkNNwNM6i4xzYLz/5 +14GkN9ncHY87eSOk6r53ptER6mQMhCe9qPRjSHnpWTTyj6IXTaYe+dDQw657B80w +cSHL7Ed25zUCAwEAAaMTMBEwDwYDVR0RBAgwBocEwKgKATANBgkqhkiG9w0BAQUF +AAOCAQEAgrwrtOWZT3fbi1AafpGaAiOBWSJqYqRhtQy0AfiZBxv1U0XaYqmZmpnq +DVAqr0NkljowD28NBrxIFO5gBNum2ZOPDl2/5vjFn+IirUCJ9u9wS7zYkTCW2lQR +xE7Ic3mfWv7wUbKDfjlWqP1IDHUxwkrBTAl+HnwOPiaKKk1ttwcrgS8AHlqASe03 +mlwnvJ+Stk54IneRaegL0L93sNAy63RZqnPCTxGz7eHcFwX8Jdr4sbxTxQqV6pIc +WPjHQcWfpkFzAF5wyOq0kveVfx0g5xPhOVDd+U+q7WastbXICpCoHp9FxISmZVik +sAyifp8agkYdzaSh55fFmKXlFnRsQw== +-----END CERTIFICATE----- diff --git a/common/tests/res/raw/subject_alt_only.crt b/common/tests/res/raw/subject_alt_only.crt new file mode 100644 index 0000000..d5808fb --- /dev/null +++ b/common/tests/res/raw/subject_alt_only.crt @@ -0,0 +1,17 @@ +-----BEGIN CERTIFICATE----- +MIICvTCCAaWgAwIBAgIJALbA0TZk2YmNMA0GCSqGSIb3DQEBBQUAMA0xCzAJBgNV +BAYTAkpQMCAXDTEwMDExMjIwNTg1NFoYDzIwNjQxMDE1MjA1ODU0WjANMQswCQYD +VQQGEwJKUDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAMEg6acVC9V4 +xNGoLNVLPbqBc8IvMvcsc88dF6MW3d9VagX3aeWU8c79tI/KOV/1AOakH7WYxw/w +yD8aOX7+9BK1Hu0qKKKbSM+ycqaMthXd6xytrNDsIx5WiGUz8zTko0Gk3orIR7p7 +rPcNzB/zwtESkscqPv85aEn7S/yClNkzLfEzm3CtaYOc0tfhBMyzi/ipXzGMxUmx +PvOLr3v/Oz5pZEQw7Kxlm4+tAtn7bJlHziQ1UW4WPIy+T3hySBEpODFiqZi7Ok3X +Zjxdii62fgo5B2Ee7q5Amo0mUIwcQTDjJ2CLAqzYnSh3tpiPJGjEIjmRyCoMQ1bx +7D+y7nSPIq8CAwEAAaMeMBwwGgYDVR0RBBMwEYIPd3d3LmV4YW1wbGUuY29tMA0G +CSqGSIb3DQEBBQUAA4IBAQBsGEh+nHc0l9FJTzWqvG3qs7i6XoJZdtThCDx4HjKJ +8GMrJtreNN4JvIxn7KC+alVbnILjzCRO+c3rsnpxKBi5cp2imjuw5Kf/x2Seimb9 +UvZbaJvBVOzy4Q1IGef9bLy3wZzy2/WfBFyvPTAkgkRaX7LN2jnYOYVhNoNFrwqe +EWxkA6fzrpyseUEFeGFFjGxRSRCDcQ25Eq6d9rkC1x21zNtt4QwZBO0wHrTy155M +JPRynf9244Pn0Sr/wsnmdsTRFIFYynrc51hQ7DkwbUxpcaewkZzilru/SwZ3+pPT +9JSqm5hJ1pg5WDlPkW7c/1VA0/141N52Q8MIU+2ZpuOj +-----END CERTIFICATE----- diff --git a/common/tests/res/raw/subject_only.crt b/common/tests/res/raw/subject_only.crt new file mode 100644 index 0000000..11b34e7 --- /dev/null +++ b/common/tests/res/raw/subject_only.crt @@ -0,0 +1,18 @@ +-----BEGIN CERTIFICATE----- +MIIC0TCCAbmgAwIBAgIJANCQbJPPw31SMA0GCSqGSIb3DQEBBQUAMCcxCzAJBgNV +BAYTAkpQMRgwFgYDVQQDEw93d3cuZXhhbXBsZS5jb20wIBcNMTAwMTEyMjA1ODE4 +WhgPMjA2NDEwMTUyMDU4MThaMCcxCzAJBgNVBAYTAkpQMRgwFgYDVQQDEw93d3cu +ZXhhbXBsZS5jb20wggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDsdUJk +4KxADA3vlDHxNbyC27Ozw4yiSVzPTHUct471YmdDRW3orO2P5a5hRnUGV70gjH9X +MU4oeOdWYAgXB9pxfLyr6621k1+uNrmaZtzp0ECH9twcwxNJJFDZsN7o9vt7V6Ej +NN9weeqDr/aeQXo07a12vyVfR6jWO8jHB0e4aemwZNoYjNvM69fivQTse2ZoRVfj +eSHhjRTX6I8ry4a31Hwt+fT1QiWWNN6o7+WOtpJAhX3eg4smhSD1svi2kOT8tdUe +NS4hWlmXmumU9G4tI8PBurcLNTm7PB2lUlbn/IV18WavqKE/Uy/1WgAx+a1EJNdp +i07AG1PsqaONKkf1AgMBAAEwDQYJKoZIhvcNAQEFBQADggEBAJrNsuL7fZZNC8gL +BdePJ7DYW2e7mXANU3bCBe2BZqmXKQxKwibZnEsqA+yMLqcSd8uxISlyHY2tw9wT +4wB9KPIttfNLbwn/rk+MbOTHpvyF60d9WhJJVUkPBl8D4VuPSl+VnlA54kU9dtZN ++ZYdxYbNtSsI/Flz9SCoOV79W9GhN+uYJhv6RwyIMIHeMpZpyX1xSUVx5dZlmerQ +WAUvghDH3fFRt2ZdnA4OXoKkTAaM3Pv7PUMsnah8bux6MQi0AuLMWFWOI1H34koH +rs2oQLwOLnuifH52ey9+tJguabo+brlYYigAuWWFEzJfBzikDkIwnE/L7wlrypIk +taXDWI4= +-----END CERTIFICATE----- diff --git a/common/tests/res/raw/subject_with_alt_names.crt b/common/tests/res/raw/subject_with_alt_names.crt new file mode 100644 index 0000000..6963c7e --- /dev/null +++ b/common/tests/res/raw/subject_with_alt_names.crt @@ -0,0 +1,19 @@ +-----BEGIN CERTIFICATE----- +MIIDBDCCAeygAwIBAgIJALv14qjcuhw9MA0GCSqGSIb3DQEBBQUAMCcxCzAJBgNV +BAYTAkpQMRgwFgYDVQQDEw93d3cuZXhhbXBsZS5jb20wIBcNMTAwMTEyMjA1OTM4 +WhgPMjA2NDEwMTUyMDU5MzhaMCcxCzAJBgNVBAYTAkpQMRgwFgYDVQQDEw93d3cu +ZXhhbXBsZS5jb20wggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCiTVgU +kBO9KNYZZLmiPR0eBrk8u61CLnm35BGKW8EFpDaINLbbIFIQvqOMekURON/N+xFY +D8roo7aFZVuHWAUqFcOJ4e6NmviK5qocLihtzAexsw4f4AzZxM3A8kcLlWLyAt7e +EVLxhcMHogY7GaF6q+33Z8p+zp6x3tj07mwyPrriCLse2PeRsRunZl/fp/VvRlr6 +YbC7CbRrhnIv5nqohs8BsbBiiFpxQftsMQmiXhY2LUzqY2RXUIOw24fHjoQkHTL2 +4z5nUM3b6ueQe+CBnobUS6fzK/36Nct4dRpev9i/ORdRLuIDKJ+QR16G1V/BJYBR +dAK+3iXvg6z8vP1XAgMBAAGjMTAvMC0GA1UdEQQmMCSCEHd3dzIuZXhhbXBsZS5j +b22CEHd3dzMuZXhhbXBsZS5jb20wDQYJKoZIhvcNAQEFBQADggEBAJQNf38uXm3h +0vsF+Yd6/HqM48Su7tWnTDAfTXnQZZkzjzITq3JXzquMXICktAVN2cLnT9zPfRAE +8V8A3BNO5zXiR5W3o/mJP5HQ3/WxpzBGM2N+YmDCJyBoQrIVaAZaXAZUaBBvn5A+ +kEVfGWquwIFuvA67xegbJOCRLD4eUzRdNsn5+NFiakWO1tkFqEzqyQ0PNPviRjgu +z9NxdPvd1JQOhydkucsPKJzlEBbGyL5QL/Jkot3Qy+FOeuNzgQUfAGtQgzRrsZDK +hrTVypLSoRXuTB2aWilu4p6aNh84xTdyqo2avtNr2MiQMZIcdamBq8LdBIAShFXI +h5G2eVGXH/Y= +-----END CERTIFICATE----- diff --git a/common/tests/res/raw/subject_with_wild_alt_name.crt b/common/tests/res/raw/subject_with_wild_alt_name.crt new file mode 100644 index 0000000..19b1174 --- /dev/null +++ b/common/tests/res/raw/subject_with_wild_alt_name.crt @@ -0,0 +1,18 @@ +-----BEGIN CERTIFICATE----- +MIIC8DCCAdigAwIBAgIJAL/oWJ64VAdXMA0GCSqGSIb3DQEBBQUAMCcxCzAJBgNV +BAYTAkpQMRgwFgYDVQQDEw93d3cuZXhhbXBsZS5jb20wIBcNMTAwMTEyMjEwMDAx +WhgPMjA2NDEwMTUyMTAwMDFaMCcxCzAJBgNVBAYTAkpQMRgwFgYDVQQDEw93d3cu +ZXhhbXBsZS5jb20wggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCbx1QB +92iea7VybLYICA4MX4LWipYrRsgXUXQrcIQ3YLTQ9rH0VwScrHL4O4JDxgXCQnR+ +4VOzD42q1KXHJAqzqGUYCNPyvZEzkGCnQ4FBIUEmxZd5SNEefJVH3Z6GizYJomTh +p78yDcoqymD9umxRC2cWFu8GscfFGMVyhsqLlOofu7UWOs22mkXPo43jDx+VOAoV +n48YP3P57a2Eo0gcd4zVL00y62VegqBO/1LW38aTS7teiCBFc1TkNYa5I40yN9lP +rB9ICHYQWyzf/7OxU9iauEK2w6DmSsQoLs9JzEhgeNZddkcc77ciSUCo2Hx0VpOJ +BFyf2rbryJeAk+FDAgMBAAGjHTAbMBkGA1UdEQQSMBCCDiouZXhhbXBsZTIuY29t +MA0GCSqGSIb3DQEBBQUAA4IBAQA2a14pRL+4laJ8sscQlucaDB/oSdb0cwhk4IkE +kKl/ZKr6rKwPZ81sJRgzvI4imLbUAKt4AJHdpI9cIQUq1gw9bzil7LKwmFtFSPmC +MYb1iadaYrvp7RE4yXrWCcSbU0hup9JQLHTrHLlqLtRuU48NHMvWYThBcS9Q/hQp +nJ/JxYy3am99MHALWLAfuRxQXhE4C5utDmBwI2KD6A8SA30s+CnuegmkYScuSqBu +Y3R0HZvKzNIU3pwAm69HCJoG+/9MZEIDJb0WJc5UygxDT45XE9zQMQe4dBOTaNXT ++ntgaB62kE10HzrzpqXAgoAWxWK4RzFcUpBWw9qYq9xOCewJ +-----END CERTIFICATE----- diff --git a/common/tests/res/raw/wild_alt_name_only.crt b/common/tests/res/raw/wild_alt_name_only.crt new file mode 100644 index 0000000..fafdebf --- /dev/null +++ b/common/tests/res/raw/wild_alt_name_only.crt @@ -0,0 +1,17 @@ +-----BEGIN CERTIFICATE----- +MIICuzCCAaOgAwIBAgIJAP82tgcvmAGxMA0GCSqGSIb3DQEBBQUAMA0xCzAJBgNV +BAYTAkpQMCAXDTEwMDExMjIxMDAyN1oYDzIwNjQxMDE1MjEwMDI3WjANMQswCQYD +VQQGEwJKUDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBALs528EQbcB1 +x4BwxthQBZrgDJzoO7KPV3dhGYoeP8EnRjapZm+T/sj9P/O4HvfxjnB+fsjYSdmE +WWUtnFrP7wtG9DUC748Ea2PMV8WFhOG58dqBNIko5XzkHB7SxkNZD5S/0KQYMGLr +rchDsDlmsEf2Qb6qiqpNEU70aSkExZJcH+B9nWdeBpsVFu7wtezwSWEc2NUa2bhW +gcXQ/aafwHZ4o2PyGwy0sgS/UifqO9tEllC2tPleSNJOmYsVudv5Bz4Q0GG38BSz +Pc0IcOoln0ZWpXbGr03V2vlXWCwzaFAl3I1T3O7YVqDiaSWoP+d0tHZzmw8aJLXd +B+KaUUGxRPsCAwEAAaMcMBowGAYDVR0RBBEwD4INKi5leGFtcGxlLmNvbTANBgkq +hkiG9w0BAQUFAAOCAQEAJbVan4QgJ0cvpJnK9UWIVJNC+UbP87RC5go2fQiTnmGv +prOrIuMqz1+vGcpIheLTLctJRHPoadXq0+UbQEIaU3pQbY6C4nNdfl+hcvmJeqrt +kOCcvmIamO68iNvTSeszuHuu4O38PefrW2Xd0nn7bjFZrzBzHFhTudmnqNliP3ue +KKQpqkUt5lCytnH8V/u/UCWdvVx5LnUa2XFGVLi3ongBIojW5fvF+yxn9ADqxdrI +va++ow5r1VxQXFJc0ZPzsDo+6TlktoDHaRQJGMqQomqHWT4i7F5UZgf6BHGfEUPU +qep+GsF3QRHSBtpObWkVDZNFvky3a1iZ2q25+hFIqQ== +-----END CERTIFICATE----- diff --git a/common/tests/src/com/android/common/DomainNameValidatorTest.java b/common/tests/src/com/android/common/DomainNameValidatorTest.java index 4fdd4cd..b825be4 100644 --- a/common/tests/src/com/android/common/DomainNameValidatorTest.java +++ b/common/tests/src/com/android/common/DomainNameValidatorTest.java @@ -15,6 +15,11 @@ */ package com.android.common; +import com.android.common.tests.R; + +import android.test.AndroidTestCase; + +import java.io.InputStream; import java.math.BigInteger; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; @@ -25,6 +30,7 @@ import java.security.SignatureException; import java.security.cert.CertificateEncodingException; import java.security.cert.CertificateException; import java.security.cert.CertificateExpiredException; +import java.security.cert.CertificateFactory; import java.security.cert.CertificateNotYetValidException; import java.security.cert.CertificateParsingException; import java.security.cert.X509Certificate; @@ -37,22 +43,16 @@ import java.util.Set; import javax.security.auth.x500.X500Principal; -import junit.framework.TestCase; - -public class DomainNameValidatorTest extends TestCase { +public class DomainNameValidatorTest extends AndroidTestCase { private static final int ALT_UNKNOWN = 0; private static final int ALT_DNS_NAME = 2; private static final int ALT_IPA_NAME = 7; /** - * Tests {@link DomainNameValidator#match} + * Tests {@link DomainNameValidator#match}, using a simple {@link X509Certificate} + * implementation. */ public void testMatch() { - // TODO Use actual X509Certificate objects, instead of StubX509Certificate. - // Comment in DomainNameValidator suggests X509Certificate fails to parse a certificate - // if subject alternative names contain a domain name that begins with '*'. - // This test won't cover this kind of errors. - checkMatch("11", new StubX509Certificate("cn=imap.g.com"), "imap.g.com", true); checkMatch("12", new StubX509Certificate("cn=imap2.g.com"), "imap.g.com", false); checkMatch("13", new StubX509Certificate("cn=sub.imap.g.com"), "imap.g.com", false); @@ -74,7 +74,6 @@ public class DomainNameValidatorTest extends TestCase { .addSubjectAlternativeName(ALT_DNS_NAME, "*.g.com") , "imap.g.com", true); - // host name is ip address checkMatch("31", new StubX509Certificate("") .addSubjectAlternativeName(ALT_IPA_NAME, "1.2.3.4") @@ -170,6 +169,70 @@ public class DomainNameValidatorTest extends TestCase { } /** + * Test {@link DomainNameValidator#match} with actual certificates. + */ + public void testWithActualCert() throws Exception { + // subject_only + // + // subject: C=JP, CN=www.example.com + // subject alt names: n/a + checkWithActualCert("11", R.raw.subject_only, "www.example.com", true); + checkWithActualCert("12", R.raw.subject_only, "www2.example.com", false); + + // subject_alt_only + // + // subject: C=JP (no CN) + // subject alt names: DNS:www.example.com + checkWithActualCert("21", R.raw.subject_alt_only, "www.example.com", true); + checkWithActualCert("22", R.raw.subject_alt_only, "www2.example.com", false); + + // subject_with_alt_names + // + // subject: C=JP, CN=www.example.com + // subject alt names: DNS:www2.example.com, DNS:www3.example.com + // * Subject should be ignored, because it has subject alt names. + checkWithActualCert("31", R.raw.subject_with_alt_names, "www.example.com", false); + checkWithActualCert("32", R.raw.subject_with_alt_names, "www2.example.com", true); + checkWithActualCert("33", R.raw.subject_with_alt_names, "www3.example.com", true); + checkWithActualCert("34", R.raw.subject_with_alt_names, "www4.example.com", false); + + // subject_with_wild_alt_name + // + // subject: C=JP, CN=www.example.com + // subject alt names: DNS:*.example2.com + // * Subject should be ignored, because it has subject alt names. + checkWithActualCert("41", R.raw.subject_with_wild_alt_name, "www.example.com", false); + checkWithActualCert("42", R.raw.subject_with_wild_alt_name, "www2.example.com", false); + checkWithActualCert("43", R.raw.subject_with_wild_alt_name, "www.example2.com", true); + checkWithActualCert("44", R.raw.subject_with_wild_alt_name, "abc.example2.com", true); + checkWithActualCert("45", R.raw.subject_with_wild_alt_name, "www.example3.com", false); + + // wild_alt_name_only + // + // subject: C=JP + // subject alt names: DNS:*.example.com + checkWithActualCert("51", R.raw.wild_alt_name_only, "www.example.com", true); + checkWithActualCert("52", R.raw.wild_alt_name_only, "www2.example.com", true); + checkWithActualCert("53", R.raw.wild_alt_name_only, "www.example2.com", false); + + // wild_alt_name_only + // + // subject: C=JP + // subject alt names: IP Address:192.168.10.1 + checkWithActualCert("61", R.raw.alt_ip_only, "192.168.10.1", true); + checkWithActualCert("61", R.raw.alt_ip_only, "192.168.10.2", false); + } + + private void checkWithActualCert(String message, int resId, String domain, + boolean expected) throws Exception { + CertificateFactory factory = CertificateFactory.getInstance("X509"); + InputStream certStream = getContext().getResources().openRawResource(resId); + X509Certificate certificate = (X509Certificate) factory.generateCertificate(certStream); + + checkMatch(message, certificate, domain, expected); + } + + /** * Minimal {@link X509Certificate} implementation for {@link DomainNameValidator}. */ private static class StubX509Certificate extends X509Certificate { |