1 /*
2 * Copyright 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 /* BluetoothKeystore Interface */
18
19 #include <btif_common.h>
20 #include <btif_keystore.h>
21
22 #include <base/bind.h>
23 #include <base/location.h>
24 #include <base/logging.h>
25 #include <hardware/bluetooth.h>
26 #include <map>
27
28 using base::Bind;
29 using base::Unretained;
30 using bluetooth::bluetooth_keystore::BluetoothKeystoreCallbacks;
31 using bluetooth::bluetooth_keystore::BluetoothKeystoreInterface;
32
33 namespace bluetooth {
34 namespace bluetooth_keystore {
35 class BluetoothKeystoreInterfaceImpl;
36 std::unique_ptr<BluetoothKeystoreInterface> bluetoothKeystoreInstance;
37
38 class BluetoothKeystoreInterfaceImpl
39 : public bluetooth::bluetooth_keystore::BluetoothKeystoreInterface,
40 public bluetooth::bluetooth_keystore::BluetoothKeystoreCallbacks {
41 ~BluetoothKeystoreInterfaceImpl() override = default;
42
init(BluetoothKeystoreCallbacks * callbacks)43 void init(BluetoothKeystoreCallbacks* callbacks) override {
44 VLOG(2) << __func__;
45 this->callbacks = callbacks;
46 }
47
set_encrypt_key_or_remove_key(std::string prefix,std::string decryptedString)48 void set_encrypt_key_or_remove_key(std::string prefix,
49 std::string decryptedString) override {
50 VLOG(2) << __func__ << " prefix: " << prefix;
51
52 if (!callbacks) {
53 LOG(WARNING) << __func__ << " callback isn't ready. prefix: " << prefix;
54 return;
55 }
56
57 // Save the value into a map.
58 key_map[prefix] = decryptedString;
59
60 do_in_jni_thread(
61 base::Bind(&bluetooth::bluetooth_keystore::BluetoothKeystoreCallbacks::
62 set_encrypt_key_or_remove_key,
63 base::Unretained(callbacks), prefix, decryptedString));
64 }
65
get_key(std::string prefix)66 std::string get_key(std::string prefix) override {
67 VLOG(2) << __func__ << " prefix: " << prefix;
68
69 if (!callbacks) {
70 LOG(WARNING) << __func__ << " callback isn't ready. prefix: " << prefix;
71 return "";
72 }
73
74 std::string decryptedString;
75 // try to find the key.
76 std::map<std::string, std::string>::iterator iter = key_map.find(prefix);
77 if (iter == key_map.end()) {
78 decryptedString = callbacks->get_key(prefix);
79 // Save the value into a map.
80 key_map[prefix] = decryptedString;
81 VLOG(2) << __func__ << ": get key from bluetoothkeystore.";
82 } else {
83 decryptedString = iter->second;
84 }
85 return decryptedString;
86 }
87
clear_map()88 void clear_map() override {
89 VLOG(2) << __func__;
90
91 std::map<std::string, std::string> empty_map;
92 key_map.swap(empty_map);
93 key_map.clear();
94 }
95
96 private:
97 BluetoothKeystoreCallbacks* callbacks = nullptr;
98 std::map<std::string, std::string> key_map;
99 };
100
getBluetoothKeystoreInterface()101 BluetoothKeystoreInterface* getBluetoothKeystoreInterface() {
102 if (!bluetoothKeystoreInstance) {
103 bluetoothKeystoreInstance.reset(new BluetoothKeystoreInterfaceImpl());
104 }
105
106 return bluetoothKeystoreInstance.get();
107 }
108
109 } // namespace bluetooth_keystore
110 } // namespace bluetooth
111