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

Commit e5508107 authored by weichinweng's avatar weichinweng
Browse files

NIAP: Add a map to store the LTTKM

When do pairing, the set_encrypt_key_or_remove_key will be later than
get_key due to they are in different thread. Use a map to store them and
will check whether the key exist in map. If no, will get the key from
the JAVA bluetoothKeystore.

Bug: 148758680
Test: m
Change-Id: I044b11a231c6f65978019ce6092657c1bdd814d1
Merged-In: I044b11a231c6f65978019ce6092657c1bdd814d1
parent 669529d6
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -384,6 +384,7 @@ static future_t* clean_up(void) {
  config_timer = NULL;

  std::unique_lock<std::recursive_mutex> lock(config_lock);
  get_bluetooth_keystore_interface()->clear_map();
  MetricIdAllocator::GetInstance().Close();
  config.reset();
  return future_new_immediate(FUTURE_SUCCESS);
+28 −4
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@
#include <base/location.h>
#include <base/logging.h>
#include <hardware/bluetooth.h>
#include <map>

using base::Bind;
using base::Unretained;
@@ -40,19 +41,22 @@ class BluetoothKeystoreInterfaceImpl
  ~BluetoothKeystoreInterfaceImpl() override = default;

  void init(BluetoothKeystoreCallbacks* callbacks) override {
    DVLOG(2) << __func__;
    VLOG(2) << __func__;
    this->callbacks = callbacks;
  }

  void set_encrypt_key_or_remove_key(std::string prefix,
                                     std::string decryptedString) override {
    DVLOG(2) << __func__ << " prefix: " << prefix;
    VLOG(2) << __func__ << " prefix: " << prefix;

    if (!callbacks) {
      LOG(WARNING) << __func__ << " callback isn't ready. prefix: " << prefix;
      return;
    }

    // Save the value into a map.
    key_map[prefix] = decryptedString;

    do_in_jni_thread(
        base::Bind(&bluetooth::bluetooth_keystore::BluetoothKeystoreCallbacks::
                       set_encrypt_key_or_remove_key,
@@ -60,18 +64,38 @@ class BluetoothKeystoreInterfaceImpl
  }

  std::string get_key(std::string prefix) override {
    DVLOG(2) << __func__ << " prefix: " << prefix;
    VLOG(2) << __func__ << " prefix: " << prefix;

    if (!callbacks) {
      LOG(WARNING) << __func__ << " callback isn't ready. prefix: " << prefix;
      return "";
    }

    return callbacks->get_key(prefix);
    std::string decryptedString;
    // try to find the key.
    std::map<std::string, std::string>::iterator iter = key_map.find(prefix);
    if (iter == key_map.end()) {
      decryptedString = callbacks->get_key(prefix);
      // Save the value into a map.
      key_map[prefix] = decryptedString;
      VLOG(2) << __func__ << ": get key from bluetoothkeystore.";
    } else {
      decryptedString = iter->second;
    }
    return decryptedString;
  }

  void clear_map() override {
    VLOG(2) << __func__;

    std::map<std::string, std::string> empty_map;
    key_map.swap(empty_map);
    key_map.clear();
  }

 private:
  BluetoothKeystoreCallbacks* callbacks = nullptr;
  std::map<std::string, std::string> key_map;
};

BluetoothKeystoreInterface* getBluetoothKeystoreInterface() {
+3 −0
Original line number Diff line number Diff line
@@ -42,6 +42,9 @@ class BluetoothKeystoreInterface {

  /** Interface for get key. */
  virtual std::string get_key(std::string prefix) = 0;

  /** Interface for clear map. */
  virtual void clear_map() = 0;
};

}  // namespace bluetooth_keystore