smp_utils.c 58 KB

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