1 /*
2  * Copyright 2020 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 <cstdint>
20 
21 #include "stack/include/bt_dev_class.h"
22 #include "stack/include/bt_device_type.h"
23 #include "stack/include/bt_name.h"
24 #include "stack/include/bt_octets.h"
25 #include "stack/include/btm_ble_sec_api_types.h"
26 #include "stack/include/hci_error_code.h"
27 #include "types/bt_transport.h"
28 #include "types/raw_address.h"
29 
30 /****************************************
31  *  Security Manager Callback Functions
32  ****************************************/
33 /* Authorize device for service.  Parameters are
34  *              Service Id (NULL - unknown service or unused)
35  */
36 typedef uint8_t(tBTM_AUTHORIZE_CALLBACK)(uint8_t service_id);
37 
38 /* Get PIN for the connection.  Parameters are
39  *              BD Address of remote
40  *              Device Class of remote
41  *              BD Name of remote
42  *              Flag indicating the minimum pin code length to be 16 digits
43  */
44 typedef uint8_t(tBTM_PIN_CALLBACK)(const RawAddress& bd_addr,
45                                    DEV_CLASS dev_class, const BD_NAME bd_name,
46                                    bool min_16_digit);
47 
48 /* New Link Key for the connection.  Parameters are
49  *              BD Address of remote
50  *              Link Key
51  *              Key Type: Combination, Local Unit, or Remote Unit
52  */
53 typedef uint8_t(tBTM_LINK_KEY_CALLBACK)(const RawAddress& bd_addr,
54                                         DEV_CLASS dev_class, BD_NAME bd_name,
55                                         const LinkKey& key, uint8_t key_type,
56                                         bool is_ctkd);
57 
58 /* Remote Name Resolved.  Parameters are
59  *              BD Address of remote
60  *              BD Name of remote
61  */
62 typedef void(tBTM_RMT_NAME_CALLBACK)(const RawAddress& bd_addr, DEV_CLASS dc,
63                                      BD_NAME bd_name);
64 
65 /* Authentication complete for the connection.  Parameters are
66  *              BD Address of remote
67  *              Device Class of remote
68  *              BD Name of remote
69  *
70  */
71 typedef void(tBTM_AUTH_COMPLETE_CALLBACK)(const RawAddress& bd_addr,
72                                           DEV_CLASS dev_class, BD_NAME bd_name,
73                                           tHCI_REASON reason);
74 
75 /* Request SIRK verification for found member. Parameters are
76  *              BD Address of remote
77  */
78 typedef uint8_t(tBTM_SIRK_VERIFICATION_CALLBACK)(const RawAddress& bd_addr);
79 
80 struct tBTM_APPL_INFO {
81   tBTM_PIN_CALLBACK* p_pin_callback{nullptr};
82   tBTM_LINK_KEY_CALLBACK* p_link_key_callback{nullptr};
83   tBTM_AUTH_COMPLETE_CALLBACK* p_auth_complete_callback{nullptr};
84   tBTM_BOND_CANCEL_CMPL_CALLBACK* p_bond_cancel_cmpl_callback{nullptr};
85   tBTM_SP_CALLBACK* p_sp_callback{nullptr};
86   tBTM_LE_CALLBACK* p_le_callback{nullptr};
87   tBTM_LE_KEY_CALLBACK* p_le_key_callback{nullptr};
88   tBTM_SIRK_VERIFICATION_CALLBACK* p_sirk_verification_callback{nullptr};
89 };
90 
91 typedef struct {
92   void (*BTM_Sec_Init)();
93   void (*BTM_Sec_Free)();
94 
95   bool (*BTM_SecRegister)(const tBTM_APPL_INFO* p_cb_info);
96 
97   void (*BTM_BleLoadLocalKeys)(uint8_t key_type, tBTM_BLE_LOCAL_KEYS* p_key);
98 
99   // Update/Query in-memory device records
100   void (*BTM_SecAddDevice)(const RawAddress& bd_addr, const DEV_CLASS dev_class,
101                            LinkKey link_key, uint8_t key_type,
102                            uint8_t pin_length);
103   void (*BTM_SecAddBleDevice)(const RawAddress& bd_addr,
104                               tBT_DEVICE_TYPE dev_type,
105                               tBLE_ADDR_TYPE addr_type);
106 
107   bool (*BTM_SecDeleteDevice)(const RawAddress& bd_addr);
108 
109   void (*BTM_SecAddBleKey)(const RawAddress& bd_addr,
110                            tBTM_LE_KEY_VALUE* p_le_key,
111                            tBTM_LE_KEY_TYPE key_type);
112 
113   void (*BTM_SecClearSecurityFlags)(const RawAddress& bd_addr);
114 
115   tBTM_STATUS (*BTM_SetEncryption)(const RawAddress& bd_addr,
116                                    tBT_TRANSPORT transport,
117                                    tBTM_SEC_CALLBACK* p_callback,
118                                    void* p_ref_data, tBTM_BLE_SEC_ACT sec_act);
119   bool (*BTM_IsEncrypted)(const RawAddress& bd_addr, tBT_TRANSPORT transport);
120   bool (*BTM_SecIsSecurityPending)(const RawAddress& bd_addr);
121   bool (*BTM_IsLinkKeyKnown)(const RawAddress& bd_addr,
122                              tBT_TRANSPORT transport);
123 
124   // Secure service management
125   bool (*BTM_SetSecurityLevel)(bool is_originator, const char* p_name,
126                                uint8_t service_id, uint16_t sec_level,
127                                uint16_t psm, uint32_t mx_proto_id,
128                                uint32_t mx_chan_id);
129   uint8_t (*BTM_SecClrService)(uint8_t service_id);
130   uint8_t (*BTM_SecClrServiceByPsm)(uint16_t psm);
131 
132   // Pairing related APIs
133   tBTM_STATUS (*BTM_SecBond)(const RawAddress& bd_addr,
134                              tBLE_ADDR_TYPE addr_type, tBT_TRANSPORT transport,
135                              tBT_DEVICE_TYPE device_type);
136   tBTM_STATUS (*BTM_SecBondCancel)(const RawAddress& bd_addr);
137 
138   void (*BTM_RemoteOobDataReply)(tBTM_STATUS res, const RawAddress& bd_addr,
139                                  const Octet16& c, const Octet16& r);
140   void (*BTM_PINCodeReply)(const RawAddress& bd_addr, tBTM_STATUS res,
141                            uint8_t pin_len, uint8_t* p_pin);
142   void (*BTM_SecConfirmReqReply)(tBTM_STATUS res, tBT_TRANSPORT transport,
143                                  const RawAddress bd_addr);
144   void (*BTM_BleSirkConfirmDeviceReply)(const RawAddress& bd_addr, uint8_t res);
145 
146   void (*BTM_BlePasskeyReply)(const RawAddress& bd_addr, uint8_t res,
147                               uint32_t passkey);
148 
149   // other misc APIs
150   uint8_t (*BTM_GetSecurityMode)();
151 
152   // remote name request related APIs
153   // TODO: remove them from this structure
154   const char* (*BTM_SecReadDevName)(const RawAddress& bd_addr);
155   bool (*BTM_SecAddRmtNameNotifyCallback)(tBTM_RMT_NAME_CALLBACK* p_callback);
156   bool (*BTM_SecDeleteRmtNameNotifyCallback)(
157       tBTM_RMT_NAME_CALLBACK* p_callback);
158 } SecurityClientInterface;
159 
160 const SecurityClientInterface& get_security_client_interface();
161