Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit b4608a09 authored by Rubin Xu's avatar Rubin Xu
Browse files

Remove CredentialHash serialization

They were added to support lockscreen FRP on a device
with Gatekeeper credential. However that feature
was removed before shipping (ag/2527709), so these
serialization routines are no longer relavent.

Bug: 36814845
Bug: 63619579
Test: atest com.android.server.locksettings
Change-Id: I69be4beeab61b5fcf4b32c52251591729f3da4f3
parent 2505d758
Loading
Loading
Loading
Loading
+0 −38
Original line number Diff line number Diff line
@@ -95,8 +95,6 @@ class LockSettingsStorage {

    @VisibleForTesting
    public static class CredentialHash {
        /** Deprecated private static final int VERSION_LEGACY = 0; */
        private static final int VERSION_GATEKEEPER = 1;

        private CredentialHash(byte[] hash, @CredentialType int type) {
            if (type != LockPatternUtils.CREDENTIAL_TYPE_NONE) {
@@ -126,42 +124,6 @@ class LockSettingsStorage {

        byte[] hash;
        @CredentialType int type;

        public byte[] toBytes() {
            try {
                ByteArrayOutputStream os = new ByteArrayOutputStream();
                DataOutputStream dos = new DataOutputStream(os);
                dos.write(VERSION_GATEKEEPER);
                dos.write(type);
                if (hash != null && hash.length > 0) {
                    dos.writeInt(hash.length);
                    dos.write(hash);
                } else {
                    dos.writeInt(0);
                }
                dos.close();
                return os.toByteArray();
            } catch (IOException e) {
                throw new IllegalStateException("Fail to serialze credential hash", e);
            }
        }

        public static CredentialHash fromBytes(byte[] bytes) {
            try {
                DataInputStream is = new DataInputStream(new ByteArrayInputStream(bytes));
                /* int version = */ is.read();
                int type = is.read();
                int hashSize = is.readInt();
                byte[] hash = null;
                if (hashSize > 0) {
                    hash = new byte[hashSize];
                    is.readFully(hash);
                }
                return new CredentialHash(hash, type);
            } catch (IOException e) {
                throw new IllegalStateException("Fail to deserialze credential hash", e);
            }
        }
    }

    public LockSettingsStorage(Context context) {
+0 −29
Original line number Diff line number Diff line
@@ -434,35 +434,6 @@ public class LockSettingsStorageTests extends AndroidTestCase {
        assertEquals(2, PersistentData.TYPE_SP_WEAVER);
    }

    public void testCredentialHash_serializeUnserialize() {
        byte[] serialized = CredentialHash.create(
                PAYLOAD, LockPatternUtils.CREDENTIAL_TYPE_PASSWORD).toBytes();
        CredentialHash deserialized = CredentialHash.fromBytes(serialized);

        assertEquals(LockPatternUtils.CREDENTIAL_TYPE_PASSWORD, deserialized.type);
        assertArrayEquals(PAYLOAD, deserialized.hash);
    }

    public void testCredentialHash_unserialize_versionGatekeeper() {
        // This test ensures that we can read serialized VERSION_GATEKEEPER CredentialHashes
        // even if we change the wire format in the future.
        byte[] serialized = new byte[] {
                1, /* VERSION_GATEKEEPER */
                2, /* CREDENTIAL_TYPE_PASSWORD */
                0, 0, 0, 5, /* hash length */
                1, 2, -1, -2, 33, /* hash */
        };
        CredentialHash deserialized = CredentialHash.fromBytes(serialized);

        assertEquals(LockPatternUtils.CREDENTIAL_TYPE_PASSWORD, deserialized.type);
        assertArrayEquals(PAYLOAD, deserialized.hash);

        // Make sure the constants we use on the wire do not change.
        assertEquals(-1, LockPatternUtils.CREDENTIAL_TYPE_NONE);
        assertEquals(1, LockPatternUtils.CREDENTIAL_TYPE_PATTERN);
        assertEquals(2, LockPatternUtils.CREDENTIAL_TYPE_PASSWORD);
    }

    private static void assertArrayEquals(byte[] expected, byte[] actual) {
        if (!Arrays.equals(expected, actual)) {
            fail("expected:<" + Arrays.toString(expected) +