btm_ble.c 98 KB

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