Skip to content

Commit f701d39

Browse files
author
Mika Leppänen
committed
Corrected PAN ID checking for NVM keys and MAC key remove on discovery
When NVM keys were used, PAN ID was not checked correctly. Now keys are used only if PAN ID matches, and if EAPOL is initiated using different PAN ID, previous keys are removed. Modified functionality to remove only MAC keys when discovery is started. EAPOL keys will remain valid and will be used on next bootstart if same PAN ID is tried.
1 parent 1eda5e3 commit f701d39

File tree

6 files changed

+74
-28
lines changed

6 files changed

+74
-28
lines changed

source/6LoWPAN/ws/ws_bootstrap.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2558,12 +2558,10 @@ void ws_bootstrap_trickle_timer(protocol_interface_info_entry_t *cur, uint16_t t
25582558
// send PAN Configuration solicit
25592559
if (cur->ws_info->pas_requests > PCS_MAX) {
25602560
// if MAX PCS sent restart discovery
2561-
uint8_t empty_key[32];
2562-
memset(empty_key, 0, 32);
2563-
tr_debug("Restart???");
2564-
//Clear Key's
2565-
tr_debug("Remove not working GTK's");
2566-
ws_pae_controller_gtk_hash_update(cur, empty_key);
2561+
2562+
// Remove network keys from MAC
2563+
ws_pae_controller_nw_keys_remove(cur);
2564+
25672565
ws_bootstrap_event_discovery_start(cur);
25682566
return;
25692567
}

source/6LoWPAN/ws/ws_pae_controller.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,28 @@ int8_t ws_pae_controller_nw_key_index_update(protocol_interface_info_entry_t *in
393393
return 0;
394394
}
395395

396+
void ws_pae_controller_nw_keys_remove(protocol_interface_info_entry_t *interface_ptr)
397+
{
398+
pae_controller_t *controller = ws_pae_controller_get(interface_ptr);
399+
if (!controller) {
400+
return;
401+
}
402+
403+
tr_info("NW keys remove");
404+
405+
if (controller->nw_keys_init_done) {
406+
nw_key_t *nw_key = controller->nw_key;
407+
for (uint8_t i = 0; i < GTK_NUM; i++) {
408+
// Deletes the key if it is set
409+
if (!sec_prot_keys_gtk_hash_empty(nw_key[i].hash)) {
410+
tr_info("NW key remove: %i, slot: %i", i, nw_key[i].slot);
411+
controller->nw_key_insert(interface_ptr, nw_key[i].slot, i, NULL);
412+
memset(&nw_key[i], 0, sizeof(nw_key_t));
413+
}
414+
}
415+
}
416+
}
417+
396418
static void ws_pae_controller_nw_key_index_check_and_set(protocol_interface_info_entry_t *interface_ptr, uint8_t index)
397419
{
398420
pae_controller_t *controller = ws_pae_controller_get(interface_ptr);

source/6LoWPAN/ws/ws_pae_controller.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,14 @@ int8_t ws_pae_controller_gtk_hash_update(protocol_interface_info_entry_t *interf
338338
*/
339339
int8_t ws_pae_controller_nw_key_index_update(protocol_interface_info_entry_t *interface_ptr, uint8_t index);
340340

341+
/**
342+
* ws_pae_controller_nw_keys_remove remove network keys
343+
*
344+
* \param interface_ptr interface
345+
*
346+
*/
347+
void ws_pae_controller_nw_keys_remove(protocol_interface_info_entry_t *interface_ptr);
348+
341349
/**
342350
* ws_pae_controller_nw_key_insert network key insert callback
343351
*

source/6LoWPAN/ws/ws_pae_supp.c

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ const char *KEYS_FILE = "pae_keys";
7979
typedef struct {
8080
char network_name[33]; /**< Network name for keys */
8181
sec_prot_gtk_keys_t *gtks; /**< Link to GTKs */
82-
uint16_t pan_id; /**< PAN ID for keys */
82+
uint16_t new_pan_id; /**< new PAN ID indicated by bootstrap */
83+
uint16_t key_pan_id; /**< PAN ID for keys */
8384
bool updated : 1; /**< Network info has been updated */
8485
} sec_prot_keys_nw_info_t;
8586

@@ -159,13 +160,6 @@ int8_t ws_pae_supp_authenticate(protocol_interface_info_entry_t *interface_ptr,
159160
return -1;
160161
}
161162

162-
if (!dest_eui_64) {
163-
pae_supp->sec_keys_nw_info.pan_id = dest_pan_id;
164-
if (pae_supp->nw_keys_used_cnt > 1) {
165-
pae_supp->nw_keys_used_cnt = 1;
166-
}
167-
}
168-
169163
if (ws_pae_supp_nw_keys_valid_check(pae_supp, dest_pan_id) >= 0) {
170164
pae_supp->auth_completed(interface_ptr, true);
171165
return 0;
@@ -174,6 +168,16 @@ int8_t ws_pae_supp_authenticate(protocol_interface_info_entry_t *interface_ptr,
174168
// Delete GTKs
175169
sec_prot_keys_gtks_init(pae_supp->sec_keys_nw_info.gtks);
176170

171+
/* PAN ID has changed, delete key data associated with border router
172+
i.e PMK, PTK, EA-IE data (border router EUI-64) */
173+
if (pae_supp->sec_keys_nw_info.key_pan_id != 0xFFFF && pae_supp->sec_keys_nw_info.key_pan_id != dest_pan_id) {
174+
sec_prot_keys_pmk_delete(&pae_supp->entry.sec_keys);
175+
sec_prot_keys_ptk_delete(&pae_supp->entry.sec_keys);
176+
sec_prot_keys_ptk_eui_64_delete(&pae_supp->entry.sec_keys);
177+
}
178+
179+
pae_supp->sec_keys_nw_info.key_pan_id = dest_pan_id;
180+
177181
// Prepare to receive new border router address
178182
pae_supp->new_br_eui_64_set = false;
179183

@@ -203,8 +207,8 @@ int8_t ws_pae_supp_nw_info_set(protocol_interface_info_entry_t *interface_ptr, u
203207
}
204208

205209
// PAN ID has been modified
206-
if (pan_id != 0xffff && pan_id != pae_supp->sec_keys_nw_info.pan_id) {
207-
pae_supp->sec_keys_nw_info.pan_id = pan_id;
210+
if (pan_id != 0xffff && pan_id != pae_supp->sec_keys_nw_info.new_pan_id) {
211+
pae_supp->sec_keys_nw_info.new_pan_id = pan_id;
208212
pae_supp->sec_keys_nw_info.updated = true;
209213
}
210214

@@ -350,7 +354,7 @@ static int8_t ws_pae_supp_nvm_nw_info_write(pae_supp_t *pae_supp)
350354
nvm_tlv_list_t tlv_list;
351355
ns_list_init(&tlv_list);
352356

353-
nvm_tlv_entry_t *tlv_entry = ws_pae_nvm_store_nw_info_tlv_create(pae_supp->sec_keys_nw_info.pan_id,
357+
nvm_tlv_entry_t *tlv_entry = ws_pae_nvm_store_nw_info_tlv_create(pae_supp->sec_keys_nw_info.key_pan_id,
354358
pae_supp->sec_keys_nw_info.network_name,
355359
&pae_supp->gtks);
356360
ns_list_add_to_end(&tlv_list, tlv_entry);
@@ -370,7 +374,7 @@ static int8_t ws_pae_supp_nvm_nw_info_read(pae_supp_t *pae_supp)
370374
ws_pae_nvm_store_tlv_file_read(NW_INFO_FILE, &tlv_list);
371375

372376
ns_list_foreach_safe(nvm_tlv_entry_t, entry, &tlv_list) {
373-
ws_pae_nvm_store_nw_info_tlv_read(entry, &pae_supp->sec_keys_nw_info.pan_id,
377+
ws_pae_nvm_store_nw_info_tlv_read(entry, &pae_supp->sec_keys_nw_info.key_pan_id,
374378
pae_supp->sec_keys_nw_info.network_name,
375379
&pae_supp->gtks);
376380
ns_list_remove(&tlv_list, entry);
@@ -465,21 +469,18 @@ static int8_t ws_pae_supp_nw_keys_valid_check(pae_supp_t *pae_supp, uint16_t pan
465469
return -1;
466470
}
467471

468-
// First attempt to authenticate, checks if keys exists
469-
if (pae_supp->nw_keys_used_cnt == 0 && pan_id == pae_supp->sec_keys_nw_info.pan_id) {
472+
/* Checks if keys matches to PAN ID, and call inserts function that will update the
473+
network keys as needed */
474+
if (pan_id == pae_supp->sec_keys_nw_info.key_pan_id) {
475+
tr_debug("Existing keys used, counter %i", pae_supp->nw_keys_used_cnt);
470476
if (pae_supp->nw_key_insert(pae_supp->interface_ptr, pae_supp->sec_keys_nw_info.gtks) >= 0) {
471477
tr_debug("Keys inserted");
472-
pae_supp->nw_keys_used_cnt++;
473-
return 0;
474478
}
475-
}
476-
477-
if (pae_supp->nw_keys_used_cnt == 0) {
478-
return -1;
479-
} else {
480-
tr_debug("Existing keys used, counter %i", pae_supp->nw_keys_used_cnt);
481479
pae_supp->nw_keys_used_cnt++;
482480
return 0;
481+
} else {
482+
pae_supp->nw_keys_used_cnt = 0;
483+
return -1;
483484
}
484485
}
485486

@@ -492,6 +493,8 @@ static void ws_pae_supp_keys_nw_info_init(sec_prot_keys_nw_info_t *sec_keys_nw_i
492493
memset(sec_keys_nw_info, 0, sizeof(sec_prot_keys_nw_info_t));
493494

494495
sec_keys_nw_info->gtks = gtks;
496+
sec_keys_nw_info->new_pan_id = 0xFFFF;
497+
sec_keys_nw_info->key_pan_id = 0xFFFF;
495498
sec_keys_nw_info->updated = false;
496499
}
497500

source/Security/protocols/sec_prot_keys.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,13 @@ uint8_t *sec_prot_keys_ptk_eui_64_get(sec_prot_keys_t *sec_keys)
254254
return sec_keys->ptk_eui_64;
255255
}
256256

257+
void sec_prot_keys_ptk_eui_64_delete(sec_prot_keys_t *sec_keys)
258+
{
259+
memset(sec_keys->ptk_eui_64, 0, 8);
260+
sec_keys->ptk_eui_64_set = false;
261+
sec_keys->updated = true;
262+
}
263+
257264
bool sec_prot_keys_ptk_lifetime_decrement(sec_prot_keys_t *sec_keys, uint32_t default_lifetime, uint8_t seconds)
258265
{
259266
if (!sec_keys->ptk_set) {

source/Security/protocols/sec_prot_keys.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,14 @@ void sec_prot_keys_ptk_eui_64_write(sec_prot_keys_t *sec_keys, const uint8_t *eu
331331
*/
332332
uint8_t *sec_prot_keys_ptk_eui_64_get(sec_prot_keys_t *sec_keys);
333333

334+
/**
335+
* sec_prot_keys_ptk_eui_64_delete deletes PTK EUI-64
336+
*
337+
* \param sec_keys security keys
338+
*
339+
*/
340+
void sec_prot_keys_ptk_eui_64_delete(sec_prot_keys_t *sec_keys);
341+
334342
/**
335343
* sec_prot_keys_ptk_lifetime_decrement decrements PTK lifetime
336344
*

0 commit comments

Comments
 (0)