• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <base/logging.h>
24 #include <hardware/bluetooth.h>
25 
26 #include <map>
27 
28 #include "btif_common.h"
29 #include "btif_storage.h"
30 #include "gd/os/parameter_provider.h"
31 #include "main/shim/config.h"
32 #include "main/shim/shim.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     VLOG(2) << __func__;
51     this->callbacks = callbacks;
52 
53     bluetooth::os::ParameterProvider::SetCommonCriteriaConfigCompareResult(
54         CONFIG_COMPARE_ALL_PASS);
55     ConvertEncryptOrDecryptKeyIfNeeded();
56   }
57 
ConvertEncryptOrDecryptKeyIfNeeded()58   void ConvertEncryptOrDecryptKeyIfNeeded() {
59     VLOG(2) << __func__;
60     if (!callbacks) {
61       LOG(INFO) << __func__ << " callback isn't ready.";
62       return;
63     }
64     do_in_jni_thread(
65         FROM_HERE, base::Bind([]() {
66           shim::BtifConfigInterface::ConvertEncryptOrDecryptKeyIfNeeded();
67         }));
68   }
69 
set_encrypt_key_or_remove_key(std::string prefix,std::string decryptedString)70   bool set_encrypt_key_or_remove_key(std::string prefix,
71                                      std::string decryptedString) override {
72     VLOG(2) << __func__ << " prefix: " << prefix;
73 
74     if (!callbacks) {
75       LOG(WARNING) << __func__ << " callback isn't ready. prefix: " << prefix;
76       return false;
77     }
78 
79     // Save the value into a map.
80     key_map[prefix] = decryptedString;
81 
82     do_in_jni_thread(
83         base::Bind(&bluetooth::bluetooth_keystore::BluetoothKeystoreCallbacks::
84                        set_encrypt_key_or_remove_key,
85                    base::Unretained(callbacks), prefix, decryptedString));
86     return true;
87   }
88 
get_key(std::string prefix)89   std::string get_key(std::string prefix) override {
90     VLOG(2) << __func__ << " prefix: " << prefix;
91 
92     if (!callbacks) {
93       LOG(WARNING) << __func__ << " callback isn't ready. prefix: " << prefix;
94       return "";
95     }
96 
97     std::string decryptedString;
98     // try to find the key.
99     std::map<std::string, std::string>::iterator iter = key_map.find(prefix);
100     if (iter == key_map.end()) {
101       decryptedString = callbacks->get_key(prefix);
102       // Save the value into a map.
103       key_map[prefix] = decryptedString;
104       VLOG(2) << __func__ << ": get key from bluetoothkeystore.";
105     } else {
106       decryptedString = iter->second;
107     }
108     return decryptedString;
109   }
110 
clear_map()111   void clear_map() override {
112     VLOG(2) << __func__;
113 
114     std::map<std::string, std::string> empty_map;
115     key_map.swap(empty_map);
116     key_map.clear();
117   }
118 
119  private:
120   BluetoothKeystoreCallbacks* callbacks = nullptr;
121   std::map<std::string, std::string> key_map;
122 };
123 
getBluetoothKeystoreInterface()124 BluetoothKeystoreInterface* getBluetoothKeystoreInterface() {
125   if (!bluetoothKeystoreInstance) {
126     bluetoothKeystoreInstance.reset(new BluetoothKeystoreInterfaceImpl());
127   }
128 
129   return bluetoothKeystoreInstance.get();
130 }
131 
132 }  // namespace bluetooth_keystore
133 }  // namespace bluetooth
134