Skip to content

Commit 34d2f15

Browse files
author
Mika Leppänen
committed
Corrected defects and coding style
1 parent 88641c1 commit 34d2f15

File tree

9 files changed

+68
-69
lines changed

9 files changed

+68
-69
lines changed

source/6LoWPAN/ws/ws_pae_auth.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -554,10 +554,10 @@ static kmp_api_t *ws_pae_auth_kmp_create_and_start(kmp_service_t *service, kmp_t
554554
}
555555

556556
kmp_api_cb_register(kmp,
557-
ws_pae_auth_kmp_api_create_confirm,
558-
ws_pae_auth_kmp_api_create_indication,
559-
ws_pae_auth_kmp_api_finished_indication,
560-
ws_pae_auth_kmp_api_finished);
557+
ws_pae_auth_kmp_api_create_confirm,
558+
ws_pae_auth_kmp_api_create_indication,
559+
ws_pae_auth_kmp_api_finished_indication,
560+
ws_pae_auth_kmp_api_finished);
561561

562562
kmp_api_data_set(kmp, supp_entry);
563563

source/Security/protocols/eap_tls_sec_prot/auth_eap_tls_sec_prot.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,16 @@ typedef enum {
5656
typedef struct {
5757
sec_prot_common_t common; /**< Common data */
5858
sec_prot_t *tls_prot; /**< TLS security protocol */
59-
uint8_t eap_id_seq; /**< EAP sequence */
6059
eapol_pdu_t recv_eapol_pdu; /**< Received EAPOL PDU */
61-
uint8_t eap_code; /**< Received EAP code */
62-
uint8_t eap_type; /**< Received EAP type */
6360
tls_data_t tls_send; /**< EAP-TLS send buffer */
6461
tls_data_t tls_recv; /**< EAP-TLS receive buffer */
62+
uint8_t eap_id_seq; /**< EAP sequence */
63+
uint8_t eap_code; /**< Received EAP code */
64+
uint8_t eap_type; /**< Received EAP type */
6565
int8_t tls_result; /**< Result of TLS operation */
66-
bool wait_tls; /**< Wait TLS (ECC calculation) before sending EAP-TLS message */
67-
bool tls_ongoing; /**< TLS handshake is ongoing */
68-
bool send_pending; /**< TLS data is not yet send to network */
66+
bool wait_tls: 1; /**< Wait TLS (ECC calculation) before sending EAP-TLS message */
67+
bool tls_ongoing: 1; /**< TLS handshake is ongoing */
68+
bool send_pending: 1; /**< TLS data is not yet send to network */
6969
} eap_tls_sec_prot_int_t;
7070

7171
static const trickle_params_t eap_tls_trickle_params = {
@@ -194,8 +194,8 @@ static int8_t auth_eap_tls_sec_prot_message_handle(sec_prot_t *prot)
194194
}
195195

196196
tr_debug("recv EAP %s type %s id %i flags %x len %i", eap_msg_trace[data->eap_code - 1],
197-
data->eap_type == EAP_IDENTITY ? "IDENTITY" : "TLS", data->recv_eapol_pdu.msg.eap.id_seq,
198-
length >= 6 ? data_ptr[0] : 0, length);
197+
data->eap_type == EAP_IDENTITY ? "IDENTITY" : "TLS", data->recv_eapol_pdu.msg.eap.id_seq,
198+
length >= 6 ? data_ptr[0] : 0, length);
199199

