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 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, uint8_t 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, uint8_t res,
105                          uint32_t passkey);
106 
107 /*******************************************************************************
108  *
109  * Function         BTM_BleConfirmReply
110  *
111  * Description      This function is called after Security Manager submitted
112  *                  numeric comparison request to the application.
113  *
114  * Parameters:      bd_addr      - Address of the device with which numeric
115  *                                 comparison was requested
116  *                  res          - comparison result BTM_SUCCESS if success
117  *
118  ******************************************************************************/
119 void BTM_BleConfirmReply(const RawAddress& bd_addr, uint8_t res);
120 
121 /*******************************************************************************
122  *
123  * Function         BTM_LeOobDataReply
124  *
125  * Description      This function is called to provide the OOB data for
126  *                  SMP in response to BTM_LE_OOB_REQ_EVT
127  *
128  * Parameters:      bd_addr     - Address of the peer device
129  *                  res         - result of the operation SMP_SUCCESS if success
130  *                  p_data      - simple pairing Randomizer  C.
131  *
132  ******************************************************************************/
133 void BTM_BleOobDataReply(const RawAddress& bd_addr, uint8_t res, uint8_t len,
134                          uint8_t* p_data);
135 
136 /*******************************************************************************
137  *
138  * Function         BTM_BleSecureConnectionOobDataReply
139  *
140  * Description      This function is called to provide the OOB data for
141  *                  SMP in response to BTM_LE_OOB_REQ_EVT when secure connection
142  *                  data is available
143  *
144  * Parameters:      bd_addr     - Address of the peer device
145  *                  p_c         - pointer to Confirmation
146  *                  p_r         - pointer to Randomizer.
147  *
148  ******************************************************************************/
149 void BTM_BleSecureConnectionOobDataReply(const RawAddress& bd_addr,
150                                          uint8_t* p_c, uint8_t* p_r);
151 
152 /*******************************************************************************
153  *
154  * Function         BTM_BleDataSignature
155  *
156  * Description      This function is called to sign the data using AES128 CMAC
157  *                  algorithm.
158  *
159  * Parameter        bd_addr: target device the data to be signed for.
160  *                  p_text: singing data
161  *                  len: length of the signing data
162  *                  signature: output parameter where data signature is going to
163  *                             be stored.
164  *
165  * Returns          true if signing sucessul, otherwise false.
166  *
167  ******************************************************************************/
168 bool BTM_BleDataSignature(const RawAddress& bd_addr, uint8_t* p_text,
169                           uint16_t len, BLE_SIGNATURE signature);
170 
171 /*******************************************************************************
172  *
173  * Function         BTM_BleVerifySignature
174  *
175  * Description      This function is called to verify the data signature
176  *
177  * Parameter        bd_addr: target device the data to be signed for.
178  *                  p_orig:  original data before signature.
179  *                  len: length of the signing data
180  *                  counter: counter used when doing data signing
181  *                  p_comp: signature to be compared against.
182 
183  * Returns          true if signature verified correctly; otherwise false.
184  *
185  ******************************************************************************/
186 bool BTM_BleVerifySignature(const RawAddress& bd_addr, uint8_t* p_orig,
187                             uint16_t len, uint32_t counter, uint8_t* p_comp);
188 
189 
190 
191 /*******************************************************************************
192  *
193  * Function         BTM_BleLoadLocalKeys
194  *
195  * Description      Local local identity key, encryption root or sign counter.
196  *
197  * Parameters:      key_type: type of key, can be BTM_BLE_KEY_TYPE_ID,
198  *                            BTM_BLE_KEY_TYPE_ER
199  *                            or BTM_BLE_KEY_TYPE_COUNTER.
200  *                  p_key: pointer to the key.
201 *
202  * Returns          non2.
203  *
204  ******************************************************************************/
205 void BTM_BleLoadLocalKeys(uint8_t key_type, tBTM_BLE_LOCAL_KEYS* p_key);
206 
207 /*******************************************************************************
208  *
209  * Function         BTM_BleGetPeerLTK
210  *
211  * Description      This function is used to get the long term key of
212  *                  a bonded peer (LE) device.
213  *
214  * Parameters:      address: address of the peer device
215  *
216  * Returns          the ltk contained in std::optional if the remote device
217  *                  is present in security database
218  *                  std::nullopt if the device is not present
219  *
220  ******************************************************************************/
221 std::optional<Octet16> BTM_BleGetPeerLTK(const RawAddress address);
222 
223 /*******************************************************************************
224  *
225  * Function         BTM_BleGetPeerIRK
226  *
227  * Description      This function is used to get the IRK of a bonded
228  *                  peer (LE) device.
229  *
230  * Parameters:      address: address of the peer device
231  *
232  * Returns          the ltk contained in std::optional if the remote device
233  *                  is present in security database
234  *                  std::nullopt if the device is not present
235  *
236  ******************************************************************************/
237 std::optional<Octet16> BTM_BleGetPeerIRK(const RawAddress address);
238 
239 /*******************************************************************************
240  *
241  * Function         BTM_BleIsLinkKeyKnown
242  *
243  * Description      This function is used to check whether the link key
244  *                  of a peer (LE) device is known or not
245  *
246  * Parameters:      address: address of the peer device
247  *
248  * Returns          true if the link key is known
249  *                  false otherwise
250  *
251  ******************************************************************************/
252 bool BTM_BleIsLinkKeyKnown(const RawAddress address);
253 
254 /*******************************************************************************
255  *
256  * Function         BTM_BleGetIdentityAddress
257  *
258  * Description      This function is called to get the identity address
259  *                  (with type) of a peer (LE) device.
260  *
261  * Parameters:      address: address of the peer device
262  *
263  * Returns          the identity address in std::optional if the remote device
264  *                  is present in security database
265  *                  std::nullopt if the device is not present
266  *
267  ******************************************************************************/
268 std::optional<tBLE_BD_ADDR> BTM_BleGetIdentityAddress(const RawAddress address);
269