hci.c (b45b7749fd0a3efec18073ae84f893078d0216d0) hci.c (e9f98c4a52a4522568d6b75f5ffa50a24b54385d)
1/*
2 * Copyright (C) 2014 BlueKitchen GmbH
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright

--- 200 unchanged lines hidden (view full) ---

209 conn->con_handle = 0xffff;
210 conn->authentication_flags = AUTH_FLAGS_NONE;
211 conn->bonding_flags = 0;
212 conn->requested_security_level = LEVEL_0;
213#ifdef ENABLE_CLASSIC
214 conn->request_role = HCI_ROLE_INVALID;
215 conn->sniff_subrating_max_latency = 0xffff;
216 conn->qos_service_type = HCI_SERVICE_TyPE_INVALID;
1/*
2 * Copyright (C) 2014 BlueKitchen GmbH
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright

--- 200 unchanged lines hidden (view full) ---

209 conn->con_handle = 0xffff;
210 conn->authentication_flags = AUTH_FLAGS_NONE;
211 conn->bonding_flags = 0;
212 conn->requested_security_level = LEVEL_0;
213#ifdef ENABLE_CLASSIC
214 conn->request_role = HCI_ROLE_INVALID;
215 conn->sniff_subrating_max_latency = 0xffff;
216 conn->qos_service_type = HCI_SERVICE_TyPE_INVALID;
217 conn->link_key_type = INVALID_LINK_KEY;
217 btstack_run_loop_set_timer_handler(&conn->timeout, hci_connection_timeout_handler);
218 btstack_run_loop_set_timer_context(&conn->timeout, conn);
219 hci_connection_timestamp(conn);
220#endif
221 conn->acl_recombination_length = 0;
222 conn->acl_recombination_pos = 0;
223 conn->num_packets_sent = 0;
224

--- 1967 unchanged lines hidden (view full) ---

2192 handle = little_endian_read_16(packet, OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1);
2193 conn = hci_connection_for_handle(handle);
2194 if (conn != NULL) {
2195 uint8_t key_size = 0;
2196 if (status == 0){
2197 key_size = packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+3];
2198 log_info("Handle %04x key Size: %u", handle, key_size);
2199 } else {
218 btstack_run_loop_set_timer_handler(&conn->timeout, hci_connection_timeout_handler);
219 btstack_run_loop_set_timer_context(&conn->timeout, conn);
220 hci_connection_timestamp(conn);
221#endif
222 conn->acl_recombination_length = 0;
223 conn->acl_recombination_pos = 0;
224 conn->num_packets_sent = 0;
225

--- 1967 unchanged lines hidden (view full) ---

2193 handle = little_endian_read_16(packet, OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1);
2194 conn = hci_connection_for_handle(handle);
2195 if (conn != NULL) {
2196 uint8_t key_size = 0;
2197 if (status == 0){
2198 key_size = packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+3];
2199 log_info("Handle %04x key Size: %u", handle, key_size);
2200 } else {
2201 key_size = 1;
2200 log_info("Read Encryption Key Size failed 0x%02x-> assuming insecure connection with key size of 1", status);
2201 }
2202 hci_handle_read_encryption_key_size_complete(conn, key_size);
2203 }
2204 break;
2205#ifdef ENABLE_CLASSIC_PAIRING_OOB
2206 case HCI_OPCODE_HCI_READ_LOCAL_OOB_DATA:
2207 case HCI_OPCODE_HCI_READ_LOCAL_EXTENDED_OOB_DATA:{

--- 439 unchanged lines hidden (view full) ---

2647 conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL);
2648 if (!conn) break;
2649 conn->authentication_flags |= RECV_LINK_KEY_NOTIFICATION;
2650 link_key_type_t link_key_type = (link_key_type_t)packet[24];
2651 // Change Connection Encryption keeps link key type
2652 if (link_key_type != CHANGED_COMBINATION_KEY){
2653 conn->link_key_type = link_key_type;
2654 }
2202 log_info("Read Encryption Key Size failed 0x%02x-> assuming insecure connection with key size of 1", status);
2203 }
2204 hci_handle_read_encryption_key_size_complete(conn, key_size);
2205 }
2206 break;
2207#ifdef ENABLE_CLASSIC_PAIRING_OOB
2208 case HCI_OPCODE_HCI_READ_LOCAL_OOB_DATA:
2209 case HCI_OPCODE_HCI_READ_LOCAL_EXTENDED_OOB_DATA:{

--- 439 unchanged lines hidden (view full) ---

2649 conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL);
2650 if (!conn) break;
2651 conn->authentication_flags |= RECV_LINK_KEY_NOTIFICATION;
2652 link_key_type_t link_key_type = (link_key_type_t)packet[24];
2653 // Change Connection Encryption keeps link key type
2654 if (link_key_type != CHANGED_COMBINATION_KEY){
2655 conn->link_key_type = link_key_type;
2656 }
2657 // cache link key. link keys stored in little-endian format for legacy reasons
2658 memcpy(&conn->link_key, &packet[8], 16);
2659
2655 // only store link key:
2656 // - if bondable enabled
2657 if (hci_stack->bondable == false) break;
2658 // - if security level sufficient
2659 if (gap_security_level_for_link_key_type(link_key_type) < conn->requested_security_level) break;
2660 // - for SSP, also check if remote side requested bonding as well
2661 if (conn->link_key_type != COMBINATION_KEY){
2662 uint8_t auth_req_ignoring_mitm = conn->io_cap_response_auth_req & 0xfe;
2663 if (auth_req_ignoring_mitm == SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_NO_BONDING){
2664 break;
2665 }
2666 }
2667 gap_store_link_key_for_bd_addr(addr, &packet[8], conn->link_key_type);
2660 // only store link key:
2661 // - if bondable enabled
2662 if (hci_stack->bondable == false) break;
2663 // - if security level sufficient
2664 if (gap_security_level_for_link_key_type(link_key_type) < conn->requested_security_level) break;
2665 // - for SSP, also check if remote side requested bonding as well
2666 if (conn->link_key_type != COMBINATION_KEY){
2667 uint8_t auth_req_ignoring_mitm = conn->io_cap_response_auth_req & 0xfe;
2668 if (auth_req_ignoring_mitm == SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_NO_BONDING){
2669 break;
2670 }
2671 }
2672 gap_store_link_key_for_bd_addr(addr, &packet[8], conn->link_key_type);
2668 // still forward event to allow dismiss of pairing dialog
2669 break;
2670 }
2671
2672 case HCI_EVENT_PIN_CODE_REQUEST:
2673 hci_add_connection_flags_for_flipped_bd_addr(&packet[2], LEGACY_PAIRING_ACTIVE);
2674 // non-bondable mode: pin code negative reply will be sent
2675 if (!hci_stack->bondable){
2676 hci_add_connection_flags_for_flipped_bd_addr(&packet[2], DENY_PIN_CODE_REQUEST);

--- 1698 unchanged lines hidden (view full) ---

4375 return true;
4376 }
4377
4378 // Handling link key request requires remote supported features
4379 if ( ((connection->authentication_flags & HANDLE_LINK_KEY_REQUEST) != 0) && ((connection->bonding_flags & BONDING_RECEIVED_REMOTE_FEATURES) != 0)){
4380 log_info("responding to link key request, have link key db: %u", hci_stack->link_key_db != NULL);
4381 connectionClearAuthenticationFlags(connection, HANDLE_LINK_KEY_REQUEST);
4382
2673 break;
2674 }
2675
2676 case HCI_EVENT_PIN_CODE_REQUEST:
2677 hci_add_connection_flags_for_flipped_bd_addr(&packet[2], LEGACY_PAIRING_ACTIVE);
2678 // non-bondable mode: pin code negative reply will be sent
2679 if (!hci_stack->bondable){
2680 hci_add_connection_flags_for_flipped_bd_addr(&packet[2], DENY_PIN_CODE_REQUEST);

--- 1698 unchanged lines hidden (view full) ---

4379 return true;
4380 }
4381
4382 // Handling link key request requires remote supported features
4383 if ( ((connection->authentication_flags & HANDLE_LINK_KEY_REQUEST) != 0) && ((connection->bonding_flags & BONDING_RECEIVED_REMOTE_FEATURES) != 0)){
4384 log_info("responding to link key request, have link key db: %u", hci_stack->link_key_db != NULL);
4385 connectionClearAuthenticationFlags(connection, HANDLE_LINK_KEY_REQUEST);
4386
4383 link_key_t link_key;
4384 link_key_type_t link_key_type;
4385 bool have_link_key = hci_stack->link_key_db && hci_stack->link_key_db->get_link_key(connection->address, link_key, &link_key_type);
4387 // lookup link key using cached key first
4388 bool have_link_key = connection->link_key_type != INVALID_LINK_KEY;
4389 if (!have_link_key && (hci_stack->link_key_db != NULL)){
4390 have_link_key = hci_stack->link_key_db->get_link_key(connection->address, connection->link_key, &connection->link_key_type);
4391 }
4386
4387 const uint16_t sc_enabled_mask = BONDING_REMOTE_SUPPORTS_SC_HOST | BONDING_REMOTE_SUPPORTS_SC_CONTROLLER;
4388 bool sc_enabled_remote = (connection->bonding_flags & sc_enabled_mask) == sc_enabled_mask;
4392
4393 const uint16_t sc_enabled_mask = BONDING_REMOTE_SUPPORTS_SC_HOST | BONDING_REMOTE_SUPPORTS_SC_CONTROLLER;
4394 bool sc_enabled_remote = (connection->bonding_flags & sc_enabled_mask) == sc_enabled_mask;
4389 bool sc_downgrade = have_link_key && (gap_secure_connection_for_link_key_type(link_key_type) == 1) && !sc_enabled_remote;
4395 bool sc_downgrade = have_link_key && (gap_secure_connection_for_link_key_type(connection->link_key_type) == 1) && !sc_enabled_remote;
4390 if (sc_downgrade){
4391 log_info("Link key based on SC, but remote does not support SC -> disconnect");
4392 connection->state = SENT_DISCONNECT;
4393 hci_send_cmd(&hci_disconnect, connection->con_handle, ERROR_CODE_AUTHENTICATION_FAILURE);
4394 return true;
4395 }
4396
4396 if (sc_downgrade){
4397 log_info("Link key based on SC, but remote does not support SC -> disconnect");
4398 connection->state = SENT_DISCONNECT;
4399 hci_send_cmd(&hci_disconnect, connection->con_handle, ERROR_CODE_AUTHENTICATION_FAILURE);
4400 return true;
4401 }
4402
4397 bool security_level_sufficient = have_link_key && (gap_security_level_for_link_key_type(link_key_type) >= connection->requested_security_level);
4403 bool security_level_sufficient = have_link_key && (gap_security_level_for_link_key_type(connection->link_key_type) >= connection->requested_security_level);
4398 if (have_link_key && security_level_sufficient){
4404 if (have_link_key && security_level_sufficient){
4399 connection->link_key_type = link_key_type;
4400 hci_send_cmd(&hci_link_key_request_reply, connection->address, &link_key);
4405 hci_send_cmd(&hci_link_key_request_reply, connection->address, &connection->link_key);
4401 } else {
4402 hci_send_cmd(&hci_link_key_request_negative_reply, connection->address);
4403 }
4404 return true;
4405 }
4406
4407 if (connection->authentication_flags & DENY_PIN_CODE_REQUEST){
4408 log_info("denying to pin request");

--- 2190 unchanged lines hidden ---
4406 } else {
4407 hci_send_cmd(&hci_link_key_request_negative_reply, connection->address);
4408 }
4409 return true;
4410 }
4411
4412 if (connection->authentication_flags & DENY_PIN_CODE_REQUEST){
4413 log_info("denying to pin request");

--- 2190 unchanged lines hidden ---