smp_utils.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600
  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 "common/bt_target.h"
  24. #if SMP_INCLUDED == TRUE
  25. #include "stack/bt_types.h"
  26. //#include "bt_utils.h"
  27. #include <string.h>
  28. //#include <ctype.h>
  29. #include "stack/hcidefs.h"
  30. #include "stack/btm_ble_api.h"
  31. #include "stack/l2c_api.h"
  32. #include "l2c_int.h"
  33. #include "smp_int.h"
  34. #include "device/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. /* Identity Address Information is used in the Transport Specific Key Distribution phase to distribute
  510. its public device address or static random address. if slave using static random address is encrypted,
  511. it should distribute its static random address */
  512. if(btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type == BLE_ADDR_RANDOM && memcmp(btm_cb.ble_ctr_cb.addr_mgnt_cb.static_rand_addr, btm_cb.ble_ctr_cb.addr_mgnt_cb.private_addr,6) == 0) {
  513. UINT8_TO_STREAM (p, 0x01);
  514. BDADDR_TO_STREAM (p, btm_cb.ble_ctr_cb.addr_mgnt_cb.static_rand_addr);
  515. } else {
  516. UINT8_TO_STREAM (p, 0);
  517. BDADDR_TO_STREAM (p, controller_get_interface()->get_address()->address);
  518. }
  519. p_buf->offset = L2CAP_MIN_OFFSET;
  520. p_buf->len = SMP_ID_ADDR_SIZE;
  521. }
  522. return p_buf;
  523. }
  524. /*******************************************************************************
  525. **
  526. ** Function smp_build_signing_info_cmd
  527. **
  528. ** Description Build signing information command.
  529. **
  530. *******************************************************************************/
  531. static BT_HDR *smp_build_signing_info_cmd(UINT8 cmd_code, tSMP_CB *p_cb)
  532. {
  533. BT_HDR *p_buf = NULL ;
  534. UINT8 *p;
  535. UNUSED(cmd_code);
  536. SMP_TRACE_EVENT("smp_build_signing_info_cmd\n");
  537. if ((p_buf = (BT_HDR *)osi_malloc(sizeof(BT_HDR) + SMP_SIGN_INFO_SIZE + L2CAP_MIN_OFFSET)) != NULL) {
  538. p = (UINT8 *)(p_buf + 1) + L2CAP_MIN_OFFSET;
  539. UINT8_TO_STREAM (p, SMP_OPCODE_SIGN_INFO);
  540. ARRAY_TO_STREAM (p, p_cb->csrk, BT_OCTET16_LEN);
  541. p_buf->offset = L2CAP_MIN_OFFSET;
  542. p_buf->len = SMP_SIGN_INFO_SIZE;
  543. }
  544. return p_buf;
  545. }
  546. /*******************************************************************************
  547. **
  548. ** Function smp_build_pairing_fail
  549. **
  550. ** Description Build Pairing Fail command.
  551. **
  552. *******************************************************************************/
  553. static BT_HDR *smp_build_pairing_fail(UINT8 cmd_code, tSMP_CB *p_cb)
  554. {
  555. BT_HDR *p_buf = NULL ;
  556. UINT8 *p;
  557. UNUSED(cmd_code);
  558. SMP_TRACE_EVENT("%s\n", __func__);
  559. if ((p_buf = (BT_HDR *)osi_malloc(sizeof(BT_HDR) + SMP_PAIR_FAIL_SIZE + L2CAP_MIN_OFFSET)) != NULL) {
  560. p = (UINT8 *)(p_buf + 1) + L2CAP_MIN_OFFSET;
  561. UINT8_TO_STREAM (p, SMP_OPCODE_PAIRING_FAILED);
  562. UINT8_TO_STREAM (p, p_cb->failure);
  563. p_buf->offset = L2CAP_MIN_OFFSET;
  564. p_buf->len = SMP_PAIR_FAIL_SIZE;
  565. }
  566. return p_buf;
  567. }
  568. /*******************************************************************************
  569. **
  570. ** Function smp_build_security_request
  571. **
  572. ** Description Build security request command.
  573. **
  574. *******************************************************************************/
  575. static BT_HDR *smp_build_security_request(UINT8 cmd_code, tSMP_CB *p_cb)
  576. {
  577. BT_HDR *p_buf = NULL ;
  578. UINT8 *p;
  579. UNUSED(cmd_code);
  580. SMP_TRACE_EVENT("%s\n", __func__);
  581. if ((p_buf = (BT_HDR *)osi_malloc(sizeof(BT_HDR) + 2 + L2CAP_MIN_OFFSET)) != NULL) {
  582. p = (UINT8 *)(p_buf + 1) + L2CAP_MIN_OFFSET;
  583. UINT8_TO_STREAM (p, SMP_OPCODE_SEC_REQ);
  584. UINT8_TO_STREAM (p, p_cb->loc_auth_req);
  585. p_buf->offset = L2CAP_MIN_OFFSET;
  586. p_buf->len = SMP_SECURITY_REQUEST_SIZE;
  587. SMP_TRACE_EVENT("opcode=%d auth_req=0x%x", SMP_OPCODE_SEC_REQ, p_cb->loc_auth_req );
  588. }
  589. return p_buf;
  590. }
  591. /*******************************************************************************
  592. **
  593. ** Function smp_build_pair_public_key_cmd
  594. **
  595. ** Description Build pairing public key command.
  596. **
  597. *******************************************************************************/
  598. static BT_HDR *smp_build_pair_public_key_cmd(UINT8 cmd_code, tSMP_CB *p_cb)
  599. {
  600. BT_HDR *p_buf = NULL ;
  601. UINT8 *p;
  602. UINT8 publ_key[2 * BT_OCTET32_LEN];
  603. UINT8 *p_publ_key = publ_key;
  604. UNUSED(cmd_code);
  605. SMP_TRACE_EVENT("%s\n", __FUNCTION__);
  606. memcpy(p_publ_key, p_cb->loc_publ_key.x, BT_OCTET32_LEN);
  607. memcpy(p_publ_key + BT_OCTET32_LEN, p_cb->loc_publ_key.y, BT_OCTET32_LEN);
  608. if ((p_buf = (BT_HDR *)osi_malloc(sizeof(BT_HDR) +
  609. SMP_PAIR_PUBL_KEY_SIZE + L2CAP_MIN_OFFSET)) != NULL) {
  610. p = (UINT8 *)(p_buf + 1) + L2CAP_MIN_OFFSET;
  611. UINT8_TO_STREAM (p, SMP_OPCODE_PAIR_PUBLIC_KEY);
  612. ARRAY_TO_STREAM (p, p_publ_key, 2 * BT_OCTET32_LEN);
  613. p_buf->offset = L2CAP_MIN_OFFSET;
  614. p_buf->len = SMP_PAIR_PUBL_KEY_SIZE;
  615. }
  616. return p_buf;
  617. }
  618. /*******************************************************************************
  619. **
  620. ** Function smp_build_pairing_commitment_cmd
  621. **
  622. ** Description Build pairing commitment command.
  623. **
  624. *******************************************************************************/
  625. static BT_HDR *smp_build_pairing_commitment_cmd(UINT8 cmd_code, tSMP_CB *p_cb)
  626. {
  627. BT_HDR *p_buf = NULL;
  628. UINT8 *p;
  629. UNUSED(cmd_code);
  630. SMP_TRACE_EVENT("%s\n", __func__);
  631. if ((p_buf = (BT_HDR *)osi_malloc(sizeof(BT_HDR) + SMP_PAIR_COMMITM_SIZE + L2CAP_MIN_OFFSET))
  632. != NULL) {
  633. p = (UINT8 *)(p_buf + 1) + L2CAP_MIN_OFFSET;
  634. UINT8_TO_STREAM (p, SMP_OPCODE_CONFIRM);
  635. ARRAY_TO_STREAM (p, p_cb->commitment, BT_OCTET16_LEN);
  636. p_buf->offset = L2CAP_MIN_OFFSET;
  637. p_buf->len = SMP_PAIR_COMMITM_SIZE;
  638. }
  639. return p_buf;
  640. }
  641. /*******************************************************************************
  642. **
  643. ** Function smp_build_pair_dhkey_check_cmd
  644. **
  645. ** Description Build pairing DHKey check command.
  646. **
  647. *******************************************************************************/
  648. static BT_HDR *smp_build_pair_dhkey_check_cmd(UINT8 cmd_code, tSMP_CB *p_cb)
  649. {
  650. BT_HDR *p_buf = NULL;
  651. UINT8 *p;
  652. UNUSED(cmd_code);
  653. SMP_TRACE_EVENT("%s\n", __FUNCTION__);
  654. if ((p_buf = (BT_HDR *)osi_malloc(sizeof(BT_HDR) +
  655. SMP_PAIR_DHKEY_CHECK_SIZE + L2CAP_MIN_OFFSET)) != NULL) {
  656. p = (UINT8 *)(p_buf + 1) + L2CAP_MIN_OFFSET;
  657. UINT8_TO_STREAM (p, SMP_OPCODE_PAIR_DHKEY_CHECK);
  658. ARRAY_TO_STREAM (p, p_cb->dhkey_check, BT_OCTET16_LEN);
  659. p_buf->offset = L2CAP_MIN_OFFSET;
  660. p_buf->len = SMP_PAIR_DHKEY_CHECK_SIZE;
  661. }
  662. return p_buf;
  663. }
  664. /*******************************************************************************
  665. **
  666. ** Function smp_build_pairing_keypress_notification_cmd
  667. **
  668. ** Description Build keypress notification command.
  669. **
  670. *******************************************************************************/
  671. static BT_HDR *smp_build_pairing_keypress_notification_cmd(UINT8 cmd_code, tSMP_CB *p_cb)
  672. {
  673. BT_HDR *p_buf = NULL ;
  674. UINT8 *p;
  675. UNUSED(cmd_code);
  676. SMP_TRACE_EVENT("%s\n", __FUNCTION__);
  677. if ((p_buf = (BT_HDR *)osi_malloc(sizeof(BT_HDR)\
  678. + SMP_PAIR_KEYPR_NOTIF_SIZE + L2CAP_MIN_OFFSET)) != NULL) {
  679. p = (UINT8 *)(p_buf + 1) + L2CAP_MIN_OFFSET;
  680. UINT8_TO_STREAM (p, SMP_OPCODE_PAIR_KEYPR_NOTIF);
  681. UINT8_TO_STREAM (p, p_cb->local_keypress_notification);
  682. p_buf->offset = L2CAP_MIN_OFFSET;
  683. p_buf->len = SMP_PAIR_KEYPR_NOTIF_SIZE;
  684. }
  685. return p_buf;
  686. }
  687. /*******************************************************************************
  688. **
  689. ** Function smp_convert_string_to_tk
  690. **
  691. ** Description This function is called to convert a 6 to 16 digits numeric
  692. ** character string into SMP TK.
  693. **
  694. **
  695. ** Returns void
  696. **
  697. *******************************************************************************/
  698. void smp_convert_string_to_tk(BT_OCTET16 tk, UINT32 passkey)
  699. {
  700. UINT8 *p = tk;
  701. tSMP_KEY key;
  702. SMP_TRACE_EVENT("smp_convert_string_to_tk\n");
  703. UINT32_TO_STREAM(p, passkey);
  704. key.key_type = SMP_KEY_TYPE_TK;
  705. key.p_data = tk;
  706. smp_sm_event(&smp_cb, SMP_KEY_READY_EVT, &key);
  707. }
  708. /*******************************************************************************
  709. **
  710. ** Function smp_mask_enc_key
  711. **
  712. ** Description This function is called to mask off the encryption key based
  713. ** on the maximum encryption key size.
  714. **
  715. **
  716. ** Returns void
  717. **
  718. *******************************************************************************/
  719. void smp_mask_enc_key(UINT8 loc_enc_size, UINT8 *p_data)
  720. {
  721. SMP_TRACE_EVENT("smp_mask_enc_key\n");
  722. if (loc_enc_size < BT_OCTET16_LEN) {
  723. for (; loc_enc_size < BT_OCTET16_LEN; loc_enc_size ++) {
  724. * (p_data + loc_enc_size) = 0;
  725. }
  726. }
  727. return;
  728. }
  729. /*******************************************************************************
  730. **
  731. ** Function smp_xor_128
  732. **
  733. ** Description utility function to do an biteise exclusive-OR of two bit
  734. ** strings of the length of BT_OCTET16_LEN.
  735. **
  736. ** Returns void
  737. **
  738. *******************************************************************************/
  739. void smp_xor_128(BT_OCTET16 a, BT_OCTET16 b)
  740. {
  741. UINT8 i, *aa = a, *bb = b;
  742. SMP_TRACE_EVENT("smp_xor_128\n");
  743. for (i = 0; i < BT_OCTET16_LEN; i++) {
  744. aa[i] = aa[i] ^ bb[i];
  745. }
  746. }
  747. /*******************************************************************************
  748. **
  749. ** Function smp_cb_cleanup
  750. **
  751. ** Description Clean up SMP control block
  752. **
  753. ** Returns void
  754. **
  755. *******************************************************************************/
  756. void smp_cb_cleanup(tSMP_CB *p_cb)
  757. {
  758. tSMP_CALLBACK *p_callback = p_cb->p_callback;
  759. UINT8 trace_level = p_cb->trace_level;
  760. UINT32 static_passkey = p_cb->static_passkey;
  761. BOOLEAN use_static_passkey = p_cb->use_static_passkey;
  762. SMP_TRACE_EVENT("smp_cb_cleanup\n");
  763. memset(p_cb, 0, sizeof(tSMP_CB));
  764. p_cb->p_callback = p_callback;
  765. p_cb->trace_level = trace_level;
  766. if(use_static_passkey) {
  767. p_cb->use_static_passkey = use_static_passkey;
  768. p_cb->static_passkey = static_passkey;
  769. }
  770. }
  771. /*******************************************************************************
  772. **
  773. ** Function smp_remove_fixed_channel
  774. **
  775. ** Description This function is called to remove the fixed channel
  776. **
  777. ** Returns void
  778. **
  779. *******************************************************************************/
  780. void smp_remove_fixed_channel(tSMP_CB *p_cb)
  781. {
  782. SMP_TRACE_DEBUG("%s\n", __func__);
  783. if (p_cb->smp_over_br) {
  784. L2CA_RemoveFixedChnl (L2CAP_SMP_BR_CID, p_cb->pairing_bda);
  785. } else {
  786. L2CA_RemoveFixedChnl (L2CAP_SMP_CID, p_cb->pairing_bda);
  787. }
  788. }
  789. /*******************************************************************************
  790. **
  791. ** Function smp_reset_control_value
  792. **
  793. ** Description This function is called to reset the control block value when
  794. ** pairing procedure finished.
  795. **
  796. **
  797. ** Returns void
  798. **
  799. *******************************************************************************/
  800. void smp_reset_control_value(tSMP_CB *p_cb)
  801. {
  802. SMP_TRACE_EVENT("smp_reset_control_value\n");
  803. btu_stop_timer (&p_cb->rsp_timer_ent);
  804. p_cb->flags = 0;
  805. /* set the link idle timer to drop the link when pairing is done
  806. usually service discovery will follow authentication complete, to avoid
  807. racing condition for a link down/up, set link idle timer to be
  808. SMP_LINK_TOUT_MIN to guarantee SMP key exchange */
  809. L2CA_SetIdleTimeoutByBdAddr(p_cb->pairing_bda, SMP_LINK_TOUT_MIN, BT_TRANSPORT_LE);
  810. /* We can tell L2CAP to remove the fixed channel (if it has one) */
  811. smp_remove_fixed_channel(p_cb);
  812. smp_cb_cleanup(p_cb);
  813. }
  814. /*******************************************************************************
  815. **
  816. ** Function smp_proc_pairing_cmpl
  817. **
  818. ** Description This function is called to process pairing complete
  819. **
  820. **
  821. ** Returns void
  822. **
  823. *******************************************************************************/
  824. void smp_proc_pairing_cmpl(tSMP_CB *p_cb)
  825. {
  826. tSMP_EVT_DATA evt_data = {0};
  827. tSMP_CALLBACK *p_callback = p_cb->p_callback;
  828. BD_ADDR pairing_bda;
  829. tBTM_SEC_DEV_REC *p_rec = btm_find_dev (p_cb->pairing_bda);
  830. SMP_TRACE_DEBUG ("smp_proc_pairing_cmpl \n");
  831. evt_data.cmplt.reason = p_cb->status;
  832. evt_data.cmplt.smp_over_br = p_cb->smp_over_br;
  833. evt_data.cmplt.auth_mode = 0;
  834. if (p_cb->status == SMP_SUCCESS) {
  835. evt_data.cmplt.sec_level = p_cb->sec_level;
  836. if (p_cb->auth_mode) { // the first encryption
  837. evt_data.cmplt.auth_mode = p_cb->auth_mode;
  838. if (p_rec) {
  839. p_rec->ble.auth_mode = p_cb->auth_mode;
  840. }
  841. } else if (p_rec) {
  842. evt_data.cmplt.auth_mode = p_rec->ble.auth_mode;
  843. }
  844. }
  845. evt_data.cmplt.is_pair_cancel = FALSE;
  846. if (p_cb->is_pair_cancel) {
  847. evt_data.cmplt.is_pair_cancel = TRUE;
  848. }
  849. SMP_TRACE_DEBUG ("send SMP_COMPLT_EVT reason=0x%0x sec_level=0x%0x\n",
  850. evt_data.cmplt.reason,
  851. evt_data.cmplt.sec_level );
  852. memcpy (pairing_bda, p_cb->pairing_bda, BD_ADDR_LEN);
  853. if (p_cb->role == HCI_ROLE_SLAVE) {
  854. if(p_rec && p_rec->ble.skip_update_conn_param) {
  855. //clear flag
  856. p_rec->ble.skip_update_conn_param = false;
  857. } else {
  858. L2CA_EnableUpdateBleConnParams(p_cb->pairing_bda, TRUE);
  859. }
  860. }
  861. smp_reset_control_value(p_cb);
  862. if (p_callback) {
  863. (*p_callback) (SMP_COMPLT_EVT, pairing_bda, &evt_data);
  864. }
  865. }
  866. /*******************************************************************************
  867. **
  868. ** Function smp_command_has_invalid_parameters
  869. **
  870. ** Description Checks if the received SMP command has invalid parameters i.e.
  871. ** if the command length is valid and the command parameters are
  872. ** inside specified range.
  873. ** It returns TRUE if the command has invalid parameters.
  874. **
  875. ** Returns TRUE if the command has invalid parameters, FALSE otherwise.
  876. **
  877. *******************************************************************************/
  878. BOOLEAN smp_command_has_invalid_parameters(tSMP_CB *p_cb)
  879. {
  880. UINT8 cmd_code = p_cb->rcvd_cmd_code;
  881. SMP_TRACE_DEBUG("%s for cmd code 0x%02x\n", __func__, cmd_code);
  882. if ((cmd_code > (SMP_OPCODE_MAX + 1 /* for SMP_OPCODE_PAIR_COMMITM */)) ||
  883. (cmd_code < SMP_OPCODE_MIN)) {
  884. SMP_TRACE_WARNING("Somehow received command with the RESERVED code 0x%02x\n", cmd_code);
  885. return TRUE;
  886. }
  887. if (!(*smp_cmd_len_is_valid[cmd_code])(p_cb)) {
  888. return TRUE;
  889. }
  890. if (!(*smp_cmd_param_ranges_are_valid[cmd_code])(p_cb)) {
  891. return TRUE;
  892. }
  893. return FALSE;
  894. }
  895. /*******************************************************************************
  896. **
  897. ** Function smp_command_has_valid_fixed_length
  898. **
  899. ** Description Checks if the received command size is equal to the size
  900. ** according to specs.
  901. **
  902. ** Returns TRUE if the command size is as expected, FALSE otherwise.
  903. **
  904. ** Note The command is expected to have fixed length.
  905. *******************************************************************************/
  906. BOOLEAN smp_command_has_valid_fixed_length(tSMP_CB *p_cb)
  907. {
  908. UINT8 cmd_code = p_cb->rcvd_cmd_code;
  909. SMP_TRACE_DEBUG("%s for cmd code 0x%02x\n", __func__, cmd_code);
  910. if (p_cb->rcvd_cmd_len != smp_cmd_size_per_spec[cmd_code]) {
  911. SMP_TRACE_WARNING("Rcvd from the peer cmd 0x%02x with invalid length\
  912. 0x%02x (per spec the length is 0x%02x).\n",
  913. cmd_code, p_cb->rcvd_cmd_len, smp_cmd_size_per_spec[cmd_code]);
  914. return FALSE;
  915. }
  916. return TRUE;
  917. }
  918. /*******************************************************************************
  919. **
  920. ** Function smp_pairing_request_response_parameters_are_valid
  921. **
  922. ** Description Validates parameter ranges in the received SMP command
  923. ** pairing request or pairing response.
  924. ** The parameters to validate:
  925. ** IO capability,
  926. ** OOB data flag,
  927. ** Bonding_flags in AuthReq
  928. ** Maximum encryption key size.
  929. ** Returns FALSE if at least one of these parameters is out of range.
  930. **
  931. *******************************************************************************/
  932. BOOLEAN smp_pairing_request_response_parameters_are_valid(tSMP_CB *p_cb)
  933. {
  934. UINT8 io_caps = p_cb->peer_io_caps;
  935. UINT8 oob_flag = p_cb->peer_oob_flag;
  936. UINT8 bond_flag = p_cb->peer_auth_req & 0x03; //0x03 is gen bond with appropriate mask
  937. UINT8 enc_size = p_cb->peer_enc_size;
  938. SMP_TRACE_DEBUG("%s for cmd code 0x%02x\n", __func__, p_cb->rcvd_cmd_code);
  939. if (io_caps >= BTM_IO_CAP_MAX) {
  940. SMP_TRACE_WARNING("Rcvd from the peer cmd 0x%02x with IO Capabilty \
  941. value (0x%02x) out of range).\n",
  942. p_cb->rcvd_cmd_code, io_caps);
  943. return FALSE;
  944. }
  945. if (!((oob_flag == SMP_OOB_NONE) || (oob_flag == SMP_OOB_PRESENT))) {
  946. SMP_TRACE_WARNING("Rcvd from the peer cmd 0x%02x with OOB data flag value \
  947. (0x%02x) out of range).\n",
  948. p_cb->rcvd_cmd_code, oob_flag);
  949. return FALSE;
  950. }
  951. if (!((bond_flag == SMP_AUTH_NO_BOND) || (bond_flag == SMP_AUTH_BOND))) {
  952. SMP_TRACE_WARNING("Rcvd from the peer cmd 0x%02x with Bonding_Flags value (0x%02x)\
  953. out of range).\n",
  954. p_cb->rcvd_cmd_code, bond_flag);
  955. return FALSE;
  956. }
  957. if ((enc_size < SMP_ENCR_KEY_SIZE_MIN) || (enc_size > SMP_ENCR_KEY_SIZE_MAX)) {
  958. SMP_TRACE_WARNING("Rcvd from the peer cmd 0x%02x with Maximum Encryption \
  959. Key value (0x%02x) out of range).\n",
  960. p_cb->rcvd_cmd_code, enc_size);
  961. return FALSE;
  962. }
  963. return TRUE;
  964. }
  965. /*******************************************************************************
  966. **
  967. ** Function smp_pairing_keypress_notification_is_valid
  968. **
  969. ** Description Validates Notification Type parameter range in the received SMP command
  970. ** pairing keypress notification.
  971. ** Returns FALSE if this parameter is out of range.
  972. **
  973. *******************************************************************************/
  974. BOOLEAN smp_pairing_keypress_notification_is_valid(tSMP_CB *p_cb)
  975. {
  976. tBTM_SP_KEY_TYPE keypress_notification = p_cb->peer_keypress_notification;
  977. SMP_TRACE_DEBUG("%s for cmd code 0x%02x\n", __func__, p_cb->rcvd_cmd_code);
  978. if (keypress_notification >= BTM_SP_KEY_OUT_OF_RANGE) {
  979. SMP_TRACE_WARNING("Rcvd from the peer cmd 0x%02x with Pairing Keypress \
  980. Notification value (0x%02x) out of range).\n",
  981. p_cb->rcvd_cmd_code, keypress_notification);
  982. return FALSE;
  983. }
  984. return TRUE;
  985. }
  986. /*******************************************************************************
  987. **
  988. ** Function smp_parameter_unconditionally_valid
  989. **
  990. ** Description Always returns TRUE.
  991. **
  992. *******************************************************************************/
  993. BOOLEAN smp_parameter_unconditionally_valid(tSMP_CB *p_cb)
  994. {
  995. return TRUE;
  996. }
  997. /*******************************************************************************
  998. **
  999. ** Function smp_parameter_unconditionally_invalid
  1000. **
  1001. ** Description Always returns FALSE.
  1002. **
  1003. *******************************************************************************/
  1004. BOOLEAN smp_parameter_unconditionally_invalid(tSMP_CB *p_cb)
  1005. {
  1006. return FALSE;
  1007. }
  1008. /*******************************************************************************
  1009. **
  1010. ** Function smp_reject_unexpected_pairing_command
  1011. **
  1012. ** Description send pairing failure to an unexpected pairing command during
  1013. ** an active pairing process.
  1014. **
  1015. ** Returns void
  1016. **
  1017. *******************************************************************************/
  1018. void smp_reject_unexpected_pairing_command(BD_ADDR bd_addr)
  1019. {
  1020. BT_HDR *p_buf;
  1021. UINT8 *p;
  1022. SMP_TRACE_DEBUG ("%s\n", __FUNCTION__);
  1023. if ((p_buf = (BT_HDR *)osi_malloc(sizeof(BT_HDR) + \
  1024. SMP_PAIR_FAIL_SIZE + L2CAP_MIN_OFFSET)) != NULL) {
  1025. p = (UINT8 *)(p_buf + 1) + L2CAP_MIN_OFFSET;
  1026. UINT8_TO_STREAM (p, SMP_OPCODE_PAIRING_FAILED);
  1027. UINT8_TO_STREAM (p, SMP_PAIR_NOT_SUPPORT);
  1028. p_buf->offset = L2CAP_MIN_OFFSET;
  1029. p_buf->len = SMP_PAIR_FAIL_SIZE;
  1030. smp_send_msg_to_L2CAP(bd_addr, p_buf);
  1031. }
  1032. }
  1033. /*******************************************************************************
  1034. ** Function smp_select_association_model
  1035. **
  1036. ** Description This function selects association model to use for STK
  1037. ** generation. Selection is based on both sides' io capability,
  1038. ** oob data flag and authentication request.
  1039. **
  1040. ** Note If Secure Connections Only mode is required locally then we
  1041. ** come to this point only if both sides support Secure Connections
  1042. ** mode, i.e. if p_cb->secure_connections_only_mode_required = TRUE then we come
  1043. ** to this point only if
  1044. ** (p_cb->peer_auth_req & SMP_SC_SUPPORT_BIT) ==
  1045. ** (p_cb->loc_auth_req & SMP_SC_SUPPORT_BIT) ==
  1046. ** SMP_SC_SUPPORT_BIT
  1047. **
  1048. *******************************************************************************/
  1049. tSMP_ASSO_MODEL smp_select_association_model(tSMP_CB *p_cb)
  1050. {
  1051. tSMP_ASSO_MODEL model = SMP_MODEL_OUT_OF_RANGE;
  1052. p_cb->le_secure_connections_mode_is_used = FALSE;
  1053. SMP_TRACE_EVENT("%s\n", __FUNCTION__);
  1054. SMP_TRACE_DEBUG("%s p_cb->peer_io_caps = %d p_cb->local_io_capability = %d\n",
  1055. __FUNCTION__, p_cb->peer_io_caps, p_cb->local_io_capability);
  1056. SMP_TRACE_DEBUG("%s p_cb->peer_oob_flag = %d p_cb->loc_oob_flag = %d\n",
  1057. __FUNCTION__, p_cb->peer_oob_flag, p_cb->loc_oob_flag);
  1058. SMP_TRACE_DEBUG("%s p_cb->peer_auth_req = 0x%02x p_cb->loc_auth_req = 0x%02x\n",
  1059. __FUNCTION__, p_cb->peer_auth_req, p_cb->loc_auth_req);
  1060. SMP_TRACE_DEBUG("%s p_cb->secure_connections_only_mode_required = %s\n",
  1061. __FUNCTION__, p_cb->secure_connections_only_mode_required ?
  1062. "TRUE" : "FALSE");
  1063. if ((p_cb->peer_auth_req & SMP_SC_SUPPORT_BIT) && (p_cb->loc_auth_req & SMP_SC_SUPPORT_BIT)) {
  1064. p_cb->le_secure_connections_mode_is_used = TRUE;
  1065. }
  1066. SMP_TRACE_DEBUG("use_sc_process = %d\n", p_cb->le_secure_connections_mode_is_used);
  1067. if (p_cb->le_secure_connections_mode_is_used) {
  1068. model = smp_select_association_model_secure_connections(p_cb);
  1069. } else {
  1070. model = smp_select_legacy_association_model(p_cb);
  1071. }
  1072. return model;
  1073. }
  1074. /*******************************************************************************
  1075. ** Function smp_select_legacy_association_model
  1076. **
  1077. ** Description This function is called to select association mode if at least
  1078. ** one side doesn't support secure connections.
  1079. **
  1080. *******************************************************************************/
  1081. tSMP_ASSO_MODEL smp_select_legacy_association_model(tSMP_CB *p_cb)
  1082. {
  1083. tSMP_ASSO_MODEL model = SMP_MODEL_OUT_OF_RANGE;
  1084. SMP_TRACE_DEBUG("%s\n", __func__);
  1085. /* if OOB data is present on both devices, then use OOB association model */
  1086. if (p_cb->peer_oob_flag == SMP_OOB_PRESENT && p_cb->loc_oob_flag == SMP_OOB_PRESENT) {
  1087. return SMP_MODEL_OOB;
  1088. }
  1089. /* else if neither device requires MITM, then use Just Works association model */
  1090. if (SMP_NO_MITM_REQUIRED (p_cb->peer_auth_req) && SMP_NO_MITM_REQUIRED(p_cb->loc_auth_req)) {
  1091. return SMP_MODEL_ENCRYPTION_ONLY;
  1092. }
  1093. /* otherwise use IO capability to select association model */
  1094. if (p_cb->peer_io_caps < SMP_IO_CAP_MAX && p_cb->local_io_capability < SMP_IO_CAP_MAX) {
  1095. if (p_cb->role == HCI_ROLE_MASTER) {
  1096. model = smp_association_table[p_cb->role][p_cb->peer_io_caps]
  1097. [p_cb->local_io_capability];
  1098. } else {
  1099. model = smp_association_table[p_cb->role][p_cb->local_io_capability]
  1100. [p_cb->peer_io_caps];
  1101. }
  1102. }
  1103. return model;
  1104. }
  1105. /*******************************************************************************
  1106. ** Function smp_select_association_model_secure_connections
  1107. **
  1108. ** Description This function is called to select association mode if both
  1109. ** sides support secure connections.
  1110. **
  1111. *******************************************************************************/
  1112. tSMP_ASSO_MODEL smp_select_association_model_secure_connections(tSMP_CB *p_cb)
  1113. {
  1114. tSMP_ASSO_MODEL model = SMP_MODEL_OUT_OF_RANGE;
  1115. SMP_TRACE_DEBUG("%s\n", __func__);
  1116. /* if OOB data is present on at least one device, then use OOB association model */
  1117. if (p_cb->peer_oob_flag == SMP_OOB_PRESENT || p_cb->loc_oob_flag == SMP_OOB_PRESENT) {
  1118. return SMP_MODEL_SEC_CONN_OOB;
  1119. }
  1120. /* else if neither device requires MITM, then use Just Works association model */
  1121. if (SMP_NO_MITM_REQUIRED (p_cb->peer_auth_req) && SMP_NO_MITM_REQUIRED(p_cb->loc_auth_req)) {
  1122. return SMP_MODEL_SEC_CONN_JUSTWORKS;
  1123. }
  1124. /* otherwise use IO capability to select association model */
  1125. if (p_cb->peer_io_caps < SMP_IO_CAP_MAX && p_cb->local_io_capability < SMP_IO_CAP_MAX) {
  1126. if (p_cb->role == HCI_ROLE_MASTER) {
  1127. model = smp_association_table_sc[p_cb->role][p_cb->peer_io_caps]
  1128. [p_cb->local_io_capability];
  1129. } else {
  1130. model = smp_association_table_sc[p_cb->role][p_cb->local_io_capability]
  1131. [p_cb->peer_io_caps];
  1132. }
  1133. }
  1134. return model;
  1135. }
  1136. /*******************************************************************************
  1137. ** Function smp_reverse_array
  1138. **
  1139. ** Description This function reverses array bytes
  1140. **
  1141. *******************************************************************************/
  1142. void smp_reverse_array(UINT8 *arr, UINT8 len)
  1143. {
  1144. UINT8 i = 0, tmp;
  1145. SMP_TRACE_DEBUG("smp_reverse_array\n");
  1146. for (i = 0; i < len / 2; i ++) {
  1147. tmp = arr[i];
  1148. arr[i] = arr[len - 1 - i];
  1149. arr[len - 1 - i] = tmp;
  1150. }
  1151. }
  1152. /*******************************************************************************
  1153. ** Function smp_calculate_random_input
  1154. **
  1155. ** Description This function returns random input value to be used in commitment
  1156. ** calculation for SC passkey entry association mode
  1157. ** (if bit["round"] in "random" array == 1 then returns 0x81
  1158. ** else returns 0x80).
  1159. **
  1160. ** Returns ri value
  1161. **
  1162. *******************************************************************************/
  1163. UINT8 smp_calculate_random_input(UINT8 *random, UINT8 round)
  1164. {
  1165. UINT8 i = round / 8;
  1166. UINT8 j = round % 8;
  1167. UINT8 ri;
  1168. SMP_TRACE_DEBUG("random: 0x%02x, round: %d, i: %d, j: %d\n", random[i], round, i, j);
  1169. ri = ((random[i] >> j) & 1) | 0x80;
  1170. SMP_TRACE_DEBUG("%s ri=0x%02x\n", __func__, ri);
  1171. return ri;
  1172. }
  1173. /*******************************************************************************
  1174. ** Function smp_collect_local_io_capabilities
  1175. **
  1176. ** Description This function puts into IOcap array local device
  1177. ** IOCapability, OOB data, AuthReq.
  1178. **
  1179. ** Returns void
  1180. **
  1181. *******************************************************************************/
  1182. void smp_collect_local_io_capabilities(UINT8 *iocap, tSMP_CB *p_cb)
  1183. {
  1184. SMP_TRACE_DEBUG("%s\n", __func__);
  1185. iocap[0] = p_cb->local_io_capability;
  1186. iocap[1] = p_cb->loc_oob_flag;
  1187. iocap[2] = p_cb->loc_auth_req;
  1188. }
  1189. /*******************************************************************************
  1190. ** Function smp_collect_peer_io_capabilities
  1191. **
  1192. ** Description This function puts into IOcap array peer device
  1193. ** IOCapability, OOB data, AuthReq.
  1194. **
  1195. ** Returns void
  1196. **
  1197. *******************************************************************************/
  1198. void smp_collect_peer_io_capabilities(UINT8 *iocap, tSMP_CB *p_cb)
  1199. {
  1200. SMP_TRACE_DEBUG("%s\n", __func__);
  1201. iocap[0] = p_cb->peer_io_caps;
  1202. iocap[1] = p_cb->peer_oob_flag;
  1203. iocap[2] = p_cb->peer_auth_req;
  1204. }
  1205. /*******************************************************************************
  1206. ** Function smp_collect_local_ble_address
  1207. **
  1208. ** Description This function puts into le_addr array local device le address:
  1209. ** le_addr[0-5] = local BD ADDR,
  1210. ** le_addr[6] = local le address type (PUBLIC/RANDOM).
  1211. **
  1212. ** Returns void
  1213. **
  1214. *******************************************************************************/
  1215. void smp_collect_local_ble_address(UINT8 *le_addr, tSMP_CB *p_cb)
  1216. {
  1217. tBLE_ADDR_TYPE addr_type = 0;
  1218. BD_ADDR bda;
  1219. UINT8 *p = le_addr;
  1220. SMP_TRACE_DEBUG("%s\n", __func__);
  1221. BTM_ReadConnectionAddr( p_cb->pairing_bda, bda, &addr_type);
  1222. BDADDR_TO_STREAM(p, bda);
  1223. UINT8_TO_STREAM(p, addr_type);
  1224. }
  1225. /*******************************************************************************
  1226. ** Function smp_collect_peer_ble_address
  1227. **
  1228. ** Description This function puts into le_addr array peer device le address:
  1229. ** le_addr[0-5] = peer BD ADDR,
  1230. ** le_addr[6] = peer le address type (PUBLIC/RANDOM).
  1231. **
  1232. ** Returns void
  1233. **
  1234. *******************************************************************************/
  1235. void smp_collect_peer_ble_address(UINT8 *le_addr, tSMP_CB *p_cb)
  1236. {
  1237. tBLE_ADDR_TYPE addr_type = 0;
  1238. BD_ADDR bda;
  1239. UINT8 *p = le_addr;
  1240. SMP_TRACE_DEBUG("%s\n", __func__);
  1241. if (!BTM_ReadRemoteConnectionAddr(p_cb->pairing_bda, bda, &addr_type)) {
  1242. SMP_TRACE_ERROR("can not collect peer le addr information for unknown device\n");
  1243. return;
  1244. }
  1245. BDADDR_TO_STREAM(p, bda);
  1246. UINT8_TO_STREAM(p, addr_type);
  1247. }
  1248. /*******************************************************************************
  1249. ** Function smp_check_commitment
  1250. **
  1251. ** Description This function compares peer commitment values:
  1252. ** - expected (i.e. calculated locally),
  1253. ** - received from the peer.
  1254. **
  1255. ** Returns TRUE if the values are the same
  1256. ** FALSE otherwise
  1257. **
  1258. *******************************************************************************/
  1259. BOOLEAN smp_check_commitment(tSMP_CB *p_cb)
  1260. {
  1261. BT_OCTET16 expected;
  1262. SMP_TRACE_DEBUG("%s\n", __func__);
  1263. smp_calculate_peer_commitment(p_cb, expected);
  1264. print128(expected, (const UINT8 *)"calculated peer commitment");
  1265. print128(p_cb->remote_commitment, (const UINT8 *)"received peer commitment");
  1266. if (memcmp(p_cb->remote_commitment, expected, BT_OCTET16_LEN)) {
  1267. SMP_TRACE_WARNING("Commitment check fails\n");
  1268. return FALSE;
  1269. }
  1270. SMP_TRACE_DEBUG("Commitment check succeeds\n");
  1271. return TRUE;
  1272. }
  1273. /*******************************************************************************
  1274. **
  1275. ** Function smp_save_secure_connections_long_term_key
  1276. **
  1277. ** Description The function saves SC LTK as BLE key for future use as local
  1278. ** and/or peer key.
  1279. **
  1280. ** Returns void
  1281. **
  1282. *******************************************************************************/
  1283. void smp_save_secure_connections_long_term_key(tSMP_CB *p_cb)
  1284. {
  1285. tBTM_LE_LENC_KEYS lle_key;
  1286. tBTM_LE_PENC_KEYS ple_key;
  1287. SMP_TRACE_DEBUG("%s-Save LTK as local LTK key\n", __func__);
  1288. memcpy(lle_key.ltk, p_cb->ltk, BT_OCTET16_LEN);
  1289. lle_key.div = 0;
  1290. lle_key.key_size = p_cb->loc_enc_size;
  1291. lle_key.sec_level = p_cb->sec_level;
  1292. btm_sec_save_le_key(p_cb->pairing_bda, BTM_LE_KEY_LENC, (tBTM_LE_KEY_VALUE *)&lle_key, TRUE);
  1293. SMP_TRACE_DEBUG("%s-Save LTK as peer LTK key\n", __func__);
  1294. ple_key.ediv = 0;
  1295. memset(ple_key.rand, 0, BT_OCTET8_LEN);
  1296. memcpy(ple_key.ltk, p_cb->ltk, BT_OCTET16_LEN);
  1297. ple_key.sec_level = p_cb->sec_level;
  1298. ple_key.key_size = p_cb->loc_enc_size;
  1299. btm_sec_save_le_key(p_cb->pairing_bda, BTM_LE_KEY_PENC, (tBTM_LE_KEY_VALUE *)&ple_key, TRUE);
  1300. }
  1301. /*******************************************************************************
  1302. **
  1303. ** Function smp_calculate_f5_mackey_and_long_term_key
  1304. **
  1305. ** Description The function calculates MacKey and LTK and saves them in CB.
  1306. ** To calculate MacKey and LTK it calls smp_calc_f5(...).
  1307. ** MacKey is used in dhkey calculation, LTK is used to encrypt
  1308. ** the link.
  1309. **
  1310. ** Returns FALSE if out of resources, TRUE otherwise.
  1311. **
  1312. *******************************************************************************/
  1313. BOOLEAN smp_calculate_f5_mackey_and_long_term_key(tSMP_CB *p_cb)
  1314. {
  1315. UINT8 a[7];
  1316. UINT8 b[7];
  1317. UINT8 *p_na;
  1318. UINT8 *p_nb;
  1319. SMP_TRACE_DEBUG("%s\n", __func__);
  1320. if (p_cb->role == HCI_ROLE_MASTER) {
  1321. smp_collect_local_ble_address(a, p_cb);
  1322. smp_collect_peer_ble_address(b, p_cb);
  1323. p_na = p_cb->rand;
  1324. p_nb = p_cb->rrand;
  1325. } else {
  1326. smp_collect_local_ble_address(b, p_cb);
  1327. smp_collect_peer_ble_address(a, p_cb);
  1328. p_na = p_cb->rrand;
  1329. p_nb = p_cb->rand;
  1330. }
  1331. if (!smp_calculate_f5(p_cb->dhkey, p_na, p_nb, a, b, p_cb->mac_key, p_cb->ltk)) {
  1332. SMP_TRACE_ERROR("%s failed\n", __func__);
  1333. return FALSE;
  1334. }
  1335. SMP_TRACE_EVENT ("%s is completed\n", __func__);
  1336. return TRUE;
  1337. }
  1338. /*******************************************************************************
  1339. **
  1340. ** Function smp_request_oob_data
  1341. **
  1342. ** Description Requests application to provide OOB data.
  1343. **
  1344. ** Returns TRUE - OOB data has to be provided by application
  1345. ** FALSE - otherwise (unexpected)
  1346. **
  1347. *******************************************************************************/
  1348. BOOLEAN smp_request_oob_data(tSMP_CB *p_cb)
  1349. {
  1350. tSMP_OOB_DATA_TYPE req_oob_type = SMP_OOB_INVALID_TYPE;
  1351. SMP_TRACE_DEBUG("%s\n", __func__);
  1352. if (p_cb->peer_oob_flag == SMP_OOB_PRESENT && p_cb->loc_oob_flag == SMP_OOB_PRESENT) {
  1353. /* both local and peer rcvd data OOB */
  1354. req_oob_type = SMP_OOB_BOTH;
  1355. } else if (p_cb->peer_oob_flag == SMP_OOB_PRESENT) {
  1356. /* peer rcvd OOB local data, local didn't receive OOB peer data */
  1357. req_oob_type = SMP_OOB_LOCAL;
  1358. } else if (p_cb->loc_oob_flag == SMP_OOB_PRESENT) {
  1359. req_oob_type = SMP_OOB_PEER;
  1360. }
  1361. SMP_TRACE_DEBUG("req_oob_type = %d\n", req_oob_type);
  1362. if (req_oob_type == SMP_OOB_INVALID_TYPE) {
  1363. return FALSE;
  1364. }
  1365. p_cb->req_oob_type = req_oob_type;
  1366. p_cb->cb_evt = SMP_SC_OOB_REQ_EVT;
  1367. smp_sm_event(p_cb, SMP_TK_REQ_EVT, &req_oob_type);
  1368. return TRUE;
  1369. }
  1370. #endif