OSDN Git Service

NIAP: implement bluetooth keystore interface.(2/2)
authorweichinweng <weichinweng@google.com>
Tue, 21 Apr 2020 01:42:27 +0000 (09:42 +0800)
committerweichinweng <weichinweng@google.com>
Tue, 19 May 2020 01:12:18 +0000 (09:12 +0800)
Bug: 148758680
Test: m

Change-Id: I89f782039123d1deac173d13670de4d9dae5252a
Merged-In: I89f782039123d1deac173d13670de4d9dae5252a

btif/Android.bp
btif/include/btif_keystore.h [new file with mode: 0644]
btif/src/bluetooth.cc
btif/src/btif_keystore.cc [new file with mode: 0644]
include/hardware/bluetooth.h
include/hardware/bt_keystore.h [new file with mode: 0644]

index 4316951..8d510ef 100644 (file)
@@ -87,6 +87,7 @@ cc_library_static {
         "src/btif_storage.cc",
         "src/btif_uid.cc",
         "src/btif_util.cc",
+        "src/btif_keystore.cc",
         "src/stack_manager.cc",
     ],
     header_libs: [
diff --git a/btif/include/btif_keystore.h b/btif/include/btif_keystore.h
new file mode 100644 (file)
index 0000000..460f3b8
--- /dev/null
@@ -0,0 +1,25 @@
+/*
+ * Copyright 2020 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <hardware/bt_keystore.h>
+
+namespace bluetooth {
+namespace bluetooth_keystore {
+
+BluetoothKeystoreInterface* getBluetoothKeystoreInterface();
+
+}  // namespace bluetooth_keystore
+}  // namespace bluetooth
\ No newline at end of file
index 3e305a2..0187b1d 100644 (file)
@@ -59,6 +59,7 @@
 #include "btif_debug_btsnoop.h"
 #include "btif_debug_conn.h"
 #include "btif_hf.h"
+#include "btif_keystore.h"
 #include "btif_storage.h"
 #include "btsnoop.h"
 #include "btsnoop_mem.h"
@@ -398,6 +399,9 @@ static const void* get_profile_interface(const char* profile_id) {
 
   if (is_profile(profile_id, BT_PROFILE_HEARING_AID_ID))
     return btif_hearing_aid_get_interface();
+
+  if (is_profile(profile_id, BT_KEYSTORE_ID))
+    return bluetooth::bluetooth_keystore::getBluetoothKeystoreInterface();
   return NULL;
 }
 
diff --git a/btif/src/btif_keystore.cc b/btif/src/btif_keystore.cc
new file mode 100644 (file)
index 0000000..c605448
--- /dev/null
@@ -0,0 +1,86 @@
+/*
+ * Copyright 2020 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/* BluetoothKeystore Interface */
+
+#include <btif_common.h>
+#include <btif_keystore.h>
+
+#include <base/bind.h>
+#include <base/location.h>
+#include <base/logging.h>
+#include <hardware/bluetooth.h>
+
+using base::Bind;
+using base::Unretained;
+using bluetooth::bluetooth_keystore::BluetoothKeystoreCallbacks;
+using bluetooth::bluetooth_keystore::BluetoothKeystoreInterface;
+
+namespace bluetooth {
+namespace bluetooth_keystore {
+class BluetoothKeystoreInterfaceImpl;
+std::unique_ptr<BluetoothKeystoreInterface> bluetoothKeystoreInstance;
+
+class BluetoothKeystoreInterfaceImpl
+    : public bluetooth::bluetooth_keystore::BluetoothKeystoreInterface,
+      public bluetooth::bluetooth_keystore::BluetoothKeystoreCallbacks {
+  ~BluetoothKeystoreInterfaceImpl() override = default;
+
+  void init(BluetoothKeystoreCallbacks* callbacks) override {
+    DVLOG(2) << __func__;
+    this->callbacks = callbacks;
+  }
+
+  void set_encrypt_key_or_remove_key(std::string prefix,
+                                     std::string decryptedString) override {
+    DVLOG(2) << __func__ << " prefix: " << prefix;
+
+    if (!callbacks) {
+      LOG(WARNING) << __func__ << " callback isn't ready. prefix: " << prefix;
+      return;
+    }
+
+    do_in_jni_thread(
+        base::Bind(&bluetooth::bluetooth_keystore::BluetoothKeystoreCallbacks::
+                       set_encrypt_key_or_remove_key,
+                   base::Unretained(callbacks), prefix, decryptedString));
+  }
+
+  std::string get_key(std::string prefix) override {
+    DVLOG(2) << __func__ << " prefix: " << prefix;
+
+    if (!callbacks) {
+      LOG(WARNING) << __func__ << " callback isn't ready. prefix: " << prefix;
+      return "";
+    }
+
+    return callbacks->get_key(prefix);
+  }
+
+ private:
+  BluetoothKeystoreCallbacks* callbacks = nullptr;
+};
+
+BluetoothKeystoreInterface* getBluetoothKeystoreInterface() {
+  if (!bluetoothKeystoreInstance) {
+    bluetoothKeystoreInstance.reset(new BluetoothKeystoreInterfaceImpl());
+  }
+
+  return bluetoothKeystoreInstance.get();
+}
+
+}  // namespace bluetooth_keystore
+}  // namespace bluetooth
index 0c53976..8ac32b2 100644 (file)
@@ -48,6 +48,7 @@
 #define BT_PROFILE_AV_RC_ID "avrcp"
 #define BT_PROFILE_AV_RC_CTRL_ID "avrcp_ctrl"
 #define BT_PROFILE_HEARING_AID_ID "hearing_aid"
+#define BT_KEYSTORE_ID "bluetooth_keystore"
 
 /** Bluetooth Device Name */
 typedef struct { uint8_t name[249]; } __attribute__((packed)) bt_bdname_t;
diff --git a/include/hardware/bt_keystore.h b/include/hardware/bt_keystore.h
new file mode 100644 (file)
index 0000000..45cbb3c
--- /dev/null
@@ -0,0 +1,48 @@
+/*
+ * Copyright 2020 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+namespace bluetooth {
+namespace bluetooth_keystore {
+
+class BluetoothKeystoreCallbacks {
+ public:
+  virtual ~BluetoothKeystoreCallbacks() = default;
+
+  /** Callback for key encrypt or remove key */
+  virtual void set_encrypt_key_or_remove_key(std::string prefix,
+                                             std::string encryptedString) = 0;
+
+  /** Callback for get key. */
+  virtual std::string get_key(std::string prefix) = 0;
+};
+
+class BluetoothKeystoreInterface {
+ public:
+  virtual ~BluetoothKeystoreInterface() = default;
+
+  /** Register the bluetooth keystore callbacks */
+  virtual void init(BluetoothKeystoreCallbacks* callbacks) = 0;
+
+  /** Interface for key encrypt or remove key */
+  virtual void set_encrypt_key_or_remove_key(std::string prefix,
+                                             std::string encryptedString) = 0;
+
+  /** Interface for get key. */
+  virtual std::string get_key(std::string prefix) = 0;
+};
+
+}  // namespace bluetooth_keystore
+}  // namespace bluetooth