smp_act.cc 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102
  1. /******************************************************************************
  2. *
  3. * Copyright 2003-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. #include <cutils/log.h>
  19. #include <log/log.h>
  20. #include <string.h>
  21. #include "btif_common.h"
  22. #include "btif_storage.h"
  23. #include "device/include/interop.h"
  24. #include "internal_include/bt_target.h"
  25. #include "stack/btm/btm_int.h"
  26. #include "stack/include/l2c_api.h"
  27. #include "stack/smp/p_256_ecc_pp.h"
  28. #include "stack/smp/smp_int.h"
  29. #include "utils/include/bt_utils.h"
  30. #define SMP_KEY_DIST_TYPE_MAX 4
  31. const tSMP_ACT smp_distribute_act[] = {
  32. smp_generate_ltk, /* SMP_SEC_KEY_TYPE_ENC - '1' bit index */
  33. smp_send_id_info, /* SMP_SEC_KEY_TYPE_ID - '1' bit index */
  34. smp_generate_csrk, /* SMP_SEC_KEY_TYPE_CSRK - '1' bit index */
  35. smp_set_derive_link_key /* SMP_SEC_KEY_TYPE_LK - '1' bit index */
  36. };
  37. static bool lmp_version_below(const RawAddress& bda, uint8_t version) {
  38. tACL_CONN* acl = btm_bda_to_acl(bda, BT_TRANSPORT_LE);
  39. if (acl == NULL || acl->lmp_version == 0) {
  40. SMP_TRACE_WARNING("%s cannot retrieve LMP version...", __func__);
  41. return false;
  42. }
  43. SMP_TRACE_WARNING("%s LMP version %d < %d", __func__, acl->lmp_version,
  44. version);
  45. return acl->lmp_version < version;
  46. }
  47. static bool pts_test_send_authentication_complete_failure(tSMP_CB* p_cb) {
  48. uint8_t reason = p_cb->cert_failure;
  49. if (reason == SMP_PAIR_AUTH_FAIL || reason == SMP_PAIR_FAIL_UNKNOWN ||
  50. reason == SMP_PAIR_NOT_SUPPORT || reason == SMP_PASSKEY_ENTRY_FAIL ||
  51. reason == SMP_REPEATED_ATTEMPTS) {
  52. tSMP_INT_DATA smp_int_data;
  53. smp_int_data.status = reason;
  54. smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &smp_int_data);
  55. return true;
  56. }
  57. return false;
  58. }
  59. /*******************************************************************************
  60. * Function smp_update_key_mask
  61. * Description This function updates the key mask for sending or receiving.
  62. ******************************************************************************/
  63. static void smp_update_key_mask(tSMP_CB* p_cb, uint8_t key_type, bool recv) {
  64. SMP_TRACE_DEBUG(
  65. "%s before update role=%d recv=%d local_i_key = %02x, local_r_key = %02x",
  66. __func__, p_cb->role, recv, p_cb->local_i_key, p_cb->local_r_key);
  67. if (((p_cb->le_secure_connections_mode_is_used) || (p_cb->smp_over_br)) &&
  68. ((key_type == SMP_SEC_KEY_TYPE_ENC) ||
  69. (key_type == SMP_SEC_KEY_TYPE_LK))) {
  70. /* in LE SC mode LTK, CSRK and BR/EDR LK are derived locally instead of
  71. ** being exchanged with the peer */
  72. p_cb->local_i_key &= ~key_type;
  73. p_cb->local_r_key &= ~key_type;
  74. } else if (p_cb->role == HCI_ROLE_SLAVE) {
  75. if (recv)
  76. p_cb->local_i_key &= ~key_type;
  77. else
  78. p_cb->local_r_key &= ~key_type;
  79. } else {
  80. if (recv)
  81. p_cb->local_r_key &= ~key_type;
  82. else
  83. p_cb->local_i_key &= ~key_type;
  84. }
  85. SMP_TRACE_DEBUG("updated local_i_key = %02x, local_r_key = %02x",
  86. p_cb->local_i_key, p_cb->local_r_key);
  87. }
  88. /*******************************************************************************
  89. * Function smp_send_app_cback
  90. * Description notifies application about the events the application is
  91. * interested in
  92. ******************************************************************************/
  93. void smp_send_app_cback(tSMP_CB* p_cb, tSMP_INT_DATA* p_data) {
  94. tSMP_EVT_DATA cb_data;
  95. tSMP_STATUS callback_rc;
  96. SMP_TRACE_DEBUG("%s p_cb->cb_evt=%d", __func__, p_cb->cb_evt);
  97. if (p_cb->p_callback && p_cb->cb_evt != 0) {
  98. switch (p_cb->cb_evt) {
  99. case SMP_IO_CAP_REQ_EVT:
  100. cb_data.io_req.auth_req = p_cb->peer_auth_req;
  101. cb_data.io_req.oob_data = SMP_OOB_NONE;
  102. cb_data.io_req.io_cap = btif_storage_get_local_io_caps_ble();
  103. cb_data.io_req.max_key_size = SMP_MAX_ENC_KEY_SIZE;
  104. cb_data.io_req.init_keys = p_cb->local_i_key;
  105. cb_data.io_req.resp_keys = p_cb->local_r_key;
  106. SMP_TRACE_WARNING("io_cap = %d", cb_data.io_req.io_cap);
  107. break;
  108. case SMP_NC_REQ_EVT:
  109. cb_data.passkey = p_data->passkey;
  110. break;
  111. case SMP_SC_OOB_REQ_EVT:
  112. cb_data.req_oob_type = p_data->req_oob_type;
  113. break;
  114. case SMP_SC_LOC_OOB_DATA_UP_EVT:
  115. cb_data.loc_oob_data = p_cb->sc_oob_data.loc_oob_data;
  116. break;
  117. case SMP_BR_KEYS_REQ_EVT:
  118. cb_data.io_req.auth_req = 0;
  119. cb_data.io_req.oob_data = SMP_OOB_NONE;
  120. cb_data.io_req.io_cap = 0;
  121. cb_data.io_req.max_key_size = SMP_MAX_ENC_KEY_SIZE;
  122. cb_data.io_req.init_keys = SMP_BR_SEC_DEFAULT_KEY;
  123. cb_data.io_req.resp_keys = SMP_BR_SEC_DEFAULT_KEY;
  124. break;
  125. default:
  126. break;
  127. }
  128. callback_rc =
  129. (*p_cb->p_callback)(p_cb->cb_evt, p_cb->pairing_bda, &cb_data);
  130. SMP_TRACE_DEBUG("%s: callback_rc=%d p_cb->cb_evt=%d", __func__,
  131. callback_rc, p_cb->cb_evt);
  132. if (callback_rc == SMP_SUCCESS) {
  133. switch (p_cb->cb_evt) {
  134. case SMP_IO_CAP_REQ_EVT:
  135. p_cb->loc_auth_req = cb_data.io_req.auth_req;
  136. p_cb->local_io_capability = cb_data.io_req.io_cap;
  137. p_cb->loc_oob_flag = cb_data.io_req.oob_data;
  138. p_cb->loc_enc_size = cb_data.io_req.max_key_size;
  139. p_cb->local_i_key = cb_data.io_req.init_keys;
  140. p_cb->local_r_key = cb_data.io_req.resp_keys;
  141. if (!(p_cb->loc_auth_req & SMP_AUTH_BOND)) {
  142. SMP_TRACE_WARNING("Non bonding: No keys will be exchanged");
  143. p_cb->local_i_key = 0;
  144. p_cb->local_r_key = 0;
  145. }
  146. SMP_TRACE_WARNING(
  147. "rcvd auth_req: 0x%02x, io_cap: %d "
  148. "loc_oob_flag: %d loc_enc_size: %d, "
  149. "local_i_key: 0x%02x, local_r_key: 0x%02x",
  150. p_cb->loc_auth_req, p_cb->local_io_capability, p_cb->loc_oob_flag,
  151. p_cb->loc_enc_size, p_cb->local_i_key, p_cb->local_r_key);
  152. p_cb->secure_connections_only_mode_required =
  153. (btm_cb.security_mode == BTM_SEC_MODE_SC) ? true : false;
  154. /* just for PTS, force SC bit */
  155. if (p_cb->secure_connections_only_mode_required) {
  156. p_cb->loc_auth_req |= SMP_SC_SUPPORT_BIT;
  157. }
  158. if (!p_cb->secure_connections_only_mode_required &&
  159. (!(p_cb->loc_auth_req & SMP_SC_SUPPORT_BIT) ||
  160. lmp_version_below(p_cb->pairing_bda, HCI_PROTO_VERSION_4_2) ||
  161. interop_match_addr(INTEROP_DISABLE_LE_SECURE_CONNECTIONS,
  162. (const RawAddress*)&p_cb->pairing_bda))) {
  163. p_cb->loc_auth_req &= ~SMP_SC_SUPPORT_BIT;
  164. p_cb->loc_auth_req &= ~SMP_KP_SUPPORT_BIT;
  165. p_cb->local_i_key &= ~SMP_SEC_KEY_TYPE_LK;
  166. p_cb->local_r_key &= ~SMP_SEC_KEY_TYPE_LK;
  167. }
  168. if (lmp_version_below(p_cb->pairing_bda, HCI_PROTO_VERSION_5_0)) {
  169. p_cb->loc_auth_req &= ~SMP_H7_SUPPORT_BIT;
  170. }
  171. SMP_TRACE_WARNING(
  172. "set auth_req: 0x%02x, local_i_key: 0x%02x, local_r_key: 0x%02x",
  173. p_cb->loc_auth_req, p_cb->local_i_key, p_cb->local_r_key);
  174. smp_sm_event(p_cb, SMP_IO_RSP_EVT, NULL);
  175. break;
  176. case SMP_BR_KEYS_REQ_EVT:
  177. p_cb->loc_enc_size = cb_data.io_req.max_key_size;
  178. p_cb->local_i_key = cb_data.io_req.init_keys;
  179. p_cb->local_r_key = cb_data.io_req.resp_keys;
  180. p_cb->loc_auth_req |= SMP_H7_SUPPORT_BIT;
  181. p_cb->local_i_key &= ~SMP_SEC_KEY_TYPE_LK;
  182. p_cb->local_r_key &= ~SMP_SEC_KEY_TYPE_LK;
  183. SMP_TRACE_WARNING(
  184. "for SMP over BR max_key_size: 0x%02x, local_i_key: 0x%02x, "
  185. "local_r_key: 0x%02x, p_cb->loc_auth_req: 0x%02x",
  186. p_cb->loc_enc_size, p_cb->local_i_key, p_cb->local_r_key,
  187. p_cb->loc_auth_req);
  188. smp_br_state_machine_event(p_cb, SMP_BR_KEYS_RSP_EVT, NULL);
  189. break;
  190. }
  191. }
  192. }
  193. if (!p_cb->cb_evt && p_cb->discard_sec_req) {
  194. p_cb->discard_sec_req = false;
  195. smp_sm_event(p_cb, SMP_DISCARD_SEC_REQ_EVT, NULL);
  196. }
  197. SMP_TRACE_DEBUG("%s: return", __func__);
  198. }
  199. /*******************************************************************************
  200. * Function smp_send_pair_fail
  201. * Description pairing failure to peer device if needed.
  202. ******************************************************************************/
  203. void smp_send_pair_fail(tSMP_CB* p_cb, tSMP_INT_DATA* p_data) {
  204. p_cb->status = p_data->status;
  205. p_cb->failure = p_data->status;
  206. SMP_TRACE_DEBUG("%s: status=%d failure=%d ", __func__, p_cb->status,
  207. p_cb->failure);
  208. if (p_cb->status <= SMP_MAX_FAIL_RSN_PER_SPEC &&
  209. p_cb->status != SMP_SUCCESS) {
  210. smp_send_cmd(SMP_OPCODE_PAIRING_FAILED, p_cb);
  211. p_cb->wait_for_authorization_complete = true;
  212. }
  213. }
  214. /*******************************************************************************
  215. * Function smp_send_pair_req
  216. * Description actions related to sending pairing request
  217. ******************************************************************************/
  218. void smp_send_pair_req(tSMP_CB* p_cb, tSMP_INT_DATA* p_data) {
  219. tBTM_SEC_DEV_REC* p_dev_rec = btm_find_dev(p_cb->pairing_bda);
  220. SMP_TRACE_DEBUG("%s", __func__);
  221. /* erase all keys when master sends pairing req*/
  222. if (p_dev_rec) btm_sec_clear_ble_keys(p_dev_rec);
  223. /* do not manipulate the key, let app decide,
  224. leave out to BTM to mandate key distribution for bonding case */
  225. smp_send_cmd(SMP_OPCODE_PAIRING_REQ, p_cb);
  226. }
  227. /*******************************************************************************
  228. * Function smp_send_pair_rsp
  229. * Description actions related to sending pairing response
  230. ******************************************************************************/
  231. void smp_send_pair_rsp(tSMP_CB* p_cb, tSMP_INT_DATA* p_data) {
  232. SMP_TRACE_DEBUG("%s", __func__);
  233. p_cb->local_i_key &= p_cb->peer_i_key;
  234. p_cb->local_r_key &= p_cb->peer_r_key;
  235. if (smp_send_cmd(SMP_OPCODE_PAIRING_RSP, p_cb)) {
  236. if (p_cb->selected_association_model == SMP_MODEL_SEC_CONN_OOB)
  237. smp_use_oob_private_key(p_cb, NULL);
  238. else
  239. smp_decide_association_model(p_cb, NULL);
  240. }
  241. }
  242. /*******************************************************************************
  243. * Function smp_send_confirm
  244. * Description send confirmation to the peer
  245. ******************************************************************************/
  246. void smp_send_confirm(tSMP_CB* p_cb, tSMP_INT_DATA* p_data) {
  247. SMP_TRACE_DEBUG("%s", __func__);
  248. smp_send_cmd(SMP_OPCODE_CONFIRM, p_cb);
  249. }
  250. /*******************************************************************************
  251. * Function smp_send_init
  252. * Description process pairing initializer to slave device
  253. ******************************************************************************/
  254. void smp_send_init(tSMP_CB* p_cb, tSMP_INT_DATA* p_data) {
  255. SMP_TRACE_DEBUG("%s", __func__);
  256. smp_send_cmd(SMP_OPCODE_INIT, p_cb);
  257. }
  258. /*******************************************************************************
  259. * Function smp_send_rand
  260. * Description send pairing random to the peer
  261. ******************************************************************************/
  262. void smp_send_rand(tSMP_CB* p_cb, tSMP_INT_DATA* p_data) {
  263. SMP_TRACE_DEBUG("%s", __func__);
  264. smp_send_cmd(SMP_OPCODE_RAND, p_cb);
  265. }
  266. /*******************************************************************************
  267. * Function smp_send_pair_public_key
  268. * Description send pairing public key command to the peer
  269. ******************************************************************************/
  270. void smp_send_pair_public_key(tSMP_CB* p_cb, tSMP_INT_DATA* p_data) {
  271. SMP_TRACE_DEBUG("%s", __func__);
  272. smp_send_cmd(SMP_OPCODE_PAIR_PUBLIC_KEY, p_cb);
  273. }
  274. /*******************************************************************************
  275. * Function SMP_SEND_COMMITMENT
  276. * Description send commitment command to the peer
  277. ******************************************************************************/
  278. void smp_send_commitment(tSMP_CB* p_cb, tSMP_INT_DATA* p_data) {
  279. SMP_TRACE_DEBUG("%s", __func__);
  280. smp_send_cmd(SMP_OPCODE_PAIR_COMMITM, p_cb);
  281. }
  282. /*******************************************************************************
  283. * Function smp_send_dhkey_check
  284. * Description send DHKey Check command to the peer
  285. ******************************************************************************/
  286. void smp_send_dhkey_check(tSMP_CB* p_cb, tSMP_INT_DATA* p_data) {
  287. SMP_TRACE_DEBUG("%s", __func__);
  288. smp_send_cmd(SMP_OPCODE_PAIR_DHKEY_CHECK, p_cb);
  289. }
  290. /*******************************************************************************
  291. * Function smp_send_keypress_notification
  292. * Description send Keypress Notification command to the peer
  293. ******************************************************************************/
  294. void smp_send_keypress_notification(tSMP_CB* p_cb, tSMP_INT_DATA* p_data) {
  295. p_cb->local_keypress_notification = p_data->status;
  296. smp_send_cmd(SMP_OPCODE_PAIR_KEYPR_NOTIF, p_cb);
  297. }
  298. /*******************************************************************************
  299. * Function smp_send_enc_info
  300. * Description send encryption information command.
  301. ******************************************************************************/
  302. void smp_send_enc_info(tSMP_CB* p_cb, tSMP_INT_DATA* p_data) {
  303. tBTM_LE_KEY_VALUE le_key;
  304. SMP_TRACE_DEBUG("%s: p_cb->loc_enc_size = %d", __func__, p_cb->loc_enc_size);
  305. smp_update_key_mask(p_cb, SMP_SEC_KEY_TYPE_ENC, false);
  306. smp_send_cmd(SMP_OPCODE_ENCRYPT_INFO, p_cb);
  307. smp_send_cmd(SMP_OPCODE_MASTER_ID, p_cb);
  308. /* save the DIV and key size information when acting as slave device */
  309. le_key.lenc_key.ltk = p_cb->ltk;
  310. le_key.lenc_key.div = p_cb->div;
  311. le_key.lenc_key.key_size = p_cb->loc_enc_size;
  312. le_key.lenc_key.sec_level = p_cb->sec_level;
  313. if ((p_cb->peer_auth_req & SMP_AUTH_BOND) &&
  314. (p_cb->loc_auth_req & SMP_AUTH_BOND))
  315. btm_sec_save_le_key(p_cb->pairing_bda, BTM_LE_KEY_LENC, &le_key, true);
  316. SMP_TRACE_WARNING("%s", __func__);
  317. smp_key_distribution(p_cb, NULL);
  318. }
  319. /*******************************************************************************
  320. * Function smp_send_id_info
  321. * Description send ID information command.
  322. ******************************************************************************/
  323. void smp_send_id_info(tSMP_CB* p_cb, tSMP_INT_DATA* p_data) {
  324. tBTM_LE_KEY_VALUE le_key;
  325. SMP_TRACE_DEBUG("%s", __func__);
  326. smp_update_key_mask(p_cb, SMP_SEC_KEY_TYPE_ID, false);
  327. smp_send_cmd(SMP_OPCODE_IDENTITY_INFO, p_cb);
  328. smp_send_cmd(SMP_OPCODE_ID_ADDR, p_cb);
  329. if ((p_cb->peer_auth_req & SMP_AUTH_BOND) &&
  330. (p_cb->loc_auth_req & SMP_AUTH_BOND))
  331. btm_sec_save_le_key(p_cb->pairing_bda, BTM_LE_KEY_LID, &le_key, true);
  332. SMP_TRACE_WARNING("%s", __func__);
  333. smp_key_distribution_by_transport(p_cb, NULL);
  334. }
  335. /** send CSRK command. */
  336. void smp_send_csrk_info(tSMP_CB* p_cb, tSMP_INT_DATA* p_data) {
  337. tBTM_LE_KEY_VALUE key;
  338. SMP_TRACE_DEBUG("%s", __func__);
  339. smp_update_key_mask(p_cb, SMP_SEC_KEY_TYPE_CSRK, false);
  340. if (smp_send_cmd(SMP_OPCODE_SIGN_INFO, p_cb)) {
  341. key.lcsrk_key.div = p_cb->div;
  342. key.lcsrk_key.sec_level = p_cb->sec_level;
  343. key.lcsrk_key.counter = 0; /* initialize the local counter */
  344. key.lcsrk_key.csrk = p_cb->csrk;
  345. btm_sec_save_le_key(p_cb->pairing_bda, BTM_LE_KEY_LCSRK, &key, true);
  346. }
  347. smp_key_distribution_by_transport(p_cb, NULL);
  348. }
  349. /*******************************************************************************
  350. * Function smp_send_ltk_reply
  351. * Description send LTK reply
  352. ******************************************************************************/
  353. void smp_send_ltk_reply(tSMP_CB* p_cb, tSMP_INT_DATA* p_data) {
  354. SMP_TRACE_DEBUG("%s", __func__);
  355. Octet16 stk;
  356. memcpy(stk.data(), p_data->key.p_data, stk.size());
  357. /* send stk as LTK response */
  358. btm_ble_ltk_request_reply(p_cb->pairing_bda, true, stk);
  359. }
  360. /*******************************************************************************
  361. * Function smp_proc_sec_req
  362. * Description process security request.
  363. ******************************************************************************/
  364. void smp_proc_sec_req(tSMP_CB* p_cb, tSMP_INT_DATA* p_data) {
  365. tBTM_LE_AUTH_REQ auth_req = *(tBTM_LE_AUTH_REQ*)p_data->p_data;
  366. tBTM_BLE_SEC_REQ_ACT sec_req_act;
  367. SMP_TRACE_DEBUG("%s: auth_req=0x%x", __func__, auth_req);
  368. p_cb->cb_evt = 0;
  369. btm_ble_link_sec_check(p_cb->pairing_bda, auth_req, &sec_req_act);
  370. SMP_TRACE_DEBUG("%s: sec_req_act=0x%x", __func__, sec_req_act);
  371. switch (sec_req_act) {
  372. case BTM_BLE_SEC_REQ_ACT_ENCRYPT:
  373. SMP_TRACE_DEBUG("%s: BTM_BLE_SEC_REQ_ACT_ENCRYPT", __func__);
  374. smp_sm_event(p_cb, SMP_ENC_REQ_EVT, NULL);
  375. break;
  376. case BTM_BLE_SEC_REQ_ACT_PAIR:
  377. p_cb->secure_connections_only_mode_required =
  378. (btm_cb.security_mode == BTM_SEC_MODE_SC) ? true : false;
  379. /* respond to non SC pairing request as failure in SC only mode */
  380. if (p_cb->secure_connections_only_mode_required &&
  381. (auth_req & SMP_SC_SUPPORT_BIT) == 0) {
  382. tSMP_INT_DATA smp_int_data;
  383. smp_int_data.status = SMP_PAIR_AUTH_FAIL;
  384. smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &smp_int_data);
  385. } else {
  386. /* initialize local i/r key to be default keys */
  387. p_cb->peer_auth_req = auth_req;
  388. p_cb->local_r_key = p_cb->local_i_key = SMP_SEC_DEFAULT_KEY;
  389. p_cb->cb_evt = SMP_SEC_REQUEST_EVT;
  390. }
  391. break;
  392. case BTM_BLE_SEC_REQ_ACT_DISCARD:
  393. p_cb->discard_sec_req = true;
  394. break;
  395. default:
  396. /* do nothing */
  397. break;
  398. }
  399. }
  400. /*******************************************************************************
  401. * Function smp_proc_sec_grant
  402. * Description process security grant.
  403. ******************************************************************************/
  404. void smp_proc_sec_grant(tSMP_CB* p_cb, tSMP_INT_DATA* p_data) {
  405. uint8_t res = p_data->status;
  406. SMP_TRACE_DEBUG("%s", __func__);
  407. if (res != SMP_SUCCESS) {
  408. smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, p_data);
  409. } else /*otherwise, start pairing */
  410. {
  411. /* send IO request callback */
  412. p_cb->cb_evt = SMP_IO_CAP_REQ_EVT;
  413. }
  414. }
  415. /*******************************************************************************
  416. * Function smp_proc_pair_fail
  417. * Description process pairing failure from peer device
  418. ******************************************************************************/
  419. void smp_proc_pair_fail(tSMP_CB* p_cb, tSMP_INT_DATA* p_data) {
  420. SMP_TRACE_DEBUG("%s", __func__);
  421. if (p_cb->rcvd_cmd_len < 2) {
  422. android_errorWriteLog(0x534e4554, "111214739");
  423. SMP_TRACE_WARNING("%s: rcvd_cmd_len %d too short: must be at least 2",
  424. __func__, p_cb->rcvd_cmd_len);
  425. p_cb->status = SMP_INVALID_PARAMETERS;
  426. } else {
  427. p_cb->status = p_data->status;
  428. }
  429. /* Cancel pending auth complete timer if set */
  430. alarm_cancel(p_cb->delayed_auth_timer_ent);
  431. }
  432. /*******************************************************************************
  433. * Function smp_proc_pair_cmd
  434. * Description Process the SMP pairing request/response from peer device
  435. ******************************************************************************/
  436. void smp_proc_pair_cmd(tSMP_CB* p_cb, tSMP_INT_DATA* p_data) {
  437. uint8_t* p = p_data->p_data;
  438. tBTM_SEC_DEV_REC* p_dev_rec = btm_find_dev(p_cb->pairing_bda);
  439. SMP_TRACE_DEBUG("%s: pairing_bda=%s", __func__,
  440. p_cb->pairing_bda.ToString().c_str());
  441. /* erase all keys if it is slave proc pairing req */
  442. if (p_dev_rec && (p_cb->role == HCI_ROLE_SLAVE))
  443. btm_sec_clear_ble_keys(p_dev_rec);
  444. p_cb->flags |= SMP_PAIR_FLAG_ENC_AFTER_PAIR;
  445. if (smp_command_has_invalid_length(p_cb)) {
  446. tSMP_INT_DATA smp_int_data;
  447. smp_int_data.status = SMP_INVALID_PARAMETERS;
  448. android_errorWriteLog(0x534e4554, "111850706");
  449. smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &smp_int_data);
  450. return;
  451. }
  452. STREAM_TO_UINT8(p_cb->peer_io_caps, p);
  453. STREAM_TO_UINT8(p_cb->peer_oob_flag, p);
  454. STREAM_TO_UINT8(p_cb->peer_auth_req, p);
  455. STREAM_TO_UINT8(p_cb->peer_enc_size, p);
  456. STREAM_TO_UINT8(p_cb->peer_i_key, p);
  457. STREAM_TO_UINT8(p_cb->peer_r_key, p);
  458. if (smp_command_has_invalid_parameters(p_cb)) {
  459. tSMP_INT_DATA smp_int_data;
  460. smp_int_data.status = SMP_INVALID_PARAMETERS;
  461. smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &smp_int_data);
  462. return;
  463. }
  464. // PTS Testing failure modes
  465. if (pts_test_send_authentication_complete_failure(p_cb)) return;
  466. if (p_cb->role == HCI_ROLE_SLAVE) {
  467. if (!(p_cb->flags & SMP_PAIR_FLAGS_WE_STARTED_DD)) {
  468. /* peer (master) started pairing sending Pairing Request */
  469. p_cb->local_i_key = p_cb->peer_i_key;
  470. p_cb->local_r_key = p_cb->peer_r_key;
  471. p_cb->cb_evt = SMP_SEC_REQUEST_EVT;
  472. } else /* update local i/r key according to pairing request */
  473. {
  474. /* pairing started with this side (slave) sending Security Request */
  475. p_cb->local_i_key &= p_cb->peer_i_key;
  476. p_cb->local_r_key &= p_cb->peer_r_key;
  477. p_cb->selected_association_model = smp_select_association_model(p_cb);
  478. if (p_cb->secure_connections_only_mode_required &&
  479. (!(p_cb->le_secure_connections_mode_is_used) ||
  480. (p_cb->selected_association_model ==
  481. SMP_MODEL_SEC_CONN_JUSTWORKS))) {
  482. SMP_TRACE_ERROR(
  483. "%s: pairing failed - slave requires secure connection only mode",
  484. __func__);
  485. tSMP_INT_DATA smp_int_data;
  486. smp_int_data.status = SMP_PAIR_AUTH_FAIL;
  487. smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &smp_int_data);
  488. return;
  489. }
  490. if (p_cb->selected_association_model == SMP_MODEL_SEC_CONN_OOB) {
  491. if (smp_request_oob_data(p_cb)) return;
  492. } else {
  493. smp_send_pair_rsp(p_cb, NULL);
  494. }
  495. }
  496. } else /* Master receives pairing response */
  497. {
  498. p_cb->selected_association_model = smp_select_association_model(p_cb);
  499. if (p_cb->secure_connections_only_mode_required &&
  500. (!(p_cb->le_secure_connections_mode_is_used) ||
  501. (p_cb->selected_association_model == SMP_MODEL_SEC_CONN_JUSTWORKS))) {
  502. SMP_TRACE_ERROR(
  503. "Master requires secure connection only mode "
  504. "but it can't be provided -> Master fails pairing");
  505. tSMP_INT_DATA smp_int_data;
  506. smp_int_data.status = SMP_PAIR_AUTH_FAIL;
  507. smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &smp_int_data);
  508. return;
  509. }
  510. if (p_cb->selected_association_model == SMP_MODEL_SEC_CONN_OOB) {
  511. if (smp_request_oob_data(p_cb)) return;
  512. } else {
  513. smp_decide_association_model(p_cb, NULL);
  514. }
  515. }
  516. }
  517. /** process pairing confirm from peer device */
  518. void smp_proc_confirm(tSMP_CB* p_cb, tSMP_INT_DATA* p_data) {
  519. SMP_TRACE_DEBUG("%s", __func__);
  520. if (smp_command_has_invalid_parameters(p_cb)) {
  521. tSMP_INT_DATA smp_int_data;
  522. smp_int_data.status = SMP_INVALID_PARAMETERS;
  523. smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &smp_int_data);
  524. return;
  525. }
  526. if (p_data) {
  527. uint8_t* p = p_data->p_data;
  528. if (p != NULL) {
  529. /* save the SConfirm for comparison later */
  530. STREAM_TO_ARRAY(p_cb->rconfirm.data(), p, OCTET16_LEN);
  531. }
  532. }
  533. p_cb->flags |= SMP_PAIR_FLAGS_CMD_CONFIRM;
  534. }
  535. /** process pairing initializer from peer device */
  536. void smp_proc_init(tSMP_CB* p_cb, tSMP_INT_DATA* p_data) {
  537. uint8_t* p = p_data->p_data;
  538. SMP_TRACE_DEBUG("%s", __func__);
  539. if (smp_command_has_invalid_parameters(p_cb)) {
  540. tSMP_INT_DATA smp_int_data;
  541. smp_int_data.status = SMP_INVALID_PARAMETERS;
  542. smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &smp_int_data);
  543. return;
  544. }
  545. /* save the SRand for comparison */
  546. STREAM_TO_ARRAY(p_cb->rrand.data(), p, OCTET16_LEN);
  547. }
  548. /*******************************************************************************
  549. * Function smp_proc_rand
  550. * Description process pairing random (nonce) from peer device
  551. ******************************************************************************/
  552. void smp_proc_rand(tSMP_CB* p_cb, tSMP_INT_DATA* p_data) {
  553. uint8_t* p = p_data->p_data;
  554. SMP_TRACE_DEBUG("%s", __func__);
  555. if (smp_command_has_invalid_parameters(p_cb)) {
  556. tSMP_INT_DATA smp_int_data;
  557. smp_int_data.status = SMP_INVALID_PARAMETERS;
  558. smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &smp_int_data);
  559. return;
  560. }
  561. /* save the SRand for comparison */
  562. STREAM_TO_ARRAY(p_cb->rrand.data(), p, OCTET16_LEN);
  563. }
  564. /*******************************************************************************
  565. * Function smp_process_pairing_public_key
  566. * Description process pairing public key command from the peer device
  567. * - saves the peer public key;
  568. * - sets the flag indicating that the peer public key is received;
  569. * - calls smp_wait_for_both_public_keys(...).
  570. *
  571. ******************************************************************************/
  572. void smp_process_pairing_public_key(tSMP_CB* p_cb, tSMP_INT_DATA* p_data) {
  573. uint8_t* p = p_data->p_data;
  574. SMP_TRACE_DEBUG("%s", __func__);
  575. if (smp_command_has_invalid_parameters(p_cb)) {
  576. tSMP_INT_DATA smp_int_data;
  577. smp_int_data.status = SMP_INVALID_PARAMETERS;
  578. smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &smp_int_data);
  579. return;
  580. }
  581. STREAM_TO_ARRAY(p_cb->peer_publ_key.x, p, BT_OCTET32_LEN);
  582. STREAM_TO_ARRAY(p_cb->peer_publ_key.y, p, BT_OCTET32_LEN);
  583. Point pt;
  584. memcpy(pt.x, p_cb->peer_publ_key.x, BT_OCTET32_LEN);
  585. memcpy(pt.y, p_cb->peer_publ_key.y, BT_OCTET32_LEN);
  586. if (!ECC_ValidatePoint(pt)) {
  587. android_errorWriteLog(0x534e4554, "72377774");
  588. tSMP_INT_DATA smp;
  589. smp.status = SMP_PAIR_AUTH_FAIL;
  590. smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &smp);
  591. return;
  592. }
  593. p_cb->flags |= SMP_PAIR_FLAG_HAVE_PEER_PUBL_KEY;
  594. smp_wait_for_both_public_keys(p_cb, NULL);
  595. }
  596. /*******************************************************************************
  597. * Function smp_process_pairing_commitment
  598. * Description process pairing commitment from peer device
  599. ******************************************************************************/
  600. void smp_process_pairing_commitment(tSMP_CB* p_cb, tSMP_INT_DATA* p_data) {
  601. uint8_t* p = p_data->p_data;
  602. SMP_TRACE_DEBUG("%s", __func__);
  603. if (smp_command_has_invalid_parameters(p_cb)) {
  604. tSMP_INT_DATA smp_int_data;
  605. smp_int_data.status = SMP_INVALID_PARAMETERS;
  606. smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &smp_int_data);
  607. return;
  608. }
  609. p_cb->flags |= SMP_PAIR_FLAG_HAVE_PEER_COMM;
  610. if (p != NULL) {
  611. STREAM_TO_ARRAY(p_cb->remote_commitment.data(), p, OCTET16_LEN);
  612. }
  613. }
  614. /*******************************************************************************
  615. * Function smp_process_dhkey_check
  616. * Description process DHKey Check from peer device
  617. ******************************************************************************/
  618. void smp_process_dhkey_check(tSMP_CB* p_cb, tSMP_INT_DATA* p_data) {
  619. uint8_t* p = p_data->p_data;
  620. SMP_TRACE_DEBUG("%s", __func__);
  621. if (smp_command_has_invalid_parameters(p_cb)) {
  622. tSMP_INT_DATA smp_int_data;
  623. smp_int_data.status = SMP_INVALID_PARAMETERS;
  624. smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &smp_int_data);
  625. return;
  626. }
  627. if (p != NULL) {
  628. STREAM_TO_ARRAY(p_cb->remote_dhkey_check.data(), p, OCTET16_LEN);
  629. }
  630. p_cb->flags |= SMP_PAIR_FLAG_HAVE_PEER_DHK_CHK;
  631. }
  632. /*******************************************************************************
  633. * Function smp_process_keypress_notification
  634. * Description process pairing keypress notification from peer device
  635. ******************************************************************************/
  636. void smp_process_keypress_notification(tSMP_CB* p_cb, tSMP_INT_DATA* p_data) {
  637. uint8_t* p = p_data->p_data;
  638. SMP_TRACE_DEBUG("%s", __func__);
  639. p_cb->status = p_data->status;
  640. if (smp_command_has_invalid_parameters(p_cb)) {
  641. tSMP_INT_DATA smp_int_data;
  642. smp_int_data.status = SMP_INVALID_PARAMETERS;
  643. smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &smp_int_data);
  644. return;
  645. }
  646. if (p != NULL) {
  647. STREAM_TO_UINT8(p_cb->peer_keypress_notification, p);
  648. } else {
  649. p_cb->peer_keypress_notification = BTM_SP_KEY_OUT_OF_RANGE;
  650. }
  651. p_cb->cb_evt = SMP_PEER_KEYPR_NOT_EVT;
  652. }
  653. /*******************************************************************************
  654. * Function smp_br_process_pairing_command
  655. * Description Process the SMP pairing request/response from peer device via
  656. * BR/EDR transport.
  657. ******************************************************************************/
  658. void smp_br_process_pairing_command(tSMP_CB* p_cb, tSMP_INT_DATA* p_data) {
  659. uint8_t* p = p_data->p_data;
  660. tBTM_SEC_DEV_REC* p_dev_rec = btm_find_dev(p_cb->pairing_bda);
  661. SMP_TRACE_DEBUG("%s", __func__);
  662. /* rejecting BR pairing request over non-SC BR link */
  663. if (!p_dev_rec->new_encryption_key_is_p256 && p_cb->role == HCI_ROLE_SLAVE) {
  664. tSMP_INT_DATA smp_int_data;
  665. smp_int_data.status = SMP_XTRANS_DERIVE_NOT_ALLOW;
  666. smp_br_state_machine_event(p_cb, SMP_BR_AUTH_CMPL_EVT, &smp_int_data);
  667. return;
  668. }
  669. /* erase all keys if it is slave proc pairing req*/
  670. if (p_dev_rec && (p_cb->role == HCI_ROLE_SLAVE))
  671. btm_sec_clear_ble_keys(p_dev_rec);
  672. p_cb->flags |= SMP_PAIR_FLAG_ENC_AFTER_PAIR;
  673. if (smp_command_has_invalid_length(p_cb)) {
  674. tSMP_INT_DATA smp_int_data;
  675. smp_int_data.status = SMP_INVALID_PARAMETERS;
  676. android_errorWriteLog(0x534e4554, "111213909");
  677. smp_br_state_machine_event(p_cb, SMP_BR_AUTH_CMPL_EVT, &smp_int_data);
  678. return;
  679. }
  680. STREAM_TO_UINT8(p_cb->peer_io_caps, p);
  681. STREAM_TO_UINT8(p_cb->peer_oob_flag, p);
  682. STREAM_TO_UINT8(p_cb->peer_auth_req, p);
  683. STREAM_TO_UINT8(p_cb->peer_enc_size, p);
  684. STREAM_TO_UINT8(p_cb->peer_i_key, p);
  685. STREAM_TO_UINT8(p_cb->peer_r_key, p);
  686. if (smp_command_has_invalid_parameters(p_cb)) {
  687. tSMP_INT_DATA smp_int_data;
  688. smp_int_data.status = SMP_INVALID_PARAMETERS;
  689. smp_br_state_machine_event(p_cb, SMP_BR_AUTH_CMPL_EVT, &smp_int_data);
  690. return;
  691. }
  692. /* peer (master) started pairing sending Pairing Request */
  693. /* or being master device always use received i/r key as keys to distribute */
  694. p_cb->local_i_key = p_cb->peer_i_key;
  695. p_cb->local_r_key = p_cb->peer_r_key;
  696. if (p_cb->role == HCI_ROLE_SLAVE) {
  697. p_dev_rec->new_encryption_key_is_p256 = false;
  698. /* shortcut to skip Security Grant step */
  699. p_cb->cb_evt = SMP_BR_KEYS_REQ_EVT;
  700. } else {
  701. /* Master receives pairing response */
  702. SMP_TRACE_DEBUG(
  703. "%s master rcvs valid PAIRING RESPONSE."
  704. " Supposed to move to key distribution phase. ",
  705. __func__);
  706. }
  707. /* auth_req received via BR/EDR SM channel is set to 0,
  708. but everything derived/exchanged has to be saved */
  709. p_cb->peer_auth_req |= SMP_AUTH_BOND;
  710. p_cb->loc_auth_req |= SMP_AUTH_BOND;
  711. }
  712. /*******************************************************************************
  713. * Function smp_br_process_security_grant
  714. * Description process security grant in case of pairing over BR/EDR transport.
  715. ******************************************************************************/
  716. void smp_br_process_security_grant(tSMP_CB* p_cb, tSMP_INT_DATA* p_data) {
  717. SMP_TRACE_DEBUG("%s", __func__);
  718. if (p_data->status != SMP_SUCCESS) {
  719. smp_br_state_machine_event(p_cb, SMP_BR_AUTH_CMPL_EVT, p_data);
  720. } else {
  721. /* otherwise, start pairing; send IO request callback */
  722. p_cb->cb_evt = SMP_BR_KEYS_REQ_EVT;
  723. }
  724. }
  725. /*******************************************************************************
  726. * Function smp_br_check_authorization_request
  727. * Description sets the SMP kes to be derived/distribute over BR/EDR transport
  728. * before starting the distribution/derivation
  729. ******************************************************************************/
  730. void smp_br_check_authorization_request(tSMP_CB* p_cb, tSMP_INT_DATA* p_data) {
  731. SMP_TRACE_DEBUG("%s rcvs i_keys=0x%x r_keys=0x%x (i-initiator r-responder)",
  732. __func__, p_cb->local_i_key, p_cb->local_r_key);
  733. /* In LE SC mode LK field is ignored when BR/EDR transport is used */
  734. p_cb->local_i_key &= ~SMP_SEC_KEY_TYPE_LK;
  735. p_cb->local_r_key &= ~SMP_SEC_KEY_TYPE_LK;
  736. /* In LE SC mode only IRK, IAI, CSRK are exchanged with the peer.
  737. ** Set local_r_key on master to expect only these keys. */
  738. if (p_cb->role == HCI_ROLE_MASTER) {
  739. p_cb->local_r_key &= (SMP_SEC_KEY_TYPE_ID | SMP_SEC_KEY_TYPE_CSRK);
  740. }
  741. /* Check if H7 function needs to be used for key derivation*/
  742. if ((p_cb->loc_auth_req & SMP_H7_SUPPORT_BIT) &&
  743. (p_cb->peer_auth_req & SMP_H7_SUPPORT_BIT)) {
  744. p_cb->key_derivation_h7_used = TRUE;
  745. }
  746. SMP_TRACE_DEBUG("%s: use h7 = %d", __func__, p_cb->key_derivation_h7_used);
  747. SMP_TRACE_DEBUG(
  748. "%s rcvs upgrades: i_keys=0x%x r_keys=0x%x (i-initiator r-responder)",
  749. __func__, p_cb->local_i_key, p_cb->local_r_key);
  750. if (/*((p_cb->peer_auth_req & SMP_AUTH_BOND) ||
  751. (p_cb->loc_auth_req & SMP_AUTH_BOND)) &&*/
  752. (p_cb->local_i_key || p_cb->local_r_key)) {
  753. smp_br_state_machine_event(p_cb, SMP_BR_BOND_REQ_EVT, NULL);
  754. /* if no peer key is expected, start master key distribution */
  755. if (p_cb->role == HCI_ROLE_MASTER && p_cb->local_r_key == 0)
  756. smp_key_distribution_by_transport(p_cb, NULL);
  757. } else {
  758. tSMP_INT_DATA smp_int_data;
  759. smp_int_data.status = SMP_SUCCESS;
  760. smp_br_state_machine_event(p_cb, SMP_BR_AUTH_CMPL_EVT, &smp_int_data);
  761. }
  762. }
  763. /*******************************************************************************
  764. * Function smp_br_select_next_key
  765. * Description selects the next key to derive/send when BR/EDR transport is
  766. * used.
  767. ******************************************************************************/
  768. void smp_br_select_next_key(tSMP_CB* p_cb, tSMP_INT_DATA* p_data) {
  769. SMP_TRACE_DEBUG("%s role=%d (0-master) r_keys=0x%x i_keys=0x%x", __func__,
  770. p_cb->role, p_cb->local_r_key, p_cb->local_i_key);
  771. if (p_cb->role == HCI_ROLE_SLAVE ||
  772. (!p_cb->local_r_key && p_cb->role == HCI_ROLE_MASTER)) {
  773. smp_key_pick_key(p_cb, p_data);
  774. }
  775. if (!p_cb->local_i_key && !p_cb->local_r_key) {
  776. /* state check to prevent re-entrance */
  777. if (smp_get_br_state() == SMP_BR_STATE_BOND_PENDING) {
  778. if (p_cb->total_tx_unacked == 0) {
  779. tSMP_INT_DATA smp_int_data;
  780. smp_int_data.status = SMP_SUCCESS;
  781. smp_br_state_machine_event(p_cb, SMP_BR_AUTH_CMPL_EVT, &smp_int_data);
  782. } else {
  783. p_cb->wait_for_authorization_complete = true;
  784. }
  785. }
  786. }
  787. }
  788. /** process encryption information from peer device */
  789. void smp_proc_enc_info(tSMP_CB* p_cb, tSMP_INT_DATA* p_data) {
  790. uint8_t* p = p_data->p_data;
  791. SMP_TRACE_DEBUG("%s", __func__);
  792. if (smp_command_has_invalid_parameters(p_cb)) {
  793. tSMP_INT_DATA smp_int_data;
  794. smp_int_data.status = SMP_INVALID_PARAMETERS;
  795. android_errorWriteLog(0x534e4554, "111937065");
  796. smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &smp_int_data);
  797. return;
  798. }
  799. STREAM_TO_ARRAY(p_cb->ltk.data(), p, OCTET16_LEN);
  800. smp_key_distribution(p_cb, NULL);
  801. }
  802. /** process master ID from slave device */
  803. void smp_proc_master_id(tSMP_CB* p_cb, tSMP_INT_DATA* p_data) {
  804. uint8_t* p = p_data->p_data;
  805. tBTM_LE_KEY_VALUE le_key;
  806. SMP_TRACE_DEBUG("%s", __func__);
  807. if (p_cb->rcvd_cmd_len < 11) { // 1(Code) + 2(EDIV) + 8(Rand)
  808. android_errorWriteLog(0x534e4554, "111937027");
  809. SMP_TRACE_ERROR("%s: Invalid command length: %d, should be at least 11",
  810. __func__, p_cb->rcvd_cmd_len);
  811. return;
  812. }
  813. smp_update_key_mask(p_cb, SMP_SEC_KEY_TYPE_ENC, true);
  814. STREAM_TO_UINT16(le_key.penc_key.ediv, p);
  815. STREAM_TO_ARRAY(le_key.penc_key.rand, p, BT_OCTET8_LEN);
  816. /* store the encryption keys from peer device */
  817. le_key.penc_key.ltk = p_cb->ltk;
  818. le_key.penc_key.sec_level = p_cb->sec_level;
  819. le_key.penc_key.key_size = p_cb->loc_enc_size;
  820. if ((p_cb->peer_auth_req & SMP_AUTH_BOND) &&
  821. (p_cb->loc_auth_req & SMP_AUTH_BOND))
  822. btm_sec_save_le_key(p_cb->pairing_bda, BTM_LE_KEY_PENC, &le_key, true);
  823. smp_key_distribution(p_cb, NULL);
  824. }
  825. /** process identity information from peer device */
  826. void smp_proc_id_info(tSMP_CB* p_cb, tSMP_INT_DATA* p_data) {
  827. uint8_t* p = p_data->p_data;
  828. SMP_TRACE_DEBUG("%s", __func__);
  829. if (smp_command_has_invalid_parameters(p_cb)) {
  830. tSMP_INT_DATA smp_int_data;
  831. smp_int_data.status = SMP_INVALID_PARAMETERS;
  832. android_errorWriteLog(0x534e4554, "111937065");
  833. smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &smp_int_data);
  834. return;
  835. }
  836. STREAM_TO_ARRAY(p_cb->tk.data(), p, OCTET16_LEN); /* reuse TK for IRK */
  837. smp_key_distribution_by_transport(p_cb, NULL);
  838. }
  839. /** process identity address from peer device */
  840. void smp_proc_id_addr(tSMP_CB* p_cb, tSMP_INT_DATA* p_data) {
  841. uint8_t* p = p_data->p_data;
  842. tBTM_LE_KEY_VALUE pid_key;
  843. SMP_TRACE_DEBUG("%s", __func__);
  844. if (smp_command_has_invalid_parameters(p_cb)) {
  845. tSMP_INT_DATA smp_int_data;
  846. smp_int_data.status = SMP_INVALID_PARAMETERS;
  847. android_errorWriteLog(0x534e4554, "111214770");
  848. smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &smp_int_data);
  849. return;
  850. }
  851. smp_update_key_mask(p_cb, SMP_SEC_KEY_TYPE_ID, true);
  852. STREAM_TO_UINT8(pid_key.pid_key.identity_addr_type, p);
  853. STREAM_TO_BDADDR(pid_key.pid_key.identity_addr, p);
  854. pid_key.pid_key.irk = p_cb->tk;
  855. /* to use as BD_ADDR for lk derived from ltk */
  856. p_cb->id_addr_rcvd = true;
  857. p_cb->id_addr_type = pid_key.pid_key.identity_addr_type;
  858. p_cb->id_addr = pid_key.pid_key.identity_addr;
  859. /* store the ID key from peer device */
  860. if ((p_cb->peer_auth_req & SMP_AUTH_BOND) &&
  861. (p_cb->loc_auth_req & SMP_AUTH_BOND))
  862. btm_sec_save_le_key(p_cb->pairing_bda, BTM_LE_KEY_PID, &pid_key, true);
  863. smp_key_distribution_by_transport(p_cb, NULL);
  864. }
  865. /* process security information from peer device */
  866. void smp_proc_srk_info(tSMP_CB* p_cb, tSMP_INT_DATA* p_data) {
  867. tBTM_LE_KEY_VALUE le_key;
  868. SMP_TRACE_DEBUG("%s", __func__);
  869. if (smp_command_has_invalid_parameters(p_cb)) {
  870. tSMP_INT_DATA smp_int_data;
  871. smp_int_data.status = SMP_INVALID_PARAMETERS;
  872. android_errorWriteLog(0x534e4554, "111214470");
  873. smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &smp_int_data);
  874. return;
  875. }
  876. smp_update_key_mask(p_cb, SMP_SEC_KEY_TYPE_CSRK, true);
  877. /* save CSRK to security record */
  878. le_key.pcsrk_key.sec_level = p_cb->sec_level;
  879. /* get peer CSRK */
  880. maybe_non_aligned_memcpy(le_key.pcsrk_key.csrk.data(), p_data->p_data,
  881. OCTET16_LEN);
  882. /* initialize the peer counter */
  883. le_key.pcsrk_key.counter = 0;
  884. if ((p_cb->peer_auth_req & SMP_AUTH_BOND) &&
  885. (p_cb->loc_auth_req & SMP_AUTH_BOND))
  886. btm_sec_save_le_key(p_cb->pairing_bda, BTM_LE_KEY_PCSRK, &le_key, true);
  887. smp_key_distribution_by_transport(p_cb, NULL);
  888. }
  889. /*******************************************************************************
  890. * Function smp_proc_compare
  891. * Description process compare value
  892. ******************************************************************************/
  893. void smp_proc_compare(tSMP_CB* p_cb, tSMP_INT_DATA* p_data) {
  894. SMP_TRACE_DEBUG("%s", __func__);
  895. if (!memcmp(p_cb->rconfirm.data(), p_data->key.p_data, OCTET16_LEN)) {
  896. /* compare the max encryption key size, and save the smaller one for the
  897. * link */
  898. if (p_cb->peer_enc_size < p_cb->loc_enc_size)
  899. p_cb->loc_enc_size = p_cb->peer_enc_size;
  900. if (p_cb->role == HCI_ROLE_SLAVE)
  901. smp_sm_event(p_cb, SMP_RAND_EVT, NULL);
  902. else {
  903. /* master device always use received i/r key as keys to distribute */
  904. p_cb->local_i_key = p_cb->peer_i_key;
  905. p_cb->local_r_key = p_cb->peer_r_key;
  906. smp_sm_event(p_cb, SMP_ENC_REQ_EVT, NULL);
  907. }
  908. } else {
  909. tSMP_INT_DATA smp_int_data;
  910. smp_int_data.status = SMP_CONFIRM_VALUE_ERR;
  911. p_cb->failure = SMP_CONFIRM_VALUE_ERR;
  912. smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &smp_int_data);
  913. }
  914. }
  915. /*******************************************************************************
  916. * Function smp_proc_sl_key
  917. * Description process key ready events.
  918. ******************************************************************************/
  919. void smp_proc_sl_key(tSMP_CB* p_cb, tSMP_INT_DATA* p_data) {
  920. uint8_t key_type = p_data->key.key_type;
  921. SMP_TRACE_DEBUG("%s", __func__);
  922. if (key_type == SMP_KEY_TYPE_TK) {
  923. smp_generate_srand_mrand_confirm(p_cb, NULL);
  924. } else if (key_type == SMP_KEY_TYPE_CFM) {
  925. smp_set_state(SMP_STATE_WAIT_CONFIRM);
  926. if (p_cb->flags & SMP_PAIR_FLAGS_CMD_CONFIRM)
  927. smp_sm_event(p_cb, SMP_CONFIRM_EVT, NULL);
  928. }
  929. }
  930. /*******************************************************************************
  931. * Function smp_start_enc
  932. * Description start encryption
  933. ******************************************************************************/
  934. void smp_start_enc(tSMP_CB* p_cb, tSMP_INT_DATA* p_data) {
  935. tBTM_STATUS cmd;
  936. SMP_TRACE_DEBUG("%s", __func__);
  937. if (p_data != NULL) {
  938. cmd = btm_ble_start_encrypt(p_cb->pairing_bda, true,
  939. (Octet16*)p_data->key.p_data);
  940. } else {
  941. cmd = btm_ble_start_encrypt(p_cb->pairing_bda, false, NULL);
  942. }
  943. if (cmd != BTM_CMD_STARTED && cmd != BTM_BUSY) {
  944. tSMP_INT_DATA smp_int_data;
  945. smp_int_data.status = SMP_ENC_FAIL;
  946. smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &smp_int_data);
  947. }
  948. }
  949. /*******************************************************************************
  950. * Function smp_proc_discard
  951. * Description processing for discard security request
  952. ******************************************************************************/
  953. void smp_proc_discard(tSMP_CB* p_cb, tSMP_INT_DATA* p_data) {
  954. SMP_TRACE_DEBUG("%s", __func__);
  955. if (!(p_cb->flags & SMP_PAIR_FLAGS_WE_STARTED_DD))
  956. smp_reset_control_value(p_cb);
  957. }
  958. /*******************************************************************************
  959. * Function smp_enc_cmpl
  960. * Description encryption success
  961. ******************************************************************************/
  962. void smp_enc_cmpl(tSMP_CB* p_cb, tSMP_INT_DATA* p_data) {
  963. uint8_t enc_enable = p_data->status;
  964. SMP_TRACE_DEBUG("%s", __func__);
  965. tSMP_INT_DATA smp_int_data;
  966. smp_int_data.status = enc_enable ? SMP_SUCCESS : SMP_ENC_FAIL;
  967. smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &smp_int_data);
  968. }
  969. /*******************************************************************************
  970. * Function smp_check_auth_req
  971. * Description check authentication request
  972. ******************************************************************************/
  973. void smp_check_auth_req(tSMP_CB* p_cb, tSMP_INT_DATA* p_data) {
  974. uint8_t enc_enable = p_data->status;
  975. SMP_TRACE_DEBUG(
  976. "%s rcvs enc_enable=%d i_keys=0x%x r_keys=0x%x (i-initiator r-responder)",
  977. __func__, enc_enable, p_cb->local_i_key, p_cb->local_r_key);
  978. if (enc_enable == 1) {
  979. if (p_cb->le_secure_connections_mode_is_used) {
  980. /* In LE SC mode LTK is used instead of STK and has to be always saved */
  981. p_cb->local_i_key |= SMP_SEC_KEY_TYPE_ENC;
  982. p_cb->local_r_key |= SMP_SEC_KEY_TYPE_ENC;
  983. /* In LE SC mode LK is derived from LTK only if both sides request it */
  984. if (!(p_cb->local_i_key & SMP_SEC_KEY_TYPE_LK) ||
  985. !(p_cb->local_r_key & SMP_SEC_KEY_TYPE_LK)) {
  986. p_cb->local_i_key &= ~SMP_SEC_KEY_TYPE_LK;
  987. p_cb->local_r_key &= ~SMP_SEC_KEY_TYPE_LK;
  988. }
  989. /* In LE SC mode only IRK, IAI, CSRK are exchanged with the peer.
  990. ** Set local_r_key on master to expect only these keys.
  991. */
  992. if (p_cb->role == HCI_ROLE_MASTER) {
  993. p_cb->local_r_key &= (SMP_SEC_KEY_TYPE_ID | SMP_SEC_KEY_TYPE_CSRK);
  994. }
  995. } else {
  996. /* in legacy mode derivation of BR/EDR LK is not supported */
  997. p_cb->local_i_key &= ~SMP_SEC_KEY_TYPE_LK;
  998. p_cb->local_r_key &= ~SMP_SEC_KEY_TYPE_LK;
  999. }
  1000. SMP_TRACE_DEBUG(
  1001. "%s rcvs upgrades: i_keys=0x%x r_keys=0x%x (i-initiator r-responder)",
  1002. __func__, p_cb->local_i_key, p_cb->local_r_key);
  1003. if (/*((p_cb->peer_auth_req & SMP_AUTH_BOND) ||
  1004. (p_cb->loc_auth_req & SMP_AUTH_BOND)) &&*/
  1005. (p_cb->local_i_key || p_cb->local_r_key)) {
  1006. smp_sm_event(p_cb, SMP_BOND_REQ_EVT, NULL);
  1007. } else {
  1008. tSMP_INT_DATA smp_int_data;
  1009. smp_int_data.status = enc_enable ? SMP_SUCCESS : SMP_ENC_FAIL;
  1010. smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &smp_int_data);
  1011. }
  1012. } else if (enc_enable == 0) {
  1013. tSMP_INT_DATA smp_int_data;
  1014. smp_int_data.status = enc_enable ? SMP_SUCCESS : SMP_ENC_FAIL;
  1015. /* if failed for encryption after pairing, send callback */
  1016. if (p_cb->flags & SMP_PAIR_FLAG_ENC_AFTER_PAIR)
  1017. smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &smp_int_data);
  1018. /* if enc failed for old security information */
  1019. /* if master device, clean up and abck to idle; slave device do nothing */
  1020. else if (p_cb->role == HCI_ROLE_MASTER) {
  1021. smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &smp_int_data);
  1022. }
  1023. }
  1024. }
  1025. /*******************************************************************************
  1026. * Function smp_key_pick_key
  1027. * Description Pick a key distribution function based on the key mask.
  1028. ******************************************************************************/
  1029. void smp_key_pick_key(tSMP_CB* p_cb, tSMP_INT_DATA* p_data) {
  1030. uint8_t key_to_dist =
  1031. (p_cb->role == HCI_ROLE_SLAVE) ? p_cb->local_r_key : p_cb->local_i_key;
  1032. uint8_t i = 0;
  1033. SMP_TRACE_DEBUG("%s key_to_dist=0x%x", __func__, key_to_dist);
  1034. while (i < SMP_KEY_DIST_TYPE_MAX) {
  1035. SMP_TRACE_DEBUG("key to send = %02x, i = %d", key_to_dist, i);
  1036. if (key_to_dist & (1 << i)) {
  1037. SMP_TRACE_DEBUG("smp_distribute_act[%d]", i);
  1038. (*smp_distribute_act[i])(p_cb, p_data);
  1039. break;
  1040. }
  1041. i++;
  1042. }
  1043. }
  1044. /*******************************************************************************
  1045. * Function smp_key_distribution
  1046. * Description start key distribution if required.
  1047. ******************************************************************************/
  1048. void smp_key_distribution(tSMP_CB* p_cb, tSMP_INT_DATA* p_data) {
  1049. SMP_TRACE_DEBUG("%s role=%d (0-master) r_keys=0x%x i_keys=0x%x", __func__,
  1050. p_cb->role, p_cb->local_r_key, p_cb->local_i_key);
  1051. if (p_cb->role == HCI_ROLE_SLAVE ||
  1052. (!p_cb->local_r_key && p_cb->role == HCI_ROLE_MASTER)) {
  1053. smp_key_pick_key(p_cb, p_data);
  1054. }
  1055. if (!p_cb->local_i_key && !p_cb->local_r_key) {
  1056. /* state check to prevent re-entrant */
  1057. if (smp_get_state() == SMP_STATE_BOND_PENDING) {
  1058. if (p_cb->derive_lk) {
  1059. smp_derive_link_key_from_long_term_key(p_cb, NULL);
  1060. p_cb->derive_lk = false;
  1061. }
  1062. if (p_cb->total_tx_unacked == 0) {
  1063. /*
  1064. * Instead of declaring authorization complete immediately,
  1065. * delay the event from being sent by SMP_DELAYED_AUTH_TIMEOUT_MS.
  1066. * This allows the slave to send over Pairing Failed if the
  1067. * last key is rejected. During this waiting window, the
  1068. * state should remain in SMP_STATE_BOND_PENDING.
  1069. */
  1070. if (!alarm_is_scheduled(p_cb->delayed_auth_timer_ent)) {
  1071. SMP_TRACE_DEBUG("%s delaying auth complete.", __func__);
  1072. alarm_set_on_mloop(p_cb->delayed_auth_timer_ent,
  1073. SMP_DELAYED_AUTH_TIMEOUT_MS,
  1074. smp_delayed_auth_complete_timeout, NULL);
  1075. }
  1076. } else {
  1077. p_cb->wait_for_authorization_complete = true;
  1078. }
  1079. }
  1080. }
  1081. }
  1082. /*******************************************************************************
  1083. * Function smp_decide_association_model
  1084. * Description This function is called to select assoc model to be used for
  1085. * STK generation and to start STK generation process.
  1086. *
  1087. ******************************************************************************/
  1088. void smp_decide_association_model(tSMP_CB* p_cb, tSMP_INT_DATA* p_data) {
  1089. uint8_t int_evt = 0;
  1090. tSMP_INT_DATA smp_int_data;
  1091. SMP_TRACE_DEBUG("%s Association Model = %d", __func__,
  1092. p_cb->selected_association_model);
  1093. switch (p_cb->selected_association_model) {
  1094. case SMP_MODEL_ENCRYPTION_ONLY: /* TK = 0, go calculate Confirm */
  1095. if (p_cb->role == HCI_ROLE_MASTER &&
  1096. ((p_cb->peer_auth_req & SMP_AUTH_YN_BIT) != 0) &&
  1097. ((p_cb->loc_auth_req & SMP_AUTH_YN_BIT) == 0)) {
  1098. SMP_TRACE_ERROR(
  1099. "IO capability does not meet authentication requirement");
  1100. smp_int_data.status = SMP_PAIR_AUTH_FAIL;
  1101. int_evt = SMP_AUTH_CMPL_EVT;
  1102. } else {
  1103. p_cb->sec_level = SMP_SEC_UNAUTHENTICATE;
  1104. SMP_TRACE_EVENT("p_cb->sec_level =%d (SMP_SEC_UNAUTHENTICATE) ",
  1105. p_cb->sec_level);
  1106. tSMP_KEY key;
  1107. key.key_type = SMP_KEY_TYPE_TK;
  1108. key.p_data = p_cb->tk.data();
  1109. smp_int_data.key = key;
  1110. p_cb->tk = {0};
  1111. /* TK, ready */
  1112. int_evt = SMP_KEY_READY_EVT;
  1113. }
  1114. break;
  1115. case SMP_MODEL_PASSKEY:
  1116. p_cb->sec_level = SMP_SEC_AUTHENTICATED;
  1117. SMP_TRACE_EVENT("p_cb->sec_level =%d (SMP_SEC_AUTHENTICATED) ",
  1118. p_cb->sec_level);
  1119. p_cb->cb_evt = SMP_PASSKEY_REQ_EVT;
  1120. int_evt = SMP_TK_REQ_EVT;
  1121. break;
  1122. case SMP_MODEL_OOB:
  1123. SMP_TRACE_ERROR("Association Model = SMP_MODEL_OOB");
  1124. p_cb->sec_level = SMP_SEC_AUTHENTICATED;
  1125. SMP_TRACE_EVENT("p_cb->sec_level =%d (SMP_SEC_AUTHENTICATED) ",
  1126. p_cb->sec_level);
  1127. p_cb->cb_evt = SMP_OOB_REQ_EVT;
  1128. int_evt = SMP_TK_REQ_EVT;
  1129. break;
  1130. case SMP_MODEL_KEY_NOTIF:
  1131. p_cb->sec_level = SMP_SEC_AUTHENTICATED;
  1132. SMP_TRACE_DEBUG("Need to generate Passkey");
  1133. /* generate passkey and notify application */
  1134. smp_generate_passkey(p_cb, NULL);
  1135. break;
  1136. case SMP_MODEL_SEC_CONN_JUSTWORKS:
  1137. case SMP_MODEL_SEC_CONN_NUM_COMP:
  1138. case SMP_MODEL_SEC_CONN_PASSKEY_ENT:
  1139. case SMP_MODEL_SEC_CONN_PASSKEY_DISP:
  1140. case SMP_MODEL_SEC_CONN_OOB:
  1141. int_evt = SMP_PUBL_KEY_EXCH_REQ_EVT;
  1142. break;
  1143. case SMP_MODEL_OUT_OF_RANGE:
  1144. SMP_TRACE_ERROR("Association Model = SMP_MODEL_OUT_OF_RANGE (failed)");
  1145. smp_int_data.status = SMP_UNKNOWN_IO_CAP;
  1146. int_evt = SMP_AUTH_CMPL_EVT;
  1147. break;
  1148. default:
  1149. SMP_TRACE_ERROR(
  1150. "Association Model = %d (SOMETHING IS WRONG WITH THE CODE)",
  1151. p_cb->selected_association_model);
  1152. smp_int_data.status = SMP_UNKNOWN_IO_CAP;
  1153. int_evt = SMP_AUTH_CMPL_EVT;
  1154. }
  1155. SMP_TRACE_EVENT("sec_level=%d ", p_cb->sec_level);
  1156. if (int_evt) smp_sm_event(p_cb, int_evt, &smp_int_data);
  1157. }
  1158. /*******************************************************************************
  1159. * Function smp_process_io_response
  1160. * Description process IO response for a slave device.
  1161. ******************************************************************************/
  1162. void smp_process_io_response(tSMP_CB* p_cb, tSMP_INT_DATA* p_data) {
  1163. SMP_TRACE_DEBUG("%s", __func__);
  1164. if (p_cb->flags & SMP_PAIR_FLAGS_WE_STARTED_DD) {
  1165. /* pairing started by local (slave) Security Request */
  1166. smp_set_state(SMP_STATE_SEC_REQ_PENDING);
  1167. smp_send_cmd(SMP_OPCODE_SEC_REQ, p_cb);
  1168. } else /* plan to send pairing respond */
  1169. {
  1170. /* pairing started by peer (master) Pairing Request */
  1171. p_cb->selected_association_model = smp_select_association_model(p_cb);
  1172. if (p_cb->secure_connections_only_mode_required &&
  1173. (!(p_cb->le_secure_connections_mode_is_used) ||
  1174. (p_cb->selected_association_model == SMP_MODEL_SEC_CONN_JUSTWORKS))) {
  1175. SMP_TRACE_ERROR(
  1176. "Slave requires secure connection only mode "
  1177. "but it can't be provided -> Slave fails pairing");
  1178. tSMP_INT_DATA smp_int_data;
  1179. smp_int_data.status = SMP_PAIR_AUTH_FAIL;
  1180. smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &smp_int_data);
  1181. return;
  1182. }
  1183. if (p_cb->selected_association_model == SMP_MODEL_SEC_CONN_OOB) {
  1184. if (smp_request_oob_data(p_cb)) return;
  1185. }
  1186. // PTS Testing failure modes
  1187. if (pts_test_send_authentication_complete_failure(p_cb)) return;
  1188. smp_send_pair_rsp(p_cb, NULL);
  1189. }
  1190. }
  1191. /*******************************************************************************
  1192. * Function smp_br_process_slave_keys_response
  1193. * Description process application keys response for a slave device
  1194. * (BR/EDR transport).
  1195. ******************************************************************************/
  1196. void smp_br_process_slave_keys_response(tSMP_CB* p_cb, tSMP_INT_DATA* p_data) {
  1197. smp_br_send_pair_response(p_cb, NULL);
  1198. }
  1199. /*******************************************************************************
  1200. * Function smp_br_send_pair_response
  1201. * Description actions related to sending pairing response over BR/EDR
  1202. * transport.
  1203. ******************************************************************************/
  1204. void smp_br_send_pair_response(tSMP_CB* p_cb, tSMP_INT_DATA* p_data) {
  1205. SMP_TRACE_DEBUG("%s", __func__);
  1206. p_cb->local_i_key &= p_cb->peer_i_key;
  1207. p_cb->local_r_key &= p_cb->peer_r_key;
  1208. smp_send_cmd(SMP_OPCODE_PAIRING_RSP, p_cb);
  1209. }
  1210. /*******************************************************************************
  1211. * Function smp_pairing_cmpl
  1212. * Description This function is called to send the pairing complete
  1213. * callback and remove the connection if needed.
  1214. ******************************************************************************/
  1215. void smp_pairing_cmpl(tSMP_CB* p_cb, tSMP_INT_DATA* p_data) {
  1216. if (p_cb->total_tx_unacked == 0) {
  1217. /* process the pairing complete */
  1218. smp_proc_pairing_cmpl(p_cb);
  1219. }
  1220. }
  1221. /*******************************************************************************
  1222. * Function smp_pair_terminate
  1223. * Description This function is called to send the pairing complete
  1224. * callback and remove the connection if needed.
  1225. ******************************************************************************/
  1226. void smp_pair_terminate(tSMP_CB* p_cb, tSMP_INT_DATA* p_data) {
  1227. SMP_TRACE_DEBUG("%s", __func__);
  1228. p_cb->status = SMP_CONN_TOUT;
  1229. smp_proc_pairing_cmpl(p_cb);
  1230. }
  1231. /*******************************************************************************
  1232. * Function smp_idle_terminate
  1233. * Description This function calledin idle state to determine to send
  1234. * authentication complete or not.
  1235. ******************************************************************************/
  1236. void smp_idle_terminate(tSMP_CB* p_cb, tSMP_INT_DATA* p_data) {
  1237. if (p_cb->flags & SMP_PAIR_FLAGS_WE_STARTED_DD) {
  1238. SMP_TRACE_DEBUG("Pairing terminated at IDLE state.");
  1239. p_cb->status = SMP_FAIL;
  1240. smp_proc_pairing_cmpl(p_cb);
  1241. }
  1242. }
  1243. /*******************************************************************************
  1244. * Function smp_both_have_public_keys
  1245. * Description The function is called when both local and peer public keys are
  1246. * saved.
  1247. * Actions:
  1248. * - invokes DHKey computation;
  1249. * - on slave side invokes sending local public key to the peer.
  1250. * - invokes SC phase 1 process.
  1251. ******************************************************************************/
  1252. void smp_both_have_public_keys(tSMP_CB* p_cb, tSMP_INT_DATA* p_data) {
  1253. SMP_TRACE_DEBUG("%s", __func__);
  1254. /* invokes DHKey computation */
  1255. smp_compute_dhkey(p_cb);
  1256. /* on slave side invokes sending local public key to the peer */
  1257. if (p_cb->role == HCI_ROLE_SLAVE) smp_send_pair_public_key(p_cb, NULL);
  1258. smp_sm_event(p_cb, SMP_SC_DHKEY_CMPLT_EVT, NULL);
  1259. }
  1260. /*******************************************************************************
  1261. * Function smp_start_secure_connection_phase1
  1262. * Description Start Secure Connection phase1 i.e. invokes initialization of
  1263. * Secure Connection phase 1 parameters and starts building/sending
  1264. * to the peer messages appropriate for the role and association
  1265. * model.
  1266. ******************************************************************************/
  1267. void smp_start_secure_connection_phase1(tSMP_CB* p_cb, tSMP_INT_DATA* p_data) {
  1268. SMP_TRACE_DEBUG("%s", __func__);
  1269. if (p_cb->selected_association_model == SMP_MODEL_SEC_CONN_JUSTWORKS) {
  1270. p_cb->sec_level = SMP_SEC_UNAUTHENTICATE;
  1271. SMP_TRACE_EVENT("p_cb->sec_level =%d (SMP_SEC_UNAUTHENTICATE) ",
  1272. p_cb->sec_level);
  1273. } else {
  1274. p_cb->sec_level = SMP_SEC_AUTHENTICATED;
  1275. SMP_TRACE_EVENT("p_cb->sec_level =%d (SMP_SEC_AUTHENTICATED) ",
  1276. p_cb->sec_level);
  1277. }
  1278. switch (p_cb->selected_association_model) {
  1279. case SMP_MODEL_SEC_CONN_JUSTWORKS:
  1280. case SMP_MODEL_SEC_CONN_NUM_COMP:
  1281. p_cb->local_random = {0};
  1282. smp_start_nonce_generation(p_cb);
  1283. break;
  1284. case SMP_MODEL_SEC_CONN_PASSKEY_ENT:
  1285. /* user has to provide passkey */
  1286. p_cb->cb_evt = SMP_PASSKEY_REQ_EVT;
  1287. smp_sm_event(p_cb, SMP_TK_REQ_EVT, NULL);
  1288. break;
  1289. case SMP_MODEL_SEC_CONN_PASSKEY_DISP:
  1290. /* passkey has to be provided to user */
  1291. SMP_TRACE_DEBUG("Need to generate SC Passkey");
  1292. smp_generate_passkey(p_cb, NULL);
  1293. break;
  1294. case SMP_MODEL_SEC_CONN_OOB:
  1295. /* use the available OOB information */
  1296. smp_process_secure_connection_oob_data(p_cb, NULL);
  1297. break;
  1298. default:
  1299. SMP_TRACE_ERROR("Association Model = %d is not used in LE SC",
  1300. p_cb->selected_association_model);
  1301. break;
  1302. }
  1303. }
  1304. /*******************************************************************************
  1305. * Function smp_process_local_nonce
  1306. * Description The function processes new local nonce.
  1307. *
  1308. * Note It is supposed to be called in SC phase1.
  1309. ******************************************************************************/
  1310. void smp_process_local_nonce(tSMP_CB* p_cb, tSMP_INT_DATA* p_data) {
  1311. SMP_TRACE_DEBUG("%s", __func__);
  1312. switch (p_cb->selected_association_model) {
  1313. case SMP_MODEL_SEC_CONN_JUSTWORKS:
  1314. case SMP_MODEL_SEC_CONN_NUM_COMP:
  1315. if (p_cb->role == HCI_ROLE_SLAVE) {
  1316. /* slave calculates and sends local commitment */
  1317. smp_calculate_local_commitment(p_cb);
  1318. smp_send_commitment(p_cb, NULL);
  1319. /* slave has to wait for peer nonce */
  1320. smp_set_state(SMP_STATE_WAIT_NONCE);
  1321. } else /* i.e. master */
  1322. {
  1323. if (p_cb->flags & SMP_PAIR_FLAG_HAVE_PEER_COMM) {
  1324. /* slave commitment is already received, send local nonce, wait for
  1325. * remote nonce*/
  1326. SMP_TRACE_DEBUG(
  1327. "master in assoc mode = %d "
  1328. "already rcvd slave commitment - race condition",
  1329. p_cb->selected_association_model);
  1330. p_cb->flags &= ~SMP_PAIR_FLAG_HAVE_PEER_COMM;
  1331. smp_send_rand(p_cb, NULL);
  1332. smp_set_state(SMP_STATE_WAIT_NONCE);
  1333. }
  1334. }
  1335. break;
  1336. case SMP_MODEL_SEC_CONN_PASSKEY_ENT:
  1337. case SMP_MODEL_SEC_CONN_PASSKEY_DISP:
  1338. smp_calculate_local_commitment(p_cb);
  1339. if (p_cb->role == HCI_ROLE_MASTER) {
  1340. smp_send_commitment(p_cb, NULL);
  1341. } else /* slave */
  1342. {
  1343. if (p_cb->flags & SMP_PAIR_FLAG_HAVE_PEER_COMM) {
  1344. /* master commitment is already received */
  1345. smp_send_commitment(p_cb, NULL);
  1346. smp_set_state(SMP_STATE_WAIT_NONCE);
  1347. }
  1348. }
  1349. break;
  1350. case SMP_MODEL_SEC_CONN_OOB:
  1351. if (p_cb->role == HCI_ROLE_MASTER) {
  1352. smp_send_rand(p_cb, NULL);
  1353. }
  1354. smp_set_state(SMP_STATE_WAIT_NONCE);
  1355. break;
  1356. default:
  1357. SMP_TRACE_ERROR("Association Model = %d is not used in LE SC",
  1358. p_cb->selected_association_model);
  1359. break;
  1360. }
  1361. }
  1362. /*******************************************************************************
  1363. * Function smp_process_peer_nonce
  1364. * Description The function processes newly received and saved in CB peer
  1365. * nonce. The actions depend on the selected association model and
  1366. * the role.
  1367. *
  1368. * Note It is supposed to be called in SC phase1.
  1369. ******************************************************************************/
  1370. void smp_process_peer_nonce(tSMP_CB* p_cb, tSMP_INT_DATA* p_data) {
  1371. SMP_TRACE_DEBUG("%s start ", __func__);
  1372. // PTS Testing failure modes
  1373. if (p_cb->cert_failure == SMP_CONFIRM_VALUE_ERR) {
  1374. SMP_TRACE_ERROR("%s failure case = %d", __func__, p_cb->cert_failure);
  1375. tSMP_INT_DATA smp_int_data;
  1376. smp_int_data.status = SMP_CONFIRM_VALUE_ERR;
  1377. p_cb->failure = SMP_CONFIRM_VALUE_ERR;
  1378. smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &smp_int_data);
  1379. return;
  1380. }
  1381. // PTS Testing failure modes (for LT)
  1382. if ((p_cb->cert_failure == SMP_NUMERIC_COMPAR_FAIL) &&
  1383. (p_cb->selected_association_model == SMP_MODEL_SEC_CONN_JUSTWORKS) &&
  1384. (p_cb->role == HCI_ROLE_SLAVE)) {
  1385. SMP_TRACE_ERROR("%s failure case = %d", __func__, p_cb->cert_failure);
  1386. tSMP_INT_DATA smp_int_data;
  1387. smp_int_data.status = SMP_NUMERIC_COMPAR_FAIL;
  1388. p_cb->failure = SMP_NUMERIC_COMPAR_FAIL;
  1389. smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &smp_int_data);
  1390. return;
  1391. }
  1392. switch (p_cb->selected_association_model) {
  1393. case SMP_MODEL_SEC_CONN_JUSTWORKS:
  1394. case SMP_MODEL_SEC_CONN_NUM_COMP:
  1395. /* in these models only master receives commitment */
  1396. if (p_cb->role == HCI_ROLE_MASTER) {
  1397. if (!smp_check_commitment(p_cb)) {
  1398. tSMP_INT_DATA smp_int_data;
  1399. smp_int_data.status = SMP_CONFIRM_VALUE_ERR;
  1400. p_cb->failure = SMP_CONFIRM_VALUE_ERR;
  1401. smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &smp_int_data);
  1402. break;
  1403. }
  1404. } else {
  1405. /* slave sends local nonce */
  1406. smp_send_rand(p_cb, NULL);
  1407. }
  1408. if (p_cb->selected_association_model == SMP_MODEL_SEC_CONN_JUSTWORKS) {
  1409. /* go directly to phase 2 */
  1410. smp_sm_event(p_cb, SMP_SC_PHASE1_CMPLT_EVT, NULL);
  1411. } else /* numeric comparison */
  1412. {
  1413. smp_set_state(SMP_STATE_WAIT_NONCE);
  1414. smp_sm_event(p_cb, SMP_SC_CALC_NC_EVT, NULL);
  1415. }
  1416. break;
  1417. case SMP_MODEL_SEC_CONN_PASSKEY_ENT:
  1418. case SMP_MODEL_SEC_CONN_PASSKEY_DISP:
  1419. if (!smp_check_commitment(p_cb) &&
  1420. p_cb->cert_failure != SMP_NUMERIC_COMPAR_FAIL) {
  1421. tSMP_INT_DATA smp_int_data;
  1422. smp_int_data.status = SMP_CONFIRM_VALUE_ERR;
  1423. p_cb->failure = SMP_CONFIRM_VALUE_ERR;
  1424. smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &smp_int_data);
  1425. break;
  1426. }
  1427. if (p_cb->role == HCI_ROLE_SLAVE) {
  1428. smp_send_rand(p_cb, NULL);
  1429. }
  1430. if (++p_cb->round < 20) {
  1431. smp_set_state(SMP_STATE_SEC_CONN_PHS1_START);
  1432. p_cb->flags &= ~SMP_PAIR_FLAG_HAVE_PEER_COMM;
  1433. smp_start_nonce_generation(p_cb);
  1434. break;
  1435. }
  1436. smp_sm_event(p_cb, SMP_SC_PHASE1_CMPLT_EVT, NULL);
  1437. break;
  1438. case SMP_MODEL_SEC_CONN_OOB:
  1439. if (p_cb->role == HCI_ROLE_SLAVE) {
  1440. smp_send_rand(p_cb, NULL);
  1441. }
  1442. smp_sm_event(p_cb, SMP_SC_PHASE1_CMPLT_EVT, NULL);
  1443. break;
  1444. default:
  1445. SMP_TRACE_ERROR("Association Model = %d is not used in LE SC",
  1446. p_cb->selected_association_model);
  1447. break;
  1448. }
  1449. SMP_TRACE_DEBUG("%s end ", __func__);
  1450. }
  1451. /*******************************************************************************
  1452. * Function smp_match_dhkey_checks
  1453. * Description checks if the calculated peer DHKey Check value is the same as
  1454. * received from the peer DHKey check value.
  1455. ******************************************************************************/
  1456. void smp_match_dhkey_checks(tSMP_CB* p_cb, tSMP_INT_DATA* p_data) {
  1457. SMP_TRACE_DEBUG("%s", __func__);
  1458. if (memcmp(p_data->key.p_data, p_cb->remote_dhkey_check.data(),
  1459. OCTET16_LEN)) {
  1460. SMP_TRACE_WARNING("dhkey chcks do no match");
  1461. tSMP_INT_DATA smp_int_data;
  1462. smp_int_data.status = SMP_DHKEY_CHK_FAIL;
  1463. p_cb->failure = SMP_DHKEY_CHK_FAIL;
  1464. smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &smp_int_data);
  1465. return;
  1466. }
  1467. SMP_TRACE_EVENT("dhkey chcks match");
  1468. /* compare the max encryption key size, and save the smaller one for the link
  1469. */
  1470. if (p_cb->peer_enc_size < p_cb->loc_enc_size)
  1471. p_cb->loc_enc_size = p_cb->peer_enc_size;
  1472. if (p_cb->role == HCI_ROLE_SLAVE) {
  1473. smp_sm_event(p_cb, SMP_PAIR_DHKEY_CHCK_EVT, NULL);
  1474. } else {
  1475. /* master device always use received i/r key as keys to distribute */
  1476. p_cb->local_i_key = p_cb->peer_i_key;
  1477. p_cb->local_r_key = p_cb->peer_r_key;
  1478. smp_sm_event(p_cb, SMP_ENC_REQ_EVT, NULL);
  1479. }
  1480. }
  1481. /*******************************************************************************
  1482. * Function smp_move_to_secure_connections_phase2
  1483. * Description Signal State Machine to start SC phase 2 initialization (to
  1484. * compute local DHKey Check value).
  1485. *
  1486. * Note SM is supposed to be in the state SMP_STATE_SEC_CONN_PHS2_START.
  1487. ******************************************************************************/
  1488. void smp_move_to_secure_connections_phase2(tSMP_CB* p_cb,
  1489. tSMP_INT_DATA* p_data) {
  1490. SMP_TRACE_DEBUG("%s", __func__);
  1491. smp_sm_event(p_cb, SMP_SC_PHASE1_CMPLT_EVT, NULL);
  1492. }
  1493. /*******************************************************************************
  1494. * Function smp_phase_2_dhkey_checks_are_present
  1495. * Description generates event if dhkey check from the peer is already
  1496. * received.
  1497. *
  1498. * Note It is supposed to be used on slave to prevent race condition.
  1499. * It is supposed to be called after slave dhkey check is
  1500. * calculated.
  1501. ******************************************************************************/
  1502. void smp_phase_2_dhkey_checks_are_present(tSMP_CB* p_cb,
  1503. tSMP_INT_DATA* p_data) {
  1504. SMP_TRACE_DEBUG("%s", __func__);
  1505. if (p_cb->flags & SMP_PAIR_FLAG_HAVE_PEER_DHK_CHK)
  1506. smp_sm_event(p_cb, SMP_SC_2_DHCK_CHKS_PRES_EVT, NULL);
  1507. }
  1508. /*******************************************************************************
  1509. * Function smp_wait_for_both_public_keys
  1510. * Description generates SMP_BOTH_PUBL_KEYS_RCVD_EVT event when both local and
  1511. * master public keys are available.
  1512. *
  1513. * Note on the slave it is used to prevent race condition.
  1514. *
  1515. ******************************************************************************/
  1516. void smp_wait_for_both_public_keys(tSMP_CB* p_cb, tSMP_INT_DATA* p_data) {
  1517. SMP_TRACE_DEBUG("%s", __func__);
  1518. if ((p_cb->flags & SMP_PAIR_FLAG_HAVE_PEER_PUBL_KEY) &&
  1519. (p_cb->flags & SMP_PAIR_FLAG_HAVE_LOCAL_PUBL_KEY)) {
  1520. if ((p_cb->role == HCI_ROLE_SLAVE) &&
  1521. ((p_cb->req_oob_type == SMP_OOB_LOCAL) ||
  1522. (p_cb->req_oob_type == SMP_OOB_BOTH))) {
  1523. smp_set_state(SMP_STATE_PUBLIC_KEY_EXCH);
  1524. }
  1525. smp_sm_event(p_cb, SMP_BOTH_PUBL_KEYS_RCVD_EVT, NULL);
  1526. }
  1527. }
  1528. /*******************************************************************************
  1529. * Function smp_start_passkey_verification
  1530. * Description Starts SC passkey entry verification.
  1531. ******************************************************************************/
  1532. void smp_start_passkey_verification(tSMP_CB* p_cb, tSMP_INT_DATA* p_data) {
  1533. uint8_t* p = NULL;
  1534. SMP_TRACE_DEBUG("%s", __func__);
  1535. p = p_cb->local_random.data();
  1536. UINT32_TO_STREAM(p, p_data->passkey);
  1537. p = p_cb->peer_random.data();
  1538. UINT32_TO_STREAM(p, p_data->passkey);
  1539. p_cb->round = 0;
  1540. smp_start_nonce_generation(p_cb);
  1541. }
  1542. /*******************************************************************************
  1543. * Function smp_process_secure_connection_oob_data
  1544. * Description Processes local/peer SC OOB data received from somewhere.
  1545. ******************************************************************************/
  1546. void smp_process_secure_connection_oob_data(tSMP_CB* p_cb,
  1547. tSMP_INT_DATA* p_data) {
  1548. SMP_TRACE_DEBUG("%s", __func__);
  1549. tSMP_SC_OOB_DATA* p_sc_oob_data = &p_cb->sc_oob_data;
  1550. if (p_sc_oob_data->loc_oob_data.present) {
  1551. p_cb->local_random = p_sc_oob_data->loc_oob_data.randomizer;
  1552. } else {
  1553. SMP_TRACE_EVENT("%s: local OOB randomizer is absent", __func__);
  1554. p_cb->local_random = {0};
  1555. }
  1556. if (!p_sc_oob_data->peer_oob_data.present) {
  1557. SMP_TRACE_EVENT("%s: peer OOB data is absent", __func__);
  1558. p_cb->peer_random = {0};
  1559. } else {
  1560. p_cb->peer_random = p_sc_oob_data->peer_oob_data.randomizer;
  1561. p_cb->remote_commitment = p_sc_oob_data->peer_oob_data.commitment;
  1562. /* check commitment */
  1563. if (!smp_check_commitment(p_cb)) {
  1564. tSMP_INT_DATA smp_int_data;
  1565. smp_int_data.status = SMP_CONFIRM_VALUE_ERR;
  1566. p_cb->failure = SMP_CONFIRM_VALUE_ERR;
  1567. smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &smp_int_data);
  1568. return;
  1569. }
  1570. if (p_cb->peer_oob_flag != SMP_OOB_PRESENT) {
  1571. /* the peer doesn't have local randomiser */
  1572. SMP_TRACE_EVENT(
  1573. "%s: peer didn't receive local OOB data, set local randomizer to 0",
  1574. __func__);
  1575. p_cb->local_random = {0};
  1576. }
  1577. }
  1578. print128(p_cb->local_random, (const uint8_t*)"local OOB randomizer");
  1579. print128(p_cb->peer_random, (const uint8_t*)"peer OOB randomizer");
  1580. smp_start_nonce_generation(p_cb);
  1581. }
  1582. /*******************************************************************************
  1583. * Function smp_set_local_oob_keys
  1584. * Description Saves calculated private/public keys in
  1585. * sc_oob_data.loc_oob_data, starts nonce generation
  1586. * (to be saved in sc_oob_data.loc_oob_data.randomizer).
  1587. ******************************************************************************/
  1588. void smp_set_local_oob_keys(tSMP_CB* p_cb, tSMP_INT_DATA* p_data) {
  1589. SMP_TRACE_DEBUG("%s", __func__);
  1590. memcpy(p_cb->sc_oob_data.loc_oob_data.private_key_used, p_cb->private_key,
  1591. BT_OCTET32_LEN);
  1592. p_cb->sc_oob_data.loc_oob_data.publ_key_used = p_cb->loc_publ_key;
  1593. smp_start_nonce_generation(p_cb);
  1594. }
  1595. /*******************************************************************************
  1596. * Function smp_set_local_oob_random_commitment
  1597. * Description Saves calculated randomizer and commitment in
  1598. * sc_oob_data.loc_oob_data, passes sc_oob_data.loc_oob_data up
  1599. * for safekeeping.
  1600. ******************************************************************************/
  1601. void smp_set_local_oob_random_commitment(tSMP_CB* p_cb, tSMP_INT_DATA* p_data) {
  1602. SMP_TRACE_DEBUG("%s", __func__);
  1603. p_cb->sc_oob_data.loc_oob_data.randomizer = p_cb->rand;
  1604. p_cb->sc_oob_data.loc_oob_data.commitment =
  1605. crypto_toolbox::f4(p_cb->sc_oob_data.loc_oob_data.publ_key_used.x,
  1606. p_cb->sc_oob_data.loc_oob_data.publ_key_used.x,
  1607. p_cb->sc_oob_data.loc_oob_data.randomizer, 0);
  1608. #if (SMP_DEBUG == TRUE)
  1609. uint8_t* p_print = NULL;
  1610. SMP_TRACE_DEBUG("local SC OOB data set:");
  1611. p_print = (uint8_t*)&p_cb->sc_oob_data.loc_oob_data.addr_sent_to;
  1612. smp_debug_print_nbyte_little_endian(p_print, "addr_sent_to",
  1613. sizeof(tBLE_BD_ADDR));
  1614. p_print = (uint8_t*)&p_cb->sc_oob_data.loc_oob_data.private_key_used;
  1615. smp_debug_print_nbyte_little_endian(p_print, "private_key_used",
  1616. BT_OCTET32_LEN);
  1617. p_print = (uint8_t*)&p_cb->sc_oob_data.loc_oob_data.publ_key_used.x;
  1618. smp_debug_print_nbyte_little_endian(p_print, "publ_key_used.x",
  1619. BT_OCTET32_LEN);
  1620. p_print = (uint8_t*)&p_cb->sc_oob_data.loc_oob_data.publ_key_used.y;
  1621. smp_debug_print_nbyte_little_endian(p_print, "publ_key_used.y",
  1622. BT_OCTET32_LEN);
  1623. p_print = (uint8_t*)&p_cb->sc_oob_data.loc_oob_data.randomizer;
  1624. smp_debug_print_nbyte_little_endian(p_print, "randomizer", OCTET16_LEN);
  1625. p_print = (uint8_t*)&p_cb->sc_oob_data.loc_oob_data.commitment;
  1626. smp_debug_print_nbyte_little_endian(p_print, "commitment", OCTET16_LEN);
  1627. SMP_TRACE_DEBUG("");
  1628. #endif
  1629. /* pass created OOB data up */
  1630. p_cb->cb_evt = SMP_SC_LOC_OOB_DATA_UP_EVT;
  1631. smp_send_app_cback(p_cb, NULL);
  1632. smp_cb_cleanup(p_cb);
  1633. }
  1634. /*******************************************************************************
  1635. *
  1636. * Function smp_link_encrypted
  1637. *
  1638. * Description This function is called when link is encrypted and notified
  1639. * to the slave device. Proceed to to send LTK, DIV and ER to
  1640. * master if bonding the devices.
  1641. *
  1642. *
  1643. * Returns void
  1644. *
  1645. ******************************************************************************/
  1646. void smp_link_encrypted(const RawAddress& bda, uint8_t encr_enable) {
  1647. tSMP_CB* p_cb = &smp_cb;
  1648. SMP_TRACE_DEBUG("%s: encr_enable=%d", __func__, encr_enable);
  1649. if (smp_cb.pairing_bda == bda) {
  1650. /* encryption completed with STK, remember the key size now, could be
  1651. * overwritten when key exchange happens */
  1652. if (p_cb->loc_enc_size != 0 && encr_enable) {
  1653. /* update the link encryption key size if a SMP pairing just performed */
  1654. btm_ble_update_sec_key_size(bda, p_cb->loc_enc_size);
  1655. }
  1656. tSMP_INT_DATA smp_int_data;
  1657. smp_int_data.status = encr_enable;
  1658. smp_sm_event(&smp_cb, SMP_ENCRYPTED_EVT, &smp_int_data);
  1659. }
  1660. }
  1661. void smp_cancel_start_encryption_attempt() {
  1662. SMP_TRACE_ERROR("%s: Encryption request cancelled", __func__);
  1663. smp_sm_event(&smp_cb, SMP_DISCARD_SEC_REQ_EVT, NULL);
  1664. }
  1665. /*******************************************************************************
  1666. *
  1667. * Function smp_proc_ltk_request
  1668. *
  1669. * Description This function is called when LTK request is received from
  1670. * controller.
  1671. *
  1672. * Returns void
  1673. *
  1674. ******************************************************************************/
  1675. bool smp_proc_ltk_request(const RawAddress& bda) {
  1676. SMP_TRACE_DEBUG("%s state = %d", __func__, smp_cb.state);
  1677. bool match = false;
  1678. if (bda == smp_cb.pairing_bda) {
  1679. match = true;
  1680. } else {
  1681. tBTM_SEC_DEV_REC* p_dev_rec = btm_find_dev(bda);
  1682. if (p_dev_rec != NULL && p_dev_rec->ble.pseudo_addr == smp_cb.pairing_bda &&
  1683. p_dev_rec->ble.pseudo_addr != RawAddress::kEmpty) {
  1684. match = true;
  1685. }
  1686. }
  1687. if (match && smp_cb.state == SMP_STATE_ENCRYPTION_PENDING) {
  1688. smp_sm_event(&smp_cb, SMP_ENC_REQ_EVT, NULL);
  1689. return true;
  1690. }
  1691. return false;
  1692. }
  1693. /*******************************************************************************
  1694. *
  1695. * Function smp_process_secure_connection_long_term_key
  1696. *
  1697. * Description This function is called to process SC LTK.
  1698. * SC LTK is calculated and used instead of STK.
  1699. * Here SC LTK is saved in BLE DB.
  1700. *
  1701. * Returns void
  1702. *
  1703. ******************************************************************************/
  1704. void smp_process_secure_connection_long_term_key(void) {
  1705. tSMP_CB* p_cb = &smp_cb;
  1706. SMP_TRACE_DEBUG("%s", __func__);
  1707. smp_save_secure_connections_long_term_key(p_cb);
  1708. smp_update_key_mask(p_cb, SMP_SEC_KEY_TYPE_ENC, false);
  1709. smp_key_distribution(p_cb, NULL);
  1710. }
  1711. /*******************************************************************************
  1712. *
  1713. * Function smp_set_derive_link_key
  1714. *
  1715. * Description This function is called to set flag that indicates that
  1716. * BR/EDR LK has to be derived from LTK after all keys are
  1717. * distributed.
  1718. *
  1719. * Returns void
  1720. *
  1721. ******************************************************************************/
  1722. void smp_set_derive_link_key(tSMP_CB* p_cb, tSMP_INT_DATA* p_data) {
  1723. SMP_TRACE_DEBUG("%s", __func__);
  1724. p_cb->derive_lk = true;
  1725. smp_update_key_mask(p_cb, SMP_SEC_KEY_TYPE_LK, false);
  1726. smp_key_distribution(p_cb, NULL);
  1727. }
  1728. /*******************************************************************************
  1729. *
  1730. * Function smp_derive_link_key_from_long_term_key
  1731. *
  1732. * Description This function is called to derive BR/EDR LK from LTK.
  1733. *
  1734. * Returns void
  1735. *
  1736. ******************************************************************************/
  1737. void smp_derive_link_key_from_long_term_key(tSMP_CB* p_cb,
  1738. tSMP_INT_DATA* p_data) {
  1739. tSMP_STATUS status = SMP_PAIR_FAIL_UNKNOWN;
  1740. SMP_TRACE_DEBUG("%s", __func__);
  1741. if (!smp_calculate_link_key_from_long_term_key(p_cb)) {
  1742. SMP_TRACE_ERROR("%s failed", __func__);
  1743. tSMP_INT_DATA smp_int_data;
  1744. smp_int_data.status = status;
  1745. smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &smp_int_data);
  1746. return;
  1747. }
  1748. }
  1749. /*******************************************************************************
  1750. *
  1751. * Function smp_br_process_link_key
  1752. *
  1753. * Description This function is called to process BR/EDR LK:
  1754. * - to derive SMP LTK from BR/EDR LK;
  1755. * - to save SMP LTK.
  1756. *
  1757. * Returns void
  1758. *
  1759. ******************************************************************************/
  1760. void smp_br_process_link_key(tSMP_CB* p_cb, tSMP_INT_DATA* p_data) {
  1761. tSMP_STATUS status = SMP_PAIR_FAIL_UNKNOWN;
  1762. SMP_TRACE_DEBUG("%s", __func__);
  1763. if (!smp_calculate_long_term_key_from_link_key(p_cb)) {
  1764. SMP_TRACE_ERROR("%s: failed", __func__);
  1765. tSMP_INT_DATA smp_int_data;
  1766. smp_int_data.status = status;
  1767. smp_sm_event(p_cb, SMP_BR_AUTH_CMPL_EVT, &smp_int_data);
  1768. return;
  1769. }
  1770. tBTM_SEC_DEV_REC* p_dev_rec = btm_find_dev(p_cb->pairing_bda);
  1771. if (p_dev_rec) {
  1772. SMP_TRACE_DEBUG("%s: dev_type = %d ", __func__, p_dev_rec->device_type);
  1773. p_dev_rec->device_type |= BT_DEVICE_TYPE_BLE;
  1774. } else {
  1775. SMP_TRACE_ERROR("%s failed to find Security Record", __func__);
  1776. }
  1777. SMP_TRACE_DEBUG("%s: LTK derivation from LK successfully completed",
  1778. __func__);
  1779. smp_save_secure_connections_long_term_key(p_cb);
  1780. smp_update_key_mask(p_cb, SMP_SEC_KEY_TYPE_ENC, false);
  1781. smp_br_select_next_key(p_cb, NULL);
  1782. }
  1783. /*******************************************************************************
  1784. * Function smp_key_distribution_by_transport
  1785. * Description depending on the transport used at the moment calls either
  1786. * smp_key_distribution(...) or smp_br_key_distribution(...).
  1787. ******************************************************************************/
  1788. void smp_key_distribution_by_transport(tSMP_CB* p_cb, tSMP_INT_DATA* p_data) {
  1789. SMP_TRACE_DEBUG("%s", __func__);
  1790. if (p_cb->smp_over_br) {
  1791. smp_br_select_next_key(p_cb, NULL);
  1792. } else {
  1793. smp_key_distribution(p_cb, NULL);
  1794. }
  1795. }
  1796. /*******************************************************************************
  1797. * Function smp_br_pairing_complete
  1798. * Description This function is called to send the pairing complete
  1799. * callback and remove the connection if needed.
  1800. ******************************************************************************/
  1801. void smp_br_pairing_complete(tSMP_CB* p_cb, tSMP_INT_DATA* p_data) {
  1802. SMP_TRACE_DEBUG("%s", __func__);
  1803. if (p_cb->total_tx_unacked == 0) {
  1804. /* process the pairing complete */
  1805. smp_proc_pairing_cmpl(p_cb);
  1806. }
  1807. }