blob: c578496c8fac3030d1d5677b94326dd659ea72a5 [file] [log] [blame]
The Android Open Source Project5738f832012-12-12 16:00:35 -08001/******************************************************************************
2 *
3 * Copyright (C) 1999-2012 Broadcom Corporation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
Satya Calloji444a8da2015-03-06 10:38:22 -080017******************************************************************************/
The Android Open Source Project5738f832012-12-12 16:00:35 -080018
19/******************************************************************************
20 *
Satya Calloji444a8da2015-03-06 10:38:22 -080021 * This file contains functions for the SMP L2CAP utility functions
The Android Open Source Project5738f832012-12-12 16:00:35 -080022 *
23 ******************************************************************************/
24#include "bt_target.h"
25
Marie Janssend19e0782016-07-15 12:48:27 -070026#if (SMP_INCLUDED == TRUE)
The Android Open Source Project5738f832012-12-12 16:00:35 -080027
28#include "bt_types.h"
Mike J. Chen5cd8bff2014-01-31 18:16:59 -080029#include "bt_utils.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080030#include <string.h>
31#include <ctype.h>
32#include "hcidefs.h"
33#include "btm_ble_api.h"
34#include "l2c_api.h"
35#include "l2c_int.h"
36#include "smp_int.h"
Chris Manton79ecab52014-10-31 14:54:51 -070037#include "device/include/controller.h"
Satya Calloji444a8da2015-03-06 10:38:22 -080038#include "btm_int.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080039
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -080040
41extern fixed_queue_t *btu_general_alarm_queue;
42
The Android Open Source Project5738f832012-12-12 16:00:35 -080043#define SMP_PAIRING_REQ_SIZE 7
44#define SMP_CONFIRM_CMD_SIZE (BT_OCTET16_LEN + 1)
Satya Calloji444a8da2015-03-06 10:38:22 -080045#define SMP_RAND_CMD_SIZE (BT_OCTET16_LEN + 1)
The Android Open Source Project5738f832012-12-12 16:00:35 -080046#define SMP_INIT_CMD_SIZE (BT_OCTET16_LEN + 1)
47#define SMP_ENC_INFO_SIZE (BT_OCTET16_LEN + 1)
48#define SMP_MASTER_ID_SIZE (BT_OCTET8_LEN + 2 + 1)
49#define SMP_ID_INFO_SIZE (BT_OCTET16_LEN + 1)
50#define SMP_ID_ADDR_SIZE (BD_ADDR_LEN + 1 + 1)
51#define SMP_SIGN_INFO_SIZE (BT_OCTET16_LEN + 1)
52#define SMP_PAIR_FAIL_SIZE 2
Satya Calloji444a8da2015-03-06 10:38:22 -080053#define SMP_SECURITY_REQUEST_SIZE 2
54#define SMP_PAIR_PUBL_KEY_SIZE (1 /* opcode */ + (2*BT_OCTET32_LEN))
55#define SMP_PAIR_COMMITM_SIZE (1 /* opcode */ + BT_OCTET16_LEN /*Commitment*/)
56#define SMP_PAIR_DHKEY_CHECK_SIZE (1 /* opcode */ + BT_OCTET16_LEN /*DHKey Check*/)
57#define SMP_PAIR_KEYPR_NOTIF_SIZE (1 /* opcode */ + 1 /*Notif Type*/)
The Android Open Source Project5738f832012-12-12 16:00:35 -080058
Satya Calloji444a8da2015-03-06 10:38:22 -080059/* SMP command sizes per spec */
Marie Janssend19e0782016-07-15 12:48:27 -070060static const uint8_t smp_cmd_size_per_spec[] =
Satya Calloji444a8da2015-03-06 10:38:22 -080061{
62 0,
63 SMP_PAIRING_REQ_SIZE, /* 0x01: pairing request */
64 SMP_PAIRING_REQ_SIZE, /* 0x02: pairing response */
65 SMP_CONFIRM_CMD_SIZE, /* 0x03: pairing confirm */
66 SMP_RAND_CMD_SIZE, /* 0x04: pairing random */
67 SMP_PAIR_FAIL_SIZE, /* 0x05: pairing failed */
68 SMP_ENC_INFO_SIZE, /* 0x06: encryption information */
69 SMP_MASTER_ID_SIZE, /* 0x07: master identification */
70 SMP_ID_INFO_SIZE, /* 0x08: identity information */
71 SMP_ID_ADDR_SIZE, /* 0x09: identity address information */
72 SMP_SIGN_INFO_SIZE, /* 0x0A: signing information */
73 SMP_SECURITY_REQUEST_SIZE, /* 0x0B: security request */
74 SMP_PAIR_PUBL_KEY_SIZE, /* 0x0C: pairing public key */
75 SMP_PAIR_DHKEY_CHECK_SIZE, /* 0x0D: pairing dhkey check */
76 SMP_PAIR_KEYPR_NOTIF_SIZE, /* 0x0E: pairing keypress notification */
77 SMP_PAIR_COMMITM_SIZE /* 0x0F: pairing commitment */
78};
79
Marie Janssend19e0782016-07-15 12:48:27 -070080static bool smp_parameter_unconditionally_valid(tSMP_CB *p_cb);
81static bool smp_parameter_unconditionally_invalid(tSMP_CB *p_cb);
Satya Calloji444a8da2015-03-06 10:38:22 -080082
83/* type for SMP command length validation functions */
Marie Janssend19e0782016-07-15 12:48:27 -070084typedef bool (*tSMP_CMD_LEN_VALID)(tSMP_CB *p_cb);
Satya Calloji444a8da2015-03-06 10:38:22 -080085
Marie Janssend19e0782016-07-15 12:48:27 -070086static bool smp_command_has_valid_fixed_length(tSMP_CB *p_cb);
Satya Calloji444a8da2015-03-06 10:38:22 -080087
88static const tSMP_CMD_LEN_VALID smp_cmd_len_is_valid[] =
89{
90 smp_parameter_unconditionally_invalid,
91 smp_command_has_valid_fixed_length, /* 0x01: pairing request */
92 smp_command_has_valid_fixed_length, /* 0x02: pairing response */
93 smp_command_has_valid_fixed_length, /* 0x03: pairing confirm */
94 smp_command_has_valid_fixed_length, /* 0x04: pairing random */
95 smp_command_has_valid_fixed_length, /* 0x05: pairing failed */
96 smp_command_has_valid_fixed_length, /* 0x06: encryption information */
97 smp_command_has_valid_fixed_length, /* 0x07: master identification */
98 smp_command_has_valid_fixed_length, /* 0x08: identity information */
99 smp_command_has_valid_fixed_length, /* 0x09: identity address information */
100 smp_command_has_valid_fixed_length, /* 0x0A: signing information */
101 smp_command_has_valid_fixed_length, /* 0x0B: security request */
102 smp_command_has_valid_fixed_length, /* 0x0C: pairing public key */
103 smp_command_has_valid_fixed_length, /* 0x0D: pairing dhkey check */
104 smp_command_has_valid_fixed_length, /* 0x0E: pairing keypress notification */
105 smp_command_has_valid_fixed_length /* 0x0F: pairing commitment */
106};
107
108/* type for SMP command parameter ranges validation functions */
Marie Janssend19e0782016-07-15 12:48:27 -0700109typedef bool (*tSMP_CMD_PARAM_RANGES_VALID)(tSMP_CB *p_cb);
Satya Calloji444a8da2015-03-06 10:38:22 -0800110
Marie Janssend19e0782016-07-15 12:48:27 -0700111static bool smp_pairing_request_response_parameters_are_valid(tSMP_CB *p_cb);
112static bool smp_pairing_keypress_notification_is_valid(tSMP_CB *p_cb);
Satya Calloji444a8da2015-03-06 10:38:22 -0800113
114static const tSMP_CMD_PARAM_RANGES_VALID smp_cmd_param_ranges_are_valid[] =
115{
116 smp_parameter_unconditionally_invalid,
117 smp_pairing_request_response_parameters_are_valid, /* 0x01: pairing request */
118 smp_pairing_request_response_parameters_are_valid, /* 0x02: pairing response */
119 smp_parameter_unconditionally_valid, /* 0x03: pairing confirm */
120 smp_parameter_unconditionally_valid, /* 0x04: pairing random */
121 smp_parameter_unconditionally_valid, /* 0x05: pairing failed */
122 smp_parameter_unconditionally_valid, /* 0x06: encryption information */
123 smp_parameter_unconditionally_valid, /* 0x07: master identification */
124 smp_parameter_unconditionally_valid, /* 0x08: identity information */
125 smp_parameter_unconditionally_valid, /* 0x09: identity address information */
126 smp_parameter_unconditionally_valid, /* 0x0A: signing information */
127 smp_parameter_unconditionally_valid, /* 0x0B: security request */
128 smp_parameter_unconditionally_valid, /* 0x0C: pairing public key */
129 smp_parameter_unconditionally_valid, /* 0x0D: pairing dhkey check */
130 smp_pairing_keypress_notification_is_valid, /* 0x0E: pairing keypress notification */
131 smp_parameter_unconditionally_valid /* 0x0F: pairing commitment */
132};
The Android Open Source Project5738f832012-12-12 16:00:35 -0800133
134/* type for action functions */
Marie Janssend19e0782016-07-15 12:48:27 -0700135typedef BT_HDR * (*tSMP_CMD_ACT)(uint8_t cmd_code, tSMP_CB *p_cb);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800136
Marie Janssend19e0782016-07-15 12:48:27 -0700137static BT_HDR *smp_build_pairing_cmd(uint8_t cmd_code, tSMP_CB *p_cb);
Myles Watsond35a6482016-10-27 08:52:16 -0700138static BT_HDR *smp_build_confirm_cmd(UNUSED_ATTR uint8_t cmd_code, tSMP_CB *p_cb);
139static BT_HDR *smp_build_rand_cmd(UNUSED_ATTR uint8_t cmd_code, tSMP_CB *p_cb);
140static BT_HDR *smp_build_pairing_fail(UNUSED_ATTR uint8_t cmd_code, tSMP_CB *p_cb);
141static BT_HDR *smp_build_identity_info_cmd(UNUSED_ATTR uint8_t cmd_code, tSMP_CB *p_cb);
142static BT_HDR *smp_build_encrypt_info_cmd(UNUSED_ATTR uint8_t cmd_code, tSMP_CB *p_cb);
143static BT_HDR *smp_build_security_request(UNUSED_ATTR uint8_t cmd_code, tSMP_CB *p_cb);
144static BT_HDR *smp_build_signing_info_cmd(UNUSED_ATTR uint8_t cmd_code, tSMP_CB *p_cb);
145static BT_HDR *smp_build_master_id_cmd(UNUSED_ATTR uint8_t cmd_code, tSMP_CB *p_cb);
146static BT_HDR *smp_build_id_addr_cmd(UNUSED_ATTR uint8_t cmd_code, tSMP_CB *p_cb);
147static BT_HDR *smp_build_pair_public_key_cmd(UNUSED_ATTR uint8_t cmd_code, tSMP_CB *p_cb);
148static BT_HDR *smp_build_pairing_commitment_cmd(UNUSED_ATTR uint8_t cmd_code, tSMP_CB *p_cb);
149static BT_HDR *smp_build_pair_dhkey_check_cmd(UNUSED_ATTR uint8_t cmd_code, tSMP_CB *p_cb);
150static BT_HDR *smp_build_pairing_keypress_notification_cmd(UNUSED_ATTR uint8_t cmd_code, tSMP_CB *p_cb);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800151
Satya Calloji444a8da2015-03-06 10:38:22 -0800152static const tSMP_CMD_ACT smp_cmd_build_act[] =
The Android Open Source Project5738f832012-12-12 16:00:35 -0800153{
154 NULL,
Satya Calloji444a8da2015-03-06 10:38:22 -0800155 smp_build_pairing_cmd, /* 0x01: pairing request */
156 smp_build_pairing_cmd, /* 0x02: pairing response */
157 smp_build_confirm_cmd, /* 0x03: pairing confirm */
158 smp_build_rand_cmd, /* 0x04: pairing random */
159 smp_build_pairing_fail, /* 0x05: pairing failure */
160 smp_build_encrypt_info_cmd, /* 0x06: encryption information */
161 smp_build_master_id_cmd, /* 0x07: master identification */
162 smp_build_identity_info_cmd, /* 0x08: identity information */
163 smp_build_id_addr_cmd, /* 0x09: identity address information */
164 smp_build_signing_info_cmd, /* 0x0A: signing information */
165 smp_build_security_request, /* 0x0B: security request */
166 smp_build_pair_public_key_cmd, /* 0x0C: pairing public key */
167 smp_build_pair_dhkey_check_cmd, /* 0x0D: pairing DHKey check */
168 smp_build_pairing_keypress_notification_cmd, /* 0x0E: pairing keypress notification */
169 smp_build_pairing_commitment_cmd /* 0x0F: pairing commitment */
The Android Open Source Project5738f832012-12-12 16:00:35 -0800170};
Satya Calloji444a8da2015-03-06 10:38:22 -0800171
Marie Janssend19e0782016-07-15 12:48:27 -0700172static const uint8_t smp_association_table[2][SMP_IO_CAP_MAX][SMP_IO_CAP_MAX] =
Satya Calloji444a8da2015-03-06 10:38:22 -0800173{
174 /* display only */ /* Display Yes/No */ /* keyboard only */
175 /* No Input/Output */ /* keyboard display */
176
177 /* initiator */
178 /* model = tbl[peer_io_caps][loc_io_caps] */
179 /* Display Only */
180 {{SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_PASSKEY,
181 SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_PASSKEY},
182
183 /* Display Yes/No */
184 {SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_PASSKEY,
185 SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_PASSKEY},
186
187 /* Keyboard only */
188 {SMP_MODEL_KEY_NOTIF, SMP_MODEL_KEY_NOTIF, SMP_MODEL_PASSKEY,
189 SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_KEY_NOTIF},
190
191 /* No Input No Output */
192 {SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_ENCRYPTION_ONLY,
193 SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_ENCRYPTION_ONLY},
194
195 /* keyboard display */
196 {SMP_MODEL_KEY_NOTIF, SMP_MODEL_KEY_NOTIF, SMP_MODEL_PASSKEY,
197 SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_KEY_NOTIF}},
198
199 /* responder */
200 /* model = tbl[loc_io_caps][peer_io_caps] */
201 /* Display Only */
202 {{SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_KEY_NOTIF,
203 SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_KEY_NOTIF},
204
205 /* Display Yes/No */
206 {SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_KEY_NOTIF,
207 SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_KEY_NOTIF},
208
209 /* keyboard only */
210 {SMP_MODEL_PASSKEY, SMP_MODEL_PASSKEY, SMP_MODEL_PASSKEY,
211 SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_PASSKEY},
212
213 /* No Input No Output */
214 {SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_ENCRYPTION_ONLY,
215 SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_ENCRYPTION_ONLY},
216
217 /* keyboard display */
218 {SMP_MODEL_PASSKEY, SMP_MODEL_PASSKEY, SMP_MODEL_KEY_NOTIF,
219 SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_PASSKEY}}
220};
221
Marie Janssend19e0782016-07-15 12:48:27 -0700222static const uint8_t smp_association_table_sc[2][SMP_IO_CAP_MAX][SMP_IO_CAP_MAX] =
Satya Calloji444a8da2015-03-06 10:38:22 -0800223{
224 /* display only */ /* Display Yes/No */ /* keyboard only */
225 /* No InputOutput */ /* keyboard display */
226
227 /* initiator */
228 /* model = tbl[peer_io_caps][loc_io_caps] */
229
230 /* Display Only */
231 {{SMP_MODEL_SEC_CONN_JUSTWORKS, SMP_MODEL_SEC_CONN_JUSTWORKS, SMP_MODEL_SEC_CONN_PASSKEY_ENT,
232 SMP_MODEL_SEC_CONN_JUSTWORKS, SMP_MODEL_SEC_CONN_PASSKEY_ENT},
233
234 /* Display Yes/No */
235 {SMP_MODEL_SEC_CONN_JUSTWORKS, SMP_MODEL_SEC_CONN_NUM_COMP, SMP_MODEL_SEC_CONN_PASSKEY_ENT,
236 SMP_MODEL_SEC_CONN_JUSTWORKS, SMP_MODEL_SEC_CONN_NUM_COMP},
237
238 /* keyboard only */
239 {SMP_MODEL_SEC_CONN_PASSKEY_DISP, SMP_MODEL_SEC_CONN_PASSKEY_DISP, SMP_MODEL_SEC_CONN_PASSKEY_ENT,
240 SMP_MODEL_SEC_CONN_JUSTWORKS, SMP_MODEL_SEC_CONN_PASSKEY_DISP},
241
242 /* No Input No Output */
243 {SMP_MODEL_SEC_CONN_JUSTWORKS, SMP_MODEL_SEC_CONN_JUSTWORKS, SMP_MODEL_SEC_CONN_JUSTWORKS,
244 SMP_MODEL_SEC_CONN_JUSTWORKS, SMP_MODEL_SEC_CONN_JUSTWORKS},
245
246 /* keyboard display */
247 {SMP_MODEL_SEC_CONN_PASSKEY_DISP, SMP_MODEL_SEC_CONN_NUM_COMP, SMP_MODEL_SEC_CONN_PASSKEY_ENT,
248 SMP_MODEL_SEC_CONN_JUSTWORKS, SMP_MODEL_SEC_CONN_NUM_COMP}},
249
250 /* responder */
251 /* model = tbl[loc_io_caps][peer_io_caps] */
252
253 /* Display Only */
254 {{SMP_MODEL_SEC_CONN_JUSTWORKS, SMP_MODEL_SEC_CONN_JUSTWORKS, SMP_MODEL_SEC_CONN_PASSKEY_DISP,
255 SMP_MODEL_SEC_CONN_JUSTWORKS, SMP_MODEL_SEC_CONN_PASSKEY_DISP},
256
257 /* Display Yes/No */
258 {SMP_MODEL_SEC_CONN_JUSTWORKS, SMP_MODEL_SEC_CONN_NUM_COMP, SMP_MODEL_SEC_CONN_PASSKEY_DISP,
259 SMP_MODEL_SEC_CONN_JUSTWORKS, SMP_MODEL_SEC_CONN_NUM_COMP},
260
261 /* keyboard only */
262 {SMP_MODEL_SEC_CONN_PASSKEY_ENT, SMP_MODEL_SEC_CONN_PASSKEY_ENT, SMP_MODEL_SEC_CONN_PASSKEY_ENT,
263 SMP_MODEL_SEC_CONN_JUSTWORKS, SMP_MODEL_SEC_CONN_PASSKEY_ENT},
264
265 /* No Input No Output */
266 {SMP_MODEL_SEC_CONN_JUSTWORKS, SMP_MODEL_SEC_CONN_JUSTWORKS, SMP_MODEL_SEC_CONN_JUSTWORKS,
267 SMP_MODEL_SEC_CONN_JUSTWORKS, SMP_MODEL_SEC_CONN_JUSTWORKS},
268
269 /* keyboard display */
270 {SMP_MODEL_SEC_CONN_PASSKEY_ENT, SMP_MODEL_SEC_CONN_NUM_COMP, SMP_MODEL_SEC_CONN_PASSKEY_DISP,
271 SMP_MODEL_SEC_CONN_JUSTWORKS, SMP_MODEL_SEC_CONN_NUM_COMP}}
272};
273
274static tSMP_ASSO_MODEL smp_select_legacy_association_model(tSMP_CB *p_cb);
275static tSMP_ASSO_MODEL smp_select_association_model_secure_connections(tSMP_CB *p_cb);
276
The Android Open Source Project5738f832012-12-12 16:00:35 -0800277/*******************************************************************************
278**
279** Function smp_send_msg_to_L2CAP
280**
281** Description Send message to L2CAP.
282**
283*******************************************************************************/
Marie Janssend19e0782016-07-15 12:48:27 -0700284bool smp_send_msg_to_L2CAP(BD_ADDR rem_bda, BT_HDR *p_toL2CAP)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800285{
Marie Janssend19e0782016-07-15 12:48:27 -0700286 uint16_t l2cap_ret;
287 uint16_t fixed_cid = L2CAP_SMP_CID;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800288
Satya Calloji444a8da2015-03-06 10:38:22 -0800289 if (smp_cb.smp_over_br)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800290 {
Satya Calloji444a8da2015-03-06 10:38:22 -0800291 fixed_cid = L2CAP_SMP_BR_CID;
292 }
293
Marie Janssend19e0782016-07-15 12:48:27 -0700294 SMP_TRACE_EVENT("%s", __func__);
Satya Calloji444a8da2015-03-06 10:38:22 -0800295 smp_cb.total_tx_unacked += 1;
296
297 if ((l2cap_ret = L2CA_SendFixedChnlData (fixed_cid, rem_bda, p_toL2CAP)) == L2CAP_DW_FAILED)
298 {
299 smp_cb.total_tx_unacked -= 1;
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700300 SMP_TRACE_ERROR("SMP failed to pass msg:0x%0x to L2CAP",
Marie Janssend19e0782016-07-15 12:48:27 -0700301 *((uint8_t *)(p_toL2CAP + 1) + p_toL2CAP->offset));
302 return false;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800303 }
304 else
Marie Janssend19e0782016-07-15 12:48:27 -0700305 return true;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800306}
Satya Calloji444a8da2015-03-06 10:38:22 -0800307
The Android Open Source Project5738f832012-12-12 16:00:35 -0800308/*******************************************************************************
309**
310** Function smp_send_cmd
311**
312** Description send a SMP command on L2CAP channel.
313**
314*******************************************************************************/
Marie Janssend19e0782016-07-15 12:48:27 -0700315bool smp_send_cmd(uint8_t cmd_code, tSMP_CB *p_cb)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800316{
317 BT_HDR *p_buf;
Marie Janssend19e0782016-07-15 12:48:27 -0700318 bool sent = false;
319 uint8_t failure = SMP_PAIR_INTERNAL_ERR;
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700320 SMP_TRACE_EVENT("smp_send_cmd on l2cap cmd_code=0x%x", cmd_code);
Satya Calloji444a8da2015-03-06 10:38:22 -0800321 if ( cmd_code <= (SMP_OPCODE_MAX + 1 /* for SMP_OPCODE_PAIR_COMMITM */) &&
The Android Open Source Project5738f832012-12-12 16:00:35 -0800322 smp_cmd_build_act[cmd_code] != NULL)
323 {
324 p_buf = (*smp_cmd_build_act[cmd_code])(cmd_code, p_cb);
325
326 if (p_buf != NULL &&
327 smp_send_msg_to_L2CAP(p_cb->pairing_bda, p_buf))
328 {
Marie Janssend19e0782016-07-15 12:48:27 -0700329 sent = true;
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800330 alarm_set_on_queue(p_cb->smp_rsp_timer_ent,
331 SMP_WAIT_FOR_RSP_TIMEOUT_MS, smp_rsp_timeout,
332 NULL, btu_general_alarm_queue);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800333 }
334 }
335
336 if (!sent)
337 {
Satya Calloji444a8da2015-03-06 10:38:22 -0800338 if (p_cb->smp_over_br)
339 {
340 smp_br_state_machine_event(p_cb, SMP_BR_AUTH_CMPL_EVT, &failure);
341 }
342 else
343 {
344 smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &failure);
345 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800346 }
347 return sent;
348}
349
The Android Open Source Project5738f832012-12-12 16:00:35 -0800350/*******************************************************************************
351**
352** Function smp_rsp_timeout
353**
354** Description Called when SMP wait for SMP command response timer expires
355**
356** Returns void
357**
358*******************************************************************************/
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800359void smp_rsp_timeout(UNUSED_ATTR void *data)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800360{
361 tSMP_CB *p_cb = &smp_cb;
Marie Janssend19e0782016-07-15 12:48:27 -0700362 uint8_t failure = SMP_RSP_TIMEOUT;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800363
Marie Janssend19e0782016-07-15 12:48:27 -0700364 SMP_TRACE_EVENT("%s state:%d br_state:%d", __func__, p_cb->state, p_cb->br_state);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800365
Satya Calloji444a8da2015-03-06 10:38:22 -0800366 if (p_cb->smp_over_br)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800367 {
Satya Calloji444a8da2015-03-06 10:38:22 -0800368 smp_br_state_machine_event(p_cb, SMP_BR_AUTH_CMPL_EVT, &failure);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800369 }
370 else
371 {
372 smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &failure);
373 }
374}
375
376/*******************************************************************************
377**
Jacky Cheung373d9282016-05-17 13:42:43 -0700378** Function smp_delayed_auth_complete_timeout
379**
380** Description Called when no pairing failed command received within timeout
381** period.
382**
383** Returns void
384**
385*******************************************************************************/
386void smp_delayed_auth_complete_timeout(UNUSED_ATTR void *data)
387{
388 /*
389 * Waited for potential pair failure. Send SMP_AUTH_CMPL_EVT if
390 * the state is still in bond pending.
391 */
392 if (smp_get_state() == SMP_STATE_BOND_PENDING)
393 {
Marie Janssend19e0782016-07-15 12:48:27 -0700394 uint8_t reason = SMP_SUCCESS;
Jacky Cheung373d9282016-05-17 13:42:43 -0700395 SMP_TRACE_EVENT("%s sending delayed auth complete.", __func__);
396 smp_sm_event(&smp_cb, SMP_AUTH_CMPL_EVT, &reason);
397 }
398}
399
400/*******************************************************************************
401**
The Android Open Source Project5738f832012-12-12 16:00:35 -0800402** Function smp_build_pairing_req_cmd
403**
404** Description Build pairing request command.
405**
406*******************************************************************************/
Marie Janssend19e0782016-07-15 12:48:27 -0700407BT_HDR * smp_build_pairing_cmd(uint8_t cmd_code, tSMP_CB *p_cb)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800408{
Marie Janssend19e0782016-07-15 12:48:27 -0700409 uint8_t *p;
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800410 BT_HDR *p_buf = (BT_HDR *)osi_malloc(sizeof(BT_HDR) +
411 SMP_PAIRING_REQ_SIZE + L2CAP_MIN_OFFSET);
Satya Calloji444a8da2015-03-06 10:38:22 -0800412
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800413 SMP_TRACE_EVENT("%s", __func__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800414
Marie Janssend19e0782016-07-15 12:48:27 -0700415 p = (uint8_t *)(p_buf + 1) + L2CAP_MIN_OFFSET;
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800416 UINT8_TO_STREAM(p, cmd_code);
417 UINT8_TO_STREAM(p, p_cb->local_io_capability);
418 UINT8_TO_STREAM(p, p_cb->loc_oob_flag);
419 UINT8_TO_STREAM(p, p_cb->loc_auth_req);
420 UINT8_TO_STREAM(p, p_cb->loc_enc_size);
421 UINT8_TO_STREAM(p, p_cb->local_i_key);
422 UINT8_TO_STREAM(p, p_cb->local_r_key);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800423
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800424 p_buf->offset = L2CAP_MIN_OFFSET;
425 /* 1B ERR_RSP op code + 1B cmd_op_code + 2B handle + 1B status */
426 p_buf->len = SMP_PAIRING_REQ_SIZE;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800427
428 return p_buf;
429}
430
431/*******************************************************************************
432**
433** Function smp_build_confirm_cmd
434**
435** Description Build confirm request command.
436**
437*******************************************************************************/
Myles Watsond35a6482016-10-27 08:52:16 -0700438static BT_HDR * smp_build_confirm_cmd(UNUSED_ATTR uint8_t cmd_code, tSMP_CB *p_cb)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800439{
Marie Janssend19e0782016-07-15 12:48:27 -0700440 uint8_t *p;
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800441 BT_HDR *p_buf = (BT_HDR *)osi_malloc(sizeof(BT_HDR) +
442 SMP_CONFIRM_CMD_SIZE + L2CAP_MIN_OFFSET);
443
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800444 SMP_TRACE_EVENT("%s", __func__);
Mike J. Chen5cd8bff2014-01-31 18:16:59 -0800445
Marie Janssend19e0782016-07-15 12:48:27 -0700446 p = (uint8_t *)(p_buf + 1) + L2CAP_MIN_OFFSET;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800447
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800448 UINT8_TO_STREAM(p, SMP_OPCODE_CONFIRM);
449 ARRAY_TO_STREAM(p, p_cb->confirm, BT_OCTET16_LEN);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800450
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800451 p_buf->offset = L2CAP_MIN_OFFSET;
452 p_buf->len = SMP_CONFIRM_CMD_SIZE;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800453
454 return p_buf;
455}
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800456
The Android Open Source Project5738f832012-12-12 16:00:35 -0800457/*******************************************************************************
458**
459** Function smp_build_rand_cmd
460**
Satya Calloji444a8da2015-03-06 10:38:22 -0800461** Description Build Random command.
The Android Open Source Project5738f832012-12-12 16:00:35 -0800462**
463*******************************************************************************/
Myles Watsond35a6482016-10-27 08:52:16 -0700464static BT_HDR * smp_build_rand_cmd(UNUSED_ATTR uint8_t cmd_code, tSMP_CB *p_cb)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800465{
Marie Janssend19e0782016-07-15 12:48:27 -0700466 uint8_t *p;
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800467 BT_HDR *p_buf = (BT_HDR *)osi_malloc(sizeof(BT_HDR) +
468 SMP_RAND_CMD_SIZE + L2CAP_MIN_OFFSET);
469
Satya Calloji444a8da2015-03-06 10:38:22 -0800470 SMP_TRACE_EVENT("%s", __func__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800471
Marie Janssend19e0782016-07-15 12:48:27 -0700472 p = (uint8_t *)(p_buf + 1) + L2CAP_MIN_OFFSET;
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800473 UINT8_TO_STREAM(p, SMP_OPCODE_RAND);
474 ARRAY_TO_STREAM(p, p_cb->rand, BT_OCTET16_LEN);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800475
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800476 p_buf->offset = L2CAP_MIN_OFFSET;
477 p_buf->len = SMP_RAND_CMD_SIZE;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800478
479 return p_buf;
480}
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800481
The Android Open Source Project5738f832012-12-12 16:00:35 -0800482/*******************************************************************************
483**
484** Function smp_build_encrypt_info_cmd
485**
486** Description Build security information command.
487**
488*******************************************************************************/
Myles Watsond35a6482016-10-27 08:52:16 -0700489static BT_HDR * smp_build_encrypt_info_cmd(UNUSED_ATTR uint8_t cmd_code, tSMP_CB *p_cb)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800490{
Marie Janssend19e0782016-07-15 12:48:27 -0700491 uint8_t *p;
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800492 BT_HDR *p_buf = (BT_HDR *)osi_malloc(sizeof(BT_HDR) +
493 SMP_ENC_INFO_SIZE + L2CAP_MIN_OFFSET);
494
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800495 SMP_TRACE_EVENT("%s", __func__);
Mike J. Chen5cd8bff2014-01-31 18:16:59 -0800496
Marie Janssend19e0782016-07-15 12:48:27 -0700497 p = (uint8_t *)(p_buf + 1) + L2CAP_MIN_OFFSET;
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800498 UINT8_TO_STREAM(p, SMP_OPCODE_ENCRYPT_INFO);
499 ARRAY_TO_STREAM(p, p_cb->ltk, BT_OCTET16_LEN);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800500
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800501 p_buf->offset = L2CAP_MIN_OFFSET;
502 p_buf->len = SMP_ENC_INFO_SIZE;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800503
504 return p_buf;
505}
Satya Calloji444a8da2015-03-06 10:38:22 -0800506
The Android Open Source Project5738f832012-12-12 16:00:35 -0800507/*******************************************************************************
508**
509** Function smp_build_master_id_cmd
510**
511** Description Build security information command.
512**
513*******************************************************************************/
Myles Watsond35a6482016-10-27 08:52:16 -0700514static BT_HDR * smp_build_master_id_cmd(UNUSED_ATTR uint8_t cmd_code, tSMP_CB *p_cb)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800515{
Marie Janssend19e0782016-07-15 12:48:27 -0700516 uint8_t *p;
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800517 BT_HDR *p_buf = (BT_HDR *)osi_malloc(sizeof(BT_HDR) +
518 SMP_MASTER_ID_SIZE + L2CAP_MIN_OFFSET);
Mike J. Chen5cd8bff2014-01-31 18:16:59 -0800519
Satya Calloji444a8da2015-03-06 10:38:22 -0800520 SMP_TRACE_EVENT("%s", __func__);
521
Marie Janssend19e0782016-07-15 12:48:27 -0700522 p = (uint8_t *)(p_buf + 1) + L2CAP_MIN_OFFSET;
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800523 UINT8_TO_STREAM(p, SMP_OPCODE_MASTER_ID);
524 UINT16_TO_STREAM(p, p_cb->ediv);
525 ARRAY_TO_STREAM(p, p_cb->enc_rand, BT_OCTET8_LEN);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800526
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800527 p_buf->offset = L2CAP_MIN_OFFSET;
528 p_buf->len = SMP_MASTER_ID_SIZE;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800529
530 return p_buf;
531}
Satya Calloji444a8da2015-03-06 10:38:22 -0800532
The Android Open Source Project5738f832012-12-12 16:00:35 -0800533/*******************************************************************************
534**
535** Function smp_build_identity_info_cmd
536**
537** Description Build identity information command.
538**
539*******************************************************************************/
Myles Watsond35a6482016-10-27 08:52:16 -0700540static BT_HDR * smp_build_identity_info_cmd(UNUSED_ATTR uint8_t cmd_code,
541 UNUSED_ATTR tSMP_CB *p_cb)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800542{
Marie Janssend19e0782016-07-15 12:48:27 -0700543 uint8_t *p;
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800544 BT_OCTET16 irk;
545 BT_HDR *p_buf = (BT_HDR *)osi_malloc(sizeof(BT_HDR) +
546 SMP_ID_INFO_SIZE + L2CAP_MIN_OFFSET);
547
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800548 SMP_TRACE_EVENT("%s", __func__);
Mike J. Chen5cd8bff2014-01-31 18:16:59 -0800549
Marie Janssend19e0782016-07-15 12:48:27 -0700550 p = (uint8_t *)(p_buf + 1) + L2CAP_MIN_OFFSET;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800551
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800552 BTM_GetDeviceIDRoot(irk);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800553
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800554 UINT8_TO_STREAM(p, SMP_OPCODE_IDENTITY_INFO);
555 ARRAY_TO_STREAM(p, irk, BT_OCTET16_LEN);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800556
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800557 p_buf->offset = L2CAP_MIN_OFFSET;
558 p_buf->len = SMP_ID_INFO_SIZE;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800559
560 return p_buf;
561}
Satya Calloji444a8da2015-03-06 10:38:22 -0800562
The Android Open Source Project5738f832012-12-12 16:00:35 -0800563/*******************************************************************************
564**
565** Function smp_build_id_addr_cmd
566**
567** Description Build identity address information command.
568**
569*******************************************************************************/
Myles Watsond35a6482016-10-27 08:52:16 -0700570static BT_HDR * smp_build_id_addr_cmd(UNUSED_ATTR uint8_t cmd_code,
571 UNUSED_ATTR tSMP_CB *p_cb)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800572{
Marie Janssend19e0782016-07-15 12:48:27 -0700573 uint8_t *p;
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800574 BT_HDR *p_buf = (BT_HDR *)osi_malloc(sizeof(BT_HDR) +
575 SMP_ID_ADDR_SIZE + L2CAP_MIN_OFFSET);
Satya Calloji444a8da2015-03-06 10:38:22 -0800576
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800577 SMP_TRACE_EVENT("%s", __func__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800578
Marie Janssend19e0782016-07-15 12:48:27 -0700579 p = (uint8_t *)(p_buf + 1) + L2CAP_MIN_OFFSET;
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800580 UINT8_TO_STREAM(p, SMP_OPCODE_ID_ADDR);
581 UINT8_TO_STREAM(p, 0);
582 BDADDR_TO_STREAM(p, controller_get_interface()->get_address()->address);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800583
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800584 p_buf->offset = L2CAP_MIN_OFFSET;
585 p_buf->len = SMP_ID_ADDR_SIZE;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800586
587 return p_buf;
588}
589
590/*******************************************************************************
591**
592** Function smp_build_signing_info_cmd
593**
594** Description Build signing information command.
595**
596*******************************************************************************/
Myles Watsond35a6482016-10-27 08:52:16 -0700597static BT_HDR * smp_build_signing_info_cmd(UNUSED_ATTR uint8_t cmd_code, tSMP_CB *p_cb)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800598{
Marie Janssend19e0782016-07-15 12:48:27 -0700599 uint8_t *p;
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800600 BT_HDR *p_buf = (BT_HDR *)osi_malloc(sizeof(BT_HDR) +
601 SMP_SIGN_INFO_SIZE + L2CAP_MIN_OFFSET);
602
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800603 SMP_TRACE_EVENT("%s", __func__);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800604
Marie Janssend19e0782016-07-15 12:48:27 -0700605 p = (uint8_t *)(p_buf + 1) + L2CAP_MIN_OFFSET;
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800606 UINT8_TO_STREAM(p, SMP_OPCODE_SIGN_INFO);
607 ARRAY_TO_STREAM(p, p_cb->csrk, BT_OCTET16_LEN);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800608
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800609 p_buf->offset = L2CAP_MIN_OFFSET;
610 p_buf->len = SMP_SIGN_INFO_SIZE;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800611
612 return p_buf;
613}
Satya Calloji444a8da2015-03-06 10:38:22 -0800614
The Android Open Source Project5738f832012-12-12 16:00:35 -0800615/*******************************************************************************
616**
617** Function smp_build_pairing_fail
618**
619** Description Build Pairing Fail command.
620**
621*******************************************************************************/
Myles Watsond35a6482016-10-27 08:52:16 -0700622static BT_HDR * smp_build_pairing_fail(UNUSED_ATTR uint8_t cmd_code, tSMP_CB *p_cb)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800623{
Marie Janssend19e0782016-07-15 12:48:27 -0700624 uint8_t *p;
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800625 BT_HDR *p_buf = (BT_HDR *)osi_malloc(sizeof(BT_HDR) +
626 SMP_PAIR_FAIL_SIZE + L2CAP_MIN_OFFSET);
627
Satya Calloji444a8da2015-03-06 10:38:22 -0800628 SMP_TRACE_EVENT("%s", __func__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800629
Marie Janssend19e0782016-07-15 12:48:27 -0700630 p = (uint8_t *)(p_buf + 1) + L2CAP_MIN_OFFSET;
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800631 UINT8_TO_STREAM(p, SMP_OPCODE_PAIRING_FAILED);
632 UINT8_TO_STREAM(p, p_cb->failure);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800633
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800634 p_buf->offset = L2CAP_MIN_OFFSET;
635 p_buf->len = SMP_PAIR_FAIL_SIZE;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800636
637 return p_buf;
638}
Satya Calloji444a8da2015-03-06 10:38:22 -0800639
The Android Open Source Project5738f832012-12-12 16:00:35 -0800640/*******************************************************************************
641**
642** Function smp_build_security_request
643**
644** Description Build security request command.
645**
646*******************************************************************************/
Myles Watsond35a6482016-10-27 08:52:16 -0700647static BT_HDR *smp_build_security_request(UNUSED_ATTR uint8_t cmd_code, tSMP_CB *p_cb)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800648{
Marie Janssend19e0782016-07-15 12:48:27 -0700649 uint8_t *p;
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800650 BT_HDR *p_buf = (BT_HDR *)osi_malloc(sizeof(BT_HDR) +
651 2 + L2CAP_MIN_OFFSET);
652
Satya Calloji444a8da2015-03-06 10:38:22 -0800653 SMP_TRACE_EVENT("%s", __func__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800654
Marie Janssend19e0782016-07-15 12:48:27 -0700655 p = (uint8_t *)(p_buf + 1) + L2CAP_MIN_OFFSET;
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800656 UINT8_TO_STREAM(p, SMP_OPCODE_SEC_REQ);
657 UINT8_TO_STREAM(p, p_cb->loc_auth_req);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800658
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800659 p_buf->offset = L2CAP_MIN_OFFSET;
660 p_buf->len = SMP_SECURITY_REQUEST_SIZE;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800661
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800662 SMP_TRACE_EVENT("opcode=%d auth_req=0x%x",SMP_OPCODE_SEC_REQ, p_cb->loc_auth_req );
The Android Open Source Project5738f832012-12-12 16:00:35 -0800663
664 return p_buf;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800665}
666
667/*******************************************************************************
668**
Satya Calloji444a8da2015-03-06 10:38:22 -0800669** Function smp_build_pair_public_key_cmd
670**
671** Description Build pairing public key command.
672**
673*******************************************************************************/
Myles Watsond35a6482016-10-27 08:52:16 -0700674static BT_HDR *smp_build_pair_public_key_cmd(UNUSED_ATTR uint8_t cmd_code, tSMP_CB *p_cb)
Satya Calloji444a8da2015-03-06 10:38:22 -0800675{
Marie Janssend19e0782016-07-15 12:48:27 -0700676 uint8_t *p;
677 uint8_t publ_key[2*BT_OCTET32_LEN];
678 uint8_t *p_publ_key = publ_key;
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800679 BT_HDR *p_buf = (BT_HDR *)osi_malloc(sizeof(BT_HDR) +
680 SMP_PAIR_PUBL_KEY_SIZE + L2CAP_MIN_OFFSET);
Satya Calloji444a8da2015-03-06 10:38:22 -0800681
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800682 SMP_TRACE_EVENT("%s", __func__);
Satya Calloji444a8da2015-03-06 10:38:22 -0800683
684 memcpy(p_publ_key, p_cb->loc_publ_key.x, BT_OCTET32_LEN);
685 memcpy(p_publ_key + BT_OCTET32_LEN, p_cb->loc_publ_key.y, BT_OCTET32_LEN);
686
Marie Janssend19e0782016-07-15 12:48:27 -0700687 p = (uint8_t *)(p_buf + 1) + L2CAP_MIN_OFFSET;
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800688 UINT8_TO_STREAM(p, SMP_OPCODE_PAIR_PUBLIC_KEY);
689 ARRAY_TO_STREAM(p, p_publ_key, 2*BT_OCTET32_LEN);
Satya Calloji444a8da2015-03-06 10:38:22 -0800690
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800691 p_buf->offset = L2CAP_MIN_OFFSET;
692 p_buf->len = SMP_PAIR_PUBL_KEY_SIZE;
Satya Calloji444a8da2015-03-06 10:38:22 -0800693
694 return p_buf;
695}
696
697/*******************************************************************************
698**
699** Function smp_build_pairing_commitment_cmd
700**
701** Description Build pairing commitment command.
702**
703*******************************************************************************/
Myles Watsond35a6482016-10-27 08:52:16 -0700704static BT_HDR *smp_build_pairing_commitment_cmd(UNUSED_ATTR uint8_t cmd_code, tSMP_CB *p_cb)
Satya Calloji444a8da2015-03-06 10:38:22 -0800705{
Marie Janssend19e0782016-07-15 12:48:27 -0700706 uint8_t *p;
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800707 BT_HDR *p_buf = (BT_HDR *)osi_malloc(sizeof(BT_HDR) +
708 SMP_PAIR_COMMITM_SIZE + L2CAP_MIN_OFFSET);
709
Satya Calloji444a8da2015-03-06 10:38:22 -0800710 SMP_TRACE_EVENT("%s", __func__);
Satya Calloji444a8da2015-03-06 10:38:22 -0800711
Marie Janssend19e0782016-07-15 12:48:27 -0700712 p = (uint8_t *)(p_buf + 1) + L2CAP_MIN_OFFSET;
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800713 UINT8_TO_STREAM(p, SMP_OPCODE_CONFIRM);
714 ARRAY_TO_STREAM(p, p_cb->commitment, BT_OCTET16_LEN);
Satya Calloji444a8da2015-03-06 10:38:22 -0800715
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800716 p_buf->offset = L2CAP_MIN_OFFSET;
717 p_buf->len = SMP_PAIR_COMMITM_SIZE;
Satya Calloji444a8da2015-03-06 10:38:22 -0800718
719 return p_buf;
720}
721
722/*******************************************************************************
723**
724** Function smp_build_pair_dhkey_check_cmd
725**
726** Description Build pairing DHKey check command.
727**
728*******************************************************************************/
Myles Watsond35a6482016-10-27 08:52:16 -0700729static BT_HDR *smp_build_pair_dhkey_check_cmd(UNUSED_ATTR uint8_t cmd_code, tSMP_CB *p_cb)
Satya Calloji444a8da2015-03-06 10:38:22 -0800730{
Marie Janssend19e0782016-07-15 12:48:27 -0700731 uint8_t *p;
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800732 BT_HDR *p_buf = (BT_HDR *)osi_malloc(sizeof(BT_HDR) +
733 SMP_PAIR_DHKEY_CHECK_SIZE + L2CAP_MIN_OFFSET);
734
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800735 SMP_TRACE_EVENT("%s", __func__);
Satya Calloji444a8da2015-03-06 10:38:22 -0800736
Marie Janssend19e0782016-07-15 12:48:27 -0700737 p = (uint8_t *)(p_buf + 1) + L2CAP_MIN_OFFSET;
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800738 UINT8_TO_STREAM(p, SMP_OPCODE_PAIR_DHKEY_CHECK);
739 ARRAY_TO_STREAM(p, p_cb->dhkey_check, BT_OCTET16_LEN);
Satya Calloji444a8da2015-03-06 10:38:22 -0800740
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800741 p_buf->offset = L2CAP_MIN_OFFSET;
742 p_buf->len = SMP_PAIR_DHKEY_CHECK_SIZE;
Satya Calloji444a8da2015-03-06 10:38:22 -0800743
744 return p_buf;
745}
746
747/*******************************************************************************
748**
749** Function smp_build_pairing_keypress_notification_cmd
750**
751** Description Build keypress notification command.
752**
753*******************************************************************************/
Myles Watsond35a6482016-10-27 08:52:16 -0700754static BT_HDR * smp_build_pairing_keypress_notification_cmd(UNUSED_ATTR uint8_t cmd_code, tSMP_CB *p_cb)
Satya Calloji444a8da2015-03-06 10:38:22 -0800755{
Marie Janssend19e0782016-07-15 12:48:27 -0700756 uint8_t *p;
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800757 BT_HDR *p_buf = (BT_HDR *)osi_malloc(sizeof(BT_HDR) +
758 SMP_PAIR_KEYPR_NOTIF_SIZE + L2CAP_MIN_OFFSET);
759
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800760 SMP_TRACE_EVENT("%s", __func__);
Satya Calloji444a8da2015-03-06 10:38:22 -0800761
Marie Janssend19e0782016-07-15 12:48:27 -0700762 p = (uint8_t *)(p_buf + 1) + L2CAP_MIN_OFFSET;
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800763 UINT8_TO_STREAM(p, SMP_OPCODE_PAIR_KEYPR_NOTIF);
764 UINT8_TO_STREAM(p, p_cb->local_keypress_notification);
Satya Calloji444a8da2015-03-06 10:38:22 -0800765
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800766 p_buf->offset = L2CAP_MIN_OFFSET;
767 p_buf->len = SMP_PAIR_KEYPR_NOTIF_SIZE;
Satya Calloji444a8da2015-03-06 10:38:22 -0800768
769 return p_buf;
770}
771
772/*******************************************************************************
773**
The Android Open Source Project5738f832012-12-12 16:00:35 -0800774** Function smp_convert_string_to_tk
775**
776** Description This function is called to convert a 6 to 16 digits numeric
777** character string into SMP TK.
778**
779**
780** Returns void
781**
782*******************************************************************************/
Marie Janssend19e0782016-07-15 12:48:27 -0700783void smp_convert_string_to_tk(BT_OCTET16 tk, uint32_t passkey)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800784{
Marie Janssend19e0782016-07-15 12:48:27 -0700785 uint8_t *p = tk;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800786 tSMP_KEY key;
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700787 SMP_TRACE_EVENT("smp_convert_string_to_tk");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800788 UINT32_TO_STREAM(p, passkey);
789
790 key.key_type = SMP_KEY_TYPE_TK;
791 key.p_data = tk;
792
793 smp_sm_event(&smp_cb, SMP_KEY_READY_EVT, &key);
794}
795
796/*******************************************************************************
797**
798** Function smp_mask_enc_key
799**
800** Description This function is called to mask off the encryption key based
801** on the maximum encryption key size.
802**
803**
804** Returns void
805**
806*******************************************************************************/
Marie Janssend19e0782016-07-15 12:48:27 -0700807void smp_mask_enc_key(uint8_t loc_enc_size, uint8_t * p_data)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800808{
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700809 SMP_TRACE_EVENT("smp_mask_enc_key");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800810 if (loc_enc_size < BT_OCTET16_LEN)
811 {
812 for (; loc_enc_size < BT_OCTET16_LEN; loc_enc_size ++)
813 * (p_data + loc_enc_size) = 0;
814 }
815 return;
816}
Satya Calloji444a8da2015-03-06 10:38:22 -0800817
The Android Open Source Project5738f832012-12-12 16:00:35 -0800818/*******************************************************************************
819**
820** Function smp_xor_128
821**
822** Description utility function to do an biteise exclusive-OR of two bit
823** strings of the length of BT_OCTET16_LEN.
824**
825** Returns void
826**
827*******************************************************************************/
828void smp_xor_128(BT_OCTET16 a, BT_OCTET16 b)
829{
Marie Janssend19e0782016-07-15 12:48:27 -0700830 uint8_t i, *aa = a, *bb = b;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800831
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700832 SMP_TRACE_EVENT("smp_xor_128");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800833 for (i = 0; i < BT_OCTET16_LEN; i++)
834 {
835 aa[i] = aa[i] ^ bb[i];
836 }
837}
838
The Android Open Source Project5738f832012-12-12 16:00:35 -0800839/*******************************************************************************
840**
841** Function smp_cb_cleanup
842**
843** Description Clean up SMP control block
844**
845** Returns void
846**
847*******************************************************************************/
848void smp_cb_cleanup(tSMP_CB *p_cb)
849{
850 tSMP_CALLBACK *p_callback = p_cb->p_callback;
Marie Janssend19e0782016-07-15 12:48:27 -0700851 uint8_t trace_level = p_cb->trace_level;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800852
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700853 SMP_TRACE_EVENT("smp_cb_cleanup");
Satya Calloji444a8da2015-03-06 10:38:22 -0800854
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800855 alarm_free(p_cb->smp_rsp_timer_ent);
Jacky Cheung373d9282016-05-17 13:42:43 -0700856 alarm_free(p_cb->delayed_auth_timer_ent);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800857 memset(p_cb, 0, sizeof(tSMP_CB));
858 p_cb->p_callback = p_callback;
859 p_cb->trace_level = trace_level;
Pavlin Radoslavov98ae9b22016-01-22 13:13:45 -0800860 p_cb->smp_rsp_timer_ent = alarm_new("smp.smp_rsp_timer_ent");
Jacky Cheung373d9282016-05-17 13:42:43 -0700861 p_cb->delayed_auth_timer_ent = alarm_new("smp.delayed_auth_timer_ent");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800862}
Satya Calloji444a8da2015-03-06 10:38:22 -0800863
864/*******************************************************************************
865**
866** Function smp_remove_fixed_channel
867**
868** Description This function is called to remove the fixed channel
869**
870** Returns void
871**
872*******************************************************************************/
873void smp_remove_fixed_channel(tSMP_CB *p_cb)
874{
875 SMP_TRACE_DEBUG("%s", __func__);
876
877 if (p_cb->smp_over_br)
878 L2CA_RemoveFixedChnl (L2CAP_SMP_BR_CID, p_cb->pairing_bda);
879 else
880 L2CA_RemoveFixedChnl (L2CAP_SMP_CID, p_cb->pairing_bda);
881}
882
The Android Open Source Project5738f832012-12-12 16:00:35 -0800883/*******************************************************************************
884**
885** Function smp_reset_control_value
886**
887** Description This function is called to reset the control block value when
888** pairing procedure finished.
889**
890**
891** Returns void
892**
893*******************************************************************************/
894void smp_reset_control_value(tSMP_CB *p_cb)
895{
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800896 SMP_TRACE_EVENT("%s", __func__);
897
898 alarm_cancel(p_cb->smp_rsp_timer_ent);
Satya Calloji444a8da2015-03-06 10:38:22 -0800899 p_cb->flags = 0;
900 /* set the link idle timer to drop the link when pairing is done
901 usually service discovery will follow authentication complete, to avoid
902 racing condition for a link down/up, set link idle timer to be
903 SMP_LINK_TOUT_MIN to guarantee SMP key exchange */
904 L2CA_SetIdleTimeoutByBdAddr(p_cb->pairing_bda, SMP_LINK_TOUT_MIN, BT_TRANSPORT_LE);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800905
The Android Open Source Project5738f832012-12-12 16:00:35 -0800906 /* We can tell L2CAP to remove the fixed channel (if it has one) */
Satya Calloji444a8da2015-03-06 10:38:22 -0800907 smp_remove_fixed_channel(p_cb);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800908 smp_cb_cleanup(p_cb);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800909}
Satya Calloji444a8da2015-03-06 10:38:22 -0800910
The Android Open Source Project5738f832012-12-12 16:00:35 -0800911/*******************************************************************************
912**
913** Function smp_proc_pairing_cmpl
914**
915** Description This function is called to process pairing complete
916**
917**
918** Returns void
919**
920*******************************************************************************/
921void smp_proc_pairing_cmpl(tSMP_CB *p_cb)
922{
923 tSMP_EVT_DATA evt_data = {0};
Satya Calloji444a8da2015-03-06 10:38:22 -0800924 tSMP_CALLBACK *p_callback = p_cb->p_callback;
925 BD_ADDR pairing_bda;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800926
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700927 SMP_TRACE_DEBUG ("smp_proc_pairing_cmpl ");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800928
929 evt_data.cmplt.reason = p_cb->status;
Chaojing Sune2805532015-04-22 13:40:21 -0700930 evt_data.cmplt.smp_over_br = p_cb->smp_over_br;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800931
932 if (p_cb->status == SMP_SUCCESS)
933 evt_data.cmplt.sec_level = p_cb->sec_level;
934
Marie Janssend19e0782016-07-15 12:48:27 -0700935 evt_data.cmplt.is_pair_cancel = false;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800936
937 if (p_cb->is_pair_cancel)
Marie Janssend19e0782016-07-15 12:48:27 -0700938 evt_data.cmplt.is_pair_cancel = true;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800939
940
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700941 SMP_TRACE_DEBUG ("send SMP_COMPLT_EVT reason=0x%0x sec_level=0x%0x",
The Android Open Source Project5738f832012-12-12 16:00:35 -0800942 evt_data.cmplt.reason,
943 evt_data.cmplt.sec_level );
The Android Open Source Project5738f832012-12-12 16:00:35 -0800944
Satya Calloji444a8da2015-03-06 10:38:22 -0800945 memcpy (pairing_bda, p_cb->pairing_bda, BD_ADDR_LEN);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800946
947 smp_reset_control_value(p_cb);
Satya Calloji444a8da2015-03-06 10:38:22 -0800948
949 if (p_callback)
950 (*p_callback) (SMP_COMPLT_EVT, pairing_bda, &evt_data);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800951}
952
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700953/*******************************************************************************
954**
Satya Calloji444a8da2015-03-06 10:38:22 -0800955** Function smp_command_has_invalid_parameters
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700956**
Satya Calloji444a8da2015-03-06 10:38:22 -0800957** Description Checks if the received SMP command has invalid parameters i.e.
958** if the command length is valid and the command parameters are
959** inside specified range.
Marie Janssend19e0782016-07-15 12:48:27 -0700960** It returns true if the command has invalid parameters.
Satya Calloji444a8da2015-03-06 10:38:22 -0800961**
Marie Janssend19e0782016-07-15 12:48:27 -0700962** Returns true if the command has invalid parameters, false otherwise.
Satya Calloji444a8da2015-03-06 10:38:22 -0800963**
964*******************************************************************************/
Marie Janssend19e0782016-07-15 12:48:27 -0700965bool smp_command_has_invalid_parameters(tSMP_CB *p_cb)
Satya Calloji444a8da2015-03-06 10:38:22 -0800966{
Marie Janssend19e0782016-07-15 12:48:27 -0700967 uint8_t cmd_code = p_cb->rcvd_cmd_code;
Satya Calloji444a8da2015-03-06 10:38:22 -0800968
969 SMP_TRACE_DEBUG("%s for cmd code 0x%02x", __func__, cmd_code);
970
971 if ((cmd_code > (SMP_OPCODE_MAX + 1 /* for SMP_OPCODE_PAIR_COMMITM */)) ||
972 (cmd_code < SMP_OPCODE_MIN))
973 {
974 SMP_TRACE_WARNING("Somehow received command with the RESERVED code 0x%02x", cmd_code);
Marie Janssend19e0782016-07-15 12:48:27 -0700975 return true;
Satya Calloji444a8da2015-03-06 10:38:22 -0800976 }
977
978 if (!(*smp_cmd_len_is_valid[cmd_code])(p_cb))
Marie Janssend19e0782016-07-15 12:48:27 -0700979 return true;
Satya Calloji444a8da2015-03-06 10:38:22 -0800980
981 if (!(*smp_cmd_param_ranges_are_valid[cmd_code])(p_cb))
Marie Janssend19e0782016-07-15 12:48:27 -0700982 return true;
Satya Calloji444a8da2015-03-06 10:38:22 -0800983
Marie Janssend19e0782016-07-15 12:48:27 -0700984 return false;
Satya Calloji444a8da2015-03-06 10:38:22 -0800985}
986
987/*******************************************************************************
988**
989** Function smp_command_has_valid_fixed_length
990**
991** Description Checks if the received command size is equal to the size
992** according to specs.
993**
Marie Janssend19e0782016-07-15 12:48:27 -0700994** Returns true if the command size is as expected, false otherwise.
Satya Calloji444a8da2015-03-06 10:38:22 -0800995**
996** Note The command is expected to have fixed length.
997*******************************************************************************/
Marie Janssend19e0782016-07-15 12:48:27 -0700998bool smp_command_has_valid_fixed_length(tSMP_CB *p_cb)
Satya Calloji444a8da2015-03-06 10:38:22 -0800999{
Marie Janssend19e0782016-07-15 12:48:27 -07001000 uint8_t cmd_code = p_cb->rcvd_cmd_code;
Satya Calloji444a8da2015-03-06 10:38:22 -08001001
1002 SMP_TRACE_DEBUG("%s for cmd code 0x%02x", __func__, cmd_code);
1003
1004 if (p_cb->rcvd_cmd_len != smp_cmd_size_per_spec[cmd_code])
1005 {
1006 SMP_TRACE_WARNING("Rcvd from the peer cmd 0x%02x with invalid length\
1007 0x%02x (per spec the length is 0x%02x).",
1008 cmd_code, p_cb->rcvd_cmd_len, smp_cmd_size_per_spec[cmd_code]);
Marie Janssend19e0782016-07-15 12:48:27 -07001009 return false;
Satya Calloji444a8da2015-03-06 10:38:22 -08001010 }
1011
Marie Janssend19e0782016-07-15 12:48:27 -07001012 return true;
Satya Calloji444a8da2015-03-06 10:38:22 -08001013}
1014
1015/*******************************************************************************
1016**
1017** Function smp_pairing_request_response_parameters_are_valid
1018**
1019** Description Validates parameter ranges in the received SMP command
1020** pairing request or pairing response.
1021** The parameters to validate:
1022** IO capability,
1023** OOB data flag,
1024** Bonding_flags in AuthReq
1025** Maximum encryption key size.
Marie Janssend19e0782016-07-15 12:48:27 -07001026** Returns false if at least one of these parameters is out of range.
Satya Calloji444a8da2015-03-06 10:38:22 -08001027**
1028*******************************************************************************/
Marie Janssend19e0782016-07-15 12:48:27 -07001029bool smp_pairing_request_response_parameters_are_valid(tSMP_CB *p_cb)
Satya Calloji444a8da2015-03-06 10:38:22 -08001030{
Marie Janssend19e0782016-07-15 12:48:27 -07001031 uint8_t io_caps = p_cb->peer_io_caps;
1032 uint8_t oob_flag = p_cb->peer_oob_flag;
1033 uint8_t bond_flag = p_cb->peer_auth_req & 0x03; //0x03 is gen bond with appropriate mask
1034 uint8_t enc_size = p_cb->peer_enc_size;
Satya Calloji444a8da2015-03-06 10:38:22 -08001035
1036 SMP_TRACE_DEBUG("%s for cmd code 0x%02x", __func__, p_cb->rcvd_cmd_code);
1037
1038 if (io_caps >= BTM_IO_CAP_MAX)
1039 {
1040 SMP_TRACE_WARNING("Rcvd from the peer cmd 0x%02x with IO Capabilty \
1041 value (0x%02x) out of range).",
1042 p_cb->rcvd_cmd_code, io_caps);
Marie Janssend19e0782016-07-15 12:48:27 -07001043 return false;
Satya Calloji444a8da2015-03-06 10:38:22 -08001044 }
1045
1046 if (!((oob_flag == SMP_OOB_NONE) || (oob_flag == SMP_OOB_PRESENT)))
1047 {
1048 SMP_TRACE_WARNING("Rcvd from the peer cmd 0x%02x with OOB data flag value \
1049 (0x%02x) out of range).",
1050 p_cb->rcvd_cmd_code, oob_flag);
Marie Janssend19e0782016-07-15 12:48:27 -07001051 return false;
Satya Calloji444a8da2015-03-06 10:38:22 -08001052 }
1053
1054 if (!((bond_flag == SMP_AUTH_NO_BOND) || (bond_flag == SMP_AUTH_BOND)))
1055 {
1056 SMP_TRACE_WARNING("Rcvd from the peer cmd 0x%02x with Bonding_Flags value (0x%02x)\
1057 out of range).",
1058 p_cb->rcvd_cmd_code, bond_flag);
Marie Janssend19e0782016-07-15 12:48:27 -07001059 return false;
Satya Calloji444a8da2015-03-06 10:38:22 -08001060 }
1061
1062 if ((enc_size < SMP_ENCR_KEY_SIZE_MIN) || (enc_size > SMP_ENCR_KEY_SIZE_MAX))
1063 {
1064 SMP_TRACE_WARNING("Rcvd from the peer cmd 0x%02x with Maximum Encryption \
1065 Key value (0x%02x) out of range).",
1066 p_cb->rcvd_cmd_code, enc_size);
Marie Janssend19e0782016-07-15 12:48:27 -07001067 return false;
Satya Calloji444a8da2015-03-06 10:38:22 -08001068 }
1069
Marie Janssend19e0782016-07-15 12:48:27 -07001070 return true;
Satya Calloji444a8da2015-03-06 10:38:22 -08001071}
1072
1073/*******************************************************************************
1074**
1075** Function smp_pairing_keypress_notification_is_valid
1076**
1077** Description Validates Notification Type parameter range in the received SMP command
1078** pairing keypress notification.
Marie Janssend19e0782016-07-15 12:48:27 -07001079** Returns false if this parameter is out of range.
Satya Calloji444a8da2015-03-06 10:38:22 -08001080**
1081*******************************************************************************/
Marie Janssend19e0782016-07-15 12:48:27 -07001082bool smp_pairing_keypress_notification_is_valid(tSMP_CB *p_cb)
Satya Calloji444a8da2015-03-06 10:38:22 -08001083{
1084 tBTM_SP_KEY_TYPE keypress_notification = p_cb->peer_keypress_notification;
1085
1086 SMP_TRACE_DEBUG("%s for cmd code 0x%02x", __func__, p_cb->rcvd_cmd_code);
1087
1088 if (keypress_notification >= BTM_SP_KEY_OUT_OF_RANGE)
1089 {
1090 SMP_TRACE_WARNING("Rcvd from the peer cmd 0x%02x with Pairing Keypress \
1091 Notification value (0x%02x) out of range).",
1092 p_cb->rcvd_cmd_code, keypress_notification);
Marie Janssend19e0782016-07-15 12:48:27 -07001093 return false;
Satya Calloji444a8da2015-03-06 10:38:22 -08001094 }
1095
Marie Janssend19e0782016-07-15 12:48:27 -07001096 return true;
Satya Calloji444a8da2015-03-06 10:38:22 -08001097}
1098
1099/*******************************************************************************
1100**
1101** Function smp_parameter_unconditionally_valid
1102**
Marie Janssend19e0782016-07-15 12:48:27 -07001103** Description Always returns true.
Satya Calloji444a8da2015-03-06 10:38:22 -08001104**
1105*******************************************************************************/
Marie Janssend19e0782016-07-15 12:48:27 -07001106bool smp_parameter_unconditionally_valid(UNUSED_ATTR tSMP_CB *p_cb)
Satya Calloji444a8da2015-03-06 10:38:22 -08001107{
Marie Janssend19e0782016-07-15 12:48:27 -07001108 return true;
Satya Calloji444a8da2015-03-06 10:38:22 -08001109}
1110
1111/*******************************************************************************
1112**
1113** Function smp_parameter_unconditionally_invalid
1114**
Marie Janssend19e0782016-07-15 12:48:27 -07001115** Description Always returns false.
Satya Calloji444a8da2015-03-06 10:38:22 -08001116**
1117*******************************************************************************/
Marie Janssend19e0782016-07-15 12:48:27 -07001118bool smp_parameter_unconditionally_invalid(UNUSED_ATTR tSMP_CB *p_cb)
Satya Calloji444a8da2015-03-06 10:38:22 -08001119{
Marie Janssend19e0782016-07-15 12:48:27 -07001120 return false;
Satya Calloji444a8da2015-03-06 10:38:22 -08001121}
1122
1123/*******************************************************************************
1124**
1125** Function smp_reject_unexpected_pairing_command
1126**
1127** Description send pairing failure to an unexpected pairing command during
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -07001128** an active pairing process.
1129**
1130** Returns void
1131**
1132*******************************************************************************/
Satya Calloji444a8da2015-03-06 10:38:22 -08001133void smp_reject_unexpected_pairing_command(BD_ADDR bd_addr)
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -07001134{
Marie Janssend19e0782016-07-15 12:48:27 -07001135 uint8_t *p;
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08001136 BT_HDR *p_buf = (BT_HDR *)osi_malloc(sizeof(BT_HDR) +
1137 SMP_PAIR_FAIL_SIZE + L2CAP_MIN_OFFSET);
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -07001138
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08001139 SMP_TRACE_DEBUG("%s", __func__);
Satya Calloji444a8da2015-03-06 10:38:22 -08001140
Marie Janssend19e0782016-07-15 12:48:27 -07001141 p = (uint8_t *)(p_buf + 1) + L2CAP_MIN_OFFSET;
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08001142 UINT8_TO_STREAM(p, SMP_OPCODE_PAIRING_FAILED);
1143 UINT8_TO_STREAM(p, SMP_PAIR_NOT_SUPPORT);
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -07001144
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08001145 p_buf->offset = L2CAP_MIN_OFFSET;
1146 p_buf->len = SMP_PAIR_FAIL_SIZE;
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -07001147
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -08001148 smp_send_msg_to_L2CAP(bd_addr, p_buf);
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -07001149}
1150
The Android Open Source Project5738f832012-12-12 16:00:35 -08001151/*******************************************************************************
Satya Calloji444a8da2015-03-06 10:38:22 -08001152** Function smp_select_association_model
The Android Open Source Project5738f832012-12-12 16:00:35 -08001153**
Satya Calloji444a8da2015-03-06 10:38:22 -08001154** Description This function selects association model to use for STK
1155** generation. Selection is based on both sides' io capability,
1156** oob data flag and authentication request.
The Android Open Source Project5738f832012-12-12 16:00:35 -08001157**
Satya Calloji444a8da2015-03-06 10:38:22 -08001158** Note If Secure Connections Only mode is required locally then we
1159** come to this point only if both sides support Secure Connections
Marie Janssend19e0782016-07-15 12:48:27 -07001160** mode, i.e. if p_cb->secure_connections_only_mode_required = true then we come
Satya Calloji444a8da2015-03-06 10:38:22 -08001161** to this point only if
1162** (p_cb->peer_auth_req & SMP_SC_SUPPORT_BIT) ==
1163** (p_cb->loc_auth_req & SMP_SC_SUPPORT_BIT) ==
1164** SMP_SC_SUPPORT_BIT
1165**
1166*******************************************************************************/
1167tSMP_ASSO_MODEL smp_select_association_model(tSMP_CB *p_cb)
1168{
1169 tSMP_ASSO_MODEL model = SMP_MODEL_OUT_OF_RANGE;
Marie Janssend19e0782016-07-15 12:48:27 -07001170 p_cb->le_secure_connections_mode_is_used = false;
Satya Calloji444a8da2015-03-06 10:38:22 -08001171
Marie Janssend19e0782016-07-15 12:48:27 -07001172 SMP_TRACE_EVENT("%s", __func__);
Satya Calloji444a8da2015-03-06 10:38:22 -08001173 SMP_TRACE_DEBUG("%s p_cb->peer_io_caps = %d p_cb->local_io_capability = %d",
Marie Janssend19e0782016-07-15 12:48:27 -07001174 __func__, p_cb->peer_io_caps, p_cb->local_io_capability);
Satya Calloji444a8da2015-03-06 10:38:22 -08001175 SMP_TRACE_DEBUG("%s p_cb->peer_oob_flag = %d p_cb->loc_oob_flag = %d",
Marie Janssend19e0782016-07-15 12:48:27 -07001176 __func__, p_cb->peer_oob_flag, p_cb->loc_oob_flag);
Satya Calloji444a8da2015-03-06 10:38:22 -08001177 SMP_TRACE_DEBUG("%s p_cb->peer_auth_req = 0x%02x p_cb->loc_auth_req = 0x%02x",
Marie Janssend19e0782016-07-15 12:48:27 -07001178 __func__, p_cb->peer_auth_req, p_cb->loc_auth_req);
Satya Calloji444a8da2015-03-06 10:38:22 -08001179 SMP_TRACE_DEBUG("%s p_cb->secure_connections_only_mode_required = %s",
Marie Janssend19e0782016-07-15 12:48:27 -07001180 __func__, p_cb->secure_connections_only_mode_required ?
1181 "true" : "false");
Satya Calloji444a8da2015-03-06 10:38:22 -08001182
1183 if ((p_cb->peer_auth_req & SMP_SC_SUPPORT_BIT) && (p_cb->loc_auth_req & SMP_SC_SUPPORT_BIT))
1184 {
Marie Janssend19e0782016-07-15 12:48:27 -07001185 p_cb->le_secure_connections_mode_is_used = true;
Satya Calloji444a8da2015-03-06 10:38:22 -08001186 }
1187
1188 SMP_TRACE_DEBUG("use_sc_process = %d", p_cb->le_secure_connections_mode_is_used);
1189
1190 if (p_cb->le_secure_connections_mode_is_used)
1191 {
1192 model = smp_select_association_model_secure_connections(p_cb);
1193 }
1194 else
1195 {
1196 model = smp_select_legacy_association_model(p_cb);
1197 }
1198 return model;
1199}
1200
1201/*******************************************************************************
1202** Function smp_select_legacy_association_model
1203**
1204** Description This function is called to select association mode if at least
1205** one side doesn't support secure connections.
1206**
1207*******************************************************************************/
1208tSMP_ASSO_MODEL smp_select_legacy_association_model(tSMP_CB *p_cb)
1209{
1210 tSMP_ASSO_MODEL model = SMP_MODEL_OUT_OF_RANGE;
1211
1212 SMP_TRACE_DEBUG("%s", __func__);
1213 /* if OOB data is present on both devices, then use OOB association model */
1214 if (p_cb->peer_oob_flag == SMP_OOB_PRESENT && p_cb->loc_oob_flag == SMP_OOB_PRESENT)
1215 return SMP_MODEL_OOB;
1216
1217 /* else if neither device requires MITM, then use Just Works association model */
1218 if (SMP_NO_MITM_REQUIRED (p_cb->peer_auth_req) && SMP_NO_MITM_REQUIRED(p_cb->loc_auth_req))
1219 return SMP_MODEL_ENCRYPTION_ONLY;
1220
1221 /* otherwise use IO capability to select association model */
1222 if (p_cb->peer_io_caps < SMP_IO_CAP_MAX && p_cb->local_io_capability < SMP_IO_CAP_MAX)
1223 {
1224 if (p_cb->role == HCI_ROLE_MASTER)
1225 {
1226 model = smp_association_table[p_cb->role][p_cb->peer_io_caps]
1227 [p_cb->local_io_capability];
1228 }
1229 else
1230 {
1231 model = smp_association_table[p_cb->role][p_cb->local_io_capability]
1232 [p_cb->peer_io_caps];
1233 }
1234 }
1235
1236 return model;
1237}
1238
1239/*******************************************************************************
1240** Function smp_select_association_model_secure_connections
1241**
1242** Description This function is called to select association mode if both
1243** sides support secure connections.
1244**
1245*******************************************************************************/
1246tSMP_ASSO_MODEL smp_select_association_model_secure_connections(tSMP_CB *p_cb)
1247{
1248 tSMP_ASSO_MODEL model = SMP_MODEL_OUT_OF_RANGE;
1249
1250 SMP_TRACE_DEBUG("%s", __func__);
1251 /* if OOB data is present on at least one device, then use OOB association model */
1252 if (p_cb->peer_oob_flag == SMP_OOB_PRESENT || p_cb->loc_oob_flag == SMP_OOB_PRESENT)
1253 return SMP_MODEL_SEC_CONN_OOB;
1254
1255 /* else if neither device requires MITM, then use Just Works association model */
1256 if (SMP_NO_MITM_REQUIRED (p_cb->peer_auth_req) && SMP_NO_MITM_REQUIRED(p_cb->loc_auth_req))
1257 return SMP_MODEL_SEC_CONN_JUSTWORKS;
1258
1259 /* otherwise use IO capability to select association model */
1260 if (p_cb->peer_io_caps < SMP_IO_CAP_MAX && p_cb->local_io_capability < SMP_IO_CAP_MAX)
1261 {
1262 if (p_cb->role == HCI_ROLE_MASTER)
1263 {
1264 model = smp_association_table_sc[p_cb->role][p_cb->peer_io_caps]
1265 [p_cb->local_io_capability];
1266 }
1267 else
1268 {
1269 model = smp_association_table_sc[p_cb->role][p_cb->local_io_capability]
1270 [p_cb->peer_io_caps];
1271 }
1272 }
1273
1274 return model;
1275}
1276
1277/*******************************************************************************
1278** Function smp_reverse_array
1279**
1280** Description This function reverses array bytes
1281**
1282*******************************************************************************/
Marie Janssend19e0782016-07-15 12:48:27 -07001283void smp_reverse_array(uint8_t *arr, uint8_t len)
Satya Calloji444a8da2015-03-06 10:38:22 -08001284{
Marie Janssend19e0782016-07-15 12:48:27 -07001285 uint8_t i =0, tmp;
Satya Calloji444a8da2015-03-06 10:38:22 -08001286
1287 SMP_TRACE_DEBUG("smp_reverse_array");
1288
1289 for (i = 0; i < len/2; i ++)
1290 {
1291 tmp = arr[i];
1292 arr[i] = arr[len -1 - i];
1293 arr[len -1 - i] = tmp;
1294 }
1295}
1296
1297/*******************************************************************************
1298** Function smp_calculate_random_input
1299**
1300** Description This function returns random input value to be used in commitment
1301** calculation for SC passkey entry association mode
1302** (if bit["round"] in "random" array == 1 then returns 0x81
1303** else returns 0x80).
1304**
1305** Returns ri value
1306**
1307*******************************************************************************/
Marie Janssend19e0782016-07-15 12:48:27 -07001308uint8_t smp_calculate_random_input(uint8_t *random, uint8_t round)
Satya Calloji444a8da2015-03-06 10:38:22 -08001309{
Marie Janssend19e0782016-07-15 12:48:27 -07001310 uint8_t i = round/8;
1311 uint8_t j = round%8;
1312 uint8_t ri;
Satya Calloji444a8da2015-03-06 10:38:22 -08001313
1314 SMP_TRACE_DEBUG("random: 0x%02x, round: %d, i: %d, j: %d", random[i], round, i, j);
1315 ri = ((random[i] >> j) & 1) | 0x80;
1316 SMP_TRACE_DEBUG("%s ri=0x%02x", __func__, ri);
1317 return ri;
1318}
1319
1320/*******************************************************************************
1321** Function smp_collect_local_io_capabilities
1322**
1323** Description This function puts into IOcap array local device
1324** IOCapability, OOB data, AuthReq.
The Android Open Source Project5738f832012-12-12 16:00:35 -08001325**
1326** Returns void
1327**
1328*******************************************************************************/
Marie Janssend19e0782016-07-15 12:48:27 -07001329void smp_collect_local_io_capabilities(uint8_t *iocap, tSMP_CB *p_cb)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001330{
Satya Calloji444a8da2015-03-06 10:38:22 -08001331 SMP_TRACE_DEBUG("%s", __func__);
1332
1333 iocap[0] = p_cb->local_io_capability;
1334 iocap[1] = p_cb->loc_oob_flag;
1335 iocap[2] = p_cb->loc_auth_req;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001336}
1337
The Android Open Source Project5738f832012-12-12 16:00:35 -08001338/*******************************************************************************
Satya Calloji444a8da2015-03-06 10:38:22 -08001339** Function smp_collect_peer_io_capabilities
The Android Open Source Project5738f832012-12-12 16:00:35 -08001340**
Satya Calloji444a8da2015-03-06 10:38:22 -08001341** Description This function puts into IOcap array peer device
1342** IOCapability, OOB data, AuthReq.
The Android Open Source Project5738f832012-12-12 16:00:35 -08001343**
1344** Returns void
1345**
1346*******************************************************************************/
Marie Janssend19e0782016-07-15 12:48:27 -07001347void smp_collect_peer_io_capabilities(uint8_t *iocap, tSMP_CB *p_cb)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001348{
Satya Calloji444a8da2015-03-06 10:38:22 -08001349 SMP_TRACE_DEBUG("%s", __func__);
1350
1351 iocap[0] = p_cb->peer_io_caps;
1352 iocap[1] = p_cb->peer_oob_flag;
1353 iocap[2] = p_cb->peer_auth_req;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001354}
1355
The Android Open Source Project5738f832012-12-12 16:00:35 -08001356/*******************************************************************************
Satya Calloji444a8da2015-03-06 10:38:22 -08001357** Function smp_collect_local_ble_address
The Android Open Source Project5738f832012-12-12 16:00:35 -08001358**
Satya Calloji444a8da2015-03-06 10:38:22 -08001359** Description This function puts into le_addr array local device le address:
1360** le_addr[0-5] = local BD ADDR,
1361** le_addr[6] = local le address type (PUBLIC/RANDOM).
The Android Open Source Project5738f832012-12-12 16:00:35 -08001362**
1363** Returns void
1364**
1365*******************************************************************************/
Marie Janssend19e0782016-07-15 12:48:27 -07001366void smp_collect_local_ble_address(uint8_t *le_addr, tSMP_CB *p_cb)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001367{
Satya Calloji444a8da2015-03-06 10:38:22 -08001368 tBLE_ADDR_TYPE addr_type = 0;
1369 BD_ADDR bda;
Marie Janssend19e0782016-07-15 12:48:27 -07001370 uint8_t *p = le_addr;
Satya Calloji444a8da2015-03-06 10:38:22 -08001371
1372 SMP_TRACE_DEBUG("%s", __func__);
1373
1374 BTM_ReadConnectionAddr( p_cb->pairing_bda, bda, &addr_type);
1375 BDADDR_TO_STREAM(p, bda);
1376 UINT8_TO_STREAM(p, addr_type);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001377}
1378
1379/*******************************************************************************
Satya Calloji444a8da2015-03-06 10:38:22 -08001380** Function smp_collect_peer_ble_address
The Android Open Source Project5738f832012-12-12 16:00:35 -08001381**
Satya Calloji444a8da2015-03-06 10:38:22 -08001382** Description This function puts into le_addr array peer device le address:
1383** le_addr[0-5] = peer BD ADDR,
1384** le_addr[6] = peer le address type (PUBLIC/RANDOM).
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001385**
1386** Returns void
1387**
1388*******************************************************************************/
Marie Janssend19e0782016-07-15 12:48:27 -07001389void smp_collect_peer_ble_address(uint8_t *le_addr, tSMP_CB *p_cb)
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001390{
Satya Calloji444a8da2015-03-06 10:38:22 -08001391 tBLE_ADDR_TYPE addr_type = 0;
1392 BD_ADDR bda;
Marie Janssend19e0782016-07-15 12:48:27 -07001393 uint8_t *p = le_addr;
Satya Calloji444a8da2015-03-06 10:38:22 -08001394
1395 SMP_TRACE_DEBUG("%s", __func__);
1396
1397 if (!BTM_ReadRemoteConnectionAddr(p_cb->pairing_bda, bda, &addr_type))
1398 {
1399 SMP_TRACE_ERROR("can not collect peer le addr information for unknown device");
1400 return;
1401 }
1402
1403 BDADDR_TO_STREAM(p, bda);
1404 UINT8_TO_STREAM(p, addr_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001405}
The Android Open Source Project5738f832012-12-12 16:00:35 -08001406
Satya Calloji444a8da2015-03-06 10:38:22 -08001407/*******************************************************************************
1408** Function smp_check_commitment
1409**
1410** Description This function compares peer commitment values:
1411** - expected (i.e. calculated locally),
1412** - received from the peer.
1413**
Marie Janssend19e0782016-07-15 12:48:27 -07001414** Returns true if the values are the same
1415** false otherwise
Satya Calloji444a8da2015-03-06 10:38:22 -08001416**
1417*******************************************************************************/
Marie Janssend19e0782016-07-15 12:48:27 -07001418bool smp_check_commitment(tSMP_CB *p_cb)
Satya Calloji444a8da2015-03-06 10:38:22 -08001419{
1420 BT_OCTET16 expected;
1421
1422 SMP_TRACE_DEBUG("%s", __func__);
1423
1424 smp_calculate_peer_commitment(p_cb, expected);
Marie Janssend19e0782016-07-15 12:48:27 -07001425 print128(expected, (const uint8_t *)"calculated peer commitment");
1426 print128(p_cb->remote_commitment, (const uint8_t *)"received peer commitment");
Satya Calloji444a8da2015-03-06 10:38:22 -08001427
1428 if (memcmp(p_cb->remote_commitment, expected, BT_OCTET16_LEN))
1429 {
Jakub Pawlowskice832512016-07-28 06:11:36 -07001430 SMP_TRACE_WARNING("%s: Commitment check fails", __func__);
Marie Janssend19e0782016-07-15 12:48:27 -07001431 return false;
Satya Calloji444a8da2015-03-06 10:38:22 -08001432 }
1433
Jakub Pawlowskice832512016-07-28 06:11:36 -07001434 SMP_TRACE_DEBUG("%s: Commitment check succeeds", __func__);
Marie Janssend19e0782016-07-15 12:48:27 -07001435 return true;
Satya Calloji444a8da2015-03-06 10:38:22 -08001436}
1437
1438/*******************************************************************************
1439**
1440** Function smp_save_secure_connections_long_term_key
1441**
1442** Description The function saves SC LTK as BLE key for future use as local
1443** and/or peer key.
1444**
1445** Returns void
1446**
1447*******************************************************************************/
1448void smp_save_secure_connections_long_term_key(tSMP_CB *p_cb)
1449{
1450 tBTM_LE_LENC_KEYS lle_key;
1451 tBTM_LE_PENC_KEYS ple_key;
1452
1453 SMP_TRACE_DEBUG("%s-Save LTK as local LTK key", __func__);
1454 memcpy(lle_key.ltk, p_cb->ltk, BT_OCTET16_LEN);
1455 lle_key.div = 0;
1456 lle_key.key_size = p_cb->loc_enc_size;
1457 lle_key.sec_level = p_cb->sec_level;
Marie Janssend19e0782016-07-15 12:48:27 -07001458 btm_sec_save_le_key(p_cb->pairing_bda, BTM_LE_KEY_LENC, (tBTM_LE_KEY_VALUE *)&lle_key, true);
Satya Calloji444a8da2015-03-06 10:38:22 -08001459
1460 SMP_TRACE_DEBUG("%s-Save LTK as peer LTK key", __func__);
1461 ple_key.ediv = 0;
1462 memset(ple_key.rand, 0, BT_OCTET8_LEN);
1463 memcpy(ple_key.ltk, p_cb->ltk, BT_OCTET16_LEN);
1464 ple_key.sec_level = p_cb->sec_level;
1465 ple_key.key_size = p_cb->loc_enc_size;
Marie Janssend19e0782016-07-15 12:48:27 -07001466 btm_sec_save_le_key(p_cb->pairing_bda, BTM_LE_KEY_PENC, (tBTM_LE_KEY_VALUE *)&ple_key, true);
Satya Calloji444a8da2015-03-06 10:38:22 -08001467}
1468
1469/*******************************************************************************
1470**
1471** Function smp_calculate_f5_mackey_and_long_term_key
1472**
1473** Description The function calculates MacKey and LTK and saves them in CB.
1474** To calculate MacKey and LTK it calls smp_calc_f5(...).
1475** MacKey is used in dhkey calculation, LTK is used to encrypt
1476** the link.
1477**
Marie Janssend19e0782016-07-15 12:48:27 -07001478** Returns false if out of resources, true otherwise.
Satya Calloji444a8da2015-03-06 10:38:22 -08001479**
1480*******************************************************************************/
Marie Janssend19e0782016-07-15 12:48:27 -07001481bool smp_calculate_f5_mackey_and_long_term_key(tSMP_CB *p_cb)
Satya Calloji444a8da2015-03-06 10:38:22 -08001482{
Marie Janssend19e0782016-07-15 12:48:27 -07001483 uint8_t a[7];
1484 uint8_t b[7];
1485 uint8_t *p_na;
1486 uint8_t *p_nb;
Satya Calloji444a8da2015-03-06 10:38:22 -08001487
1488 SMP_TRACE_DEBUG("%s", __func__);
1489
1490 if (p_cb->role == HCI_ROLE_MASTER)
1491 {
1492 smp_collect_local_ble_address(a, p_cb);
1493 smp_collect_peer_ble_address(b, p_cb);
1494 p_na = p_cb->rand;
1495 p_nb = p_cb->rrand;
1496 }
1497 else
1498 {
1499 smp_collect_local_ble_address(b, p_cb);
1500 smp_collect_peer_ble_address(a, p_cb);
1501 p_na = p_cb->rrand;
1502 p_nb = p_cb->rand;
1503 }
1504
1505 if(!smp_calculate_f5(p_cb->dhkey, p_na, p_nb, a, b, p_cb->mac_key, p_cb->ltk))
1506 {
1507 SMP_TRACE_ERROR("%s failed", __func__);
Marie Janssend19e0782016-07-15 12:48:27 -07001508 return false;
Satya Calloji444a8da2015-03-06 10:38:22 -08001509 }
1510
1511 SMP_TRACE_EVENT ("%s is completed", __func__);
Marie Janssend19e0782016-07-15 12:48:27 -07001512 return true;
Satya Calloji444a8da2015-03-06 10:38:22 -08001513}
1514
1515/*******************************************************************************
1516**
1517** Function smp_request_oob_data
1518**
1519** Description Requests application to provide OOB data.
1520**
Marie Janssend19e0782016-07-15 12:48:27 -07001521** Returns true - OOB data has to be provided by application
1522** false - otherwise (unexpected)
Satya Calloji444a8da2015-03-06 10:38:22 -08001523**
1524*******************************************************************************/
Marie Janssend19e0782016-07-15 12:48:27 -07001525bool smp_request_oob_data(tSMP_CB *p_cb)
Satya Calloji444a8da2015-03-06 10:38:22 -08001526{
1527 tSMP_OOB_DATA_TYPE req_oob_type = SMP_OOB_INVALID_TYPE;
1528
1529 SMP_TRACE_DEBUG("%s", __func__);
1530
1531 if (p_cb->peer_oob_flag == SMP_OOB_PRESENT && p_cb->loc_oob_flag == SMP_OOB_PRESENT)
1532 {
1533 /* both local and peer rcvd data OOB */
1534 req_oob_type = SMP_OOB_BOTH;
1535 }
1536 else if (p_cb->peer_oob_flag == SMP_OOB_PRESENT)
1537 {
1538 /* peer rcvd OOB local data, local didn't receive OOB peer data */
1539 req_oob_type = SMP_OOB_LOCAL;
1540 }
1541 else if (p_cb->loc_oob_flag == SMP_OOB_PRESENT)
1542 {
1543 req_oob_type = SMP_OOB_PEER;
1544 }
1545
1546 SMP_TRACE_DEBUG("req_oob_type = %d", req_oob_type);
1547
1548 if (req_oob_type == SMP_OOB_INVALID_TYPE)
Marie Janssend19e0782016-07-15 12:48:27 -07001549 return false;
Satya Calloji444a8da2015-03-06 10:38:22 -08001550
1551 p_cb->req_oob_type = req_oob_type;
1552 p_cb->cb_evt = SMP_SC_OOB_REQ_EVT;
1553 smp_sm_event(p_cb, SMP_TK_REQ_EVT, &req_oob_type);
1554
Marie Janssend19e0782016-07-15 12:48:27 -07001555 return true;
Satya Calloji444a8da2015-03-06 10:38:22 -08001556}
The Android Open Source Project5738f832012-12-12 16:00:35 -08001557
1558
1559#endif
1560