sm.c (5829ebe2e7f2fb392ce82005694f217303b7361e) sm.c (bbf8db22e40aa1eb7f5a53e393a53b45352aa0ee)
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

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

2152 sm_pairing_packet_set_code(setup->sm_s_pres,SM_CODE_PAIRING_RESPONSE);
2153 key_distribution_flags = sm_key_distribution_flags_for_auth_req();
2154
2155 connection->sm_engine_state = SM_RESPONDER_PH1_W4_PAIRING_CONFIRM;
2156#ifdef ENABLE_LE_SECURE_CONNECTIONS
2157 if (setup->sm_use_secure_connections){
2158 connection->sm_engine_state = SM_SC_W4_PUBLIC_KEY_COMMAND;
2159 // skip LTK/EDIV for SC
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

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

2152 sm_pairing_packet_set_code(setup->sm_s_pres,SM_CODE_PAIRING_RESPONSE);
2153 key_distribution_flags = sm_key_distribution_flags_for_auth_req();
2154
2155 connection->sm_engine_state = SM_RESPONDER_PH1_W4_PAIRING_CONFIRM;
2156#ifdef ENABLE_LE_SECURE_CONNECTIONS
2157 if (setup->sm_use_secure_connections){
2158 connection->sm_engine_state = SM_SC_W4_PUBLIC_KEY_COMMAND;
2159 // skip LTK/EDIV for SC
2160 log_info("sm: dropping encryption information flag");
2160 key_distribution_flags &= ~SM_KEYDIST_ENC_KEY;
2161 }
2162#endif
2163 sm_pairing_packet_set_initiator_key_distribution(setup->sm_s_pres, sm_pairing_packet_get_initiator_key_distribution(setup->sm_m_preq) & key_distribution_flags);
2164 sm_pairing_packet_set_responder_key_distribution(setup->sm_s_pres, sm_pairing_packet_get_responder_key_distribution(setup->sm_m_preq) & key_distribution_flags);
2161 key_distribution_flags &= ~SM_KEYDIST_ENC_KEY;
2162 }
2163#endif
2164 sm_pairing_packet_set_initiator_key_distribution(setup->sm_s_pres, sm_pairing_packet_get_initiator_key_distribution(setup->sm_m_preq) & key_distribution_flags);
2165 sm_pairing_packet_set_responder_key_distribution(setup->sm_s_pres, sm_pairing_packet_get_responder_key_distribution(setup->sm_m_preq) & key_distribution_flags);
2166 // update key distribution after ENC was dropped
2167 sm_setup_key_distribution(sm_pairing_packet_get_responder_key_distribution(setup->sm_m_preq));
2165
2166 l2cap_send_connectionless(connection->sm_handle, L2CAP_CID_SECURITY_MANAGER_PROTOCOL, (uint8_t*) &setup->sm_s_pres, sizeof(sm_pairing_packet_t));
2167 sm_timeout_reset(connection);
2168 // SC Numeric Comparison will trigger user response after public keys & nonces have been exchanged
2169 if (setup->sm_stk_generation_method == JUST_WORKS){
2170 sm_trigger_user_response(connection);
2171 }
2172 return;

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

