smp_act.c 85 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194
  1. /******************************************************************************
  2. *
  3. * Copyright (C) 2003-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. #include <string.h>
  19. #include "device/interop.h"
  20. #include "common/bt_target.h"
  21. #include "btm_int.h"
  22. #include "stack/l2c_api.h"
  23. #include "smp_int.h"
  24. #include "p_256_ecc_pp.h"
  25. //#include "utils/include/bt_utils.h"
  26. #if SMP_INCLUDED == TRUE
  27. const UINT8 smp_association_table[2][SMP_IO_CAP_MAX][SMP_IO_CAP_MAX] = {
  28. /* initiator */
  29. { {SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_PASSKEY, SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_PASSKEY}, /* Display Only */
  30. {SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_PASSKEY, SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_PASSKEY}, /* SMP_CAP_IO = 1 */
  31. {SMP_MODEL_KEY_NOTIF, SMP_MODEL_KEY_NOTIF, SMP_MODEL_PASSKEY, SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_KEY_NOTIF}, /* keyboard only */
  32. {SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_ENCRYPTION_ONLY},/* No Input No Output */
  33. {SMP_MODEL_KEY_NOTIF, SMP_MODEL_KEY_NOTIF, SMP_MODEL_PASSKEY, SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_KEY_NOTIF}
  34. }, /* keyboard display */
  35. /* responder */
  36. { {SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_KEY_NOTIF, SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_KEY_NOTIF}, /* Display Only */
  37. {SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_KEY_NOTIF, SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_KEY_NOTIF}, /* SMP_CAP_IO = 1 */
  38. {SMP_MODEL_PASSKEY, SMP_MODEL_PASSKEY, SMP_MODEL_PASSKEY, SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_PASSKEY}, /* keyboard only */
  39. {SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_ENCRYPTION_ONLY},/* No Input No Output */
  40. {SMP_MODEL_PASSKEY, SMP_MODEL_PASSKEY, SMP_MODEL_KEY_NOTIF, SMP_MODEL_ENCRYPTION_ONLY, SMP_MODEL_PASSKEY}
  41. } /* keyboard display */
  42. /* display only */ /*SMP_CAP_IO = 1 */ /* keyboard only */ /* No InputOutput */ /* keyboard display */
  43. };
  44. #define SMP_KEY_DIST_TYPE_MAX 4
  45. const tSMP_ACT smp_distribute_act [] = {
  46. #if (BLE_INCLUDED == TRUE)
  47. smp_generate_ltk,
  48. smp_send_id_info,
  49. smp_generate_csrk,
  50. smp_set_derive_link_key
  51. #else
  52. NULL,
  53. NULL,
  54. NULL,
  55. NULL
  56. #endif ///BLE_INCLUDED == TRUE
  57. };
  58. extern UINT8 bta_dm_co_ble_get_accept_auth_enable(void);
  59. extern UINT8 bta_dm_co_ble_get_auth_req(void);
  60. static bool lmp_version_below(BD_ADDR bda, uint8_t version)
  61. {
  62. tACL_CONN *acl = btm_bda_to_acl(bda, BT_TRANSPORT_LE);
  63. if (acl == NULL || acl->lmp_version == 0) {
  64. SMP_TRACE_WARNING("%s cannot retrieve LMP version...", __func__);
  65. return false;
  66. }
  67. SMP_TRACE_DEBUG("%s LMP version %d < %d", __func__, acl->lmp_version, version);
  68. return acl->lmp_version < version;
  69. }
  70. /*******************************************************************************
  71. ** Function smp_update_key_mask
  72. ** Description This function updates the key mask for sending or receiving.
  73. *******************************************************************************/
  74. static void smp_update_key_mask (tSMP_CB *p_cb, UINT8 key_type, BOOLEAN recv)
  75. {
  76. SMP_TRACE_DEBUG("%s before update role=%d recv=%d local_i_key = %02x, local_r_key = %02x\n",
  77. __func__, p_cb->role, recv, p_cb->local_i_key, p_cb->local_r_key);
  78. if (((p_cb->le_secure_connections_mode_is_used) ||
  79. (p_cb->smp_over_br)) &&
  80. ((key_type == SMP_SEC_KEY_TYPE_ENC) || (key_type == SMP_SEC_KEY_TYPE_LK))) {
  81. /* in LE SC mode LTK, CSRK and BR/EDR LK are derived locally instead of
  82. ** being exchanged with the peer */
  83. p_cb->local_i_key &= ~key_type;
  84. p_cb->local_r_key &= ~key_type;
  85. } else if (p_cb->role == HCI_ROLE_SLAVE) {
  86. if (recv) {
  87. p_cb->local_i_key &= ~key_type;
  88. } else {
  89. p_cb->local_r_key &= ~key_type;
  90. }
  91. } else {
  92. if (recv) {
  93. p_cb->local_r_key &= ~key_type;
  94. } else {
  95. p_cb->local_i_key &= ~key_type;
  96. }
  97. }
  98. SMP_TRACE_DEBUG("updated local_i_key = %02x, local_r_key = %02x\n", p_cb->local_i_key,
  99. p_cb->local_r_key);
  100. }
  101. /*******************************************************************************
  102. ** Function smp_send_app_cback
  103. ** Description notifies application about the events the application is interested in
  104. *******************************************************************************/
  105. void smp_send_app_cback(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
  106. {
  107. tSMP_EVT_DATA cb_data;
  108. tSMP_STATUS callback_rc;
  109. SMP_TRACE_DEBUG("%s p_cb->cb_evt=%d\n", __func__, p_cb->cb_evt);
  110. if (p_cb->p_callback && p_cb->cb_evt != 0) {
  111. switch (p_cb->cb_evt) {
  112. case SMP_IO_CAP_REQ_EVT:
  113. cb_data.io_req.auth_req = p_cb->peer_auth_req;
  114. cb_data.io_req.oob_data = SMP_OOB_NONE;
  115. cb_data.io_req.io_cap = SMP_DEFAULT_IO_CAPS;
  116. cb_data.io_req.max_key_size = SMP_MAX_ENC_KEY_SIZE;
  117. cb_data.io_req.init_keys = p_cb->local_i_key ;
  118. cb_data.io_req.resp_keys = p_cb->local_r_key ;
  119. SMP_TRACE_DEBUG ( "io_cap = %d", cb_data.io_req.io_cap);
  120. break;
  121. case SMP_NC_REQ_EVT:
  122. cb_data.passkey = p_data->passkey;
  123. break;
  124. case SMP_SC_OOB_REQ_EVT:
  125. cb_data.req_oob_type = p_data->req_oob_type;
  126. break;
  127. case SMP_SC_LOC_OOB_DATA_UP_EVT:
  128. cb_data.loc_oob_data = p_cb->sc_oob_data.loc_oob_data;
  129. break;
  130. case SMP_BR_KEYS_REQ_EVT:
  131. cb_data.io_req.auth_req = 0;
  132. cb_data.io_req.oob_data = SMP_OOB_NONE;
  133. cb_data.io_req.io_cap = 0;
  134. cb_data.io_req.max_key_size = SMP_MAX_ENC_KEY_SIZE;
  135. cb_data.io_req.init_keys = SMP_BR_SEC_DEFAULT_KEY;
  136. cb_data.io_req.resp_keys = SMP_BR_SEC_DEFAULT_KEY;
  137. break;
  138. default:
  139. break;
  140. }
  141. callback_rc = (*p_cb->p_callback)(p_cb->cb_evt, p_cb->pairing_bda, &cb_data);
  142. SMP_TRACE_DEBUG("callback_rc=%d p_cb->cb_evt=%d\n", callback_rc, p_cb->cb_evt );
  143. if (callback_rc == SMP_SUCCESS) {
  144. switch (p_cb->cb_evt) {
  145. case SMP_IO_CAP_REQ_EVT:
  146. p_cb->loc_auth_req = cb_data.io_req.auth_req;
  147. p_cb->local_io_capability = cb_data.io_req.io_cap;
  148. p_cb->loc_oob_flag = cb_data.io_req.oob_data;
  149. p_cb->loc_enc_size = cb_data.io_req.max_key_size;
  150. p_cb->local_i_key = cb_data.io_req.init_keys;
  151. p_cb->local_r_key = cb_data.io_req.resp_keys;
  152. if (!(p_cb->loc_auth_req & SMP_AUTH_BOND)) {
  153. SMP_TRACE_WARNING ("Non bonding: No keys will be exchanged");
  154. p_cb->local_i_key = 0;
  155. p_cb->local_r_key = 0;
  156. }
  157. SMP_TRACE_DEBUG ("rcvd auth_req: 0x%02x, io_cap: %d \
  158. loc_oob_flag: %d loc_enc_size: %d,"
  159. "local_i_key: 0x%02x, local_r_key: 0x%02x\n",
  160. p_cb->loc_auth_req, p_cb->local_io_capability, p_cb->loc_oob_flag,
  161. p_cb->loc_enc_size, p_cb->local_i_key, p_cb->local_r_key);
  162. p_cb->secure_connections_only_mode_required =
  163. (btm_cb.security_mode == BTM_SEC_MODE_SC) ? TRUE : FALSE;
  164. if (p_cb->secure_connections_only_mode_required) {
  165. p_cb->loc_auth_req |= SMP_SC_SUPPORT_BIT;
  166. }
  167. if (!(p_cb->loc_auth_req & SMP_SC_SUPPORT_BIT)
  168. || lmp_version_below(p_cb->pairing_bda, HCI_PROTO_VERSION_4_2)
  169. || interop_match(INTEROP_DISABLE_LE_SECURE_CONNECTIONS,
  170. (const bt_bdaddr_t *)&p_cb->pairing_bda)) {
  171. p_cb->loc_auth_req &= ~SMP_KP_SUPPORT_BIT;
  172. p_cb->local_i_key &= ~SMP_SEC_KEY_TYPE_LK;
  173. p_cb->local_r_key &= ~SMP_SEC_KEY_TYPE_LK;
  174. }
  175. SMP_TRACE_DEBUG("set auth_req: 0x%02x, local_i_key: 0x%02x, local_r_key: 0x%02x\n",
  176. p_cb->loc_auth_req, p_cb->local_i_key, p_cb->local_r_key);
  177. smp_sm_event(p_cb, SMP_IO_RSP_EVT, NULL);
  178. break;
  179. #if (CLASSIC_BT_INCLUDED == TRUE)
  180. case SMP_BR_KEYS_REQ_EVT:
  181. p_cb->loc_enc_size = cb_data.io_req.max_key_size;
  182. p_cb->local_i_key = cb_data.io_req.init_keys;
  183. p_cb->local_r_key = cb_data.io_req.resp_keys;
  184. p_cb->local_i_key &= ~SMP_SEC_KEY_TYPE_LK;
  185. p_cb->local_r_key &= ~SMP_SEC_KEY_TYPE_LK;
  186. SMP_TRACE_WARNING ( "for SMP over BR max_key_size: 0x%02x,\
  187. local_i_key: 0x%02x, local_r_key: 0x%02x\n",
  188. p_cb->loc_enc_size, p_cb->local_i_key, p_cb->local_r_key);
  189. smp_br_state_machine_event(p_cb, SMP_BR_KEYS_RSP_EVT, NULL);
  190. break;
  191. #endif ///CLASSIC_BT_INCLUDED == TRUE
  192. }
  193. }
  194. }
  195. if (!p_cb->cb_evt && p_cb->discard_sec_req) {
  196. p_cb->discard_sec_req = FALSE;
  197. smp_sm_event(p_cb, SMP_DISCARD_SEC_REQ_EVT, NULL);
  198. }
  199. SMP_TRACE_DEBUG("%s return\n", __func__);
  200. }
  201. /*******************************************************************************
  202. ** Function smp_send_pair_fail
  203. ** Description pairing failure to peer device if needed.
  204. *******************************************************************************/
  205. void smp_send_pair_fail(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
  206. {
  207. p_cb->status = *(UINT8 *)p_data;
  208. p_cb->failure = *(UINT8 *)p_data;
  209. SMP_TRACE_DEBUG("%s status=%d failure=%d ", __func__, p_cb->status, p_cb->failure);
  210. if (p_cb->status <= SMP_MAX_FAIL_RSN_PER_SPEC && p_cb->status != SMP_SUCCESS) {
  211. smp_send_cmd(SMP_OPCODE_PAIRING_FAILED, p_cb);
  212. p_cb->wait_for_authorization_complete = TRUE;
  213. }
  214. }
  215. /*******************************************************************************
  216. ** Function smp_send_pair_req
  217. ** Description actions related to sending pairing request
  218. *******************************************************************************/
  219. void smp_send_pair_req(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
  220. {
  221. SMP_TRACE_DEBUG("%s\n", __func__);
  222. #if (BLE_INCLUDED == TRUE)
  223. tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev (p_cb->pairing_bda);
  224. /* erase all keys when master sends pairing req*/
  225. if (p_dev_rec) {
  226. btm_sec_clear_ble_keys(p_dev_rec);
  227. }
  228. #endif ///BLE_INCLUDED == TRUE
  229. /* do not manipulate the key, let app decide,
  230. leave out to BTM to mandate key distribution for bonding case */
  231. smp_send_cmd(SMP_OPCODE_PAIRING_REQ, p_cb);
  232. }
  233. /*******************************************************************************
  234. ** Function smp_send_pair_rsp
  235. ** Description actions related to sending pairing response
  236. *******************************************************************************/
  237. void smp_send_pair_rsp(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
  238. {
  239. SMP_TRACE_DEBUG("%s\n", __func__);
  240. #if (BLE_INCLUDED == TRUE)
  241. p_cb->local_i_key &= p_cb->peer_i_key;
  242. p_cb->local_r_key &= p_cb->peer_r_key;
  243. if (smp_send_cmd (SMP_OPCODE_PAIRING_RSP, p_cb)) {
  244. if (p_cb->selected_association_model == SMP_MODEL_SEC_CONN_OOB) {
  245. smp_use_oob_private_key(p_cb, NULL);
  246. } else {
  247. smp_decide_association_model(p_cb, NULL);
  248. }
  249. }
  250. #endif ///BLE_INCLUDED == TRUE
  251. }
  252. /*******************************************************************************
  253. ** Function smp_send_confirm
  254. ** Description send confirmation to the peer
  255. *******************************************************************************/
  256. void smp_send_confirm(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
  257. {
  258. SMP_TRACE_DEBUG("%s\n", __func__);
  259. smp_send_cmd(SMP_OPCODE_CONFIRM, p_cb);
  260. }
  261. #if 0 //Unused
  262. /*******************************************************************************
  263. ** Function smp_send_init
  264. ** Description process pairing initializer to slave device
  265. *******************************************************************************/
  266. void smp_send_init(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
  267. {
  268. SMP_TRACE_DEBUG("%s\n", __func__);
  269. smp_send_cmd(SMP_OPCODE_INIT, p_cb);
  270. }
  271. #endif
  272. /*******************************************************************************
  273. ** Function smp_send_rand
  274. ** Description send pairing random to the peer
  275. *******************************************************************************/
  276. void smp_send_rand(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
  277. {
  278. SMP_TRACE_DEBUG("%s\n", __func__);
  279. smp_send_cmd(SMP_OPCODE_RAND, p_cb);
  280. }
  281. /*******************************************************************************
  282. ** Function smp_send_pair_public_key
  283. ** Description send pairing public key command to the peer
  284. *******************************************************************************/
  285. void smp_send_pair_public_key(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
  286. {
  287. SMP_TRACE_DEBUG("%s\n", __func__);
  288. smp_send_cmd(SMP_OPCODE_PAIR_PUBLIC_KEY, p_cb);
  289. }
  290. /*******************************************************************************
  291. ** Function SMP_SEND_COMMITMENT
  292. ** Description send commitment command to the peer
  293. *******************************************************************************/
  294. void smp_send_commitment(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
  295. {
  296. SMP_TRACE_DEBUG("%s", __func__);
  297. smp_send_cmd(SMP_OPCODE_PAIR_COMMITM, p_cb);
  298. }
  299. /*******************************************************************************
  300. ** Function smp_send_dhkey_check
  301. ** Description send DHKey Check command to the peer
  302. *******************************************************************************/
  303. void smp_send_dhkey_check(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
  304. {
  305. SMP_TRACE_DEBUG("%s", __func__);
  306. smp_send_cmd(SMP_OPCODE_PAIR_DHKEY_CHECK, p_cb);
  307. }
  308. /*******************************************************************************
  309. ** Function smp_send_keypress_notification
  310. ** Description send Keypress Notification command to the peer
  311. *******************************************************************************/
  312. void smp_send_keypress_notification(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
  313. {
  314. p_cb->local_keypress_notification = *(UINT8 *) p_data;
  315. smp_send_cmd(SMP_OPCODE_PAIR_KEYPR_NOTIF, p_cb);
  316. }
  317. /*******************************************************************************
  318. ** Function smp_send_enc_info
  319. ** Description send encryption information command.
  320. *******************************************************************************/
  321. void smp_send_enc_info(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
  322. {
  323. tBTM_LE_LENC_KEYS le_key;
  324. SMP_TRACE_DEBUG("%s p_cb->loc_enc_size = %d\n", __func__, p_cb->loc_enc_size);
  325. smp_update_key_mask (p_cb, SMP_SEC_KEY_TYPE_ENC, FALSE);
  326. smp_send_cmd(SMP_OPCODE_ENCRYPT_INFO, p_cb);
  327. smp_send_cmd(SMP_OPCODE_MASTER_ID, p_cb);
  328. /* save the DIV and key size information when acting as slave device */
  329. memcpy(le_key.ltk, p_cb->ltk, BT_OCTET16_LEN);
  330. le_key.div = p_cb->div;
  331. le_key.key_size = p_cb->loc_enc_size;
  332. le_key.sec_level = p_cb->sec_level;
  333. #if (BLE_INCLUDED == TRUE)
  334. if ((p_cb->peer_auth_req & SMP_AUTH_BOND) && (p_cb->loc_auth_req & SMP_AUTH_BOND)) {
  335. btm_sec_save_le_key(p_cb->pairing_bda, BTM_LE_KEY_LENC,
  336. (tBTM_LE_KEY_VALUE *)&le_key, TRUE);
  337. }
  338. SMP_TRACE_DEBUG ("%s\n", __func__);
  339. smp_key_distribution(p_cb, NULL);
  340. #endif ///BLE_INCLUDED == TRUE
  341. }
  342. /*******************************************************************************
  343. ** Function smp_send_id_info
  344. ** Description send ID information command.
  345. *******************************************************************************/
  346. void smp_send_id_info(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
  347. {
  348. SMP_TRACE_DEBUG("%s\n", __func__);
  349. smp_update_key_mask (p_cb, SMP_SEC_KEY_TYPE_ID, FALSE);
  350. smp_send_cmd(SMP_OPCODE_IDENTITY_INFO, p_cb);
  351. smp_send_cmd(SMP_OPCODE_ID_ADDR, p_cb);
  352. #if (BLE_INCLUDED == TRUE)
  353. tBTM_LE_KEY_VALUE le_key;
  354. if ((p_cb->peer_auth_req & SMP_AUTH_BOND) && (p_cb->loc_auth_req & SMP_AUTH_BOND)) {
  355. btm_sec_save_le_key(p_cb->pairing_bda, BTM_LE_KEY_LID,
  356. &le_key, TRUE);
  357. }
  358. #endif ///BLE_INCLUDED == TRUE
  359. smp_key_distribution_by_transport(p_cb, NULL);
  360. }
  361. #if (BLE_INCLUDED == TRUE)
  362. /*******************************************************************************
  363. ** Function smp_send_csrk_info
  364. ** Description send CSRK command.
  365. *******************************************************************************/
  366. void smp_send_csrk_info(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
  367. {
  368. tBTM_LE_LCSRK_KEYS key;
  369. SMP_TRACE_DEBUG("%s\n", __func__);
  370. smp_update_key_mask (p_cb, SMP_SEC_KEY_TYPE_CSRK, FALSE);
  371. if (smp_send_cmd(SMP_OPCODE_SIGN_INFO, p_cb)) {
  372. key.div = p_cb->div;
  373. key.sec_level = p_cb->sec_level;
  374. key.counter = 0; /* initialize the local counter */
  375. memcpy (key.csrk, p_cb->csrk, BT_OCTET16_LEN);
  376. btm_sec_save_le_key(p_cb->pairing_bda, BTM_LE_KEY_LCSRK, (tBTM_LE_KEY_VALUE *)&key, TRUE);
  377. }
  378. smp_key_distribution_by_transport(p_cb, NULL);
  379. }
  380. /*******************************************************************************
  381. ** Function smp_send_ltk_reply
  382. ** Description send LTK reply
  383. *******************************************************************************/
  384. void smp_send_ltk_reply(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
  385. {
  386. SMP_TRACE_DEBUG("%s", __func__);
  387. /* send stk as LTK response */
  388. btm_ble_ltk_request_reply(p_cb->pairing_bda, TRUE, p_data->key.p_data);
  389. }
  390. /*******************************************************************************
  391. ** Function smp_proc_sec_req
  392. ** Description process security request.
  393. *******************************************************************************/
  394. void smp_proc_sec_req(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
  395. {
  396. tBTM_LE_AUTH_REQ auth_req = *(tBTM_LE_AUTH_REQ *)p_data;
  397. tBTM_BLE_SEC_REQ_ACT sec_req_act;
  398. UINT8 reason;
  399. SMP_TRACE_DEBUG("%s auth_req=0x%x", __func__, auth_req);
  400. p_cb->cb_evt = 0;
  401. btm_ble_link_sec_check(p_cb->pairing_bda, auth_req, &sec_req_act);
  402. SMP_TRACE_DEBUG("%s sec_req_act=0x%x", __func__, sec_req_act);
  403. switch (sec_req_act) {
  404. case BTM_BLE_SEC_REQ_ACT_ENCRYPT:
  405. SMP_TRACE_DEBUG("%s BTM_BLE_SEC_REQ_ACT_ENCRYPT", __func__);
  406. smp_sm_event(p_cb, SMP_ENC_REQ_EVT, NULL);
  407. break;
  408. case BTM_BLE_SEC_REQ_ACT_PAIR:
  409. p_cb->secure_connections_only_mode_required =
  410. (btm_cb.security_mode == BTM_SEC_MODE_SC) ? TRUE : FALSE;
  411. /* respond to non SC pairing request as failure in SC only mode */
  412. if (p_cb->secure_connections_only_mode_required &&
  413. (auth_req & SMP_SC_SUPPORT_BIT) == 0) {
  414. reason = SMP_PAIR_AUTH_FAIL;
  415. smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &reason);
  416. } else {
  417. /* initialize local i/r key to be default keys */
  418. p_cb->peer_auth_req = auth_req;
  419. p_cb->local_r_key = p_cb->local_i_key = SMP_SEC_DEFAULT_KEY ;
  420. p_cb->cb_evt = SMP_SEC_REQUEST_EVT;
  421. }
  422. break;
  423. case BTM_BLE_SEC_REQ_ACT_DISCARD:
  424. p_cb->discard_sec_req = TRUE;
  425. break;
  426. default:
  427. /* do nothing */
  428. break;
  429. }
  430. }
  431. #endif ///BLE_INCLUDED == TRUE
  432. /*******************************************************************************
  433. ** Function smp_proc_sec_grant
  434. ** Description process security grant.
  435. *******************************************************************************/
  436. void smp_proc_sec_grant(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
  437. {
  438. UINT8 res = *(UINT8 *)p_data;
  439. SMP_TRACE_DEBUG("%s", __func__);
  440. if (res != SMP_SUCCESS) {
  441. smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, p_data);
  442. } else { /*otherwise, start pairing */
  443. /* send IO request callback */
  444. p_cb->cb_evt = SMP_IO_CAP_REQ_EVT;
  445. }
  446. }
  447. /*******************************************************************************
  448. ** Function smp_proc_pair_fail
  449. ** Description process pairing failure from peer device
  450. *******************************************************************************/
  451. void smp_proc_pair_fail(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
  452. {
  453. SMP_TRACE_DEBUG("%s", __func__);
  454. p_cb->status = *(UINT8 *)p_data;
  455. }
  456. /*******************************************************************************
  457. ** Function smp_get_auth_mode
  458. ** Description Get the SMP pairing auth mode
  459. *******************************************************************************/
  460. uint16_t smp_get_auth_mode (tSMP_ASSO_MODEL model)
  461. {
  462. SMP_TRACE_DEBUG("%s model %d", __func__, model);
  463. uint16_t auth = 0;
  464. if (model == SMP_MODEL_ENCRYPTION_ONLY || model == SMP_MODEL_SEC_CONN_JUSTWORKS) {
  465. //No MITM
  466. if(model == SMP_MODEL_SEC_CONN_JUSTWORKS) {
  467. //SC SMP_SC_SUPPORT_BIT
  468. auth |= SMP_SC_SUPPORT_BIT;
  469. }
  470. } else if (model <= SMP_MODEL_KEY_NOTIF) {
  471. //NO SC, MITM
  472. auth |= SMP_AUTH_YN_BIT;
  473. } else if (model <= SMP_MODEL_SEC_CONN_OOB) {
  474. //SC, MITM
  475. auth |= SMP_SC_SUPPORT_BIT;
  476. auth |= SMP_AUTH_YN_BIT;
  477. } else {
  478. auth = 0;
  479. }
  480. return auth;
  481. }
  482. #if (BLE_INCLUDED == TRUE)
  483. /*******************************************************************************
  484. ** Function smp_proc_pair_cmd
  485. ** Description Process the SMP pairing request/response from peer device
  486. *******************************************************************************/
  487. void smp_proc_pair_cmd(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
  488. {
  489. UINT8 *p = (UINT8 *)p_data;
  490. UINT8 reason = SMP_ENC_KEY_SIZE;
  491. tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev (p_cb->pairing_bda);
  492. SMP_TRACE_DEBUG("%s\n", __func__);
  493. /* erase all keys if it is slave proc pairing req*/
  494. if (p_dev_rec && (p_cb->role == HCI_ROLE_SLAVE)) {
  495. btm_sec_clear_ble_keys(p_dev_rec);
  496. }
  497. p_cb->flags |= SMP_PAIR_FLAG_ENC_AFTER_PAIR;
  498. STREAM_TO_UINT8(p_cb->peer_io_caps, p);
  499. STREAM_TO_UINT8(p_cb->peer_oob_flag, p);
  500. STREAM_TO_UINT8(p_cb->peer_auth_req, p);
  501. STREAM_TO_UINT8(p_cb->peer_enc_size, p);
  502. STREAM_TO_UINT8(p_cb->peer_i_key, p);
  503. STREAM_TO_UINT8(p_cb->peer_r_key, p);
  504. if (smp_command_has_invalid_parameters(p_cb)) {
  505. reason = SMP_INVALID_PARAMETERS;
  506. smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &reason);
  507. return;
  508. }
  509. p_cb->accept_specified_sec_auth = bta_dm_co_ble_get_accept_auth_enable();
  510. p_cb->origin_loc_auth_req = bta_dm_co_ble_get_auth_req();
  511. if (p_cb->role == HCI_ROLE_SLAVE) {
  512. if (!(p_cb->flags & SMP_PAIR_FLAGS_WE_STARTED_DD)) {
  513. /* peer (master) started pairing sending Pairing Request */
  514. p_cb->local_i_key = p_cb->peer_i_key;
  515. p_cb->local_r_key = p_cb->peer_r_key;
  516. p_cb->cb_evt = SMP_SEC_REQUEST_EVT;
  517. } else { /* update local i/r key according to pairing request */
  518. /* pairing started with this side (slave) sending Security Request */
  519. p_cb->local_i_key &= p_cb->peer_i_key;
  520. p_cb->local_r_key &= p_cb->peer_r_key;
  521. p_cb->selected_association_model = smp_select_association_model(p_cb);
  522. if (p_cb->secure_connections_only_mode_required &&
  523. (!(p_cb->le_secure_connections_mode_is_used) ||
  524. (p_cb->selected_association_model == SMP_MODEL_SEC_CONN_JUSTWORKS))) {
  525. SMP_TRACE_ERROR("%s pairing failed - slave requires secure connection only mode",
  526. __func__);
  527. reason = SMP_PAIR_AUTH_FAIL;
  528. smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &reason);
  529. return;
  530. }
  531. uint16_t auth = smp_get_auth_mode(p_cb->selected_association_model);
  532. if(p_cb->peer_auth_req & p_cb->loc_auth_req & SMP_AUTH_GEN_BOND) {
  533. auth |= SMP_AUTH_GEN_BOND;
  534. }
  535. p_cb->auth_mode = auth;
  536. if (p_cb->accept_specified_sec_auth) {
  537. if ((auth & p_cb->origin_loc_auth_req) != p_cb->origin_loc_auth_req ) {
  538. SMP_TRACE_ERROR("%s pairing failed - slave requires auth is 0x%x but peer auth is 0x%x local auth is 0x%x",
  539. __func__, p_cb->origin_loc_auth_req, p_cb->peer_auth_req, p_cb->loc_auth_req);
  540. if (BTM_IsAclConnectionUp(p_cb->pairing_bda, BT_TRANSPORT_LE)) {
  541. btm_remove_acl (p_cb->pairing_bda, BT_TRANSPORT_LE);
  542. }
  543. reason = SMP_PAIR_AUTH_FAIL;
  544. smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &reason);
  545. return;
  546. }
  547. }
  548. if (p_cb->selected_association_model == SMP_MODEL_SEC_CONN_OOB && p_cb->loc_oob_flag == SMP_OOB_PRESENT) {
  549. if (smp_request_oob_data(p_cb)) {
  550. return;
  551. }
  552. } else {
  553. smp_send_pair_rsp(p_cb, NULL);
  554. }
  555. }
  556. } else { /* Master receives pairing response */
  557. p_cb->selected_association_model = smp_select_association_model(p_cb);
  558. if (p_cb->secure_connections_only_mode_required &&
  559. (!(p_cb->le_secure_connections_mode_is_used) ||
  560. (p_cb->selected_association_model == SMP_MODEL_SEC_CONN_JUSTWORKS))) {
  561. SMP_TRACE_ERROR ("Master requires secure connection only mode \
  562. but it can't be provided -> Master fails pairing");
  563. reason = SMP_PAIR_AUTH_FAIL;
  564. smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &reason);
  565. return;
  566. }
  567. uint16_t auth = smp_get_auth_mode(p_cb->selected_association_model);
  568. if(p_cb->peer_auth_req & p_cb->loc_auth_req & SMP_AUTH_GEN_BOND) {
  569. auth |= SMP_AUTH_GEN_BOND;
  570. }
  571. p_cb->auth_mode = auth;
  572. if (p_cb->accept_specified_sec_auth) {
  573. if ((auth & p_cb->origin_loc_auth_req) != p_cb->origin_loc_auth_req ) {
  574. SMP_TRACE_ERROR("%s pairing failed - master requires auth is 0x%x but peer auth is 0x%x local auth is 0x%x",
  575. __func__, p_cb->origin_loc_auth_req, p_cb->peer_auth_req, p_cb->loc_auth_req);
  576. if (BTM_IsAclConnectionUp(p_cb->pairing_bda, BT_TRANSPORT_LE)) {
  577. btm_remove_acl (p_cb->pairing_bda, BT_TRANSPORT_LE);
  578. }
  579. reason = SMP_PAIR_AUTH_FAIL;
  580. smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &reason);
  581. return;
  582. }
  583. }
  584. /* Only if peer oob data present, then should request peer oob data */
  585. if (p_cb->selected_association_model == SMP_MODEL_SEC_CONN_OOB && p_cb->loc_oob_flag == SMP_OOB_PRESENT) {
  586. if (smp_request_oob_data(p_cb)) {
  587. return;
  588. }
  589. } else {
  590. smp_decide_association_model(p_cb, NULL);
  591. }
  592. }
  593. }
  594. #endif ///BLE_INCLUDED == TRUE
  595. /*******************************************************************************
  596. ** Function smp_proc_confirm
  597. ** Description process pairing confirm from peer device
  598. *******************************************************************************/
  599. void smp_proc_confirm(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
  600. {
  601. UINT8 *p = (UINT8 *)p_data;
  602. UINT8 reason = SMP_INVALID_PARAMETERS;
  603. SMP_TRACE_DEBUG("%s\n", __func__);
  604. if (smp_command_has_invalid_parameters(p_cb)) {
  605. smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &reason);
  606. return;
  607. }
  608. if (p != NULL) {
  609. /* save the SConfirm for comparison later */
  610. STREAM_TO_ARRAY(p_cb->rconfirm, p, BT_OCTET16_LEN);
  611. }
  612. p_cb->flags |= SMP_PAIR_FLAGS_CMD_CONFIRM;
  613. }
  614. #if 0 //Unused
  615. /*******************************************************************************
  616. ** Function smp_proc_init
  617. ** Description process pairing initializer from peer device
  618. *******************************************************************************/
  619. void smp_proc_init(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
  620. {
  621. UINT8 *p = (UINT8 *)p_data;
  622. UINT8 reason = SMP_INVALID_PARAMETERS;
  623. SMP_TRACE_DEBUG("%s", __func__);
  624. if (smp_command_has_invalid_parameters(p_cb)) {
  625. smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &reason);
  626. return;
  627. }
  628. /* save the SRand for comparison */
  629. STREAM_TO_ARRAY(p_cb->rrand, p, BT_OCTET16_LEN);
  630. }
  631. #endif
  632. /*******************************************************************************
  633. ** Function smp_proc_rand
  634. ** Description process pairing random (nonce) from peer device
  635. *******************************************************************************/
  636. void smp_proc_rand(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
  637. {
  638. UINT8 *p = (UINT8 *)p_data;
  639. UINT8 reason = SMP_INVALID_PARAMETERS;
  640. SMP_TRACE_DEBUG("%s\n", __func__);
  641. if (smp_command_has_invalid_parameters(p_cb)) {
  642. smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &reason);
  643. return;
  644. }
  645. /* save the SRand for comparison */
  646. STREAM_TO_ARRAY(p_cb->rrand, p, BT_OCTET16_LEN);
  647. }
  648. /*******************************************************************************
  649. ** Function smp_process_pairing_public_key
  650. ** Description process pairing public key command from the peer device
  651. ** - saves the peer public key;
  652. ** - sets the flag indicating that the peer public key is received;
  653. ** - calls smp_wait_for_both_public_keys(...).
  654. **
  655. *******************************************************************************/
  656. void smp_process_pairing_public_key(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
  657. {
  658. UINT8 *p = (UINT8 *)p_data;
  659. UINT8 reason = SMP_INVALID_PARAMETERS;
  660. SMP_TRACE_DEBUG("%s", __func__);
  661. if (smp_command_has_invalid_parameters(p_cb)) {
  662. smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &reason);
  663. return;
  664. }
  665. STREAM_TO_ARRAY(p_cb->peer_publ_key.x, p, BT_OCTET32_LEN);
  666. STREAM_TO_ARRAY(p_cb->peer_publ_key.y, p, BT_OCTET32_LEN);
  667. /* Check if the peer device's and own public key are not same. If they are same then
  668. * return pairing fail. This check is needed to avoid 'Impersonation in Passkey entry
  669. * protocol' vulnerability (CVE-2020-26558).*/
  670. if ((memcmp(p_cb->loc_publ_key.x, p_cb->peer_publ_key.x, sizeof(BT_OCTET32)) == 0)) {
  671. p_cb->status = SMP_PAIR_AUTH_FAIL;
  672. p_cb->failure = SMP_PAIR_AUTH_FAIL;
  673. reason = SMP_PAIR_AUTH_FAIL;
  674. SMP_TRACE_ERROR("%s, Peer and own device cannot have same public key.", __func__);
  675. smp_sm_event(p_cb, SMP_PAIRING_FAILED_EVT, &reason);
  676. return ;
  677. }
  678. /* In order to prevent the x and y coordinates of the public key from being modified,
  679. we need to check whether the x and y coordinates are on the given elliptic curve. */
  680. if (!ECC_CheckPointIsInElliCur_P256((Point *)&p_cb->peer_publ_key)) {
  681. SMP_TRACE_ERROR("%s, Invalid Public key.", __func__);
  682. smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &reason);
  683. }
  684. p_cb->flags |= SMP_PAIR_FLAG_HAVE_PEER_PUBL_KEY;
  685. smp_wait_for_both_public_keys(p_cb, NULL);
  686. }
  687. /*******************************************************************************
  688. ** Function smp_process_pairing_commitment
  689. ** Description process pairing commitment from peer device
  690. *******************************************************************************/
  691. void smp_process_pairing_commitment(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
  692. {
  693. UINT8 *p = (UINT8 *)p_data;
  694. UINT8 reason = SMP_INVALID_PARAMETERS;
  695. SMP_TRACE_DEBUG("%s", __func__);
  696. if (smp_command_has_invalid_parameters(p_cb)) {
  697. smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &reason);
  698. return;
  699. }
  700. p_cb->flags |= SMP_PAIR_FLAG_HAVE_PEER_COMM;
  701. if (p != NULL) {
  702. STREAM_TO_ARRAY(p_cb->remote_commitment, p, BT_OCTET16_LEN);
  703. }
  704. }
  705. /*******************************************************************************
  706. ** Function smp_process_dhkey_check
  707. ** Description process DHKey Check from peer device
  708. *******************************************************************************/
  709. void smp_process_dhkey_check(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
  710. {
  711. UINT8 *p = (UINT8 *)p_data;
  712. UINT8 reason = SMP_INVALID_PARAMETERS;
  713. SMP_TRACE_DEBUG("%s", __func__);
  714. if (smp_command_has_invalid_parameters(p_cb)) {
  715. smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &reason);
  716. return;
  717. }
  718. if (p != NULL) {
  719. STREAM_TO_ARRAY(p_cb->remote_dhkey_check, p, BT_OCTET16_LEN);
  720. }
  721. p_cb->flags |= SMP_PAIR_FLAG_HAVE_PEER_DHK_CHK;
  722. }
  723. /*******************************************************************************
  724. ** Function smp_process_keypress_notification
  725. ** Description process pairing keypress notification from peer device
  726. *******************************************************************************/
  727. void smp_process_keypress_notification(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
  728. {
  729. UINT8 *p = (UINT8 *)p_data;
  730. UINT8 reason = SMP_INVALID_PARAMETERS;
  731. SMP_TRACE_DEBUG("%s", __func__);
  732. p_cb->status = *(UINT8 *)p_data;
  733. if (smp_command_has_invalid_parameters(p_cb)) {
  734. smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &reason);
  735. return;
  736. }
  737. if (p != NULL) {
  738. STREAM_TO_UINT8(p_cb->peer_keypress_notification, p);
  739. } else {
  740. p_cb->peer_keypress_notification = BTM_SP_KEY_OUT_OF_RANGE;
  741. }
  742. p_cb->cb_evt = SMP_PEER_KEYPR_NOT_EVT;
  743. }
  744. #if (CLASSIC_BT_INCLUDED == TRUE)
  745. /*******************************************************************************
  746. ** Function smp_br_process_pairing_command
  747. ** Description Process the SMP pairing request/response from peer device via
  748. ** BR/EDR transport.
  749. *******************************************************************************/
  750. void smp_br_process_pairing_command(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
  751. {
  752. UINT8 *p = (UINT8 *)p_data;
  753. UINT8 reason = SMP_ENC_KEY_SIZE;
  754. tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev (p_cb->pairing_bda);
  755. SMP_TRACE_DEBUG("%s", __func__);
  756. /* rejecting BR pairing request over non-SC BR link */
  757. if (!p_dev_rec->new_encryption_key_is_p256 && p_cb->role == HCI_ROLE_SLAVE) {
  758. reason = SMP_XTRANS_DERIVE_NOT_ALLOW;
  759. smp_br_state_machine_event(p_cb, SMP_BR_AUTH_CMPL_EVT, &reason);
  760. return;
  761. }
  762. #if (BLE_INCLUDED == TRUE)
  763. /* erase all keys if it is slave proc pairing req*/
  764. if (p_dev_rec && (p_cb->role == HCI_ROLE_SLAVE)) {
  765. btm_sec_clear_ble_keys(p_dev_rec);
  766. }
  767. #endif ///BLE_INCLUDED == TRUE
  768. p_cb->flags |= SMP_PAIR_FLAG_ENC_AFTER_PAIR;
  769. STREAM_TO_UINT8(p_cb->peer_io_caps, p);
  770. STREAM_TO_UINT8(p_cb->peer_oob_flag, p);
  771. STREAM_TO_UINT8(p_cb->peer_auth_req, p);
  772. STREAM_TO_UINT8(p_cb->peer_enc_size, p);
  773. STREAM_TO_UINT8(p_cb->peer_i_key, p);
  774. STREAM_TO_UINT8(p_cb->peer_r_key, p);
  775. if (smp_command_has_invalid_parameters(p_cb)) {
  776. reason = SMP_INVALID_PARAMETERS;
  777. smp_br_state_machine_event(p_cb, SMP_BR_AUTH_CMPL_EVT, &reason);
  778. return;
  779. }
  780. /* peer (master) started pairing sending Pairing Request */
  781. /* or being master device always use received i/r key as keys to distribute */
  782. p_cb->local_i_key = p_cb->peer_i_key;
  783. p_cb->local_r_key = p_cb->peer_r_key;
  784. if (p_cb->role == HCI_ROLE_SLAVE) {
  785. p_dev_rec->new_encryption_key_is_p256 = FALSE;
  786. /* shortcut to skip Security Grant step */
  787. p_cb->cb_evt = SMP_BR_KEYS_REQ_EVT;
  788. } else { /* Master receives pairing response */
  789. SMP_TRACE_DEBUG("%s master rcvs valid PAIRING RESPONSE."
  790. " Supposed to move to key distribution phase. ", __func__);
  791. }
  792. /* auth_req received via BR/EDR SM channel is set to 0,
  793. but everything derived/exchanged has to be saved */
  794. p_cb->peer_auth_req |= SMP_AUTH_BOND;
  795. p_cb->loc_auth_req |= SMP_AUTH_BOND;
  796. }
  797. /*******************************************************************************
  798. ** Function smp_br_process_security_grant
  799. ** Description process security grant in case of pairing over BR/EDR transport.
  800. *******************************************************************************/
  801. void smp_br_process_security_grant(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
  802. {
  803. UINT8 res = *(UINT8 *)p_data;
  804. SMP_TRACE_DEBUG("%s", __func__);
  805. if (res != SMP_SUCCESS) {
  806. smp_br_state_machine_event(p_cb, SMP_BR_AUTH_CMPL_EVT, p_data);
  807. } else { /*otherwise, start pairing */
  808. /* send IO request callback */
  809. p_cb->cb_evt = SMP_BR_KEYS_REQ_EVT;
  810. }
  811. }
  812. /*******************************************************************************
  813. ** Function smp_br_check_authorization_request
  814. ** Description sets the SMP kes to be derived/distribute over BR/EDR transport
  815. ** before starting the distribution/derivation
  816. *******************************************************************************/
  817. void smp_br_check_authorization_request(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
  818. {
  819. UINT8 reason = SMP_SUCCESS;
  820. SMP_TRACE_DEBUG("%s rcvs i_keys=0x%x r_keys=0x%x "
  821. "(i-initiator r-responder)", __FUNCTION__, p_cb->local_i_key,
  822. p_cb->local_r_key);
  823. /* In LE SC mode LK field is ignored when BR/EDR transport is used */
  824. p_cb->local_i_key &= ~SMP_SEC_KEY_TYPE_LK;
  825. p_cb->local_r_key &= ~SMP_SEC_KEY_TYPE_LK;
  826. /* In LE SC mode only IRK, IAI, CSRK are exchanged with the peer.
  827. ** Set local_r_key on master to expect only these keys. */
  828. if (p_cb->role == HCI_ROLE_MASTER) {
  829. p_cb->local_r_key &= (SMP_SEC_KEY_TYPE_ID | SMP_SEC_KEY_TYPE_CSRK);
  830. }
  831. SMP_TRACE_DEBUG("%s rcvs upgrades: i_keys=0x%x r_keys=0x%x "
  832. "(i-initiator r-responder)", __FUNCTION__, p_cb->local_i_key,
  833. p_cb->local_r_key);
  834. if (/*((p_cb->peer_auth_req & SMP_AUTH_BOND) ||
  835. (p_cb->loc_auth_req & SMP_AUTH_BOND)) &&*/
  836. (p_cb->local_i_key || p_cb->local_r_key)) {
  837. smp_br_state_machine_event(p_cb, SMP_BR_BOND_REQ_EVT, NULL);
  838. /* if no peer key is expected, start master key distribution */
  839. if (p_cb->role == HCI_ROLE_MASTER && p_cb->local_r_key == 0) {
  840. smp_key_distribution_by_transport(p_cb, NULL);
  841. }
  842. } else {
  843. smp_br_state_machine_event(p_cb, SMP_BR_AUTH_CMPL_EVT, &reason);
  844. }
  845. }
  846. /*******************************************************************************
  847. ** Function smp_br_select_next_key
  848. ** Description selects the next key to derive/send when BR/EDR transport is
  849. ** used.
  850. *******************************************************************************/
  851. void smp_br_select_next_key(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
  852. {
  853. UINT8 reason = SMP_SUCCESS;
  854. SMP_TRACE_DEBUG("%s role=%d (0-master) r_keys=0x%x i_keys=0x%x",
  855. __func__, p_cb->role, p_cb->local_r_key, p_cb->local_i_key);
  856. if (p_cb->role == HCI_ROLE_SLAVE ||
  857. (!p_cb->local_r_key && p_cb->role == HCI_ROLE_MASTER)) {
  858. smp_key_pick_key(p_cb, p_data);
  859. }
  860. if (!p_cb->local_i_key && !p_cb->local_r_key) {
  861. /* state check to prevent re-entrance */
  862. if (smp_get_br_state() == SMP_BR_STATE_BOND_PENDING) {
  863. if (p_cb->total_tx_unacked == 0) {
  864. smp_br_state_machine_event(p_cb, SMP_BR_AUTH_CMPL_EVT, &reason);
  865. } else {
  866. p_cb->wait_for_authorization_complete = TRUE;
  867. }
  868. }
  869. }
  870. }
  871. #endif ///CLASSIC_BT_INCLUDED == TRUE
  872. #if (BLE_INCLUDED == TRUE)
  873. /*******************************************************************************
  874. ** Function smp_proc_enc_info
  875. ** Description process encryption information from peer device
  876. *******************************************************************************/
  877. void smp_proc_enc_info(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
  878. {
  879. UINT8 *p = (UINT8 *)p_data;
  880. SMP_TRACE_DEBUG("%s\n", __func__);
  881. STREAM_TO_ARRAY(p_cb->ltk, p, BT_OCTET16_LEN);
  882. smp_key_distribution(p_cb, NULL);
  883. }
  884. #endif ///BLE_INCLUDED == TRUE
  885. /*******************************************************************************
  886. ** Function smp_proc_master_id
  887. ** Description process master ID from slave device
  888. *******************************************************************************/
  889. void smp_proc_master_id(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
  890. {
  891. UINT8 *p = (UINT8 *)p_data;
  892. tBTM_LE_PENC_KEYS le_key;
  893. SMP_TRACE_DEBUG("%s\np_cb->peer_auth_req = %d,p_cb->loc_auth_req= %d\n", __func__,
  894. p_cb->peer_auth_req, p_cb->loc_auth_req);
  895. smp_update_key_mask (p_cb, SMP_SEC_KEY_TYPE_ENC, TRUE);
  896. STREAM_TO_UINT16(le_key.ediv, p);
  897. STREAM_TO_ARRAY(le_key.rand, p, BT_OCTET8_LEN );
  898. /* store the encryption keys from peer device */
  899. memcpy(le_key.ltk, p_cb->ltk, BT_OCTET16_LEN);
  900. le_key.sec_level = p_cb->sec_level;
  901. le_key.key_size = p_cb->loc_enc_size;
  902. #if (BLE_INCLUDED == TRUE)
  903. if ((p_cb->peer_auth_req & SMP_AUTH_BOND) && (p_cb->loc_auth_req & SMP_AUTH_BOND)) {
  904. btm_sec_save_le_key(p_cb->pairing_bda,
  905. BTM_LE_KEY_PENC,
  906. (tBTM_LE_KEY_VALUE *)&le_key, TRUE);
  907. }
  908. smp_key_distribution(p_cb, NULL);
  909. #endif ///BLE_INCLUDED == TRUE
  910. }
  911. /*******************************************************************************
  912. ** Function smp_proc_enc_info
  913. ** Description process identity information from peer device
  914. *******************************************************************************/
  915. void smp_proc_id_info(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
  916. {
  917. UINT8 *p = (UINT8 *)p_data;
  918. SMP_TRACE_DEBUG("%s", __func__);
  919. STREAM_TO_ARRAY (p_cb->tk, p, BT_OCTET16_LEN); /* reuse TK for IRK */
  920. smp_key_distribution_by_transport(p_cb, NULL);
  921. }
  922. /*******************************************************************************
  923. ** Function smp_proc_id_addr
  924. ** Description process identity address from peer device
  925. *******************************************************************************/
  926. void smp_proc_id_addr(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
  927. {
  928. UINT8 *p = (UINT8 *)p_data;
  929. tBTM_LE_PID_KEYS pid_key;
  930. SMP_TRACE_DEBUG("%s", __func__);
  931. smp_update_key_mask (p_cb, SMP_SEC_KEY_TYPE_ID, TRUE);
  932. STREAM_TO_UINT8(pid_key.addr_type, p);
  933. STREAM_TO_BDADDR(pid_key.static_addr, p);
  934. memcpy(pid_key.irk, p_cb->tk, BT_OCTET16_LEN);
  935. /* to use as BD_ADDR for lk derived from ltk */
  936. p_cb->id_addr_rcvd = TRUE;
  937. p_cb->id_addr_type = pid_key.addr_type;
  938. memcpy(p_cb->id_addr, pid_key.static_addr, BD_ADDR_LEN);
  939. #if (BLE_INCLUDED == TRUE)
  940. /* store the ID key from peer device */
  941. if ((p_cb->peer_auth_req & SMP_AUTH_BOND) && (p_cb->loc_auth_req & SMP_AUTH_BOND)) {
  942. btm_sec_save_le_key(p_cb->pairing_bda, BTM_LE_KEY_PID,
  943. (tBTM_LE_KEY_VALUE *)&pid_key, TRUE);
  944. }
  945. #endif ///BLE_INCLUDED == TRUE
  946. smp_key_distribution_by_transport(p_cb, NULL);
  947. }
  948. /*******************************************************************************
  949. ** Function smp_proc_srk_info
  950. ** Description process security information from peer device
  951. *******************************************************************************/
  952. void smp_proc_srk_info(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
  953. {
  954. #if (BLE_INCLUDED == TRUE)
  955. tBTM_LE_PCSRK_KEYS le_key;
  956. SMP_TRACE_DEBUG("%s", __func__);
  957. smp_update_key_mask (p_cb, SMP_SEC_KEY_TYPE_CSRK, TRUE);
  958. /* save CSRK to security record */
  959. le_key.sec_level = p_cb->sec_level;
  960. memcpy (le_key.csrk, p_data, BT_OCTET16_LEN); /* get peer CSRK */
  961. le_key.counter = 0; /* initialize the peer counter */
  962. if ((p_cb->peer_auth_req & SMP_AUTH_BOND) && (p_cb->loc_auth_req & SMP_AUTH_BOND)) {
  963. btm_sec_save_le_key(p_cb->pairing_bda,
  964. BTM_LE_KEY_PCSRK,
  965. (tBTM_LE_KEY_VALUE *)&le_key, TRUE);
  966. }
  967. #endif ///BLE_INCLUDED == TRUE
  968. smp_key_distribution_by_transport(p_cb, NULL);
  969. }
  970. /*******************************************************************************
  971. ** Function smp_proc_compare
  972. ** Description process compare value
  973. *******************************************************************************/
  974. void smp_proc_compare(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
  975. {
  976. UINT8 reason;
  977. SMP_TRACE_DEBUG("%s\n", __func__);
  978. if (!memcmp(p_cb->rconfirm, p_data->key.p_data, BT_OCTET16_LEN)) {
  979. /* compare the max encryption key size, and save the smaller one for the link */
  980. if ( p_cb->peer_enc_size < p_cb->loc_enc_size) {
  981. p_cb->loc_enc_size = p_cb->peer_enc_size;
  982. }
  983. if (p_cb->role == HCI_ROLE_SLAVE) {
  984. smp_sm_event(p_cb, SMP_RAND_EVT, NULL);
  985. } else {
  986. /* master device always use received i/r key as keys to distribute */
  987. p_cb->local_i_key = p_cb->peer_i_key;
  988. p_cb->local_r_key = p_cb->peer_r_key;
  989. smp_sm_event(p_cb, SMP_ENC_REQ_EVT, NULL);
  990. }
  991. } else {
  992. reason = p_cb->failure = SMP_CONFIRM_VALUE_ERR;
  993. smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &reason);
  994. }
  995. }
  996. #if (BLE_INCLUDED == TRUE)
  997. /*******************************************************************************
  998. ** Function smp_proc_sl_key
  999. ** Description process key ready events.
  1000. *******************************************************************************/
  1001. void smp_proc_sl_key(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
  1002. {
  1003. UINT8 key_type = p_data->key.key_type;
  1004. SMP_TRACE_DEBUG("%s\n", __func__);
  1005. if (key_type == SMP_KEY_TYPE_TK) {
  1006. smp_generate_srand_mrand_confirm(p_cb, NULL);
  1007. } else if (key_type == SMP_KEY_TYPE_CFM) {
  1008. smp_set_state(SMP_STATE_WAIT_CONFIRM);
  1009. if (p_cb->flags & SMP_PAIR_FLAGS_CMD_CONFIRM) {
  1010. smp_sm_event(p_cb, SMP_CONFIRM_EVT, NULL);
  1011. }
  1012. }
  1013. }
  1014. /*******************************************************************************
  1015. ** Function smp_start_enc
  1016. ** Description start encryption
  1017. *******************************************************************************/
  1018. void smp_start_enc(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
  1019. {
  1020. tBTM_STATUS cmd;
  1021. UINT8 reason = SMP_ENC_FAIL;
  1022. SMP_TRACE_DEBUG("%s\n", __func__);
  1023. if (p_data != NULL) {
  1024. cmd = btm_ble_start_encrypt(p_cb->pairing_bda, TRUE, p_data->key.p_data);
  1025. } else {
  1026. cmd = btm_ble_start_encrypt(p_cb->pairing_bda, FALSE, NULL);
  1027. }
  1028. if (cmd != BTM_CMD_STARTED && cmd != BTM_BUSY) {
  1029. smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &reason);
  1030. }
  1031. }
  1032. #endif ///BLE_INCLUDED == TRUE
  1033. /*******************************************************************************
  1034. ** Function smp_proc_discard
  1035. ** Description processing for discard security request
  1036. *******************************************************************************/
  1037. void smp_proc_discard(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
  1038. {
  1039. SMP_TRACE_DEBUG("%s\n", __func__);
  1040. if (!(p_cb->flags & SMP_PAIR_FLAGS_WE_STARTED_DD)) {
  1041. smp_reset_control_value(p_cb);
  1042. }
  1043. }
  1044. /*******************************************************************************
  1045. ** Function smp_enc_cmpl
  1046. ** Description encryption success
  1047. *******************************************************************************/
  1048. void smp_enc_cmpl(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
  1049. {
  1050. UINT8 enc_enable = *(UINT8 *)p_data;
  1051. UINT8 reason = enc_enable ? SMP_SUCCESS : SMP_ENC_FAIL;
  1052. SMP_TRACE_DEBUG("%s\n", __func__);
  1053. smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &reason);
  1054. }
  1055. /*******************************************************************************
  1056. ** Function smp_check_auth_req
  1057. ** Description check authentication request
  1058. *******************************************************************************/
  1059. void smp_check_auth_req(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
  1060. {
  1061. UINT8 enc_enable = *(UINT8 *)p_data;
  1062. UINT8 reason = enc_enable ? SMP_SUCCESS : SMP_ENC_FAIL;
  1063. SMP_TRACE_DEBUG("%s rcvs enc_enable=%d i_keys=0x%x r_keys=0x%x "
  1064. "(i-initiator r-responder)\n",
  1065. __func__, enc_enable, p_cb->local_i_key, p_cb->local_r_key);
  1066. if (enc_enable == 1) {
  1067. if (p_cb->le_secure_connections_mode_is_used) {
  1068. /* In LE SC mode LTK is used instead of STK and has to be always saved */
  1069. p_cb->local_i_key |= SMP_SEC_KEY_TYPE_ENC;
  1070. p_cb->local_r_key |= SMP_SEC_KEY_TYPE_ENC;
  1071. /* In LE SC mode LK is derived from LTK only if both sides request it */
  1072. if (!(p_cb->local_i_key & SMP_SEC_KEY_TYPE_LK) ||
  1073. !(p_cb->local_r_key & SMP_SEC_KEY_TYPE_LK)) {
  1074. p_cb->local_i_key &= ~SMP_SEC_KEY_TYPE_LK;
  1075. p_cb->local_r_key &= ~SMP_SEC_KEY_TYPE_LK;
  1076. }
  1077. /* In LE SC mode only IRK, IAI, CSRK are exchanged with the peer.
  1078. ** Set local_r_key on master to expect only these keys.
  1079. */
  1080. if (p_cb->role == HCI_ROLE_MASTER) {
  1081. p_cb->local_r_key &= (SMP_SEC_KEY_TYPE_ID | SMP_SEC_KEY_TYPE_CSRK);
  1082. }
  1083. } else {
  1084. /* in legacy mode derivation of BR/EDR LK is not supported */
  1085. p_cb->local_i_key &= ~SMP_SEC_KEY_TYPE_LK;
  1086. p_cb->local_r_key &= ~SMP_SEC_KEY_TYPE_LK;
  1087. }
  1088. SMP_TRACE_DEBUG("%s rcvs upgrades: i_keys=0x%x r_keys=0x%x "
  1089. "(i-initiator r-responder)\n",
  1090. __func__, p_cb->local_i_key, p_cb->local_r_key);
  1091. if (/*((p_cb->peer_auth_req & SMP_AUTH_BOND) ||
  1092. (p_cb->loc_auth_req & SMP_AUTH_BOND)) &&*/
  1093. (p_cb->local_i_key || p_cb->local_r_key)) {
  1094. smp_sm_event(p_cb, SMP_BOND_REQ_EVT, NULL);
  1095. } else {
  1096. smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &reason);
  1097. }
  1098. } else if (enc_enable == 0) {
  1099. /* if failed for encryption after pairing, send callback */
  1100. if (p_cb->flags & SMP_PAIR_FLAG_ENC_AFTER_PAIR) {
  1101. smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &reason);
  1102. }
  1103. /* if enc failed for old security information */
  1104. /* if master device, clean up and abck to idle; slave device do nothing */
  1105. else if (p_cb->role == HCI_ROLE_MASTER) {
  1106. smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &reason);
  1107. }
  1108. }
  1109. }
  1110. /*******************************************************************************
  1111. ** Function smp_key_pick_key
  1112. ** Description Pick a key distribution function based on the key mask.
  1113. *******************************************************************************/
  1114. void smp_key_pick_key(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
  1115. {
  1116. UINT8 key_to_dist = (p_cb->role == HCI_ROLE_SLAVE) ? p_cb->local_r_key : p_cb->local_i_key;
  1117. UINT8 i = 0;
  1118. SMP_TRACE_DEBUG("%s key_to_dist=0x%x\n", __func__, key_to_dist);
  1119. while (i < SMP_KEY_DIST_TYPE_MAX) {
  1120. SMP_TRACE_DEBUG("key to send = %02x, i = %d\n", key_to_dist, i);
  1121. if (key_to_dist & (1 << i) && smp_distribute_act[i] != NULL) {
  1122. SMP_TRACE_DEBUG("smp_distribute_act[%d]\n", i);
  1123. (* smp_distribute_act[i])(p_cb, p_data);
  1124. break;
  1125. }
  1126. i ++;
  1127. }
  1128. }
  1129. #if (BLE_INCLUDED == TRUE)
  1130. /*******************************************************************************
  1131. ** Function smp_key_distribution
  1132. ** Description start key distribution if required.
  1133. *******************************************************************************/
  1134. void smp_key_distribution(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
  1135. {
  1136. UINT8 reason = SMP_SUCCESS;
  1137. SMP_TRACE_DEBUG("\n%s role=%d (0-master) r_keys=0x%x i_keys=0x%x\n",
  1138. __func__, p_cb->role, p_cb->local_r_key, p_cb->local_i_key);
  1139. if (p_cb->role == HCI_ROLE_SLAVE ||
  1140. (!p_cb->local_r_key && p_cb->role == HCI_ROLE_MASTER)) {
  1141. smp_key_pick_key(p_cb, p_data);
  1142. }
  1143. if (!p_cb->local_i_key && !p_cb->local_r_key) {
  1144. /* state check to prevent re-entrant */
  1145. if (smp_get_state() == SMP_STATE_BOND_PENDING) {
  1146. if (p_cb->derive_lk) {
  1147. smp_derive_link_key_from_long_term_key(p_cb, NULL);
  1148. p_cb->derive_lk = FALSE;
  1149. }
  1150. if (p_cb->total_tx_unacked == 0) {
  1151. smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &reason);
  1152. } else {
  1153. p_cb->wait_for_authorization_complete = TRUE;
  1154. }
  1155. }
  1156. }
  1157. }
  1158. /*******************************************************************************
  1159. ** Function smp_decide_association_model
  1160. ** Description This function is called to select assoc model to be used for
  1161. ** STK generation and to start STK generation process.
  1162. **
  1163. *******************************************************************************/
  1164. void smp_decide_association_model(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
  1165. {
  1166. UINT8 failure = SMP_UNKNOWN_IO_CAP;
  1167. UINT8 int_evt = 0;
  1168. tSMP_KEY key;
  1169. tSMP_INT_DATA *p = NULL;
  1170. SMP_TRACE_DEBUG("%s Association Model = %d\n", __func__, p_cb->selected_association_model);
  1171. switch (p_cb->selected_association_model) {
  1172. case SMP_MODEL_ENCRYPTION_ONLY: /* TK = 0, go calculate Confirm */
  1173. p_cb->sec_level = SMP_SEC_UNAUTHENTICATE;
  1174. SMP_TRACE_EVENT ("p_cb->sec_level =%d (SMP_SEC_UNAUTHENTICATE) \n", p_cb->sec_level );
  1175. key.key_type = SMP_KEY_TYPE_TK;
  1176. key.p_data = p_cb->tk;
  1177. p = (tSMP_INT_DATA *)&key;
  1178. memset(p_cb->tk, 0, BT_OCTET16_LEN);
  1179. /* TK, ready */
  1180. int_evt = SMP_KEY_READY_EVT;
  1181. break;
  1182. case SMP_MODEL_PASSKEY:
  1183. p_cb->sec_level = SMP_SEC_AUTHENTICATED;
  1184. SMP_TRACE_EVENT ("p_cb->sec_level =%d (SMP_SEC_AUTHENTICATED) \n", p_cb->sec_level );
  1185. p_cb->cb_evt = SMP_PASSKEY_REQ_EVT;
  1186. int_evt = SMP_TK_REQ_EVT;
  1187. break;
  1188. case SMP_MODEL_OOB:
  1189. SMP_TRACE_ERROR ("Association Model = SMP_MODEL_OOB\n");
  1190. p_cb->sec_level = SMP_SEC_AUTHENTICATED;
  1191. SMP_TRACE_EVENT ("p_cb->sec_level =%d (SMP_SEC_AUTHENTICATED) \n", p_cb->sec_level );
  1192. p_cb->cb_evt = SMP_OOB_REQ_EVT;
  1193. int_evt = SMP_TK_REQ_EVT;
  1194. break;
  1195. case SMP_MODEL_KEY_NOTIF:
  1196. p_cb->sec_level = SMP_SEC_AUTHENTICATED;
  1197. SMP_TRACE_DEBUG("Need to generate Passkey\n");
  1198. /* generate passkey and notify application */
  1199. smp_generate_passkey(p_cb, NULL);
  1200. break;
  1201. case SMP_MODEL_SEC_CONN_JUSTWORKS:
  1202. case SMP_MODEL_SEC_CONN_NUM_COMP:
  1203. case SMP_MODEL_SEC_CONN_PASSKEY_ENT:
  1204. case SMP_MODEL_SEC_CONN_PASSKEY_DISP:
  1205. case SMP_MODEL_SEC_CONN_OOB:
  1206. int_evt = SMP_PUBL_KEY_EXCH_REQ_EVT;
  1207. break;
  1208. case SMP_MODEL_OUT_OF_RANGE:
  1209. SMP_TRACE_ERROR("Association Model = SMP_MODEL_OUT_OF_RANGE (failed)\n");
  1210. p = (tSMP_INT_DATA *)&failure;
  1211. int_evt = SMP_AUTH_CMPL_EVT;
  1212. break;
  1213. default:
  1214. SMP_TRACE_ERROR("Association Model = %d (SOMETHING IS WRONG WITH THE CODE)\n",
  1215. p_cb->selected_association_model);
  1216. p = (tSMP_INT_DATA *)&failure;
  1217. int_evt = SMP_AUTH_CMPL_EVT;
  1218. }
  1219. SMP_TRACE_EVENT ("sec_level=%d \n", p_cb->sec_level );
  1220. if (int_evt) {
  1221. smp_sm_event(p_cb, int_evt, p);
  1222. }
  1223. }
  1224. /*******************************************************************************
  1225. ** Function smp_process_io_response
  1226. ** Description process IO response for a slave device.
  1227. *******************************************************************************/
  1228. void smp_process_io_response(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
  1229. {
  1230. uint8_t reason = SMP_PAIR_AUTH_FAIL;
  1231. SMP_TRACE_DEBUG("%s\n", __func__);
  1232. if (p_cb->flags & SMP_PAIR_FLAGS_WE_STARTED_DD) {
  1233. /* pairing started by local (slave) Security Request */
  1234. smp_set_state(SMP_STATE_SEC_REQ_PENDING);
  1235. smp_send_cmd(SMP_OPCODE_SEC_REQ, p_cb);
  1236. } else { /* plan to send pairing respond */
  1237. /* pairing started by peer (master) Pairing Request */
  1238. p_cb->selected_association_model = smp_select_association_model(p_cb);
  1239. if (p_cb->secure_connections_only_mode_required &&
  1240. (!(p_cb->le_secure_connections_mode_is_used) ||
  1241. (p_cb->selected_association_model == SMP_MODEL_SEC_CONN_JUSTWORKS))) {
  1242. SMP_TRACE_ERROR ("Slave requires secure connection only mode \
  1243. but it can't be provided -> Slave fails pairing\n");
  1244. smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &reason);
  1245. return;
  1246. }
  1247. uint16_t auth = smp_get_auth_mode(p_cb->selected_association_model);
  1248. if(p_cb->peer_auth_req & p_cb->loc_auth_req & SMP_AUTH_GEN_BOND) {
  1249. auth |= SMP_AUTH_GEN_BOND;
  1250. }
  1251. p_cb->auth_mode = auth;
  1252. if (p_cb->accept_specified_sec_auth) {
  1253. if ((auth & p_cb->origin_loc_auth_req) != p_cb->origin_loc_auth_req ) {
  1254. SMP_TRACE_ERROR("pairing failed - slave requires auth is 0x%x but peer auth is 0x%x local auth is 0x%x",
  1255. p_cb->origin_loc_auth_req, p_cb->peer_auth_req, p_cb->loc_auth_req);
  1256. if (BTM_IsAclConnectionUp(p_cb->pairing_bda, BT_TRANSPORT_LE)) {
  1257. btm_remove_acl (p_cb->pairing_bda, BT_TRANSPORT_LE);
  1258. }
  1259. reason = SMP_PAIR_AUTH_FAIL;
  1260. smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &reason);
  1261. return;
  1262. }
  1263. }
  1264. if (p_cb->selected_association_model == SMP_MODEL_SEC_CONN_OOB && p_cb->loc_oob_flag == SMP_OOB_PRESENT) {
  1265. if (smp_request_oob_data(p_cb)) {
  1266. return;
  1267. }
  1268. }
  1269. smp_send_pair_rsp(p_cb, NULL);
  1270. }
  1271. }
  1272. #endif ///BLE_INCLUDED == TRUE
  1273. /*******************************************************************************
  1274. ** Function smp_br_process_slave_keys_response
  1275. ** Description process application keys response for a slave device
  1276. ** (BR/EDR transport).
  1277. *******************************************************************************/
  1278. void smp_br_process_slave_keys_response(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
  1279. {
  1280. smp_br_send_pair_response(p_cb, NULL);
  1281. }
  1282. /*******************************************************************************
  1283. ** Function smp_br_send_pair_response
  1284. ** Description actions related to sending pairing response over BR/EDR transport.
  1285. *******************************************************************************/
  1286. void smp_br_send_pair_response(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
  1287. {
  1288. SMP_TRACE_DEBUG("%s\n", __func__);
  1289. p_cb->local_i_key &= p_cb->peer_i_key;
  1290. p_cb->local_r_key &= p_cb->peer_r_key;
  1291. smp_send_cmd (SMP_OPCODE_PAIRING_RSP, p_cb);
  1292. }
  1293. /*******************************************************************************
  1294. ** Function smp_pairing_cmpl
  1295. ** Description This function is called to send the pairing complete callback
  1296. ** and remove the connection if needed.
  1297. *******************************************************************************/
  1298. void smp_pairing_cmpl(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
  1299. {
  1300. if (p_cb->total_tx_unacked == 0) {
  1301. /* process the pairing complete */
  1302. smp_proc_pairing_cmpl(p_cb);
  1303. }
  1304. }
  1305. /*******************************************************************************
  1306. ** Function smp_pair_terminate
  1307. ** Description This function is called to send the pairing complete callback
  1308. ** and remove the connection if needed.
  1309. *******************************************************************************/
  1310. void smp_pair_terminate(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
  1311. {
  1312. SMP_TRACE_DEBUG("%s\n", __func__);
  1313. p_cb->status = SMP_CONN_TOUT;
  1314. smp_proc_pairing_cmpl(p_cb);
  1315. }
  1316. /*******************************************************************************
  1317. ** Function smp_idle_terminate
  1318. ** Description This function calledin idle state to determine to send authentication
  1319. ** complete or not.
  1320. *******************************************************************************/
  1321. void smp_idle_terminate(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
  1322. {
  1323. if (p_cb->flags & SMP_PAIR_FLAGS_WE_STARTED_DD) {
  1324. SMP_TRACE_DEBUG("Pairing terminated at IDLE state.\n");
  1325. p_cb->status = SMP_FAIL;
  1326. smp_proc_pairing_cmpl(p_cb);
  1327. }
  1328. }
  1329. #if (BLE_INCLUDED == TRUE)
  1330. /*******************************************************************************
  1331. ** Function smp_fast_conn_param
  1332. ** Description apply default connection parameter for pairing process
  1333. *******************************************************************************/
  1334. void smp_fast_conn_param(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
  1335. {
  1336. if(p_cb->role == BTM_ROLE_MASTER) {
  1337. #if (BT_MULTI_CONNECTION_ENBALE == FALSE)
  1338. L2CA_EnableUpdateBleConnParams(p_cb->pairing_bda, FALSE);
  1339. #endif
  1340. } else {
  1341. #if (SMP_SLAVE_CON_PARAMS_UPD_ENABLE == TRUE)
  1342. tBTM_SEC_DEV_REC *p_rec = btm_find_dev (p_cb->pairing_bda);
  1343. if(p_rec && p_rec->ble.skip_update_conn_param) {
  1344. //do nothing
  1345. return;
  1346. }
  1347. /* Disable L2CAP connection parameter updates while bonding since
  1348. some peripherals are not able to revert to fast connection parameters
  1349. during the start of service discovery. Connection paramter updates
  1350. get enabled again once service discovery completes. */
  1351. #if (BT_MULTI_CONNECTION_ENBALE == FALSE)
  1352. L2CA_EnableUpdateBleConnParams(p_cb->pairing_bda, FALSE);
  1353. #endif
  1354. #endif
  1355. }
  1356. }
  1357. /*******************************************************************************
  1358. ** Function smp_both_have_public_keys
  1359. ** Description The function is called when both local and peer public keys are
  1360. ** saved.
  1361. ** Actions:
  1362. ** - invokes DHKey computation;
  1363. ** - on slave side invokes sending local public key to the peer.
  1364. ** - invokes SC phase 1 process.
  1365. *******************************************************************************/
  1366. void smp_both_have_public_keys(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
  1367. {
  1368. SMP_TRACE_DEBUG("%s\n", __func__);
  1369. /* invokes DHKey computation */
  1370. smp_compute_dhkey(p_cb);
  1371. /* on slave side invokes sending local public key to the peer */
  1372. if (p_cb->role == HCI_ROLE_SLAVE) {
  1373. smp_send_pair_public_key(p_cb, NULL);
  1374. }
  1375. smp_sm_event(p_cb, SMP_SC_DHKEY_CMPLT_EVT, NULL);
  1376. }
  1377. /*******************************************************************************
  1378. ** Function smp_start_secure_connection_phase1
  1379. ** Description The function starts Secure Connection phase1 i.e. invokes initialization of Secure Connection
  1380. ** phase 1 parameters and starts building/sending to the peer
  1381. ** messages appropriate for the role and association model.
  1382. *******************************************************************************/
  1383. void smp_start_secure_connection_phase1(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
  1384. {
  1385. SMP_TRACE_DEBUG("%s\n", __func__);
  1386. if (p_cb->selected_association_model == SMP_MODEL_SEC_CONN_JUSTWORKS) {
  1387. p_cb->sec_level = SMP_SEC_UNAUTHENTICATE;
  1388. SMP_TRACE_EVENT ("p_cb->sec_level =%d (SMP_SEC_UNAUTHENTICATE)\n ", p_cb->sec_level );
  1389. } else {
  1390. p_cb->sec_level = SMP_SEC_AUTHENTICATED;
  1391. SMP_TRACE_EVENT ("p_cb->sec_level =%d (SMP_SEC_AUTHENTICATED)\n ", p_cb->sec_level );
  1392. }
  1393. switch (p_cb->selected_association_model) {
  1394. case SMP_MODEL_SEC_CONN_JUSTWORKS:
  1395. case SMP_MODEL_SEC_CONN_NUM_COMP:
  1396. memset(p_cb->local_random, 0, BT_OCTET16_LEN);
  1397. smp_start_nonce_generation(p_cb);
  1398. break;
  1399. case SMP_MODEL_SEC_CONN_PASSKEY_ENT:
  1400. /* user has to provide passkey */
  1401. p_cb->cb_evt = SMP_PASSKEY_REQ_EVT;
  1402. smp_sm_event(p_cb, SMP_TK_REQ_EVT, NULL);
  1403. break;
  1404. case SMP_MODEL_SEC_CONN_PASSKEY_DISP:
  1405. /* passkey has to be provided to user */
  1406. SMP_TRACE_DEBUG("Need to generate SC Passkey\n");
  1407. smp_generate_passkey(p_cb, NULL);
  1408. break;
  1409. case SMP_MODEL_SEC_CONN_OOB:
  1410. /* use the available OOB information */
  1411. smp_process_secure_connection_oob_data(p_cb, NULL);
  1412. break;
  1413. default:
  1414. SMP_TRACE_ERROR ("Association Model = %d is not used in LE SC\n",
  1415. p_cb->selected_association_model);
  1416. break;
  1417. }
  1418. }
  1419. /*******************************************************************************
  1420. ** Function smp_process_local_nonce
  1421. ** Description The function processes new local nonce.
  1422. **
  1423. ** Note It is supposed to be called in SC phase1.
  1424. *******************************************************************************/
  1425. void smp_process_local_nonce(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
  1426. {
  1427. SMP_TRACE_DEBUG("%s\n", __func__);
  1428. switch (p_cb->selected_association_model) {
  1429. case SMP_MODEL_SEC_CONN_JUSTWORKS:
  1430. case SMP_MODEL_SEC_CONN_NUM_COMP:
  1431. if (p_cb->role == HCI_ROLE_SLAVE) {
  1432. /* slave calculates and sends local commitment */
  1433. smp_calculate_local_commitment(p_cb);
  1434. smp_send_commitment(p_cb, NULL);
  1435. /* slave has to wait for peer nonce */
  1436. smp_set_state(SMP_STATE_WAIT_NONCE);
  1437. } else { /* i.e. master */
  1438. if (p_cb->flags & SMP_PAIR_FLAG_HAVE_PEER_COMM) {
  1439. /* slave commitment is already received, send local nonce, wait for remote nonce*/
  1440. SMP_TRACE_DEBUG("master in assoc mode = %d \
  1441. already rcvd slave commitment - race condition\n",
  1442. p_cb->selected_association_model);
  1443. p_cb->flags &= ~SMP_PAIR_FLAG_HAVE_PEER_COMM;
  1444. smp_send_rand(p_cb, NULL);
  1445. smp_set_state(SMP_STATE_WAIT_NONCE);
  1446. }
  1447. }
  1448. break;
  1449. case SMP_MODEL_SEC_CONN_PASSKEY_ENT:
  1450. case SMP_MODEL_SEC_CONN_PASSKEY_DISP:
  1451. smp_calculate_local_commitment(p_cb);
  1452. if (p_cb->role == HCI_ROLE_MASTER) {
  1453. smp_send_commitment(p_cb, NULL);
  1454. } else { /* slave */
  1455. if (p_cb->flags & SMP_PAIR_FLAG_HAVE_PEER_COMM) {
  1456. /* master commitment is already received */
  1457. smp_send_commitment(p_cb, NULL);
  1458. smp_set_state(SMP_STATE_WAIT_NONCE);
  1459. }
  1460. }
  1461. break;
  1462. case SMP_MODEL_SEC_CONN_OOB:
  1463. if (p_cb->role == HCI_ROLE_MASTER) {
  1464. smp_send_rand(p_cb, NULL);
  1465. }
  1466. smp_set_state(SMP_STATE_WAIT_NONCE);
  1467. break;
  1468. default:
  1469. SMP_TRACE_ERROR ("Association Model = %d is not used in LE SC\n",
  1470. p_cb->selected_association_model);
  1471. break;
  1472. }
  1473. }
  1474. /*******************************************************************************
  1475. ** Function smp_process_peer_nonce
  1476. ** Description The function processes newly received and saved in CB peer nonce.
  1477. ** The actions depend on the selected association model and the role.
  1478. **
  1479. ** Note It is supposed to be called in SC phase1.
  1480. *******************************************************************************/
  1481. void smp_process_peer_nonce(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
  1482. {
  1483. UINT8 reason;
  1484. SMP_TRACE_DEBUG("%s start \n", __func__);
  1485. switch (p_cb->selected_association_model) {
  1486. case SMP_MODEL_SEC_CONN_JUSTWORKS:
  1487. case SMP_MODEL_SEC_CONN_NUM_COMP:
  1488. /* in these models only master receives commitment */
  1489. if (p_cb->role == HCI_ROLE_MASTER) {
  1490. if (!smp_check_commitment(p_cb)) {
  1491. reason = p_cb->failure = SMP_CONFIRM_VALUE_ERR;
  1492. smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &reason);
  1493. break;
  1494. }
  1495. } else {
  1496. /* slave sends local nonce */
  1497. smp_send_rand(p_cb, NULL);
  1498. }
  1499. if (p_cb->selected_association_model == SMP_MODEL_SEC_CONN_JUSTWORKS) {
  1500. /* go directly to phase 2 */
  1501. smp_sm_event(p_cb, SMP_SC_PHASE1_CMPLT_EVT, NULL);
  1502. } else { /* numeric comparison */
  1503. smp_set_state(SMP_STATE_WAIT_NONCE);
  1504. smp_sm_event(p_cb, SMP_SC_CALC_NC_EVT, NULL);
  1505. }
  1506. break;
  1507. case SMP_MODEL_SEC_CONN_PASSKEY_ENT:
  1508. case SMP_MODEL_SEC_CONN_PASSKEY_DISP:
  1509. if (!smp_check_commitment(p_cb)) {
  1510. reason = p_cb->failure = SMP_CONFIRM_VALUE_ERR;
  1511. smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &reason);
  1512. break;
  1513. }
  1514. if (p_cb->role == HCI_ROLE_SLAVE) {
  1515. smp_send_rand(p_cb, NULL);
  1516. }
  1517. if (++p_cb->round < 20) {
  1518. smp_set_state(SMP_STATE_SEC_CONN_PHS1_START);
  1519. p_cb->flags &= ~SMP_PAIR_FLAG_HAVE_PEER_COMM;
  1520. smp_start_nonce_generation(p_cb);
  1521. break;
  1522. }
  1523. smp_sm_event(p_cb, SMP_SC_PHASE1_CMPLT_EVT, NULL);
  1524. break;
  1525. case SMP_MODEL_SEC_CONN_OOB:
  1526. if (p_cb->role == HCI_ROLE_SLAVE) {
  1527. smp_send_rand(p_cb, NULL);
  1528. }
  1529. smp_sm_event(p_cb, SMP_SC_PHASE1_CMPLT_EVT, NULL);
  1530. break;
  1531. default:
  1532. SMP_TRACE_ERROR ("Association Model = %d is not used in LE SC\n",
  1533. p_cb->selected_association_model);
  1534. break;
  1535. }
  1536. SMP_TRACE_DEBUG("%s end\n ", __FUNCTION__);
  1537. }
  1538. #endif ///BLE_INCLUDED == TRUE
  1539. /*******************************************************************************
  1540. ** Function smp_match_dhkey_checks
  1541. ** Description checks if the calculated peer DHKey Check value is the same as
  1542. ** received from the peer DHKey check value.
  1543. *******************************************************************************/
  1544. void smp_match_dhkey_checks(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
  1545. {
  1546. UINT8 reason = SMP_DHKEY_CHK_FAIL;
  1547. SMP_TRACE_DEBUG("%s\n", __func__);
  1548. if (memcmp(p_data->key.p_data, p_cb->remote_dhkey_check, BT_OCTET16_LEN)) {
  1549. SMP_TRACE_WARNING ("dhkey checks do no match\n");
  1550. p_cb->failure = reason;
  1551. smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &reason);
  1552. return;
  1553. }
  1554. SMP_TRACE_EVENT ("dhkey checks match\n");
  1555. /* compare the max encryption key size, and save the smaller one for the link */
  1556. if (p_cb->peer_enc_size < p_cb->loc_enc_size) {
  1557. p_cb->loc_enc_size = p_cb->peer_enc_size;
  1558. }
  1559. if (p_cb->role == HCI_ROLE_SLAVE) {
  1560. smp_sm_event(p_cb, SMP_PAIR_DHKEY_CHCK_EVT, NULL);
  1561. } else {
  1562. /* master device always use received i/r key as keys to distribute */
  1563. p_cb->local_i_key = p_cb->peer_i_key;
  1564. p_cb->local_r_key = p_cb->peer_r_key;
  1565. smp_sm_event(p_cb, SMP_ENC_REQ_EVT, NULL);
  1566. }
  1567. }
  1568. /*******************************************************************************
  1569. ** Function smp_move_to_secure_connections_phase2
  1570. ** Description Signal State Machine to start SC phase 2 initialization (to
  1571. ** compute local DHKey Check value).
  1572. **
  1573. ** Note SM is supposed to be in the state SMP_STATE_SEC_CONN_PHS2_START.
  1574. *******************************************************************************/
  1575. void smp_move_to_secure_connections_phase2(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
  1576. {
  1577. SMP_TRACE_DEBUG("%s\n", __func__);
  1578. smp_sm_event(p_cb, SMP_SC_PHASE1_CMPLT_EVT, NULL);
  1579. }
  1580. /*******************************************************************************
  1581. ** Function smp_phase_2_dhkey_checks_are_present
  1582. ** Description generates event if dhkey check from the peer is already received.
  1583. **
  1584. ** Note It is supposed to be used on slave to prevent race condition.
  1585. ** It is supposed to be called after slave dhkey check is calculated.
  1586. *******************************************************************************/
  1587. void smp_phase_2_dhkey_checks_are_present(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
  1588. {
  1589. SMP_TRACE_DEBUG("%s\n", __func__);
  1590. if (p_cb->flags & SMP_PAIR_FLAG_HAVE_PEER_DHK_CHK) {
  1591. smp_sm_event(p_cb, SMP_SC_2_DHCK_CHKS_PRES_EVT, NULL);
  1592. }
  1593. }
  1594. /*******************************************************************************
  1595. ** Function smp_wait_for_both_public_keys
  1596. ** Description generates SMP_BOTH_PUBL_KEYS_RCVD_EVT event when both local and master
  1597. ** public keys are available.
  1598. **
  1599. ** Note on the slave it is used to prevent race condition.
  1600. **
  1601. *******************************************************************************/
  1602. void smp_wait_for_both_public_keys(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
  1603. {
  1604. SMP_TRACE_DEBUG("%s\n", __func__);
  1605. if ((p_cb->flags & SMP_PAIR_FLAG_HAVE_PEER_PUBL_KEY) &&
  1606. (p_cb->flags & SMP_PAIR_FLAG_HAVE_LOCAL_PUBL_KEY)) {
  1607. if ((p_cb->role == HCI_ROLE_SLAVE) &&
  1608. ((p_cb->req_oob_type == SMP_OOB_LOCAL) || (p_cb->req_oob_type == SMP_OOB_BOTH))) {
  1609. smp_set_state(SMP_STATE_PUBLIC_KEY_EXCH);
  1610. }
  1611. smp_sm_event(p_cb, SMP_BOTH_PUBL_KEYS_RCVD_EVT, NULL);
  1612. }
  1613. }
  1614. #if (BLE_INCLUDED == TRUE)
  1615. /*******************************************************************************
  1616. ** Function smp_start_passkey_verification
  1617. ** Description Starts SC passkey entry verification.
  1618. *******************************************************************************/
  1619. void smp_start_passkey_verification(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
  1620. {
  1621. UINT8 *p = NULL;
  1622. SMP_TRACE_DEBUG("%s\n", __func__);
  1623. p = p_cb->local_random;
  1624. UINT32_TO_STREAM(p, p_data->passkey);
  1625. p = p_cb->peer_random;
  1626. UINT32_TO_STREAM(p, p_data->passkey);
  1627. p_cb->round = 0;
  1628. smp_start_nonce_generation(p_cb);
  1629. }
  1630. /*******************************************************************************
  1631. ** Function smp_process_secure_connection_oob_data
  1632. ** Description Processes local/peer SC OOB data received from somewhere.
  1633. *******************************************************************************/
  1634. void smp_process_secure_connection_oob_data(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
  1635. {
  1636. SMP_TRACE_DEBUG("%s\n", __func__);
  1637. tSMP_SC_OOB_DATA *p_sc_oob_data = &p_cb->sc_oob_data;
  1638. if (p_sc_oob_data->loc_oob_data.present) {
  1639. memcpy(p_cb->local_random, p_sc_oob_data->loc_oob_data.randomizer,
  1640. sizeof(p_cb->local_random));
  1641. } else {
  1642. SMP_TRACE_EVENT ("local OOB randomizer is absent\n");
  1643. memset(p_cb->local_random, 0, sizeof (p_cb->local_random));
  1644. }
  1645. if (!p_sc_oob_data->peer_oob_data.present) {
  1646. SMP_TRACE_EVENT ("peer OOB data is absent\n");
  1647. memset(p_cb->peer_random, 0, sizeof (p_cb->peer_random));
  1648. } else {
  1649. memcpy(p_cb->peer_random, p_sc_oob_data->peer_oob_data.randomizer,
  1650. sizeof(p_cb->peer_random));
  1651. memcpy(p_cb->remote_commitment, p_sc_oob_data->peer_oob_data.commitment,
  1652. sizeof(p_cb->remote_commitment));
  1653. UINT8 reason = SMP_CONFIRM_VALUE_ERR;
  1654. /* check commitment */
  1655. if (!smp_check_commitment(p_cb)) {
  1656. p_cb->failure = reason;
  1657. smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &reason);
  1658. return;
  1659. }
  1660. if (p_cb->peer_oob_flag != SMP_OOB_PRESENT) {
  1661. /* the peer doesn't have local randomiser */
  1662. SMP_TRACE_EVENT ("peer didn't receive local OOB data, set local randomizer to 0\n");
  1663. memset(p_cb->local_random, 0, sizeof (p_cb->local_random));
  1664. }
  1665. }
  1666. print128(p_cb->local_random, (const UINT8 *)"local OOB randomizer");
  1667. print128(p_cb->peer_random, (const UINT8 *)"peer OOB randomizer");
  1668. smp_start_nonce_generation(p_cb);
  1669. }
  1670. /*******************************************************************************
  1671. ** Function smp_set_local_oob_keys
  1672. ** Description Saves calculated private/public keys in sc_oob_data.loc_oob_data,
  1673. ** starts nonce generation
  1674. ** (to be saved in sc_oob_data.loc_oob_data.randomizer).
  1675. *******************************************************************************/
  1676. void smp_set_local_oob_keys(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
  1677. {
  1678. SMP_TRACE_DEBUG("%s\n", __func__);
  1679. memcpy(p_cb->sc_oob_data.loc_oob_data.private_key_used, p_cb->private_key,
  1680. BT_OCTET32_LEN);
  1681. p_cb->sc_oob_data.loc_oob_data.publ_key_used = p_cb->loc_publ_key;
  1682. smp_start_nonce_generation(p_cb);
  1683. }
  1684. /*******************************************************************************
  1685. ** Function smp_set_local_oob_random_commitment
  1686. ** Description Saves calculated randomizer and commitment in sc_oob_data.loc_oob_data,
  1687. ** passes sc_oob_data.loc_oob_data up for safekeeping.
  1688. *******************************************************************************/
  1689. void smp_set_local_oob_random_commitment(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
  1690. {
  1691. SMP_TRACE_DEBUG("%s\n", __func__);
  1692. memcpy(p_cb->sc_oob_data.loc_oob_data.randomizer, p_cb->rand,
  1693. BT_OCTET16_LEN);
  1694. smp_calculate_f4(p_cb->sc_oob_data.loc_oob_data.publ_key_used.x,
  1695. p_cb->sc_oob_data.loc_oob_data.publ_key_used.x,
  1696. p_cb->sc_oob_data.loc_oob_data.randomizer, 0,
  1697. p_cb->sc_oob_data.loc_oob_data.commitment);
  1698. p_cb->sc_oob_data.loc_oob_data.present = true;
  1699. #if SMP_DEBUG == TRUE
  1700. UINT8 *p_print = NULL;
  1701. SMP_TRACE_DEBUG("local SC OOB data set:\n");
  1702. p_print = (UINT8 *) &p_cb->sc_oob_data.loc_oob_data.addr_sent_to;
  1703. smp_debug_print_nbyte_little_endian (p_print, (const UINT8 *)"addr_sent_to",
  1704. sizeof(tBLE_BD_ADDR));
  1705. p_print = (UINT8 *) &p_cb->sc_oob_data.loc_oob_data.private_key_used;
  1706. smp_debug_print_nbyte_little_endian (p_print, (const UINT8 *)"private_key_used",
  1707. BT_OCTET32_LEN);
  1708. p_print = (UINT8 *) &p_cb->sc_oob_data.loc_oob_data.publ_key_used.x;
  1709. smp_debug_print_nbyte_little_endian (p_print, (const UINT8 *)"publ_key_used.x",
  1710. BT_OCTET32_LEN);
  1711. p_print = (UINT8 *) &p_cb->sc_oob_data.loc_oob_data.publ_key_used.y;
  1712. smp_debug_print_nbyte_little_endian (p_print, (const UINT8 *)"publ_key_used.y",
  1713. BT_OCTET32_LEN);
  1714. p_print = (UINT8 *) &p_cb->sc_oob_data.loc_oob_data.randomizer;
  1715. smp_debug_print_nbyte_little_endian (p_print, (const UINT8 *)"randomizer",
  1716. BT_OCTET16_LEN);
  1717. p_print = (UINT8 *) &p_cb->sc_oob_data.loc_oob_data.commitment;
  1718. smp_debug_print_nbyte_little_endian (p_print, (const UINT8 *) "commitment",
  1719. BT_OCTET16_LEN);
  1720. SMP_TRACE_DEBUG("");
  1721. #endif
  1722. /* pass created OOB data up */
  1723. p_cb->cb_evt = SMP_SC_LOC_OOB_DATA_UP_EVT;
  1724. smp_send_app_cback(p_cb, NULL);
  1725. // Store the data for later use when we are paired with
  1726. smp_save_local_oob_data(p_cb);
  1727. smp_cb_cleanup(p_cb);
  1728. }
  1729. /*******************************************************************************
  1730. **
  1731. ** Function smp_link_encrypted
  1732. **
  1733. ** Description This function is called when link is encrypted and notified to
  1734. ** slave device. Proceed to to send LTK, DIV and ER to master if
  1735. ** bonding the devices.
  1736. **
  1737. **
  1738. ** Returns void
  1739. **
  1740. *******************************************************************************/
  1741. void smp_link_encrypted(BD_ADDR bda, UINT8 encr_enable)
  1742. {
  1743. tSMP_CB *p_cb = &smp_cb;
  1744. tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev (bda);
  1745. SMP_TRACE_DEBUG("%s encr_enable=%d\n", __func__, encr_enable);
  1746. if (memcmp(&smp_cb.pairing_bda[0], bda, BD_ADDR_LEN) == 0) {
  1747. /* encryption completed with STK, remmeber the key size now, could be overwite
  1748. * when key exchange happens */
  1749. if (p_cb->loc_enc_size != 0 && encr_enable) {
  1750. /* update the link encryption key size if a SMP pairing just performed */
  1751. btm_ble_update_sec_key_size(bda, p_cb->loc_enc_size);
  1752. }
  1753. smp_sm_event(&smp_cb, SMP_ENCRYPTED_EVT, &encr_enable);
  1754. } else if (p_dev_rec && !p_dev_rec->role_master && !p_dev_rec->enc_init_by_we ){
  1755. /*
  1756. if enc_init_by_we is false, it means that client initiates encryption before slave calls esp_ble_set_encryption()
  1757. we need initiate pairing_bda and p_cb->role then encryption, for example iPhones
  1758. */
  1759. memcpy(&smp_cb.pairing_bda[0], bda, BD_ADDR_LEN);
  1760. p_cb->state = SMP_STATE_ENCRYPTION_PENDING;
  1761. p_cb->role = HCI_ROLE_SLAVE;
  1762. p_dev_rec->enc_init_by_we = FALSE;
  1763. smp_sm_event(&smp_cb, SMP_ENCRYPTED_EVT, &encr_enable);
  1764. } else if (p_dev_rec && p_dev_rec->role_master && p_dev_rec->enc_init_by_we){
  1765. memcpy(&smp_cb.pairing_bda[0], bda, BD_ADDR_LEN);
  1766. p_cb->state = SMP_STATE_ENCRYPTION_PENDING;
  1767. p_cb->role = HCI_ROLE_MASTER;
  1768. p_dev_rec->enc_init_by_we = FALSE;
  1769. smp_sm_event(&smp_cb, SMP_ENCRYPTED_EVT, &encr_enable);
  1770. }
  1771. }
  1772. /*******************************************************************************
  1773. **
  1774. ** Function smp_proc_ltk_request
  1775. **
  1776. ** Description This function is called when LTK request is received from
  1777. ** controller.
  1778. **
  1779. ** Returns void
  1780. **
  1781. *******************************************************************************/
  1782. BOOLEAN smp_proc_ltk_request(BD_ADDR bda)
  1783. {
  1784. SMP_TRACE_DEBUG("%s state = %d\n", __func__, smp_cb.state);
  1785. BOOLEAN match = FALSE;
  1786. if (!memcmp(bda, smp_cb.pairing_bda, BD_ADDR_LEN)) {
  1787. match = TRUE;
  1788. } else {
  1789. BD_ADDR dummy_bda = {0};
  1790. tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev(bda);
  1791. if (p_dev_rec != NULL &&
  1792. 0 == memcmp(p_dev_rec->ble.pseudo_addr, smp_cb.pairing_bda, BD_ADDR_LEN) &&
  1793. 0 != memcmp(p_dev_rec->ble.pseudo_addr, dummy_bda, BD_ADDR_LEN)) {
  1794. match = TRUE;
  1795. }
  1796. }
  1797. if (match && smp_cb.state == SMP_STATE_ENCRYPTION_PENDING) {
  1798. smp_sm_event(&smp_cb, SMP_ENC_REQ_EVT, NULL);
  1799. return TRUE;
  1800. }
  1801. return FALSE;
  1802. }
  1803. /*******************************************************************************
  1804. **
  1805. ** Function smp_process_secure_connection_long_term_key
  1806. **
  1807. ** Description This function is called to process SC LTK.
  1808. ** SC LTK is calculated and used instead of STK.
  1809. ** Here SC LTK is saved in BLE DB.
  1810. **
  1811. ** Returns void
  1812. **
  1813. *******************************************************************************/
  1814. void smp_process_secure_connection_long_term_key(void)
  1815. {
  1816. tSMP_CB *p_cb = &smp_cb;
  1817. SMP_TRACE_DEBUG("%s\n", __func__);
  1818. smp_save_secure_connections_long_term_key(p_cb);
  1819. smp_update_key_mask (p_cb, SMP_SEC_KEY_TYPE_ENC, FALSE);
  1820. smp_key_distribution(p_cb, NULL);
  1821. }
  1822. /*******************************************************************************
  1823. **
  1824. ** Function smp_set_derive_link_key
  1825. **
  1826. ** Description This function is called to set flag that indicates that
  1827. ** BR/EDR LK has to be derived from LTK after all keys are
  1828. ** distributed.
  1829. **
  1830. ** Returns void
  1831. **
  1832. *******************************************************************************/
  1833. void smp_set_derive_link_key(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
  1834. {
  1835. SMP_TRACE_DEBUG ("%s\n", __func__);
  1836. p_cb->derive_lk = TRUE;
  1837. smp_update_key_mask (p_cb, SMP_SEC_KEY_TYPE_LK, FALSE);
  1838. smp_key_distribution(p_cb, NULL);
  1839. }
  1840. /*******************************************************************************
  1841. **
  1842. ** Function smp_derive_link_key_from_long_term_key
  1843. **
  1844. ** Description This function is called to derive BR/EDR LK from LTK.
  1845. **
  1846. ** Returns void
  1847. **
  1848. *******************************************************************************/
  1849. void smp_derive_link_key_from_long_term_key(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
  1850. {
  1851. tSMP_STATUS status = SMP_PAIR_FAIL_UNKNOWN;
  1852. SMP_TRACE_DEBUG("%s\n", __func__);
  1853. if (!smp_calculate_link_key_from_long_term_key(p_cb)) {
  1854. SMP_TRACE_ERROR("%s failed\n", __FUNCTION__);
  1855. smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &status);
  1856. return;
  1857. }
  1858. }
  1859. #endif ///BLE_INCLUDED == TRUE
  1860. #if (CLASSIC_BT_INCLUDED == TRUE)
  1861. /*******************************************************************************
  1862. **
  1863. ** Function smp_br_process_link_key
  1864. **
  1865. ** Description This function is called to process BR/EDR LK:
  1866. ** - to derive SMP LTK from BR/EDR LK;
  1867. *8 - to save SMP LTK.
  1868. **
  1869. ** Returns void
  1870. **
  1871. *******************************************************************************/
  1872. void smp_br_process_link_key(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
  1873. {
  1874. tSMP_STATUS status = SMP_PAIR_FAIL_UNKNOWN;
  1875. SMP_TRACE_DEBUG("%s\n", __func__);
  1876. if (!smp_calculate_long_term_key_from_link_key(p_cb)) {
  1877. SMP_TRACE_ERROR ("%s failed\n", __FUNCTION__);
  1878. smp_sm_event(p_cb, SMP_BR_AUTH_CMPL_EVT, &status);
  1879. return;
  1880. }
  1881. SMP_TRACE_DEBUG("%s: LTK derivation from LK successfully completed\n", __FUNCTION__);
  1882. smp_save_secure_connections_long_term_key(p_cb);
  1883. smp_update_key_mask (p_cb, SMP_SEC_KEY_TYPE_ENC, FALSE);
  1884. smp_br_select_next_key(p_cb, NULL);
  1885. }
  1886. #endif ///CLASSIC_BT_INCLUDED == TRUE
  1887. /*******************************************************************************
  1888. ** Function smp_key_distribution_by_transport
  1889. ** Description depending on the transport used at the moment calls either
  1890. ** smp_key_distribution(...) or smp_br_key_distribution(...).
  1891. *******************************************************************************/
  1892. void smp_key_distribution_by_transport(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
  1893. {
  1894. SMP_TRACE_DEBUG("%s\n", __func__);
  1895. if (p_cb->smp_over_br) {
  1896. #if (CLASSIC_BT_INCLUDED == TRUE)
  1897. smp_br_select_next_key(p_cb, NULL);
  1898. #endif ///CLASSIC_BT_INCLUDED == TRUE
  1899. } else {
  1900. #if (BLE_INCLUDED == TRUE)
  1901. smp_key_distribution(p_cb, NULL);
  1902. #endif ///BLE_INCLUDED == TRUE
  1903. }
  1904. }
  1905. /*******************************************************************************
  1906. ** Function smp_br_pairing_complete
  1907. ** Description This function is called to send the pairing complete callback
  1908. ** and remove the connection if needed.
  1909. *******************************************************************************/
  1910. void smp_br_pairing_complete(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
  1911. {
  1912. SMP_TRACE_DEBUG("%s\n", __func__);
  1913. if (p_cb->total_tx_unacked == 0) {
  1914. /* process the pairing complete */
  1915. smp_proc_pairing_cmpl(p_cb);
  1916. }
  1917. }
  1918. #endif