blob: 518531b4ac387c14770b9cc2e90d0e38c1779186 [file] [log] [blame]
The Android Open Source Project5738f832012-12-12 16:00:35 -08001/******************************************************************************
2 *
3 * Copyright (C) 1999-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
19/******************************************************************************
20 *
21 * This file contains functions for BLE device control utilities, and LE
22 * security functions.
23 *
24 ******************************************************************************/
Mike J. Chenbf9a8aa2014-02-11 13:51:29 -080025#include "bt_target.h"
26
27#if BLE_INCLUDED == TRUE
The Android Open Source Project5738f832012-12-12 16:00:35 -080028
29#include <string.h>
30
31#include "bt_types.h"
32#include "hcimsgs.h"
33#include "btu.h"
34#include "btm_int.h"
35#include "btm_ble_api.h"
36#include "smp_api.h"
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -080037#include "l2c_int.h"
38#include "gap_api.h"
Mike J. Chen5cd8bff2014-01-31 18:16:59 -080039#include "bt_utils.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080040
Satya Calloji0943c102014-05-12 09:13:02 -070041#if (defined BLE_VND_INCLUDED && BLE_VND_INCLUDED == TRUE)
42#include "vendor_ble.h"
43#endif
44
The Android Open Source Project5738f832012-12-12 16:00:35 -080045#if SMP_INCLUDED == TRUE
46extern BOOLEAN AES_CMAC ( BT_OCTET16 key, UINT8 *input, UINT16 length, UINT16 tlen, UINT8 *p_signature);
47extern void smp_link_encrypted(BD_ADDR bda, UINT8 encr_enable);
48extern BOOLEAN smp_proc_ltk_request(BD_ADDR bda);
49#endif
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -080050extern void gatt_notify_enc_cmpl(BD_ADDR bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -080051
52/*******************************************************************************/
53/* External Function to be called by other modules */
54/*******************************************************************************/
55/********************************************************
56**
57** Function BTM_SecAddBleDevice
58**
59** Description Add/modify device. This function will be normally called
60** during host startup to restore all required information
61** for a LE device stored in the NVRAM.
62**
63** Parameters: bd_addr - BD address of the peer
64** bd_name - Name of the peer device. NULL if unknown.
65** dev_type - Remote device's device type.
66** addr_type - LE device address type.
67**
68** Returns TRUE if added OK, else FALSE
69**
70*******************************************************************************/
71BOOLEAN BTM_SecAddBleDevice (BD_ADDR bd_addr, BD_NAME bd_name, tBT_DEVICE_TYPE dev_type,
72 tBLE_ADDR_TYPE addr_type)
73{
The Android Open Source Project5738f832012-12-12 16:00:35 -080074 tBTM_SEC_DEV_REC *p_dev_rec;
75 UINT8 i = 0;
76 tBTM_INQ_INFO *p_info=NULL;
77
Sharvil Nanavati83c52562014-05-04 00:46:57 -070078 BTM_TRACE_DEBUG ("BTM_SecAddBleDevice dev_type=0x%x", dev_type);
The Android Open Source Project5738f832012-12-12 16:00:35 -080079 p_dev_rec = btm_find_dev (bd_addr);
80
81 if (!p_dev_rec)
82 {
Sharvil Nanavati83c52562014-05-04 00:46:57 -070083 BTM_TRACE_DEBUG("Add a new device");
The Android Open Source Project5738f832012-12-12 16:00:35 -080084
85 /* There is no device record, allocate one.
86 * If we can not find an empty spot for this one, let it fail. */
87 for (i = 0; i < BTM_SEC_MAX_DEVICE_RECORDS; i++)
88 {
89 if (!(btm_cb.sec_dev_rec[i].sec_flags & BTM_SEC_IN_USE))
90 {
Sharvil Nanavati83c52562014-05-04 00:46:57 -070091 BTM_TRACE_DEBUG ("allocate a new dev rec idx=0x%x ", i );
The Android Open Source Project5738f832012-12-12 16:00:35 -080092 p_dev_rec = &btm_cb.sec_dev_rec[i];
93
94 /* Mark this record as in use and initialize */
95 memset (p_dev_rec, 0, sizeof (tBTM_SEC_DEV_REC));
96 p_dev_rec->sec_flags = BTM_SEC_IN_USE;
97 memcpy (p_dev_rec->bd_addr, bd_addr, BD_ADDR_LEN);
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -070098 p_dev_rec->hci_handle = BTM_GetHCIConnHandle (bd_addr, BT_TRANSPORT_BR_EDR);
99 p_dev_rec->ble_hci_handle = BTM_GetHCIConnHandle (bd_addr, BT_TRANSPORT_LE);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800100
101 /* update conn params, use default value for background connection params */
102 p_dev_rec->conn_params.min_conn_int =
103 p_dev_rec->conn_params.max_conn_int =
104 p_dev_rec->conn_params.supervision_tout =
105 p_dev_rec->conn_params.slave_latency = BTM_BLE_CONN_PARAM_UNDEF;
106
Sharvil Nanavati83c52562014-05-04 00:46:57 -0700107 BTM_TRACE_DEBUG ("hci_handl=0x%x ", p_dev_rec->ble_hci_handle );
The Android Open Source Project5738f832012-12-12 16:00:35 -0800108 break;
109 }
110 }
111
112 if (!p_dev_rec)
113 return(FALSE);
114 }
115 else
116 {
Sharvil Nanavati83c52562014-05-04 00:46:57 -0700117 BTM_TRACE_DEBUG("Device already exist");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800118 }
119
120 memset(p_dev_rec->sec_bd_name, 0, sizeof(tBTM_BD_NAME));
121
122 if (bd_name && bd_name[0])
123 {
124 p_dev_rec->sec_flags |= BTM_SEC_NAME_KNOWN;
125 BCM_STRNCPY_S ((char *)p_dev_rec->sec_bd_name, sizeof (p_dev_rec->sec_bd_name),
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800126 (char *)bd_name, BTM_MAX_REM_BD_NAME_LEN);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800127 }
128 p_dev_rec->device_type = dev_type;
129 p_dev_rec->ble.ble_addr_type = addr_type;
Sharvil Nanavati83c52562014-05-04 00:46:57 -0700130 BTM_TRACE_DEBUG ("p_dev_rec->device_type =0x%x addr_type=0x%x sec_flags=0x%x",
The Android Open Source Project5738f832012-12-12 16:00:35 -0800131 dev_type, addr_type, p_dev_rec->sec_flags);
132
133 /* sync up with the Inq Data base*/
134 p_info = BTM_InqDbRead(bd_addr);
135 if (p_info)
136 {
137 p_info->results.ble_addr_type = p_dev_rec->ble.ble_addr_type ;
138 p_info->results.device_type = p_dev_rec->device_type;
Sharvil Nanavati83c52562014-05-04 00:46:57 -0700139 BTM_TRACE_DEBUG ("InqDb device_type =0x%x addr_type=0x%x",
The Android Open Source Project5738f832012-12-12 16:00:35 -0800140 p_info->results.device_type, p_info->results.ble_addr_type);
141 }
142
The Android Open Source Project5738f832012-12-12 16:00:35 -0800143 return(TRUE);
144}
145
146/*******************************************************************************
147**
148** Function BTM_SecAddBleKey
149**
150** Description Add/modify LE device information. This function will be
151** normally called during host startup to restore all required
152** information stored in the NVRAM.
153**
154** Parameters: bd_addr - BD address of the peer
155** p_le_key - LE key values.
156** key_type - LE SMP key type.
157*
158** Returns TRUE if added OK, else FALSE
159**
160*******************************************************************************/
161BOOLEAN BTM_SecAddBleKey (BD_ADDR bd_addr, tBTM_LE_KEY_VALUE *p_le_key, tBTM_LE_KEY_TYPE key_type)
162{
Mike J. Chenbf9a8aa2014-02-11 13:51:29 -0800163#if SMP_INCLUDED == TRUE
The Android Open Source Project5738f832012-12-12 16:00:35 -0800164 tBTM_SEC_DEV_REC *p_dev_rec;
Sharvil Nanavati83c52562014-05-04 00:46:57 -0700165 BTM_TRACE_DEBUG ("BTM_SecAddBleKey");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800166 p_dev_rec = btm_find_dev (bd_addr);
167 if (!p_dev_rec || !p_le_key ||
168 (key_type != BTM_LE_KEY_PENC && key_type != BTM_LE_KEY_PID &&
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800169 key_type != BTM_LE_KEY_PCSRK && key_type != BTM_LE_KEY_LENC &&
170 key_type != BTM_LE_KEY_LCSRK))
The Android Open Source Project5738f832012-12-12 16:00:35 -0800171 {
Sharvil Nanavati83c52562014-05-04 00:46:57 -0700172 BTM_TRACE_WARNING ("BTM_SecAddBleKey() Wrong Type, or No Device record \
The Android Open Source Project5738f832012-12-12 16:00:35 -0800173 for bdaddr: %08x%04x, Type: %d",
174 (bd_addr[0]<<24)+(bd_addr[1]<<16)+(bd_addr[2]<<8)+bd_addr[3],
175 (bd_addr[4]<<8)+bd_addr[5], key_type);
176 return(FALSE);
177 }
178
Sharvil Nanavati83c52562014-05-04 00:46:57 -0700179 BTM_TRACE_DEBUG ("BTM_SecAddLeKey() BDA: %08x%04x, Type: 0x%02x",
The Android Open Source Project5738f832012-12-12 16:00:35 -0800180 (bd_addr[0]<<24)+(bd_addr[1]<<16)+(bd_addr[2]<<8)+bd_addr[3],
181 (bd_addr[4]<<8)+bd_addr[5], key_type);
182
183 if (key_type == BTM_LE_KEY_PENC || key_type == BTM_LE_KEY_PID ||
184 key_type == BTM_LE_KEY_PCSRK || key_type == BTM_LE_KEY_LENC ||
185 key_type == BTM_LE_KEY_LCSRK)
186 {
187 btm_sec_save_le_key (bd_addr, key_type, p_le_key, FALSE);
188 }
189
190#endif
191
192 return(TRUE);
193}
194
195/*******************************************************************************
196**
197** Function BTM_BleLoadLocalKeys
198**
199** Description Local local identity key, encryption root or sign counter.
200**
201** Parameters: key_type: type of key, can be BTM_BLE_KEY_TYPE_ID, BTM_BLE_KEY_TYPE_ER
202** or BTM_BLE_KEY_TYPE_COUNTER.
203** p_key: pointer to the key.
204*
205** Returns non2.
206**
207*******************************************************************************/
208void BTM_BleLoadLocalKeys(UINT8 key_type, tBTM_BLE_LOCAL_KEYS *p_key)
209{
The Android Open Source Project5738f832012-12-12 16:00:35 -0800210 tBTM_DEVCB *p_devcb = &btm_cb.devcb;
Sharvil Nanavati83c52562014-05-04 00:46:57 -0700211 BTM_TRACE_DEBUG ("BTM_BleLoadLocalKeys");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800212 if (p_key != NULL)
213 {
214 switch (key_type)
215 {
216 case BTM_BLE_KEY_TYPE_ID:
217 memcpy(&p_devcb->id_keys, &p_key->id_keys, sizeof(tBTM_BLE_LOCAL_ID_KEYS));
218 break;
219
220 case BTM_BLE_KEY_TYPE_ER:
221 memcpy(p_devcb->er, p_key->er, sizeof(BT_OCTET16));
222 break;
223
224 default:
Sharvil Nanavati83c52562014-05-04 00:46:57 -0700225 BTM_TRACE_ERROR("unknow local key type: %d", key_type);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800226 break;
227 }
228 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800229}
230
231/*******************************************************************************
232**
233** Function BTM_GetDeviceEncRoot
234**
235** Description This function is called to read the local device encryption
236** root.
237**
238** Returns void
239** the local device ER is copied into er
240**
241*******************************************************************************/
242void BTM_GetDeviceEncRoot (BT_OCTET16 er)
243{
Sharvil Nanavati83c52562014-05-04 00:46:57 -0700244 BTM_TRACE_DEBUG ("BTM_GetDeviceEncRoot");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800245
The Android Open Source Project5738f832012-12-12 16:00:35 -0800246 memcpy (er, btm_cb.devcb.er, BT_OCTET16_LEN);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800247}
248
249/*******************************************************************************
250**
251** Function BTM_GetDeviceIDRoot
252**
253** Description This function is called to read the local device identity
254** root.
255**
256** Returns void
257** the local device IR is copied into irk
258**
259*******************************************************************************/
260void BTM_GetDeviceIDRoot (BT_OCTET16 irk)
261{
Sharvil Nanavati83c52562014-05-04 00:46:57 -0700262 BTM_TRACE_DEBUG ("BTM_GetDeviceIDRoot ");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800263
The Android Open Source Project5738f832012-12-12 16:00:35 -0800264 memcpy (irk, btm_cb.devcb.id_keys.irk, BT_OCTET16_LEN);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800265}
266
267/*******************************************************************************
268**
269** Function BTM_GetDeviceDHK
270**
271** Description This function is called to read the local device DHK.
272**
273** Returns void
274** the local device DHK is copied into dhk
275**
276*******************************************************************************/
277void BTM_GetDeviceDHK (BT_OCTET16 dhk)
278{
Sharvil Nanavati83c52562014-05-04 00:46:57 -0700279 BTM_TRACE_DEBUG ("BTM_GetDeviceDHK");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800280 memcpy (dhk, btm_cb.devcb.id_keys.dhk, BT_OCTET16_LEN);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800281}
282
283/*******************************************************************************
284**
285** Function BTM_ReadConnectionAddr
286**
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800287** Description This function is called to get the local device address information
The Android Open Source Project5738f832012-12-12 16:00:35 -0800288** .
289**
290** Returns void
291**
292*******************************************************************************/
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800293void BTM_ReadConnectionAddr (BD_ADDR remote_bda, BD_ADDR local_conn_addr, tBLE_ADDR_TYPE *p_addr_type)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800294{
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700295 tACL_CONN *p_acl = btm_bda_to_acl(remote_bda, BT_TRANSPORT_LE);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800296
297 if (p_acl == NULL)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800298 {
Sharvil Nanavati83c52562014-05-04 00:46:57 -0700299 BTM_TRACE_ERROR("No connection exist!");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800300 return;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800301 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800302 memcpy(local_conn_addr, p_acl->conn_addr, BD_ADDR_LEN);
303 * p_addr_type = p_acl->conn_addr_type;
304
Sharvil Nanavati83c52562014-05-04 00:46:57 -0700305 BTM_TRACE_DEBUG ("BTM_ReadConnectionAddr address type: %d addr: 0x%02x",
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800306 p_acl->conn_addr_type, p_acl->conn_addr[0]);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800307}
Mike J. Chenbf9a8aa2014-02-11 13:51:29 -0800308
Andre Eisenbach6975b4d2013-08-05 16:55:38 -0700309/*******************************************************************************
310**
311** Function BTM_IsBleConnection
312**
313** Description This function is called to check if the connection handle
314** for an LE link
315**
316** Returns TRUE if connection is LE link, otherwise FALSE.
317**
318*******************************************************************************/
319BOOLEAN BTM_IsBleConnection (UINT16 conn_handle)
320{
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700321#if (BLE_INCLUDED == TRUE)
Andre Eisenbach6975b4d2013-08-05 16:55:38 -0700322 UINT8 xx;
323 tACL_CONN *p;
324
Sharvil Nanavati83c52562014-05-04 00:46:57 -0700325 BTM_TRACE_API ("BTM_IsBleConnection: conn_handle: %d", conn_handle);
Andre Eisenbach6975b4d2013-08-05 16:55:38 -0700326
327 xx = btm_handle_to_acl_index (conn_handle);
328 if (xx >= MAX_L2CAP_LINKS)
329 return FALSE;
330
331 p = &btm_cb.acl_db[xx];
332
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700333 return (p->transport == BT_TRANSPORT_LE);
334#else
335 return FALSE;
336#endif
Andre Eisenbach6975b4d2013-08-05 16:55:38 -0700337}
The Android Open Source Project5738f832012-12-12 16:00:35 -0800338
339/*******************************************************************************
340**
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800341** Function BTM_ReadRemoteConnectionAddr
342**
343** Description This function is read the remote device address currently used
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800344**
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700345** Parameters pseudo_addr: pseudo random address available
346** conn_addr:connection address used
347** p_addr_type : BD Address type, Public or Random of the address used
348**
349** Returns BOOLEAN , TRUE if connection to remote device exists, else FALSE
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800350**
351*******************************************************************************/
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700352BOOLEAN BTM_ReadRemoteConnectionAddr(BD_ADDR pseudo_addr, BD_ADDR conn_addr,
353 tBLE_ADDR_TYPE *p_addr_type)
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800354{
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700355 BOOLEAN st = TRUE;
356#if (BLE_PRIVACY_SPT == TRUE)
357 tACL_CONN *p = btm_bda_to_acl (pseudo_addr, BT_TRANSPORT_LE);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800358
Zhihai Xu6f908b22014-03-11 15:01:45 -0700359 if (p == NULL)
360 {
Sharvil Nanavati83c52562014-05-04 00:46:57 -0700361 BTM_TRACE_ERROR("BTM_ReadRemoteConnectionAddr can not find connection"
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700362 " with matching address");
Zhihai Xu6f908b22014-03-11 15:01:45 -0700363 return FALSE;
364 }
365
366 memcpy(conn_addr, p->active_remote_addr, BD_ADDR_LEN);
367 *p_addr_type = p->active_remote_addr_type;
368#else
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700369 tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev(pseudo_addr);
370
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800371 memcpy(conn_addr, pseudo_addr, BD_ADDR_LEN);
Andre Eisenbach6975b4d2013-08-05 16:55:38 -0700372 if (p_dev_rec != NULL)
373 {
374 *p_addr_type = p_dev_rec->ble.ble_addr_type;
375 }
Zhihai Xu6f908b22014-03-11 15:01:45 -0700376#endif
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800377 return st;
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700378
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800379}
380/*******************************************************************************
381**
The Android Open Source Project5738f832012-12-12 16:00:35 -0800382** Function BTM_SecurityGrant
383**
384** Description This function is called to grant security process.
385**
386** Parameters bd_addr - peer device bd address.
387** res - result of the operation BTM_SUCCESS if success.
388** Otherwise, BTM_REPEATED_ATTEMPTS is too many attempts.
389**
390** Returns None
391**
392*******************************************************************************/
393void BTM_SecurityGrant(BD_ADDR bd_addr, UINT8 res)
394{
Mike J. Chenbf9a8aa2014-02-11 13:51:29 -0800395#if SMP_INCLUDED == TRUE
The Android Open Source Project5738f832012-12-12 16:00:35 -0800396 tSMP_STATUS res_smp = (res == BTM_SUCCESS) ? SMP_SUCCESS : SMP_REPEATED_ATTEMPTS;
Sharvil Nanavati83c52562014-05-04 00:46:57 -0700397 BTM_TRACE_DEBUG ("BTM_SecurityGrant");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800398 SMP_SecurityGrant(bd_addr, res_smp);
399#endif
400}
401
402/*******************************************************************************
403**
404** Function BTM_BlePasskeyReply
405**
406** Description This function is called after Security Manager submitted
407** passkey request to the application.
408**
409** Parameters: bd_addr - Address of the device for which passkey was requested
410** res - result of the operation BTM_SUCCESS if success
411** key_len - length in bytes of the Passkey
412** p_passkey - pointer to array with the passkey
413** trusted_mask - bitwise OR of trusted services (array of UINT32)
414**
415*******************************************************************************/
416void BTM_BlePasskeyReply (BD_ADDR bd_addr, UINT8 res, UINT32 passkey)
417{
Mike J. Chenbf9a8aa2014-02-11 13:51:29 -0800418#if SMP_INCLUDED == TRUE
The Android Open Source Project5738f832012-12-12 16:00:35 -0800419 tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev (bd_addr);
420 tSMP_STATUS res_smp = (res == BTM_SUCCESS) ? SMP_SUCCESS : SMP_PASSKEY_ENTRY_FAIL;
421
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800422 if (p_dev_rec == NULL)
423 {
Sharvil Nanavati83c52562014-05-04 00:46:57 -0700424 BTM_TRACE_ERROR("Passkey reply to Unknown device");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800425 return;
426 }
427
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700428 p_dev_rec->sec_flags |= BTM_SEC_LE_LINK_KEY_AUTHED;
Sharvil Nanavati83c52562014-05-04 00:46:57 -0700429 BTM_TRACE_DEBUG ("BTM_BlePasskeyReply");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800430 SMP_PasskeyReply(bd_addr, res_smp, passkey);
431#endif
432}
433
434/*******************************************************************************
435**
436** Function BTM_BleOobDataReply
437**
438** Description This function is called to provide the OOB data for
439** SMP in response to BTM_LE_OOB_REQ_EVT
440**
441** Parameters: bd_addr - Address of the peer device
442** res - result of the operation SMP_SUCCESS if success
443** p_data - simple pairing Randomizer C.
444**
445*******************************************************************************/
446void BTM_BleOobDataReply(BD_ADDR bd_addr, UINT8 res, UINT8 len, UINT8 *p_data)
447{
Mike J. Chenbf9a8aa2014-02-11 13:51:29 -0800448#if SMP_INCLUDED == TRUE
The Android Open Source Project5738f832012-12-12 16:00:35 -0800449 tSMP_STATUS res_smp = (res == BTM_SUCCESS) ? SMP_SUCCESS : SMP_OOB_FAIL;
450 tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev (bd_addr);
451
Sharvil Nanavati83c52562014-05-04 00:46:57 -0700452 BTM_TRACE_DEBUG ("BTM_BleOobDataReply");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800453
454 if (p_dev_rec == NULL)
455 {
Sharvil Nanavati83c52562014-05-04 00:46:57 -0700456 BTM_TRACE_ERROR("BTM_BleOobDataReply() to Unknown device");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800457 return;
458 }
459
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700460 p_dev_rec->sec_flags |= BTM_SEC_LE_LINK_KEY_AUTHED;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800461 SMP_OobDataReply(bd_addr, res_smp, len, p_data);
462#endif
463}
464
465/******************************************************************************
466**
467** Function BTM_BleSetConnScanParams
468**
469** Description Set scan parameter used in BLE connection request
470**
471** Parameters: scan_interval: scan interval
472** scan_window: scan window
473**
474** Returns void
475**
476*******************************************************************************/
477void BTM_BleSetConnScanParams (UINT16 scan_interval, UINT16 scan_window)
478{
Mike J. Chenbf9a8aa2014-02-11 13:51:29 -0800479#if SMP_INCLUDED == TRUE
The Android Open Source Project5738f832012-12-12 16:00:35 -0800480 tBTM_BLE_CB *p_ble_cb = &btm_cb.ble_ctr_cb;
481 BOOLEAN new_param = FALSE;
482
483 if (BTM_BLE_VALID_PRAM(scan_interval, BTM_BLE_SCAN_INT_MIN, BTM_BLE_SCAN_INT_MAX) &&
484 BTM_BLE_VALID_PRAM(scan_window, BTM_BLE_SCAN_WIN_MIN, BTM_BLE_SCAN_WIN_MAX))
485 {
486 btu_stop_timer(&p_ble_cb->scan_param_idle_timer);
487
488 if (p_ble_cb->scan_int != scan_interval)
489 {
490 p_ble_cb->scan_int = scan_interval;
491 new_param = TRUE;
492 }
493
494 if (p_ble_cb->scan_win != scan_window)
495 {
496 p_ble_cb->scan_win = scan_window;
497 new_param = TRUE;
498 }
499
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800500 if (new_param && p_ble_cb->conn_state == BLE_BG_CONN)
501 {
502 btm_ble_suspend_bg_conn();
503 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800504 }
505 else
506 {
Sharvil Nanavati83c52562014-05-04 00:46:57 -0700507 BTM_TRACE_ERROR("Illegal Connection Scan Parameters");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800508 }
509#endif
510}
511
512/********************************************************
513**
514** Function BTM_BleSetPrefConnParams
515**
516** Description Set a peripheral's preferred connection parameters
517**
518** Parameters: bd_addr - BD address of the peripheral
519** scan_interval: scan interval
520** scan_window: scan window
521** min_conn_int - minimum preferred connection interval
522** max_conn_int - maximum preferred connection interval
523** slave_latency - preferred slave latency
524** supervision_tout - preferred supervision timeout
525**
526** Returns void
527**
528*******************************************************************************/
529void BTM_BleSetPrefConnParams (BD_ADDR bd_addr,
530 UINT16 min_conn_int, UINT16 max_conn_int,
531 UINT16 slave_latency, UINT16 supervision_tout)
532{
The Android Open Source Project5738f832012-12-12 16:00:35 -0800533 tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev (bd_addr);
534
Sharvil Nanavati83c52562014-05-04 00:46:57 -0700535 BTM_TRACE_API ("BTM_BleSetPrefConnParams min: %u max: %u latency: %u \
The Android Open Source Project5738f832012-12-12 16:00:35 -0800536 tout: %u",
537 min_conn_int, max_conn_int, slave_latency, supervision_tout);
538
539 if (BTM_BLE_VALID_PRAM(min_conn_int, BTM_BLE_CONN_INT_MIN, BTM_BLE_CONN_INT_MAX) &&
540 BTM_BLE_VALID_PRAM(max_conn_int, BTM_BLE_CONN_INT_MIN, BTM_BLE_CONN_INT_MAX) &&
541 BTM_BLE_VALID_PRAM(supervision_tout, BTM_BLE_CONN_SUP_TOUT_MIN, BTM_BLE_CONN_SUP_TOUT_MAX) &&
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800542 (slave_latency <= BTM_BLE_CONN_LATENCY_MAX || slave_latency == BTM_BLE_CONN_PARAM_UNDEF))
The Android Open Source Project5738f832012-12-12 16:00:35 -0800543 {
544 if (p_dev_rec)
545 {
546 /* expect conn int and stout and slave latency to be updated all together */
547 if (min_conn_int != BTM_BLE_CONN_PARAM_UNDEF || max_conn_int != BTM_BLE_CONN_PARAM_UNDEF)
548 {
549 if (min_conn_int != BTM_BLE_CONN_PARAM_UNDEF)
550 p_dev_rec->conn_params.min_conn_int = min_conn_int;
551 else
552 p_dev_rec->conn_params.min_conn_int = max_conn_int;
553
554 if (max_conn_int != BTM_BLE_CONN_PARAM_UNDEF)
555 p_dev_rec->conn_params.max_conn_int = max_conn_int;
556 else
557 p_dev_rec->conn_params.max_conn_int = min_conn_int;
558
559 if (slave_latency != BTM_BLE_CONN_PARAM_UNDEF)
560 p_dev_rec->conn_params.slave_latency = slave_latency;
561 else
562 p_dev_rec->conn_params.slave_latency = BTM_BLE_CONN_SLAVE_LATENCY_DEF;
563
564 if (supervision_tout != BTM_BLE_CONN_PARAM_UNDEF)
565 p_dev_rec->conn_params.supervision_tout = supervision_tout;
566 else
567 p_dev_rec->conn_params.slave_latency = BTM_BLE_CONN_TIMEOUT_DEF;
568
569 }
570
571 }
572 else
573 {
Sharvil Nanavati83c52562014-05-04 00:46:57 -0700574 BTM_TRACE_ERROR("Unknown Device, setting rejected");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800575 }
576 }
577 else
578 {
Sharvil Nanavati83c52562014-05-04 00:46:57 -0700579 BTM_TRACE_ERROR("Illegal Connection Parameters");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800580 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800581}
582
583/*******************************************************************************
584**
585** Function BTM_ReadDevInfo
586**
587** Description This function is called to read the device/address type
588** of BD address.
589**
590** Parameter remote_bda: remote device address
591** p_dev_type: output parameter to read the device type.
592** p_addr_type: output parameter to read the address type.
593**
594*******************************************************************************/
595void BTM_ReadDevInfo (BD_ADDR remote_bda, tBT_DEVICE_TYPE *p_dev_type, tBLE_ADDR_TYPE *p_addr_type)
596{
The Android Open Source Project5738f832012-12-12 16:00:35 -0800597 tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev (remote_bda);
598 tBTM_INQ_INFO *p_inq_info = BTM_InqDbRead(remote_bda);
599
600 *p_dev_type = BT_DEVICE_TYPE_BREDR;
601 *p_addr_type = BLE_ADDR_PUBLIC;
602
603 if (!p_dev_rec)
604 {
605 /* Check with the BT manager if details about remote device are known */
606 if (p_inq_info != NULL)
607 {
608 *p_dev_type = p_inq_info->results.device_type ;
609 *p_addr_type = p_inq_info->results.ble_addr_type;
Andre Eisenbach306bdda2013-11-06 23:35:48 -0800610 } else {
611 /* unknown device, assume BR/EDR */
Sharvil Nanavati83c52562014-05-04 00:46:57 -0700612 BTM_TRACE_DEBUG ("btm_find_dev_type - unknown device, BR/EDR assumed");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800613 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800614 }
615 else /* there is a security device record exisitng */
616 {
617 /* new inquiry result, overwrite device type in security device record */
618 if (p_inq_info)
619 {
620 p_dev_rec->device_type = p_inq_info->results.device_type;
621 p_dev_rec->ble.ble_addr_type = p_inq_info->results.ble_addr_type;
622 }
623 *p_dev_type = p_dev_rec->device_type;
624 *p_addr_type = p_dev_rec->ble.ble_addr_type;
625
626 }
627
Sharvil Nanavati83c52562014-05-04 00:46:57 -0700628 BTM_TRACE_DEBUG ("btm_find_dev_type - device_type = %d addr_type = %d", *p_dev_type , *p_addr_type);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800629}
630
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800631/*******************************************************************************
632**
633** Function BTM_BleReceiverTest
634**
635** Description This function is called to start the LE Receiver test
636**
637** Parameter rx_freq - Frequency Range
638** p_cmd_cmpl_cback - Command Complete callback
639**
640*******************************************************************************/
641void BTM_BleReceiverTest(UINT8 rx_freq, tBTM_CMPL_CB *p_cmd_cmpl_cback)
642{
643 btm_cb.devcb.p_le_test_cmd_cmpl_cb = p_cmd_cmpl_cback;
644
645 if (btsnd_hcic_ble_receiver_test(rx_freq) == FALSE)
646 {
Sharvil Nanavati83c52562014-05-04 00:46:57 -0700647 BTM_TRACE_ERROR("%s: Unable to Trigger LE receiver test", __FUNCTION__);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800648 }
649}
650
651/*******************************************************************************
652**
653** Function BTM_BleTransmitterTest
654**
655** Description This function is called to start the LE Transmitter test
656**
657** Parameter tx_freq - Frequency Range
658** test_data_len - Length in bytes of payload data in each packet
659** packet_payload - Pattern to use in the payload
660** p_cmd_cmpl_cback - Command Complete callback
661**
662*******************************************************************************/
663void BTM_BleTransmitterTest(UINT8 tx_freq, UINT8 test_data_len,
664 UINT8 packet_payload, tBTM_CMPL_CB *p_cmd_cmpl_cback)
665{
666 btm_cb.devcb.p_le_test_cmd_cmpl_cb = p_cmd_cmpl_cback;
667 if (btsnd_hcic_ble_transmitter_test(tx_freq, test_data_len, packet_payload) == FALSE)
668 {
Sharvil Nanavati83c52562014-05-04 00:46:57 -0700669 BTM_TRACE_ERROR("%s: Unable to Trigger LE transmitter test", __FUNCTION__);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800670 }
671}
672
673/*******************************************************************************
674**
675** Function BTM_BleTestEnd
676**
677** Description This function is called to stop the in-progress TX or RX test
678**
679** Parameter p_cmd_cmpl_cback - Command complete callback
680**
681*******************************************************************************/
682void BTM_BleTestEnd(tBTM_CMPL_CB *p_cmd_cmpl_cback)
683{
684 btm_cb.devcb.p_le_test_cmd_cmpl_cb = p_cmd_cmpl_cback;
685
686 if (btsnd_hcic_ble_test_end() == FALSE)
687 {
Sharvil Nanavati83c52562014-05-04 00:46:57 -0700688 BTM_TRACE_ERROR("%s: Unable to End the LE TX/RX test", __FUNCTION__);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800689 }
690}
The Android Open Source Project5738f832012-12-12 16:00:35 -0800691
692/*******************************************************************************
693** Internal Functions
694*******************************************************************************/
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800695void btm_ble_test_command_complete(UINT8 *p)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800696{
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800697 tBTM_CMPL_CB *p_cb = btm_cb.devcb.p_le_test_cmd_cmpl_cb;
698 UINT8 status;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800699
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800700 btm_cb.devcb.p_le_test_cmd_cmpl_cb = NULL;
701
702 if (p_cb)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800703 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800704 (*p_cb)(p);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800705 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800706}
707
708/*******************************************************************************
709**
Andre Eisenbach6975b4d2013-08-05 16:55:38 -0700710** Function BTM_UseLeLink
711**
712** Description This function is to select the underneath physical link to use.
713**
714** Returns TRUE to use LE, FALSE use BR/EDR.
715**
716*******************************************************************************/
717BOOLEAN BTM_UseLeLink (BD_ADDR bd_addr)
718{
719 tACL_CONN *p;
720 tBT_DEVICE_TYPE dev_type;
721 tBLE_ADDR_TYPE addr_type;
722 BOOLEAN use_le = FALSE;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800723
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700724 if ((p = btm_bda_to_acl(bd_addr, BT_TRANSPORT_BR_EDR)) != NULL)
Andre Eisenbach6975b4d2013-08-05 16:55:38 -0700725 {
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700726 return use_le;
727 }
728 else if ((p = btm_bda_to_acl(bd_addr, BT_TRANSPORT_LE)) != NULL)
729 {
730 use_le = TRUE;
Andre Eisenbach6975b4d2013-08-05 16:55:38 -0700731 }
732 else
733 {
734 BTM_ReadDevInfo(bd_addr, &dev_type, &addr_type);
735 use_le = (dev_type == BT_DEVICE_TYPE_BLE);
736 }
737 return use_le;
738}
The Android Open Source Project5738f832012-12-12 16:00:35 -0800739/*******************************************************************************
740**
741** Function btm_ble_rand_enc_complete
742**
743** Description This function is the callback functions for HCI_Rand command
744** and HCI_Encrypt command is completed.
745** This message is received from the HCI.
746**
747** Returns void
748**
749*******************************************************************************/
750void btm_ble_rand_enc_complete (UINT8 *p, UINT16 op_code, tBTM_RAND_ENC_CB *p_enc_cplt_cback)
751{
752 tBTM_RAND_ENC params;
753 UINT8 *p_dest = params.param_buf;
754
Sharvil Nanavati83c52562014-05-04 00:46:57 -0700755 BTM_TRACE_DEBUG ("btm_ble_rand_enc_complete");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800756
757 memset(&params, 0, sizeof(tBTM_RAND_ENC));
758
759 /* If there was a callback address for vcs complete, call it */
760 if (p_enc_cplt_cback && p)
761 {
762 /* Pass paramters to the callback function */
763 STREAM_TO_UINT8(params.status, p); /* command status */
764
765 if (params.status == HCI_SUCCESS)
766 {
767 params.opcode = op_code;
768
769 if (op_code == HCI_BLE_RAND)
770 params.param_len = BT_OCTET8_LEN;
771 else
772 params.param_len = BT_OCTET16_LEN;
773
774 memcpy(p_dest, p, params.param_len); /* Fetch return info from HCI event message */
775 }
776 if (p_enc_cplt_cback)
777 (*p_enc_cplt_cback)(&params); /* Call the Encryption complete callback function */
778 }
779}
780
781
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800782 #if (SMP_INCLUDED == TRUE)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800783
784/*******************************************************************************
785**
786** Function btm_ble_get_enc_key_type
787**
788** Description This function is to increment local sign counter
789** Returns None
790**
791*******************************************************************************/
792void btm_ble_increment_sign_ctr(BD_ADDR bd_addr, BOOLEAN is_local )
793{
794 tBTM_SEC_DEV_REC *p_dev_rec;
795
Sharvil Nanavati83c52562014-05-04 00:46:57 -0700796 BTM_TRACE_DEBUG ("btm_ble_increment_sign_ctr is_local=%d", is_local);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800797
798 if ((p_dev_rec = btm_find_dev (bd_addr)) != NULL)
799 {
800 if (is_local)
801 p_dev_rec->ble.keys.local_counter++;
802 else
803 p_dev_rec->ble.keys.counter++;
Sharvil Nanavati83c52562014-05-04 00:46:57 -0700804 BTM_TRACE_DEBUG ("is_local=%d local sign counter=%d peer sign counter=%d",
The Android Open Source Project5738f832012-12-12 16:00:35 -0800805 is_local,
806 p_dev_rec->ble.keys.local_counter,
807 p_dev_rec->ble.keys.counter);
808 }
809}
810
811/*******************************************************************************
812**
813** Function btm_ble_get_enc_key_type
814**
815** Description This function is to get the BLE key type that has been exchanged
816** in betweem local device and peer device.
817**
818** Returns p_key_type: output parameter to carry the key type value.
819**
820*******************************************************************************/
821BOOLEAN btm_ble_get_enc_key_type(BD_ADDR bd_addr, UINT8 *p_key_types)
822{
823 tBTM_SEC_DEV_REC *p_dev_rec;
824
Sharvil Nanavati83c52562014-05-04 00:46:57 -0700825 BTM_TRACE_DEBUG ("btm_ble_get_enc_key_type");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800826
827 if ((p_dev_rec = btm_find_dev (bd_addr)) != NULL)
828 {
829 *p_key_types = p_dev_rec->ble.key_type;
830 return TRUE;
831 }
832 return FALSE;
833}
834
835/*******************************************************************************
836**
837** Function btm_get_local_div
838**
839** Description This function is called to read the local DIV
840**
841** Returns TURE - if a valid DIV is availavle
842*******************************************************************************/
843BOOLEAN btm_get_local_div (BD_ADDR bd_addr, UINT16 *p_div)
844{
845 tBTM_SEC_DEV_REC *p_dev_rec;
846 BOOLEAN status = FALSE;
Sharvil Nanavati83c52562014-05-04 00:46:57 -0700847 BTM_TRACE_DEBUG ("btm_get_local_div");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800848
Sharvil Nanavati83c52562014-05-04 00:46:57 -0700849 BTM_TRACE_DEBUG("bd_addr:%02x-%02x-%02x-%02x-%02x-%02x",
The Android Open Source Project5738f832012-12-12 16:00:35 -0800850 bd_addr[0],bd_addr[1],
851 bd_addr[2],bd_addr[3],
852 bd_addr[4],bd_addr[5]);
853
854 p_dev_rec = btm_find_dev (bd_addr);
855
856 if (p_dev_rec && p_dev_rec->ble.keys.div)
857 {
858 status = TRUE;
859 *p_div = p_dev_rec->ble.keys.div;
860 }
Sharvil Nanavati83c52562014-05-04 00:46:57 -0700861 BTM_TRACE_DEBUG ("btm_get_local_div status=%d (1-OK) DIV=0x%x", status, *p_div);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800862 return status;
863}
864
865/*******************************************************************************
866**
867** Function btm_sec_save_le_key
868**
869** Description This function is called by the SMP to update
870** an BLE key. SMP is internal, whereas all the keys shall
871** be sent to the application. The function is also called
872** when application passes ble key stored in NVRAM to the btm_sec.
873** pass_to_application parameter is false in this case.
874**
875** Returns void
876**
877*******************************************************************************/
878void btm_sec_save_le_key(BD_ADDR bd_addr, tBTM_LE_KEY_TYPE key_type, tBTM_LE_KEY_VALUE *p_keys,
879 BOOLEAN pass_to_application)
880{
881 tBTM_SEC_DEV_REC *p_rec;
882 tBTM_LE_EVT_DATA cb_data;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800883 UINT8 i;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800884
Sharvil Nanavati83c52562014-05-04 00:46:57 -0700885 BTM_TRACE_DEBUG ("btm_sec_save_le_key key_type=0x%x pass_to_application=%d",key_type, pass_to_application);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800886 /* Store the updated key in the device database */
887
Sharvil Nanavati83c52562014-05-04 00:46:57 -0700888 BTM_TRACE_DEBUG("bd_addr:%02x-%02x-%02x-%02x-%02x-%02x",
The Android Open Source Project5738f832012-12-12 16:00:35 -0800889 bd_addr[0],bd_addr[1],
890 bd_addr[2],bd_addr[3],
891 bd_addr[4],bd_addr[5]);
892
893 if ((p_rec = btm_find_dev (bd_addr)) != NULL && p_keys)
894 {
895 switch (key_type)
896 {
897 case BTM_LE_KEY_PENC:
898 memcpy(p_rec->ble.keys.ltk, p_keys->penc_key.ltk, BT_OCTET16_LEN);
899 memcpy(p_rec->ble.keys.rand, p_keys->penc_key.rand, BT_OCTET8_LEN);
900 p_rec->ble.keys.sec_level = p_keys->penc_key.sec_level;
901 p_rec->ble.keys.ediv = p_keys->penc_key.ediv;
902 p_rec->ble.keys.key_size = p_keys->penc_key.key_size;
903 p_rec->ble.key_type |= BTM_LE_KEY_PENC;
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700904 p_rec->sec_flags |= BTM_SEC_LE_LINK_KEY_KNOWN;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800905 if (p_keys->penc_key.sec_level == SMP_SEC_AUTHENTICATED)
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700906 p_rec->sec_flags |= BTM_SEC_LE_LINK_KEY_AUTHED;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800907 else
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700908 p_rec->sec_flags &= ~BTM_SEC_LE_LINK_KEY_AUTHED;
Sharvil Nanavati83c52562014-05-04 00:46:57 -0700909 BTM_TRACE_DEBUG("BTM_LE_KEY_PENC key_type=0x%x sec_flags=0x%x sec_leve=0x%x",
The Android Open Source Project5738f832012-12-12 16:00:35 -0800910 p_rec->ble.key_type,
911 p_rec->sec_flags,
912 p_rec->ble.keys.sec_level);
913 break;
914
915 case BTM_LE_KEY_PID:
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800916 for (i=0; i<BT_OCTET16_LEN; i++)
917 {
918 p_rec->ble.keys.irk[i] = p_keys->pid_key.irk[i];
919 }
920
921 //memcpy( p_rec->ble.keys.irk, p_keys->pid_key, BT_OCTET16_LEN); todo will crash the system
922 memcpy(p_rec->ble.static_addr, p_keys->pid_key.static_addr, BD_ADDR_LEN);
923 p_rec->ble.static_addr_type = p_keys->pid_key.addr_type;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800924 p_rec->ble.key_type |= BTM_LE_KEY_PID;
Sharvil Nanavati83c52562014-05-04 00:46:57 -0700925 BTM_TRACE_DEBUG("BTM_LE_KEY_PID key_type=0x%x save peer IRK", p_rec->ble.key_type);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800926 break;
927
928 case BTM_LE_KEY_PCSRK:
929 memcpy(p_rec->ble.keys.csrk, p_keys->pcsrk_key.csrk, BT_OCTET16_LEN);
930 p_rec->ble.keys.srk_sec_level = p_keys->pcsrk_key.sec_level;
931 p_rec->ble.keys.counter = p_keys->pcsrk_key.counter;
932 p_rec->ble.key_type |= BTM_LE_KEY_PCSRK;
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700933 p_rec->sec_flags |= BTM_SEC_LE_LINK_KEY_KNOWN;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800934 if ( p_keys->pcsrk_key.sec_level== SMP_SEC_AUTHENTICATED)
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700935 p_rec->sec_flags |= BTM_SEC_LE_LINK_KEY_AUTHED;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800936 else
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700937 p_rec->sec_flags &= ~BTM_SEC_LE_LINK_KEY_AUTHED;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800938
Sharvil Nanavati83c52562014-05-04 00:46:57 -0700939 BTM_TRACE_DEBUG("BTM_LE_KEY_PCSRK key_type=0x%x sec_flags=0x%x sec_level=0x%x peer_counter=%d",
The Android Open Source Project5738f832012-12-12 16:00:35 -0800940 p_rec->ble.key_type,
941 p_rec->sec_flags,
942 p_rec->ble.keys.srk_sec_level,
943 p_rec->ble.keys.counter );
944 break;
945
946 case BTM_LE_KEY_LENC:
947 p_rec->ble.keys.div = p_keys->lenc_key.div; /* update DIV */
948 p_rec->ble.keys.sec_level = p_keys->lenc_key.sec_level;
949 p_rec->ble.keys.key_size = p_keys->lenc_key.key_size;
950 p_rec->ble.key_type |= BTM_LE_KEY_LENC;
951
Sharvil Nanavati83c52562014-05-04 00:46:57 -0700952 BTM_TRACE_DEBUG("BTM_LE_KEY_LENC key_type=0x%x DIV=0x%x key_size=0x%x sec_level=0x%x",
The Android Open Source Project5738f832012-12-12 16:00:35 -0800953 p_rec->ble.key_type,
954 p_rec->ble.keys.div,
955 p_rec->ble.keys.key_size,
956 p_rec->ble.keys.sec_level );
957 break;
958
959 case BTM_LE_KEY_LCSRK:/* local CSRK has been delivered */
960 p_rec->ble.keys.div = p_keys->lcsrk_key.div; /* update DIV */
961 p_rec->ble.keys.local_csrk_sec_level = p_keys->lcsrk_key.sec_level;
962 p_rec->ble.keys.local_counter = p_keys->lcsrk_key.counter;
963 p_rec->ble.key_type |= BTM_LE_KEY_LCSRK;
Sharvil Nanavati83c52562014-05-04 00:46:57 -0700964 BTM_TRACE_DEBUG("BTM_LE_KEY_LCSRK key_type=0x%x DIV=0x%x scrk_sec_level=0x%x local_counter=%d",
The Android Open Source Project5738f832012-12-12 16:00:35 -0800965 p_rec->ble.key_type,
966 p_rec->ble.keys.div,
967 p_rec->ble.keys.local_csrk_sec_level,
968 p_rec->ble.keys.local_counter );
969 break;
970
971 default:
Sharvil Nanavati83c52562014-05-04 00:46:57 -0700972 BTM_TRACE_WARNING("btm_sec_save_le_key (Bad key_type 0x%02x)", key_type);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800973 return;
974 }
975
Sharvil Nanavati83c52562014-05-04 00:46:57 -0700976 BTM_TRACE_DEBUG ("BLE key type 0x%02x updated for BDA: %08x%04x (btm_sec_save_le_key)", key_type,
The Android Open Source Project5738f832012-12-12 16:00:35 -0800977 (bd_addr[0]<<24)+(bd_addr[1]<<16)+(bd_addr[2]<<8)+bd_addr[3],
978 (bd_addr[4]<<8)+bd_addr[5]);
979
980 /* Notify the application that one of the BLE keys has been updated
981 If link key is in progress, it will get sent later.*/
982 if (pass_to_application && btm_cb.api.p_le_callback)
983 {
984 cb_data.key.p_key_value = p_keys;
985 cb_data.key.key_type = key_type;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800986 (*btm_cb.api.p_le_callback) (BTM_LE_KEY_EVT, bd_addr, &cb_data);
987 }
988 return;
989 }
990
Sharvil Nanavati83c52562014-05-04 00:46:57 -0700991 BTM_TRACE_WARNING ("BLE key type 0x%02x called for Unknown BDA or type: %08x%04x !! (btm_sec_save_le_key)", key_type,
The Android Open Source Project5738f832012-12-12 16:00:35 -0800992 (bd_addr[0]<<24)+(bd_addr[1]<<16)+(bd_addr[2]<<8)+bd_addr[3],
993 (bd_addr[4]<<8)+bd_addr[5]);
994
995 if (p_rec)
996 {
Sharvil Nanavati83c52562014-05-04 00:46:57 -0700997 BTM_TRACE_DEBUG ("sec_flags=0x%x", p_rec->sec_flags);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800998 }
999}
1000
1001/*******************************************************************************
1002**
1003** Function btm_ble_update_sec_key_size
1004**
1005** Description update the current lin kencryption key size
1006**
1007** Returns void
1008**
1009*******************************************************************************/
1010void btm_ble_update_sec_key_size(BD_ADDR bd_addr, UINT8 enc_key_size)
1011{
1012 tBTM_SEC_DEV_REC *p_rec;
1013
Sharvil Nanavati83c52562014-05-04 00:46:57 -07001014 BTM_TRACE_DEBUG("btm_ble_update_sec_key_size enc_key_size = %d", enc_key_size);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001015
1016 if ((p_rec = btm_find_dev (bd_addr)) != NULL )
1017 {
1018 p_rec->enc_key_size = enc_key_size;
1019 }
1020}
1021
1022/*******************************************************************************
1023**
1024** Function btm_ble_read_sec_key_size
1025**
1026** Description update the current lin kencryption key size
1027**
1028** Returns void
1029**
1030*******************************************************************************/
1031UINT8 btm_ble_read_sec_key_size(BD_ADDR bd_addr)
1032{
1033 tBTM_SEC_DEV_REC *p_rec;
1034
1035 if ((p_rec = btm_find_dev (bd_addr)) != NULL )
1036 {
1037 return p_rec->enc_key_size;
1038 }
1039 else
1040 return 0;
1041}
1042
1043/*******************************************************************************
1044**
1045** Function btm_ble_link_sec_check
1046**
1047** Description Check BLE link security level match.
1048**
1049** Returns TRUE: check is OK and the *p_sec_req_act contain the action
1050**
1051*******************************************************************************/
1052void btm_ble_link_sec_check(BD_ADDR bd_addr, tBTM_LE_AUTH_REQ auth_req, tBTM_BLE_SEC_REQ_ACT *p_sec_req_act)
1053{
1054 tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev (bd_addr);
Andre Eisenbach6975b4d2013-08-05 16:55:38 -07001055 UINT8 req_sec_level = BTM_LE_SEC_NONE, cur_sec_level = BTM_LE_SEC_NONE;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001056
Sharvil Nanavati83c52562014-05-04 00:46:57 -07001057 BTM_TRACE_DEBUG ("btm_ble_link_sec_check auth_req =0x%x", auth_req);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001058
1059 if (p_dev_rec == NULL)
1060 {
Sharvil Nanavati83c52562014-05-04 00:46:57 -07001061 BTM_TRACE_ERROR ("btm_ble_link_sec_check received for unknown device");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001062 return;
1063 }
1064
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001065 if (p_dev_rec->sec_state == BTM_SEC_STATE_ENCRYPTING ||
1066 p_dev_rec->sec_state == BTM_SEC_STATE_AUTHENTICATING)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001067 {
1068 /* race condition: discard the security request while master is encrypting the link */
1069 *p_sec_req_act = BTM_BLE_SEC_REQ_ACT_DISCARD;
1070 }
1071 else
1072 {
1073 req_sec_level = BTM_LE_SEC_UNAUTHENTICATE;
1074 if ((auth_req == (BTM_LE_AUTH_REQ_BOND|BTM_LE_AUTH_REQ_MITM)) ||
1075 (auth_req == (BTM_LE_AUTH_REQ_MITM)) )
1076 {
1077 req_sec_level = BTM_LE_SEC_AUTHENTICATED;
1078 }
1079
Sharvil Nanavati83c52562014-05-04 00:46:57 -07001080 BTM_TRACE_DEBUG ("dev_rec sec_flags=0x%x", p_dev_rec->sec_flags);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001081
1082 /* currently encrpted */
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -07001083 if (p_dev_rec->sec_flags & BTM_SEC_LE_ENCRYPTED)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001084 {
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -07001085 if (p_dev_rec->sec_flags & BTM_SEC_LE_LINK_KEY_AUTHED)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001086 cur_sec_level = BTM_LE_SEC_AUTHENTICATED;
1087 else
1088 cur_sec_level = BTM_LE_SEC_UNAUTHENTICATE;
1089 }
1090 else /* unencrypted link */
1091 {
1092 /* if bonded, get the key security level */
1093 if (p_dev_rec->ble.key_type & BTM_LE_KEY_PENC)
1094 cur_sec_level = p_dev_rec->ble.keys.sec_level;
1095 else
1096 cur_sec_level = BTM_LE_SEC_NONE;
1097 }
1098
1099 if (cur_sec_level >= req_sec_level)
1100 {
1101 if (cur_sec_level == BTM_LE_SEC_NONE)
1102 {
1103 *p_sec_req_act = BTM_BLE_SEC_REQ_ACT_NONE;
1104 }
1105 else
1106 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001107 /* To avoid re-encryption on an encrypted link for an equal condition encryption */
1108 /* if link has been encrypted, do nothing, go straight to furhter action
The Android Open Source Project5738f832012-12-12 16:00:35 -08001109 if (p_dev_rec->sec_flags & BTM_SEC_ENCRYPTED)
1110 *p_sec_req_act = BTM_BLE_SEC_REQ_ACT_DISCARD;
1111 else
1112 */
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001113 *p_sec_req_act = BTM_BLE_SEC_REQ_ACT_ENCRYPT;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001114 }
1115 }
1116 else
1117 {
1118 *p_sec_req_act = BTM_BLE_SEC_REQ_ACT_PAIR; /* start the pariring process to upgrade the keys*/
1119 }
1120 }
1121
Sharvil Nanavati83c52562014-05-04 00:46:57 -07001122 BTM_TRACE_DEBUG("cur_sec_level=%d req_sec_level=%d sec_req_act=%d",
The Android Open Source Project5738f832012-12-12 16:00:35 -08001123 cur_sec_level,
1124 req_sec_level,
1125 *p_sec_req_act);
1126
1127}
1128
1129/*******************************************************************************
1130**
1131** Function btm_ble_set_encryption
1132**
1133** Description This function is called to ensure that LE connection is
1134** encrypted. Should be called only on an open connection.
1135** Typically only needed for connections that first want to
1136** bring up unencrypted links, then later encrypt them.
1137**
1138** Returns void
1139** the local device ER is copied into er
1140**
1141*******************************************************************************/
1142tBTM_STATUS btm_ble_set_encryption (BD_ADDR bd_addr, void *p_ref_data, UINT8 link_role)
1143{
1144 tBTM_STATUS cmd = BTM_NO_RESOURCES;
1145 tBTM_BLE_SEC_ACT sec_act = *(tBTM_BLE_SEC_ACT *)p_ref_data ;
1146 tBTM_SEC_DEV_REC *p_rec = btm_find_dev (bd_addr);
1147
The Android Open Source Project5738f832012-12-12 16:00:35 -08001148 if (p_rec == NULL)
1149 {
Sharvil Nanavati83c52562014-05-04 00:46:57 -07001150 BTM_TRACE_WARNING ("btm_ble_set_encryption (NULL device record!! sec_act=0x%x", sec_act);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001151 return(BTM_WRONG_MODE);
1152 }
1153
Sharvil Nanavati83c52562014-05-04 00:46:57 -07001154 BTM_TRACE_DEBUG ("btm_ble_set_encryption sec_act=0x%x role_master=%d", sec_act, p_rec->role_master);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001155
The Android Open Source Project5738f832012-12-12 16:00:35 -08001156 if (sec_act == BTM_BLE_SEC_ENCRYPT_MITM)
1157 {
1158 p_rec->security_required |= BTM_SEC_IN_MITM;
1159 }
1160
1161 switch (sec_act)
1162 {
1163 case BTM_BLE_SEC_ENCRYPT:
1164 if (link_role == BTM_ROLE_MASTER)
1165 {
Sunny Kapdi51822b42013-08-07 12:30:20 -07001166 if(p_rec->sec_state == BTM_SEC_STATE_ENCRYPTING) {
Sharvil Nanavati83c52562014-05-04 00:46:57 -07001167 BTM_TRACE_DEBUG ("State is already encrypting::");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001168 cmd = BTM_CMD_STARTED;
1169 }
Sunny Kapdi51822b42013-08-07 12:30:20 -07001170 else {
1171 /* start link layer encryption using the security info stored */
1172 if (btm_ble_start_encrypt(bd_addr, FALSE, NULL))
1173 {
1174 p_rec->sec_state = BTM_SEC_STATE_ENCRYPTING;
1175 cmd = BTM_CMD_STARTED;
1176 }
1177 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001178 break;
1179 }
1180 /* if salve role then fall through to call SMP_Pair below which will send a
1181 sec_request to request the master to encrypt the link */
1182 case BTM_BLE_SEC_ENCRYPT_NO_MITM:
1183 case BTM_BLE_SEC_ENCRYPT_MITM:
1184
1185 if (SMP_Pair(bd_addr) == SMP_STARTED)
1186 {
1187 cmd = BTM_CMD_STARTED;
1188 p_rec->sec_state = BTM_SEC_STATE_AUTHENTICATING;
1189 }
1190 break;
1191
1192 default:
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -07001193 cmd = BTM_WRONG_MODE;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001194 break;
1195 }
1196 return cmd;
1197}
1198
1199/*******************************************************************************
1200**
1201** Function btm_ble_ltk_request
1202**
1203** Description This function is called when encryption request is received
1204** on a slave device.
1205**
1206**
1207** Returns void
1208**
1209*******************************************************************************/
1210void btm_ble_ltk_request(UINT16 handle, UINT8 rand[8], UINT16 ediv)
1211{
1212 tBTM_CB *p_cb = &btm_cb;
1213 tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev_by_handle (handle);
1214 BT_OCTET8 dummy_stk = {0};
1215
Sharvil Nanavati83c52562014-05-04 00:46:57 -07001216 BTM_TRACE_DEBUG ("btm_ble_ltk_request");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001217
1218 p_cb->ediv = ediv;
1219
1220 memcpy(p_cb->enc_rand, rand, BT_OCTET8_LEN);
1221
Andre Eisenbach6975b4d2013-08-05 16:55:38 -07001222 if (p_dev_rec != NULL)
1223 {
1224 if (!smp_proc_ltk_request(p_dev_rec->bd_addr))
1225 btm_ble_ltk_request_reply(p_dev_rec->bd_addr, FALSE, dummy_stk);
1226 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001227
1228}
1229
1230/*******************************************************************************
1231**
1232** Function btm_ble_start_encrypt
1233**
1234** Description This function is called to start LE encryption.
1235**
1236**
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -07001237** Returns BTM_SUCCESS if encryption was started successfully
The Android Open Source Project5738f832012-12-12 16:00:35 -08001238**
1239*******************************************************************************/
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -07001240tBTM_STATUS btm_ble_start_encrypt(BD_ADDR bda, BOOLEAN use_stk, BT_OCTET16 stk)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001241{
1242 tBTM_CB *p_cb = &btm_cb;
1243 tBTM_SEC_DEV_REC *p_rec = btm_find_dev (bda);
1244 BT_OCTET8 dummy_rand = {0};
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -07001245 tBTM_STATUS rt = BTM_NO_RESOURCES;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001246
Sharvil Nanavati83c52562014-05-04 00:46:57 -07001247 BTM_TRACE_DEBUG ("btm_ble_start_encrypt");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001248
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -07001249 if (!p_rec )
1250 {
Sharvil Nanavati83c52562014-05-04 00:46:57 -07001251 BTM_TRACE_ERROR("Link is not active, can not encrypt!");
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -07001252 return BTM_WRONG_MODE;
1253 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001254
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -07001255 if (p_rec->sec_state == BTM_SEC_STATE_ENCRYPTING)
1256 {
Sharvil Nanavati83c52562014-05-04 00:46:57 -07001257 BTM_TRACE_WARNING("Link Encryption is active, Busy!");
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -07001258 return BTM_BUSY;
1259 }
1260
1261 p_cb->enc_handle = p_rec->ble_hci_handle;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001262
1263 if (use_stk)
1264 {
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -07001265 if (btsnd_hcic_ble_start_enc(p_rec->ble_hci_handle, dummy_rand, 0, stk))
1266 rt = BTM_CMD_STARTED;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001267 }
Andre Eisenbachca22ac42013-02-13 17:02:11 +09001268 else if (p_rec->ble.key_type & BTM_LE_KEY_PENC)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001269 {
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -07001270 if (btsnd_hcic_ble_start_enc(p_rec->ble_hci_handle, p_rec->ble.keys.rand,
The Android Open Source Project5738f832012-12-12 16:00:35 -08001271 p_rec->ble.keys.ediv, p_rec->ble.keys.ltk))
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -07001272 rt = BTM_CMD_STARTED;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001273 }
Andre Eisenbachca22ac42013-02-13 17:02:11 +09001274 else
1275 {
Sharvil Nanavati83c52562014-05-04 00:46:57 -07001276 BTM_TRACE_ERROR("No key available to encrypt the link");
Andre Eisenbach6975b4d2013-08-05 16:55:38 -07001277 }
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -07001278 if (rt == BTM_CMD_STARTED)
Andre Eisenbach6975b4d2013-08-05 16:55:38 -07001279 {
1280 if (p_rec->sec_state == BTM_SEC_STATE_IDLE)
1281 p_rec->sec_state = BTM_SEC_STATE_ENCRYPTING;
Andre Eisenbachca22ac42013-02-13 17:02:11 +09001282 }
1283
Andre Eisenbach6975b4d2013-08-05 16:55:38 -07001284 return rt;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001285}
1286
1287/*******************************************************************************
1288**
1289** Function btm_ble_link_encrypted
1290**
1291** Description This function is called when LE link encrption status is changed.
1292**
1293** Returns void
1294**
1295*******************************************************************************/
1296void btm_ble_link_encrypted(BD_ADDR bd_addr, UINT8 encr_enable)
1297{
1298 tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev (bd_addr);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001299 BOOLEAN enc_cback;
1300
1301 if (!p_dev_rec)
1302 {
Sharvil Nanavati83c52562014-05-04 00:46:57 -07001303 BTM_TRACE_WARNING ("btm_ble_link_encrypted (No Device Found!) encr_enable=%d", encr_enable);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001304 return;
1305 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001306
Sharvil Nanavati83c52562014-05-04 00:46:57 -07001307 BTM_TRACE_DEBUG ("btm_ble_link_encrypted encr_enable=%d", encr_enable);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001308
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001309 enc_cback = (p_dev_rec->sec_state == BTM_SEC_STATE_ENCRYPTING);
1310
The Android Open Source Project5738f832012-12-12 16:00:35 -08001311 smp_link_encrypted(bd_addr, encr_enable);
1312
Sharvil Nanavati83c52562014-05-04 00:46:57 -07001313 BTM_TRACE_DEBUG(" p_dev_rec->sec_flags=0x%x", p_dev_rec->sec_flags);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001314
1315 if (encr_enable && p_dev_rec->enc_key_size == 0)
1316 p_dev_rec->enc_key_size = p_dev_rec->ble.keys.key_size;
1317
1318 p_dev_rec->sec_state = BTM_SEC_STATE_IDLE;
1319 if (p_dev_rec->p_callback && enc_cback)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001320 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001321 if (encr_enable)
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -07001322 btm_sec_dev_rec_cback_event(p_dev_rec, BTM_SUCCESS, TRUE);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001323 else if (p_dev_rec->role_master)
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -07001324 btm_sec_dev_rec_cback_event(p_dev_rec, BTM_ERR_PROCESSING, TRUE);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001325
The Android Open Source Project5738f832012-12-12 16:00:35 -08001326 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001327 /* to notify GATT to send data if any request is pending */
1328 gatt_notify_enc_cmpl(p_dev_rec->bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001329}
1330
1331/*******************************************************************************
1332** Function btm_enc_proc_ltk
1333** Description send LTK reply when it's ready.
1334*******************************************************************************/
1335static void btm_enc_proc_ltk(tSMP_ENC *p)
1336{
1337 UINT8 i;
Sharvil Nanavati83c52562014-05-04 00:46:57 -07001338 BTM_TRACE_DEBUG ("btm_enc_proc_ltk");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001339 if (p && p->param_len == BT_OCTET16_LEN)
1340 {
1341 for (i = 0; i < (BT_OCTET16_LEN - btm_cb.key_size); i ++)
1342 p->param_buf[BT_OCTET16_LEN - i - 1] = 0;
1343 btsnd_hcic_ble_ltk_req_reply(btm_cb.enc_handle, p->param_buf);
1344 }
1345}
1346
1347/*******************************************************************************
1348** Function btm_enc_proc_slave_y
1349** Description calculate LTK when Y is ready
1350*******************************************************************************/
1351static void btm_enc_proc_slave_y(tSMP_ENC *p)
1352{
1353 UINT16 div, y;
1354 UINT8 *pp = p->param_buf;
1355 tBTM_CB *p_cb = &btm_cb;
1356 tSMP_ENC output;
1357 tBTM_SEC_DEV_REC *p_dev_rec;
1358
Sharvil Nanavati83c52562014-05-04 00:46:57 -07001359 BTM_TRACE_DEBUG ("btm_enc_proc_slave_y");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001360 if (p != NULL)
1361 {
1362 STREAM_TO_UINT16(y, pp);
1363
1364 div = p_cb->ediv ^ y;
1365 p_dev_rec = btm_find_dev_by_handle (p_cb->enc_handle);
1366
1367 if ( p_dev_rec &&
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001368 p_dev_rec->ble.keys.div == div )
The Android Open Source Project5738f832012-12-12 16:00:35 -08001369 {
Sharvil Nanavati83c52562014-05-04 00:46:57 -07001370 BTM_TRACE_DEBUG ("LTK request OK");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001371 /* calculating LTK , LTK = E er(div) */
1372 SMP_Encrypt(p_cb->devcb.er, BT_OCTET16_LEN, (UINT8 *)&div, 2, &output);
1373 btm_enc_proc_ltk(&output);
1374 }
1375 else
1376 {
Sharvil Nanavati83c52562014-05-04 00:46:57 -07001377 BTM_TRACE_DEBUG ("LTK request failed - send negative reply");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001378 btsnd_hcic_ble_ltk_req_neg_reply(p_cb->enc_handle);
1379 if (p_dev_rec)
1380 btm_ble_link_encrypted(p_dev_rec->bd_addr, 0);
1381
1382 }
1383 }
1384}
1385
1386/*******************************************************************************
1387**
1388** Function btm_ble_ltk_request_reply
1389**
1390** Description This function is called to send a LTK request reply on a slave
1391** device.
1392**
1393** Returns void
1394**
1395*******************************************************************************/
1396void btm_ble_ltk_request_reply(BD_ADDR bda, BOOLEAN use_stk, BT_OCTET16 stk)
1397{
1398 tBTM_SEC_DEV_REC *p_rec = btm_find_dev (bda);
1399 tBTM_CB *p_cb = &btm_cb;
1400 tSMP_ENC output;
1401
1402 if (p_rec == NULL)
1403 {
Sharvil Nanavati83c52562014-05-04 00:46:57 -07001404 BTM_TRACE_ERROR("btm_ble_ltk_request_reply received for unknown device");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001405 return;
1406 }
1407
Sharvil Nanavati83c52562014-05-04 00:46:57 -07001408 BTM_TRACE_DEBUG ("btm_ble_ltk_request_reply");
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -07001409 p_cb->enc_handle = p_rec->ble_hci_handle;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001410 p_cb->key_size = p_rec->ble.keys.key_size;
1411
Sharvil Nanavati83c52562014-05-04 00:46:57 -07001412 BTM_TRACE_ERROR("key size = %d", p_rec->ble.keys.key_size);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001413 if (use_stk)
1414 {
1415 btsnd_hcic_ble_ltk_req_reply(btm_cb.enc_handle, stk);
1416 }
1417 else /* calculate LTK using peer device */
1418 {
1419 /* generate Y= Encrypt(DHK, Rand) received from encrypt request */
1420 SMP_Encrypt(p_cb->devcb.id_keys.dhk, BT_OCTET16_LEN, p_cb->enc_rand,
1421 BT_OCTET8_LEN, &output);
1422 btm_enc_proc_slave_y(&output);
1423 }
1424}
1425
1426/*******************************************************************************
1427**
1428** Function btm_ble_io_capabilities_req
1429**
1430** Description This function is called to handle SMP get IO capability request.
1431**
1432** Returns void
1433**
1434*******************************************************************************/
1435UINT8 btm_ble_io_capabilities_req(tBTM_SEC_DEV_REC *p_dev_rec, tBTM_LE_IO_REQ *p_data)
1436{
1437 UINT8 callback_rc = BTM_SUCCESS;
Sharvil Nanavati83c52562014-05-04 00:46:57 -07001438 BTM_TRACE_DEBUG ("btm_ble_io_capabilities_req");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001439 if (btm_cb.api.p_le_callback)
1440 {
1441 /* the callback function implementation may change the IO capability... */
1442 callback_rc = (*btm_cb.api.p_le_callback) (BTM_LE_IO_REQ_EVT, p_dev_rec->bd_addr, (tBTM_LE_EVT_DATA *)p_data);
1443 }
1444#if BTM_OOB_INCLUDED == TRUE
1445 if ((callback_rc == BTM_SUCCESS) || (BTM_OOB_UNKNOWN != p_data->oob_data))
1446#else
1447 if (callback_rc == BTM_SUCCESS)
1448#endif
1449 {
Satya Callojiffb39602014-04-30 15:55:39 -07001450#if BTM_BLE_CONFORMANCE_TESTING == TRUE
1451 if (btm_cb.devcb.keep_rfu_in_auth_req)
1452 {
Sharvil Nanavati83c52562014-05-04 00:46:57 -07001453 BTM_TRACE_DEBUG ("btm_ble_io_capabilities_req keep_rfu_in_auth_req = %u",
Satya Callojiffb39602014-04-30 15:55:39 -07001454 btm_cb.devcb.keep_rfu_in_auth_req);
1455 p_data->auth_req &= BTM_LE_AUTH_REQ_MASK_KEEP_RFU;
1456 btm_cb.devcb.keep_rfu_in_auth_req = FALSE;
1457 }
1458 else
1459 { /* default */
1460 p_data->auth_req &= BTM_LE_AUTH_REQ_MASK;
1461 }
1462#else
The Android Open Source Project5738f832012-12-12 16:00:35 -08001463 p_data->auth_req &= BTM_LE_AUTH_REQ_MASK;
Satya Callojiffb39602014-04-30 15:55:39 -07001464#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08001465
Sharvil Nanavati83c52562014-05-04 00:46:57 -07001466 BTM_TRACE_DEBUG ("btm_ble_io_capabilities_req 1: p_dev_rec->security_required = %d auth_req:%d",
The Android Open Source Project5738f832012-12-12 16:00:35 -08001467 p_dev_rec->security_required, p_data->auth_req);
Sharvil Nanavati83c52562014-05-04 00:46:57 -07001468 BTM_TRACE_DEBUG ("btm_ble_io_capabilities_req 2: i_keys=0x%x r_keys=0x%x (bit 0-LTK 1-IRK 2-CSRK)",
The Android Open Source Project5738f832012-12-12 16:00:35 -08001469 p_data->init_keys,
1470 p_data->resp_keys);
1471
1472 /* if authentication requires MITM protection, put on the mask */
1473 if (p_dev_rec->security_required & BTM_SEC_IN_MITM)
1474 p_data->auth_req |= BTM_LE_AUTH_REQ_MITM;
1475
1476 if (!(p_data->auth_req & SMP_AUTH_BOND))
1477 {
Sharvil Nanavati83c52562014-05-04 00:46:57 -07001478 BTM_TRACE_DEBUG("Non bonding: No keys should be exchanged");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001479 p_data->init_keys = 0;
1480 p_data->resp_keys = 0;
1481 }
1482
Sharvil Nanavati83c52562014-05-04 00:46:57 -07001483 BTM_TRACE_DEBUG ("btm_ble_io_capabilities_req 3: auth_req:%d", p_data->auth_req);
1484 BTM_TRACE_DEBUG ("btm_ble_io_capabilities_req 4: i_keys=0x%x r_keys=0x%x",
The Android Open Source Project5738f832012-12-12 16:00:35 -08001485 p_data->init_keys,
1486 p_data->resp_keys);
1487
Sharvil Nanavati83c52562014-05-04 00:46:57 -07001488 BTM_TRACE_DEBUG ("btm_ble_io_capabilities_req 5: p_data->io_cap = %d auth_req:%d",
The Android Open Source Project5738f832012-12-12 16:00:35 -08001489 p_data->io_cap, p_data->auth_req);
1490
1491 /* remove MITM protection requirement if IO cap does not allow it */
1492 if ((p_data->io_cap == BTM_IO_CAP_NONE) && p_data->oob_data == SMP_OOB_NONE)
1493 p_data->auth_req &= ~BTM_LE_AUTH_REQ_MITM;
1494
Sharvil Nanavati83c52562014-05-04 00:46:57 -07001495 BTM_TRACE_DEBUG ("btm_ble_io_capabilities_req 6: IO_CAP:%d oob_data:%d auth_req:%d",
The Android Open Source Project5738f832012-12-12 16:00:35 -08001496 p_data->io_cap, p_data->oob_data, p_data->auth_req);
1497 }
1498 return callback_rc;
1499}
1500
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -07001501#if (BLE_PRIVACY_SPT == TRUE )
Zhihai Xu6f908b22014-03-11 15:01:45 -07001502/*******************************************************************************
1503**
1504** Function btm_ble_resolve_random_addr_on_conn_cmpl
1505**
1506** Description resolve random address complete on connection complete event.
1507**
1508** Returns void
1509**
1510*******************************************************************************/
1511static void btm_ble_resolve_random_addr_on_conn_cmpl(void * p_rec, void *p_data)
1512{
1513 UINT8 *p = (UINT8 *)p_data;
1514 tBTM_SEC_DEV_REC *match_rec = (tBTM_SEC_DEV_REC *) p_rec;
1515 UINT8 role, status, bda_type;
1516 UINT16 handle;
1517 BD_ADDR bda;
1518 UINT16 conn_interval, conn_latency, conn_timeout;
1519 BOOLEAN match = FALSE;
1520
1521 STREAM_TO_UINT8 (status, p);
1522 STREAM_TO_UINT16 (handle, p);
1523 STREAM_TO_UINT8 (role, p);
1524 STREAM_TO_UINT8 (bda_type, p);
1525 STREAM_TO_BDADDR (bda, p);
1526 STREAM_TO_UINT16 (conn_interval, p);
1527 STREAM_TO_UINT16 (conn_latency, p);
1528 STREAM_TO_UINT16 (conn_timeout, p);
1529
1530 handle = HCID_GET_HANDLE (handle);
1531
Sharvil Nanavati83c52562014-05-04 00:46:57 -07001532 BTM_TRACE_EVENT ("btm_ble_resolve_random_addr_master_cmpl");
Zhihai Xu6f908b22014-03-11 15:01:45 -07001533
1534 if (match_rec)
1535 {
Sharvil Nanavati83c52562014-05-04 00:46:57 -07001536 BTM_TRACE_ERROR("Random match");
Zhihai Xu6f908b22014-03-11 15:01:45 -07001537 match = TRUE;
1538 match_rec->ble.active_addr_type = BTM_BLE_ADDR_RRA;
1539 memcpy(match_rec->ble.cur_rand_addr, bda, BD_ADDR_LEN);
1540 memcpy(bda, match_rec->bd_addr, BD_ADDR_LEN);
1541 }
1542 else
1543 {
Sharvil Nanavati83c52562014-05-04 00:46:57 -07001544 BTM_TRACE_ERROR("Random unmatch");
Zhihai Xu6f908b22014-03-11 15:01:45 -07001545 }
1546
1547 btm_ble_connected(bda, handle, HCI_ENCRYPT_MODE_DISABLED, role, bda_type, match);
1548
1549 l2cble_conn_comp (handle, role, bda, bda_type, conn_interval,
1550 conn_latency, conn_timeout);
1551
1552 return;
1553}
1554#endif
1555
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001556/*******************************************************************************
1557**
1558** Function btm_ble_connected
1559**
1560** Description This function is when a LE connection to the peer device is
1561** establsihed
1562**
1563** Returns void
1564**
1565*******************************************************************************/
1566void btm_ble_connected (UINT8 *bda, UINT16 handle, UINT8 enc_mode, UINT8 role,
1567 tBLE_ADDR_TYPE addr_type, BOOLEAN addr_matched)
1568{
1569 tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev (bda);
1570 tBTM_BLE_CB *p_cb = &btm_cb.ble_ctr_cb;
Mike J. Chen5cd8bff2014-01-31 18:16:59 -08001571 UNUSED(addr_matched);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001572
Sharvil Nanavati83c52562014-05-04 00:46:57 -07001573 BTM_TRACE_EVENT ("btm_ble_connected");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001574
1575 /* Commenting out trace due to obf/compilation problems.
1576 */
1577#if (BT_USE_TRACES == TRUE)
1578 if (p_dev_rec)
1579 {
Sharvil Nanavati83c52562014-05-04 00:46:57 -07001580 BTM_TRACE_EVENT ("Security Manager: btm_ble_connected : handle:%d enc_mode:%d bda:%x RName:%s",
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001581 handle, enc_mode,
1582 (bda[2]<<24)+(bda[3]<<16)+(bda[4]<<8)+bda[5],
1583 p_dev_rec->sec_bd_name);
1584
Sharvil Nanavati83c52562014-05-04 00:46:57 -07001585 BTM_TRACE_DEBUG ("btm_ble_connected sec_flags=0x%x",p_dev_rec->sec_flags);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001586 }
1587 else
1588 {
Sharvil Nanavati83c52562014-05-04 00:46:57 -07001589 BTM_TRACE_EVENT ("Security Manager: btm_ble_connected: handle:%d enc_mode:%d bda:%x ",
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001590 handle, enc_mode,
1591 (bda[2]<<24)+(bda[3]<<16)+(bda[4]<<8)+bda[5]);
1592 }
1593#endif
1594
1595 if (!p_dev_rec)
1596 {
1597 /* There is no device record for new connection. Allocate one */
1598 p_dev_rec = btm_sec_alloc_dev (bda);
1599 }
1600 else /* Update the timestamp for this device */
1601 {
1602 p_dev_rec->timestamp = btm_cb.dev_rec_count++;
1603 }
1604
1605 /* update device information */
1606 p_dev_rec->device_type |= BT_DEVICE_TYPE_BLE;
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -07001607 p_dev_rec->ble_hci_handle = handle;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001608 p_dev_rec->ble.ble_addr_type = addr_type;
1609
Zhihai Xu6f908b22014-03-11 15:01:45 -07001610 p_dev_rec->role_master = FALSE;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001611 if (role == HCI_ROLE_MASTER)
1612 p_dev_rec->role_master = TRUE;
1613
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -07001614#if (defined BLE_PRIVACY_SPT && BLE_PRIVACY_SPT == TRUE)
Zhihai Xu6f908b22014-03-11 15:01:45 -07001615 if (!addr_matched)
1616 p_dev_rec->ble.active_addr_type = BTM_BLE_ADDR_PSEUDO;
1617
1618 if (p_dev_rec->ble.ble_addr_type == BLE_ADDR_RANDOM && !addr_matched)
1619 memcpy(p_dev_rec->ble.cur_rand_addr, bda, BD_ADDR_LEN);
Satya Calloji0943c102014-05-12 09:13:02 -07001620
1621#if (defined BLE_VND_INCLUDED && BLE_VND_INCLUDED == TRUE)
1622 btm_ble_vendor_disable_irk_list();
1623#endif
Zhihai Xu6f908b22014-03-11 15:01:45 -07001624#endif
1625
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001626 if (role == HCI_ROLE_SLAVE)
1627 p_cb->inq_var.adv_mode = BTM_BLE_ADV_DISABLE;
1628 p_cb->inq_var.directed_conn = FALSE;
1629
1630 return;
1631}
1632
1633/*****************************************************************************
1634** Function btm_ble_conn_complete
1635**
1636** Description LE connection complete.
1637**
1638******************************************************************************/
1639void btm_ble_conn_complete(UINT8 *p, UINT16 evt_len)
1640{
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -07001641#if (BLE_PRIVACY_SPT == TRUE )
Zhihai Xu6f908b22014-03-11 15:01:45 -07001642 UINT8 *p_data = p;
1643#endif
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001644 UINT8 role, status, bda_type;
1645 UINT16 handle;
1646 BD_ADDR bda;
1647 UINT16 conn_interval, conn_latency, conn_timeout;
1648 BOOLEAN match = FALSE;
Mike J. Chen5cd8bff2014-01-31 18:16:59 -08001649 UNUSED(evt_len);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001650
1651 STREAM_TO_UINT8 (status, p);
1652 STREAM_TO_UINT16 (handle, p);
1653 STREAM_TO_UINT8 (role, p);
1654 STREAM_TO_UINT8 (bda_type, p);
1655 STREAM_TO_BDADDR (bda, p);
1656
1657 if (status == 0)
1658 {
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -07001659#if (BLE_PRIVACY_SPT == TRUE )
Satya Calloji0943c102014-05-12 09:13:02 -07001660#if (BLE_VND_INCLUDED == TRUE)
1661 match = btm_public_addr_to_random_pseudo (bda, &bda_type);
1662#endif
Zhihai Xu6f908b22014-03-11 15:01:45 -07001663 /* possiblly receive connection complete with resolvable random on
1664 slave role while the device has been paired */
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -07001665 if (!match && role == HCI_ROLE_SLAVE && BTM_BLE_IS_RESOLVE_BDA(bda))
Zhihai Xu6f908b22014-03-11 15:01:45 -07001666 {
1667 btm_ble_resolve_random_addr(bda, btm_ble_resolve_random_addr_on_conn_cmpl, p_data);
1668 }
1669 else
1670#endif
1671 {
1672 STREAM_TO_UINT16 (conn_interval, p);
1673 STREAM_TO_UINT16 (conn_latency, p);
1674 STREAM_TO_UINT16 (conn_timeout, p);
1675 handle = HCID_GET_HANDLE (handle);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001676
Zhihai Xu6f908b22014-03-11 15:01:45 -07001677 btm_ble_connected(bda, handle, HCI_ENCRYPT_MODE_DISABLED, role, bda_type, match);
1678 l2cble_conn_comp (handle, role, bda, bda_type, conn_interval,
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001679 conn_latency, conn_timeout);
Zhihai Xu6f908b22014-03-11 15:01:45 -07001680 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001681 }
1682 else
1683 {
1684 role = HCI_ROLE_UNKNOWN;
1685
Satya Callojiffb39602014-04-30 15:55:39 -07001686 if (status != HCI_ERR_DIRECTED_ADVERTISING_TIMEOUT)
Zhihai Xu83d709a2014-01-14 17:46:26 -08001687 {
Satya Callojiffb39602014-04-30 15:55:39 -07001688 btm_ble_set_conn_st(BLE_CONN_IDLE);
Zhihai Xu83d709a2014-01-14 17:46:26 -08001689 }
1690 /* this is to work around broadcom firmware problem to handle
1691 * unsolicited command complete event for HCI_LE_Create_Connection_Cancel
1692 * and LE connection complete event with status error code (0x2)
1693 * unknown connection identifier from bluetooth controller
1694 * the workaround is to release the HCI connection to avoid out of sync
1695 * with bluetooth controller, which cause BT can't be turned off.
1696 */
1697 else if ((status == HCI_ERR_NO_CONNECTION) &&
1698 (btm_ble_get_conn_st() != BLE_CONN_CANCEL))
1699 {
1700 tL2C_LCB *p_lcb;
1701 handle = HCID_GET_HANDLE (handle);
1702 p_lcb = l2cu_find_lcb_by_handle (handle);
1703 if (p_lcb != NULL)
1704 {
1705 l2c_link_hci_disc_comp (handle, HCI_ERR_PEER_USER);
1706 btm_sec_disconnected (handle, HCI_ERR_PEER_USER);
1707 }
1708 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001709 }
Andre Eisenbach6975b4d2013-08-05 16:55:38 -07001710 btm_ble_set_conn_st(BLE_CONN_IDLE);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001711 btm_ble_update_mode_operation(role, bda, TRUE);
1712}
1713
The Android Open Source Project5738f832012-12-12 16:00:35 -08001714/*****************************************************************************
1715** Function btm_proc_smp_cback
1716**
1717** Description This function is the SMP callback handler.
1718**
1719******************************************************************************/
1720UINT8 btm_proc_smp_cback(tSMP_EVT event, BD_ADDR bd_addr, tSMP_EVT_DATA *p_data)
1721{
1722 tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev (bd_addr);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001723 UINT8 res = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001724
Sharvil Nanavati83c52562014-05-04 00:46:57 -07001725 BTM_TRACE_DEBUG ("btm_proc_smp_cback event = %d", event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001726
1727 if (p_dev_rec != NULL)
1728 {
1729 switch (event)
1730 {
1731 case SMP_IO_CAP_REQ_EVT:
1732 btm_ble_io_capabilities_req(p_dev_rec, (tBTM_LE_IO_REQ *)&p_data->io_req);
1733 break;
1734
1735 case SMP_PASSKEY_REQ_EVT:
1736 case SMP_PASSKEY_NOTIF_EVT:
1737 case SMP_OOB_REQ_EVT:
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -07001738 p_dev_rec->sec_flags |= BTM_SEC_LE_AUTHENTICATED;
1739
The Android Open Source Project5738f832012-12-12 16:00:35 -08001740 case SMP_SEC_REQUEST_EVT:
Andre Eisenbach6975b4d2013-08-05 16:55:38 -07001741 memcpy (btm_cb.pairing_bda, bd_addr, BD_ADDR_LEN);
1742 p_dev_rec->sec_state = BTM_SEC_STATE_AUTHENTICATING;
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -07001743 btm_cb.pairing_flags |= BTM_PAIR_FLAGS_LE_ACTIVE;
Andre Eisenbach6975b4d2013-08-05 16:55:38 -07001744 /* fall through */
The Android Open Source Project5738f832012-12-12 16:00:35 -08001745 case SMP_COMPLT_EVT:
1746 if (btm_cb.api.p_le_callback)
1747 {
1748 /* the callback function implementation may change the IO capability... */
Sharvil Nanavati83c52562014-05-04 00:46:57 -07001749 BTM_TRACE_DEBUG ("btm_cb.api.p_le_callback=0x%x", btm_cb.api.p_le_callback );
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001750 (*btm_cb.api.p_le_callback) (event, bd_addr, (tBTM_LE_EVT_DATA *)p_data);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001751 }
1752
1753 if (event == SMP_COMPLT_EVT)
1754 {
Sharvil Nanavati83c52562014-05-04 00:46:57 -07001755 BTM_TRACE_DEBUG ("evt=SMP_COMPLT_EVT before update sec_level=0x%x sec_flags=0x%x", p_data->cmplt.sec_level , p_dev_rec->sec_flags );
The Android Open Source Project5738f832012-12-12 16:00:35 -08001756
1757 res = (p_data->cmplt.reason == SMP_SUCCESS) ? BTM_SUCCESS : BTM_ERR_PROCESSING;
1758
Sharvil Nanavati83c52562014-05-04 00:46:57 -07001759 BTM_TRACE_DEBUG ("after update result=%d sec_level=0x%x sec_flags=0x%x",
The Android Open Source Project5738f832012-12-12 16:00:35 -08001760 res, p_data->cmplt.sec_level , p_dev_rec->sec_flags );
1761
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -07001762 btm_sec_dev_rec_cback_event(p_dev_rec, res, TRUE);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001763
1764 if (p_data->cmplt.is_pair_cancel && btm_cb.api.p_bond_cancel_cmpl_callback )
1765 {
Sharvil Nanavati83c52562014-05-04 00:46:57 -07001766 BTM_TRACE_DEBUG ("Pairing Cancel completed");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001767 (*btm_cb.api.p_bond_cancel_cmpl_callback)(BTM_SUCCESS);
1768 }
1769#if BTM_BLE_CONFORMANCE_TESTING == TRUE
1770 if (res != BTM_SUCCESS)
1771 {
Andre Eisenbach6975b4d2013-08-05 16:55:38 -07001772 if (!btm_cb.devcb.no_disc_if_pair_fail && p_data->cmplt.reason != SMP_CONN_TOUT)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001773 {
Sharvil Nanavati83c52562014-05-04 00:46:57 -07001774 BTM_TRACE_DEBUG ("Pairing failed - Remove ACL");
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -07001775 btm_remove_acl(bd_addr, BT_TRANSPORT_LE);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001776 }
1777 else
1778 {
Sharvil Nanavati83c52562014-05-04 00:46:57 -07001779 BTM_TRACE_DEBUG ("Pairing failed - Not Removing ACL");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001780 p_dev_rec->sec_state = BTM_SEC_STATE_IDLE;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001781 }
1782 }
1783#else
Andre Eisenbach6975b4d2013-08-05 16:55:38 -07001784 if (res != BTM_SUCCESS && p_data->cmplt.reason != SMP_CONN_TOUT)
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -07001785 btm_remove_acl(bd_addr, BT_TRANSPORT_LE);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001786#endif
1787
Sharvil Nanavati83c52562014-05-04 00:46:57 -07001788 BTM_TRACE_DEBUG ("btm_cb pairing_state=%x pairing_flags=%x pin_code_len=%x",
The Android Open Source Project5738f832012-12-12 16:00:35 -08001789 btm_cb.pairing_state,
1790 btm_cb.pairing_flags,
1791 btm_cb.pin_code_len );
Sharvil Nanavati83c52562014-05-04 00:46:57 -07001792 BTM_TRACE_DEBUG ("btm_cb.pairing_bda %02x:%02x:%02x:%02x:%02x:%02x",
The Android Open Source Project5738f832012-12-12 16:00:35 -08001793 btm_cb.pairing_bda[0], btm_cb.pairing_bda[1], btm_cb.pairing_bda[2],
1794 btm_cb.pairing_bda[3], btm_cb.pairing_bda[4], btm_cb.pairing_bda[5]);
1795
1796 memset (btm_cb.pairing_bda, 0xff, BD_ADDR_LEN);
Andre Eisenbach6975b4d2013-08-05 16:55:38 -07001797 btm_cb.pairing_state = BTM_PAIR_STATE_IDLE;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001798 btm_cb.pairing_flags = 0;
1799 }
1800 break;
1801
1802 default:
Sharvil Nanavati83c52562014-05-04 00:46:57 -07001803 BTM_TRACE_DEBUG ("unknown event = %d", event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001804 break;
1805
1806
1807 }
1808 }
1809 else
1810 {
Sharvil Nanavati83c52562014-05-04 00:46:57 -07001811 BTM_TRACE_ERROR("btm_proc_smp_cback received for unknown device");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001812 }
1813
1814 return 0;
1815}
1816
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001817 #endif /* SMP_INCLUDED */
The Android Open Source Project5738f832012-12-12 16:00:35 -08001818
1819/*******************************************************************************
1820**
1821** Function BTM_BleDataSignature
1822**
1823** Description This function is called to sign the data using AES128 CMAC
1824** algorith.
1825**
1826** Parameter bd_addr: target device the data to be signed for.
1827** p_text: singing data
1828** len: length of the data to be signed.
1829** signature: output parameter where data signature is going to
1830** be stored.
1831**
1832** Returns TRUE if signing sucessul, otherwise FALSE.
1833**
1834*******************************************************************************/
1835BOOLEAN BTM_BleDataSignature (BD_ADDR bd_addr, UINT8 *p_text, UINT16 len,
1836 BLE_SIGNATURE signature)
1837{
1838 BOOLEAN ret = FALSE;
Mike J. Chenbf9a8aa2014-02-11 13:51:29 -08001839#if SMP_INCLUDED == TRUE
The Android Open Source Project5738f832012-12-12 16:00:35 -08001840 tBTM_SEC_DEV_REC *p_rec = btm_find_dev (bd_addr);
1841 UINT8 *p_buf, *pp;
1842
1843 BT_OCTET16 er;
1844 UINT16 div;
1845 UINT8 temp[4]; /* for (r || DIV) r=1*/
1846 UINT16 r=1;
1847 UINT8 *p=temp, *p_mac = (UINT8 *)signature;
1848 tSMP_ENC output;
1849 BT_OCTET16 local_csrk;
1850
Sharvil Nanavati83c52562014-05-04 00:46:57 -07001851 BTM_TRACE_DEBUG ("BTM_BleDataSignature");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001852 if (p_rec == NULL)
1853 {
Sharvil Nanavati83c52562014-05-04 00:46:57 -07001854 BTM_TRACE_ERROR("data signing can not be done from unknow device");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001855 }
1856 else
1857 {
1858 if ((p_buf = (UINT8 *)GKI_getbuf((UINT16)(len + 4))) != NULL)
1859 {
Sharvil Nanavati83c52562014-05-04 00:46:57 -07001860 BTM_TRACE_DEBUG("Start to generate Local CSRK");
Andre Eisenbach6975b4d2013-08-05 16:55:38 -07001861 pp = p_buf;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001862 /* prepare plain text */
1863 if (p_text)
1864 {
1865 memcpy(p_buf, p_text, len);
1866 pp = (p_buf + len);
1867 }
1868
1869#if BTM_BLE_CONFORMANCE_TESTING == TRUE
1870 if ( btm_cb.devcb.enable_test_local_sign_cntr)
1871 {
Sharvil Nanavati83c52562014-05-04 00:46:57 -07001872 BTM_TRACE_DEBUG ("Use Test local counter value from script counter_val=%d", btm_cb.devcb.test_local_sign_cntr);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001873 UINT32_TO_STREAM(pp, btm_cb.devcb.test_local_sign_cntr);
1874 }
1875 else
1876 {
1877 UINT32_TO_STREAM(pp, p_rec->ble.keys.local_counter);
1878 }
1879#else
1880 UINT32_TO_STREAM(pp, p_rec->ble.keys.local_counter);
1881#endif
1882 /* compute local csrk */
1883 if (btm_get_local_div(bd_addr, &div))
1884 {
Sharvil Nanavati83c52562014-05-04 00:46:57 -07001885 BTM_TRACE_DEBUG ("compute_csrk div=%x", div);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001886 BTM_GetDeviceEncRoot(er);
1887
1888 /* CSRK = d1(ER, DIV, 1) */
1889 UINT16_TO_STREAM(p, div);
1890 UINT16_TO_STREAM(p, r);
1891
1892 if (!SMP_Encrypt(er, BT_OCTET16_LEN, temp, 4, &output))
1893 {
Sharvil Nanavati83c52562014-05-04 00:46:57 -07001894 BTM_TRACE_ERROR("Local CSRK generation failed ");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001895 }
1896 else
1897 {
Sharvil Nanavati83c52562014-05-04 00:46:57 -07001898 BTM_TRACE_DEBUG("local CSRK generation success");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001899 memcpy((void *)local_csrk, output.param_buf, BT_OCTET16_LEN);
1900
1901
1902#if BTM_BLE_CONFORMANCE_TESTING == TRUE
1903 if (btm_cb.devcb.enable_test_local_sign_cntr)
1904 {
1905 UINT32_TO_STREAM(p_mac, btm_cb.devcb.test_local_sign_cntr);
1906 }
1907 else
1908 {
1909 UINT32_TO_STREAM(p_mac, p_rec->ble.keys.local_counter);
1910 }
1911#else
1912 UINT32_TO_STREAM(p_mac, p_rec->ble.keys.local_counter);
1913#endif
1914
1915 if ((ret = AES_CMAC(local_csrk, p_buf, (UINT16)(len + 4), BTM_CMAC_TLEN_SIZE, p_mac)) == TRUE)
1916 {
1917 btm_ble_increment_sign_ctr(bd_addr, TRUE);
1918
1919#if BTM_BLE_CONFORMANCE_TESTING == TRUE
1920 if ( btm_cb.devcb.enable_test_mac_val)
1921 {
Sharvil Nanavati83c52562014-05-04 00:46:57 -07001922 BTM_TRACE_DEBUG ("Use MAC value from script");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001923 memcpy(p_mac, btm_cb.devcb.test_mac, BTM_CMAC_TLEN_SIZE);
1924 }
1925#endif
1926 }
Sharvil Nanavati83c52562014-05-04 00:46:57 -07001927 BTM_TRACE_DEBUG("BTM_BleDataSignature p_mac = %d", p_mac);
1928 BTM_TRACE_DEBUG("p_mac[0] = 0x%02x p_mac[1] = 0x%02x p_mac[2] = 0x%02x p_mac[3] = 0x%02x",
The Android Open Source Project5738f832012-12-12 16:00:35 -08001929 *p_mac, *(p_mac + 1), *(p_mac + 2), *(p_mac + 3));
Sharvil Nanavati83c52562014-05-04 00:46:57 -07001930 BTM_TRACE_DEBUG("p_mac[4] = 0x%02x p_mac[5] = 0x%02x p_mac[6] = 0x%02x p_mac[7] = 0x%02x",
The Android Open Source Project5738f832012-12-12 16:00:35 -08001931 *(p_mac + 4), *(p_mac + 5), *(p_mac + 6), *(p_mac + 7));
1932
1933 GKI_freebuf(p_buf);
1934 }
1935 }
1936 }
1937 }
Mike J. Chenbf9a8aa2014-02-11 13:51:29 -08001938#endif /* SMP_INCLUDED */
The Android Open Source Project5738f832012-12-12 16:00:35 -08001939 return ret;
1940}
1941
1942/*******************************************************************************
1943**
1944** Function BTM_BleVerifySignature
1945**
1946** Description This function is called to verify the data signature
1947**
1948** Parameter bd_addr: target device the data to be signed for.
1949** p_orig: original data before signature.
1950** len: length of the signing data
1951** counter: counter used when doing data signing
1952** p_comp: signature to be compared against.
1953
1954** Returns TRUE if signature verified correctly; otherwise FALSE.
1955**
1956*******************************************************************************/
1957BOOLEAN BTM_BleVerifySignature (BD_ADDR bd_addr, UINT8 *p_orig, UINT16 len, UINT32 counter,
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001958 UINT8 *p_comp)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001959{
1960 BOOLEAN verified = FALSE;
Mike J. Chenbf9a8aa2014-02-11 13:51:29 -08001961#if SMP_INCLUDED == TRUE
The Android Open Source Project5738f832012-12-12 16:00:35 -08001962 tBTM_SEC_DEV_REC *p_rec = btm_find_dev (bd_addr);
1963 UINT8 p_mac[BTM_CMAC_TLEN_SIZE];
1964
1965 if (p_rec == NULL || (p_rec && !(p_rec->ble.key_type & BTM_LE_KEY_PCSRK)))
1966 {
Sharvil Nanavati83c52562014-05-04 00:46:57 -07001967 BTM_TRACE_ERROR("can not verify signature for unknown device");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001968 }
1969 else if (counter < p_rec->ble.keys.counter)
1970 {
Sharvil Nanavati83c52562014-05-04 00:46:57 -07001971 BTM_TRACE_ERROR("signature received with out dated sign counter");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001972 }
1973 else if (p_orig == NULL)
1974 {
Sharvil Nanavati83c52562014-05-04 00:46:57 -07001975 BTM_TRACE_ERROR("No signature to verify");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001976 }
1977 else
1978 {
Sharvil Nanavati83c52562014-05-04 00:46:57 -07001979 BTM_TRACE_DEBUG ("BTM_BleVerifySignature rcv_cnt=%d >= expected_cnt=%d", counter, p_rec->ble.keys.counter);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001980
1981 if (AES_CMAC(p_rec->ble.keys.csrk, p_orig, len, BTM_CMAC_TLEN_SIZE, p_mac))
1982 {
1983 if (memcmp(p_mac, p_comp, BTM_CMAC_TLEN_SIZE) == 0)
1984 {
1985 btm_ble_increment_sign_ctr(bd_addr, FALSE);
1986 verified = TRUE;
1987 }
1988 }
1989 }
Mike J. Chenbf9a8aa2014-02-11 13:51:29 -08001990#endif /* SMP_INCLUDED */
The Android Open Source Project5738f832012-12-12 16:00:35 -08001991 return verified;
1992}
1993
The Android Open Source Project5738f832012-12-12 16:00:35 -08001994/*******************************************************************************
1995** Utility functions for LE device IR/ER generation
1996*******************************************************************************/
1997/*******************************************************************************
1998**
1999** Function btm_notify_new_key
2000**
2001** Description This function is to notify application new keys have been
2002** generated.
2003**
2004** Returns void
2005**
2006*******************************************************************************/
2007static void btm_notify_new_key(UINT8 key_type)
2008{
2009 tBTM_BLE_LOCAL_KEYS *p_locak_keys = NULL;
2010
Sharvil Nanavati83c52562014-05-04 00:46:57 -07002011 BTM_TRACE_DEBUG ("btm_notify_new_key key_type=%d", key_type);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002012
2013 if (btm_cb.api.p_le_key_callback)
2014 {
2015 switch (key_type)
2016 {
2017 case BTM_BLE_KEY_TYPE_ID:
Sharvil Nanavati83c52562014-05-04 00:46:57 -07002018 BTM_TRACE_DEBUG ("BTM_BLE_KEY_TYPE_ID");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002019 p_locak_keys = (tBTM_BLE_LOCAL_KEYS *)&btm_cb.devcb.id_keys;
2020 break;
2021
2022 case BTM_BLE_KEY_TYPE_ER:
Sharvil Nanavati83c52562014-05-04 00:46:57 -07002023 BTM_TRACE_DEBUG ("BTM_BLE_KEY_TYPE_ER");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002024 p_locak_keys = (tBTM_BLE_LOCAL_KEYS *)&btm_cb.devcb.er;
2025 break;
2026
2027 default:
Sharvil Nanavati83c52562014-05-04 00:46:57 -07002028 BTM_TRACE_ERROR("unknown key type: %d", key_type);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002029 break;
2030 }
2031 if (p_locak_keys != NULL)
2032 (*btm_cb.api.p_le_key_callback) (key_type, p_locak_keys);
2033 }
2034}
2035
2036/*******************************************************************************
2037**
2038** Function btm_ble_process_er2
2039**
2040** Description This function is called when ER is generated, store it in
2041** local control block.
2042**
2043** Returns void
2044**
2045*******************************************************************************/
2046static void btm_ble_process_er2(tBTM_RAND_ENC *p)
2047{
Sharvil Nanavati83c52562014-05-04 00:46:57 -07002048 BTM_TRACE_DEBUG ("btm_ble_process_er2");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002049
2050 if (p &&p->opcode == HCI_BLE_RAND)
2051 {
2052 memcpy(&btm_cb.devcb.er[8], p->param_buf, BT_OCTET8_LEN);
2053 btm_notify_new_key(BTM_BLE_KEY_TYPE_ER);
2054 }
2055 else
2056 {
Sharvil Nanavati83c52562014-05-04 00:46:57 -07002057 BTM_TRACE_ERROR("Generating ER2 exception.");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002058 memset(&btm_cb.devcb.er, 0, sizeof(BT_OCTET16));
2059 }
2060}
2061
2062/*******************************************************************************
2063**
2064** Function btm_ble_process_er
2065**
2066** Description This function is called when ER is generated, store it in
2067** local control block.
2068**
2069** Returns void
2070**
2071*******************************************************************************/
2072static void btm_ble_process_er(tBTM_RAND_ENC *p)
2073{
Sharvil Nanavati83c52562014-05-04 00:46:57 -07002074 BTM_TRACE_DEBUG ("btm_ble_process_er");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002075
2076 if (p &&p->opcode == HCI_BLE_RAND)
2077 {
2078 memcpy(&btm_cb.devcb.er[0], p->param_buf, BT_OCTET8_LEN);
2079
2080 if (!btsnd_hcic_ble_rand((void *)btm_ble_process_er2))
2081 {
2082 memset(&btm_cb.devcb.er, 0, sizeof(BT_OCTET16));
Sharvil Nanavati83c52562014-05-04 00:46:57 -07002083 BTM_TRACE_ERROR("Generating ER2 failed.");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002084 }
2085 }
2086 else
2087 {
Sharvil Nanavati83c52562014-05-04 00:46:57 -07002088 BTM_TRACE_ERROR("Generating ER1 exception.");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002089 }
2090}
2091
2092/*******************************************************************************
2093**
2094** Function btm_ble_process_irk
2095**
2096** Description This function is called when IRK is generated, store it in
2097** local control block.
2098**
2099** Returns void
2100**
2101*******************************************************************************/
2102static void btm_ble_process_irk(tSMP_ENC *p)
2103{
Sharvil Nanavati83c52562014-05-04 00:46:57 -07002104 BTM_TRACE_DEBUG ("btm_ble_process_irk");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002105 if (p &&p->opcode == HCI_BLE_ENCRYPT)
2106 {
2107 memcpy(btm_cb.devcb.id_keys.irk, p->param_buf, BT_OCTET16_LEN);
2108 btm_notify_new_key(BTM_BLE_KEY_TYPE_ID);
2109 }
2110 else
2111 {
Sharvil Nanavati83c52562014-05-04 00:46:57 -07002112 BTM_TRACE_ERROR("Generating IRK exception.");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002113 }
2114
2115 /* proceed generate ER */
2116 if (!btsnd_hcic_ble_rand((void *)btm_ble_process_er))
2117 {
Sharvil Nanavati83c52562014-05-04 00:46:57 -07002118 BTM_TRACE_ERROR("Generating ER failed.");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002119 }
2120}
2121
2122/*******************************************************************************
2123**
2124** Function btm_ble_process_dhk
2125**
2126** Description This function is called when DHK is calculated, store it in
2127** local control block, and proceed to generate ER, a 128-bits
2128** random number.
2129**
2130** Returns void
2131**
2132*******************************************************************************/
2133static void btm_ble_process_dhk(tSMP_ENC *p)
2134{
2135#if SMP_INCLUDED == TRUE
2136 UINT8 btm_ble_irk_pt = 0x01;
2137 tSMP_ENC output;
2138
Sharvil Nanavati83c52562014-05-04 00:46:57 -07002139 BTM_TRACE_DEBUG ("btm_ble_process_dhk");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002140
2141 if (p && p->opcode == HCI_BLE_ENCRYPT)
2142 {
2143 memcpy(btm_cb.devcb.id_keys.dhk, p->param_buf, BT_OCTET16_LEN);
Sharvil Nanavati83c52562014-05-04 00:46:57 -07002144 BTM_TRACE_DEBUG("BLE DHK generated.");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002145
2146 /* IRK = D1(IR, 1) */
2147 if (!SMP_Encrypt(btm_cb.devcb.id_keys.ir, BT_OCTET16_LEN, &btm_ble_irk_pt,
2148 1, &output))
2149 {
2150 /* reset all identity root related key */
2151 memset(&btm_cb.devcb.id_keys, 0, sizeof(tBTM_BLE_LOCAL_ID_KEYS));
2152 }
2153 else
2154 {
2155 btm_ble_process_irk(&output);
2156 }
2157 }
2158 else
2159 {
2160 /* reset all identity root related key */
2161 memset(&btm_cb.devcb.id_keys, 0, sizeof(tBTM_BLE_LOCAL_ID_KEYS));
2162 }
2163#endif
2164}
2165
2166/*******************************************************************************
2167**
2168** Function btm_ble_process_ir2
2169**
2170** Description This function is called when IR is generated, proceed to calculate
2171** DHK = Eir({0x03, 0, 0 ...})
2172**
2173**
2174** Returns void
2175**
2176*******************************************************************************/
2177static void btm_ble_process_ir2(tBTM_RAND_ENC *p)
2178{
2179#if SMP_INCLUDED == TRUE
2180 UINT8 btm_ble_dhk_pt = 0x03;
2181 tSMP_ENC output;
2182
Sharvil Nanavati83c52562014-05-04 00:46:57 -07002183 BTM_TRACE_DEBUG ("btm_ble_process_ir2");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002184
2185 if (p && p->opcode == HCI_BLE_RAND)
2186 {
2187 /* remembering in control block */
2188 memcpy(&btm_cb.devcb.id_keys.ir[8], p->param_buf, BT_OCTET8_LEN);
2189 /* generate DHK= Eir({0x03, 0x00, 0x00 ...}) */
2190
2191
2192 SMP_Encrypt(btm_cb.devcb.id_keys.ir, BT_OCTET16_LEN, &btm_ble_dhk_pt,
2193 1, &output);
2194 btm_ble_process_dhk(&output);
2195
Sharvil Nanavati83c52562014-05-04 00:46:57 -07002196 BTM_TRACE_DEBUG("BLE IR generated.");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002197 }
2198 else
2199 {
2200 memset(&btm_cb.devcb.id_keys, 0, sizeof(tBTM_BLE_LOCAL_ID_KEYS));
2201 }
2202#endif
2203}
2204
2205/*******************************************************************************
2206**
2207** Function btm_ble_process_ir
2208**
2209** Description This function is called when IR is generated, proceed to calculate
2210** DHK = Eir({0x02, 0, 0 ...})
2211**
2212**
2213** Returns void
2214**
2215*******************************************************************************/
2216static void btm_ble_process_ir(tBTM_RAND_ENC *p)
2217{
Sharvil Nanavati83c52562014-05-04 00:46:57 -07002218 BTM_TRACE_DEBUG ("btm_ble_process_ir");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002219
2220 if (p && p->opcode == HCI_BLE_RAND)
2221 {
2222 /* remembering in control block */
2223 memcpy(btm_cb.devcb.id_keys.ir, p->param_buf, BT_OCTET8_LEN);
2224
2225 if (!btsnd_hcic_ble_rand((void *)btm_ble_process_ir2))
2226 {
Sharvil Nanavati83c52562014-05-04 00:46:57 -07002227 BTM_TRACE_ERROR("Generating IR2 failed.");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002228 memset(&btm_cb.devcb.id_keys, 0, sizeof(tBTM_BLE_LOCAL_ID_KEYS));
2229 }
2230 }
2231}
2232
2233/*******************************************************************************
2234**
2235** Function btm_ble_reset_id
2236**
2237** Description This function is called to reset LE device identity.
2238**
2239** Returns void
2240**
2241*******************************************************************************/
2242void btm_ble_reset_id( void )
2243{
Sharvil Nanavati83c52562014-05-04 00:46:57 -07002244 BTM_TRACE_DEBUG ("btm_ble_reset_id");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002245
2246 /* regenrate Identity Root*/
2247 if (!btsnd_hcic_ble_rand((void *)btm_ble_process_ir))
2248 {
Sharvil Nanavati83c52562014-05-04 00:46:57 -07002249 BTM_TRACE_DEBUG("Generating IR failed.");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002250 }
2251}
2252
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002253 #if BTM_BLE_CONFORMANCE_TESTING == TRUE
The Android Open Source Project5738f832012-12-12 16:00:35 -08002254/*******************************************************************************
2255**
2256** Function btm_ble_set_no_disc_if_pair_fail
2257**
2258** Description This function indicates that whether no disconnect of the ACL
2259** should be used if pairing failed
2260**
2261** Returns void
2262**
2263*******************************************************************************/
2264void btm_ble_set_no_disc_if_pair_fail(BOOLEAN disable_disc )
2265{
Sharvil Nanavati83c52562014-05-04 00:46:57 -07002266 BTM_TRACE_DEBUG ("btm_ble_set_disc_enable_if_pair_fail disable_disc=%d", disable_disc);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002267 btm_cb.devcb.no_disc_if_pair_fail = disable_disc;
2268}
2269
2270/*******************************************************************************
2271**
2272** Function btm_ble_set_test_mac_value
2273**
2274** Description This function set test MAC value
2275**
2276** Returns void
2277**
2278*******************************************************************************/
2279void btm_ble_set_test_mac_value(BOOLEAN enable, UINT8 *p_test_mac_val )
2280{
Sharvil Nanavati83c52562014-05-04 00:46:57 -07002281 BTM_TRACE_DEBUG ("btm_ble_set_test_mac_value enable=%d", enable);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002282 btm_cb.devcb.enable_test_mac_val = enable;
2283 memcpy(btm_cb.devcb.test_mac, p_test_mac_val, BT_OCTET8_LEN);
2284}
2285
2286/*******************************************************************************
2287**
2288** Function btm_ble_set_test_local_sign_cntr_value
2289**
2290** Description This function set test local sign counter value
2291**
2292** Returns void
2293**
2294*******************************************************************************/
2295void btm_ble_set_test_local_sign_cntr_value(BOOLEAN enable, UINT32 test_local_sign_cntr )
2296{
Sharvil Nanavati83c52562014-05-04 00:46:57 -07002297 BTM_TRACE_DEBUG ("btm_ble_set_test_local_sign_cntr_value enable=%d local_sign_cntr=%d",
The Android Open Source Project5738f832012-12-12 16:00:35 -08002298 enable, test_local_sign_cntr);
2299 btm_cb.devcb.enable_test_local_sign_cntr = enable;
2300 btm_cb.devcb.test_local_sign_cntr = test_local_sign_cntr;
2301}
2302
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002303/*******************************************************************************
2304**
2305** Function btm_set_random_address
2306**
2307** Description This function set a random address to local controller.
2308**
2309** Returns void
2310**
2311*******************************************************************************/
2312void btm_set_random_address(BD_ADDR random_bda)
2313{
2314 tBTM_LE_RANDOM_CB *p_cb = &btm_cb.ble_ctr_cb.addr_mgnt_cb;
2315 BOOLEAN adv_mode = btm_cb.ble_ctr_cb.inq_var.adv_mode ;
2316
Sharvil Nanavati83c52562014-05-04 00:46:57 -07002317 BTM_TRACE_DEBUG ("btm_set_random_address");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002318
2319 if (adv_mode == BTM_BLE_ADV_ENABLE)
2320 btsnd_hcic_ble_set_adv_enable (BTM_BLE_ADV_DISABLE);
2321
2322 memcpy(p_cb->private_addr, random_bda, BD_ADDR_LEN);
2323 btsnd_hcic_ble_set_random_addr(p_cb->private_addr);
2324
2325 if (adv_mode == BTM_BLE_ADV_ENABLE)
2326 btsnd_hcic_ble_set_adv_enable (BTM_BLE_ADV_ENABLE);
2327
2328
2329}
Satya Callojiffb39602014-04-30 15:55:39 -07002330
2331/*******************************************************************************
2332**
2333** Function btm_ble_set_keep_rfu_in_auth_req
2334**
2335** Description This function indicates if RFU bits have to be kept as is
2336** (by default they have to be set to 0 by the sender).
2337**
2338** Returns void
2339**
2340*******************************************************************************/
2341void btm_ble_set_keep_rfu_in_auth_req(BOOLEAN keep_rfu)
2342{
Sharvil Nanavati83c52562014-05-04 00:46:57 -07002343 BTM_TRACE_DEBUG ("btm_ble_set_keep_rfu_in_auth_req keep_rfus=%d", keep_rfu);
Satya Callojiffb39602014-04-30 15:55:39 -07002344 btm_cb.devcb.keep_rfu_in_auth_req = keep_rfu;
2345}
2346
The Android Open Source Project5738f832012-12-12 16:00:35 -08002347#endif /* BTM_BLE_CONFORMANCE_TESTING */
2348
The Android Open Source Project5738f832012-12-12 16:00:35 -08002349#endif /* BLE_INCLUDED */