Andrew Scull | 1416bd0 | 2018-01-05 18:33:58 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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.server.locksettings; |
| 17 | |
| 18 | import static android.app.admin.DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC; |
| 19 | import static android.app.admin.DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED; |
| 20 | |
Andrew Scull | 1416bd0 | 2018-01-05 18:33:58 +0000 | [diff] [blame] | 21 | import static com.android.server.testutils.TestUtils.assertExpectException; |
| 22 | |
Kenny Root | d01bb41 | 2019-11-22 09:34:03 -0800 | [diff] [blame] | 23 | import static org.junit.Assert.assertEquals; |
Andrew Scull | 1416bd0 | 2018-01-05 18:33:58 +0000 | [diff] [blame] | 24 | import static org.mockito.Mockito.anyInt; |
Andrew Scull | e6527c1 | 2018-01-05 18:33:58 +0000 | [diff] [blame] | 25 | import static org.mockito.Mockito.atLeastOnce; |
Andrew Scull | 1416bd0 | 2018-01-05 18:33:58 +0000 | [diff] [blame] | 26 | import static org.mockito.Mockito.verify; |
Rich Cannings | f64ec63 | 2019-02-21 12:40:36 -0800 | [diff] [blame] | 27 | import static org.mockito.Mockito.when; |
Andrew Scull | 1416bd0 | 2018-01-05 18:33:58 +0000 | [diff] [blame] | 28 | |
| 29 | import android.os.RemoteException; |
Pavel Grafov | 57f1b66 | 2019-03-27 14:55:38 +0000 | [diff] [blame] | 30 | import android.platform.test.annotations.Presubmit; |
| 31 | |
| 32 | import androidx.test.filters.SmallTest; |
Kenny Root | d01bb41 | 2019-11-22 09:34:03 -0800 | [diff] [blame] | 33 | import androidx.test.runner.AndroidJUnit4; |
Andrew Scull | 1416bd0 | 2018-01-05 18:33:58 +0000 | [diff] [blame] | 34 | |
| 35 | import com.android.internal.widget.LockPatternUtils; |
| 36 | import com.android.internal.widget.VerifyCredentialResponse; |
Andrew Scull | e6527c1 | 2018-01-05 18:33:58 +0000 | [diff] [blame] | 37 | |
Kenny Root | d01bb41 | 2019-11-22 09:34:03 -0800 | [diff] [blame] | 38 | import org.junit.Before; |
| 39 | import org.junit.Test; |
| 40 | import org.junit.runner.RunWith; |
Andrew Scull | e6527c1 | 2018-01-05 18:33:58 +0000 | [diff] [blame] | 41 | import org.mockito.ArgumentCaptor; |
| 42 | |
Rich Cannings | f64ec63 | 2019-02-21 12:40:36 -0800 | [diff] [blame] | 43 | import java.util.ArrayList; |
| 44 | |
Andrew Scull | 1416bd0 | 2018-01-05 18:33:58 +0000 | [diff] [blame] | 45 | /** |
| 46 | * Run the synthetic password tests with caching enabled. |
| 47 | * |
| 48 | * By default, those tests run without caching. Untrusted credential reset depends on caching so |
| 49 | * this class included those tests. |
| 50 | */ |
Pavel Grafov | 57f1b66 | 2019-03-27 14:55:38 +0000 | [diff] [blame] | 51 | @SmallTest |
| 52 | @Presubmit |
Kenny Root | d01bb41 | 2019-11-22 09:34:03 -0800 | [diff] [blame] | 53 | @RunWith(AndroidJUnit4.class) |
Andrew Scull | 1416bd0 | 2018-01-05 18:33:58 +0000 | [diff] [blame] | 54 | public class CachedSyntheticPasswordTests extends SyntheticPasswordTests { |
| 55 | |
Kenny Root | d01bb41 | 2019-11-22 09:34:03 -0800 | [diff] [blame] | 56 | @Before |
| 57 | public void enableSpCache() throws Exception { |
Andrew Scull | 1416bd0 | 2018-01-05 18:33:58 +0000 | [diff] [blame] | 58 | enableSpCaching(true); |
| 59 | } |
| 60 | |
| 61 | private void enableSpCaching(boolean enable) { |
| 62 | when(mDevicePolicyManagerInternal |
| 63 | .canUserHaveUntrustedCredentialReset(anyInt())).thenReturn(enable); |
| 64 | } |
| 65 | |
Kenny Root | d01bb41 | 2019-11-22 09:34:03 -0800 | [diff] [blame] | 66 | @Test |
Andrew Scull | 1416bd0 | 2018-01-05 18:33:58 +0000 | [diff] [blame] | 67 | public void testSyntheticPasswordClearCredentialUntrusted() throws RemoteException { |
Rich Cannings | f64ec63 | 2019-02-21 12:40:36 -0800 | [diff] [blame] | 68 | final byte[] password = "testSyntheticPasswordClearCredential-password".getBytes(); |
| 69 | final byte[] newPassword = "testSyntheticPasswordClearCredential-newpassword".getBytes(); |
Andrew Scull | 1416bd0 | 2018-01-05 18:33:58 +0000 | [diff] [blame] | 70 | |
Rich Cannings | f64ec63 | 2019-02-21 12:40:36 -0800 | [diff] [blame] | 71 | initializeCredentialUnderSP(password, PRIMARY_USER_ID); |
Andrew Scull | 1416bd0 | 2018-01-05 18:33:58 +0000 | [diff] [blame] | 72 | long sid = mGateKeeperService.getSecureUserId(PRIMARY_USER_ID); |
| 73 | // clear password |
| 74 | mService.setLockCredential(null, LockPatternUtils.CREDENTIAL_TYPE_NONE, null, |
Irina Dumitrescu | c90674d | 2019-02-28 17:34:19 +0000 | [diff] [blame] | 75 | PASSWORD_QUALITY_UNSPECIFIED, PRIMARY_USER_ID, true); |
Andrew Scull | 1416bd0 | 2018-01-05 18:33:58 +0000 | [diff] [blame] | 76 | assertEquals(0, mGateKeeperService.getSecureUserId(PRIMARY_USER_ID)); |
| 77 | |
| 78 | // set a new password |
Rich Cannings | f64ec63 | 2019-02-21 12:40:36 -0800 | [diff] [blame] | 79 | mService.setLockCredential(newPassword, LockPatternUtils.CREDENTIAL_TYPE_PASSWORD, null, |
Irina Dumitrescu | c90674d | 2019-02-28 17:34:19 +0000 | [diff] [blame] | 80 | PASSWORD_QUALITY_ALPHABETIC, PRIMARY_USER_ID, false); |
Rich Cannings | f64ec63 | 2019-02-21 12:40:36 -0800 | [diff] [blame] | 81 | assertEquals(VerifyCredentialResponse.RESPONSE_OK, mService.verifyCredential(newPassword, |
| 82 | LockPatternUtils.CREDENTIAL_TYPE_PASSWORD, 0, PRIMARY_USER_ID) |
| 83 | .getResponseCode()); |
Andrew Scull | 1416bd0 | 2018-01-05 18:33:58 +0000 | [diff] [blame] | 84 | assertNotEquals(sid, mGateKeeperService.getSecureUserId(PRIMARY_USER_ID)); |
| 85 | } |
| 86 | |
Kenny Root | d01bb41 | 2019-11-22 09:34:03 -0800 | [diff] [blame] | 87 | @Test |
Andrew Scull | 1416bd0 | 2018-01-05 18:33:58 +0000 | [diff] [blame] | 88 | public void testSyntheticPasswordChangeCredentialUntrusted() throws RemoteException { |
Rich Cannings | f64ec63 | 2019-02-21 12:40:36 -0800 | [diff] [blame] | 89 | final byte[] password = "testSyntheticPasswordClearCredential-password".getBytes(); |
| 90 | final byte[] newPassword = "testSyntheticPasswordClearCredential-newpassword".getBytes(); |
Andrew Scull | 1416bd0 | 2018-01-05 18:33:58 +0000 | [diff] [blame] | 91 | |
Rich Cannings | f64ec63 | 2019-02-21 12:40:36 -0800 | [diff] [blame] | 92 | initializeCredentialUnderSP(password, PRIMARY_USER_ID); |
Andrew Scull | 1416bd0 | 2018-01-05 18:33:58 +0000 | [diff] [blame] | 93 | long sid = mGateKeeperService.getSecureUserId(PRIMARY_USER_ID); |
| 94 | // Untrusted change password |
Rich Cannings | f64ec63 | 2019-02-21 12:40:36 -0800 | [diff] [blame] | 95 | mService.setLockCredential(newPassword, LockPatternUtils.CREDENTIAL_TYPE_PASSWORD, null, |
Irina Dumitrescu | c90674d | 2019-02-28 17:34:19 +0000 | [diff] [blame] | 96 | PASSWORD_QUALITY_ALPHABETIC, PRIMARY_USER_ID, true); |
Andrew Scull | 1416bd0 | 2018-01-05 18:33:58 +0000 | [diff] [blame] | 97 | assertNotEquals(0, mGateKeeperService.getSecureUserId(PRIMARY_USER_ID)); |
| 98 | assertNotEquals(sid, mGateKeeperService.getSecureUserId(PRIMARY_USER_ID)); |
| 99 | |
| 100 | // Verify the password |
Rich Cannings | f64ec63 | 2019-02-21 12:40:36 -0800 | [diff] [blame] | 101 | assertEquals(VerifyCredentialResponse.RESPONSE_OK, mService.verifyCredential(newPassword, |
| 102 | LockPatternUtils.CREDENTIAL_TYPE_PASSWORD, 0, PRIMARY_USER_ID).getResponseCode()); |
Andrew Scull | 1416bd0 | 2018-01-05 18:33:58 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Kenny Root | d01bb41 | 2019-11-22 09:34:03 -0800 | [diff] [blame] | 105 | @Test |
Andrew Scull | e6527c1 | 2018-01-05 18:33:58 +0000 | [diff] [blame] | 106 | public void testUntrustedCredentialChangeMaintainsAuthSecret() throws RemoteException { |
Rich Cannings | f64ec63 | 2019-02-21 12:40:36 -0800 | [diff] [blame] | 107 | final byte[] password = |
| 108 | "testUntrustedCredentialChangeMaintainsAuthSecret-password".getBytes(); |
| 109 | final byte[] newPassword = |
| 110 | "testUntrustedCredentialChangeMaintainsAuthSecret-newpassword".getBytes(); |
Andrew Scull | e6527c1 | 2018-01-05 18:33:58 +0000 | [diff] [blame] | 111 | |
Rich Cannings | f64ec63 | 2019-02-21 12:40:36 -0800 | [diff] [blame] | 112 | initializeCredentialUnderSP(password, PRIMARY_USER_ID); |
Andrew Scull | e6527c1 | 2018-01-05 18:33:58 +0000 | [diff] [blame] | 113 | // Untrusted change password |
Rich Cannings | f64ec63 | 2019-02-21 12:40:36 -0800 | [diff] [blame] | 114 | mService.setLockCredential(newPassword, LockPatternUtils.CREDENTIAL_TYPE_PASSWORD, null, |
Irina Dumitrescu | c90674d | 2019-02-28 17:34:19 +0000 | [diff] [blame] | 115 | PASSWORD_QUALITY_ALPHABETIC, PRIMARY_USER_ID, true); |
Andrew Scull | e6527c1 | 2018-01-05 18:33:58 +0000 | [diff] [blame] | 116 | |
| 117 | // Verify the password |
Rich Cannings | f64ec63 | 2019-02-21 12:40:36 -0800 | [diff] [blame] | 118 | assertEquals(VerifyCredentialResponse.RESPONSE_OK, mService.verifyCredential(newPassword, |
| 119 | LockPatternUtils.CREDENTIAL_TYPE_PASSWORD, 0, PRIMARY_USER_ID) |
| 120 | .getResponseCode()); |
Andrew Scull | e6527c1 | 2018-01-05 18:33:58 +0000 | [diff] [blame] | 121 | |
| 122 | // Ensure the same secret was passed each time |
| 123 | ArgumentCaptor<ArrayList<Byte>> secret = ArgumentCaptor.forClass(ArrayList.class); |
| 124 | verify(mAuthSecretService, atLeastOnce()).primaryUserCredential(secret.capture()); |
| 125 | assertEquals(1, secret.getAllValues().stream().distinct().count()); |
| 126 | } |
| 127 | |
Kenny Root | d01bb41 | 2019-11-22 09:34:03 -0800 | [diff] [blame] | 128 | @Test |
Andrew Scull | 1416bd0 | 2018-01-05 18:33:58 +0000 | [diff] [blame] | 129 | public void testUntrustedCredentialChangeBlockedIfSpNotCached() throws RemoteException { |
Rich Cannings | f64ec63 | 2019-02-21 12:40:36 -0800 | [diff] [blame] | 130 | final byte[] password = |
| 131 | "testUntrustedCredentialChangeBlockedIfSpNotCached-password".getBytes(); |
| 132 | final byte[] newPassword = |
| 133 | "testUntrustedCredentialChangeBlockedIfSpNotCached-newpassword".getBytes(); |
Andrew Scull | 1416bd0 | 2018-01-05 18:33:58 +0000 | [diff] [blame] | 134 | |
| 135 | // Disable caching for this test |
| 136 | enableSpCaching(false); |
| 137 | |
Rich Cannings | f64ec63 | 2019-02-21 12:40:36 -0800 | [diff] [blame] | 138 | initializeCredentialUnderSP(password, PRIMARY_USER_ID); |
Andrew Scull | 1416bd0 | 2018-01-05 18:33:58 +0000 | [diff] [blame] | 139 | long sid = mGateKeeperService.getSecureUserId(PRIMARY_USER_ID); |
| 140 | // Untrusted change password |
Irina Dumitrescu | c90674d | 2019-02-28 17:34:19 +0000 | [diff] [blame] | 141 | assertExpectException( |
| 142 | IllegalStateException.class, |
| 143 | /* messageRegex= */ "Untrusted credential reset not possible without cached SP", |
Rich Cannings | f64ec63 | 2019-02-21 12:40:36 -0800 | [diff] [blame] | 144 | () -> mService.setLockCredential(newPassword, |
| 145 | LockPatternUtils.CREDENTIAL_TYPE_PASSWORD, null, |
Irina Dumitrescu | c90674d | 2019-02-28 17:34:19 +0000 | [diff] [blame] | 146 | PASSWORD_QUALITY_ALPHABETIC, PRIMARY_USER_ID, true)); |
Andrew Scull | 1416bd0 | 2018-01-05 18:33:58 +0000 | [diff] [blame] | 147 | assertEquals(sid, mGateKeeperService.getSecureUserId(PRIMARY_USER_ID)); |
| 148 | |
| 149 | // Verify the new password doesn't work but the old one still does |
Rich Cannings | f64ec63 | 2019-02-21 12:40:36 -0800 | [diff] [blame] | 150 | assertEquals(VerifyCredentialResponse.RESPONSE_ERROR, mService.verifyCredential(newPassword, |
| 151 | LockPatternUtils.CREDENTIAL_TYPE_PASSWORD, 0, PRIMARY_USER_ID) |
Andrew Scull | 1416bd0 | 2018-01-05 18:33:58 +0000 | [diff] [blame] | 152 | .getResponseCode()); |
Rich Cannings | f64ec63 | 2019-02-21 12:40:36 -0800 | [diff] [blame] | 153 | assertEquals(VerifyCredentialResponse.RESPONSE_OK, mService.verifyCredential(password, |
| 154 | LockPatternUtils.CREDENTIAL_TYPE_PASSWORD, 0, PRIMARY_USER_ID) |
Andrew Scull | 1416bd0 | 2018-01-05 18:33:58 +0000 | [diff] [blame] | 155 | .getResponseCode()); |
| 156 | } |
| 157 | |
| 158 | } |