smp_api.cc 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. /******************************************************************************
  2. *
  3. * Copyright 2008-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 the implementation of the SMP interface used by
  21. * applications that can run over an SMP.
  22. *
  23. ******************************************************************************/
  24. #include <base/logging.h>
  25. #include <string.h>
  26. #include "bt_target.h"
  27. #include "bt_utils.h"
  28. #include "stack_config.h"
  29. #include "btm_int.h"
  30. #include "hcimsgs.h"
  31. #include "l2c_int.h"
  32. #include "l2cdefs.h"
  33. #include "smp_api.h"
  34. #include "smp_int.h"
  35. #include "btu.h"
  36. #include "p_256_ecc_pp.h"
  37. /*******************************************************************************
  38. *
  39. * Function SMP_Init
  40. *
  41. * Description This function initializes the SMP unit.
  42. *
  43. * Returns void
  44. *
  45. ******************************************************************************/
  46. void SMP_Init(void) {
  47. memset(&smp_cb, 0, sizeof(tSMP_CB));
  48. smp_cb.smp_rsp_timer_ent = alarm_new("smp.smp_rsp_timer_ent");
  49. smp_cb.delayed_auth_timer_ent = alarm_new("smp.delayed_auth_timer_ent");
  50. #if defined(SMP_INITIAL_TRACE_LEVEL)
  51. smp_cb.trace_level = SMP_INITIAL_TRACE_LEVEL;
  52. #else
  53. smp_cb.trace_level = BT_TRACE_LEVEL_NONE; /* No traces */
  54. #endif
  55. SMP_TRACE_EVENT("%s", __func__);
  56. smp_l2cap_if_init();
  57. /* initialization of P-256 parameters */
  58. p_256_init_curve(KEY_LENGTH_DWORDS_P256);
  59. /* Initialize failure case for certification */
  60. smp_cb.cert_failure =
  61. stack_config_get_interface()->get_pts_smp_failure_case();
  62. if (smp_cb.cert_failure)
  63. SMP_TRACE_ERROR("%s PTS FAILURE MODE IN EFFECT (CASE %d)", __func__,
  64. smp_cb.cert_failure);
  65. }
  66. /*******************************************************************************
  67. *
  68. * Function SMP_SetTraceLevel
  69. *
  70. * Description This function sets the trace level for SMP. If called with
  71. * a value of 0xFF, it simply returns the current trace level.
  72. *
  73. * Input Parameters:
  74. * level: The level to set the GATT tracing to:
  75. * 0xff-returns the current setting.
  76. * 0-turns off tracing.
  77. * >= 1-Errors.
  78. * >= 2-Warnings.
  79. * >= 3-APIs.
  80. * >= 4-Events.
  81. * >= 5-Debug.
  82. *
  83. * Returns The new or current trace level
  84. *
  85. ******************************************************************************/
  86. extern uint8_t SMP_SetTraceLevel(uint8_t new_level) {
  87. if (new_level != 0xFF) smp_cb.trace_level = new_level;
  88. return (smp_cb.trace_level);
  89. }
  90. /*******************************************************************************
  91. *
  92. * Function SMP_Register
  93. *
  94. * Description This function register for the SMP services callback.
  95. *
  96. * Returns void
  97. *
  98. ******************************************************************************/
  99. bool SMP_Register(tSMP_CALLBACK* p_cback) {
  100. SMP_TRACE_EVENT("SMP_Register state=%d", smp_cb.state);
  101. if (smp_cb.p_callback != NULL) {
  102. SMP_TRACE_ERROR("SMP_Register: duplicate registration, overwrite it");
  103. }
  104. smp_cb.p_callback = p_cback;
  105. return (true);
  106. }
  107. /*******************************************************************************
  108. *
  109. * Function SMP_Pair
  110. *
  111. * Description This function call to perform a SMP pairing with peer
  112. * device. Device support one SMP pairing at one time.
  113. *
  114. * Parameters bd_addr - peer device bd address.
  115. *
  116. * Returns None
  117. *
  118. ******************************************************************************/
  119. tSMP_STATUS SMP_Pair(const RawAddress& bd_addr) {
  120. tSMP_CB* p_cb = &smp_cb;
  121. SMP_TRACE_EVENT("%s: state=%d br_state=%d flag=0x%x, bd_addr=%s", __func__,
  122. p_cb->state, p_cb->br_state, p_cb->flags,
  123. bd_addr.ToString().c_str());
  124. if (p_cb->state != SMP_STATE_IDLE ||
  125. p_cb->flags & SMP_PAIR_FLAGS_WE_STARTED_DD || p_cb->smp_over_br) {
  126. /* pending security on going, reject this one */
  127. return SMP_BUSY;
  128. } else {
  129. p_cb->flags = SMP_PAIR_FLAGS_WE_STARTED_DD;
  130. p_cb->pairing_bda = bd_addr;
  131. if (!L2CA_ConnectFixedChnl(L2CAP_SMP_CID, bd_addr)) {
  132. tSMP_INT_DATA smp_int_data;
  133. smp_int_data.status = SMP_PAIR_INTERNAL_ERR;
  134. SMP_TRACE_ERROR("%s: L2C connect fixed channel failed.", __func__);
  135. smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &smp_int_data);
  136. return SMP_PAIR_INTERNAL_ERR;
  137. }
  138. return SMP_STARTED;
  139. }
  140. }
  141. /*******************************************************************************
  142. *
  143. * Function SMP_BR_PairWith
  144. *
  145. * Description This function is called to start a SMP pairing over BR/EDR.
  146. * Device support one SMP pairing at one time.
  147. *
  148. * Parameters bd_addr - peer device bd address.
  149. *
  150. * Returns SMP_STARTED if pairing started, otherwise the reason for
  151. * failure.
  152. *
  153. ******************************************************************************/
  154. tSMP_STATUS SMP_BR_PairWith(const RawAddress& bd_addr) {
  155. tSMP_CB* p_cb = &smp_cb;
  156. SMP_TRACE_EVENT("%s: state=%d br_state=%d flag=0x%x, bd_addr=%s", __func__,
  157. p_cb->state, p_cb->br_state, p_cb->flags,
  158. bd_addr.ToString().c_str());
  159. if (p_cb->state != SMP_STATE_IDLE || p_cb->smp_over_br ||
  160. p_cb->flags & SMP_PAIR_FLAGS_WE_STARTED_DD) {
  161. /* pending security on going, reject this one */
  162. return SMP_BUSY;
  163. }
  164. p_cb->role = HCI_ROLE_MASTER;
  165. p_cb->flags = SMP_PAIR_FLAGS_WE_STARTED_DD;
  166. p_cb->smp_over_br = true;
  167. p_cb->pairing_bda = bd_addr;
  168. if (!L2CA_ConnectFixedChnl(L2CAP_SMP_BR_CID, bd_addr)) {
  169. SMP_TRACE_ERROR("%s: L2C connect fixed channel failed.", __func__);
  170. tSMP_INT_DATA smp_int_data;
  171. smp_int_data.status = SMP_PAIR_INTERNAL_ERR;
  172. smp_br_state_machine_event(p_cb, SMP_BR_AUTH_CMPL_EVT, &smp_int_data);
  173. return SMP_PAIR_INTERNAL_ERR;
  174. }
  175. return SMP_STARTED;
  176. }
  177. /*******************************************************************************
  178. *
  179. * Function SMP_PairCancel
  180. *
  181. * Description This function call to cancel a SMP pairing with peer device.
  182. *
  183. * Parameters bd_addr - peer device bd address.
  184. *
  185. * Returns true - Pairining is cancelled
  186. *
  187. ******************************************************************************/
  188. bool SMP_PairCancel(const RawAddress& bd_addr) {
  189. tSMP_CB* p_cb = &smp_cb;
  190. uint8_t err_code = SMP_PAIR_FAIL_UNKNOWN;
  191. // PTS SMP failure test cases
  192. if (p_cb->cert_failure == SMP_PASSKEY_ENTRY_FAIL ||
  193. p_cb->cert_failure == SMP_NUMERIC_COMPAR_FAIL)
  194. err_code = p_cb->cert_failure;
  195. BTM_TRACE_EVENT("SMP_CancelPair state=%d flag=0x%x ", p_cb->state,
  196. p_cb->flags);
  197. if (p_cb->state != SMP_STATE_IDLE && p_cb->pairing_bda == bd_addr) {
  198. p_cb->is_pair_cancel = true;
  199. SMP_TRACE_DEBUG("Cancel Pairing: set fail reason Unknown");
  200. tSMP_INT_DATA smp_int_data;
  201. smp_int_data.status = SMP_PAIR_FAIL_UNKNOWN;
  202. smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &smp_int_data);
  203. return true;
  204. }
  205. return false;
  206. }
  207. /*******************************************************************************
  208. *
  209. * Function SMP_SecurityGrant
  210. *
  211. * Description This function is called to grant security process.
  212. *
  213. * Parameters bd_addr - peer device bd address.
  214. * res - result of the operation SMP_SUCCESS if success.
  215. * Otherwise, SMP_REPEATED_ATTEMPTS if too many
  216. * attempts.
  217. *
  218. * Returns None
  219. *
  220. ******************************************************************************/
  221. void SMP_SecurityGrant(const RawAddress& bd_addr, uint8_t res) {
  222. SMP_TRACE_EVENT("SMP_SecurityGrant ");
  223. if (smp_cb.smp_over_br) {
  224. if (smp_cb.br_state != SMP_BR_STATE_WAIT_APP_RSP ||
  225. smp_cb.cb_evt != SMP_SEC_REQUEST_EVT || smp_cb.pairing_bda != bd_addr) {
  226. return;
  227. }
  228. /* clear the SMP_SEC_REQUEST_EVT event after get grant */
  229. /* avoid generating duplicate pair request */
  230. smp_cb.cb_evt = 0;
  231. tSMP_INT_DATA smp_int_data;
  232. smp_int_data.status = res;
  233. smp_br_state_machine_event(&smp_cb, SMP_BR_API_SEC_GRANT_EVT,
  234. &smp_int_data);
  235. return;
  236. }
  237. if (smp_cb.state != SMP_STATE_WAIT_APP_RSP ||
  238. smp_cb.cb_evt != SMP_SEC_REQUEST_EVT || smp_cb.pairing_bda != bd_addr)
  239. return;
  240. /* clear the SMP_SEC_REQUEST_EVT event after get grant */
  241. /* avoid generate duplicate pair request */
  242. smp_cb.cb_evt = 0;
  243. tSMP_INT_DATA smp_int_data;
  244. smp_int_data.status = res;
  245. smp_sm_event(&smp_cb, SMP_API_SEC_GRANT_EVT, &smp_int_data);
  246. }
  247. /*******************************************************************************
  248. *
  249. * Function SMP_PasskeyReply
  250. *
  251. * Description This function is called after Security Manager submitted
  252. * passkey request to the application.
  253. *
  254. * Parameters: bd_addr - Address of the device for which passkey was
  255. * requested
  256. * res - result of the operation SMP_SUCCESS if success
  257. * passkey - numeric value in the range of
  258. * BTM_MIN_PASSKEY_VAL(0) -
  259. * BTM_MAX_PASSKEY_VAL(999999(0xF423F)).
  260. *
  261. ******************************************************************************/
  262. void SMP_PasskeyReply(const RawAddress& bd_addr, uint8_t res,
  263. uint32_t passkey) {
  264. tSMP_CB* p_cb = &smp_cb;
  265. SMP_TRACE_EVENT("SMP_PasskeyReply: Key: %d Result:%d", passkey, res);
  266. /* If timeout already expired or has been canceled, ignore the reply */
  267. if (p_cb->cb_evt != SMP_PASSKEY_REQ_EVT) {
  268. SMP_TRACE_WARNING("SMP_PasskeyReply() - Wrong State: %d", p_cb->state);
  269. return;
  270. }
  271. if (bd_addr != p_cb->pairing_bda) {
  272. SMP_TRACE_ERROR("SMP_PasskeyReply() - Wrong BD Addr");
  273. return;
  274. }
  275. if (btm_find_dev(bd_addr) == NULL) {
  276. SMP_TRACE_ERROR("SMP_PasskeyReply() - no dev CB");
  277. return;
  278. }
  279. if (passkey > BTM_MAX_PASSKEY_VAL || res != SMP_SUCCESS) {
  280. SMP_TRACE_WARNING(
  281. "SMP_PasskeyReply() - Wrong key len: %d or passkey entry fail",
  282. passkey);
  283. /* send pairing failure */
  284. tSMP_INT_DATA smp_int_data;
  285. smp_int_data.status = SMP_PASSKEY_ENTRY_FAIL;
  286. smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &smp_int_data);
  287. } else if (p_cb->selected_association_model ==
  288. SMP_MODEL_SEC_CONN_PASSKEY_ENT) {
  289. tSMP_INT_DATA smp_int_data;
  290. smp_int_data.passkey = passkey;
  291. smp_sm_event(&smp_cb, SMP_SC_KEY_READY_EVT, &smp_int_data);
  292. } else {
  293. smp_convert_string_to_tk(&p_cb->tk, passkey);
  294. }
  295. return;
  296. }
  297. /*******************************************************************************
  298. *
  299. * Function SMP_ConfirmReply
  300. *
  301. * Description This function is called after Security Manager submitted
  302. * numeric comparison request to the application.
  303. *
  304. * Parameters: bd_addr - Address of the device with which numeric
  305. * comparison was requested
  306. * res - comparison result SMP_SUCCESS if success
  307. *
  308. ******************************************************************************/
  309. void SMP_ConfirmReply(const RawAddress& bd_addr, uint8_t res) {
  310. tSMP_CB* p_cb = &smp_cb;
  311. SMP_TRACE_EVENT("%s: Result:%d", __func__, res);
  312. /* If timeout already expired or has been canceled, ignore the reply */
  313. if (p_cb->cb_evt != SMP_NC_REQ_EVT) {
  314. SMP_TRACE_WARNING("%s() - Wrong State: %d", __func__, p_cb->state);
  315. return;
  316. }
  317. if (bd_addr != p_cb->pairing_bda) {
  318. SMP_TRACE_ERROR("%s() - Wrong BD Addr", __func__);
  319. return;
  320. }
  321. if (btm_find_dev(bd_addr) == NULL) {
  322. SMP_TRACE_ERROR("%s() - no dev CB", __func__);
  323. return;
  324. }
  325. if (res != SMP_SUCCESS) {
  326. SMP_TRACE_WARNING("%s() - Numeric Comparison fails", __func__);
  327. /* send pairing failure */
  328. tSMP_INT_DATA smp_int_data;
  329. smp_int_data.status = SMP_NUMERIC_COMPAR_FAIL;
  330. smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &smp_int_data);
  331. } else {
  332. smp_sm_event(p_cb, SMP_SC_NC_OK_EVT, NULL);
  333. }
  334. }
  335. /*******************************************************************************
  336. *
  337. * Function SMP_OobDataReply
  338. *
  339. * Description This function is called to provide the OOB data for
  340. * SMP in response to SMP_OOB_REQ_EVT
  341. *
  342. * Parameters: bd_addr - Address of the peer device
  343. * res - result of the operation SMP_SUCCESS if success
  344. * p_data - simple pairing Randomizer C.
  345. *
  346. ******************************************************************************/
  347. void SMP_OobDataReply(const RawAddress& bd_addr, tSMP_STATUS res, uint8_t len,
  348. uint8_t* p_data) {
  349. tSMP_CB* p_cb = &smp_cb;
  350. tSMP_KEY key;
  351. SMP_TRACE_EVENT("%s State: %d res:%d", __func__, smp_cb.state, res);
  352. /* If timeout already expired or has been canceled, ignore the reply */
  353. if (p_cb->state != SMP_STATE_WAIT_APP_RSP || p_cb->cb_evt != SMP_OOB_REQ_EVT)
  354. return;
  355. if (res != SMP_SUCCESS || len == 0 || !p_data) {
  356. tSMP_INT_DATA smp_int_data;
  357. smp_int_data.status = SMP_OOB_FAIL;
  358. smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &smp_int_data);
  359. } else {
  360. if (len > OCTET16_LEN) len = OCTET16_LEN;
  361. memcpy(p_cb->tk.data(), p_data, len);
  362. key.key_type = SMP_KEY_TYPE_TK;
  363. key.p_data = p_cb->tk.data();
  364. tSMP_INT_DATA smp_int_data;
  365. smp_int_data.key = key;
  366. smp_sm_event(&smp_cb, SMP_KEY_READY_EVT, &smp_int_data);
  367. }
  368. }
  369. /*******************************************************************************
  370. *
  371. * Function SMP_SecureConnectionOobDataReply
  372. *
  373. * Description This function is called to provide the SC OOB data for
  374. * SMP in response to SMP_SC_OOB_REQ_EVT
  375. *
  376. * Parameters: p_data - pointer to the data
  377. *
  378. ******************************************************************************/
  379. void SMP_SecureConnectionOobDataReply(uint8_t* p_data) {
  380. tSMP_CB* p_cb = &smp_cb;
  381. tSMP_SC_OOB_DATA* p_oob = (tSMP_SC_OOB_DATA*)p_data;
  382. if (!p_oob) {
  383. SMP_TRACE_ERROR("%s received no data", __func__);
  384. tSMP_INT_DATA smp_int_data;
  385. smp_int_data.status = SMP_OOB_FAIL;
  386. smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &smp_int_data);
  387. return;
  388. }
  389. SMP_TRACE_EVENT(
  390. "%s req_oob_type: %d, loc_oob_data.present: %d, "
  391. "peer_oob_data.present: %d",
  392. __func__, p_cb->req_oob_type, p_oob->loc_oob_data.present,
  393. p_oob->peer_oob_data.present);
  394. if (p_cb->state != SMP_STATE_WAIT_APP_RSP ||
  395. p_cb->cb_evt != SMP_SC_OOB_REQ_EVT)
  396. return;
  397. bool data_missing = false;
  398. switch (p_cb->req_oob_type) {
  399. case SMP_OOB_PEER:
  400. if (!p_oob->peer_oob_data.present) data_missing = true;
  401. break;
  402. case SMP_OOB_LOCAL:
  403. if (!p_oob->loc_oob_data.present) data_missing = true;
  404. break;
  405. case SMP_OOB_BOTH:
  406. if (!p_oob->loc_oob_data.present || !p_oob->peer_oob_data.present)
  407. data_missing = true;
  408. break;
  409. default:
  410. SMP_TRACE_EVENT("Unexpected OOB data type requested. Fail OOB");
  411. data_missing = true;
  412. break;
  413. }
  414. tSMP_INT_DATA smp_int_data;
  415. if (data_missing) {
  416. smp_int_data.status = SMP_OOB_FAIL;
  417. smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &smp_int_data);
  418. return;
  419. }
  420. p_cb->sc_oob_data = *p_oob;
  421. smp_int_data.p_data = p_data;
  422. smp_sm_event(&smp_cb, SMP_SC_OOB_DATA_EVT, &smp_int_data);
  423. }
  424. /*******************************************************************************
  425. *
  426. * Function SMP_KeypressNotification
  427. *
  428. * Description This function is called to notify Security Manager about
  429. * Keypress Notification.
  430. *
  431. * Parameters: bd_addr Address of the device to send keypress
  432. * notification to
  433. * value Keypress notification parameter value
  434. *
  435. ******************************************************************************/
  436. void SMP_KeypressNotification(const RawAddress& bd_addr, uint8_t value) {
  437. tSMP_CB* p_cb = &smp_cb;
  438. SMP_TRACE_EVENT("%s: Value: %d", __func__, value);
  439. if (bd_addr != p_cb->pairing_bda) {
  440. SMP_TRACE_ERROR("%s() - Wrong BD Addr", __func__);
  441. return;
  442. }
  443. if (btm_find_dev(bd_addr) == NULL) {
  444. SMP_TRACE_ERROR("%s() - no dev CB", __func__);
  445. return;
  446. }
  447. /* Keypress Notification is used by a device with KeyboardOnly IO capabilities
  448. * during the passkey entry protocol */
  449. if (p_cb->local_io_capability != SMP_IO_CAP_IN) {
  450. SMP_TRACE_ERROR("%s() - wrong local IO capabilities %d", __func__,
  451. p_cb->local_io_capability);
  452. return;
  453. }
  454. if (p_cb->selected_association_model != SMP_MODEL_SEC_CONN_PASSKEY_ENT) {
  455. SMP_TRACE_ERROR("%s() - wrong protocol %d", __func__,
  456. p_cb->selected_association_model);
  457. return;
  458. }
  459. tSMP_INT_DATA smp_int_data;
  460. smp_int_data.status = value;
  461. smp_sm_event(p_cb, SMP_KEYPRESS_NOTIFICATION_EVENT, &smp_int_data);
  462. }
  463. /*******************************************************************************
  464. *
  465. * Function SMP_CreateLocalSecureConnectionsOobData
  466. *
  467. * Description This function is called to start creation of local SC OOB
  468. * data set (tSMP_LOC_OOB_DATA).
  469. *
  470. * Parameters: bd_addr - Address of the device to send OOB data block to
  471. *
  472. * Returns Boolean - true: creation of local SC OOB data set started.
  473. ******************************************************************************/
  474. bool SMP_CreateLocalSecureConnectionsOobData(tBLE_BD_ADDR* addr_to_send_to) {
  475. tSMP_CB* p_cb = &smp_cb;
  476. if (addr_to_send_to == NULL) {
  477. SMP_TRACE_ERROR("%s addr_to_send_to is not provided", __func__);
  478. return false;
  479. }
  480. VLOG(2) << __func__ << " addr type:" << +addr_to_send_to->type
  481. << ", BDA:" << addr_to_send_to->bda << ", state:" << p_cb->state
  482. << ", br_state: " << p_cb->br_state;
  483. if ((p_cb->state != SMP_STATE_IDLE) || (p_cb->smp_over_br)) {
  484. SMP_TRACE_WARNING(
  485. "%s creation of local OOB data set "
  486. "starts only in IDLE state",
  487. __func__);
  488. return false;
  489. }
  490. p_cb->sc_oob_data.loc_oob_data.addr_sent_to = *addr_to_send_to;
  491. smp_sm_event(p_cb, SMP_CR_LOC_SC_OOB_DATA_EVT, NULL);
  492. return true;
  493. }