smp_utils.c 55 KB

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