1 /******************************************************************************
2 *
3 * Copyright 1999-2012 Broadcom Corporation
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 <bluetooth/log.h>
22
23 #include <cstdint>
24 #include <string>
25
26 #include "internal_include/bt_target.h"
27 #include "macros.h"
28 #include "stack/include/bt_device_type.h"
29 #include "stack/include/bt_name.h"
30 #include "stack/include/bt_octets.h"
31 #include "stack/include/btm_sec_api_types.h"
32 #include "stack/include/hci_error_code.h"
33 #include "types/ble_address_with_type.h"
34 #include "types/raw_address.h"
35 #include "types/remote_version_type.h"
36
37 typedef struct {
38 uint16_t min_conn_int;
39 uint16_t max_conn_int;
40 uint16_t peripheral_latency;
41 uint16_t supervision_tout;
42 } tBTM_LE_CONN_PRAMS;
43
44 /* The MSB of the clock offset field indicates whether the offset is valid. */
45 #define BTM_CLOCK_OFFSET_VALID 0x8000
46
47 /*
48 * Define structure for Security Service Record.
49 * A record exists for each service registered with the Security Manager
50 */
51 #define BTM_SEC_OUT_FLAGS (BTM_SEC_OUT_AUTHENTICATE | BTM_SEC_OUT_ENCRYPT)
52 #define BTM_SEC_IN_FLAGS (BTM_SEC_IN_AUTHENTICATE | BTM_SEC_IN_ENCRYPT)
53
54 #define BTM_SEC_OUT_LEVEL4_FLAGS \
55 (BTM_SEC_OUT_AUTHENTICATE | BTM_SEC_OUT_ENCRYPT | BTM_SEC_OUT_MITM | BTM_SEC_MODE4_LEVEL4)
56
57 #define BTM_SEC_IN_LEVEL4_FLAGS \
58 (BTM_SEC_IN_AUTHENTICATE | BTM_SEC_IN_ENCRYPT | BTM_SEC_IN_MITM | BTM_SEC_MODE4_LEVEL4)
59 typedef struct {
60 uint32_t mx_proto_id; /* Service runs over this multiplexer protocol */
61 uint32_t orig_mx_chan_id; /* Channel on the multiplexer protocol */
62 uint32_t term_mx_chan_id; /* Channel on the multiplexer protocol */
63 uint16_t psm; /* L2CAP PSM value */
64 uint16_t security_flags; /* Bitmap of required security features */
65 uint8_t service_id; /* Passed in authorization callback */
66 uint8_t orig_service_name[BT_MAX_SERVICE_NAME_LEN + 1];
67 uint8_t term_service_name[BT_MAX_SERVICE_NAME_LEN + 1];
68 } tBTM_SEC_SERV_REC;
69
70 /* LE Security information of device in Peripheral Role */
71 typedef struct {
72 Octet16 irk; /* peer diverified identity root */
73 Octet16 pltk; /* peer long term key */
74 Octet16 pcsrk; /* peer SRK peer device used to secured sign local data */
75
76 Octet16 lltk; /* local long term key */
77 Octet16 lcsrk; /* local SRK peer device used to secured sign local data */
78
79 BT_OCTET8 rand; /* random vector for LTK generation */
80 uint16_t ediv; /* LTK diversifier of this peripheral device */
81 uint16_t div; /* local DIV to generate local LTK=d1(ER, DIV, 0) and
82 CSRK=d1(ER, DIV, 1) */
83 uint8_t sec_level; /* local pairing security level */
84 uint8_t key_size; /* key size of the LTK delivered to peer device */
85 uint8_t srk_sec_level; /* security property of peer SRK for this device */
86 uint8_t local_csrk_sec_level; /* security property of local CSRK for this
87 device */
88
89 uint32_t counter; /* peer sign counter for verifying rcv signed cmd */
90 uint32_t local_counter; /* local sign counter for sending signed write cmd*/
91
92 tBTM_LE_KEY_TYPE key_type; /* bit mask of valid key types in record */
93 } tBTM_SEC_BLE_KEYS;
94
95 // TODO: move it to btm_ble_addr.h
96 enum tBLE_RAND_ADDR_TYPE : uint8_t {
97 BTM_BLE_ADDR_PSEUDO = 0,
98 BTM_BLE_ADDR_RRA = 1,
99 BTM_BLE_ADDR_STATIC = 2,
100 };
101
102 class tBTM_BLE_ADDR_INFO {
103 public:
104 RawAddress pseudo_addr; /* LE pseudo address of the device if different from device address */
105 public:
AddressType()106 tBLE_ADDR_TYPE AddressType() const { return ble_addr_type_; }
SetAddressType(tBLE_ADDR_TYPE ble_addr_type)107 void SetAddressType(tBLE_ADDR_TYPE ble_addr_type) {
108 if (is_ble_addr_type_known(ble_addr_type)) {
109 ble_addr_type_ = ble_addr_type;
110 } else {
111 bluetooth::log::error("Unknown address type:0x{:x}", ble_addr_type);
112 }
113 }
114
115 tBLE_BD_ADDR identity_address_with_type;
116
117 #define BTM_RESOLVING_LIST_BIT 0x02
118 uint8_t in_controller_list; /* in controller resolving list or not */
119 uint8_t resolving_list_index;
120 RawAddress cur_rand_addr; /* current random address */
121
122 tBLE_RAND_ADDR_TYPE active_addr_type;
123
124 private:
125 tBLE_ADDR_TYPE ble_addr_type_; /* LE device type: public or random address */
126 };
127
128 enum : uint16_t {
129 BTM_SEC_AUTHENTICATED = 0x0002,
130 BTM_SEC_ENCRYPTED = 0x0004,
131 BTM_SEC_NAME_KNOWN = 0x0008,
132 BTM_SEC_LINK_KEY_KNOWN = 0x0010,
133 BTM_SEC_LINK_KEY_AUTHED = 0x0020,
134 BTM_SEC_ROLE_SWITCHED = 0x0040, // UNUSED - only cleared
135 BTM_SEC_IN_USE = 0x0080, // UNUSED - only set
136 /* LE link security flag */
137 /* LE link is encrypted after pairing with MITM */
138 BTM_SEC_LE_AUTHENTICATED = 0x0200,
139 /* LE link is encrypted */
140 BTM_SEC_LE_ENCRYPTED = 0x0400,
141 /* not used */
142 BTM_SEC_LE_NAME_KNOWN = 0x0800, // UNUSED
143 /* bonded with peer (peer LTK and/or SRK is saved) */
144 BTM_SEC_LE_LINK_KEY_KNOWN = 0x1000,
145 /* pairing is done with MITM */
146 BTM_SEC_LE_LINK_KEY_AUTHED = 0x2000,
147 /* pairing is done with 16 digit pin */
148 BTM_SEC_16_DIGIT_PIN_AUTHED = 0x4000,
149 };
150
151 enum class tSECURITY_STATE : uint8_t {
152 IDLE = 0,
153 AUTHENTICATING = 1,
154 ENCRYPTING = 2,
155 GETTING_NAME = 3,
156 AUTHORIZING = 4,
157 SWITCHING_ROLE = 5,
158 /* disconnecting BR/EDR */
159 DISCONNECTING = 6,
160 /* delay to check for encryption to work around */
161 /* controller problems */
162 DELAY_FOR_ENC = 7,
163 };
164
security_state_text(const tSECURITY_STATE & state)165 static inline std::string security_state_text(const tSECURITY_STATE& state) {
166 switch (state) {
167 CASE_RETURN_STRING(tSECURITY_STATE::IDLE);
168 CASE_RETURN_STRING(tSECURITY_STATE::AUTHENTICATING);
169 CASE_RETURN_STRING(tSECURITY_STATE::ENCRYPTING);
170 CASE_RETURN_STRING(tSECURITY_STATE::GETTING_NAME);
171 CASE_RETURN_STRING(tSECURITY_STATE::AUTHORIZING);
172 CASE_RETURN_STRING(tSECURITY_STATE::SWITCHING_ROLE);
173 CASE_RETURN_STRING(tSECURITY_STATE::DISCONNECTING);
174 CASE_RETURN_STRING(tSECURITY_STATE::DELAY_FOR_ENC);
175 default:
176 RETURN_UNKNOWN_TYPE_STRING(tSECURITY_STATE, state);
177 }
178 }
179
180 typedef enum : uint8_t {
181 BTM_SM4_UNKNOWN = 0x00,
182 BTM_SM4_KNOWN = 0x10,
183 BTM_SM4_TRUE = 0x11,
184 BTM_SM4_REQ_PEND = 0x08, /* set this bit when getting remote features */
185 BTM_SM4_UPGRADE = 0x04, /* set this bit when upgrading link key */
186 BTM_SM4_RETRY = 0x02, /* set this bit to retry on HCI_ERR_KEY_MISSING or \
187 HCI_ERR_LMP_ERR_TRANS_COLLISION */
188 BTM_SM4_DD_ACP = 0x20, /* set this bit to indicate peer initiated dedicated bonding */
189 BTM_SM4_CONN_PEND = 0x40, /* set this bit to indicate accepting acl conn; to
190 be cleared on \ btm_acl_created */
191 } tBTM_SM4_BIT;
192
193 /*
194 * Define structure for Security Device Record.
195 * A record exists for each device authenticated with this device
196 */
197 struct tBTM_SEC_REC {
198 tSECURITY_STATE classic_link; /* Operating state of Classic link */
199 tSECURITY_STATE le_link; /* Operating state of LE link */
200
201 tHCI_STATUS sec_status; /* Status in encryption change event */
202 uint16_t sec_flags; /* Current device security state */
203
204 uint8_t pin_code_length; /* Length of the pin_code used for pairing */
205 uint32_t required_security_flags_for_pairing;
206 uint16_t security_required; /* Security required for connection */
207 // security callback and its argument
208 tBTM_SEC_CALLBACK* p_callback;
209 void* p_ref_data;
210
211 bool link_key_not_sent; /* link key notification has not been sent waiting for
212 name */
213 tBTM_IO_CAP rmt_io_caps; /* IO capability of the peer device */
214 tBTM_AUTH_REQ rmt_auth_req; /* the auth_req flag as in the IO caps rsp evt */
215 bool new_encryption_key_is_p256; /* Set to true when the newly generated LK
216 ** is generated from P-256.
217 ** Link encrypted with such LK can be used
218 ** for SM over BR/EDR. */
219
220 // BREDR Link Key Info
221 LinkKey link_key; /* Device link key */
222 uint8_t link_key_type; /* Type of key used in pairing */
223 uint8_t enc_key_size; /* current link encryption key size */
224 tBTM_BOND_TYPE bond_type; /* Whether the BR/EDR pairing was persistent or temporary */
225
226 // LE Link Key Info
227 tBTM_SEC_BLE_KEYS ble_keys;
228
229 public:
is_device_authenticatedtBTM_SEC_REC230 bool is_device_authenticated() const { return sec_flags & BTM_SEC_AUTHENTICATED; }
set_device_authenticatedtBTM_SEC_REC231 void set_device_authenticated() { sec_flags |= BTM_SEC_AUTHENTICATED; }
reset_device_authenticatedtBTM_SEC_REC232 void reset_device_authenticated() { sec_flags &= ~BTM_SEC_AUTHENTICATED; }
233
is_device_encryptedtBTM_SEC_REC234 bool is_device_encrypted() const { return sec_flags & BTM_SEC_ENCRYPTED; }
set_device_encryptedtBTM_SEC_REC235 void set_device_encrypted() { sec_flags |= BTM_SEC_ENCRYPTED; }
reset_device_encryptedtBTM_SEC_REC236 void reset_device_encrypted() { sec_flags &= ~BTM_SEC_ENCRYPTED; }
237
is_name_knowntBTM_SEC_REC238 bool is_name_known() const { return sec_flags & BTM_SEC_NAME_KNOWN; }
set_device_knowntBTM_SEC_REC239 void set_device_known() { sec_flags |= BTM_SEC_NAME_KNOWN; }
reset_device_knowntBTM_SEC_REC240 void reset_device_known() { sec_flags &= ~BTM_SEC_NAME_KNOWN; }
241
is_link_key_knowntBTM_SEC_REC242 bool is_link_key_known() const { return sec_flags & BTM_SEC_LINK_KEY_KNOWN; }
set_link_key_knowntBTM_SEC_REC243 void set_link_key_known() { sec_flags |= BTM_SEC_LINK_KEY_KNOWN; }
reset_link_key_knowntBTM_SEC_REC244 void reset_link_key_known() { sec_flags &= ~BTM_SEC_LINK_KEY_KNOWN; }
245
is_link_key_authenticatedtBTM_SEC_REC246 bool is_link_key_authenticated() const { return sec_flags & BTM_SEC_LINK_KEY_AUTHED; }
set_link_key_authenticatedtBTM_SEC_REC247 void set_link_key_authenticated() { sec_flags |= BTM_SEC_LINK_KEY_AUTHED; }
reset_link_key_authenticatedtBTM_SEC_REC248 void reset_link_key_authenticated() { sec_flags &= ~BTM_SEC_LINK_KEY_AUTHED; }
249
is_le_device_authenticatedtBTM_SEC_REC250 bool is_le_device_authenticated() const { return sec_flags & BTM_SEC_LE_AUTHENTICATED; }
set_le_device_authenticatedtBTM_SEC_REC251 void set_le_device_authenticated() { sec_flags |= BTM_SEC_LE_AUTHENTICATED; }
reset_le_device_authenticatedtBTM_SEC_REC252 void reset_le_device_authenticated() { sec_flags &= ~BTM_SEC_LE_AUTHENTICATED; }
253
is_le_device_encryptedtBTM_SEC_REC254 bool is_le_device_encrypted() const { return sec_flags & BTM_SEC_LE_ENCRYPTED; }
set_le_device_encryptedtBTM_SEC_REC255 void set_le_device_encrypted() { sec_flags |= BTM_SEC_LE_ENCRYPTED; }
reset_le_device_encryptedtBTM_SEC_REC256 void reset_le_device_encrypted() { sec_flags &= ~BTM_SEC_LE_ENCRYPTED; }
257
is_le_link_key_knowntBTM_SEC_REC258 bool is_le_link_key_known() const { return sec_flags & BTM_SEC_LE_LINK_KEY_KNOWN; }
set_le_link_key_knowntBTM_SEC_REC259 void set_le_link_key_known() { sec_flags |= BTM_SEC_LE_LINK_KEY_KNOWN; }
reset_le_link_key_knowntBTM_SEC_REC260 void reset_le_link_key_known() { sec_flags &= ~BTM_SEC_LE_LINK_KEY_KNOWN; }
261
is_le_link_key_authenticatedtBTM_SEC_REC262 bool is_le_link_key_authenticated() const { return sec_flags & BTM_SEC_LE_LINK_KEY_AUTHED; }
set_le_link_key_authenticatedtBTM_SEC_REC263 void set_le_link_key_authenticated() { sec_flags |= BTM_SEC_LE_LINK_KEY_AUTHED; }
reset_le_link_key_authenticatedtBTM_SEC_REC264 void reset_le_link_key_authenticated() { sec_flags &= ~BTM_SEC_LE_LINK_KEY_AUTHED; }
265
is_le_link_16_digit_key_authenticatedtBTM_SEC_REC266 bool is_le_link_16_digit_key_authenticated() const {
267 return sec_flags & BTM_SEC_16_DIGIT_PIN_AUTHED;
268 }
set_le_link_16_digit_key_authenticatedtBTM_SEC_REC269 void set_le_link_16_digit_key_authenticated() { sec_flags |= BTM_SEC_16_DIGIT_PIN_AUTHED; }
reset_le_link_16_digit_key_authenticatedtBTM_SEC_REC270 void reset_le_link_16_digit_key_authenticated() { sec_flags &= ~BTM_SEC_16_DIGIT_PIN_AUTHED; }
271
is_security_state_bredr_encryptingtBTM_SEC_REC272 bool is_security_state_bredr_encrypting() const {
273 return classic_link == tSECURITY_STATE::ENCRYPTING;
274 }
is_security_state_le_encryptingtBTM_SEC_REC275 bool is_security_state_le_encrypting() const { return le_link == tSECURITY_STATE::ENCRYPTING; }
is_security_state_encryptingtBTM_SEC_REC276 bool is_security_state_encrypting() const {
277 return is_security_state_bredr_encrypting() || is_security_state_le_encrypting();
278 }
is_security_state_getting_nametBTM_SEC_REC279 bool is_security_state_getting_name() const {
280 return classic_link == tSECURITY_STATE::GETTING_NAME;
281 }
282
is_bond_type_unknowntBTM_SEC_REC283 bool is_bond_type_unknown() const { return bond_type == BOND_TYPE_UNKNOWN; }
is_bond_type_persistenttBTM_SEC_REC284 bool is_bond_type_persistent() const { return bond_type == BOND_TYPE_PERSISTENT; }
is_bond_type_temporarytBTM_SEC_REC285 bool is_bond_type_temporary() const { return bond_type == BOND_TYPE_TEMPORARY; }
286
get_encryption_key_sizetBTM_SEC_REC287 uint8_t get_encryption_key_size() const { return enc_key_size; }
288
289 void increment_sign_counter(bool local);
290
ToStringtBTM_SEC_REC291 std::string ToString() const {
292 return std::format(
293 "bredr_linkkey_known:{:c},le_linkkey_known:{:c},bond_type:{},bredr_linkkey_type:{},ble_"
294 "enc_key_size:{},bredr_authenticated:{:c},le_authenticated:{:c},16_digit_key_"
295 "authenticated:{:c},bredr_encrypted:{:c},le_encrypted:{:c}",
296 is_link_key_known() ? 'T' : 'F', is_le_link_key_known() ? 'T' : 'F',
297 bond_type_text(bond_type), linkkey_type_text(link_key_type), enc_key_size,
298 is_device_authenticated() ? 'T' : 'F', is_le_device_authenticated() ? 'T' : 'F',
299 is_le_link_16_digit_key_authenticated() ? 'T' : 'F', is_device_encrypted() ? 'T' : 'F',
300 is_le_device_encrypted() ? 'T' : 'F');
301 }
302 };
303
304 class tBTM_SEC_DEV_REC {
305 public:
RemoteAddress()306 RawAddress RemoteAddress() const { return bd_addr; }
307
308 /* Data length extension */
set_suggested_tx_octect(uint16_t octets)309 void set_suggested_tx_octect(uint16_t octets) { suggested_tx_octets = octets; }
310
get_suggested_tx_octets()311 uint16_t get_suggested_tx_octets() const { return suggested_tx_octets; }
IsLocallyInitiated()312 bool IsLocallyInitiated() const { return is_originator; }
313
get_br_edr_hci_handle()314 uint16_t get_br_edr_hci_handle() const { return hci_handle; }
get_ble_hci_handle()315 uint16_t get_ble_hci_handle() const { return ble_hci_handle; }
316
is_device_type_br_edr()317 bool is_device_type_br_edr() const { return device_type == BT_DEVICE_TYPE_BREDR; }
is_device_type_ble()318 bool is_device_type_ble() const { return device_type == BT_DEVICE_TYPE_BLE; }
is_device_type_dual_mode()319 bool is_device_type_dual_mode() const { return device_type == BT_DEVICE_TYPE_DUMO; }
320
is_device_type_has_ble()321 bool is_device_type_has_ble() const { return device_type & BT_DEVICE_TYPE_BLE; }
322
SupportsSecureConnections()323 bool SupportsSecureConnections() const { return remote_supports_secure_connections; }
324
ToString()325 std::string ToString() const {
326 return std::format(
327 "{} {:6s} cod:{} remote_info:{:<14s} sm4:0x{:02x} SecureConn:{:c} "
328 "name:\"{}\" sec_prop:{}",
329 bd_addr, DeviceTypeText(device_type), dev_class_text(dev_class),
330 remote_version_info.ToString(), sm4, remote_supports_secure_connections ? 'T' : 'F',
331 reinterpret_cast<char const*>(sec_bd_name), sec_rec.ToString());
332 }
333
334 public:
335 RawAddress bd_addr; /* BD_ADDR of the device */
336 tBTM_BLE_ADDR_INFO ble;
337 BD_NAME sec_bd_name; /* User friendly name of the device. (may be
338 truncated to save space in dev_rec table) */
339 DEV_CLASS dev_class; /* DEV_CLASS of the device */
340 tBT_DEVICE_TYPE device_type;
341
342 uint32_t timestamp; /* Timestamp of the last connection */
343 uint16_t hci_handle; /* Handle to BR/EDR ACL connection when exists */
344 uint16_t ble_hci_handle; /* use in DUMO connection */
345
346 uint16_t suggested_tx_octets; /* Recently suggested tx octets for data length extension */
347 uint16_t clock_offset; /* Latest known clock offset */
348
349 // whether the peer device can read GAP characteristics only visible in
350 // "discoverable" mode
351 bool can_read_discoverable{true};
352
353 bool remote_features_needed; /* set to true if the local device is in */
354 /* "Secure Connections Only" mode and it receives */
355 /* HCI_IO_CAPABILITY_REQUEST_EVT from the peer before */
356 /* it knows peer's support for Secure Connections */
357 uint8_t sm4; /* BTM_SM4_TRUE, if the peer supports SM4 */
358 bool remote_supports_hci_role_switch = false;
359 bool remote_supports_bredr;
360 bool remote_supports_ble;
361 bool remote_supports_secure_connections;
362 bool remote_feature_received = false;
363
364 tREMOTE_VERSION_INFO remote_version_info;
365
366 bool role_central; /* true if current mode is central (BLE) */
367 bool is_originator; /* true if device is originating ACL connection */
368
369 // BLE connection parameters
370 tBTM_LE_CONN_PRAMS conn_params;
371 // security related properties
372 tBTM_SEC_REC sec_rec;
373 };
374
375 namespace std {
376 template <>
377 struct formatter<tSECURITY_STATE> : string_formatter<tSECURITY_STATE, &security_state_text> {};
378 template <>
379 struct formatter<tBLE_RAND_ADDR_TYPE> : enum_formatter<tBLE_RAND_ADDR_TYPE> {};
380 } // namespace std
381