200200
if (data->eap_type == EAP_IDENTITY) {
201201
return EAP_TLS_MSG_IDENTITY;
@@ -342,7 +342,7 @@ static void auth_eap_tls_sec_prot_state_machine(sec_prot_t *prot)
342342
auth_eap_tls_sec_prot_seq_id_update(prot);
343343

344344
// Sends EAP request, Identity
345-
auth_eap_tls_sec_prot_message_send(prot, EAP_REQ , EAP_IDENTITY, EAP_TLS_EXCHANGE_NONE);
345+
auth_eap_tls_sec_prot_message_send(prot, EAP_REQ, EAP_IDENTITY, EAP_TLS_EXCHANGE_NONE);
346346

347347
// Start trickle timer to re-send if no response
348348
sec_prot_timer_trickle_start(&data->common, &eap_tls_trickle_params);
@@ -424,16 +424,16 @@ static void auth_eap_tls_sec_prot_state_machine(sec_prot_t *prot)
424424
data->wait_tls = true;
425425
}
426426
}
427-
// All fragments send for a message, no new fragment received
428427
} else if (result == EAP_TLS_MSG_SEND_DONE) {
428+
// All fragments send for a message, no new fragment received
429429
eap_tls_sec_prot_lib_message_free(&data->tls_send);
430430
}
431431
// Wait TLS to process the received message
432432
if (data->wait_tls) {
433433
return;
434434
}
435-
// Call from TLS
436435
} else {
436+
// Call from TLS
437437
data->wait_tls = false;
438438
}
439439

@@ -446,8 +446,8 @@ static void auth_eap_tls_sec_prot_state_machine(sec_prot_t *prot)
446446

