btm_ble.c 89 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455
  1. /******************************************************************************
  2. *
  3. * Copyright (C) 1999-2012 Broadcom Corporation
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at:
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. ******************************************************************************/
  18. /******************************************************************************
  19. *
  20. * This file contains functions for BLE device control utilities, and LE
  21. * security functions.
  22. *
  23. ******************************************************************************/
  24. #include "bt_target.h"
  25. #if BLE_INCLUDED == TRUE
  26. #include <string.h>
  27. #include "bt_types.h"
  28. #include "hcimsgs.h"
  29. #include "btu.h"
  30. #include "btm_int.h"
  31. #include "btm_ble_api.h"
  32. #include "smp_api.h"
  33. #include "l2c_int.h"
  34. #include "gap_api.h"
  35. //#include "bt_utils.h"
  36. #include "controller.h"
  37. //#define LOG_TAG "bt_btm_ble"
  38. //#include "osi/include/log.h"
  39. #if SMP_INCLUDED == TRUE
  40. extern BOOLEAN aes_cipher_msg_auth_code(BT_OCTET16 key, UINT8 *input, UINT16 length,
  41. UINT16 tlen, UINT8 *p_signature);
  42. extern void smp_link_encrypted(BD_ADDR bda, UINT8 encr_enable);
  43. extern BOOLEAN smp_proc_ltk_request(BD_ADDR bda);
  44. #endif
  45. extern void gatt_notify_enc_cmpl(BD_ADDR bd_addr);
  46. /*******************************************************************************/
  47. /* External Function to be called by other modules */
  48. /*******************************************************************************/
  49. /********************************************************
  50. **
  51. ** Function BTM_SecAddBleDevice
  52. **
  53. ** Description Add/modify device. This function will be normally called
  54. ** during host startup to restore all required information
  55. ** for a LE device stored in the NVRAM.
  56. **
  57. ** Parameters: bd_addr - BD address of the peer
  58. ** bd_name - Name of the peer device. NULL if unknown.
  59. ** dev_type - Remote device's device type.
  60. ** addr_type - LE device address type.
  61. **
  62. ** Returns TRUE if added OK, else FALSE
  63. **
  64. *******************************************************************************/
  65. BOOLEAN BTM_SecAddBleDevice (BD_ADDR bd_addr, BD_NAME bd_name, tBT_DEVICE_TYPE dev_type,
  66. tBLE_ADDR_TYPE addr_type)
  67. {
  68. tBTM_SEC_DEV_REC *p_dev_rec;
  69. UINT8 i = 0;
  70. tBTM_INQ_INFO *p_info = NULL;
  71. BTM_TRACE_DEBUG ("BTM_SecAddBleDevice dev_type=0x%x", dev_type);
  72. p_dev_rec = btm_find_dev (bd_addr);
  73. if (!p_dev_rec) {
  74. BTM_TRACE_DEBUG("Add a new device");
  75. /* There is no device record, allocate one.
  76. * If we can not find an empty spot for this one, let it fail. */
  77. for (i = 0; i < BTM_SEC_MAX_DEVICE_RECORDS; i++) {
  78. if (!(btm_cb.sec_dev_rec[i].sec_flags & BTM_SEC_IN_USE)) {
  79. BTM_TRACE_DEBUG ("allocate a new dev rec idx=0x%x ", i );
  80. p_dev_rec = &btm_cb.sec_dev_rec[i];
  81. /* Mark this record as in use and initialize */
  82. memset (p_dev_rec, 0, sizeof (tBTM_SEC_DEV_REC));
  83. p_dev_rec->sec_flags = BTM_SEC_IN_USE;
  84. memcpy (p_dev_rec->bd_addr, bd_addr, BD_ADDR_LEN);
  85. p_dev_rec->hci_handle = BTM_GetHCIConnHandle (bd_addr, BT_TRANSPORT_BR_EDR);
  86. p_dev_rec->ble_hci_handle = BTM_GetHCIConnHandle (bd_addr, BT_TRANSPORT_LE);
  87. /* update conn params, use default value for background connection params */
  88. p_dev_rec->conn_params.min_conn_int =
  89. p_dev_rec->conn_params.max_conn_int =
  90. p_dev_rec->conn_params.supervision_tout =
  91. p_dev_rec->conn_params.slave_latency = BTM_BLE_CONN_PARAM_UNDEF;
  92. BTM_TRACE_DEBUG ("hci_handl=0x%x ", p_dev_rec->ble_hci_handle );
  93. break;
  94. }
  95. }
  96. if (!p_dev_rec) {
  97. return (FALSE);
  98. }
  99. } else {
  100. BTM_TRACE_DEBUG("Device already exist");
  101. }
  102. memset(p_dev_rec->sec_bd_name, 0, sizeof(tBTM_BD_NAME));
  103. if (bd_name && bd_name[0]) {
  104. p_dev_rec->sec_flags |= BTM_SEC_NAME_KNOWN;
  105. BCM_STRNCPY_S ((char *)p_dev_rec->sec_bd_name, sizeof (p_dev_rec->sec_bd_name),
  106. (char *)bd_name, BTM_MAX_REM_BD_NAME_LEN);
  107. }
  108. p_dev_rec->device_type |= dev_type;
  109. p_dev_rec->ble.ble_addr_type = addr_type;
  110. memcpy (p_dev_rec->ble.pseudo_addr, bd_addr, BD_ADDR_LEN);
  111. /* sync up with the Inq Data base*/
  112. p_info = BTM_InqDbRead(bd_addr);
  113. if (p_info) {
  114. p_info->results.ble_addr_type = p_dev_rec->ble.ble_addr_type ;
  115. p_info->results.device_type = p_dev_rec->device_type;
  116. BTM_TRACE_DEBUG ("InqDb device_type =0x%x addr_type=0x%x",
  117. p_info->results.device_type, p_info->results.ble_addr_type);
  118. }
  119. return (TRUE);
  120. }
  121. /*******************************************************************************
  122. **
  123. ** Function BTM_SecAddBleKey
  124. **
  125. ** Description Add/modify LE device information. This function will be
  126. ** normally called during host startup to restore all required
  127. ** information stored in the NVRAM.
  128. **
  129. ** Parameters: bd_addr - BD address of the peer
  130. ** p_le_key - LE key values.
  131. ** key_type - LE SMP key type.
  132. *
  133. ** Returns TRUE if added OK, else FALSE
  134. **
  135. *******************************************************************************/
  136. BOOLEAN BTM_SecAddBleKey (BD_ADDR bd_addr, tBTM_LE_KEY_VALUE *p_le_key, tBTM_LE_KEY_TYPE key_type)
  137. {
  138. #if SMP_INCLUDED == TRUE
  139. tBTM_SEC_DEV_REC *p_dev_rec;
  140. BTM_TRACE_DEBUG ("BTM_SecAddBleKey");
  141. p_dev_rec = btm_find_dev (bd_addr);
  142. if (!p_dev_rec || !p_le_key ||
  143. (key_type != BTM_LE_KEY_PENC && key_type != BTM_LE_KEY_PID &&
  144. key_type != BTM_LE_KEY_PCSRK && key_type != BTM_LE_KEY_LENC &&
  145. key_type != BTM_LE_KEY_LCSRK && key_type != BTM_LE_KEY_LID)) {
  146. BTM_TRACE_WARNING ("BTM_SecAddBleKey() Wrong Type, or No Device record \
  147. for bdaddr: %08x%04x, Type: %d",
  148. (bd_addr[0] << 24) + (bd_addr[1] << 16) + (bd_addr[2] << 8) + bd_addr[3],
  149. (bd_addr[4] << 8) + bd_addr[5], key_type);
  150. return (FALSE);
  151. }
  152. BTM_TRACE_DEBUG ("BTM_SecAddLeKey() BDA: %08x%04x, Type: 0x%02x",
  153. (bd_addr[0] << 24) + (bd_addr[1] << 16) + (bd_addr[2] << 8) + bd_addr[3],
  154. (bd_addr[4] << 8) + bd_addr[5], key_type);
  155. btm_sec_save_le_key (bd_addr, key_type, p_le_key, FALSE);
  156. #if (BLE_PRIVACY_SPT == TRUE)
  157. if (key_type == BTM_LE_KEY_PID || key_type == BTM_LE_KEY_LID) {
  158. btm_ble_resolving_list_load_dev (p_dev_rec);
  159. }
  160. #endif
  161. #endif
  162. return (TRUE);
  163. }
  164. /*******************************************************************************
  165. **
  166. ** Function BTM_BleLoadLocalKeys
  167. **
  168. ** Description Local local identity key, encryption root or sign counter.
  169. **
  170. ** Parameters: key_type: type of key, can be BTM_BLE_KEY_TYPE_ID, BTM_BLE_KEY_TYPE_ER
  171. ** or BTM_BLE_KEY_TYPE_COUNTER.
  172. ** p_key: pointer to the key.
  173. *
  174. ** Returns non2.
  175. **
  176. *******************************************************************************/
  177. void BTM_BleLoadLocalKeys(UINT8 key_type, tBTM_BLE_LOCAL_KEYS *p_key)
  178. {
  179. tBTM_DEVCB *p_devcb = &btm_cb.devcb;
  180. BTM_TRACE_DEBUG ("%s", __func__);
  181. if (p_key != NULL) {
  182. switch (key_type) {
  183. case BTM_BLE_KEY_TYPE_ID:
  184. memcpy(&p_devcb->id_keys, &p_key->id_keys, sizeof(tBTM_BLE_LOCAL_ID_KEYS));
  185. break;
  186. case BTM_BLE_KEY_TYPE_ER:
  187. memcpy(p_devcb->ble_encryption_key_value, p_key->er, sizeof(BT_OCTET16));
  188. break;
  189. default:
  190. BTM_TRACE_ERROR("unknow local key type: %d", key_type);
  191. break;
  192. }
  193. }
  194. }
  195. /*******************************************************************************
  196. **
  197. ** Function BTM_GetDeviceEncRoot
  198. **
  199. ** Description This function is called to read the local device encryption
  200. ** root.
  201. **
  202. ** Returns void
  203. ** the local device ER is copied into ble_encr_key_value
  204. **
  205. *******************************************************************************/
  206. void BTM_GetDeviceEncRoot (BT_OCTET16 ble_encr_key_value)
  207. {
  208. BTM_TRACE_DEBUG ("%s", __func__);
  209. memcpy (ble_encr_key_value, btm_cb.devcb.ble_encryption_key_value, BT_OCTET16_LEN);
  210. }
  211. /*******************************************************************************
  212. **
  213. ** Function BTM_GetDeviceIDRoot
  214. **
  215. ** Description This function is called to read the local device identity
  216. ** root.
  217. **
  218. ** Returns void
  219. ** the local device IR is copied into irk
  220. **
  221. *******************************************************************************/
  222. void BTM_GetDeviceIDRoot (BT_OCTET16 irk)
  223. {
  224. BTM_TRACE_DEBUG ("BTM_GetDeviceIDRoot ");
  225. memcpy (irk, btm_cb.devcb.id_keys.irk, BT_OCTET16_LEN);
  226. }
  227. /*******************************************************************************
  228. **
  229. ** Function BTM_GetDeviceDHK
  230. **
  231. ** Description This function is called to read the local device DHK.
  232. **
  233. ** Returns void
  234. ** the local device DHK is copied into dhk
  235. **
  236. *******************************************************************************/
  237. void BTM_GetDeviceDHK (BT_OCTET16 dhk)
  238. {
  239. BTM_TRACE_DEBUG ("BTM_GetDeviceDHK");
  240. memcpy (dhk, btm_cb.devcb.id_keys.dhk, BT_OCTET16_LEN);
  241. }
  242. /*******************************************************************************
  243. **
  244. ** Function BTM_ReadConnectionAddr
  245. **
  246. ** Description This function is called to get the local device address information
  247. ** .
  248. **
  249. ** Returns void
  250. **
  251. *******************************************************************************/
  252. void BTM_ReadConnectionAddr (BD_ADDR remote_bda, BD_ADDR local_conn_addr, tBLE_ADDR_TYPE *p_addr_type)
  253. {
  254. tACL_CONN *p_acl = btm_bda_to_acl(remote_bda, BT_TRANSPORT_LE);
  255. if (p_acl == NULL) {
  256. BTM_TRACE_ERROR("No connection exist!");
  257. return;
  258. }
  259. memcpy(local_conn_addr, p_acl->conn_addr, BD_ADDR_LEN);
  260. * p_addr_type = p_acl->conn_addr_type;
  261. BTM_TRACE_DEBUG ("BTM_ReadConnectionAddr address type: %d addr: 0x%02x",
  262. p_acl->conn_addr_type, p_acl->conn_addr[0]);
  263. }
  264. /*******************************************************************************
  265. **
  266. ** Function BTM_IsBleConnection
  267. **
  268. ** Description This function is called to check if the connection handle
  269. ** for an LE link
  270. **
  271. ** Returns TRUE if connection is LE link, otherwise FALSE.
  272. **
  273. *******************************************************************************/
  274. BOOLEAN BTM_IsBleConnection (UINT16 conn_handle)
  275. {
  276. #if (BLE_INCLUDED == TRUE)
  277. UINT8 xx;
  278. tACL_CONN *p;
  279. BTM_TRACE_API ("BTM_IsBleConnection: conn_handle: %d", conn_handle);
  280. xx = btm_handle_to_acl_index (conn_handle);
  281. if (xx >= MAX_L2CAP_LINKS) {
  282. return FALSE;
  283. }
  284. p = &btm_cb.acl_db[xx];
  285. return (p->transport == BT_TRANSPORT_LE);
  286. #else
  287. return FALSE;
  288. #endif
  289. }
  290. /*******************************************************************************
  291. **
  292. ** Function BTM_ReadRemoteConnectionAddr
  293. **
  294. ** Description This function is read the remote device address currently used
  295. **
  296. ** Parameters pseudo_addr: pseudo random address available
  297. ** conn_addr:connection address used
  298. ** p_addr_type : BD Address type, Public or Random of the address used
  299. **
  300. ** Returns BOOLEAN , TRUE if connection to remote device exists, else FALSE
  301. **
  302. *******************************************************************************/
  303. BOOLEAN BTM_ReadRemoteConnectionAddr(BD_ADDR pseudo_addr, BD_ADDR conn_addr,
  304. tBLE_ADDR_TYPE *p_addr_type)
  305. {
  306. BOOLEAN st = TRUE;
  307. #if (BLE_PRIVACY_SPT == TRUE)
  308. tACL_CONN *p = btm_bda_to_acl (pseudo_addr, BT_TRANSPORT_LE);
  309. if (p == NULL) {
  310. BTM_TRACE_ERROR("BTM_ReadRemoteConnectionAddr can not find connection"
  311. " with matching address");
  312. return FALSE;
  313. }
  314. memcpy(conn_addr, p->active_remote_addr, BD_ADDR_LEN);
  315. *p_addr_type = p->active_remote_addr_type;
  316. #else
  317. tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev(pseudo_addr);
  318. memcpy(conn_addr, pseudo_addr, BD_ADDR_LEN);
  319. if (p_dev_rec != NULL) {
  320. *p_addr_type = p_dev_rec->ble.ble_addr_type;
  321. }
  322. #endif
  323. return st;
  324. }
  325. /*******************************************************************************
  326. **
  327. ** Function BTM_SecurityGrant
  328. **
  329. ** Description This function is called to grant security process.
  330. **
  331. ** Parameters bd_addr - peer device bd address.
  332. ** res - result of the operation BTM_SUCCESS if success.
  333. ** Otherwise, BTM_REPEATED_ATTEMPTS is too many attempts.
  334. **
  335. ** Returns None
  336. **
  337. *******************************************************************************/
  338. void BTM_SecurityGrant(BD_ADDR bd_addr, UINT8 res)
  339. {
  340. #if SMP_INCLUDED == TRUE
  341. tSMP_STATUS res_smp = (res == BTM_SUCCESS) ? SMP_SUCCESS : SMP_REPEATED_ATTEMPTS;
  342. BTM_TRACE_DEBUG ("BTM_SecurityGrant");
  343. SMP_SecurityGrant(bd_addr, res_smp);
  344. #endif
  345. }
  346. /*******************************************************************************
  347. **
  348. ** Function BTM_BlePasskeyReply
  349. **
  350. ** Description This function is called after Security Manager submitted
  351. ** passkey request to the application.
  352. **
  353. ** Parameters: bd_addr - Address of the device for which passkey was requested
  354. ** res - result of the operation BTM_SUCCESS if success
  355. ** key_len - length in bytes of the Passkey
  356. ** p_passkey - pointer to array with the passkey
  357. ** trusted_mask - bitwise OR of trusted services (array of UINT32)
  358. **
  359. *******************************************************************************/
  360. void BTM_BlePasskeyReply (BD_ADDR bd_addr, UINT8 res, UINT32 passkey)
  361. {
  362. #if SMP_INCLUDED == TRUE
  363. tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev (bd_addr);
  364. tSMP_STATUS res_smp = (res == BTM_SUCCESS) ? SMP_SUCCESS : SMP_PASSKEY_ENTRY_FAIL;
  365. if (p_dev_rec == NULL) {
  366. BTM_TRACE_ERROR("Passkey reply to Unknown device");
  367. return;
  368. }
  369. p_dev_rec->sec_flags |= BTM_SEC_LE_AUTHENTICATED;
  370. BTM_TRACE_DEBUG ("BTM_BlePasskeyReply");
  371. SMP_PasskeyReply(bd_addr, res_smp, passkey);
  372. #endif
  373. }
  374. /*******************************************************************************
  375. **
  376. ** Function BTM_BleConfirmReply
  377. **
  378. ** Description This function is called after Security Manager submitted
  379. ** numeric comparison request to the application.
  380. **
  381. ** Parameters: bd_addr - Address of the device with which numeric
  382. ** comparison was requested
  383. ** res - comparison result BTM_SUCCESS if success
  384. **
  385. *******************************************************************************/
  386. void BTM_BleConfirmReply (BD_ADDR bd_addr, UINT8 res)
  387. {
  388. tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev (bd_addr);
  389. tSMP_STATUS res_smp = (res == BTM_SUCCESS) ? SMP_SUCCESS : SMP_PASSKEY_ENTRY_FAIL;
  390. if (p_dev_rec == NULL) {
  391. BTM_TRACE_ERROR("Passkey reply to Unknown device");
  392. return;
  393. }
  394. p_dev_rec->sec_flags |= BTM_SEC_LE_AUTHENTICATED;
  395. BTM_TRACE_DEBUG ("%s\n", __func__);
  396. SMP_ConfirmReply(bd_addr, res_smp);
  397. }
  398. /*******************************************************************************
  399. **
  400. ** Function BTM_BleOobDataReply
  401. **
  402. ** Description This function is called to provide the OOB data for
  403. ** SMP in response to BTM_LE_OOB_REQ_EVT
  404. **
  405. ** Parameters: bd_addr - Address of the peer device
  406. ** res - result of the operation SMP_SUCCESS if success
  407. ** p_data - simple pairing Randomizer C.
  408. **
  409. *******************************************************************************/
  410. void BTM_BleOobDataReply(BD_ADDR bd_addr, UINT8 res, UINT8 len, UINT8 *p_data)
  411. {
  412. #if SMP_INCLUDED == TRUE
  413. tSMP_STATUS res_smp = (res == BTM_SUCCESS) ? SMP_SUCCESS : SMP_OOB_FAIL;
  414. tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev (bd_addr);
  415. BTM_TRACE_DEBUG ("BTM_BleOobDataReply");
  416. if (p_dev_rec == NULL) {
  417. BTM_TRACE_ERROR("BTM_BleOobDataReply() to Unknown device");
  418. return;
  419. }
  420. p_dev_rec->sec_flags |= BTM_SEC_LE_AUTHENTICATED;
  421. SMP_OobDataReply(bd_addr, res_smp, len, p_data);
  422. #endif
  423. }
  424. /******************************************************************************
  425. **
  426. ** Function BTM_BleSetConnScanParams
  427. **
  428. ** Description Set scan parameter used in BLE connection request
  429. **
  430. ** Parameters: scan_interval: scan interval
  431. ** scan_window: scan window
  432. **
  433. ** Returns void
  434. **
  435. *******************************************************************************/
  436. void BTM_BleSetConnScanParams (UINT32 scan_interval, UINT32 scan_window)
  437. {
  438. #if SMP_INCLUDED == TRUE
  439. tBTM_BLE_CB *p_ble_cb = &btm_cb.ble_ctr_cb;
  440. BOOLEAN new_param = FALSE;
  441. if (BTM_BLE_ISVALID_PARAM(scan_interval, BTM_BLE_SCAN_INT_MIN, BTM_BLE_SCAN_INT_MAX) &&
  442. BTM_BLE_ISVALID_PARAM(scan_window, BTM_BLE_SCAN_WIN_MIN, BTM_BLE_SCAN_WIN_MAX)) {
  443. if (p_ble_cb->scan_int != scan_interval) {
  444. p_ble_cb->scan_int = scan_interval;
  445. new_param = TRUE;
  446. }
  447. if (p_ble_cb->scan_win != scan_window) {
  448. p_ble_cb->scan_win = scan_window;
  449. new_param = TRUE;
  450. }
  451. if (new_param && p_ble_cb->conn_state == BLE_BG_CONN) {
  452. btm_ble_suspend_bg_conn();
  453. }
  454. } else {
  455. BTM_TRACE_ERROR("Illegal Connection Scan Parameters");
  456. }
  457. #endif
  458. }
  459. /********************************************************
  460. **
  461. ** Function BTM_BleSetPrefConnParams
  462. **
  463. ** Description Set a peripheral's preferred connection parameters
  464. **
  465. ** Parameters: bd_addr - BD address of the peripheral
  466. ** scan_interval: scan interval
  467. ** scan_window: scan window
  468. ** min_conn_int - minimum preferred connection interval
  469. ** max_conn_int - maximum preferred connection interval
  470. ** slave_latency - preferred slave latency
  471. ** supervision_tout - preferred supervision timeout
  472. **
  473. ** Returns void
  474. **
  475. *******************************************************************************/
  476. void BTM_BleSetPrefConnParams (BD_ADDR bd_addr,
  477. UINT16 min_conn_int, UINT16 max_conn_int,
  478. UINT16 slave_latency, UINT16 supervision_tout)
  479. {
  480. tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev (bd_addr);
  481. BTM_TRACE_API ("BTM_BleSetPrefConnParams min: %u max: %u latency: %u \
  482. tout: %u",
  483. min_conn_int, max_conn_int, slave_latency, supervision_tout);
  484. if (BTM_BLE_ISVALID_PARAM(min_conn_int, BTM_BLE_CONN_INT_MIN, BTM_BLE_CONN_INT_MAX) &&
  485. BTM_BLE_ISVALID_PARAM(max_conn_int, BTM_BLE_CONN_INT_MIN, BTM_BLE_CONN_INT_MAX) &&
  486. BTM_BLE_ISVALID_PARAM(supervision_tout, BTM_BLE_CONN_SUP_TOUT_MIN, BTM_BLE_CONN_SUP_TOUT_MAX) &&
  487. (slave_latency <= BTM_BLE_CONN_LATENCY_MAX || slave_latency == BTM_BLE_CONN_PARAM_UNDEF)) {
  488. if (p_dev_rec) {
  489. /* expect conn int and stout and slave latency to be updated all together */
  490. if (min_conn_int != BTM_BLE_CONN_PARAM_UNDEF || max_conn_int != BTM_BLE_CONN_PARAM_UNDEF) {
  491. if (min_conn_int != BTM_BLE_CONN_PARAM_UNDEF) {
  492. p_dev_rec->conn_params.min_conn_int = min_conn_int;
  493. } else {
  494. p_dev_rec->conn_params.min_conn_int = max_conn_int;
  495. }
  496. if (max_conn_int != BTM_BLE_CONN_PARAM_UNDEF) {
  497. p_dev_rec->conn_params.max_conn_int = max_conn_int;
  498. } else {
  499. p_dev_rec->conn_params.max_conn_int = min_conn_int;
  500. }
  501. if (slave_latency != BTM_BLE_CONN_PARAM_UNDEF) {
  502. p_dev_rec->conn_params.slave_latency = slave_latency;
  503. } else {
  504. p_dev_rec->conn_params.slave_latency = BTM_BLE_CONN_SLAVE_LATENCY_DEF;
  505. }
  506. if (supervision_tout != BTM_BLE_CONN_PARAM_UNDEF) {
  507. p_dev_rec->conn_params.supervision_tout = supervision_tout;
  508. } else {
  509. p_dev_rec->conn_params.supervision_tout = BTM_BLE_CONN_TIMEOUT_DEF;
  510. }
  511. }
  512. } else {
  513. BTM_TRACE_ERROR("Unknown Device, setting rejected");
  514. }
  515. } else {
  516. BTM_TRACE_ERROR("Illegal Connection Parameters");
  517. }
  518. }
  519. /*******************************************************************************
  520. **
  521. ** Function BTM_ReadDevInfo
  522. **
  523. ** Description This function is called to read the device/address type
  524. ** of BD address.
  525. **
  526. ** Parameter remote_bda: remote device address
  527. ** p_dev_type: output parameter to read the device type.
  528. ** p_addr_type: output parameter to read the address type.
  529. **
  530. *******************************************************************************/
  531. void BTM_ReadDevInfo (BD_ADDR remote_bda, tBT_DEVICE_TYPE *p_dev_type, tBLE_ADDR_TYPE *p_addr_type)
  532. {
  533. tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev (remote_bda);
  534. tBTM_INQ_INFO *p_inq_info = BTM_InqDbRead(remote_bda);
  535. *p_addr_type = BLE_ADDR_PUBLIC;
  536. if (!p_dev_rec) {
  537. *p_dev_type = BT_DEVICE_TYPE_BREDR;
  538. /* Check with the BT manager if details about remote device are known */
  539. if (p_inq_info != NULL) {
  540. *p_dev_type = p_inq_info->results.device_type ;
  541. *p_addr_type = p_inq_info->results.ble_addr_type;
  542. } else {
  543. /* unknown device, assume BR/EDR */
  544. BTM_TRACE_DEBUG ("btm_find_dev_type - unknown device, BR/EDR assumed");
  545. }
  546. } else { /* there is a security device record exisitng */
  547. /* new inquiry result, overwrite device type in security device record */
  548. if (p_inq_info) {
  549. p_dev_rec->device_type = p_inq_info->results.device_type;
  550. p_dev_rec->ble.ble_addr_type = p_inq_info->results.ble_addr_type;
  551. }
  552. if (memcmp(p_dev_rec->bd_addr, remote_bda, BD_ADDR_LEN) == 0 &&
  553. memcmp(p_dev_rec->ble.pseudo_addr, remote_bda, BD_ADDR_LEN) == 0) {
  554. *p_dev_type = p_dev_rec->device_type;
  555. *p_addr_type = p_dev_rec->ble.ble_addr_type;
  556. } else if (memcmp(p_dev_rec->ble.pseudo_addr, remote_bda, BD_ADDR_LEN) == 0) {
  557. *p_dev_type = BT_DEVICE_TYPE_BLE;
  558. *p_addr_type = p_dev_rec->ble.ble_addr_type;
  559. } else { /* matching static adddress only */
  560. *p_dev_type = BT_DEVICE_TYPE_BREDR;
  561. *p_addr_type = BLE_ADDR_PUBLIC;
  562. }
  563. }
  564. BTM_TRACE_DEBUG ("btm_find_dev_type - device_type = %d addr_type = %d", *p_dev_type , *p_addr_type);
  565. }
  566. /*******************************************************************************
  567. **
  568. ** Function BTM_ReadConnectedTransportAddress
  569. **
  570. ** Description This function is called to read the paired device/address type of other device paired
  571. ** corresponding to the BD_address
  572. **
  573. ** Parameter remote_bda: remote device address, carry out the transport address
  574. ** transport: active transport
  575. **
  576. ** Return TRUE if an active link is identified; FALSE otherwise
  577. **
  578. *******************************************************************************/
  579. BOOLEAN BTM_ReadConnectedTransportAddress(BD_ADDR remote_bda, tBT_TRANSPORT transport)
  580. {
  581. tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev(remote_bda);
  582. /* if no device can be located, return */
  583. if (p_dev_rec == NULL) {
  584. return FALSE;
  585. }
  586. if (transport == BT_TRANSPORT_BR_EDR) {
  587. if (btm_bda_to_acl(p_dev_rec->bd_addr, transport) != NULL) {
  588. memcpy(remote_bda, p_dev_rec->bd_addr, BD_ADDR_LEN);
  589. return TRUE;
  590. } else if (p_dev_rec->device_type & BT_DEVICE_TYPE_BREDR) {
  591. memcpy(remote_bda, p_dev_rec->bd_addr, BD_ADDR_LEN);
  592. } else {
  593. memset(remote_bda, 0, BD_ADDR_LEN);
  594. }
  595. return FALSE;
  596. }
  597. if (transport == BT_TRANSPORT_LE) {
  598. memcpy(remote_bda, p_dev_rec->ble.pseudo_addr, BD_ADDR_LEN);
  599. if (btm_bda_to_acl(p_dev_rec->ble.pseudo_addr, transport) != NULL) {
  600. return TRUE;
  601. } else {
  602. return FALSE;
  603. }
  604. }
  605. return FALSE;
  606. }
  607. /*******************************************************************************
  608. **
  609. ** Function BTM_BleReceiverTest
  610. **
  611. ** Description This function is called to start the LE Receiver test
  612. **
  613. ** Parameter rx_freq - Frequency Range
  614. ** p_cmd_cmpl_cback - Command Complete callback
  615. **
  616. *******************************************************************************/
  617. void BTM_BleReceiverTest(UINT8 rx_freq, tBTM_CMPL_CB *p_cmd_cmpl_cback)
  618. {
  619. btm_cb.devcb.p_le_test_cmd_cmpl_cb = p_cmd_cmpl_cback;
  620. if (btsnd_hcic_ble_receiver_test(rx_freq) == FALSE) {
  621. BTM_TRACE_ERROR("%s: Unable to Trigger LE receiver test", __FUNCTION__);
  622. }
  623. }
  624. /*******************************************************************************
  625. **
  626. ** Function BTM_BleTransmitterTest
  627. **
  628. ** Description This function is called to start the LE Transmitter test
  629. **
  630. ** Parameter tx_freq - Frequency Range
  631. ** test_data_len - Length in bytes of payload data in each packet
  632. ** packet_payload - Pattern to use in the payload
  633. ** p_cmd_cmpl_cback - Command Complete callback
  634. **
  635. *******************************************************************************/
  636. void BTM_BleTransmitterTest(UINT8 tx_freq, UINT8 test_data_len,
  637. UINT8 packet_payload, tBTM_CMPL_CB *p_cmd_cmpl_cback)
  638. {
  639. btm_cb.devcb.p_le_test_cmd_cmpl_cb = p_cmd_cmpl_cback;
  640. if (btsnd_hcic_ble_transmitter_test(tx_freq, test_data_len, packet_payload) == FALSE) {
  641. BTM_TRACE_ERROR("%s: Unable to Trigger LE transmitter test", __FUNCTION__);
  642. }
  643. }
  644. /*******************************************************************************
  645. **
  646. ** Function BTM_BleTestEnd
  647. **
  648. ** Description This function is called to stop the in-progress TX or RX test
  649. **
  650. ** Parameter p_cmd_cmpl_cback - Command complete callback
  651. **
  652. *******************************************************************************/
  653. void BTM_BleTestEnd(tBTM_CMPL_CB *p_cmd_cmpl_cback)
  654. {
  655. btm_cb.devcb.p_le_test_cmd_cmpl_cb = p_cmd_cmpl_cback;
  656. if (btsnd_hcic_ble_test_end() == FALSE) {
  657. BTM_TRACE_ERROR("%s: Unable to End the LE TX/RX test", __FUNCTION__);
  658. }
  659. }
  660. /*******************************************************************************
  661. ** Internal Functions
  662. *******************************************************************************/
  663. void btm_ble_test_command_complete(UINT8 *p)
  664. {
  665. tBTM_CMPL_CB *p_cb = btm_cb.devcb.p_le_test_cmd_cmpl_cb;
  666. btm_cb.devcb.p_le_test_cmd_cmpl_cb = NULL;
  667. if (p_cb) {
  668. (*p_cb)(p);
  669. }
  670. }
  671. /*******************************************************************************
  672. **
  673. ** Function BTM_UseLeLink
  674. **
  675. ** Description This function is to select the underneath physical link to use.
  676. **
  677. ** Returns TRUE to use LE, FALSE use BR/EDR.
  678. **
  679. *******************************************************************************/
  680. BOOLEAN BTM_UseLeLink (BD_ADDR bd_addr)
  681. {
  682. tACL_CONN *p;
  683. tBT_DEVICE_TYPE dev_type;
  684. tBLE_ADDR_TYPE addr_type;
  685. BOOLEAN use_le = FALSE;
  686. if ((p = btm_bda_to_acl(bd_addr, BT_TRANSPORT_BR_EDR)) != NULL) {
  687. return use_le;
  688. } else if ((p = btm_bda_to_acl(bd_addr, BT_TRANSPORT_LE)) != NULL) {
  689. use_le = TRUE;
  690. } else {
  691. BTM_ReadDevInfo(bd_addr, &dev_type, &addr_type);
  692. use_le = (dev_type == BT_DEVICE_TYPE_BLE);
  693. }
  694. return use_le;
  695. }
  696. /*******************************************************************************
  697. **
  698. ** Function BTM_SetBleDataLength
  699. **
  700. ** Description This function is to set maximum BLE transmission packet size
  701. **
  702. ** Returns BTM_SUCCESS if success; otherwise failed.
  703. **
  704. *******************************************************************************/
  705. tBTM_STATUS BTM_SetBleDataLength(BD_ADDR bd_addr, UINT16 tx_pdu_length)
  706. {
  707. tACL_CONN *p_acl = btm_bda_to_acl(bd_addr, BT_TRANSPORT_LE);
  708. BTM_TRACE_DEBUG("%s: tx_pdu_length =%d", __FUNCTION__, tx_pdu_length);
  709. if (!controller_get_interface()->supports_ble_packet_extension()) {
  710. BTM_TRACE_ERROR("%s failed, request not supported", __FUNCTION__);
  711. return BTM_ILLEGAL_VALUE;
  712. }
  713. if (!HCI_LE_DATA_LEN_EXT_SUPPORTED(p_acl->peer_le_features)) {
  714. BTM_TRACE_ERROR("%s failed, peer does not support request", __FUNCTION__);
  715. return BTM_ILLEGAL_VALUE;
  716. }
  717. if (p_acl != NULL) {
  718. if (tx_pdu_length > BTM_BLE_DATA_SIZE_MAX) {
  719. tx_pdu_length = BTM_BLE_DATA_SIZE_MAX;
  720. } else if (tx_pdu_length < BTM_BLE_DATA_SIZE_MIN) {
  721. tx_pdu_length = BTM_BLE_DATA_SIZE_MIN;
  722. }
  723. /* always set the TxTime to be max, as controller does not care for now */
  724. btsnd_hcic_ble_set_data_length(p_acl->hci_handle, tx_pdu_length,
  725. BTM_BLE_DATA_TX_TIME_MAX);
  726. return BTM_SUCCESS;
  727. } else {
  728. BTM_TRACE_ERROR("%s: Wrong mode: no LE link exist or LE not supported", __FUNCTION__);
  729. return BTM_WRONG_MODE;
  730. }
  731. }
  732. /*******************************************************************************
  733. **
  734. ** Function btm_ble_rand_enc_complete
  735. **
  736. ** Description This function is the callback functions for HCI_Rand command
  737. ** and HCI_Encrypt command is completed.
  738. ** This message is received from the HCI.
  739. **
  740. ** Returns void
  741. **
  742. *******************************************************************************/
  743. void btm_ble_rand_enc_complete (UINT8 *p, UINT16 op_code, tBTM_RAND_ENC_CB *p_enc_cplt_cback)
  744. {
  745. tBTM_RAND_ENC params;
  746. UINT8 *p_dest = params.param_buf;
  747. BTM_TRACE_DEBUG ("btm_ble_rand_enc_complete");
  748. memset(&params, 0, sizeof(tBTM_RAND_ENC));
  749. /* If there was a callback address for vcs complete, call it */
  750. if (p_enc_cplt_cback && p) {
  751. /* Pass paramters to the callback function */
  752. STREAM_TO_UINT8(params.status, p); /* command status */
  753. if (params.status == HCI_SUCCESS) {
  754. params.opcode = op_code;
  755. if (op_code == HCI_BLE_RAND) {
  756. params.param_len = BT_OCTET8_LEN;
  757. } else {
  758. params.param_len = BT_OCTET16_LEN;
  759. }
  760. memcpy(p_dest, p, params.param_len); /* Fetch return info from HCI event message */
  761. }
  762. if (p_enc_cplt_cback) {
  763. (*p_enc_cplt_cback)(&params); /* Call the Encryption complete callback function */
  764. }
  765. }
  766. }
  767. #if (SMP_INCLUDED == TRUE)
  768. /*******************************************************************************
  769. **
  770. ** Function btm_ble_get_enc_key_type
  771. **
  772. ** Description This function is to increment local sign counter
  773. ** Returns None
  774. **
  775. *******************************************************************************/
  776. void btm_ble_increment_sign_ctr(BD_ADDR bd_addr, BOOLEAN is_local )
  777. {
  778. tBTM_SEC_DEV_REC *p_dev_rec;
  779. BTM_TRACE_DEBUG ("btm_ble_increment_sign_ctr is_local=%d", is_local);
  780. if ((p_dev_rec = btm_find_dev (bd_addr)) != NULL) {
  781. if (is_local) {
  782. p_dev_rec->ble.keys.local_counter++;
  783. } else {
  784. p_dev_rec->ble.keys.counter++;
  785. }
  786. BTM_TRACE_DEBUG ("is_local=%d local sign counter=%d peer sign counter=%d",
  787. is_local,
  788. p_dev_rec->ble.keys.local_counter,
  789. p_dev_rec->ble.keys.counter);
  790. }
  791. }
  792. /*******************************************************************************
  793. **
  794. ** Function btm_ble_get_enc_key_type
  795. **
  796. ** Description This function is to get the BLE key type that has been exchanged
  797. ** in betweem local device and peer device.
  798. **
  799. ** Returns p_key_type: output parameter to carry the key type value.
  800. **
  801. *******************************************************************************/
  802. BOOLEAN btm_ble_get_enc_key_type(BD_ADDR bd_addr, UINT8 *p_key_types)
  803. {
  804. tBTM_SEC_DEV_REC *p_dev_rec;
  805. BTM_TRACE_DEBUG ("btm_ble_get_enc_key_type");
  806. if ((p_dev_rec = btm_find_dev (bd_addr)) != NULL) {
  807. *p_key_types = p_dev_rec->ble.key_type;
  808. return TRUE;
  809. }
  810. return FALSE;
  811. }
  812. /*******************************************************************************
  813. **
  814. ** Function btm_get_local_div
  815. **
  816. ** Description This function is called to read the local DIV
  817. **
  818. ** Returns TURE - if a valid DIV is availavle
  819. *******************************************************************************/
  820. BOOLEAN btm_get_local_div (BD_ADDR bd_addr, UINT16 *p_div)
  821. {
  822. tBTM_SEC_DEV_REC *p_dev_rec;
  823. BOOLEAN status = FALSE;
  824. BTM_TRACE_DEBUG ("btm_get_local_div");
  825. BTM_TRACE_DEBUG("bd_addr:%02x-%02x-%02x-%02x-%02x-%02x",
  826. bd_addr[0], bd_addr[1],
  827. bd_addr[2], bd_addr[3],
  828. bd_addr[4], bd_addr[5]);
  829. *p_div = 0;
  830. p_dev_rec = btm_find_dev (bd_addr);
  831. if (p_dev_rec && p_dev_rec->ble.keys.div) {
  832. status = TRUE;
  833. *p_div = p_dev_rec->ble.keys.div;
  834. }
  835. BTM_TRACE_DEBUG ("btm_get_local_div status=%d (1-OK) DIV=0x%x", status, *p_div);
  836. return status;
  837. }
  838. /*******************************************************************************
  839. **
  840. ** Function btm_sec_save_le_key
  841. **
  842. ** Description This function is called by the SMP to update
  843. ** an BLE key. SMP is internal, whereas all the keys shall
  844. ** be sent to the application. The function is also called
  845. ** when application passes ble key stored in NVRAM to the btm_sec.
  846. ** pass_to_application parameter is false in this case.
  847. **
  848. ** Returns void
  849. **
  850. *******************************************************************************/
  851. void btm_sec_save_le_key(BD_ADDR bd_addr, tBTM_LE_KEY_TYPE key_type, tBTM_LE_KEY_VALUE *p_keys,
  852. BOOLEAN pass_to_application)
  853. {
  854. tBTM_SEC_DEV_REC *p_rec;
  855. tBTM_LE_EVT_DATA cb_data;
  856. UINT8 i;
  857. BTM_TRACE_DEBUG ("btm_sec_save_le_key key_type=0x%x pass_to_application=%d", key_type, pass_to_application);
  858. /* Store the updated key in the device database */
  859. BTM_TRACE_DEBUG("bd_addr:%02x-%02x-%02x-%02x-%02x-%02x",
  860. bd_addr[0], bd_addr[1],
  861. bd_addr[2], bd_addr[3],
  862. bd_addr[4], bd_addr[5]);
  863. if ((p_rec = btm_find_dev (bd_addr)) != NULL && (p_keys || key_type == BTM_LE_KEY_LID)) {
  864. btm_ble_init_pseudo_addr (p_rec, bd_addr);
  865. switch (key_type) {
  866. case BTM_LE_KEY_PENC:
  867. memcpy(p_rec->ble.keys.pltk, p_keys->penc_key.ltk, BT_OCTET16_LEN);
  868. memcpy(p_rec->ble.keys.rand, p_keys->penc_key.rand, BT_OCTET8_LEN);
  869. p_rec->ble.keys.sec_level = p_keys->penc_key.sec_level;
  870. p_rec->ble.keys.ediv = p_keys->penc_key.ediv;
  871. p_rec->ble.keys.key_size = p_keys->penc_key.key_size;
  872. p_rec->ble.key_type |= BTM_LE_KEY_PENC;
  873. p_rec->sec_flags |= BTM_SEC_LE_LINK_KEY_KNOWN;
  874. if (p_keys->penc_key.sec_level == SMP_SEC_AUTHENTICATED) {
  875. p_rec->sec_flags |= BTM_SEC_LE_LINK_KEY_AUTHED;
  876. } else {
  877. p_rec->sec_flags &= ~BTM_SEC_LE_LINK_KEY_AUTHED;
  878. }
  879. BTM_TRACE_DEBUG("BTM_LE_KEY_PENC key_type=0x%x sec_flags=0x%x sec_leve=0x%x",
  880. p_rec->ble.key_type,
  881. p_rec->sec_flags,
  882. p_rec->ble.keys.sec_level);
  883. break;
  884. case BTM_LE_KEY_PID:
  885. for (i = 0; i < BT_OCTET16_LEN; i++) {
  886. p_rec->ble.keys.irk[i] = p_keys->pid_key.irk[i];
  887. }
  888. //memcpy( p_rec->ble.keys.irk, p_keys->pid_key, BT_OCTET16_LEN); todo will crash the system
  889. memcpy(p_rec->ble.static_addr, p_keys->pid_key.static_addr, BD_ADDR_LEN);
  890. p_rec->ble.static_addr_type = p_keys->pid_key.addr_type;
  891. p_rec->ble.key_type |= BTM_LE_KEY_PID;
  892. BTM_TRACE_DEBUG("BTM_LE_KEY_PID key_type=0x%x save peer IRK", p_rec->ble.key_type);
  893. /* update device record address as static address */
  894. memcpy(p_rec->bd_addr, p_keys->pid_key.static_addr, BD_ADDR_LEN);
  895. /* combine DUMO device security record if needed */
  896. btm_consolidate_dev(p_rec);
  897. break;
  898. case BTM_LE_KEY_PCSRK:
  899. memcpy(p_rec->ble.keys.pcsrk, p_keys->pcsrk_key.csrk, BT_OCTET16_LEN);
  900. p_rec->ble.keys.srk_sec_level = p_keys->pcsrk_key.sec_level;
  901. p_rec->ble.keys.counter = p_keys->pcsrk_key.counter;
  902. p_rec->ble.key_type |= BTM_LE_KEY_PCSRK;
  903. p_rec->sec_flags |= BTM_SEC_LE_LINK_KEY_KNOWN;
  904. if ( p_keys->pcsrk_key.sec_level == SMP_SEC_AUTHENTICATED) {
  905. p_rec->sec_flags |= BTM_SEC_LE_LINK_KEY_AUTHED;
  906. } else {
  907. p_rec->sec_flags &= ~BTM_SEC_LE_LINK_KEY_AUTHED;
  908. }
  909. BTM_TRACE_DEBUG("BTM_LE_KEY_PCSRK key_type=0x%x sec_flags=0x%x sec_level=0x%x peer_counter=%d",
  910. p_rec->ble.key_type,
  911. p_rec->sec_flags,
  912. p_rec->ble.keys.srk_sec_level,
  913. p_rec->ble.keys.counter );
  914. break;
  915. case BTM_LE_KEY_LENC:
  916. memcpy(p_rec->ble.keys.lltk, p_keys->lenc_key.ltk, BT_OCTET16_LEN);
  917. p_rec->ble.keys.div = p_keys->lenc_key.div; /* update DIV */
  918. p_rec->ble.keys.sec_level = p_keys->lenc_key.sec_level;
  919. p_rec->ble.keys.key_size = p_keys->lenc_key.key_size;
  920. p_rec->ble.key_type |= BTM_LE_KEY_LENC;
  921. BTM_TRACE_DEBUG("BTM_LE_KEY_LENC key_type=0x%x DIV=0x%x key_size=0x%x sec_level=0x%x",
  922. p_rec->ble.key_type,
  923. p_rec->ble.keys.div,
  924. p_rec->ble.keys.key_size,
  925. p_rec->ble.keys.sec_level );
  926. break;
  927. case BTM_LE_KEY_LCSRK:/* local CSRK has been delivered */
  928. memcpy (p_rec->ble.keys.lcsrk, p_keys->lcsrk_key.csrk, BT_OCTET16_LEN);
  929. p_rec->ble.keys.div = p_keys->lcsrk_key.div; /* update DIV */
  930. p_rec->ble.keys.local_csrk_sec_level = p_keys->lcsrk_key.sec_level;
  931. p_rec->ble.keys.local_counter = p_keys->lcsrk_key.counter;
  932. p_rec->ble.key_type |= BTM_LE_KEY_LCSRK;
  933. BTM_TRACE_DEBUG("BTM_LE_KEY_LCSRK key_type=0x%x DIV=0x%x scrk_sec_level=0x%x local_counter=%d",
  934. p_rec->ble.key_type,
  935. p_rec->ble.keys.div,
  936. p_rec->ble.keys.local_csrk_sec_level,
  937. p_rec->ble.keys.local_counter );
  938. break;
  939. case BTM_LE_KEY_LID:
  940. p_rec->ble.key_type |= BTM_LE_KEY_LID;
  941. break;
  942. default:
  943. BTM_TRACE_WARNING("btm_sec_save_le_key (Bad key_type 0x%02x)", key_type);
  944. return;
  945. }
  946. BTM_TRACE_DEBUG ("BLE key type 0x%02x updated for BDA: %08x%04x (btm_sec_save_le_key)", key_type,
  947. (bd_addr[0] << 24) + (bd_addr[1] << 16) + (bd_addr[2] << 8) + bd_addr[3],
  948. (bd_addr[4] << 8) + bd_addr[5]);
  949. /* Notify the application that one of the BLE keys has been updated
  950. If link key is in progress, it will get sent later.*/
  951. if (pass_to_application && btm_cb.api.p_le_callback) {
  952. cb_data.key.p_key_value = p_keys;
  953. cb_data.key.key_type = key_type;
  954. (*btm_cb.api.p_le_callback) (BTM_LE_KEY_EVT, bd_addr, &cb_data);
  955. }
  956. return;
  957. }
  958. BTM_TRACE_WARNING ("BLE key type 0x%02x called for Unknown BDA or type: %08x%04x !! (btm_sec_save_le_key)", key_type,
  959. (bd_addr[0] << 24) + (bd_addr[1] << 16) + (bd_addr[2] << 8) + bd_addr[3],
  960. (bd_addr[4] << 8) + bd_addr[5]);
  961. if (p_rec) {
  962. BTM_TRACE_DEBUG ("sec_flags=0x%x", p_rec->sec_flags);
  963. }
  964. }
  965. /*******************************************************************************
  966. **
  967. ** Function btm_ble_update_sec_key_size
  968. **
  969. ** Description update the current lin kencryption key size
  970. **
  971. ** Returns void
  972. **
  973. *******************************************************************************/
  974. void btm_ble_update_sec_key_size(BD_ADDR bd_addr, UINT8 enc_key_size)
  975. {
  976. tBTM_SEC_DEV_REC *p_rec;
  977. BTM_TRACE_DEBUG("btm_ble_update_sec_key_size enc_key_size = %d", enc_key_size);
  978. if ((p_rec = btm_find_dev (bd_addr)) != NULL ) {
  979. p_rec->enc_key_size = enc_key_size;
  980. }
  981. }
  982. /*******************************************************************************
  983. **
  984. ** Function btm_ble_read_sec_key_size
  985. **
  986. ** Description update the current lin kencryption key size
  987. **
  988. ** Returns void
  989. **
  990. *******************************************************************************/
  991. UINT8 btm_ble_read_sec_key_size(BD_ADDR bd_addr)
  992. {
  993. tBTM_SEC_DEV_REC *p_rec;
  994. if ((p_rec = btm_find_dev (bd_addr)) != NULL ) {
  995. return p_rec->enc_key_size;
  996. } else {
  997. return 0;
  998. }
  999. }
  1000. /*******************************************************************************
  1001. **
  1002. ** Function btm_ble_link_sec_check
  1003. **
  1004. ** Description Check BLE link security level match.
  1005. **
  1006. ** Returns TRUE: check is OK and the *p_sec_req_act contain the action
  1007. **
  1008. *******************************************************************************/
  1009. void btm_ble_link_sec_check(BD_ADDR bd_addr, tBTM_LE_AUTH_REQ auth_req, tBTM_BLE_SEC_REQ_ACT *p_sec_req_act)
  1010. {
  1011. tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev (bd_addr);
  1012. UINT8 req_sec_level = BTM_LE_SEC_NONE, cur_sec_level = BTM_LE_SEC_NONE;
  1013. BTM_TRACE_DEBUG ("btm_ble_link_sec_check auth_req =0x%x", auth_req);
  1014. if (p_dev_rec == NULL) {
  1015. BTM_TRACE_ERROR ("btm_ble_link_sec_check received for unknown device");
  1016. return;
  1017. }
  1018. if (p_dev_rec->sec_state == BTM_SEC_STATE_ENCRYPTING ||
  1019. p_dev_rec->sec_state == BTM_SEC_STATE_AUTHENTICATING) {
  1020. /* race condition: discard the security request while master is encrypting the link */
  1021. *p_sec_req_act = BTM_BLE_SEC_REQ_ACT_DISCARD;
  1022. } else {
  1023. req_sec_level = BTM_LE_SEC_UNAUTHENTICATE;
  1024. if (auth_req & BTM_LE_AUTH_REQ_MITM) {
  1025. req_sec_level = BTM_LE_SEC_AUTHENTICATED;
  1026. }
  1027. BTM_TRACE_DEBUG ("dev_rec sec_flags=0x%x", p_dev_rec->sec_flags);
  1028. /* currently encrpted */
  1029. if (p_dev_rec->sec_flags & BTM_SEC_LE_ENCRYPTED) {
  1030. if (p_dev_rec->sec_flags & BTM_SEC_LE_AUTHENTICATED) {
  1031. cur_sec_level = BTM_LE_SEC_AUTHENTICATED;
  1032. } else {
  1033. cur_sec_level = BTM_LE_SEC_UNAUTHENTICATE;
  1034. }
  1035. } else { /* unencrypted link */
  1036. /* if bonded, get the key security level */
  1037. if (p_dev_rec->ble.key_type & BTM_LE_KEY_PENC) {
  1038. cur_sec_level = p_dev_rec->ble.keys.sec_level;
  1039. } else {
  1040. cur_sec_level = BTM_LE_SEC_NONE;
  1041. }
  1042. }
  1043. if (cur_sec_level >= req_sec_level) {
  1044. /* To avoid re-encryption on an encrypted link for an equal condition encryption */
  1045. *p_sec_req_act = BTM_BLE_SEC_REQ_ACT_ENCRYPT;
  1046. } else {
  1047. *p_sec_req_act = BTM_BLE_SEC_REQ_ACT_PAIR; /* start the pariring process to upgrade the keys*/
  1048. }
  1049. }
  1050. BTM_TRACE_DEBUG("cur_sec_level=%d req_sec_level=%d sec_req_act=%d",
  1051. cur_sec_level,
  1052. req_sec_level,
  1053. *p_sec_req_act);
  1054. }
  1055. /*******************************************************************************
  1056. **
  1057. ** Function btm_ble_set_encryption
  1058. **
  1059. ** Description This function is called to ensure that LE connection is
  1060. ** encrypted. Should be called only on an open connection.
  1061. ** Typically only needed for connections that first want to
  1062. ** bring up unencrypted links, then later encrypt them.
  1063. **
  1064. ** Returns void
  1065. ** the local device ER is copied into er
  1066. **
  1067. *******************************************************************************/
  1068. tBTM_STATUS btm_ble_set_encryption (BD_ADDR bd_addr, void *p_ref_data, UINT8 link_role)
  1069. {
  1070. tBTM_STATUS cmd = BTM_NO_RESOURCES;
  1071. tBTM_BLE_SEC_ACT sec_act = *(tBTM_BLE_SEC_ACT *)p_ref_data ;
  1072. tBTM_SEC_DEV_REC *p_rec = btm_find_dev (bd_addr);
  1073. tBTM_BLE_SEC_REQ_ACT sec_req_act;
  1074. tBTM_LE_AUTH_REQ auth_req;
  1075. if (p_rec == NULL) {
  1076. BTM_TRACE_WARNING ("btm_ble_set_encryption (NULL device record!! sec_act=0x%x", sec_act);
  1077. return (BTM_WRONG_MODE);
  1078. }
  1079. BTM_TRACE_DEBUG ("btm_ble_set_encryption sec_act=0x%x role_master=%d", sec_act, p_rec->role_master);
  1080. if (sec_act == BTM_BLE_SEC_ENCRYPT_MITM) {
  1081. p_rec->security_required |= BTM_SEC_IN_MITM;
  1082. }
  1083. switch (sec_act) {
  1084. case BTM_BLE_SEC_ENCRYPT:
  1085. if (link_role == BTM_ROLE_MASTER) {
  1086. /* start link layer encryption using the security info stored */
  1087. cmd = btm_ble_start_encrypt(bd_addr, FALSE, NULL);
  1088. break;
  1089. }
  1090. /* if salve role then fall through to call SMP_Pair below which will send a
  1091. sec_request to request the master to encrypt the link */
  1092. case BTM_BLE_SEC_ENCRYPT_NO_MITM:
  1093. case BTM_BLE_SEC_ENCRYPT_MITM:
  1094. if (link_role == BTM_ROLE_MASTER) {
  1095. auth_req = (sec_act == BTM_BLE_SEC_ENCRYPT_NO_MITM)
  1096. ? SMP_AUTH_GEN_BOND : (SMP_AUTH_GEN_BOND | SMP_AUTH_YN_BIT);
  1097. btm_ble_link_sec_check (bd_addr, auth_req, &sec_req_act);
  1098. if (sec_req_act == BTM_BLE_SEC_REQ_ACT_ENCRYPT) {
  1099. cmd = btm_ble_start_encrypt(bd_addr, FALSE, NULL);
  1100. break;
  1101. }
  1102. }
  1103. if (SMP_Pair(bd_addr) == SMP_STARTED) {
  1104. cmd = BTM_CMD_STARTED;
  1105. p_rec->sec_state = BTM_SEC_STATE_AUTHENTICATING;
  1106. }
  1107. break;
  1108. default:
  1109. cmd = BTM_WRONG_MODE;
  1110. break;
  1111. }
  1112. return cmd;
  1113. }
  1114. /*******************************************************************************
  1115. **
  1116. ** Function btm_ble_ltk_request
  1117. **
  1118. ** Description This function is called when encryption request is received
  1119. ** on a slave device.
  1120. **
  1121. **
  1122. ** Returns void
  1123. **
  1124. *******************************************************************************/
  1125. void btm_ble_ltk_request(UINT16 handle, UINT8 rand[8], UINT16 ediv)
  1126. {
  1127. tBTM_CB *p_cb = &btm_cb;
  1128. tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev_by_handle (handle);
  1129. BT_OCTET8 dummy_stk = {0};
  1130. BTM_TRACE_DEBUG ("btm_ble_ltk_request");
  1131. p_cb->ediv = ediv;
  1132. memcpy(p_cb->enc_rand, rand, BT_OCTET8_LEN);
  1133. if (p_dev_rec != NULL) {
  1134. if (!smp_proc_ltk_request(p_dev_rec->bd_addr)) {
  1135. btm_ble_ltk_request_reply(p_dev_rec->bd_addr, FALSE, dummy_stk);
  1136. }
  1137. }
  1138. }
  1139. /*******************************************************************************
  1140. **
  1141. ** Function btm_ble_start_encrypt
  1142. **
  1143. ** Description This function is called to start LE encryption.
  1144. **
  1145. **
  1146. ** Returns BTM_SUCCESS if encryption was started successfully
  1147. **
  1148. *******************************************************************************/
  1149. tBTM_STATUS btm_ble_start_encrypt(BD_ADDR bda, BOOLEAN use_stk, BT_OCTET16 stk)
  1150. {
  1151. tBTM_CB *p_cb = &btm_cb;
  1152. tBTM_SEC_DEV_REC *p_rec = btm_find_dev (bda);
  1153. BT_OCTET8 dummy_rand = {0};
  1154. tBTM_STATUS rt = BTM_NO_RESOURCES;
  1155. BTM_TRACE_DEBUG ("btm_ble_start_encrypt");
  1156. if (!p_rec ) {
  1157. BTM_TRACE_ERROR("Link is not active, can not encrypt!");
  1158. return BTM_WRONG_MODE;
  1159. }
  1160. if (p_rec->sec_state == BTM_SEC_STATE_ENCRYPTING) {
  1161. BTM_TRACE_WARNING("Link Encryption is active, Busy!");
  1162. return BTM_BUSY;
  1163. }
  1164. p_cb->enc_handle = p_rec->ble_hci_handle;
  1165. if (use_stk) {
  1166. if (btsnd_hcic_ble_start_enc(p_rec->ble_hci_handle, dummy_rand, 0, stk)) {
  1167. rt = BTM_CMD_STARTED;
  1168. }
  1169. } else if (p_rec->ble.key_type & BTM_LE_KEY_PENC) {
  1170. if (btsnd_hcic_ble_start_enc(p_rec->ble_hci_handle, p_rec->ble.keys.rand,
  1171. p_rec->ble.keys.ediv, p_rec->ble.keys.pltk)) {
  1172. rt = BTM_CMD_STARTED;
  1173. }
  1174. } else {
  1175. BTM_TRACE_ERROR("No key available to encrypt the link");
  1176. }
  1177. if (rt == BTM_CMD_STARTED) {
  1178. if (p_rec->sec_state == BTM_SEC_STATE_IDLE) {
  1179. p_rec->sec_state = BTM_SEC_STATE_ENCRYPTING;
  1180. }
  1181. }
  1182. return rt;
  1183. }
  1184. /*******************************************************************************
  1185. **
  1186. ** Function btm_ble_link_encrypted
  1187. **
  1188. ** Description This function is called when LE link encrption status is changed.
  1189. **
  1190. ** Returns void
  1191. **
  1192. *******************************************************************************/
  1193. void btm_ble_link_encrypted(BD_ADDR bd_addr, UINT8 encr_enable)
  1194. {
  1195. tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev (bd_addr);
  1196. BOOLEAN enc_cback;
  1197. if (!p_dev_rec) {
  1198. BTM_TRACE_WARNING ("btm_ble_link_encrypted (No Device Found!) encr_enable=%d", encr_enable);
  1199. return;
  1200. }
  1201. BTM_TRACE_DEBUG ("btm_ble_link_encrypted encr_enable=%d", encr_enable);
  1202. enc_cback = (p_dev_rec->sec_state == BTM_SEC_STATE_ENCRYPTING);
  1203. smp_link_encrypted(bd_addr, encr_enable);
  1204. BTM_TRACE_DEBUG(" p_dev_rec->sec_flags=0x%x", p_dev_rec->sec_flags);
  1205. if (encr_enable && p_dev_rec->enc_key_size == 0) {
  1206. p_dev_rec->enc_key_size = p_dev_rec->ble.keys.key_size;
  1207. }
  1208. p_dev_rec->sec_state = BTM_SEC_STATE_IDLE;
  1209. if (p_dev_rec->p_callback && enc_cback) {
  1210. if (encr_enable) {
  1211. btm_sec_dev_rec_cback_event(p_dev_rec, BTM_SUCCESS, TRUE);
  1212. } else if (p_dev_rec->role_master) {
  1213. btm_sec_dev_rec_cback_event(p_dev_rec, BTM_ERR_PROCESSING, TRUE);
  1214. }
  1215. }
  1216. /* to notify GATT to send data if any request is pending */
  1217. gatt_notify_enc_cmpl(p_dev_rec->ble.pseudo_addr);
  1218. }
  1219. /*******************************************************************************
  1220. **
  1221. ** Function btm_ble_ltk_request_reply
  1222. **
  1223. ** Description This function is called to send a LTK request reply on a slave
  1224. ** device.
  1225. **
  1226. ** Returns void
  1227. **
  1228. *******************************************************************************/
  1229. void btm_ble_ltk_request_reply(BD_ADDR bda, BOOLEAN use_stk, BT_OCTET16 stk)
  1230. {
  1231. tBTM_SEC_DEV_REC *p_rec = btm_find_dev (bda);
  1232. tBTM_CB *p_cb = &btm_cb;
  1233. if (p_rec == NULL) {
  1234. BTM_TRACE_ERROR("btm_ble_ltk_request_reply received for unknown device");
  1235. return;
  1236. }
  1237. BTM_TRACE_DEBUG ("btm_ble_ltk_request_reply");
  1238. p_cb->enc_handle = p_rec->ble_hci_handle;
  1239. p_cb->key_size = p_rec->ble.keys.key_size;
  1240. BTM_TRACE_ERROR("key size = %d", p_rec->ble.keys.key_size);
  1241. if (use_stk) {
  1242. btsnd_hcic_ble_ltk_req_reply(btm_cb.enc_handle, stk);
  1243. } else { /* calculate LTK using peer device */
  1244. if (p_rec->ble.key_type & BTM_LE_KEY_LENC) {
  1245. btsnd_hcic_ble_ltk_req_reply(btm_cb.enc_handle, p_rec->ble.keys.lltk);
  1246. } else {
  1247. btsnd_hcic_ble_ltk_req_neg_reply(btm_cb.enc_handle);
  1248. }
  1249. }
  1250. }
  1251. /*******************************************************************************
  1252. **
  1253. ** Function btm_ble_io_capabilities_req
  1254. **
  1255. ** Description This function is called to handle SMP get IO capability request.
  1256. **
  1257. ** Returns void
  1258. **
  1259. *******************************************************************************/
  1260. UINT8 btm_ble_io_capabilities_req(tBTM_SEC_DEV_REC *p_dev_rec, tBTM_LE_IO_REQ *p_data)
  1261. {
  1262. UINT8 callback_rc = BTM_SUCCESS;
  1263. BTM_TRACE_DEBUG ("btm_ble_io_capabilities_req");
  1264. if (btm_cb.api.p_le_callback) {
  1265. /* the callback function implementation may change the IO capability... */
  1266. callback_rc = (*btm_cb.api.p_le_callback) (BTM_LE_IO_REQ_EVT, p_dev_rec->bd_addr, (tBTM_LE_EVT_DATA *)p_data);
  1267. }
  1268. #if BTM_OOB_INCLUDED == TRUE
  1269. if ((callback_rc == BTM_SUCCESS) || (BTM_OOB_UNKNOWN != p_data->oob_data))
  1270. #else
  1271. if (callback_rc == BTM_SUCCESS)
  1272. #endif
  1273. {
  1274. #if BTM_BLE_CONFORMANCE_TESTING == TRUE
  1275. if (btm_cb.devcb.keep_rfu_in_auth_req) {
  1276. BTM_TRACE_DEBUG ("btm_ble_io_capabilities_req keep_rfu_in_auth_req = %u",
  1277. btm_cb.devcb.keep_rfu_in_auth_req);
  1278. p_data->auth_req &= BTM_LE_AUTH_REQ_MASK_KEEP_RFU;
  1279. btm_cb.devcb.keep_rfu_in_auth_req = FALSE;
  1280. } else {
  1281. /* default */
  1282. p_data->auth_req &= BTM_LE_AUTH_REQ_MASK;
  1283. }
  1284. #else
  1285. p_data->auth_req &= BTM_LE_AUTH_REQ_MASK;
  1286. #endif
  1287. BTM_TRACE_DEBUG ("btm_ble_io_capabilities_req 1: p_dev_rec->security_required = %d auth_req:%d",
  1288. p_dev_rec->security_required, p_data->auth_req);
  1289. BTM_TRACE_DEBUG ("btm_ble_io_capabilities_req 2: i_keys=0x%x r_keys=0x%x (bit 0-LTK 1-IRK 2-CSRK)",
  1290. p_data->init_keys,
  1291. p_data->resp_keys);
  1292. /* if authentication requires MITM protection, put on the mask */
  1293. if (p_dev_rec->security_required & BTM_SEC_IN_MITM) {
  1294. p_data->auth_req |= BTM_LE_AUTH_REQ_MITM;
  1295. }
  1296. if (!(p_data->auth_req & SMP_AUTH_BOND)) {
  1297. BTM_TRACE_DEBUG("Non bonding: No keys should be exchanged");
  1298. p_data->init_keys = 0;
  1299. p_data->resp_keys = 0;
  1300. }
  1301. BTM_TRACE_DEBUG ("btm_ble_io_capabilities_req 3: auth_req:%d\n", p_data->auth_req);
  1302. BTM_TRACE_DEBUG ("btm_ble_io_capabilities_req 4: i_keys=0x%x r_keys=0x%x\n",
  1303. p_data->init_keys,
  1304. p_data->resp_keys);
  1305. BTM_TRACE_DEBUG ("btm_ble_io_capabilities_req 5: p_data->io_cap = %d auth_req:%d\n",
  1306. p_data->io_cap, p_data->auth_req);
  1307. /* remove MITM protection requirement if IO cap does not allow it */
  1308. if ((p_data->io_cap == BTM_IO_CAP_NONE) && p_data->oob_data == SMP_OOB_NONE) {
  1309. p_data->auth_req &= ~BTM_LE_AUTH_REQ_MITM;
  1310. }
  1311. if (!(p_data->auth_req & SMP_SC_SUPPORT_BIT)) {
  1312. /* if Secure Connections are not supported then remove LK derivation,
  1313. ** and keypress notifications.
  1314. */
  1315. BTM_TRACE_DEBUG("%s-SC not supported -> No LK derivation, no keypress notifications",
  1316. __func__);
  1317. p_data->auth_req &= ~SMP_KP_SUPPORT_BIT;
  1318. p_data->init_keys &= ~SMP_SEC_KEY_TYPE_LK;
  1319. p_data->resp_keys &= ~SMP_SEC_KEY_TYPE_LK;
  1320. }
  1321. BTM_TRACE_DEBUG ("btm_ble_io_capabilities_req 6: IO_CAP:%d oob_data:%d auth_req:0x%02x\n",
  1322. p_data->io_cap, p_data->oob_data, p_data->auth_req);
  1323. }
  1324. return callback_rc;
  1325. }
  1326. /*******************************************************************************
  1327. **
  1328. ** Function btm_ble_br_keys_req
  1329. **
  1330. ** Description This function is called to handle SMP request for keys sent
  1331. ** over BR/EDR.
  1332. **
  1333. ** Returns void
  1334. **
  1335. *******************************************************************************/
  1336. UINT8 btm_ble_br_keys_req(tBTM_SEC_DEV_REC *p_dev_rec, tBTM_LE_IO_REQ *p_data)
  1337. {
  1338. UINT8 callback_rc = BTM_SUCCESS;
  1339. BTM_TRACE_DEBUG ("%s\n", __func__);
  1340. if (btm_cb.api.p_le_callback) {
  1341. /* the callback function implementation may change the IO capability... */
  1342. callback_rc = (*btm_cb.api.p_le_callback) (BTM_LE_IO_REQ_EVT, p_dev_rec->bd_addr,
  1343. (tBTM_LE_EVT_DATA *)p_data);
  1344. }
  1345. return callback_rc;
  1346. }
  1347. #if (BLE_PRIVACY_SPT == TRUE )
  1348. /*******************************************************************************
  1349. **
  1350. ** Function btm_ble_resolve_random_addr_on_conn_cmpl
  1351. **
  1352. ** Description resolve random address complete on connection complete event.
  1353. **
  1354. ** Returns void
  1355. **
  1356. *******************************************************************************/
  1357. static void btm_ble_resolve_random_addr_on_conn_cmpl(void *p_rec, void *p_data)
  1358. {
  1359. UINT8 *p = (UINT8 *)p_data;
  1360. tBTM_SEC_DEV_REC *match_rec = (tBTM_SEC_DEV_REC *) p_rec;
  1361. UINT8 role, bda_type;
  1362. UINT16 handle;
  1363. BD_ADDR bda;
  1364. UINT16 conn_interval, conn_latency, conn_timeout;
  1365. BOOLEAN match = FALSE;
  1366. ++p;
  1367. STREAM_TO_UINT16 (handle, p);
  1368. STREAM_TO_UINT8 (role, p);
  1369. STREAM_TO_UINT8 (bda_type, p);
  1370. STREAM_TO_BDADDR (bda, p);
  1371. STREAM_TO_UINT16 (conn_interval, p);
  1372. STREAM_TO_UINT16 (conn_latency, p);
  1373. STREAM_TO_UINT16 (conn_timeout, p);
  1374. handle = HCID_GET_HANDLE (handle);
  1375. BTM_TRACE_EVENT ("%s\n", __func__);
  1376. if (match_rec) {
  1377. LOG_INFO("%s matched and resolved random address", __func__);
  1378. match = TRUE;
  1379. match_rec->ble.active_addr_type = BTM_BLE_ADDR_RRA;
  1380. memcpy(match_rec->ble.cur_rand_addr, bda, BD_ADDR_LEN);
  1381. if (!btm_ble_init_pseudo_addr (match_rec, bda)) {
  1382. /* assign the original address to be the current report address */
  1383. memcpy(bda, match_rec->ble.pseudo_addr, BD_ADDR_LEN);
  1384. } else {
  1385. memcpy(bda, match_rec->bd_addr, BD_ADDR_LEN);
  1386. }
  1387. } else {
  1388. LOG_INFO("%s unable to match and resolve random address", __func__);
  1389. }
  1390. btm_ble_connected(bda, handle, HCI_ENCRYPT_MODE_DISABLED, role, bda_type, match);
  1391. l2cble_conn_comp (handle, role, bda, bda_type, conn_interval,
  1392. conn_latency, conn_timeout);
  1393. return;
  1394. }
  1395. #endif
  1396. /*******************************************************************************
  1397. **
  1398. ** Function btm_ble_connected
  1399. **
  1400. ** Description This function is when a LE connection to the peer device is
  1401. ** establsihed
  1402. **
  1403. ** Returns void
  1404. **
  1405. *******************************************************************************/
  1406. void btm_ble_connected (UINT8 *bda, UINT16 handle, UINT8 enc_mode, UINT8 role,
  1407. tBLE_ADDR_TYPE addr_type, BOOLEAN addr_matched)
  1408. {
  1409. tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev (bda);
  1410. tBTM_BLE_CB *p_cb = &btm_cb.ble_ctr_cb;
  1411. UNUSED(addr_matched);
  1412. BTM_TRACE_EVENT ("btm_ble_connected");
  1413. /* Commenting out trace due to obf/compilation problems.
  1414. */
  1415. #if (BT_USE_TRACES == TRUE)
  1416. if (p_dev_rec) {
  1417. BTM_TRACE_EVENT ("Security Manager: btm_ble_connected : handle:%d enc_mode:%d bda:%x RName:%s",
  1418. handle, enc_mode,
  1419. (bda[2] << 24) + (bda[3] << 16) + (bda[4] << 8) + bda[5],
  1420. p_dev_rec->sec_bd_name);
  1421. BTM_TRACE_DEBUG ("btm_ble_connected sec_flags=0x%x", p_dev_rec->sec_flags);
  1422. } else {
  1423. BTM_TRACE_EVENT ("Security Manager: btm_ble_connected: handle:%d enc_mode:%d bda:%x ",
  1424. handle, enc_mode,
  1425. (bda[2] << 24) + (bda[3] << 16) + (bda[4] << 8) + bda[5]);
  1426. }
  1427. #endif
  1428. if (!p_dev_rec) {
  1429. /* There is no device record for new connection. Allocate one */
  1430. if ((p_dev_rec = btm_sec_alloc_dev (bda)) == NULL) {
  1431. return;
  1432. }
  1433. } else { /* Update the timestamp for this device */
  1434. p_dev_rec->timestamp = btm_cb.dev_rec_count++;
  1435. }
  1436. /* update device information */
  1437. p_dev_rec->device_type |= BT_DEVICE_TYPE_BLE;
  1438. p_dev_rec->ble_hci_handle = handle;
  1439. p_dev_rec->ble.ble_addr_type = addr_type;
  1440. /* update pseudo address */
  1441. memcpy(p_dev_rec->ble.pseudo_addr, bda, BD_ADDR_LEN);
  1442. p_dev_rec->role_master = FALSE;
  1443. if (role == HCI_ROLE_MASTER) {
  1444. p_dev_rec->role_master = TRUE;
  1445. }
  1446. #if (defined BLE_PRIVACY_SPT && BLE_PRIVACY_SPT == TRUE)
  1447. if (!addr_matched) {
  1448. p_dev_rec->ble.active_addr_type = BTM_BLE_ADDR_PSEUDO;
  1449. }
  1450. if (p_dev_rec->ble.ble_addr_type == BLE_ADDR_RANDOM && !addr_matched) {
  1451. memcpy(p_dev_rec->ble.cur_rand_addr, bda, BD_ADDR_LEN);
  1452. }
  1453. #endif
  1454. p_cb->inq_var.directed_conn = BTM_BLE_CONNECT_EVT;
  1455. return;
  1456. }
  1457. /*****************************************************************************
  1458. ** Function btm_ble_conn_complete
  1459. **
  1460. ** Description LE connection complete.
  1461. **
  1462. ******************************************************************************/
  1463. void btm_ble_conn_complete(UINT8 *p, UINT16 evt_len, BOOLEAN enhanced)
  1464. {
  1465. #if (BLE_PRIVACY_SPT == TRUE )
  1466. UINT8 *p_data = p, peer_addr_type;
  1467. #endif
  1468. UINT8 role, status, bda_type;
  1469. UINT16 handle;
  1470. BD_ADDR bda, local_rpa, peer_rpa;
  1471. UINT16 conn_interval, conn_latency, conn_timeout;
  1472. BOOLEAN match = FALSE;
  1473. UNUSED(evt_len);
  1474. STREAM_TO_UINT8 (status, p);
  1475. STREAM_TO_UINT16 (handle, p);
  1476. STREAM_TO_UINT8 (role, p);
  1477. STREAM_TO_UINT8 (bda_type, p);
  1478. STREAM_TO_BDADDR (bda, p);
  1479. if (status == 0) {
  1480. #if (BLE_PRIVACY_SPT == TRUE )
  1481. peer_addr_type = bda_type;
  1482. match = btm_identity_addr_to_random_pseudo (bda, &bda_type, TRUE);
  1483. if (enhanced) {
  1484. STREAM_TO_BDADDR (local_rpa, p);
  1485. STREAM_TO_BDADDR (peer_rpa, p);
  1486. }
  1487. /* possiblly receive connection complete with resolvable random on
  1488. slave role while the device has been paired */
  1489. if (!match && role == HCI_ROLE_SLAVE && BTM_BLE_IS_RESOLVE_BDA(bda)) {
  1490. btm_ble_resolve_random_addr(bda, btm_ble_resolve_random_addr_on_conn_cmpl, p_data);
  1491. } else
  1492. #endif
  1493. {
  1494. STREAM_TO_UINT16 (conn_interval, p);
  1495. STREAM_TO_UINT16 (conn_latency, p);
  1496. STREAM_TO_UINT16 (conn_timeout, p);
  1497. handle = HCID_GET_HANDLE (handle);
  1498. btm_ble_connected(bda, handle, HCI_ENCRYPT_MODE_DISABLED, role, bda_type, match);
  1499. l2cble_conn_comp (handle, role, bda, bda_type, conn_interval,
  1500. conn_latency, conn_timeout);
  1501. #if (BLE_PRIVACY_SPT == TRUE)
  1502. if (enhanced) {
  1503. btm_ble_refresh_local_resolvable_private_addr(bda, local_rpa);
  1504. if (peer_addr_type & BLE_ADDR_TYPE_ID_BIT) {
  1505. btm_ble_refresh_peer_resolvable_private_addr(bda, peer_rpa, BLE_ADDR_RANDOM);
  1506. }
  1507. }
  1508. #endif
  1509. }
  1510. } else {
  1511. role = HCI_ROLE_UNKNOWN;
  1512. if (status != HCI_ERR_DIRECTED_ADVERTISING_TIMEOUT) {
  1513. btm_ble_set_conn_st(BLE_CONN_IDLE);
  1514. #if (defined BLE_PRIVACY_SPT && BLE_PRIVACY_SPT == TRUE)
  1515. btm_ble_disable_resolving_list(BTM_BLE_RL_INIT, TRUE);
  1516. #endif
  1517. } else {
  1518. #if (defined BLE_PRIVACY_SPT && BLE_PRIVACY_SPT == TRUE)
  1519. btm_cb.ble_ctr_cb.inq_var.adv_mode = BTM_BLE_ADV_DISABLE;
  1520. btm_ble_disable_resolving_list(BTM_BLE_RL_ADV, TRUE);
  1521. #endif
  1522. }
  1523. }
  1524. btm_ble_update_mode_operation(role, bda, status);
  1525. }
  1526. /*****************************************************************************
  1527. ** Function btm_ble_create_ll_conn_complete
  1528. **
  1529. ** Description LE connection complete.
  1530. **
  1531. ******************************************************************************/
  1532. void btm_ble_create_ll_conn_complete (UINT8 status)
  1533. {
  1534. if (status != HCI_SUCCESS) {
  1535. btm_ble_set_conn_st(BLE_CONN_IDLE);
  1536. btm_ble_update_mode_operation(HCI_ROLE_UNKNOWN, NULL, status);
  1537. }
  1538. }
  1539. /*****************************************************************************
  1540. ** Function btm_proc_smp_cback
  1541. **
  1542. ** Description This function is the SMP callback handler.
  1543. **
  1544. ******************************************************************************/
  1545. UINT8 btm_proc_smp_cback(tSMP_EVT event, BD_ADDR bd_addr, tSMP_EVT_DATA *p_data)
  1546. {
  1547. tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev (bd_addr);
  1548. UINT8 res = 0;
  1549. BTM_TRACE_DEBUG ("btm_proc_smp_cback event = %d", event);
  1550. if (p_dev_rec != NULL) {
  1551. switch (event) {
  1552. case SMP_IO_CAP_REQ_EVT:
  1553. btm_ble_io_capabilities_req(p_dev_rec, (tBTM_LE_IO_REQ *)&p_data->io_req);
  1554. break;
  1555. case SMP_BR_KEYS_REQ_EVT:
  1556. btm_ble_br_keys_req(p_dev_rec, (tBTM_LE_IO_REQ *)&p_data->io_req);
  1557. break;
  1558. case SMP_PASSKEY_REQ_EVT:
  1559. case SMP_PASSKEY_NOTIF_EVT:
  1560. case SMP_OOB_REQ_EVT:
  1561. case SMP_NC_REQ_EVT:
  1562. case SMP_SC_OOB_REQ_EVT:
  1563. /* fall through */
  1564. p_dev_rec->sec_flags |= BTM_SEC_LE_AUTHENTICATED;
  1565. case SMP_SEC_REQUEST_EVT:
  1566. if (event == SMP_SEC_REQUEST_EVT && btm_cb.pairing_state != BTM_PAIR_STATE_IDLE) {
  1567. BTM_TRACE_DEBUG("%s: Ignoring SMP Security request", __func__);
  1568. break;
  1569. }
  1570. memcpy (btm_cb.pairing_bda, bd_addr, BD_ADDR_LEN);
  1571. p_dev_rec->sec_state = BTM_SEC_STATE_AUTHENTICATING;
  1572. btm_cb.pairing_flags |= BTM_PAIR_FLAGS_LE_ACTIVE;
  1573. /* fall through */
  1574. case SMP_COMPLT_EVT:
  1575. if (btm_cb.api.p_le_callback) {
  1576. /* the callback function implementation may change the IO capability... */
  1577. BTM_TRACE_DEBUG ("btm_cb.api.p_le_callback=%p", btm_cb.api.p_le_callback );
  1578. (*btm_cb.api.p_le_callback) (event, bd_addr, (tBTM_LE_EVT_DATA *)p_data);
  1579. }
  1580. if (event == SMP_COMPLT_EVT) {
  1581. BTM_TRACE_DEBUG ("evt=SMP_COMPLT_EVT before update sec_level=0x%x sec_flags=0x%x", p_data->cmplt.sec_level , p_dev_rec->sec_flags );
  1582. res = (p_data->cmplt.reason == SMP_SUCCESS) ? BTM_SUCCESS : BTM_ERR_PROCESSING;
  1583. BTM_TRACE_DEBUG ("after update result=%d sec_level=0x%x sec_flags=0x%x",
  1584. res, p_data->cmplt.sec_level , p_dev_rec->sec_flags );
  1585. if (p_data->cmplt.is_pair_cancel && btm_cb.api.p_bond_cancel_cmpl_callback ) {
  1586. BTM_TRACE_DEBUG ("Pairing Cancel completed");
  1587. (*btm_cb.api.p_bond_cancel_cmpl_callback)(BTM_SUCCESS);
  1588. }
  1589. #if BTM_BLE_CONFORMANCE_TESTING == TRUE
  1590. if (res != BTM_SUCCESS) {
  1591. if (!btm_cb.devcb.no_disc_if_pair_fail && p_data->cmplt.reason != SMP_CONN_TOUT) {
  1592. BTM_TRACE_DEBUG ("Pairing failed - prepare to remove ACL");
  1593. l2cu_start_post_bond_timer(p_dev_rec->ble_hci_handle);
  1594. } else {
  1595. BTM_TRACE_DEBUG ("Pairing failed - Not Removing ACL");
  1596. p_dev_rec->sec_state = BTM_SEC_STATE_IDLE;
  1597. }
  1598. }
  1599. #else
  1600. if (res != BTM_SUCCESS && p_data->cmplt.reason != SMP_CONN_TOUT) {
  1601. BTM_TRACE_DEBUG ("Pairing failed - prepare to remove ACL");
  1602. l2cu_start_post_bond_timer(p_dev_rec->ble_hci_handle);
  1603. }
  1604. #endif
  1605. BTM_TRACE_DEBUG ("btm_cb pairing_state=%x pairing_flags=%x pin_code_len=%x",
  1606. btm_cb.pairing_state,
  1607. btm_cb.pairing_flags,
  1608. btm_cb.pin_code_len );
  1609. BTM_TRACE_DEBUG ("btm_cb.pairing_bda %02x:%02x:%02x:%02x:%02x:%02x",
  1610. btm_cb.pairing_bda[0], btm_cb.pairing_bda[1], btm_cb.pairing_bda[2],
  1611. btm_cb.pairing_bda[3], btm_cb.pairing_bda[4], btm_cb.pairing_bda[5]);
  1612. /* Reset btm state only if the callback address matches pairing address*/
  1613. if (memcmp(bd_addr, btm_cb.pairing_bda, BD_ADDR_LEN) == 0) {
  1614. memset (btm_cb.pairing_bda, 0xff, BD_ADDR_LEN);
  1615. btm_cb.pairing_state = BTM_PAIR_STATE_IDLE;
  1616. btm_cb.pairing_flags = 0;
  1617. }
  1618. if (res == BTM_SUCCESS) {
  1619. p_dev_rec->sec_state = BTM_SEC_STATE_IDLE;
  1620. #if (defined BLE_PRIVACY_SPT && BLE_PRIVACY_SPT == TRUE)
  1621. /* add all bonded device into resolving list if IRK is available*/
  1622. btm_ble_resolving_list_load_dev(p_dev_rec);
  1623. #endif
  1624. }
  1625. btm_sec_dev_rec_cback_event(p_dev_rec, res, TRUE);
  1626. }
  1627. break;
  1628. default:
  1629. BTM_TRACE_DEBUG ("unknown event = %d", event);
  1630. break;
  1631. }
  1632. } else {
  1633. BTM_TRACE_ERROR("btm_proc_smp_cback received for unknown device");
  1634. }
  1635. return 0;
  1636. }
  1637. #endif /* SMP_INCLUDED */
  1638. /*******************************************************************************
  1639. **
  1640. ** Function BTM_BleDataSignature
  1641. **
  1642. ** Description This function is called to sign the data using AES128 CMAC
  1643. ** algorith.
  1644. **
  1645. ** Parameter bd_addr: target device the data to be signed for.
  1646. ** p_text: singing data
  1647. ** len: length of the data to be signed.
  1648. ** signature: output parameter where data signature is going to
  1649. ** be stored.
  1650. **
  1651. ** Returns TRUE if signing sucessul, otherwise FALSE.
  1652. **
  1653. *******************************************************************************/
  1654. BOOLEAN BTM_BleDataSignature (BD_ADDR bd_addr, UINT8 *p_text, UINT16 len,
  1655. BLE_SIGNATURE signature)
  1656. {
  1657. tBTM_SEC_DEV_REC *p_rec = btm_find_dev (bd_addr);
  1658. BTM_TRACE_DEBUG ("%s", __func__);
  1659. BOOLEAN ret = FALSE;
  1660. if (p_rec == NULL) {
  1661. BTM_TRACE_ERROR("%s-data signing can not be done from unknown device", __func__);
  1662. } else {
  1663. UINT8 *p_mac = (UINT8 *)signature;
  1664. UINT8 *p_buf, *pp;
  1665. if ((p_buf = (UINT8 *)GKI_getbuf((UINT16)(len + 4))) != NULL) {
  1666. BTM_TRACE_DEBUG("%s-Start to generate Local CSRK", __func__);
  1667. pp = p_buf;
  1668. /* prepare plain text */
  1669. if (p_text) {
  1670. memcpy(p_buf, p_text, len);
  1671. pp = (p_buf + len);
  1672. }
  1673. UINT32_TO_STREAM(pp, p_rec->ble.keys.local_counter);
  1674. UINT32_TO_STREAM(p_mac, p_rec->ble.keys.local_counter);
  1675. if ((ret = aes_cipher_msg_auth_code(p_rec->ble.keys.lcsrk, p_buf, (UINT16)(len + 4),
  1676. BTM_CMAC_TLEN_SIZE, p_mac)) == TRUE) {
  1677. btm_ble_increment_sign_ctr(bd_addr, TRUE);
  1678. }
  1679. BTM_TRACE_DEBUG("%s p_mac = %p", __func__, p_mac);
  1680. BTM_TRACE_DEBUG("p_mac[0] = 0x%02x p_mac[1] = 0x%02x p_mac[2] = 0x%02x p_mac[3] = 0x%02x",
  1681. *p_mac, *(p_mac + 1), *(p_mac + 2), *(p_mac + 3));
  1682. BTM_TRACE_DEBUG("p_mac[4] = 0x%02x p_mac[5] = 0x%02x p_mac[6] = 0x%02x p_mac[7] = 0x%02x",
  1683. *(p_mac + 4), *(p_mac + 5), *(p_mac + 6), *(p_mac + 7));
  1684. GKI_freebuf(p_buf);
  1685. }
  1686. }
  1687. return ret;
  1688. }
  1689. /*******************************************************************************
  1690. **
  1691. ** Function BTM_BleVerifySignature
  1692. **
  1693. ** Description This function is called to verify the data signature
  1694. **
  1695. ** Parameter bd_addr: target device the data to be signed for.
  1696. ** p_orig: original data before signature.
  1697. ** len: length of the signing data
  1698. ** counter: counter used when doing data signing
  1699. ** p_comp: signature to be compared against.
  1700. ** Returns TRUE if signature verified correctly; otherwise FALSE.
  1701. **
  1702. *******************************************************************************/
  1703. BOOLEAN BTM_BleVerifySignature (BD_ADDR bd_addr, UINT8 *p_orig, UINT16 len, UINT32 counter,
  1704. UINT8 *p_comp)
  1705. {
  1706. BOOLEAN verified = FALSE;
  1707. #if SMP_INCLUDED == TRUE
  1708. tBTM_SEC_DEV_REC *p_rec = btm_find_dev (bd_addr);
  1709. UINT8 p_mac[BTM_CMAC_TLEN_SIZE];
  1710. if (p_rec == NULL || (p_rec && !(p_rec->ble.key_type & BTM_LE_KEY_PCSRK))) {
  1711. BTM_TRACE_ERROR("can not verify signature for unknown device");
  1712. } else if (counter < p_rec->ble.keys.counter) {
  1713. BTM_TRACE_ERROR("signature received with out dated sign counter");
  1714. } else if (p_orig == NULL) {
  1715. BTM_TRACE_ERROR("No signature to verify");
  1716. } else {
  1717. BTM_TRACE_DEBUG ("%s rcv_cnt=%d >= expected_cnt=%d", __func__, counter,
  1718. p_rec->ble.keys.counter);
  1719. if (aes_cipher_msg_auth_code(p_rec->ble.keys.pcsrk, p_orig, len, BTM_CMAC_TLEN_SIZE, p_mac)) {
  1720. if (memcmp(p_mac, p_comp, BTM_CMAC_TLEN_SIZE) == 0) {
  1721. btm_ble_increment_sign_ctr(bd_addr, FALSE);
  1722. verified = TRUE;
  1723. }
  1724. }
  1725. }
  1726. #endif /* SMP_INCLUDED */
  1727. return verified;
  1728. }
  1729. /*******************************************************************************
  1730. **
  1731. ** Function BTM_GetLeSecurityState
  1732. **
  1733. ** Description This function is called to get security mode 1 flags and
  1734. ** encryption key size for LE peer.
  1735. **
  1736. ** Returns BOOLEAN TRUE if LE device is found, FALSE otherwise.
  1737. **
  1738. *******************************************************************************/
  1739. BOOLEAN BTM_GetLeSecurityState (BD_ADDR bd_addr, UINT8 *p_le_dev_sec_flags, UINT8 *p_le_key_size)
  1740. {
  1741. #if (BLE_INCLUDED == TRUE)
  1742. tBTM_SEC_DEV_REC *p_dev_rec;
  1743. UINT16 dev_rec_sec_flags;
  1744. #endif
  1745. *p_le_dev_sec_flags = 0;
  1746. *p_le_key_size = 0;
  1747. #if (BLE_INCLUDED == TRUE && SMP_INCLUDED == TRUE)
  1748. if ((p_dev_rec = btm_find_dev (bd_addr)) == NULL) {
  1749. BTM_TRACE_ERROR ("%s fails", __func__);
  1750. return (FALSE);
  1751. }
  1752. if (p_dev_rec->ble_hci_handle == BTM_SEC_INVALID_HANDLE) {
  1753. BTM_TRACE_ERROR ("%s-this is not LE device", __func__);
  1754. return (FALSE);
  1755. }
  1756. dev_rec_sec_flags = p_dev_rec->sec_flags;
  1757. if (dev_rec_sec_flags & BTM_SEC_LE_ENCRYPTED) {
  1758. /* link is encrypted with LTK or STK */
  1759. *p_le_key_size = p_dev_rec->enc_key_size;
  1760. *p_le_dev_sec_flags |= BTM_SEC_LE_LINK_ENCRYPTED;
  1761. *p_le_dev_sec_flags |= (dev_rec_sec_flags & BTM_SEC_LE_AUTHENTICATED)
  1762. ? BTM_SEC_LE_LINK_PAIRED_WITH_MITM /* set auth LTK flag */
  1763. : BTM_SEC_LE_LINK_PAIRED_WITHOUT_MITM; /* set unauth LTK flag */
  1764. } else if (p_dev_rec->ble.key_type & BTM_LE_KEY_PENC) {
  1765. /* link is unencrypted, still LTK is available */
  1766. *p_le_key_size = p_dev_rec->ble.keys.key_size;
  1767. *p_le_dev_sec_flags |= (dev_rec_sec_flags & BTM_SEC_LE_LINK_KEY_AUTHED)
  1768. ? BTM_SEC_LE_LINK_PAIRED_WITH_MITM /* set auth LTK flag */
  1769. : BTM_SEC_LE_LINK_PAIRED_WITHOUT_MITM; /* set unauth LTK flag */
  1770. }
  1771. BTM_TRACE_DEBUG ("%s - le_dev_sec_flags: 0x%02x, le_key_size: %d",
  1772. __func__, *p_le_dev_sec_flags, *p_le_key_size);
  1773. return TRUE;
  1774. #else
  1775. return FALSE;
  1776. #endif
  1777. }
  1778. /*******************************************************************************
  1779. **
  1780. ** Function BTM_BleSecurityProcedureIsRunning
  1781. **
  1782. ** Description This function indicates if LE security procedure is
  1783. ** currently running with the peer.
  1784. **
  1785. ** Returns BOOLEAN TRUE if security procedure is running, FALSE otherwise.
  1786. **
  1787. *******************************************************************************/
  1788. BOOLEAN BTM_BleSecurityProcedureIsRunning(BD_ADDR bd_addr)
  1789. {
  1790. #if (BLE_INCLUDED == TRUE)
  1791. tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev (bd_addr);
  1792. if (p_dev_rec == NULL) {
  1793. BTM_TRACE_ERROR ("%s device with BDA: %08x%04x is not found",
  1794. __func__, (bd_addr[0] << 24) + (bd_addr[1] << 16) + (bd_addr[2] << 8) + bd_addr[3],
  1795. (bd_addr[4] << 8) + bd_addr[5]);
  1796. return FALSE;
  1797. }
  1798. return (p_dev_rec->sec_state == BTM_SEC_STATE_ENCRYPTING ||
  1799. p_dev_rec->sec_state == BTM_SEC_STATE_AUTHENTICATING);
  1800. #else
  1801. return FALSE;
  1802. #endif
  1803. }
  1804. /*******************************************************************************
  1805. **
  1806. ** Function BTM_BleGetSupportedKeySize
  1807. **
  1808. ** Description This function gets the maximum encryption key size in bytes
  1809. ** the local device can suport.
  1810. ** record.
  1811. **
  1812. ** Returns the key size or 0 if the size can't be retrieved.
  1813. **
  1814. *******************************************************************************/
  1815. extern UINT8 BTM_BleGetSupportedKeySize (BD_ADDR bd_addr)
  1816. {
  1817. #ifndef L2CAP_LE_COC_INCLUDED
  1818. #define L2CAP_LE_COC_INCLUDED FALSE
  1819. #endif
  1820. #if ((BLE_INCLUDED == TRUE) && (L2CAP_LE_COC_INCLUDED == TRUE))
  1821. tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev (bd_addr);
  1822. tBTM_LE_IO_REQ dev_io_cfg;
  1823. UINT8 callback_rc;
  1824. if (!p_dev_rec) {
  1825. BTM_TRACE_ERROR ("%s device with BDA: %08x%04x is not found",
  1826. __func__, (bd_addr[0] << 24) + (bd_addr[1] << 16) + (bd_addr[2] << 8) + bd_addr[3],
  1827. (bd_addr[4] << 8) + bd_addr[5]);
  1828. return 0;
  1829. }
  1830. if (btm_cb.api.p_le_callback == NULL) {
  1831. BTM_TRACE_ERROR ("%s can't access supported key size", __func__);
  1832. return 0;
  1833. }
  1834. callback_rc = (*btm_cb.api.p_le_callback) (BTM_LE_IO_REQ_EVT, p_dev_rec->bd_addr,
  1835. (tBTM_LE_EVT_DATA *) &dev_io_cfg);
  1836. if (callback_rc != BTM_SUCCESS) {
  1837. BTM_TRACE_ERROR ("%s can't access supported key size", __func__);
  1838. return 0;
  1839. }
  1840. BTM_TRACE_DEBUG ("%s device supports key size = %d", __func__, dev_io_cfg.max_key_size);
  1841. return (dev_io_cfg.max_key_size);
  1842. #else
  1843. return 0;
  1844. #endif
  1845. }
  1846. /*******************************************************************************
  1847. ** Utility functions for LE device IR/ER generation
  1848. *******************************************************************************/
  1849. /*******************************************************************************
  1850. **
  1851. ** Function btm_notify_new_key
  1852. **
  1853. ** Description This function is to notify application new keys have been
  1854. ** generated.
  1855. **
  1856. ** Returns void
  1857. **
  1858. *******************************************************************************/
  1859. static void btm_notify_new_key(UINT8 key_type)
  1860. {
  1861. tBTM_BLE_LOCAL_KEYS *p_locak_keys = NULL;
  1862. BTM_TRACE_DEBUG ("btm_notify_new_key key_type=%d", key_type);
  1863. if (btm_cb.api.p_le_key_callback) {
  1864. switch (key_type) {
  1865. case BTM_BLE_KEY_TYPE_ID:
  1866. BTM_TRACE_DEBUG ("BTM_BLE_KEY_TYPE_ID");
  1867. p_locak_keys = (tBTM_BLE_LOCAL_KEYS *)&btm_cb.devcb.id_keys;
  1868. break;
  1869. case BTM_BLE_KEY_TYPE_ER:
  1870. BTM_TRACE_DEBUG ("BTM_BLE_KEY_TYPE_ER");
  1871. p_locak_keys = (tBTM_BLE_LOCAL_KEYS *)&btm_cb.devcb.ble_encryption_key_value;
  1872. break;
  1873. default:
  1874. BTM_TRACE_ERROR("unknown key type: %d", key_type);
  1875. break;
  1876. }
  1877. if (p_locak_keys != NULL) {
  1878. (*btm_cb.api.p_le_key_callback) (key_type, p_locak_keys);
  1879. }
  1880. }
  1881. }
  1882. /*******************************************************************************
  1883. **
  1884. ** Function btm_ble_process_er2
  1885. **
  1886. ** Description This function is called when ER is generated, store it in
  1887. ** local control block.
  1888. **
  1889. ** Returns void
  1890. **
  1891. *******************************************************************************/
  1892. static void btm_ble_process_er2(tBTM_RAND_ENC *p)
  1893. {
  1894. BTM_TRACE_DEBUG ("btm_ble_process_er2");
  1895. if (p && p->opcode == HCI_BLE_RAND) {
  1896. memcpy(&btm_cb.devcb.ble_encryption_key_value[8], p->param_buf, BT_OCTET8_LEN);
  1897. btm_notify_new_key(BTM_BLE_KEY_TYPE_ER);
  1898. } else {
  1899. BTM_TRACE_ERROR("Generating ER2 exception.");
  1900. memset(&btm_cb.devcb.ble_encryption_key_value, 0, sizeof(BT_OCTET16));
  1901. }
  1902. }
  1903. /*******************************************************************************
  1904. **
  1905. ** Function btm_ble_process_er
  1906. **
  1907. ** Description This function is called when ER is generated, store it in
  1908. ** local control block.
  1909. **
  1910. ** Returns void
  1911. **
  1912. *******************************************************************************/
  1913. static void btm_ble_process_er(tBTM_RAND_ENC *p)
  1914. {
  1915. BTM_TRACE_DEBUG ("btm_ble_process_er");
  1916. if (p && p->opcode == HCI_BLE_RAND) {
  1917. memcpy(&btm_cb.devcb.ble_encryption_key_value[0], p->param_buf, BT_OCTET8_LEN);
  1918. if (!btsnd_hcic_ble_rand((void *)btm_ble_process_er2)) {
  1919. memset(&btm_cb.devcb.ble_encryption_key_value, 0, sizeof(BT_OCTET16));
  1920. BTM_TRACE_ERROR("Generating ER2 failed.");
  1921. }
  1922. } else {
  1923. BTM_TRACE_ERROR("Generating ER1 exception.");
  1924. }
  1925. }
  1926. /*******************************************************************************
  1927. **
  1928. ** Function btm_ble_process_irk
  1929. **
  1930. ** Description This function is called when IRK is generated, store it in
  1931. ** local control block.
  1932. **
  1933. ** Returns void
  1934. **
  1935. *******************************************************************************/
  1936. static void btm_ble_process_irk(tSMP_ENC *p)
  1937. {
  1938. BTM_TRACE_DEBUG ("btm_ble_process_irk");
  1939. if (p && p->opcode == HCI_BLE_ENCRYPT) {
  1940. memcpy(btm_cb.devcb.id_keys.irk, p->param_buf, BT_OCTET16_LEN);
  1941. btm_notify_new_key(BTM_BLE_KEY_TYPE_ID);
  1942. #if BLE_PRIVACY_SPT == TRUE
  1943. /* if privacy is enabled, new RPA should be calculated */
  1944. if (btm_cb.ble_ctr_cb.privacy_mode != BTM_PRIVACY_NONE) {
  1945. btm_gen_resolvable_private_addr((void *)btm_gen_resolve_paddr_low);
  1946. }
  1947. #endif
  1948. } else {
  1949. BTM_TRACE_ERROR("Generating IRK exception.");
  1950. }
  1951. /* proceed generate ER */
  1952. if (!btsnd_hcic_ble_rand((void *)btm_ble_process_er)) {
  1953. BTM_TRACE_ERROR("Generating ER failed.");
  1954. }
  1955. }
  1956. /*******************************************************************************
  1957. **
  1958. ** Function btm_ble_process_dhk
  1959. **
  1960. ** Description This function is called when DHK is calculated, store it in
  1961. ** local control block, and proceed to generate ER, a 128-bits
  1962. ** random number.
  1963. **
  1964. ** Returns void
  1965. **
  1966. *******************************************************************************/
  1967. static void btm_ble_process_dhk(tSMP_ENC *p)
  1968. {
  1969. #if SMP_INCLUDED == TRUE
  1970. UINT8 btm_ble_irk_pt = 0x01;
  1971. tSMP_ENC output;
  1972. BTM_TRACE_DEBUG ("btm_ble_process_dhk");
  1973. if (p && p->opcode == HCI_BLE_ENCRYPT) {
  1974. memcpy(btm_cb.devcb.id_keys.dhk, p->param_buf, BT_OCTET16_LEN);
  1975. BTM_TRACE_DEBUG("BLE DHK generated.");
  1976. /* IRK = D1(IR, 1) */
  1977. if (!SMP_Encrypt(btm_cb.devcb.id_keys.ir, BT_OCTET16_LEN, &btm_ble_irk_pt,
  1978. 1, &output)) {
  1979. /* reset all identity root related key */
  1980. memset(&btm_cb.devcb.id_keys, 0, sizeof(tBTM_BLE_LOCAL_ID_KEYS));
  1981. } else {
  1982. btm_ble_process_irk(&output);
  1983. }
  1984. } else {
  1985. /* reset all identity root related key */
  1986. memset(&btm_cb.devcb.id_keys, 0, sizeof(tBTM_BLE_LOCAL_ID_KEYS));
  1987. }
  1988. #endif
  1989. }
  1990. /*******************************************************************************
  1991. **
  1992. ** Function btm_ble_process_ir2
  1993. **
  1994. ** Description This function is called when IR is generated, proceed to calculate
  1995. ** DHK = Eir({0x03, 0, 0 ...})
  1996. **
  1997. **
  1998. ** Returns void
  1999. **
  2000. *******************************************************************************/
  2001. static void btm_ble_process_ir2(tBTM_RAND_ENC *p)
  2002. {
  2003. #if SMP_INCLUDED == TRUE
  2004. UINT8 btm_ble_dhk_pt = 0x03;
  2005. tSMP_ENC output;
  2006. BTM_TRACE_DEBUG ("btm_ble_process_ir2");
  2007. if (p && p->opcode == HCI_BLE_RAND) {
  2008. /* remembering in control block */
  2009. memcpy(&btm_cb.devcb.id_keys.ir[8], p->param_buf, BT_OCTET8_LEN);
  2010. /* generate DHK= Eir({0x03, 0x00, 0x00 ...}) */
  2011. SMP_Encrypt(btm_cb.devcb.id_keys.ir, BT_OCTET16_LEN, &btm_ble_dhk_pt,
  2012. 1, &output);
  2013. btm_ble_process_dhk(&output);
  2014. BTM_TRACE_DEBUG("BLE IR generated.");
  2015. } else {
  2016. memset(&btm_cb.devcb.id_keys, 0, sizeof(tBTM_BLE_LOCAL_ID_KEYS));
  2017. }
  2018. #endif
  2019. }
  2020. /*******************************************************************************
  2021. **
  2022. ** Function btm_ble_process_ir
  2023. **
  2024. ** Description This function is called when IR is generated, proceed to calculate
  2025. ** DHK = Eir({0x02, 0, 0 ...})
  2026. **
  2027. **
  2028. ** Returns void
  2029. **
  2030. *******************************************************************************/
  2031. static void btm_ble_process_ir(tBTM_RAND_ENC *p)
  2032. {
  2033. BTM_TRACE_DEBUG ("btm_ble_process_ir");
  2034. if (p && p->opcode == HCI_BLE_RAND) {
  2035. /* remembering in control block */
  2036. memcpy(btm_cb.devcb.id_keys.ir, p->param_buf, BT_OCTET8_LEN);
  2037. if (!btsnd_hcic_ble_rand((void *)btm_ble_process_ir2)) {
  2038. BTM_TRACE_ERROR("Generating IR2 failed.");
  2039. memset(&btm_cb.devcb.id_keys, 0, sizeof(tBTM_BLE_LOCAL_ID_KEYS));
  2040. }
  2041. }
  2042. }
  2043. /*******************************************************************************
  2044. **
  2045. ** Function btm_ble_reset_id
  2046. **
  2047. ** Description This function is called to reset LE device identity.
  2048. **
  2049. ** Returns void
  2050. **
  2051. *******************************************************************************/
  2052. void btm_ble_reset_id( void )
  2053. {
  2054. BTM_TRACE_DEBUG ("btm_ble_reset_id");
  2055. /* regenrate Identity Root*/
  2056. if (!btsnd_hcic_ble_rand((void *)btm_ble_process_ir)) {
  2057. BTM_TRACE_DEBUG("Generating IR failed.");
  2058. }
  2059. }
  2060. #if BTM_BLE_CONFORMANCE_TESTING == TRUE
  2061. /*******************************************************************************
  2062. **
  2063. ** Function btm_ble_set_no_disc_if_pair_fail
  2064. **
  2065. ** Description This function indicates that whether no disconnect of the ACL
  2066. ** should be used if pairing failed
  2067. **
  2068. ** Returns void
  2069. **
  2070. *******************************************************************************/
  2071. void btm_ble_set_no_disc_if_pair_fail(BOOLEAN disable_disc )
  2072. {
  2073. BTM_TRACE_DEBUG ("btm_ble_set_disc_enable_if_pair_fail disable_disc=%d", disable_disc);
  2074. btm_cb.devcb.no_disc_if_pair_fail = disable_disc;
  2075. }
  2076. /*******************************************************************************
  2077. **
  2078. ** Function btm_ble_set_test_mac_value
  2079. **
  2080. ** Description This function set test MAC value
  2081. **
  2082. ** Returns void
  2083. **
  2084. *******************************************************************************/
  2085. void btm_ble_set_test_mac_value(BOOLEAN enable, UINT8 *p_test_mac_val )
  2086. {
  2087. BTM_TRACE_DEBUG ("btm_ble_set_test_mac_value enable=%d", enable);
  2088. btm_cb.devcb.enable_test_mac_val = enable;
  2089. memcpy(btm_cb.devcb.test_mac, p_test_mac_val, BT_OCTET8_LEN);
  2090. }
  2091. /*******************************************************************************
  2092. **
  2093. ** Function btm_ble_set_test_local_sign_cntr_value
  2094. **
  2095. ** Description This function set test local sign counter value
  2096. **
  2097. ** Returns void
  2098. **
  2099. *******************************************************************************/
  2100. void btm_ble_set_test_local_sign_cntr_value(BOOLEAN enable, UINT32 test_local_sign_cntr )
  2101. {
  2102. BTM_TRACE_DEBUG ("btm_ble_set_test_local_sign_cntr_value enable=%d local_sign_cntr=%d",
  2103. enable, test_local_sign_cntr);
  2104. btm_cb.devcb.enable_test_local_sign_cntr = enable;
  2105. btm_cb.devcb.test_local_sign_cntr = test_local_sign_cntr;
  2106. }
  2107. /*******************************************************************************
  2108. **
  2109. ** Function btm_set_random_address
  2110. **
  2111. ** Description This function set a random address to local controller.
  2112. **
  2113. ** Returns void
  2114. **
  2115. *******************************************************************************/
  2116. void btm_set_random_address(BD_ADDR random_bda)
  2117. {
  2118. tBTM_LE_RANDOM_CB *p_cb = &btm_cb.ble_ctr_cb.addr_mgnt_cb;
  2119. BOOLEAN adv_mode = btm_cb.ble_ctr_cb.inq_var.adv_mode ;
  2120. BTM_TRACE_DEBUG ("btm_set_random_address");
  2121. if (adv_mode == BTM_BLE_ADV_ENABLE) {
  2122. btsnd_hcic_ble_set_adv_enable (BTM_BLE_ADV_DISABLE);
  2123. }
  2124. memcpy(p_cb->private_addr, random_bda, BD_ADDR_LEN);
  2125. btsnd_hcic_ble_set_random_addr(p_cb->private_addr);
  2126. if (adv_mode == BTM_BLE_ADV_ENABLE) {
  2127. btsnd_hcic_ble_set_adv_enable (BTM_BLE_ADV_ENABLE);
  2128. }
  2129. }
  2130. /*******************************************************************************
  2131. **
  2132. ** Function btm_ble_set_keep_rfu_in_auth_req
  2133. **
  2134. ** Description This function indicates if RFU bits have to be kept as is
  2135. ** (by default they have to be set to 0 by the sender).
  2136. **
  2137. ** Returns void
  2138. **
  2139. *******************************************************************************/
  2140. void btm_ble_set_keep_rfu_in_auth_req(BOOLEAN keep_rfu)
  2141. {
  2142. BTM_TRACE_DEBUG ("btm_ble_set_keep_rfu_in_auth_req keep_rfus=%d", keep_rfu);
  2143. btm_cb.devcb.keep_rfu_in_auth_req = keep_rfu;
  2144. }
  2145. #endif /* BTM_BLE_CONFORMANCE_TESTING */
  2146. #endif /* BLE_INCLUDED */