1 /* 2 * 3 * Copyright 2019 The Android Open Source Project 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at: 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 */ 18 19 #pragma once 20 21 #include "hci/address_with_type.h" 22 #include "hci/octets.h" 23 24 namespace bluetooth { 25 namespace security { 26 namespace record { 27 28 class SecurityRecord { 29 public: SecurityRecord(hci::AddressWithType address)30 explicit SecurityRecord(hci::AddressWithType address) : pseudo_address_(address) {} 31 32 SecurityRecord& operator=(const SecurityRecord& other) = default; 33 34 /** 35 * Returns true if a device is currently pairing to another device 36 */ IsPairing()37 bool IsPairing() const { 38 return pairing_; 39 } 40 41 /* Link key has been exchanged, but not stored */ IsPaired()42 bool IsPaired() const { 43 return IsClassicLinkKeyValid(); 44 } 45 SetLinkKey(std::array<uint8_t,16> link_key,hci::KeyType key_type)46 void SetLinkKey(std::array<uint8_t, 16> link_key, hci::KeyType key_type) { 47 link_key_ = link_key; 48 key_type_ = key_type; 49 CancelPairing(); 50 } 51 CancelPairing()52 void CancelPairing() { 53 pairing_ = false; 54 } 55 GetLinkKey()56 std::array<uint8_t, 16> GetLinkKey() { 57 log::assert_that(IsClassicLinkKeyValid(), "assert failed: IsClassicLinkKeyValid()"); 58 return link_key_; 59 } 60 GetKeyType()61 hci::KeyType GetKeyType() { 62 log::assert_that(IsClassicLinkKeyValid(), "assert failed: IsClassicLinkKeyValid()"); 63 return key_type_; 64 } 65 GetPseudoAddress()66 std::optional<hci::AddressWithType> GetPseudoAddress() { 67 return pseudo_address_; 68 } 69 SetAuthenticated(bool is_authenticated)70 void SetAuthenticated(bool is_authenticated) { 71 this->is_authenticated_ = is_authenticated; 72 } 73 IsAuthenticated()74 bool IsAuthenticated() { 75 return this->is_authenticated_; 76 } 77 SetRequiresMitmProtection(bool requires_mitm_protection)78 void SetRequiresMitmProtection(bool requires_mitm_protection) { 79 this->requires_mitm_protection_ = requires_mitm_protection; 80 } 81 RequiresMitmProtection()82 bool RequiresMitmProtection() { 83 return this->requires_mitm_protection_; 84 } 85 SetIsEncryptionRequired(bool is_encryption_required)86 void SetIsEncryptionRequired(bool is_encryption_required) { 87 this->is_encryption_required_ = is_encryption_required; 88 } 89 IsEncryptionRequired()90 bool IsEncryptionRequired() { 91 return this->is_encryption_required_; 92 } 93 IsClassicLinkKeyValid()94 bool IsClassicLinkKeyValid() const { 95 return !std::all_of(link_key_.begin(), link_key_.end(), [](uint8_t b) { return b == 0; }); 96 } 97 SetIsTemporary(bool is_temp)98 void SetIsTemporary(bool is_temp) { 99 this->is_temporary_ = is_temp; 100 } 101 IsTemporary()102 bool IsTemporary() { 103 return this->is_temporary_; 104 } 105 106 private: 107 108 std::array<uint8_t, 16> link_key_ = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; 109 hci::KeyType key_type_ = hci::KeyType::DEBUG_COMBINATION; 110 111 bool is_temporary_ = false; 112 bool pairing_ = false; 113 bool is_authenticated_ = false; 114 bool requires_mitm_protection_ = false; 115 bool is_encryption_required_ = false; 116 117 public: 118 /* First address we have ever seen this device with, that we used to create bond */ 119 std::optional<hci::AddressWithType> pseudo_address_; 120 121 /* Identity Address */ 122 std::optional<hci::AddressWithType> identity_address_; 123 124 std::optional<hci::Octet16> remote_ltk; 125 uint8_t key_size; 126 uint8_t security_level; 127 std::optional<uint16_t> remote_ediv; 128 std::optional<std::array<uint8_t, 8>> remote_rand; 129 std::optional<hci::Octet16> remote_irk; 130 std::optional<hci::Octet16> remote_signature_key; 131 132 std::optional<hci::Octet16> local_ltk; 133 std::optional<uint16_t> local_ediv; 134 std::optional<std::array<uint8_t, 8>> local_rand; 135 }; 136 137 } // namespace record 138 } // namespace security 139 } // namespace bluetooth 140