btm_ble.cc 86 KB

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