OSDN Git Service

DO NOT MERGE ANYWHERE SMP: Delay authorization complete.
[android-x86/system-bt.git] / stack / smp / smp_act.c
1 /******************************************************************************
2  *
3  *  Copyright (C) 2003-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 #include <string.h>
20 #include "device/include/interop.h"
21 #include "include/bt_target.h"
22 #include "stack/btm/btm_int.h"
23 #include "stack/include/l2c_api.h"
24 #include "stack/smp/smp_int.h"
25 #include "utils/include/bt_utils.h"
26
27 #if SMP_INCLUDED == TRUE
28 const UINT8 smp_association_table[2][SMP_IO_CAP_MAX][SMP_IO_CAP_MAX] =
29 {
30     /* initiator */
31     {{SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_PASSKEY,   SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_PASSKEY}, /* Display Only */
32         {SMP_MODEL_ENCRYPTION_ONLY,  SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_PASSKEY, SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_PASSKEY}, /* SMP_CAP_IO = 1 */
33         {SMP_MODEL_KEY_NOTIF, SMP_MODEL_KEY_NOTIF,  SMP_MODEL_PASSKEY,   SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_KEY_NOTIF}, /* keyboard only */
34         {SMP_MODEL_ENCRYPTION_ONLY,  SMP_MODEL_ENCRYPTION_ONLY,   SMP_MODEL_ENCRYPTION_ONLY,  SMP_MODEL_ENCRYPTION_ONLY,    SMP_MODEL_ENCRYPTION_ONLY},/* No Input No Output */
35         {SMP_MODEL_KEY_NOTIF, SMP_MODEL_KEY_NOTIF,  SMP_MODEL_PASSKEY,   SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_KEY_NOTIF}}, /* keyboard display */
36     /* responder */
37     {{SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_ENCRYPTION_ONLY,   SMP_MODEL_KEY_NOTIF, SMP_MODEL_ENCRYPTION_ONLY,    SMP_MODEL_KEY_NOTIF}, /* Display Only */
38         {SMP_MODEL_ENCRYPTION_ONLY,  SMP_MODEL_ENCRYPTION_ONLY,   SMP_MODEL_KEY_NOTIF,   SMP_MODEL_ENCRYPTION_ONLY,    SMP_MODEL_KEY_NOTIF}, /* SMP_CAP_IO = 1 */
39         {SMP_MODEL_PASSKEY,   SMP_MODEL_PASSKEY,    SMP_MODEL_PASSKEY,   SMP_MODEL_ENCRYPTION_ONLY,    SMP_MODEL_PASSKEY}, /* keyboard only */
40         {SMP_MODEL_ENCRYPTION_ONLY,  SMP_MODEL_ENCRYPTION_ONLY,   SMP_MODEL_ENCRYPTION_ONLY,  SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_ENCRYPTION_ONLY},/* No Input No Output */
41         {SMP_MODEL_PASSKEY,   SMP_MODEL_PASSKEY,    SMP_MODEL_KEY_NOTIF, SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_PASSKEY}} /* keyboard display */
42     /* display only */    /*SMP_CAP_IO = 1 */  /* keyboard only */   /* No InputOutput */  /* keyboard display */
43 };
44
45 #define SMP_KEY_DIST_TYPE_MAX       4
46 const tSMP_ACT smp_distribute_act [] =
47 {
48     smp_generate_ltk,
49     smp_send_id_info,
50     smp_generate_csrk,
51     smp_set_derive_link_key
52 };
53
54 static bool lmp_version_below(BD_ADDR bda, uint8_t version)
55 {
56     tACL_CONN *acl = btm_bda_to_acl(bda, BT_TRANSPORT_LE);
57     if (acl == NULL || acl->lmp_version == 0)
58     {
59         SMP_TRACE_WARNING("%s cannot retrieve LMP version...", __func__);
60         return false;
61     }
62     SMP_TRACE_WARNING("%s LMP version %d < %d", __func__, acl->lmp_version, version);
63     return acl->lmp_version < version;
64 }
65
66 /*******************************************************************************
67 ** Function         smp_update_key_mask
68 ** Description      This function updates the key mask for sending or receiving.
69 *******************************************************************************/
70 static void smp_update_key_mask (tSMP_CB *p_cb, UINT8 key_type, BOOLEAN recv)
71 {
72     SMP_TRACE_DEBUG("%s before update role=%d recv=%d local_i_key = %02x, local_r_key = %02x",
73         __func__, p_cb->role, recv, p_cb->local_i_key, p_cb->local_r_key);
74
75     if (((p_cb->le_secure_connections_mode_is_used) ||
76         (p_cb->smp_over_br)) &&
77         ((key_type == SMP_SEC_KEY_TYPE_ENC) || (key_type == SMP_SEC_KEY_TYPE_LK)))
78     {
79         /* in LE SC mode LTK, CSRK and BR/EDR LK are derived locally instead of
80         ** being exchanged with the peer */
81         p_cb->local_i_key &= ~key_type;
82         p_cb->local_r_key &= ~key_type;
83     }
84     else
85     if (p_cb->role == HCI_ROLE_SLAVE)
86     {
87         if (recv)
88             p_cb->local_i_key &= ~key_type;
89         else
90             p_cb->local_r_key &= ~key_type;
91     }
92     else
93     {
94         if (recv)
95             p_cb->local_r_key &= ~key_type;
96         else
97             p_cb->local_i_key &= ~key_type;
98     }
99
100     SMP_TRACE_DEBUG("updated local_i_key = %02x, local_r_key = %02x", p_cb->local_i_key,
101                       p_cb->local_r_key);
102 }
103
104 /*******************************************************************************
105 ** Function     smp_send_app_cback
106 ** Description  notifies application about the events the application is interested in
107 *******************************************************************************/
108 void smp_send_app_cback(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
109 {
110     tSMP_EVT_DATA   cb_data;
111     tSMP_STATUS callback_rc;
112     SMP_TRACE_DEBUG("%s p_cb->cb_evt=%d", __func__, p_cb->cb_evt);
113     if (p_cb->p_callback && p_cb->cb_evt != 0)
114     {
115         switch (p_cb->cb_evt)
116         {
117             case SMP_IO_CAP_REQ_EVT:
118                 cb_data.io_req.auth_req = p_cb->peer_auth_req;
119                 cb_data.io_req.oob_data = SMP_OOB_NONE;
120                 cb_data.io_req.io_cap = SMP_DEFAULT_IO_CAPS;
121                 cb_data.io_req.max_key_size = SMP_MAX_ENC_KEY_SIZE;
122                 cb_data.io_req.init_keys = p_cb->local_i_key ;
123                 cb_data.io_req.resp_keys = p_cb->local_r_key ;
124                 SMP_TRACE_WARNING ( "io_cap = %d",cb_data.io_req.io_cap);
125                 break;
126
127             case SMP_NC_REQ_EVT:
128                 cb_data.passkey = p_data->passkey;
129                 break;
130             case SMP_SC_OOB_REQ_EVT:
131                 cb_data.req_oob_type = p_data->req_oob_type;
132                 break;
133             case SMP_SC_LOC_OOB_DATA_UP_EVT:
134                 cb_data.loc_oob_data = p_cb->sc_oob_data.loc_oob_data;
135                 break;
136
137             case SMP_BR_KEYS_REQ_EVT:
138                 cb_data.io_req.auth_req = 0;
139                 cb_data.io_req.oob_data = SMP_OOB_NONE;
140                 cb_data.io_req.io_cap = 0;
141                 cb_data.io_req.max_key_size = SMP_MAX_ENC_KEY_SIZE;
142                 cb_data.io_req.init_keys = SMP_BR_SEC_DEFAULT_KEY;
143                 cb_data.io_req.resp_keys = SMP_BR_SEC_DEFAULT_KEY;
144                 break;
145
146             default:
147                 break;
148         }
149
150         callback_rc = (*p_cb->p_callback)(p_cb->cb_evt, p_cb->pairing_bda, &cb_data);
151
152         SMP_TRACE_DEBUG("callback_rc=%d  p_cb->cb_evt=%d",callback_rc, p_cb->cb_evt );
153
154         if (callback_rc == SMP_SUCCESS)
155         {
156             switch (p_cb->cb_evt)
157             {
158                 case SMP_IO_CAP_REQ_EVT:
159                     p_cb->loc_auth_req   = cb_data.io_req.auth_req;
160                     p_cb->local_io_capability  = cb_data.io_req.io_cap;
161                     p_cb->loc_oob_flag = cb_data.io_req.oob_data;
162                     p_cb->loc_enc_size = cb_data.io_req.max_key_size;
163                     p_cb->local_i_key = cb_data.io_req.init_keys;
164                     p_cb->local_r_key = cb_data.io_req.resp_keys;
165
166                     if (!(p_cb->loc_auth_req & SMP_AUTH_BOND))
167                     {
168                         SMP_TRACE_WARNING ("Non bonding: No keys will be exchanged");
169                         p_cb->local_i_key = 0;
170                         p_cb->local_r_key = 0;
171                     }
172
173                     SMP_TRACE_WARNING ( "rcvd auth_req: 0x%02x, io_cap: %d \
174                         loc_oob_flag: %d loc_enc_size: %d,"
175                         "local_i_key: 0x%02x, local_r_key: 0x%02x",
176                         p_cb->loc_auth_req, p_cb->local_io_capability, p_cb->loc_oob_flag,
177                         p_cb->loc_enc_size, p_cb->local_i_key, p_cb->local_r_key);
178
179                     p_cb->secure_connections_only_mode_required =
180                         (btm_cb.security_mode == BTM_SEC_MODE_SC) ? TRUE : FALSE;
181
182                     if (p_cb->secure_connections_only_mode_required)
183                     {
184                         p_cb->loc_auth_req |= SMP_SC_SUPPORT_BIT;
185                     }
186
187                     if (!(p_cb->loc_auth_req & SMP_SC_SUPPORT_BIT)
188                         || lmp_version_below(p_cb->pairing_bda, HCI_PROTO_VERSION_4_2)
189                         || interop_match(INTEROP_DISABLE_LE_SECURE_CONNECTIONS,
190                             (const bt_bdaddr_t *)&p_cb->pairing_bda))
191                     {
192                         p_cb->loc_auth_req &= ~SMP_KP_SUPPORT_BIT;
193                         p_cb->local_i_key &= ~SMP_SEC_KEY_TYPE_LK;
194                         p_cb->local_r_key &= ~SMP_SEC_KEY_TYPE_LK;
195                     }
196
197                     SMP_TRACE_WARNING("set auth_req: 0x%02x, local_i_key: 0x%02x, local_r_key: 0x%02x",
198                         p_cb->loc_auth_req, p_cb->local_i_key, p_cb->local_r_key);
199
200                     smp_sm_event(p_cb, SMP_IO_RSP_EVT, NULL);
201                     break;
202
203                 case SMP_BR_KEYS_REQ_EVT:
204                     p_cb->loc_enc_size = cb_data.io_req.max_key_size;
205                     p_cb->local_i_key = cb_data.io_req.init_keys;
206                     p_cb->local_r_key = cb_data.io_req.resp_keys;
207
208                     p_cb->local_i_key &= ~SMP_SEC_KEY_TYPE_LK;
209                     p_cb->local_r_key &= ~SMP_SEC_KEY_TYPE_LK;
210
211                     SMP_TRACE_WARNING ( "for SMP over BR max_key_size: 0x%02x,\
212                         local_i_key: 0x%02x, local_r_key: 0x%02x",
213                         p_cb->loc_enc_size, p_cb->local_i_key, p_cb->local_r_key);
214
215                     smp_br_state_machine_event(p_cb, SMP_BR_KEYS_RSP_EVT, NULL);
216                     break;
217             }
218         }
219     }
220
221     if (!p_cb->cb_evt && p_cb->discard_sec_req)
222     {
223         p_cb->discard_sec_req = FALSE;
224         smp_sm_event(p_cb, SMP_DISCARD_SEC_REQ_EVT, NULL);
225     }
226
227     SMP_TRACE_DEBUG("%s return", __func__);
228 }
229
230 /*******************************************************************************
231 ** Function     smp_send_pair_fail
232 ** Description  pairing failure to peer device if needed.
233 *******************************************************************************/
234 void smp_send_pair_fail(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
235 {
236     p_cb->status = *(UINT8 *)p_data;
237     p_cb->failure = *(UINT8 *)p_data;
238
239     SMP_TRACE_DEBUG("%s status=%d failure=%d ", __func__, p_cb->status, p_cb->failure);
240
241     if (p_cb->status <= SMP_MAX_FAIL_RSN_PER_SPEC && p_cb->status != SMP_SUCCESS)
242     {
243         smp_send_cmd(SMP_OPCODE_PAIRING_FAILED, p_cb);
244         p_cb->wait_for_authorization_complete = TRUE;
245     }
246 }
247
248 /*******************************************************************************
249 ** Function     smp_send_pair_req
250 ** Description  actions related to sending pairing request
251 *******************************************************************************/
252 void smp_send_pair_req(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
253 {
254     tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev (p_cb->pairing_bda);
255     SMP_TRACE_DEBUG("%s", __func__);
256
257     /* erase all keys when master sends pairing req*/
258     if (p_dev_rec)
259         btm_sec_clear_ble_keys(p_dev_rec);
260     /* do not manipulate the key, let app decide,
261        leave out to BTM to mandate key distribution for bonding case */
262     smp_send_cmd(SMP_OPCODE_PAIRING_REQ, p_cb);
263 }
264
265 /*******************************************************************************
266 ** Function     smp_send_pair_rsp
267 ** Description  actions related to sending pairing response
268 *******************************************************************************/
269 void smp_send_pair_rsp(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
270 {
271     SMP_TRACE_DEBUG("%s", __func__);
272
273     p_cb->local_i_key &= p_cb->peer_i_key;
274     p_cb->local_r_key &= p_cb->peer_r_key;
275
276     if (smp_send_cmd (SMP_OPCODE_PAIRING_RSP, p_cb))
277     {
278         if (p_cb->selected_association_model == SMP_MODEL_SEC_CONN_OOB)
279             smp_use_oob_private_key(p_cb, NULL);
280         else
281             smp_decide_association_model(p_cb, NULL);
282     }
283 }
284
285 /*******************************************************************************
286 ** Function     smp_send_confirm
287 ** Description  send confirmation to the peer
288 *******************************************************************************/
289 void smp_send_confirm(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
290 {
291     SMP_TRACE_DEBUG("%s", __func__);
292     smp_send_cmd(SMP_OPCODE_CONFIRM, p_cb);
293 }
294
295 /*******************************************************************************
296 ** Function     smp_send_init
297 ** Description  process pairing initializer to slave device
298 *******************************************************************************/
299 void smp_send_init(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
300 {
301     SMP_TRACE_DEBUG("%s", __func__);
302     smp_send_cmd(SMP_OPCODE_INIT, p_cb);
303 }
304
305 /*******************************************************************************
306 ** Function     smp_send_rand
307 ** Description  send pairing random to the peer
308 *******************************************************************************/
309 void smp_send_rand(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
310 {
311     SMP_TRACE_DEBUG("%s", __func__);
312     smp_send_cmd(SMP_OPCODE_RAND, p_cb);
313 }
314
315 /*******************************************************************************
316 ** Function     smp_send_pair_public_key
317 ** Description  send pairing public key command to the peer
318 *******************************************************************************/
319 void smp_send_pair_public_key(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
320 {
321     SMP_TRACE_DEBUG("%s", __func__);
322     smp_send_cmd(SMP_OPCODE_PAIR_PUBLIC_KEY, p_cb);
323 }
324
325 /*******************************************************************************
326 ** Function     SMP_SEND_COMMITMENT
327 ** Description send commitment command to the peer
328 *******************************************************************************/
329 void smp_send_commitment(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
330 {
331     SMP_TRACE_DEBUG("%s", __func__);
332     smp_send_cmd(SMP_OPCODE_PAIR_COMMITM, p_cb);
333 }
334
335 /*******************************************************************************
336 ** Function     smp_send_dhkey_check
337 ** Description send DHKey Check command to the peer
338 *******************************************************************************/
339 void smp_send_dhkey_check(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
340 {
341     SMP_TRACE_DEBUG("%s", __func__);
342     smp_send_cmd(SMP_OPCODE_PAIR_DHKEY_CHECK, p_cb);
343 }
344
345 /*******************************************************************************
346 ** Function     smp_send_keypress_notification
347 ** Description send Keypress Notification command to the peer
348 *******************************************************************************/
349 void smp_send_keypress_notification(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
350 {
351     p_cb->local_keypress_notification = *(UINT8 *) p_data;
352     smp_send_cmd(SMP_OPCODE_PAIR_KEYPR_NOTIF, p_cb);
353 }
354
355 /*******************************************************************************
356 ** Function     smp_send_enc_info
357 ** Description  send encryption information command.
358 *******************************************************************************/
359 void smp_send_enc_info(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
360 {
361     tBTM_LE_LENC_KEYS   le_key;
362
363     SMP_TRACE_DEBUG("%s p_cb->loc_enc_size = %d", __func__, p_cb->loc_enc_size);
364     smp_update_key_mask (p_cb, SMP_SEC_KEY_TYPE_ENC, FALSE);
365
366     smp_send_cmd(SMP_OPCODE_ENCRYPT_INFO, p_cb);
367     smp_send_cmd(SMP_OPCODE_MASTER_ID, p_cb);
368
369     /* save the DIV and key size information when acting as slave device */
370     memcpy(le_key.ltk, p_cb->ltk, BT_OCTET16_LEN);
371     le_key.div =  p_cb->div;
372     le_key.key_size = p_cb->loc_enc_size;
373     le_key.sec_level = p_cb->sec_level;
374
375     if ((p_cb->peer_auth_req & SMP_AUTH_BOND) && (p_cb->loc_auth_req & SMP_AUTH_BOND))
376         btm_sec_save_le_key(p_cb->pairing_bda, BTM_LE_KEY_LENC,
377                             (tBTM_LE_KEY_VALUE *)&le_key, TRUE);
378
379     SMP_TRACE_WARNING ("%s", __func__);
380
381     smp_key_distribution(p_cb, NULL);
382 }
383
384 /*******************************************************************************
385 ** Function     smp_send_id_info
386 ** Description  send ID information command.
387 *******************************************************************************/
388 void smp_send_id_info(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
389 {
390     tBTM_LE_KEY_VALUE   le_key;
391     SMP_TRACE_DEBUG("%s", __func__);
392     smp_update_key_mask (p_cb, SMP_SEC_KEY_TYPE_ID, FALSE);
393
394     smp_send_cmd(SMP_OPCODE_IDENTITY_INFO, p_cb);
395     smp_send_cmd(SMP_OPCODE_ID_ADDR, p_cb);
396
397     if ((p_cb->peer_auth_req & SMP_AUTH_BOND) && (p_cb->loc_auth_req & SMP_AUTH_BOND))
398         btm_sec_save_le_key(p_cb->pairing_bda, BTM_LE_KEY_LID,
399                             &le_key, TRUE);
400
401     SMP_TRACE_WARNING ("%s", __func__);
402     smp_key_distribution_by_transport(p_cb, NULL);
403 }
404
405 /*******************************************************************************
406 ** Function     smp_send_csrk_info
407 ** Description  send CSRK command.
408 *******************************************************************************/
409 void smp_send_csrk_info(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
410 {
411     tBTM_LE_LCSRK_KEYS  key;
412     SMP_TRACE_DEBUG("%s", __func__);
413     smp_update_key_mask (p_cb, SMP_SEC_KEY_TYPE_CSRK, FALSE);
414
415     if (smp_send_cmd(SMP_OPCODE_SIGN_INFO, p_cb))
416     {
417         key.div = p_cb->div;
418         key.sec_level = p_cb->sec_level;
419         key.counter = 0; /* initialize the local counter */
420         memcpy (key.csrk, p_cb->csrk, BT_OCTET16_LEN);
421         btm_sec_save_le_key(p_cb->pairing_bda, BTM_LE_KEY_LCSRK, (tBTM_LE_KEY_VALUE *)&key, TRUE);
422     }
423
424     smp_key_distribution_by_transport(p_cb, NULL);
425 }
426
427 /*******************************************************************************
428 ** Function     smp_send_ltk_reply
429 ** Description  send LTK reply
430 *******************************************************************************/
431 void smp_send_ltk_reply(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
432 {
433     SMP_TRACE_DEBUG("%s", __func__);
434     /* send stk as LTK response */
435     btm_ble_ltk_request_reply(p_cb->pairing_bda, TRUE, p_data->key.p_data);
436 }
437
438 /*******************************************************************************
439 ** Function     smp_proc_sec_req
440 ** Description  process security request.
441 *******************************************************************************/
442 void smp_proc_sec_req(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
443 {
444     tBTM_LE_AUTH_REQ auth_req = *(tBTM_LE_AUTH_REQ *)p_data;
445     tBTM_BLE_SEC_REQ_ACT sec_req_act;
446     UINT8 reason;
447
448     SMP_TRACE_DEBUG("%s auth_req=0x%x", __func__, auth_req);
449
450     p_cb->cb_evt = 0;
451
452     btm_ble_link_sec_check(p_cb->pairing_bda, auth_req,  &sec_req_act);
453
454     SMP_TRACE_DEBUG("%s sec_req_act=0x%x", __func__, sec_req_act);
455
456     switch (sec_req_act)
457     {
458         case  BTM_BLE_SEC_REQ_ACT_ENCRYPT:
459             SMP_TRACE_DEBUG("%s BTM_BLE_SEC_REQ_ACT_ENCRYPT", __func__);
460             smp_sm_event(p_cb, SMP_ENC_REQ_EVT, NULL);
461             break;
462
463         case BTM_BLE_SEC_REQ_ACT_PAIR:
464             p_cb->secure_connections_only_mode_required =
465                     (btm_cb.security_mode == BTM_SEC_MODE_SC) ? TRUE : FALSE;
466
467             /* respond to non SC pairing request as failure in SC only mode */
468             if (p_cb->secure_connections_only_mode_required &&
469                 (auth_req & SMP_SC_SUPPORT_BIT) == 0)
470             {
471                 reason = SMP_PAIR_AUTH_FAIL;
472                 smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &reason);
473             }
474             else
475             {
476                 /* initialize local i/r key to be default keys */
477                 p_cb->peer_auth_req = auth_req;
478                 p_cb->local_r_key = p_cb->local_i_key = SMP_SEC_DEFAULT_KEY ;
479                 p_cb->cb_evt = SMP_SEC_REQUEST_EVT;
480             }
481             break;
482
483         case BTM_BLE_SEC_REQ_ACT_DISCARD:
484             p_cb->discard_sec_req = TRUE;
485             break;
486
487         default:
488             /* do nothing */
489             break;
490     }
491 }
492
493 /*******************************************************************************
494 ** Function     smp_proc_sec_grant
495 ** Description  process security grant.
496 *******************************************************************************/
497 void smp_proc_sec_grant(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
498 {
499     UINT8 res= *(UINT8 *)p_data;
500     SMP_TRACE_DEBUG("%s", __func__);
501     if (res != SMP_SUCCESS)
502     {
503         smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, p_data);
504     }
505     else /*otherwise, start pairing */
506     {
507         /* send IO request callback */
508         p_cb->cb_evt = SMP_IO_CAP_REQ_EVT;
509     }
510 }
511
512 /*******************************************************************************
513 ** Function     smp_proc_pair_fail
514 ** Description  process pairing failure from peer device
515 *******************************************************************************/
516 void smp_proc_pair_fail(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
517 {
518     SMP_TRACE_DEBUG("%s", __func__);
519     p_cb->status = *(UINT8 *)p_data;
520
521     /* Cancel pending auth complete timer if set */
522     if (p_cb->delayed_auth_timer_enabled) {
523         btu_stop_quick_timer(&p_cb->delayed_auth_timer_ent);
524         p_cb->delayed_auth_timer_enabled = FALSE;
525     }
526 }
527
528 /*******************************************************************************
529 ** Function     smp_proc_pair_cmd
530 ** Description  Process the SMP pairing request/response from peer device
531 *******************************************************************************/
532 void smp_proc_pair_cmd(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
533 {
534     UINT8   *p = (UINT8 *)p_data;
535     UINT8   reason = SMP_ENC_KEY_SIZE;
536     tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev (p_cb->pairing_bda);
537
538     SMP_TRACE_DEBUG("%s", __func__);
539     /* erase all keys if it is slave proc pairing req*/
540     if (p_dev_rec && (p_cb->role == HCI_ROLE_SLAVE))
541         btm_sec_clear_ble_keys(p_dev_rec);
542
543     p_cb->flags |= SMP_PAIR_FLAG_ENC_AFTER_PAIR;
544
545     STREAM_TO_UINT8(p_cb->peer_io_caps, p);
546     STREAM_TO_UINT8(p_cb->peer_oob_flag, p);
547     STREAM_TO_UINT8(p_cb->peer_auth_req, p);
548     STREAM_TO_UINT8(p_cb->peer_enc_size, p);
549     STREAM_TO_UINT8(p_cb->peer_i_key, p);
550     STREAM_TO_UINT8(p_cb->peer_r_key, p);
551
552     if (smp_command_has_invalid_parameters(p_cb))
553     {
554         reason = SMP_INVALID_PARAMETERS;
555         smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &reason);
556         return;
557     }
558
559     if (p_cb->role == HCI_ROLE_SLAVE)
560     {
561         if (!(p_cb->flags & SMP_PAIR_FLAGS_WE_STARTED_DD))
562         {
563             /* peer (master) started pairing sending Pairing Request */
564             p_cb->local_i_key = p_cb->peer_i_key;
565             p_cb->local_r_key = p_cb->peer_r_key;
566
567             p_cb->cb_evt = SMP_SEC_REQUEST_EVT;
568         }
569         else /* update local i/r key according to pairing request */
570         {
571             /* pairing started with this side (slave) sending Security Request */
572             p_cb->local_i_key &= p_cb->peer_i_key;
573             p_cb->local_r_key &= p_cb->peer_r_key;
574             p_cb->selected_association_model = smp_select_association_model(p_cb);
575
576             if (p_cb->secure_connections_only_mode_required &&
577                 (!(p_cb->le_secure_connections_mode_is_used) ||
578                (p_cb->selected_association_model == SMP_MODEL_SEC_CONN_JUSTWORKS)))
579             {
580                 SMP_TRACE_ERROR("%s pairing failed - slave requires secure connection only mode",
581                     __func__);
582                 reason = SMP_PAIR_AUTH_FAIL;
583                 smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &reason);
584                 return;
585             }
586
587             if (p_cb->selected_association_model == SMP_MODEL_SEC_CONN_OOB)
588             {
589                 if (smp_request_oob_data(p_cb)) return;
590             }
591             else
592             {
593                 smp_send_pair_rsp(p_cb, NULL);
594             }
595         }
596     }
597     else /* Master receives pairing response */
598     {
599         p_cb->selected_association_model = smp_select_association_model(p_cb);
600
601         if (p_cb->secure_connections_only_mode_required &&
602             (!(p_cb->le_secure_connections_mode_is_used) ||
603            (p_cb->selected_association_model == SMP_MODEL_SEC_CONN_JUSTWORKS)))
604         {
605             SMP_TRACE_ERROR ("Master requires secure connection only mode \
606                 but it can't be provided -> Master fails pairing");
607             reason = SMP_PAIR_AUTH_FAIL;
608             smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &reason);
609             return;
610         }
611
612         if (p_cb->selected_association_model == SMP_MODEL_SEC_CONN_OOB)
613         {
614             if (smp_request_oob_data(p_cb)) return;
615         }
616         else
617         {
618             smp_decide_association_model(p_cb, NULL);
619         }
620     }
621 }
622
623 /*******************************************************************************
624 ** Function     smp_proc_confirm
625 ** Description  process pairing confirm from peer device
626 *******************************************************************************/
627 void smp_proc_confirm(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
628 {
629     UINT8 *p = (UINT8 *)p_data;
630     UINT8 reason = SMP_INVALID_PARAMETERS;
631
632     SMP_TRACE_DEBUG("%s", __func__);
633
634     if (smp_command_has_invalid_parameters(p_cb))
635     {
636         smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &reason);
637         return;
638     }
639
640     if (p != NULL)
641     {
642         /* save the SConfirm for comparison later */
643         STREAM_TO_ARRAY(p_cb->rconfirm, p, BT_OCTET16_LEN);
644     }
645
646     p_cb->flags |= SMP_PAIR_FLAGS_CMD_CONFIRM;
647 }
648
649 /*******************************************************************************
650 ** Function     smp_proc_init
651 ** Description  process pairing initializer from peer device
652 *******************************************************************************/
653 void smp_proc_init(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
654 {
655     UINT8 *p = (UINT8 *)p_data;
656     UINT8 reason = SMP_INVALID_PARAMETERS;
657
658     SMP_TRACE_DEBUG("%s", __func__);
659
660     if (smp_command_has_invalid_parameters(p_cb))
661     {
662         smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &reason);
663         return;
664     }
665
666     /* save the SRand for comparison */
667     STREAM_TO_ARRAY(p_cb->rrand, p, BT_OCTET16_LEN);
668 }
669
670 /*******************************************************************************
671 ** Function     smp_proc_rand
672 ** Description  process pairing random (nonce) from peer device
673 *******************************************************************************/
674 void smp_proc_rand(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
675 {
676     UINT8 *p = (UINT8 *)p_data;
677     UINT8 reason = SMP_INVALID_PARAMETERS;
678
679     SMP_TRACE_DEBUG("%s", __func__);
680
681     if (smp_command_has_invalid_parameters(p_cb))
682     {
683         smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &reason);
684         return;
685     }
686
687     /* save the SRand for comparison */
688     STREAM_TO_ARRAY(p_cb->rrand, p, BT_OCTET16_LEN);
689 }
690
691 /*******************************************************************************
692 ** Function     smp_process_pairing_public_key
693 ** Description  process pairing public key command from the peer device
694 **              - saves the peer public key;
695 **              - sets the flag indicating that the peer public key is received;
696 **              - calls smp_wait_for_both_public_keys(...).
697 **
698 *******************************************************************************/
699 void smp_process_pairing_public_key(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
700 {
701     UINT8 *p = (UINT8 *)p_data;
702     UINT8 reason = SMP_INVALID_PARAMETERS;
703
704     SMP_TRACE_DEBUG("%s", __func__);
705
706     if (smp_command_has_invalid_parameters(p_cb))
707     {
708         smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &reason);
709         return;
710     }
711
712     STREAM_TO_ARRAY(p_cb->peer_publ_key.x, p, BT_OCTET32_LEN);
713     STREAM_TO_ARRAY(p_cb->peer_publ_key.y, p, BT_OCTET32_LEN);
714     p_cb->flags |= SMP_PAIR_FLAG_HAVE_PEER_PUBL_KEY;
715
716     smp_wait_for_both_public_keys(p_cb, NULL);
717 }
718
719 /*******************************************************************************
720 ** Function     smp_process_pairing_commitment
721 ** Description  process pairing commitment from peer device
722 *******************************************************************************/
723 void smp_process_pairing_commitment(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
724 {
725     UINT8 *p = (UINT8 *)p_data;
726     UINT8 reason = SMP_INVALID_PARAMETERS;
727
728     SMP_TRACE_DEBUG("%s", __func__);
729
730     if (smp_command_has_invalid_parameters(p_cb))
731     {
732         smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &reason);
733         return;
734     }
735
736     p_cb->flags |= SMP_PAIR_FLAG_HAVE_PEER_COMM;
737
738     if (p != NULL)
739     {
740         STREAM_TO_ARRAY(p_cb->remote_commitment, p, BT_OCTET16_LEN);
741     }
742 }
743
744 /*******************************************************************************
745 ** Function     smp_process_dhkey_check
746 ** Description  process DHKey Check from peer device
747 *******************************************************************************/
748 void smp_process_dhkey_check(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
749 {
750     UINT8 *p = (UINT8 *)p_data;
751     UINT8 reason = SMP_INVALID_PARAMETERS;
752
753     SMP_TRACE_DEBUG("%s", __func__);
754
755     if (smp_command_has_invalid_parameters(p_cb))
756     {
757         smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &reason);
758         return;
759     }
760
761     if (p != NULL)
762     {
763         STREAM_TO_ARRAY(p_cb->remote_dhkey_check, p, BT_OCTET16_LEN);
764     }
765
766     p_cb->flags |= SMP_PAIR_FLAG_HAVE_PEER_DHK_CHK;
767 }
768
769 /*******************************************************************************
770 ** Function     smp_process_keypress_notification
771 ** Description  process pairing keypress notification from peer device
772 *******************************************************************************/
773 void smp_process_keypress_notification(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
774 {
775     UINT8 *p = (UINT8 *)p_data;
776     UINT8 reason = SMP_INVALID_PARAMETERS;
777
778     SMP_TRACE_DEBUG("%s", __func__);
779     p_cb->status = *(UINT8 *)p_data;
780
781     if (smp_command_has_invalid_parameters(p_cb))
782     {
783         smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &reason);
784         return;
785     }
786
787     if (p != NULL)
788     {
789         STREAM_TO_UINT8(p_cb->peer_keypress_notification, p);
790     }
791     else
792     {
793         p_cb->peer_keypress_notification = BTM_SP_KEY_OUT_OF_RANGE;
794     }
795     p_cb->cb_evt = SMP_PEER_KEYPR_NOT_EVT;
796 }
797
798 /*******************************************************************************
799 ** Function     smp_br_process_pairing_command
800 ** Description  Process the SMP pairing request/response from peer device via
801 **              BR/EDR transport.
802 *******************************************************************************/
803 void smp_br_process_pairing_command(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
804 {
805     UINT8   *p = (UINT8 *)p_data;
806     UINT8   reason = SMP_ENC_KEY_SIZE;
807     tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev (p_cb->pairing_bda);
808
809     SMP_TRACE_DEBUG("%s", __func__);
810     /* rejecting BR pairing request over non-SC BR link */
811     if (!p_dev_rec->new_encryption_key_is_p256 && p_cb->role == HCI_ROLE_SLAVE)
812     {
813         reason = SMP_XTRANS_DERIVE_NOT_ALLOW;
814         smp_br_state_machine_event(p_cb, SMP_BR_AUTH_CMPL_EVT, &reason);
815         return;
816     }
817
818     /* erase all keys if it is slave proc pairing req*/
819     if (p_dev_rec && (p_cb->role == HCI_ROLE_SLAVE))
820         btm_sec_clear_ble_keys(p_dev_rec);
821
822     p_cb->flags |= SMP_PAIR_FLAG_ENC_AFTER_PAIR;
823
824     STREAM_TO_UINT8(p_cb->peer_io_caps, p);
825     STREAM_TO_UINT8(p_cb->peer_oob_flag, p);
826     STREAM_TO_UINT8(p_cb->peer_auth_req, p);
827     STREAM_TO_UINT8(p_cb->peer_enc_size, p);
828     STREAM_TO_UINT8(p_cb->peer_i_key, p);
829     STREAM_TO_UINT8(p_cb->peer_r_key, p);
830
831     if (smp_command_has_invalid_parameters(p_cb))
832     {
833         reason = SMP_INVALID_PARAMETERS;
834         smp_br_state_machine_event(p_cb, SMP_BR_AUTH_CMPL_EVT, &reason);
835         return;
836     }
837
838     /* peer (master) started pairing sending Pairing Request */
839     /* or being master device always use received i/r key as keys to distribute */
840     p_cb->local_i_key = p_cb->peer_i_key;
841     p_cb->local_r_key = p_cb->peer_r_key;
842
843     if (p_cb->role == HCI_ROLE_SLAVE)
844     {
845         p_dev_rec->new_encryption_key_is_p256 = FALSE;
846         /* shortcut to skip Security Grant step */
847         p_cb->cb_evt = SMP_BR_KEYS_REQ_EVT;
848     }
849     else /* Master receives pairing response */
850     {
851         SMP_TRACE_DEBUG("%s master rcvs valid PAIRING RESPONSE."
852                           " Supposed to move to key distribution phase. ", __func__);
853     }
854
855     /* auth_req received via BR/EDR SM channel is set to 0,
856        but everything derived/exchanged has to be saved */
857     p_cb->peer_auth_req |= SMP_AUTH_BOND;
858     p_cb->loc_auth_req |= SMP_AUTH_BOND;
859 }
860
861 /*******************************************************************************
862 ** Function     smp_br_process_security_grant
863 ** Description  process security grant in case of pairing over BR/EDR transport.
864 *******************************************************************************/
865 void smp_br_process_security_grant(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
866 {
867     UINT8 res= *(UINT8 *)p_data;
868     SMP_TRACE_DEBUG("%s", __func__);
869     if (res != SMP_SUCCESS)
870     {
871         smp_br_state_machine_event(p_cb, SMP_BR_AUTH_CMPL_EVT, p_data);
872     }
873     else /*otherwise, start pairing */
874     {
875         /* send IO request callback */
876         p_cb->cb_evt = SMP_BR_KEYS_REQ_EVT;
877     }
878 }
879
880 /*******************************************************************************
881 ** Function     smp_br_check_authorization_request
882 ** Description  sets the SMP kes to be derived/distribute over BR/EDR transport
883 **              before starting the distribution/derivation
884 *******************************************************************************/
885 void smp_br_check_authorization_request(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
886 {
887     UINT8 reason = SMP_SUCCESS;
888
889     SMP_TRACE_DEBUG("%s rcvs i_keys=0x%x r_keys=0x%x "
890                       "(i-initiator r-responder)", __FUNCTION__, p_cb->local_i_key,
891                       p_cb->local_r_key);
892
893     /* In LE SC mode LK field is ignored when BR/EDR transport is used */
894     p_cb->local_i_key &= ~SMP_SEC_KEY_TYPE_LK;
895     p_cb->local_r_key &= ~SMP_SEC_KEY_TYPE_LK;
896
897     /* In LE SC mode only IRK, IAI, CSRK are exchanged with the peer.
898     ** Set local_r_key on master to expect only these keys. */
899     if (p_cb->role == HCI_ROLE_MASTER)
900     {
901         p_cb->local_r_key &= (SMP_SEC_KEY_TYPE_ID | SMP_SEC_KEY_TYPE_CSRK);
902     }
903
904     SMP_TRACE_DEBUG("%s rcvs upgrades: i_keys=0x%x r_keys=0x%x "
905                       "(i-initiator r-responder)", __FUNCTION__, p_cb->local_i_key,
906                       p_cb->local_r_key);
907
908     if (/*((p_cb->peer_auth_req & SMP_AUTH_BOND) ||
909             (p_cb->loc_auth_req & SMP_AUTH_BOND)) &&*/
910         (p_cb->local_i_key || p_cb->local_r_key))
911     {
912         smp_br_state_machine_event(p_cb, SMP_BR_BOND_REQ_EVT, NULL);
913
914         /* if no peer key is expected, start master key distribution */
915         if (p_cb->role == HCI_ROLE_MASTER && p_cb->local_r_key == 0)
916             smp_key_distribution_by_transport(p_cb, NULL);
917     }
918     else
919     {
920         smp_br_state_machine_event(p_cb, SMP_BR_AUTH_CMPL_EVT, &reason);
921     }
922 }
923
924 /*******************************************************************************
925 ** Function     smp_br_select_next_key
926 ** Description  selects the next key to derive/send when BR/EDR transport is
927 **              used.
928 *******************************************************************************/
929 void smp_br_select_next_key(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
930 {
931     UINT8   reason = SMP_SUCCESS;
932     SMP_TRACE_DEBUG("%s role=%d (0-master) r_keys=0x%x i_keys=0x%x",
933                        __func__, p_cb->role, p_cb->local_r_key, p_cb->local_i_key);
934
935     if (p_cb->role == HCI_ROLE_SLAVE||
936         (!p_cb->local_r_key && p_cb->role == HCI_ROLE_MASTER))
937     {
938         smp_key_pick_key(p_cb, p_data);
939     }
940
941     if (!p_cb->local_i_key && !p_cb->local_r_key)
942     {
943         /* state check to prevent re-entrance */
944         if (smp_get_br_state() == SMP_BR_STATE_BOND_PENDING)
945         {
946             if (p_cb->total_tx_unacked == 0)
947                 smp_br_state_machine_event(p_cb, SMP_BR_AUTH_CMPL_EVT, &reason);
948             else
949                 p_cb->wait_for_authorization_complete = TRUE;
950         }
951     }
952 }
953
954 /*******************************************************************************
955 ** Function     smp_proc_enc_info
956 ** Description  process encryption information from peer device
957 *******************************************************************************/
958 void smp_proc_enc_info(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
959 {
960     UINT8   *p = (UINT8 *)p_data;
961
962     SMP_TRACE_DEBUG("%s", __func__);
963     STREAM_TO_ARRAY(p_cb->ltk, p, BT_OCTET16_LEN);
964
965     smp_key_distribution(p_cb, NULL);
966 }
967 /*******************************************************************************
968 ** Function     smp_proc_master_id
969 ** Description  process master ID from slave device
970 *******************************************************************************/
971 void smp_proc_master_id(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
972 {
973     UINT8   *p = (UINT8 *)p_data;
974     tBTM_LE_PENC_KEYS   le_key;
975
976     SMP_TRACE_DEBUG("%s", __func__);
977     smp_update_key_mask (p_cb, SMP_SEC_KEY_TYPE_ENC, TRUE);
978
979     STREAM_TO_UINT16(le_key.ediv, p);
980     STREAM_TO_ARRAY(le_key.rand, p, BT_OCTET8_LEN );
981
982     /* store the encryption keys from peer device */
983     memcpy(le_key.ltk, p_cb->ltk, BT_OCTET16_LEN);
984     le_key.sec_level = p_cb->sec_level;
985     le_key.key_size  = p_cb->loc_enc_size;
986
987     if ((p_cb->peer_auth_req & SMP_AUTH_BOND) && (p_cb->loc_auth_req & SMP_AUTH_BOND))
988         btm_sec_save_le_key(p_cb->pairing_bda,
989                             BTM_LE_KEY_PENC,
990                             (tBTM_LE_KEY_VALUE *)&le_key, TRUE);
991
992     smp_key_distribution(p_cb, NULL);
993 }
994
995 /*******************************************************************************
996 ** Function     smp_proc_enc_info
997 ** Description  process identity information from peer device
998 *******************************************************************************/
999 void smp_proc_id_info(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
1000 {
1001     UINT8   *p = (UINT8 *)p_data;
1002
1003     SMP_TRACE_DEBUG("%s", __func__);
1004     STREAM_TO_ARRAY (p_cb->tk, p, BT_OCTET16_LEN);   /* reuse TK for IRK */
1005     smp_key_distribution_by_transport(p_cb, NULL);
1006 }
1007
1008 /*******************************************************************************
1009 ** Function     smp_proc_id_addr
1010 ** Description  process identity address from peer device
1011 *******************************************************************************/
1012 void smp_proc_id_addr(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
1013 {
1014     UINT8   *p = (UINT8 *)p_data;
1015     tBTM_LE_PID_KEYS    pid_key;
1016
1017     SMP_TRACE_DEBUG("%s", __func__);
1018     smp_update_key_mask (p_cb, SMP_SEC_KEY_TYPE_ID, TRUE);
1019
1020     STREAM_TO_UINT8(pid_key.addr_type, p);
1021     STREAM_TO_BDADDR(pid_key.static_addr, p);
1022     memcpy(pid_key.irk, p_cb->tk, BT_OCTET16_LEN);
1023
1024     /* to use as BD_ADDR for lk derived from ltk */
1025     p_cb->id_addr_rcvd = TRUE;
1026     p_cb->id_addr_type = pid_key.addr_type;
1027     memcpy(p_cb->id_addr, pid_key.static_addr, BD_ADDR_LEN);
1028
1029     /* store the ID key from peer device */
1030     if ((p_cb->peer_auth_req & SMP_AUTH_BOND) && (p_cb->loc_auth_req & SMP_AUTH_BOND))
1031         btm_sec_save_le_key(p_cb->pairing_bda, BTM_LE_KEY_PID,
1032                             (tBTM_LE_KEY_VALUE *)&pid_key, TRUE);
1033     smp_key_distribution_by_transport(p_cb, NULL);
1034 }
1035
1036 /*******************************************************************************
1037 ** Function     smp_proc_srk_info
1038 ** Description  process security information from peer device
1039 *******************************************************************************/
1040 void smp_proc_srk_info(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
1041 {
1042     tBTM_LE_PCSRK_KEYS   le_key;
1043
1044     SMP_TRACE_DEBUG("%s", __func__);
1045     smp_update_key_mask (p_cb, SMP_SEC_KEY_TYPE_CSRK, TRUE);
1046
1047     /* save CSRK to security record */
1048     le_key.sec_level = p_cb->sec_level;
1049     memcpy (le_key.csrk, p_data, BT_OCTET16_LEN);   /* get peer CSRK */
1050     le_key.counter = 0; /* initialize the peer counter */
1051
1052     if ((p_cb->peer_auth_req & SMP_AUTH_BOND) && (p_cb->loc_auth_req & SMP_AUTH_BOND))
1053         btm_sec_save_le_key(p_cb->pairing_bda,
1054                             BTM_LE_KEY_PCSRK,
1055                             (tBTM_LE_KEY_VALUE *)&le_key, TRUE);
1056     smp_key_distribution_by_transport(p_cb, NULL);
1057 }
1058
1059 /*******************************************************************************
1060 ** Function     smp_proc_compare
1061 ** Description  process compare value
1062 *******************************************************************************/
1063 void smp_proc_compare(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
1064 {
1065     UINT8   reason;
1066
1067     SMP_TRACE_DEBUG("%s", __func__);
1068     if (!memcmp(p_cb->rconfirm, p_data->key.p_data, BT_OCTET16_LEN))
1069     {
1070         /* compare the max encryption key size, and save the smaller one for the link */
1071         if ( p_cb->peer_enc_size < p_cb->loc_enc_size)
1072             p_cb->loc_enc_size = p_cb->peer_enc_size;
1073
1074         if (p_cb->role == HCI_ROLE_SLAVE)
1075             smp_sm_event(p_cb, SMP_RAND_EVT, NULL);
1076         else
1077         {
1078             /* master device always use received i/r key as keys to distribute */
1079             p_cb->local_i_key = p_cb->peer_i_key;
1080             p_cb->local_r_key = p_cb->peer_r_key;
1081
1082             smp_sm_event(p_cb, SMP_ENC_REQ_EVT, NULL);
1083         }
1084
1085     }
1086     else
1087     {
1088         reason = p_cb->failure = SMP_CONFIRM_VALUE_ERR;
1089         smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &reason);
1090     }
1091 }
1092
1093 /*******************************************************************************
1094 ** Function     smp_proc_sl_key
1095 ** Description  process key ready events.
1096 *******************************************************************************/
1097 void smp_proc_sl_key(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
1098 {
1099     UINT8 key_type = p_data->key.key_type;
1100
1101     SMP_TRACE_DEBUG("%s", __func__);
1102     if (key_type == SMP_KEY_TYPE_TK)
1103     {
1104         smp_generate_srand_mrand_confirm(p_cb, NULL);
1105     }
1106     else if (key_type == SMP_KEY_TYPE_CFM)
1107     {
1108         smp_set_state(SMP_STATE_WAIT_CONFIRM);
1109
1110         if (p_cb->flags & SMP_PAIR_FLAGS_CMD_CONFIRM)
1111             smp_sm_event(p_cb, SMP_CONFIRM_EVT, NULL);
1112     }
1113 }
1114
1115 /*******************************************************************************
1116 ** Function     smp_start_enc
1117 ** Description  start encryption
1118 *******************************************************************************/
1119 void smp_start_enc(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
1120 {
1121     tBTM_STATUS cmd;
1122     UINT8 reason = SMP_ENC_FAIL;
1123
1124     SMP_TRACE_DEBUG("%s", __func__);
1125     if (p_data != NULL)
1126         cmd = btm_ble_start_encrypt(p_cb->pairing_bda, TRUE, p_data->key.p_data);
1127     else
1128         cmd = btm_ble_start_encrypt(p_cb->pairing_bda, FALSE, NULL);
1129
1130     if (cmd != BTM_CMD_STARTED && cmd != BTM_BUSY)
1131         smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &reason);
1132 }
1133
1134 /*******************************************************************************
1135 ** Function     smp_proc_discard
1136 ** Description   processing for discard security request
1137 *******************************************************************************/
1138 void smp_proc_discard(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
1139 {
1140     SMP_TRACE_DEBUG("%s", __func__);
1141     if (!(p_cb->flags & SMP_PAIR_FLAGS_WE_STARTED_DD))
1142         smp_reset_control_value(p_cb);
1143 }
1144
1145 /*******************************************************************************
1146 ** Function     smp_enc_cmpl
1147 ** Description   encryption success
1148 *******************************************************************************/
1149 void smp_enc_cmpl(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
1150 {
1151     UINT8 enc_enable = *(UINT8 *)p_data;
1152     UINT8 reason = enc_enable ? SMP_SUCCESS : SMP_ENC_FAIL;
1153
1154     SMP_TRACE_DEBUG("%s", __func__);
1155     smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &reason);
1156 }
1157
1158 /*******************************************************************************
1159 ** Function     smp_check_auth_req
1160 ** Description  check authentication request
1161 *******************************************************************************/
1162 void smp_check_auth_req(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
1163 {
1164     UINT8 enc_enable = *(UINT8 *)p_data;
1165     UINT8 reason = enc_enable ? SMP_SUCCESS : SMP_ENC_FAIL;
1166
1167     SMP_TRACE_DEBUG("%s rcvs enc_enable=%d i_keys=0x%x r_keys=0x%x "
1168                       "(i-initiator r-responder)",
1169                       __func__, enc_enable, p_cb->local_i_key, p_cb->local_r_key);
1170     if (enc_enable == 1)
1171     {
1172         if (p_cb->le_secure_connections_mode_is_used)
1173         {
1174             /* In LE SC mode LTK is used instead of STK and has to be always saved */
1175             p_cb->local_i_key |= SMP_SEC_KEY_TYPE_ENC;
1176             p_cb->local_r_key |= SMP_SEC_KEY_TYPE_ENC;
1177
1178            /* In LE SC mode LK is derived from LTK only if both sides request it */
1179            if (!(p_cb->local_i_key & SMP_SEC_KEY_TYPE_LK) ||
1180                !(p_cb->local_r_key & SMP_SEC_KEY_TYPE_LK))
1181             {
1182                 p_cb->local_i_key &= ~SMP_SEC_KEY_TYPE_LK;
1183                 p_cb->local_r_key &= ~SMP_SEC_KEY_TYPE_LK;
1184             }
1185
1186             /* In LE SC mode only IRK, IAI, CSRK are exchanged with the peer.
1187             ** Set local_r_key on master to expect only these keys.
1188             */
1189             if (p_cb->role == HCI_ROLE_MASTER)
1190             {
1191                 p_cb->local_r_key &= (SMP_SEC_KEY_TYPE_ID | SMP_SEC_KEY_TYPE_CSRK);
1192             }
1193         }
1194         else
1195         {
1196             /* in legacy mode derivation of BR/EDR LK is not supported */
1197             p_cb->local_i_key &= ~SMP_SEC_KEY_TYPE_LK;
1198             p_cb->local_r_key &= ~SMP_SEC_KEY_TYPE_LK;
1199         }
1200         SMP_TRACE_DEBUG("%s rcvs upgrades: i_keys=0x%x r_keys=0x%x "
1201                           "(i-initiator r-responder)",
1202                           __func__, p_cb->local_i_key, p_cb->local_r_key);
1203
1204         if (/*((p_cb->peer_auth_req & SMP_AUTH_BOND) ||
1205              (p_cb->loc_auth_req & SMP_AUTH_BOND)) &&*/
1206             (p_cb->local_i_key || p_cb->local_r_key))
1207         {
1208             smp_sm_event(p_cb, SMP_BOND_REQ_EVT, NULL);
1209         }
1210         else
1211             smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &reason);
1212     }
1213     else if (enc_enable == 0)
1214     {
1215         /* if failed for encryption after pairing, send callback */
1216         if (p_cb->flags & SMP_PAIR_FLAG_ENC_AFTER_PAIR)
1217             smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &reason);
1218         /* if enc failed for old security information */
1219         /* if master device, clean up and abck to idle; slave device do nothing */
1220         else if (p_cb->role == HCI_ROLE_MASTER)
1221         {
1222             smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &reason);
1223         }
1224     }
1225 }
1226
1227 /*******************************************************************************
1228 ** Function     smp_key_pick_key
1229 ** Description  Pick a key distribution function based on the key mask.
1230 *******************************************************************************/
1231 void smp_key_pick_key(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
1232 {
1233     UINT8   key_to_dist = (p_cb->role == HCI_ROLE_SLAVE) ? p_cb->local_r_key : p_cb->local_i_key;
1234     UINT8   i = 0;
1235
1236     SMP_TRACE_DEBUG("%s key_to_dist=0x%x", __func__, key_to_dist);
1237     while (i < SMP_KEY_DIST_TYPE_MAX)
1238     {
1239         SMP_TRACE_DEBUG("key to send = %02x, i = %d",  key_to_dist, i);
1240
1241         if (key_to_dist & (1 << i))
1242         {
1243             SMP_TRACE_DEBUG("smp_distribute_act[%d]", i);
1244             (* smp_distribute_act[i])(p_cb, p_data);
1245             break;
1246         }
1247         i ++;
1248     }
1249 }
1250 /*******************************************************************************
1251 ** Function     smp_key_distribution
1252 ** Description  start key distribution if required.
1253 *******************************************************************************/
1254 void smp_key_distribution(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
1255 {
1256     UINT8   reason = SMP_SUCCESS;
1257     SMP_TRACE_DEBUG("%s role=%d (0-master) r_keys=0x%x i_keys=0x%x",
1258                       __func__, p_cb->role, p_cb->local_r_key, p_cb->local_i_key);
1259
1260     if (p_cb->role == HCI_ROLE_SLAVE ||
1261        (!p_cb->local_r_key && p_cb->role == HCI_ROLE_MASTER))
1262     {
1263         smp_key_pick_key(p_cb, p_data);
1264     }
1265
1266     if (!p_cb->local_i_key && !p_cb->local_r_key)
1267     {
1268         /* state check to prevent re-entrant */
1269         if (smp_get_state() == SMP_STATE_BOND_PENDING)
1270         {
1271             if (p_cb->derive_lk)
1272             {
1273                 smp_derive_link_key_from_long_term_key(p_cb, NULL);
1274                 p_cb->derive_lk = FALSE;
1275             }
1276
1277             if (p_cb->total_tx_unacked == 0)
1278             {
1279                 /*
1280                  * Instead of declaring authorization complete immediately,
1281                  * delay the event from being sent until 500ms later.
1282                  * This allows the slave to send over Pairing Failed if the
1283                  * last key is rejected.  During this waiting window, the
1284                  * state should remain in SMP_STATE_BOND_PENDING.
1285                  */
1286                 if (!p_cb->delayed_auth_timer_enabled)
1287                 {
1288                     SMP_TRACE_DEBUG("%s delaying auth complete.", __func__);
1289                     btu_start_quick_timer(&p_cb->delayed_auth_timer_ent,
1290                                           BTU_TTYPE_SMP_DELAYED_AUTH,
1291                                           SMP_DELAYED_AUTH_TOUT);
1292                     p_cb->delayed_auth_timer_enabled = TRUE;
1293                 }
1294             } else {
1295                 p_cb->wait_for_authorization_complete = TRUE;
1296             }
1297         }
1298     }
1299 }
1300
1301 /*******************************************************************************
1302 ** Function         smp_decide_association_model
1303 ** Description      This function is called to select assoc model to be used for
1304 **                  STK generation and to start STK generation process.
1305 **
1306 *******************************************************************************/
1307 void smp_decide_association_model(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
1308 {
1309     UINT8   failure = SMP_UNKNOWN_IO_CAP;
1310     UINT8 int_evt = 0;
1311     tSMP_KEY key;
1312     tSMP_INT_DATA   *p = NULL;
1313
1314     SMP_TRACE_DEBUG("%s Association Model = %d", __func__, p_cb->selected_association_model);
1315
1316     switch (p_cb->selected_association_model)
1317     {
1318         case SMP_MODEL_ENCRYPTION_ONLY:  /* TK = 0, go calculate Confirm */
1319             if (p_cb->role == HCI_ROLE_MASTER &&
1320                 ((p_cb->peer_auth_req & SMP_AUTH_YN_BIT) != 0) &&
1321                 ((p_cb->loc_auth_req & SMP_AUTH_YN_BIT) == 0))
1322             {
1323                 SMP_TRACE_ERROR ("IO capability does not meet authentication requirement");
1324                 failure = SMP_PAIR_AUTH_FAIL;
1325                 p = (tSMP_INT_DATA *)&failure;
1326                 int_evt = SMP_AUTH_CMPL_EVT;
1327             }
1328             else
1329             {
1330                 p_cb->sec_level = SMP_SEC_UNAUTHENTICATE;
1331                 SMP_TRACE_EVENT ("p_cb->sec_level =%d (SMP_SEC_UNAUTHENTICATE) ", p_cb->sec_level );
1332
1333                 key.key_type = SMP_KEY_TYPE_TK;
1334                 key.p_data = p_cb->tk;
1335                 p = (tSMP_INT_DATA *)&key;
1336
1337                 memset(p_cb->tk, 0, BT_OCTET16_LEN);
1338                 /* TK, ready  */
1339                 int_evt = SMP_KEY_READY_EVT;
1340             }
1341             break;
1342
1343         case SMP_MODEL_PASSKEY:
1344             p_cb->sec_level = SMP_SEC_AUTHENTICATED;
1345             SMP_TRACE_EVENT ("p_cb->sec_level =%d (SMP_SEC_AUTHENTICATED) ", p_cb->sec_level );
1346
1347             p_cb->cb_evt = SMP_PASSKEY_REQ_EVT;
1348             int_evt = SMP_TK_REQ_EVT;
1349             break;
1350
1351         case SMP_MODEL_OOB:
1352             SMP_TRACE_ERROR ("Association Model = SMP_MODEL_OOB");
1353             p_cb->sec_level = SMP_SEC_AUTHENTICATED;
1354             SMP_TRACE_EVENT ("p_cb->sec_level =%d (SMP_SEC_AUTHENTICATED) ", p_cb->sec_level );
1355
1356             p_cb->cb_evt = SMP_OOB_REQ_EVT;
1357             int_evt = SMP_TK_REQ_EVT;
1358             break;
1359
1360         case SMP_MODEL_KEY_NOTIF:
1361             p_cb->sec_level = SMP_SEC_AUTHENTICATED;
1362             SMP_TRACE_DEBUG("Need to generate Passkey");
1363
1364             /* generate passkey and notify application */
1365             smp_generate_passkey(p_cb, NULL);
1366             break;
1367
1368         case SMP_MODEL_SEC_CONN_JUSTWORKS:
1369         case SMP_MODEL_SEC_CONN_NUM_COMP:
1370         case SMP_MODEL_SEC_CONN_PASSKEY_ENT:
1371         case SMP_MODEL_SEC_CONN_PASSKEY_DISP:
1372         case SMP_MODEL_SEC_CONN_OOB:
1373             int_evt = SMP_PUBL_KEY_EXCH_REQ_EVT;
1374             break;
1375
1376         case SMP_MODEL_OUT_OF_RANGE:
1377             SMP_TRACE_ERROR("Association Model = SMP_MODEL_OUT_OF_RANGE (failed)");
1378             p = (tSMP_INT_DATA *)&failure;
1379             int_evt = SMP_AUTH_CMPL_EVT;
1380             break;
1381
1382         default:
1383             SMP_TRACE_ERROR("Association Model = %d (SOMETHING IS WRONG WITH THE CODE)",
1384                              p_cb->selected_association_model);
1385             p = (tSMP_INT_DATA *)&failure;
1386             int_evt = SMP_AUTH_CMPL_EVT;
1387     }
1388
1389     SMP_TRACE_EVENT ("sec_level=%d ", p_cb->sec_level );
1390     if (int_evt)
1391         smp_sm_event(p_cb, int_evt, p);
1392 }
1393
1394 /*******************************************************************************
1395 ** Function     smp_process_io_response
1396 ** Description  process IO response for a slave device.
1397 *******************************************************************************/
1398 void smp_process_io_response(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
1399 {
1400     uint8_t reason = SMP_PAIR_AUTH_FAIL;
1401
1402     SMP_TRACE_DEBUG("%s", __func__);
1403     if (p_cb->flags & SMP_PAIR_FLAGS_WE_STARTED_DD)
1404     {
1405         /* pairing started by local (slave) Security Request */
1406         smp_set_state(SMP_STATE_SEC_REQ_PENDING);
1407         smp_send_cmd(SMP_OPCODE_SEC_REQ, p_cb);
1408     }
1409     else /* plan to send pairing respond */
1410     {
1411         /* pairing started by peer (master) Pairing Request */
1412         p_cb->selected_association_model = smp_select_association_model(p_cb);
1413
1414         if (p_cb->secure_connections_only_mode_required &&
1415             (!(p_cb->le_secure_connections_mode_is_used) ||
1416             (p_cb->selected_association_model == SMP_MODEL_SEC_CONN_JUSTWORKS)))
1417         {
1418             SMP_TRACE_ERROR ("Slave requires secure connection only mode \
1419                               but it can't be provided -> Slave fails pairing");
1420             smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &reason);
1421             return;
1422         }
1423
1424         if (p_cb->selected_association_model == SMP_MODEL_SEC_CONN_OOB)
1425         {
1426             if (smp_request_oob_data(p_cb)) return;
1427         }
1428         smp_send_pair_rsp(p_cb, NULL);
1429     }
1430 }
1431
1432 /*******************************************************************************
1433 ** Function     smp_br_process_slave_keys_response
1434 ** Description  process application keys response for a slave device
1435 **              (BR/EDR transport).
1436 *******************************************************************************/
1437 void smp_br_process_slave_keys_response(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
1438 {
1439     smp_br_send_pair_response(p_cb, NULL);
1440 }
1441
1442 /*******************************************************************************
1443 ** Function     smp_br_send_pair_response
1444 ** Description  actions related to sending pairing response over BR/EDR transport.
1445 *******************************************************************************/
1446 void smp_br_send_pair_response(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
1447 {
1448     SMP_TRACE_DEBUG("%s", __func__);
1449
1450     p_cb->local_i_key &= p_cb->peer_i_key;
1451     p_cb->local_r_key &= p_cb->peer_r_key;
1452
1453     smp_send_cmd (SMP_OPCODE_PAIRING_RSP, p_cb);
1454 }
1455
1456 /*******************************************************************************
1457 ** Function         smp_pairing_cmpl
1458 ** Description      This function is called to send the pairing complete callback
1459 **                  and remove the connection if needed.
1460 *******************************************************************************/
1461 void smp_pairing_cmpl(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
1462 {
1463     if (p_cb->total_tx_unacked == 0)
1464     {
1465         /* update connection parameter to remote preferred */
1466         L2CA_EnableUpdateBleConnParams(p_cb->pairing_bda, TRUE);
1467         /* process the pairing complete */
1468         smp_proc_pairing_cmpl(p_cb);
1469     }
1470 }
1471
1472 /*******************************************************************************
1473 ** Function         smp_pair_terminate
1474 ** Description      This function is called to send the pairing complete callback
1475 **                  and remove the connection if needed.
1476 *******************************************************************************/
1477 void smp_pair_terminate(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
1478 {
1479     SMP_TRACE_DEBUG("%s", __func__);
1480     p_cb->status = SMP_CONN_TOUT;
1481     smp_proc_pairing_cmpl(p_cb);
1482 }
1483
1484 /*******************************************************************************
1485 ** Function         smp_idle_terminate
1486 ** Description      This function calledin idle state to determine to send authentication
1487 **                  complete or not.
1488 *******************************************************************************/
1489 void smp_idle_terminate(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
1490 {
1491     if (p_cb->flags & SMP_PAIR_FLAGS_WE_STARTED_DD)
1492     {
1493         SMP_TRACE_DEBUG("Pairing terminated at IDLE state.");
1494         p_cb->status = SMP_FAIL;
1495         smp_proc_pairing_cmpl(p_cb);
1496     }
1497 }
1498
1499 /*******************************************************************************
1500 ** Function     smp_fast_conn_param
1501 ** Description  apply default connection parameter for pairing process
1502 *******************************************************************************/
1503 void smp_fast_conn_param(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
1504 {
1505     /* disable connection parameter update */
1506     L2CA_EnableUpdateBleConnParams(p_cb->pairing_bda, FALSE);
1507 }
1508
1509 /*******************************************************************************
1510 ** Function     smp_both_have_public_keys
1511 ** Description  The function is called when both local and peer public keys are
1512 **              saved.
1513 **              Actions:
1514 **              - invokes DHKey computation;
1515 **              - on slave side invokes sending local public key to the peer.
1516 **              - invokes SC phase 1 process.
1517 *******************************************************************************/
1518 void smp_both_have_public_keys(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
1519 {
1520     SMP_TRACE_DEBUG("%s",__func__);
1521
1522     /* invokes DHKey computation */
1523     smp_compute_dhkey(p_cb);
1524
1525     /* on slave side invokes sending local public key to the peer */
1526     if (p_cb->role == HCI_ROLE_SLAVE)
1527         smp_send_pair_public_key(p_cb, NULL);
1528
1529     smp_sm_event(p_cb, SMP_SC_DHKEY_CMPLT_EVT, NULL);
1530 }
1531
1532 /*******************************************************************************
1533 ** Function     smp_start_secure_connection_phase1
1534 ** Description  The function starts Secure Connection phase1 i.e. invokes initialization of Secure Connection
1535 **              phase 1 parameters and starts building/sending to the peer
1536 **              messages appropriate for the role and association model.
1537 *******************************************************************************/
1538 void smp_start_secure_connection_phase1(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
1539 {
1540     SMP_TRACE_DEBUG("%s", __func__);
1541
1542     if (p_cb->selected_association_model == SMP_MODEL_SEC_CONN_JUSTWORKS)
1543     {
1544         p_cb->sec_level = SMP_SEC_UNAUTHENTICATE;
1545         SMP_TRACE_EVENT ("p_cb->sec_level =%d (SMP_SEC_UNAUTHENTICATE) ", p_cb->sec_level );
1546     }
1547     else
1548     {
1549         p_cb->sec_level = SMP_SEC_AUTHENTICATED;
1550         SMP_TRACE_EVENT ("p_cb->sec_level =%d (SMP_SEC_AUTHENTICATED) ", p_cb->sec_level );
1551     }
1552
1553     switch(p_cb->selected_association_model)
1554     {
1555         case SMP_MODEL_SEC_CONN_JUSTWORKS:
1556         case SMP_MODEL_SEC_CONN_NUM_COMP:
1557             memset(p_cb->local_random, 0, BT_OCTET16_LEN);
1558             smp_start_nonce_generation(p_cb);
1559             break;
1560         case SMP_MODEL_SEC_CONN_PASSKEY_ENT:
1561             /* user has to provide passkey */
1562             p_cb->cb_evt = SMP_PASSKEY_REQ_EVT;
1563             smp_sm_event(p_cb, SMP_TK_REQ_EVT, NULL);
1564             break;
1565         case SMP_MODEL_SEC_CONN_PASSKEY_DISP:
1566             /* passkey has to be provided to user */
1567             SMP_TRACE_DEBUG("Need to generate SC Passkey");
1568             smp_generate_passkey(p_cb, NULL);
1569             break;
1570         case SMP_MODEL_SEC_CONN_OOB:
1571             /* use the available OOB information */
1572             smp_process_secure_connection_oob_data(p_cb, NULL);
1573             break;
1574         default:
1575             SMP_TRACE_ERROR ("Association Model = %d is not used in LE SC",
1576                               p_cb->selected_association_model);
1577             break;
1578     }
1579 }
1580
1581 /*******************************************************************************
1582 ** Function     smp_process_local_nonce
1583 ** Description  The function processes new local nonce.
1584 **
1585 ** Note         It is supposed to be called in SC phase1.
1586 *******************************************************************************/
1587 void smp_process_local_nonce(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
1588 {
1589     SMP_TRACE_DEBUG("%s", __func__);
1590
1591     switch(p_cb->selected_association_model)
1592     {
1593         case SMP_MODEL_SEC_CONN_JUSTWORKS:
1594         case SMP_MODEL_SEC_CONN_NUM_COMP:
1595             if (p_cb->role == HCI_ROLE_SLAVE)
1596             {
1597                 /* slave calculates and sends local commitment */
1598                 smp_calculate_local_commitment(p_cb);
1599                 smp_send_commitment(p_cb, NULL);
1600                 /* slave has to wait for peer nonce */
1601                 smp_set_state(SMP_STATE_WAIT_NONCE);
1602             }
1603             else    /* i.e. master */
1604             {
1605                 if (p_cb->flags & SMP_PAIR_FLAG_HAVE_PEER_COMM)
1606                 {
1607                     /* slave commitment is already received, send local nonce, wait for remote nonce*/
1608                     SMP_TRACE_DEBUG("master in assoc mode = %d \
1609                     already rcvd slave commitment - race condition",
1610                                       p_cb->selected_association_model);
1611                     p_cb->flags &= ~SMP_PAIR_FLAG_HAVE_PEER_COMM;
1612                     smp_send_rand(p_cb, NULL);
1613                     smp_set_state(SMP_STATE_WAIT_NONCE);
1614                 }
1615             }
1616             break;
1617         case SMP_MODEL_SEC_CONN_PASSKEY_ENT:
1618         case SMP_MODEL_SEC_CONN_PASSKEY_DISP:
1619             smp_calculate_local_commitment(p_cb);
1620
1621             if (p_cb->role == HCI_ROLE_MASTER)
1622             {
1623                 smp_send_commitment(p_cb, NULL);
1624             }
1625             else    /* slave */
1626             {
1627                 if (p_cb->flags & SMP_PAIR_FLAG_HAVE_PEER_COMM)
1628                 {
1629                     /* master commitment is already received */
1630                     smp_send_commitment(p_cb, NULL);
1631                     smp_set_state(SMP_STATE_WAIT_NONCE);
1632                 }
1633             }
1634             break;
1635         case SMP_MODEL_SEC_CONN_OOB:
1636             if (p_cb->role == HCI_ROLE_MASTER)
1637             {
1638                 smp_send_rand(p_cb, NULL);
1639             }
1640
1641             smp_set_state(SMP_STATE_WAIT_NONCE);
1642             break;
1643         default:
1644             SMP_TRACE_ERROR ("Association Model = %d is not used in LE SC",
1645                               p_cb->selected_association_model);
1646             break;
1647     }
1648 }
1649
1650 /*******************************************************************************
1651 ** Function     smp_process_peer_nonce
1652 ** Description  The function processes newly received and saved in CB peer nonce.
1653 **              The actions depend on the selected association model and the role.
1654 **
1655 ** Note         It is supposed to be called in SC phase1.
1656 *******************************************************************************/
1657 void smp_process_peer_nonce(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
1658 {
1659     UINT8   reason;
1660
1661     SMP_TRACE_DEBUG("%s start ", __func__);
1662
1663     switch(p_cb->selected_association_model)
1664     {
1665         case SMP_MODEL_SEC_CONN_JUSTWORKS:
1666         case SMP_MODEL_SEC_CONN_NUM_COMP:
1667             /* in these models only master receives commitment */
1668             if (p_cb->role == HCI_ROLE_MASTER)
1669             {
1670                 if (!smp_check_commitment(p_cb))
1671                 {
1672                     reason = p_cb->failure = SMP_CONFIRM_VALUE_ERR;
1673                     smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &reason);
1674                     break;
1675                 }
1676             }
1677             else
1678             {
1679                 /* slave sends local nonce */
1680                 smp_send_rand(p_cb, NULL);
1681             }
1682
1683             if(p_cb->selected_association_model == SMP_MODEL_SEC_CONN_JUSTWORKS)
1684             {
1685                 /* go directly to phase 2 */
1686                 smp_sm_event(p_cb, SMP_SC_PHASE1_CMPLT_EVT, NULL);
1687             }
1688             else    /* numeric comparison */
1689             {
1690                 smp_set_state(SMP_STATE_WAIT_NONCE);
1691                 smp_sm_event(p_cb, SMP_SC_CALC_NC_EVT, NULL);
1692             }
1693             break;
1694         case SMP_MODEL_SEC_CONN_PASSKEY_ENT:
1695         case SMP_MODEL_SEC_CONN_PASSKEY_DISP:
1696             if (!smp_check_commitment(p_cb))
1697             {
1698                 reason = p_cb->failure = SMP_CONFIRM_VALUE_ERR;
1699                 smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &reason);
1700                 break;
1701             }
1702
1703             if (p_cb->role == HCI_ROLE_SLAVE)
1704             {
1705                 smp_send_rand(p_cb, NULL);
1706             }
1707
1708             if (++p_cb->round < 20)
1709             {
1710                 smp_set_state(SMP_STATE_SEC_CONN_PHS1_START);
1711                 p_cb->flags &= ~SMP_PAIR_FLAG_HAVE_PEER_COMM;
1712                 smp_start_nonce_generation(p_cb);
1713                 break;
1714             }
1715
1716             smp_sm_event(p_cb, SMP_SC_PHASE1_CMPLT_EVT, NULL);
1717             break;
1718         case SMP_MODEL_SEC_CONN_OOB:
1719             if (p_cb->role == HCI_ROLE_SLAVE)
1720             {
1721                 smp_send_rand(p_cb, NULL);
1722             }
1723
1724             smp_sm_event(p_cb, SMP_SC_PHASE1_CMPLT_EVT, NULL);
1725             break;
1726         default:
1727             SMP_TRACE_ERROR ("Association Model = %d is not used in LE SC",
1728                               p_cb->selected_association_model);
1729             break;
1730     }
1731
1732     SMP_TRACE_DEBUG("%s end ",__FUNCTION__);
1733 }
1734
1735 /*******************************************************************************
1736 ** Function     smp_match_dhkey_checks
1737 ** Description  checks if the calculated peer DHKey Check value is the same as
1738 **              received from the peer DHKey check value.
1739 *******************************************************************************/
1740 void smp_match_dhkey_checks(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
1741 {
1742     UINT8 reason = SMP_DHKEY_CHK_FAIL;
1743
1744     SMP_TRACE_DEBUG("%s", __func__);
1745
1746     if (memcmp(p_data->key.p_data, p_cb->remote_dhkey_check, BT_OCTET16_LEN))
1747     {
1748         SMP_TRACE_WARNING ("dhkey chcks do no match");
1749         p_cb->failure = reason;
1750         smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &reason);
1751         return;
1752     }
1753
1754     SMP_TRACE_EVENT ("dhkey chcks match");
1755
1756     /* compare the max encryption key size, and save the smaller one for the link */
1757     if (p_cb->peer_enc_size < p_cb->loc_enc_size)
1758         p_cb->loc_enc_size = p_cb->peer_enc_size;
1759
1760     if (p_cb->role == HCI_ROLE_SLAVE)
1761     {
1762         smp_sm_event(p_cb, SMP_PAIR_DHKEY_CHCK_EVT, NULL);
1763     }
1764     else
1765     {
1766         /* master device always use received i/r key as keys to distribute */
1767         p_cb->local_i_key = p_cb->peer_i_key;
1768         p_cb->local_r_key = p_cb->peer_r_key;
1769         smp_sm_event(p_cb, SMP_ENC_REQ_EVT, NULL);
1770     }
1771 }
1772
1773 /*******************************************************************************
1774 ** Function     smp_move_to_secure_connections_phase2
1775 ** Description  Signal State Machine to start SC phase 2 initialization (to
1776 **              compute local DHKey Check value).
1777 **
1778 ** Note         SM is supposed to be in the state SMP_STATE_SEC_CONN_PHS2_START.
1779 *******************************************************************************/
1780 void smp_move_to_secure_connections_phase2(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
1781 {
1782     SMP_TRACE_DEBUG("%s",__func__);
1783     smp_sm_event(p_cb, SMP_SC_PHASE1_CMPLT_EVT, NULL);
1784 }
1785
1786 /*******************************************************************************
1787 ** Function     smp_phase_2_dhkey_checks_are_present
1788 ** Description  generates event if dhkey check from the peer is already received.
1789 **
1790 ** Note         It is supposed to be used on slave to prevent race condition.
1791 **              It is supposed to be called after slave dhkey check is calculated.
1792 *******************************************************************************/
1793 void smp_phase_2_dhkey_checks_are_present(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
1794 {
1795     SMP_TRACE_DEBUG("%s",__func__);
1796
1797     if (p_cb->flags & SMP_PAIR_FLAG_HAVE_PEER_DHK_CHK)
1798         smp_sm_event(p_cb, SMP_SC_2_DHCK_CHKS_PRES_EVT, NULL);
1799 }
1800
1801 /*******************************************************************************
1802 ** Function     smp_wait_for_both_public_keys
1803 ** Description  generates SMP_BOTH_PUBL_KEYS_RCVD_EVT event when both local and master
1804 **              public keys are available.
1805 **
1806 ** Note         on the slave it is used to prevent race condition.
1807 **
1808 *******************************************************************************/
1809 void smp_wait_for_both_public_keys(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
1810 {
1811     SMP_TRACE_DEBUG("%s",__func__);
1812
1813     if ((p_cb->flags & SMP_PAIR_FLAG_HAVE_PEER_PUBL_KEY) &&
1814         (p_cb->flags & SMP_PAIR_FLAG_HAVE_LOCAL_PUBL_KEY))
1815     {
1816         if ((p_cb->role == HCI_ROLE_SLAVE) &&
1817             ((p_cb->req_oob_type == SMP_OOB_LOCAL) || (p_cb->req_oob_type == SMP_OOB_BOTH)))
1818         {
1819             smp_set_state(SMP_STATE_PUBLIC_KEY_EXCH);
1820         }
1821         smp_sm_event(p_cb, SMP_BOTH_PUBL_KEYS_RCVD_EVT, NULL);
1822     }
1823 }
1824
1825 /*******************************************************************************
1826 ** Function     smp_start_passkey_verification
1827 ** Description  Starts SC passkey entry verification.
1828 *******************************************************************************/
1829 void smp_start_passkey_verification(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
1830 {
1831     UINT8 *p = NULL;
1832
1833     SMP_TRACE_DEBUG("%s", __func__);
1834     p = p_cb->local_random;
1835     UINT32_TO_STREAM(p, p_data->passkey);
1836
1837     p = p_cb->peer_random;
1838     UINT32_TO_STREAM(p, p_data->passkey);
1839
1840     p_cb->round = 0;
1841     smp_start_nonce_generation(p_cb);
1842 }
1843
1844 /*******************************************************************************
1845 ** Function     smp_process_secure_connection_oob_data
1846 ** Description  Processes local/peer SC OOB data received from somewhere.
1847 *******************************************************************************/
1848 void smp_process_secure_connection_oob_data(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
1849 {
1850     SMP_TRACE_DEBUG("%s", __func__);
1851
1852     tSMP_SC_OOB_DATA *p_sc_oob_data = &p_cb->sc_oob_data;
1853     if (p_sc_oob_data->loc_oob_data.present)
1854     {
1855         memcpy(p_cb->local_random, p_sc_oob_data->loc_oob_data.randomizer,
1856                sizeof(p_cb->local_random));
1857     }
1858     else
1859     {
1860         SMP_TRACE_EVENT ("local OOB randomizer is absent");
1861         memset(p_cb->local_random, 0, sizeof (p_cb->local_random));
1862     }
1863
1864     if (!p_sc_oob_data->peer_oob_data.present)
1865     {
1866         SMP_TRACE_EVENT ("peer OOB data is absent");
1867         memset(p_cb->peer_random, 0, sizeof (p_cb->peer_random));
1868     }
1869     else
1870     {
1871         memcpy(p_cb->peer_random, p_sc_oob_data->peer_oob_data.randomizer,
1872                sizeof(p_cb->peer_random));
1873         memcpy(p_cb->remote_commitment, p_sc_oob_data->peer_oob_data.commitment,
1874                sizeof(p_cb->remote_commitment));
1875
1876         UINT8 reason = SMP_CONFIRM_VALUE_ERR;
1877         /* check commitment */
1878         if (!smp_check_commitment(p_cb))
1879         {
1880             p_cb->failure = reason;
1881             smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &reason);
1882             return;
1883         }
1884
1885         if (p_cb->peer_oob_flag != SMP_OOB_PRESENT)
1886         {
1887             /* the peer doesn't have local randomiser */
1888             SMP_TRACE_EVENT ("peer didn't receive local OOB data, set local randomizer to 0");
1889             memset(p_cb->local_random, 0, sizeof (p_cb->local_random));
1890         }
1891     }
1892
1893     print128(p_cb->local_random, (const UINT8 *)"local OOB randomizer");
1894     print128(p_cb->peer_random, (const UINT8 *)"peer OOB randomizer");
1895     smp_start_nonce_generation(p_cb);
1896 }
1897
1898 /*******************************************************************************
1899 ** Function     smp_set_local_oob_keys
1900 ** Description  Saves calculated private/public keys in sc_oob_data.loc_oob_data,
1901 **              starts nonce generation
1902 **              (to be saved in sc_oob_data.loc_oob_data.randomizer).
1903 *******************************************************************************/
1904 void smp_set_local_oob_keys(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
1905 {
1906     SMP_TRACE_DEBUG("%s", __func__);
1907
1908     memcpy(p_cb->sc_oob_data.loc_oob_data.private_key_used, p_cb->private_key,
1909            BT_OCTET32_LEN);
1910     p_cb->sc_oob_data.loc_oob_data.publ_key_used = p_cb->loc_publ_key;
1911     smp_start_nonce_generation(p_cb);
1912 }
1913
1914 /*******************************************************************************
1915 ** Function     smp_set_local_oob_random_commitment
1916 ** Description  Saves calculated randomizer and commitment in sc_oob_data.loc_oob_data,
1917 **              passes sc_oob_data.loc_oob_data up for safekeeping.
1918 *******************************************************************************/
1919 void smp_set_local_oob_random_commitment(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
1920 {
1921     SMP_TRACE_DEBUG("%s", __func__);
1922     memcpy(p_cb->sc_oob_data.loc_oob_data.randomizer, p_cb->rand,
1923            BT_OCTET16_LEN);
1924
1925     smp_calculate_f4(p_cb->sc_oob_data.loc_oob_data.publ_key_used.x,
1926                      p_cb->sc_oob_data.loc_oob_data.publ_key_used.x,
1927                      p_cb->sc_oob_data.loc_oob_data.randomizer, 0,
1928                      p_cb->sc_oob_data.loc_oob_data.commitment);
1929
1930 #if SMP_DEBUG == TRUE
1931     UINT8   *p_print = NULL;
1932     SMP_TRACE_DEBUG("local SC OOB data set:");
1933     p_print = (UINT8*) &p_cb->sc_oob_data.loc_oob_data.addr_sent_to;
1934     smp_debug_print_nbyte_little_endian (p_print, (const UINT8 *)"addr_sent_to",
1935                                          sizeof(tBLE_BD_ADDR));
1936     p_print = (UINT8*) &p_cb->sc_oob_data.loc_oob_data.private_key_used;
1937     smp_debug_print_nbyte_little_endian (p_print, (const UINT8 *)"private_key_used",
1938                                          BT_OCTET32_LEN);
1939     p_print = (UINT8*) &p_cb->sc_oob_data.loc_oob_data.publ_key_used.x;
1940     smp_debug_print_nbyte_little_endian (p_print, (const UINT8 *)"publ_key_used.x",
1941                                          BT_OCTET32_LEN);
1942     p_print = (UINT8*) &p_cb->sc_oob_data.loc_oob_data.publ_key_used.y;
1943     smp_debug_print_nbyte_little_endian (p_print, (const UINT8 *)"publ_key_used.y",
1944                                          BT_OCTET32_LEN);
1945     p_print = (UINT8*) &p_cb->sc_oob_data.loc_oob_data.randomizer;
1946     smp_debug_print_nbyte_little_endian (p_print, (const UINT8 *)"randomizer",
1947                                          BT_OCTET16_LEN);
1948     p_print = (UINT8*) &p_cb->sc_oob_data.loc_oob_data.commitment;
1949     smp_debug_print_nbyte_little_endian (p_print,(const UINT8 *) "commitment",
1950                                          BT_OCTET16_LEN);
1951     SMP_TRACE_DEBUG("");
1952 #endif
1953
1954     /* pass created OOB data up */
1955     p_cb->cb_evt = SMP_SC_LOC_OOB_DATA_UP_EVT;
1956     smp_send_app_cback(p_cb, NULL);
1957
1958     smp_cb_cleanup(p_cb);
1959 }
1960
1961 /*******************************************************************************
1962 **
1963 ** Function         smp_link_encrypted
1964 **
1965 ** Description      This function is called when link is encrypted and notified to
1966 **                  slave device. Proceed to to send LTK, DIV and ER to master if
1967 **                  bonding the devices.
1968 **
1969 **
1970 ** Returns          void
1971 **
1972 *******************************************************************************/
1973 void smp_link_encrypted(BD_ADDR bda, UINT8 encr_enable)
1974 {
1975     tSMP_CB *p_cb = &smp_cb;
1976
1977     SMP_TRACE_DEBUG("%s encr_enable=%d", __func__, encr_enable);
1978
1979     if (memcmp(&smp_cb.pairing_bda[0], bda, BD_ADDR_LEN) == 0)
1980     {
1981         /* encryption completed with STK, remmeber the key size now, could be overwite
1982         *  when key exchange happens                                        */
1983         if (p_cb->loc_enc_size != 0 && encr_enable)
1984         {
1985             /* update the link encryption key size if a SMP pairing just performed */
1986             btm_ble_update_sec_key_size(bda, p_cb->loc_enc_size);
1987         }
1988
1989         smp_sm_event(&smp_cb, SMP_ENCRYPTED_EVT, &encr_enable);
1990     }
1991 }
1992
1993 /*******************************************************************************
1994 **
1995 ** Function         smp_proc_ltk_request
1996 **
1997 ** Description      This function is called when LTK request is received from
1998 **                  controller.
1999 **
2000 ** Returns          void
2001 **
2002 *******************************************************************************/
2003 BOOLEAN smp_proc_ltk_request(BD_ADDR bda)
2004 {
2005     SMP_TRACE_DEBUG("%s state = %d",  __func__, smp_cb.state);
2006     BOOLEAN match = FALSE;
2007
2008     if (!memcmp(bda, smp_cb.pairing_bda, BD_ADDR_LEN))
2009     {
2010         match = TRUE;
2011     } else {
2012         BD_ADDR dummy_bda = {0};
2013         tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev(bda);
2014         if (p_dev_rec != NULL &&
2015             0 == memcmp(p_dev_rec->ble.pseudo_addr, smp_cb.pairing_bda, BD_ADDR_LEN) &&
2016             0 != memcmp(p_dev_rec->ble.pseudo_addr, dummy_bda, BD_ADDR_LEN))
2017         {
2018             match = TRUE;
2019         }
2020     }
2021
2022     if (match && smp_cb.state == SMP_STATE_ENCRYPTION_PENDING)
2023     {
2024         smp_sm_event(&smp_cb, SMP_ENC_REQ_EVT, NULL);
2025         return TRUE;
2026     }
2027
2028     return FALSE;
2029 }
2030
2031 /*******************************************************************************
2032 **
2033 ** Function         smp_process_secure_connection_long_term_key
2034 **
2035 ** Description      This function is called to process SC LTK.
2036 **                  SC LTK is calculated and used instead of STK.
2037 **                  Here SC LTK is saved in BLE DB.
2038 **
2039 ** Returns          void
2040 **
2041 *******************************************************************************/
2042 void smp_process_secure_connection_long_term_key(void)
2043 {
2044     tSMP_CB     *p_cb = &smp_cb;
2045
2046     SMP_TRACE_DEBUG("%s", __func__);
2047     smp_save_secure_connections_long_term_key(p_cb);
2048
2049     smp_update_key_mask (p_cb, SMP_SEC_KEY_TYPE_ENC, FALSE);
2050     smp_key_distribution(p_cb, NULL);
2051 }
2052
2053 /*******************************************************************************
2054 **
2055 ** Function         smp_set_derive_link_key
2056 **
2057 ** Description      This function is called to set flag that indicates that
2058 **                  BR/EDR LK has to be derived from LTK after all keys are
2059 **                  distributed.
2060 **
2061 ** Returns          void
2062 **
2063 *******************************************************************************/
2064 void smp_set_derive_link_key(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
2065 {
2066     SMP_TRACE_DEBUG ("%s", __func__);
2067     p_cb->derive_lk = TRUE;
2068     smp_update_key_mask (p_cb, SMP_SEC_KEY_TYPE_LK, FALSE);
2069     smp_key_distribution(p_cb, NULL);
2070 }
2071
2072 /*******************************************************************************
2073 **
2074 ** Function         smp_derive_link_key_from_long_term_key
2075 **
2076 ** Description      This function is called to derive BR/EDR LK from LTK.
2077 **
2078 ** Returns          void
2079 **
2080 *******************************************************************************/
2081 void smp_derive_link_key_from_long_term_key(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
2082 {
2083     tSMP_STATUS status = SMP_PAIR_FAIL_UNKNOWN;
2084
2085     SMP_TRACE_DEBUG("%s", __func__);
2086     if (!smp_calculate_link_key_from_long_term_key(p_cb))
2087     {
2088         SMP_TRACE_ERROR("%s failed", __FUNCTION__);
2089         smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &status);
2090         return;
2091     }
2092 }
2093
2094 /*******************************************************************************
2095 **
2096 ** Function         smp_br_process_link_key
2097 **
2098 ** Description      This function is called to process BR/EDR LK:
2099 **                  - to derive SMP LTK from BR/EDR LK;
2100 *8                  - to save SMP LTK.
2101 **
2102 ** Returns          void
2103 **
2104 *******************************************************************************/
2105 void smp_br_process_link_key(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
2106 {
2107     tSMP_STATUS status = SMP_PAIR_FAIL_UNKNOWN;
2108
2109     SMP_TRACE_DEBUG("%s", __func__);
2110     if (!smp_calculate_long_term_key_from_link_key(p_cb))
2111     {
2112         SMP_TRACE_ERROR ("%s failed",__FUNCTION__);
2113         smp_sm_event(p_cb, SMP_BR_AUTH_CMPL_EVT, &status);
2114         return;
2115     }
2116
2117     SMP_TRACE_DEBUG("%s: LTK derivation from LK successfully completed", __FUNCTION__);
2118     smp_save_secure_connections_long_term_key(p_cb);
2119     smp_update_key_mask (p_cb, SMP_SEC_KEY_TYPE_ENC, FALSE);
2120     smp_br_select_next_key(p_cb, NULL);
2121 }
2122
2123 /*******************************************************************************
2124 ** Function     smp_key_distribution_by_transport
2125 ** Description  depending on the transport used at the moment calls either
2126 **              smp_key_distribution(...) or smp_br_key_distribution(...).
2127 *******************************************************************************/
2128 void smp_key_distribution_by_transport(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
2129 {
2130     SMP_TRACE_DEBUG("%s", __func__);
2131     if (p_cb->smp_over_br)
2132     {
2133         smp_br_select_next_key(p_cb, NULL);
2134     }
2135     else
2136     {
2137         smp_key_distribution(p_cb, NULL);
2138     }
2139 }
2140
2141 /*******************************************************************************
2142 ** Function         smp_br_pairing_complete
2143 ** Description      This function is called to send the pairing complete callback
2144 **                  and remove the connection if needed.
2145 *******************************************************************************/
2146 void smp_br_pairing_complete(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
2147 {
2148     SMP_TRACE_DEBUG("%s", __func__);
2149
2150     if (p_cb->total_tx_unacked == 0)
2151     {
2152         /* process the pairing complete */
2153         smp_proc_pairing_cmpl(p_cb);
2154     }
2155 }
2156
2157 #endif