• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <storage/device.h>
22 #include <memory>
23 #include <utility>
24 
25 #include "crypto_toolbox/crypto_toolbox.h"
26 #include "hci/address_with_type.h"
27 #include "storage/device.h"
28 
29 namespace bluetooth {
30 namespace security {
31 namespace record {
32 
33 class SecurityRecord {
34  public:
SecurityRecord(hci::AddressWithType address)35   explicit SecurityRecord(hci::AddressWithType address) : pseudo_address_(address) {}
36 
37   SecurityRecord& operator=(const SecurityRecord& other) = default;
38 
39   /**
40    * Returns true if a device is currently pairing to another device
41    */
IsPairing()42   bool IsPairing() const {
43     return pairing_;
44   }
45 
46   /* Link key has been exchanged, but not stored */
IsPaired()47   bool IsPaired() const {
48     return IsClassicLinkKeyValid();
49   }
50 
SetLinkKey(std::array<uint8_t,16> link_key,hci::KeyType key_type)51   void SetLinkKey(std::array<uint8_t, 16> link_key, hci::KeyType key_type) {
52     link_key_ = link_key;
53     key_type_ = key_type;
54     CancelPairing();
55   }
56 
CancelPairing()57   void CancelPairing() {
58     pairing_ = false;
59   }
60 
GetLinkKey()61   std::array<uint8_t, 16> GetLinkKey() {
62     ASSERT(IsClassicLinkKeyValid());
63     return link_key_;
64   }
65 
GetKeyType()66   hci::KeyType GetKeyType() {
67     ASSERT(IsClassicLinkKeyValid());
68     return key_type_;
69   }
70 
GetPseudoAddress()71   std::optional<hci::AddressWithType> GetPseudoAddress() {
72     return pseudo_address_;
73   }
74 
SetAuthenticated(bool is_authenticated)75   void SetAuthenticated(bool is_authenticated) {
76     this->is_authenticated_ = is_authenticated;
77   }
78 
IsAuthenticated()79   bool IsAuthenticated() {
80     return this->is_authenticated_;
81   }
82 
SetRequiresMitmProtection(bool requires_mitm_protection)83   void SetRequiresMitmProtection(bool requires_mitm_protection) {
84     this->requires_mitm_protection_ = requires_mitm_protection;
85   }
86 
RequiresMitmProtection()87   bool RequiresMitmProtection() {
88     return this->requires_mitm_protection_;
89   }
90 
SetIsEncryptionRequired(bool is_encryption_required)91   void SetIsEncryptionRequired(bool is_encryption_required) {
92     this->is_encryption_required_ = is_encryption_required;
93   }
94 
IsEncryptionRequired()95   bool IsEncryptionRequired() {
96     return this->is_encryption_required_;
97   }
98 
IsClassicLinkKeyValid()99   bool IsClassicLinkKeyValid() const {
100     return !std::all_of(link_key_.begin(), link_key_.end(), [](uint8_t b) { return b == 0; });
101   }
102 
SetIsTemporary(bool is_temp)103   void SetIsTemporary(bool is_temp) {
104     this->is_temporary_ = is_temp;
105   }
106 
IsTemporary()107   bool IsTemporary() {
108     return this->is_temporary_;
109   }
110 
111  private:
112 
113   std::array<uint8_t, 16> link_key_ = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
114   hci::KeyType key_type_ = hci::KeyType::DEBUG_COMBINATION;
115 
116   bool is_temporary_ = false;
117   bool pairing_ = false;
118   bool is_authenticated_ = false;
119   bool requires_mitm_protection_ = false;
120   bool is_encryption_required_ = false;
121 
122  public:
123   /* First address we have ever seen this device with, that we used to create bond */
124   std::optional<hci::AddressWithType> pseudo_address_;
125 
126   /* Identity Address */
127   std::optional<hci::AddressWithType> identity_address_;
128 
129   std::optional<crypto_toolbox::Octet16> remote_ltk;
130   uint8_t key_size;
131   uint8_t security_level;
132   std::optional<uint16_t> remote_ediv;
133   std::optional<std::array<uint8_t, 8>> remote_rand;
134   std::optional<crypto_toolbox::Octet16> remote_irk;
135   std::optional<crypto_toolbox::Octet16> remote_signature_key;
136 
137   std::optional<crypto_toolbox::Octet16> local_ltk;
138   std::optional<uint16_t> local_ediv;
139   std::optional<std::array<uint8_t, 8>> local_rand;
140 };
141 
142 }  // namespace record
143 }  // namespace security
144 }  // namespace bluetooth
145