1 /******************************************************************************
2 *
3 * Copyright (C) 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 "bta/bta_api.h"
22 #include "bta/bta_sys.h"
23 #include "bta/bta_dm_co.h"
24 #include "bta/bta_dm_ci.h"
25 #include "btc/btc_dm.h"
26 #if (defined(BTIF_INCLUDED) && BTIF_INCLUDED == TRUE)
27 #include "common/bt_defs.h"
28 #if (BTM_OOB_INCLUDED == TRUE)
29 #include "btif_dm.h"
30 #endif
31 #endif /* #if (defined(BTIF_INCLUDED) && BTIF_INCLUDED == TRUE) */
32 #if (defined BLE_INCLUDED && BLE_INCLUDED == TRUE && SMP_INCLUDED == TRUE)
33 #include "common/bte_appl.h"
34
35 #define BTM_BLE_ONLY_ACCEPT_SPECIFIED_SEC_AUTH_DISABLE 0
36 #define BTM_BLE_ONLY_ACCEPT_SPECIFIED_SEC_AUTH_ENABLE 1
37
38 #define BTM_BLE_OOB_DISABLE 0
39 #define BTM_BLE_OOB_ENABLE 1
40
41 tBTE_APPL_CFG bte_appl_cfg = {
42 #if SMP_INCLUDED == TRUE
43 BTA_LE_AUTH_REQ_SC_MITM_BOND, // Authentication requirements
44 #else
45 BTM_AUTH_SPGB_YES, // Authentication requirements
46 #endif
47 BTM_LOCAL_IO_CAPS_BLE,
48 BTM_BLE_INITIATOR_KEY_SIZE,
49 BTM_BLE_RESPONDER_KEY_SIZE,
50 BTM_BLE_MAX_KEY_SIZE,
51 BTM_BLE_MIN_KEY_SIZE,
52 BTM_BLE_ONLY_ACCEPT_SPECIFIED_SEC_AUTH_DISABLE,
53 BTM_BLE_OOB_DISABLE,
54 };
55
bta_dm_co_security_param_init(void)56 void bta_dm_co_security_param_init(void)
57 {
58 bte_appl_cfg.ble_auth_req = BTA_LE_AUTH_REQ_SC_MITM_BOND;
59 bte_appl_cfg.ble_io_cap = BTM_LOCAL_IO_CAPS_BLE;
60 bte_appl_cfg.ble_init_key = BTM_BLE_INITIATOR_KEY_SIZE;
61 bte_appl_cfg.ble_resp_key = BTM_BLE_RESPONDER_KEY_SIZE;
62 bte_appl_cfg.ble_max_key_size = BTM_BLE_MAX_KEY_SIZE;
63 bte_appl_cfg.ble_min_key_size = BTM_BLE_MIN_KEY_SIZE;
64 bte_appl_cfg.ble_accept_auth_enable = BTM_BLE_ONLY_ACCEPT_SPECIFIED_SEC_AUTH_DISABLE;
65 bte_appl_cfg.oob_support = BTM_BLE_OOB_DISABLE;
66 };
67 #endif
68
69 #if (defined CLASSIC_BT_INCLUDED && CLASSIC_BT_INCLUDED == TRUE && BT_SSP_INCLUDED == TRUE)
70 #include "common/bte_appl.h"
71 #include "btm_int.h"
72 tBTE_BT_APPL_CFG bte_bt_appl_cfg = {
73 0, //Todo, Authentication requirements
74 BTM_LOCAL_IO_CAPS,
75 NULL, //Todo, OOB data
76 };
77 #endif
78
79 /*******************************************************************************
80 **
81 ** Function bta_dm_co_get_compress_memory
82 **
83 ** Description This callout function is executed by DM to get memory for compression
84
85 ** Parameters id - BTA SYS ID
86 ** memory_p - memory return by callout
87 ** memory_size - memory size
88 **
89 ** Returns TRUE for success, FALSE for fail.
90 **
91 *******************************************************************************/
bta_dm_co_get_compress_memory(tBTA_SYS_ID id,UINT8 ** memory_p,UINT32 * memory_size)92 BOOLEAN bta_dm_co_get_compress_memory(tBTA_SYS_ID id, UINT8 **memory_p, UINT32 *memory_size)
93 {
94 UNUSED(id);
95 UNUSED(memory_p);
96 UNUSED(memory_size);
97 return TRUE;
98 }
99
100 /*******************************************************************************
101 **
102 ** Function bta_dm_co_bt_set_io_cap
103 **
104 ** Description This function is used to set IO capabilities
105 **
106 ** Parameters bt_io_cap - IO capabilities
107 **
108 ** @return - ESP_BT_STATUS_SUCCESS : success
109 ** - other : failed
110 **
111 *******************************************************************************/
bta_dm_co_bt_set_io_cap(UINT8 bt_io_cap)112 esp_err_t bta_dm_co_bt_set_io_cap(UINT8 bt_io_cap)
113 {
114 esp_err_t ret = ESP_BT_STATUS_SUCCESS;
115 #if (BT_SSP_INCLUDED == TRUE)
116 if(bt_io_cap < BTM_IO_CAP_MAX ) {
117 bte_bt_appl_cfg.bt_io_cap = bt_io_cap;
118 btm_cb.devcb.loc_io_caps = bt_io_cap;
119 ret = ESP_BT_STATUS_SUCCESS;
120 } else {
121 ret = ESP_BT_STATUS_FAIL;
122 APPL_TRACE_ERROR("%s error:Invalid io cap value.",__func__);
123 }
124 #endif ///BT_SSP_INCLUDED == TRUE
125 return ret;
126 }
127
128 /*******************************************************************************
129 **
130 ** Function bta_dm_co_io_req
131 **
132 ** Description This callout function is executed by DM to get IO capabilities
133 ** of the local device for the Simple Pairing process
134 **
135 ** Parameters bd_addr - The peer device
136 ** *p_io_cap - The local Input/Output capabilities
137 ** *p_oob_data - TRUE, if OOB data is available for the peer device.
138 ** *p_auth_req - TRUE, if MITM protection is required.
139 **
140 ** Returns void.
141 **
142 *******************************************************************************/
bta_dm_co_io_req(BD_ADDR bd_addr,tBTA_IO_CAP * p_io_cap,tBTA_OOB_DATA * p_oob_data,tBTA_AUTH_REQ * p_auth_req,BOOLEAN is_orig)143 void bta_dm_co_io_req(BD_ADDR bd_addr, tBTA_IO_CAP *p_io_cap, tBTA_OOB_DATA *p_oob_data,
144 tBTA_AUTH_REQ *p_auth_req, BOOLEAN is_orig)
145 {
146 UNUSED(bd_addr);
147 #if (defined(BTIF_INCLUDED) && BTIF_INCLUDED == TRUE)
148 #if (BTM_OOB_INCLUDED == TRUE)
149 btif_dm_set_oob_for_io_req(p_oob_data);
150 #endif
151 btif_dm_proc_io_req(bd_addr, p_io_cap, p_oob_data, p_auth_req, is_orig);
152 #else
153 BTIF_TRACE_DEBUG("bta_dm_co_io_req: func not ported\n");
154 #endif /* #if (defined(BTIF_INCLUDED) && BTIF_INCLUDED == TRUE) */
155 BTIF_TRACE_DEBUG("bta_dm_co_io_req *p_oob_data = %d", *p_oob_data);
156 BTIF_TRACE_DEBUG("bta_dm_co_io_req *p_io_cap = %d", *p_io_cap);
157 BTIF_TRACE_DEBUG("bta_dm_co_io_req *p_auth_req = %d", *p_auth_req);
158 BTIF_TRACE_DEBUG("bta_dm_co_io_req is_orig = %d", is_orig);
159 }
160
161 /*******************************************************************************
162 **
163 ** Function bta_dm_co_io_rsp
164 **
165 ** Description This callout function is executed by DM to report IO capabilities
166 ** of the peer device for the Simple Pairing process
167 **
168 ** Parameters bd_addr - The peer device
169 ** io_cap - The remote Input/Output capabilities
170 ** oob_data - TRUE, if OOB data is available for the peer device.
171 ** auth_req - TRUE, if MITM protection is required.
172 **
173 ** Returns void.
174 **
175 *******************************************************************************/
bta_dm_co_io_rsp(BD_ADDR bd_addr,tBTA_IO_CAP io_cap,tBTA_OOB_DATA oob_data,tBTA_AUTH_REQ auth_req)176 void bta_dm_co_io_rsp(BD_ADDR bd_addr, tBTA_IO_CAP io_cap,
177 tBTA_OOB_DATA oob_data, tBTA_AUTH_REQ auth_req)
178 {
179 #if (defined(BTIF_INCLUDED) && BTIF_INCLUDED == TRUE)
180 btif_dm_proc_io_rsp(bd_addr, io_cap, oob_data, auth_req);
181 #else
182 BTIF_TRACE_DEBUG("bta_dm_co_io_rsp: func not ported\n");
183 #endif /* #if (defined(BTIF_INCLUDED) && BTIF_INCLUDED == TRUE) */
184 }
185
186 /*******************************************************************************
187 **
188 ** Function bta_dm_co_lk_upgrade
189 **
190 ** Description This callout function is executed by DM to check if the
191 ** platform wants allow link key upgrade
192 **
193 ** Parameters bd_addr - The peer device
194 ** *p_upgrade - TRUE, if link key upgrade is desired.
195 **
196 ** Returns void.
197 **
198 *******************************************************************************/
bta_dm_co_lk_upgrade(BD_ADDR bd_addr,BOOLEAN * p_upgrade)199 void bta_dm_co_lk_upgrade(BD_ADDR bd_addr, BOOLEAN *p_upgrade )
200 {
201 UNUSED(bd_addr);
202 UNUSED(p_upgrade);
203 }
204
205 #if (BTM_OOB_INCLUDED == TRUE)
206 /*******************************************************************************
207 **
208 ** Function bta_dm_co_loc_oob
209 **
210 ** Description This callout function is executed by DM to report the OOB
211 ** data of the local device for the Simple Pairing process
212 **
213 ** Parameters valid - TRUE, if the local OOB data is retrieved from LM
214 ** c - Simple Pairing Hash C
215 ** r - Simple Pairing Randomnizer R
216 **
217 ** Returns void.
218 **
219 *******************************************************************************/
bta_dm_co_loc_oob(BOOLEAN valid,BT_OCTET16 c,BT_OCTET16 r)220 void bta_dm_co_loc_oob(BOOLEAN valid, BT_OCTET16 c, BT_OCTET16 r)
221 {
222 #if (defined(BTIF_INCLUDED) && BTIF_INCLUDED == TRUE)
223 BTIF_TRACE_DEBUG("bta_dm_co_loc_oob, valid = %d", valid);
224 #ifdef BTIF_DM_OOB_TEST
225 btif_dm_proc_loc_oob(valid, c, r);
226 #endif
227 #else
228 BTIF_TRACE_DEBUG("bta_dm_co_loc_oob: func not ported\n");
229 #endif /* #if (defined(BTIF_INCLUDED) && BTIF_INCLUDED == TRUE) */
230 }
231
232 /*******************************************************************************
233 **
234 ** Function bta_dm_co_rmt_oob
235 **
236 ** Description This callout function is executed by DM to request the OOB
237 ** data for the remote device for the Simple Pairing process
238 ** Need to call bta_dm_ci_rmt_oob() in response
239 **
240 ** Parameters bd_addr - The peer device
241 **
242 ** Returns void.
243 **
244 *******************************************************************************/
bta_dm_co_rmt_oob(BD_ADDR bd_addr)245 void bta_dm_co_rmt_oob(BD_ADDR bd_addr)
246 {
247 BT_OCTET16 p_c = {0};
248 BT_OCTET16 p_r = {0};
249 BOOLEAN result = FALSE;
250
251 #ifdef BTIF_DM_OOB_TEST
252 #if (defined(BTIF_INCLUDED) && BTIF_INCLUDED == TRUE)
253 result = btif_dm_proc_rmt_oob(bd_addr, p_c, p_r);
254 #else
255 BTIF_TRACE_DEBUG("bta_dm_rmt_oob: func not ported\n");
256 #endif /* #if (defined(BTIF_INCLUDED) && BTIF_INCLUDED == TRUE) */
257 #endif
258
259 BTIF_TRACE_DEBUG("bta_dm_co_rmt_oob: result=%d", result);
260 bta_dm_ci_rmt_oob(result, bd_addr, p_c, p_r);
261 }
262
263 #endif /* BTM_OOB_INCLUDED */
264
265
266 // REMOVE FOR BLUEDROID ?
267
268 #if (defined BLE_INCLUDED && BLE_INCLUDED == TRUE)
269 /*******************************************************************************
270 **
271 ** Function bta_dm_co_le_io_key_req
272 **
273 ** Description This callout function is executed by DM to get BLE key information
274 ** before SMP pairing gets going.
275 **
276 ** Parameters bd_addr - The peer device
277 ** *p_max_key_size - max key size local device supported.
278 ** *p_init_key - initiator keys.
279 ** *p_resp_key - responder keys.
280 **
281 ** Returns void.
282 **
283 *******************************************************************************/
bta_dm_co_le_io_key_req(BD_ADDR bd_addr,UINT8 * p_max_key_size,tBTA_LE_KEY_TYPE * p_init_key,tBTA_LE_KEY_TYPE * p_resp_key)284 void bta_dm_co_le_io_key_req(BD_ADDR bd_addr, UINT8 *p_max_key_size,
285 tBTA_LE_KEY_TYPE *p_init_key,
286 tBTA_LE_KEY_TYPE *p_resp_key )
287 {
288 UNUSED(bd_addr);
289 #if (SMP_INCLUDED == TRUE)
290 BTIF_TRACE_DEBUG("##################################");
291 BTIF_TRACE_DEBUG("bta_dm_co_le_io_key_req: only setting max size to 16");
292 BTIF_TRACE_DEBUG("##################################");
293 *p_max_key_size = 16;
294 *p_init_key = *p_resp_key =
295 (BTA_LE_KEY_PENC | BTA_LE_KEY_PID | BTA_LE_KEY_PCSRK | BTA_LE_KEY_LENC | BTA_LE_KEY_LID | BTA_LE_KEY_LCSRK);
296 #endif ///SMP_INCLUDED == TRUE
297 }
298
299
300 /*******************************************************************************
301 **
302 ** Function bta_dm_co_ble_local_key_reload
303 **
304 ** Description This callout function is to load the local BLE keys if available
305 ** on the device.
306 **
307 ** Parameters none
308 **
309 ** Returns void.
310 **
311 *******************************************************************************/
bta_dm_co_ble_load_local_keys(tBTA_DM_BLE_LOCAL_KEY_MASK * p_key_mask,BT_OCTET16 er,tBTA_BLE_LOCAL_ID_KEYS * p_id_keys)312 void bta_dm_co_ble_load_local_keys(tBTA_DM_BLE_LOCAL_KEY_MASK *p_key_mask, BT_OCTET16 er,
313 tBTA_BLE_LOCAL_ID_KEYS *p_id_keys)
314 {
315 #if (defined(BTIF_INCLUDED) && BTIF_INCLUDED == TRUE)
316 BTIF_TRACE_DEBUG("##################################");
317 BTIF_TRACE_DEBUG("bta_dm_co_ble_load_local_keys: Load local keys if any are persisted");
318 BTIF_TRACE_DEBUG("##################################");
319 btif_dm_get_ble_local_keys( p_key_mask, er, p_id_keys);
320 #endif ///defined(BTIF_INCLUDED) && BTIF_INCLUDED == TRUE
321 #if (SMP_INCLUDED == TRUE)
322 btc_dm_get_ble_local_keys( p_key_mask, er, p_id_keys);
323 #endif ///SMP_INCLUDED == TRUE
324 }
325
326 /*******************************************************************************
327 **
328 ** Function bta_dm_co_ble_io_req
329 **
330 ** Description This callout function is executed by DM to get BLE IO capabilities
331 ** before SMP pairing gets going.
332 **
333 ** Parameters bd_addr - The peer device
334 ** *p_io_cap - The local Input/Output capabilities
335 ** *p_oob_data - TRUE, if OOB data is available for the peer device.
336 ** *p_auth_req - Auth request setting (Bonding and MITM required or not)
337 ** *p_max_key_size - max key size local device supported.
338 ** *p_init_key - initiator keys.
339 ** *p_resp_key - responder keys.
340 **
341 ** Returns void.
342 **
343 *******************************************************************************/
bta_dm_co_ble_io_req(BD_ADDR bd_addr,tBTA_IO_CAP * p_io_cap,tBTA_OOB_DATA * p_oob_data,tBTA_LE_AUTH_REQ * p_auth_req,UINT8 * p_max_key_size,tBTA_LE_KEY_TYPE * p_init_key,tBTA_LE_KEY_TYPE * p_resp_key)344 void bta_dm_co_ble_io_req(BD_ADDR bd_addr, tBTA_IO_CAP *p_io_cap,
345 tBTA_OOB_DATA *p_oob_data,
346 tBTA_LE_AUTH_REQ *p_auth_req,
347 UINT8 *p_max_key_size,
348 tBTA_LE_KEY_TYPE *p_init_key,
349 tBTA_LE_KEY_TYPE *p_resp_key )
350 {
351 #if (SMP_INCLUDED == TRUE)
352 UNUSED(bd_addr);
353 /* if OOB is not supported, this call-out function does not need to do anything
354 * otherwise, look for the OOB data associated with the address and set *p_oob_data accordingly
355 * If the answer can not be obtained right away,
356 * set *p_oob_data to BTA_OOB_UNKNOWN and call bta_dm_ci_io_req() when the answer is available */
357
358 *p_oob_data = bte_appl_cfg.oob_support;
359
360 /* *p_auth_req by default is FALSE for devices with NoInputNoOutput; TRUE for other devices. */
361
362 *p_auth_req = bte_appl_cfg.ble_auth_req | (bte_appl_cfg.ble_auth_req & BTA_LE_AUTH_REQ_MITM) | ((*p_auth_req) & BTA_LE_AUTH_REQ_MITM);
363
364 if (bte_appl_cfg.ble_io_cap <= 4) {
365 *p_io_cap = bte_appl_cfg.ble_io_cap;
366 }
367
368 if (bte_appl_cfg.ble_init_key <= BTM_BLE_INITIATOR_KEY_SIZE) {
369 *p_init_key = bte_appl_cfg.ble_init_key;
370 }
371
372 if (bte_appl_cfg.ble_resp_key <= BTM_BLE_RESPONDER_KEY_SIZE) {
373 *p_resp_key = bte_appl_cfg.ble_resp_key;
374 }
375
376 if (bte_appl_cfg.ble_max_key_size >= 7 && bte_appl_cfg.ble_max_key_size <= 16) {
377 *p_max_key_size = bte_appl_cfg.ble_max_key_size;
378 }
379 #endif ///SMP_INCLUDED == TRUE
380 }
381
bta_dm_co_ble_set_io_cap(UINT8 ble_io_cap)382 void bta_dm_co_ble_set_io_cap(UINT8 ble_io_cap)
383 {
384 #if (SMP_INCLUDED == TRUE)
385 if(ble_io_cap < BTM_IO_CAP_MAX ) {
386 bte_appl_cfg.ble_io_cap = ble_io_cap;
387 } else {
388 APPL_TRACE_ERROR("%s error:Invalid io cap value.",__func__);
389 }
390 #endif ///SMP_INCLUDED == TRUE
391 }
392
bta_dm_co_ble_set_auth_req(UINT8 ble_auth_req)393 void bta_dm_co_ble_set_auth_req(UINT8 ble_auth_req)
394 {
395 #if (SMP_INCLUDED == TRUE)
396 bte_appl_cfg.ble_auth_req = ble_auth_req;
397 #endif ///SMP_INCLUDED == TRUE
398 }
399
bta_dm_co_ble_set_init_key_req(UINT8 init_key)400 void bta_dm_co_ble_set_init_key_req(UINT8 init_key)
401 {
402 #if (SMP_INCLUDED == TRUE)
403 init_key &= 0x0f; // 4~7bit reservd, only used the 0~3bit
404 bte_appl_cfg.ble_init_key = init_key;
405 #endif ///SMP_INCLUDED == TRUE
406 }
407
bta_dm_co_ble_set_rsp_key_req(UINT8 rsp_key)408 void bta_dm_co_ble_set_rsp_key_req(UINT8 rsp_key)
409 {
410 #if (SMP_INCLUDED == TRUE)
411 rsp_key &= 0x0f; // 4~7bit reservd, only used the 0~3bit
412 bte_appl_cfg.ble_resp_key = rsp_key;
413 #endif ///SMP_INCLUDED == TRUE
414 }
415
bta_dm_co_ble_set_max_key_size(UINT8 ble_key_size)416 void bta_dm_co_ble_set_max_key_size(UINT8 ble_key_size)
417 {
418 #if (SMP_INCLUDED == TRUE)
419 if(ble_key_size >= bte_appl_cfg.ble_min_key_size && ble_key_size <= BTM_BLE_MAX_KEY_SIZE) {
420 bte_appl_cfg.ble_max_key_size = ble_key_size;
421 } else {
422 APPL_TRACE_ERROR("%s error:Invalid key size value, key_size =%d",__func__, ble_key_size);
423 }
424 #endif ///SMP_INCLUDED == TRUE
425 }
426
bta_dm_co_ble_set_min_key_size(UINT8 ble_key_size)427 void bta_dm_co_ble_set_min_key_size(UINT8 ble_key_size)
428 {
429 #if (SMP_INCLUDED == TRUE)
430 if(ble_key_size >= BTM_BLE_MIN_KEY_SIZE && ble_key_size <= bte_appl_cfg.ble_max_key_size) {
431 bte_appl_cfg.ble_min_key_size = ble_key_size;
432 } else {
433 APPL_TRACE_ERROR("%s error:Invalid key size value, key_size =%d",__func__, ble_key_size);
434 }
435 #endif ///SMP_INCLUDED == TRUE
436 }
437
bta_dm_co_ble_set_accept_auth_enable(UINT8 enable)438 void bta_dm_co_ble_set_accept_auth_enable(UINT8 enable)
439 {
440 #if (SMP_INCLUDED == TRUE)
441 if (enable) {
442 enable = BTM_BLE_ONLY_ACCEPT_SPECIFIED_SEC_AUTH_ENABLE;
443 }
444 bte_appl_cfg.ble_accept_auth_enable = enable;
445 #endif ///SMP_INCLUDED == TRUE
446 }
447
bta_dm_co_ble_get_accept_auth_enable(void)448 UINT8 bta_dm_co_ble_get_accept_auth_enable(void)
449 {
450 #if (SMP_INCLUDED == TRUE)
451 return bte_appl_cfg.ble_accept_auth_enable;
452 #endif ///SMP_INCLUDED == TRUE
453 return 0;
454 }
455
bta_dm_co_ble_get_auth_req(void)456 UINT8 bta_dm_co_ble_get_auth_req(void)
457 {
458 #if (SMP_INCLUDED == TRUE)
459 return bte_appl_cfg.ble_auth_req;
460 #endif ///SMP_INCLUDED == TRUE
461 return 0;
462 }
463
bta_dm_co_ble_oob_support(UINT8 enable)464 void bta_dm_co_ble_oob_support(UINT8 enable)
465 {
466 #if (SMP_INCLUDED == TRUE)
467 if (enable) {
468 bte_appl_cfg.oob_support = BTM_BLE_OOB_ENABLE;
469 } else {
470 bte_appl_cfg.oob_support = BTM_BLE_OOB_DISABLE;
471 }
472 #endif ///SMP_INCLUDED == TRUE
473 }
474
475 #endif
476