gatt_auth.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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. 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. ** Function gatt_enc_cmpl_cback
  144. **
  145. ** Description link encryption complete callback.
  146. **
  147. ** Returns
  148. **
  149. *******************************************************************************/
  150. void gatt_enc_cmpl_cback(BD_ADDR bd_addr, tBT_TRANSPORT transport, void *p_ref_data, tBTM_STATUS result)
  151. {
  152. tGATT_TCB *p_tcb;
  153. UINT8 sec_flag;
  154. BOOLEAN status = FALSE;
  155. UNUSED(p_ref_data);
  156. GATT_TRACE_DEBUG("gatt_enc_cmpl_cback");
  157. if ((p_tcb = gatt_find_tcb_by_addr(bd_addr, transport)) != NULL) {
  158. if (gatt_get_sec_act(p_tcb) == GATT_SEC_ENC_PENDING) {
  159. return;
  160. }
  161. tGATT_PENDING_ENC_CLCB *p_buf =
  162. (tGATT_PENDING_ENC_CLCB *)fixed_queue_dequeue(p_tcb->pending_enc_clcb, 0);
  163. if (p_buf != NULL) {
  164. if (result == BTM_SUCCESS) {
  165. if (gatt_get_sec_act(p_tcb) == GATT_SEC_ENCRYPT_MITM ) {
  166. BTM_GetSecurityFlagsByTransport(bd_addr, &sec_flag, transport);
  167. if (sec_flag & BTM_SEC_FLAG_LKEY_AUTHED) {
  168. status = TRUE;
  169. }
  170. } else {
  171. status = TRUE;
  172. }
  173. }
  174. gatt_sec_check_complete(status , p_buf->p_clcb, p_tcb->sec_act);
  175. osi_free(p_buf);
  176. /* start all other pending operation in queue */
  177. for (size_t count = fixed_queue_length(p_tcb->pending_enc_clcb);
  178. count > 0; count--) {
  179. p_buf = (tGATT_PENDING_ENC_CLCB *)fixed_queue_dequeue(p_tcb->pending_enc_clcb, 0);
  180. if (p_buf != NULL) {
  181. gatt_security_check_start(p_buf->p_clcb);
  182. osi_free(p_buf);
  183. } else {
  184. break;
  185. }
  186. }
  187. } else {
  188. GATT_TRACE_ERROR("Unknown operation encryption completed");
  189. }
  190. } else {
  191. GATT_TRACE_ERROR("enc callback for unknown bd_addr");
  192. }
  193. }
  194. /*******************************************************************************
  195. **
  196. ** Function gatt_notify_enc_cmpl
  197. **
  198. ** Description link encryption complete notification for all encryption process
  199. ** initiated outside GATT.
  200. **
  201. ** Returns
  202. **
  203. *******************************************************************************/
  204. void gatt_notify_enc_cmpl(BD_ADDR bd_addr)
  205. {
  206. tGATT_TCB *p_tcb;
  207. UINT8 i = 0;
  208. if ((p_tcb = gatt_find_tcb_by_addr(bd_addr, BT_TRANSPORT_LE)) != NULL) {
  209. for (i = 0; i < GATT_MAX_APPS; i++) {
  210. if (gatt_cb.cl_rcb[i].in_use && gatt_cb.cl_rcb[i].app_cb.p_enc_cmpl_cb) {
  211. (*gatt_cb.cl_rcb[i].app_cb.p_enc_cmpl_cb)(gatt_cb.cl_rcb[i].gatt_if, bd_addr);
  212. }
  213. }
  214. if (gatt_get_sec_act(p_tcb) == GATT_SEC_ENC_PENDING) {
  215. gatt_set_sec_act(p_tcb, GATT_SEC_NONE);
  216. size_t count = fixed_queue_length(p_tcb->pending_enc_clcb);
  217. for (; count > 0; count--) {
  218. tGATT_PENDING_ENC_CLCB *p_buf =
  219. (tGATT_PENDING_ENC_CLCB *)fixed_queue_dequeue(p_tcb->pending_enc_clcb, 0);
  220. if (p_buf != NULL) {
  221. gatt_security_check_start(p_buf->p_clcb);
  222. osi_free(p_buf);
  223. } else {
  224. break;
  225. }
  226. }
  227. }
  228. } else {
  229. GATT_TRACE_DEBUG("notify GATT for encryption completion of unknown device");
  230. }
  231. return;
  232. }
  233. /*******************************************************************************
  234. **
  235. ** Function gatt_set_sec_act
  236. **
  237. ** Description This function set the sec_act in clcb
  238. **
  239. ** Returns none
  240. **
  241. *******************************************************************************/
  242. void gatt_set_sec_act(tGATT_TCB *p_tcb, tGATT_SEC_ACTION sec_act)
  243. {
  244. if (p_tcb) {
  245. p_tcb->sec_act = sec_act;
  246. }
  247. }
  248. /*******************************************************************************
  249. **
  250. ** Function gatt_get_sec_act
  251. **
  252. ** Description This function get the sec_act in clcb
  253. **
  254. ** Returns none
  255. **
  256. *******************************************************************************/
  257. tGATT_SEC_ACTION gatt_get_sec_act(tGATT_TCB *p_tcb)
  258. {
  259. tGATT_SEC_ACTION sec_act = GATT_SEC_NONE;
  260. if (p_tcb) {
  261. sec_act = p_tcb->sec_act;
  262. }
  263. return sec_act;
  264. }
  265. /*******************************************************************************
  266. **
  267. ** Function gatt_determine_sec_act
  268. **
  269. ** Description This routine determine the security action based on auth_request and
  270. ** current link status
  271. **
  272. ** Returns tGATT_SEC_ACTION security action
  273. **
  274. *******************************************************************************/
  275. tGATT_SEC_ACTION gatt_determine_sec_act(tGATT_CLCB *p_clcb )
  276. {
  277. tGATT_SEC_ACTION act = GATT_SEC_OK;
  278. UINT8 sec_flag;
  279. tGATT_TCB *p_tcb = p_clcb->p_tcb;
  280. tGATT_AUTH_REQ auth_req = p_clcb->auth_req;
  281. BOOLEAN is_link_encrypted = FALSE;
  282. BOOLEAN is_link_key_known = FALSE;
  283. BOOLEAN is_key_mitm = FALSE;
  284. #if (SMP_INCLUDED == TRUE)
  285. UINT8 key_type;
  286. tBTM_BLE_SEC_REQ_ACT sec_act = BTM_LE_SEC_NONE;
  287. #endif ///SMP_INCLUDED == TRUE
  288. if (auth_req == GATT_AUTH_REQ_NONE ) {
  289. return act;
  290. }
  291. BTM_GetSecurityFlagsByTransport(p_tcb->peer_bda, &sec_flag, p_clcb->p_tcb->transport);
  292. #if (SMP_INCLUDED == TRUE)
  293. btm_ble_link_sec_check(p_tcb->peer_bda, auth_req, &sec_act);
  294. #endif ///SMP_INCLUDED == TRUE
  295. /* if a encryption is pending, need to wait */
  296. if (
  297. #if (SMP_INCLUDED == TRUE)
  298. sec_act == BTM_BLE_SEC_REQ_ACT_DISCARD &&
  299. #endif ///SMP_INCLUDED == TRUE
  300. auth_req != GATT_AUTH_REQ_NONE) {
  301. return GATT_SEC_ENC_PENDING;
  302. }
  303. if (sec_flag & (BTM_SEC_FLAG_ENCRYPTED | BTM_SEC_FLAG_LKEY_KNOWN)) {
  304. if (sec_flag & BTM_SEC_FLAG_ENCRYPTED) {
  305. is_link_encrypted = TRUE;
  306. }
  307. is_link_key_known = TRUE;
  308. if (sec_flag & BTM_SEC_FLAG_LKEY_AUTHED) {
  309. is_key_mitm = TRUE;
  310. }
  311. }
  312. /* first check link key upgrade required or not */
  313. switch (auth_req) {
  314. case GATT_AUTH_REQ_MITM:
  315. case GATT_AUTH_REQ_SIGNED_MITM:
  316. if (!is_key_mitm) {
  317. act = GATT_SEC_ENCRYPT_MITM;
  318. }
  319. break;
  320. case GATT_AUTH_REQ_NO_MITM:
  321. case GATT_AUTH_REQ_SIGNED_NO_MITM:
  322. if (!is_link_key_known) {
  323. act = GATT_SEC_ENCRYPT_NO_MITM;
  324. }
  325. break;
  326. default:
  327. break;
  328. }
  329. /* now check link needs to be encrypted or not if the link key upgrade is not required */
  330. if (act == GATT_SEC_OK) {
  331. if (p_tcb->transport == BT_TRANSPORT_LE &&
  332. (p_clcb->operation == GATTC_OPTYPE_WRITE) &&
  333. (p_clcb->op_subtype == GATT_WRITE_NO_RSP)) {
  334. /* this is a write command request
  335. check data signing required or not */
  336. if (!is_link_encrypted) {
  337. #if (SMP_INCLUDED == TRUE)
  338. btm_ble_get_enc_key_type(p_tcb->peer_bda, &key_type);
  339. #endif ///SMP_INCLUDED == TRUE
  340. if (
  341. #if (SMP_INCLUDED == TRUE)
  342. (key_type & BTM_LE_KEY_LCSRK) &&
  343. #endif ///SMP_INCLUDED == TRUE
  344. ((auth_req == GATT_AUTH_REQ_SIGNED_NO_MITM) ||
  345. (auth_req == GATT_AUTH_REQ_SIGNED_MITM))) {
  346. act = GATT_SEC_SIGN_DATA;
  347. } else {
  348. act = GATT_SEC_ENCRYPT;
  349. }
  350. }
  351. } else {
  352. if (!is_link_encrypted) {
  353. act = GATT_SEC_ENCRYPT;
  354. }
  355. }
  356. }
  357. return act ;
  358. }
  359. /*******************************************************************************
  360. **
  361. ** Function gatt_get_link_encrypt_status
  362. **
  363. ** Description This routine get the encryption status of the specified link
  364. **
  365. **
  366. ** Returns tGATT_STATUS link encryption status
  367. **
  368. *******************************************************************************/
  369. tGATT_STATUS gatt_get_link_encrypt_status(tGATT_TCB *p_tcb)
  370. {
  371. tGATT_STATUS encrypt_status = GATT_NOT_ENCRYPTED;
  372. UINT8 sec_flag = 0;
  373. BTM_GetSecurityFlagsByTransport(p_tcb->peer_bda, &sec_flag, p_tcb->transport);
  374. if ((sec_flag & BTM_SEC_FLAG_ENCRYPTED) && (sec_flag & BTM_SEC_FLAG_LKEY_KNOWN)) {
  375. encrypt_status = GATT_ENCRYPED_NO_MITM;
  376. if (sec_flag & BTM_SEC_FLAG_LKEY_AUTHED) {
  377. encrypt_status = GATT_ENCRYPED_MITM;
  378. }
  379. }
  380. GATT_TRACE_DEBUG("gatt_get_link_encrypt_status status=0x%x", encrypt_status);
  381. return encrypt_status ;
  382. }
  383. /*******************************************************************************
  384. **
  385. ** Function gatt_convert_sec_action
  386. **
  387. ** Description Convert GATT security action enum into equivalent BTM BLE security action enum
  388. **
  389. ** Returns BOOLEAN TRUE - conversation is successful
  390. **
  391. *******************************************************************************/
  392. static BOOLEAN gatt_convert_sec_action(tGATT_SEC_ACTION gatt_sec_act, tBTM_BLE_SEC_ACT *p_btm_sec_act )
  393. {
  394. BOOLEAN status = TRUE;
  395. switch (gatt_sec_act) {
  396. case GATT_SEC_ENCRYPT:
  397. *p_btm_sec_act = BTM_BLE_SEC_ENCRYPT;
  398. break;
  399. case GATT_SEC_ENCRYPT_NO_MITM:
  400. *p_btm_sec_act = BTM_BLE_SEC_ENCRYPT_NO_MITM;
  401. break;
  402. case GATT_SEC_ENCRYPT_MITM:
  403. *p_btm_sec_act = BTM_BLE_SEC_ENCRYPT_MITM;
  404. break;
  405. default:
  406. status = FALSE;
  407. break;
  408. }
  409. return status;
  410. }
  411. /*******************************************************************************
  412. **
  413. ** Function gatt_check_enc_req
  414. **
  415. ** Description check link security.
  416. **
  417. ** Returns TRUE if encrypted, otherwise FALSE.
  418. **
  419. *******************************************************************************/
  420. BOOLEAN gatt_security_check_start(tGATT_CLCB *p_clcb)
  421. {
  422. tGATT_TCB *p_tcb = p_clcb->p_tcb;
  423. tGATT_SEC_ACTION gatt_sec_act;
  424. tBTM_BLE_SEC_ACT btm_ble_sec_act;
  425. BOOLEAN status = TRUE;
  426. #if (SMP_INCLUDED == TRUE)
  427. tBTM_STATUS btm_status;
  428. #endif ///SMP_INCLUDED == TRUE
  429. tGATT_SEC_ACTION sec_act_old = gatt_get_sec_act(p_tcb);
  430. gatt_sec_act = gatt_determine_sec_act(p_clcb);
  431. if (sec_act_old == GATT_SEC_NONE) {
  432. gatt_set_sec_act(p_tcb, gatt_sec_act);
  433. }
  434. switch (gatt_sec_act ) {
  435. case GATT_SEC_SIGN_DATA:
  436. #if (SMP_INCLUDED == TRUE)
  437. GATT_TRACE_DEBUG("gatt_security_check_start: Do data signing");
  438. gatt_sign_data(p_clcb);
  439. #endif ///SMP_INCLUDED == TRUE
  440. break;
  441. case GATT_SEC_ENCRYPT:
  442. case GATT_SEC_ENCRYPT_NO_MITM:
  443. case GATT_SEC_ENCRYPT_MITM:
  444. if (sec_act_old < GATT_SEC_ENCRYPT) {
  445. GATT_TRACE_DEBUG("gatt_security_check_start: Encrypt now or key upgreade first");
  446. gatt_convert_sec_action(gatt_sec_act, &btm_ble_sec_act);
  447. #if (SMP_INCLUDED == TRUE)
  448. btm_status = BTM_SetEncryption(p_tcb->peer_bda, p_tcb->transport , gatt_enc_cmpl_cback, &btm_ble_sec_act);
  449. if ( (btm_status != BTM_SUCCESS) && (btm_status != BTM_CMD_STARTED)) {
  450. GATT_TRACE_ERROR("gatt_security_check_start BTM_SetEncryption failed btm_status=%d", btm_status);
  451. status = FALSE;
  452. }
  453. #endif ///SMP_INCLUDED == TRUE
  454. }
  455. if (status) {
  456. gatt_add_pending_enc_channel_clcb (p_tcb, p_clcb);
  457. }
  458. break;
  459. case GATT_SEC_ENC_PENDING:
  460. gatt_add_pending_enc_channel_clcb (p_tcb, p_clcb);
  461. /* wait for link encrypotion to finish */
  462. break;
  463. default:
  464. gatt_sec_check_complete(TRUE, p_clcb, gatt_sec_act);
  465. break;
  466. }
  467. if (status == FALSE) {
  468. gatt_set_sec_act(p_tcb, GATT_SEC_NONE);
  469. gatt_set_ch_state(p_tcb, GATT_CH_OPEN);
  470. }
  471. return status;
  472. }
  473. #endif /* BLE_INCLUDED */