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{
65       BTM_PAIR_STATE_IDLE};               /* The current pairing state    */
66   uint8_t pairing_flags{0};               /* The current pairing flags    */
67   RawAddress pairing_bda;                 /* The device currently pairing */
68   alarm_t* pairing_timer{nullptr};        /* Timer for pairing process    */
69   alarm_t* execution_wait_timer{nullptr}; /* To avoid concurrent auth request */
70   list_t* sec_dev_rec{nullptr}; /* list of tBTM_SEC_DEV_REC */
71   tBTM_SEC_SERV_REC* p_out_serv{nullptr};
72   tBTM_MKEY_CALLBACK* mkey_cback{nullptr};
73 
74   RawAddress connecting_bda;
75 
76   fixed_queue_t* sec_pending_q{nullptr}; /* pending sequrity requests in
77                                             tBTM_SEC_QUEUE_ENTRY format */
78 
79   tBTM_SEC_SERV_REC sec_serv_rec[BTM_SEC_MAX_SERVICE_RECORDS];
80 
81   DEV_CLASS connecting_dc;
82 
83   void Init(uint8_t initial_security_mode);
84   void Free();
85 
86   tBTM_SEC_SERV_REC* find_first_serv_rec(bool is_originator, uint16_t psm);
87 
88   bool IsDeviceBonded(const RawAddress bd_addr);
89   bool IsDeviceEncrypted(const RawAddress bd_addr, tBT_TRANSPORT transport);
90   bool IsDeviceAuthenticated(const RawAddress bd_addr, tBT_TRANSPORT transport);
91   bool IsLinkKeyAuthenticated(const RawAddress bd_addr,
92                               tBT_TRANSPORT transport);
93 
94   bool IsLinkKeyKnown(const RawAddress bd_addr, tBT_TRANSPORT transport);
95 
96   tBTM_SEC_REC* getSecRec(const RawAddress bd_addr);
97 
98   bool AddService(bool is_originator, const char* p_name, uint8_t service_id,
99                   uint16_t sec_level, uint16_t psm, uint32_t mx_proto_id,
100                   uint32_t mx_chan_id);
101   uint8_t RemoveServiceById(uint8_t service_id);
102   uint8_t RemoveServiceByPsm(uint16_t psm);
103 
104   void change_pairing_state(tBTM_PAIRING_STATE new_state);
105 
106   // misc static methods
107   static const char* btm_pair_state_descr(tBTM_PAIRING_STATE state);
108 };
109 
110 extern tBTM_SEC_CB btm_sec_cb;
111 
112 void BTM_Sec_Init();
113 void BTM_Sec_Free();
114