OSDN Git Service

Remove memory reference to invalid mem in error log
[android-x86/system-bt.git] / stack / smp / smp_utils.cc
1 /******************************************************************************
2  *
3  *  Copyright 1999-2012 Broadcom Corporation
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  *****************************************************************************/
18
19 /******************************************************************************
20  *
21  *  This file contains functions for the SMP L2CAP utility functions
22  *
23  ******************************************************************************/
24 #include "bt_target.h"
25
26 #include <ctype.h>
27 #include <string.h>
28 #include "bt_types.h"
29 #include "bt_utils.h"
30 #include "btm_ble_api.h"
31 #include "btm_int.h"
32 #include "device/include/controller.h"
33 #include "hcidefs.h"
34 #include "l2c_api.h"
35 #include "l2c_int.h"
36 #include "osi/include/osi.h"
37 #include "smp_int.h"
38
39 #define SMP_PAIRING_REQ_SIZE 7
40 #define SMP_CONFIRM_CMD_SIZE (BT_OCTET16_LEN + 1)
41 #define SMP_RAND_CMD_SIZE (BT_OCTET16_LEN + 1)
42 #define SMP_INIT_CMD_SIZE (BT_OCTET16_LEN + 1)
43 #define SMP_ENC_INFO_SIZE (BT_OCTET16_LEN + 1)
44 #define SMP_MASTER_ID_SIZE (BT_OCTET8_LEN + 2 + 1)
45 #define SMP_ID_INFO_SIZE (BT_OCTET16_LEN + 1)
46 #define SMP_ID_ADDR_SIZE (BD_ADDR_LEN + 1 + 1)
47 #define SMP_SIGN_INFO_SIZE (BT_OCTET16_LEN + 1)
48 #define SMP_PAIR_FAIL_SIZE 2
49 #define SMP_SECURITY_REQUEST_SIZE 2
50 #define SMP_PAIR_PUBL_KEY_SIZE (1 /* opcode */ + (2 * BT_OCTET32_LEN))
51 #define SMP_PAIR_COMMITM_SIZE (1 /* opcode */ + BT_OCTET16_LEN /*Commitment*/)
52 #define SMP_PAIR_DHKEY_CHECK_SIZE \
53   (1 /* opcode */ + BT_OCTET16_LEN /*DHKey Check*/)
54 #define SMP_PAIR_KEYPR_NOTIF_SIZE (1 /* opcode */ + 1 /*Notif Type*/)
55
56 /* SMP command sizes per spec */
57 static const uint8_t smp_cmd_size_per_spec[] = {
58     0,
59     SMP_PAIRING_REQ_SIZE,      /* 0x01: pairing request */
60     SMP_PAIRING_REQ_SIZE,      /* 0x02: pairing response */
61     SMP_CONFIRM_CMD_SIZE,      /* 0x03: pairing confirm */
62     SMP_RAND_CMD_SIZE,         /* 0x04: pairing random */
63     SMP_PAIR_FAIL_SIZE,        /* 0x05: pairing failed */
64     SMP_ENC_INFO_SIZE,         /* 0x06: encryption information */
65     SMP_MASTER_ID_SIZE,        /* 0x07: master identification */
66     SMP_ID_INFO_SIZE,          /* 0x08: identity information */
67     SMP_ID_ADDR_SIZE,          /* 0x09: identity address information */
68     SMP_SIGN_INFO_SIZE,        /* 0x0A: signing information */
69     SMP_SECURITY_REQUEST_SIZE, /* 0x0B: security request */
70     SMP_PAIR_PUBL_KEY_SIZE,    /* 0x0C: pairing public key */
71     SMP_PAIR_DHKEY_CHECK_SIZE, /* 0x0D: pairing dhkey check */
72     SMP_PAIR_KEYPR_NOTIF_SIZE, /* 0x0E: pairing keypress notification */
73     SMP_PAIR_COMMITM_SIZE      /* 0x0F: pairing commitment */
74 };
75
76 static bool smp_parameter_unconditionally_valid(tSMP_CB* p_cb);
77 static bool smp_parameter_unconditionally_invalid(tSMP_CB* p_cb);
78
79 /* type for SMP command length validation functions */
80 typedef bool (*tSMP_CMD_LEN_VALID)(tSMP_CB* p_cb);
81
82 static bool smp_command_has_valid_fixed_length(tSMP_CB* p_cb);
83
84 static const tSMP_CMD_LEN_VALID smp_cmd_len_is_valid[] = {
85     smp_parameter_unconditionally_invalid,
86     smp_command_has_valid_fixed_length, /* 0x01: pairing request */
87     smp_command_has_valid_fixed_length, /* 0x02: pairing response */
88     smp_command_has_valid_fixed_length, /* 0x03: pairing confirm */
89     smp_command_has_valid_fixed_length, /* 0x04: pairing random */
90     smp_command_has_valid_fixed_length, /* 0x05: pairing failed */
91     smp_command_has_valid_fixed_length, /* 0x06: encryption information */
92     smp_command_has_valid_fixed_length, /* 0x07: master identification */
93     smp_command_has_valid_fixed_length, /* 0x08: identity information */
94     smp_command_has_valid_fixed_length, /* 0x09: identity address information */
95     smp_command_has_valid_fixed_length, /* 0x0A: signing information */
96     smp_command_has_valid_fixed_length, /* 0x0B: security request */
97     smp_command_has_valid_fixed_length, /* 0x0C: pairing public key */
98     smp_command_has_valid_fixed_length, /* 0x0D: pairing dhkey check */
99     smp_command_has_valid_fixed_length, /* 0x0E: pairing keypress notification*/
100     smp_command_has_valid_fixed_length  /* 0x0F: pairing commitment */
101 };
102
103 /* type for SMP command parameter ranges validation functions */
104 typedef bool (*tSMP_CMD_PARAM_RANGES_VALID)(tSMP_CB* p_cb);
105
106 static bool smp_pairing_request_response_parameters_are_valid(tSMP_CB* p_cb);
107 static bool smp_pairing_keypress_notification_is_valid(tSMP_CB* p_cb);
108
109 static const tSMP_CMD_PARAM_RANGES_VALID smp_cmd_param_ranges_are_valid[] = {
110     smp_parameter_unconditionally_invalid,
111     smp_pairing_request_response_parameters_are_valid, /* 0x01: pairing
112                                                           request */
113     smp_pairing_request_response_parameters_are_valid, /* 0x02: pairing
114                                                           response */
115     smp_parameter_unconditionally_valid, /* 0x03: pairing confirm */
116     smp_parameter_unconditionally_valid, /* 0x04: pairing random */
117     smp_parameter_unconditionally_valid, /* 0x05: pairing failed */
118     smp_parameter_unconditionally_valid, /* 0x06: encryption information */
119     smp_parameter_unconditionally_valid, /* 0x07: master identification */
120     smp_parameter_unconditionally_valid, /* 0x08: identity information */
121     smp_parameter_unconditionally_valid, /* 0x09: identity address
122                                             information */
123     smp_parameter_unconditionally_valid, /* 0x0A: signing information */
124     smp_parameter_unconditionally_valid, /* 0x0B: security request */
125     smp_parameter_unconditionally_valid, /* 0x0C: pairing public key */
126     smp_parameter_unconditionally_valid, /* 0x0D: pairing dhkey check */
127     smp_pairing_keypress_notification_is_valid, /* 0x0E: pairing keypress
128                                                    notification */
129     smp_parameter_unconditionally_valid         /* 0x0F: pairing commitment */
130 };
131
132 /* type for action functions */
133 typedef BT_HDR* (*tSMP_CMD_ACT)(uint8_t cmd_code, tSMP_CB* p_cb);
134
135 static BT_HDR* smp_build_pairing_cmd(uint8_t cmd_code, tSMP_CB* p_cb);
136 static BT_HDR* smp_build_confirm_cmd(UNUSED_ATTR uint8_t cmd_code,
137                                      tSMP_CB* p_cb);
138 static BT_HDR* smp_build_rand_cmd(UNUSED_ATTR uint8_t cmd_code, tSMP_CB* p_cb);
139 static BT_HDR* smp_build_pairing_fail(UNUSED_ATTR uint8_t cmd_code,
140                                       tSMP_CB* p_cb);
141 static BT_HDR* smp_build_identity_info_cmd(UNUSED_ATTR uint8_t cmd_code,
142                                            tSMP_CB* p_cb);
143 static BT_HDR* smp_build_encrypt_info_cmd(UNUSED_ATTR uint8_t cmd_code,
144                                           tSMP_CB* p_cb);
145 static BT_HDR* smp_build_security_request(UNUSED_ATTR uint8_t cmd_code,
146                                           tSMP_CB* p_cb);
147 static BT_HDR* smp_build_signing_info_cmd(UNUSED_ATTR uint8_t cmd_code,
148                                           tSMP_CB* p_cb);
149 static BT_HDR* smp_build_master_id_cmd(UNUSED_ATTR uint8_t cmd_code,
150                                        tSMP_CB* p_cb);
151 static BT_HDR* smp_build_id_addr_cmd(UNUSED_ATTR uint8_t cmd_code,
152                                      tSMP_CB* p_cb);
153 static BT_HDR* smp_build_pair_public_key_cmd(UNUSED_ATTR uint8_t cmd_code,
154                                              tSMP_CB* p_cb);
155 static BT_HDR* smp_build_pairing_commitment_cmd(UNUSED_ATTR uint8_t cmd_code,
156                                                 tSMP_CB* p_cb);
157 static BT_HDR* smp_build_pair_dhkey_check_cmd(UNUSED_ATTR uint8_t cmd_code,
158                                               tSMP_CB* p_cb);
159 static BT_HDR* smp_build_pairing_keypress_notification_cmd(
160     UNUSED_ATTR uint8_t cmd_code, tSMP_CB* p_cb);
161
162 static const tSMP_CMD_ACT smp_cmd_build_act[] = {
163     NULL, smp_build_pairing_cmd,    /* 0x01: pairing request */
164     smp_build_pairing_cmd,          /* 0x02: pairing response */
165     smp_build_confirm_cmd,          /* 0x03: pairing confirm */
166     smp_build_rand_cmd,             /* 0x04: pairing random */
167     smp_build_pairing_fail,         /* 0x05: pairing failure */
168     smp_build_encrypt_info_cmd,     /* 0x06: encryption information */
169     smp_build_master_id_cmd,        /* 0x07: master identification */
170     smp_build_identity_info_cmd,    /* 0x08: identity information */
171     smp_build_id_addr_cmd,          /* 0x09: identity address information */
172     smp_build_signing_info_cmd,     /* 0x0A: signing information */
173     smp_build_security_request,     /* 0x0B: security request */
174     smp_build_pair_public_key_cmd,  /* 0x0C: pairing public key */
175     smp_build_pair_dhkey_check_cmd, /* 0x0D: pairing DHKey check */
176     smp_build_pairing_keypress_notification_cmd, /* 0x0E: pairing keypress
177                                                     notification */
178     smp_build_pairing_commitment_cmd             /* 0x0F: pairing commitment */
179 };
180
181 static const uint8_t smp_association_table[2][SMP_IO_CAP_MAX][SMP_IO_CAP_MAX] =
182     {
183         /* display only */ /* Display Yes/No */ /* keyboard only */
184         /* No Input/Output */                   /* keyboard display */
185
186         /* initiator */
187         /* model = tbl[peer_io_caps][loc_io_caps] */
188         /* Display Only */
189         {{SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_ENCRYPTION_ONLY,
190           SMP_MODEL_PASSKEY, SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_PASSKEY},
191
192          /* Display Yes/No */
193          {SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_ENCRYPTION_ONLY,
194           SMP_MODEL_PASSKEY, SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_PASSKEY},
195
196          /* Keyboard only */
197          {SMP_MODEL_KEY_NOTIF, SMP_MODEL_KEY_NOTIF, SMP_MODEL_PASSKEY,
198           SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_KEY_NOTIF},
199
200          /* No Input No Output */
201          {SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_ENCRYPTION_ONLY,
202           SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_ENCRYPTION_ONLY,
203           SMP_MODEL_ENCRYPTION_ONLY},
204
205          /* keyboard display */
206          {SMP_MODEL_KEY_NOTIF, SMP_MODEL_KEY_NOTIF, SMP_MODEL_PASSKEY,
207           SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_KEY_NOTIF}},
208
209         /* responder */
210         /* model = tbl[loc_io_caps][peer_io_caps] */
211         /* Display Only */
212         {{SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_ENCRYPTION_ONLY,
213           SMP_MODEL_KEY_NOTIF, SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_KEY_NOTIF},
214
215          /* Display Yes/No */
216          {SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_ENCRYPTION_ONLY,
217           SMP_MODEL_KEY_NOTIF, SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_KEY_NOTIF},
218
219          /* keyboard only */
220          {SMP_MODEL_PASSKEY, SMP_MODEL_PASSKEY, SMP_MODEL_PASSKEY,
221           SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_PASSKEY},
222
223          /* No Input No Output */
224          {SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_ENCRYPTION_ONLY,
225           SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_ENCRYPTION_ONLY,
226           SMP_MODEL_ENCRYPTION_ONLY},
227
228          /* keyboard display */
229          {SMP_MODEL_PASSKEY, SMP_MODEL_PASSKEY, SMP_MODEL_KEY_NOTIF,
230           SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_PASSKEY}}};
231
232 static const uint8_t
233     smp_association_table_sc[2][SMP_IO_CAP_MAX][SMP_IO_CAP_MAX] = {
234         /* display only */ /* Display Yes/No */ /* keyboard only */
235         /* No InputOutput */                    /* keyboard display */
236
237         /* initiator */
238         /* model = tbl[peer_io_caps][loc_io_caps] */
239
240         /* Display Only */
241         {{SMP_MODEL_SEC_CONN_JUSTWORKS, SMP_MODEL_SEC_CONN_JUSTWORKS,
242           SMP_MODEL_SEC_CONN_PASSKEY_ENT, SMP_MODEL_SEC_CONN_JUSTWORKS,
243           SMP_MODEL_SEC_CONN_PASSKEY_ENT},
244
245          /* Display Yes/No */
246          {SMP_MODEL_SEC_CONN_JUSTWORKS, SMP_MODEL_SEC_CONN_NUM_COMP,
247           SMP_MODEL_SEC_CONN_PASSKEY_ENT, SMP_MODEL_SEC_CONN_JUSTWORKS,
248           SMP_MODEL_SEC_CONN_NUM_COMP},
249
250          /* keyboard only */
251          {SMP_MODEL_SEC_CONN_PASSKEY_DISP, SMP_MODEL_SEC_CONN_PASSKEY_DISP,
252           SMP_MODEL_SEC_CONN_PASSKEY_ENT, SMP_MODEL_SEC_CONN_JUSTWORKS,
253           SMP_MODEL_SEC_CONN_PASSKEY_DISP},
254
255          /* No Input No Output */
256          {SMP_MODEL_SEC_CONN_JUSTWORKS, SMP_MODEL_SEC_CONN_JUSTWORKS,
257           SMP_MODEL_SEC_CONN_JUSTWORKS, SMP_MODEL_SEC_CONN_JUSTWORKS,
258           SMP_MODEL_SEC_CONN_JUSTWORKS},
259
260          /* keyboard display */
261          {SMP_MODEL_SEC_CONN_PASSKEY_DISP, SMP_MODEL_SEC_CONN_NUM_COMP,
262           SMP_MODEL_SEC_CONN_PASSKEY_ENT, SMP_MODEL_SEC_CONN_JUSTWORKS,
263           SMP_MODEL_SEC_CONN_NUM_COMP}},
264
265         /* responder */
266         /* model = tbl[loc_io_caps][peer_io_caps] */
267
268         /* Display Only */
269         {{SMP_MODEL_SEC_CONN_JUSTWORKS, SMP_MODEL_SEC_CONN_JUSTWORKS,
270           SMP_MODEL_SEC_CONN_PASSKEY_DISP, SMP_MODEL_SEC_CONN_JUSTWORKS,
271           SMP_MODEL_SEC_CONN_PASSKEY_DISP},
272
273          /* Display Yes/No */
274          {SMP_MODEL_SEC_CONN_JUSTWORKS, SMP_MODEL_SEC_CONN_NUM_COMP,
275           SMP_MODEL_SEC_CONN_PASSKEY_DISP, SMP_MODEL_SEC_CONN_JUSTWORKS,
276           SMP_MODEL_SEC_CONN_NUM_COMP},
277
278          /* keyboard only */
279          {SMP_MODEL_SEC_CONN_PASSKEY_ENT, SMP_MODEL_SEC_CONN_PASSKEY_ENT,
280           SMP_MODEL_SEC_CONN_PASSKEY_ENT, SMP_MODEL_SEC_CONN_JUSTWORKS,
281           SMP_MODEL_SEC_CONN_PASSKEY_ENT},
282
283          /* No Input No Output */
284          {SMP_MODEL_SEC_CONN_JUSTWORKS, SMP_MODEL_SEC_CONN_JUSTWORKS,
285           SMP_MODEL_SEC_CONN_JUSTWORKS, SMP_MODEL_SEC_CONN_JUSTWORKS,
286           SMP_MODEL_SEC_CONN_JUSTWORKS},
287
288          /* keyboard display */
289          {SMP_MODEL_SEC_CONN_PASSKEY_ENT, SMP_MODEL_SEC_CONN_NUM_COMP,
290           SMP_MODEL_SEC_CONN_PASSKEY_DISP, SMP_MODEL_SEC_CONN_JUSTWORKS,
291           SMP_MODEL_SEC_CONN_NUM_COMP}}};
292
293 static tSMP_ASSO_MODEL smp_select_legacy_association_model(tSMP_CB* p_cb);
294 static tSMP_ASSO_MODEL smp_select_association_model_secure_connections(
295     tSMP_CB* p_cb);
296
297 /*******************************************************************************
298  *
299  * Function         smp_send_msg_to_L2CAP
300  *
301  * Description      Send message to L2CAP.
302  *
303  ******************************************************************************/
304 bool smp_send_msg_to_L2CAP(const RawAddress& rem_bda, BT_HDR* p_toL2CAP) {
305   uint16_t l2cap_ret;
306   uint16_t fixed_cid = L2CAP_SMP_CID;
307
308   if (smp_cb.smp_over_br) {
309     fixed_cid = L2CAP_SMP_BR_CID;
310   }
311
312   SMP_TRACE_EVENT("%s", __func__);
313   smp_cb.total_tx_unacked += 1;
314
315   l2cap_ret = L2CA_SendFixedChnlData(fixed_cid, rem_bda, p_toL2CAP);
316   if (l2cap_ret == L2CAP_DW_FAILED) {
317     smp_cb.total_tx_unacked -= 1;
318     SMP_TRACE_ERROR("SMP failed to pass msg to L2CAP");
319     return false;
320   } else
321     return true;
322 }
323
324 /*******************************************************************************
325  *
326  * Function         smp_send_cmd
327  *
328  * Description      send a SMP command on L2CAP channel.
329  *
330  ******************************************************************************/
331 bool smp_send_cmd(uint8_t cmd_code, tSMP_CB* p_cb) {
332   BT_HDR* p_buf;
333   bool sent = false;
334   SMP_TRACE_EVENT("smp_send_cmd on l2cap cmd_code=0x%x", cmd_code);
335   if (cmd_code <= (SMP_OPCODE_MAX + 1 /* for SMP_OPCODE_PAIR_COMMITM */) &&
336       smp_cmd_build_act[cmd_code] != NULL) {
337     p_buf = (*smp_cmd_build_act[cmd_code])(cmd_code, p_cb);
338
339     if (p_buf != NULL && smp_send_msg_to_L2CAP(p_cb->pairing_bda, p_buf)) {
340       sent = true;
341       alarm_set_on_mloop(p_cb->smp_rsp_timer_ent, SMP_WAIT_FOR_RSP_TIMEOUT_MS,
342                          smp_rsp_timeout, NULL);
343     }
344   }
345
346   if (!sent) {
347     tSMP_INT_DATA smp_int_data;
348     smp_int_data.status = SMP_PAIR_INTERNAL_ERR;
349     if (p_cb->smp_over_br) {
350       smp_br_state_machine_event(p_cb, SMP_BR_AUTH_CMPL_EVT, &smp_int_data);
351     } else {
352       smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &smp_int_data);
353     }
354   }
355   return sent;
356 }
357
358 /*******************************************************************************
359  *
360  * Function         smp_rsp_timeout
361  *
362  * Description      Called when SMP wait for SMP command response timer expires
363  *
364  * Returns          void
365  *
366  ******************************************************************************/
367 void smp_rsp_timeout(UNUSED_ATTR void* data) {
368   tSMP_CB* p_cb = &smp_cb;
369
370   SMP_TRACE_EVENT("%s state:%d br_state:%d", __func__, p_cb->state,
371                   p_cb->br_state);
372
373   tSMP_INT_DATA smp_int_data;
374   smp_int_data.status = SMP_RSP_TIMEOUT;
375   if (p_cb->smp_over_br) {
376     smp_br_state_machine_event(p_cb, SMP_BR_AUTH_CMPL_EVT, &smp_int_data);
377   } else {
378     smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &smp_int_data);
379   }
380 }
381
382 /*******************************************************************************
383  *
384  * Function         smp_delayed_auth_complete_timeout
385  *
386  * Description      Called when no pairing failed command received within
387  *                  timeout period.
388  *
389  * Returns          void
390  *
391  ******************************************************************************/
392 void smp_delayed_auth_complete_timeout(UNUSED_ATTR void* data) {
393   /*
394    * Waited for potential pair failure. Send SMP_AUTH_CMPL_EVT if
395    * the state is still in bond pending.
396    */
397   if (smp_get_state() == SMP_STATE_BOND_PENDING) {
398     SMP_TRACE_EVENT("%s sending delayed auth complete.", __func__);
399     tSMP_INT_DATA smp_int_data;
400     smp_int_data.status = SMP_SUCCESS;
401     smp_sm_event(&smp_cb, SMP_AUTH_CMPL_EVT, &smp_int_data);
402   }
403 }
404
405 /*******************************************************************************
406  *
407  * Function         smp_build_pairing_req_cmd
408  *
409  * Description      Build pairing request command.
410  *
411  ******************************************************************************/
412 BT_HDR* smp_build_pairing_cmd(uint8_t cmd_code, tSMP_CB* p_cb) {
413   uint8_t* p;
414   BT_HDR* p_buf = (BT_HDR*)osi_malloc(sizeof(BT_HDR) + SMP_PAIRING_REQ_SIZE +
415                                       L2CAP_MIN_OFFSET);
416
417   SMP_TRACE_EVENT("%s", __func__);
418
419   p = (uint8_t*)(p_buf + 1) + L2CAP_MIN_OFFSET;
420   UINT8_TO_STREAM(p, cmd_code);
421   UINT8_TO_STREAM(p, p_cb->local_io_capability);
422   UINT8_TO_STREAM(p, p_cb->loc_oob_flag);
423   UINT8_TO_STREAM(p, p_cb->loc_auth_req);
424   UINT8_TO_STREAM(p, p_cb->loc_enc_size);
425   UINT8_TO_STREAM(p, p_cb->local_i_key);
426   UINT8_TO_STREAM(p, p_cb->local_r_key);
427
428   p_buf->offset = L2CAP_MIN_OFFSET;
429   /* 1B ERR_RSP op code + 1B cmd_op_code + 2B handle + 1B status */
430   p_buf->len = SMP_PAIRING_REQ_SIZE;
431
432   return p_buf;
433 }
434
435 /*******************************************************************************
436  *
437  * Function         smp_build_confirm_cmd
438  *
439  * Description      Build confirm request command.
440  *
441  ******************************************************************************/
442 static BT_HDR* smp_build_confirm_cmd(UNUSED_ATTR uint8_t cmd_code,
443                                      tSMP_CB* p_cb) {
444   uint8_t* p;
445   BT_HDR* p_buf = (BT_HDR*)osi_malloc(sizeof(BT_HDR) + SMP_CONFIRM_CMD_SIZE +
446                                       L2CAP_MIN_OFFSET);
447
448   SMP_TRACE_EVENT("%s", __func__);
449
450   p = (uint8_t*)(p_buf + 1) + L2CAP_MIN_OFFSET;
451
452   UINT8_TO_STREAM(p, SMP_OPCODE_CONFIRM);
453   ARRAY_TO_STREAM(p, p_cb->confirm, BT_OCTET16_LEN);
454
455   p_buf->offset = L2CAP_MIN_OFFSET;
456   p_buf->len = SMP_CONFIRM_CMD_SIZE;
457
458   return p_buf;
459 }
460
461 /*******************************************************************************
462  *
463  * Function         smp_build_rand_cmd
464  *
465  * Description      Build Random command.
466  *
467  ******************************************************************************/
468 static BT_HDR* smp_build_rand_cmd(UNUSED_ATTR uint8_t cmd_code, tSMP_CB* p_cb) {
469   uint8_t* p;
470   BT_HDR* p_buf = (BT_HDR*)osi_malloc(sizeof(BT_HDR) + SMP_RAND_CMD_SIZE +
471                                       L2CAP_MIN_OFFSET);
472
473   SMP_TRACE_EVENT("%s", __func__);
474
475   p = (uint8_t*)(p_buf + 1) + L2CAP_MIN_OFFSET;
476   UINT8_TO_STREAM(p, SMP_OPCODE_RAND);
477   ARRAY_TO_STREAM(p, p_cb->rand, BT_OCTET16_LEN);
478
479   p_buf->offset = L2CAP_MIN_OFFSET;
480   p_buf->len = SMP_RAND_CMD_SIZE;
481
482   return p_buf;
483 }
484
485 /*******************************************************************************
486  *
487  * Function         smp_build_encrypt_info_cmd
488  *
489  * Description      Build security information command.
490  *
491  ******************************************************************************/
492 static BT_HDR* smp_build_encrypt_info_cmd(UNUSED_ATTR uint8_t cmd_code,
493                                           tSMP_CB* p_cb) {
494   uint8_t* p;
495   BT_HDR* p_buf = (BT_HDR*)osi_malloc(sizeof(BT_HDR) + SMP_ENC_INFO_SIZE +
496                                       L2CAP_MIN_OFFSET);
497
498   SMP_TRACE_EVENT("%s", __func__);
499
500   p = (uint8_t*)(p_buf + 1) + L2CAP_MIN_OFFSET;
501   UINT8_TO_STREAM(p, SMP_OPCODE_ENCRYPT_INFO);
502   ARRAY_TO_STREAM(p, p_cb->ltk, BT_OCTET16_LEN);
503
504   p_buf->offset = L2CAP_MIN_OFFSET;
505   p_buf->len = SMP_ENC_INFO_SIZE;
506
507   return p_buf;
508 }
509
510 /*******************************************************************************
511  *
512  * Function         smp_build_master_id_cmd
513  *
514  * Description      Build security information command.
515  *
516  ******************************************************************************/
517 static BT_HDR* smp_build_master_id_cmd(UNUSED_ATTR uint8_t cmd_code,
518                                        tSMP_CB* p_cb) {
519   uint8_t* p;
520   BT_HDR* p_buf = (BT_HDR*)osi_malloc(sizeof(BT_HDR) + SMP_MASTER_ID_SIZE +
521                                       L2CAP_MIN_OFFSET);
522
523   SMP_TRACE_EVENT("%s", __func__);
524
525   p = (uint8_t*)(p_buf + 1) + L2CAP_MIN_OFFSET;
526   UINT8_TO_STREAM(p, SMP_OPCODE_MASTER_ID);
527   UINT16_TO_STREAM(p, p_cb->ediv);
528   ARRAY_TO_STREAM(p, p_cb->enc_rand, BT_OCTET8_LEN);
529
530   p_buf->offset = L2CAP_MIN_OFFSET;
531   p_buf->len = SMP_MASTER_ID_SIZE;
532
533   return p_buf;
534 }
535
536 /*******************************************************************************
537  *
538  * Function         smp_build_identity_info_cmd
539  *
540  * Description      Build identity information command.
541  *
542  ******************************************************************************/
543 static BT_HDR* smp_build_identity_info_cmd(UNUSED_ATTR uint8_t cmd_code,
544                                            UNUSED_ATTR tSMP_CB* p_cb) {
545   uint8_t* p;
546   BT_OCTET16 irk;
547   BT_HDR* p_buf =
548       (BT_HDR*)osi_malloc(sizeof(BT_HDR) + SMP_ID_INFO_SIZE + L2CAP_MIN_OFFSET);
549
550   SMP_TRACE_EVENT("%s", __func__);
551
552   p = (uint8_t*)(p_buf + 1) + L2CAP_MIN_OFFSET;
553
554   BTM_GetDeviceIDRoot(irk);
555
556   UINT8_TO_STREAM(p, SMP_OPCODE_IDENTITY_INFO);
557   ARRAY_TO_STREAM(p, irk, BT_OCTET16_LEN);
558
559   p_buf->offset = L2CAP_MIN_OFFSET;
560   p_buf->len = SMP_ID_INFO_SIZE;
561
562   return p_buf;
563 }
564
565 /*******************************************************************************
566  *
567  * Function         smp_build_id_addr_cmd
568  *
569  * Description      Build identity address information command.
570  *
571  ******************************************************************************/
572 static BT_HDR* smp_build_id_addr_cmd(UNUSED_ATTR uint8_t cmd_code,
573                                      UNUSED_ATTR tSMP_CB* p_cb) {
574   uint8_t* p;
575   BT_HDR* p_buf =
576       (BT_HDR*)osi_malloc(sizeof(BT_HDR) + SMP_ID_ADDR_SIZE + L2CAP_MIN_OFFSET);
577
578   SMP_TRACE_EVENT("%s", __func__);
579
580   p = (uint8_t*)(p_buf + 1) + L2CAP_MIN_OFFSET;
581   UINT8_TO_STREAM(p, SMP_OPCODE_ID_ADDR);
582   UINT8_TO_STREAM(p, 0);
583   BDADDR_TO_STREAM(p, *controller_get_interface()->get_address());
584
585   p_buf->offset = L2CAP_MIN_OFFSET;
586   p_buf->len = SMP_ID_ADDR_SIZE;
587
588   return p_buf;
589 }
590
591 /*******************************************************************************
592  *
593  * Function         smp_build_signing_info_cmd
594  *
595  * Description      Build signing information command.
596  *
597  ******************************************************************************/
598 static BT_HDR* smp_build_signing_info_cmd(UNUSED_ATTR uint8_t cmd_code,
599                                           tSMP_CB* p_cb) {
600   uint8_t* p;
601   BT_HDR* p_buf = (BT_HDR*)osi_malloc(sizeof(BT_HDR) + SMP_SIGN_INFO_SIZE +
602                                       L2CAP_MIN_OFFSET);
603
604   SMP_TRACE_EVENT("%s", __func__);
605
606   p = (uint8_t*)(p_buf + 1) + L2CAP_MIN_OFFSET;
607   UINT8_TO_STREAM(p, SMP_OPCODE_SIGN_INFO);
608   ARRAY_TO_STREAM(p, p_cb->csrk, BT_OCTET16_LEN);
609
610   p_buf->offset = L2CAP_MIN_OFFSET;
611   p_buf->len = SMP_SIGN_INFO_SIZE;
612
613   return p_buf;
614 }
615
616 /*******************************************************************************
617  *
618  * Function         smp_build_pairing_fail
619  *
620  * Description      Build Pairing Fail command.
621  *
622  ******************************************************************************/
623 static BT_HDR* smp_build_pairing_fail(UNUSED_ATTR uint8_t cmd_code,
624                                       tSMP_CB* p_cb) {
625   uint8_t* p;
626   BT_HDR* p_buf = (BT_HDR*)osi_malloc(sizeof(BT_HDR) + SMP_PAIR_FAIL_SIZE +
627                                       L2CAP_MIN_OFFSET);
628
629   SMP_TRACE_EVENT("%s", __func__);
630
631   p = (uint8_t*)(p_buf + 1) + L2CAP_MIN_OFFSET;
632   UINT8_TO_STREAM(p, SMP_OPCODE_PAIRING_FAILED);
633   UINT8_TO_STREAM(p, p_cb->failure);
634
635   p_buf->offset = L2CAP_MIN_OFFSET;
636   p_buf->len = SMP_PAIR_FAIL_SIZE;
637
638   return p_buf;
639 }
640
641 /*******************************************************************************
642  *
643  * Function         smp_build_security_request
644  *
645  * Description      Build security request command.
646  *
647  ******************************************************************************/
648 static BT_HDR* smp_build_security_request(UNUSED_ATTR uint8_t cmd_code,
649                                           tSMP_CB* p_cb) {
650   uint8_t* p;
651   BT_HDR* p_buf = (BT_HDR*)osi_malloc(sizeof(BT_HDR) + 2 + L2CAP_MIN_OFFSET);
652
653   SMP_TRACE_EVENT("%s", __func__);
654
655   p = (uint8_t*)(p_buf + 1) + L2CAP_MIN_OFFSET;
656   UINT8_TO_STREAM(p, SMP_OPCODE_SEC_REQ);
657   UINT8_TO_STREAM(p, p_cb->loc_auth_req);
658
659   p_buf->offset = L2CAP_MIN_OFFSET;
660   p_buf->len = SMP_SECURITY_REQUEST_SIZE;
661
662   SMP_TRACE_EVENT("opcode=%d auth_req=0x%x", SMP_OPCODE_SEC_REQ,
663                   p_cb->loc_auth_req);
664
665   return p_buf;
666 }
667
668 /*******************************************************************************
669  *
670  * Function         smp_build_pair_public_key_cmd
671  *
672  * Description      Build pairing public key command.
673  *
674  ******************************************************************************/
675 static BT_HDR* smp_build_pair_public_key_cmd(UNUSED_ATTR uint8_t cmd_code,
676                                              tSMP_CB* p_cb) {
677   uint8_t* p;
678   uint8_t publ_key[2 * BT_OCTET32_LEN];
679   uint8_t* p_publ_key = publ_key;
680   BT_HDR* p_buf = (BT_HDR*)osi_malloc(sizeof(BT_HDR) + SMP_PAIR_PUBL_KEY_SIZE +
681                                       L2CAP_MIN_OFFSET);
682
683   SMP_TRACE_EVENT("%s", __func__);
684
685   memcpy(p_publ_key, p_cb->loc_publ_key.x, BT_OCTET32_LEN);
686   memcpy(p_publ_key + BT_OCTET32_LEN, p_cb->loc_publ_key.y, BT_OCTET32_LEN);
687
688   p = (uint8_t*)(p_buf + 1) + L2CAP_MIN_OFFSET;
689   UINT8_TO_STREAM(p, SMP_OPCODE_PAIR_PUBLIC_KEY);
690   ARRAY_TO_STREAM(p, p_publ_key, 2 * BT_OCTET32_LEN);
691
692   p_buf->offset = L2CAP_MIN_OFFSET;
693   p_buf->len = SMP_PAIR_PUBL_KEY_SIZE;
694
695   return p_buf;
696 }
697
698 /*******************************************************************************
699  *
700  * Function         smp_build_pairing_commitment_cmd
701  *
702  * Description      Build pairing commitment command.
703  *
704  ******************************************************************************/
705 static BT_HDR* smp_build_pairing_commitment_cmd(UNUSED_ATTR uint8_t cmd_code,
706                                                 tSMP_CB* p_cb) {
707   uint8_t* p;
708   BT_HDR* p_buf = (BT_HDR*)osi_malloc(sizeof(BT_HDR) + SMP_PAIR_COMMITM_SIZE +
709                                       L2CAP_MIN_OFFSET);
710
711   SMP_TRACE_EVENT("%s", __func__);
712
713   p = (uint8_t*)(p_buf + 1) + L2CAP_MIN_OFFSET;
714   UINT8_TO_STREAM(p, SMP_OPCODE_CONFIRM);
715   ARRAY_TO_STREAM(p, p_cb->commitment, BT_OCTET16_LEN);
716
717   p_buf->offset = L2CAP_MIN_OFFSET;
718   p_buf->len = SMP_PAIR_COMMITM_SIZE;
719
720   return p_buf;
721 }
722
723 /*******************************************************************************
724  *
725  * Function         smp_build_pair_dhkey_check_cmd
726  *
727  * Description      Build pairing DHKey check command.
728  *
729  ******************************************************************************/
730 static BT_HDR* smp_build_pair_dhkey_check_cmd(UNUSED_ATTR uint8_t cmd_code,
731                                               tSMP_CB* p_cb) {
732   uint8_t* p;
733   BT_HDR* p_buf = (BT_HDR*)osi_malloc(
734       sizeof(BT_HDR) + SMP_PAIR_DHKEY_CHECK_SIZE + L2CAP_MIN_OFFSET);
735
736   SMP_TRACE_EVENT("%s", __func__);
737
738   p = (uint8_t*)(p_buf + 1) + L2CAP_MIN_OFFSET;
739   UINT8_TO_STREAM(p, SMP_OPCODE_PAIR_DHKEY_CHECK);
740   ARRAY_TO_STREAM(p, p_cb->dhkey_check, BT_OCTET16_LEN);
741
742   p_buf->offset = L2CAP_MIN_OFFSET;
743   p_buf->len = SMP_PAIR_DHKEY_CHECK_SIZE;
744
745   return p_buf;
746 }
747
748 /*******************************************************************************
749  *
750  * Function         smp_build_pairing_keypress_notification_cmd
751  *
752  * Description      Build keypress notification command.
753  *
754  ******************************************************************************/
755 static BT_HDR* smp_build_pairing_keypress_notification_cmd(
756     UNUSED_ATTR uint8_t cmd_code, tSMP_CB* p_cb) {
757   uint8_t* p;
758   BT_HDR* p_buf = (BT_HDR*)osi_malloc(
759       sizeof(BT_HDR) + SMP_PAIR_KEYPR_NOTIF_SIZE + L2CAP_MIN_OFFSET);
760
761   SMP_TRACE_EVENT("%s", __func__);
762
763   p = (uint8_t*)(p_buf + 1) + L2CAP_MIN_OFFSET;
764   UINT8_TO_STREAM(p, SMP_OPCODE_PAIR_KEYPR_NOTIF);
765   UINT8_TO_STREAM(p, p_cb->local_keypress_notification);
766
767   p_buf->offset = L2CAP_MIN_OFFSET;
768   p_buf->len = SMP_PAIR_KEYPR_NOTIF_SIZE;
769
770   return p_buf;
771 }
772
773 /*******************************************************************************
774  *
775  * Function         smp_convert_string_to_tk
776  *
777  * Description      This function is called to convert a 6 to 16 digits numeric
778  *                  character string into SMP TK.
779  *
780  *
781  * Returns          void
782  *
783  ******************************************************************************/
784 void smp_convert_string_to_tk(BT_OCTET16 tk, uint32_t passkey) {
785   uint8_t* p = tk;
786   tSMP_KEY key;
787   SMP_TRACE_EVENT("smp_convert_string_to_tk");
788   UINT32_TO_STREAM(p, passkey);
789
790   key.key_type = SMP_KEY_TYPE_TK;
791   key.p_data = tk;
792
793   tSMP_INT_DATA smp_int_data;
794   smp_int_data.key = key;
795   smp_sm_event(&smp_cb, SMP_KEY_READY_EVT, &smp_int_data);
796 }
797
798 /*******************************************************************************
799  *
800  * Function         smp_mask_enc_key
801  *
802  * Description      This function is called to mask off the encryption key based
803  *                  on the maximum encryption key size.
804  *
805  *
806  * Returns          void
807  *
808  ******************************************************************************/
809 void smp_mask_enc_key(uint8_t loc_enc_size, uint8_t* p_data) {
810   SMP_TRACE_EVENT("smp_mask_enc_key");
811   if (loc_enc_size < BT_OCTET16_LEN) {
812     for (; loc_enc_size < BT_OCTET16_LEN; loc_enc_size++)
813       *(p_data + loc_enc_size) = 0;
814   }
815   return;
816 }
817
818 /*******************************************************************************
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  ******************************************************************************/
828 void smp_xor_128(BT_OCTET16 a, BT_OCTET16 b) {
829   uint8_t i, *aa = a, *bb = b;
830
831   SMP_TRACE_EVENT("smp_xor_128");
832   for (i = 0; i < BT_OCTET16_LEN; i++) {
833     aa[i] = aa[i] ^ bb[i];
834   }
835 }
836
837 /*******************************************************************************
838  *
839  * Function         smp_cb_cleanup
840  *
841  * Description      Clean up SMP control block
842  *
843  * Returns          void
844  *
845  ******************************************************************************/
846 void smp_cb_cleanup(tSMP_CB* p_cb) {
847   tSMP_CALLBACK* p_callback = p_cb->p_callback;
848   uint8_t trace_level = p_cb->trace_level;
849   alarm_t* smp_rsp_timer_ent = p_cb->smp_rsp_timer_ent;
850   alarm_t* delayed_auth_timer_ent = p_cb->delayed_auth_timer_ent;
851
852   SMP_TRACE_EVENT("smp_cb_cleanup");
853
854   alarm_cancel(p_cb->smp_rsp_timer_ent);
855   alarm_cancel(p_cb->delayed_auth_timer_ent);
856   memset(p_cb, 0, sizeof(tSMP_CB));
857   p_cb->p_callback = p_callback;
858   p_cb->trace_level = trace_level;
859   p_cb->smp_rsp_timer_ent = smp_rsp_timer_ent;
860   p_cb->delayed_auth_timer_ent = delayed_auth_timer_ent;
861 }
862
863 /*******************************************************************************
864  *
865  * Function         smp_remove_fixed_channel
866  *
867  * Description      This function is called to remove the fixed channel
868  *
869  * Returns          void
870  *
871  ******************************************************************************/
872 void smp_remove_fixed_channel(tSMP_CB* p_cb) {
873   SMP_TRACE_DEBUG("%s", __func__);
874
875   if (p_cb->smp_over_br)
876     L2CA_RemoveFixedChnl(L2CAP_SMP_BR_CID, p_cb->pairing_bda);
877   else
878     L2CA_RemoveFixedChnl(L2CAP_SMP_CID, p_cb->pairing_bda);
879 }
880
881 /*******************************************************************************
882  *
883  * Function         smp_reset_control_value
884  *
885  * Description      This function is called to reset the control block value
886  *                  when the pairing procedure finished.
887  *
888  *
889  * Returns          void
890  *
891  ******************************************************************************/
892 void smp_reset_control_value(tSMP_CB* p_cb) {
893   SMP_TRACE_EVENT("%s", __func__);
894
895   alarm_cancel(p_cb->smp_rsp_timer_ent);
896   p_cb->flags = 0;
897   /* set the link idle timer to drop the link when pairing is done
898      usually service discovery will follow authentication complete, to avoid
899      racing condition for a link down/up, set link idle timer to be
900      SMP_LINK_TOUT_MIN to guarantee SMP key exchange */
901   L2CA_SetIdleTimeoutByBdAddr(p_cb->pairing_bda, SMP_LINK_TOUT_MIN,
902                               BT_TRANSPORT_LE);
903
904   /* We can tell L2CAP to remove the fixed channel (if it has one) */
905   smp_remove_fixed_channel(p_cb);
906   smp_cb_cleanup(p_cb);
907 }
908
909 /*******************************************************************************
910  *
911  * Function         smp_proc_pairing_cmpl
912  *
913  * Description      This function is called to process pairing complete
914  *
915  *
916  * Returns          void
917  *
918  ******************************************************************************/
919 void smp_proc_pairing_cmpl(tSMP_CB* p_cb) {
920   tSMP_EVT_DATA evt_data = {0};
921   tSMP_CALLBACK* p_callback = p_cb->p_callback;
922
923   SMP_TRACE_DEBUG("smp_proc_pairing_cmpl ");
924
925   evt_data.cmplt.reason = p_cb->status;
926   evt_data.cmplt.smp_over_br = p_cb->smp_over_br;
927
928   if (p_cb->status == SMP_SUCCESS) evt_data.cmplt.sec_level = p_cb->sec_level;
929
930   evt_data.cmplt.is_pair_cancel = false;
931
932   if (p_cb->is_pair_cancel) evt_data.cmplt.is_pair_cancel = true;
933
934   SMP_TRACE_DEBUG("send SMP_COMPLT_EVT reason=0x%0x sec_level=0x%0x",
935                   evt_data.cmplt.reason, evt_data.cmplt.sec_level);
936
937   RawAddress pairing_bda = p_cb->pairing_bda;
938
939   smp_reset_control_value(p_cb);
940
941   if (p_callback) (*p_callback)(SMP_COMPLT_EVT, pairing_bda, &evt_data);
942 }
943
944 /*******************************************************************************
945  *
946  * Function         smp_command_has_invalid_parameters
947  *
948  * Description      Checks if the received SMP command has invalid parameters
949  *                  i.e. if the command length is valid and the command
950  *                  parameters are inside specified range.
951  *                  It returns true if the command has invalid parameters.
952  *
953  * Returns          true if the command has invalid parameters, false otherwise.
954  *
955  ******************************************************************************/
956 bool smp_command_has_invalid_parameters(tSMP_CB* p_cb) {
957   uint8_t cmd_code = p_cb->rcvd_cmd_code;
958
959   if ((cmd_code > (SMP_OPCODE_MAX + 1 /* for SMP_OPCODE_PAIR_COMMITM */)) ||
960       (cmd_code < SMP_OPCODE_MIN)) {
961     SMP_TRACE_WARNING("%s: Received command with RESERVED code 0x%02x",
962                       __func__, cmd_code);
963     return true;
964   }
965
966   if (!(*smp_cmd_len_is_valid[cmd_code])(p_cb)) {
967     SMP_TRACE_WARNING("%s: Command length not valid for cmd_code 0x%02x",
968                       __func__, cmd_code);
969     return true;
970   }
971
972   if (!(*smp_cmd_param_ranges_are_valid[cmd_code])(p_cb)) {
973     SMP_TRACE_WARNING("%s: Parameter ranges not valid code 0x%02x", __func__,
974                       cmd_code);
975     return true;
976   }
977
978   return false;
979 }
980
981 /*******************************************************************************
982  *
983  * Function         smp_command_has_valid_fixed_length
984  *
985  * Description      Checks if the received command size is equal to the size
986  *                  according to specs.
987  *
988  * Returns          true if the command size is as expected, false otherwise.
989  *
990  * Note             The command is expected to have fixed length.
991  ******************************************************************************/
992 bool smp_command_has_valid_fixed_length(tSMP_CB* p_cb) {
993   uint8_t cmd_code = p_cb->rcvd_cmd_code;
994
995   SMP_TRACE_DEBUG("%s for cmd code 0x%02x", __func__, cmd_code);
996
997   if (p_cb->rcvd_cmd_len != smp_cmd_size_per_spec[cmd_code]) {
998     SMP_TRACE_WARNING(
999         "Rcvd from the peer cmd 0x%02x with invalid length "
1000         "0x%02x (per spec the length is 0x%02x).",
1001         cmd_code, p_cb->rcvd_cmd_len, smp_cmd_size_per_spec[cmd_code]);
1002     return false;
1003   }
1004
1005   return true;
1006 }
1007
1008 /*******************************************************************************
1009  *
1010  * Function         smp_pairing_request_response_parameters_are_valid
1011  *
1012  * Description      Validates parameter ranges in the received SMP command
1013  *                  pairing request or pairing response.
1014  *                  The parameters to validate:
1015  *                  IO capability,
1016  *                  OOB data flag,
1017  *                  Bonding_flags in AuthReq
1018  *                  Maximum encryption key size.
1019  *                  Returns false if at least one of these parameters is out of
1020  *                  range.
1021  *
1022  ******************************************************************************/
1023 bool smp_pairing_request_response_parameters_are_valid(tSMP_CB* p_cb) {
1024   uint8_t io_caps = p_cb->peer_io_caps;
1025   uint8_t oob_flag = p_cb->peer_oob_flag;
1026   uint8_t bond_flag =
1027       p_cb->peer_auth_req & 0x03;  // 0x03 is gen bond with appropriate mask
1028   uint8_t enc_size = p_cb->peer_enc_size;
1029
1030   SMP_TRACE_DEBUG("%s for cmd code 0x%02x", __func__, p_cb->rcvd_cmd_code);
1031
1032   if (io_caps >= BTM_IO_CAP_MAX) {
1033     SMP_TRACE_WARNING(
1034         "Rcvd from the peer cmd 0x%02x with IO Capability "
1035         "value (0x%02x) out of range).",
1036         p_cb->rcvd_cmd_code, io_caps);
1037     return false;
1038   }
1039
1040   if (!((oob_flag == SMP_OOB_NONE) || (oob_flag == SMP_OOB_PRESENT))) {
1041     SMP_TRACE_WARNING(
1042         "Rcvd from the peer cmd 0x%02x with OOB data flag value "
1043         "(0x%02x) out of range).",
1044         p_cb->rcvd_cmd_code, oob_flag);
1045     return false;
1046   }
1047
1048   if (!((bond_flag == SMP_AUTH_NO_BOND) || (bond_flag == SMP_AUTH_BOND))) {
1049     SMP_TRACE_WARNING(
1050         "Rcvd from the peer cmd 0x%02x with Bonding_Flags value (0x%02x) "
1051         "out of range).",
1052         p_cb->rcvd_cmd_code, bond_flag);
1053     return false;
1054   }
1055
1056   if ((enc_size < SMP_ENCR_KEY_SIZE_MIN) ||
1057       (enc_size > SMP_ENCR_KEY_SIZE_MAX)) {
1058     SMP_TRACE_WARNING(
1059         "Rcvd from the peer cmd 0x%02x with Maximum Encryption "
1060         "Key value (0x%02x) out of range).",
1061         p_cb->rcvd_cmd_code, enc_size);
1062     return false;
1063   }
1064
1065   return true;
1066 }
1067
1068 /*******************************************************************************
1069  *
1070  * Function         smp_pairing_keypress_notification_is_valid
1071  *
1072  * Description      Validates Notification Type parameter range in the received
1073  *                  SMP command pairing keypress notification.
1074  *                  Returns false if this parameter is out of range.
1075  *
1076  ******************************************************************************/
1077 bool smp_pairing_keypress_notification_is_valid(tSMP_CB* p_cb) {
1078   tBTM_SP_KEY_TYPE keypress_notification = p_cb->peer_keypress_notification;
1079
1080   SMP_TRACE_DEBUG("%s for cmd code 0x%02x", __func__, p_cb->rcvd_cmd_code);
1081
1082   if (keypress_notification >= BTM_SP_KEY_OUT_OF_RANGE) {
1083     SMP_TRACE_WARNING(
1084         "Rcvd from the peer cmd 0x%02x with Pairing Keypress "
1085         "Notification value (0x%02x) out of range).",
1086         p_cb->rcvd_cmd_code, keypress_notification);
1087     return false;
1088   }
1089
1090   return true;
1091 }
1092
1093 /*******************************************************************************
1094  *
1095  * Function         smp_parameter_unconditionally_valid
1096  *
1097  * Description      Always returns true.
1098  *
1099  ******************************************************************************/
1100 bool smp_parameter_unconditionally_valid(UNUSED_ATTR tSMP_CB* p_cb) {
1101   return true;
1102 }
1103
1104 /*******************************************************************************
1105  *
1106  * Function         smp_parameter_unconditionally_invalid
1107  *
1108  * Description      Always returns false.
1109  *
1110  ******************************************************************************/
1111 bool smp_parameter_unconditionally_invalid(UNUSED_ATTR tSMP_CB* p_cb) {
1112   return false;
1113 }
1114
1115 /*******************************************************************************
1116  *
1117  * Function         smp_reject_unexpected_pairing_command
1118  *
1119  * Description      send pairing failure to an unexpected pairing command during
1120  *                  an active pairing process.
1121  *
1122  * Returns          void
1123  *
1124  ******************************************************************************/
1125 void smp_reject_unexpected_pairing_command(const RawAddress& bd_addr) {
1126   uint8_t* p;
1127   BT_HDR* p_buf = (BT_HDR*)osi_malloc(sizeof(BT_HDR) + SMP_PAIR_FAIL_SIZE +
1128                                       L2CAP_MIN_OFFSET);
1129
1130   SMP_TRACE_DEBUG("%s", __func__);
1131
1132   p = (uint8_t*)(p_buf + 1) + L2CAP_MIN_OFFSET;
1133   UINT8_TO_STREAM(p, SMP_OPCODE_PAIRING_FAILED);
1134   UINT8_TO_STREAM(p, SMP_PAIR_NOT_SUPPORT);
1135
1136   p_buf->offset = L2CAP_MIN_OFFSET;
1137   p_buf->len = SMP_PAIR_FAIL_SIZE;
1138
1139   smp_send_msg_to_L2CAP(bd_addr, p_buf);
1140 }
1141
1142 /*******************************************************************************
1143  * Function         smp_select_association_model
1144  *
1145  * Description      This function selects association model to use for STK
1146  *                  generation. Selection is based on both sides' io capability,
1147  *                  oob data flag and authentication request.
1148  *
1149  * Note             If Secure Connections Only mode is required locally then we
1150  *                  come to this point only if both sides support Secure
1151  *                  Connections mode, i.e.
1152  *                  if p_cb->secure_connections_only_mode_required = true
1153  *                  then we come to this point only if
1154  *                      (p_cb->peer_auth_req & SMP_SC_SUPPORT_BIT) ==
1155  *                      (p_cb->loc_auth_req & SMP_SC_SUPPORT_BIT) ==
1156  *                      SMP_SC_SUPPORT_BIT
1157  *
1158  ******************************************************************************/
1159 tSMP_ASSO_MODEL smp_select_association_model(tSMP_CB* p_cb) {
1160   tSMP_ASSO_MODEL model = SMP_MODEL_OUT_OF_RANGE;
1161   p_cb->le_secure_connections_mode_is_used = false;
1162
1163   SMP_TRACE_EVENT("%s", __func__);
1164   SMP_TRACE_DEBUG("%s p_cb->peer_io_caps = %d p_cb->local_io_capability = %d",
1165                   __func__, p_cb->peer_io_caps, p_cb->local_io_capability);
1166   SMP_TRACE_DEBUG("%s p_cb->peer_oob_flag = %d p_cb->loc_oob_flag = %d",
1167                   __func__, p_cb->peer_oob_flag, p_cb->loc_oob_flag);
1168   SMP_TRACE_DEBUG("%s p_cb->peer_auth_req = 0x%02x p_cb->loc_auth_req = 0x%02x",
1169                   __func__, p_cb->peer_auth_req, p_cb->loc_auth_req);
1170   SMP_TRACE_DEBUG(
1171       "%s p_cb->secure_connections_only_mode_required = %s", __func__,
1172       p_cb->secure_connections_only_mode_required ? "true" : "false");
1173
1174   if ((p_cb->peer_auth_req & SMP_SC_SUPPORT_BIT) &&
1175       (p_cb->loc_auth_req & SMP_SC_SUPPORT_BIT)) {
1176     p_cb->le_secure_connections_mode_is_used = true;
1177   }
1178
1179   if ((p_cb->peer_auth_req & SMP_H7_SUPPORT_BIT) &&
1180       (p_cb->loc_auth_req & SMP_H7_SUPPORT_BIT)) {
1181     p_cb->key_derivation_h7_used = TRUE;
1182   }
1183
1184   SMP_TRACE_DEBUG("use_sc_process = %d, h7 use = %d",
1185                   p_cb->le_secure_connections_mode_is_used,
1186                   p_cb->key_derivation_h7_used);
1187
1188   if (p_cb->le_secure_connections_mode_is_used) {
1189     model = smp_select_association_model_secure_connections(p_cb);
1190   } else {
1191     model = smp_select_legacy_association_model(p_cb);
1192   }
1193   return model;
1194 }
1195
1196 /*******************************************************************************
1197  * Function         smp_select_legacy_association_model
1198  *
1199  * Description      This function is called to select association mode if at
1200  *                  least one side doesn't support secure connections.
1201  *
1202  ******************************************************************************/
1203 tSMP_ASSO_MODEL smp_select_legacy_association_model(tSMP_CB* p_cb) {
1204   tSMP_ASSO_MODEL model = SMP_MODEL_OUT_OF_RANGE;
1205
1206   SMP_TRACE_DEBUG("%s", __func__);
1207   /* if OOB data is present on both devices, then use OOB association model */
1208   if (p_cb->peer_oob_flag == SMP_OOB_PRESENT &&
1209       p_cb->loc_oob_flag == SMP_OOB_PRESENT)
1210     return SMP_MODEL_OOB;
1211
1212   /* else if neither device requires MITM, then use Just Works association model
1213    */
1214   if (SMP_NO_MITM_REQUIRED(p_cb->peer_auth_req) &&
1215       SMP_NO_MITM_REQUIRED(p_cb->loc_auth_req))
1216     return SMP_MODEL_ENCRYPTION_ONLY;
1217
1218   /* otherwise use IO capability to select association model */
1219   if (p_cb->peer_io_caps < SMP_IO_CAP_MAX &&
1220       p_cb->local_io_capability < SMP_IO_CAP_MAX) {
1221     if (p_cb->role == HCI_ROLE_MASTER) {
1222       model = smp_association_table[p_cb->role][p_cb->peer_io_caps]
1223                                    [p_cb->local_io_capability];
1224     } else {
1225       model = smp_association_table[p_cb->role][p_cb->local_io_capability]
1226                                    [p_cb->peer_io_caps];
1227     }
1228   }
1229
1230   return model;
1231 }
1232
1233 /*******************************************************************************
1234  * Function         smp_select_association_model_secure_connections
1235  *
1236  * Description      This function is called to select association mode if both
1237  *                  sides support secure connections.
1238  *
1239  ******************************************************************************/
1240 tSMP_ASSO_MODEL smp_select_association_model_secure_connections(tSMP_CB* p_cb) {
1241   tSMP_ASSO_MODEL model = SMP_MODEL_OUT_OF_RANGE;
1242
1243   SMP_TRACE_DEBUG("%s", __func__);
1244   /* if OOB data is present on at least one device, then use OOB association
1245    * model */
1246   if (p_cb->peer_oob_flag == SMP_OOB_PRESENT ||
1247       p_cb->loc_oob_flag == SMP_OOB_PRESENT)
1248     return SMP_MODEL_SEC_CONN_OOB;
1249
1250   /* else if neither device requires MITM, then use Just Works association model
1251    */
1252   if (SMP_NO_MITM_REQUIRED(p_cb->peer_auth_req) &&
1253       SMP_NO_MITM_REQUIRED(p_cb->loc_auth_req))
1254     return SMP_MODEL_SEC_CONN_JUSTWORKS;
1255
1256   /* otherwise use IO capability to select association model */
1257   if (p_cb->peer_io_caps < SMP_IO_CAP_MAX &&
1258       p_cb->local_io_capability < SMP_IO_CAP_MAX) {
1259     if (p_cb->role == HCI_ROLE_MASTER) {
1260       model = smp_association_table_sc[p_cb->role][p_cb->peer_io_caps]
1261                                       [p_cb->local_io_capability];
1262     } else {
1263       model = smp_association_table_sc[p_cb->role][p_cb->local_io_capability]
1264                                       [p_cb->peer_io_caps];
1265     }
1266   }
1267
1268   return model;
1269 }
1270
1271 /*******************************************************************************
1272  * Function         smp_reverse_array
1273  *
1274  * Description      This function reverses array bytes
1275  *
1276  ******************************************************************************/
1277 void smp_reverse_array(uint8_t* arr, uint8_t len) {
1278   uint8_t i = 0, tmp;
1279
1280   SMP_TRACE_DEBUG("smp_reverse_array");
1281
1282   for (i = 0; i < len / 2; i++) {
1283     tmp = arr[i];
1284     arr[i] = arr[len - 1 - i];
1285     arr[len - 1 - i] = tmp;
1286   }
1287 }
1288
1289 /*******************************************************************************
1290  * Function         smp_calculate_random_input
1291  *
1292  * Description      This function returns random input value to be used in
1293  *                  commitment calculation for SC passkey entry association mode
1294  *                  (if bit["round"] in "random" array == 1 then returns 0x81
1295  *                   else returns 0x80).
1296  *
1297  * Returns          ri value
1298  *
1299  ******************************************************************************/
1300 uint8_t smp_calculate_random_input(uint8_t* random, uint8_t round) {
1301   uint8_t i = round / 8;
1302   uint8_t j = round % 8;
1303   uint8_t ri;
1304
1305   SMP_TRACE_DEBUG("random: 0x%02x, round: %d, i: %d, j: %d", random[i], round,
1306                   i, j);
1307   ri = ((random[i] >> j) & 1) | 0x80;
1308   SMP_TRACE_DEBUG("%s ri=0x%02x", __func__, ri);
1309   return ri;
1310 }
1311
1312 /*******************************************************************************
1313  * Function         smp_collect_local_io_capabilities
1314  *
1315  * Description      This function puts into IOcap array local device
1316  *                  IOCapability, OOB data, AuthReq.
1317  *
1318  * Returns          void
1319  *
1320  ******************************************************************************/
1321 void smp_collect_local_io_capabilities(uint8_t* iocap, tSMP_CB* p_cb) {
1322   SMP_TRACE_DEBUG("%s", __func__);
1323
1324   iocap[0] = p_cb->local_io_capability;
1325   iocap[1] = p_cb->loc_oob_flag;
1326   iocap[2] = p_cb->loc_auth_req;
1327 }
1328
1329 /*******************************************************************************
1330  * Function         smp_collect_peer_io_capabilities
1331  *
1332  * Description      This function puts into IOcap array peer device
1333  *                  IOCapability, OOB data, AuthReq.
1334  *
1335  * Returns          void
1336  *
1337  ******************************************************************************/
1338 void smp_collect_peer_io_capabilities(uint8_t* iocap, tSMP_CB* p_cb) {
1339   SMP_TRACE_DEBUG("%s", __func__);
1340
1341   iocap[0] = p_cb->peer_io_caps;
1342   iocap[1] = p_cb->peer_oob_flag;
1343   iocap[2] = p_cb->peer_auth_req;
1344 }
1345
1346 /*******************************************************************************
1347  * Function         smp_collect_local_ble_address
1348  *
1349  * Description      Put the the local device LE address into the le_addr array:
1350  *                  le_addr[0-5] = local BD ADDR,
1351  *                  le_addr[6] = local le address type (PUBLIC/RANDOM).
1352  *
1353  * Returns          void
1354  *
1355  ******************************************************************************/
1356 void smp_collect_local_ble_address(uint8_t* le_addr, tSMP_CB* p_cb) {
1357   tBLE_ADDR_TYPE addr_type = 0;
1358   RawAddress bda;
1359   uint8_t* p = le_addr;
1360
1361   SMP_TRACE_DEBUG("%s", __func__);
1362
1363   BTM_ReadConnectionAddr(p_cb->pairing_bda, bda, &addr_type);
1364   BDADDR_TO_STREAM(p, bda);
1365   UINT8_TO_STREAM(p, addr_type);
1366 }
1367
1368 /*******************************************************************************
1369  * Function         smp_collect_peer_ble_address
1370  *
1371  * Description      Put the peer device LE addr into the le_addr array:
1372  *                  le_addr[0-5] = peer BD ADDR,
1373  *                  le_addr[6] = peer le address type (PUBLIC/RANDOM).
1374  *
1375  * Returns          void
1376  *
1377  ******************************************************************************/
1378 void smp_collect_peer_ble_address(uint8_t* le_addr, tSMP_CB* p_cb) {
1379   tBLE_ADDR_TYPE addr_type = 0;
1380   RawAddress bda;
1381   uint8_t* p = le_addr;
1382
1383   SMP_TRACE_DEBUG("%s", __func__);
1384
1385   if (!BTM_ReadRemoteConnectionAddr(p_cb->pairing_bda, bda, &addr_type)) {
1386     SMP_TRACE_ERROR(
1387         "can not collect peer le addr information for unknown device");
1388     return;
1389   }
1390
1391   BDADDR_TO_STREAM(p, bda);
1392   UINT8_TO_STREAM(p, addr_type);
1393 }
1394
1395 /*******************************************************************************
1396  * Function         smp_check_commitment
1397  *
1398  * Description      This function compares peer commitment values:
1399  *                  - expected (i.e. calculated locally),
1400  *                  - received from the peer.
1401  *
1402  * Returns          true  if the values are the same
1403  *                  false otherwise
1404  *
1405  ******************************************************************************/
1406 bool smp_check_commitment(tSMP_CB* p_cb) {
1407   BT_OCTET16 expected;
1408
1409   SMP_TRACE_DEBUG("%s", __func__);
1410
1411   smp_calculate_peer_commitment(p_cb, expected);
1412   print128(expected, (const uint8_t*)"calculated peer commitment");
1413   print128(p_cb->remote_commitment, (const uint8_t*)"received peer commitment");
1414
1415   if (memcmp(p_cb->remote_commitment, expected, BT_OCTET16_LEN)) {
1416     SMP_TRACE_WARNING("%s: Commitment check fails", __func__);
1417     return false;
1418   }
1419
1420   SMP_TRACE_DEBUG("%s: Commitment check succeeds", __func__);
1421   return true;
1422 }
1423
1424 /*******************************************************************************
1425  *
1426  * Function         smp_save_secure_connections_long_term_key
1427  *
1428  * Description      The function saves SC LTK as BLE key for future use as local
1429  *                  and/or peer key.
1430  *
1431  * Returns          void
1432  *
1433  ******************************************************************************/
1434 void smp_save_secure_connections_long_term_key(tSMP_CB* p_cb) {
1435   tBTM_LE_LENC_KEYS lle_key;
1436   tBTM_LE_PENC_KEYS ple_key;
1437
1438   SMP_TRACE_DEBUG("%s-Save LTK as local LTK key", __func__);
1439   memcpy(lle_key.ltk, p_cb->ltk, BT_OCTET16_LEN);
1440   lle_key.div = 0;
1441   lle_key.key_size = p_cb->loc_enc_size;
1442   lle_key.sec_level = p_cb->sec_level;
1443   btm_sec_save_le_key(p_cb->pairing_bda, BTM_LE_KEY_LENC,
1444                       (tBTM_LE_KEY_VALUE*)&lle_key, true);
1445
1446   SMP_TRACE_DEBUG("%s-Save LTK as peer LTK key", __func__);
1447   ple_key.ediv = 0;
1448   memset(ple_key.rand, 0, BT_OCTET8_LEN);
1449   memcpy(ple_key.ltk, p_cb->ltk, BT_OCTET16_LEN);
1450   ple_key.sec_level = p_cb->sec_level;
1451   ple_key.key_size = p_cb->loc_enc_size;
1452   btm_sec_save_le_key(p_cb->pairing_bda, BTM_LE_KEY_PENC,
1453                       (tBTM_LE_KEY_VALUE*)&ple_key, true);
1454 }
1455
1456 /*******************************************************************************
1457  *
1458  * Function         smp_calculate_f5_mackey_and_long_term_key
1459  *
1460  * Description      The function calculates MacKey and LTK and saves them in CB.
1461  *                  To calculate MacKey and LTK it calls smp_calc_f5(...).
1462  *                  MacKey is used in dhkey calculation, LTK is used to encrypt
1463  *                  the link.
1464  *
1465  * Returns          false if out of resources, true otherwise.
1466  *
1467  ******************************************************************************/
1468 bool smp_calculate_f5_mackey_and_long_term_key(tSMP_CB* p_cb) {
1469   uint8_t a[7];
1470   uint8_t b[7];
1471   uint8_t* p_na;
1472   uint8_t* p_nb;
1473
1474   SMP_TRACE_DEBUG("%s", __func__);
1475
1476   if (p_cb->role == HCI_ROLE_MASTER) {
1477     smp_collect_local_ble_address(a, p_cb);
1478     smp_collect_peer_ble_address(b, p_cb);
1479     p_na = p_cb->rand;
1480     p_nb = p_cb->rrand;
1481   } else {
1482     smp_collect_local_ble_address(b, p_cb);
1483     smp_collect_peer_ble_address(a, p_cb);
1484     p_na = p_cb->rrand;
1485     p_nb = p_cb->rand;
1486   }
1487
1488   if (!smp_calculate_f5(p_cb->dhkey, p_na, p_nb, a, b, p_cb->mac_key,
1489                         p_cb->ltk)) {
1490     SMP_TRACE_ERROR("%s failed", __func__);
1491     return false;
1492   }
1493
1494   SMP_TRACE_EVENT("%s is completed", __func__);
1495   return true;
1496 }
1497
1498 /*******************************************************************************
1499  *
1500  * Function         smp_request_oob_data
1501  *
1502  * Description      Requests application to provide OOB data.
1503  *
1504  * Returns          true - OOB data has to be provided by application
1505  *                  false - otherwise (unexpected)
1506  *
1507  ******************************************************************************/
1508 bool smp_request_oob_data(tSMP_CB* p_cb) {
1509   tSMP_OOB_DATA_TYPE req_oob_type = SMP_OOB_INVALID_TYPE;
1510
1511   SMP_TRACE_DEBUG("%s", __func__);
1512
1513   if (p_cb->peer_oob_flag == SMP_OOB_PRESENT &&
1514       p_cb->loc_oob_flag == SMP_OOB_PRESENT) {
1515     /* both local and peer rcvd data OOB */
1516     req_oob_type = SMP_OOB_BOTH;
1517   } else if (p_cb->peer_oob_flag == SMP_OOB_PRESENT) {
1518     /* peer rcvd OOB local data, local didn't receive OOB peer data */
1519     req_oob_type = SMP_OOB_LOCAL;
1520   } else if (p_cb->loc_oob_flag == SMP_OOB_PRESENT) {
1521     req_oob_type = SMP_OOB_PEER;
1522   }
1523
1524   SMP_TRACE_DEBUG("req_oob_type = %d", req_oob_type);
1525
1526   if (req_oob_type == SMP_OOB_INVALID_TYPE) return false;
1527
1528   p_cb->req_oob_type = req_oob_type;
1529   p_cb->cb_evt = SMP_SC_OOB_REQ_EVT;
1530   tSMP_INT_DATA smp_int_data;
1531   smp_int_data.req_oob_type = req_oob_type;
1532   smp_sm_event(p_cb, SMP_TK_REQ_EVT, &smp_int_data);
1533
1534   return true;
1535 }