sm.c (09ea1b629026db052907d315fb73ba2c4e8111c1) | sm.c (27ef8bc836da6d760bc5222c9f84020afdc31ed8) |
---|---|
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 --- 1302 unchanged lines hidden (view full) --- 1311 break; 1312 } 1313} 1314 1315static void sm_key_distribution_handle_all_received(sm_connection_t * sm_conn){ 1316 1317 int le_db_index = -1; 1318 | 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 --- 1302 unchanged lines hidden (view full) --- 1311 break; 1312 } 1313} 1314 1315static void sm_key_distribution_handle_all_received(sm_connection_t * sm_conn){ 1316 1317 int le_db_index = -1; 1318 |
1319 // lookup device based on IRK 1320 if (setup->sm_key_distribution_received_set & SM_KEYDIST_FLAG_IDENTITY_INFORMATION){ 1321 int i; 1322 for (i=0; i < le_device_db_max_count(); i++){ 1323 sm_key_t irk; 1324 bd_addr_t address; 1325 int address_type; 1326 le_device_db_info(i, &address_type, address, irk); 1327 if (memcmp(irk, setup->sm_peer_irk, 16) == 0){ 1328 log_info("sm: device found for IRK, updating"); 1329 le_db_index = i; 1330 break; | 1319 // only store pairing information if both sides are bondable, i.e., the bonadble flag is set 1320 int bonding_enabed = ( sm_pairing_packet_get_auth_req(setup->sm_m_preq) 1321 & sm_pairing_packet_get_auth_req(setup->sm_s_pres) 1322 & SM_AUTHREQ_BONDING ) != 0; 1323 1324 if (bonding_enabed){ 1325 1326 // lookup device based on IRK 1327 if (setup->sm_key_distribution_received_set & SM_KEYDIST_FLAG_IDENTITY_INFORMATION){ 1328 int i; 1329 for (i=0; i < le_device_db_max_count(); i++){ 1330 sm_key_t irk; 1331 bd_addr_t address; 1332 int address_type; 1333 le_device_db_info(i, &address_type, address, irk); 1334 if (memcmp(irk, setup->sm_peer_irk, 16) == 0){ 1335 log_info("sm: device found for IRK, updating"); 1336 le_db_index = i; 1337 break; 1338 } |
1331 } 1332 } | 1339 } 1340 } |
1333 } | |
1334 | 1341 |
1335 // if not found, lookup via public address if possible 1336 log_info("sm peer addr type %u, peer addres %s", setup->sm_peer_addr_type, bd_addr_to_str(setup->sm_peer_address)); 1337 if (le_db_index < 0 && setup->sm_peer_addr_type == BD_ADDR_TYPE_LE_PUBLIC){ 1338 int i; 1339 for (i=0; i < le_device_db_max_count(); i++){ 1340 bd_addr_t address; 1341 int address_type; 1342 le_device_db_info(i, &address_type, address, NULL); 1343 log_info("device %u, sm peer addr type %u, peer addres %s", i, address_type, bd_addr_to_str(address)); 1344 if (address_type == BD_ADDR_TYPE_LE_PUBLIC && memcmp(address, setup->sm_peer_address, 6) == 0){ 1345 log_info("sm: device found for public address, updating"); 1346 le_db_index = i; 1347 break; | 1342 // if not found, lookup via public address if possible 1343 log_info("sm peer addr type %u, peer addres %s", setup->sm_peer_addr_type, bd_addr_to_str(setup->sm_peer_address)); 1344 if (le_db_index < 0 && setup->sm_peer_addr_type == BD_ADDR_TYPE_LE_PUBLIC){ 1345 int i; 1346 for (i=0; i < le_device_db_max_count(); i++){ 1347 bd_addr_t address; 1348 int address_type; 1349 le_device_db_info(i, &address_type, address, NULL); 1350 log_info("device %u, sm peer addr type %u, peer addres %s", i, address_type, bd_addr_to_str(address)); 1351 if (address_type == BD_ADDR_TYPE_LE_PUBLIC && memcmp(address, setup->sm_peer_address, 6) == 0){ 1352 log_info("sm: device found for public address, updating"); 1353 le_db_index = i; 1354 break; 1355 } |
1348 } 1349 } | 1356 } 1357 } |
1350 } | |
1351 | 1358 |
1352 // if not found, add to db 1353 if (le_db_index < 0) { 1354 le_db_index = le_device_db_add(setup->sm_peer_addr_type, setup->sm_peer_address, setup->sm_peer_irk); 1355 } | 1359 // if not found, add to db 1360 if (le_db_index < 0) { 1361 le_db_index = le_device_db_add(setup->sm_peer_addr_type, setup->sm_peer_address, setup->sm_peer_irk); 1362 } |
1356 | 1363 |
1357 if (le_db_index >= 0){ | 1364 if (le_db_index >= 0){ |
1358 | 1365 |
1359 sm_notify_client_index(SM_EVENT_IDENTITY_CREATED, sm_conn->sm_handle, setup->sm_peer_addr_type, setup->sm_peer_address, le_db_index); | 1366 sm_notify_client_index(SM_EVENT_IDENTITY_CREATED, sm_conn->sm_handle, setup->sm_peer_addr_type, setup->sm_peer_address, le_db_index); |
1360 1361#ifdef ENABLE_LE_SIGNED_WRITE | 1367 1368#ifdef ENABLE_LE_SIGNED_WRITE |
1362 // store local CSRK 1363 if (setup->sm_key_distribution_send_set & SM_KEYDIST_FLAG_SIGNING_IDENTIFICATION){ 1364 log_info("sm: store local CSRK"); 1365 le_device_db_local_csrk_set(le_db_index, setup->sm_local_csrk); 1366 le_device_db_local_counter_set(le_db_index, 0); 1367 } | 1369 // store local CSRK 1370 if (setup->sm_key_distribution_send_set & SM_KEYDIST_FLAG_SIGNING_IDENTIFICATION){ 1371 log_info("sm: store local CSRK"); 1372 le_device_db_local_csrk_set(le_db_index, setup->sm_local_csrk); 1373 le_device_db_local_counter_set(le_db_index, 0); 1374 } |
1368 | 1375 |
1369 // store remote CSRK 1370 if (setup->sm_key_distribution_received_set & SM_KEYDIST_FLAG_SIGNING_IDENTIFICATION){ 1371 log_info("sm: store remote CSRK"); 1372 le_device_db_remote_csrk_set(le_db_index, setup->sm_peer_csrk); 1373 le_device_db_remote_counter_set(le_db_index, 0); 1374 } | 1376 // store remote CSRK 1377 if (setup->sm_key_distribution_received_set & SM_KEYDIST_FLAG_SIGNING_IDENTIFICATION){ 1378 log_info("sm: store remote CSRK"); 1379 le_device_db_remote_csrk_set(le_db_index, setup->sm_peer_csrk); 1380 le_device_db_remote_counter_set(le_db_index, 0); 1381 } |
1375#endif | 1382#endif |
1376 // store encryption information for secure connections: LTK generated by ECDH 1377 if (setup->sm_use_secure_connections){ 1378 log_info("sm: store SC LTK (key size %u, authenticated %u)", sm_conn->sm_actual_encryption_key_size, sm_conn->sm_connection_authenticated); 1379 uint8_t zero_rand[8]; 1380 memset(zero_rand, 0, 8); 1381 le_device_db_encryption_set(le_db_index, 0, zero_rand, setup->sm_ltk, sm_conn->sm_actual_encryption_key_size, 1382 sm_conn->sm_connection_authenticated, sm_conn->sm_connection_authorization_state == AUTHORIZATION_GRANTED); 1383 } | 1383 // store encryption information for secure connections: LTK generated by ECDH 1384 if (setup->sm_use_secure_connections){ 1385 log_info("sm: store SC LTK (key size %u, authenticated %u)", sm_conn->sm_actual_encryption_key_size, sm_conn->sm_connection_authenticated); 1386 uint8_t zero_rand[8]; 1387 memset(zero_rand, 0, 8); 1388 le_device_db_encryption_set(le_db_index, 0, zero_rand, setup->sm_ltk, sm_conn->sm_actual_encryption_key_size, 1389 sm_conn->sm_connection_authenticated, sm_conn->sm_connection_authorization_state == AUTHORIZATION_GRANTED); 1390 } |
1384 | 1391 |
1385 // store encryption information for legacy pairing: peer LTK, EDIV, RAND 1386 else if ( (setup->sm_key_distribution_received_set & SM_KEYDIST_FLAG_ENCRYPTION_INFORMATION) 1387 && (setup->sm_key_distribution_received_set & SM_KEYDIST_FLAG_MASTER_IDENTIFICATION )){ 1388 log_info("sm: set encryption information (key size %u, authenticated %u)", sm_conn->sm_actual_encryption_key_size, sm_conn->sm_connection_authenticated); 1389 le_device_db_encryption_set(le_db_index, setup->sm_peer_ediv, setup->sm_peer_rand, setup->sm_peer_ltk, 1390 sm_conn->sm_actual_encryption_key_size, sm_conn->sm_connection_authenticated, sm_conn->sm_connection_authorization_state == AUTHORIZATION_GRANTED); | 1392 // store encryption information for legacy pairing: peer LTK, EDIV, RAND 1393 else if ( (setup->sm_key_distribution_received_set & SM_KEYDIST_FLAG_ENCRYPTION_INFORMATION) 1394 && (setup->sm_key_distribution_received_set & SM_KEYDIST_FLAG_MASTER_IDENTIFICATION )){ 1395 log_info("sm: set encryption information (key size %u, authenticated %u)", sm_conn->sm_actual_encryption_key_size, sm_conn->sm_connection_authenticated); 1396 le_device_db_encryption_set(le_db_index, setup->sm_peer_ediv, setup->sm_peer_rand, setup->sm_peer_ltk, 1397 sm_conn->sm_actual_encryption_key_size, sm_conn->sm_connection_authenticated, sm_conn->sm_connection_authorization_state == AUTHORIZATION_GRANTED); |
1391 | 1398 |
1399 } |
|
1392 } | 1400 } |
1401 } else { 1402 log_info("Ignoring received keys, bonding not enabled"); |
|
1393 } 1394 1395 // keep le_db_index 1396 sm_conn->sm_le_db_index = le_db_index; 1397} 1398 1399static void sm_pairing_error(sm_connection_t * sm_conn, uint8_t reason){ 1400 setup->sm_pairing_failed_reason = reason; --- 2861 unchanged lines hidden --- | 1403 } 1404 1405 // keep le_db_index 1406 sm_conn->sm_le_db_index = le_db_index; 1407} 1408 1409static void sm_pairing_error(sm_connection_t * sm_conn, uint8_t reason){ 1410 setup->sm_pairing_failed_reason = reason; --- 2861 unchanged lines hidden --- |