1 /*
2  *
3  * Copyright 2023 The Android Open Source Project
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  */
18 
19 #pragma once
20 
21 #include <base/functional/callback_forward.h>
22 #include <hardware/bt_common_types.h>
23 
24 #include <cstdint>
25 #include <optional>
26 
27 #include "btm_ble_api_types.h"
28 #include "btm_ble_sec_api_types.h"
29 #include "stack/include/bt_device_type.h"
30 #include "types/raw_address.h"
31 
32 /*******************************************************************************
33  *
34  * Function         BTM_SecAddBleDevice
35  *
36  * Description      Add/modify device.  This function will be normally called
37  *                  during host startup to restore all required information
38  *                  for a LE device stored in the NVRAM.
39  *
40  * Parameters:      bd_addr          - BD address of the peer
41  *                  dev_type         - Remote device's device type.
42  *                  addr_type        - LE device address type.
43  *
44  ******************************************************************************/
45 void BTM_SecAddBleDevice(const RawAddress& bd_addr, tBT_DEVICE_TYPE dev_type,
46                          tBLE_ADDR_TYPE addr_type);
47 
48 /*******************************************************************************
49  *
50  * Function         BTM_SecAddBleKey
51  *
52  * Description      Add/modify LE device information.  This function will be
53  *                  normally called during host startup to restore all required
54  *                  information stored in the NVRAM.
55  *
56  * Parameters:      bd_addr          - BD address of the peer
57  *                  p_le_key         - LE key values.
58  *                  key_type         - LE SMP key type.
59  *
60  ******************************************************************************/
61 void BTM_SecAddBleKey(const RawAddress& bd_addr, tBTM_LE_KEY_VALUE* p_le_key,
62                       tBTM_LE_KEY_TYPE key_type);
63 
64 /** Returns local device encryption root (ER) */
65 const Octet16& BTM_GetDeviceEncRoot();
66 
67 /** Returns local device identity root (IR) */
68 const Octet16& BTM_GetDeviceIDRoot();
69 
70 /** Return local device DHK. */
71 const Octet16& BTM_GetDeviceDHK();
72 
73 /*******************************************************************************
74  *
75  * Function         BTM_SecurityGrant
76  *
77  * Description      This function is called to grant security process.
78  *
79  * Parameters       bd_addr - peer device bd address.
80  *                  res     - result of the operation tBTM_STATUS::BTM_SUCCESS if success.
81  *                            Otherwise, BTM_REPEATED_ATTEMPTS is too many
82  *                            attempts.
83  *
84  * Returns          None
85  *
86  ******************************************************************************/
87 void BTM_SecurityGrant(const RawAddress& bd_addr, tBTM_STATUS res);
88 
89 /*******************************************************************************
90  *
91  * Function         BTM_BlePasskeyReply
92  *
93  * Description      This function is called after Security Manager submitted
94  *                  passkey request to the application.
95  *
96  * Parameters:      bd_addr - Address of the device for which passkey was
97  *                            requested
98  *                  res     - result of the operation SMP_SUCCESS if success
99  *                  passkey - numeric value in the range of
100  *                               BTM_MIN_PASSKEY_VAL(0) -
101  *                               BTM_MAX_PASSKEY_VAL(999999(0xF423F)).
102  *
103  ******************************************************************************/
104 void BTM_BlePasskeyReply(const RawAddress& bd_addr, tBTM_STATUS res, uint32_t passkey);
105 
106 /*******************************************************************************
107  *
108  * Function         BTM_BleConfirmReply
109  *
110  * Description      This function is called after Security Manager submitted
111  *                  numeric comparison request to the application.
112  *
113  * Parameters:      bd_addr      - Address of the device with which numeric
114  *                                 comparison was requested
115  *                  res          - comparison result tBTM_STATUS::BTM_SUCCESS if success
116  *
117  ******************************************************************************/
118 void BTM_BleConfirmReply(const RawAddress& bd_addr, tBTM_STATUS res);
119 
120 /*******************************************************************************
121  *
122  * Function         BTM_LeOobDataReply
123  *
124  * Description      This function is called to provide the OOB data for
125  *                  SMP in response to BTM_LE_OOB_REQ_EVT
126  *
127  * Parameters:      bd_addr     - Address of the peer device
128  *                  res         - result of the operation SMP_SUCCESS if success
129  *                  p_data      - simple pairing Randomizer  C.
130  *
131  ******************************************************************************/
132 void BTM_BleOobDataReply(const RawAddress& bd_addr, tBTM_STATUS res, uint8_t len, uint8_t* p_data);
133 
134 /*******************************************************************************
135  *
136  * Function         BTM_BleSecureConnectionOobDataReply
137  *
138  * Description      This function is called to provide the OOB data for
139  *                  SMP in response to BTM_LE_OOB_REQ_EVT when secure connection
140  *                  data is available
141  *
142  * Parameters:      bd_addr     - Address of the peer device
143  *                  p_c         - pointer to Confirmation
144  *                  p_r         - pointer to Randomizer.
145  *
146  ******************************************************************************/
147 void BTM_BleSecureConnectionOobDataReply(const RawAddress& bd_addr, uint8_t* p_c, uint8_t* p_r);
148 
149 /*******************************************************************************
150  *
151  * Function         BTM_BleDataSignature
152  *
153  * Description      This function is called to sign the data using AES128 CMAC
154  *                  algorithm.
155  *
156  * Parameter        bd_addr: target device the data to be signed for.
157  *                  p_text: singing data
158  *                  len: length of the signing data
159  *                  signature: output parameter where data signature is going to
160  *                             be stored.
161  *
162  * Returns          true if signing sucessul, otherwise false.
163  *
164  ******************************************************************************/
165 bool BTM_BleDataSignature(const RawAddress& bd_addr, uint8_t* p_text, uint16_t len,
166                           BLE_SIGNATURE signature);
167 
168 /*******************************************************************************
169  *
170  * Function         BTM_BleVerifySignature
171  *
172  * Description      This function is called to verify the data signature
173  *
174  * Parameter        bd_addr: target device the data to be signed for.
175  *                  p_orig:  original data before signature.
176  *                  len: length of the signing data
177  *                  counter: counter used when doing data signing
178  *                  p_comp: signature to be compared against.
179 
180  * Returns          true if signature verified correctly; otherwise false.
181  *
182  ******************************************************************************/
183 bool BTM_BleVerifySignature(const RawAddress& bd_addr, uint8_t* p_orig, uint16_t len,
184                             uint32_t counter, uint8_t* p_comp);
185 
186 /*******************************************************************************
187  *
188  * Function         BTM_BleLoadLocalKeys
189  *
190  * Description      Local local identity key, encryption root or sign counter.
191  *
192  * Parameters:      key_type: type of key, can be BTM_BLE_KEY_TYPE_ID,
193  *                            BTM_BLE_KEY_TYPE_ER
194  *                            or BTM_BLE_KEY_TYPE_COUNTER.
195  *                  p_key: pointer to the key.
196  *
197  * Returns          non2.
198  *
199  ******************************************************************************/
200 void BTM_BleLoadLocalKeys(uint8_t key_type, tBTM_BLE_LOCAL_KEYS* p_key);
201 
202 /*******************************************************************************
203  *
204  * Function         BTM_BleGetPeerLTK
205  *
206  * Description      This function is used to get the long term key of
207  *                  a bonded peer (LE) device.
208  *
209  * Parameters:      address: address of the peer device
210  *
211  * Returns          the ltk contained in std::optional if the remote device
212  *                  is present in security database
213  *                  std::nullopt if the device is not present
214  *
215  ******************************************************************************/
216 std::optional<Octet16> BTM_BleGetPeerLTK(const RawAddress address);
217 
218 /*******************************************************************************
219  *
220  * Function         BTM_BleGetPeerIRK
221  *
222  * Description      This function is used to get the IRK of a bonded
223  *                  peer (LE) device.
224  *
225  * Parameters:      address: address of the peer device
226  *
227  * Returns          the ltk contained in std::optional if the remote device
228  *                  is present in security database
229  *                  std::nullopt if the device is not present
230  *
231  ******************************************************************************/
232 std::optional<Octet16> BTM_BleGetPeerIRK(const RawAddress address);
233 
234 /*******************************************************************************
235  *
236  * Function         BTM_BleIsLinkKeyKnown
237  *
238  * Description      This function is used to check whether the link key
239  *                  of a peer (LE) device is known or not
240  *
241  * Parameters:      address: address of the peer device
242  *
243  * Returns          true if the link key is known
244  *                  false otherwise
245  *
246  ******************************************************************************/
247 bool BTM_BleIsLinkKeyKnown(const RawAddress address);
248 
249 /*******************************************************************************
250  *
251  * Function         BTM_BleGetIdentityAddress
252  *
253  * Description      This function is called to get the identity address
254  *                  (with type) of a peer (LE) device.
255  *
256  * Parameters:      address: address of the peer device
257  *
258  * Returns          the identity address in std::optional if the remote device
259  *                  is present in security database
260  *                  std::nullopt if the device is not present
261  *
262  ******************************************************************************/
263 std::optional<tBLE_BD_ADDR> BTM_BleGetIdentityAddress(const RawAddress address);
264