btm_ble.c 100 KB

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