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

Commit ef23226e authored by Gabriel Biren's avatar Gabriel Biren
Browse files

Add removeAll logic for legacy keystore when

WifiBlobStore is not supported.

Bug: 365543479
Flag: android.net.wifi.flags.wifi_keystore_remove_all_api
Test: atest WifiKeystoreTest
Test: Manual test - add several certificates to legacy
      keystore and verify that they are cleared by
      calling removeAll
Change-Id: Ie8dc210b452729fe450dacd9ad4cc2dd60af7f57
parent b2f78088
Loading
Loading
Loading
Loading
+28 −2
Original line number Diff line number Diff line
@@ -172,6 +172,26 @@ public final class WifiKeystore {
        return new String[0];
    }

    /**
     * Remove all blobs that are stored in Legacy Keystore.
     *
     * @return True if the operation was successful, false otherwise.
     */
    private static boolean removeAllFromLegacyKs() {
        // Assume that the calling identity has already been cleared.
        try {
            ILegacyKeystore legacyKeystore = WifiBlobStore.getLegacyKeystore();
            String[] legacyAliases = legacyKeystore.list("", Process.WIFI_UID);
            for (String alias : legacyAliases) {
                legacyKeystore.remove(alias, Process.WIFI_UID);
            }
            return true;
        } catch (Exception e) {
            Log.e(TAG, "Failed to remove all blobs from Legacy Keystore: " + e);
            return false;
        }
    }

    /**
     * Remove all blobs that are stored in the database.
     *
@@ -183,10 +203,16 @@ public final class WifiKeystore {
    @SystemApi
    @FlaggedApi(Flags.FLAG_WIFI_KEYSTORE_REMOVE_ALL_API)
    public static boolean removeAll() {
        Log.i(TAG, "Removing all blobs from the database");
        Log.i(TAG, "Removing all blobs from " + sPrimaryDbName);
        final long identity = Binder.clearCallingIdentity();
        try {
            // If supplicant can access WifiBlobStore, then all certificates will be in that
            // database. Otherwise, all certificates will be in Legacy Keystore.
            if (WifiBlobStore.supplicantCanAccessBlobstore()) {
                return WifiBlobStore.getInstance().removeAll();
            } else {
                return removeAllFromLegacyKs();
            }
        } catch (Exception e) {
            Log.e(TAG, "Failed to remove all blobs: " + e);
            return false;
+18 −2
Original line number Diff line number Diff line
@@ -194,13 +194,29 @@ public class WifiKeystoreTest {
    }

    /**
     * Test that removeAll only affects the WifiBlobStore database.
     * Test that removeAll only affects WifiBlobStore if that database is supported.
     */
    @Test
    public void testRemoveAll() throws Exception {
    public void testRemoveAll_wifiBlobStore() throws Exception {
        when(WifiBlobStore.supplicantCanAccessBlobstore()).thenReturn(true);
        when(mWifiBlobStore.removeAll()).thenReturn(true);
        assertTrue(WifiKeystore.removeAll());
        verify(mWifiBlobStore, times(1)).removeAll();
        verifyNoInteractions(mLegacyKeystore);
    }

    /**
     * Test that removeAll only affects Legacy Keystore if WifiBlobStore is not supported.
     */
    @Test
    public void testRemoveAll_legacyKeystore() throws Exception {
        String[] legacyAliases = {"alias1", "alias2", "alias3"};
        when(WifiBlobStore.supplicantCanAccessBlobstore()).thenReturn(false);
        when(mLegacyKeystore.list(anyString(), anyInt())).thenReturn(legacyAliases);
        assertTrue(WifiKeystore.removeAll());

        verify(mLegacyKeystore, times(1)).list(anyString(), anyInt());
        verify(mLegacyKeystore, times(legacyAliases.length)).remove(anyString(), anyInt());
        verifyNoInteractions(mWifiBlobStore);
    }
}