btm_ble.c 105 KB

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