1 /******************************************************************************
2  *
3  *  Copyright 2009-2012 Broadcom Corporation
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 #include <stdio.h>
19 #include <stdlib.h>
20 
21 #include "bt_utils.h"
22 #include "bta_api.h"
23 #include "bta_dm_ci.h"
24 #include "bta_dm_co.h"
25 #include "bta_sys.h"
26 #include "bte_appl.h"
27 #include "btif_dm.h"
28 #include "btif_storage.h"
29 #include "osi/include/osi.h"
30 
31 // tBTE_APPL_CFG.ble_io_cap is set to BTM_IO_CAP_UNKNOWN at structure
32 // initialization since btif_storage isn't ready yet for data to be fetched.
33 // This value is initialized properly during first use by fetching properly
34 // from btif_storage.
35 tBTE_APPL_CFG bte_appl_cfg = {
36     BTA_LE_AUTH_REQ_SC_MITM_BOND,  // Authentication requirements
37     BTM_IO_CAP_UNKNOWN, BTM_BLE_INITIATOR_KEY_SIZE, BTM_BLE_RESPONDER_KEY_SIZE,
38     BTM_BLE_MAX_KEY_SIZE};
39 
40 /*******************************************************************************
41  *
42  * Function         bta_dm_co_get_compress_memory
43  *
44  * Description      This callout function is executed by DM to get memory for
45  compression
46 
47  * Parameters       id  -  BTA SYS ID
48  *                  memory_p - memory return by callout
49  *                  memory_size - memory size
50  *
51  * Returns          true for success, false for fail.
52  *
53  ******************************************************************************/
bta_dm_co_get_compress_memory(UNUSED_ATTR tBTA_SYS_ID id,UNUSED_ATTR uint8_t ** memory_p,UNUSED_ATTR uint32_t * memory_size)54 bool bta_dm_co_get_compress_memory(UNUSED_ATTR tBTA_SYS_ID id,
55                                    UNUSED_ATTR uint8_t** memory_p,
56                                    UNUSED_ATTR uint32_t* memory_size) {
57   return true;
58 }
59 
60 /*******************************************************************************
61  *
62  * Function         bta_dm_co_io_req
63  *
64  * Description      This callout function is executed by DM to get IO
65  *                  capabilities of the local device for the Simple Pairing
66  *                  process.
67  *
68  * Parameters       bd_addr  - The peer device
69  *                  *p_io_cap - The local Input/Output capabilities
70  *                  *p_oob_data - true, if OOB data is available for the peer
71  *                                device.
72  *                  *p_auth_req - true, if MITM protection is required.
73  *
74  * Returns          void.
75  *
76  ******************************************************************************/
bta_dm_co_io_req(UNUSED_ATTR const RawAddress & bd_addr,tBTA_IO_CAP * p_io_cap,tBTA_OOB_DATA * p_oob_data,tBTA_AUTH_REQ * p_auth_req,bool is_orig)77 void bta_dm_co_io_req(UNUSED_ATTR const RawAddress& bd_addr,
78                       tBTA_IO_CAP* p_io_cap, tBTA_OOB_DATA* p_oob_data,
79                       tBTA_AUTH_REQ* p_auth_req, bool is_orig) {
80   btif_dm_set_oob_for_io_req(p_oob_data);
81   btif_dm_proc_io_req(bd_addr, p_io_cap, p_oob_data, p_auth_req, is_orig);
82   BTIF_TRACE_DEBUG("bta_dm_co_io_req *p_oob_data = %d", *p_oob_data);
83   BTIF_TRACE_DEBUG("bta_dm_co_io_req *p_io_cap = %d", *p_io_cap);
84   BTIF_TRACE_DEBUG("bta_dm_co_io_req *p_auth_req = %d", *p_auth_req);
85   BTIF_TRACE_DEBUG("bta_dm_co_io_req is_orig = %d", is_orig);
86 }
87 
88 /*******************************************************************************
89  *
90  * Function         bta_dm_co_io_rsp
91  *
92  * Description      This callout function is executed by DM to report IO
93  *                  capabilities of the peer device for the Simple Pairing
94  *                  process.
95  *
96  * Parameters       bd_addr  - The peer device
97  *                  io_cap - The remote Input/Output capabilities
98  *                  oob_data - true, if OOB data is available for the peer
99  *                             device.
100  *                  auth_req - true, if MITM protection is required.
101  *
102  * Returns          void.
103  *
104  ******************************************************************************/
bta_dm_co_io_rsp(const RawAddress & bd_addr,tBTA_IO_CAP io_cap,tBTA_OOB_DATA oob_data,tBTA_AUTH_REQ auth_req)105 void bta_dm_co_io_rsp(const RawAddress& bd_addr, tBTA_IO_CAP io_cap,
106                       tBTA_OOB_DATA oob_data, tBTA_AUTH_REQ auth_req) {
107   btif_dm_proc_io_rsp(bd_addr, io_cap, oob_data, auth_req);
108 }
109 
110 /*******************************************************************************
111  *
112  * Function         bta_dm_co_lk_upgrade
113  *
114  * Description      This callout function is executed by DM to check if the
115  *                  platform wants allow link key upgrade
116  *
117  * Parameters       bd_addr  - The peer device
118  *                  *p_upgrade - true, if link key upgrade is desired.
119  *
120  * Returns          void.
121  *
122  ******************************************************************************/
bta_dm_co_lk_upgrade(UNUSED_ATTR const RawAddress & bd_addr,UNUSED_ATTR bool * p_upgrade)123 void bta_dm_co_lk_upgrade(UNUSED_ATTR const RawAddress& bd_addr,
124                           UNUSED_ATTR bool* p_upgrade) {}
125 
126 /*******************************************************************************
127  *
128  * Function         bta_dm_co_loc_oob
129  *
130  * Description      This callout function is executed by DM to report the OOB
131  *                  data of the local device for the Simple Pairing process
132  *
133  * Parameters       valid - true, if the local OOB data is retrieved from LM
134  *                  c     - Simple Pairing Hash C
135  *                  r     - Simple Pairing Randomnizer R
136  *
137  * Returns          void.
138  *
139  ******************************************************************************/
bta_dm_co_loc_oob(bool valid,const Octet16 & c,const Octet16 & r)140 void bta_dm_co_loc_oob(bool valid, const Octet16& c, const Octet16& r) {
141   BTIF_TRACE_DEBUG("bta_dm_co_loc_oob, valid = %d", valid);
142 #ifdef BTIF_DM_OOB_TEST
143   btif_dm_proc_loc_oob(valid, c, r);
144 #endif
145 }
146 
147 /*******************************************************************************
148  *
149  * Function         bta_dm_co_rmt_oob
150  *
151  * Description      This callout function is executed by DM to request the OOB
152  *                  data for the remote device for the Simple Pairing process
153  *                  Need to call bta_dm_ci_rmt_oob() in response
154  *
155  * Parameters       bd_addr  - The peer device
156  *
157  * Returns          void.
158  *
159  ******************************************************************************/
bta_dm_co_rmt_oob(const RawAddress & bd_addr)160 void bta_dm_co_rmt_oob(const RawAddress& bd_addr) {
161   Octet16 c;
162   Octet16 r;
163   bool result = false;
164 
165 #ifdef BTIF_DM_OOB_TEST
166   result = btif_dm_proc_rmt_oob(bd_addr, &c, &r);
167 #endif
168 
169   BTIF_TRACE_DEBUG("bta_dm_co_rmt_oob: result=%d", result);
170   bta_dm_ci_rmt_oob(result, bd_addr, c, r);
171 }
172 
173 /*******************************************************************************
174  *
175  * Function         bta_dm_co_le_io_key_req
176  *
177  * Description      This callout function is executed by DM to get BLE key
178  *                  information
179  *                  before SMP pairing gets going.
180  *
181  * Parameters       bd_addr  - The peer device
182  *                  *p_max_key_size - max key size local device supported.
183  *                  *p_init_key - initiator keys.
184  *                  *p_resp_key - responder keys.
185  *
186  * Returns          void.
187  *
188  ******************************************************************************/
bta_dm_co_le_io_key_req(UNUSED_ATTR const RawAddress & bd_addr,uint8_t * p_max_key_size,tBTA_LE_KEY_TYPE * p_init_key,tBTA_LE_KEY_TYPE * p_resp_key)189 void bta_dm_co_le_io_key_req(UNUSED_ATTR const RawAddress& bd_addr,
190                              uint8_t* p_max_key_size,
191                              tBTA_LE_KEY_TYPE* p_init_key,
192                              tBTA_LE_KEY_TYPE* p_resp_key) {
193   BTIF_TRACE_ERROR("##################################");
194   BTIF_TRACE_ERROR("bta_dm_co_le_io_key_req: only setting max size to 16");
195   BTIF_TRACE_ERROR("##################################");
196   *p_max_key_size = 16;
197   *p_init_key = *p_resp_key =
198       (BTA_LE_KEY_PENC | BTA_LE_KEY_PID | BTA_LE_KEY_PCSRK | BTA_LE_KEY_LENC |
199        BTA_LE_KEY_LID | BTA_LE_KEY_LCSRK);
200 }
201 
202 /*******************************************************************************
203  *
204  * Function         bta_dm_co_ble_local_key_reload
205  *
206  * Description      This callout function is to load the local BLE keys if
207  *                  available on the device.
208  *
209  * Parameters       none
210  *
211  * Returns          void.
212  *
213  ******************************************************************************/
bta_dm_co_ble_load_local_keys(tBTA_DM_BLE_LOCAL_KEY_MASK * p_key_mask,Octet16 * p_er,tBTA_BLE_LOCAL_ID_KEYS * p_id_keys)214 void bta_dm_co_ble_load_local_keys(tBTA_DM_BLE_LOCAL_KEY_MASK* p_key_mask,
215                                    Octet16* p_er,
216                                    tBTA_BLE_LOCAL_ID_KEYS* p_id_keys) {
217   BTIF_TRACE_DEBUG("##################################");
218   BTIF_TRACE_DEBUG(
219       "bta_dm_co_ble_load_local_keys:  Load local keys if any are persisted");
220   BTIF_TRACE_DEBUG("##################################");
221   btif_dm_get_ble_local_keys(p_key_mask, p_er, p_id_keys);
222 }
223 
224 /*******************************************************************************
225  *
226  * Function         bta_dm_co_ble_io_req
227  *
228  * Description      This callout function is executed by DM to get BLE IO
229  *                  capabilities before SMP pairing gets going.
230  *
231  * Parameters       bd_addr  - The peer device
232  *                  *p_io_cap - The local Input/Output capabilities
233  *                  *p_oob_data - true, if OOB data is available for the peer
234  *                                device.
235  *                  *p_auth_req -  Auth request setting (Bonding and MITM
236  *                                 required or not)
237  *                  *p_max_key_size - max key size local device supported.
238  *                  *p_init_key - initiator keys.
239  *                  *p_resp_key - responder keys.
240  *
241  * Returns          void.
242  *
243  ******************************************************************************/
bta_dm_co_ble_io_req(const RawAddress & bd_addr,tBTA_IO_CAP * p_io_cap,tBTA_OOB_DATA * p_oob_data,tBTA_LE_AUTH_REQ * p_auth_req,uint8_t * p_max_key_size,tBTA_LE_KEY_TYPE * p_init_key,tBTA_LE_KEY_TYPE * p_resp_key)244 void bta_dm_co_ble_io_req(const RawAddress& bd_addr, tBTA_IO_CAP* p_io_cap,
245                           tBTA_OOB_DATA* p_oob_data,
246                           tBTA_LE_AUTH_REQ* p_auth_req, uint8_t* p_max_key_size,
247                           tBTA_LE_KEY_TYPE* p_init_key,
248                           tBTA_LE_KEY_TYPE* p_resp_key) {
249   bte_appl_cfg.ble_io_cap = btif_storage_get_local_io_caps_ble();
250 
251   /* Retrieve the properties from file system if possible */
252   tBTE_APPL_CFG nv_config;
253   if (btif_dm_get_smp_config(&nv_config)) bte_appl_cfg = nv_config;
254 
255   /* *p_auth_req by default is false for devices with NoInputNoOutput; true for
256    * other devices. */
257 
258   if (bte_appl_cfg.ble_auth_req)
259     *p_auth_req = bte_appl_cfg.ble_auth_req |
260                   (bte_appl_cfg.ble_auth_req & 0x04) | ((*p_auth_req) & 0x04);
261 
262   /* if OOB is not supported, this call-out function does not need to do
263    * anything
264    * otherwise, look for the OOB data associated with the address and set
265    * *p_oob_data accordingly.
266    * If the answer can not be obtained right away,
267    * set *p_oob_data to BTA_OOB_UNKNOWN and call bta_dm_ci_io_req() when the
268    * answer is available.
269    */
270 
271   btif_dm_set_oob_for_le_io_req(bd_addr, p_oob_data, p_auth_req);
272 
273   if (bte_appl_cfg.ble_io_cap <= 4) *p_io_cap = bte_appl_cfg.ble_io_cap;
274 
275   if (bte_appl_cfg.ble_init_key <= BTM_BLE_INITIATOR_KEY_SIZE)
276     *p_init_key = bte_appl_cfg.ble_init_key;
277 
278   if (bte_appl_cfg.ble_resp_key <= BTM_BLE_RESPONDER_KEY_SIZE)
279     *p_resp_key = bte_appl_cfg.ble_resp_key;
280 
281   if (bte_appl_cfg.ble_max_key_size > 7 && bte_appl_cfg.ble_max_key_size <= 16)
282     *p_max_key_size = bte_appl_cfg.ble_max_key_size;
283 }
284