1 /*
2  * Copyright 2023 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 <bluetooth/log.h>
20 
21 #include <string>
22 
23 #include "macros.h"
24 #include "stack/include/btm_ble_sec_api_types.h"
25 #include "stack/include/btm_sec_api_types.h"
26 #include "stack/include/btm_status.h"
27 #include "types/raw_address.h"
28 
29 typedef enum : uint8_t {
30   BTM_BLE_SEC_REQ_ACT_NONE = 0,
31   /* encrypt the link using current key or key refresh */
32   BTM_BLE_SEC_REQ_ACT_ENCRYPT = 1,
33   BTM_BLE_SEC_REQ_ACT_PAIR = 2,
34   /* discard the sec request while encryption is started but not completed */
35   BTM_BLE_SEC_REQ_ACT_DISCARD = 3,
36 } tBTM_BLE_SEC_REQ_ACT;
37 
btm_ble_sec_req_act_text(const tBTM_BLE_SEC_REQ_ACT & action)38 inline std::string btm_ble_sec_req_act_text(
39     const tBTM_BLE_SEC_REQ_ACT& action) {
40   switch (action) {
41     CASE_RETURN_TEXT(BTM_BLE_SEC_REQ_ACT_NONE);
42     CASE_RETURN_TEXT(BTM_BLE_SEC_REQ_ACT_ENCRYPT);
43     CASE_RETURN_TEXT(BTM_BLE_SEC_REQ_ACT_PAIR);
44     CASE_RETURN_TEXT(BTM_BLE_SEC_REQ_ACT_DISCARD);
45     default:
46       return "UNKNOWN ACTION";
47   }
48 }
49 /* LE security function from btm_sec.cc */
50 void btm_ble_link_sec_check(const RawAddress& bd_addr,
51                             tBTM_LE_AUTH_REQ auth_req,
52                             tBTM_BLE_SEC_REQ_ACT* p_sec_req_act);
53 void btm_ble_ltk_request_reply(const RawAddress& bda, bool use_stk,
54                                const Octet16& stk);
55 tBTM_STATUS btm_proc_smp_cback(tSMP_EVT event, const RawAddress& bd_addr,
56                                const tSMP_EVT_DATA* p_data);
57 tBTM_STATUS btm_ble_set_encryption(const RawAddress& bd_addr,
58                                    tBTM_BLE_SEC_ACT sec_act, uint8_t link_role);
59 tBTM_STATUS btm_ble_start_encrypt(const RawAddress& bda, bool use_stk,
60                                   Octet16* p_stk);
61 void btm_ble_link_encrypted(const RawAddress& bd_addr, uint8_t encr_enable);
62 
63 void btm_ble_reset_id(void);
64 
65 bool btm_get_local_div(const RawAddress& bd_addr, uint16_t* p_div);
66 bool btm_ble_get_enc_key_type(const RawAddress& bd_addr, uint8_t* p_key_types);
67 
68 void btm_sec_save_le_key(const RawAddress& bd_addr, tBTM_LE_KEY_TYPE key_type,
69                          tBTM_LE_KEY_VALUE* p_keys, bool pass_to_application);
70 void btm_ble_update_sec_key_size(const RawAddress& bd_addr,
71                                  uint8_t enc_key_size);
72 uint8_t btm_ble_read_sec_key_size(const RawAddress& bd_addr);
73 
74 tBTM_STATUS btm_ble_start_sec_check(const RawAddress& bd_addr, uint16_t psm,
75                                     bool is_originator,
76                                     tBTM_SEC_CALLBACK* p_callback,
77                                     void* p_ref_data);
78 
79 namespace fmt {
80 template <>
81 struct formatter<tBTM_BLE_SEC_REQ_ACT>
82     : string_formatter<tBTM_BLE_SEC_REQ_ACT, &btm_ble_sec_req_act_text> {};
83 }  // namespace fmt
84