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 #include "btif_storage.h"
22 
23 #include <base/bind.h>
24 #include <base/location.h>
25 #include <base/logging.h>
26 #include <hardware/bluetooth.h>
27 #include <map>
28 
29 using base::Bind;
30 using base::Unretained;
31 using bluetooth::bluetooth_keystore::BluetoothKeystoreCallbacks;
32 using bluetooth::bluetooth_keystore::BluetoothKeystoreInterface;
33 
34 namespace bluetooth {
35 namespace bluetooth_keystore {
36 class BluetoothKeystoreInterfaceImpl;
37 std::unique_ptr<BluetoothKeystoreInterface> bluetoothKeystoreInstance;
38 
39 class BluetoothKeystoreInterfaceImpl
40     : public bluetooth::bluetooth_keystore::BluetoothKeystoreInterface,
41       public bluetooth::bluetooth_keystore::BluetoothKeystoreCallbacks {
42   ~BluetoothKeystoreInterfaceImpl() override = default;
43 
init(BluetoothKeystoreCallbacks * callbacks)44   void init(BluetoothKeystoreCallbacks* callbacks) override {
45     VLOG(2) << __func__;
46     this->callbacks = callbacks;
47     // Get bonded devices number to get all bonded devices key.
48     do_in_jni_thread(
49         FROM_HERE, base::Bind([]() { btif_storage_get_num_bonded_devices(); }));
50   }
51 
set_encrypt_key_or_remove_key(std::string prefix,std::string decryptedString)52   void set_encrypt_key_or_remove_key(std::string prefix,
53                                      std::string decryptedString) override {
54     VLOG(2) << __func__ << " prefix: " << prefix;
55 
56     if (!callbacks) {
57       LOG(WARNING) << __func__ << " callback isn't ready. prefix: " << prefix;
58       return;
59     }
60 
61     // Save the value into a map.
62     key_map[prefix] = decryptedString;
63 
64     do_in_jni_thread(
65         base::Bind(&bluetooth::bluetooth_keystore::BluetoothKeystoreCallbacks::
66                        set_encrypt_key_or_remove_key,
67                    base::Unretained(callbacks), prefix, decryptedString));
68   }
69 
get_key(std::string prefix)70   std::string get_key(std::string prefix) override {
71     VLOG(2) << __func__ << " prefix: " << prefix;
72 
73     if (!callbacks) {
74       LOG(WARNING) << __func__ << " callback isn't ready. prefix: " << prefix;
75       return "";
76     }
77 
78     std::string decryptedString;
79     // try to find the key.
80     std::map<std::string, std::string>::iterator iter = key_map.find(prefix);
81     if (iter == key_map.end()) {
82       decryptedString = callbacks->get_key(prefix);
83       // Save the value into a map.
84       key_map[prefix] = decryptedString;
85       VLOG(2) << __func__ << ": get key from bluetoothkeystore.";
86     } else {
87       decryptedString = iter->second;
88     }
89     return decryptedString;
90   }
91 
clear_map()92   void clear_map() override {
93     VLOG(2) << __func__;
94 
95     std::map<std::string, std::string> empty_map;
96     key_map.swap(empty_map);
97     key_map.clear();
98   }
99 
100  private:
101   BluetoothKeystoreCallbacks* callbacks = nullptr;
102   std::map<std::string, std::string> key_map;
103 };
104 
getBluetoothKeystoreInterface()105 BluetoothKeystoreInterface* getBluetoothKeystoreInterface() {
106   if (!bluetoothKeystoreInstance) {
107     bluetoothKeystoreInstance.reset(new BluetoothKeystoreInterfaceImpl());
108   }
109 
110   return bluetoothKeystoreInstance.get();
111 }
112 
113 }  // namespace bluetooth_keystore
114 }  // namespace bluetooth
115