1bd086f1963f13d13a03928f41b9b7979bebffa26Robert Berrypackage com.android.server.locksettings.recoverablekeystore.storage;
2bd086f1963f13d13a03928f41b9b7979bebffa26Robert Berry
35658837b464275b7ae87fec5529c7344bf21b733Robert Berryimport static org.junit.Assert.assertArrayEquals;
4bd086f1963f13d13a03928f41b9b7979bebffa26Robert Berryimport static org.junit.Assert.assertEquals;
55658837b464275b7ae87fec5529c7344bf21b733Robert Berryimport static org.junit.Assert.assertFalse;
6bd086f1963f13d13a03928f41b9b7979bebffa26Robert Berryimport static org.junit.Assert.assertNull;
75658837b464275b7ae87fec5529c7344bf21b733Robert Berryimport static org.junit.Assert.assertTrue;
8bd086f1963f13d13a03928f41b9b7979bebffa26Robert Berry
95658837b464275b7ae87fec5529c7344bf21b733Robert Berryimport android.content.Context;
105658837b464275b7ae87fec5529c7344bf21b733Robert Berryimport android.os.FileUtils;
115658837b464275b7ae87fec5529c7344bf21b733Robert Berryimport android.security.keystore.recovery.KeyChainProtectionParams;
120916e7ca44aba5e6c89d75007da805697fdace9eDmitry Dementyevimport android.security.keystore.recovery.KeyChainSnapshot;
135658837b464275b7ae87fec5529c7344bf21b733Robert Berryimport android.security.keystore.recovery.KeyDerivationParams;
145658837b464275b7ae87fec5529c7344bf21b733Robert Berryimport android.security.keystore.recovery.WrappedApplicationKey;
155658837b464275b7ae87fec5529c7344bf21b733Robert Berryimport android.support.test.InstrumentationRegistry;
16bd086f1963f13d13a03928f41b9b7979bebffa26Robert Berryimport android.support.test.filters.SmallTest;
17bd086f1963f13d13a03928f41b9b7979bebffa26Robert Berryimport android.support.test.runner.AndroidJUnit4;
18bd086f1963f13d13a03928f41b9b7979bebffa26Robert Berry
197cd217b33d0bbef9882a2b594dcfd130461c7bc5Robert Berryimport com.android.server.locksettings.recoverablekeystore.TestData;
207cd217b33d0bbef9882a2b594dcfd130461c7bc5Robert Berry
215658837b464275b7ae87fec5529c7344bf21b733Robert Berryimport com.google.common.io.Files;
225658837b464275b7ae87fec5529c7344bf21b733Robert Berry
235658837b464275b7ae87fec5529c7344bf21b733Robert Berryimport org.junit.After;
245658837b464275b7ae87fec5529c7344bf21b733Robert Berryimport org.junit.Before;
25bd086f1963f13d13a03928f41b9b7979bebffa26Robert Berryimport org.junit.Test;
26bd086f1963f13d13a03928f41b9b7979bebffa26Robert Berryimport org.junit.runner.RunWith;
27bd086f1963f13d13a03928f41b9b7979bebffa26Robert Berry
285658837b464275b7ae87fec5529c7344bf21b733Robert Berryimport java.io.File;
295658837b464275b7ae87fec5529c7344bf21b733Robert Berryimport java.nio.charset.StandardCharsets;
305658837b464275b7ae87fec5529c7344bf21b733Robert Berryimport java.security.cert.CertPath;
317cd217b33d0bbef9882a2b594dcfd130461c7bc5Robert Berryimport java.security.cert.CertificateException;
32bd086f1963f13d13a03928f41b9b7979bebffa26Robert Berryimport java.util.ArrayList;
335658837b464275b7ae87fec5529c7344bf21b733Robert Berryimport java.util.List;
34bd086f1963f13d13a03928f41b9b7979bebffa26Robert Berry
35bd086f1963f13d13a03928f41b9b7979bebffa26Robert Berry@SmallTest
36bd086f1963f13d13a03928f41b9b7979bebffa26Robert Berry@RunWith(AndroidJUnit4.class)
37bd086f1963f13d13a03928f41b9b7979bebffa26Robert Berrypublic class RecoverySnapshotStorageTest {
385658837b464275b7ae87fec5529c7344bf21b733Robert Berry    private static final int COUNTER_ID = 432546;
395658837b464275b7ae87fec5529c7344bf21b733Robert Berry    private static final int MAX_ATTEMPTS = 10;
405658837b464275b7ae87fec5529c7344bf21b733Robert Berry    private static final byte[] SERVER_PARAMS = new byte[] { 12, 8, 2, 4, 15, 64 };
415658837b464275b7ae87fec5529c7344bf21b733Robert Berry    private static final byte[] KEY_BLOB = new byte[] { 124, 56, 53, 99, 0, 0, 1 };
425658837b464275b7ae87fec5529c7344bf21b733Robert Berry    private static final CertPath CERT_PATH = TestData.CERT_PATH_2;
435658837b464275b7ae87fec5529c7344bf21b733Robert Berry    private static final int SECRET_TYPE = KeyChainProtectionParams.TYPE_LOCKSCREEN;
445658837b464275b7ae87fec5529c7344bf21b733Robert Berry    private static final int LOCK_SCREEN_UI = KeyChainProtectionParams.UI_FORMAT_PATTERN;
455658837b464275b7ae87fec5529c7344bf21b733Robert Berry    private static final byte[] SALT = new byte[] { 1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1 };
465658837b464275b7ae87fec5529c7344bf21b733Robert Berry    private static final int MEMORY_DIFFICULTY = 12;
475658837b464275b7ae87fec5529c7344bf21b733Robert Berry    private static final byte[] SECRET = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0 };
485658837b464275b7ae87fec5529c7344bf21b733Robert Berry
495658837b464275b7ae87fec5529c7344bf21b733Robert Berry    private static final String TEST_KEY_1_ALIAS = "alias1";
505658837b464275b7ae87fec5529c7344bf21b733Robert Berry    private static final byte[] TEST_KEY_1_BYTES = new byte[] { 100, 32, 43, 66, 77, 88 };
515658837b464275b7ae87fec5529c7344bf21b733Robert Berry
525658837b464275b7ae87fec5529c7344bf21b733Robert Berry    private static final String TEST_KEY_2_ALIAS = "alias11";
535658837b464275b7ae87fec5529c7344bf21b733Robert Berry    private static final byte[] TEST_KEY_2_BYTES = new byte[] { 100, 0, 0, 99, 33, 11 };
545658837b464275b7ae87fec5529c7344bf21b733Robert Berry
555658837b464275b7ae87fec5529c7344bf21b733Robert Berry    private static final String TEST_KEY_3_ALIAS = "alias111";
565658837b464275b7ae87fec5529c7344bf21b733Robert Berry    private static final byte[] TEST_KEY_3_BYTES = new byte[] { 1, 1, 1, 0, 2, 8, 100 };
575658837b464275b7ae87fec5529c7344bf21b733Robert Berry
585658837b464275b7ae87fec5529c7344bf21b733Robert Berry    private static final int TEST_UID = 1000;
595658837b464275b7ae87fec5529c7344bf21b733Robert Berry    private static final String SNAPSHOT_DIRECTORY = "recoverablekeystore/snapshots";
605658837b464275b7ae87fec5529c7344bf21b733Robert Berry    private static final String SNAPSHOT_FILE_PATH = "1000.xml";
615658837b464275b7ae87fec5529c7344bf21b733Robert Berry    private static final String SNAPSHOT_TOP_LEVEL_DIRECTORY = "recoverablekeystore";
625658837b464275b7ae87fec5529c7344bf21b733Robert Berry
637cd217b33d0bbef9882a2b594dcfd130461c7bc5Robert Berry    private static final KeyChainSnapshot MINIMAL_KEYCHAIN_SNAPSHOT =
645658837b464275b7ae87fec5529c7344bf21b733Robert Berry            createTestKeyChainSnapshot(1);
65bd086f1963f13d13a03928f41b9b7979bebffa26Robert Berry
665658837b464275b7ae87fec5529c7344bf21b733Robert Berry    private Context mContext;
675658837b464275b7ae87fec5529c7344bf21b733Robert Berry    private RecoverySnapshotStorage mRecoverySnapshotStorage;
685658837b464275b7ae87fec5529c7344bf21b733Robert Berry
695658837b464275b7ae87fec5529c7344bf21b733Robert Berry    @Before
705658837b464275b7ae87fec5529c7344bf21b733Robert Berry    public void setUp() {
715658837b464275b7ae87fec5529c7344bf21b733Robert Berry        mContext = InstrumentationRegistry.getTargetContext();
725658837b464275b7ae87fec5529c7344bf21b733Robert Berry        mRecoverySnapshotStorage = new RecoverySnapshotStorage(mContext.getFilesDir());
735658837b464275b7ae87fec5529c7344bf21b733Robert Berry    }
745658837b464275b7ae87fec5529c7344bf21b733Robert Berry
755658837b464275b7ae87fec5529c7344bf21b733Robert Berry    @After
765658837b464275b7ae87fec5529c7344bf21b733Robert Berry    public void tearDown() {
775658837b464275b7ae87fec5529c7344bf21b733Robert Berry        File file = new File(mContext.getFilesDir(), SNAPSHOT_TOP_LEVEL_DIRECTORY);
785658837b464275b7ae87fec5529c7344bf21b733Robert Berry        FileUtils.deleteContentsAndDir(file);
795658837b464275b7ae87fec5529c7344bf21b733Robert Berry    }
80bd086f1963f13d13a03928f41b9b7979bebffa26Robert Berry
81bd086f1963f13d13a03928f41b9b7979bebffa26Robert Berry    @Test
82bd086f1963f13d13a03928f41b9b7979bebffa26Robert Berry    public void get_isNullForNonExistentSnapshot() {
83bd086f1963f13d13a03928f41b9b7979bebffa26Robert Berry        assertNull(mRecoverySnapshotStorage.get(1000));
84bd086f1963f13d13a03928f41b9b7979bebffa26Robert Berry    }
85bd086f1963f13d13a03928f41b9b7979bebffa26Robert Berry
86bd086f1963f13d13a03928f41b9b7979bebffa26Robert Berry    @Test
87bd086f1963f13d13a03928f41b9b7979bebffa26Robert Berry    public void get_returnsSetSnapshot() {
885658837b464275b7ae87fec5529c7344bf21b733Robert Berry        mRecoverySnapshotStorage.put(TEST_UID, MINIMAL_KEYCHAIN_SNAPSHOT);
8916d9db57f513eb13eeb2486d2d4770f59faf5550Dmitry Dementyev
905658837b464275b7ae87fec5529c7344bf21b733Robert Berry        assertEquals(MINIMAL_KEYCHAIN_SNAPSHOT, mRecoverySnapshotStorage.get(TEST_UID));
915658837b464275b7ae87fec5529c7344bf21b733Robert Berry    }
925658837b464275b7ae87fec5529c7344bf21b733Robert Berry
935658837b464275b7ae87fec5529c7344bf21b733Robert Berry    @Test
945658837b464275b7ae87fec5529c7344bf21b733Robert Berry    public void get_readsFromDiskIfNoneInMemory() {
955658837b464275b7ae87fec5529c7344bf21b733Robert Berry        mRecoverySnapshotStorage.put(TEST_UID, MINIMAL_KEYCHAIN_SNAPSHOT);
965658837b464275b7ae87fec5529c7344bf21b733Robert Berry        RecoverySnapshotStorage storage = new RecoverySnapshotStorage(mContext.getFilesDir());
9716d9db57f513eb13eeb2486d2d4770f59faf5550Dmitry Dementyev
985658837b464275b7ae87fec5529c7344bf21b733Robert Berry        assertKeyChainSnapshotsAreEqual(MINIMAL_KEYCHAIN_SNAPSHOT, storage.get(TEST_UID));
99bd086f1963f13d13a03928f41b9b7979bebffa26Robert Berry    }
100bd086f1963f13d13a03928f41b9b7979bebffa26Robert Berry
101bd086f1963f13d13a03928f41b9b7979bebffa26Robert Berry    @Test
1025658837b464275b7ae87fec5529c7344bf21b733Robert Berry    public void get_deletesFileIfItIsInvalidSnapshot() throws Exception {
1035658837b464275b7ae87fec5529c7344bf21b733Robert Berry        File folder = new File(mContext.getFilesDir(), SNAPSHOT_DIRECTORY);
1045658837b464275b7ae87fec5529c7344bf21b733Robert Berry        folder.mkdirs();
1055658837b464275b7ae87fec5529c7344bf21b733Robert Berry        File file = new File(folder, SNAPSHOT_FILE_PATH);
1065658837b464275b7ae87fec5529c7344bf21b733Robert Berry        byte[] fileContents = "<keyChainSnapshot></keyChainSnapshot>".getBytes(
1075658837b464275b7ae87fec5529c7344bf21b733Robert Berry                StandardCharsets.UTF_8);
1085658837b464275b7ae87fec5529c7344bf21b733Robert Berry        Files.write(fileContents, file);
1095658837b464275b7ae87fec5529c7344bf21b733Robert Berry        assertTrue(file.exists());
110bd086f1963f13d13a03928f41b9b7979bebffa26Robert Berry
1115658837b464275b7ae87fec5529c7344bf21b733Robert Berry        assertNull(mRecoverySnapshotStorage.get(TEST_UID));
112bd086f1963f13d13a03928f41b9b7979bebffa26Robert Berry
1135658837b464275b7ae87fec5529c7344bf21b733Robert Berry        assertFalse(file.exists());
1145658837b464275b7ae87fec5529c7344bf21b733Robert Berry    }
1155658837b464275b7ae87fec5529c7344bf21b733Robert Berry
1165658837b464275b7ae87fec5529c7344bf21b733Robert Berry    @Test
1175658837b464275b7ae87fec5529c7344bf21b733Robert Berry    public void put_overwritesOldFiles() {
1185658837b464275b7ae87fec5529c7344bf21b733Robert Berry        int snapshotVersion = 2;
1195658837b464275b7ae87fec5529c7344bf21b733Robert Berry        mRecoverySnapshotStorage.put(TEST_UID, MINIMAL_KEYCHAIN_SNAPSHOT);
1205658837b464275b7ae87fec5529c7344bf21b733Robert Berry
1215658837b464275b7ae87fec5529c7344bf21b733Robert Berry        mRecoverySnapshotStorage.put(TEST_UID, createTestKeyChainSnapshot(snapshotVersion));
1225658837b464275b7ae87fec5529c7344bf21b733Robert Berry
1235658837b464275b7ae87fec5529c7344bf21b733Robert Berry        KeyChainSnapshot snapshot = new RecoverySnapshotStorage(mContext.getFilesDir())
1245658837b464275b7ae87fec5529c7344bf21b733Robert Berry                .get(TEST_UID);
1255658837b464275b7ae87fec5529c7344bf21b733Robert Berry        assertEquals(snapshotVersion, snapshot.getSnapshotVersion());
126bd086f1963f13d13a03928f41b9b7979bebffa26Robert Berry    }
1277cd217b33d0bbef9882a2b594dcfd130461c7bc5Robert Berry
1285658837b464275b7ae87fec5529c7344bf21b733Robert Berry    @Test
1295658837b464275b7ae87fec5529c7344bf21b733Robert Berry    public void put_doesNotThrowIfCannotCreateFiles() throws Exception {
1305658837b464275b7ae87fec5529c7344bf21b733Robert Berry        File evilFile = new File(mContext.getFilesDir(), "recoverablekeystore");
1315658837b464275b7ae87fec5529c7344bf21b733Robert Berry        Files.write(new byte[] { 1 }, evilFile);
1325658837b464275b7ae87fec5529c7344bf21b733Robert Berry
1335658837b464275b7ae87fec5529c7344bf21b733Robert Berry        mRecoverySnapshotStorage.put(TEST_UID, MINIMAL_KEYCHAIN_SNAPSHOT);
1345658837b464275b7ae87fec5529c7344bf21b733Robert Berry
1355658837b464275b7ae87fec5529c7344bf21b733Robert Berry        assertNull(new RecoverySnapshotStorage(mContext.getFilesDir()).get(TEST_UID));
1365658837b464275b7ae87fec5529c7344bf21b733Robert Berry    }
1375658837b464275b7ae87fec5529c7344bf21b733Robert Berry
1385658837b464275b7ae87fec5529c7344bf21b733Robert Berry    @Test
1395658837b464275b7ae87fec5529c7344bf21b733Robert Berry    public void remove_removesSnapshotsFromMemory() {
1405658837b464275b7ae87fec5529c7344bf21b733Robert Berry        mRecoverySnapshotStorage.put(TEST_UID, MINIMAL_KEYCHAIN_SNAPSHOT);
1415658837b464275b7ae87fec5529c7344bf21b733Robert Berry        mRecoverySnapshotStorage.remove(TEST_UID);
1425658837b464275b7ae87fec5529c7344bf21b733Robert Berry
1435658837b464275b7ae87fec5529c7344bf21b733Robert Berry        assertNull(mRecoverySnapshotStorage.get(TEST_UID));
1445658837b464275b7ae87fec5529c7344bf21b733Robert Berry    }
1455658837b464275b7ae87fec5529c7344bf21b733Robert Berry
1465658837b464275b7ae87fec5529c7344bf21b733Robert Berry    @Test
1475658837b464275b7ae87fec5529c7344bf21b733Robert Berry    public void remove_removesSnapshotsFromDisk() {
1485658837b464275b7ae87fec5529c7344bf21b733Robert Berry        mRecoverySnapshotStorage.put(TEST_UID, MINIMAL_KEYCHAIN_SNAPSHOT);
1495658837b464275b7ae87fec5529c7344bf21b733Robert Berry
1505658837b464275b7ae87fec5529c7344bf21b733Robert Berry        new RecoverySnapshotStorage(mContext.getFilesDir()).remove(TEST_UID);
1515658837b464275b7ae87fec5529c7344bf21b733Robert Berry
1525658837b464275b7ae87fec5529c7344bf21b733Robert Berry        assertNull(new RecoverySnapshotStorage(mContext.getFilesDir()).get(TEST_UID));
1535658837b464275b7ae87fec5529c7344bf21b733Robert Berry    }
1545658837b464275b7ae87fec5529c7344bf21b733Robert Berry
1555658837b464275b7ae87fec5529c7344bf21b733Robert Berry    private void assertKeyChainSnapshotsAreEqual(KeyChainSnapshot a, KeyChainSnapshot b) {
1565658837b464275b7ae87fec5529c7344bf21b733Robert Berry        assertEquals(b.getCounterId(), a.getCounterId());
1575658837b464275b7ae87fec5529c7344bf21b733Robert Berry        assertEquals(b.getSnapshotVersion(), a.getSnapshotVersion());
1585658837b464275b7ae87fec5529c7344bf21b733Robert Berry        assertArrayEquals(b.getServerParams(), a.getServerParams());
1595658837b464275b7ae87fec5529c7344bf21b733Robert Berry        assertEquals(b.getMaxAttempts(), a.getMaxAttempts());
1605658837b464275b7ae87fec5529c7344bf21b733Robert Berry        assertArrayEquals(b.getEncryptedRecoveryKeyBlob(), a.getEncryptedRecoveryKeyBlob());
1615658837b464275b7ae87fec5529c7344bf21b733Robert Berry        assertEquals(b.getTrustedHardwareCertPath(), a.getTrustedHardwareCertPath());
1625658837b464275b7ae87fec5529c7344bf21b733Robert Berry
1635658837b464275b7ae87fec5529c7344bf21b733Robert Berry        List<WrappedApplicationKey> aKeys = a.getWrappedApplicationKeys();
1645658837b464275b7ae87fec5529c7344bf21b733Robert Berry        List<WrappedApplicationKey> bKeys = b.getWrappedApplicationKeys();
1655658837b464275b7ae87fec5529c7344bf21b733Robert Berry        assertEquals(bKeys.size(), aKeys.size());
1665658837b464275b7ae87fec5529c7344bf21b733Robert Berry        for (int i = 0; i < aKeys.size(); i++) {
1675658837b464275b7ae87fec5529c7344bf21b733Robert Berry            assertWrappedApplicationKeysAreEqual(aKeys.get(i), bKeys.get(i));
1685658837b464275b7ae87fec5529c7344bf21b733Robert Berry        }
1695658837b464275b7ae87fec5529c7344bf21b733Robert Berry
1705658837b464275b7ae87fec5529c7344bf21b733Robert Berry        List<KeyChainProtectionParams> aParams = a.getKeyChainProtectionParams();
1715658837b464275b7ae87fec5529c7344bf21b733Robert Berry        List<KeyChainProtectionParams> bParams = b.getKeyChainProtectionParams();
1725658837b464275b7ae87fec5529c7344bf21b733Robert Berry        assertEquals(bParams.size(), aParams.size());
1735658837b464275b7ae87fec5529c7344bf21b733Robert Berry        for (int i = 0; i < aParams.size(); i++) {
1745658837b464275b7ae87fec5529c7344bf21b733Robert Berry            assertKeyChainProtectionParamsAreEqual(aParams.get(i), bParams.get(i));
1755658837b464275b7ae87fec5529c7344bf21b733Robert Berry        }
1765658837b464275b7ae87fec5529c7344bf21b733Robert Berry    }
1775658837b464275b7ae87fec5529c7344bf21b733Robert Berry
1785658837b464275b7ae87fec5529c7344bf21b733Robert Berry    private void assertWrappedApplicationKeysAreEqual(
1795658837b464275b7ae87fec5529c7344bf21b733Robert Berry            WrappedApplicationKey a, WrappedApplicationKey b) {
1805658837b464275b7ae87fec5529c7344bf21b733Robert Berry        assertEquals(b.getAlias(), a.getAlias());
1815658837b464275b7ae87fec5529c7344bf21b733Robert Berry        assertArrayEquals(b.getEncryptedKeyMaterial(), a.getEncryptedKeyMaterial());
1825658837b464275b7ae87fec5529c7344bf21b733Robert Berry    }
1835658837b464275b7ae87fec5529c7344bf21b733Robert Berry
1845658837b464275b7ae87fec5529c7344bf21b733Robert Berry    private void assertKeyChainProtectionParamsAreEqual(
1855658837b464275b7ae87fec5529c7344bf21b733Robert Berry            KeyChainProtectionParams a, KeyChainProtectionParams b) {
1865658837b464275b7ae87fec5529c7344bf21b733Robert Berry        assertEquals(b.getUserSecretType(), a.getUserSecretType());
1875658837b464275b7ae87fec5529c7344bf21b733Robert Berry        assertEquals(b.getLockScreenUiFormat(), a.getLockScreenUiFormat());
1885658837b464275b7ae87fec5529c7344bf21b733Robert Berry        assertKeyDerivationParamsAreEqual(a.getKeyDerivationParams(), b.getKeyDerivationParams());
1895658837b464275b7ae87fec5529c7344bf21b733Robert Berry    }
1905658837b464275b7ae87fec5529c7344bf21b733Robert Berry
1915658837b464275b7ae87fec5529c7344bf21b733Robert Berry    private void assertKeyDerivationParamsAreEqual(KeyDerivationParams a, KeyDerivationParams b) {
1925658837b464275b7ae87fec5529c7344bf21b733Robert Berry        assertEquals(b.getAlgorithm(), a.getAlgorithm());
1935658837b464275b7ae87fec5529c7344bf21b733Robert Berry        assertEquals(b.getMemoryDifficulty(), a.getMemoryDifficulty());
1945658837b464275b7ae87fec5529c7344bf21b733Robert Berry        assertArrayEquals(b.getSalt(), a.getSalt());
1955658837b464275b7ae87fec5529c7344bf21b733Robert Berry    }
1965658837b464275b7ae87fec5529c7344bf21b733Robert Berry
1975658837b464275b7ae87fec5529c7344bf21b733Robert Berry    private static KeyChainSnapshot createTestKeyChainSnapshot(int snapshotVersion) {
1985658837b464275b7ae87fec5529c7344bf21b733Robert Berry        KeyDerivationParams keyDerivationParams =
1995658837b464275b7ae87fec5529c7344bf21b733Robert Berry                KeyDerivationParams.createScryptParams(SALT, MEMORY_DIFFICULTY);
2005658837b464275b7ae87fec5529c7344bf21b733Robert Berry        KeyChainProtectionParams keyChainProtectionParams = new KeyChainProtectionParams.Builder()
2015658837b464275b7ae87fec5529c7344bf21b733Robert Berry                .setKeyDerivationParams(keyDerivationParams)
2025658837b464275b7ae87fec5529c7344bf21b733Robert Berry                .setUserSecretType(SECRET_TYPE)
2035658837b464275b7ae87fec5529c7344bf21b733Robert Berry                .setLockScreenUiFormat(LOCK_SCREEN_UI)
2045658837b464275b7ae87fec5529c7344bf21b733Robert Berry                .setSecret(SECRET)
2055658837b464275b7ae87fec5529c7344bf21b733Robert Berry                .build();
2065658837b464275b7ae87fec5529c7344bf21b733Robert Berry        ArrayList<KeyChainProtectionParams> keyChainProtectionParamsList =
2075658837b464275b7ae87fec5529c7344bf21b733Robert Berry                new ArrayList<>(1);
2085658837b464275b7ae87fec5529c7344bf21b733Robert Berry        keyChainProtectionParamsList.add(keyChainProtectionParams);
2095658837b464275b7ae87fec5529c7344bf21b733Robert Berry
2105658837b464275b7ae87fec5529c7344bf21b733Robert Berry        ArrayList<WrappedApplicationKey> keyList = new ArrayList<>();
2115658837b464275b7ae87fec5529c7344bf21b733Robert Berry        keyList.add(createKey(TEST_KEY_1_ALIAS, TEST_KEY_1_BYTES));
2125658837b464275b7ae87fec5529c7344bf21b733Robert Berry        keyList.add(createKey(TEST_KEY_2_ALIAS, TEST_KEY_2_BYTES));
2135658837b464275b7ae87fec5529c7344bf21b733Robert Berry        keyList.add(createKey(TEST_KEY_3_ALIAS, TEST_KEY_3_BYTES));
2145658837b464275b7ae87fec5529c7344bf21b733Robert Berry
2157cd217b33d0bbef9882a2b594dcfd130461c7bc5Robert Berry        try {
2167cd217b33d0bbef9882a2b594dcfd130461c7bc5Robert Berry            return new KeyChainSnapshot.Builder()
2175658837b464275b7ae87fec5529c7344bf21b733Robert Berry                    .setCounterId(COUNTER_ID)
2185658837b464275b7ae87fec5529c7344bf21b733Robert Berry                    .setSnapshotVersion(snapshotVersion)
2195658837b464275b7ae87fec5529c7344bf21b733Robert Berry                    .setServerParams(SERVER_PARAMS)
2205658837b464275b7ae87fec5529c7344bf21b733Robert Berry                    .setMaxAttempts(MAX_ATTEMPTS)
2215658837b464275b7ae87fec5529c7344bf21b733Robert Berry                    .setEncryptedRecoveryKeyBlob(KEY_BLOB)
2225658837b464275b7ae87fec5529c7344bf21b733Robert Berry                    .setKeyChainProtectionParams(keyChainProtectionParamsList)
2235658837b464275b7ae87fec5529c7344bf21b733Robert Berry                    .setWrappedApplicationKeys(keyList)
2245658837b464275b7ae87fec5529c7344bf21b733Robert Berry                    .setTrustedHardwareCertPath(CERT_PATH)
2257cd217b33d0bbef9882a2b594dcfd130461c7bc5Robert Berry                    .build();
2267cd217b33d0bbef9882a2b594dcfd130461c7bc5Robert Berry        } catch (CertificateException e) {
2277cd217b33d0bbef9882a2b594dcfd130461c7bc5Robert Berry            throw new RuntimeException(e);
2287cd217b33d0bbef9882a2b594dcfd130461c7bc5Robert Berry        }
2297cd217b33d0bbef9882a2b594dcfd130461c7bc5Robert Berry    }
2305658837b464275b7ae87fec5529c7344bf21b733Robert Berry
2315658837b464275b7ae87fec5529c7344bf21b733Robert Berry    private static WrappedApplicationKey createKey(String alias, byte[] bytes) {
2325658837b464275b7ae87fec5529c7344bf21b733Robert Berry        return new WrappedApplicationKey.Builder()
2335658837b464275b7ae87fec5529c7344bf21b733Robert Berry                .setAlias(alias)
2345658837b464275b7ae87fec5529c7344bf21b733Robert Berry                .setEncryptedKeyMaterial(bytes)
2355658837b464275b7ae87fec5529c7344bf21b733Robert Berry                .build();
2365658837b464275b7ae87fec5529c7344bf21b733Robert Berry    }
237bd086f1963f13d13a03928f41b9b7979bebffa26Robert Berry}
238