aboutsummaryrefslogtreecommitdiff
path: root/gd/storage/config_cache.cc
blob: b1dc1bd0b603557752cf65632a3481ff2d141aac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
/*
 * 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 "storage/config_cache.h"

#include <ios>
#include <sstream>
#include <utility>

#include "hci/enum_helper.h"
#include "os/parameter_provider.h"
#include "storage/mutation.h"

namespace {

const std::unordered_set<std::string_view> kEncryptKeyNameList = {
    "LinkKey", "LE_KEY_PENC", "LE_KEY_PID", "LE_KEY_LID", "LE_KEY_PCSRK", "LE_KEY_LENC", "LE_KEY_LCSRK"};

bool TrimAfterNewLine(std::string& value) {
  std::string value_no_newline;
  size_t newline_position = value.find_first_of('\n');
  if (newline_position != std::string::npos) {
    value.erase(newline_position);
    return true;
  }
  return false;
}

bool InEncryptKeyNameList(std::string key) {
  return kEncryptKeyNameList.find(key) != kEncryptKeyNameList.end();
}

}  // namespace

namespace bluetooth {
namespace storage {

const std::unordered_set<std::string_view> kLePropertyNames = {
    "LE_KEY_PENC", "LE_KEY_PID", "LE_KEY_PCSRK", "LE_KEY_LENC", "LE_KEY_LCSRK"};

const std::unordered_set<std::string_view> kClassicPropertyNames = {
    "LinkKey", "SdpDiMaufacturer", "SdpDiModel", "SdpDiHardwareVersion", "SdpDiVendorSource"};

const std::string ConfigCache::kDefaultSectionName = "Global";

std::string kEncryptedStr = "encrypted";

ConfigCache::ConfigCache(size_t temp_device_capacity, std::unordered_set<std::string_view> persistent_property_names)
    : persistent_property_names_(std::move(persistent_property_names)),
      information_sections_(),
      persistent_devices_(),
      temporary_devices_(temp_device_capacity) {}

void ConfigCache::SetPersistentConfigChangedCallback(std::function<void()> persistent_config_changed_callback) {
  std::lock_guard<std::recursive_mutex> lock(mutex_);
  persistent_config_changed_callback_ = std::move(persistent_config_changed_callback);
}

ConfigCache::ConfigCache(ConfigCache&& other) noexcept
    : persistent_config_changed_callback_(std::move(other.persistent_config_changed_callback_)),
      persistent_property_names_(std::move(other.persistent_property_names_)),
      information_sections_(std::move(other.information_sections_)),
      persistent_devices_(std::move(other.persistent_devices_)),
      temporary_devices_(std::move(other.temporary_devices_)) {
  // std::function will be in a valid but unspecified state after std::move(), hence resetting it
  other.persistent_config_changed_callback_ = {};
}

ConfigCache& ConfigCache::operator=(ConfigCache&& other) noexcept {
  if (&other == this) {
    return *this;
  }
  std::lock_guard<std::recursive_mutex> my_lock(mutex_);
  std::lock_guard<std::recursive_mutex> others_lock(other.mutex_);
  persistent_config_changed_callback_.swap(other.persistent_config_changed_callback_);
  other.persistent_config_changed_callback_ = {};
  persistent_property_names_ = std::move(other.persistent_property_names_);
  information_sections_ = std::move(other.information_sections_);
  persistent_devices_ = std::move(other.persistent_devices_);
  temporary_devices_ = std::move(other.temporary_devices_);
  return *this;
}

bool ConfigCache::operator==(const ConfigCache& rhs) const {
  std::lock_guard<std::recursive_mutex> my_lock(mutex_);
  std::lock_guard<std::recursive_mutex> others_lock(rhs.mutex_);
  return persistent_property_names_ == rhs.persistent_property_names_ &&
         information_sections_ == rhs.information_sections_ && persistent_devices_ == rhs.persistent_devices_ &&
         temporary_devices_ == rhs.temporary_devices_;
}

bool ConfigCache::operator!=(const ConfigCache& rhs) const {
  return !(*this == rhs);
}

void ConfigCache::Clear() {
  std::lock_guard<std::recursive_mutex> lock(mutex_);
  if (information_sections_.size() > 0) {
    information_sections_.clear();
    PersistentConfigChangedCallback();
  }
  if (persistent_devices_.size() > 0) {
    persistent_devices_.clear();
    PersistentConfigChangedCallback();
  }
  if (temporary_devices_.size() > 0) {
    temporary_devices_.clear();
  }
}

bool ConfigCache::HasSection(const std::string& section) const {
  std::lock_guard<std::recursive_mutex> lock(mutex_);
  return information_sections_.contains(section) || persistent_devices_.contains(section) ||
         temporary_devices_.contains(section);
}

bool ConfigCache::HasProperty(const std::string& section, const std::string& property) const {
  std::lock_guard<std::recursive_mutex> lock(mutex_);
  auto section_iter = information_sections_.find(section);
  if (section_iter != information_sections_.end()) {
    return section_iter->second.find(property) != section_iter->second.end();
  }
  section_iter = persistent_devices_.find(section);
  if (section_iter != persistent_devices_.end()) {
    return section_iter->second.find(property) != section_iter->second.end();
  }
  section_iter = temporary_devices_.find(section);
  if (section_iter != temporary_devices_.end()) {
    return section_iter->second.find(property) != section_iter->second.end();
  }
  return false;
}

std::optional<std::string> ConfigCache::GetProperty(const std::string& section, const std::string& property) const {
  std::lock_guard<std::recursive_mutex> lock(mutex_);
  auto section_iter = information_sections_.find(section);
  if (section_iter != information_sections_.end()) {
    auto property_iter = section_iter->second.find(property);
    if (property_iter != section_iter->second.end()) {
      return property_iter->second;
    }
  }
  section_iter = persistent_devices_.find(section);
  if (section_iter != persistent_devices_.end()) {
    auto property_iter = section_iter->second.find(property);
    if (property_iter != section_iter->second.end()) {
      std::string value = property_iter->second;
      if (os::ParameterProvider::GetBtKeystoreInterface() != nullptr && value == kEncryptedStr) {
        return os::ParameterProvider::GetBtKeystoreInterface()->get_key(section + "-" + property);
      }
      return value;
    }
  }
  section_iter = temporary_devices_.find(section);
  if (section_iter != temporary_devices_.end()) {
    auto property_iter = section_iter->second.find(property);
    if (property_iter != section_iter->second.end()) {
      return property_iter->second;
    }
  }
  return std::nullopt;
}

void ConfigCache::SetProperty(std::string section, std::string property, std::string value) {
  std::lock_guard<std::recursive_mutex> lock(mutex_);
  if (TrimAfterNewLine(section) || TrimAfterNewLine(property) || TrimAfterNewLine(value)) {
    android_errorWriteLog(0x534e4554, "70808273");
  }
  ASSERT_LOG(!section.empty(), "Empty section name not allowed");
  ASSERT_LOG(!property.empty(), "Empty property name not allowed");
  if (!IsDeviceSection(section)) {
    auto section_iter = information_sections_.find(section);
    if (section_iter == information_sections_.end()) {
      section_iter = information_sections_.try_emplace_back(section, common::ListMap<std::string, std::string>{}).first;
    }
    section_iter->second.insert_or_assign(property, std::move(value));
    PersistentConfigChangedCallback();
    return;
  }
  auto section_iter = persistent_devices_.find(section);
  if (section_iter == persistent_devices_.end() && IsPersistentProperty(property)) {
    // move paired devices or create new paired device when a link key is set
    auto section_properties = temporary_devices_.extract(section);
    if (section_properties) {
      section_iter = persistent_devices_.try_emplace_back(section, std::move(section_properties->second)).first;
    } else {
      section_iter = persistent_devices_.try_emplace_back(section, common::ListMap<std::string, std::string>{}).first;
    }
  }
  if (section_iter != persistent_devices_.end()) {
    bool is_encrypted = value == kEncryptedStr;
    if ((!value.empty()) && os::ParameterProvider::GetBtKeystoreInterface() != nullptr &&
        os::ParameterProvider::IsCommonCriteriaMode() && InEncryptKeyNameList(property) && !is_encrypted) {
      if (os::ParameterProvider::GetBtKeystoreInterface()->set_encrypt_key_or_remove_key(
              section + "-" + property, value)) {
        value = kEncryptedStr;
      }
    }
    section_iter->second.insert_or_assign(property, std::move(value));
    PersistentConfigChangedCallback();
    return;
  }
  section_iter = temporary_devices_.find(section);
  if (section_iter == temporary_devices_.end()) {
    auto triple = temporary_devices_.try_emplace(section, common::ListMap<std::string, std::string>{});
    section_iter = std::get<0>(triple);
  }
  section_iter->second.insert_or_assign(property, std::move(value));
}

bool ConfigCache::RemoveSection(const std::string& section) {
  std::lock_guard<std::recursive_mutex> lock(mutex_);
  // sections are unique among all three maps, hence removing from one of them is enough
  if (information_sections_.extract(section) || persistent_devices_.extract(section)) {
    PersistentConfigChangedCallback();
    return true;
  } else {
    return temporary_devices_.extract(section).has_value();
  }
}

bool ConfigCache::RemoveProperty(const std::string& section, const std::string& property) {
  std::lock_guard<std::recursive_mutex> lock(mutex_);
  auto section_iter = information_sections_.find(section);
  if (section_iter != information_sections_.end()) {
    auto value = section_iter->second.extract(property);
    // if section is empty after removal, remove the whole section as empty section is not allowed
    if (section_iter->second.size() == 0) {
      information_sections_.erase(section_iter);
    }
    if (value.has_value()) {
      PersistentConfigChangedCallback();
      return true;
    } else {
      return false;
    }
  }
  section_iter = persistent_devices_.find(section);
  if (section_iter != persistent_devices_.end()) {
    auto value = section_iter->second.extract(property);
    // if section is empty after removal, remove the whole section as empty section is not allowed
    if (section_iter->second.size() == 0) {
      persistent_devices_.erase(section_iter);
    } else if (value && IsPersistentProperty(property)) {
      // move unpaired device
      auto section_properties = persistent_devices_.extract(section);
      temporary_devices_.insert_or_assign(section, std::move(section_properties->second));
    }
    if (value.has_value()) {
      PersistentConfigChangedCallback();
      if (os::ParameterProvider::GetBtKeystoreInterface() != nullptr && os::ParameterProvider::IsCommonCriteriaMode() &&
          InEncryptKeyNameList(property)) {
        os::ParameterProvider::GetBtKeystoreInterface()->set_encrypt_key_or_remove_key(section + "-" + property, "");
      }
      return true;
    } else {
      return false;
    }
  }
  section_iter = temporary_devices_.find(section);
  if (section_iter != temporary_devices_.end()) {
    auto value = section_iter->second.extract(property);
    if (section_iter->second.size() == 0) {
      temporary_devices_.erase(section_iter);
    }
    return value.has_value();
  }
  return false;
}

void ConfigCache::ConvertEncryptOrDecryptKeyIfNeeded() {
  std::lock_guard<std::recursive_mutex> lock(mutex_);
  LOG_INFO("%s", __func__);
  auto persistent_sections = GetPersistentSections();
  for (const auto& section : persistent_sections) {
    auto section_iter = persistent_devices_.find(section);
    for (const auto& property : kEncryptKeyNameList) {
      auto property_iter = section_iter->second.find(std::string(property));
      if (property_iter != section_iter->second.end()) {
        bool is_encrypted = property_iter->second == kEncryptedStr;
        if ((!property_iter->second.empty()) && os::ParameterProvider::GetBtKeystoreInterface() != nullptr &&
            os::ParameterProvider::IsCommonCriteriaMode() && !is_encrypted) {
          if (os::ParameterProvider::GetBtKeystoreInterface()->set_encrypt_key_or_remove_key(
                  section + "-" + std::string(property), property_iter->second)) {
            SetProperty(section, std::string(property), kEncryptedStr);
          }
        }
        if (os::ParameterProvider::GetBtKeystoreInterface() != nullptr && is_encrypted) {
          std::string value_str =
              os::ParameterProvider::GetBtKeystoreInterface()->get_key(section + "-" + std::string(property));
          if (!os::ParameterProvider::IsCommonCriteriaMode()) {
            SetProperty(section, std::string(property), value_str);
          }
        }
      }
    }
  }
}

bool ConfigCache::IsDeviceSection(const std::string& section) {
  return hci::Address::IsValidAddress(section);
}

bool ConfigCache::IsPersistentProperty(const std::string& property) const {
  return persistent_property_names_.find(property) != persistent_property_names_.end();
}

void ConfigCache::RemoveSectionWithProperty(const std::string& property) {
  std::lock_guard<std::recursive_mutex> lock(mutex_);
  size_t num_persistent_removed = 0;
  for (auto* config_section : {&information_sections_, &persistent_devices_}) {
    for (auto it = config_section->begin(); it != config_section->end();) {
      if (it->second.contains(property)) {
        LOG_INFO("Removing persistent section %s with property %s", it->first.c_str(), property.c_str());
        it = config_section->erase(it);
        num_persistent_removed++;
        continue;
      }
      it++;
    }
  }
  for (auto it = temporary_devices_.begin(); it != temporary_devices_.end();) {
    if (it->second.contains(property)) {
      LOG_INFO("Removing temporary section %s with property %s", it->first.c_str(), property.c_str());
      it = temporary_devices_.erase(it);
      continue;
    }
    it++;
  }
  if (num_persistent_removed > 0) {
    PersistentConfigChangedCallback();
  }
}

std::vector<std::string> ConfigCache::GetPersistentSections() const {
  std::lock_guard<std::recursive_mutex> lock(mutex_);
  std::vector<std::string> paired_devices;
  paired_devices.reserve(persistent_devices_.size());
  for (const auto& elem : persistent_devices_) {
    paired_devices.emplace_back(elem.first);
  }
  return paired_devices;
}

void ConfigCache::Commit(std::queue<MutationEntry>& mutation_entries) {
  std::lock_guard<std::recursive_mutex> lock(mutex_);
  while (!mutation_entries.empty()) {
    auto entry = std::move(mutation_entries.front());
    mutation_entries.pop();
    switch (entry.entry_type) {
      case MutationEntry::EntryType::SET:
        SetProperty(std::move(entry.section), std::move(entry.property), std::move(entry.value));
        break;
      case MutationEntry::EntryType::REMOVE_PROPERTY:
        RemoveProperty(entry.section, entry.property);
        break;
      case MutationEntry::EntryType::REMOVE_SECTION:
        RemoveSection(entry.section);
        break;
        // do not write a default case so that when a new enum is defined, compilation would fail automatically
    }
  }
}

std::string ConfigCache::SerializeToLegacyFormat() const {
  std::lock_guard<std::recursive_mutex> lock(mutex_);
  std::stringstream serialized;
  for (const auto* config_section : {&information_sections_, &persistent_devices_}) {
    for (const auto& section : *config_section) {
      serialized << "[" << section.first << "]" << std::endl;
      for (const auto& property : section.second) {
        serialized << property.first << " = " << property.second << std::endl;
      }
      serialized << std::endl;
    }
  }
  return serialized.str();
}

std::vector<ConfigCache::SectionAndPropertyValue> ConfigCache::GetSectionNamesWithProperty(
    const std::string& property) const {
  std::lock_guard<std::recursive_mutex> lock(mutex_);
  std::vector<SectionAndPropertyValue> result;
  for (auto* config_section : {&information_sections_, &persistent_devices_}) {
    for (const auto& elem : *config_section) {
      auto it = elem.second.find(property);
      if (it != elem.second.end()) {
        result.emplace_back(SectionAndPropertyValue{.section = elem.first, .property = it->second});
        continue;
      }
    }
  }
  for (const auto& elem : temporary_devices_) {
    auto it = elem.second.find(property);
    if (it != elem.second.end()) {
      result.emplace_back(SectionAndPropertyValue{.section = elem.first, .property = it->second});
      continue;
    }
  }
  return result;
}

namespace {

bool FixDeviceTypeInconsistencyInSection(
    const std::string& section_name, common::ListMap<std::string, std::string>& device_section_entries) {
  if (!hci::Address::IsValidAddress(section_name)) {
    return false;
  }
  bool is_le = false;
  bool is_classic = false;
  // default
  hci::DeviceType device_type = hci::DeviceType::BR_EDR;
  for (const auto& entry : device_section_entries) {
    if (kLePropertyNames.find(entry.first) != kLePropertyNames.end()) {
      is_le = true;
    }
    if (kClassicPropertyNames.find(entry.first) != kClassicPropertyNames.end()) {
      is_classic = true;
    }
  }
  if (is_classic && is_le) {
    device_type = hci::DeviceType::DUAL;
  } else if (is_classic) {
    device_type = hci::DeviceType::BR_EDR;
  } else if (is_le) {
    device_type = hci::DeviceType::LE;
  }
  bool inconsistent = true;
  std::string device_type_str = std::to_string(device_type);
  auto it = device_section_entries.find("DevType");
  if (it != device_section_entries.end()) {
    inconsistent = device_type_str != it->second;
    if (inconsistent) {
      it->second = std::move(device_type_str);
    }
  } else {
    device_section_entries.insert_or_assign("DevType", std::move(device_type_str));
  }
  return inconsistent;
}

}  // namespace

bool ConfigCache::FixDeviceTypeInconsistencies() {
  std::lock_guard<std::recursive_mutex> lock(mutex_);
  bool persistent_device_changed = false;
  for (auto* config_section : {&information_sections_, &persistent_devices_}) {
    for (auto& elem : *config_section) {
      if (FixDeviceTypeInconsistencyInSection(elem.first, elem.second)) {
        persistent_device_changed = true;
      }
    }
  }
  bool temp_device_changed = false;
  for (auto& elem : temporary_devices_) {
    if (FixDeviceTypeInconsistencyInSection(elem.first, elem.second)) {
      temp_device_changed = true;
    }
  }
  if (persistent_device_changed) {
    PersistentConfigChangedCallback();
  }
  return persistent_device_changed || temp_device_changed;
}

bool ConfigCache::HasAtLeastOneMatchingPropertiesInSection(
    const std::string& section, const std::unordered_set<std::string_view>& property_names) const {
  std::lock_guard<std::recursive_mutex> lock(mutex_);
  const common::ListMap<std::string, std::string>* section_ptr;
  if (!IsDeviceSection(section)) {
    auto section_iter = information_sections_.find(section);
    if (section_iter == information_sections_.end()) {
      return false;
    }
    section_ptr = &section_iter->second;
  } else {
    auto section_iter = persistent_devices_.find(section);
    if (section_iter == persistent_devices_.end()) {
      section_iter = temporary_devices_.find(section);
      if (section_iter == temporary_devices_.end()) {
        return false;
      }
    }
    section_ptr = &section_iter->second;
  }
  for (const auto& property : *section_ptr) {
    if (property_names.count(property.first) > 0) {
      return true;
    }
  }
  return false;
}

bool ConfigCache::IsPersistentSection(const std::string& section) const {
  std::lock_guard<std::recursive_mutex> lock(mutex_);
  return persistent_devices_.contains(section);
}

}  // namespace storage
}  // namespace bluetooth