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