btm_ble.c 102 KB

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