smp_utils.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570
  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 *)GKI_getbuf(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 *)GKI_getbuf(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 *)GKI_getbuf(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 *)GKI_getbuf(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 *)GKI_getbuf(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 *)GKI_getbuf(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 *)GKI_getbuf(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 *)GKI_getbuf(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 *)GKI_getbuf(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 *)GKI_getbuf(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 *)GKI_getbuf(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 *)GKI_getbuf(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 *)GKI_getbuf(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 *)GKI_getbuf(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. smp_reset_control_value(p_cb);
  830. if (p_callback) {
  831. (*p_callback) (SMP_COMPLT_EVT, pairing_bda, &evt_data);
  832. }
  833. }
  834. /*******************************************************************************
  835. **
  836. ** Function smp_command_has_invalid_parameters
  837. **
  838. ** Description Checks if the received SMP command has invalid parameters i.e.
  839. ** if the command length is valid and the command parameters are
  840. ** inside specified range.
  841. ** It returns TRUE if the command has invalid parameters.
  842. **
  843. ** Returns TRUE if the command has invalid parameters, FALSE otherwise.
  844. **
  845. *******************************************************************************/
  846. BOOLEAN smp_command_has_invalid_parameters(tSMP_CB *p_cb)
  847. {
  848. UINT8 cmd_code = p_cb->rcvd_cmd_code;
  849. SMP_TRACE_DEBUG("%s for cmd code 0x%02x\n", __func__, cmd_code);
  850. if ((cmd_code > (SMP_OPCODE_MAX + 1 /* for SMP_OPCODE_PAIR_COMMITM */)) ||
  851. (cmd_code < SMP_OPCODE_MIN)) {
  852. SMP_TRACE_WARNING("Somehow received command with the RESERVED code 0x%02x\n", cmd_code);
  853. return TRUE;
  854. }
  855. if (!(*smp_cmd_len_is_valid[cmd_code])(p_cb)) {
  856. return TRUE;
  857. }
  858. if (!(*smp_cmd_param_ranges_are_valid[cmd_code])(p_cb)) {
  859. return TRUE;
  860. }
  861. return FALSE;
  862. }
  863. /*******************************************************************************
  864. **
  865. ** Function smp_command_has_valid_fixed_length
  866. **
  867. ** Description Checks if the received command size is equal to the size
  868. ** according to specs.
  869. **
  870. ** Returns TRUE if the command size is as expected, FALSE otherwise.
  871. **
  872. ** Note The command is expected to have fixed length.
  873. *******************************************************************************/
  874. BOOLEAN smp_command_has_valid_fixed_length(tSMP_CB *p_cb)
  875. {
  876. UINT8 cmd_code = p_cb->rcvd_cmd_code;
  877. SMP_TRACE_DEBUG("%s for cmd code 0x%02x\n", __func__, cmd_code);
  878. if (p_cb->rcvd_cmd_len != smp_cmd_size_per_spec[cmd_code]) {
  879. SMP_TRACE_WARNING("Rcvd from the peer cmd 0x%02x with invalid length\
  880. 0x%02x (per spec the length is 0x%02x).\n",
  881. cmd_code, p_cb->rcvd_cmd_len, smp_cmd_size_per_spec[cmd_code]);
  882. return FALSE;
  883. }
  884. return TRUE;
  885. }
  886. /*******************************************************************************
  887. **
  888. ** Function smp_pairing_request_response_parameters_are_valid
  889. **
  890. ** Description Validates parameter ranges in the received SMP command
  891. ** pairing request or pairing response.
  892. ** The parameters to validate:
  893. ** IO capability,
  894. ** OOB data flag,
  895. ** Bonding_flags in AuthReq
  896. ** Maximum encryption key size.
  897. ** Returns FALSE if at least one of these parameters is out of range.
  898. **
  899. *******************************************************************************/
  900. BOOLEAN smp_pairing_request_response_parameters_are_valid(tSMP_CB *p_cb)
  901. {
  902. UINT8 io_caps = p_cb->peer_io_caps;
  903. UINT8 oob_flag = p_cb->peer_oob_flag;
  904. UINT8 bond_flag = p_cb->peer_auth_req & 0x03; //0x03 is gen bond with appropriate mask
  905. UINT8 enc_size = p_cb->peer_enc_size;
  906. SMP_TRACE_DEBUG("%s for cmd code 0x%02x\n", __func__, p_cb->rcvd_cmd_code);
  907. if (io_caps >= BTM_IO_CAP_MAX) {
  908. SMP_TRACE_WARNING("Rcvd from the peer cmd 0x%02x with IO Capabilty \
  909. value (0x%02x) out of range).\n",
  910. p_cb->rcvd_cmd_code, io_caps);
  911. return FALSE;
  912. }
  913. if (!((oob_flag == SMP_OOB_NONE) || (oob_flag == SMP_OOB_PRESENT))) {
  914. SMP_TRACE_WARNING("Rcvd from the peer cmd 0x%02x with OOB data flag value \
  915. (0x%02x) out of range).\n",
  916. p_cb->rcvd_cmd_code, oob_flag);
  917. return FALSE;
  918. }
  919. if (!((bond_flag == SMP_AUTH_NO_BOND) || (bond_flag == SMP_AUTH_BOND))) {
  920. SMP_TRACE_WARNING("Rcvd from the peer cmd 0x%02x with Bonding_Flags value (0x%02x)\
  921. out of range).\n",
  922. p_cb->rcvd_cmd_code, bond_flag);
  923. return FALSE;
  924. }
  925. if ((enc_size < SMP_ENCR_KEY_SIZE_MIN) || (enc_size > SMP_ENCR_KEY_SIZE_MAX)) {
  926. SMP_TRACE_WARNING("Rcvd from the peer cmd 0x%02x with Maximum Encryption \
  927. Key value (0x%02x) out of range).\n",
  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 SMP command
  938. ** pairing keypress notification.
  939. ** Returns FALSE if this parameter is out of range.
  940. **
  941. *******************************************************************************/
  942. BOOLEAN smp_pairing_keypress_notification_is_valid(tSMP_CB *p_cb)
  943. {
  944. tBTM_SP_KEY_TYPE keypress_notification = p_cb->peer_keypress_notification;
  945. SMP_TRACE_DEBUG("%s for cmd code 0x%02x\n", __func__, p_cb->rcvd_cmd_code);
  946. if (keypress_notification >= BTM_SP_KEY_OUT_OF_RANGE) {
  947. SMP_TRACE_WARNING("Rcvd from the peer cmd 0x%02x with Pairing Keypress \
  948. Notification value (0x%02x) out of range).\n",
  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. BOOLEAN smp_parameter_unconditionally_valid(tSMP_CB *p_cb)
  962. {
  963. return TRUE;
  964. }
  965. /*******************************************************************************
  966. **
  967. ** Function smp_parameter_unconditionally_invalid
  968. **
  969. ** Description Always returns FALSE.
  970. **
  971. *******************************************************************************/
  972. BOOLEAN smp_parameter_unconditionally_invalid(tSMP_CB *p_cb)
  973. {
  974. return FALSE;
  975. }
  976. /*******************************************************************************
  977. **
  978. ** Function smp_reject_unexpected_pairing_command
  979. **
  980. ** Description send pairing failure to an unexpected pairing command during
  981. ** an active pairing process.
  982. **
  983. ** Returns void
  984. **
  985. *******************************************************************************/
  986. void smp_reject_unexpected_pairing_command(BD_ADDR bd_addr)
  987. {
  988. BT_HDR *p_buf;
  989. UINT8 *p;
  990. SMP_TRACE_DEBUG ("%s\n", __FUNCTION__);
  991. if ((p_buf = (BT_HDR *)GKI_getbuf(sizeof(BT_HDR) + \
  992. SMP_PAIR_FAIL_SIZE + L2CAP_MIN_OFFSET)) != NULL) {
  993. p = (UINT8 *)(p_buf + 1) + L2CAP_MIN_OFFSET;
  994. UINT8_TO_STREAM (p, SMP_OPCODE_PAIRING_FAILED);
  995. UINT8_TO_STREAM (p, SMP_PAIR_NOT_SUPPORT);
  996. p_buf->offset = L2CAP_MIN_OFFSET;
  997. p_buf->len = SMP_PAIR_FAIL_SIZE;
  998. smp_send_msg_to_L2CAP(bd_addr, p_buf);
  999. }
  1000. }
  1001. /*******************************************************************************
  1002. ** Function smp_select_association_model
  1003. **
  1004. ** Description This function selects association model to use for STK
  1005. ** generation. Selection is based on both sides' io capability,
  1006. ** oob data flag and authentication request.
  1007. **
  1008. ** Note If Secure Connections Only mode is required locally then we
  1009. ** come to this point only if both sides support Secure Connections
  1010. ** mode, i.e. if p_cb->secure_connections_only_mode_required = TRUE then we come
  1011. ** to this point only if
  1012. ** (p_cb->peer_auth_req & SMP_SC_SUPPORT_BIT) ==
  1013. ** (p_cb->loc_auth_req & SMP_SC_SUPPORT_BIT) ==
  1014. ** SMP_SC_SUPPORT_BIT
  1015. **
  1016. *******************************************************************************/
  1017. tSMP_ASSO_MODEL smp_select_association_model(tSMP_CB *p_cb)
  1018. {
  1019. tSMP_ASSO_MODEL model = SMP_MODEL_OUT_OF_RANGE;
  1020. p_cb->le_secure_connections_mode_is_used = FALSE;
  1021. SMP_TRACE_EVENT("%s\n", __FUNCTION__);
  1022. SMP_TRACE_DEBUG("%s p_cb->peer_io_caps = %d p_cb->local_io_capability = %d\n",
  1023. __FUNCTION__, p_cb->peer_io_caps, p_cb->local_io_capability);
  1024. SMP_TRACE_DEBUG("%s p_cb->peer_oob_flag = %d p_cb->loc_oob_flag = %d\n",
  1025. __FUNCTION__, p_cb->peer_oob_flag, p_cb->loc_oob_flag);
  1026. SMP_TRACE_DEBUG("%s p_cb->peer_auth_req = 0x%02x p_cb->loc_auth_req = 0x%02x\n",
  1027. __FUNCTION__, p_cb->peer_auth_req, p_cb->loc_auth_req);
  1028. SMP_TRACE_DEBUG("%s p_cb->secure_connections_only_mode_required = %s\n",
  1029. __FUNCTION__, p_cb->secure_connections_only_mode_required ?
  1030. "TRUE" : "FALSE");
  1031. if ((p_cb->peer_auth_req & SMP_SC_SUPPORT_BIT) && (p_cb->loc_auth_req & SMP_SC_SUPPORT_BIT)) {
  1032. p_cb->le_secure_connections_mode_is_used = TRUE;
  1033. }
  1034. SMP_TRACE_DEBUG("use_sc_process = %d\n", p_cb->le_secure_connections_mode_is_used);
  1035. if (p_cb->le_secure_connections_mode_is_used) {
  1036. model = smp_select_association_model_secure_connections(p_cb);
  1037. } else {
  1038. model = smp_select_legacy_association_model(p_cb);
  1039. }
  1040. return model;
  1041. }
  1042. /*******************************************************************************
  1043. ** Function smp_select_legacy_association_model
  1044. **
  1045. ** Description This function is called to select association mode if at least
  1046. ** one side doesn't support secure connections.
  1047. **
  1048. *******************************************************************************/
  1049. tSMP_ASSO_MODEL smp_select_legacy_association_model(tSMP_CB *p_cb)
  1050. {
  1051. tSMP_ASSO_MODEL model = SMP_MODEL_OUT_OF_RANGE;
  1052. SMP_TRACE_DEBUG("%s\n", __func__);
  1053. /* if OOB data is present on both devices, then use OOB association model */
  1054. if (p_cb->peer_oob_flag == SMP_OOB_PRESENT && p_cb->loc_oob_flag == SMP_OOB_PRESENT) {
  1055. return SMP_MODEL_OOB;
  1056. }
  1057. /* else if neither device requires MITM, then use Just Works association model */
  1058. if (SMP_NO_MITM_REQUIRED (p_cb->peer_auth_req) && SMP_NO_MITM_REQUIRED(p_cb->loc_auth_req)) {
  1059. return SMP_MODEL_ENCRYPTION_ONLY;
  1060. }
  1061. /* otherwise use IO capability to select association model */
  1062. if (p_cb->peer_io_caps < SMP_IO_CAP_MAX && p_cb->local_io_capability < SMP_IO_CAP_MAX) {
  1063. if (p_cb->role == HCI_ROLE_MASTER) {
  1064. model = smp_association_table[p_cb->role][p_cb->peer_io_caps]
  1065. [p_cb->local_io_capability];
  1066. } else {
  1067. model = smp_association_table[p_cb->role][p_cb->local_io_capability]
  1068. [p_cb->peer_io_caps];
  1069. }
  1070. }
  1071. return model;
  1072. }
  1073. /*******************************************************************************
  1074. ** Function smp_select_association_model_secure_connections
  1075. **
  1076. ** Description This function is called to select association mode if both
  1077. ** sides support secure connections.
  1078. **
  1079. *******************************************************************************/
  1080. tSMP_ASSO_MODEL smp_select_association_model_secure_connections(tSMP_CB *p_cb)
  1081. {
  1082. tSMP_ASSO_MODEL model = SMP_MODEL_OUT_OF_RANGE;
  1083. SMP_TRACE_DEBUG("%s\n", __func__);
  1084. /* if OOB data is present on at least one device, then use OOB association model */
  1085. if (p_cb->peer_oob_flag == SMP_OOB_PRESENT || p_cb->loc_oob_flag == SMP_OOB_PRESENT) {
  1086. return SMP_MODEL_SEC_CONN_OOB;
  1087. }
  1088. /* else if neither device requires MITM, then use Just Works association model */
  1089. if (SMP_NO_MITM_REQUIRED (p_cb->peer_auth_req) && SMP_NO_MITM_REQUIRED(p_cb->loc_auth_req)) {
  1090. return SMP_MODEL_SEC_CONN_JUSTWORKS;
  1091. }
  1092. /* otherwise use IO capability to select association model */
  1093. if (p_cb->peer_io_caps < SMP_IO_CAP_MAX && p_cb->local_io_capability < SMP_IO_CAP_MAX) {
  1094. if (p_cb->role == HCI_ROLE_MASTER) {
  1095. model = smp_association_table_sc[p_cb->role][p_cb->peer_io_caps]
  1096. [p_cb->local_io_capability];
  1097. } else {
  1098. model = smp_association_table_sc[p_cb->role][p_cb->local_io_capability]
  1099. [p_cb->peer_io_caps];
  1100. }
  1101. }
  1102. return model;
  1103. }
  1104. /*******************************************************************************
  1105. ** Function smp_reverse_array
  1106. **
  1107. ** Description This function reverses array bytes
  1108. **
  1109. *******************************************************************************/
  1110. void smp_reverse_array(UINT8 *arr, UINT8 len)
  1111. {
  1112. UINT8 i = 0, tmp;
  1113. SMP_TRACE_DEBUG("smp_reverse_array\n");
  1114. for (i = 0; i < len / 2; i ++) {
  1115. tmp = arr[i];
  1116. arr[i] = arr[len - 1 - i];
  1117. arr[len - 1 - i] = tmp;
  1118. }
  1119. }
  1120. /*******************************************************************************
  1121. ** Function smp_calculate_random_input
  1122. **
  1123. ** Description This function returns random input value to be used in commitment
  1124. ** calculation for SC passkey entry association mode
  1125. ** (if bit["round"] in "random" array == 1 then returns 0x81
  1126. ** else returns 0x80).
  1127. **
  1128. ** Returns ri value
  1129. **
  1130. *******************************************************************************/
  1131. UINT8 smp_calculate_random_input(UINT8 *random, UINT8 round)
  1132. {
  1133. UINT8 i = round / 8;
  1134. UINT8 j = round % 8;
  1135. UINT8 ri;
  1136. SMP_TRACE_DEBUG("random: 0x%02x, round: %d, i: %d, j: %d\n", random[i], round, i, j);
  1137. ri = ((random[i] >> j) & 1) | 0x80;
  1138. SMP_TRACE_DEBUG("%s ri=0x%02x\n", __func__, ri);
  1139. return ri;
  1140. }
  1141. /*******************************************************************************
  1142. ** Function smp_collect_local_io_capabilities
  1143. **
  1144. ** Description This function puts into IOcap array local device
  1145. ** IOCapability, OOB data, AuthReq.
  1146. **
  1147. ** Returns void
  1148. **
  1149. *******************************************************************************/
  1150. void smp_collect_local_io_capabilities(UINT8 *iocap, tSMP_CB *p_cb)
  1151. {
  1152. SMP_TRACE_DEBUG("%s\n", __func__);
  1153. iocap[0] = p_cb->local_io_capability;
  1154. iocap[1] = p_cb->loc_oob_flag;
  1155. iocap[2] = p_cb->loc_auth_req;
  1156. }
  1157. /*******************************************************************************
  1158. ** Function smp_collect_peer_io_capabilities
  1159. **
  1160. ** Description This function puts into IOcap array peer device
  1161. ** IOCapability, OOB data, AuthReq.
  1162. **
  1163. ** Returns void
  1164. **
  1165. *******************************************************************************/
  1166. void smp_collect_peer_io_capabilities(UINT8 *iocap, tSMP_CB *p_cb)
  1167. {
  1168. SMP_TRACE_DEBUG("%s\n", __func__);
  1169. iocap[0] = p_cb->peer_io_caps;
  1170. iocap[1] = p_cb->peer_oob_flag;
  1171. iocap[2] = p_cb->peer_auth_req;
  1172. }
  1173. /*******************************************************************************
  1174. ** Function smp_collect_local_ble_address
  1175. **
  1176. ** Description This function puts into le_addr array local device le address:
  1177. ** le_addr[0-5] = local BD ADDR,
  1178. ** le_addr[6] = local le address type (PUBLIC/RANDOM).
  1179. **
  1180. ** Returns void
  1181. **
  1182. *******************************************************************************/
  1183. void smp_collect_local_ble_address(UINT8 *le_addr, tSMP_CB *p_cb)
  1184. {
  1185. tBLE_ADDR_TYPE addr_type = 0;
  1186. BD_ADDR bda;
  1187. UINT8 *p = le_addr;
  1188. SMP_TRACE_DEBUG("%s\n", __func__);
  1189. BTM_ReadConnectionAddr( p_cb->pairing_bda, bda, &addr_type);
  1190. BDADDR_TO_STREAM(p, bda);
  1191. UINT8_TO_STREAM(p, addr_type);
  1192. }
  1193. /*******************************************************************************
  1194. ** Function smp_collect_peer_ble_address
  1195. **
  1196. ** Description This function puts into le_addr array peer device le address:
  1197. ** le_addr[0-5] = peer BD ADDR,
  1198. ** le_addr[6] = peer le address type (PUBLIC/RANDOM).
  1199. **
  1200. ** Returns void
  1201. **
  1202. *******************************************************************************/
  1203. void smp_collect_peer_ble_address(UINT8 *le_addr, tSMP_CB *p_cb)
  1204. {
  1205. tBLE_ADDR_TYPE addr_type = 0;
  1206. BD_ADDR bda;
  1207. UINT8 *p = le_addr;
  1208. SMP_TRACE_DEBUG("%s\n", __func__);
  1209. if (!BTM_ReadRemoteConnectionAddr(p_cb->pairing_bda, bda, &addr_type)) {
  1210. SMP_TRACE_ERROR("can not collect peer le addr information for unknown device\n");
  1211. return;
  1212. }
  1213. BDADDR_TO_STREAM(p, bda);
  1214. UINT8_TO_STREAM(p, addr_type);
  1215. }
  1216. /*******************************************************************************
  1217. ** Function smp_check_commitment
  1218. **
  1219. ** Description This function compares peer commitment values:
  1220. ** - expected (i.e. calculated locally),
  1221. ** - received from the peer.
  1222. **
  1223. ** Returns TRUE if the values are the same
  1224. ** FALSE otherwise
  1225. **
  1226. *******************************************************************************/
  1227. BOOLEAN smp_check_commitment(tSMP_CB *p_cb)
  1228. {
  1229. BT_OCTET16 expected;
  1230. SMP_TRACE_DEBUG("%s\n", __func__);
  1231. smp_calculate_peer_commitment(p_cb, expected);
  1232. print128(expected, (const UINT8 *)"calculated peer commitment");
  1233. print128(p_cb->remote_commitment, (const UINT8 *)"received peer commitment");
  1234. if (memcmp(p_cb->remote_commitment, expected, BT_OCTET16_LEN)) {
  1235. SMP_TRACE_WARNING("Commitment check fails\n");
  1236. return FALSE;
  1237. }
  1238. SMP_TRACE_DEBUG("Commitment check succeeds\n");
  1239. return TRUE;
  1240. }
  1241. /*******************************************************************************
  1242. **
  1243. ** Function smp_save_secure_connections_long_term_key
  1244. **
  1245. ** Description The function saves SC LTK as BLE key for future use as local
  1246. ** and/or peer key.
  1247. **
  1248. ** Returns void
  1249. **
  1250. *******************************************************************************/
  1251. void smp_save_secure_connections_long_term_key(tSMP_CB *p_cb)
  1252. {
  1253. tBTM_LE_LENC_KEYS lle_key;
  1254. tBTM_LE_PENC_KEYS ple_key;
  1255. SMP_TRACE_DEBUG("%s-Save LTK as local LTK key\n", __func__);
  1256. memcpy(lle_key.ltk, p_cb->ltk, BT_OCTET16_LEN);
  1257. lle_key.div = 0;
  1258. lle_key.key_size = p_cb->loc_enc_size;
  1259. lle_key.sec_level = p_cb->sec_level;
  1260. btm_sec_save_le_key(p_cb->pairing_bda, BTM_LE_KEY_LENC, (tBTM_LE_KEY_VALUE *)&lle_key, TRUE);
  1261. SMP_TRACE_DEBUG("%s-Save LTK as peer LTK key\n", __func__);
  1262. ple_key.ediv = 0;
  1263. memset(ple_key.rand, 0, BT_OCTET8_LEN);
  1264. memcpy(ple_key.ltk, p_cb->ltk, BT_OCTET16_LEN);
  1265. ple_key.sec_level = p_cb->sec_level;
  1266. ple_key.key_size = p_cb->loc_enc_size;
  1267. btm_sec_save_le_key(p_cb->pairing_bda, BTM_LE_KEY_PENC, (tBTM_LE_KEY_VALUE *)&ple_key, TRUE);
  1268. }
  1269. /*******************************************************************************
  1270. **
  1271. ** Function smp_calculate_f5_mackey_and_long_term_key
  1272. **
  1273. ** Description The function calculates MacKey and LTK and saves them in CB.
  1274. ** To calculate MacKey and LTK it calls smp_calc_f5(...).
  1275. ** MacKey is used in dhkey calculation, LTK is used to encrypt
  1276. ** the link.
  1277. **
  1278. ** Returns FALSE if out of resources, TRUE otherwise.
  1279. **
  1280. *******************************************************************************/
  1281. BOOLEAN smp_calculate_f5_mackey_and_long_term_key(tSMP_CB *p_cb)
  1282. {
  1283. UINT8 a[7];
  1284. UINT8 b[7];
  1285. UINT8 *p_na;
  1286. UINT8 *p_nb;
  1287. SMP_TRACE_DEBUG("%s\n", __func__);
  1288. if (p_cb->role == HCI_ROLE_MASTER) {
  1289. smp_collect_local_ble_address(a, p_cb);
  1290. smp_collect_peer_ble_address(b, p_cb);
  1291. p_na = p_cb->rand;
  1292. p_nb = p_cb->rrand;
  1293. } else {
  1294. smp_collect_local_ble_address(b, p_cb);
  1295. smp_collect_peer_ble_address(a, p_cb);
  1296. p_na = p_cb->rrand;
  1297. p_nb = p_cb->rand;
  1298. }
  1299. if (!smp_calculate_f5(p_cb->dhkey, p_na, p_nb, a, b, p_cb->mac_key, p_cb->ltk)) {
  1300. SMP_TRACE_ERROR("%s failed\n", __func__);
  1301. return FALSE;
  1302. }
  1303. SMP_TRACE_EVENT ("%s is completed\n", __func__);
  1304. return TRUE;
  1305. }
  1306. /*******************************************************************************
  1307. **
  1308. ** Function smp_request_oob_data
  1309. **
  1310. ** Description Requests application to provide OOB data.
  1311. **
  1312. ** Returns TRUE - OOB data has to be provided by application
  1313. ** FALSE - otherwise (unexpected)
  1314. **
  1315. *******************************************************************************/
  1316. BOOLEAN smp_request_oob_data(tSMP_CB *p_cb)
  1317. {
  1318. tSMP_OOB_DATA_TYPE req_oob_type = SMP_OOB_INVALID_TYPE;
  1319. SMP_TRACE_DEBUG("%s\n", __func__);
  1320. if (p_cb->peer_oob_flag == SMP_OOB_PRESENT && p_cb->loc_oob_flag == SMP_OOB_PRESENT) {
  1321. /* both local and peer rcvd data OOB */
  1322. req_oob_type = SMP_OOB_BOTH;
  1323. } else if (p_cb->peer_oob_flag == SMP_OOB_PRESENT) {
  1324. /* peer rcvd OOB local data, local didn't receive OOB peer data */
  1325. req_oob_type = SMP_OOB_LOCAL;
  1326. } else if (p_cb->loc_oob_flag == SMP_OOB_PRESENT) {
  1327. req_oob_type = SMP_OOB_PEER;
  1328. }
  1329. SMP_TRACE_DEBUG("req_oob_type = %d\n", req_oob_type);
  1330. if (req_oob_type == SMP_OOB_INVALID_TYPE) {
  1331. return FALSE;
  1332. }
  1333. p_cb->req_oob_type = req_oob_type;
  1334. p_cb->cb_evt = SMP_SC_OOB_REQ_EVT;
  1335. smp_sm_event(p_cb, SMP_TK_REQ_EVT, &req_oob_type);
  1336. return TRUE;
  1337. }
  1338. #endif