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