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_keystore.h"
20 
21 #include <base/functional/bind.h>
22 #include <base/location.h>
23 #include <bluetooth/log.h>
24 #include <hardware/bluetooth.h>
25 
26 #include <map>
27 
28 #include "btif_common.h"
29 #include "btif_storage.h"
30 #include "main/shim/config.h"
31 #include "main/shim/shim.h"
32 #include "os/parameter_provider.h"
33 
34 using base::Bind;
35 using base::Unretained;
36 using bluetooth::bluetooth_keystore::BluetoothKeystoreCallbacks;
37 using bluetooth::bluetooth_keystore::BluetoothKeystoreInterface;
38 
39 namespace bluetooth {
40 namespace bluetooth_keystore {
41 class BluetoothKeystoreInterfaceImpl;
42 std::unique_ptr<BluetoothKeystoreInterface> bluetoothKeystoreInstance;
43 const int CONFIG_COMPARE_ALL_PASS = 0b11;
44 
45 class BluetoothKeystoreInterfaceImpl
46     : public bluetooth::bluetooth_keystore::BluetoothKeystoreInterface {
47   ~BluetoothKeystoreInterfaceImpl() override = default;
48 
init(BluetoothKeystoreCallbacks * callbacks)49   void init(BluetoothKeystoreCallbacks* callbacks) override {
50     log::verbose("");
51     this->callbacks = callbacks;
52 
53     bluetooth::os::ParameterProvider::SetCommonCriteriaConfigCompareResult(
54         CONFIG_COMPARE_ALL_PASS);
55     ConvertEncryptOrDecryptKeyIfNeeded();
56   }
57 
ConvertEncryptOrDecryptKeyIfNeeded()58   void ConvertEncryptOrDecryptKeyIfNeeded() {
59     log::verbose("");
60     if (!callbacks) {
61       log::info("callback isn't ready.");
62       return;
63     }
64     do_in_jni_thread(base::BindOnce([]() {
65       shim::BtifConfigInterface::ConvertEncryptOrDecryptKeyIfNeeded();
66     }));
67   }
68 
set_encrypt_key_or_remove_key(std::string prefix,std::string decryptedString)69   bool set_encrypt_key_or_remove_key(std::string prefix,
70                                      std::string decryptedString) override {
71     log::verbose("prefix: {}", prefix);
72 
73     if (!callbacks) {
74       log::warn("callback isn't ready. prefix: {}", prefix);
75       return false;
76     }
77 
78     // Save the value into a map.
79     key_map[prefix] = decryptedString;
80 
81     do_in_jni_thread(base::BindOnce(
82         &bluetooth::bluetooth_keystore::BluetoothKeystoreCallbacks::
83             set_encrypt_key_or_remove_key,
84         base::Unretained(callbacks), prefix, decryptedString));
85     return true;
86   }
87 
get_key(std::string prefix)88   std::string get_key(std::string prefix) override {
89     log::verbose("prefix: {}", prefix);
90 
91     if (!callbacks) {
92       log::warn("callback isn't ready. prefix: {}", prefix);
93       return "";
94     }
95 
96     std::string decryptedString;
97     // try to find the key.
98     std::map<std::string, std::string>::iterator iter = key_map.find(prefix);
99     if (iter == key_map.end()) {
100       decryptedString = callbacks->get_key(prefix);
101       // Save the value into a map.
102       key_map[prefix] = decryptedString;
103       log::verbose("get key from bluetoothkeystore.");
104     } else {
105       decryptedString = iter->second;
106     }
107     return decryptedString;
108   }
109 
clear_map()110   void clear_map() override {
111     log::verbose("");
112 
113     std::map<std::string, std::string> empty_map;
114     key_map.swap(empty_map);
115     key_map.clear();
116   }
117 
118  private:
119   BluetoothKeystoreCallbacks* callbacks = nullptr;
120   std::map<std::string, std::string> key_map;
121 };
122 
getBluetoothKeystoreInterface()123 BluetoothKeystoreInterface* getBluetoothKeystoreInterface() {
124   if (!bluetoothKeystoreInstance) {
125     bluetoothKeystoreInstance.reset(new BluetoothKeystoreInterfaceImpl());
126   }
127 
128   return bluetoothKeystoreInstance.get();
129 }
130 
131 }  // namespace bluetooth_keystore
132 }  // namespace bluetooth
133