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

Commit f2f7ead3 authored by William Escande's avatar William Escande Committed by Gerrit Code Review
Browse files

Merge "KeystoreService: pass nativeInterface as parameter" into main

parents aa4fa212 a107ef5c
Loading
Loading
Loading
Loading
+4 −1
Original line number Diff line number Diff line
@@ -122,6 +122,7 @@ import com.android.bluetooth.bass_client.BassClientService;
import com.android.bluetooth.btservice.InteropUtil.InteropFeature;
import com.android.bluetooth.btservice.RemoteDevices.DeviceProperties;
import com.android.bluetooth.btservice.activityattribution.ActivityAttributionService;
import com.android.bluetooth.btservice.bluetoothkeystore.BluetoothKeystoreNativeInterface;
import com.android.bluetooth.btservice.bluetoothkeystore.BluetoothKeystoreService;
import com.android.bluetooth.btservice.storage.DatabaseManager;
import com.android.bluetooth.btservice.storage.MetadataDatabase;
@@ -639,7 +640,9 @@ public class AdapterService extends Service {
        mAdapterProperties = new AdapterProperties(this);
        mAdapterStateMachine = new AdapterState(this, mLooper);
        mJniCallbacks = new JniCallbacks(this, mAdapterProperties);
        mBluetoothKeystoreService = new BluetoothKeystoreService(isCommonCriteriaMode());
        mBluetoothKeystoreService =
                new BluetoothKeystoreService(
                        new BluetoothKeystoreNativeInterface(), isCommonCriteriaMode());
        mBluetoothKeystoreService.start();
        int configCompareResult = mBluetoothKeystoreService.getCompareResult();

+32 −42
Original line number Diff line number Diff line
@@ -18,44 +18,27 @@ package com.android.bluetooth.btservice.bluetoothkeystore;

import android.util.Log;

import com.android.internal.annotations.GuardedBy;

import java.io.IOException;
import java.security.NoSuchAlgorithmException;

final class BluetoothKeystoreNativeInterface {
/** Native interface to be used by BluetoothKeystoreService */
public class BluetoothKeystoreNativeInterface {
    private static final String TAG = BluetoothKeystoreNativeInterface.class.getSimpleName();

    private static final String TAG = "BluetoothKeystoreNativeInterface";

    @GuardedBy("INSTANCE_LOCK")
    private static BluetoothKeystoreNativeInterface sInstance;
    private static final Object INSTANCE_LOCK = new Object();
    private BluetoothKeystoreService mBluetoothKeystoreService = null;

    static {
        classInitNative();
    }

    private BluetoothKeystoreNativeInterface() {
    }

    /**
     * Get singleton instance.
     */
    public static BluetoothKeystoreNativeInterface getInstance() {
        synchronized (INSTANCE_LOCK) {
            if (sInstance == null) {
                sInstance = new BluetoothKeystoreNativeInterface();
            }
            return sInstance;
        }
    }

    /**
     * Initializes the native interface.
     *
     * priorities to configure.
     * <p>priorities to configure.
     */
    public void init() {
    public void init(BluetoothKeystoreService service) {
        mBluetoothKeystoreService = service;
        initNative();
    }

@@ -64,6 +47,7 @@ final class BluetoothKeystoreNativeInterface {
     */
    public void cleanup() {
        cleanupNative();
        mBluetoothKeystoreService = null;
    }

    // Callbacks from the native stack back into the Java framework.
@@ -71,8 +55,16 @@ final class BluetoothKeystoreNativeInterface {
    // state machine the message should be routed to.

    private void setEncryptKeyOrRemoveKeyCallback(String prefixString, String decryptedString) {
        BluetoothKeystoreService service = BluetoothKeystoreService.getBluetoothKeystoreService();
        if (service != null) {
        final BluetoothKeystoreService service = mBluetoothKeystoreService;

        if (service == null) {
            Log.e(
                    TAG,
                    "setEncryptKeyOrRemoveKeyCallback: Event ignored, service not available: "
                            + prefixString);
            return;
        }

        try {
            service.setEncryptKeyOrRemoveKey(prefixString, decryptedString);
        } catch (InterruptedException e) {
@@ -82,19 +74,17 @@ final class BluetoothKeystoreNativeInterface {
        } catch (NoSuchAlgorithmException e) {
            Log.e(TAG, "encrypt could not find the algorithm: SHA256");
        }
        } else {
            Log.e(TAG, "Event ignored, service not available: " + prefixString);
        }
    }

    private String getKeyCallback(String prefixString) {
        BluetoothKeystoreService service = BluetoothKeystoreService.getBluetoothKeystoreService();
        if (service != null) {
            return service.getKey(prefixString);
        } else {
            Log.e(TAG, "Event ignored, service not available: " + prefixString);
        final BluetoothKeystoreService service = mBluetoothKeystoreService;

        if (service == null) {
            Log.e(TAG, "getKeyCallback: Event ignored, service not available: " + prefixString);
            return null;
        }

        return service.getKey(prefixString);
    }

    // Native methods that call into the JNI interface
+5 −38
Original line number Diff line number Diff line
@@ -46,7 +46,6 @@ import java.util.Base64;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

@@ -99,7 +98,7 @@ public class BluetoothKeystoreService {
    private static final int CONFIG_BACKUP_COMPARE_PASS = 0b10;
    private int mCompareResult;

    BluetoothKeystoreNativeInterface mBluetoothKeystoreNativeInterface;
    private final BluetoothKeystoreNativeInterface mBluetoothKeystoreNativeInterface;

    private ComputeDataThread mEncryptDataThread;
    private ComputeDataThread mDecryptDataThread;
@@ -113,8 +112,10 @@ public class BluetoothKeystoreService {
    private Base64.Decoder mDecoder = Base64.getDecoder();
    private Base64.Encoder mEncoder = Base64.getEncoder();

    public BluetoothKeystoreService(boolean isCommonCriteriaMode) {
    public BluetoothKeystoreService(
            BluetoothKeystoreNativeInterface nativeInterface, boolean isCommonCriteriaMode) {
        debugLog("new BluetoothKeystoreService isCommonCriteriaMode: " + isCommonCriteriaMode);
        mBluetoothKeystoreNativeInterface = nativeInterface;
        mIsCommonCriteriaMode = isCommonCriteriaMode;
        mCompareResult = CONFIG_COMPARE_INIT;
        startThread();
@@ -140,13 +141,6 @@ public class BluetoothKeystoreService {
            return;
        }

        mBluetoothKeystoreNativeInterface = Objects.requireNonNull(
                BluetoothKeystoreNativeInterface.getInstance(),
                "BluetoothKeystoreNativeInterface cannot be null when BluetoothKeystore starts");

        // Mark service as started
        setBluetoothKeystoreService(this);

        try {
            if (!keyStore.containsAlias(KEYALIAS) && mIsCommonCriteriaMode) {
                infoLog("Enable Common Criteria mode for the first time, pass hash check.");
@@ -187,12 +181,9 @@ public class BluetoothKeystoreService {
            debugLog("cleanup() called before start()");
            return;
        }
        // Mark service as stopped
        setBluetoothKeystoreService(null);

        // Cleanup native interface
        mBluetoothKeystoreNativeInterface.cleanup();
        mBluetoothKeystoreNativeInterface = null;

        if (mIsCommonCriteriaMode) {
            cleanupForCommonCriteriaModeEnable();
@@ -296,37 +287,13 @@ public class BluetoothKeystoreService {
        stopThread();
        startThread();
        // Initialize native interface
        if (mBluetoothKeystoreNativeInterface != null) {
            mBluetoothKeystoreNativeInterface.init();
        }
        mBluetoothKeystoreNativeInterface.init(this);
    }

    private boolean isAvailable() {
        return !mCleaningUp;
    }

    /**
     * Get the BluetoothKeystoreService instance
     */
    public static synchronized BluetoothKeystoreService getBluetoothKeystoreService() {
        if (sBluetoothKeystoreService == null) {
            debugLog("getBluetoothKeystoreService(): service is NULL");
            return null;
        }

        if (!sBluetoothKeystoreService.isAvailable()) {
            debugLog("getBluetoothKeystoreService(): service is not available");
            return null;
        }
        return sBluetoothKeystoreService;
    }

    private static synchronized void setBluetoothKeystoreService(
            BluetoothKeystoreService instance) {
        debugLog("setBluetoothKeystoreService(): set to: " + instance);
        sBluetoothKeystoreService = instance;
    }

    /**
     * Gets result of the checksum comparison
     */
+17 −12
Original line number Diff line number Diff line
@@ -20,15 +20,6 @@ import android.os.Binder;
import android.os.Process;
import android.util.Log;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.Assert;
import org.junit.Assume;
@@ -36,12 +27,25 @@ import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RunWith(JUnit4.class)
public final class BluetoothKeystoreServiceTest {
    private static final String TAG = "BluetoothKeystoreServiceTest";
    private BluetoothKeystoreService mBluetoothKeystoreService;

    @Mock private BluetoothKeystoreNativeInterface mMockNativeInterface;

    // Please also check bt_stack string configuration if you want to change the content.
    private static final String CONFIG_FILE_PREFIX = "bt_config-origin";
    private static final String CONFIG_BACKUP_PREFIX = "bt_config-backup";
@@ -119,8 +123,9 @@ public final class BluetoothKeystoreServiceTest {

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
        Assume.assumeTrue("Ignore test when the user is not primary.", isPrimaryUser());
        mBluetoothKeystoreService = new BluetoothKeystoreService(true);
        mBluetoothKeystoreService = new BluetoothKeystoreService(mMockNativeInterface, true);
        Assert.assertNotNull(mBluetoothKeystoreService);
        // backup origin config data.
        try {
@@ -280,7 +285,7 @@ public final class BluetoothKeystoreServiceTest {
        mBluetoothKeystoreService.cleanupForCommonCriteriaModeEnable();

        // new mBluetoothKeystoreService and the Common Criteria mode is false.
        mBluetoothKeystoreService = new BluetoothKeystoreService(false);
        mBluetoothKeystoreService = new BluetoothKeystoreService(mMockNativeInterface, false);
        Assert.assertNotNull(mBluetoothKeystoreService);

        mBluetoothKeystoreService.loadConfigData();
@@ -304,7 +309,7 @@ public final class BluetoothKeystoreServiceTest {
        mBluetoothKeystoreService.cleanupForCommonCriteriaModeDisable();

        // new mBluetoothKeystoreService and the Common Criteria mode is true.
        mBluetoothKeystoreService = new BluetoothKeystoreService(true);
        mBluetoothKeystoreService = new BluetoothKeystoreService(mMockNativeInterface, true);
        mBluetoothKeystoreService.loadConfigData();

        Assert.assertTrue(mBluetoothKeystoreService.getCompareResult() == 0);