1 /* 2 * Copyright 2019 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 #pragma once 18 19 #include <set> 20 21 #include "hci/address_with_type.h" 22 #include "security/record/security_record.h" 23 #include "security/record/security_record_storage.h" 24 25 namespace bluetooth { 26 namespace security { 27 namespace record { 28 29 class SecurityRecordDatabase { 30 public: SecurityRecordDatabase(record::SecurityRecordStorage security_record_storage)31 SecurityRecordDatabase(record::SecurityRecordStorage security_record_storage) 32 : security_record_storage_(security_record_storage) {} 33 34 using iterator = std::set<std::shared_ptr<SecurityRecord>>::iterator; 35 FindOrCreate(hci::AddressWithType address)36 std::shared_ptr<SecurityRecord> FindOrCreate(hci::AddressWithType address) { 37 auto it = Find(address); 38 // Security record check 39 if (it != records_.end()) return *it; 40 41 // No security record, create one 42 auto record_ptr = std::make_shared<SecurityRecord>(address); 43 records_.insert(record_ptr); 44 return record_ptr; 45 } 46 Remove(const hci::AddressWithType & address)47 void Remove(const hci::AddressWithType& address) { 48 auto it = Find(address); 49 50 // No record exists 51 if (it == records_.end()) return; 52 53 records_.erase(it); 54 security_record_storage_.RemoveDevice(address); 55 } 56 Find(hci::AddressWithType address)57 iterator Find(hci::AddressWithType address) { 58 for (auto it = records_.begin(); it != records_.end(); ++it) { 59 std::shared_ptr<SecurityRecord> record = *it; 60 if (record->identity_address_.has_value() && record->identity_address_.value() == address) return it; 61 if (record->GetPseudoAddress() == address) return it; 62 if (record->remote_irk.has_value() && address.IsRpaThatMatchesIrk(record->remote_irk.value())) return it; 63 } 64 return records_.end(); 65 } 66 LoadRecordsFromStorage()67 void LoadRecordsFromStorage() { 68 security_record_storage_.LoadSecurityRecords(&records_); 69 } 70 SaveRecordsToStorage()71 void SaveRecordsToStorage() { 72 security_record_storage_.SaveSecurityRecords(&records_); 73 } 74 75 std::set<std::shared_ptr<SecurityRecord>> records_; 76 record::SecurityRecordStorage security_record_storage_; 77 }; 78 79 } // namespace record 80 } // namespace security 81 } // namespace bluetooth 82