447447
// Start trickle timer to re-send if no response
448448
sec_prot_timer_trickle_start(&data->common, &eap_tls_trickle_params);
449-
// TLS done, indicate success to peer
450449
} else {
450+
// TLS done, indicate success to peer
451451
if (data->tls_result == EAP_TLS_RESULT_HANDSHAKE_OVER) {
452452
// Sends EAP success
453453
auth_eap_tls_sec_prot_message_send(prot, EAP_SUCCESS, 0, EAP_TLS_EXCHANGE_NONE);

source/Security/protocols/eap_tls_sec_prot/eap_tls_sec_prot_lib.c

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "ns_list.h"
2222
#include "ns_trace.h"
2323
#include "nsdynmemLIB.h"
24+
#include "common_functions.h"
2425
#include "fhss_config.h"
2526
#include "NWK_INTERFACE/Include/protocol.h"
2627
#include "6LoWPAN/ws/ws_config.h"
@@ -46,11 +47,9 @@ const uint8_t eap_msg_trace[4][10] = {"REQ", "RESPONSE", "SUCCESS", "FAILURE"};
4647

4748
int8_t eap_tls_sec_prot_lib_message_allocate(tls_data_t *data, uint8_t head_len, uint16_t len)
4849
{
49-
if (data->data) {
50-
ns_dyn_mem_free(data->data);
51-
}
50+
ns_dyn_mem_free(data->data);
5251

53-
data->data = ns_dyn_mem_alloc(head_len + len);
52+
data->data = ns_dyn_mem_temporary_alloc(head_len + len);
5453
if (!data->data) {
5554
return -1;
5655
}
@@ -62,9 +61,7 @@ int8_t eap_tls_sec_prot_lib_message_allocate(tls_data_t *data, uint8_t head_len,
6261

6362
void eap_tls_sec_prot_lib_message_free(tls_data_t *data)
6463
{
65-
if (data->data) {
66-
ns_dyn_mem_free(data->data);
67-
}
64+
ns_dyn_mem_free(data->data);
6865
data->handled_len = 0;
6966
data->data = 0;
7067
data->total_len = 0;
@@ -84,26 +81,28 @@ int8_t eap_tls_sec_prot_lib_message_handle(uint8_t *data, uint16_t length, bool
8481
// EAP-TLS start
8582
if (data[0] & EAP_TLS_START) {
8683
result = EAP_TLS_MSG_START;
87-
// More fragments
8884
} else if (data[0] & EAP_TLS_MORE_FRAGMENTS) {
85+
// More fragments
8986
eap_tls_sec_prot_lib_message_allocate(tls_send, TLS_HEAD_LEN, 0);
9087

9188
// Handles the length field
9289
if (data[0] & EAP_TLS_FRAGMENT_LENGTH) {
9390
if (length < 5) {
9491
return EAP_TLS_MSG_ERROR;
9592
}
96-
uint32_t len = (data[3] << 8) | (data[4] & 0xff);
93+
94+
uint32_t len = common_read_32_bit(&data[1]);
95+
9796
//For first fragment allocates data for incoming TLS packet
9897
if (!tls_recv->data) {
9998
eap_tls_sec_prot_lib_message_allocate(tls_recv, 0, len);
10099
}
101100
length -= 4;
102101
data += 4;
103102
}
104-
// Last (or only) fragment or fragment acknowledge
105103
} else if (data[0] == 0) {
106-
// If sending data updates acknowledged fragments
104+
// Last (or only) fragment or fragment acknowledge. If sending data
105+
// updates acknowledged fragments.
107106
if (new_seq_id && eap_tls_sec_prot_lib_ack_update(tls_send)) {
108107
// All send, free data
109108
eap_tls_sec_prot_lib_message_allocate(tls_send, TLS_HEAD_LEN, 0);
@@ -143,7 +142,7 @@ uint8_t *eap_tls_sec_prot_lib_message_build(uint8_t eap_code, uint8_t eap_type,
143142
}
144143

145144
tr_debug("send EAP %s type %s id %i flags %x len %i", eap_msg_trace[eap_code - 1],
146-
eap_type == EAP_IDENTITY ? "IDENTITY" : "TLS", eap_id_seq, flags, eap_len);
145+
eap_type == EAP_IDENTITY ? "IDENTITY" : "TLS", eap_id_seq, flags, eap_len);
147146

148147
eapol_pdu_t eapol_pdu;
149148

@@ -166,8 +165,8 @@ static int8_t eap_tls_sec_prot_lib_ack_update(tls_data_t *tls)
166165
}
167166

168167
if (tls->handled_len + TLS_FRAGMENT_LEN < tls->total_len) {
169-
tls->handled_len += TLS_FRAGMENT_LEN;
170-
return false;
168+
tls->handled_len += TLS_FRAGMENT_LEN;
169+
return false;
171170
}
172171

173172
tls->handled_len = tls->total_len;
@@ -208,10 +207,7 @@ static uint8_t *eap_tls_sec_prot_lib_fragment_write(uint8_t *data, uint16_t tota
208207
*message_len += 4;
209208
*flags |= EAP_TLS_MORE_FRAGMENTS | EAP_TLS_FRAGMENT_LENGTH;
210209
data_begin[0] = *flags;
211-
data_begin[1] = 0x00;
212-
data_begin[2] = 0x00;
213-
data_begin[3] = total_len >> 8;
214-
data_begin[4] = total_len & 0x00FF;
210+
common_write_32_bit(total_len, &data_begin[1]);
215211
} else {
216212
*flags |= EAP_TLS_MORE_FRAGMENTS;
217213
data_begin[0] = *flags;

source/Security/protocols/eap_tls_sec_prot/eap_tls_sec_prot_lib.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ typedef enum {
3030
} eap_tls_sec_prot_tls_exchange_e;
3131

3232
typedef enum {
33-
EAP_TLS_MSG_IDENTITY = 0,
34-
EAP_TLS_MSG_START,
35-
EAP_TLS_MSG_CONTINUE,
36-
EAP_TLS_MSG_SEND_DONE,
37-
EAP_TLS_MSG_RECEIVE_DONE,
38-
EAP_TLS_MSG_ERROR
33+
EAP_TLS_MSG_IDENTITY = 0,
34+
EAP_TLS_MSG_START,
35+
EAP_TLS_MSG_CONTINUE,
36+
EAP_TLS_MSG_SEND_DONE,
37+
EAP_TLS_MSG_RECEIVE_DONE,
38+
EAP_TLS_MSG_ERROR
3939
} eap_tls_sec_prot_msg_e;
4040

4141
typedef enum {

source/Security/protocols/eap_tls_sec_prot/supp_eap_tls_sec_prot.c

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,16 @@ typedef enum {
5656
typedef struct {
5757
sec_prot_common_t common; /**< Common data */
5858
sec_prot_t *tls_prot; /**< TLS security protocol */
59-
uint8_t eap_id_seq; /**< EAP sequence */
6059
eapol_pdu_t recv_eapol_pdu; /**< Received EAPOL PDU */
61-
uint8_t eap_code; /**< Received EAP code */
62-
uint8_t eap_type; /**< Received EAP type */
6360
tls_data_t tls_send; /**< EAP-TLS send buffer */
6461
tls_data_t tls_recv; /**< EAP-TLS receive buffer */
62+
uint8_t eap_id_seq; /**< EAP sequence */
63+
uint8_t eap_code; /**< Received EAP code */
64+
uint8_t eap_type; /**< Received EAP type */
6565
int8_t tls_result; /**< Result of TLS operation */
66-
bool wait_tls; /**< Wait TLS (ECC calculation) before sending EAP-TLS message */
67-
bool tls_ongoing; /**< TLS handshake is ongoing */
68-
bool send_pending; /**< TLS data is not yet send to network */
66+
bool wait_tls: 1; /**< Wait TLS (ECC calculation) before sending EAP-TLS message */
67+
bool tls_ongoing: 1; /**< TLS handshake is ongoing */
68+
bool send_pending: 1; /**< TLS data is not yet send to network */
6969
} eap_tls_sec_prot_int_t;
7070

7171
static const trickle_params_t eap_tls_trickle_params = {
@@ -190,8 +190,8 @@ static int8_t supp_eap_tls_sec_prot_message_handle(sec_prot_t *prot)
190190
}
191191

192192
tr_debug("recv EAP %s type %s id %i flags %x len %i", eap_msg_trace[data->eap_code - 1],
193-
data->eap_type == EAP_IDENTITY ? "IDENTITY" : "TLS", data->recv_eapol_pdu.msg.eap.id_seq,
194-
length >= 6 ? data_ptr[0] : 0, length);
193+
data->eap_type == EAP_IDENTITY ? "IDENTITY" : "TLS", data->recv_eapol_pdu.msg.eap.id_seq,
194+
length >= 6 ? data_ptr[0] : 0, length);
195195

196196
if (data->eap_type == EAP_IDENTITY) {
197197
return EAP_TLS_MSG_IDENTITY;
@@ -429,9 +429,8 @@ static void supp_eap_tls_sec_prot_state_machine(sec_prot_t *prot)
429429
sec_prot_result_set(&data->common, SEC_RESULT_ERROR);
430430
sec_prot_state_set(prot, &data->common, EAP_TLS_STATE_FINISH);
431431
return;
432-
// EAP request
433432
} else if (data->eap_code == EAP_REQ) {
434-
// Handle EAP request, TLS EAP
433+
// EAP request, handle EAP request, TLS EAP
435434
result = supp_eap_tls_sec_prot_message_handle(prot);
436435
if (result == EAP_TLS_MSG_ERROR) {
437436
return;
@@ -454,8 +453,8 @@ static void supp_eap_tls_sec_prot_state_machine(sec_prot_t *prot)
454453
if (data->wait_tls) {
455454
return;
456455
}
457-
// Call from TLS
458456
} else {
457+
// Call from TLS
459458
data->wait_tls = false;
460459
if (!data->tls_send.data) {
461460
// If no more data send response, TLS EAP (empty)

source/Security/protocols/sec_prot_keys.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@
4343
typedef struct {
4444
uint8_t key[GTK_LEN]; /**< Group Transient Key (128 bits) */
4545
uint32_t lifetime; /**< Lifetime is seconds */
46-
bool set; /**< Group Transient Key set (valid value) */
47-
bool live; /**< Group Transient Key live (as indicated by authenticator) */
48-
bool hash; /**< Group Transient Key matches to hash */
46+
bool set: 1; /**< Group Transient Key set (valid value) */
47+
bool live: 1; /**< Group Transient Key live (as indicated by authenticator) */
48+
bool hash: 1; /**< Group Transient Key matches to hash */
4949
} gtk_key_t;
5050

5151
typedef struct {
@@ -60,7 +60,7 @@ typedef struct {
6060
int8_t gtk_set_index; /**< Group Transient Key to insert */
6161
sec_prot_gtk_keys_t *gtks; /**< Group Transient Keys */
6262
const sec_prot_certs_t *certs; /**< Certificates */
63-
bool pmk_set; /**< Pairwise Master Key set */
63+
bool pmk_set: 1; /**< Pairwise Master Key set */
6464
} sec_prot_keys_t;
6565

6666
/**

source/Security/protocols/tls_sec_prot/tls_sec_prot.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -528,8 +528,8 @@ static int8_t tls_sec_prot_tls_configure_and_connect(sec_prot_t *prot, bool is_s
528528
}
529529

530530
tls_sec_prot_lib_set_cb_register((tls_security_t *)&data->tls_sec_inst, prot,
531-
tls_sec_prot_tls_send, tls_sec_prot_tls_receive, tls_sec_prot_tls_export_keys,
532-
tls_sec_prot_tls_set_timer, tls_sec_prot_tls_get_timer);
531+
tls_sec_prot_tls_send, tls_sec_prot_tls_receive, tls_sec_prot_tls_export_keys,
532+
tls_sec_prot_tls_set_timer, tls_sec_prot_tls_get_timer);
533533

534534
if (tls_sec_prot_lib_connect((tls_security_t *)&data->tls_sec_inst, is_server, prot->sec_keys->certs) < 0) {
535535
return -1;

source/Security/protocols/tls_sec_prot/tls_sec_prot_lib.c

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "ns_list.h"
2323
#include "ns_trace.h"
2424
#include "nsdynmemLIB.h"
25+
#include "common_functions.h"
2526
#include "Security/protocols/sec_prot_certs.h"
2627
#include "Security/protocols/tls_sec_prot/tls_sec_prot_lib.h"
2728
#include "mbedtls/sha256.h"
@@ -74,7 +75,7 @@ static int tls_sec_lib_entropy_poll(void *data, unsigned char *output, size_t le
7475
static int tls_sec_prot_lib_ssl_send(void *ctx, const unsigned char *buf, size_t len);
7576
static int tls_sec_prot_lib_ssl_recv(void *ctx, unsigned char *buf, size_t len);
7677
static int tls_sec_prot_lib_ssl_export_keys(void *ctx, const unsigned char *ms,
77-
const unsigned char *kb, size_t maclen, size_t keylen, size_t ivlen);
78+
const unsigned char *kb, size_t maclen, size_t keylen, size_t ivlen);
7879
static void tls_sec_prot_lib_random_extract(tls_security_t *sec, const uint8_t *buf, uint16_t len);
7980
#ifdef TLS_SEC_PROT_LIB_TLS_DEBUG
8081
static void tls_sec_prot_lib_debug(void *ctx, int level, const char *file, int line, const char *string);
@@ -95,12 +96,12 @@ int8_t tls_sec_prot_lib_init(tls_security_t *sec)
9596
mbedtls_pk_init(&sec->pkey);
9697

9798
if (mbedtls_entropy_add_source(&sec->entropy, tls_sec_lib_entropy_poll, NULL,
98-
128, MBEDTLS_ENTROPY_SOURCE_WEAK) < 0) {
99+
128, MBEDTLS_ENTROPY_SOURCE_WEAK) < 0) {
99100
return -1;
100101
}
101102

102103
if ((mbedtls_ctr_drbg_seed(&sec->ctr_drbg, mbedtls_entropy_func, &sec->entropy,
103-
(const unsigned char *) pers, strlen(pers))) != 0) {
104+
(const unsigned char *) pers, strlen(pers))) != 0) {
104105
return -1;
105106
}
106107

@@ -113,9 +114,9 @@ uint16_t tls_sec_prot_lib_size(void)
113114
}
114115

115116
void tls_sec_prot_lib_set_cb_register(tls_security_t *sec, void *handle,
116-
tls_sec_prot_lib_send *send, tls_sec_prot_lib_receive *receive,
117-
tls_sec_prot_lib_export_keys *export_keys, tls_sec_prot_lib_set_timer *set_timer,
118-
tls_sec_prot_lib_get_timer *get_timer)
117+
tls_sec_prot_lib_send *send, tls_sec_prot_lib_receive *receive,
118+
tls_sec_prot_lib_export_keys *export_keys, tls_sec_prot_lib_set_timer *set_timer,
119+
tls_sec_prot_lib_get_timer *get_timer)
119120
{
120121
if (!sec) {
121122
return;
@@ -159,7 +160,7 @@ static int tls_sec_prot_lib_configure_certificates(tls_security_t *sec, const se
159160
break;
160161
}
161162
if (mbedtls_x509_crt_parse(&sec->owncert, cert, cert_len) < 0) {
162-
return -1;
163+
return -1;
163164
}
164165
index++;
165166
}
@@ -238,8 +239,10 @@ int8_t tls_sec_prot_lib_connect(tls_security_t *sec, bool is_server, const sec_p
238239
// Configure random number generator
239240
mbedtls_ssl_conf_rng(&sec->conf, mbedtls_ctr_drbg_random, &sec->ctr_drbg);
240241

242+
#ifdef MBEDTLS_ECP_RESTARTABLE
241243
// Set ECC calculation maximum operations (affects only client)
242244
mbedtls_ecp_set_max_ops(ECC_CALCULATION_MAX_OPS);
245+
#endif
243246

244247
if ((mbedtls_ssl_setup(&sec->ssl, &sec->conf)) != 0) {
245248
return -1;
@@ -392,7 +395,8 @@ static void tls_sec_prot_lib_random_extract(tls_security_t *sec, const uint8_t *
392395
}
393396

394397
static int tls_sec_prot_lib_ssl_export_keys(void *ctx, const unsigned char *ms,
395-
const unsigned char *kb, size_t maclen, size_t keylen, size_t ivlen)
398+
const unsigned char *kb, size_t maclen,
399+
size_t keylen, size_t ivlen)
396400
{
397401
(void) kb;
398402
(void) maclen;
@@ -407,7 +411,7 @@ static int tls_sec_prot_lib_ssl_export_keys(void *ctx, const unsigned char *ms,
407411
memcpy(&random[32], sec->server_random, 32);
408412

409413
sec->ssl.handshake->tls_prf(ms, 48, "client EAP encryption",
410-
random, 64, eap_tls_key_material, 128);
414+
random, 64, eap_tls_key_material, 128);
411415

412416
sec->export_keys(sec->handle, ms, eap_tls_key_material);
413417
return 0;

source/Security/protocols/tls_sec_prot/tls_sec_prot_lib.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,9 @@ typedef void tls_sec_prot_lib_export_keys(void *handle, const uint8_t *master_se
129129
*
130130
*/
131131
void tls_sec_prot_lib_set_cb_register(tls_security_t *sec, void *handle,
132-
tls_sec_prot_lib_send *send, tls_sec_prot_lib_receive *receive,
133-
tls_sec_prot_lib_export_keys *export_keys, tls_sec_prot_lib_set_timer *set_timer,
134-
tls_sec_prot_lib_get_timer *get_timer);
132+
tls_sec_prot_lib_send *send, tls_sec_prot_lib_receive *receive,
133+
tls_sec_prot_lib_export_keys *export_keys, tls_sec_prot_lib_set_timer *set_timer,
134+
tls_sec_prot_lib_get_timer *get_timer);
135135

136136
/**
137137
* tls_sec_prot_lib_free free security library internal data (e.g. TLS data)

0 commit comments

Comments
 (0)