gatt_auth.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. /******************************************************************************
  2. *
  3. * Copyright (C) 1999-2012 Broadcom Corporation
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at:
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. ******************************************************************************/
  18. /******************************************************************************
  19. *
  20. * this file contains GATT authentication handling functions
  21. *
  22. ******************************************************************************/
  23. #include "common/bt_target.h"
  24. #include "osi/allocator.h"
  25. #if BLE_INCLUDED == TRUE
  26. #include <string.h>
  27. #include "gatt_int.h"
  28. #include "stack/gatt_api.h"
  29. #include "btm_int.h"
  30. /*******************************************************************************
  31. **
  32. ** Function gatt_sign_data
  33. **
  34. ** Description This function sign the data for write command.
  35. **
  36. ** Returns TRUE if encrypted, otherwise FALSE.
  37. **
  38. *******************************************************************************/
  39. #if (SMP_INCLUDED == TRUE)
  40. static BOOLEAN gatt_sign_data (tGATT_CLCB *p_clcb)
  41. {
  42. tGATT_VALUE *p_attr = (tGATT_VALUE *)p_clcb->p_attr_buf;
  43. UINT8 *p_data = NULL, *p;
  44. UINT16 payload_size = p_clcb->p_tcb->payload_size;
  45. BOOLEAN status = FALSE;
  46. UINT8 *p_signature;
  47. /* do not need to mark channel securoty activity for data signing */
  48. gatt_set_sec_act(p_clcb->p_tcb, GATT_SEC_OK);
  49. p_data = (UINT8 *)osi_malloc((UINT16)(p_attr->len + 3)); /* 3 = 2 byte handle + opcode */
  50. if (p_data != NULL) {
  51. p = p_data;
  52. UINT8_TO_STREAM(p, GATT_SIGN_CMD_WRITE);
  53. UINT16_TO_STREAM(p, p_attr->handle);
  54. ARRAY_TO_STREAM(p, p_attr->value, p_attr->len);
  55. /* sign data length should be attribulte value length plus 2B handle + 1B op code */
  56. if ((payload_size - GATT_AUTH_SIGN_LEN - 3) < p_attr->len) {
  57. p_attr->len = payload_size - GATT_AUTH_SIGN_LEN - 3;
  58. }
  59. p_signature = p_attr->value + p_attr->len;
  60. if (BTM_BleDataSignature(p_clcb->p_tcb->peer_bda,
  61. p_data,
  62. (UINT16)(p_attr->len + 3), /* 3 = 2 byte handle + opcode */
  63. p_signature)) {
  64. p_attr->len += BTM_BLE_AUTH_SIGN_LEN;
  65. gatt_set_ch_state(p_clcb->p_tcb, GATT_CH_OPEN);
  66. #if (GATTC_INCLUDED == TRUE)
  67. gatt_act_write(p_clcb, GATT_SEC_SIGN_DATA);
  68. #endif ///GATTC_INCLUDED == TRUE
  69. } else {
  70. gatt_end_operation(p_clcb, GATT_INTERNAL_ERROR, NULL);
  71. }
  72. osi_free(p_data);
  73. }
  74. return status;
  75. }
  76. #endif ///SMP_INCLUDED == TRUE
  77. /*******************************************************************************
  78. **
  79. ** Function gatt_verify_signature
  80. **
  81. ** Description This function start to verify the sign data when receiving
  82. ** the data from peer device.
  83. **
  84. ** Returns
  85. **
  86. *******************************************************************************/
  87. #if (SMP_INCLUDED == TRUE)
  88. void gatt_verify_signature(tGATT_TCB *p_tcb, BT_HDR *p_buf)
  89. {
  90. UINT16 cmd_len;
  91. #if (GATTS_INCLUDED == TRUE)
  92. UINT8 op_code;
  93. #endif ///GATTS_INCLUDED == TRUE
  94. UINT8 *p, *p_orig = (UINT8 *)(p_buf + 1) + p_buf->offset;
  95. UINT32 counter;
  96. if (p_buf->len < GATT_AUTH_SIGN_LEN + 4) {
  97. GATT_TRACE_ERROR("%s: Data length %u less than expected %u",
  98. __func__, p_buf->len, GATT_AUTH_SIGN_LEN + 4);
  99. return;
  100. }
  101. cmd_len = p_buf->len - GATT_AUTH_SIGN_LEN + 4;
  102. p = p_orig + cmd_len - 4;
  103. STREAM_TO_UINT32(counter, p);
  104. if (BTM_BleVerifySignature(p_tcb->peer_bda, p_orig, cmd_len, counter, p)) {
  105. #if (GATTS_INCLUDED == TRUE)
  106. STREAM_TO_UINT8(op_code, p_orig);
  107. gatt_server_handle_client_req (p_tcb, op_code, (UINT16)(p_buf->len - 1), p_orig);
  108. #endif ///GATTS_INCLUDED == TRUE
  109. } else {
  110. /* if this is a bad signature, assume from attacker, ignore it */
  111. GATT_TRACE_ERROR("Signature Verification Failed, data ignored");
  112. }
  113. return;
  114. }
  115. #endif ///SMP_INCLUDED == TRUE
  116. /*******************************************************************************
  117. **
  118. ** Function gatt_sec_check_complete
  119. **
  120. ** Description security check complete and proceed to data sending action.
  121. **
  122. ** Returns void.
  123. **
  124. *******************************************************************************/
  125. void gatt_sec_check_complete(BOOLEAN sec_check_ok, tGATT_CLCB *p_clcb, UINT8 sec_act)
  126. {
  127. if (p_clcb && p_clcb->p_tcb) {
  128. if (fixed_queue_is_empty(p_clcb->p_tcb->pending_enc_clcb)) {
  129. gatt_set_sec_act(p_clcb->p_tcb, GATT_SEC_NONE);
  130. }
  131. #if (GATTC_INCLUDED == TRUE)
  132. if (!sec_check_ok) {
  133. gatt_end_operation(p_clcb, GATT_AUTH_FAIL, NULL);
  134. } else if (p_clcb->operation == GATTC_OPTYPE_WRITE) {
  135. gatt_act_write(p_clcb, sec_act);
  136. } else if (p_clcb->operation == GATTC_OPTYPE_READ) {
  137. gatt_act_read(p_clcb, p_clcb->counter);
  138. }
  139. #endif ///GATTC_INCLUDED == TRUE
  140. }
  141. }
  142. /*******************************************************************************
  143. **
  144. ** Function gatt_enc_cmpl_cback
  145. **
  146. ** Description link encryption complete callback.
  147. **
  148. ** Returns
  149. **
  150. *******************************************************************************/
  151. void gatt_enc_cmpl_cback(BD_ADDR bd_addr, tBT_TRANSPORT transport, void *p_ref_data, tBTM_STATUS result)
  152. {
  153. tGATT_TCB *p_tcb;
  154. UINT8 sec_flag;
  155. BOOLEAN status = FALSE;
  156. UNUSED(p_ref_data);
  157. GATT_TRACE_DEBUG("gatt_enc_cmpl_cback");
  158. if ((p_tcb = gatt_find_tcb_by_addr(bd_addr, transport)) != NULL) {
  159. if (gatt_get_sec_act(p_tcb) == GATT_SEC_ENC_PENDING) {
  160. return;
  161. }
  162. tGATT_PENDING_ENC_CLCB *p_buf =
  163. (tGATT_PENDING_ENC_CLCB *)fixed_queue_dequeue(p_tcb->pending_enc_clcb, 0);
  164. if (p_buf != NULL) {
  165. if (result == BTM_SUCCESS) {
  166. if (gatt_get_sec_act(p_tcb) == GATT_SEC_ENCRYPT_MITM ) {
  167. BTM_GetSecurityFlagsByTransport(bd_addr, &sec_flag, transport);
  168. if (sec_flag & BTM_SEC_FLAG_LKEY_AUTHED) {
  169. status = TRUE;
  170. }
  171. } else {
  172. status = TRUE;
  173. }
  174. }
  175. gatt_sec_check_complete(status , p_buf->p_clcb, p_tcb->sec_act);
  176. osi_free(p_buf);
  177. /* start all other pending operation in queue */
  178. for (size_t count = fixed_queue_length(p_tcb->pending_enc_clcb);
  179. count > 0; count--) {
  180. p_buf = (tGATT_PENDING_ENC_CLCB *)fixed_queue_dequeue(p_tcb->pending_enc_clcb, 0);
  181. if (p_buf != NULL) {
  182. gatt_security_check_start(p_buf->p_clcb);
  183. osi_free(p_buf);
  184. } else {
  185. break;
  186. }
  187. }
  188. } else {
  189. GATT_TRACE_ERROR("Unknown operation encryption completed");
  190. }
  191. } else {
  192. GATT_TRACE_ERROR("enc callback for unknown bd_addr");
  193. }
  194. }
  195. /*******************************************************************************
  196. **
  197. ** Function gatt_notify_enc_cmpl
  198. **
  199. ** Description link encryption complete notification for all encryption process
  200. ** initiated outside GATT.
  201. **
  202. ** Returns
  203. **
  204. *******************************************************************************/
  205. void gatt_notify_enc_cmpl(BD_ADDR bd_addr)
  206. {
  207. tGATT_TCB *p_tcb;
  208. UINT8 i = 0;
  209. if ((p_tcb = gatt_find_tcb_by_addr(bd_addr, BT_TRANSPORT_LE)) != NULL) {
  210. for (i = 0; i < GATT_MAX_APPS; i++) {
  211. if (gatt_cb.cl_rcb[i].in_use && gatt_cb.cl_rcb[i].app_cb.p_enc_cmpl_cb) {
  212. (*gatt_cb.cl_rcb[i].app_cb.p_enc_cmpl_cb)(gatt_cb.cl_rcb[i].gatt_if, bd_addr);
  213. }
  214. }
  215. if (gatt_get_sec_act(p_tcb) == GATT_SEC_ENC_PENDING) {
  216. gatt_set_sec_act(p_tcb, GATT_SEC_NONE);
  217. size_t count = fixed_queue_length(p_tcb->pending_enc_clcb);
  218. for (; count > 0; count--) {
  219. tGATT_PENDING_ENC_CLCB *p_buf =
  220. (tGATT_PENDING_ENC_CLCB *)fixed_queue_dequeue(p_tcb->pending_enc_clcb, 0);
  221. if (p_buf != NULL) {
  222. gatt_security_check_start(p_buf->p_clcb);
  223. osi_free(p_buf);
  224. } else {
  225. break;
  226. }
  227. }
  228. }
  229. } else {
  230. GATT_TRACE_DEBUG("notify GATT for encryption completion of unknown device");
  231. }
  232. return;
  233. }
  234. /*******************************************************************************
  235. **
  236. ** Function gatt_set_sec_act
  237. **
  238. ** Description This function set the sec_act in clcb
  239. **
  240. ** Returns none
  241. **
  242. *******************************************************************************/
  243. void gatt_set_sec_act(tGATT_TCB *p_tcb, tGATT_SEC_ACTION sec_act)
  244. {
  245. if (p_tcb) {
  246. p_tcb->sec_act = sec_act;
  247. }
  248. }
  249. /*******************************************************************************
  250. **
  251. ** Function gatt_get_sec_act
  252. **
  253. ** Description This function get the sec_act in clcb
  254. **
  255. ** Returns none
  256. **
  257. *******************************************************************************/
  258. tGATT_SEC_ACTION gatt_get_sec_act(tGATT_TCB *p_tcb)
  259. {
  260. tGATT_SEC_ACTION sec_act = GATT_SEC_NONE;
  261. if (p_tcb) {
  262. sec_act = p_tcb->sec_act;
  263. }
  264. return sec_act;
  265. }
  266. /*******************************************************************************
  267. **
  268. ** Function gatt_determine_sec_act
  269. **
  270. ** Description This routine determine the security action based on auth_request and
  271. ** current link status
  272. **
  273. ** Returns tGATT_SEC_ACTION security action
  274. **
  275. *******************************************************************************/
  276. tGATT_SEC_ACTION gatt_determine_sec_act(tGATT_CLCB *p_clcb )
  277. {
  278. tGATT_SEC_ACTION act = GATT_SEC_OK;
  279. UINT8 sec_flag;
  280. tGATT_TCB *p_tcb = p_clcb->p_tcb;
  281. tGATT_AUTH_REQ auth_req = p_clcb->auth_req;
  282. BOOLEAN is_link_encrypted = FALSE;
  283. BOOLEAN is_link_key_known = FALSE;
  284. BOOLEAN is_key_mitm = FALSE;
  285. #if (SMP_INCLUDED == TRUE)
  286. UINT8 key_type;
  287. tBTM_BLE_SEC_REQ_ACT sec_act = BTM_LE_SEC_NONE;
  288. #endif ///SMP_INCLUDED == TRUE
  289. if (auth_req == GATT_AUTH_REQ_NONE ) {
  290. return act;
  291. }
  292. BTM_GetSecurityFlagsByTransport(p_tcb->peer_bda, &sec_flag, p_clcb->p_tcb->transport);
  293. #if (SMP_INCLUDED == TRUE)
  294. btm_ble_link_sec_check(p_tcb->peer_bda, auth_req, &sec_act);
  295. #endif ///SMP_INCLUDED == TRUE
  296. /* if a encryption is pending, need to wait */
  297. if (
  298. #if (SMP_INCLUDED == TRUE)
  299. sec_act == BTM_BLE_SEC_REQ_ACT_DISCARD &&
  300. #endif ///SMP_INCLUDED == TRUE
  301. auth_req != GATT_AUTH_REQ_NONE) {
  302. return GATT_SEC_ENC_PENDING;
  303. }
  304. if (sec_flag & (BTM_SEC_FLAG_ENCRYPTED | BTM_SEC_FLAG_LKEY_KNOWN)) {
  305. if (sec_flag & BTM_SEC_FLAG_ENCRYPTED) {
  306. is_link_encrypted = TRUE;
  307. }
  308. is_link_key_known = TRUE;
  309. if (sec_flag & BTM_SEC_FLAG_LKEY_AUTHED) {
  310. is_key_mitm = TRUE;
  311. }
  312. }
  313. /* first check link key upgrade required or not */
  314. switch (auth_req) {
  315. case GATT_AUTH_REQ_MITM:
  316. case GATT_AUTH_REQ_SIGNED_MITM:
  317. if (!is_key_mitm) {
  318. act = GATT_SEC_ENCRYPT_MITM;
  319. }
  320. break;
  321. case GATT_AUTH_REQ_NO_MITM:
  322. case GATT_AUTH_REQ_SIGNED_NO_MITM:
  323. if (!is_link_key_known) {
  324. act = GATT_SEC_ENCRYPT_NO_MITM;
  325. }
  326. break;
  327. default:
  328. break;
  329. }
  330. /* now check link needs to be encrypted or not if the link key upgrade is not required */
  331. if (act == GATT_SEC_OK) {
  332. if (p_tcb->transport == BT_TRANSPORT_LE &&
  333. (p_clcb->operation == GATTC_OPTYPE_WRITE) &&
  334. (p_clcb->op_subtype == GATT_WRITE_NO_RSP)) {
  335. /* this is a write command request
  336. check data signing required or not */
  337. if (!is_link_encrypted) {
  338. #if (SMP_INCLUDED == TRUE)
  339. btm_ble_get_enc_key_type(p_tcb->peer_bda, &key_type);
  340. #endif ///SMP_INCLUDED == TRUE
  341. if (
  342. #if (SMP_INCLUDED == TRUE)
  343. (key_type & BTM_LE_KEY_LCSRK) &&
  344. #endif ///SMP_INCLUDED == TRUE
  345. ((auth_req == GATT_AUTH_REQ_SIGNED_NO_MITM) ||
  346. (auth_req == GATT_AUTH_REQ_SIGNED_MITM))) {
  347. act = GATT_SEC_SIGN_DATA;
  348. } else {
  349. act = GATT_SEC_ENCRYPT;
  350. }
  351. }
  352. } else {
  353. if (!is_link_encrypted) {
  354. act = GATT_SEC_ENCRYPT;
  355. }
  356. }
  357. }
  358. return act ;
  359. }
  360. /*******************************************************************************
  361. **
  362. ** Function gatt_get_link_encrypt_status
  363. **
  364. ** Description This routine get the encryption status of the specified link
  365. **
  366. **
  367. ** Returns tGATT_STATUS link encryption status
  368. **
  369. *******************************************************************************/
  370. tGATT_STATUS gatt_get_link_encrypt_status(tGATT_TCB *p_tcb)
  371. {
  372. tGATT_STATUS encrypt_status = GATT_NOT_ENCRYPTED;
  373. UINT8 sec_flag = 0;
  374. BTM_GetSecurityFlagsByTransport(p_tcb->peer_bda, &sec_flag, p_tcb->transport);
  375. if ((sec_flag & BTM_SEC_FLAG_ENCRYPTED) && (sec_flag & BTM_SEC_FLAG_LKEY_KNOWN)) {
  376. encrypt_status = GATT_ENCRYPED_NO_MITM;
  377. if (sec_flag & BTM_SEC_FLAG_LKEY_AUTHED) {
  378. encrypt_status = GATT_ENCRYPED_MITM;
  379. }
  380. }
  381. GATT_TRACE_DEBUG("gatt_get_link_encrypt_status status=0x%x", encrypt_status);
  382. return encrypt_status ;
  383. }
  384. /*******************************************************************************
  385. **
  386. ** Function gatt_convert_sec_action
  387. **
  388. ** Description Convert GATT security action enum into equivalent BTM BLE security action enum
  389. **
  390. ** Returns BOOLEAN TRUE - conversation is successful
  391. **
  392. *******************************************************************************/
  393. static BOOLEAN gatt_convert_sec_action(tGATT_SEC_ACTION gatt_sec_act, tBTM_BLE_SEC_ACT *p_btm_sec_act )
  394. {
  395. BOOLEAN status = TRUE;
  396. switch (gatt_sec_act) {
  397. case GATT_SEC_ENCRYPT:
  398. *p_btm_sec_act = BTM_BLE_SEC_ENCRYPT;
  399. break;
  400. case GATT_SEC_ENCRYPT_NO_MITM:
  401. *p_btm_sec_act = BTM_BLE_SEC_ENCRYPT_NO_MITM;
  402. break;
  403. case GATT_SEC_ENCRYPT_MITM:
  404. *p_btm_sec_act = BTM_BLE_SEC_ENCRYPT_MITM;
  405. break;
  406. default:
  407. status = FALSE;
  408. break;
  409. }
  410. return status;
  411. }
  412. /*******************************************************************************
  413. **
  414. ** Function gatt_check_enc_req
  415. **
  416. ** Description check link security.
  417. **
  418. ** Returns TRUE if encrypted, otherwise FALSE.
  419. **
  420. *******************************************************************************/
  421. BOOLEAN gatt_security_check_start(tGATT_CLCB *p_clcb)
  422. {
  423. tGATT_TCB *p_tcb = p_clcb->p_tcb;
  424. tGATT_SEC_ACTION gatt_sec_act;
  425. tBTM_BLE_SEC_ACT btm_ble_sec_act;
  426. BOOLEAN status = TRUE;
  427. #if (SMP_INCLUDED == TRUE)
  428. tBTM_STATUS btm_status;
  429. #endif ///SMP_INCLUDED == TRUE
  430. tGATT_SEC_ACTION sec_act_old = gatt_get_sec_act(p_tcb);
  431. gatt_sec_act = gatt_determine_sec_act(p_clcb);
  432. if (sec_act_old == GATT_SEC_NONE) {
  433. gatt_set_sec_act(p_tcb, gatt_sec_act);
  434. }
  435. switch (gatt_sec_act ) {
  436. case GATT_SEC_SIGN_DATA:
  437. #if (SMP_INCLUDED == TRUE)
  438. GATT_TRACE_DEBUG("gatt_security_check_start: Do data signing");
  439. gatt_sign_data(p_clcb);
  440. #endif ///SMP_INCLUDED == TRUE
  441. break;
  442. case GATT_SEC_ENCRYPT:
  443. case GATT_SEC_ENCRYPT_NO_MITM:
  444. case GATT_SEC_ENCRYPT_MITM:
  445. if (sec_act_old < GATT_SEC_ENCRYPT) {
  446. GATT_TRACE_DEBUG("gatt_security_check_start: Encrypt now or key upgreade first");
  447. gatt_convert_sec_action(gatt_sec_act, &btm_ble_sec_act);
  448. #if (SMP_INCLUDED == TRUE)
  449. btm_status = BTM_SetEncryption(p_tcb->peer_bda, p_tcb->transport , gatt_enc_cmpl_cback, &btm_ble_sec_act);
  450. if ( (btm_status != BTM_SUCCESS) && (btm_status != BTM_CMD_STARTED)) {
  451. GATT_TRACE_ERROR("gatt_security_check_start BTM_SetEncryption failed btm_status=%d", btm_status);
  452. status = FALSE;
  453. }
  454. #endif ///SMP_INCLUDED == TRUE
  455. }
  456. if (status) {
  457. gatt_add_pending_enc_channel_clcb (p_tcb, p_clcb);
  458. }
  459. break;
  460. case GATT_SEC_ENC_PENDING:
  461. gatt_add_pending_enc_channel_clcb (p_tcb, p_clcb);
  462. /* wait for link encrypotion to finish */
  463. break;
  464. default:
  465. gatt_sec_check_complete(TRUE, p_clcb, gatt_sec_act);
  466. break;
  467. }
  468. if (status == FALSE) {
  469. gatt_set_sec_act(p_tcb, GATT_SEC_NONE);
  470. gatt_set_ch_state(p_tcb, GATT_CH_OPEN);
  471. }
  472. return status;
  473. }
  474. #endif /* BLE_INCLUDED */