Esteban Talavera | 17aafef | 2014-09-30 17:03:32 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package com.android.cts.deviceowner; |
| 17 | |
Chad Brubaker | fe9883f | 2016-04-20 13:34:40 -0700 | [diff] [blame] | 18 | import android.net.http.X509TrustManagerExtensions; |
Chad Brubaker | f6a85eb | 2016-03-25 10:49:43 -0700 | [diff] [blame] | 19 | import android.security.NetworkSecurityPolicy; |
| 20 | |
Esteban Talavera | 17aafef | 2014-09-30 17:03:32 +0100 | [diff] [blame] | 21 | import java.io.ByteArrayInputStream; |
Robin Lee | 53d2671 | 2015-09-30 17:19:29 +0100 | [diff] [blame] | 22 | import java.security.GeneralSecurityException; |
| 23 | import java.security.KeyStore; |
| 24 | import java.security.cert.Certificate; |
Esteban Talavera | 17aafef | 2014-09-30 17:03:32 +0100 | [diff] [blame] | 25 | import java.security.cert.CertificateException; |
| 26 | import java.security.cert.CertificateFactory; |
Chad Brubaker | fe9883f | 2016-04-20 13:34:40 -0700 | [diff] [blame] | 27 | import java.security.cert.X509Certificate; |
Robin Lee | 53d2671 | 2015-09-30 17:19:29 +0100 | [diff] [blame] | 28 | import java.util.Arrays; |
Esteban Talavera | 17aafef | 2014-09-30 17:03:32 +0100 | [diff] [blame] | 29 | import java.util.List; |
| 30 | |
Robin Lee | 53d2671 | 2015-09-30 17:19:29 +0100 | [diff] [blame] | 31 | import javax.net.ssl.TrustManager; |
| 32 | import javax.net.ssl.TrustManagerFactory; |
| 33 | import javax.net.ssl.X509TrustManager; |
| 34 | |
| 35 | import static com.android.cts.deviceowner.FakeKeys.FAKE_DSA_1; |
| 36 | import static com.android.cts.deviceowner.FakeKeys.FAKE_RSA_1; |
| 37 | |
Esteban Talavera | 17aafef | 2014-09-30 17:03:32 +0100 | [diff] [blame] | 38 | public class CaCertManagementTest extends BaseDeviceOwnerTest { |
Robin Lee | 53d2671 | 2015-09-30 17:19:29 +0100 | [diff] [blame] | 39 | /** |
| 40 | * Test: device admins should be able to list all installed certs. |
| 41 | * |
| 42 | * <p>The list of certificates must never be {@code null}. |
| 43 | */ |
Esteban Talavera | 17aafef | 2014-09-30 17:03:32 +0100 | [diff] [blame] | 44 | public void testCanRetrieveListOfInstalledCaCerts() { |
| 45 | List<byte[]> caCerts = mDevicePolicyManager.getInstalledCaCerts(getWho()); |
| 46 | assertNotNull(caCerts); |
| 47 | } |
| 48 | |
Robin Lee | 53d2671 | 2015-09-30 17:19:29 +0100 | [diff] [blame] | 49 | /** |
| 50 | * Test: a valid cert should be installable and also removable. |
| 51 | */ |
Esteban Talavera | 17aafef | 2014-09-30 17:03:32 +0100 | [diff] [blame] | 52 | public void testCanInstallAndUninstallACaCert() |
Robin Lee | 53d2671 | 2015-09-30 17:19:29 +0100 | [diff] [blame] | 53 | throws CertificateException, GeneralSecurityException { |
| 54 | assertUninstalled(FAKE_RSA_1.caCertificate); |
| 55 | assertUninstalled(FAKE_DSA_1.caCertificate); |
| 56 | |
Esteban Talavera | 17aafef | 2014-09-30 17:03:32 +0100 | [diff] [blame] | 57 | assertTrue(mDevicePolicyManager.installCaCert(getWho(), FAKE_RSA_1.caCertificate)); |
Robin Lee | 53d2671 | 2015-09-30 17:19:29 +0100 | [diff] [blame] | 58 | assertInstalled(FAKE_RSA_1.caCertificate); |
| 59 | assertUninstalled(FAKE_DSA_1.caCertificate); |
| 60 | |
Esteban Talavera | 17aafef | 2014-09-30 17:03:32 +0100 | [diff] [blame] | 61 | mDevicePolicyManager.uninstallCaCert(getWho(), FAKE_RSA_1.caCertificate); |
Robin Lee | 53d2671 | 2015-09-30 17:19:29 +0100 | [diff] [blame] | 62 | assertUninstalled(FAKE_RSA_1.caCertificate); |
| 63 | assertUninstalled(FAKE_DSA_1.caCertificate); |
Esteban Talavera | 17aafef | 2014-09-30 17:03:32 +0100 | [diff] [blame] | 64 | } |
| 65 | |
Robin Lee | 53d2671 | 2015-09-30 17:19:29 +0100 | [diff] [blame] | 66 | /** |
| 67 | * Test: removing one certificate must not remove any others. |
| 68 | */ |
| 69 | public void testUninstallationIsSelective() |
| 70 | throws CertificateException, GeneralSecurityException { |
Esteban Talavera | 17aafef | 2014-09-30 17:03:32 +0100 | [diff] [blame] | 71 | assertTrue(mDevicePolicyManager.installCaCert(getWho(), FAKE_RSA_1.caCertificate)); |
| 72 | assertTrue(mDevicePolicyManager.installCaCert(getWho(), FAKE_DSA_1.caCertificate)); |
Robin Lee | 53d2671 | 2015-09-30 17:19:29 +0100 | [diff] [blame] | 73 | |
Esteban Talavera | 17aafef | 2014-09-30 17:03:32 +0100 | [diff] [blame] | 74 | mDevicePolicyManager.uninstallCaCert(getWho(), FAKE_DSA_1.caCertificate); |
Robin Lee | 53d2671 | 2015-09-30 17:19:29 +0100 | [diff] [blame] | 75 | assertInstalled(FAKE_RSA_1.caCertificate); |
| 76 | assertUninstalled(FAKE_DSA_1.caCertificate); |
| 77 | |
Esteban Talavera | 17aafef | 2014-09-30 17:03:32 +0100 | [diff] [blame] | 78 | mDevicePolicyManager.uninstallCaCert(getWho(), FAKE_RSA_1.caCertificate); |
| 79 | } |
| 80 | |
Robin Lee | 53d2671 | 2015-09-30 17:19:29 +0100 | [diff] [blame] | 81 | /** |
| 82 | * Test: uninstallAllUserCaCerts should be equivalent to calling uninstallCaCert on every |
| 83 | * supplementary installed certificate. |
| 84 | */ |
| 85 | public void testCanUninstallAllUserCaCerts() |
| 86 | throws CertificateException, GeneralSecurityException { |
Esteban Talavera | 17aafef | 2014-09-30 17:03:32 +0100 | [diff] [blame] | 87 | assertTrue(mDevicePolicyManager.installCaCert(getWho(), FAKE_RSA_1.caCertificate)); |
| 88 | assertTrue(mDevicePolicyManager.installCaCert(getWho(), FAKE_DSA_1.caCertificate)); |
Robin Lee | 53d2671 | 2015-09-30 17:19:29 +0100 | [diff] [blame] | 89 | |
Esteban Talavera | 17aafef | 2014-09-30 17:03:32 +0100 | [diff] [blame] | 90 | mDevicePolicyManager.uninstallAllUserCaCerts(getWho()); |
Robin Lee | 53d2671 | 2015-09-30 17:19:29 +0100 | [diff] [blame] | 91 | assertUninstalled(FAKE_RSA_1.caCertificate); |
| 92 | assertUninstalled(FAKE_DSA_1.caCertificate); |
Esteban Talavera | 17aafef | 2014-09-30 17:03:32 +0100 | [diff] [blame] | 93 | } |
| 94 | |
Robin Lee | 53d2671 | 2015-09-30 17:19:29 +0100 | [diff] [blame] | 95 | private void assertInstalled(byte[] caBytes) |
| 96 | throws CertificateException, GeneralSecurityException { |
| 97 | Certificate caCert = readCertificate(caBytes); |
| 98 | assertTrue(isCaCertInstalledAndTrusted(caCert)); |
Esteban Talavera | 17aafef | 2014-09-30 17:03:32 +0100 | [diff] [blame] | 99 | } |
| 100 | |
Robin Lee | 53d2671 | 2015-09-30 17:19:29 +0100 | [diff] [blame] | 101 | private void assertUninstalled(byte[] caBytes) |
| 102 | throws CertificateException, GeneralSecurityException { |
| 103 | Certificate caCert = readCertificate(caBytes); |
| 104 | assertFalse(isCaCertInstalledAndTrusted(caCert)); |
| 105 | } |
| 106 | |
Chad Brubaker | fe9883f | 2016-04-20 13:34:40 -0700 | [diff] [blame] | 107 | private static X509TrustManager getFirstX509TrustManager(TrustManagerFactory tmf) { |
| 108 | for (TrustManager trustManager : tmf.getTrustManagers()) { |
| 109 | if (trustManager instanceof X509TrustManager) { |
| 110 | return (X509TrustManager) trustManager; |
| 111 | } |
| 112 | } |
| 113 | throw new RuntimeException("Unable to find X509TrustManager"); |
| 114 | } |
| 115 | |
Robin Lee | 53d2671 | 2015-09-30 17:19:29 +0100 | [diff] [blame] | 116 | /** |
| 117 | * Whether a given cert, or one a lot like it, has been installed system-wide and is available |
| 118 | * to all apps. |
| 119 | * |
| 120 | * <p>A CA certificate is "installed" if it matches all of the following conditions: |
| 121 | * <ul> |
| 122 | * <li>{@link DevicePolicyManager#hasCaCertInstalled} returns {@code true}.</li> |
| 123 | * <li>{@link DevicePolicyManager#getInstalledCaCerts} lists a matching certificate (not |
| 124 | * necessarily exactly the same) in its response.</li> |
| 125 | * <li>Any new instances of {@link TrustManager} should report the certificate among their |
| 126 | * accepted issuer list -- older instances may keep the set of issuers they were created |
| 127 | * with until explicitly refreshed.</li> |
| 128 | * |
| 129 | * @return {@code true} if installed by all metrics, {@code false} if not installed by any |
| 130 | * metric. In any other case an {@link AssertionError} will be thrown. |
| 131 | */ |
| 132 | private boolean isCaCertInstalledAndTrusted(Certificate caCert) |
| 133 | throws GeneralSecurityException, CertificateException { |
| 134 | boolean installed = mDevicePolicyManager.hasCaCertInstalled(getWho(), caCert.getEncoded()); |
| 135 | |
| 136 | boolean listed = false; |
| 137 | for (byte[] certBuffer : mDevicePolicyManager.getInstalledCaCerts(getWho())) { |
| 138 | if (caCert.equals(readCertificate(certBuffer))) { |
| 139 | listed = true; |
Esteban Talavera | 17aafef | 2014-09-30 17:03:32 +0100 | [diff] [blame] | 140 | } |
| 141 | } |
Robin Lee | 53d2671 | 2015-09-30 17:19:29 +0100 | [diff] [blame] | 142 | |
Chad Brubaker | f6a85eb | 2016-03-25 10:49:43 -0700 | [diff] [blame] | 143 | NetworkSecurityPolicy.getInstance().handleTrustStorageUpdate(); |
| 144 | |
Chad Brubaker | fe9883f | 2016-04-20 13:34:40 -0700 | [diff] [blame] | 145 | // Verify that the user added CA is reflected in the default X509TrustManager. |
Robin Lee | 53d2671 | 2015-09-30 17:19:29 +0100 | [diff] [blame] | 146 | final TrustManagerFactory tmf = |
| 147 | TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); |
Chad Brubaker | fe9883f | 2016-04-20 13:34:40 -0700 | [diff] [blame] | 148 | // Use platform provided CA store. |
Robin Lee | 53d2671 | 2015-09-30 17:19:29 +0100 | [diff] [blame] | 149 | tmf.init((KeyStore) null); |
Chad Brubaker | fe9883f | 2016-04-20 13:34:40 -0700 | [diff] [blame] | 150 | X509TrustManager tm = getFirstX509TrustManager(tmf); |
| 151 | boolean trusted = Arrays.asList(tm.getAcceptedIssuers()).contains(caCert); |
| 152 | X509TrustManagerExtensions xtm = new X509TrustManagerExtensions(tm); |
| 153 | boolean userAddedCertificate = xtm.isUserAddedCertificate((X509Certificate) caCert); |
Robin Lee | 53d2671 | 2015-09-30 17:19:29 +0100 | [diff] [blame] | 154 | |
| 155 | // All three responses should match - if an installed certificate isn't trusted or (worse) |
| 156 | // a trusted certificate isn't even installed we should |
| 157 | assertEquals(installed, listed); |
| 158 | assertEquals(installed, trusted); |
Chad Brubaker | fe9883f | 2016-04-20 13:34:40 -0700 | [diff] [blame] | 159 | assertEquals(installed, userAddedCertificate); |
Robin Lee | 53d2671 | 2015-09-30 17:19:29 +0100 | [diff] [blame] | 160 | return installed; |
Esteban Talavera | 17aafef | 2014-09-30 17:03:32 +0100 | [diff] [blame] | 161 | } |
| 162 | |
Robin Lee | 53d2671 | 2015-09-30 17:19:29 +0100 | [diff] [blame] | 163 | /** |
| 164 | * Convert an encoded certificate back into a {@link Certificate}. |
| 165 | * |
| 166 | * Instantiates a fresh CertificateFactory every time for repeatability. |
| 167 | */ |
Esteban Talavera | 17aafef | 2014-09-30 17:03:32 +0100 | [diff] [blame] | 168 | private static Certificate readCertificate(byte[] certBuffer) throws CertificateException { |
| 169 | final CertificateFactory certFactory = CertificateFactory.getInstance("X.509"); |
| 170 | return certFactory.generateCertificate(new ByteArrayInputStream(certBuffer)); |
| 171 | } |
| 172 | } |