2816 switch (sm_conn->sm_engine_state){
2817 case SM_INITIATOR_PH0_W4_CONNECTION_ENCRYPTED:
2818 sm_conn->sm_engine_state = SM_INITIATOR_CONNECTED;
2819 sm_done_for_handle(sm_conn->sm_handle);
2820 break;
2821 case SM_PH2_W4_CONNECTION_ENCRYPTED:
2822 if (sm_conn->sm_role){
2823 // slave
2168
2169 l2cap_send_connectionless(connection->sm_handle, L2CAP_CID_SECURITY_MANAGER_PROTOCOL, (uint8_t*) &setup->sm_s_pres, sizeof(sm_pairing_packet_t));
2170 sm_timeout_reset(connection);
2171 // SC Numeric Comparison will trigger user response after public keys & nonces have been exchanged
2172 if (setup->sm_stk_generation_method == JUST_WORKS){
2173 sm_trigger_user_response(connection);
2174 }
2175 return;

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

2819 switch (sm_conn->sm_engine_state){
2820 case SM_INITIATOR_PH0_W4_CONNECTION_ENCRYPTED:
2821 sm_conn->sm_engine_state = SM_INITIATOR_CONNECTED;
2822 sm_done_for_handle(sm_conn->sm_handle);
2823 break;
2824 case SM_PH2_W4_CONNECTION_ENCRYPTED:
2825 if (sm_conn->sm_role){
2826 // slave
2824 sm_conn->sm_engine_state = SM_PH3_GET_RANDOM;
2827 if (setup->sm_use_secure_connections){
2828 sm_conn->sm_engine_state = SM_PH3_DISTRIBUTE_KEYS;
2829 } else {
2830 sm_conn->sm_engine_state = SM_PH3_GET_RANDOM;
2831 }
2825 } else {
2826 // master
2827 if (sm_key_distribution_all_received(sm_conn)){
2828 // skip receiving keys as there are none
2829 sm_key_distribution_handle_all_received(sm_conn);
2830 sm_conn->sm_engine_state = SM_PH3_GET_RANDOM;
2831 } else {
2832 sm_conn->sm_engine_state = SM_PH3_RECEIVE_KEYS;

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

3293 if (sm_key_distribution_all_received(sm_conn)){
3294
3295 sm_key_distribution_handle_all_received(sm_conn);
3296
3297 if (sm_conn->sm_role){
3298 sm_conn->sm_engine_state = SM_RESPONDER_IDLE;
3299 sm_done_for_handle(sm_conn->sm_handle);
3300 } else {
2832 } else {
2833 // master
2834 if (sm_key_distribution_all_received(sm_conn)){
2835 // skip receiving keys as there are none
2836 sm_key_distribution_handle_all_received(sm_conn);
2837 sm_conn->sm_engine_state = SM_PH3_GET_RANDOM;
2838 } else {
2839 sm_conn->sm_engine_state = SM_PH3_RECEIVE_KEYS;

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

3300 if (sm_key_distribution_all_received(sm_conn)){
3301
3302 sm_key_distribution_handle_all_received(sm_conn);
3303
3304 if (sm_conn->sm_role){
3305 sm_conn->sm_engine_state = SM_RESPONDER_IDLE;
3306 sm_done_for_handle(sm_conn->sm_handle);
3307 } else {
3301 sm_conn->sm_engine_state = SM_PH3_GET_RANDOM;
3302#ifdef ENABLE_LE_SECURE_CONNECTIONS
3303 if (setup->sm_use_secure_connections){
3304 sm_conn->sm_engine_state = SM_PH3_DISTRIBUTE_KEYS;
3308 if (setup->sm_use_secure_connections){
3309 sm_conn->sm_engine_state = SM_PH3_DISTRIBUTE_KEYS;
3310 } else {
3311 sm_conn->sm_engine_state = SM_PH3_GET_RANDOM;
3305 }
3312 }
3306#endif
3307 }
3308 }
3309 break;
3310 default:
3311 // Unexpected PDU
3312 log_info("Unexpected PDU %u in state %u", packet[0], sm_conn->sm_engine_state);
3313 break;
3314 }

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

3552 sm_run();
3553}
3554
3555void sm_just_works_confirm(hci_con_handle_t con_handle){
3556 sm_connection_t * sm_conn = sm_get_connection_for_handle(con_handle);
3557 if (!sm_conn) return; // wrong connection
3558 setup->sm_user_response = SM_USER_RESPONSE_CONFIRM;
3559 if (sm_conn->sm_engine_state == SM_PH1_W4_USER_RESPONSE){
3313 }
3314 }
3315 break;
3316 default:
3317 // Unexpected PDU
3318 log_info("Unexpected PDU %u in state %u", packet[0], sm_conn->sm_engine_state);
3319 break;
3320 }

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

3558 sm_run();
3559}
3560
3561void sm_just_works_confirm(hci_con_handle_t con_handle){
3562 sm_connection_t * sm_conn = sm_get_connection_for_handle(con_handle);
3563 if (!sm_conn) return; // wrong connection
3564 setup->sm_user_response = SM_USER_RESPONSE_CONFIRM;
3565 if (sm_conn->sm_engine_state == SM_PH1_W4_USER_RESPONSE){
3560 sm_conn->sm_engine_state = SM_PH2_C1_GET_RANDOM_A;
3561
3562#ifdef ENABLE_LE_SECURE_CONNECTIONS
3563 if (setup->sm_use_secure_connections){
3564 sm_conn->sm_engine_state = SM_SC_SEND_PUBLIC_KEY_COMMAND;
3566 if (setup->sm_use_secure_connections){
3567 sm_conn->sm_engine_state = SM_SC_SEND_PUBLIC_KEY_COMMAND;
3568 } else {
3569 sm_conn->sm_engine_state = SM_PH2_C1_GET_RANDOM_A;
3565 }
3570 }
3566#endif
3567 }
3568
3569#ifdef ENABLE_LE_SECURE_CONNECTIONS
3570 if (sm_conn->sm_engine_state == SM_SC_W4_USER_RESPONSE){
3571 sm_sc_prepare_dhkey_check(sm_conn);
3572 }
3573#endif
3574

--- 83 unchanged lines hidden ---
3571 }
3572
3573#ifdef ENABLE_LE_SECURE_CONNECTIONS
3574 if (sm_conn->sm_engine_state == SM_SC_W4_USER_RESPONSE){
3575 sm_sc_prepare_dhkey_check(sm_conn);
3576 }
3577#endif
3578

--- 83 unchanged lines hidden ---