Skip to content

Commit 92df57b

Browse files
author
Mika Leppänen
committed
Added key data access functions to key storage
1 parent 5972bc3 commit 92df57b

File tree

15 files changed

+666
-112
lines changed

15 files changed

+666
-112
lines changed

source/6LoWPAN/ws/ws_pae_auth.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -493,13 +493,13 @@ static void ws_pae_auth_kmp_api_finished_indication(kmp_api_t *kmp, kmp_result_e
493493
// After EAP-TLS start 4WH towards supplicant
494494
type = IEEE_802_11_4WH;
495495
// Insert GTK0
496-
supp_entry->sec_keys.gtk_set_index = 0;
496+
sec_prot_keys_gtk_insert_index_set(supp_entry->sec_keys.gtks, 0);
497497
tr_debug("PAE start 4WH, eui-64: %s", trace_array(kmp_address_eui_64_get(supp_entry->addr), 8));
498498
} else if (type == IEEE_802_11_4WH) {
499499
// After 4WH start GKH towards supplicant
500500
type = IEEE_802_11_GKH;
501501
// Insert GTK1
502-
supp_entry->sec_keys.gtk_set_index = 1;
502+
sec_prot_keys_gtk_insert_index_set(supp_entry->sec_keys.gtks, 1);
503503
tr_debug("PAE start GKH, eui-64: %s", trace_array(kmp_address_eui_64_get(supp_entry->addr), 8));
504504
} else if (type == IEEE_802_11_GKH) {
505505
tr_debug("PAE authenticated, eui-64: %s", trace_array(kmp_address_eui_64_get(supp_entry->addr), 8));

source/6LoWPAN/ws/ws_pae_controller.c

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,20 @@ static NS_LIST_DEFINE(pae_controller_list, pae_controller_t, link);
5858

5959
static void ws_pae_controller_test_keys_set(sec_prot_gtk_keys_t *gtks)
6060
{
61+
uint8_t gtk[2][GTK_LEN];
62+
6163
// Test data
62-
for (int i = 0; i < 16; i++) {
63-
gtks->gtk[0].key[i] = 0xcf - i;
64-
gtks->gtk[1].key[i] = 0xef - i;
64+
for (int i = 0; i < GTK_LEN; i++) {
65+
gtk[0][i] = 0xcf - i;
66+
gtk[1][i] = 0xef - i;
6567
}
66-
gtks->gtk[0].set = 1;
67-
gtks->gtk[0].live = 1;
6868

69-
gtks->gtk[1].set = 1;
70-
gtks->gtk[1].live = 1;
69+
sec_prot_keys_gtk_set(gtks, 0, gtk[0]);
70+
sec_prot_keys_gtk_set(gtks, 1, gtk[1]);
71+
72+
sec_prot_keys_gtkl_set(gtks, 0xFF);
73+
74+
sec_prot_keys_gtk_insert_index_set(gtks, 0);
7175
}
7276

7377
int8_t ws_pae_controller_authenticate(protocol_interface_info_entry_t *interface_ptr)
@@ -93,7 +97,11 @@ int8_t ws_pae_controller_authenticate(protocol_interface_info_entry_t *interface
9397
if (ws_pae_supp_authenticate(controller->interface_ptr, controller->target_eui_64) > 0) {
9498
// Already authenticated
9599
ws_pae_controller_test_keys_set(&controller->gtks);
96-
controller->key_insert(controller->interface_ptr, 0, controller->gtks.gtk[0].key);
100+
101+
uint8_t index;
102+
uint8_t *gtk = sec_prot_keys_get_gtk_to_insert(&controller->gtks, &index);
103+
104+
controller->key_insert(controller->interface_ptr, index, gtk);
97105
controller->auth_completed(interface_ptr, true);
98106
}
99107

@@ -123,7 +131,10 @@ int8_t ws_pae_controller_authenticator_start(protocol_interface_info_entry_t *in
123131
controller->pae_delete = ws_pae_auth_delete;
124132
controller->pae_timer = ws_pae_auth_timer;
125133

126-
controller->key_insert(controller->interface_ptr, 0, controller->gtks.gtk[0].key);
134+
uint8_t index;
135+
uint8_t *gtk = sec_prot_keys_get_gtk_to_insert(&controller->gtks, &index);
136+
137+
controller->key_insert(controller->interface_ptr, index, gtk);
127138

128139
return 0;
129140
}

source/6LoWPAN/ws/ws_pae_supp.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -651,9 +651,13 @@ static void ws_pae_supp_kmp_api_finished_indication(kmp_api_t *kmp, kmp_result_e
651651

652652
sec_prot_keys_t *keys = sec_keys;
653653
// Key is to be inserted
654-
if (keys && keys->gtk_set_index >= 0) {
655-
pae_supp->key_insert(pae_supp->interface_ptr, keys->gtk_set_index, keys->gtks->gtk[keys->gtk_set_index].key);
656-
keys->gtk_set_index = -1;
654+
if (keys) {
655+
uint8_t gtk_index;
656+
uint8_t *gtk = sec_prot_keys_get_gtk_to_insert(keys->gtks, &gtk_index);
657+
if (gtk) {
658+
pae_supp->key_insert(pae_supp->interface_ptr, gtk_index, gtk);
659+
sec_prot_keys_gtk_insert_index_clear(keys->gtks);
660+
}
657661
}
658662

659663
if ((type == IEEE_802_11_4WH || type == IEEE_802_11_GKH) && result == KMP_RESULT_OK) {

source/Security/eapol/kde_helper.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,10 @@ uint8_t *kde_lifetime_write(uint8_t *ptr, uint32_t lifetime)
159159
return ptr;
160160
}
161161

162-
uint8_t *kde_gtkl_write(uint8_t *ptr, uint8_t gtkl0, uint8_t gtkl1, uint8_t gtkl2, uint8_t gtkl3)
162+
uint8_t *kde_gtkl_write(uint8_t *ptr, uint8_t gtkl)
163163
{
164164
ptr = kde_header_write(ptr, WISUN_OUI, KDE_GTKL, KDE_GTKL_LEN);
165-
*ptr++ = gtkl0 | (gtkl1 << 1) | (gtkl2 << 2) | (gtkl3 << 3);
165+
*ptr++ = gtkl;
166166

167167
return ptr;
168168
}
@@ -205,17 +205,14 @@ int8_t kde_lifetime_read(const uint8_t *ptr, uint16_t len, uint32_t *lifetime)
205205
return 0;
206206
}
207207

208-
int8_t kde_gtkl_read(const uint8_t *ptr, uint16_t len, uint8_t *gtkl0, uint8_t *gtkl1, uint8_t *gtkl2, uint8_t *gtkl3)
208+
int8_t kde_gtkl_read(const uint8_t *ptr, uint16_t len, uint8_t *gtkl)
209209
{
210210
ptr = kde_search(ptr, len, WISUN_OUI, KDE_GTKL, KDE_GTKL_LEN);
211211
if (ptr == NULL) {
212212
return -1;
213213
}
214214

215-
*gtkl0 = *ptr & 0x01;
216-
*gtkl1 = (*ptr >> 1) & 0x01;
217-
*gtkl2 = (*ptr >> 2) & 0x01;
218-
*gtkl3 = (*ptr >> 3) & 0x01;
215+
*gtkl = *ptr;
219216

220217
return 0;
221218
}

source/Security/eapol/kde_helper.h

Lines changed: 110 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,130 @@
1818
#ifndef KDE_HELPER_H_
1919
#define KDE_HELPER_H_
2020

21+
/*
22+
* EAPOL KDE helper functions
23+
*
24+
*/
25+
2126
#define KDE_GTK_LEN 6 + 2 + 16
2227
#define KDE_PMKID_LEN 6 + 16
2328
#define KDE_LIFETIME_LEN 6 + 4
2429
#define KDE_GTKL_LEN 6 + 1
2530

31+
/**
32+
* kde_padded_length_calc calculates padded length for kde (see IEEE 802.11 chapter 11.6.2 EAPOL-Key frames)
33+
*
34+
* \param kde_length length without padding
35+
*
36+
* \return padded length
37+
*/
2638
uint16_t kde_padded_length_calc(uint16_t kde_length);
39+
40+
/**
41+
* kde_padding_write writes padded bytes
42+
*
43+
* \param start_ptr first byte of the padding
44+
* \param end_ptr last bytes next byte
45+
*
46+
*/
2747
void kde_padding_write(uint8_t *start_ptr, uint8_t *end_ptr);
2848

49+
/**
50+
* kde_gtk_write writes GTK
51+
*
52+
* \param ptr pointer where to write
53+
* \param key_id key identifier (index)
54+
* \param gtk GTK
55+
*
56+
* return incremented write pointer
57+
*
58+
*/
2959
uint8_t *kde_gtk_write(uint8_t *ptr, uint8_t key_id, const uint8_t *gtk);
60+
61+
/**
62+
* kde_pmkid_write writes PMKID
63+
*
64+
* \param ptr pointer where to write
65+
* \param pmkid PMKID
66+
*
67+
* return incremented write pointer
68+
*
69+
*/
3070
uint8_t *kde_pmkid_write(uint8_t *ptr, const uint8_t *pmkid);
71+
72+
/**
73+
* kde_lifetime_write writes GTK lifetime
74+
*
75+
* \param ptr pointer where to write
76+
* \param lifetime GTK lifetime
77+
*
78+
* return incremented write pointer
79+
*
80+
*/
3181
uint8_t *kde_lifetime_write(uint8_t *ptr, uint32_t lifetime);
32-
uint8_t *kde_gtkl_write(uint8_t *ptr, uint8_t gtkl0, uint8_t gtkl1, uint8_t gtkl2, uint8_t gtkl3);
3382

83+
/**
84+
* kde_gtkl_write writes GTK liveness information
85+
*
86+
* \param ptr pointer where to write
87+
* \param gtkl GTK liveness bit field
88+
*
89+
* return incremented write pointer
90+
*
91+
*/
92+
uint8_t *kde_gtkl_write(uint8_t *ptr, uint8_t gtkl);
93+
94+
/**
95+
* kde_gtk_read reads GTK
96+
*
97+
* \param ptr pointer where to read
98+
* \param len length of the remaining data
99+
* \param key_id key identifier (index)
100+
* \param gtk GTK
101+
*
102+
* \return < 0 failure
103+
* \return >= 0 success
104+
*
105+
*/
34106
int8_t kde_gtk_read(const uint8_t *ptr, uint16_t len, uint8_t *key_id, uint8_t *gtk);
107+
108+
/**
109+
* kde_pmkid_read reads PMKID
110+
*
111+
* \param ptr pointer where to read
112+
* \param len length of the remaining data
113+
* \param pmkid PMKID
114+
*
115+
* \return < 0 failure
116+
* \return >= 0 success
117+
*
118+
*/
35119
int8_t kde_pmkid_read(const uint8_t *ptr, uint16_t len, uint8_t *pmkid);
120+
121+
/**
122+
* kde_lifetime_read reads GTK lifetime
123+
*
124+
* \param ptr pointer where to read
125+
* \param len length of the remaining data
126+
* \param lifetime GTK lifetime
127+
*
128+
* \return < 0 failure
129+
* \return >= 0 success
130+
*
131+
*/
36132
int8_t kde_lifetime_read(const uint8_t *ptr, uint16_t len, uint32_t *lifetime);
37-
int8_t kde_gtkl_read(const uint8_t *ptr, uint16_t len, uint8_t *gtkl0, uint8_t *gtkl1, uint8_t *gtkl2, uint8_t *gtkl3);
133+
134+
/**
135+
* kde_gtkl_read reads GTK liveness information
136+
*
137+
* \param ptr pointer where to read
138+
* \param len length of the remaining data
139+
* \param gtkl GTK liveness bit field
140+
*
141+
* \return < 0 failure
142+
* \return >= 0 success
143+
*
144+
*/
145+
int8_t kde_gtkl_read(const uint8_t *ptr, uint16_t len, uint8_t *gtkl);
38146

39147
#endif /* KDE_HELPER_H_ */

source/Security/protocols/fwh_sec_prot/auth_fwh_sec_prot.c

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ static void auth_fwh_sec_prot_state_machine(sec_prot_t *prot);
8787
static int8_t auth_fwh_sec_prot_message_send(sec_prot_t *prot, fwh_sec_prot_msg_e msg);
8888
static void auth_fwh_sec_prot_timer_timeout(sec_prot_t *prot, uint16_t ticks);
8989

90-
static int8_t auth_fwh_sec_prot_pmkid_generate(sec_prot_t *prot, uint8_t *pmkid);
9190
static int8_t auth_fwh_sec_prot_ptk_generate(sec_prot_t *prot, sec_prot_keys_t *sec_keys);
9291
static int8_t auth_fwh_sec_prot_mic_validate(sec_prot_t *prot);
9392

@@ -186,13 +185,13 @@ static fwh_sec_prot_msg_e auth_fwh_sec_prot_message_get(eapol_pdu_t *eapol_pdu,
186185
switch (key_mask) {
187186
case KEY_INFO_KEY_MIC:
188187
// Only accept message from supplicant with expected replay counter
189-
if (eapol_pdu->msg.key.replay_counter == sec_keys->pmk_key_replay_cnt) {
188+
if (eapol_pdu->msg.key.replay_counter == sec_prot_keys_pmk_replay_cnt_get(sec_keys)) {
190189
msg = FWH_MESSAGE_2;
191190
}
192191
break;
193192
case KEY_INFO_KEY_MIC | KEY_INFO_SECURED_KEY_FRAME:
194193
// Only accept message from supplicant with expected replay counter
195-
if (eapol_pdu->msg.key.replay_counter == sec_keys->pmk_key_replay_cnt) {
194+
if (eapol_pdu->msg.key.replay_counter == sec_prot_keys_pmk_replay_cnt_get(sec_keys)) {
196195
msg = FWH_MESSAGE_4;
197196
}
198197
break;
@@ -233,21 +232,24 @@ static int8_t auth_fwh_sec_prot_message_send(sec_prot_t *prot, fwh_sec_prot_msg_
233232
switch (msg) {
234233
case FWH_MESSAGE_1: {
235234
uint8_t pmkid[PMKID_LEN];
236-
if (auth_fwh_sec_prot_pmkid_generate(prot, pmkid) < 0) {
235+
if (sec_prot_lib_pmkid_generate(prot, pmkid, true) < 0) {
237236
ns_dyn_mem_free(kde_start);
238237
return -1;
239238
}
240239
kde_end = kde_pmkid_write(kde_end, pmkid);
241240
}
242241
break;
243242
case FWH_MESSAGE_3: {
244-
int8_t gtk_set_index = prot->sec_keys->gtk_set_index;
245-
sec_prot_gtk_keys_t *gtks = prot->sec_keys->gtks;
246-
if (gtk_set_index >= 0 && gtks->gtk[gtk_set_index].set && gtks->gtk[gtk_set_index].live) {
247-
kde_end = kde_gtk_write(kde_end, gtk_set_index, gtks->gtk[gtk_set_index].key);
243+
uint8_t gtk_index;
244+
uint8_t *gtk = sec_prot_keys_get_gtk_to_insert(prot->sec_keys->gtks, &gtk_index);
245+
if (gtk) {
246+
kde_end = kde_gtk_write(kde_end, gtk_index, gtk);
247+
248+
uint32_t gtk_lifetime = sec_prot_keys_gtk_lifetime_get(prot->sec_keys->gtks, gtk_index);
249+
kde_end = kde_lifetime_write(kde_end, gtk_lifetime);
248250
}
249-
kde_end = kde_lifetime_write(kde_end, GTK_DEFAULT_LIFETIME);
250-
kde_end = kde_gtkl_write(kde_end, gtks->gtk[0].live, gtks->gtk[1].live, gtks->gtk[2].live, gtks->gtk[3].live);
251+
uint8_t gtkl = sec_prot_keys_gtkl_get(prot->sec_keys->gtks);
252+
kde_end = kde_gtkl_write(kde_end, gtkl);
251253
kde_padding_write(kde_end, kde_start + kde_len);
252254
}
253255
break;
@@ -262,13 +264,15 @@ static int8_t auth_fwh_sec_prot_message_send(sec_prot_t *prot, fwh_sec_prot_msg_
262264

263265
switch (msg) {
264266
case FWH_MESSAGE_1:
265-
eapol_pdu.msg.key.replay_counter = ++prot->sec_keys->pmk_key_replay_cnt;
267+
sec_prot_keys_pmk_replay_cnt_increment(prot->sec_keys);
268+
eapol_pdu.msg.key.replay_counter = sec_prot_keys_pmk_replay_cnt_get(prot->sec_keys);
266269
eapol_pdu.msg.key.key_information.key_ack = true;
267270
eapol_pdu.msg.key.key_length = 32;
268271
eapol_pdu.msg.key.key_nonce = data->nonce;
269272
break;
270273
case FWH_MESSAGE_3:
271-
eapol_pdu.msg.key.replay_counter = ++prot->sec_keys->pmk_key_replay_cnt;
274+
sec_prot_keys_pmk_replay_cnt_increment(prot->sec_keys);
275+
eapol_pdu.msg.key.replay_counter = sec_prot_keys_pmk_replay_cnt_get(prot->sec_keys);
272276
eapol_pdu.msg.key.key_information.install = true;
273277
eapol_pdu.msg.key.key_information.key_ack = true;
274278
eapol_pdu.msg.key.key_information.key_mic = true;
@@ -317,6 +321,13 @@ static void auth_fwh_sec_prot_state_machine(sec_prot_t *prot)
317321
case FWH_STATE_CREATE_REQ:
318322
tr_debug("4WH start");
319323

324+
uint8_t *pmk = sec_prot_keys_pmk_get(prot->sec_keys);
325+
if (!pmk) { // If PMK is not set fails
326+
prot->create_conf(prot, SEC_RESULT_ERROR);
327+
sec_prot_state_set(prot, &data->common, FWH_STATE_FINISHED);
328+
return;
329+
}
330+
320331
// KMP-CREATE.confirm
321332
prot->create_conf(prot, SEC_RESULT_OK);
322333

@@ -394,15 +405,6 @@ static void auth_fwh_sec_prot_state_machine(sec_prot_t *prot)
394405
}
395406
}
396407

397-
static int8_t auth_fwh_sec_prot_pmkid_generate(sec_prot_t *prot, uint8_t *pmkid)
398-
{
399-
uint8_t local_eui64[8];
400-
uint8_t remote_eui64[8];
401-
prot->addr_get(prot, local_eui64, remote_eui64);
402-
403-
return sec_prot_lib_pmkid_calc(prot->sec_keys->pmk, local_eui64, remote_eui64, pmkid);
404-
}
405-
406408
static int8_t auth_fwh_sec_prot_ptk_generate(sec_prot_t *prot, sec_prot_keys_t *sec_keys)
407409
{
408410
fwh_sec_prot_int_t *data = fwh_sec_prot_get(prot);
@@ -417,7 +419,8 @@ static int8_t auth_fwh_sec_prot_ptk_generate(sec_prot_t *prot, sec_prot_keys_t *
417419
return 1;
418420
}
419421

420-
sec_prot_lib_ptk_calc(sec_keys->pmk, local_eui64, remote_eui64, data->nonce, remote_nonce, data->new_ptk);
422+
uint8_t *pmk = sec_prot_keys_pmk_get(sec_keys);
423+
sec_prot_lib_ptk_calc(pmk, local_eui64, remote_eui64, data->nonce, remote_nonce, data->new_ptk);
421424

422425
return 0;
423426
}

0 commit comments

Comments
 (0)