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 18 #pragma once 19 20 #include <cstdint> 21 22 #include "internal_include/bt_target.h" 23 #include "osi/include/alarm.h" 24 #include "osi/include/fixed_queue.h" 25 #include "osi/include/list.h" 26 #include "stack/btm/btm_sec_int_types.h" 27 #include "stack/btm/security_device_record.h" 28 #include "stack/include/bt_octets.h" 29 #include "stack/include/security_client_callbacks.h" 30 #include "types/raw_address.h" 31 32 class tBTM_SEC_CB { 33 public: 34 tBTM_CFG cfg; /* Device configuration */ 35 36 /***************************************************** 37 ** Local Device control block (on security) 38 *****************************************************/ 39 tBTM_SEC_DEVCB devcb; 40 41 uint16_t enc_handle{0}; 42 BT_OCTET8 enc_rand; /* received rand value from LTK request*/ 43 uint16_t ediv{0}; /* received ediv value from LTK request */ 44 uint8_t key_size{0}; 45 46 public: 47 /***************************************************** 48 ** Security Management 49 *****************************************************/ 50 tBTM_APPL_INFO api; 51 52 tBTM_SEC_DEV_REC* p_collided_dev_rec{nullptr}; 53 alarm_t* sec_collision_timer{nullptr}; 54 uint64_t collision_start_time{0}; 55 uint32_t dev_rec_count{0}; /* Counter used for device record timestamp */ 56 uint8_t security_mode{0}; 57 bool pairing_disabled{false}; 58 bool security_mode_changed{false}; /* mode changed during bonding */ 59 bool pin_type_changed{false}; /* pin type changed during bonding */ 60 bool sec_req_pending{false}; /* true if a request is pending */ 61 62 uint8_t pin_code_len{0}; /* for legacy devices */ 63 PIN_CODE pin_code; /* for legacy devices */ 64 tBTM_PAIRING_STATE pairing_state{BTM_PAIR_STATE_IDLE}; /* The current pairing state */ 65 uint8_t pairing_flags{0}; /* The current pairing flags */ 66 RawAddress pairing_bda; /* The device currently pairing */ 67 alarm_t* pairing_timer{nullptr}; /* Timer for pairing process */ 68 alarm_t* execution_wait_timer{nullptr}; /* To avoid concurrent auth request */ 69 list_t* sec_dev_rec{nullptr}; /* list of tBTM_SEC_DEV_REC */ 70 tBTM_SEC_SERV_REC* p_out_serv{nullptr}; 71 tBTM_MKEY_CALLBACK* mkey_cback{nullptr}; 72 73 RawAddress connecting_bda; 74 75 fixed_queue_t* sec_pending_q{nullptr}; /* pending sequrity requests in 76 tBTM_SEC_QUEUE_ENTRY format */ 77 78 tBTM_SEC_SERV_REC sec_serv_rec[BTM_SEC_MAX_SERVICE_RECORDS]; 79 80 DEV_CLASS connecting_dc; 81 82 void Init(uint8_t initial_security_mode); 83 void Free(); 84 85 tBTM_SEC_SERV_REC* find_first_serv_rec(bool is_originator, uint16_t psm); 86 87 bool IsDeviceBonded(const RawAddress bd_addr); 88 bool IsDeviceEncrypted(const RawAddress bd_addr, tBT_TRANSPORT transport); 89 bool IsDeviceAuthenticated(const RawAddress bd_addr, tBT_TRANSPORT transport); 90 bool IsLinkKeyAuthenticated(const RawAddress bd_addr, tBT_TRANSPORT transport); 91 92 bool IsLinkKeyKnown(const RawAddress bd_addr, tBT_TRANSPORT transport); 93 94 tBTM_SEC_REC* getSecRec(const RawAddress bd_addr); 95 96 bool AddService(bool is_originator, const char* p_name, uint8_t service_id, uint16_t sec_level, 97 uint16_t psm, uint32_t mx_proto_id, uint32_t mx_chan_id); 98 uint8_t RemoveServiceById(uint8_t service_id); 99 uint8_t RemoveServiceByPsm(uint16_t psm); 100 101 void change_pairing_state(tBTM_PAIRING_STATE new_state); 102 103 // misc static methods 104 static const char* btm_pair_state_descr(tBTM_PAIRING_STATE state); 105 }; 106 107 extern tBTM_SEC_CB btm_sec_cb; 108 109 #define BTM_BLE_SEC_CALLBACK(event_, bda_, data_) \ 110 do { \ 111 if (btm_sec_cb.api.p_le_callback != nullptr) { \ 112 tBTM_STATUS status_ = (*btm_sec_cb.api.p_le_callback)((event_), (bda_), (data_)); \ 113 if (status_ != tBTM_STATUS::BTM_SUCCESS) { \ 114 log::warn("Security callback failed {} for {}", btm_status_text(status_), (bda_)); \ 115 } \ 116 } \ 117 } while (0) 118 119 void BTM_Sec_Init(); 120 void BTM_Sec_Free(); 121