smp_utils.cc 56 KB

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